Container.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. *
  12. * container <Container ID>
  13. * {
  14. * // Container properties.
  15. * layout = <Layout Type> // A value from the Layout::Type enum. E.g.: LAYOUT_VERTICAL
  16. * style = <Style ID> // A style from the form's theme.
  17. * position = <x, y> // Position of the container on-screen, measured in pixels.
  18. * size = <width, height> // Size of the container, measured in pixels.
  19. *
  20. * // All the nested controls within this container.
  21. * container { }
  22. * label { }
  23. * textBox { }
  24. * button{}
  25. * checkBox{}
  26. * radioButton{}
  27. * slider{}
  28. * }
  29. */
  30. class Container : public Control
  31. {
  32. public:
  33. /**
  34. * Get this container's layout.
  35. *
  36. * @return This container's layout object.
  37. */
  38. Layout* getLayout();
  39. /**
  40. * Add a control to this layout.
  41. * The control will be assigned the next available index.
  42. *
  43. * @param control The Control to add.
  44. *
  45. * @return The index assigned to the added Control.
  46. */
  47. unsigned int addControl(Control* control);
  48. /**
  49. * Insert a control at a specific index.
  50. *
  51. * @param control The control to add.
  52. * @param index The index at which to insert the control.
  53. */
  54. void insertControl(Control* control, unsigned int index);
  55. /**
  56. * Remove a control at a specific index.
  57. *
  58. * @param index The index from which to remove the control.
  59. */
  60. void removeControl(unsigned int index);
  61. /**
  62. * Remove a control with the given ID.
  63. *
  64. * @param ID The ID of the control to remove.
  65. */
  66. void removeControl(const char* id);
  67. /**
  68. * Remove a specific control.
  69. *
  70. * @param control The control to remove.
  71. */
  72. void removeControl(Control* control);
  73. /**
  74. * Get the Control at a specific index.
  75. *
  76. * @param index The index at which to retrieve the Control.
  77. *
  78. * @return The Control at the given index.
  79. */
  80. Control* getControl(unsigned int index) const;
  81. /**
  82. * Get a Control with a specific ID that belongs to this Layout.
  83. *
  84. * @param id The ID of the Control to search for.
  85. */
  86. Control* getControl(const char* id) const;
  87. /**
  88. * Get the vector of controls within this container.
  89. *
  90. * @return The vector of the controls within this container.
  91. */
  92. std::vector<Control*> getControls() const;
  93. /**
  94. * Gets the first animation in the control with the specified ID.
  95. *
  96. * @param id The ID of the animation to get. Returns the first animation if ID is NULL.
  97. * @return The first animation with the specified ID.
  98. */
  99. Animation* getAnimation(const char* id = NULL) const;
  100. protected:
  101. /**
  102. * Constructor.
  103. */
  104. Container();
  105. /**
  106. * Destructor.
  107. */
  108. virtual ~Container();
  109. /**
  110. * Create an empty container. A container's layout type must be specified at creation time.
  111. *
  112. * @param type The container's layout type.
  113. */
  114. static Container* create(Layout::Type type);
  115. /**
  116. * Create a container with a given style and properties, including a list of controls.
  117. *
  118. * @param style The style to apply to this container.
  119. * @param properties The properties to set on this container, including nested controls.
  120. * @param theme The theme to search for control styles within.
  121. *
  122. * @return The new container.
  123. */
  124. static Container* create(Theme::Style* style, Properties* properties, Theme* theme);
  125. /**
  126. * Updates each control within this container,
  127. * and positions them according to the container's layout.
  128. *
  129. * @param clip The clipping rectangle of this container's parent container.
  130. */
  131. virtual void update(const Rectangle& clip);
  132. /**
  133. * Draws the themed border and background of this container and all its controls.
  134. *
  135. * @param spriteBatch The sprite batch containing this container's border images.
  136. * @param clip The clipping rectangle of this container's parent container.
  137. */
  138. void drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip);
  139. /**
  140. * Draws the icons of all controls within this container.
  141. *
  142. * @param spriteBatch The sprite batch containing this control's icons.
  143. * @param clip The clipping rectangle of this container's parent container.
  144. */
  145. virtual void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  146. /**
  147. * Draws the text of all controls within this container.
  148. *
  149. * @param clip The clipping rectangle of this container's parent container.
  150. */
  151. virtual void drawText(const Rectangle& clip);
  152. /**
  153. * Touch callback on touch events. Controls return true if they consume the touch event.
  154. *
  155. * @param evt The touch event that occurred.
  156. * @param x The x position of the touch in pixels. Left edge is zero.
  157. * @param y The y position of the touch in pixels. Top edge is zero.
  158. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  159. *
  160. * @return Whether the touch event was consumed by a control within this container.
  161. *
  162. * @see Touch::TouchEvent
  163. */
  164. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  165. /**
  166. * Keyboard callback on key events. Passes key events on to the currently focused control.
  167. *
  168. * @param evt The key event that occured.
  169. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  170. * If evt is KEY_CHAR then key is the unicode value of the character.
  171. *
  172. * @see Keyboard::KeyEvent
  173. * @see Keyboard::Key
  174. */
  175. virtual void keyEvent(Keyboard::KeyEvent evt, int key);
  176. /**
  177. * Gets a Layout::Type enum from a matching string.
  178. */
  179. static Layout::Type getLayoutType(const char* layoutString);
  180. /**
  181. * Returns whether this control is a container.
  182. * This is true in this case.
  183. */
  184. bool isContainer();
  185. /**
  186. * Returns whether this container or any of its controls have been modified and require an update.
  187. */
  188. virtual bool isDirty();
  189. /**
  190. * Adds controls nested within a properties object to this container,
  191. * searching for styles within the given theme.
  192. */
  193. void addControls(Theme* theme, Properties* properties);
  194. Layout* _layout; // This container's layout.
  195. std::vector<Control*> _controls; // List of controls within this container.
  196. private:
  197. Container(const Container& copy);
  198. };
  199. }
  200. #endif