2015-08-31 18:36:27 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2012 James Chen
|
2015-09-14 11:07:51 +08:00
|
|
|
Copyright (c) 2013-2015 zilongshanren
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/UIEditBox/UIEditBoxImpl-common.h"
|
2015-08-31 18:36:27 +08:00
|
|
|
|
|
|
|
#define kLabelZOrder 9999
|
|
|
|
|
2016-03-20 21:53:44 +08:00
|
|
|
#include "ui/UIEditBox/UIEditBox.h"
|
2015-08-31 18:36:27 +08:00
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "2d/CCLabel.h"
|
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
|
|
|
|
static const int CC_EDIT_BOX_PADDING = 5;
|
|
|
|
|
2016-04-25 11:17:09 +08:00
|
|
|
#if CC_TARGET_PLATFORM == CC_PLATFORM_WINRT
|
|
|
|
#define PASSWORD_CHAR "*"
|
|
|
|
#else
|
|
|
|
#define PASSWORD_CHAR "\u25CF"
|
|
|
|
#endif
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
|
|
|
EditBoxImplCommon::EditBoxImplCommon(EditBox* pEditText)
|
|
|
|
: EditBoxImpl(pEditText)
|
|
|
|
, _label(nullptr)
|
|
|
|
, _labelPlaceHolder(nullptr)
|
|
|
|
, _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
|
2015-10-12 12:54:06 +08:00
|
|
|
, _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
|
2015-08-31 18:36:27 +08:00
|
|
|
, _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
|
|
|
|
, _colText(Color3B::WHITE)
|
|
|
|
, _colPlaceHolder(Color3B::GRAY)
|
|
|
|
, _maxLength(-1)
|
2016-11-09 10:38:00 +08:00
|
|
|
, _alignment(TextHAlignment::LEFT)
|
2015-08-31 18:36:27 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
EditBoxImplCommon::~EditBoxImplCommon()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool EditBoxImplCommon::initWithSize(const Size& size)
|
|
|
|
{
|
|
|
|
do
|
|
|
|
{
|
|
|
|
|
2015-09-02 11:04:50 +08:00
|
|
|
Rect rect = Rect(0, 0, size.width, size.height);
|
2015-08-31 18:36:27 +08:00
|
|
|
|
|
|
|
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();
|
2016-12-07 09:21:17 +08:00
|
|
|
_label->setAnchorPoint(Vec2(0,1));
|
|
|
|
_label->setOverflow(Label::Overflow::CLAMP);
|
2015-08-31 18:36:27 +08:00
|
|
|
_label->setVisible(false);
|
|
|
|
_editBox->addChild(_label, kLabelZOrder);
|
|
|
|
|
|
|
|
_labelPlaceHolder = Label::create();
|
2016-12-07 09:21:17 +08:00
|
|
|
_labelPlaceHolder->setAnchorPoint(Vec2(0, 1.0f));
|
2015-08-31 18:36:27 +08:00
|
|
|
_labelPlaceHolder->setColor(Color3B::GRAY);
|
2016-12-07 09:21:17 +08:00
|
|
|
_labelPlaceHolder->enableWrap(false);
|
2015-08-31 18:36:27 +08:00
|
|
|
_editBox->addChild(_labelPlaceHolder, kLabelZOrder);
|
|
|
|
|
|
|
|
setFont(pDefaultFontName, size.height*2/3);
|
|
|
|
setPlaceholderFont(pDefaultFontName, size.height*2/3);
|
|
|
|
}
|
|
|
|
|
2016-12-07 09:21:17 +08:00
|
|
|
void EditBoxImplCommon::placeInactiveLabels(const Size& size)
|
2015-08-31 18:36:27 +08:00
|
|
|
{
|
2016-12-07 09:21:17 +08:00
|
|
|
_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);
|
|
|
|
}
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setInactiveText(const char* pText)
|
|
|
|
{
|
|
|
|
if(EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
|
|
|
|
{
|
|
|
|
std::string passwordString;
|
2016-04-26 14:50:39 +08:00
|
|
|
for(size_t i = 0; i < strlen(pText); ++i)
|
2016-04-25 11:17:09 +08:00
|
|
|
passwordString.append(PASSWORD_CHAR);
|
2016-02-03 23:12:37 +08:00
|
|
|
_label->setString(passwordString);
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_label->setString(pText);
|
|
|
|
}
|
|
|
|
// Clip the text width to fit to the text box
|
|
|
|
float fMaxWidth = _editBox->getContentSize().width;
|
|
|
|
float fMaxHeight = _editBox->getContentSize().height;
|
|
|
|
Size labelSize = _label->getContentSize();
|
|
|
|
if(labelSize.width > fMaxWidth || labelSize.height > fMaxHeight)
|
|
|
|
{
|
|
|
|
_label->setDimensions(fMaxWidth, fMaxHeight);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2015-11-17 00:53:23 +08:00
|
|
|
this->setNativeFont(pFontName, fontSize * _label->getNodeToWorldAffineTransform().a);
|
2015-08-31 18:36:27 +08:00
|
|
|
|
|
|
|
if(strlen(pFontName) > 0)
|
|
|
|
{
|
|
|
|
_label->setSystemFontName(pFontName);
|
|
|
|
}
|
|
|
|
if(fontSize > 0)
|
|
|
|
{
|
|
|
|
_label->setSystemFontSize(fontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setFontColor(const Color4B& color)
|
|
|
|
{
|
|
|
|
this->setNativeFontColor(color);
|
|
|
|
|
|
|
|
_label->setTextColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setPlaceholderFont(const char* pFontName, int fontSize)
|
|
|
|
{
|
2015-11-17 00:53:23 +08:00
|
|
|
this->setNativePlaceholderFont(pFontName, fontSize * _labelPlaceHolder->getNodeToWorldAffineTransform().a);
|
2015-08-31 18:36:27 +08:00
|
|
|
|
|
|
|
if( strlen(pFontName) > 0)
|
|
|
|
{
|
|
|
|
_labelPlaceHolder->setSystemFontName(pFontName);
|
|
|
|
}
|
|
|
|
if(fontSize > 0)
|
|
|
|
{
|
|
|
|
_labelPlaceHolder->setSystemFontSize(fontSize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setPlaceholderFontColor(const Color4B &color)
|
|
|
|
{
|
|
|
|
this->setNativePlaceholderFontColor(color);
|
|
|
|
|
|
|
|
_labelPlaceHolder->setTextColor(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setInputMode(EditBox::InputMode inputMode)
|
|
|
|
{
|
|
|
|
_editBoxInputMode = inputMode;
|
|
|
|
this->setNativeInputMode(inputMode);
|
2016-12-07 09:21:17 +08:00
|
|
|
this->placeInactiveLabels(_editBox->getContentSize());
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setMaxLength(int maxLength)
|
|
|
|
{
|
|
|
|
_maxLength = maxLength;
|
|
|
|
this->setNativeMaxLength(maxLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
int EditBoxImplCommon::getMaxLength()
|
|
|
|
{
|
|
|
|
return _maxLength;
|
|
|
|
}
|
|
|
|
|
2016-11-09 10:38:00 +08:00
|
|
|
void EditBoxImplCommon::setTextHorizontalAlignment(cocos2d::TextHAlignment alignment)
|
|
|
|
{
|
|
|
|
_alignment = alignment;
|
|
|
|
this->setNativeTextHorizontalAlignment(alignment);
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
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());
|
2016-11-09 10:38:00 +08:00
|
|
|
|
|
|
|
refreshLabelAlignment();
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
if(_text.size() == 0)
|
|
|
|
{
|
|
|
|
_label->setVisible(false);
|
|
|
|
_labelPlaceHolder->setVisible(true);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_label->setVisible(true);
|
|
|
|
_labelPlaceHolder->setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-09 10:38:00 +08:00
|
|
|
void EditBoxImplCommon::refreshLabelAlignment()
|
|
|
|
{
|
|
|
|
_label->setHorizontalAlignment(_alignment);
|
|
|
|
_labelPlaceHolder->setHorizontalAlignment(_alignment);
|
|
|
|
}
|
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
void EditBoxImplCommon::setText(const char* text)
|
|
|
|
{
|
|
|
|
this->setNativeText(text);
|
|
|
|
_text = text;
|
|
|
|
refreshInactiveText();
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* EditBoxImplCommon::getText(void)
|
|
|
|
{
|
|
|
|
return _text.c_str();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setPlaceHolder(const char* pText)
|
|
|
|
{
|
|
|
|
if (pText != NULL)
|
|
|
|
{
|
|
|
|
_placeHolder = pText;
|
2016-02-03 23:12:37 +08:00
|
|
|
_labelPlaceHolder->setString(_placeHolder);
|
2016-06-23 16:09:19 +08:00
|
|
|
|
2015-08-31 18:36:27 +08:00
|
|
|
this->setNativePlaceHolder(pText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setVisible(bool visible)
|
|
|
|
{
|
2016-06-23 16:09:19 +08:00
|
|
|
if(visible) {
|
|
|
|
refreshInactiveText();
|
|
|
|
} else {
|
|
|
|
this->setNativeVisible(visible);
|
|
|
|
_label->setVisible(visible);
|
|
|
|
_labelPlaceHolder->setVisible(visible);
|
|
|
|
}
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::setContentSize(const Size& size)
|
|
|
|
{
|
|
|
|
_contentSize = size;
|
|
|
|
CCLOG("[Edit text] content size = (%f, %f)", size.width, size.height);
|
2016-12-07 09:21:17 +08:00
|
|
|
placeInactiveLabels(size);
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void EditBoxImplCommon::draw(Renderer* /*renderer*/, const Mat4& /*transform*/, uint32_t flags)
|
2015-08-31 18:36:27 +08:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
|
2016-06-23 16:09:19 +08:00
|
|
|
this->setNativeVisible(true);
|
2015-08-31 18:36:27 +08:00
|
|
|
this->nativeOpenKeyboard();
|
|
|
|
}
|
|
|
|
|
|
|
|
void EditBoxImplCommon::closeKeyboard()
|
|
|
|
{
|
|
|
|
this->nativeCloseKeyboard();
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void EditBoxImplCommon::onEndEditing(const std::string& /*text*/)
|
2015-08-31 18:36:27 +08:00
|
|
|
{
|
|
|
|
this->setNativeVisible(false);
|
|
|
|
|
2016-06-23 16:09:19 +08:00
|
|
|
refreshInactiveText();
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-11-03 14:02:53 +08:00
|
|
|
void EditBoxImplCommon::editBoxEditingDidEnd(const std::string& text, EditBoxDelegate::EditBoxEndAction action)
|
2015-08-31 18:36:27 +08:00
|
|
|
{
|
|
|
|
// LOGD("textFieldShouldEndEditing...");
|
|
|
|
_text = text;
|
|
|
|
|
|
|
|
cocos2d::ui::EditBoxDelegate *pDelegate = _editBox->getDelegate();
|
|
|
|
if (pDelegate != nullptr)
|
|
|
|
{
|
2016-11-03 14:02:53 +08:00
|
|
|
pDelegate->editBoxEditingDidEndWithAction(_editBox, action);
|
2015-08-31 18:36:27 +08:00
|
|
|
pDelegate->editBoxEditingDidEnd(_editBox);
|
|
|
|
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)
|
|
|
|
{
|
2015-11-18 15:05:13 +08:00
|
|
|
this->onEndEditing(_text);
|
2015-08-31 18:36:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|