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