2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2022-07-10 09:47:41 +08:00
|
|
|
https://axis-project.github.io/
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "UITextFieldTest.h"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
|
|
|
using namespace axis::ui;
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
UITextFieldTests::UITextFieldTests()
|
|
|
|
{
|
|
|
|
ADD_TEST_CASE(UITextFieldTest);
|
|
|
|
ADD_TEST_CASE(UITextFieldTest_MaxLength);
|
|
|
|
ADD_TEST_CASE(UITextFieldTest_Password);
|
|
|
|
ADD_TEST_CASE(UITextFieldTest_LineWrap);
|
|
|
|
ADD_TEST_CASE(UITextFieldTest_TrueTypeFont);
|
|
|
|
ADD_TEST_CASE(UITextFieldTest_BMFont);
|
|
|
|
ADD_TEST_CASE(UITextFieldTest_PlaceHolderColor);
|
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest::UITextFieldTest() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest::~UITextFieldTest() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UITextFieldTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("input words here", "Arial", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(
|
|
|
|
0.225f,
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_MaxLength
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_MaxLength::UITextFieldTest_MaxLength() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_MaxLength::~UITextFieldTest_MaxLength() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UITextFieldTest_MaxLength::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
screenSize.width / 2.0f, screenSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField max length", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("input words here", "Arial", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
textField->setMaxLengthEnabled(true);
|
|
|
|
textField->setMaxLength(3);
|
|
|
|
textField->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest_MaxLength::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest_MaxLength::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(
|
|
|
|
0.225f,
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME max length %d", textField->getMaxLength()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME max length %d", textField->getMaxLength()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words max length %d", textField->getMaxLength()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word max length %d", textField->getMaxLength()));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_Password
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_Password::UITextFieldTest_Password() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_Password::~UITextFieldTest_Password() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UITextFieldTest_Password::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
screenSize.width / 2.0f, screenSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField password", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("input password here", "Arial", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
textField->setPasswordEnabled(true);
|
|
|
|
textField->setPasswordStyleText("*");
|
|
|
|
textField->setPosition(Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest_Password::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest_Password::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(
|
|
|
|
0.225f,
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME password"));
|
2021-12-28 21:27:32 +08:00
|
|
|
}
|
2021-12-31 12:12:40 +08:00
|
|
|
break;
|
2021-12-28 16:06:23 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME password"));
|
|
|
|
}
|
|
|
|
break;
|
2021-12-31 11:00:35 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words password"));
|
|
|
|
break;
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word password"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-12-31 11:00:35 +08:00
|
|
|
}
|
2021-12-28 21:27:32 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
// UITextFieldTest_LineWrap
|
|
|
|
UITextFieldTest_LineWrap::UITextFieldTest_LineWrap() : _displayValueLabel(nullptr) {}
|
|
|
|
|
|
|
|
UITextFieldTest_LineWrap::~UITextFieldTest_LineWrap() {}
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
bool UITextFieldTest_LineWrap::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("No Event", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField line wrap", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2021-12-31 12:12:40 +08:00
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("input words here", "fonts/Marker Felt.ttf", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
textField->ignoreContentAdaptWithSize(false);
|
|
|
|
((Label*)(textField->getVirtualRenderer()))->setLineBreakWithoutSpace(true);
|
|
|
|
textField->setContentSize(Size(240.0f, 170.0f));
|
|
|
|
textField->setString("input words here");
|
|
|
|
textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
|
|
|
|
textField->setTextVerticalAlignment(TextVAlignment::CENTER);
|
|
|
|
textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest_LineWrap::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest_LineWrap::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
|
|
|
textField->runAction(MoveTo::create(0.225f, Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + 30)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
|
|
|
|
textField->setTextVerticalAlignment(TextVAlignment::CENTER);
|
|
|
|
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_TrueTypeFont
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_TrueTypeFont::UITextFieldTest_TrueTypeFont() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_TrueTypeFont::~UITextFieldTest_TrueTypeFont() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UITextFieldTest_TrueTypeFont::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("True Type Font Test - No Event", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("input words here", "fonts/A Damn Mess.ttf", 30);
|
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest_TrueTypeFont::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest_TrueTypeFont::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(
|
|
|
|
0.225f,
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_BMFont
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_BMFont::UITextFieldTest_BMFont() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_BMFont::~UITextFieldTest_BMFont() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UITextFieldTest_BMFont::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel = Text::create("BMFont Test - No Event", "fonts/Marker Felt.ttf", 32);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("BMFont Text", "fonts/bitmapFontTest3.fnt", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
textField->setCursorEnabled(true);
|
|
|
|
textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest_BMFont::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest_BMFont::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(
|
|
|
|
0.225f,
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_PlaceHolderColor
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_PlaceHolderColor::UITextFieldTest_PlaceHolderColor() : _displayValueLabel(nullptr) {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
UITextFieldTest_PlaceHolderColor::~UITextFieldTest_PlaceHolderColor() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
bool UITextFieldTest_PlaceHolderColor::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getContentSize();
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add a label in which the textfield events will be displayed
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel =
|
|
|
|
Text::create("You should see 16.50000, 34.0000 in the output window the first time you type",
|
|
|
|
"fonts/Marker Felt.ttf", 12);
|
2019-11-23 20:27:39 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vec2(0.5f, -1.0f));
|
2021-12-31 12:12:40 +08:00
|
|
|
_displayValueLabel->setPosition(Vec2(
|
|
|
|
widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getContentSize().height * 1.5f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Add the alert
|
2021-12-31 12:12:40 +08:00
|
|
|
Text* alert = Text::create("TextField", "fonts/Marker Felt.ttf", 30);
|
|
|
|
alert->setPosition(
|
|
|
|
Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getContentSize().height * 3.075f));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2021-12-31 12:12:40 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
// Create the textfield
|
2021-12-31 12:12:40 +08:00
|
|
|
TextField* textField = TextField::create("input words here", "Arial", 30);
|
2019-11-23 20:27:39 +08:00
|
|
|
textField->setPlaceHolder("input text here");
|
|
|
|
textField->setPlaceHolderColor(Color4B::GREEN);
|
|
|
|
textField->setTextColor(Color4B::RED);
|
|
|
|
textField->setPosition(Vec2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2022-07-16 10:43:05 +08:00
|
|
|
textField->addEventListener(AX_CALLBACK_2(UITextFieldTest_PlaceHolderColor::textFieldEvent, this));
|
2019-11-23 20:27:39 +08:00
|
|
|
_uiLayer->addChild(textField);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void UITextFieldTest_PlaceHolderColor::textFieldEvent(Ref* pSender, TextField::EventType type)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
case TextField::EventType::ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(
|
|
|
|
0.225f,
|
|
|
|
Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("attach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size screenSize = Director::getInstance()->getWinSize();
|
|
|
|
textField->runAction(MoveTo::create(0.175f, Vec2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
|
|
|
_displayValueLabel->setString(StringUtils::format("detach with IME"));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::INSERT_TEXT:
|
|
|
|
{
|
|
|
|
_displayValueLabel->setString(StringUtils::format("insert words"));
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("%f, %f", dynamic_cast<TextField*>(pSender)->getContentSize().width,
|
2021-12-31 12:12:40 +08:00
|
|
|
dynamic_cast<TextField*>(pSender)->getContentSize().height);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TextField::EventType::DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setString(StringUtils::format("delete word"));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
}
|