123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 |
- /****************************************************************************
- Copyright (c) 2010-2012 cocos2d-x.org
- Copyright (c) 2012 James Chen
- Copyright (c) 2013-2015 zilongshanren
- Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
- 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 "ui/UIEditBox/UIEditBoxImpl-common.h"
- #define kLabelZOrder 9999
- #include "ui/UIEditBox/UIEditBox.h"
- #include "base/CCDirector.h"
- #include "2d/CCLabel.h"
- #include "ui/UIHelper.h"
- static const int CC_EDIT_BOX_PADDING = 5;
- static cocos2d::Size applyPadding(const cocos2d::Size& sizeToCorrect) {
- return cocos2d::Size(sizeToCorrect.width - CC_EDIT_BOX_PADDING * 2, sizeToCorrect.height);
- }
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- #define PASSWORD_CHAR "*"
- #else
- #define PASSWORD_CHAR "\u25CF"
- #endif
- NS_CC_BEGIN
- namespace ui {
- EditBoxImplCommon::EditBoxImplCommon(EditBox* pEditText)
- : EditBoxImpl(pEditText)
- , _label(nullptr)
- , _labelPlaceHolder(nullptr)
- , _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
- , _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
- , _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
- , _fontSize(-1)
- , _placeholderFontSize(-1)
- , _colText(Color3B::WHITE)
- , _colPlaceHolder(Color3B::GRAY)
- , _maxLength(-1)
- , _alignment(TextHAlignment::LEFT)
- , _editingMode(false)
- {
- }
- EditBoxImplCommon::~EditBoxImplCommon()
- {
- }
- bool EditBoxImplCommon::initWithSize(const Size& size)
- {
- do
- {
-
- Rect rect = Rect(0, 0, size.width, size.height);
-
- this->createNativeControl(rect);
-
- initInactiveLabels(size);
- setContentSize(size);
-
- return true;
- }while (0);
-
- return false;
- }
- void EditBoxImplCommon::initInactiveLabels(const Size& size)
- {
- const char* pDefaultFontName = this->getNativeDefaultFontName();
- _label = Label::create();
- _label->setAnchorPoint(Vec2(0,1));
- _label->setOverflow(Label::Overflow::CLAMP);
- _label->setVisible(false);
- _editBox->addChild(_label, kLabelZOrder);
-
- _labelPlaceHolder = Label::create();
- _labelPlaceHolder->setAnchorPoint(Vec2(0, 1.0f));
- _labelPlaceHolder->setTextColor(Color4B::GRAY);
- _labelPlaceHolder->enableWrap(false);
- _editBox->addChild(_labelPlaceHolder, kLabelZOrder);
-
- setFont(pDefaultFontName, size.height*2/3);
- setPlaceholderFont(pDefaultFontName, size.height*2/3);
- }
- void EditBoxImplCommon::placeInactiveLabels(const Size& size)
- {
- _label->setDimensions(size.width, size.height);
-
- auto placeholderSize = _labelPlaceHolder->getContentSize();
-
- if(_editBoxInputMode == EditBox::InputMode::ANY){
- _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height - CC_EDIT_BOX_PADDING));
- _label->setVerticalAlignment(TextVAlignment::TOP);
- _label->enableWrap(true);
-
- _labelPlaceHolder->setPosition(Vec2(CC_EDIT_BOX_PADDING,
- size.height - CC_EDIT_BOX_PADDING));
- _labelPlaceHolder->setVerticalAlignment(TextVAlignment::TOP);
- }
- else {
- _label->enableWrap(false);
- _label->setPosition(Vec2(CC_EDIT_BOX_PADDING, size.height));
- _label->setVerticalAlignment(TextVAlignment::CENTER);
-
- _labelPlaceHolder->setPosition(Vec2(CC_EDIT_BOX_PADDING,
- (size.height + placeholderSize.height) / 2));
- _labelPlaceHolder->setVerticalAlignment(TextVAlignment::CENTER);
- }
- }
- void EditBoxImplCommon::setInactiveText(const char* pText)
- {
- if(EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
- {
- std::string passwordString;
- for(size_t i = 0; i < strlen(pText); ++i)
- passwordString.append(PASSWORD_CHAR);
- _label->setString(passwordString);
- }
- else
- {
- _label->setString(pText);
- }
- // Clip the text width to fit to the text box
- const auto maxSize = applyPadding(_editBox->getContentSize());
- Size labelSize = _label->getContentSize();
- if(labelSize.width > maxSize.width || labelSize.height > maxSize.height)
- {
- _label->setDimensions(maxSize.width, maxSize.height);
- }
- }
-
- void EditBoxImplCommon::setFont(const char* pFontName, int fontSize)
- {
- _fontName = pFontName;
- _fontSize = fontSize;
- this->setNativeFont(pFontName, fontSize * _label->getNodeToWorldAffineTransform().a);
- if (!_fontName.empty())
- {
- _label->setSystemFontName(pFontName);
- }
- if (fontSize > 0)
- {
- _label->setSystemFontSize(fontSize);
- }
- }
- void EditBoxImplCommon::setFontColor(const Color4B& color)
- {
- _colText = color;
- this->setNativeFontColor(color);
- _label->setTextColor(color);
- }
- void EditBoxImplCommon::setPlaceholderFont(const char* pFontName, int fontSize)
- {
- _placeholderFontName = pFontName;
- _placeholderFontSize = fontSize;
- this->setNativePlaceholderFont(pFontName, fontSize * _labelPlaceHolder->getNodeToWorldAffineTransform().a);
- if (!_placeholderFontName.empty())
- {
- _labelPlaceHolder->setSystemFontName(pFontName);
- }
- if (fontSize > 0)
- {
- _labelPlaceHolder->setSystemFontSize(fontSize);
- }
- }
-
- void EditBoxImplCommon::setPlaceholderFontColor(const Color4B &color)
- {
- _colPlaceHolder = color;
- this->setNativePlaceholderFontColor(color);
- _labelPlaceHolder->setTextColor(color);
- }
- void EditBoxImplCommon::setInputMode(EditBox::InputMode inputMode)
- {
- _editBoxInputMode = inputMode;
- this->setNativeInputMode(inputMode);
- this->placeInactiveLabels(applyPadding(_editBox->getContentSize()));
- }
- void EditBoxImplCommon::setMaxLength(int maxLength)
- {
- _maxLength = maxLength;
- this->setNativeMaxLength(maxLength);
- }
- void EditBoxImplCommon::setTextHorizontalAlignment(cocos2d::TextHAlignment alignment)
- {
- _alignment = alignment;
- this->setNativeTextHorizontalAlignment(alignment);
- refreshLabelAlignment();
- }
- void EditBoxImplCommon::setInputFlag(EditBox::InputFlag inputFlag)
- {
- _editBoxInputFlag = inputFlag;
- this->setNativeInputFlag(inputFlag);
- }
- void EditBoxImplCommon::setReturnType(EditBox::KeyboardReturnType returnType)
- {
- _keyboardReturnType = returnType;
- this->setNativeReturnType(returnType);
- }
-
- void EditBoxImplCommon::refreshInactiveText()
- {
- setInactiveText(_text.c_str());
- refreshLabelAlignment();
- if (!_editingMode) {
- if (_text.size() == 0) {
- _label->setVisible(false);
- _labelPlaceHolder->setVisible(true);
- } else {
- _label->setVisible(true);
- _labelPlaceHolder->setVisible(false);
- }
- }
- }
- void EditBoxImplCommon::refreshLabelAlignment()
- {
- _label->setHorizontalAlignment(_alignment);
- _labelPlaceHolder->setHorizontalAlignment(_alignment);
- }
- void EditBoxImplCommon::setText(const char* text)
- {
- if (nullptr != text) {
- this->setNativeText(text);
- _text = text;
- refreshInactiveText();
- }
- }
- void EditBoxImplCommon::setPlaceHolder(const char* pText)
- {
- if (pText != NULL)
- {
- _placeHolder = pText;
- this->setNativePlaceHolder(pText);
- _labelPlaceHolder->setString(_placeHolder);
- }
- }
- void EditBoxImplCommon::setVisible(bool visible)
- {
- if(visible) {
- refreshInactiveText();
- } else {
- this->setNativeVisible(visible);
- _label->setVisible(visible);
- _labelPlaceHolder->setVisible(visible);
- }
- }
- void EditBoxImplCommon::setContentSize(const Size& size)
- {
- _contentSize = applyPadding(size);
- CCLOG("[Edit text] content size = (%f, %f)", _contentSize.width, _contentSize.height);
- placeInactiveLabels(_contentSize);
- }
- void EditBoxImplCommon::draw(Renderer* /*renderer*/, const Mat4& /*transform*/, uint32_t flags)
- {
- if(flags)
- {
- auto rect = ui::Helper::convertBoundingBoxToScreen(_editBox);
- this->updateNativeFrame(rect);
- }
- }
- void EditBoxImplCommon::onEnter(void)
- {
- const char* pText = getText();
- if (pText) {
- setInactiveText(pText);
- }
- }
- void EditBoxImplCommon::openKeyboard()
- {
- _label->setVisible(false);
- _labelPlaceHolder->setVisible(false);
- _editingMode = true;
- this->setNativeVisible(true);
- this->nativeOpenKeyboard();
- }
- void EditBoxImplCommon::closeKeyboard()
- {
- this->nativeCloseKeyboard();
- _editingMode = false;
- }
- void EditBoxImplCommon::onEndEditing(const std::string& /*text*/)
- {
- _editingMode = false;
- this->setNativeVisible(false);
- refreshInactiveText();
- }
-
- void EditBoxImplCommon::editBoxEditingDidBegin()
- {
- // LOGD("textFieldShouldBeginEditing...");
- cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
-
- if (pDelegate != nullptr)
- {
- pDelegate->editBoxEditingDidBegin(_editBox);
- }
-
- #if CC_ENABLE_SCRIPT_BINDING
- if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
- {
- cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "began", _editBox);
- cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
- cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
- }
- #endif
- }
- void EditBoxImplCommon::editBoxEditingDidEnd(const std::string& text, EditBoxDelegate::EditBoxEndAction action)
- {
- // LOGD("textFieldShouldEndEditing...");
- _text = text;
-
- cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
- if (pDelegate != nullptr)
- {
- pDelegate->editBoxEditingDidEndWithAction(_editBox, action);
- #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
- #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- #elif _MSC_VER >= 1400 //vs 2005 or higher
- #pragma warning (push)
- #pragma warning (disable: 4996)
- #endif
- pDelegate->editBoxEditingDidEnd(_editBox);
- #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
- #pragma GCC diagnostic warning "-Wdeprecated-declarations"
- #elif _MSC_VER >= 1400 //vs 2005 or higher
- #pragma warning (pop)
- #endif
-
- pDelegate->editBoxReturn(_editBox);
- }
-
- #if CC_ENABLE_SCRIPT_BINDING
- if (_editBox != nullptr && 0 != _editBox->getScriptEditBoxHandler())
- {
- cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "ended", _editBox);
- cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
- cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
- memset(data.eventName, 0, sizeof(data.eventName));
- strncpy(data.eventName, "return", sizeof(data.eventName));
- event.data = (void *)&data;
- cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
- }
- #endif
-
- if (_editBox != nullptr)
- {
- this->onEndEditing(_text);
- }
- }
- void EditBoxImplCommon::editBoxEditingChanged(const std::string& text)
- {
- // LOGD("editBoxTextChanged...");
- cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
- _text = text;
- if (pDelegate != nullptr)
- {
- pDelegate->editBoxTextChanged(_editBox, text);
- }
-
- #if CC_ENABLE_SCRIPT_BINDING
- if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
- {
- cocos2d::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox);
- cocos2d::ScriptEvent event(cocos2d::kCommonEvent, (void *)&data);
- cocos2d::ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&event);
- }
- #endif
- }
- }
- NS_CC_END
|