2014-03-11 17:13:54 +08:00
|
|
|
/****************************************************************************
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2014-03-11 17:13:54 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "ui/UITextField.h"
|
2014-06-28 23:37:29 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2014-07-29 09:56:33 +08:00
|
|
|
#include "ui/UIHelper.h"
|
|
|
|
#include "base/ccUTF8.h"
|
2015-06-18 10:23:52 +08:00
|
|
|
#include "2d/CCCamera.h"
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
namespace ui {
|
|
|
|
|
2017-11-28 16:42:14 +08:00
|
|
|
UICCTextField * UICCTextField::create()
|
|
|
|
{
|
|
|
|
UICCTextField *ret = new (std::nothrow) UICCTextField();
|
|
|
|
|
|
|
|
if(ret)
|
|
|
|
ret->autorelease();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
UICCTextField::UICCTextField()
|
|
|
|
: _maxLengthEnabled(false)
|
|
|
|
, _maxLength(0)
|
|
|
|
, _attachWithIME(false)
|
|
|
|
, _detachWithIME(false)
|
|
|
|
, _insertText(false)
|
|
|
|
, _deleteBackward(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
UICCTextField::~UICCTextField()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
UICCTextField * UICCTextField::create(const std::string& placeholder, const std::string& fontName, float fontSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
UICCTextField *pRet = new (std::nothrow) UICCTextField();
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
if(pRet && pRet->initWithPlaceHolder("", fontName, fontSize))
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
pRet->autorelease();
|
2014-05-12 14:59:00 +08:00
|
|
|
if (!placeholder.empty())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
pRet->setPlaceHolder(placeholder);
|
|
|
|
}
|
|
|
|
return pRet;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(pRet);
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::onEnter()
|
|
|
|
{
|
2016-03-14 23:09:04 +08:00
|
|
|
TextFieldTTF::onEnter();
|
2014-03-11 17:13:54 +08:00
|
|
|
TextFieldTTF::setDelegate(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool UICCTextField::onTextFieldAttachWithIME(TextFieldTTF* /*pSender*/)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
setAttachWithIME(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool UICCTextField::onTextFieldInsertText(TextFieldTTF* /*pSender*/, const char *text, size_t nLen)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (nLen == 1 && strcmp(text, "\n") == 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
setInsertText(true);
|
|
|
|
if (_maxLengthEnabled)
|
|
|
|
{
|
2016-04-26 14:50:39 +08:00
|
|
|
if (static_cast<int>(TextFieldTTF::getCharCount()) >= _maxLength)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool UICCTextField::onTextFieldDeleteBackward(TextFieldTTF* /*pSender*/, const char* /*delText*/, size_t /*nLen*/)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
setDeleteBackward(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool UICCTextField::onTextFieldDetachWithIME(TextFieldTTF* /*pSender*/)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
setDetachWithIME(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
void UICCTextField::insertText(const char* text, size_t len)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
std::string input_text = text;
|
|
|
|
|
|
|
|
if (strcmp(text, "\n") != 0)
|
|
|
|
{
|
|
|
|
if (_maxLengthEnabled)
|
|
|
|
{
|
2021-09-02 13:39:28 +08:00
|
|
|
int32_t text_count = StringUtils::getCharacterCountInUTF8String(getString());
|
2014-03-11 17:13:54 +08:00
|
|
|
if (text_count >= _maxLength)
|
|
|
|
{
|
|
|
|
// password
|
2016-04-01 00:52:55 +08:00
|
|
|
if (this->isSecureTextEntry())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2016-02-03 23:12:37 +08:00
|
|
|
setPasswordText(getString());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-02 13:39:28 +08:00
|
|
|
int32_t input_count = StringUtils::getCharacterCountInUTF8String(text);
|
|
|
|
int32_t total = text_count + input_count;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
if (total > _maxLength)
|
|
|
|
{
|
2021-09-02 13:39:28 +08:00
|
|
|
int32_t length = _maxLength - text_count;
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-07-30 10:45:35 +08:00
|
|
|
input_text = Helper::getSubStringOfUTF8String(input_text, 0, length);
|
2014-07-29 09:56:33 +08:00
|
|
|
len = input_text.length();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TextFieldTTF::insertText(input_text.c_str(), len);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::openIME()
|
|
|
|
{
|
|
|
|
TextFieldTTF::attachWithIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::closeIME()
|
|
|
|
{
|
|
|
|
TextFieldTTF::detachWithIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setMaxLengthEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_maxLengthEnabled = enable;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool UICCTextField::isMaxLengthEnabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _maxLengthEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setMaxLength(int length)
|
|
|
|
{
|
|
|
|
_maxLength = length;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
int UICCTextField::getMaxLength()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _maxLength;
|
|
|
|
}
|
|
|
|
|
2016-04-01 09:31:18 +08:00
|
|
|
std::size_t UICCTextField::getCharCount()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return TextFieldTTF::getCharCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setPasswordEnabled(bool enable)
|
|
|
|
{
|
2016-03-30 18:19:28 +08:00
|
|
|
this->setSecureTextEntry(enable);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool UICCTextField::isPasswordEnabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2016-04-01 00:52:55 +08:00
|
|
|
return this->isSecureTextEntry();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
void UICCTextField::setPasswordStyleText(const std::string& styleText)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2016-04-01 00:52:55 +08:00
|
|
|
this->setPasswordTextStyle(styleText);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-12 14:59:00 +08:00
|
|
|
void UICCTextField::setPasswordText(const std::string& text)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-03-28 18:54:06 +08:00
|
|
|
std::string tempStr = "";
|
2021-09-02 13:39:28 +08:00
|
|
|
int32_t text_count = StringUtils::getCharacterCountInUTF8String(text);
|
|
|
|
int32_t max = text_count;
|
2014-03-28 18:54:06 +08:00
|
|
|
|
|
|
|
if (_maxLengthEnabled)
|
|
|
|
{
|
|
|
|
if (text_count > _maxLength)
|
|
|
|
{
|
|
|
|
max = _maxLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < max; ++i)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
tempStr.append(_passwordStyleText);
|
|
|
|
}
|
2014-03-28 18:54:06 +08:00
|
|
|
|
|
|
|
Label::setString(tempStr);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setAttachWithIME(bool attach)
|
|
|
|
{
|
|
|
|
_attachWithIME = attach;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool UICCTextField::getAttachWithIME()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _attachWithIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setDetachWithIME(bool detach)
|
|
|
|
{
|
|
|
|
_detachWithIME = detach;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool UICCTextField::getDetachWithIME()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _detachWithIME;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setInsertText(bool insert)
|
|
|
|
{
|
|
|
|
_insertText = insert;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool UICCTextField::getInsertText()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _insertText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UICCTextField::setDeleteBackward(bool deleteBackward)
|
|
|
|
{
|
|
|
|
_deleteBackward = deleteBackward;
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool UICCTextField::getDeleteBackward()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _deleteBackward;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const int TEXTFIELD_RENDERER_Z = (-1);
|
|
|
|
|
|
|
|
IMPLEMENT_CLASS_GUI_INFO(TextField)
|
|
|
|
|
|
|
|
|
|
|
|
TextField::TextField():
|
|
|
|
_textFieldRenderer(nullptr),
|
|
|
|
_touchWidth(0.0f),
|
|
|
|
_touchHeight(0.0f),
|
|
|
|
_useTouchArea(false),
|
|
|
|
_textFieldEventListener(nullptr),
|
2014-05-12 11:29:22 +08:00
|
|
|
_eventCallback(nullptr),
|
2014-06-28 23:37:29 +08:00
|
|
|
_textFieldRendererAdaptDirty(true),
|
|
|
|
_fontName("Thonburi"),
|
|
|
|
_fontSize(10),
|
|
|
|
_fontType(FontType::SYSTEM)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
TextField::~TextField()
|
|
|
|
{
|
|
|
|
_textFieldEventListener = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
TextField* TextField::create()
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextField* widget = new (std::nothrow) TextField();
|
2014-03-11 17:13:54 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-02 18:35:08 +08:00
|
|
|
TextField* TextField::create(const std::string &placeholder, const std::string &fontName, int fontSize)
|
|
|
|
{
|
2014-08-28 07:31:57 +08:00
|
|
|
TextField* widget = new (std::nothrow) TextField();
|
2014-04-02 18:35:08 +08:00
|
|
|
if (widget && widget->init())
|
|
|
|
{
|
|
|
|
widget->setFontName(fontName);
|
|
|
|
widget->setFontSize(fontSize);
|
2015-01-07 17:21:41 +08:00
|
|
|
widget->setPlaceHolder(placeholder);
|
2014-04-02 18:35:08 +08:00
|
|
|
widget->autorelease();
|
|
|
|
return widget;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(widget);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
bool TextField::init()
|
|
|
|
{
|
|
|
|
if (Widget::init())
|
|
|
|
{
|
|
|
|
setTouchEnabled(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::onEnter()
|
|
|
|
{
|
|
|
|
Widget::onEnter();
|
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::initRenderer()
|
|
|
|
{
|
2017-11-28 16:42:14 +08:00
|
|
|
_textFieldRenderer = UICCTextField::create();
|
2014-03-24 15:25:44 +08:00
|
|
|
addProtectedChild(_textFieldRenderer, TEXTFIELD_RENDERER_Z, -1);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setTouchSize(const Size &size)
|
|
|
|
{
|
|
|
|
_touchWidth = size.width;
|
|
|
|
_touchHeight = size.height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setTouchAreaEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_useTouchArea = enable;
|
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
bool TextField::hitTest(const Vec2 &pt, const Camera* camera, Vec3* /*p*/) const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
if (false == _useTouchArea)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-06-18 10:23:52 +08:00
|
|
|
return Widget::hitTest(pt, camera, nullptr);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-06-18 10:23:52 +08:00
|
|
|
|
2015-07-27 16:59:35 +08:00
|
|
|
auto size = getContentSize();
|
|
|
|
auto anch = getAnchorPoint();
|
|
|
|
Rect rect((size.width - _touchWidth) * anch.x, (size.height - _touchHeight) * anch.y, _touchWidth, _touchHeight);
|
2015-06-18 10:23:52 +08:00
|
|
|
return isScreenPointInRect(pt, camera, getWorldToNodeTransform(), rect, nullptr);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-06-18 10:23:52 +08:00
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
Size TextField::getTouchSize()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return Size(_touchWidth, _touchHeight);
|
|
|
|
}
|
|
|
|
|
2014-09-09 13:59:35 +08:00
|
|
|
void TextField::setString(const std::string& text)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
std::string strText(text);
|
2014-03-28 18:54:06 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (isMaxLengthEnabled())
|
|
|
|
{
|
2014-03-28 18:54:06 +08:00
|
|
|
int max = _textFieldRenderer->getMaxLength();
|
2021-09-02 13:39:28 +08:00
|
|
|
int32_t text_count = StringUtils::getCharacterCountInUTF8String(text);
|
2015-07-27 17:25:48 +08:00
|
|
|
if (text_count > max)
|
2014-03-28 18:54:06 +08:00
|
|
|
{
|
2014-07-30 10:45:35 +08:00
|
|
|
strText = Helper::getSubStringOfUTF8String(strText, 0, max);
|
2014-03-28 18:54:06 +08:00
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-03-28 18:54:06 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
if (isPasswordEnabled())
|
|
|
|
{
|
2016-05-04 03:18:25 +08:00
|
|
|
_textFieldRenderer->setPasswordText(strText);
|
2014-03-11 17:13:54 +08:00
|
|
|
_textFieldRenderer->setString("");
|
2016-05-04 03:18:25 +08:00
|
|
|
_textFieldRenderer->insertText(strText.c_str(), strText.size());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-05-04 03:18:25 +08:00
|
|
|
_textFieldRenderer->setString(strText);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setPlaceHolder(const std::string& value)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setPlaceHolder(value);
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
const std::string& TextField::getPlaceHolder()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getPlaceHolder();
|
|
|
|
}
|
2014-08-13 11:28:37 +08:00
|
|
|
|
|
|
|
const Color4B& TextField::getPlaceHolderColor()const
|
|
|
|
{
|
|
|
|
return _textFieldRenderer->getColorSpaceHolder();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setPlaceHolderColor(const cocos2d::Color3B &color)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setColorSpaceHolder(color);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setPlaceHolderColor(const cocos2d::Color4B &color)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setColorSpaceHolder(color);
|
|
|
|
}
|
2015-05-26 03:06:27 +08:00
|
|
|
|
|
|
|
const Color4B& TextField::getTextColor()const
|
|
|
|
{
|
|
|
|
return _textFieldRenderer->getTextColor();
|
|
|
|
}
|
|
|
|
|
2014-08-13 11:28:37 +08:00
|
|
|
void TextField::setTextColor(const cocos2d::Color4B &textColor)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setTextColor(textColor);
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void TextField::setFontSize(int size)
|
|
|
|
{
|
2014-12-16 16:44:04 +08:00
|
|
|
if (_fontType == FontType::SYSTEM)
|
|
|
|
{
|
2014-06-28 23:37:29 +08:00
|
|
|
_textFieldRenderer->setSystemFontSize(size);
|
2014-12-16 16:44:04 +08:00
|
|
|
}
|
2017-12-29 11:24:02 +08:00
|
|
|
else if (_fontType == FontType::BMFONT) {
|
|
|
|
_textFieldRenderer->setBMFontSize(size);
|
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
else
|
|
|
|
{
|
2014-06-28 23:37:29 +08:00
|
|
|
TTFConfig config = _textFieldRenderer->getTTFConfig();
|
|
|
|
config.fontSize = size;
|
|
|
|
_textFieldRenderer->setTTFConfig(config);
|
|
|
|
}
|
|
|
|
_fontSize = size;
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
int TextField::getFontSize()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-28 23:37:29 +08:00
|
|
|
return _fontSize;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setFontName(const std::string& name)
|
|
|
|
{
|
2014-06-28 23:37:29 +08:00
|
|
|
if(FileUtils::getInstance()->isFileExist(name))
|
|
|
|
{
|
2017-12-29 11:24:02 +08:00
|
|
|
std::string lcName = name;
|
|
|
|
std::transform(lcName.begin(), lcName.end(), lcName.begin(), ::tolower);
|
|
|
|
if(lcName.substr(lcName.length() - 4) == ".fnt") {
|
|
|
|
_textFieldRenderer->setBMFontFilePath(name);
|
|
|
|
_fontType = FontType::BMFONT;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
TTFConfig config = _textFieldRenderer->getTTFConfig();
|
|
|
|
config.fontFilePath = name;
|
|
|
|
config.fontSize = _fontSize;
|
|
|
|
_textFieldRenderer->setTTFConfig(config);
|
|
|
|
_fontType = FontType::TTF;
|
|
|
|
}
|
2014-12-16 16:44:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-28 23:37:29 +08:00
|
|
|
_textFieldRenderer->setSystemFontName(name);
|
2014-10-09 18:28:09 +08:00
|
|
|
if (_fontType == FontType::TTF)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->requestSystemFontRefresh();
|
|
|
|
}
|
2014-06-28 23:37:29 +08:00
|
|
|
_fontType = FontType::SYSTEM;
|
|
|
|
}
|
|
|
|
_fontName = name;
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
const std::string& TextField::getFontName()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-28 23:37:29 +08:00
|
|
|
return _fontName;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::didNotSelectSelf()
|
|
|
|
{
|
|
|
|
_textFieldRenderer->detachWithIME();
|
|
|
|
}
|
|
|
|
|
2014-09-09 13:59:35 +08:00
|
|
|
const std::string& TextField::getString()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getString();
|
|
|
|
}
|
2014-06-05 17:59:54 +08:00
|
|
|
|
2014-06-06 17:06:55 +08:00
|
|
|
int TextField::getStringLength() const {
|
2014-06-05 17:59:54 +08:00
|
|
|
return _textFieldRenderer->getStringLength();
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
bool TextField::onTouchBegan(Touch *touch, Event *unusedEvent)
|
|
|
|
{
|
|
|
|
bool pass = Widget::onTouchBegan(touch, unusedEvent);
|
|
|
|
if (_hitted)
|
|
|
|
{
|
2015-09-30 17:39:44 +08:00
|
|
|
if (isFocusEnabled())
|
|
|
|
{
|
|
|
|
requestFocus();
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
_textFieldRenderer->attachWithIME();
|
2014-12-16 16:44:04 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-06-10 14:53:27 +08:00
|
|
|
this->didNotSelectSelf();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
return pass;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setMaxLengthEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setMaxLengthEnabled(enable);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool TextField::isMaxLengthEnabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->isMaxLengthEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setMaxLength(int length)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setMaxLength(length);
|
|
|
|
|
2014-09-09 13:59:35 +08:00
|
|
|
setString(getString());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
int TextField::getMaxLength()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getMaxLength();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setPasswordEnabled(bool enable)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setPasswordEnabled(enable);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool TextField::isPasswordEnabled()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->isPasswordEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setPasswordStyleText(const char *styleText)
|
|
|
|
{
|
2014-07-01 10:57:12 +08:00
|
|
|
_textFieldRenderer->setPasswordStyleText(styleText);
|
2014-03-11 17:13:54 +08:00
|
|
|
|
2014-09-09 13:59:35 +08:00
|
|
|
setString(getString());
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
const char* TextField::getPasswordStyleText()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2016-04-01 00:52:55 +08:00
|
|
|
return _textFieldRenderer->getPasswordTextStyle().c_str();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2016-11-16 09:48:37 +08:00
|
|
|
void TextField::update(float /*dt*/)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
if (getDetachWithIME())
|
|
|
|
{
|
|
|
|
detachWithIMEEvent();
|
|
|
|
setDetachWithIME(false);
|
|
|
|
}
|
2015-01-26 18:26:59 +08:00
|
|
|
|
2015-01-12 10:18:55 +08:00
|
|
|
if (getAttachWithIME())
|
|
|
|
{
|
|
|
|
attachWithIMEEvent();
|
|
|
|
setAttachWithIME(false);
|
|
|
|
}
|
2015-08-12 11:05:07 +08:00
|
|
|
|
|
|
|
if (getDeleteBackward())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
|
2015-08-12 11:05:07 +08:00
|
|
|
|
|
|
|
deleteBackwardEvent();
|
|
|
|
setDeleteBackward(false);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2015-08-12 11:05:07 +08:00
|
|
|
|
|
|
|
if (getInsertText())
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2015-08-12 11:05:07 +08:00
|
|
|
//we update the content size first such that when user call getContentSize() in event callback won't be wrong
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
updateContentSizeWithTextureSize(_textFieldRenderer->getContentSize());
|
2015-01-26 18:26:59 +08:00
|
|
|
|
2015-08-12 11:05:07 +08:00
|
|
|
insertTextEvent();
|
|
|
|
setInsertText(false);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool TextField::getAttachWithIME()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getAttachWithIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setAttachWithIME(bool attach)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setAttachWithIME(attach);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool TextField::getDetachWithIME()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getDetachWithIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setDetachWithIME(bool detach)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setDetachWithIME(detach);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool TextField::getInsertText()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getInsertText();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setInsertText(bool insertText)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setInsertText(insertText);
|
|
|
|
}
|
|
|
|
|
2014-05-27 11:03:38 +08:00
|
|
|
bool TextField::getDeleteBackward()const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getDeleteBackward();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setDeleteBackward(bool deleteBackward)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setDeleteBackward(deleteBackward);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::attachWithIMEEvent()
|
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-05-12 11:29:22 +08:00
|
|
|
if (_eventCallback) {
|
|
|
|
_eventCallback(this, EventType::ATTACH_WITH_IME);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::ATTACH_WITH_IME));
|
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::detachWithIMEEvent()
|
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-12-16 16:44:04 +08:00
|
|
|
if (_eventCallback)
|
|
|
|
{
|
2014-05-12 11:29:22 +08:00
|
|
|
_eventCallback(this, EventType::DETACH_WITH_IME);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::DETACH_WITH_IME));
|
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::insertTextEvent()
|
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-12-16 16:44:04 +08:00
|
|
|
if (_eventCallback)
|
|
|
|
{
|
2014-05-12 11:29:22 +08:00
|
|
|
_eventCallback(this, EventType::INSERT_TEXT);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::INSERT_TEXT));
|
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::deleteBackwardEvent()
|
|
|
|
{
|
2014-08-01 20:58:42 +08:00
|
|
|
this->retain();
|
2014-12-16 16:44:04 +08:00
|
|
|
if (_eventCallback)
|
|
|
|
{
|
2014-05-12 11:29:22 +08:00
|
|
|
_eventCallback(this, EventType::DELETE_BACKWARD);
|
|
|
|
}
|
2014-11-27 16:49:19 +08:00
|
|
|
if (_ccEventCallback)
|
|
|
|
{
|
|
|
|
_ccEventCallback(this, static_cast<int>(EventType::DELETE_BACKWARD));
|
|
|
|
}
|
2014-08-01 20:58:42 +08:00
|
|
|
this->release();
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-05-12 11:29:22 +08:00
|
|
|
|
2014-05-14 16:14:28 +08:00
|
|
|
void TextField::addEventListener(const ccTextFieldCallback& callback)
|
2014-05-12 11:29:22 +08:00
|
|
|
{
|
|
|
|
_eventCallback = callback;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
|
|
|
|
void TextField::onSizeChanged()
|
|
|
|
{
|
|
|
|
Widget::onSizeChanged();
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRendererAdaptDirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::adaptRenderers()
|
|
|
|
{
|
|
|
|
if (_textFieldRendererAdaptDirty)
|
|
|
|
{
|
|
|
|
textfieldRendererScaleChangedWithSize();
|
|
|
|
_textFieldRendererAdaptDirty = false;
|
|
|
|
}
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::textfieldRendererScaleChangedWithSize()
|
|
|
|
{
|
2014-07-17 16:23:38 +08:00
|
|
|
if (!_ignoreSize)
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
2014-06-20 10:40:16 +08:00
|
|
|
_textFieldRenderer->setDimensions(_contentSize.width, _contentSize.height);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
2014-04-17 14:08:25 +08:00
|
|
|
_textFieldRenderer->setPosition(_contentSize.width / 2.0f, _contentSize.height / 2.0f);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
2015-01-20 18:40:25 +08:00
|
|
|
Size TextField::getAutoRenderSize()
|
|
|
|
{
|
|
|
|
Size virtualSize = _textFieldRenderer->getContentSize();
|
|
|
|
if (!_ignoreSize)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setDimensions(0, 0);
|
|
|
|
virtualSize = _textFieldRenderer->getContentSize();
|
|
|
|
_textFieldRenderer->setDimensions(_contentSize.width, _contentSize.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
return virtualSize;
|
|
|
|
}
|
|
|
|
|
2014-09-12 14:39:22 +08:00
|
|
|
Size TextField::getVirtualRendererSize() const
|
2014-03-11 17:13:54 +08:00
|
|
|
{
|
|
|
|
return _textFieldRenderer->getContentSize();
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* TextField::getVirtualRenderer()
|
|
|
|
{
|
|
|
|
return _textFieldRenderer;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string TextField::getDescription() const
|
|
|
|
{
|
|
|
|
return "TextField";
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::attachWithIME()
|
|
|
|
{
|
|
|
|
_textFieldRenderer->attachWithIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget* TextField::createCloneInstance()
|
|
|
|
{
|
|
|
|
return TextField::create();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::copySpecialProperties(Widget *widget)
|
|
|
|
{
|
|
|
|
TextField* textField = dynamic_cast<TextField*>(widget);
|
|
|
|
if (textField)
|
|
|
|
{
|
2014-09-09 13:59:35 +08:00
|
|
|
setString(textField->_textFieldRenderer->getString());
|
|
|
|
setPlaceHolder(textField->getString());
|
2014-06-28 23:37:29 +08:00
|
|
|
setFontSize(textField->_fontSize);
|
|
|
|
setFontName(textField->_fontName);
|
2014-03-11 17:13:54 +08:00
|
|
|
setMaxLengthEnabled(textField->isMaxLengthEnabled());
|
|
|
|
setMaxLength(textField->getMaxLength());
|
|
|
|
setPasswordEnabled(textField->isPasswordEnabled());
|
2016-04-01 00:52:55 +08:00
|
|
|
setPasswordStyleText(textField->getPasswordStyleText());
|
2014-03-11 17:13:54 +08:00
|
|
|
setAttachWithIME(textField->getAttachWithIME());
|
|
|
|
setDetachWithIME(textField->getDetachWithIME());
|
|
|
|
setInsertText(textField->getInsertText());
|
|
|
|
setDeleteBackward(textField->getDeleteBackward());
|
2014-05-23 15:03:46 +08:00
|
|
|
_eventCallback = textField->_eventCallback;
|
2014-11-27 16:49:19 +08:00
|
|
|
_ccEventCallback = textField->_ccEventCallback;
|
2014-05-23 15:03:46 +08:00
|
|
|
_textFieldEventListener = textField->_textFieldEventListener;
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setTextAreaSize(const Size &size)
|
|
|
|
{
|
2014-07-17 16:23:38 +08:00
|
|
|
this->setContentSize(size);
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setTextHorizontalAlignment(TextHAlignment alignment)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setHorizontalAlignment(alignment);
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:34:13 +08:00
|
|
|
TextHAlignment TextField::getTextHorizontalAlignment() const
|
|
|
|
{
|
|
|
|
return _textFieldRenderer->getHorizontalAlignment();
|
|
|
|
}
|
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
void TextField::setTextVerticalAlignment(TextVAlignment alignment)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setVerticalAlignment(alignment);
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:34:13 +08:00
|
|
|
TextVAlignment TextField::getTextVerticalAlignment() const
|
|
|
|
{
|
|
|
|
return _textFieldRenderer->getVerticalAlignment();
|
|
|
|
}
|
2016-03-14 23:09:04 +08:00
|
|
|
|
|
|
|
void TextField::setCursorEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setCursorEnabled(enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setCursorChar(char cursor)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setCursorChar(cursor);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setCursorPosition(std::size_t cursorPosition)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setCursorPosition(cursorPosition);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextField::setCursorFromPoint(const Vec2 &point, const Camera* camera)
|
|
|
|
{
|
|
|
|
_textFieldRenderer->setCursorFromPoint(point, camera);
|
|
|
|
}
|
|
|
|
|
2015-05-26 15:34:13 +08:00
|
|
|
|
2014-03-11 17:13:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|