TextBox.h 4.9 KB

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