TextBox.h 7.0 KB

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