TextBox.h 7.0 KB

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