Control.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. #ifndef CONTROL_H_
  2. #define CONTROL_H_
  3. #include "Ref.h"
  4. #include "Rectangle.h"
  5. #include "Vector2.h"
  6. #include "SpriteBatch.h"
  7. #include "Theme.h"
  8. #include "ThemeStyle.h"
  9. #include "Touch.h"
  10. #include "Keyboard.h"
  11. namespace gameplay
  12. {
  13. /**
  14. * Base class for UI controls.
  15. */
  16. class Control : public Ref, public AnimationTarget
  17. {
  18. friend class Form;
  19. friend class Container;
  20. friend class Layout;
  21. friend class AbsoluteLayout;
  22. friend class VerticalLayout;
  23. friend class FlowLayout;
  24. friend class ScrollLayout;
  25. public:
  26. /**
  27. * The possible states a control can be in.
  28. */
  29. enum State
  30. {
  31. /**
  32. * State of an enabled but inactive control.
  33. */
  34. NORMAL = 0x01,
  35. /**
  36. * State of a control currently in focus.
  37. */
  38. FOCUS = 0x02,
  39. /**
  40. * State of a control that is currently being acted on,
  41. * e.g. through touch or mouse-click events.
  42. */
  43. ACTIVE = 0x04,
  44. /**
  45. * State of a control that has been disabled.
  46. */
  47. DISABLED = 0x08,
  48. };
  49. /**
  50. * Defines the set of alignments for positioning controls and justifying text.
  51. */
  52. enum Alignment
  53. {
  54. // Specify horizontal alignment, use default vertical alignment (ALIGN_TOP).
  55. ALIGN_LEFT = 0x01,
  56. ALIGN_HCENTER = 0x02,
  57. ALIGN_RIGHT = 0x04,
  58. // Specify vertical alignment, use default horizontal alignment (ALIGN_LEFT).
  59. ALIGN_TOP = 0x10,
  60. ALIGN_VCENTER = 0x20,
  61. ALIGN_BOTTOM = 0x40,
  62. // Specify both vertical and horizontal alignment.
  63. ALIGN_TOP_LEFT = ALIGN_TOP | ALIGN_LEFT,
  64. ALIGN_VCENTER_LEFT = ALIGN_VCENTER | ALIGN_LEFT,
  65. ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT,
  66. ALIGN_TOP_HCENTER = ALIGN_TOP | ALIGN_HCENTER,
  67. ALIGN_VCENTER_HCENTER = ALIGN_VCENTER | ALIGN_HCENTER,
  68. ALIGN_BOTTOM_HCENTER = ALIGN_BOTTOM | ALIGN_HCENTER,
  69. ALIGN_TOP_RIGHT = ALIGN_TOP | ALIGN_RIGHT,
  70. ALIGN_VCENTER_RIGHT = ALIGN_VCENTER | ALIGN_RIGHT,
  71. ALIGN_BOTTOM_RIGHT = ALIGN_BOTTOM | ALIGN_RIGHT
  72. };
  73. /**
  74. * A constant used for setting themed attributes on all control states simultaneously.
  75. */
  76. static const unsigned char STATE_ALL = NORMAL | FOCUS | ACTIVE | DISABLED;
  77. /**
  78. * Implement Control::Listener and call Control::addListener()
  79. * in order to listen for events on controls.
  80. */
  81. class Listener
  82. {
  83. public:
  84. /**
  85. * Defines the Listener's event types.
  86. */
  87. enum EventType
  88. {
  89. /**
  90. * Mouse-down or touch-press event.
  91. */
  92. PRESS = 0x01,
  93. /**
  94. * Mouse-up or touch-release event.
  95. */
  96. RELEASE = 0x02,
  97. /**
  98. * Event triggered after consecutive PRESS and RELEASE events take place
  99. * within the bounds of a control.
  100. */
  101. CLICK = 0x04,
  102. /**
  103. * Event triggered when the value of a slider, check box, or radio button
  104. * changes.
  105. */
  106. VALUE_CHANGED = 0x08,
  107. /**
  108. * Event triggered when the contents of a text box are modified.
  109. */
  110. TEXT_CHANGED = 0x10
  111. };
  112. /**
  113. * Method called by controls when an event is triggered.
  114. *
  115. * @param control The control triggering the event.
  116. * @param evt The event triggered.
  117. */
  118. virtual void controlEvent(Control* control, EventType evt) = 0;
  119. };
  120. /**
  121. * Position animation property. Data = x, y
  122. */
  123. static const int ANIMATE_POSITION = 1;
  124. /**
  125. * Position x animation property. Data = x
  126. */
  127. static const int ANIMATE_POSITION_X = 2;
  128. /**
  129. * Position y animation property. Data = y
  130. */
  131. static const int ANIMATE_POSITION_Y = 3;
  132. /**
  133. * Size animation property. Data = width, height
  134. */
  135. static const int ANIMATE_SIZE = 4;
  136. /**
  137. * Size width animation property. Data = width
  138. */
  139. static const int ANIMATE_SIZE_WIDTH = 5;
  140. /**
  141. * Size height animation property. Data = height
  142. */
  143. static const int ANIMATE_SIZE_HEIGHT = 6;
  144. /**
  145. * Opacity property. Data = opacity
  146. */
  147. static const int ANIMATE_OPACITY = 7;
  148. /**
  149. * Get this control's ID string.
  150. *
  151. * @return This control's ID.
  152. */
  153. const char* getID() const;
  154. /**
  155. * Set the position of this control relative to its parent container.
  156. *
  157. * @param x The x coordinate.
  158. * @param y The y coordinate.
  159. */
  160. void setPosition(float x, float y);
  161. /**
  162. * Set the desired size of this control, including its border and padding, before clipping.
  163. *
  164. * @param width The width.
  165. * @param height The height.
  166. */
  167. virtual void setSize(float width, float height);
  168. /**
  169. * Set the bounds of this control, relative to its parent container and including its
  170. * border and padding, before clipping.
  171. *
  172. * @param bounds The new bounds to set.
  173. */
  174. virtual void setBounds(const Rectangle& bounds);
  175. /**
  176. * Get the bounds of this control, relative to its parent container and including its
  177. * border and padding, before clipping.
  178. *
  179. * @return The bounds of this control.
  180. */
  181. const Rectangle& getBounds() const;
  182. /**
  183. * Get the x coordinate of this control's bounds.
  184. *
  185. * @return The x coordinate of this control's bounds.
  186. */
  187. float getX() const;
  188. /**
  189. * Get the y coordinate of this control's bounds.
  190. *
  191. * @return The y coordinate of this control's bounds.
  192. */
  193. float getY() const;
  194. /**
  195. * Get the width of this control's bounds.
  196. *
  197. * @return The width of this control's bounds.
  198. */
  199. float getWidth() const;
  200. /**
  201. * Get the height of this control's bounds.
  202. *
  203. * @return The height of this control's bounds.
  204. */
  205. float getHeight() const;
  206. /**
  207. * Set the alignment of this control within its parent container.
  208. *
  209. * @param alignment This control's alignment.
  210. */
  211. void setAlignment(Alignment alignment);
  212. /**
  213. * Get the alignment of this control within its parent container.
  214. *
  215. * @return The alignment of this control within its parent container.
  216. */
  217. Alignment getAlignment() const;
  218. /**
  219. * Set this control to fit horizontally within its parent container.
  220. *
  221. * @param autoWidth Whether to size this control to fit horizontally within its parent container.
  222. */
  223. virtual void setAutoWidth(bool autoWidth);
  224. /**
  225. * Get whether this control's width is set to automatically adjust to
  226. * fit horizontally within its parent container.
  227. *
  228. * @return Whether this control's width is set to automatically adjust.
  229. */
  230. bool getAutoWidth() const;
  231. /**
  232. * Set this control to fit vertically within its parent container.
  233. *
  234. * @param autoHeight Whether to size this control to fit vertically within its parent container.
  235. */
  236. virtual void setAutoHeight(bool autoHeight);
  237. /**
  238. * Get whether this control's height is set to automatically adjust to
  239. * fit vertically within its parent container.
  240. *
  241. * @return Whether this control's height is set to automatically adjust.
  242. */
  243. bool getAutoHeight() const;
  244. /**
  245. * Set the size of this control's border.
  246. *
  247. * @param top The height of the border's top side.
  248. * @param bottom The height of the border's bottom side.
  249. * @param left The width of the border's left side.
  250. * @param right The width of the border's right side.
  251. * @param states The states to set this property on.
  252. * One or more members of the Control::State enum, ORed together.
  253. */
  254. void setBorder(float top, float bottom, float left, float right, unsigned char states = STATE_ALL);
  255. /**
  256. * Get the measurements of this control's border for a given state.
  257. *
  258. * @return This control's border.
  259. */
  260. const Theme::Border& getBorder(State state = NORMAL) const;
  261. /**
  262. * Set the texture region of this control's skin.
  263. *
  264. * @param region The texture region, in pixels.
  265. * @param states The states to set this property on.
  266. * One or more members of the Control::State enum, ORed together.
  267. */
  268. void setSkinRegion(const Rectangle& region, unsigned char states = STATE_ALL);
  269. /**
  270. * Get the texture region of this control's skin for a given state.
  271. *
  272. * @param state The state to get this property from.
  273. *
  274. * @return The texture region of this control's skin.
  275. */
  276. const Rectangle& getSkinRegion(State state = NORMAL) const;
  277. /**
  278. * Get the texture coordinates of an area of this control's skin for a given state.
  279. *
  280. * @param area The area of the skin to get the coordinates of.
  281. * @param state The state to get this property from.
  282. *
  283. * @return The texture coordinates of an area of this control's skin.
  284. */
  285. const Theme::UVs& getSkinUVs(Theme::Skin::SkinArea area, State state = NORMAL) const;
  286. /**
  287. * Set the blend color of this control's skin.
  288. *
  289. * @param color The new blend color.
  290. * @param states The states to set this property on.
  291. * One or more members of the Control::State enum, ORed together.
  292. */
  293. void setSkinColor(const Vector4& color, unsigned char states = STATE_ALL);
  294. /**
  295. * Get the blend color of this control's skin for a given state.
  296. *
  297. * @param state The state to get this property from.
  298. *
  299. * @return The blend color of this control's skin.
  300. */
  301. const Vector4& getSkinColor(State state = NORMAL) const;
  302. /**
  303. * Set this control's margin.
  304. *
  305. * @param top Height of top margin.
  306. * @param bottom Height of bottom margin.
  307. * @param left Width of left margin.
  308. * @param right Width of right margin.
  309. */
  310. void setMargin(float top, float bottom, float left, float right);
  311. /**
  312. * Get this control's margin.
  313. *
  314. * @return This control's margin.
  315. */
  316. const Theme::Margin& getMargin() const;
  317. /**
  318. * Set this control's padding.
  319. *
  320. * @param top Height of top padding.
  321. * @param bottom Height of bottom padding.
  322. * @param left Width of left padding.
  323. * @param right Width of right padding.
  324. */
  325. void setPadding(float top, float bottom, float left, float right);
  326. /**
  327. * Get this control's padding.
  328. *
  329. * @return This control's padding.
  330. */
  331. const Theme::Padding& getPadding() const;
  332. /**
  333. * Set the texture region of an image used by this control.
  334. *
  335. * @param id The ID of the image to modify.
  336. * @param region The new texture region of the image.
  337. * @param states The states to set this property on.
  338. * One or more members of the Control::State enum, ORed together.
  339. */
  340. void setImageRegion(const char* id, const Rectangle& region, unsigned char states = STATE_ALL);
  341. /**
  342. * Get the texture region of an image used by this control for a given state.
  343. *
  344. * @param id The ID of the image.
  345. * @param state The state to get this property from.
  346. *
  347. * @return The texture region of the specified image.
  348. */
  349. const Rectangle& getImageRegion(const char* id, State state) const;
  350. /**
  351. * Set the blend color of an image used by this control.
  352. *
  353. * @param id The ID of the image to modify.
  354. * @param color The new blend color of the image.
  355. * @param states The states to set this property on.
  356. * One or more members of the Control::State enum, ORed together.
  357. */
  358. void setImageColor(const char* id, const Vector4& color, unsigned char states = STATE_ALL);
  359. /**
  360. * Get the blend color of an image used by this control for a given state.
  361. *
  362. * @param id The ID of the image.
  363. * @param state The state to get this property from.
  364. *
  365. * @return The blend color of the specified image.
  366. */
  367. const Vector4& getImageColor(const char* id, State state) const;
  368. /**
  369. * Get the texture coordinates of an image used by this control for a given state.
  370. *
  371. * @param id The ID of the image.
  372. * @param state The state to get this property from.
  373. *
  374. * @return The texture coordinates of the specified image.
  375. */
  376. const Theme::UVs& getImageUVs(const char* id, State state) const;
  377. /**
  378. * Set the texture region of this control's cursor.
  379. *
  380. * @param region The cursor region.
  381. * @param states The states to set this property on.
  382. * One or more members of the Control::State enum, ORed together.
  383. */
  384. void setCursorRegion(const Rectangle& region, unsigned char states);
  385. /**
  386. * Get the texture region of this control's cursor for a given state.
  387. *
  388. * @param state The state to get this property from.
  389. *
  390. * @return The texture region of this control's cursor.
  391. */
  392. const Rectangle& getCursorRegion(State state) const;
  393. /**
  394. * Set the blend color of this control's cursor.
  395. *
  396. * @param color The new blend color.
  397. * @param states The states to set this property on.
  398. * One or more members of the Control::State enum, ORed together.
  399. */
  400. void setCursorColor(const Vector4& color, unsigned char states);
  401. /**
  402. * Get the blend color of this control's cursor for a given state.
  403. *
  404. * @param state The state to get this property from.
  405. *
  406. * @return The blend color of this control's cursor.
  407. */
  408. const Vector4& getCursorColor(State state);
  409. /**
  410. * Get the texture coordinates of this control's cursor for a given state.
  411. *
  412. * @param state The state to get this property from.
  413. *
  414. * @return The texture coordinates of this control's cursor.
  415. */
  416. const Theme::UVs& getCursorUVs(State state);
  417. /**
  418. * Set the font used by this control.
  419. *
  420. * @param font The new font to use.
  421. * @param states The states to set this property on.
  422. * One or more members of the Control::State enum, ORed together.
  423. */
  424. void setFont(Font* font, unsigned char states = STATE_ALL);
  425. /**
  426. * Get the font used by this control for a given state.
  427. *
  428. * @param state The state to get this property from.
  429. *
  430. * @return the font used by this control.
  431. */
  432. Font* getFont(State state = NORMAL) const;
  433. /**
  434. * Set this control's font size.
  435. *
  436. * @param size The new font size.
  437. * @param states The states to set this property on.
  438. * One or more members of the Control::State enum, ORed together.
  439. */
  440. void setFontSize(unsigned int size, unsigned char states = STATE_ALL);
  441. /**
  442. * Get this control's font size for a given state.
  443. *
  444. * @param state The state to get this property from.
  445. *
  446. * @return This control's font size.
  447. */
  448. unsigned int getFontSize(State state = NORMAL) const;
  449. /**
  450. * Set this control's text color.
  451. *
  452. * @param color The new text color.
  453. * @param states The states to set this property on.
  454. * One or more members of the Control::State enum, ORed together.
  455. */
  456. void setTextColor(const Vector4& color, unsigned char states = STATE_ALL);
  457. /**
  458. * Get this control's text color for a given state.
  459. *
  460. * @param state The state to get this property from.
  461. *
  462. * @return This control's text color.
  463. */
  464. const Vector4& getTextColor(State state = NORMAL) const;
  465. /**
  466. * Set this control's text alignment.
  467. *
  468. * @param alignment The new text alignment.
  469. * @param states The states to set this property on.
  470. * One or more members of the Control::State enum, ORed together.
  471. */
  472. void setTextAlignment(Font::Justify alignment, unsigned char states = STATE_ALL);
  473. /**
  474. * Get this control's text alignment for a given state.
  475. *
  476. * @param state The state to get this property from.
  477. *
  478. * @return This control's text alignment for the given state.
  479. */
  480. Font::Justify getTextAlignment(State state = NORMAL) const;
  481. /**
  482. * Set whether text is drawn from right to left within this control.
  483. *
  484. * @param rightToLeft Whether text is drawn from right to left within this control.
  485. * @param states The states to set this property on.
  486. * One or more members of the Control::State enum, ORed together.
  487. */
  488. void setTextRightToLeft(bool rightToLeft, unsigned char states = STATE_ALL);
  489. /**
  490. * Get whether text is drawn from right to left within this control, for a given state.
  491. *
  492. * @param state The state to get this property from.
  493. *
  494. * @return Whether text is drawn from right to left within this control, for the given state.
  495. */
  496. bool getTextRightToLeft(State state = NORMAL) const;
  497. /**
  498. * Set the opacity of this control.
  499. *
  500. * @param opacity The new opacity.
  501. * @param states The states to set this property on.
  502. * One or more members of the Control::State enum, ORed together.
  503. */
  504. void setOpacity(float opacity, unsigned char states = STATE_ALL);
  505. /**
  506. * Get the opacity of this control for a given state.
  507. *
  508. * @param state The state to get this property from.
  509. *
  510. * @return The opacity of this control for a given state.
  511. */
  512. float getOpacity(State state = NORMAL) const;
  513. /**
  514. * Get the bounds of this control, relative to its parent container, after clipping.
  515. *
  516. * @return The bounds of this control.
  517. */
  518. const Rectangle& getClipBounds() const;
  519. /**
  520. * Get the content area of this control, in screen coordinates, after clipping.
  521. *
  522. * @return The clipping area of this control.
  523. */
  524. const Rectangle& getClip() const;
  525. /**
  526. * Change this control's state.
  527. *
  528. * @param state The state to switch this control to.
  529. */
  530. void setState(State state);
  531. /**
  532. * Get this control's current state.
  533. *
  534. * @return This control's current state.
  535. */
  536. State getState() const;
  537. /**
  538. * Disable this control.
  539. */
  540. void disable();
  541. /**
  542. * Enable this control.
  543. */
  544. void enable();
  545. /**
  546. * Get whether this control is currently enabled.
  547. *
  548. * @return Whether this control is currently enabled.
  549. */
  550. bool isEnabled();
  551. /**
  552. * Set whether this control consumes touch events,
  553. * preventing them from being passed to the game.
  554. *
  555. * @param consume Whether this control consumes touch events.
  556. */
  557. void setConsumeTouchEvents(bool consume);
  558. /**
  559. * Get whether this control consumes touch events.
  560. *
  561. * @return Whether this control consumes touch events.
  562. */
  563. bool getConsumeTouchEvents();
  564. /**
  565. * Set the style this control will use when rendering.
  566. *
  567. * @param style The style this control will use when rendering.
  568. */
  569. void setStyle(Theme::Style* style);
  570. /**
  571. * Get this control's style.
  572. *
  573. * @return This control's style.
  574. */
  575. Theme::Style* getStyle() const;
  576. /**
  577. * Add a listener to be notified of specific events affecting
  578. * this control. Event types can be OR'ed together.
  579. * E.g. To listen to touch-press and touch-release events,
  580. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  581. * as the second parameter.
  582. *
  583. * @param listener The listener to add.
  584. * @param eventFlags The events to listen for.
  585. */
  586. virtual void addListener(Control::Listener* listener, int eventFlags);
  587. /**
  588. * @see AnimationTarget#getAnimationPropertyComponentCount
  589. */
  590. unsigned int getAnimationPropertyComponentCount(int propertyId) const;
  591. /**
  592. * @see AnimationTarget#getAnimationProperty
  593. */
  594. void getAnimationPropertyValue(int propertyId, AnimationValue* value);
  595. /**
  596. * @see AnimationTarget#setAnimationProperty
  597. */
  598. void setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight = 1.0f);
  599. protected:
  600. /**
  601. * Constructor.
  602. */
  603. Control();
  604. /**
  605. * Destructor.
  606. */
  607. virtual ~Control();
  608. /**
  609. * Get the overlay type corresponding to this control's current state.
  610. *
  611. * @return The overlay type corresponding to this control's current state.
  612. */
  613. Theme::Style::OverlayType getOverlayType() const;
  614. /**
  615. * Touch callback on touch events. Controls return true if they consume the touch event.
  616. *
  617. * @param evt The touch event that occurred.
  618. * @param x The x position of the touch in pixels. Left edge is zero.
  619. * @param y The y position of the touch in pixels. Top edge is zero.
  620. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  621. *
  622. * @return Whether the touch event was consumed by this control.
  623. *
  624. * @see Touch::TouchEvent
  625. */
  626. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  627. /**
  628. * Keyboard callback on key events.
  629. *
  630. * @param evt The key event that occured.
  631. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  632. * If evt is KEY_CHAR then key is the unicode value of the character.
  633. *
  634. * @see Keyboard::KeyEvent
  635. * @see Keyboard::Key
  636. */
  637. virtual void keyEvent(Keyboard::KeyEvent evt, int key);
  638. /**
  639. * Called when a control's properties change. Updates this control's internal rendering
  640. * properties, such as its text viewport.
  641. *
  642. * @param clip The clipping rectangle of this control's parent container.
  643. * @param offset Layout-computed positioning offset to add to the control's position.
  644. */
  645. virtual void update(const Rectangle& clip, const Vector2& offset);
  646. /**
  647. * Draw the images associated with this control.
  648. *
  649. * @param spriteBatch The sprite batch containing this control's icons.
  650. * @param clip The clipping rectangle of this control's parent container.
  651. * @param offset Layout-computed positioning offset to add to the control's position.
  652. */
  653. virtual void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  654. /**
  655. * Draw this control's text.
  656. *
  657. * @param clip The clipping rectangle of this control's parent container.
  658. * @param offset Layout-computed positioning offset to add to the control's position.
  659. */
  660. virtual void drawText(const Rectangle& clip);
  661. /**
  662. * Initialize properties common to STATE_ALL Controls.
  663. */
  664. virtual void initialize(Theme::Style* style, Properties* properties);
  665. /**
  666. * Container and classes that extend it should implement this and return true.
  667. *
  668. * @return true if this object is of class Container, false otherwise.
  669. */
  670. virtual bool isContainer();
  671. /**
  672. * Returns whether this control has been modified and requires an update.
  673. *
  674. * @return Whether this control has been modified and requires an update.
  675. */
  676. virtual bool isDirty();
  677. /**
  678. * Get a Control::State enum from a matching string.
  679. *
  680. * @param state The string to match.
  681. *
  682. * @return The Control::State enum that matches the given string.
  683. */
  684. static State getState(const char* state);
  685. /**
  686. * Get a Theme::ThemeImage from its ID, for a given state.
  687. *
  688. * @param id The ID of the image to retrieve.
  689. * @param state The state to get this image from.
  690. *
  691. * @return The requested Theme::ThemeImage, or NULL if none was found.
  692. */
  693. Theme::ThemeImage* getImage(const char* id, State state);
  694. /**
  695. * Notify this control's listeners of a specific event.
  696. *
  697. * @param eventType The event to trigger.
  698. */
  699. void notifyListeners(Listener::EventType eventType);
  700. /**
  701. * Gets the Alignment by string.
  702. *
  703. * @param alignment The string representation of the Alignment type.
  704. * @return The Alignment enum value corresponding to the given string.
  705. */
  706. static Alignment getAlignment(const char* alignment);
  707. /**
  708. * The Control's ID.
  709. */
  710. std::string _id;
  711. /**
  712. * Determines overlay used during draw().
  713. */
  714. State _state;
  715. /**
  716. * Position, relative to parent container's clipping window, and desired size.
  717. */
  718. Rectangle _bounds;
  719. /**
  720. * Position, relative to parent container's clipping window, including border and padding, after clipping.
  721. */
  722. Rectangle _clipBounds;
  723. /**
  724. * Absolute bounds, including border and padding, before clipping.
  725. */
  726. Rectangle _absoluteBounds;
  727. /**
  728. * Absolute bounds, including border and padding, after clipping.
  729. */
  730. Rectangle _absoluteClipBounds;
  731. /**
  732. * Absolute bounds of content area (i.e. without border and padding), before clipping.
  733. */
  734. Rectangle _viewportBounds;
  735. /**
  736. * Absolute bounds of content area (i.e. without border and padding), after clipping.
  737. */
  738. Rectangle _viewportClipBounds;
  739. bool _dirty;
  740. /**
  741. * Flag for whether the Control consume's touch events.
  742. */
  743. bool _consumeTouchEvents;
  744. /**
  745. * The Control's Alignmnet
  746. */
  747. Alignment _alignment;
  748. /**
  749. * Whether the Control's width is auto-sized.
  750. */
  751. bool _autoWidth;
  752. /**
  753. * Whether the Control's height is auto-sized.
  754. */
  755. bool _autoHeight;
  756. /**
  757. * The Control's Theme::Style.
  758. */
  759. Theme::Style* _style;
  760. /**
  761. * Listeners map of EventType's to a list of Listeners.
  762. */
  763. std::map<Listener::EventType, std::list<Listener*>*>* _listeners;
  764. /**
  765. * The current opacity of the control.
  766. */
  767. float _opacity;
  768. private:
  769. /*
  770. * Constructor.
  771. */
  772. Control(const Control& copy);
  773. Theme::Style::Overlay** getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays);
  774. Theme::Style::Overlay* getOverlay(Control::State state) const;
  775. void overrideStyle();
  776. void overrideThemedProperties(Properties* properties, unsigned char states);
  777. void setImageList(Theme::ImageList* imageList, unsigned char states = STATE_ALL);
  778. void setCursor(Theme::ThemeImage* cursor, unsigned char states = STATE_ALL);
  779. void setSkin(Theme::Skin* skin, unsigned char states = STATE_ALL);
  780. Theme::Skin* getSkin(State state);
  781. void addSpecificListener(Control::Listener* listener, Listener::EventType eventType);
  782. /**
  783. * Draws the themed border and background of a control.
  784. *
  785. * @param spriteBatch The sprite batch containing this control's border images.
  786. * @param clip The clipping rectangle of this control's parent container.
  787. */
  788. virtual void drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip);
  789. virtual void draw(SpriteBatch* spriteBatch, const Rectangle& clip, bool needsClear, float targetHeight);
  790. bool _styleOverridden;
  791. Theme::Skin* _skin;
  792. Rectangle _clearBounds; // Previous frame's absolute clip bounds, to be cleared if necessary.
  793. };
  794. }
  795. #endif