Container.h 7.4 KB

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