axmol/core/ui/UIEditBox/UIEditBoxImpl-common.cpp

450 lines
13 KiB
C++
Raw Normal View History

2019-11-23 20:27:39 +08:00
/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2012 James Chen
Copyright (c) 2013-2015 zilongshanren
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2019-present Axmol Engine contributors (see AUTHORS.md).
https://axmol.dev/
2021-12-25 10:04:45 +08:00
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:
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
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/UIEditBox/UIEditBoxImpl-common.h"
2021-12-25 10:04:45 +08:00
#define kLabelZOrder 9999
2019-11-23 20:27:39 +08:00
#include "ui/UIEditBox/UIEditBox.h"
#include "base/Director.h"
#include "2d/Label.h"
2019-11-23 20:27:39 +08:00
#include "ui/UIHelper.h"
2022-07-16 10:43:05 +08:00
static const int AX_EDIT_BOX_PADDING = 5;
2019-11-23 20:27:39 +08:00
2022-07-16 10:43:05 +08:00
#if (AX_TARGET_PLATFORM == AX_PLATFORM_WIN32)
2021-12-25 10:04:45 +08:00
# define PASSWORD_CHAR "*"
2019-11-23 20:27:39 +08:00
#else
2021-12-25 10:04:45 +08:00
# define PASSWORD_CHAR "\u25CF"
2019-11-23 20:27:39 +08:00
#endif
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
2021-10-23 23:27:14 +08:00
static Vec2 applyPadding(const Vec2& sizeToCorrect)
{
2022-07-16 10:43:05 +08:00
return Vec2(sizeToCorrect.width - AX_EDIT_BOX_PADDING * 2, sizeToCorrect.height);
2021-10-23 23:27:14 +08:00
}
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
EditBoxImplCommon::EditBoxImplCommon(EditBox* pEditText)
: EditBoxImpl(pEditText)
, _label(nullptr)
, _labelPlaceHolder(nullptr)
, _editBoxInputMode(EditBox::InputMode::SINGLE_LINE)
, _editBoxInputFlag(EditBox::InputFlag::INITIAL_CAPS_ALL_CHARACTERS)
, _keyboardReturnType(EditBox::KeyboardReturnType::DEFAULT)
, _alignment(TextHAlignment::LEFT)
, _fontSize(-1)
, _placeholderFontSize(-1)
, _colText(Color3B::WHITE)
, _colPlaceHolder(Color3B::GRAY)
, _maxLength(-1)
, _editingMode(false)
{}
EditBoxImplCommon::~EditBoxImplCommon() {}
2019-11-23 20:27:39 +08:00
2021-10-23 23:27:14 +08:00
bool EditBoxImplCommon::initWithSize(const Vec2& size)
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
do
2019-11-23 20:27:39 +08:00
{
Rect rect = Rect(0, 0, size.width, size.height);
this->createNativeControl(rect);
initInactiveLabels(size);
setContentSize(size);
return true;
2021-12-25 10:04:45 +08:00
} while (0);
2019-11-23 20:27:39 +08:00
return false;
}
2021-10-23 23:27:14 +08:00
void EditBoxImplCommon::initInactiveLabels(const Vec2& size)
2019-11-23 20:27:39 +08:00
{
const char* pDefaultFontName = this->getNativeDefaultFontName();
_label = Label::create();
2021-12-25 10:04:45 +08:00
_label->setAnchorPoint(Vec2(0.0f, 1.0f));
2019-11-23 20:27:39 +08:00
_label->setOverflow(Label::Overflow::CLAMP);
_label->setVisible(false);
_label->setGlobalZOrder(_editBox->getGlobalZOrder());
2019-11-23 20:27:39 +08:00
_editBox->addChild(_label, kLabelZOrder);
_labelPlaceHolder = Label::create();
_labelPlaceHolder->setAnchorPoint(Vec2(0.0f, 1.0f));
_labelPlaceHolder->setTextColor(Color4B::GRAY);
_labelPlaceHolder->enableWrap(false);
_labelPlaceHolder->setGlobalZOrder(_editBox->getGlobalZOrder());
2019-11-23 20:27:39 +08:00
_editBox->addChild(_labelPlaceHolder, kLabelZOrder);
2021-12-25 10:04:45 +08:00
setFont(pDefaultFontName, size.height * 2 / 3);
setPlaceholderFont(pDefaultFontName, size.height * 2 / 3);
2019-11-23 20:27:39 +08:00
}
2021-10-23 23:27:14 +08:00
void EditBoxImplCommon::placeInactiveLabels(const Vec2& size)
2019-11-23 20:27:39 +08:00
{
_label->setDimensions(size.width, size.height);
auto placeholderSize = _labelPlaceHolder->getContentSize();
2021-12-25 10:04:45 +08:00
if (_editBoxInputMode == EditBox::InputMode::ANY)
{
2022-07-16 10:43:05 +08:00
_label->setPosition(Vec2((float)AX_EDIT_BOX_PADDING, size.height - AX_EDIT_BOX_PADDING));
2019-11-23 20:27:39 +08:00
_label->setVerticalAlignment(TextVAlignment::TOP);
_label->enableWrap(true);
2022-07-16 10:43:05 +08:00
_labelPlaceHolder->setPosition(Vec2((float)AX_EDIT_BOX_PADDING, size.height - AX_EDIT_BOX_PADDING));
2019-11-23 20:27:39 +08:00
_labelPlaceHolder->setVerticalAlignment(TextVAlignment::TOP);
}
2021-12-25 10:04:45 +08:00
else
{
2019-11-23 20:27:39 +08:00
_label->enableWrap(false);
2022-07-16 10:43:05 +08:00
_label->setPosition(Vec2((float)AX_EDIT_BOX_PADDING, size.height));
2019-11-23 20:27:39 +08:00
_label->setVerticalAlignment(TextVAlignment::CENTER);
2022-07-16 10:43:05 +08:00
_labelPlaceHolder->setPosition(Vec2((float)AX_EDIT_BOX_PADDING, (size.height + placeholderSize.height) / 2));
2019-11-23 20:27:39 +08:00
_labelPlaceHolder->setVerticalAlignment(TextVAlignment::CENTER);
}
}
void EditBoxImplCommon::setInactiveText(const char* pText)
{
2021-12-25 10:04:45 +08:00
if (EditBox::InputFlag::PASSWORD == _editBoxInputFlag)
2019-11-23 20:27:39 +08:00
{
std::string passwordString;
2021-12-25 10:04:45 +08:00
for (size_t i = 0, len = strlen(pText); i < len; ++i)
2019-11-23 20:27:39 +08:00
passwordString.append(PASSWORD_CHAR);
_label->setString(passwordString);
}
else
{
_label->setString(pText);
}
// Clip the text width to fit to the text box
const auto maxSize = applyPadding(_editBox->getContentSize());
2021-12-25 10:04:45 +08:00
Vec2 labelSize = _label->getContentSize();
if (labelSize.width > maxSize.width || labelSize.height > maxSize.height)
2019-11-23 20:27:39 +08:00
{
_label->setDimensions(maxSize.width, maxSize.height);
}
}
void EditBoxImplCommon::setFont(const char* pFontName, int fontSize)
{
_fontName = pFontName;
_fontSize = fontSize;
this->setNativeFont(pFontName, fontSize * _label->getNodeToWorldAffineTransform().a);
// edit by bixniaojiao
if (FileUtils::getInstance()->isFileExist(pFontName))
2019-11-23 20:27:39 +08:00
{
TTFConfig ttfConfig(pFontName, fontSize);
_label->setTTFConfig(ttfConfig);
2019-11-23 20:27:39 +08:00
}
else
2019-11-23 20:27:39 +08:00
{
if (!_fontName.empty())
{
_label->setSystemFontName(pFontName);
}
if (fontSize > 0)
{
_label->setSystemFontSize(fontSize);
}
2019-11-23 20:27:39 +08:00
}
}
void EditBoxImplCommon::setFontColor(const Color4B& color)
{
_colText = color;
this->setNativeFontColor(color);
_label->setTextColor(color);
}
void EditBoxImplCommon::setPlaceholderFont(const char* pFontName, int fontSize)
{
_placeholderFontName = pFontName;
_placeholderFontSize = fontSize;
this->setNativePlaceholderFont(pFontName, fontSize * _labelPlaceHolder->getNodeToWorldAffineTransform().a);
// edit by bixniaojiao
if (FileUtils::getInstance()->isFileExist(pFontName))
2019-11-23 20:27:39 +08:00
{
TTFConfig ttfConfig(pFontName, fontSize);
_labelPlaceHolder->setTTFConfig(ttfConfig);
2019-11-23 20:27:39 +08:00
}
else
2019-11-23 20:27:39 +08:00
{
if (!_fontName.empty())
{
_labelPlaceHolder->setSystemFontName(pFontName);
}
if (fontSize > 0)
{
_labelPlaceHolder->setSystemFontSize(fontSize);
}
2019-11-23 20:27:39 +08:00
}
}
2021-12-25 10:04:45 +08:00
void EditBoxImplCommon::setPlaceholderFontColor(const Color4B& color)
2019-11-23 20:27:39 +08:00
{
_colPlaceHolder = color;
this->setNativePlaceholderFontColor(color);
_labelPlaceHolder->setTextColor(color);
}
void EditBoxImplCommon::setInputMode(EditBox::InputMode inputMode)
{
_editBoxInputMode = inputMode;
this->setNativeInputMode(inputMode);
this->placeInactiveLabels(applyPadding(_editBox->getContentSize()));
}
void EditBoxImplCommon::setMaxLength(int maxLength)
{
_maxLength = maxLength;
this->setNativeMaxLength(maxLength);
}
2022-08-08 18:02:17 +08:00
void EditBoxImplCommon::setTextHorizontalAlignment(ax::TextHAlignment alignment)
2019-11-23 20:27:39 +08:00
{
_alignment = alignment;
this->setNativeTextHorizontalAlignment(alignment);
refreshLabelAlignment();
}
void EditBoxImplCommon::setInputFlag(EditBox::InputFlag inputFlag)
{
_editBoxInputFlag = inputFlag;
this->setNativeInputFlag(inputFlag);
}
void EditBoxImplCommon::setReturnType(EditBox::KeyboardReturnType returnType)
{
_keyboardReturnType = returnType;
this->setNativeReturnType(returnType);
}
void EditBoxImplCommon::refreshInactiveText()
{
setInactiveText(_text.c_str());
refreshLabelAlignment();
2021-12-25 10:04:45 +08:00
if (!_editingMode)
{
if (_text.empty())
{
2019-11-23 20:27:39 +08:00
_label->setVisible(false);
_labelPlaceHolder->setVisible(true);
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
_label->setVisible(true);
_labelPlaceHolder->setVisible(false);
}
}
}
void EditBoxImplCommon::refreshLabelAlignment()
{
_label->setHorizontalAlignment(_alignment);
_labelPlaceHolder->setHorizontalAlignment(_alignment);
}
void EditBoxImplCommon::setText(const char* text)
{
2021-12-25 10:04:45 +08:00
if (nullptr != text)
{
2019-11-23 20:27:39 +08:00
this->setNativeText(text);
_text = text;
refreshInactiveText();
}
}
void EditBoxImplCommon::setPlaceHolder(const char* pText)
{
if (pText != NULL)
{
_placeHolder = pText;
this->setNativePlaceHolder(pText);
_labelPlaceHolder->setString(_placeHolder);
}
}
void EditBoxImplCommon::setVisible(bool visible)
{
2021-12-25 10:04:45 +08:00
if (visible)
{
2019-11-23 20:27:39 +08:00
refreshInactiveText();
2021-12-25 10:04:45 +08:00
}
else
{
2019-11-23 20:27:39 +08:00
this->setNativeVisible(visible);
_label->setVisible(visible);
_labelPlaceHolder->setVisible(visible);
}
}
2021-10-23 23:27:14 +08:00
void EditBoxImplCommon::setContentSize(const Vec2& size)
2019-11-23 20:27:39 +08:00
{
_contentSize = applyPadding(size);
AXLOGD("[Edit text] content size = ({}, {})", _contentSize.width, _contentSize.height);
2019-11-23 20:27:39 +08:00
placeInactiveLabels(_contentSize);
}
void EditBoxImplCommon::setGlobalZOrder(float globalZOrder)
{
if (_label)
{
_label->setGlobalZOrder(globalZOrder);
}
if (_labelPlaceHolder)
{
_labelPlaceHolder->setGlobalZOrder(globalZOrder);
}
}
2019-11-23 20:27:39 +08:00
void EditBoxImplCommon::draw(Renderer* /*renderer*/, const Mat4& /*transform*/, uint32_t flags)
{
2021-12-25 10:04:45 +08:00
if (flags)
2019-11-23 20:27:39 +08:00
{
auto rect = ui::Helper::convertBoundingBoxToScreen(_editBox);
this->updateNativeFrame(rect);
}
}
void EditBoxImplCommon::onEnter(void)
{
const char* pText = getText();
2021-12-25 10:04:45 +08:00
if (pText)
{
2019-11-23 20:27:39 +08:00
setInactiveText(pText);
}
}
void EditBoxImplCommon::openKeyboard()
{
_editBox->setBrightStyle(Widget::BrightStyle::HIGHLIGHT);
_label->setVisible(false);
_labelPlaceHolder->setVisible(false);
_editingMode = true;
this->setNativeVisible(true);
this->nativeOpenKeyboard();
}
void EditBoxImplCommon::closeKeyboard()
{
this->nativeCloseKeyboard();
_editingMode = false;
}
void EditBoxImplCommon::onEndEditing(std::string_view /*text*/)
2019-11-23 20:27:39 +08:00
{
_editBox->setBrightStyle(Widget::BrightStyle::NORMAL);
_editingMode = false;
this->setNativeVisible(false);
refreshInactiveText();
}
void EditBoxImplCommon::editBoxEditingDidBegin()
{
// LOGD("textFieldShouldBeginEditing...");
2022-08-08 18:02:17 +08:00
ax::ui::EditBoxDelegate* pDelegate = _editBox->getDelegate();
2019-11-23 20:27:39 +08:00
if (pDelegate != nullptr)
{
pDelegate->editBoxEditingDidBegin(_editBox);
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
{
2022-08-08 18:02:17 +08:00
ax::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "began", _editBox);
ax::ScriptEvent event(ax::kCommonEvent, (void*)&data);
ax::ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
}
#endif
}
void EditBoxImplCommon::editBoxEditingDidEnd(std::string_view text, EditBoxDelegate::EditBoxEndAction action)
2019-11-23 20:27:39 +08:00
{
// LOGD("textFieldShouldEndEditing...");
_text = text;
2022-08-08 18:02:17 +08:00
ax::ui::EditBoxDelegate* pDelegate = _editBox->getDelegate();
2019-11-23 20:27:39 +08:00
if (pDelegate != nullptr)
{
pDelegate->editBoxEditingDidEndWithAction(_editBox, action);
pDelegate->editBoxReturn(_editBox);
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
if (_editBox != nullptr && 0 != _editBox->getScriptEditBoxHandler())
{
2022-08-08 18:02:17 +08:00
ax::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "ended", _editBox);
ax::ScriptEvent event(ax::kCommonEvent, (void*)&data);
ax::ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
memset(data.eventName, 0, sizeof(data.eventName));
strncpy(data.eventName, "return", sizeof(data.eventName));
2021-12-25 10:04:45 +08:00
event.data = (void*)&data;
2022-08-08 18:02:17 +08:00
ax::ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
}
#endif
if (_editBox != nullptr)
{
this->onEndEditing(_text);
}
}
void EditBoxImplCommon::editBoxEditingChanged(std::string_view text)
2019-11-23 20:27:39 +08:00
{
// LOGD("editBoxTextChanged...");
2022-08-08 18:02:17 +08:00
ax::ui::EditBoxDelegate* pDelegate = _editBox->getDelegate();
2021-12-25 10:04:45 +08:00
_text = text;
2019-11-23 20:27:39 +08:00
if (pDelegate != nullptr)
{
pDelegate->editBoxTextChanged(_editBox, text);
}
2022-07-16 10:43:05 +08:00
#if AX_ENABLE_SCRIPT_BINDING
2019-11-23 20:27:39 +08:00
if (NULL != _editBox && 0 != _editBox->getScriptEditBoxHandler())
{
2022-08-08 18:02:17 +08:00
ax::CommonScriptData data(_editBox->getScriptEditBoxHandler(), "changed", _editBox);
ax::ScriptEvent event(ax::kCommonEvent, (void*)&data);
ax::ScriptEngineManager::sendEventToLua(event);
2019-11-23 20:27:39 +08:00
}
#endif
}
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
NS_AX_END