TextBox.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #ifndef TEXTBOX_H_
  2. #define TEXTBOX_H_
  3. #include <string>
  4. #include "Label.h"
  5. namespace gameplay
  6. {
  7. /**
  8. * Defines a text control.
  9. *
  10. * Listeners can listen for a TEXT_CHANGED event, and then query the text box
  11. * for the last keypress it received.
  12. * On mobile device you can tap or click within the text box to
  13. * bring up the virtual keyboard.
  14. *
  15. * @see http://blackberry.github.io/GamePlay/docs/file-formats.html#wiki-UI_Forms
  16. */
  17. class TextBox : public Label
  18. {
  19. friend class Container;
  20. friend class ControlFactory;
  21. public:
  22. /**
  23. * Input modes. Default is Text.
  24. */
  25. enum InputMode
  26. {
  27. /**
  28. * Text: Text is displayed directly.
  29. */
  30. TEXT = 0x01,
  31. /**
  32. * Password: Text is replaced by _passwordChar, which is '*' by default.
  33. */
  34. PASSWORD = 0x02
  35. };
  36. /**
  37. * Creates a new TextBox.
  38. *
  39. * @param id The textbox ID.
  40. * @param style The textbox style (optional).
  41. *
  42. * @return The new textbox.
  43. * @script{create}
  44. */
  45. static TextBox* create(const char* id, Theme::Style* style = NULL);
  46. /**
  47. * Returns the current location of the caret with the text of this TextBox.
  48. *
  49. * @return The current caret location.
  50. */
  51. unsigned int getCaretLocation() const;
  52. /**
  53. * Sets the location of the caret within this text box.
  54. *
  55. * @param index The new location of the caret within the text of this TextBox.
  56. */
  57. void setCaretLocation(unsigned int index);
  58. /**
  59. * Get the last key pressed within this text box.
  60. *
  61. * @return The last key pressed within this text box.
  62. */
  63. int getLastKeypress();
  64. /**
  65. * @see Control::getType
  66. */
  67. const char* getType() const;
  68. /**
  69. * Set the character displayed in password mode.
  70. *
  71. * @param character Character to display in password mode.
  72. */
  73. void setPasswordChar(char character);
  74. /**
  75. * Get the character displayed in password mode.
  76. *
  77. * @return The character displayed in password mode.
  78. */
  79. char getPasswordChar() const;
  80. /**
  81. * Set the input mode.
  82. *
  83. * @param inputMode Input mode to set.
  84. */
  85. void setInputMode(InputMode inputMode);
  86. /**
  87. * Get the input mode.
  88. *
  89. * @return The input mode.
  90. */
  91. InputMode getInputMode() const;
  92. virtual void addListener(Control::Listener* listener, int eventFlags);
  93. protected:
  94. /**
  95. * Constructor.
  96. */
  97. TextBox();
  98. /**
  99. * Destructor.
  100. */
  101. ~TextBox();
  102. /**
  103. * Create a text box with a given style and properties.
  104. *
  105. * @param style The style to apply to this text box.
  106. * @param properties A properties object containing a definition of the text box (optional).
  107. *
  108. * @return The new text box.
  109. */
  110. static Control* create(Theme::Style* style, Properties* properties = NULL);
  111. /**
  112. * @see Control::initialize
  113. */
  114. void initialize(const char* typeName, Theme::Style* style, Properties* properties);
  115. /**
  116. * Touch callback on touch events. Controls return true if they consume the touch event.
  117. *
  118. * @param evt The touch event that occurred.
  119. * @param x The x position of the touch in pixels. Left edge is zero.
  120. * @param y The y position of the touch in pixels. Top edge is zero.
  121. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  122. *
  123. * @return Whether the touch event was consumed by the control.
  124. *
  125. * @see Touch::TouchEvent
  126. */
  127. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  128. /**
  129. * Keyboard callback on key events.
  130. *
  131. * @param evt The key event that occurred.
  132. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  133. * If evt is KEY_CHAR then key is the unicode value of the character.
  134. *
  135. * @see Keyboard::KeyEvent
  136. * @see Keyboard::Key
  137. */
  138. bool keyEvent(Keyboard::KeyEvent evt, int key);
  139. /**
  140. * @see Control#controlEvent
  141. */
  142. void controlEvent(Control::Listener::EventType evt);
  143. /**
  144. * @see Control::updateState
  145. */
  146. void updateState(State state);
  147. /**
  148. * @see Control::drawImages
  149. */
  150. unsigned int drawImages(Form* form, const Rectangle& clip);
  151. /**
  152. * @see Control::drawText
  153. */
  154. unsigned int drawText(Form* form, const Rectangle& clip);
  155. /**
  156. * Gets an InputMode by string.
  157. *
  158. * @param inputMode The string representation of the InputMode type.
  159. * @return The InputMode enum value corresponding to the given string.
  160. */
  161. static InputMode getInputMode(const char* inputMode);
  162. /**
  163. * Get the text which should be displayed, depending on
  164. * _inputMode.
  165. *
  166. * @return The text to be displayed.
  167. */
  168. std::string getDisplayedText() const;
  169. /**
  170. * The current location of the TextBox's caret.
  171. */
  172. unsigned int _caretLocation;
  173. /**
  174. * The previous position of the TextBox's caret.
  175. */
  176. Vector2 _prevCaretLocation;
  177. /**
  178. * The last character that was entered into the TextBox.
  179. */
  180. int _lastKeypress;
  181. /**
  182. * The font size to be used in the TextBox.
  183. */
  184. unsigned int _fontSize;
  185. /**
  186. * The Theme::Image for the TextBox's caret.
  187. */
  188. Theme::ThemeImage* _caretImage;
  189. /**
  190. * The character displayed in password mode.
  191. */
  192. char _passwordChar;
  193. /**
  194. * The mode used to display the typed text.
  195. */
  196. InputMode _inputMode;
  197. /**
  198. * Indicate if the CTRL key is currently pressed.
  199. */
  200. bool _ctrlPressed;
  201. /**
  202. * Indicate if the SHIFT key is currently pressed.
  203. */
  204. bool _shiftPressed = false;
  205. private:
  206. /**
  207. * Constructor.
  208. */
  209. TextBox(const TextBox& copy);
  210. void setCaretLocation(int x, int y);
  211. void getCaretLocation(Vector2* p);
  212. };
  213. }
  214. #endif