Merge pull request #264 from yangws/master

fixed a bug: TextInputTextWithAction can't stop CCTextFieldTTF's action when click enter button.
This commit is contained in:
Walzer 2011-05-11 03:28:16 -07:00
commit d0ff1f274e
2 changed files with 25 additions and 19 deletions

View File

@ -179,27 +179,33 @@ void CCTextFieldTTF::insertText(const char * text, int len)
len = nPos; len = nPos;
sInsert.erase(nPos); sInsert.erase(nPos);
} }
if (len <= 0)
if (len > 0)
{ {
// close keyboard if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len))
CCEGLView * pGlView = CCDirector::sharedDirector()->getOpenGLView();
if (pGlView)
{ {
pGlView->setIMEKeyboardState(false); // delegate doesn't want insert text
return;
} }
return;
m_nCharCount += _calcCharCount(sInsert.c_str());
std::string sText(*m_pInputText);
sText.append(sInsert);
setString(sText.c_str());
} }
if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, sInsert.c_str(), len)) if (sInsert.npos == nPos) {
return;
}
// '\n' has inserted, let delegate process first
if (m_pDelegate && m_pDelegate->onTextFieldInsertText(this, "\n", 1))
{ {
// delegate doesn't want insert text
return; return;
} }
m_nCharCount += _calcCharCount(sInsert.c_str()); // if lelegate hasn't process, detach with ime as default
std::string sText(*m_pInputText); detachWithIME();
sText.append(sInsert);
setString(sText.c_str());
} }
void CCTextFieldTTF::deleteBackward() void CCTextFieldTTF::deleteBackward()

View File

@ -368,18 +368,18 @@ bool TextFieldTTFActionTest::onTextFieldDetachWithIME(CCTextFieldTTF * pSender)
bool TextFieldTTFActionTest::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen) bool TextFieldTTFActionTest::onTextFieldInsertText(CCTextFieldTTF * pSender, const char * text, int nLen)
{ {
// if insert enter, treat as default to detach with ime
if ('\n' == *text)
{
return false;
}
// if the textfield's char count more than m_nCharLimit, doesn't insert text anymore. // if the textfield's char count more than m_nCharLimit, doesn't insert text anymore.
if (pSender->getCharCount() >= m_nCharLimit) if (pSender->getCharCount() >= m_nCharLimit)
{ {
return true; return true;
} }
// if insert enter, treat as default
if ('\n' == *text)
{
return false;
}
// create a insert text sprite and do some action // create a insert text sprite and do some action
CCLabelTTF * label = CCLabelTTF::labelWithString(text, FONT_NAME, FONT_SIZE); CCLabelTTF * label = CCLabelTTF::labelWithString(text, FONT_NAME, FONT_SIZE);
this->addChild(label); this->addChild(label);