axmol/cocos/ui/UITextFieldEx.h

207 lines
5.5 KiB
C
Raw Normal View History

2019-11-24 23:15:56 +08:00
//
// Copyright (c) 2014~2017 purelib - All Rights Reserved
//
#ifndef _UITEXTFIELDEX_H_
#define _UITEXTFIELDEX_H_
#include "cocos2d.h"
2020-08-04 00:14:35 +08:00
#include "base/ccMacros.h"
#include "base/SimpleTimer.h"
2019-11-24 23:15:56 +08:00
NS_CC_BEGIN
2021-12-25 10:04:45 +08:00
namespace ui
{
/**
@brief A extension implementation of ui::TextField
*/
class CC_DLL TextFieldEx : public cocos2d::Node, public IMEDelegate
{
public:
2019-11-24 23:15:56 +08:00
/**
2021-12-25 10:04:45 +08:00
* @js ctor
*/
TextFieldEx();
/**
* @js NA
* @lua NA
*/
virtual ~TextFieldEx();
2019-11-24 23:15:56 +08:00
2021-12-26 23:26:34 +08:00
static TextFieldEx* create(std::string_view placeholder,
std::string_view fontName,
2021-12-25 10:04:45 +08:00
float fontSize,
float cursorWidth = 2,
const Color4B& color = Color4B::WHITE);
2019-11-24 23:15:56 +08:00
2021-12-26 23:26:34 +08:00
bool initWithPlaceHolder(std::string_view placeholder,
std::string_view fontName,
2021-12-25 10:04:45 +08:00
float fontSize,
float cursorWidth = 2,
const Color4B& color = Color4B::WHITE);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void enableIME(Node* control);
void disableIME(void);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Label* getRenderLabel();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
inline int getCharCount() const { return charCount; };
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual void setPlaceholderColor(const Color4B& color);
virtual const Color4B& getPlaceholderColor() const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual void setTextColor(const Color4B& textColor);
virtual const Color4B& getTextColor(void) const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void setCursorColor(const Color3B& color);
const Color3B& getCursorColor(void) const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// input text property
2021-12-26 23:26:34 +08:00
virtual void setString(std::string_view text);
virtual std::string_view getString() const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// Continuous touch event trigger support.
void setContinuousTouchDelayTime(float delay) { _continuousTouchDelayTime = delay; }
float getContinuousTouchDelayTime() const { return _continuousTouchDelayTime; }
void setContinuousTouchCallback(std::function<void(const Point& worldPoint)> callback)
{
_continuousTouchCallback = std::move(callback);
}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// place holder text property
// place holder text displayed when there is no text in the text field.
2021-12-26 23:26:34 +08:00
virtual void setPlaceholderText(std::string_view text);
virtual std::string_view getPlaceholderText(void) const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual void setPasswordEnabled(bool value);
virtual bool isPasswordEnabled() const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/*virtual void visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
* override;*/
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool empty(void) const { return this->charCount == 0 || this->inputText.empty(); }
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual const Size& getContentSize() const override;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual void setEnabled(bool bEnabled);
virtual bool isEnabled(void) const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void setEditable(bool bEditable) { editable = bEditable; }
bool isEditable(void) const { return editable; }
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void setMaxLength(int maxLength) { setCharLimit(maxLength); }
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int getFontType() const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/// fonts
void setTextFontSize(float size);
float getTextFontSize() const;
2021-12-26 23:26:34 +08:00
void setTextFontName(std::string_view fontName);
std::string_view getTextFontName() const;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
CC_SYNTHESIZE(size_t, charLimit, CharLimit);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool isSystemFont(void) const { return systemFontUsed; }
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
public:
std::function<void(void)> onTextModify;
std::function<void(void)> onOpenIME;
std::function<void(void)> onCloseIME;
// IMEDelegate interface
//////////////////////////////////////////////////////////////////////////
void openIME(void);
void closeIME(void);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual void insertText(const char* text, size_t len) override;
2020-08-04 00:14:35 +08:00
2021-12-25 10:04:45 +08:00
protected:
//////////////////////////////////////////////////////////////////////////
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual bool canAttachWithIME() override;
virtual bool canDetachWithIME() override;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
virtual void deleteBackward() override;
2021-12-26 23:26:34 +08:00
virtual std::string_view getContentText() override;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void handleDeleteKeyEvent();
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/**
@brief Open keyboard and receive input text.
*/
virtual bool attachWithIME() override;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
/**
@brief End text input and close keyboard.
*/
virtual bool detachWithIME() override;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void keyboardDidShow(IMEKeyboardNotificationInfo& /*info*/) override;
void keyboardDidHide(IMEKeyboardNotificationInfo& /*info*/) override;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void updateContentSize(void);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void __initCursor(int height, int width = 6, const Color4B& color = Color4B::WHITE);
void __showCursor(void);
void __hideCursor(void);
void __updateCursorPosition(void);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void __moveCursor(int direction);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
void __moveCursorTo(float x);
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
protected:
bool systemFontUsed;
std::string fontName;
float fontSize;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool editable;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Label* renderLabel;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
size_t charCount;
std::string inputText;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
std::string placeHolder;
Color4B colorSpaceHolder;
Color4B colorText;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool secureTextEntry;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
Sprite* cursor;
bool cursorVisible;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int insertPosUtf8;
int insertPos; // The actual input content insertPos, step: bytes
int cursorPos; // The cursor normalzed pos,
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool enabled;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
EventListenerTouchOneByOne* touchListener;
EventListenerKeyboard* kbdListener;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
bool touchCursorControlEnabled;
float asteriskWidth;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
int _fontType;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
cocos2d::stimer::TIMER_ID _continuousTouchDelayTimerID;
float _continuousTouchDelayTime;
std::function<void(const Point& worldPoint)> _continuousTouchCallback;
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
static bool s_keyboardVisible;
};
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
// end of input group
/// @}
2019-11-24 23:15:56 +08:00
2021-12-25 10:04:45 +08:00
}; // namespace ui
2019-11-24 23:15:56 +08:00
NS_CC_END
#ifdef _UITEXTFIELDEX_INLINE_
2021-12-25 10:04:45 +08:00
# include "UITextFieldEx.cpp"
2019-11-24 23:15:56 +08:00
#endif
#endif