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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayer* restartTextInputTest()
|
|
|
|
{
|
2011-04-28 14:19:12 +08:00
|
|
|
TextInputTest* pContainerLayer = new TextInputTest;
|
2011-04-26 18:04:07 +08:00
|
|
|
pContainerLayer->autorelease();
|
|
|
|
|
2011-04-28 14:19:12 +08:00
|
|
|
KeyboardNotificationLayer* 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayer* nextTextInputTest()
|
|
|
|
{
|
|
|
|
testIdx++;
|
|
|
|
testIdx = testIdx % kTextInputTestsCount;
|
|
|
|
|
|
|
|
return restartTextInputTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
CCLayer* backTextInputTest()
|
|
|
|
{
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2011-04-29 16:46:29 +08:00
|
|
|
static CCRect getRect(CCNode * pNode)
|
2011-04-26 18:04:07 +08:00
|
|
|
{
|
|
|
|
CCRect rc;
|
|
|
|
rc.origin = pNode->getPosition();
|
|
|
|
rc.size = pNode->getContentSize();
|
|
|
|
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()
|
|
|
|
: m_pNotificationLayer(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
void TextInputTest::restartCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCScene* s = new TextInputTestScene();
|
|
|
|
s->addChild(restartTextInputTest());
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextInputTest::nextCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCScene* s = new TextInputTestScene();
|
|
|
|
s->addChild( nextTextInputTest() );
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextInputTest::backCallback(CCObject* pSender)
|
|
|
|
{
|
|
|
|
CCScene* s = new TextInputTestScene();
|
|
|
|
s->addChild( backTextInputTest() );
|
|
|
|
CCDirector::sharedDirector()->replaceScene(s);
|
|
|
|
s->release();
|
|
|
|
}
|
|
|
|
|
2011-04-28 14:19:12 +08:00
|
|
|
void TextInputTest::addKeyboardNotificationLayer(KeyboardNotificationLayer * pLayer)
|
|
|
|
{
|
|
|
|
m_pNotificationLayer = pLayer;
|
|
|
|
addChild(pLayer);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextInputTest::title()
|
|
|
|
{
|
|
|
|
return "text input test";
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
void TextInputTest::onEnter()
|
|
|
|
{
|
|
|
|
CCLayer::onEnter();
|
|
|
|
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF* label = CCLabelTTF::create(title().c_str(), "Arial", 24);
|
2011-04-26 18:04:07 +08:00
|
|
|
addChild(label);
|
2011-05-12 14:45:44 +08:00
|
|
|
label->setPosition(ccp(s.width/2, s.height-50));
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2011-04-28 14:19:12 +08:00
|
|
|
std::string subTitle = m_pNotificationLayer->subtitle();
|
2011-04-26 18:04:07 +08:00
|
|
|
if(! subTitle.empty())
|
|
|
|
{
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF* l = CCLabelTTF::create(subTitle.c_str(), "Thonburi", 16);
|
2011-04-26 18:04:07 +08:00
|
|
|
addChild(l, 1);
|
2011-05-12 14:45:44 +08:00
|
|
|
l->setPosition(ccp(s.width/2, s.height-80));
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
2011-05-04 14:13:15 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenuItemImage *item1 = CCMenuItemImage::create("Images/b1.png", "Images/b2.png", this, menu_selector(TextInputTest::backCallback));
|
|
|
|
CCMenuItemImage *item2 = CCMenuItemImage::create("Images/r1.png","Images/r2.png", this, menu_selector(TextInputTest::restartCallback) );
|
|
|
|
CCMenuItemImage *item3 = CCMenuItemImage::create("Images/f1.png", "Images/f2.png", this, menu_selector(TextInputTest::nextCallback) );
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCMenu *menu = CCMenu::create(item1, item2, item3, NULL);
|
2011-04-26 18:04:07 +08:00
|
|
|
menu->setPosition(CCPointZero);
|
|
|
|
item1->setPosition(ccp( s.width/2 - 100,30));
|
|
|
|
item2->setPosition(ccp( s.width/2, 30));
|
|
|
|
item3->setPosition(ccp( s.width/2 + 100,30));
|
|
|
|
|
|
|
|
addChild(menu, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement KeyboardNotificationLayer
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
KeyboardNotificationLayer::KeyboardNotificationLayer()
|
|
|
|
: m_pTrackNode(0)
|
|
|
|
{
|
2012-06-15 15:10:40 +08:00
|
|
|
setTouchEnabled(true);
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardNotificationLayer::registerWithTouchDispatcher()
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
CCDirector* pDirector = CCDirector::sharedDirector();
|
2012-03-21 17:35:20 +08:00
|
|
|
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
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);
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
if (! m_pTrackNode)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
CCRect rectTracked = getRect(m_pTrackNode);
|
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.
|
2011-04-26 18:04:07 +08:00
|
|
|
if (! CCRect::CCRectIntersectsRect(rectTracked, info.end))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-27 17:01:27 +08:00
|
|
|
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
|
2011-04-26 18:04:07 +08:00
|
|
|
float adjustVert = CCRect::CCRectGetMaxY(info.end) - CCRect::CCRectGetMinY(rectTracked);
|
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
|
2011-04-26 18:04:07 +08:00
|
|
|
CCArray * children = getChildren();
|
|
|
|
CCNode * node = 0;
|
|
|
|
int count = children->count();
|
|
|
|
CCPoint pos;
|
|
|
|
for (int i = 0; i < count; ++i)
|
|
|
|
{
|
|
|
|
node = (CCNode*)children->objectAtIndex(i);
|
|
|
|
pos = node->getPosition();
|
|
|
|
pos.y += adjustVert;
|
|
|
|
node->setPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
// CCLayer function
|
|
|
|
|
|
|
|
bool KeyboardNotificationLayer::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
CCLOG("++++++++++++++++++++++++++++++++++++++++++++");
|
2012-04-19 14:35:52 +08:00
|
|
|
m_beginPos = pTouch->locationInView();
|
2011-05-04 14:13:15 +08:00
|
|
|
m_beginPos = CCDirector::sharedDirector()->convertToGL(m_beginPos);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyboardNotificationLayer::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
|
|
|
|
{
|
|
|
|
if (! m_pTrackNode)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
CCPoint endPos = pTouch->locationInView();
|
2011-05-04 14:13:15 +08:00
|
|
|
endPos = CCDirector::sharedDirector()->convertToGL(endPos);
|
|
|
|
|
|
|
|
float delta = 5.0f;
|
|
|
|
if (::abs(endPos.x - m_beginPos.x) > delta
|
|
|
|
|| ::abs(endPos.y - m_beginPos.y) > delta)
|
|
|
|
{
|
|
|
|
// not click
|
|
|
|
m_beginPos.x = m_beginPos.y = -1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// decide the trackNode is clicked.
|
|
|
|
CCRect rect;
|
|
|
|
CCPoint point = convertTouchToNodeSpaceAR(pTouch);
|
|
|
|
CCLOG("KeyboardNotificationLayer:clickedAt(%f,%f)", point.x, point.y);
|
|
|
|
|
|
|
|
rect = getRect(m_pTrackNode);
|
|
|
|
CCLOG("KeyboardNotificationLayer:TrackNode at(origin:%f,%f, size:%f,%f)",
|
|
|
|
rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
|
|
|
|
|
|
|
|
this->onClickTrackNode(CCRect::CCRectContainsPoint(rect, point));
|
|
|
|
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
|
|
|
{
|
2011-05-04 14:13:15 +08:00
|
|
|
CCTextFieldTTF * pTextField = (CCTextFieldTTF*)m_pTrackNode;
|
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
|
|
|
CCLOG("TextFieldTTFDefaultTest:CCTextFieldTTF attachWithIME");
|
|
|
|
pTextField->attachWithIME();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
|
|
|
CCLOG("TextFieldTTFDefaultTest:CCTextFieldTTF detachWithIME");
|
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFDefaultTest::onEnter()
|
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
|
|
|
// add CCTextFieldTTF
|
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
|
|
|
CCTextFieldTTF * pTextField = CCTextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
|
|
|
|
FONT_NAME,
|
|
|
|
FONT_SIZE);
|
|
|
|
addChild(pTextField);
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
// on android, CCTextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
|
|
|
// so we had to set a higher position to make it visable
|
|
|
|
pTextField->setPosition(ccp(s.width / 2, s.height/2 + 50));
|
2011-05-12 21:32:57 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
pTextField->setPosition(ccp(s.width / 2, s.height / 2));
|
2011-05-12 21:32:57 +08:00
|
|
|
#endif
|
2011-05-04 14:13:15 +08:00
|
|
|
|
|
|
|
m_pTrackNode = pTextField;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextFieldTTFActionTest
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
std::string TextFieldTTFActionTest::subtitle()
|
|
|
|
{
|
|
|
|
return "CCTextFieldTTF with action and char limit test";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTFActionTest::onClickTrackNode(bool bClicked)
|
|
|
|
{
|
|
|
|
CCTextFieldTTF * pTextField = (CCTextFieldTTF*)m_pTrackNode;
|
|
|
|
if (bClicked)
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest be clicked
|
|
|
|
CCLOG("TextFieldTTFActionTest:CCTextFieldTTF attachWithIME");
|
|
|
|
pTextField->attachWithIME();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// TextFieldTTFTest not be clicked
|
|
|
|
CCLOG("TextFieldTTFActionTest:CCTextFieldTTF detachWithIME");
|
|
|
|
pTextField->detachWithIME();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTFActionTest::onEnter()
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
|
|
|
KeyboardNotificationLayer::onEnter();
|
|
|
|
|
2011-05-12 14:45:44 +08:00
|
|
|
m_nCharLimit = 12;
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
m_pTextFieldAction = CCRepeatForever::create(
|
|
|
|
(CCActionInterval*)CCSequence::create(
|
|
|
|
CCFadeOut::create(0.25),
|
|
|
|
CCFadeIn::create(0.25),
|
2011-04-29 18:09:54 +08:00
|
|
|
0
|
|
|
|
));
|
2011-04-29 16:46:29 +08:00
|
|
|
m_pTextFieldAction->retain();
|
|
|
|
m_bAction = false;
|
|
|
|
|
|
|
|
// add CCTextFieldTTF
|
2011-04-26 18:04:07 +08:00
|
|
|
CCSize s = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
m_pTextField = CCTextFieldTTF::textFieldWithPlaceHolder("<click here for input>",
|
2011-04-29 16:46:29 +08:00
|
|
|
FONT_NAME,
|
|
|
|
FONT_SIZE);
|
2011-05-04 14:13:15 +08:00
|
|
|
addChild(m_pTextField);
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
m_pTextField->setDelegate(this);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
|
|
|
|
// on android, CCTextFieldTTF cannot auto adjust its position when soft-keyboard pop up
|
|
|
|
// so we had to set a higher position
|
|
|
|
m_pTextField->setPosition(ccp(s.width / 2, s.height/2 + 50));
|
2011-05-12 21:32:57 +08:00
|
|
|
#else
|
2012-04-19 14:35:52 +08:00
|
|
|
m_pTextField->setPosition(ccp(s.width / 2, s.height / 2));
|
2011-05-12 21:32:57 +08:00
|
|
|
#endif
|
2011-04-26 18:04:07 +08:00
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
m_pTrackNode = m_pTextField;
|
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();
|
2011-04-29 16:46:29 +08:00
|
|
|
m_pTextFieldAction->release();
|
2011-04-26 18:04:07 +08:00
|
|
|
}
|
|
|
|
|
2011-04-29 18:09:54 +08:00
|
|
|
// CCTextFieldDelegate protocol
|
2011-05-04 14:13:15 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldAttachWithIME(CCTextFieldTTF * pSender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2011-04-29 16:46:29 +08:00
|
|
|
if (! m_bAction)
|
|
|
|
{
|
2011-05-04 14:13:15 +08:00
|
|
|
m_pTextField->runAction(m_pTextFieldAction);
|
2011-04-29 18:09:54 +08:00
|
|
|
m_bAction = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldDetachWithIME(CCTextFieldTTF * pSender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
2011-04-29 16:46:29 +08:00
|
|
|
if (m_bAction)
|
|
|
|
{
|
2011-05-04 14:13:15 +08:00
|
|
|
m_pTextField->stopAction(m_pTextFieldAction);
|
|
|
|
m_pTextField->setOpacity(255);
|
2011-04-29 18:09:54 +08:00
|
|
|
m_bAction = false;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldInsertText(CCTextFieldTTF * pSender, 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;
|
|
|
|
}
|
|
|
|
|
2011-04-29 18:09:54 +08:00
|
|
|
// if the textfield's char count more than m_nCharLimit, doesn't insert text anymore.
|
2011-04-29 16:46:29 +08:00
|
|
|
if (pSender->getCharCount() >= m_nCharLimit)
|
|
|
|
{
|
|
|
|
return true;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// create a insert text sprite and do some action
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF * label = CCLabelTTF::create(text, FONT_NAME, FONT_SIZE);
|
2011-04-29 18:09:54 +08:00
|
|
|
this->addChild(label);
|
2011-04-29 16:46:29 +08:00
|
|
|
ccColor3B color = { 226, 121, 7};
|
|
|
|
label->setColor(color);
|
2011-04-29 18:09:54 +08:00
|
|
|
|
|
|
|
// move the sprite from top to position
|
|
|
|
CCPoint endPos = pSender->getPosition();
|
2011-04-29 16:46:29 +08:00
|
|
|
if (pSender->getCharCount())
|
|
|
|
{
|
|
|
|
endPos.x += pSender->getContentSize().width / 2;
|
2011-04-29 18:09:54 +08:00
|
|
|
}
|
|
|
|
CCSize inputTextSize = label->getContentSize();
|
|
|
|
CCPoint beginPos(endPos.x, CCDirector::sharedDirector()->getWinSize().height - inputTextSize.height * 2);
|
|
|
|
|
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);
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAction * seq = CCSequence::create(
|
|
|
|
CCSpawn::create(
|
|
|
|
CCMoveTo::create(duration, endPos),
|
|
|
|
CCScaleTo::create(duration, 1),
|
|
|
|
CCFadeOut::create(duration),
|
2011-04-29 18:09:54 +08:00
|
|
|
0),
|
2012-06-14 15:13:16 +08:00
|
|
|
CCCallFuncN::create(this, callfuncN_selector(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction)),
|
2011-04-29 18:09:54 +08:00
|
|
|
0);
|
|
|
|
label->runAction(seq);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
bool TextFieldTTFActionTest::onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
|
|
|
// create a delete text sprite and do some action
|
2012-06-14 15:13:16 +08:00
|
|
|
CCLabelTTF * label = CCLabelTTF::create(delText, FONT_NAME, FONT_SIZE);
|
2011-04-29 18:09:54 +08:00
|
|
|
this->addChild(label);
|
|
|
|
|
|
|
|
// move the sprite to fly out
|
|
|
|
CCPoint beginPos = pSender->getPosition();
|
|
|
|
CCSize textfieldSize = pSender->getContentSize();
|
|
|
|
CCSize labelSize = label->getContentSize();
|
|
|
|
beginPos.x += (textfieldSize.width - labelSize.width) / 2.0f;
|
|
|
|
|
|
|
|
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
|
|
|
|
CCPoint endPos(- winSize.width / 4.0f, winSize.height * (0.5 + (float)rand() / (2.0f * RAND_MAX)));
|
|
|
|
|
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);
|
|
|
|
|
2012-06-14 15:13:16 +08:00
|
|
|
CCAction * seq = CCSequence::create(
|
|
|
|
CCSpawn::create(
|
|
|
|
CCMoveTo::create(duration, endPos),
|
|
|
|
CCRepeat::create(
|
|
|
|
CCRotateBy::create(rotateDuration, (rand()%2) ? 360 : -360),
|
2011-04-29 18:09:54 +08:00
|
|
|
repeatTime),
|
2012-06-14 15:13:16 +08:00
|
|
|
CCFadeOut::create(duration),
|
2011-04-29 18:09:54 +08:00
|
|
|
0),
|
2012-06-14 15:13:16 +08:00
|
|
|
CCCallFuncN::create(this, callfuncN_selector(TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction)),
|
2011-04-29 18:09:54 +08:00
|
|
|
0);
|
|
|
|
label->runAction(seq);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
bool TextFieldTTFActionTest::onDraw(CCTextFieldTTF * pSender)
|
2011-04-29 18:09:54 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2011-04-29 16:46:29 +08:00
|
|
|
|
2011-05-04 14:13:15 +08:00
|
|
|
void TextFieldTTFActionTest::callbackRemoveNodeWhenDidAction(CCNode * pNode)
|
2011-04-29 16:46:29 +08:00
|
|
|
{
|
|
|
|
this->removeChild(pNode, true);
|
|
|
|
}
|
|
|
|
|
2011-04-26 18:04:07 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// implement TextInputTestScene
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void TextInputTestScene::runThisTest()
|
|
|
|
{
|
|
|
|
CCLayer* pLayer = nextTextInputTest();
|
|
|
|
addChild(pLayer);
|
|
|
|
|
|
|
|
CCDirector::sharedDirector()->replaceScene(this);
|
|
|
|
}
|