2019-11-23 20:27:39 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
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 "2d/CCTextFieldTTF.h"
|
|
|
|
|
|
|
|
#include "base/CCDirector.h"
|
|
|
|
#include "platform/CCFileUtils.h"
|
|
|
|
#include "base/ccUTF8.h"
|
|
|
|
#include "2d/CCSprite.h"
|
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
#define CURSOR_TIME_SHOW_HIDE 0.5f
|
|
|
|
#define CURSOR_DEFAULT_CHAR '|'
|
|
|
|
#define PASSWORD_STYLE_TEXT_DEFAULT "\xe2\x80\xa2"
|
2021-12-25 10:04:45 +08:00
|
|
|
static std::size_t _calcCharCount(const char* text)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
int n = 0;
|
2019-11-23 20:27:39 +08:00
|
|
|
char ch = 0;
|
|
|
|
while ((ch = *text))
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
CC_BREAK_IF(!ch);
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
if (0x80 != (0xC0 & ch))
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
}
|
|
|
|
++text;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldAttachWithIME(TextFieldTTF* /*sender*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldDetachWithIME(TextFieldTTF* /*sender*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldInsertText(TextFieldTTF* /*sender*/, const char* /*text*/, size_t /*nLen*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldDelegate::onTextFieldDeleteBackward(TextFieldTTF* /*sender*/, const char* /*delText*/, size_t /*nLen*/)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool TextFieldDelegate::onVisit(TextFieldTTF* /*sender*/,
|
|
|
|
Renderer* /*renderer*/,
|
|
|
|
const Mat4& /*transform*/,
|
|
|
|
uint32_t /*flags*/)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// constructor and destructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
TextFieldTTF::TextFieldTTF()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _delegate(0)
|
|
|
|
, _charCount(0)
|
|
|
|
, _inputText("")
|
|
|
|
, _placeHolder("") // prevent Label initWithString assertion
|
|
|
|
, _colorText(Color4B::WHITE)
|
|
|
|
, _secureTextEntry(false)
|
|
|
|
, _passwordStyleText(PASSWORD_STYLE_TEXT_DEFAULT)
|
|
|
|
, _cursorEnabled(false)
|
|
|
|
, _cursorPosition(0)
|
|
|
|
, _cursorChar(CURSOR_DEFAULT_CHAR)
|
|
|
|
, _cursorShowingTime(0.0f)
|
|
|
|
, _isAttachWithIME(false)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_colorSpaceHolder.r = _colorSpaceHolder.g = _colorSpaceHolder.b = 127;
|
2021-12-25 10:04:45 +08:00
|
|
|
_colorSpaceHolder.a = 255;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextFieldTTF::~TextFieldTTF() {}
|
2019-11-23 20:27:39 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// static constructor
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
TextFieldTTF* TextFieldTTF::textFieldWithPlaceHolder(std::string_view placeholder,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& dimensions,
|
|
|
|
TextHAlignment alignment,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
TextFieldTTF* ret = new TextFieldTTF();
|
|
|
|
if (ret->initWithPlaceHolder("", dimensions, alignment, fontName, fontSize))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (placeholder.size() > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->setPlaceHolder(placeholder);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
TextFieldTTF* TextFieldTTF::textFieldWithPlaceHolder(std::string_view placeholder,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
TextFieldTTF* ret = new TextFieldTTF();
|
|
|
|
if (ret->initWithPlaceHolder("", fontName, fontSize))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->autorelease();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (placeholder.size() > 0)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
ret->setPlaceHolder(placeholder);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(ret);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// initialize
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool TextFieldTTF::initWithPlaceHolder(std::string_view placeholder,
|
2021-12-25 10:04:45 +08:00
|
|
|
const Vec2& dimensions,
|
|
|
|
TextHAlignment alignment,
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
setDimensions(dimensions.width, dimensions.height);
|
|
|
|
setAlignment(alignment, TextVAlignment::CENTER);
|
|
|
|
|
|
|
|
return initWithPlaceHolder(placeholder, fontName, fontSize);
|
|
|
|
}
|
2021-12-26 23:26:34 +08:00
|
|
|
bool TextFieldTTF::initWithPlaceHolder(std::string_view placeholder, std::string_view fontName, float fontSize)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_placeHolder = placeholder;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
do
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// If fontName is ttf file and it corrected, use TTFConfig
|
|
|
|
if (FileUtils::getInstance()->isFileExist(fontName))
|
|
|
|
{
|
|
|
|
TTFConfig ttfConfig(fontName, fontSize, GlyphCollection::DYNAMIC);
|
|
|
|
if (setTTFConfig(ttfConfig))
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setSystemFontName(fontName);
|
|
|
|
setSystemFontSize(fontSize);
|
|
|
|
|
|
|
|
} while (false);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
|
|
|
Label::setString(_placeHolder);
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || \
|
|
|
|
CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
2019-11-23 20:27:39 +08:00
|
|
|
// On desktop default enable cursor
|
|
|
|
if (_currentLabelType == LabelType::TTF)
|
|
|
|
{
|
|
|
|
setCursorEnabled(true);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// IMEDelegate
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
bool TextFieldTTF::attachWithIME()
|
|
|
|
{
|
|
|
|
bool ret = IMEDelegate::attachWithIME();
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
// open keyboard
|
2021-04-22 22:01:47 +08:00
|
|
|
auto pGlView = _director->getOpenGLView();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (pGlView)
|
|
|
|
{
|
|
|
|
pGlView->setIMEKeyboardState(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldTTF::detachWithIME()
|
|
|
|
{
|
|
|
|
bool ret = IMEDelegate::detachWithIME();
|
|
|
|
if (ret)
|
|
|
|
{
|
|
|
|
// close keyboard
|
2021-04-22 22:01:47 +08:00
|
|
|
auto glView = _director->getOpenGLView();
|
2019-11-23 20:27:39 +08:00
|
|
|
if (glView)
|
|
|
|
{
|
|
|
|
glView->setIMEKeyboardState(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::didAttachWithIME()
|
|
|
|
{
|
|
|
|
setAttachWithIME(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::didDetachWithIME()
|
|
|
|
{
|
|
|
|
setAttachWithIME(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldTTF::canAttachWithIME()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (_delegate) ? (!_delegate->onTextFieldAttachWithIME(this)) : true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldTTF::canDetachWithIME()
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (_delegate) ? (!_delegate->onTextFieldDetachWithIME(this)) : true;
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TextFieldTTF::insertText(const char* text, size_t len)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string insert(text, len);
|
|
|
|
|
|
|
|
// insert \n means input end
|
|
|
|
int pos = static_cast<int>(insert.find(StringUtils::AsciiCharacters::NewLine));
|
|
|
|
if ((int)insert.npos != pos)
|
|
|
|
{
|
|
|
|
len = pos;
|
|
|
|
insert.erase(pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
|
|
|
|
{
|
|
|
|
// delegate doesn't want to insert text
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::size_t countInsertChar = _calcCharCount(insert.c_str());
|
|
|
|
_charCount += countInsertChar;
|
|
|
|
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
|
|
|
stringUTF8.replace(_inputText);
|
|
|
|
stringUTF8.insert(_cursorPosition, insert);
|
|
|
|
|
|
|
|
setCursorPosition(_cursorPosition + countInsertChar);
|
|
|
|
|
|
|
|
setString(stringUTF8.getAsCharSequence());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string sText(_inputText);
|
|
|
|
sText.append(insert);
|
|
|
|
setString(sText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if ((int)insert.npos == pos)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// '\n' inserted, let delegate process first
|
|
|
|
if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if delegate hasn't processed, detach from IME by default
|
|
|
|
detachWithIME();
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::deleteBackward()
|
|
|
|
{
|
|
|
|
size_t len = _inputText.length();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!len)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// there is no string
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the delete byte number
|
2021-12-25 10:04:45 +08:00
|
|
|
size_t deleteLen = 1; // default, erase 1 byte
|
2019-11-23 20:27:39 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
while (0x80 == (0xC0 & _inputText.at(len - deleteLen)))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
++deleteLen;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_delegate &&
|
|
|
|
_delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + len - deleteLen, static_cast<int>(deleteLen)))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
// delegate doesn't want to delete backwards
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if all text deleted, show placeholder string
|
|
|
|
if (len <= deleteLen)
|
|
|
|
{
|
|
|
|
_inputText = "";
|
|
|
|
_charCount = 0;
|
|
|
|
setCursorPosition(0);
|
|
|
|
setString(_inputText);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set new input text
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
if (_cursorPosition)
|
|
|
|
{
|
|
|
|
setCursorPosition(_cursorPosition - 1);
|
|
|
|
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
|
|
|
stringUTF8.replace(_inputText);
|
|
|
|
stringUTF8.deleteChar(_cursorPosition);
|
|
|
|
|
|
|
|
_charCount = stringUTF8.length();
|
|
|
|
setString(stringUTF8.getAsCharSequence());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::string text(_inputText.c_str(), len - deleteLen);
|
|
|
|
setString(text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view TextFieldTTF::getContentText()
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _inputText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setCursorPosition(std::size_t cursorPosition)
|
|
|
|
{
|
|
|
|
if (_cursorEnabled && cursorPosition <= (std::size_t)_charCount)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_cursorPosition = cursorPosition;
|
2019-11-23 20:27:39 +08:00
|
|
|
_cursorShowingTime = CURSOR_TIME_SHOW_HIDE * 2.0f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TextFieldTTF::setCursorFromPoint(const Vec2& point, const Camera* camera)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
// Reset Label, no cursor
|
|
|
|
bool oldIsAttachWithIME = _isAttachWithIME;
|
2021-12-25 10:04:45 +08:00
|
|
|
_isAttachWithIME = false;
|
2019-11-23 20:27:39 +08:00
|
|
|
updateCursorDisplayText();
|
|
|
|
|
|
|
|
Rect rect;
|
|
|
|
rect.size = getContentSize();
|
|
|
|
if (isScreenPointInRect(point, camera, getWorldToNodeTransform(), rect, nullptr))
|
|
|
|
{
|
|
|
|
int latterPosition = 0;
|
|
|
|
for (; latterPosition < _lengthOfString; ++latterPosition)
|
|
|
|
{
|
|
|
|
if (_lettersInfo[latterPosition].valid && _lettersInfo[latterPosition].atlasIndex >= 0)
|
|
|
|
{
|
|
|
|
auto sprite = getLetter(latterPosition);
|
|
|
|
if (sprite)
|
|
|
|
{
|
2021-10-23 23:27:14 +08:00
|
|
|
rect.size = Vec2(sprite->getContentSize().width, _lineHeight);
|
2019-11-23 20:27:39 +08:00
|
|
|
if (isScreenPointInRect(point, camera, sprite->getWorldToNodeTransform(), rect, nullptr))
|
|
|
|
{
|
|
|
|
setCursorPosition(latterPosition);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (latterPosition == _lengthOfString)
|
|
|
|
{
|
|
|
|
setCursorPosition(latterPosition);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set cursor
|
|
|
|
_isAttachWithIME = oldIsAttachWithIME;
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setAttachWithIME(bool isAttachWithIME)
|
|
|
|
{
|
|
|
|
if (isAttachWithIME != _isAttachWithIME)
|
|
|
|
{
|
|
|
|
_isAttachWithIME = isAttachWithIME;
|
|
|
|
|
|
|
|
if (_isAttachWithIME)
|
|
|
|
{
|
|
|
|
setCursorPosition(_charCount);
|
|
|
|
}
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setTextColorInternally(const Color4B& color)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_currentLabelType == LabelType::BMFONT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
Label::setColor(Color3B(color));
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
Label::setTextColor(color);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TextFieldTTF::setTextColor(const Color4B& color)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_colorText = color;
|
|
|
|
if (!_inputText.empty())
|
|
|
|
{
|
|
|
|
setTextColorInternally(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void TextFieldTTF::visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_delegate && _delegate->onVisit(this, renderer, parentTransform, parentFlags))
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
Label::visit(renderer, parentTransform, parentFlags);
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::update(float delta)
|
|
|
|
{
|
|
|
|
if (_cursorEnabled && _isAttachWithIME)
|
|
|
|
{
|
|
|
|
_cursorShowingTime -= delta;
|
|
|
|
if (_cursorShowingTime < -CURSOR_TIME_SHOW_HIDE)
|
|
|
|
{
|
|
|
|
_cursorShowingTime = CURSOR_TIME_SHOW_HIDE;
|
|
|
|
}
|
|
|
|
// before cursor inserted '\b', need next letter
|
|
|
|
auto sprite = getLetter((int)_cursorPosition + 1);
|
|
|
|
|
|
|
|
if (sprite)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_currentLabelType == LabelType::BMFONT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
sprite->setColor(getColor());
|
|
|
|
}
|
|
|
|
if (_cursorShowingTime >= 0.0f)
|
|
|
|
{
|
|
|
|
sprite->setOpacity(255);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sprite->setOpacity(0);
|
|
|
|
}
|
|
|
|
sprite->setDirty(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const Color4B& TextFieldTTF::getColorSpaceHolder()
|
|
|
|
{
|
|
|
|
return _colorSpaceHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setColorSpaceHolder(const Color3B& color)
|
|
|
|
{
|
|
|
|
setColorSpaceHolder(Color4B(color));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setColorSpaceHolder(const Color4B& color)
|
|
|
|
{
|
|
|
|
_colorSpaceHolder = color;
|
|
|
|
if (_inputText.empty())
|
|
|
|
{
|
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
// properties
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
// input text property
|
2021-12-01 03:35:34 +08:00
|
|
|
void TextFieldTTF::setString(std::string_view text)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
std::string displayText;
|
|
|
|
|
|
|
|
std::size_t charCount = 0;
|
|
|
|
|
|
|
|
if (!text.empty())
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_inputText = text;
|
2019-11-23 20:27:39 +08:00
|
|
|
displayText = _inputText;
|
2021-12-25 10:04:45 +08:00
|
|
|
charCount = _calcCharCount(_inputText.c_str());
|
2019-11-23 20:27:39 +08:00
|
|
|
if (_secureTextEntry)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
displayText = "";
|
2019-11-23 20:27:39 +08:00
|
|
|
size_t length = charCount;
|
|
|
|
while (length)
|
|
|
|
{
|
|
|
|
displayText.append(_passwordStyleText);
|
|
|
|
--length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_inputText = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_cursorEnabled && charCount != _charCount)
|
|
|
|
{
|
|
|
|
_cursorPosition = charCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
// Need for recreate all letters in Label
|
|
|
|
Label::removeAllChildrenWithCleanup(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
// if there is no input text, display placeholder instead
|
|
|
|
if (_inputText.empty() && (!_cursorEnabled || !_isAttachWithIME))
|
|
|
|
{
|
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
|
|
|
Label::setString(_placeHolder);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
makeStringSupportCursor(displayText);
|
|
|
|
setTextColorInternally(_colorText);
|
|
|
|
Label::setString(displayText);
|
|
|
|
}
|
|
|
|
_charCount = charCount;
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void TextFieldTTF::appendString(std::string_view text)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
insertText(text.data(), text.length());
|
2019-11-23 20:27:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::makeStringSupportCursor(std::string& displayText)
|
|
|
|
{
|
|
|
|
if (_cursorEnabled && _isAttachWithIME)
|
|
|
|
{
|
|
|
|
if (displayText.empty())
|
|
|
|
{
|
|
|
|
// \b - Next char not change x position
|
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
|
|
|
|
displayText.push_back(StringUtils::AsciiCharacters::NextCharNoChangeX);
|
|
|
|
displayText.push_back(_cursorChar);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
|
|
|
stringUTF8.replace(displayText);
|
|
|
|
|
|
|
|
if (_cursorPosition > stringUTF8.length())
|
|
|
|
{
|
|
|
|
_cursorPosition = stringUTF8.length();
|
|
|
|
}
|
|
|
|
std::string cursorChar;
|
|
|
|
// \b - Next char not change x position
|
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
|
|
|
|
cursorChar.push_back(StringUtils::AsciiCharacters::NextCharNoChangeX);
|
|
|
|
cursorChar.push_back(_cursorChar);
|
|
|
|
stringUTF8.insert(_cursorPosition, cursorChar);
|
|
|
|
|
|
|
|
displayText = stringUTF8.getAsCharSequence();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::updateCursorDisplayText()
|
|
|
|
{
|
|
|
|
// Update Label content
|
|
|
|
setString(_inputText);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setCursorChar(char cursor)
|
|
|
|
{
|
|
|
|
if (_cursorChar != cursor)
|
|
|
|
{
|
|
|
|
_cursorChar = cursor;
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::controlKey(EventKeyboard::KeyCode keyCode)
|
|
|
|
{
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
switch (keyCode)
|
|
|
|
{
|
|
|
|
case EventKeyboard::KeyCode::KEY_HOME:
|
|
|
|
case EventKeyboard::KeyCode::KEY_KP_HOME:
|
|
|
|
setCursorPosition(0);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_END:
|
|
|
|
setCursorPosition(_charCount);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_DELETE:
|
|
|
|
case EventKeyboard::KeyCode::KEY_KP_DELETE:
|
|
|
|
if (_cursorPosition < (std::size_t)_charCount)
|
|
|
|
{
|
|
|
|
StringUtils::StringUTF8 stringUTF8;
|
|
|
|
|
|
|
|
stringUTF8.replace(_inputText);
|
|
|
|
stringUTF8.deleteChar(_cursorPosition);
|
|
|
|
setCursorPosition(_cursorPosition);
|
|
|
|
_charCount = stringUTF8.length();
|
|
|
|
setString(stringUTF8.getAsCharSequence());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
|
|
|
|
if (_cursorPosition)
|
|
|
|
{
|
|
|
|
setCursorPosition(_cursorPosition - 1);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
|
|
|
|
if (_cursorPosition < (std::size_t)_charCount)
|
|
|
|
{
|
|
|
|
setCursorPosition(_cursorPosition + 1);
|
|
|
|
updateCursorDisplayText();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case EventKeyboard::KeyCode::KEY_ESCAPE:
|
|
|
|
detachWithIME();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view TextFieldTTF::getString() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _inputText;
|
|
|
|
}
|
|
|
|
|
|
|
|
// place holder text property
|
2021-12-26 23:26:34 +08:00
|
|
|
void TextFieldTTF::setPlaceHolder(std::string_view text)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
_placeHolder = text;
|
|
|
|
if (_inputText.empty() && !_isAttachWithIME)
|
|
|
|
{
|
|
|
|
setTextColorInternally(_colorSpaceHolder);
|
|
|
|
Label::setString(_placeHolder);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view TextFieldTTF::getPlaceHolder() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _placeHolder;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TextFieldTTF::setCursorEnabled(bool enabled)
|
|
|
|
{
|
|
|
|
if (_cursorEnabled == enabled)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_cursorEnabled = enabled;
|
|
|
|
if (_cursorEnabled)
|
|
|
|
{
|
|
|
|
_cursorPosition = _charCount;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
scheduleUpdate();
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2019-11-23 20:27:39 +08:00
|
|
|
_cursorPosition = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_currentLabelType == LabelType::TTF || _currentLabelType == LabelType::BMFONT)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
unscheduleUpdate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// secureTextEntry
|
|
|
|
void TextFieldTTF::setSecureTextEntry(bool value)
|
|
|
|
{
|
|
|
|
if (_secureTextEntry != value)
|
|
|
|
{
|
|
|
|
_secureTextEntry = value;
|
|
|
|
setString(_inputText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
void TextFieldTTF::setPasswordTextStyle(std::string_view text)
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
if (text.length() < 1)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (text != _passwordStyleText)
|
|
|
|
{
|
2019-11-23 20:27:39 +08:00
|
|
|
_passwordStyleText = text;
|
|
|
|
setString(_inputText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
std::string_view TextFieldTTF::getPasswordTextStyle() const
|
2019-11-23 20:27:39 +08:00
|
|
|
{
|
|
|
|
return _passwordStyleText;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool TextFieldTTF::isSecureTextEntry() const
|
|
|
|
{
|
|
|
|
return _secureTextEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_CC_END
|