UITextField.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #ifndef __UITEXTFIELD_H__
  22. #define __UITEXTFIELD_H__
  23. #include "ui/UIWidget.h"
  24. #include "2d/CCTextFieldTTF.h"
  25. #include "ui/GUIExport.h"
  26. NS_CC_BEGIN
  27. /**
  28. * @addtogroup ui
  29. * @{
  30. */
  31. namespace ui {
  32. /**
  33. * @brief A helper class which inherit from @see `TextFieldTTF` and implements the @see `TextFieldDelegate` protocol.
  34. * It is mainly be used internally by @see `UITextField` class.
  35. * @js NA
  36. * @lua NA
  37. */
  38. class CC_GUI_DLL UICCTextField: public TextFieldTTF, public TextFieldDelegate
  39. {
  40. public:
  41. /**
  42. * @brief Create an empty UICCTextField.
  43. *
  44. * @return A UICCTextField instance.
  45. */
  46. static UICCTextField* create();
  47. /**
  48. * Default constructor
  49. */
  50. UICCTextField();
  51. /**
  52. * Default destructor
  53. */
  54. ~UICCTextField();
  55. virtual void onEnter() override;
  56. /**
  57. * Create a UICCTextField instance with a placeholder, a fontName and a fontSize.
  58. *@param placeholder Placeholder in string.
  59. *@param fontName Font name in string.
  60. *@param fontSize Font size in float.
  61. *@return A UICCTextField instance.
  62. */
  63. static UICCTextField* create(const std::string& placeholder,
  64. const std::string& fontName,
  65. float fontSize);
  66. //override functions
  67. virtual bool onTextFieldAttachWithIME(TextFieldTTF *pSender) override;
  68. virtual bool onTextFieldDetachWithIME(TextFieldTTF * pSender) override;
  69. virtual bool onTextFieldInsertText(TextFieldTTF * pSender,
  70. const char * text,
  71. size_t nLen) override;
  72. virtual bool onTextFieldDeleteBackward(TextFieldTTF * pSender,
  73. const char * delText,
  74. size_t nLen) override;
  75. void insertText(const char* text, size_t len) override;
  76. /**
  77. * Open up the IME.
  78. */
  79. void openIME();
  80. /**
  81. * Close the IME.
  82. */
  83. void closeIME();
  84. /**
  85. * Toggle enable max length limitation.
  86. *@param enable True to enable max length, false otherwise.
  87. */
  88. void setMaxLengthEnabled(bool enable);
  89. /**
  90. * Query max length enable state.
  91. *@return Whether max length is enabled or not.
  92. */
  93. bool isMaxLengthEnabled()const;
  94. /**
  95. * Set maximize length.
  96. *@param length The maximize length in integer.
  97. */
  98. void setMaxLength(int length);
  99. /**
  100. * Get maximize length.
  101. *@return Maximize length.
  102. */
  103. int getMaxLength()const;
  104. /**
  105. * Return the total inputed characters.
  106. *@return Total inputed character count.
  107. */
  108. std::size_t getCharCount()const;
  109. /**
  110. * @brief Toggle password input mode.
  111. *
  112. * @param enable True if enable password input, false otherwise.
  113. */
  114. void setPasswordEnabled(bool enable);
  115. /**
  116. * @brief Query whether password input mode is enabled or not.
  117. *
  118. * @return True if password input is enabled, false otherwise.
  119. */
  120. bool isPasswordEnabled()const;
  121. /**
  122. * @brief Change password style text.
  123. *
  124. * @param styleText The styleText for password mask, the default value is "*".
  125. */
  126. void setPasswordStyleText(const std::string& styleText);
  127. /**
  128. * @brief Set the password text content.
  129. *
  130. * @param text The content of password.
  131. */
  132. void setPasswordText(const std::string& text);
  133. /**
  134. * @brief Toggle attach with IME.
  135. *
  136. * @param attach True if attach with IME, false otherwise.
  137. */
  138. void setAttachWithIME(bool attach);
  139. /**
  140. * @brief Query whether the IME is attached or not.
  141. *
  142. * @return True if IME is attached, false otherwise.
  143. */
  144. bool getAttachWithIME()const;
  145. /**
  146. * @brief Toggle detach with IME.
  147. *
  148. * @param detach True if detach with IME, false otherwise.
  149. */
  150. void setDetachWithIME(bool detach);
  151. /**
  152. * @brief Query whether IME is detached or not.
  153. *
  154. * @return True if IME is detached, false otherwise.
  155. */
  156. bool getDetachWithIME()const;
  157. /**
  158. * @brief Toggle enable text insert.
  159. *
  160. * @param insert True if enable insert text, false otherwise.
  161. */
  162. void setInsertText(bool insert);
  163. /**
  164. * @brief Query whether insert text is enabled or not.
  165. *
  166. * @return True if insert text is enabled, false otherwise.
  167. */
  168. bool getInsertText()const;
  169. /**
  170. * @brief Toggle enable delete backward.
  171. *
  172. * @param deleteBackward True if enable delete backward, false otherwise.
  173. */
  174. void setDeleteBackward(bool deleteBackward);
  175. /**
  176. * @brief Query whether delete backward is enabled or not.
  177. *
  178. * @return True if delete backward is enabled, false otherwise.
  179. */
  180. bool getDeleteBackward()const;
  181. protected:
  182. bool _maxLengthEnabled;
  183. int _maxLength;
  184. bool _attachWithIME;
  185. bool _detachWithIME;
  186. bool _insertText;
  187. bool _deleteBackward;
  188. };
  189. /**
  190. * TextField event type.
  191. * @deprecated Use @see `TextField::EventType` instead.
  192. */
  193. typedef enum
  194. {
  195. TEXTFIELD_EVENT_ATTACH_WITH_IME,
  196. TEXTFIELD_EVENT_DETACH_WITH_IME,
  197. TEXTFIELD_EVENT_INSERT_TEXT,
  198. TEXTFIELD_EVENT_DELETE_BACKWARD,
  199. }TextFiledEventType;
  200. /**
  201. * A callback which would be called when a TextField event happens.
  202. * @deprecated Use @see `ccTextFieldCallback` instead.
  203. */
  204. typedef void (Ref::*SEL_TextFieldEvent)(Ref*, TextFiledEventType);
  205. #define textfieldeventselector(_SELECTOR) (SEL_TextFieldEvent)(&_SELECTOR)
  206. /**
  207. * @brief A widget which allows users to input text.
  208. * The rendering of the input text are based on @see `TextFieldTTF'.
  209. * If you want to use system control behavior, please use @see `EditBox` instead.
  210. * @js NA
  211. * @lua NA
  212. */
  213. class CC_GUI_DLL TextField : public Widget
  214. {
  215. DECLARE_CLASS_GUI_INFO
  216. public:
  217. /**
  218. * TextField event type.
  219. */
  220. enum class EventType
  221. {
  222. ATTACH_WITH_IME,
  223. DETACH_WITH_IME,
  224. INSERT_TEXT,
  225. DELETE_BACKWARD,
  226. };
  227. /**
  228. * A callback which would be called when a TextField event happens.
  229. */
  230. typedef std::function<void(Ref*, EventType)> ccTextFieldCallback;
  231. /**
  232. * @brief Default constructor.
  233. *
  234. */
  235. TextField();
  236. /**
  237. * @brief Default destructor.
  238. */
  239. virtual ~TextField();
  240. /**
  241. * @brief Create an empty TextField.
  242. *
  243. * @return A TextField instance.
  244. */
  245. static TextField* create();
  246. /**
  247. * @brief Create a TextField with a placeholder, a font name and a font size.
  248. *
  249. * @param placeholder The placeholder string.
  250. * @param fontName The font name.
  251. * @param fontSize The font size.
  252. * @return A TextField instance.
  253. */
  254. static TextField* create(const std::string& placeholder,
  255. const std::string& fontName,
  256. int fontSize);
  257. /**
  258. * @brief Set the touch size
  259. * The touch size is used for @see `hitTest`.
  260. * @param size A delimitation zone.
  261. */
  262. void setTouchSize(const Size &size);
  263. /**
  264. * @brief Get current touch size of TextField.
  265. *
  266. * @return The TextField's touch size.
  267. */
  268. Size getTouchSize()const;
  269. /**
  270. * @brief Toggle enable touch area.
  271. *
  272. * @param enable True if enable touch area, false otherwise.
  273. */
  274. void setTouchAreaEnabled(bool enable);
  275. virtual bool hitTest(const Vec2 &pt, const Camera* camera, Vec3 *p) const override;
  276. /**
  277. * @brief Set placeholder of TextField.
  278. *
  279. * @param value The string value of placeholder.
  280. */
  281. void setPlaceHolder(const std::string& value);
  282. /**
  283. * @brief Get the placeholder of TextField.
  284. *
  285. * @return A placeholder string.
  286. */
  287. const std::string& getPlaceHolder()const;
  288. /**
  289. * @brief Query the placeholder string color.
  290. *
  291. * @return The color of placeholder.
  292. */
  293. const Color4B& getPlaceHolderColor()const;
  294. /**
  295. * @brief Change the placeholder color.
  296. *
  297. * @param color A color value in `Color3B`.
  298. */
  299. void setPlaceHolderColor(const Color3B& color);
  300. /**
  301. * @brief Change the placeholder color.
  302. *
  303. * @param color A color value in `Color4B`.
  304. */
  305. void setPlaceHolderColor(const Color4B& color);
  306. /**
  307. * @brief Query the text string color.
  308. *
  309. * @return The color of the text.
  310. */
  311. const Color4B& getTextColor()const;
  312. /**
  313. * @brief Change the text color.
  314. *
  315. * @param textColor The color value in `Color4B`.
  316. */
  317. void setTextColor(const Color4B& textColor);
  318. /**
  319. * @brief Change font size of TextField.
  320. *
  321. * @param size The integer font size.
  322. */
  323. void setFontSize(int size);
  324. /**
  325. * @brief Query the font size.
  326. *
  327. * @return The integer font size.
  328. */
  329. int getFontSize()const;
  330. /**
  331. * @brief Change the font name of TextField.
  332. *
  333. * @param name The font name string.
  334. */
  335. void setFontName(const std::string& name);
  336. /**
  337. * @brief Query the TextField's font name.
  338. *
  339. * @return The font name string.
  340. */
  341. const std::string& getFontName()const;
  342. /**
  343. * @brief Detach the IME.
  344. */
  345. virtual void didNotSelectSelf();
  346. /**
  347. *Change content of TextField.
  348. *@deprecated Use @see `setString(const std::string&)` instead.
  349. *@param text A string content.
  350. */
  351. CC_DEPRECATED_ATTRIBUTE void setText(const std::string& text){this->setString(text);}
  352. /**
  353. *Query the content of TextField.
  354. *@deprecated Use @see `getString` instead.
  355. *@return The string value of TextField.
  356. */
  357. CC_DEPRECATED_ATTRIBUTE const std::string& getStringValue()const{return this->getString();}
  358. /**
  359. *Change content of TextField.
  360. *@param text A string content.
  361. */
  362. void setString(const std::string& text);
  363. /**
  364. *Query the content of TextField.
  365. *@return The string value of TextField.
  366. */
  367. const std::string& getString()const;
  368. virtual bool onTouchBegan(Touch *touch, Event *unusedEvent) override;
  369. /**
  370. * @brief Toggle maximize length enable
  371. *
  372. * @param enable True if enable maximize length, false otherwise.
  373. */
  374. void setMaxLengthEnabled(bool enable);
  375. /**
  376. * @brief Query whether max length is enabled or not.
  377. *
  378. * @return True if maximize length is enabled, false otherwise.
  379. */
  380. bool isMaxLengthEnabled()const;
  381. /**
  382. * @brief Change maximize input length limitation.
  383. *
  384. * @param length A character count in integer.
  385. */
  386. void setMaxLength(int length);
  387. /**
  388. * @brief Query maximize input length of TextField.
  389. *
  390. * @return The integer value of maximize input length.
  391. */
  392. int getMaxLength()const;
  393. /**
  394. * @brief Query the input string length.
  395. *
  396. * @return A integer length value.
  397. */
  398. int getStringLength() const;
  399. /**
  400. * @brief Toggle enable password input mode.
  401. *
  402. * @param enable True if enable password input mode, false otherwise.
  403. */
  404. void setPasswordEnabled(bool enable);
  405. /**
  406. * @brief Query whether password is enabled or not.
  407. *
  408. * @return True if password is enabled, false otherwise.
  409. */
  410. bool isPasswordEnabled()const;
  411. /**
  412. * @brief Change password style text.
  413. *
  414. * @param styleText The styleText for password mask, the default value is "*".
  415. */
  416. void setPasswordStyleText(const char* styleText);
  417. /**
  418. * @brief Query the password style text.
  419. *
  420. * @return A password style text.
  421. */
  422. const char* getPasswordStyleText()const;
  423. virtual void update(float dt) override;
  424. /**
  425. * @brief Query whether the IME is attached or not.
  426. *
  427. * @return True if IME is attached, false otherwise.
  428. */
  429. bool getAttachWithIME()const;
  430. /**
  431. * @brief Toggle attach with IME.
  432. *
  433. * @param attach True if attach with IME, false otherwise.
  434. */
  435. void setAttachWithIME(bool attach);
  436. /**
  437. * @brief Query whether IME is detached or not.
  438. *
  439. * @return True if IME is detached, false otherwise.
  440. */
  441. bool getDetachWithIME()const;
  442. /**
  443. * @brief Toggle detach with IME.
  444. *
  445. * @param detach True if detach with IME, false otherwise.
  446. */
  447. void setDetachWithIME(bool detach);
  448. /**
  449. * @brief Whether it is ready to get the inserted text or not.
  450. *
  451. * @return True if the insert text is ready, false otherwise.
  452. */
  453. bool getInsertText()const;
  454. /**
  455. * @brief Toggle enable insert text mode
  456. *
  457. * @param insertText True if enable insert text, false otherwise.
  458. */
  459. void setInsertText(bool insertText);
  460. /**
  461. * @brief Whether it is ready to delete backward in TextField.
  462. *
  463. * @return True is the delete backward is enabled, false otherwise.
  464. */
  465. bool getDeleteBackward()const;
  466. /**
  467. * @brief Toggle enable delete backward mode.
  468. *
  469. * @param deleteBackward True is delete backward is enabled, false otherwise.
  470. */
  471. void setDeleteBackward(bool deleteBackward);
  472. /**
  473. * Add a event listener to TextField, when some predefined event happens, the callback will be called.
  474. *@deprecated Use @see `addEventListener` instead.
  475. *@param target A pointer of `Ref*` type.
  476. *@param selector A member function pointer with type of `SEL_TextFieldEvent`.
  477. */
  478. CC_DEPRECATED_ATTRIBUTE void addEventListenerTextField(Ref* target, SEL_TextFieldEvent selector);
  479. /**
  480. * Add a event listener to TextField, when some predefined event happens, the callback will be called.
  481. *@param callback A callback function with type of `ccTextFieldCallback`.
  482. */
  483. void addEventListener(const ccTextFieldCallback& callback);
  484. /**
  485. * Returns the "class name" of widget.
  486. */
  487. virtual std::string getDescription() const override;
  488. /**
  489. * @brief Get the renderer size in auto mode.
  490. *
  491. * @return A delimitation zone.
  492. */
  493. virtual Size getAutoRenderSize();
  494. //override functions.
  495. virtual Size getVirtualRendererSize() const override;
  496. virtual Node* getVirtualRenderer() override;
  497. virtual void onEnter() override;
  498. /**
  499. * @brief Attach the IME for inputing.
  500. *
  501. */
  502. void attachWithIME();
  503. /**
  504. * @brief Change the text area size.
  505. *
  506. * @param size A delimitation zone.
  507. */
  508. void setTextAreaSize(const Size &size);
  509. /**
  510. * @brief Change horizontal text alignment.
  511. *
  512. * @param alignment A alignment arguments in @see `TextHAlignment`.
  513. */
  514. void setTextHorizontalAlignment(TextHAlignment alignment);
  515. /**
  516. * @brief Inquire the horizontal alignment
  517. *
  518. * @return The horizontal alignment
  519. */
  520. TextHAlignment getTextHorizontalAlignment() const;
  521. /**
  522. * @brief Change the vertical text alignment.
  523. *
  524. * @param alignment A alignment arguments in @see `TextVAlignment`.
  525. */
  526. void setTextVerticalAlignment(TextVAlignment alignment);
  527. /**
  528. * @brief Inquire the horizontal alignment
  529. *
  530. * @return The horizontal alignment
  531. */
  532. TextVAlignment getTextVerticalAlignment() const;
  533. /**
  534. * Set enable cursor use.
  535. * @js NA
  536. */
  537. void setCursorEnabled(bool enabled);
  538. /**
  539. * Set char showing cursor.
  540. * @js NA
  541. */
  542. void setCursorChar(char cursor);
  543. /**
  544. * Set cursor position, if enabled
  545. * @js NA
  546. */
  547. void setCursorPosition(std::size_t cursorPosition);
  548. /**
  549. * Set cursor position to hit letter, if enabled
  550. * @js NA
  551. */
  552. void setCursorFromPoint(const Vec2 &point, const Camera* camera);
  553. CC_CONSTRUCTOR_ACCESS:
  554. virtual bool init() override;
  555. protected:
  556. virtual void initRenderer() override;
  557. void attachWithIMEEvent();
  558. void detachWithIMEEvent();
  559. void insertTextEvent();
  560. void deleteBackwardEvent();
  561. virtual void onSizeChanged() override;
  562. void textfieldRendererScaleChangedWithSize();
  563. virtual Widget* createCloneInstance() override;
  564. virtual void copySpecialProperties(Widget* model) override;
  565. virtual void adaptRenderers() override;
  566. protected:
  567. UICCTextField* _textFieldRenderer;
  568. float _touchWidth;
  569. float _touchHeight;
  570. bool _useTouchArea;
  571. Ref* _textFieldEventListener;
  572. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  573. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  574. #elif _MSC_VER >= 1400 //vs 2005 or higher
  575. #pragma warning (push)
  576. #pragma warning (disable: 4996)
  577. #endif
  578. SEL_TextFieldEvent _textFieldEventSelector;
  579. #if defined(__GNUC__) && ((__GNUC__ >= 4) || ((__GNUC__ == 3) && (__GNUC_MINOR__ >= 1)))
  580. #pragma GCC diagnostic warning "-Wdeprecated-declarations"
  581. #elif _MSC_VER >= 1400 //vs 2005 or higher
  582. #pragma warning (pop)
  583. #endif
  584. ccTextFieldCallback _eventCallback;
  585. bool _textFieldRendererAdaptDirty;
  586. private:
  587. enum class FontType
  588. {
  589. SYSTEM,
  590. TTF,
  591. BMFONT
  592. };
  593. std::string _fontName;
  594. int _fontSize;
  595. FontType _fontType;
  596. };
  597. }
  598. // end of ui group
  599. /// @}
  600. NS_CC_END
  601. #endif /* defined(__TextField__) */