2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
https://axys1.github.io/
|
2012-04-19 14:35:52 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#ifndef __AX_TEXT_FIELD_H__
|
|
|
|
#define __AX_TEXT_FIELD_H__
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "2d/CCLabel.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "base/CCIMEDelegate.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
* @addtogroup ui
|
2012-06-20 18:09:11 +08:00
|
|
|
* @{
|
|
|
|
*/
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2015-03-26 15:47:14 +08:00
|
|
|
|
|
|
|
class TextFieldTTF;
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* A input protocol for TextField.
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL TextFieldDelegate
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
2015-09-22 16:08:23 +08:00
|
|
|
* Destructor for TextFieldDelegate.
|
2015-03-28 14:28:16 +08:00
|
|
|
* @js NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2013-11-20 08:57:23 +08:00
|
|
|
virtual ~TextFieldDelegate() {}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief If the sender doesn't want to attach to the IME, return true.
|
|
|
|
*/
|
2016-11-16 09:48:37 +08:00
|
|
|
virtual bool onTextFieldAttachWithIME(TextFieldTTF* sender);
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief If the sender doesn't want to detach from the IME, return true.
|
|
|
|
*/
|
2016-11-16 09:48:37 +08:00
|
|
|
virtual bool onTextFieldDetachWithIME(TextFieldTTF* sender);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief If the sender doesn't want to insert the text, return true.
|
|
|
|
*/
|
2016-11-16 09:48:37 +08:00
|
|
|
virtual bool onTextFieldInsertText(TextFieldTTF* sender, const char* text, size_t nLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief If the sender doesn't want to delete the delText, return true.
|
|
|
|
*/
|
2016-11-16 09:48:37 +08:00
|
|
|
virtual bool onTextFieldDeleteBackward(TextFieldTTF* sender, const char* delText, size_t nLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief If the sender doesn't want to draw, return true.
|
2015-03-28 14:28:16 +08:00
|
|
|
* @js NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2016-11-16 09:48:37 +08:00
|
|
|
virtual bool onVisit(TextFieldTTF* sender, Renderer* renderer, const Mat4& transform, uint32_t flags);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief A simple text input field with TTF font.
|
|
|
|
*/
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL TextFieldTTF : public Label, public IMEDelegate
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
2015-03-27 20:56:12 +08:00
|
|
|
* Default constructor.
|
2013-09-13 16:46:31 +08:00
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
TextFieldTTF();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
2015-03-27 20:56:12 +08:00
|
|
|
* Default destructor.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
virtual ~TextFieldTTF();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/** Creates a TextFieldTTF from a fontname, alignment, dimension and font size.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static 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);
|
|
|
|
|
2015-03-28 14:28:16 +08:00
|
|
|
/** Creates a TextFieldTTF from a fontname and font size.
|
2021-12-25 10:04:45 +08:00
|
|
|
* @js NA
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
static TextFieldTTF* textFieldWithPlaceHolder(std::string_view placeholder,
|
|
|
|
std::string_view fontName,
|
2021-12-25 10:04:45 +08:00
|
|
|
float fontSize);
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Initializes the TextFieldTTF with a font name, alignment, dimension and font size. */
|
2021-12-26 23:26:34 +08:00
|
|
|
bool 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);
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/** Initializes the TextFieldTTF with a font name and font size. */
|
2021-12-26 23:26:34 +08:00
|
|
|
bool initWithPlaceHolder(std::string_view placeholder, std::string_view fontName, float fontSize);
|
2011-04-19 09:32:33 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief Open keyboard and receive input text.
|
|
|
|
*/
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual bool attachWithIME() override;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2015-03-25 18:17:58 +08:00
|
|
|
*@brief End text input and close keyboard.
|
|
|
|
*/
|
2015-03-15 02:33:15 +08:00
|
|
|
virtual bool detachWithIME() override;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2011-04-19 09:32:33 +08:00
|
|
|
// properties
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
TextFieldDelegate* getDelegate() const { return _delegate; }
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
void setDelegate(TextFieldDelegate* delegate) { _delegate = delegate; }
|
2013-07-23 18:26:26 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
2015-10-23 15:37:33 +08:00
|
|
|
* Query the currently inputed character count.
|
2015-03-25 18:17:58 +08:00
|
|
|
*@return The total input character count.
|
|
|
|
*/
|
2016-09-12 09:44:21 +08:00
|
|
|
std::size_t getCharCount() const { return _charCount; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Query the color of place holder.
|
|
|
|
*@return The place holder color.
|
|
|
|
*/
|
2014-03-31 11:16:12 +08:00
|
|
|
virtual const Color4B& getColorSpaceHolder();
|
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
*@brief Change input placeholder color.
|
2015-03-26 15:47:14 +08:00
|
|
|
*@param color A color value in `Color3B`.
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2013-07-05 16:49:22 +08:00
|
|
|
virtual void setColorSpaceHolder(const Color3B& color);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the placeholder color.
|
|
|
|
*@param color The placeholder color in Color4B.
|
|
|
|
*/
|
2014-03-31 11:16:12 +08:00
|
|
|
virtual void setColorSpaceHolder(const Color4B& color);
|
2011-04-28 18:01:29 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Change the color of input text.
|
|
|
|
*@param textColor The text color in Color4B.
|
|
|
|
*/
|
2014-03-31 11:16:12 +08:00
|
|
|
virtual void setTextColor(const Color4B& textColor) override;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Change input text of TextField.
|
|
|
|
*@param text The input text of TextField.
|
|
|
|
*/
|
2021-12-01 03:35:34 +08:00
|
|
|
virtual void setString(std::string_view text) override;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Append to input text of TextField.
|
|
|
|
*@param text The append text of TextField.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void appendString(std::string_view text);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Query the input text of TextField.
|
|
|
|
*@return Get the input text of TextField.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual std::string_view getString() const override;
|
2011-04-19 09:32:33 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
|
|
|
* Change placeholder text.
|
|
|
|
* place holder text displayed when there is no text in the text field.
|
|
|
|
*@param text The placeholder string.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void setPlaceHolder(std::string_view text);
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Query the placeholder string.
|
|
|
|
*@return The placeholder string.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual std::string_view getPlaceHolder() const;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2015-03-25 18:17:58 +08:00
|
|
|
/**
|
2015-10-23 15:37:33 +08:00
|
|
|
* Set enable secure text entry representation.
|
2015-03-25 18:17:58 +08:00
|
|
|
* If you want to display password in TextField, this option is very helpful.
|
|
|
|
*@param value Whether or not to display text with secure text entry.
|
2015-03-28 14:28:16 +08:00
|
|
|
* @js NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2013-04-19 09:59:08 +08:00
|
|
|
virtual void setSecureTextEntry(bool value);
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual void setPasswordTextStyle(std::string_view text);
|
|
|
|
std::string_view getPasswordTextStyle() const;
|
2015-03-25 18:17:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Query whether the currently display mode is secure text entry or not.
|
|
|
|
*@return Whether current text is displayed as secure text entry.
|
2015-03-28 14:28:16 +08:00
|
|
|
* @js NA
|
2015-03-25 18:17:58 +08:00
|
|
|
*/
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual bool isSecureTextEntry() const;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void visit(Renderer* renderer, const Mat4& parentTransform, uint32_t parentFlags) override;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
virtual void update(float delta) override;
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Set enable cursor use.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2016-03-11 20:09:50 +08:00
|
|
|
void setCursorEnabled(bool enabled);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Set char showing cursor.
|
|
|
|
* @js NA
|
|
|
|
*/
|
2016-03-07 19:22:03 +08:00
|
|
|
void setCursorChar(char cursor);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Set cursor position, if enabled
|
|
|
|
* @js NA
|
|
|
|
*/
|
2016-03-07 19:22:03 +08:00
|
|
|
void setCursorPosition(std::size_t cursorPosition);
|
|
|
|
|
|
|
|
/**
|
2021-12-25 10:04:45 +08:00
|
|
|
* Set cursor position to hit letter, if enabled
|
|
|
|
* @js NA
|
|
|
|
*/
|
|
|
|
void setCursorFromPoint(const Vec2& point, const Camera* camera);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
2014-03-14 21:06:40 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2013-06-20 14:13:12 +08:00
|
|
|
// IMEDelegate interface
|
2012-04-19 14:35:52 +08:00
|
|
|
//////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2013-11-08 03:58:48 +08:00
|
|
|
virtual bool canAttachWithIME() override;
|
|
|
|
virtual bool canDetachWithIME() override;
|
2016-03-07 19:22:03 +08:00
|
|
|
virtual void didAttachWithIME() override;
|
|
|
|
virtual void didDetachWithIME() override;
|
2021-12-25 10:04:45 +08:00
|
|
|
virtual void insertText(const char* text, size_t len) override;
|
2013-11-08 03:58:48 +08:00
|
|
|
virtual void deleteBackward() override;
|
2021-12-26 23:26:34 +08:00
|
|
|
virtual std::string_view getContentText() override;
|
2016-03-07 19:22:03 +08:00
|
|
|
virtual void controlKey(EventKeyboard::KeyCode keyCode) override;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
TextFieldDelegate* _delegate;
|
2016-04-01 09:31:18 +08:00
|
|
|
std::size_t _charCount;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
|
|
|
std::string _inputText;
|
|
|
|
|
|
|
|
std::string _placeHolder;
|
2014-03-31 11:16:12 +08:00
|
|
|
Color4B _colorSpaceHolder;
|
|
|
|
Color4B _colorText;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
|
|
|
bool _secureTextEntry;
|
2016-04-01 00:52:55 +08:00
|
|
|
std::string _passwordStyleText;
|
2014-03-14 21:06:40 +08:00
|
|
|
|
2016-03-07 19:22:03 +08:00
|
|
|
// Need use cursor
|
2016-03-11 20:09:50 +08:00
|
|
|
bool _cursorEnabled;
|
2016-03-07 19:22:03 +08:00
|
|
|
// Current position cursor
|
|
|
|
std::size_t _cursorPosition;
|
|
|
|
// Char showing cursor
|
|
|
|
char _cursorChar;
|
|
|
|
// >0 - show, <0 - hide
|
|
|
|
float _cursorShowingTime;
|
|
|
|
|
|
|
|
bool _isAttachWithIME;
|
|
|
|
|
|
|
|
void makeStringSupportCursor(std::string& displayText);
|
|
|
|
void updateCursorDisplayText();
|
|
|
|
void setAttachWithIME(bool isAttachWithIME);
|
2017-12-29 11:24:02 +08:00
|
|
|
void setTextColorInternally(const Color4B& color);
|
2016-03-07 19:22:03 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
private:
|
|
|
|
class LengthStack;
|
2021-12-25 10:04:45 +08:00
|
|
|
LengthStack* _lens;
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2015-03-25 18:17:58 +08:00
|
|
|
// end of ui group
|
2012-06-20 18:09:11 +08:00
|
|
|
/// @}
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // __AX_TEXT_FIELD_H__
|