/**************************************************************************** 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" #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) #include "CCEditBoxImplIOS.h" #endif NS_CC_EXT_BEGIN CCEditBox::CCEditBox(void) : m_pEditBoxImpl(NULL) , m_pDelegate(NULL) , m_eEditBoxInputMode(kEditBoxInputModeAny) , m_eEditBoxInputFlag(kEditBoxInputFlagInitialCapsAllCharacters) , m_eKeyboardReturnType(kKeyboardReturnTypeDefault) , m_colText(ccWHITE) , m_colPlaceHolder(ccGRAY) , m_nMaxLength(0) , m_fAdjustHeight(0.0f) { } CCEditBox::~CCEditBox(void) { CC_SAFE_DELETE(m_pEditBoxImpl); } void CCEditBox::touchDownAction(CCObject *sender, CCControlEvent controlEvent) { m_pEditBoxImpl->openKeyboard(); } CCEditBox* CCEditBox::create(const CCSize& size) { CCEditBox* pRet = new CCEditBox(); if (pRet != NULL && pRet->initWithSize(size)) { pRet->autorelease(); } else { CC_SAFE_DELETE(pRet); } return pRet; } bool CCEditBox::initWithSize(const CCSize& size) { if (CCControlButton::init()) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) m_pEditBoxImpl = new CCEditBoxImplIOS(this); #endif m_pEditBoxImpl->initWithSize(size); CCScale9Sprite* pNormal9Sprite = CCScale9Sprite::create("extensions/button.png"); CCScale9Sprite* pPressed9Sprite = CCScale9Sprite::create("extensions/buttonHighlighted.png"); CCScale9Sprite* pDisabled9Sprite = CCScale9Sprite::create("extensions/button.png"); setBackgroundSpriteForState(pNormal9Sprite, CCControlStateNormal); setBackgroundSpriteForState(pPressed9Sprite, CCControlStateHighlighted); setBackgroundSpriteForState(pDisabled9Sprite, CCControlStateDisabled); this->setPreferredSize(size); this->setPosition(ccp(0, 0)); this->addTargetWithActionForControlEvent(this, cccontrol_selector(CCEditBox::touchDownAction), CCControlEventTouchDown); 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) { return m_pEditBoxImpl->getText(); } return NULL; } void CCEditBox::setFontColor(const ccColor3B& color) { m_colText = color; if (m_pEditBoxImpl != NULL) { m_pEditBoxImpl->setFontColor(color); } } 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); } } void CCEditBox::setContentSize(const CCSize& size) { CCControlButton::setContentSize(size); if (m_pEditBoxImpl != NULL) { m_pEditBoxImpl->setContentSize(size); } } void CCEditBox::visit(void) { CCControlButton::visit(); if (m_pEditBoxImpl != NULL) { m_pEditBoxImpl->visit(); } } void CCEditBox::onExit(void) { CCControlButton::onExit(); if (m_pEditBoxImpl != NULL) { // remove system edit control m_pEditBoxImpl->closeKeyboard(); } } static CCRect getRect(CCNode * pNode) { 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; } void CCEditBox::keyboardWillShow(CCIMEKeyboardNotificationInfo& info) { //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); CCRect rectTracked = getRect(this); //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. if (! rectTracked.intersectsRect(info.end)) { CCLog("info end x = %f, y = %f; width = %f, height = %f", info.end.origin.x, info.end.origin.y, info.end.size.width, info.end.size.height); CCLog("rectTracked end x = %f, y = %f; width = %f, height = %f", rectTracked.origin.x, rectTracked.origin.y, rectTracked.size.width, rectTracked.size.height); CCLog("needn't to adjust view layout."); return; } // assume keyboard at the bottom of screen, calculate the vertical adjustment. m_fAdjustHeight = info.end.getMaxY() - rectTracked.getMinY(); CCLOG("CCEditBox:needAdjustVerticalPosition(%f)", m_fAdjustHeight); if (m_pEditBoxImpl != NULL) { m_pEditBoxImpl->doAnimationWhenKeyboardMove(info.duration, m_fAdjustHeight); } } void CCEditBox::keyboardDidShow(CCIMEKeyboardNotificationInfo& info) { if (m_pDelegate != NULL) { m_pDelegate->editBoxEditingDidBegin(this); } } void CCEditBox::keyboardWillHide(CCIMEKeyboardNotificationInfo& info) { if (m_pEditBoxImpl != NULL) { m_pEditBoxImpl->doAnimationWhenKeyboardMove(info.duration, -m_fAdjustHeight); } } void CCEditBox::keyboardDidHide(CCIMEKeyboardNotificationInfo& info) { if (m_pDelegate != NULL) { m_pDelegate->editBoxEditingDidEnd(this); m_pDelegate->editBoxReturn(this); } } NS_CC_EXT_END