UIEditBox.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2012 James Chen
  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 __UIEDITTEXT_H__
  23. #define __UIEDITTEXT_H__
  24. #include "base/CCIMEDelegate.h"
  25. #include "ui/GUIDefine.h"
  26. #include "ui/UIButton.h"
  27. #include "ui/UIScale9Sprite.h"
  28. NS_CC_BEGIN
  29. /**
  30. * @addtogroup ui
  31. * @{
  32. */
  33. namespace ui {
  34. class EditBox;
  35. class EditBoxImpl;
  36. /**
  37. *@brief Editbox delegate class.
  38. * It's useful when you want to do some customization during Editbox input event
  39. *
  40. * @js NA
  41. * @lua NA
  42. */
  43. class CC_GUI_DLL EditBoxDelegate
  44. {
  45. public:
  46. /**
  47. * Reason for ending edit (for platforms where it is known)
  48. */
  49. enum class EditBoxEndAction {
  50. UNKNOWN,
  51. TAB_TO_NEXT,
  52. TAB_TO_PREVIOUS,
  53. RETURN
  54. };
  55. virtual ~EditBoxDelegate() {};
  56. /**
  57. * This method is called when an edit box gains focus after keyboard is shown.
  58. * @param editBox The edit box object that generated the event.
  59. */
  60. virtual void editBoxEditingDidBegin(EditBox* editBox) {};
  61. /**
  62. * This method is called when an edit box loses focus after keyboard is hidden.
  63. * @param editBox The edit box object that generated the event.
  64. * @deprecated Use editBoxEditingDidEndWithAction() instead to receive reason for end
  65. */
  66. CC_DEPRECATED_ATTRIBUTE virtual void editBoxEditingDidEnd(EditBox* editBox) {};
  67. /**
  68. * This method is called when the edit box text was changed.
  69. * @param editBox The edit box object that generated the event.
  70. * @param text The new text.
  71. */
  72. virtual void editBoxTextChanged(EditBox* editBox, const std::string& text) {};
  73. /**
  74. * This method is called when the return button was pressed or the outside area of keyboard was touched.
  75. * @param editBox The edit box object that generated the event.
  76. */
  77. virtual void editBoxReturn(EditBox* editBox) = 0;
  78. /**
  79. * This method is called when an edit box loses focus after keyboard is hidden.
  80. * @param editBox The edit box object that generated the event.
  81. * @param type The reason why editing ended.
  82. */
  83. virtual void editBoxEditingDidEndWithAction(EditBox* editBox, EditBoxEndAction action) {};
  84. };
  85. /**
  86. * @brief Class for edit box.
  87. *
  88. * You can use this widget to gather small amounts of text from the user.
  89. *
  90. */
  91. class CC_GUI_DLL EditBox
  92. : public Widget
  93. , public IMEDelegate
  94. {
  95. public:
  96. /**
  97. * The popup keyboard return type.
  98. */
  99. enum class KeyboardReturnType
  100. {
  101. DEFAULT,
  102. DONE,
  103. SEND,
  104. SEARCH,
  105. GO,
  106. NEXT
  107. };
  108. /**
  109. * @brief The EditBox::InputMode defines the type of text that the user is allowed
  110. * to enter.
  111. */
  112. enum class InputMode
  113. {
  114. /**
  115. * The user is allowed to enter any text, including line breaks.
  116. */
  117. ANY,
  118. /**
  119. * The user is allowed to enter an e-mail address.
  120. */
  121. EMAIL_ADDRESS,
  122. /**
  123. * The user is allowed to enter an integer value.
  124. */
  125. NUMERIC,
  126. /**
  127. * The user is allowed to enter a phone number.
  128. */
  129. PHONE_NUMBER,
  130. /**
  131. * The user is allowed to enter a URL.
  132. */
  133. URL,
  134. /**
  135. * The user is allowed to enter a real number value.
  136. * This extends kEditBoxInputModeNumeric by allowing a decimal point.
  137. */
  138. DECIMAL,
  139. /**
  140. * The user is allowed to enter any text, except for line breaks.
  141. */
  142. SINGLE_LINE,
  143. };
  144. /**
  145. * @brief The EditBox::InputFlag defines how the input text is displayed/formatted.
  146. */
  147. enum class InputFlag
  148. {
  149. /**
  150. * Indicates that the text entered is confidential data that should be
  151. * obscured whenever possible. This implies EDIT_BOX_INPUT_FLAG_SENSITIVE.
  152. */
  153. PASSWORD,
  154. /**
  155. * Indicates that the text entered is sensitive data that the
  156. * implementation must never store into a dictionary or table for use
  157. * in predictive, auto-completing, or other accelerated input schemes.
  158. * A credit card number is an example of sensitive data.
  159. */
  160. SENSITIVE,
  161. /**
  162. * This flag is a hint to the implementation that during text editing,
  163. * the initial letter of each word should be capitalized.
  164. */
  165. INITIAL_CAPS_WORD,
  166. /**
  167. * This flag is a hint to the implementation that during text editing,
  168. * the initial letter of each sentence should be capitalized.
  169. */
  170. INITIAL_CAPS_SENTENCE,
  171. /**
  172. * Capitalize all characters automatically.
  173. */
  174. INITIAL_CAPS_ALL_CHARACTERS,
  175. /**
  176. * Lowercase all characters automatically.
  177. */
  178. LOWERCASE_ALL_CHARACTERS
  179. };
  180. /**
  181. * create a edit box with size.
  182. * @return An autorelease pointer of EditBox, you don't need to release it only if you retain it again.
  183. */
  184. static EditBox* create(const Size& size,
  185. Scale9Sprite* normalSprite,
  186. Scale9Sprite* pressedSprite = nullptr,
  187. Scale9Sprite* disabledSprite = nullptr);
  188. /**
  189. * create a edit box with size.
  190. * @return An autorelease pointer of EditBox, you don't need to release it only if you retain it again.
  191. */
  192. static EditBox* create(const Size& size,
  193. const std::string& normal9SpriteBg,
  194. TextureResType texType = TextureResType::LOCAL);
  195. /**
  196. * Constructor.
  197. * @js ctor
  198. * @lua new
  199. */
  200. EditBox();
  201. /**
  202. * Destructor.
  203. * @js NA
  204. * @lua NA
  205. */
  206. virtual ~EditBox();
  207. /**
  208. * Init edit box with specified size. This method should be invoked right after constructor.
  209. * @param size The size of edit box.
  210. * @param normal9SpriteBg background image of edit box.
  211. * @param texType the resource type, the default value is TextureResType::LOCAL
  212. * @return Whether initialization is successfully or not.
  213. */
  214. bool initWithSizeAndBackgroundSprite(const Size& size,
  215. const std::string& normal9SpriteBg,
  216. TextureResType texType = TextureResType::LOCAL);
  217. /**
  218. * Init edit box with specified size. This method should be invoked right after constructor.
  219. * @param size The size of edit box.
  220. * @param normal9SpriteBg background image of edit box.
  221. * @return Whether initialization is successfully or not.
  222. */
  223. bool initWithSizeAndBackgroundSprite(const Size& size, Scale9Sprite* normal9SpriteBg);
  224. /**
  225. * Gets/Sets the delegate for edit box.
  226. * @lua NA
  227. */
  228. void setDelegate(EditBoxDelegate* delegate);
  229. /**
  230. * @js NA
  231. * @lua NA
  232. */
  233. EditBoxDelegate* getDelegate();
  234. #if CC_ENABLE_SCRIPT_BINDING
  235. /**
  236. * Registers a script function that will be called for EditBox events.
  237. *
  238. * This handler will be removed automatically after onExit() called.
  239. * @code
  240. * -- lua sample
  241. * local function editboxEventHandler(eventType)
  242. * if eventType == "began" then
  243. * -- triggered when an edit box gains focus after keyboard is shown
  244. * elseif eventType == "ended" then
  245. * -- triggered when an edit box loses focus after keyboard is hidden.
  246. * elseif eventType == "changed" then
  247. * -- triggered when the edit box text was changed.
  248. * elseif eventType == "return" then
  249. * -- triggered when the return button was pressed or the outside area of keyboard was touched.
  250. * end
  251. * end
  252. *
  253. * local editbox = EditBox:create(Size(...), Scale9Sprite:create(...))
  254. * editbox = registerScriptEditBoxHandler(editboxEventHandler)
  255. * @endcode
  256. *
  257. * @param handler A number that indicates a lua function.
  258. * @js NA
  259. * @lua NA
  260. */
  261. void registerScriptEditBoxHandler(int handler);
  262. /**
  263. * Unregisters a script function that will be called for EditBox events.
  264. * @js NA
  265. * @lua NA
  266. */
  267. void unregisterScriptEditBoxHandler();
  268. /**
  269. * get a script Handler
  270. * @js NA
  271. * @lua NA
  272. */
  273. int getScriptEditBoxHandler(){ return _scriptEditBoxHandler ;}
  274. #endif // #if CC_ENABLE_SCRIPT_BINDING
  275. /**
  276. * Set the text entered in the edit box.
  277. * @param pText The given text.
  278. */
  279. void setText(const char* pText);
  280. /**
  281. * Get the text entered in the edit box.
  282. * @return The text entered in the edit box.
  283. */
  284. const char* getText() const;
  285. /**
  286. * Set the font. Only system font is allowed.
  287. * @param pFontName The font name.
  288. * @param fontSize The font size.
  289. */
  290. void setFont(const char* pFontName, int fontSize);
  291. /**
  292. * Set the font name. Only system font is allowed.
  293. * @param pFontName The font name.
  294. */
  295. void setFontName(const char* pFontName);
  296. /**
  297. * Get the font name.
  298. * @return The font name.
  299. */
  300. const char* getFontName() const;
  301. /**
  302. * Set the font size.
  303. * @param fontSize The font size.
  304. */
  305. void setFontSize(int fontSize);
  306. /**
  307. * Get the font size.
  308. * @return The font size.
  309. */
  310. int getFontSize() const;
  311. /**
  312. * Set the font color of the widget's text.
  313. */
  314. void setFontColor(const Color3B& color);
  315. void setFontColor(const Color4B& color);
  316. /**
  317. * Get the font color of the widget's text.
  318. */
  319. const Color4B& getFontColor() const;
  320. /**
  321. * Set the placeholder's font. Only system font is allowed.
  322. * @param pFontName The font name.
  323. * @param fontSize The font size.
  324. */
  325. void setPlaceholderFont(const char* pFontName, int fontSize);
  326. /**
  327. * Set the placeholder's font name. only system font is allowed.
  328. * @param pFontName The font name.
  329. */
  330. void setPlaceholderFontName(const char* pFontName);
  331. /**
  332. * Get the placeholder's font name. only system font is allowed.
  333. * @return The font name.
  334. */
  335. const char* getPlaceholderFontName() const;
  336. /**
  337. * Set the placeholder's font size.
  338. * @param fontSize The font size.
  339. */
  340. void setPlaceholderFontSize(int fontSize);
  341. /**
  342. * Get the placeholder's font size.
  343. * @return The font size.
  344. */
  345. int getPlaceholderFontSize() const;
  346. /**
  347. * Set the font color of the placeholder text when the edit box is empty.
  348. */
  349. void setPlaceholderFontColor(const Color3B& color);
  350. /**
  351. * Set the font color of the placeholder text when the edit box is empty.
  352. */
  353. void setPlaceholderFontColor(const Color4B& color);
  354. /**
  355. * Get the font color of the placeholder text when the edit box is empty.
  356. */
  357. const Color4B& getPlaceholderFontColor() const;
  358. /**
  359. * Set a text in the edit box that acts as a placeholder when an
  360. * edit box is empty.
  361. * @param pText The given text.
  362. */
  363. void setPlaceHolder(const char* pText);
  364. /**
  365. * Get a text in the edit box that acts as a placeholder when an
  366. * edit box is empty.
  367. */
  368. const char* getPlaceHolder() const;
  369. /**
  370. * Set the input mode of the edit box.
  371. * @param inputMode One of the EditBox::InputMode constants.
  372. */
  373. void setInputMode(InputMode inputMode);
  374. /**
  375. * Get the input mode of the edit box.
  376. * @return One of the EditBox::InputMode constants.
  377. */
  378. InputMode getInputMode() const;
  379. /**
  380. * Sets the maximum input length of the edit box.
  381. * Setting this value enables multiline input mode by default.
  382. * Available on Android, iOS and Windows Phone.
  383. *
  384. * @param maxLength The maximum length.
  385. */
  386. void setMaxLength(int maxLength);
  387. /**
  388. * Gets the maximum input length of the edit box.
  389. *
  390. * @return Maximum input length.
  391. */
  392. int getMaxLength();
  393. /**
  394. * Set the input flags that are to be applied to the edit box.
  395. * @param inputFlag One of the EditBox::InputFlag constants.
  396. */
  397. void setInputFlag(InputFlag inputFlag);
  398. /**
  399. * Get the input flags that are to be applied to the edit box.
  400. * @return One of the EditBox::InputFlag constants.
  401. */
  402. InputFlag getInputFlag() const;
  403. /**
  404. * Set the return type that are to be applied to the edit box.
  405. * @param returnType One of the EditBox::KeyboardReturnType constants.
  406. */
  407. void setReturnType(KeyboardReturnType returnType);
  408. /**
  409. * Get the return type that are to be applied to the edit box.
  410. * @return One of the EditBox::KeyboardReturnType constants.
  411. */
  412. KeyboardReturnType getReturnType() const;
  413. /**
  414. * Set the text horizontal alignment.
  415. */
  416. void setTextHorizontalAlignment(TextHAlignment alignment);
  417. /**
  418. * Get the text horizontal alignment.
  419. */
  420. TextHAlignment getTextHorizontalAlignment() const;
  421. /* override functions */
  422. virtual void setPosition(const Vec2& pos) override;
  423. virtual void setVisible(bool visible) override;
  424. virtual void setContentSize(const Size& size) override;
  425. virtual void setAnchorPoint(const Vec2& anchorPoint) override;
  426. /**
  427. * Returns the "class name" of widget.
  428. */
  429. virtual std::string getDescription() const override;
  430. /**
  431. * @js NA
  432. * @lua NA
  433. */
  434. virtual void draw(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags) override;
  435. /**
  436. * @js NA
  437. * @lua NA
  438. */
  439. virtual void onEnter() override;
  440. /**
  441. * @js NA
  442. * @lua NA
  443. */
  444. virtual void onExit() override;
  445. /**
  446. * @js NA
  447. * @lua NA
  448. */
  449. virtual void keyboardWillShow(IMEKeyboardNotificationInfo& info) override;
  450. /**
  451. * @js NA
  452. * @lua NA
  453. */
  454. virtual void keyboardDidShow(IMEKeyboardNotificationInfo& info) override;
  455. /**
  456. * @js NA
  457. * @lua NA
  458. */
  459. virtual void keyboardWillHide(IMEKeyboardNotificationInfo& info) override;
  460. /**
  461. * @js NA
  462. * @lua NA
  463. */
  464. virtual void keyboardDidHide(IMEKeyboardNotificationInfo& info) override;
  465. /* callback functions
  466. * @js NA
  467. * @lua NA
  468. */
  469. void touchDownAction(Ref *sender, TouchEventType controlEvent);
  470. protected:
  471. virtual void adaptRenderers() override;
  472. void updatePosition(float dt);
  473. EditBoxImpl* _editBoxImpl;
  474. EditBoxDelegate* _delegate;
  475. Scale9Sprite *_backgroundSprite;
  476. float _adjustHeight;
  477. #if CC_ENABLE_SCRIPT_BINDING
  478. int _scriptEditBoxHandler;
  479. #endif
  480. };
  481. }
  482. // end of ui group
  483. /// @}
  484. NS_CC_END
  485. #endif /* __UIEDITTEXT_H__ */