TextBox.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. label <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. }
  30. @endverbatim
  31. */
  32. class TextBox : public Label
  33. {
  34. friend class Container;
  35. public:
  36. /**
  37. * Create a new text box control.
  38. *
  39. * @param id The control's ID.
  40. * @param style The control's style.
  41. *
  42. * @return The new text box.
  43. * @script{create}
  44. */
  45. static TextBox* create(const char* id, Theme::Style* style);
  46. /**
  47. * Add a listener to be notified of specific events affecting
  48. * this control. Event types can be OR'ed together.
  49. * E.g. To listen to touch-press and touch-release events,
  50. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  51. * as the second parameter.
  52. *
  53. * @param listener The listener to add.
  54. * @param eventFlags The events to listen for.
  55. */
  56. virtual void addListener(Control::Listener* listener, int eventFlags);
  57. /**
  58. * Get the last key pressed within this text box.
  59. *
  60. * @return The last key pressed within this text box.
  61. */
  62. int getLastKeypress();
  63. /**
  64. * @see Control::getType
  65. */
  66. const char* getType() const;
  67. protected:
  68. /**
  69. * Constructor.
  70. */
  71. TextBox();
  72. /**
  73. * Destructor.
  74. */
  75. ~TextBox();
  76. /**
  77. * Create a text box with a given style and properties.
  78. *
  79. * @param style The style to apply to this text box.
  80. * @param properties The properties to set on this text box.
  81. *
  82. * @return The new text box.
  83. */
  84. static TextBox* create(Theme::Style* style, Properties* properties);
  85. /**
  86. * Touch callback on touch events. Controls return true if they consume the touch event.
  87. *
  88. * @param evt The touch event that occurred.
  89. * @param x The x position of the touch in pixels. Left edge is zero.
  90. * @param y The y position of the touch in pixels. Top edge is zero.
  91. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  92. *
  93. * @return Whether the touch event was consumed by the control.
  94. *
  95. * @see Touch::TouchEvent
  96. */
  97. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  98. /**
  99. * Keyboard callback on key events.
  100. *
  101. * @param evt The key event that occurred.
  102. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  103. * If evt is KEY_CHAR then key is the unicode value of the character.
  104. *
  105. * @see Keyboard::KeyEvent
  106. * @see Keyboard::Key
  107. */
  108. bool keyEvent(Keyboard::KeyEvent evt, int key);
  109. /**
  110. * Called when a control's properties change. Updates this control's internal rendering
  111. * properties, such as its text viewport.
  112. *
  113. * @param container This control's parent container.
  114. * @param offset Positioning offset to add to the control's position.
  115. */
  116. void update(const Control* container, const Vector2& offset);
  117. /**
  118. * Draw the images associated with this control.
  119. *
  120. * @param spriteBatch The sprite batch containing this control's icons.
  121. * @param clip The clipping rectangle of this control's parent container.
  122. */
  123. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  124. /**
  125. * The current position of the TextBox's caret.
  126. */
  127. Vector2 _caretLocation;
  128. /**
  129. * The previous position of the TextBox's caret.
  130. */
  131. Vector2 _prevCaretLocation;
  132. /**
  133. * The index into the TextBox's string that the caret is.
  134. */
  135. unsigned int textIndex;
  136. /**
  137. * The last character that was entered into the TextBox.
  138. */
  139. int _lastKeypress;
  140. /**
  141. * The font size to be used in the TextBox.
  142. */
  143. unsigned int _fontSize;
  144. /**
  145. * The Theme::Image for the TextBox's caret.
  146. */
  147. Theme::ThemeImage* _caretImage;
  148. private:
  149. /**
  150. * Constructor.
  151. */
  152. TextBox(const TextBox& copy);
  153. void setCaretLocation(int x, int y);
  154. };
  155. }
  156. #endif