CCTextFieldTTF.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #ifndef __CC_TEXT_FIELD_H__
  23. #define __CC_TEXT_FIELD_H__
  24. #include "2d/CCLabel.h"
  25. #include "base/CCIMEDelegate.h"
  26. /**
  27. * @addtogroup ui
  28. * @{
  29. */
  30. NS_CC_BEGIN
  31. class TextFieldTTF;
  32. /**
  33. * A input protocol for TextField.
  34. */
  35. class CC_DLL TextFieldDelegate
  36. {
  37. public:
  38. /**
  39. * Destructor for TextFieldDelegate.
  40. * @js NA
  41. */
  42. virtual ~TextFieldDelegate() {}
  43. /**
  44. *@brief If the sender doesn't want to attach to the IME, return true.
  45. */
  46. virtual bool onTextFieldAttachWithIME(TextFieldTTF* sender);
  47. /**
  48. *@brief If the sender doesn't want to detach from the IME, return true.
  49. */
  50. virtual bool onTextFieldDetachWithIME(TextFieldTTF* sender);
  51. /**
  52. *@brief If the sender doesn't want to insert the text, return true.
  53. */
  54. virtual bool onTextFieldInsertText(TextFieldTTF* sender, const char* text, size_t nLen);
  55. /**
  56. *@brief If the sender doesn't want to delete the delText, return true.
  57. */
  58. virtual bool onTextFieldDeleteBackward(TextFieldTTF* sender, const char* delText, size_t nLen);
  59. /**
  60. *@brief If the sender doesn't want to draw, return true.
  61. * @js NA
  62. */
  63. virtual bool onVisit(TextFieldTTF* sender, Renderer* renderer, const Mat4& transform, uint32_t flags);
  64. };
  65. /**
  66. *@brief A simple text input field with TTF font.
  67. */
  68. class CC_DLL TextFieldTTF : public Label, public IMEDelegate
  69. {
  70. public:
  71. /**
  72. * Default constructor.
  73. * @js ctor
  74. */
  75. TextFieldTTF();
  76. /**
  77. * Default destructor.
  78. * @js NA
  79. * @lua NA
  80. */
  81. virtual ~TextFieldTTF();
  82. /** Creates a TextFieldTTF from a fontname, alignment, dimension and font size.
  83. * @js NA
  84. */
  85. static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
  86. /** Creates a TextFieldTTF from a fontname and font size.
  87. * @js NA
  88. */
  89. static TextFieldTTF * textFieldWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
  90. /** Initializes the TextFieldTTF with a font name, alignment, dimension and font size. */
  91. bool initWithPlaceHolder(const std::string& placeholder, const Size& dimensions, TextHAlignment alignment, const std::string& fontName, float fontSize);
  92. /** Initializes the TextFieldTTF with a font name and font size. */
  93. bool initWithPlaceHolder(const std::string& placeholder, const std::string& fontName, float fontSize);
  94. /**
  95. *@brief Open keyboard and receive input text.
  96. */
  97. virtual bool attachWithIME() override;
  98. /**
  99. *@brief End text input and close keyboard.
  100. */
  101. virtual bool detachWithIME() override;
  102. //////////////////////////////////////////////////////////////////////////
  103. // properties
  104. //////////////////////////////////////////////////////////////////////////
  105. /**
  106. * @lua NA
  107. */
  108. TextFieldDelegate* getDelegate() const { return _delegate; }
  109. /**
  110. * @lua NA
  111. */
  112. void setDelegate(TextFieldDelegate* delegate) { _delegate = delegate; }
  113. /**
  114. * Query the currently inputed character count.
  115. *@return The total input character count.
  116. */
  117. std::size_t getCharCount() const { return _charCount; }
  118. /**
  119. * Query the color of place holder.
  120. *@return The place holder color.
  121. */
  122. virtual const Color4B& getColorSpaceHolder();
  123. /**
  124. *@brief Change input placeholder color.
  125. *@param color A color value in `Color3B`.
  126. */
  127. virtual void setColorSpaceHolder(const Color3B& color);
  128. /**
  129. * Change the placeholder color.
  130. *@param color The placeholder color in Color4B.
  131. */
  132. virtual void setColorSpaceHolder(const Color4B& color);
  133. /**
  134. * Change the color of input text.
  135. *@param textColor The text color in Color4B.
  136. */
  137. virtual void setTextColor(const Color4B& textColor) override;
  138. /**
  139. * Change input text of TextField.
  140. *@param text The input text of TextField.
  141. */
  142. virtual void setString(const std::string& text) override;
  143. /**
  144. * Append to input text of TextField.
  145. *@param text The append text of TextField.
  146. */
  147. virtual void appendString(const std::string& text);
  148. /**
  149. * Query the input text of TextField.
  150. *@return Get the input text of TextField.
  151. */
  152. virtual const std::string& getString() const override;
  153. /**
  154. * Change placeholder text.
  155. * place holder text displayed when there is no text in the text field.
  156. *@param text The placeholder string.
  157. */
  158. virtual void setPlaceHolder(const std::string& text);
  159. /**
  160. * Query the placeholder string.
  161. *@return The placeholder string.
  162. */
  163. virtual const std::string& getPlaceHolder() const;
  164. /**
  165. * Set enable secure text entry representation.
  166. * If you want to display password in TextField, this option is very helpful.
  167. *@param value Whether or not to display text with secure text entry.
  168. * @js NA
  169. */
  170. virtual void setSecureTextEntry(bool value);
  171. virtual void setPasswordTextStyle(const std::string& text);
  172. const std::string& getPasswordTextStyle() const;
  173. /**
  174. * Query whether the currently display mode is secure text entry or not.
  175. *@return Whether current text is displayed as secure text entry.
  176. * @js NA
  177. */
  178. virtual bool isSecureTextEntry()const;
  179. virtual void visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  180. virtual void update(float delta) override;
  181. /**
  182. * Set enable cursor use.
  183. * @js NA
  184. */
  185. void setCursorEnabled(bool enabled);
  186. /**
  187. * Set char showing cursor.
  188. * @js NA
  189. */
  190. void setCursorChar(char cursor);
  191. /**
  192. * Set cursor position, if enabled
  193. * @js NA
  194. */
  195. void setCursorPosition(std::size_t cursorPosition);
  196. /**
  197. * Set cursor position to hit letter, if enabled
  198. * @js NA
  199. */
  200. void setCursorFromPoint(const Vec2 &point, const Camera* camera);
  201. protected:
  202. //////////////////////////////////////////////////////////////////////////
  203. // IMEDelegate interface
  204. //////////////////////////////////////////////////////////////////////////
  205. virtual bool canAttachWithIME() override;
  206. virtual bool canDetachWithIME() override;
  207. virtual void didAttachWithIME() override;
  208. virtual void didDetachWithIME() override;
  209. virtual void insertText(const char * text, size_t len) override;
  210. virtual void deleteBackward() override;
  211. virtual const std::string& getContentText() override;
  212. virtual void controlKey(EventKeyboard::KeyCode keyCode) override;
  213. TextFieldDelegate * _delegate;
  214. std::size_t _charCount;
  215. std::string _inputText;
  216. std::string _placeHolder;
  217. Color4B _colorSpaceHolder;
  218. Color4B _colorText;
  219. bool _secureTextEntry;
  220. std::string _passwordStyleText;
  221. // Need use cursor
  222. bool _cursorEnabled;
  223. // Current position cursor
  224. std::size_t _cursorPosition;
  225. // Char showing cursor
  226. char _cursorChar;
  227. // >0 - show, <0 - hide
  228. float _cursorShowingTime;
  229. bool _isAttachWithIME;
  230. void makeStringSupportCursor(std::string& displayText);
  231. void updateCursorDisplayText();
  232. void setAttachWithIME(bool isAttachWithIME);
  233. void setTextColorInternally(const Color4B& color);
  234. private:
  235. class LengthStack;
  236. LengthStack * _lens;
  237. };
  238. NS_CC_END
  239. // end of ui group
  240. /// @}
  241. #endif // __CC_TEXT_FIELD_H__