axmol/core/ui/UITextField.h

655 lines
16 KiB
C
Raw Normal View History

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-08-08 18:02:17 +08:00
https://axys1.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.
****************************************************************************/
#ifndef __UITEXTFIELD_H__
#define __UITEXTFIELD_H__
#include "ui/UIWidget.h"
#include "2d/CCTextFieldTTF.h"
#include "ui/GUIExport.h"
NS_AX_BEGIN
2019-11-23 20:27:39 +08:00
/**
* @addtogroup ui
* @{
*/
2021-12-25 10:04:45 +08:00
namespace ui
{
2019-11-23 20:27:39 +08:00
/**
* @brief A helper class which inherit from @see `TextFieldTTF` and implements the @see `TextFieldDelegate` protocol.
* It is mainly be used internally by @see `UITextField` class.
* @js NA
* @lua NA
*/
2022-07-16 10:43:05 +08:00
class AX_GUI_DLL UICCTextField : public TextFieldTTF, public TextFieldDelegate
2019-11-23 20:27:39 +08:00
{
public:
/**
* @brief Create an empty UICCTextField.
*
* @return A UICCTextField instance.
*/
static UICCTextField* create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Default constructor
*/
UICCTextField();
/**
* Default destructor
*/
~UICCTextField();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void onEnter() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Create a UICCTextField instance with a placeholder, a fontName and a fontSize.
*@param placeholder Placeholder in string.
*@param fontName Font name in string.
*@param fontSize Font size in float.
*@return A UICCTextField instance.
*/
static UICCTextField* create(std::string_view placeholder, std::string_view fontName, float fontSize);
2021-12-25 10:04:45 +08:00
// override functions
virtual bool onTextFieldAttachWithIME(TextFieldTTF* pSender) override;
virtual bool onTextFieldDetachWithIME(TextFieldTTF* pSender) override;
virtual bool onTextFieldInsertText(TextFieldTTF* pSender, const char* text, size_t nLen) override;
virtual bool onTextFieldDeleteBackward(TextFieldTTF* pSender, const char* delText, size_t nLen) override;
2019-11-23 20:27:39 +08:00
void insertText(const char* text, size_t len) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Open up the IME.
*/
void openIME();
/**
* Close the IME.
*/
void closeIME();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Toggle enable max length limitation.
*@param enable True to enable max length, false otherwise.
*/
void setMaxLengthEnabled(bool enable);
/**
* Query max length enable state.
*@return Whether max length is enabled or not.
*/
2021-12-25 10:04:45 +08:00
bool isMaxLengthEnabled() const;
2019-11-23 20:27:39 +08:00
/**
* Set maximize length.
*@param length The maximize length in integer.
*/
void setMaxLength(int length);
/**
* Get maximize length.
*@return Maximize length.
*/
2021-12-25 10:04:45 +08:00
int getMaxLength() const;
2019-11-23 20:27:39 +08:00
/**
* Return the total inputed characters.
*@return Total inputed character count.
*/
2021-12-25 10:04:45 +08:00
std::size_t getCharCount() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle password input mode.
*
* @param enable True if enable password input, false otherwise.
*/
void setPasswordEnabled(bool enable);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether password input mode is enabled or not.
*
* @return True if password input is enabled, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool isPasswordEnabled() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Change password style text.
*
* @param styleText The styleText for password mask, the default value is "*".
*/
void setPasswordStyleText(std::string_view styleText);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Set the password text content.
*
* @param text The content of password.
*/
void setPasswordText(std::string_view text);
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle attach with IME.
*
* @param attach True if attach with IME, false otherwise.
*/
void setAttachWithIME(bool attach);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether the IME is attached or not.
*
* @return True if IME is attached, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getAttachWithIME() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle detach with IME.
*
* @param detach True if detach with IME, false otherwise.
*/
void setDetachWithIME(bool detach);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether IME is detached or not.
*
* @return True if IME is detached, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getDetachWithIME() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle enable text insert.
*
* @param insert True if enable insert text, false otherwise.
*/
void setInsertText(bool insert);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether insert text is enabled or not.
*
* @return True if insert text is enabled, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getInsertText() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle enable delete backward.
*
* @param deleteBackward True if enable delete backward, false otherwise.
*/
void setDeleteBackward(bool deleteBackward);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether delete backward is enabled or not.
*
* @return True if delete backward is enabled, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getDeleteBackward() const;
2019-11-23 20:27:39 +08:00
protected:
bool _maxLengthEnabled;
int _maxLength;
bool _attachWithIME;
bool _detachWithIME;
bool _insertText;
bool _deleteBackward;
};
/**
* @brief A widget which allows users to input text.
* The rendering of the input text are based on @see `TextFieldTTF'.
* If you want to use system control behavior, please use @see `EditBox` instead.
* @js NA
* @lua NA
*/
2022-07-16 10:43:05 +08:00
class AX_GUI_DLL TextField : public Widget
2019-11-23 20:27:39 +08:00
{
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
DECLARE_CLASS_GUI_INFO
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
public:
/**
* TextField event type.
*/
enum class EventType
{
ATTACH_WITH_IME,
DETACH_WITH_IME,
INSERT_TEXT,
DELETE_BACKWARD,
};
/**
* A callback which would be called when a TextField event happens.
*/
typedef std::function<void(Ref*, EventType)> ccTextFieldCallback;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Default constructor.
*
*/
TextField();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Default destructor.
*/
virtual ~TextField();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Create an empty TextField.
*
* @return A TextField instance.
*/
static TextField* create();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Create a TextField with a placeholder, a font name and a font size.
*
* @param placeholder The placeholder string.
* @param fontName The font name.
* @param fontSize The font size.
* @return A TextField instance.
*/
static TextField* create(std::string_view placeholder, std::string_view fontName, int fontSize);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Set the touch size
* The touch size is used for @see `hitTest`.
* @param size A delimitation zone.
*/
2021-12-25 10:04:45 +08:00
void setTouchSize(const Vec2& size);
2019-11-23 20:27:39 +08:00
/**
* @brief Get current touch size of TextField.
*
* @return The TextField's touch size.
*/
2021-12-25 10:04:45 +08:00
Vec2 getTouchSize() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle enable touch area.
*
* @param enable True if enable touch area, false otherwise.
*/
void setTouchAreaEnabled(bool enable);
2021-12-25 10:04:45 +08:00
virtual bool hitTest(const Vec2& pt, const Camera* camera, Vec3* p) const override;
2019-11-23 20:27:39 +08:00
/**
* @brief Set placeholder of TextField.
*
* @param value The string value of placeholder.
*/
void setPlaceHolder(std::string_view value);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Get the placeholder of TextField.
*
* @return A placeholder string.
*/
std::string_view getPlaceHolder() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query the placeholder string color.
*
* @return The color of placeholder.
*/
2021-12-25 10:04:45 +08:00
const Color4B& getPlaceHolderColor() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Change the placeholder color.
*
* @param color A color value in `Color3B`.
*/
void setPlaceHolderColor(const Color3B& color);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Change the placeholder color.
*
* @param color A color value in `Color4B`.
*/
void setPlaceHolderColor(const Color4B& color);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query the text string color.
*
* @return The color of the text.
*/
2021-12-25 10:04:45 +08:00
const Color4B& getTextColor() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Change the text color.
*
* @param textColor The color value in `Color4B`.
*/
void setTextColor(const Color4B& textColor);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Change font size of TextField.
*
* @param size The integer font size.
*/
void setFontSize(int size);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query the font size.
*
* @return The integer font size.
*/
2021-12-25 10:04:45 +08:00
int getFontSize() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Change the font name of TextField.
*
* @param name The font name string.
*/
void setFontName(std::string_view name);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query the TextField's font name.
*
* @return The font name string.
*/
std::string_view getFontName() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Detach the IME.
*/
virtual void didNotSelectSelf();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
*Change content of TextField.
*@param text A string content.
*/
void setString(std::string_view text);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
*Query the content of TextField.
*@return The string value of TextField.
*/
std::string_view getString() const;
2021-12-25 10:04:45 +08:00
virtual bool onTouchBegan(Touch* touch, Event* unusedEvent) override;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle maximize length enable
*
* @param enable True if enable maximize length, false otherwise.
*/
void setMaxLengthEnabled(bool enable);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether max length is enabled or not.
*
* @return True if maximize length is enabled, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool isMaxLengthEnabled() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Change maximize input length limitation.
*
* @param length A character count in integer.
*/
void setMaxLength(int length);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query maximize input length of TextField.
*
* @return The integer value of maximize input length.
*/
2021-12-25 10:04:45 +08:00
int getMaxLength() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Query the input string length.
*
* @return A integer length value.
*/
int getStringLength() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle enable password input mode.
*
* @param enable True if enable password input mode, false otherwise.
*/
void setPasswordEnabled(bool enable);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether password is enabled or not.
*
* @return True if password is enabled, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool isPasswordEnabled() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Change password style text.
*
* @param styleText The styleText for password mask, the default value is "*".
*/
2022-06-20 01:49:50 +08:00
void setPasswordStyleText(std::string_view styleText);
2019-11-23 20:27:39 +08:00
/**
* @brief Query the password style text.
*
* @return A password style text.
*/
2022-06-20 01:49:50 +08:00
std::string_view getPasswordStyleText() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual void update(float dt) override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether the IME is attached or not.
*
* @return True if IME is attached, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getAttachWithIME() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle attach with IME.
*
* @param attach True if attach with IME, false otherwise.
*/
void setAttachWithIME(bool attach);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Query whether IME is detached or not.
*
* @return True if IME is detached, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getDetachWithIME() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle detach with IME.
*
* @param detach True if detach with IME, false otherwise.
*/
void setDetachWithIME(bool detach);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Whether it is ready to get the inserted text or not.
*
* @return True if the insert text is ready, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getInsertText() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle enable insert text mode
*
* @param insertText True if enable insert text, false otherwise.
*/
void setInsertText(bool insertText);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Whether it is ready to delete backward in TextField.
*
* @return True is the delete backward is enabled, false otherwise.
*/
2021-12-25 10:04:45 +08:00
bool getDeleteBackward() const;
2019-11-23 20:27:39 +08:00
/**
* @brief Toggle enable delete backward mode.
*
* @param deleteBackward True is delete backward is enabled, false otherwise.
*/
void setDeleteBackward(bool deleteBackward);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Add a event listener to TextField, when some predefined event happens, the callback will be called.
*@param callback A callback function with type of `ccTextFieldCallback`.
*/
void addEventListener(const ccTextFieldCallback& callback);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Returns the "class name" of widget.
*/
virtual std::string getDescription() const override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Get the renderer size in auto mode.
*
* @return A delimitation zone.
*/
2021-10-23 23:27:14 +08:00
virtual Vec2 getAutoRenderSize();
2021-12-25 10:04:45 +08:00
// override functions.
2021-10-23 23:27:14 +08:00
virtual Vec2 getVirtualRendererSize() const override;
2019-11-23 20:27:39 +08:00
virtual Node* getVirtualRenderer() override;
virtual void onEnter() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Attach the IME for inputing.
*
*/
void attachWithIME();
2021-12-25 10:04:45 +08:00
/**
* @brief Detach the IME from inputing.
*
*/
void detachWithIME();
2019-11-23 20:27:39 +08:00
/**
* @brief Change the text area size.
*
* @param size A delimitation zone.
*/
2021-12-25 10:04:45 +08:00
void setTextAreaSize(const Vec2& size);
2019-11-23 20:27:39 +08:00
/**
* @brief Change horizontal text alignment.
*
* @param alignment A alignment arguments in @see `TextHAlignment`.
*/
void setTextHorizontalAlignment(TextHAlignment alignment);
/**
* @brief Inquire the horizontal alignment
*
* @return The horizontal alignment
*/
TextHAlignment getTextHorizontalAlignment() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* @brief Change the vertical text alignment.
*
* @param alignment A alignment arguments in @see `TextVAlignment`.
*/
void setTextVerticalAlignment(TextVAlignment alignment);
/**
* @brief Inquire the horizontal alignment
*
* @return The horizontal alignment
*/
TextVAlignment getTextVerticalAlignment() const;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set enable cursor use.
* @js NA
*/
void setCursorEnabled(bool enabled);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set char showing cursor.
* @js NA
*/
void setCursorChar(char cursor);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set cursor position, if enabled
* @js NA
*/
void setCursorPosition(std::size_t cursorPosition);
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
/**
* Set cursor position to hit letter, if enabled
* @js NA
*/
2021-12-25 10:04:45 +08:00
void setCursorFromPoint(const Vec2& point, const Camera* camera);
virtual bool init() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
virtual void initRenderer() override;
void attachWithIMEEvent();
void detachWithIMEEvent();
void insertTextEvent();
void deleteBackwardEvent();
virtual void onSizeChanged() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
void textfieldRendererScaleChangedWithSize();
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
virtual Widget* createCloneInstance() override;
virtual void copySpecialProperties(Widget* model) override;
virtual void adaptRenderers() override;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
protected:
UICCTextField* _textFieldRenderer;
float _touchWidth;
float _touchHeight;
bool _useTouchArea;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
Ref* _textFieldEventListener;
ccTextFieldCallback _eventCallback;
2021-12-25 10:04:45 +08:00
2019-11-23 20:27:39 +08:00
bool _textFieldRendererAdaptDirty;
2021-12-25 10:04:45 +08:00
2020-10-07 00:07:53 +08:00
protected:
2019-11-23 20:27:39 +08:00
enum class FontType
{
SYSTEM,
TTF,
BMFONT
};
std::string _fontName;
int _fontSize;
FontType _fontType;
};
2021-12-25 10:04:45 +08:00
} // namespace ui
2019-11-23 20:27:39 +08:00
// end of ui group
/// @}
NS_AX_END
2019-11-23 20:27:39 +08:00
#endif /* defined(__TextField__) */