Merge pull request #245 from yangws/input.

TextInputTest on on ios.
This commit is contained in:
minggo 2011-05-02 18:48:48 -07:00
commit 0db248e1f1
5 changed files with 335 additions and 74 deletions

View File

@ -31,11 +31,56 @@ THE SOFTWARE.
NS_CC_BEGIN;
/**
@brief A simple text input field with system TTF font.
*/
class CCTextFieldTTF;
class CC_DLL CCTextFieldTTF : public CCLabelTTF, public CCIMEDelegate//, public CCTargetedTouchDelegate
class CC_DLL CCTextFieldDelegate
{
public:
/**
@brief If the sender doesn't want to attach with IME, return true;
*/
virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * sender)
{
return false;
}
/**
@brief If the sender doesn't want to detach with IME, return true;
*/
virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * sender)
{
return false;
}
/**
@brief If the sender doesn't want to insert the text, return true;
*/
virtual bool onTextFieldInsertText(CCTextFieldTTF * sender, const char * text, int nLen)
{
return false;
}
/**
@brief If the sender doesn't want to delete the delText, return true;
*/
virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * sender, const char * delText, int nLen)
{
return false;
}
/**
@brief If doesn't want draw sender as default, return true.
*/
virtual bool onDraw(CCTextFieldTTF * sender)
{
return false;
}
};
/**
@brief A simple text input field with TTF font.
*/
class CC_DLL CCTextFieldTTF : public CCLabelTTF, public CCIMEDelegate
{
public:
CCTextFieldTTF();
@ -66,6 +111,10 @@ public:
// properties
//////////////////////////////////////////////////////////////////////////
CC_SYNTHESIZE(CCTextFieldDelegate *, m_pDelegate, Delegate);
CC_SYNTHESIZE_READONLY(int, m_nCharCount, CharCount);
CC_SYNTHESIZE(ccColor3B, m_ColorSpaceHolder, ColorSpaceHolder);
// input text property
public:
virtual void setString(const char *text);
@ -80,9 +129,10 @@ public:
virtual const char * getPlaceHolder(void);
protected:
std::string * m_pPlaceHolder;
bool m_bLock; // when insertText or deleteBackward called, m_bLock is true
protected:
virtual void draw();
//////////////////////////////////////////////////////////////////////////
// CCIMEDelegate interface
//////////////////////////////////////////////////////////////////////////

View File

@ -153,7 +153,16 @@ bool CCIMEDispatcher::attachDelegateWithIME(CCIMEDelegate * pDelegate)
CCIMEDelegate * pOldDelegate = m_pImpl->m_DelegateWithIme;
m_pImpl->m_DelegateWithIme = 0;
pOldDelegate->didDetachWithIME();
m_pImpl->m_DelegateWithIme = *iter;
pDelegate->didAttachWithIME();
bRet = true;
break;
}
// havn't delegate attached with IME yet
CC_BREAK_IF(! pDelegate->canAttachWithIME());
m_pImpl->m_DelegateWithIme = *iter;
pDelegate->didAttachWithIME();
bRet = true;

View File

@ -29,15 +29,34 @@ THE SOFTWARE.
NS_CC_BEGIN;
static int _calcCharCount(const char * pszText)
{
int n = 0;
char ch = 0;
while (ch = *pszText)
{
CC_BREAK_IF(! ch);
if (0x80 != (0xC0 & ch))
{
++n;
}
++pszText;
}
return n;
}
//////////////////////////////////////////////////////////////////////////
// constructor and destructor
//////////////////////////////////////////////////////////////////////////
CCTextFieldTTF::CCTextFieldTTF()
: m_pInputText(new std::string)
: m_pDelegate(0)
, m_nCharCount(0)
, m_pInputText(new std::string)
, m_pPlaceHolder(new std::string) // prevent CCLabelTTF initWithString assertion
, m_bLock(false)
{
m_ColorSpaceHolder.r = m_ColorSpaceHolder.g = m_ColorSpaceHolder.b = 127;
}
CCTextFieldTTF::~CCTextFieldTTF()
@ -51,34 +70,34 @@ CCTextFieldTTF::~CCTextFieldTTF()
//////////////////////////////////////////////////////////////////////////
CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
{
CCTextFieldTTF *pRet = new CCTextFieldTTF();
if(pRet && pRet->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
{
pRet->autorelease();
{
CCTextFieldTTF *pRet = new CCTextFieldTTF();
if(pRet && pRet->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
{
pRet->autorelease();
if (placeholder)
{
pRet->setPlaceHolder(placeholder);
}
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
{
CCTextFieldTTF *pRet = new CCTextFieldTTF();
if(pRet && pRet->initWithString("", fontName, fontSize))
{
pRet->autorelease();
CCTextFieldTTF *pRet = new CCTextFieldTTF();
if(pRet && pRet->initWithString("", fontName, fontSize))
{
pRet->autorelease();
if (placeholder)
{
pRet->setPlaceHolder(placeholder);
}
return pRet;
}
CC_SAFE_DELETE(pRet);
return pRet;
}
CC_SAFE_DELETE(pRet);
return NULL;
}
@ -86,23 +105,23 @@ CCTextFieldTTF * CCTextFieldTTF::textFieldWithPlaceHolder(const char *placeholde
// initialize
//////////////////////////////////////////////////////////////////////////
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
{
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
{
if (placeholder)
{
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = new std::string(placeholder);
}
return CCLabelTTF::initWithString(m_pPlaceHolder->c_str(), dimensions, alignment, fontName, fontSize);
}
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
{
return CCLabelTTF::initWithString(m_pPlaceHolder->c_str(), dimensions, alignment, fontName, fontSize);
}
bool CCTextFieldTTF::initWithPlaceHolder(const char *placeholder, const char *fontName, float fontSize)
{
if (placeholder)
{
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = new std::string(placeholder);
}
return CCLabelTTF::initWithString(m_pPlaceHolder->c_str(), fontName, fontSize);
return CCLabelTTF::initWithString(m_pPlaceHolder->c_str(), fontName, fontSize);
}
//////////////////////////////////////////////////////////////////////////
@ -141,12 +160,12 @@ bool CCTextFieldTTF::detachWithIME()
bool CCTextFieldTTF::canAttachWithIME()
{
return true;
return (m_pDelegate) ? (! m_pDelegate->onTextFieldAttachWithIME(this)) : true;
}
bool CCTextFieldTTF::canDetachWithIME()
{
return true;
return (m_pDelegate) ? (! m_pDelegate->onTextFieldDetachWithIME(this)) : true;
}
void CCTextFieldTTF::insertText(const char * text, int len)
@ -171,11 +190,16 @@ void CCTextFieldTTF::insertText(const char * text, int len)
return;
}
m_bLock = true;
if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
{
// delegate doesn't want insert text
return;
}
m_nCharCount += _calcCharCount(sInsert.c_str());
std::string sText(*m_pInputText);
sText.append(sInsert);
setString(sText.c_str());
m_bLock = false;
}
void CCTextFieldTTF::deleteBackward()
@ -187,8 +211,6 @@ void CCTextFieldTTF::deleteBackward()
return;
}
m_bLock = true;
// get the delete byte number
int nDeleteLen = 1; // default, erase 1 byte
@ -196,11 +218,19 @@ void CCTextFieldTTF::deleteBackward()
{
++nDeleteLen;
}
if (m_pDelegate && m_pDelegate->onTextFieldDeleteBackward(this, m_pInputText->c_str() + nStrLen - nDeleteLen, nDeleteLen))
{
// delegate don't wan't delete backward
return;
}
// if delete all text, show space holder string
if (nStrLen <= nDeleteLen)
{
CC_SAFE_DELETE(m_pInputText);
m_pInputText = new std::string;
m_nCharCount = 0;
CCLabelTTF::setString(m_pPlaceHolder->c_str());
return;
}
@ -208,16 +238,34 @@ void CCTextFieldTTF::deleteBackward()
// set new input text
std::string sText(m_pInputText->c_str(), nStrLen - nDeleteLen);
setString(sText.c_str());
m_bLock = false;
}
void CCTextFieldTTF::draw()
{
if (m_pDelegate && m_pDelegate->onDraw(this))
{
return;
}
if (m_pInputText->length())
{
CCLabelTTF::draw();
return;
}
// draw placeholder
ccColor3B color = getColor();
setColor(m_ColorSpaceHolder);
CCLabelTTF::draw();
setColor(color);
}
//////////////////////////////////////////////////////////////////////////
// properties
//////////////////////////////////////////////////////////////////////////
// input text property
void CCTextFieldTTF::setString(const char *text)
{
// input text property
void CCTextFieldTTF::setString(const char *text)
{
CC_SAFE_DELETE(m_pInputText);
if (text)
@ -229,22 +277,28 @@ void CCTextFieldTTF::setString(const char *text)
m_pInputText = new std::string;
}
// if there is no input text, display placeholder instead if (! m_pInputText->length()) { CCLabelTTF::setString(m_pPlaceHolder->c_str()); } else
// if there is no input text, display placeholder instead
if (! m_pInputText->length())
{
CCLabelTTF::setString(m_pPlaceHolder->c_str());
}
else
{
CCLabelTTF::setString(m_pInputText->c_str());
}
}
const char* CCTextFieldTTF::getString(void)
{
return m_pInputText->c_str();
}
m_nCharCount = _calcCharCount(m_pInputText->c_str());
}
const char* CCTextFieldTTF::getString(void)
{
return m_pInputText->c_str();
}
// place holder text property
void CCTextFieldTTF::setPlaceHolder(const char * text)
{
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = (text) ? new std::string(text) : new std::string;
CC_SAFE_DELETE(m_pPlaceHolder);
m_pPlaceHolder = (text) ? new std::string(text) : new std::string;
if (! m_pInputText->length())
{
CCLabelTTF::setString(m_pPlaceHolder->c_str());

View File

@ -54,7 +54,7 @@ CCLayer* backTextInputTest()
return restartTextInputTest();
}
CCRect getRect(CCNode * pNode)
static CCRect getRect(CCNode * pNode)
{
CCRect rc;
rc.origin = pNode->getPosition();
@ -128,7 +128,7 @@ void TextInputTest::onEnter()
addChild(l, 1);
l->setPosition(ccp(s.width/2, s.height-80));
}
#if 0
CCMenuItemImage *item1 = CCMenuItemImage::itemFromNormalImage("Images/b1.png", "Images/b2.png", this, menu_selector(TextInputTest::backCallback));
CCMenuItemImage *item2 = CCMenuItemImage::itemFromNormalImage("Images/r1.png","Images/r2.png", this, menu_selector(TextInputTest::restartCallback) );
CCMenuItemImage *item3 = CCMenuItemImage::itemFromNormalImage("Images/f1.png", "Images/f2.png", this, menu_selector(TextInputTest::nextCallback) );
@ -140,6 +140,7 @@ void TextInputTest::onEnter()
item3->setPosition(ccp( s.width/2 + 100,30));
addChild(menu, 1);
#endif
}
void TextInputTest::onExit()
@ -212,31 +213,53 @@ void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo&
// implement TextFieldTTFTest
//////////////////////////////////////////////////////////////////////////
TextFieldTTFTest::TextFieldTTFTest()
: m_nSelected(-1)
{
CCSize s = CCDirector::sharedDirector()->getWinSize();
m_pTextField[0] = CCTextFieldTTF::textFieldWithPlaceHolder("<click here>",
CCSizeMake(240, 28),
CCTextAlignmentCenter,
"Thonburi",
24);
addChild(m_pTextField[0]);
m_pTextField[0]->setPosition(ccp(s.width/2, s.height/2 + 16));
m_pTextField[1] = CCTextFieldTTF::textFieldWithPlaceHolder("<click here>",
"Thonburi",
24);
addChild(m_pTextField[1]);
m_pTextField[1]->setPosition(ccp(s.width/2, s.height/2 - 16));
}
#define FONT_NAME "Thonburi"
#define FONT_SIZE 24
std::string TextFieldTTFTest::subtitle()
{
return "CCTextFieldTTF test";
}
void TextFieldTTFTest::onEnter()
{
KeyboardNotificationLayer::onEnter();
m_nSelected = -1;
m_nCharLimit = 10;
m_pTextFieldAction = CCRepeatForever::actionWithAction(
(CCActionInterval*)CCSequence::actions(
CCFadeOut::actionWithDuration(0.25),
CCFadeIn::actionWithDuration(0.25),
0
));
m_pTextFieldAction->retain();
m_bAction = false;
// add CCTextFieldTTF
CCSize s = CCDirector::sharedDirector()->getWinSize();
m_pTextField[0] = CCTextFieldTTF::textFieldWithPlaceHolder("<CCTextFieldTTF with action>",
FONT_NAME,
FONT_SIZE);
addChild(m_pTextField[0]);
m_pTextField[0]->setDelegate(this);
m_pTextField[0]->setPosition(ccp(s.width/2, s.height/2 + 20));
m_pTextField[1] = CCTextFieldTTF::textFieldWithPlaceHolder("<CCTextFieldTTF as default>",
FONT_NAME,
FONT_SIZE);
addChild(m_pTextField[1]);
m_pTextField[1]->setPosition(ccp(s.width/2, s.height/2 - 20));
}
void TextFieldTTFTest::onExit()
{
m_pTextFieldAction->release();
}
bool TextFieldTTFTest::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
m_beginPos = pTouch->locationInView(pTouch->view());
@ -294,6 +317,117 @@ void TextFieldTTFTest::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent)
}
}
// CCTextFieldDelegate protocol
bool TextFieldTTFTest::onTextFieldAttachWithIME(CCTextFieldTTF * pSender)
{
if (! m_bAction)
{
m_pTextField[0]->runAction(m_pTextFieldAction);
m_bAction = true;
}
return false;
}
bool TextFieldTTFTest::onTextFieldDetachWithIME(CCTextFieldTTF * pSender)
{
if (m_bAction)
{
m_pTextField[0]->stopAction(m_pTextFieldAction);
m_pTextField[0]->setOpacity(255);
m_bAction = false;
}
return false;
}
bool TextFieldTTFTest::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen)
{
// if the textfield's char count more than m_nCharLimit, doesn't insert text anymore.
if (pSender->getCharCount() >= m_nCharLimit)
{
return true;
}
// if insert enter, treat as default
if ('\n' == *text)
{
return false;
}
// create a insert text sprite and do some action
CCLabelTTF * label = CCLabelTTF::labelWithString(text, FONT_NAME, FONT_SIZE);
this->addChild(label);
ccColor3B color = { 226, 121, 7};
label->setColor(color);
// move the sprite from top to position
CCPoint endPos = pSender->getPosition();
if (pSender->getCharCount())
{
endPos.x += pSender->getContentSize().width / 2;
}
CCSize inputTextSize = label->getContentSize();
CCPoint beginPos(endPos.x, CCDirector::sharedDirector()->getWinSize().height - inputTextSize.height * 2);
ccTime duration = 0.5;
label->setPosition(beginPos);
label->setScale(8);
CCAction * seq = CCSequence::actions(
CCSpawn::actions(
CCMoveTo::actionWithDuration(duration, endPos),
CCScaleTo::actionWithDuration(duration, 1),
CCFadeOut::actionWithDuration(duration),
0),
CCCallFuncN::actionWithTarget(this, callfuncN_selector(TextFieldTTFTest::callbackRemoveNodeWhenDidAction)),
0);
label->runAction(seq);
return false;
}
bool TextFieldTTFTest::onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen)
{
// create a delete text sprite and do some action
CCLabelTTF * label = CCLabelTTF::labelWithString(delText, FONT_NAME, FONT_SIZE);
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)));
ccTime duration = 1;
ccTime rotateDuration = 0.2f;
int repeatTime = 5;
label->setPosition(beginPos);
CCAction * seq = CCSequence::actions(
CCSpawn::actions(
CCMoveTo::actionWithDuration(duration, endPos),
CCRepeat::actionWithAction(
CCRotateBy::actionWithDuration(rotateDuration, (rand()%2) ? 360 : -360),
repeatTime),
CCFadeOut::actionWithDuration(duration),
0),
CCCallFuncN::actionWithTarget(this, callfuncN_selector(TextFieldTTFTest::callbackRemoveNodeWhenDidAction)),
0);
label->runAction(seq);
return false;
}
bool TextFieldTTFTest::onDraw(CCTextFieldTTF * pSender)
{
return false;
}
void TextFieldTTFTest::callbackRemoveNodeWhenDidAction(CCNode * pNode)
{
this->removeChild(pNode, true);
}
//////////////////////////////////////////////////////////////////////////
// implement TextInputTestScene
//////////////////////////////////////////////////////////////////////////

View File

@ -35,18 +35,32 @@ protected:
CCNode * m_pTrackNode;
};
class TextFieldTTFTest : public KeyboardNotificationLayer
class TextFieldTTFTest : public KeyboardNotificationLayer, public CCTextFieldDelegate
{
CCPoint m_beginPos;
CCTextFieldTTF * m_pTextField[2];
CCAction * m_pTextFieldAction;
bool m_bAction;
int m_nSelected;
public:
TextFieldTTFTest();
int m_nCharLimit; // the textfield max char limit
public:
virtual std::string subtitle();
// CCLayer
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent);
// CCTextFieldDelegate
virtual bool onTextFieldAttachWithIME(CCTextFieldTTF * pSender);
virtual bool onTextFieldDetachWithIME(CCTextFieldTTF * pSender);
virtual bool onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen);
virtual bool onTextFieldDeleteBackward(CCTextFieldTTF * pSender, const char * delText, int nLen);
virtual bool onDraw(CCTextFieldTTF * pSender);
void callbackRemoveNodeWhenDidAction(CCNode * pNode);
};
class TextInputTestScene : public TestScene