Control.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. #ifndef CONTROL_H_
  2. #define CONTROL_H_
  3. #include "Ref.h"
  4. #include "Rectangle.h"
  5. #include "Vector2.h"
  6. #include "SpriteBatch.h"
  7. #include "Theme.h"
  8. #include "Touch.h"
  9. #include "Keyboard.h"
  10. namespace gameplay
  11. {
  12. /**
  13. * Base class for UI controls.
  14. */
  15. class Control : public Ref
  16. {
  17. friend class Form;
  18. friend class Container;
  19. friend class Layout;
  20. friend class AbsoluteLayout;
  21. friend class VerticalLayout;
  22. public:
  23. /**
  24. * The possible states a control can be in.
  25. */
  26. enum State
  27. {
  28. NORMAL,
  29. FOCUS,
  30. ACTIVE,
  31. DISABLED
  32. };
  33. /**
  34. * Get this control's ID string.
  35. *
  36. * @return This control's ID.
  37. */
  38. const char* getID() const;
  39. /**
  40. * Set the position of this control relative to its parent container.
  41. *
  42. * @param x The x coordinate.
  43. * @param y The y coordinate.
  44. */
  45. void setPosition(float x, float y);
  46. /**
  47. * Get the position of this control relative to its parent container.
  48. *
  49. * @return The position of this control relative to its parent container.
  50. */
  51. const Vector2& getPosition() const;
  52. /**
  53. * Set the desired size of this control, including its border and padding, before clipping.
  54. *
  55. * @param width The width.
  56. * @param height The height.
  57. */
  58. void setSize(float width, float height);
  59. /**
  60. * Get the desired size of this control, including its border and padding, before clipping.
  61. *
  62. * @return The size of this control.
  63. */
  64. const Vector2& getSize() const;
  65. /**
  66. * Get the bounds of this control, relative to its parent container, after clipping.
  67. *
  68. * @return The bounds of this control.
  69. */
  70. const Rectangle& getBounds() const;
  71. /**
  72. * Get the content area of this control, in screen coordinates, after clipping.
  73. *
  74. * @return The clipping area of this control.
  75. */
  76. const Rectangle& getClip() const;
  77. /**
  78. * Set width and/or height to auto-size to size a Control to tightly fit
  79. * its text and themed visual elements (CheckBox / RadioButton toggle etc.).
  80. *
  81. * Similarly set this on the width and/or height of a Container to tightly fit
  82. * the Container around all its children.
  83. *
  84. * @param width Whether to automatically determine this Control's width.
  85. * @param height Whether to automatically determine this Control's height.
  86. */
  87. void setAutoSize(bool width, bool height);
  88. /**
  89. * Change this control's state.
  90. *
  91. * @param state The state to switch this control to.
  92. */
  93. void setState(State state);
  94. /**
  95. * Get this control's current state.
  96. *
  97. * @return This control's current state.
  98. */
  99. State getState();
  100. /**
  101. * Disable this control.
  102. */
  103. void disable();
  104. /**
  105. * Enable this control.
  106. */
  107. void enable();
  108. /**
  109. * Get whether this control is currently enabled.
  110. *
  111. * @return Whether this control is currently enabled.
  112. */
  113. bool isEnabled();
  114. /**
  115. * Set whether this control consumes touch events,
  116. * preventing them from being passed to the game.
  117. *
  118. * @param consume Whether this control consumes touch events.
  119. */
  120. void setConsumeTouchEvents(bool consume);
  121. /**
  122. * Get whether this control consumes touch events.
  123. *
  124. * @return Whether this control consumes touch events.
  125. */
  126. bool getConsumeTouchEvents();
  127. /**
  128. * Set the style this control will use when rendering.
  129. *
  130. * @param style The style this control will use when rendering.
  131. */
  132. void setStyle(Theme::Style* style);
  133. /**
  134. * Get this control's style.
  135. *
  136. * @return This control's style.
  137. */
  138. Theme::Style* getStyle() const;
  139. protected:
  140. Control();
  141. virtual ~Control();
  142. /**
  143. * Get the overlay type corresponding to this control's current state.
  144. *
  145. * @return The overlay type corresponding to this control's current state.
  146. */
  147. Theme::Style::OverlayType getOverlayType() const;
  148. /**
  149. * Touch callback on touch events. Controls return true if they consume the touch event.
  150. *
  151. * @param evt The touch event that occurred.
  152. * @param x The x position of the touch in pixels. Left edge is zero.
  153. * @param y The y position of the touch in pixels. Top edge is zero.
  154. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  155. *
  156. * @return Whether the touch event was consumed by this control.
  157. *
  158. * @see Touch::TouchEvent
  159. */
  160. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  161. /**
  162. * Keyboard callback on key events.
  163. *
  164. * @param evt The key event that occured.
  165. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  166. * If evt is KEY_CHAR then key is the unicode value of the character.
  167. *
  168. * @see Keyboard::KeyEvent
  169. * @see Keyboard::Key
  170. */
  171. virtual void keyEvent(Keyboard::KeyEvent evt, int key);
  172. /**
  173. * Called when a control's properties change. Updates this control's internal rendering
  174. * properties, such as its text viewport.
  175. *
  176. * @param clip The clipping rectangle of this control's parent container.
  177. */
  178. virtual void update(const Rectangle& clip);
  179. /**
  180. * Draws the themed border and background of a control.
  181. *
  182. * @param spriteBatch The sprite batch containing this control's border images.
  183. * @param clip The clipping rectangle of this control's parent container.
  184. */
  185. virtual void drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip);
  186. /**
  187. * Draw the icons associated with this control.
  188. *
  189. * @param spriteBatch The sprite batch containing this control's icons.
  190. * @param clip The clipping rectangle of this control's parent container.
  191. */
  192. virtual void drawSprites(SpriteBatch* spriteBatch, const Rectangle& clip);
  193. /**
  194. * Draw this control's text.
  195. *
  196. * @param clip The clipping rectangle of this control's parent container.
  197. */
  198. virtual void drawText(const Rectangle& clip);
  199. /**
  200. * Initialize properties common to all Controls.
  201. */
  202. virtual void init(Theme::Style* style, Properties* properties);
  203. /**
  204. * Container and classes that extend it should implement this and return true.
  205. */
  206. virtual bool isContainer();
  207. /**
  208. * Returns whether this control has been modified and requires an update.
  209. */
  210. virtual bool isDirty();
  211. /**
  212. * Get a Control::State enum from a matching string.
  213. */
  214. static State getStateFromString(const char* state);
  215. std::string _id;
  216. State _state; // Determines overlay used during draw().
  217. Vector2 _position; // Position, relative to parent container's clipping window.
  218. Vector2 _size; // Desired size. Will be clipped.
  219. Rectangle _bounds; // The position and size of this control, relative to parent container's bounds, including border and padding, after clipping.
  220. Rectangle _clip; // Clipping window of this control's content.
  221. bool _autoWidth;
  222. bool _autoHeight;
  223. bool _dirty;
  224. bool _consumeTouchEvents;
  225. Theme::Style* _style;
  226. private:
  227. Control(const Control& copy);
  228. };
  229. }
  230. #endif