axmol/core/ui/UITextFieldEx.cpp

1051 lines
27 KiB
C++
Raw Normal View History

2024-05-11 00:21:38 +08:00
/****************************************************************************
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
https://axmol.dev/
2024-05-11 00:21:38 +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.
****************************************************************************/
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
#include "UITextFieldEx.h"
#include "base/Director.h"
2019-11-24 23:15:56 +08:00
NS_AX_BEGIN
2019-11-24 23:15:56 +08:00
2023-03-08 08:34:17 +08:00
#if defined(WINAPI_FAMILY) && WINAPI_FAMILY == WINAPI_FAMILY_DESKTOP_APP
2024-05-11 00:21:38 +08:00
# define axbeep(t) MessageBeep(t)
2019-11-24 23:15:56 +08:00
#else
2024-05-11 00:21:38 +08:00
# define axbeep(t)
2019-11-24 23:15:56 +08:00
#endif
2024-05-11 00:21:38 +08:00
static Label* _createLabel(std::string_view text,
std::string_view font,
2021-12-25 10:04:45 +08:00
float fontSize,
const Vec2& dimensions = Vec2::ZERO,
TextHAlignment hAlignment = TextHAlignment::LEFT,
TextVAlignment vAlignment = TextVAlignment::TOP)
2019-11-25 01:35:26 +08:00
{
if (FileUtils::getInstance()->isFileExist(font))
{
return Label::createWithTTF(text, font, fontSize, dimensions, hAlignment, vAlignment);
}
else
{
return Label::createWithSystemFont(text, font, fontSize, dimensions, hAlignment, vAlignment);
}
}
2024-05-11 00:21:38 +08:00
static bool _checkVisibility(Node* theNode)
2019-11-24 23:15:56 +08:00
{
2022-07-16 10:43:05 +08:00
// AX_ASSERT(theNode != NULL);
2019-11-24 23:15:56 +08:00
bool visible = false;
for (Node* ptr = theNode; (ptr != nullptr && (visible = ptr->isVisible())); ptr = ptr->getParent())
;
return visible;
}
2024-05-11 00:21:38 +08:00
static bool _containsTouchPoint(ax::Node* target, ax::Touch* touch)
2019-11-24 23:15:56 +08:00
{
assert(target != nullptr);
2022-08-08 18:02:17 +08:00
ax::Point pt = target->convertTouchToNodeSpace(touch);
2019-11-24 23:15:56 +08:00
2021-10-23 23:27:14 +08:00
const Vec2& size = target->getContentSize();
2019-11-24 23:15:56 +08:00
2022-08-08 18:02:17 +08:00
ax::Rect rc(0, 0, size.width, size.height);
2019-11-24 23:15:56 +08:00
bool contains = (rc.containsPoint(pt));
// AXLOGD("check {:#x} coordinate:({}, {}), contains:%d", target, pt.x, pt.y, contains);
2019-11-24 23:15:56 +08:00
return contains;
}
2022-08-08 18:02:17 +08:00
static bool engine_inj_containsPoint(ax::Node* target, const ax::Vec2& worldPoint)
2019-11-24 23:15:56 +08:00
{
2022-08-08 18:02:17 +08:00
ax::Point pt = target->convertToNodeSpace(worldPoint);
2019-11-24 23:15:56 +08:00
2021-10-23 23:27:14 +08:00
const Vec2& size = target->getContentSize();
2019-11-24 23:15:56 +08:00
2022-08-08 18:02:17 +08:00
ax::Rect rc(0, 0, size.width, size.height);
2019-11-24 23:15:56 +08:00
bool contains = (rc.containsPoint(pt));
// AXLOGD("check {:#x} coordinate:({}, {}), contains:%d", target, pt.x, pt.y, contains);
2019-11-24 23:15:56 +08:00
return contains;
}
static uint32_t engine_inj_c4b2dw(const Color4B& value)
{
2021-12-25 10:04:45 +08:00
auto rvalue = (uint32_t)value.a << 24 | (uint32_t)value.b << 16 | (uint32_t)value.g << 8 | (uint32_t)value.r;
2019-11-24 23:15:56 +08:00
return rvalue;
}
static Sprite* engine_inj_create_lump(const Color4B& color, int height, int width)
{
unsigned int* pixels((unsigned int*)malloc(height * width * sizeof(unsigned int)));
// Fill Pixels
2021-12-25 10:04:45 +08:00
uint32_t* ptr = pixels;
2020-08-04 00:14:35 +08:00
const Color4B fillColor = Color4B::WHITE;
2019-11-24 23:15:56 +08:00
for (int i = 0; i < height * width; ++i)
{
2021-12-25 10:04:45 +08:00
ptr[i] = engine_inj_c4b2dw(fillColor); // 0xffffffff;
2019-11-24 23:15:56 +08:00
}
// create cursor by pixels
Texture2D* texture = new Texture2D();
2022-02-06 13:06:49 +08:00
texture->initWithData(pixels, height * width * sizeof(unsigned int), backend::PixelFormat::RGBA8, width, height);
2019-11-24 23:15:56 +08:00
auto cursor = Sprite::createWithTexture(texture);
cursor->setColor(Color3B(color));
cursor->setOpacity(color.a);
texture->release();
free(pixels);
return cursor;
}
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/// calculate the UTF-8 string's char count.
static int _calcCharCount(const char* text)
{
int n = 0;
char ch = 0;
while ((ch = *text) != 0x0)
2019-11-24 23:15:56 +08:00
{
2022-07-16 10:43:05 +08:00
AX_BREAK_IF(!ch);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (0x80 != (0xC0 & ch))
{
++n;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
++text;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return n;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/// calculate the UTF-8 string's char count.
static int _truncateUTF8String(const char* text, int limit, int& nb)
{
int n = 0;
char ch = 0;
nb = 0;
while ((ch = *text) != 0x0)
2019-11-24 23:15:56 +08:00
{
2022-07-16 10:43:05 +08:00
AX_BREAK_IF(!ch || n > limit);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (0x80 != (0xC0 & ch))
{
++n;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
++nb;
++text;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return n;
}
2019-11-24 23:15:56 +08:00
static void internalSetLableFont(Label* l, std::string_view fontName, float fontSize)
2021-12-25 10:04:45 +08:00
{
if (FileUtils::getInstance()->isFileExist(fontName))
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
TTFConfig config = l->getTTFConfig();
config.fontFilePath = fontName;
config.fontSize = fontSize;
l->setTTFConfig(config);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
l->setSystemFontName(fontName);
l->requestSystemFontRefresh();
l->setSystemFontSize(fontSize);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
static float internalCalcStringWidth(std::string_view s, std::string_view fontName, float fontSize)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
auto label = _createLabel(std::string{s}, fontName, fontSize);
2021-12-25 10:04:45 +08:00
return label->getContentSize().width;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
static std::string internalUTF8MoveLeft(std::string_view utf8Text, int length /* default utf8Text.length() */)
{
if (!utf8Text.empty() && length > 0)
{
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// get the delete byte number
int deleteLen = 1; // default, erase 1 byte
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
while (length >= deleteLen && 0x80 == (0xC0 & utf8Text.at(length - deleteLen)))
{
++deleteLen;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return std::string{utf8Text.data(), static_cast<size_t>(length - deleteLen)};
}
else
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return std::string{utf8Text};
}
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
static std::string internalUTF8MoveRight(std::string_view utf8Text, int length /* default utf8Text.length() */)
{
if (!utf8Text.empty() && length >= 0)
{
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// get the delete byte number
size_t addLen = 1; // default, erase 1 byte
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
while ((length + addLen) < utf8Text.size() && 0x80 == (0xC0 & utf8Text.at(length + addLen)))
{
++addLen;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return std::string{utf8Text.data(), static_cast<size_t>(length + addLen)};
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return std::string{utf8Text};
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
//////////////////////////////////////////////////////////////////////////
// constructor and destructor
//////////////////////////////////////////////////////////////////////////
bool TextFieldEx::s_keyboardVisible = false;
TextFieldEx::TextFieldEx()
2024-05-11 00:21:38 +08:00
: _editable(true)
, _renderLabel(nullptr)
, _charCount(0)
, _inputText("")
, _placeHolder("")
, _colorText(Color4B::WHITE)
, _colorSpaceHolder(Color4B::GRAY)
, _secureTextEntry(false)
, _cursor(nullptr)
, _touchListener(nullptr)
, _kbdListener(nullptr)
2021-12-25 10:04:45 +08:00
, onTextModify(nullptr)
, onOpenIME(nullptr)
, onCloseIME(nullptr)
2024-05-11 00:21:38 +08:00
, _charLimit(std::numeric_limits<int>::max())
, _systemFontUsed(false)
, _fontSize(24)
, _insertPosUtf8(0)
, _insertPos(0)
, _cursorPos(0)
, _touchCursorControlEnabled(true)
, _cursorVisible(false)
2021-12-25 10:04:45 +08:00
, _continuousTouchDelayTimerID(nullptr)
, _continuousTouchDelayTime(0.6)
{}
TextFieldEx::~TextFieldEx()
{
2024-05-11 00:21:38 +08:00
if (_kbdListener != nullptr)
_eventDispatcher->removeEventListener(_kbdListener);
if (_touchListener != nullptr)
_eventDispatcher->removeEventListener(_touchListener);
2021-12-25 10:04:45 +08:00
}
//////////////////////////////////////////////////////////////////////////
// static constructor
//////////////////////////////////////////////////////////////////////////
TextFieldEx* TextFieldEx::create(std::string_view placeholder,
std::string_view fontName,
2021-12-25 10:04:45 +08:00
float fontSize,
float cursorWidth,
const Color4B& cursorColor)
{
TextFieldEx* ret = new TextFieldEx();
if (ret && ret->initWithPlaceHolder("", fontName, fontSize, cursorWidth, cursorColor))
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
ret->autorelease();
if (placeholder.size() > 0)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
ret->setPlaceholderText(placeholder);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
return ret;
2019-11-24 23:15:56 +08:00
}
2022-07-16 10:43:05 +08:00
AX_SAFE_DELETE(ret);
2021-12-25 10:04:45 +08:00
return nullptr;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
//////////////////////////////////////////////////////////////////////////
// initialize
//////////////////////////////////////////////////////////////////////////
bool TextFieldEx::initWithPlaceHolder(std::string_view placeholder,
std::string_view fontName,
2021-12-25 10:04:45 +08:00
float fontSize,
float cursorWidth,
const Color4B& cursorColor)
{
2024-05-11 00:21:38 +08:00
_placeHolder = placeholder;
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_renderLabel =
_createLabel(placeholder, fontName, fontSize, Vec2::ZERO, TextHAlignment::CENTER, TextVAlignment::CENTER);
_renderLabel->setAnchorPoint(Point::ANCHOR_MIDDLE_LEFT);
this->addChild(_renderLabel);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_director->getScheduler()->runOnAxmolThread([this] { _renderLabel->setPosition(Point(0, this->getContentSize().height / 2)); });
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
__initCursor(fontSize, cursorWidth, cursorColor);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_fontName = fontName;
_fontSize = fontSize;
_systemFontUsed = !FileUtils::getInstance()->isFileExist(fontName);
2021-12-25 10:04:45 +08:00
return true;
}
std::string_view TextFieldEx::getTextFontName() const
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
return _fontName;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
void TextFieldEx::setTextFontName(std::string_view fontName)
2021-12-25 10:04:45 +08:00
{
if (FileUtils::getInstance()->isFileExist(fontName))
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
TTFConfig config = _renderLabel->getTTFConfig();
2021-12-25 10:04:45 +08:00
config.fontFilePath = fontName;
2024-05-11 00:21:38 +08:00
config.fontSize = _fontSize;
_renderLabel->setTTFConfig(config);
_systemFontUsed = false;
2021-12-25 10:04:45 +08:00
_fontType = 1;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_renderLabel->setSystemFontName(fontName);
if (!_systemFontUsed)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_renderLabel->requestSystemFontRefresh();
2019-11-24 23:15:56 +08:00
}
2024-05-11 00:21:38 +08:00
_renderLabel->setSystemFontSize(_fontSize);
_systemFontUsed = true;
2021-12-25 10:04:45 +08:00
_fontType = 0;
2019-11-24 23:15:56 +08:00
}
2024-05-11 00:21:38 +08:00
_fontName = fontName;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
using namespace std::string_view_literals;
2024-05-11 00:21:38 +08:00
_asteriskWidth = internalCalcStringWidth("*"sv, _fontName, _fontSize);
2021-12-25 10:04:45 +08:00
}
void TextFieldEx::setTextFontSize(float size)
{
2024-05-11 00:21:38 +08:00
if (_systemFontUsed)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_renderLabel->setSystemFontSize(size);
2021-12-25 10:04:45 +08:00
}
else
{
2024-05-11 00:21:38 +08:00
TTFConfig config = _renderLabel->getTTFConfig();
2021-12-25 10:04:45 +08:00
config.fontSize = size;
2024-05-11 00:21:38 +08:00
_renderLabel->setTTFConfig(config);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_fontSize = size;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
using namespace std::string_view_literals;
2024-05-11 00:21:38 +08:00
_asteriskWidth = internalCalcStringWidth("*"sv, _fontName, _fontSize);
2021-12-25 10:04:45 +08:00
}
float TextFieldEx::getTextFontSize() const
{
2024-05-11 00:21:38 +08:00
return _fontSize;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::enableIME(Node* control)
{
2024-05-11 00:21:38 +08:00
if (_touchListener != nullptr)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return;
2019-11-24 23:15:56 +08:00
}
2024-05-11 00:21:38 +08:00
_touchListener = EventListenerTouchOneByOne::create();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (control == nullptr)
control = this;
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_touchListener->onTouchBegan = [control,this](Touch* touch, Event*) {
bool focus = (_checkVisibility(this) && _editable && this->_enabled &&
_containsTouchPoint(control, touch));
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (this->_continuousTouchDelayTimerID != nullptr)
{
stimer::kill(this->_continuousTouchDelayTimerID);
this->_continuousTouchDelayTimerID = nullptr;
}
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
if (focus && _cursorVisible)
2021-12-25 10:04:45 +08:00
{
auto worldPoint = touch->getLocation();
if (this->_continuousTouchCallback)
{
this->_continuousTouchDelayTimerID = stimer::delay(
this->_continuousTouchDelayTime, [=, this]() { this->_continuousTouchCallback(worldPoint); });
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
return true;
};
2024-05-11 00:21:38 +08:00
_touchListener->onTouchEnded = [control, this](Touch* touch, Event* e) {
2021-12-25 10:04:45 +08:00
if (this->_continuousTouchDelayTimerID != nullptr)
{
stimer::kill(this->_continuousTouchDelayTimerID);
this->_continuousTouchDelayTimerID = nullptr;
}
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
bool focus = (_checkVisibility(this) && _editable && this->_enabled &&
_containsTouchPoint(control, touch));
2021-12-25 10:04:45 +08:00
if (focus)
{
2024-05-11 00:21:38 +08:00
if (!s_keyboardVisible || !_cursorVisible)
2021-12-25 10:04:45 +08:00
openIME();
2024-05-11 00:21:38 +08:00
if (_touchCursorControlEnabled)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
auto renderLabelPoint = _renderLabel->convertToNodeSpace(touch->getLocation());
2021-12-25 10:04:45 +08:00
__moveCursorTo(renderLabelPoint.x);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
else
{
closeIME();
}
};
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/// enable use keyboard <- -> to move cursor.
2024-05-11 00:21:38 +08:00
_kbdListener = EventListenerKeyboard::create();
_kbdListener->onKeyPressed = [this](EventKeyboard::KeyCode code, Event*) {
if (_cursorVisible)
2021-12-25 10:04:45 +08:00
{
switch (code)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
this->__moveCursor(-1);
break;
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
this->__moveCursor(1);
break;
case EventKeyboard::KeyCode::KEY_DELETE:
case EventKeyboard::KeyCode::KEY_KP_DELETE:
this->handleDeleteKeyEvent();
break;
default:;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
};
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_eventDispatcher->addEventListenerWithSceneGraphPriority(_kbdListener, this);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::disableIME(void)
{
2024-05-11 00:21:38 +08:00
_eventDispatcher->removeEventListener(_kbdListener);
_eventDispatcher->removeEventListener(_touchListener);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_kbdListener = nullptr;
_touchListener = nullptr;
2021-12-25 10:04:45 +08:00
closeIME();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Label* TextFieldEx::getRenderLabel()
{
2024-05-11 00:21:38 +08:00
return _renderLabel;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
//////////////////////////////////////////////////////////////////////////
// IMEDelegate
//////////////////////////////////////////////////////////////////////////
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool TextFieldEx::attachWithIME()
{
bool ret = IMEDelegate::attachWithIME();
if (ret)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
// open keyboard
GLView* pGlView = _director->getGLView();
2021-12-25 10:04:45 +08:00
if (pGlView)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
pGlView->setIMEKeyboardState(true);
2019-11-24 23:15:56 +08:00
}
}
2021-12-25 10:04:45 +08:00
return ret;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool TextFieldEx::detachWithIME()
{
bool ret = IMEDelegate::detachWithIME();
if (ret)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
// close keyboard
GLView* glView = _director->getGLView();
2021-12-25 10:04:45 +08:00
if (glView)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
glView->setIMEKeyboardState(false);
2019-11-24 23:15:56 +08:00
}
}
2021-12-25 10:04:45 +08:00
return ret;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::keyboardDidShow(IMEKeyboardNotificationInfo& /*info*/)
{
s_keyboardVisible = true;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::keyboardDidHide(IMEKeyboardNotificationInfo& /*info*/)
{
s_keyboardVisible = false;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::openIME(void)
{
AXLOGD("TextFieldEx:: openIME");
2021-12-25 10:04:45 +08:00
this->attachWithIME();
__updateCursorPosition();
__showCursor();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (this->onOpenIME)
this->onOpenIME();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::closeIME(void)
{
AXLOGD("TextFieldEx:: closeIME");
2021-12-25 10:04:45 +08:00
__hideCursor();
this->detachWithIME();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (this->onCloseIME)
this->onCloseIME();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool TextFieldEx::canAttachWithIME()
{
return true; //(_delegate) ? (! _delegate->onTextFieldAttachWithIME(this)) : true;
}
bool TextFieldEx::canDetachWithIME()
{
return true; //(_delegate) ? (! _delegate->onTextFieldDetachWithIME(this)) : true;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::insertText(const char* text, size_t len)
{
2024-05-11 00:21:38 +08:00
if (!_editable || !this->_enabled)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return;
2019-11-24 23:15:56 +08:00
}
2024-05-11 00:21:38 +08:00
if (_charLimit > 0 && _charCount >= _charLimit)
2021-12-25 10:04:45 +08:00
{ // regard zero as unlimited
2024-05-11 00:21:38 +08:00
axbeep(0);
2021-12-25 10:04:45 +08:00
return;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int nb;
2024-05-11 00:21:38 +08:00
auto n = _truncateUTF8String(text, static_cast<int>(_charLimit - _charCount), nb);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string insert(text, nb);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// insert \n means input end
auto pos = insert.find('\n');
if (insert.npos != pos)
{
len = pos;
insert.erase(pos);
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (len > 0)
{
// if (_delegate && _delegate->onTextFieldInsertText(this, insert.c_str(), len))
//{
// // delegate doesn't want to insert text
// return;
// }
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_charCount += n; // _calcCharCount(insert.c_str());
std::string sText(_inputText);
sText.insert(_insertPos, insert); // original is: sText.append(insert);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// bool needUpdatePos
this->setString(sText);
while (n-- > 0)
__moveCursor(1);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// this->contentDirty = true;
// __updateCursorPosition();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (this->onTextModify)
this->onTextModify();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
if (insert.npos == pos)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
return;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// '\n' inserted, let delegate process first
/*if (_delegate && _delegate->onTextFieldInsertText(this, "\n", 1))
{
return;
}*/
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// if delegate hasn't processed, detach from IME by default
this->closeIME();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::deleteBackward()
{
2024-05-11 00:21:38 +08:00
if (!_editable || !this->_enabled || 0 == _charCount)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
axbeep(0);
2021-12-25 10:04:45 +08:00
return;
}
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
size_t len = _inputText.length();
if (0 == len || _insertPos == 0)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
axbeep(0);
2021-12-25 10:04:45 +08:00
// there is no string
// __updateCursorPosition();
return;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// get the delete byte number
size_t deleteLen = 1; // default, erase 1 byte
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
while (0x80 == (0xC0 & _inputText.at(_insertPos - deleteLen)))
2021-12-25 10:04:45 +08:00
{
++deleteLen;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + len - deleteLen,
// static_cast<int>(deleteLen)))
//{
// // delegate doesn't wan't to delete backwards
// return;
// }
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// if all text deleted, show placeholder string
if (len <= deleteLen)
{
2019-11-24 23:15:56 +08:00
__moveCursor(-1);
2024-05-11 00:21:38 +08:00
_inputText.clear();
_charCount = 0;
_renderLabel->setTextColor(_colorSpaceHolder);
_renderLabel->setString(_placeHolder);
2021-12-25 10:04:45 +08:00
// __updateCursorPosition();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// this->contentDirty = true;
2019-11-24 23:15:56 +08:00
if (this->onTextModify)
this->onTextModify();
2021-12-25 10:04:45 +08:00
return;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
// set new input text
2024-05-11 00:21:38 +08:00
std::string text = _inputText; // (inputText.c_str(), len - deleteLen);
text.erase(_insertPos - deleteLen, deleteLen);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
__moveCursor(-1);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
this->setString(text);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
//__updateCursorPosition();
// __moveCursor(-1);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (this->onTextModify)
this->onTextModify();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::handleDeleteKeyEvent()
{
2024-05-11 00:21:38 +08:00
if (!_editable || !this->_enabled || 0 == _charCount)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
axbeep(0);
2021-12-25 10:04:45 +08:00
return;
}
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
size_t len = _inputText.length();
if (0 == len || _insertPosUtf8 == _charCount)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
axbeep(0);
2021-12-25 10:04:45 +08:00
// there is no string
// __updateCursorPosition();
return;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// get the delete byte number
size_t deleteLen = 1; // default, erase 1 byte
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
while ((_inputText.length() > _insertPos + deleteLen) && 0x80 == (0xC0 & _inputText.at(_insertPos + deleteLen)))
2021-12-25 10:04:45 +08:00
{
++deleteLen;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// if (_delegate && _delegate->onTextFieldDeleteBackward(this, _inputText.c_str() + len - deleteLen,
// static_cast<int>(deleteLen)))
//{
// // delegate doesn't wan't to delete backwards
// return;
// }
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// if all text deleted, show placeholder string
if (len <= deleteLen)
{
2024-05-11 00:21:38 +08:00
_inputText.clear();
_charCount = 0;
_renderLabel->setTextColor(_colorSpaceHolder);
_renderLabel->setString(_placeHolder);
2021-12-25 10:04:45 +08:00
__updateCursorPosition();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// this->contentDirty = true;
2019-11-24 23:15:56 +08:00
if (this->onTextModify)
this->onTextModify();
2021-12-25 10:04:45 +08:00
return;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
// set new input text
2024-05-11 00:21:38 +08:00
std::string text = _inputText; // (inputText.c_str(), len - deleteLen);
text.erase(_insertPos, deleteLen);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// __moveCursor(-1);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
this->setString(text);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (this->onTextModify)
this->onTextModify();
}
2019-11-24 23:15:56 +08:00
std::string_view TextFieldEx::getContentText()
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
return _inputText;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::setTextColor(const Color4B& color)
{
2024-05-11 00:21:38 +08:00
_colorText = color;
if (!_inputText.empty())
_renderLabel->setTextColor(_colorText);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
const Color4B& TextFieldEx::getTextColor(void) const
{
2024-05-11 00:21:38 +08:00
return _colorText;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::setCursorColor(const Color3B& color)
{
2024-05-11 00:21:38 +08:00
_cursor->setColor(color);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
const Color3B& TextFieldEx::getCursorColor(void) const
{
2024-05-11 00:21:38 +08:00
return _cursor->getColor();
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
const Color4B& TextFieldEx::getPlaceholderColor() const
{
2024-05-11 00:21:38 +08:00
return _colorSpaceHolder;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::setPlaceholderColor(const Color4B& color)
{
2024-05-11 00:21:38 +08:00
_colorSpaceHolder = color;
if (_inputText.empty())
_renderLabel->setTextColor(color);
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
//////////////////////////////////////////////////////////////////////////
// properties
//////////////////////////////////////////////////////////////////////////
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// input text property
void TextFieldEx::setString(std::string_view text)
2021-12-25 10:04:45 +08:00
{
static char bulletString[] = {(char)0xe2, (char)0x80, (char)0xa2, (char)0x00};
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_inputText = text;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string secureText;
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
std::string* displayText = &_inputText;
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
if (!_inputText.empty())
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
if (_secureTextEntry)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
size_t length = _inputText.length();
2021-12-25 10:04:45 +08:00
displayText = &secureText;
while (length > 0)
{
displayText->append(bulletString);
--length;
}
2019-11-24 23:15:56 +08:00
}
}
2021-12-25 10:04:45 +08:00
// if there is no input text, display placeholder instead
2024-05-11 00:21:38 +08:00
if (_inputText.empty())
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_renderLabel->setTextColor(_colorSpaceHolder);
_renderLabel->setString(_placeHolder);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_renderLabel->setTextColor(_colorText);
_renderLabel->setString(*displayText);
2019-11-24 23:15:56 +08:00
}
2024-05-11 00:21:38 +08:00
bool bInsertAtEnd = (_insertPosUtf8 == _charCount);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_charCount = _calcCharCount(_inputText.c_str());
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (bInsertAtEnd)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_insertPosUtf8 = static_cast<int>(_charCount);
_insertPos = static_cast<int>(_inputText.length());
_cursorPos = static_cast<int>(displayText->length());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::updateContentSize(void)
{
2024-05-11 00:21:38 +08:00
this->setContentSize(_renderLabel->getContentSize());
2021-12-25 10:04:45 +08:00
}
std::string_view TextFieldEx::getString() const
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
return _inputText;
2021-12-25 10:04:45 +08:00
}
// place holder text property
void TextFieldEx::setPlaceholderText(std::string_view text)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
_placeHolder = text;
if (_inputText.empty())
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_renderLabel->setTextColor(_colorSpaceHolder);
_renderLabel->setString(_placeHolder);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
std::string_view TextFieldEx::getPlaceholderText() const
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
return _placeHolder;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// secureTextEntry
void TextFieldEx::setPasswordEnabled(bool value)
{
2024-05-11 00:21:38 +08:00
if (_secureTextEntry != value)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_secureTextEntry = value;
2021-12-25 10:04:45 +08:00
this->setString(this->getString());
__updateCursorPosition();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
bool TextFieldEx::isPasswordEnabled() const
{
2024-05-11 00:21:38 +08:00
return _secureTextEntry;
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::setEnabled(bool bEnabled)
{
2024-05-11 00:21:38 +08:00
if (this->_enabled != bEnabled)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
if (!bEnabled)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
this->closeIME();
2019-11-24 23:15:56 +08:00
}
2024-05-11 00:21:38 +08:00
this->_enabled = bEnabled;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int TextFieldEx::getFontType() const
{
return _fontType;
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::__initCursor(int height, int width, const Color4B& color)
{
2024-05-11 00:21:38 +08:00
_cursor = engine_inj_create_lump(Color4B(color), height, width);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
this->addChild(_cursor);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
_cursor->setPosition(Point(0, this->getContentSize().height / 2));
// nodes_layout::setNodeLB(_cursor, ax::Point::ZERO);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
__hideCursor();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
__updateCursorPosition();
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::__showCursor(void)
{
2024-05-11 00:21:38 +08:00
if (_cursor)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_cursorVisible = true;
_cursor->setVisible(true);
_cursor->runAction(RepeatForever::create(Blink::create(1, 1)));
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::__hideCursor(void)
{
2024-05-11 00:21:38 +08:00
if (_cursor)
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_cursor->setVisible(false);
_cursorVisible = false;
_cursor->stopAllActions();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::__updateCursorPosition(void)
{
2024-05-11 00:21:38 +08:00
if (_cursor && _insertPosUtf8 == _charCount)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
if (0 == this->getCharCount())
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
_cursor->setPosition(Point(0, this->getContentSize().height / 2));
2021-12-25 10:04:45 +08:00
}
else
{
2024-05-11 00:21:38 +08:00
_cursor->setPosition(Point(_renderLabel->getContentSize().width, this->getContentSize().height / 2));
2019-11-24 23:15:56 +08:00
}
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::__moveCursor(int direction)
{
2024-05-11 00:21:38 +08:00
auto newOffset = _insertPosUtf8 + direction;
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
if (newOffset > 0 && newOffset <= _charCount)
2021-12-25 10:04:45 +08:00
{
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string_view displayText;
2024-05-11 00:21:38 +08:00
if (!_secureTextEntry)
2021-12-25 10:04:45 +08:00
displayText = this->getString();
2024-05-11 00:21:38 +08:00
else if (!_inputText.empty())
displayText = _renderLabel->getString();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
if (direction < 0)
{
2024-05-11 00:21:38 +08:00
_insertPos = static_cast<int>(internalUTF8MoveLeft(_inputText, _insertPos).size());
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
auto s = internalUTF8MoveLeft(displayText, _cursorPos);
2019-11-24 23:15:56 +08:00
2024-05-11 00:21:38 +08:00
auto width = internalCalcStringWidth(s, _fontName, _fontSize);
_cursor->setPosition(Point(width, this->getContentSize().height / 2));
_cursorPos = static_cast<int>(s.length());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
else
{
2024-05-11 00:21:38 +08:00
_insertPos = static_cast<int>(internalUTF8MoveRight(_inputText, _insertPos).size());
2021-12-25 10:04:45 +08:00
2024-05-11 00:21:38 +08:00
auto s = internalUTF8MoveRight(displayText, _cursorPos);
auto width = internalCalcStringWidth(s, _fontName, _fontSize);
_cursor->setPosition(Point(width, this->getContentSize().height / 2));
_cursorPos = static_cast<int>(s.length());
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
2024-05-11 00:21:38 +08:00
_insertPosUtf8 = newOffset;
2021-12-25 10:04:45 +08:00
}
else if (newOffset == 0)
{
2024-05-11 00:21:38 +08:00
_cursor->setPosition(Point(0, this->getContentSize().height / 2));
_insertPosUtf8 = newOffset;
_insertPos = 0;
_cursorPos = 0;
2021-12-25 10:04:45 +08:00
}
else
{
// MessageBeep(0);
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void TextFieldEx::__moveCursorTo(float x)
{ // test
// normalized x
float normalizedX = 0;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string_view displayText;
2024-05-11 00:21:38 +08:00
if (!_secureTextEntry)
2021-12-25 10:04:45 +08:00
{
2024-05-11 00:21:38 +08:00
displayText = _inputText;
2021-12-25 10:04:45 +08:00
}
else
{
2024-05-11 00:21:38 +08:00
if (!_inputText.empty())
2019-11-24 23:15:56 +08:00
{
2024-05-11 00:21:38 +08:00
displayText = _renderLabel->getString();
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
}
int length = static_cast<int>(displayText.length());
2024-05-11 00:21:38 +08:00
int n = static_cast<int>(_charCount); // UTF8 char counter
2021-12-25 10:04:45 +08:00
int insertWhere = 0;
int insertWhereUtf8 = 0;
while (length > 0)
{
2024-05-11 00:21:38 +08:00
auto checkX = internalCalcStringWidth(displayText, _fontName, _fontSize);
2021-12-25 10:04:45 +08:00
if (x >= checkX)
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
insertWhere = length;
insertWhereUtf8 = n;
normalizedX = checkX;
break;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
// clamp backward
size_t backwardLen = 1; // default, erase 1 byte
while (0x80 == (0xC0 & displayText.at(displayText.length() - backwardLen)))
2019-11-24 23:15:56 +08:00
{
2021-12-25 10:04:45 +08:00
++backwardLen;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
--n;
displayText.remove_suffix(backwardLen);
length -= backwardLen;
2019-11-24 23:15:56 +08:00
}
2021-12-25 10:04:45 +08:00
2024-05-11 00:21:38 +08:00
_insertPos = !_secureTextEntry ? insertWhere : insertWhereUtf8;
_cursorPos = insertWhere;
_insertPosUtf8 = insertWhereUtf8;
_cursor->setPosition(Point(normalizedX, this->getContentSize().height / 2));
2021-12-25 10:04:45 +08:00
}
}; // namespace ui
2019-11-24 23:15:56 +08:00
NS_AX_END
2019-11-24 23:15:56 +08:00