TextBox.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. *
  18. * label <labelID>
  19. * {
  20. * style = <styleID>
  21. * position = <x, y>
  22. * size = <width, height>
  23. * text = <string>
  24. * }
  25. */
  26. class TextBox : public Label
  27. {
  28. friend class Container;
  29. public:
  30. /**
  31. * Add a listener to be notified of specific events affecting
  32. * this control. Event types can be OR'ed together.
  33. * E.g. To listen to touch-press and touch-release events,
  34. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  35. * as the second parameter.
  36. *
  37. * @param listener The listener to add.
  38. * @param eventFlags The events to listen for.
  39. */
  40. virtual void addListener(Control::Listener* listener, int eventFlags);
  41. /**
  42. * Get the last key pressed within this text box.
  43. *
  44. * @return The last key pressed within this text box.
  45. */
  46. int getLastKeypress();
  47. protected:
  48. /**
  49. * Constructor.
  50. */
  51. TextBox();
  52. /**
  53. * Destructor.
  54. */
  55. ~TextBox();
  56. /**
  57. * Create a text box with a given style and properties.
  58. *
  59. * @param style The style to apply to this text box.
  60. * @param properties The properties to set on this text box.
  61. *
  62. * @return The new text box.
  63. */
  64. static TextBox* create(Theme::Style* style, Properties* properties);
  65. /**
  66. * Touch callback on touch events. Controls return true if they consume the touch event.
  67. *
  68. * @param evt The touch event that occurred.
  69. * @param x The x position of the touch in pixels. Left edge is zero.
  70. * @param y The y position of the touch in pixels. Top edge is zero.
  71. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  72. *
  73. * @return Whether the touch event was consumed by the control.
  74. *
  75. * @see Touch::TouchEvent
  76. */
  77. bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  78. /**
  79. * Keyboard callback on key events.
  80. *
  81. * @param evt The key event that occured.
  82. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  83. * If evt is KEY_CHAR then key is the unicode value of the character.
  84. *
  85. * @see Keyboard::KeyEvent
  86. * @see Keyboard::Key
  87. */
  88. void keyEvent(Keyboard::KeyEvent evt, int key);
  89. /**
  90. * Called when a control's properties change. Updates this control's internal rendering
  91. * properties, such as its text viewport.
  92. *
  93. * @param clip The clipping rectangle of this control's parent container.
  94. */
  95. void update(const Rectangle& clip);
  96. /**
  97. * Draw the images associated with this control.
  98. *
  99. * @param spriteBatch The sprite batch containing this control's icons.
  100. * @param clip The clipping rectangle of this control's parent container.
  101. */
  102. void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  103. Vector2 _caretLocation;
  104. unsigned int textIndex;
  105. int _lastKeypress;
  106. private:
  107. /**
  108. * Constructor.
  109. */
  110. TextBox(const TextBox& copy);
  111. void setCaretLocation(int x, int y);
  112. };
  113. }
  114. #endif