Container.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #ifndef CONTAINER_H_
  2. #define CONTAINER_H_
  3. #include "Control.h"
  4. #include "Layout.h"
  5. namespace gameplay
  6. {
  7. /**
  8. * A container is a UI control that can contain other controls.
  9. *
  10. * The following properties are available for containers:
  11. @verbatim
  12. container <containerID>
  13. {
  14. // Container properties.
  15. layout = <Layout::Type> // A value from the Layout::Type enum. E.g.: LAYOUT_VERTICAL
  16. style = <styleID> // A style from the form's theme.
  17. alignment = <Control::Alignment constant> // Note: 'position' will be ignored.
  18. position = <x, y> // Position of the container on-screen, measured in pixels.
  19. autoWidth = <bool>
  20. autoHeight = <bool>
  21. size = <width, height> // Size of the container, measured in pixels.
  22. width = <width> // Can be used in place of 'size', e.g. with 'autoHeight = true'
  23. height = <height> // Can be used in place of 'size', e.g. with 'autoWidth = true'
  24. scroll = <Container::Scroll constant>
  25. // All the nested controls within this container.
  26. container
  27. {
  28. ...
  29. }
  30. label { }
  31. textBox { }
  32. button { }
  33. checkBox { }
  34. radioButton { }
  35. slider { }
  36. }
  37. @endverbatim
  38. */
  39. class Container : public Control
  40. {
  41. public:
  42. /**
  43. * The definition for container scrolling.
  44. */
  45. enum Scroll
  46. {
  47. SCROLL_NONE = 0,
  48. SCROLL_HORIZONTAL = 0x01,
  49. SCROLL_VERTICAL = 0x02,
  50. SCROLL_BOTH = SCROLL_HORIZONTAL | SCROLL_VERTICAL
  51. };
  52. /**
  53. * Get this container's layout.
  54. *
  55. * @return This container's layout object.
  56. */
  57. Layout* getLayout();
  58. /**
  59. * Add a control to this layout.
  60. * The control will be assigned the next available index.
  61. *
  62. * @param control The Control to add.
  63. *
  64. * @return The index assigned to the added Control.
  65. */
  66. unsigned int addControl(Control* control);
  67. /**
  68. * Insert a control at a specific index.
  69. *
  70. * @param control The control to add.
  71. * @param index The index at which to insert the control.
  72. */
  73. void insertControl(Control* control, unsigned int index);
  74. /**
  75. * Remove a control at a specific index.
  76. *
  77. * @param index The index from which to remove the control.
  78. */
  79. void removeControl(unsigned int index);
  80. /**
  81. * Remove a control with the given ID.
  82. *
  83. * @param id The ID of the control to remove.
  84. */
  85. void removeControl(const char* id);
  86. /**
  87. * Remove a specific control.
  88. *
  89. * @param control The control to remove.
  90. */
  91. void removeControl(Control* control);
  92. /**
  93. * Get the Control at a specific index.
  94. *
  95. * @param index The index at which to retrieve the Control.
  96. *
  97. * @return The Control at the given index.
  98. */
  99. Control* getControl(unsigned int index) const;
  100. /**
  101. * Get a Control with a specific ID that belongs to this Layout.
  102. *
  103. * @param id The ID of the Control to search for.
  104. */
  105. Control* getControl(const char* id) const;
  106. /**
  107. * Get the vector of controls within this container.
  108. *
  109. * @return The vector of the controls within this container.
  110. */
  111. const std::vector<Control*>& getControls() const;
  112. /**
  113. * Sets the scrolling for the container.
  114. *
  115. * @param scroll The scroll for the
  116. */
  117. void setScroll(Scroll scroll);
  118. Scroll getScroll() const;
  119. /**
  120. * Gets the first animation in the control with the specified ID.
  121. *
  122. * @param id The ID of the animation to get. Returns the first animation if ID is NULL.
  123. * @return The first animation with the specified ID.
  124. */
  125. Animation* getAnimation(const char* id = NULL) const;
  126. protected:
  127. /**
  128. * Constructor.
  129. */
  130. Container();
  131. /**
  132. * Destructor.
  133. */
  134. virtual ~Container();
  135. /**
  136. * Create an empty container. A container's layout type must be specified at creation time.
  137. *
  138. * @param type The container's layout type.
  139. */
  140. static Container* create(Layout::Type type);
  141. /**
  142. * Create a container with a given style and properties, including a list of controls.
  143. *
  144. * @param style The style to apply to this container.
  145. * @param properties The properties to set on this container, including nested controls.
  146. * @param theme The theme to search for control styles within.
  147. *
  148. * @return The new container.
  149. */
  150. static Container* create(Theme::Style* style, Properties* properties, Theme* theme);
  151. /**
  152. * Updates each control within this container,
  153. * and positions them according to the container's layout.
  154. *
  155. * @param clip The clipping rectangle of this container's parent container.
  156. */
  157. virtual void update(const Rectangle& clip, const Vector2& offset);
  158. /**
  159. * Touch callback on touch events. Controls return true if they consume the touch event.
  160. *
  161. * @param evt The touch event that occurred.
  162. * @param x The x position of the touch in pixels. Left edge is zero.
  163. * @param y The y position of the touch in pixels. Top edge is zero.
  164. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  165. *
  166. * @return Whether the touch event was consumed by a control within this container.
  167. *
  168. * @see Touch::TouchEvent
  169. */
  170. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  171. /**
  172. * Keyboard callback on key events. Passes key events on to the currently focused control.
  173. *
  174. * @param evt The key event that occured.
  175. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  176. * If evt is KEY_CHAR then key is the unicode value of the character.
  177. *
  178. * @see Keyboard::KeyEvent
  179. * @see Keyboard::Key
  180. */
  181. virtual void keyEvent(Keyboard::KeyEvent evt, int key);
  182. /**
  183. * Gets a Layout::Type enum from a matching string.
  184. */
  185. static Layout::Type getLayoutType(const char* layoutString);
  186. /**
  187. * Returns whether this control is a container.
  188. * This is true in this case.
  189. */
  190. bool isContainer();
  191. /**
  192. * Returns whether this container or any of its controls have been modified and require an update.
  193. */
  194. virtual bool isDirty();
  195. /**
  196. * Adds controls nested within a properties object to this container,
  197. * searching for styles within the given theme.
  198. */
  199. void addControls(Theme* theme, Properties* properties);
  200. virtual void draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, float targetHeight);
  201. /**
  202. * Update scroll position and velocity.
  203. */
  204. void updateScroll(const Container* container);
  205. bool touchEventScroll(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  206. static Scroll getScroll(const char* scroll);
  207. /**
  208. * The container's layout.
  209. */
  210. Layout* _layout;
  211. /**
  212. * List of controls within the container.
  213. */
  214. std::vector<Control*> _controls;
  215. Theme::ThemeImage* _scrollBarTopCap;
  216. Theme::ThemeImage* _scrollBarVertical;
  217. Theme::ThemeImage* _scrollBarBottomCap;
  218. Theme::ThemeImage* _scrollBarLeftCap;
  219. Theme::ThemeImage* _scrollBarHorizontal;
  220. Theme::ThemeImage* _scrollBarRightCap;
  221. // Flag representing whether scrolling is enabled, and in which directions.
  222. Scroll _scroll;
  223. // Data required when scrolling is enabled.
  224. /**
  225. * x, width: Horizontal scrollbar position and size.
  226. * y, height: Vertical scrollbar position and size.
  227. */
  228. Rectangle _scrollBarBounds;
  229. // How far this layout has been scrolled in each direction.
  230. Vector2 _scrollPosition;
  231. // Whether the user is currently touching / holding the mouse down
  232. // within this layout's container.
  233. bool _scrolling;
  234. // First touch point.
  235. int _firstX;
  236. int _firstY;
  237. // Latest touch point.
  238. int _lastX;
  239. int _lastY;
  240. // Time recorded on touch down.
  241. long _startTimeX;
  242. long _startTimeY;
  243. long _lastTime;
  244. // Speed to continue scrolling at after touch release.
  245. Vector2 _velocity;
  246. // Friction dampens velocity.
  247. float _friction;
  248. // Detect a change in scroll direction.
  249. bool _goingRight;
  250. bool _goingDown;
  251. private:
  252. Container(const Container& copy);
  253. };
  254. }
  255. #endif