TextBox.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef TEXTBOX_H_
  2. #define TEXTBOX_H_
  3. #include "Base.h"
  4. #include "Label.h"
  5. #include "Theme.h"
  6. #include "Keyboard.h"
  7. namespace gameplay
  8. {
  9. /**
  10. * An editable text label. Tap or click within the text box to bring up the
  11. * virtual keyboard.
  12. *
  13. * Listeners can listen for a TEXT_CHANGED event, and then query the text box
  14. * for the last keypress it received.
  15. *
  16. * The following properties are available for text boxes:
  17. @verbatim
  18. textBox <labelID>
  19. {
  20. style = <styleID>
  21. alignment = <Control::Alignment constant> // Note: 'position' will be ignored.
  22. position = <x, y>
  23. autoWidth = <bool>
  24. autoHeight = <bool>
  25. size = <width, height>
  26. width = <width> // Can be used in place of 'size', e.g. with 'autoHeight = true'
  27. height = <height> // Can be used in place of 'size', e.g. with 'autoWidth = true'
  28. text = <string>
  29. inputMode = <TextBox::InputMode constant>
  30. }
  31. @endverbatim
  32. */
  33. class TextBox : public Label
  34. {
  35. friend class Container;
  36. public:
  37. /**
  38. * Input modes. Default is Text.
  39. */
  40. enum InputMode {
  41. /**
  42. * Text: Text is displayed directly.
  43. */
  44. TEXT = 0x01,
  45. /**
  46. * Password: Text is replaced by _passwordChar, which is '*' by default.
  47. */
  48. PASSWORD = 0x02
  49. };
  50. /**
  51. * Create a new text box control.
  52. *
  53. * @param id The control's ID.
  54. * @param style The control's style.
  55. *
  56. * @return The new text box.
  57. * @script{create}
  58. */
  59. static TextBox* create(const char* id, Theme::Style* style);
  60. /**
  61. * Initialize this textbox.
  62. */
  63. virtual void initialize(Theme::Style* style, Properties* properties);
  64. /**
  65. * Add a listener to be notified of specific events affecting
  66. * this control. Event types can be OR'ed together.
  67. * E.g. To listen to touch-press and touch-release events,
  68. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  69. * as the second parameter.
  70. *
  71. * @param listener The listener to add.
  72. * @param eventFlags The events to listen for.
  73. */
  74. virtual void addListener(Control::Listener* listener, int eventFlags);
  75. /**
  76. * Get the last key pressed within this text box.
  77. *
  78. * @return The last key pressed within this text box.
  79. */
  80. int getLastKeypress();
  81. /**
  82. * @see Control::getType
  83. */
  84. const char* getType() const;
  85. /**
  86. * Set the character displayed in password mode.
  87. *
  88. * @param character Character to display in password mode.
  89. */
  90. void setPasswordChar(char character);
  91. /**
  92. * Get the character displayed in password mode.
  93. *
  94. * @return The character displayed in password mode.
  95. */
  96. char getPasswordChar() const;
  97. /**
  98. * Set the input mode.
  99. *
  100. * @param inputMode Input mode to set.
  101. */
  102. void setInputMode(InputMode inputMode);
  103. /**
  104. * Get the input mode.
  105. *
  106. * @return The input mode.
  107. */
  108. InputMode getInputMode() const;
  109. protected:
  110. /**
  111. * Constructor.
  112. */
  113. TextBox();
  114. /**
  115. * Destructor.
  116. */
  117. ~TextBox();
  118. /**
  119. * Create a text box with a given style and properties.
  120. *
  121. * @param style The style to apply to this text box.
  122. * @param properties The properties to set on this text box.
  123. *
  124. * @return The new text box.
  125. */
  126. static TextBox* create(Theme::Style* style, Properties* properties);
  127. /**
  128. * Touch callback on touch events. Controls return true if they consume the touch event.
  129. *
  130. * @param evt The touch event that occurred.
  131. * @param x The x position of the touch in pixels. Left edge is zero.
  132. * @param y The y position of the touch in pixels. Top edge is zero.
  133. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  134. *
  135. * @return Whether the touch event was consumed by the control.
  136. *
  137. * @see Touch::TouchEvent
  138. */
  139. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  140. /**
  141. * Keyboard callback on key events.
  142. *
  143. * @param evt The key event that occurred.
  144. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  145. * If evt is KEY_CHAR then key is the unicode value of the character.
  146. *
  147. * @see Keyboard::KeyEvent
  148. * @see Keyboard::Key
  149. */
  150. bool keyEvent(Keyboard::KeyEvent evt, int key);
  151. /**
  152. * Called when a control's properties change. Updates this control's internal rendering
  153. * properties, such as its text viewport.
  154. *
  155. * @param container This control's parent container.
  156. * @param offset Positioning offset to add to the control's position.
  157. */
  158. void update(const Control* container, const Vector2& offset);
  159. /**
  160. * Draw the images associated with this control.
  161. *
  162. * @param spriteBatch The sprite batch containing this control's icons.
  163. * @param clip The clipping rectangle of this control's parent container.
  164. */
  165. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  166. /**
  167. * Draw this textbox's text.
  168. *
  169. * @param clip The clipping rectangle of this textbox's
  170. * parent container.
  171. */
  172. virtual void drawText(const Rectangle& clip);
  173. /**
  174. * Gets an InputMode by string.
  175. *
  176. * @param inputMode The string representation of the InputMode type.
  177. * @return The InputMode enum value corresponding to the given string.
  178. */
  179. static InputMode getInputMode(const char* inputMode);
  180. /**
  181. * Get the text which should be displayed, depending on
  182. * _inputMode.
  183. *
  184. * @return The text to be displayed.
  185. */
  186. std::string getDisplayedText() const;
  187. /**
  188. * The current position of the TextBox's caret.
  189. */
  190. Vector2 _caretLocation;
  191. /**
  192. * The previous position of the TextBox's caret.
  193. */
  194. Vector2 _prevCaretLocation;
  195. /**
  196. * The last character that was entered into the TextBox.
  197. */
  198. int _lastKeypress;
  199. /**
  200. * The font size to be used in the TextBox.
  201. */
  202. unsigned int _fontSize;
  203. /**
  204. * The Theme::Image for the TextBox's caret.
  205. */
  206. Theme::ThemeImage* _caretImage;
  207. /**
  208. * The character displayed in password mode.
  209. */
  210. char _passwordChar;
  211. /**
  212. * The mode used to display the typed text.
  213. */
  214. InputMode _inputMode;
  215. /**
  216. * Indicate if the CTRL key is currently pressed.
  217. */
  218. bool _ctrlPressed;
  219. private:
  220. /**
  221. * Constructor.
  222. */
  223. TextBox(const TextBox& copy);
  224. void setCaretLocation(int x, int y);
  225. };
  226. }
  227. #endif