2012-08-15 14:33:56 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 James Chen
|
|
|
|
|
|
|
|
http://www.cocos2d-x.org
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "CCEditBox.h"
|
2012-08-17 14:29:07 +08:00
|
|
|
#include "CCEditBoxImpl.h"
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_BEGIN
|
|
|
|
|
|
|
|
CCEditBox::CCEditBox(void)
|
|
|
|
: m_pEditBoxImpl(NULL)
|
|
|
|
, m_pDelegate(NULL)
|
2012-08-17 14:29:07 +08:00
|
|
|
, m_eEditBoxInputMode(kEditBoxInputModeSingleLine)
|
2012-08-15 14:33:56 +08:00
|
|
|
, m_eEditBoxInputFlag(kEditBoxInputFlagInitialCapsAllCharacters)
|
|
|
|
, m_eKeyboardReturnType(kKeyboardReturnTypeDefault)
|
2013-05-24 11:42:27 +08:00
|
|
|
, m_nFontSize(-1)
|
|
|
|
, m_nPlaceholderFontSize(-1)
|
2012-08-15 14:33:56 +08:00
|
|
|
, m_colText(ccWHITE)
|
|
|
|
, m_colPlaceHolder(ccGRAY)
|
|
|
|
, m_nMaxLength(0)
|
|
|
|
, m_fAdjustHeight(0.0f)
|
2013-05-24 11:42:27 +08:00
|
|
|
, m_nScriptEditBoxHandler(0)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
CCEditBox::~CCEditBox(void)
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(m_pEditBoxImpl);
|
2013-05-24 11:42:27 +08:00
|
|
|
unregisterScriptEditBoxHandler();
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CCEditBox::touchDownAction(CCObject *sender, CCControlEvent controlEvent)
|
2013-03-12 17:23:12 +08:00
|
|
|
{
|
2012-08-15 14:33:56 +08:00
|
|
|
m_pEditBoxImpl->openKeyboard();
|
|
|
|
}
|
|
|
|
|
2012-08-17 14:29:07 +08:00
|
|
|
CCEditBox* CCEditBox::create(const CCSize& size, CCScale9Sprite* pNormal9SpriteBg, CCScale9Sprite* pPressed9SpriteBg/* = NULL*/, CCScale9Sprite* pDisabled9SpriteBg/* = NULL*/)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
|
|
|
CCEditBox* pRet = new CCEditBox();
|
|
|
|
|
2012-08-17 14:29:07 +08:00
|
|
|
if (pRet != NULL && pRet->initWithSizeAndBackgroundSprite(size, pNormal9SpriteBg))
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2012-08-17 14:29:07 +08:00
|
|
|
if (pPressed9SpriteBg != NULL)
|
|
|
|
{
|
|
|
|
pRet->setBackgroundSpriteForState(pPressed9SpriteBg, CCControlStateHighlighted);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pDisabled9SpriteBg != NULL)
|
|
|
|
{
|
|
|
|
pRet->setBackgroundSpriteForState(pDisabled9SpriteBg, CCControlStateDisabled);
|
|
|
|
}
|
2012-08-15 14:33:56 +08:00
|
|
|
pRet->autorelease();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
}
|
|
|
|
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
|
2012-08-17 14:29:07 +08:00
|
|
|
bool CCEditBox::initWithSizeAndBackgroundSprite(const CCSize& size, CCScale9Sprite* pPressed9SpriteBg)
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2012-08-17 14:29:07 +08:00
|
|
|
if (CCControlButton::initWithBackgroundSprite(pPressed9SpriteBg))
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2012-08-17 14:29:07 +08:00
|
|
|
m_pEditBoxImpl = __createSystemEditBox(this);
|
2012-08-15 14:33:56 +08:00
|
|
|
m_pEditBoxImpl->initWithSize(size);
|
|
|
|
|
2013-03-14 14:29:22 +08:00
|
|
|
this->setZoomOnTouchDown(false);
|
2012-08-15 14:33:56 +08:00
|
|
|
this->setPreferredSize(size);
|
|
|
|
this->setPosition(ccp(0, 0));
|
2012-08-22 09:47:04 +08:00
|
|
|
this->addTargetWithActionForControlEvent(this, cccontrol_selector(CCEditBox::touchDownAction), CCControlEventTouchUpInside);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setDelegate(CCEditBoxDelegate* pDelegate)
|
|
|
|
{
|
|
|
|
m_pDelegate = pDelegate;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setDelegate(pDelegate);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setText(const char* pText)
|
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
|
|
|
m_strText = pText;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setText(pText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCEditBox::getText(void)
|
|
|
|
{
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
const char* pText = m_pEditBoxImpl->getText();
|
|
|
|
if(pText != NULL)
|
|
|
|
return pText;
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
return "";
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-02-20 21:41:08 +08:00
|
|
|
void CCEditBox::setFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2013-04-02 21:31:30 +08:00
|
|
|
m_strFontName = pFontName;
|
|
|
|
m_nFontSize = fontSize;
|
2013-02-20 21:41:08 +08:00
|
|
|
if (pFontName != NULL)
|
|
|
|
{
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setFont(pFontName, fontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 21:31:30 +08:00
|
|
|
void CCEditBox::setFontName(const char* pFontName)
|
|
|
|
{
|
|
|
|
m_strFontName = pFontName;
|
|
|
|
if (m_pEditBoxImpl != NULL && m_nFontSize != -1)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setFont(pFontName, m_nFontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setFontSize(int fontSize)
|
|
|
|
{
|
|
|
|
m_nFontSize = fontSize;
|
|
|
|
if (m_pEditBoxImpl != NULL && m_strFontName.length() > 0)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setFont(m_strFontName.c_str(), m_nFontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBox::setFontColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
m_colText = color;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setFontColor(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-25 14:05:38 +08:00
|
|
|
void CCEditBox::setPlaceholderFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2013-04-02 21:31:30 +08:00
|
|
|
m_strPlaceholderFontName = pFontName;
|
|
|
|
m_nPlaceholderFontSize = fontSize;
|
2013-02-25 14:05:38 +08:00
|
|
|
if (pFontName != NULL)
|
|
|
|
{
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setPlaceholderFont(pFontName, fontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-02 21:31:30 +08:00
|
|
|
void CCEditBox::setPlaceholderFontName(const char* pFontName)
|
|
|
|
{
|
|
|
|
m_strPlaceholderFontName = pFontName;
|
|
|
|
if (m_pEditBoxImpl != NULL && m_nPlaceholderFontSize != -1)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setPlaceholderFont(pFontName, m_nFontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setPlaceholderFontSize(int fontSize)
|
|
|
|
{
|
|
|
|
m_nPlaceholderFontSize = fontSize;
|
|
|
|
if (m_pEditBoxImpl != NULL && m_strPlaceholderFontName.length() > 0)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setPlaceholderFont(m_strPlaceholderFontName.c_str(), m_nFontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBox::setPlaceholderFontColor(const ccColor3B& color)
|
|
|
|
{
|
|
|
|
m_colText = color;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setPlaceholderFontColor(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setPlaceHolder(const char* pText)
|
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
|
|
|
m_strPlaceHolder = pText;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setPlaceHolder(pText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* CCEditBox::getPlaceHolder(void)
|
|
|
|
{
|
|
|
|
return m_strPlaceHolder.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setInputMode(EditBoxInputMode inputMode)
|
|
|
|
{
|
|
|
|
m_eEditBoxInputMode = inputMode;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setInputMode(inputMode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
m_nMaxLength = maxLength;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setMaxLength(maxLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int CCEditBox::getMaxLength()
|
|
|
|
{
|
|
|
|
return m_nMaxLength;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setInputFlag(EditBoxInputFlag inputFlag)
|
|
|
|
{
|
|
|
|
m_eEditBoxInputFlag = inputFlag;
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setInputFlag(inputFlag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::setReturnType(KeyboardReturnType returnType)
|
|
|
|
{
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setReturnType(returnType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* override function */
|
|
|
|
void CCEditBox::setPosition(const CCPoint& pos)
|
|
|
|
{
|
|
|
|
CCControlButton::setPosition(pos);
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setPosition(pos);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-03 18:58:51 +08:00
|
|
|
void CCEditBox::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
CCControlButton::setVisible(visible);
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setVisible(visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBox::setContentSize(const CCSize& size)
|
|
|
|
{
|
|
|
|
CCControlButton::setContentSize(size);
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setContentSize(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 21:41:08 +08:00
|
|
|
void CCEditBox::setAnchorPoint(const CCPoint& anchorPoint)
|
|
|
|
{
|
|
|
|
CCControlButton::setAnchorPoint(anchorPoint);
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->setAnchorPoint(anchorPoint);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBox::visit(void)
|
|
|
|
{
|
|
|
|
CCControlButton::visit();
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->visit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 17:23:12 +08:00
|
|
|
void CCEditBox::onEnter(void)
|
|
|
|
{
|
|
|
|
CCControlButton::onEnter();
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->onEnter();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
void CCEditBox::onExit(void)
|
|
|
|
{
|
|
|
|
CCControlButton::onExit();
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
// remove system edit control
|
|
|
|
m_pEditBoxImpl->closeKeyboard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static CCRect getRect(CCNode * pNode)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
CCSize contentSize = pNode->getContentSize();
|
|
|
|
CCRect rect = CCRectMake(0, 0, contentSize.width, contentSize.height);
|
|
|
|
return CCRectApplyAffineTransform(rect, pNode->nodeToWorldTransform());
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2012-08-17 14:29:07 +08:00
|
|
|
// CCLOG("CCEditBox::keyboardWillShow");
|
2012-08-15 14:33:56 +08:00
|
|
|
CCRect rectTracked = getRect(this);
|
2013-02-20 21:52:49 +08:00
|
|
|
// some adjustment for margin between the keyboard and the edit box.
|
|
|
|
rectTracked.origin.y -= 4;
|
2013-03-14 14:29:22 +08:00
|
|
|
|
2012-08-17 14:29:07 +08:00
|
|
|
// if the keyboard area doesn't intersect with the tracking node area, nothing needs to be done.
|
|
|
|
if (!rectTracked.intersectsRect(info.end))
|
2012-08-15 14:33:56 +08:00
|
|
|
{
|
2012-08-17 14:29:07 +08:00
|
|
|
CCLOG("needn't to adjust view layout.");
|
2012-08-15 14:33:56 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume keyboard at the bottom of screen, calculate the vertical adjustment.
|
|
|
|
m_fAdjustHeight = info.end.getMaxY() - rectTracked.getMinY();
|
2012-08-17 14:29:07 +08:00
|
|
|
// CCLOG("CCEditBox:needAdjustVerticalPosition(%f)", m_fAdjustHeight);
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->doAnimationWhenKeyboardMove(info.duration, m_fAdjustHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::keyboardDidShow(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::keyboardWillHide(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2012-08-17 14:29:07 +08:00
|
|
|
// CCLOG("CCEditBox::keyboardWillHide");
|
2012-08-15 14:33:56 +08:00
|
|
|
if (m_pEditBoxImpl != NULL)
|
|
|
|
{
|
|
|
|
m_pEditBoxImpl->doAnimationWhenKeyboardMove(info.duration, -m_fAdjustHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::keyboardDidHide(CCIMEKeyboardNotificationInfo& info)
|
|
|
|
{
|
2013-03-12 17:23:12 +08:00
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
}
|
|
|
|
|
2013-05-24 11:42:27 +08:00
|
|
|
void CCEditBox::registerScriptEditBoxHandler(int handler)
|
|
|
|
{
|
|
|
|
unregisterScriptEditBoxHandler();
|
|
|
|
m_nScriptEditBoxHandler = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCEditBox::unregisterScriptEditBoxHandler(void)
|
|
|
|
{
|
|
|
|
if (0 != m_nScriptEditBoxHandler)
|
|
|
|
{
|
|
|
|
CCScriptEngineManager::sharedManager()->getScriptEngine()->removeScriptHandler(m_nScriptEditBoxHandler);
|
|
|
|
m_nScriptEditBoxHandler = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-15 14:33:56 +08:00
|
|
|
|
|
|
|
NS_CC_EXT_END
|