2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
#include "UITextFieldTest.h"
|
|
|
|
|
|
|
|
// UITextFieldTest
|
|
|
|
UITextFieldTest::UITextFieldTest()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UITextFieldTest::~UITextFieldTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UITextFieldTest::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
Size widgetSize = _widget->getSize();
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add a label in which the textfield events will be displayed
|
2014-04-03 15:52:42 +08:00
|
|
|
_displayValueLabel = Text::create("No Event","fonts/Marker Felt.ttf",32);
|
2014-04-15 18:23:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vector2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getSize().height * 1.5f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 15:52:42 +08:00
|
|
|
Text* alert = Text::create("TextField","fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-04-15 18:23:40 +08:00
|
|
|
alert->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 3.075f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the textfield
|
2014-04-02 18:35:08 +08:00
|
|
|
TextField* textField = TextField::create("input words here","fonts/Marker Felt.ttf",30);
|
|
|
|
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2013-11-08 18:43:06 +08:00
|
|
|
textField->addEventListenerTextField(this, textfieldeventselector(UITextFieldTest::textFieldEvent));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
2014-03-04 16:51:35 +08:00
|
|
|
|
2013-09-16 20:54:13 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void UITextFieldTest::textFieldEvent(Ref *pSender, TextFiledEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
2013-09-17 20:13:23 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TEXTFIELD_EVENT_ATTACH_WITH_IME:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
2013-09-17 20:13:23 +08:00
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
2013-09-23 04:50:54 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.225f,
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("attach with IME")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DETACH_WITH_IME:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
2013-09-17 20:13:23 +08:00
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.175f, Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("detach with IME")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
case TEXTFIELD_EVENT_INSERT_TEXT:
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("insert words")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DELETE_BACKWARD:
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("delete word")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_MaxLength
|
|
|
|
UITextFieldTest_MaxLength::UITextFieldTest_MaxLength()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UITextFieldTest_MaxLength::~UITextFieldTest_MaxLength()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UITextFieldTest_MaxLength::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
// Add a label in which the textfield events will be displayed
|
2014-04-03 15:52:42 +08:00
|
|
|
_displayValueLabel = Text::create("No Event","fonts/Marker Felt.ttf",32);
|
2014-04-15 18:23:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vector2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f + _displayValueLabel->getSize().height * 1.5f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 15:52:42 +08:00
|
|
|
Text *alert = Text::create("TextField max length","fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-04-15 18:23:40 +08:00
|
|
|
alert->setPosition(Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f - alert->getSize().height * 3.075f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the textfield
|
2014-04-03 15:52:42 +08:00
|
|
|
TextField* textField = TextField::create("input words here","fonts/Marker Felt.ttf",30);
|
2013-11-06 16:04:06 +08:00
|
|
|
textField->setMaxLengthEnabled(true);
|
2013-09-16 20:54:13 +08:00
|
|
|
textField->setMaxLength(3);
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->setPosition(Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f));
|
2013-11-08 18:43:06 +08:00
|
|
|
textField->addEventListenerTextField(this, textfieldeventselector(UITextFieldTest_MaxLength::textFieldEvent));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void UITextFieldTest_MaxLength::textFieldEvent(Ref *pSender, TextFiledEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
2013-09-17 20:13:23 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TEXTFIELD_EVENT_ATTACH_WITH_IME:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
2013-09-17 20:13:23 +08:00
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
2013-09-23 04:50:54 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.225f,
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("attach with IME max length %d", textField->getMaxLength())->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DETACH_WITH_IME:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
2013-09-17 20:13:23 +08:00
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.175f, Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("detach with IME max length %d", textField->getMaxLength())->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
case TEXTFIELD_EVENT_INSERT_TEXT:
|
2013-09-17 20:13:23 +08:00
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
_displayValueLabel->setText(String::createWithFormat("insert words max length %d", textField->getMaxLength())->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DELETE_BACKWARD:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
_displayValueLabel->setText(String::createWithFormat("delete word max length %d", textField->getMaxLength())->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// UITextFieldTest_Password
|
|
|
|
UITextFieldTest_Password::UITextFieldTest_Password()
|
2013-12-23 15:35:35 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UITextFieldTest_Password::~UITextFieldTest_Password()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UITextFieldTest_Password::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
|
|
|
|
|
|
|
// Add a label in which the textfield events will be displayed
|
2014-04-03 15:52:42 +08:00
|
|
|
_displayValueLabel = Text::create("No Event","fonts/Marker Felt.ttf",32);
|
2014-04-15 18:23:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vector2(0.5f, -1.0f));
|
|
|
|
_displayValueLabel->setPosition(Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f + _displayValueLabel->getSize().height * 1.5f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 15:52:42 +08:00
|
|
|
Text *alert = Text::create("TextField password","fonts/Marker Felt.ttf",30);
|
2013-09-16 20:54:13 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-04-15 18:23:40 +08:00
|
|
|
alert->setPosition(Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f - alert->getSize().height * 3.075f));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
// Create the textfield
|
2014-04-03 15:52:42 +08:00
|
|
|
TextField* textField = TextField::create("input password here","fonts/Marker Felt.ttf",30);
|
2013-11-06 16:04:06 +08:00
|
|
|
textField->setPasswordEnabled(true);
|
2013-09-16 20:54:13 +08:00
|
|
|
textField->setPasswordStyleText("*");
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->setPosition(Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f));
|
2013-11-08 18:43:06 +08:00
|
|
|
textField->addEventListenerTextField(this, textfieldeventselector(UITextFieldTest_Password::textFieldEvent));
|
2013-12-23 15:35:35 +08:00
|
|
|
_uiLayer->addChild(textField);
|
2013-09-16 20:54:13 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-02-20 10:53:49 +08:00
|
|
|
void UITextFieldTest_Password::textFieldEvent(Ref *pSender, TextFiledEventType type)
|
2013-09-16 20:54:13 +08:00
|
|
|
{
|
2013-09-17 20:13:23 +08:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TEXTFIELD_EVENT_ATTACH_WITH_IME:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
2013-09-17 20:13:23 +08:00
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
2013-09-23 04:50:54 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.225f,
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2.0f)));
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("attach with IME password")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DETACH_WITH_IME:
|
|
|
|
{
|
2013-12-23 15:35:35 +08:00
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
2013-09-17 20:13:23 +08:00
|
|
|
Size screenSize = CCDirector::getInstance()->getWinSize();
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.175f, Vector2(screenSize.width / 2.0f, screenSize.height / 2.0f)));
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("detach with IME password")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2013-11-06 16:04:06 +08:00
|
|
|
case TEXTFIELD_EVENT_INSERT_TEXT:
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("insert words password")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DELETE_BACKWARD:
|
2013-12-23 15:35:35 +08:00
|
|
|
_displayValueLabel->setText(String::createWithFormat("delete word password")->getCString());
|
2013-09-17 20:13:23 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2013-09-16 20:54:13 +08:00
|
|
|
}
|
2014-03-04 16:51:35 +08:00
|
|
|
|
|
|
|
|
|
|
|
// UITextFieldTest_LineWrap
|
|
|
|
UITextFieldTest_LineWrap::UITextFieldTest_LineWrap()
|
2014-04-09 22:53:59 +08:00
|
|
|
: _displayValueLabel(nullptr)
|
2014-03-04 16:51:35 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
UITextFieldTest_LineWrap::~UITextFieldTest_LineWrap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool UITextFieldTest_LineWrap::init()
|
|
|
|
{
|
|
|
|
if (UIScene::init())
|
|
|
|
{
|
|
|
|
Size widgetSize = _widget->getSize();
|
|
|
|
|
|
|
|
// Add a label in which the textfield events will be displayed
|
2014-04-03 15:52:42 +08:00
|
|
|
_displayValueLabel = Text::create("No Event","fonts/Marker Felt.ttf",30);
|
2014-04-15 18:23:40 +08:00
|
|
|
_displayValueLabel->setAnchorPoint(Vector2(0.5f, -1));
|
|
|
|
_displayValueLabel->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + _displayValueLabel->getSize().height * 1.5));
|
2014-03-04 16:51:35 +08:00
|
|
|
_uiLayer->addChild(_displayValueLabel);
|
|
|
|
|
|
|
|
// Add the alert
|
2014-04-03 15:52:42 +08:00
|
|
|
Text *alert = Text::create("TextField line wrap","fonts/Marker Felt.ttf",30);
|
2014-03-04 16:51:35 +08:00
|
|
|
alert->setColor(Color3B(159, 168, 176));
|
2014-04-15 18:23:40 +08:00
|
|
|
alert->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f - alert->getSize().height * 3.075));
|
2014-03-04 16:51:35 +08:00
|
|
|
_uiLayer->addChild(alert);
|
|
|
|
|
|
|
|
// Create the textfield
|
2014-04-03 15:52:42 +08:00
|
|
|
TextField* textField = TextField::create("input words here","fonts/Marker Felt.ttf",30);
|
2014-03-06 21:58:45 +08:00
|
|
|
textField->ignoreContentAdaptWithSize(false);
|
|
|
|
textField->setSize(Size(240, 160));
|
2014-03-04 16:51:35 +08:00
|
|
|
textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
|
|
|
|
textField->setTextVerticalAlignment(TextVAlignment::CENTER);
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->setPosition(Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f));
|
2014-03-04 16:51:35 +08:00
|
|
|
textField->addEventListenerTextField(this, textfieldeventselector(UITextFieldTest_LineWrap::textFieldEvent));
|
|
|
|
_uiLayer->addChild(textField);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UITextFieldTest_LineWrap::textFieldEvent(Ref *pSender, TextFiledEventType type)
|
|
|
|
{
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case TEXTFIELD_EVENT_ATTACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size widgetSize = _widget->getSize();
|
2014-03-24 10:12:40 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.225f,
|
2014-04-15 18:23:40 +08:00
|
|
|
Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f + textField->getContentSize().height / 2)));
|
2014-03-04 16:51:35 +08:00
|
|
|
textField->setTextHorizontalAlignment(TextHAlignment::LEFT);
|
|
|
|
textField->setTextVerticalAlignment(TextVAlignment::TOP);
|
|
|
|
|
|
|
|
_displayValueLabel->setText(CCString::createWithFormat("attach with IME")->getCString());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DETACH_WITH_IME:
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(pSender);
|
|
|
|
Size widgetSize = _widget->getSize();
|
2014-04-15 18:23:40 +08:00
|
|
|
textField->runAction(CCMoveTo::create(0.175f, Vector2(widgetSize.width / 2.0f, widgetSize.height / 2.0f)));
|
2014-03-04 16:51:35 +08:00
|
|
|
textField->setTextHorizontalAlignment(TextHAlignment::CENTER);
|
|
|
|
textField->setTextVerticalAlignment(TextVAlignment::CENTER);
|
|
|
|
|
|
|
|
_displayValueLabel->setText(CCString::createWithFormat("detach with IME")->getCString());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_INSERT_TEXT:
|
|
|
|
_displayValueLabel->setText(CCString::createWithFormat("insert words")->getCString());
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TEXTFIELD_EVENT_DELETE_BACKWARD:
|
|
|
|
_displayValueLabel->setText(CCString::createWithFormat("delete word")->getCString());
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|