TextBox.h 3.9 KB

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