axmol/cocos/ui/UITextFieldEx.h

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