2011-05-04 14:13:15 +08:00
|
|
|
// #define COCOS2D_DEBUG 1
|
2011-04-27 17:01:27 +08:00
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
#include "TextInputTest.h"
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// local function
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
2011-05-04 14:13:15 +08:00
|
|
|
kTextFieldTTFDefaultTest = 0,
|
|
|
|
kTextFieldTTFActionTest,
|
2011-04-26 18:04:07 +08:00
|
|
|
kTextInputTestsCount,
|
|
|
|
};
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
#define FONT_NAME "Thonburi"
|
2011-05-11 22:15:51 +08:00
|
|
|
#define FONT_SIZE 36
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
static int testIdx = -1;
|
|
|
|
|
2011-04-28 14:19:12 +08:00
|
|
|
KeyboardNotificationLayer* createTextInputTest(int nIndex)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
switch(nIndex)
|
|
|
|
{
|
2011-05-04 14:13:15 +08:00
|
|
|
case kTextFieldTTFDefaultTest: return new TextFieldTTFDefaultTest();
|
|
|
|
case kTextFieldTTFActionTest: return new TextFieldTTFActionTest();
|
|
|
|
default: return 0;
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* restartTextInputTest()
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2011-04-28 14:19:12 +08:00
|
|
|
TextInputTest* pContainerLayer = new TextInputTest;
|
2011-04-26 18:04:07 +08:00
|
|
|
pContainerLayer->autorelease();
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pTestLayer = createTextInputTest(testIdx);
|
2011-04-26 18:04:07 +08:00
|
|
|
pTestLayer->autorelease();
|
2011-04-28 14:19:12 +08:00
|
|
|
pContainerLayer->addKeyboardNotificationLayer(pTestLayer);
|
2011-04-26 18:04:07 +08:00
|
|
|
|
|
|
|
return pContainerLayer;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* nextTextInputTest()
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
testIdx++;
|
|
|
|
testIdx = testIdx % kTextInputTestsCount;
|
|
|
|
|
|
|
|
return restartTextInputTest();
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
Layer* backTextInputTest()
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
testIdx--;
|
|
|
|
int total = kTextInputTestsCount;
|
|
|
|
if( testIdx < 0 )
|
2012-04-19 14:35:52 +08:00
|
|
|
testIdx += total;
|
2011-04-26 18:04:07 +08:00
|
|
|
|
|
|
|
return restartTextInputTest();
|
|
|
|
}
|
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
static Rect getRect(Node * node)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-06-20 14:17:10 +08:00
|
|
|
Rect rc;
|
2013-07-23 08:25:44 +08:00
|
|
|
rc.origin = node->getPosition();
|
|
|
|
rc.size = node->getContentSize();
|
2011-04-26 18:04:07 +08:00
|
|
|
rc.origin.x -= rc.size.width / 2;
|
|
|
|
rc.origin.y -= rc.size.height / 2;
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextInputTest
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-04-28 14:19:12 +08:00
|
|
|
TextInputTest::TextInputTest()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _notificationLayer(0)
|
2011-04-28 14:19:12 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void TextInputTest::restartCallback(Object* sender)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new TextInputTestScene();
|
2011-04-26 18:04:07 +08:00
|
|
|
s->addChild(restartTextInputTest());
|
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2011-04-26 18:04:07 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void TextInputTest::nextCallback(Object* sender)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new TextInputTestScene();
|
2011-04-26 18:04:07 +08:00
|
|
|
s->addChild( nextTextInputTest() );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2011-04-26 18:04:07 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
void TextInputTest::backCallback(Object* sender)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = new TextInputTestScene();
|
2011-04-26 18:04:07 +08:00
|
|
|
s->addChild( backTextInputTest() );
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(s);
|
2011-04-26 18:04:07 +08:00
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
void TextInputTest::addKeyboardNotificationLayer(KeyboardNotificationLayer * layer)
|
2011-04-28 14:19:12 +08:00
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
_notificationLayer = layer;
|
|
|
|
addChild(layer);
|
2011-04-28 14:19:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextInputTest::title()
|
|
|
|
{
|
|
|
|
return "text input test";
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
void TextInputTest::onEnter()
|
|
|
|
{
|
2013-06-12 07:30:05 +08:00
|
|
|
BaseTest::onEnter();
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement KeyboardNotificationLayer
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
KeyboardNotificationLayer::KeyboardNotificationLayer()
|
2013-06-15 14:03:30 +08:00
|
|
|
: _trackNode(0)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-09-03 18:22:03 +08:00
|
|
|
// Register Touch Event
|
2013-10-23 11:27:24 +08:00
|
|
|
auto listener = EventListenerTouchOneByOne::create();
|
2013-09-03 18:22:03 +08:00
|
|
|
listener->onTouchBegan = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchBegan, this);
|
|
|
|
listener->onTouchEnded = CC_CALLBACK_2(KeyboardNotificationLayer::onTouchEnded, this);
|
2013-10-26 15:04:01 +08:00
|
|
|
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
//void KeyboardNotificationLayer::registerWithTouchDispatcher()
|
|
|
|
//{
|
|
|
|
// auto director = Director::getInstance();
|
|
|
|
// director->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
|
|
|
|
//}
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
void KeyboardNotificationLayer::keyboardWillShow(IMEKeyboardNotificationInfo& info)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2011-04-27 17:01:27 +08:00
|
|
|
CCLOG("TextInputTest:keyboardWillShowAt(origin:%f,%f, size:%f,%f)",
|
|
|
|
info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _trackNode)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto rectTracked = getRect(_trackNode);
|
2011-04-27 17:01:27 +08:00
|
|
|
CCLOG("TextInputTest:trackingNodeAt(origin:%f,%f, size:%f,%f)",
|
|
|
|
rectTracked.origin.x, rectTracked.origin.y, rectTracked.size.width, rectTracked.size.height);
|
|
|
|
|
|
|
|
// if the keyboard area doesn't intersect with the tracking node area, nothing need to do.
|
2012-08-01 15:30:12 +08:00
|
|
|
if (! rectTracked.intersectsRect(info.end))
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-27 17:01:27 +08:00
|
|
|
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
|
2012-08-01 15:30:12 +08:00
|
|
|
float adjustVert = info.end.getMaxY() - rectTracked.getMinY();
|
2011-04-27 17:01:27 +08:00
|
|
|
CCLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert);
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2011-04-27 17:01:27 +08:00
|
|
|
// move all the children node of KeyboardNotificationLayer
|
2013-08-16 16:05:27 +08:00
|
|
|
auto children = getChildren();
|
2013-06-20 14:17:10 +08:00
|
|
|
Node * node = 0;
|
2011-04-26 18:04:07 +08:00
|
|
|
int count = children->count();
|
2013-06-20 14:17:10 +08:00
|
|
|
Point pos;
|
2011-04-26 18:04:07 +08:00
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
2013-08-22 10:45:47 +08:00
|
|
|
node = (Node*)children->getObjectAtIndex(i);
|
2011-04-26 18:04:07 +08:00
|
|
|
pos = node->getPosition();
|
|
|
|
pos.y += adjustVert;
|
|
|
|
node->setPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// Layer function
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
bool KeyboardNotificationLayer::onTouchBegan(Touch *touch, Event *event)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
CCLOG("++++++++++++++++++++++++++++++++++++++++++++");
|
2013-07-26 06:53:24 +08:00
|
|
|
_beginPos = touch->getLocation();
|
2011-05-04 14:13:15 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 18:22:03 +08:00
|
|
|
void KeyboardNotificationLayer::onTouchEnded(Touch *touch, Event *event)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _trackNode)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto endPos = touch->getLocation();
|
2011-05-04 14:13:15 +08:00
|
|
|
|
|
|
|
float delta = 5.0f;
|
2013-06-15 14:03:30 +08:00
|
|
|
if (::abs(endPos.x - _beginPos.x) > delta
|
|
|
|
|| ::abs(endPos.y - _beginPos.y) > delta)
|
2011-05-04 14:13:15 +08:00
|
|
|
{
|
|
|
|
// not click
|
2013-06-15 14:03:30 +08:00
|
|
|
_beginPos.x = _beginPos.y = -1;
|
2011-05-04 14:13:15 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide the trackNode is clicked.
|
2013-06-20 14:17:10 +08:00
|
|
|
Rect rect;
|
2013-08-16 16:05:27 +08:00
|
|
|
auto point = convertTouchToNodeSpaceAR(touch);
|
2011-05-04 14:13:15 +08:00
|
|
|
CCLOG("KeyboardNotificationLayer:clickedAt(%f,%f)", point.x, point.y);
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
rect = getRect(_trackNode);
|
2011-05-04 14:13:15 +08:00
|
|
|
CCLOG("KeyboardNotificationLayer:TrackNode at(origin:%f,%f, size:%f,%f)",
|
|
|
|
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
|
|
|
|
|
2012-08-01 15:30:12 +08:00
|
|
|
this->onClickTrackNode(rect.containsPoint(point));
|
2011-05-04 14:13:15 +08:00
|
|
|
CCLOG("----------------------------------");
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2011-05-04 14:13:15 +08:00
|
|
|
// implement TextFieldTTFDefaultTest
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
std::string TextFieldTTFDefaultTest::subtitle()
|
|
|
|
{
|
|
|
|
return "TextFieldTTF with default behavior test";
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFDefaultTest::onClickTrackNode(bool bClicked)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pTextField = (TextFieldTTF*)_trackNode;
|
2011-05-04 14:13:15 +08:00
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
2013-06-20 14:17:10 +08:00
|
|
|
CCLOG("TextFieldTTFDefaultTest:TextFieldTTF attachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->attachWithIME();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
2013-06-20 14:17:10 +08:00
|
|
|
CCLOG("TextFieldTTFDefaultTest:TextFieldTTF detachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFDefaultTest::onEnter()
|
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// add TextFieldTTF
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pTextField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
|
2011-05-04 14:13:15 +08:00
|
|
|
FONT_NAME,
|
|
|
|
FONT_SIZE);
|
|
|
|
addChild(pTextField);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2013-06-20 14:17:10 +08:00
|
|
|
// on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
2012-04-19 14:35:52 +08:00
|
|
|
// so we had to set a higher position to make it visable
|
2013-07-12 14:11:55 +08:00
|
|
|
pTextField->setPosition(Point(s.width / 2, s.height/2 + 50));
|
2011-05-12 21:32:57 +08:00
|
|
|
#else
|
2013-07-12 14:11:55 +08:00
|
|
|
pTextField->setPosition(Point(s.width / 2, s.height / 2));
|
2011-05-12 21:32:57 +08:00
|
|
|
#endif
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_trackNode = pTextField;
|
2011-05-04 14:13:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextFieldTTFActionTest
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
std::string TextFieldTTFActionTest::subtitle()
|
|
|
|
{
|
|
|
|
return "CCTextFieldTTF with action and char limit test";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTFActionTest::onClickTrackNode(bool bClicked)
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto pTextField = (TextFieldTTF*)_trackNode;
|
2011-05-04 14:13:15 +08:00
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
2013-06-20 14:17:10 +08:00
|
|
|
CCLOG("TextFieldTTFActionTest:TextFieldTTF attachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->attachWithIME();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
2013-06-20 14:17:10 +08:00
|
|
|
CCLOG("TextFieldTTFActionTest:TextFieldTTF detachWithIME");
|
2011-05-04 14:13:15 +08:00
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTFActionTest::onEnter()
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_charLimit = 12;
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
_textFieldAction = RepeatForever::create(
|
|
|
|
Sequence::create(
|
|
|
|
FadeOut::create(0.25),
|
|
|
|
FadeIn::create(0.25),
|
2013-07-24 06:20:22 +08:00
|
|
|
NULL
|
2011-04-29 18:09:54 +08:00
|
|
|
));
|
2013-06-15 14:03:30 +08:00
|
|
|
_textFieldAction->retain();
|
|
|
|
_action = false;
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// add TextFieldTTF
|
2013-08-16 16:05:27 +08:00
|
|
|
auto s = Director::getInstance()->getWinSize();
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
_textField = TextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
|
2011-04-29 16:46:29 +08:00
|
|
|
FONT_NAME,
|
|
|
|
FONT_SIZE);
|
2013-06-15 14:03:30 +08:00
|
|
|
addChild(_textField);
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_textField->setDelegate(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
2013-06-20 14:17:10 +08:00
|
|
|
// on android, TextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
2012-04-19 14:35:52 +08:00
|
|
|
// so we had to set a higher position
|
2013-07-12 18:04:32 +08:00
|
|
|
_textField->setPosition(Point(s.width / 2, s.height/2 + 50));
|
2011-05-12 21:32:57 +08:00
|
|
|
#else
|
2013-07-12 14:11:55 +08:00
|
|
|
_textField->setPosition(Point(s.width / 2, s.height / 2));
|
2011-05-12 21:32:57 +08:00
|
|
|
#endif
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_trackNode = _textField;
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFActionTest::onExit()
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
2011-05-11 18:45:53 +08:00
|
|
|
KeyboardNotificationLayer::onExit();
|
2013-06-15 14:03:30 +08:00
|
|
|
_textFieldAction->release();
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:17:10 +08:00
|
|
|
// TextFieldDelegate protocol
|
2013-07-26 06:53:24 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldAttachWithIME(TextFieldTTF * sender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (! _action)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textField->runAction(_textFieldAction);
|
|
|
|
_action = true;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldDetachWithIME(TextFieldTTF * sender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_action)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_textField->stopAction(_textFieldAction);
|
|
|
|
_textField->setOpacity(255);
|
|
|
|
_action = false;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldInsertText(TextFieldTTF * sender, const char * text, int nLen)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2011-05-11 18:22:55 +08:00
|
|
|
// if insert enter, treat as default to detach with ime
|
|
|
|
if ('\n' == *text)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
// if the textfield's char count more than _charLimit, doesn't insert text anymore.
|
2013-07-26 06:53:24 +08:00
|
|
|
if (sender->getCharCount() >= _charLimit)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
|
|
|
return true;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a insert text sprite and do some action
|
2013-08-16 16:05:27 +08:00
|
|
|
auto label = LabelTTF::create(text, FONT_NAME, FONT_SIZE);
|
2011-04-29 18:09:54 +08:00
|
|
|
this->addChild(label);
|
2013-07-07 21:08:14 +08:00
|
|
|
Color3B color(226, 121, 7);
|
2011-04-29 16:46:29 +08:00
|
|
|
label->setColor(color);
|
2011-04-29 18:09:54 +08:00
|
|
|
|
|
|
|
// move the sprite from top to position
|
2013-08-16 16:05:27 +08:00
|
|
|
auto endPos = sender->getPosition();
|
2013-07-26 06:53:24 +08:00
|
|
|
if (sender->getCharCount())
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-07-26 06:53:24 +08:00
|
|
|
endPos.x += sender->getContentSize().width / 2;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
2013-08-16 16:05:27 +08:00
|
|
|
auto inputTextSize = label->getContentSize();
|
2013-07-12 06:24:23 +08:00
|
|
|
Point beginPos(endPos.x, Director::getInstance()->getWinSize().height - inputTextSize.height * 2);
|
2011-04-29 18:09:54 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
float duration = 0.5;
|
2011-04-29 18:09:54 +08:00
|
|
|
label->setPosition(beginPos);
|
|
|
|
label->setScale(8);
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto seq = Sequence::create(
|
2013-06-20 14:17:10 +08:00
|
|
|
Spawn::create(
|
|
|
|
MoveTo::create(duration, endPos),
|
|
|
|
ScaleTo::create(duration, 1),
|
|
|
|
FadeOut::create(duration),
|
2013-07-24 06:20:22 +08:00
|
|
|
NULL),
|
2013-07-16 03:43:22 +08:00
|
|
|
CallFuncN::create(CC_CALLBACK_1(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction, this)),
|
2013-07-24 06:20:22 +08:00
|
|
|
NULL);
|
2011-04-29 18:09:54 +08:00
|
|
|
label->runAction(seq);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldDeleteBackward(TextFieldTTF * sender, const char * delText, int nLen)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
|
|
|
// create a delete text sprite and do some action
|
2013-08-16 16:05:27 +08:00
|
|
|
auto label = LabelTTF::create(delText, FONT_NAME, FONT_SIZE);
|
2011-04-29 18:09:54 +08:00
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
// move the sprite to fly out
|
2013-08-16 16:05:27 +08:00
|
|
|
auto beginPos = sender->getPosition();
|
|
|
|
auto textfieldSize = sender->getContentSize();
|
|
|
|
auto labelSize = label->getContentSize();
|
2011-04-29 18:09:54 +08:00
|
|
|
beginPos.x += (textfieldSize.width - labelSize.width) / 2.0f;
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto winSize = Director::getInstance()->getWinSize();
|
2013-06-20 14:17:10 +08:00
|
|
|
Point endPos(- winSize.width / 4.0f, winSize.height * (0.5 + (float)rand() / (2.0f * RAND_MAX)));
|
2011-04-29 18:09:54 +08:00
|
|
|
|
2012-06-08 13:55:28 +08:00
|
|
|
float duration = 1;
|
|
|
|
float rotateDuration = 0.2f;
|
2011-04-29 18:09:54 +08:00
|
|
|
int repeatTime = 5;
|
|
|
|
label->setPosition(beginPos);
|
|
|
|
|
2013-08-16 16:05:27 +08:00
|
|
|
auto seq = Sequence::create(
|
2013-06-20 14:17:10 +08:00
|
|
|
Spawn::create(
|
|
|
|
MoveTo::create(duration, endPos),
|
|
|
|
Repeat::create(
|
|
|
|
RotateBy::create(rotateDuration, (rand()%2) ? 360 : -360),
|
2011-04-29 18:09:54 +08:00
|
|
|
repeatTime),
|
2013-06-20 14:17:10 +08:00
|
|
|
FadeOut::create(duration),
|
2013-07-24 06:20:22 +08:00
|
|
|
NULL),
|
2013-07-16 03:43:22 +08:00
|
|
|
CallFuncN::create(CC_CALLBACK_1(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction, this)),
|
2013-07-24 06:20:22 +08:00
|
|
|
NULL);
|
2011-04-29 18:09:54 +08:00
|
|
|
label->runAction(seq);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-07-26 06:53:24 +08:00
|
|
|
bool TextFieldTTFActionTest::onDraw(TextFieldTTF * sender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2013-07-23 08:25:44 +08:00
|
|
|
void TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction(Node * node)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
2013-07-23 08:25:44 +08:00
|
|
|
this->removeChild(node, true);
|
2011-04-29 16:46:29 +08:00
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextInputTestScene
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TextInputTestScene::runThisTest()
|
|
|
|
{
|
2013-08-16 16:05:27 +08:00
|
|
|
auto layer = nextTextInputTest();
|
2013-07-23 08:25:44 +08:00
|
|
|
addChild(layer);
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2013-07-12 06:24:23 +08:00
|
|
|
Director::getInstance()->replaceScene(this);
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|