TextBox.h 4.5 KB

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