Container.h 15 KB

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