Container.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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> // Whether scrolling is allowed and in which directions.
  25. scrollBarsAutoHide = <bool> // Whether scrollbars fade out when not in use.
  26. // All the nested controls within this container.
  27. container
  28. {
  29. ...
  30. }
  31. label { }
  32. textBox { }
  33. button { }
  34. checkBox { }
  35. radioButton { }
  36. slider { }
  37. }
  38. @endverbatim
  39. */
  40. class Container : public Control
  41. {
  42. public:
  43. /**
  44. * Constant used to auto-hide scrollbars.
  45. */
  46. static const int ANIMATE_SCROLLBAR_OPACITY = 8;
  47. /**
  48. * The definition for container scrolling.
  49. */
  50. enum Scroll
  51. {
  52. SCROLL_NONE = 0,
  53. SCROLL_HORIZONTAL = 0x01,
  54. SCROLL_VERTICAL = 0x02,
  55. SCROLL_BOTH = SCROLL_HORIZONTAL | SCROLL_VERTICAL
  56. };
  57. /**
  58. * Create a new container.
  59. *
  60. * @param id The container's ID.
  61. * @param style The container's style.
  62. * @param layoutType The container's layout type.
  63. *
  64. * @return The new container.
  65. * @script{create}
  66. */
  67. static Container* create(const char* id, Theme::Style* style, Layout::Type layoutType = Layout::LAYOUT_ABSOLUTE);
  68. /**
  69. * Get this container's layout.
  70. *
  71. * @return This container's layout object.
  72. */
  73. Layout* getLayout();
  74. /**
  75. * Add a control to this layout.
  76. * The control will be assigned the next available index.
  77. *
  78. * @param control The Control to add.
  79. *
  80. * @return The index assigned to the added Control.
  81. */
  82. unsigned int addControl(Control* control);
  83. /**
  84. * Insert a control at a specific index.
  85. *
  86. * @param control The control to add.
  87. * @param index The index at which to insert the control.
  88. */
  89. void insertControl(Control* control, unsigned int index);
  90. /**
  91. * Remove a control at a specific index.
  92. *
  93. * @param index The index from which to remove the control.
  94. */
  95. void removeControl(unsigned int index);
  96. /**
  97. * Remove a control with the given ID.
  98. *
  99. * @param id The ID of the control to remove.
  100. */
  101. void removeControl(const char* id);
  102. /**
  103. * Remove a specific control.
  104. *
  105. * @param control The control to remove.
  106. */
  107. void removeControl(Control* control);
  108. /**
  109. * Get the Control at a specific index.
  110. *
  111. * @param index The index at which to retrieve the Control.
  112. *
  113. * @return The Control at the given index.
  114. */
  115. Control* getControl(unsigned int index) const;
  116. /**
  117. * Get a Control with a specific ID that belongs to this Layout.
  118. *
  119. * @param id The ID of the Control to search for.
  120. */
  121. Control* getControl(const char* id) const;
  122. /**
  123. * Get the vector of controls within this container.
  124. *
  125. * @return The vector of the controls within this container.
  126. * @script{ignore}
  127. */
  128. const std::vector<Control*>& getControls() const;
  129. /**
  130. * Sets the allowed scroll directions for this container.
  131. *
  132. * @param scroll The allowed scroll directions for this container.
  133. */
  134. void setScroll(Scroll scroll);
  135. /**
  136. * Gets the allowed scroll directions for this container.
  137. *
  138. * @return The allowed scroll directions for this container.
  139. */
  140. Scroll getScroll() const;
  141. /**
  142. * Set whether scrollbars auto hidden when they become static.
  143. *
  144. * @param autoHide true to auto hide the scrollbars when they become static.
  145. */
  146. void setScrollBarsAutoHide(bool autoHide);
  147. /**
  148. * Whether scrollbars are always visible, or only visible while scrolling.
  149. *
  150. * @return Whether scrollbars are always visible.
  151. */
  152. bool isScrollBarsAutoHide() const;
  153. /**
  154. * @see AnimationTarget::getAnimation
  155. */
  156. Animation* getAnimation(const char* id = NULL) const;
  157. /**
  158. * @see Control::isContainer
  159. */
  160. bool isContainer() const;
  161. /**
  162. * @see Control::getType
  163. */
  164. const char* getType() const;
  165. /**
  166. * @see AnimationTarget::getAnimationPropertyComponentCount
  167. */
  168. virtual unsigned int getAnimationPropertyComponentCount(int propertyId) const;
  169. /**
  170. * @see AnimationTarget::getAnimationProperty
  171. */
  172. virtual void getAnimationPropertyValue(int propertyId, AnimationValue* value);
  173. /**
  174. * @see AnimationTarget::setAnimationProperty
  175. */
  176. virtual void setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight = 1.0f);
  177. protected:
  178. /**
  179. * Constructor.
  180. */
  181. Container();
  182. /**
  183. * Destructor.
  184. */
  185. virtual ~Container();
  186. /**
  187. * Create an empty container. A container's layout type must be specified at creation time.
  188. *
  189. * @param type The container's layout type.
  190. *
  191. * @return The new container.
  192. */
  193. static Container* create(Layout::Type type);
  194. /**
  195. * Create a container with a given style and properties, including a list of controls.
  196. *
  197. * @param style The style to apply to this container.
  198. * @param properties The properties to set on this container, including nested controls.
  199. * @param theme The theme to search for control styles within.
  200. *
  201. * @return The new container.
  202. */
  203. static Container* create(Theme::Style* style, Properties* properties, Theme* theme);
  204. /**
  205. * Updates each control within this container,
  206. * and positions them according to the container's layout.
  207. *
  208. * @param container This container's parent container.
  209. * @param offset The offset.
  210. */
  211. virtual void update(const Control* container, const Vector2& offset);
  212. /**
  213. * Touch callback on touch events. Controls return true if they consume the touch event.
  214. *
  215. * @param evt The touch event that occurred.
  216. * @param x The x position of the touch in pixels. Left edge is zero.
  217. * @param y The y position of the touch in pixels. Top edge is zero.
  218. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  219. *
  220. * @return Whether the touch event was consumed by a control within this container.
  221. *
  222. * @see Touch::TouchEvent
  223. */
  224. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  225. /**
  226. * Keyboard callback on key events. Passes key events on to the currently focused control.
  227. *
  228. * @param evt The key event that occurred.
  229. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  230. * If evt is KEY_CHAR then key is the unicode value of the character.
  231. *
  232. * @return Whether the key event was consumed by this control.
  233. *
  234. * @see Keyboard::KeyEvent
  235. * @see Keyboard::Key
  236. */
  237. virtual bool keyEvent(Keyboard::KeyEvent evt, int key);
  238. /**
  239. * Mouse callback on mouse events.
  240. *
  241. * @param evt The mouse event that occurred.
  242. * @param x The x position of the mouse in pixels. Left edge is zero.
  243. * @param y The y position of the mouse in pixels. Top edge is zero.
  244. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  245. *
  246. * @return True if the mouse event is consumed or false if it is not consumed.
  247. *
  248. * @see Mouse::MouseEvent
  249. */
  250. virtual bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  251. /**
  252. * Gets a Layout::Type enum from a matching string.
  253. *
  254. * @param layoutString The layout string to parse
  255. */
  256. static Layout::Type getLayoutType(const char* layoutString);
  257. /**
  258. * Returns whether this container or any of its controls have been modified and require an update.
  259. *
  260. * @return true if this container or any of its controls have been modified and require an update.
  261. */
  262. virtual bool isDirty();
  263. /**
  264. * Adds controls nested within a properties object to this container,
  265. * searching for styles within the given theme.
  266. *
  267. * @param theme The them to add controls from
  268. * @param properties The properties to use.
  269. */
  270. void addControls(Theme* theme, Properties* properties);
  271. /**
  272. * Draws a sprite batch for the specified clipping rect .
  273. *
  274. * @param spriteBatch The sprite batch to use.
  275. * @param clip The clipping rectangle.
  276. * @param needsClear Whether it needs to be cleared.
  277. * @param cleared Whether it was previously cleared
  278. * @param targetHeight The targets height
  279. */
  280. virtual void draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, bool cleared, float targetHeight);
  281. /**
  282. * Update scroll position and velocity.
  283. */
  284. void updateScroll();
  285. /**
  286. * Applies touch events to scroll state.
  287. *
  288. * @param evt The touch event that occurred.
  289. * @param x The x position of the touch in pixels. Left edge is zero.
  290. * @param y The y position of the touch in pixels. Top edge is zero.
  291. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  292. *
  293. * @return Whether the touch event was consumed by scrolling within this container.
  294. *
  295. * @see Touch::TouchEvent
  296. */
  297. bool touchEventScroll(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  298. /**
  299. * Mouse scroll event callback.
  300. *
  301. * @param evt The mouse scroll event that occurred.
  302. * @param x The x position of the scroll in pixels. Left edge is zero.
  303. * @param y The y position of the scroll in pixels. Top edge is zero.
  304. * @param wheelDelta The value change of the mouse's scroll wheel.
  305. *
  306. * @return Whether the scroll event was consumed by scrolling within this container.
  307. *
  308. * @see Mouse::MouseEvent
  309. */
  310. bool mouseEventScroll(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  311. /**
  312. * Mouse pointer event callback.
  313. *
  314. * @param mouse Whether to treat the event as a mouse event or a touch event.
  315. * @param evt The pointer event (either a Mouse::MouseEvent or a Touch::TouchEvent).
  316. * @param x The x position of the pointer event in pixels. Left edge is zero.
  317. * @param y The y position of the pointer event in pixels. Top edge is zero.
  318. * @param data The event's data (depends on whether it is a mouse event or a touch event).
  319. *
  320. * @return Whether the pointer event was consumed by this container.
  321. *
  322. * @see Mouse::MouseEvent
  323. * @see Touch::TouchEvent
  324. */
  325. bool pointerEvent(bool mouse, char evt, int x, int y, int data);
  326. /**
  327. * Get a Scroll enum from a matching string.
  328. *
  329. * @param scroll A string representing a Scroll enum.
  330. *
  331. * @return The Scroll enum value that matches the given string.
  332. */
  333. static Scroll getScroll(const char* scroll);
  334. /**
  335. * The container's layout.
  336. */
  337. Layout* _layout;
  338. /**
  339. * List of controls within the container.
  340. */
  341. std::vector<Control*> _controls;
  342. /**
  343. * Scrollbar top cap image.
  344. */
  345. Theme::ThemeImage* _scrollBarTopCap;
  346. /**
  347. * Scrollbar vertical image.
  348. */
  349. Theme::ThemeImage* _scrollBarVertical;
  350. /**
  351. * Scrollbar bottom cap image.
  352. */
  353. Theme::ThemeImage* _scrollBarBottomCap;
  354. /**
  355. * Scrollbar left cap image.
  356. */
  357. Theme::ThemeImage* _scrollBarLeftCap;
  358. /**
  359. * Scrollbar horizontal image.
  360. */
  361. Theme::ThemeImage* _scrollBarHorizontal;
  362. /**
  363. * Scrollbar horizontal image.
  364. */
  365. Theme::ThemeImage* _scrollBarRightCap;
  366. /**
  367. * Flag representing whether scrolling is enabled, and in which directions.
  368. */
  369. Scroll _scroll;
  370. /**
  371. * Scroll bar bounds
  372. */
  373. Rectangle _scrollBarBounds;
  374. /**
  375. * How far this layout has been scrolled in each direction.
  376. */
  377. Vector2 _scrollPosition;
  378. /**
  379. * Should the scrollbars auto hide. Default is false.
  380. */
  381. bool _scrollBarsAutoHide;
  382. /**
  383. * Used to animate scrollbars fading out.
  384. */
  385. float _scrollBarOpacity;
  386. /**
  387. * Whether the user is currently touching / holding the mouse down within this layout's container.
  388. */
  389. bool _scrolling;
  390. /**
  391. * First scrolling touch x position
  392. */
  393. int _scrollingFirstX;
  394. /**
  395. * First scrolling touch y position
  396. */
  397. int _scrollingFirstY;
  398. /**
  399. * The last y position when scrolling
  400. */
  401. int _scrollingLastX;
  402. /**
  403. * The last x position when scrolling
  404. */
  405. int _scrollingLastY;
  406. /**
  407. * Time we started scrolling in the x
  408. */
  409. double _scrollingStartTimeX;
  410. /**
  411. * Time we started scrolling in the y
  412. */
  413. double _scrollingStartTimeY;
  414. /**
  415. * The last time we were scrolling
  416. */
  417. double _scrollingLastTime;
  418. /**
  419. * Speed to continue scrolling at after touch release.
  420. */
  421. Vector2 _scrollingVelocity;
  422. /**
  423. * Friction dampens velocity.
  424. */
  425. float _scrollingFriction;
  426. /**
  427. * Are we scrolling to the right?
  428. */
  429. bool _scrollingRight;
  430. /**
  431. * Are we scrolling down?
  432. */
  433. bool _scrollingDown;
  434. /**
  435. * Locked to scrolling vertically by grabbing the scrollbar with the mouse.
  436. */
  437. bool _scrollingMouseVertically;
  438. /**
  439. * Locked to scrolling horizontally by grabbing the scrollbar with the mouse.
  440. */
  441. bool _scrollingMouseHorizontally;
  442. private:
  443. /**
  444. * Constructor.
  445. */
  446. Container(const Container& copy);
  447. AnimationClip* _scrollBarOpacityClip;
  448. int _zIndexDefault;
  449. int _focusIndexDefault;
  450. int _focusIndexMax;
  451. float _totalWidth;
  452. float _totalHeight;
  453. };
  454. }
  455. #endif