Control.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. public:
  24. /**
  25. * The possible states a control can be in.
  26. */
  27. enum State
  28. {
  29. /**
  30. * State of an enabled but inactive control.
  31. */
  32. NORMAL = 0x01,
  33. /**
  34. * State of a control currently in focus.
  35. */
  36. FOCUS = 0x02,
  37. /**
  38. * State of a control that is currently being acted on,
  39. * e.g. through touch or mouse-click events.
  40. */
  41. ACTIVE = 0x04,
  42. /**
  43. * State of a control that has been disabled.
  44. */
  45. DISABLED = 0x08,
  46. };
  47. /**
  48. * A constant used for setting themed attributes on all control states simultaneously.
  49. */
  50. static const unsigned char STATE_ALL = NORMAL | FOCUS | ACTIVE | DISABLED;
  51. /**
  52. * Implement Control::Listener and call Control::addListener()
  53. * in order to listen for events on controls.
  54. */
  55. class Listener
  56. {
  57. public:
  58. enum EventType
  59. {
  60. /**
  61. * Mouse-down or touch-press event.
  62. */
  63. PRESS = 0x01,
  64. /**
  65. * Mouse-up or touch-release event.
  66. */
  67. RELEASE = 0x02,
  68. /**
  69. * Event triggered after consecutive PRESS and RELEASE events take place
  70. * within the bounds of a control.
  71. */
  72. CLICK = 0x04,
  73. /**
  74. * Event triggered when the value of a slider, check box, or radio button
  75. * changes.
  76. */
  77. VALUE_CHANGED = 0x08,
  78. /**
  79. * Event triggered when the contents of a text box are modified.
  80. */
  81. TEXT_CHANGED = 0x10
  82. };
  83. /**
  84. * Method called by controls when an event is triggered.
  85. *
  86. * @param control The control triggering the event.
  87. * @param evt The event triggered.
  88. */
  89. virtual void controlEvent(Control* control, EventType evt) = 0;
  90. };
  91. /**
  92. * Position animation property. Data = x, y
  93. */
  94. static const int ANIMATE_POSITION = 1;
  95. /**
  96. * Position x animation property. Data = x
  97. */
  98. static const int ANIMATE_POSITION_X = 2;
  99. /**
  100. * Position y animation property. Data = y
  101. */
  102. static const int ANIMATE_POSITION_Y = 3;
  103. /**
  104. * Size animation property. Data = width, height
  105. */
  106. static const int ANIMATE_SIZE = 4;
  107. /**
  108. * Size width animation property. Data = width
  109. */
  110. static const int ANIMATE_SIZE_WIDTH = 5;
  111. /**
  112. * Size height animation property. Data = height
  113. */
  114. static const int ANIMATE_SIZE_HEIGHT = 6;
  115. /**
  116. * Opacity property. Data = opacity
  117. */
  118. static const int ANIMATE_OPACITY = 7;
  119. /**
  120. * Get this control's ID string.
  121. *
  122. * @return This control's ID.
  123. */
  124. const char* getID() const;
  125. /**
  126. * Set the position of this control relative to its parent container.
  127. *
  128. * @param x The x coordinate.
  129. * @param y The y coordinate.
  130. */
  131. void setPosition(float x, float y);
  132. /**
  133. * Set the desired size of this control, including its border and padding, before clipping.
  134. *
  135. * @param width The width.
  136. * @param height The height.
  137. */
  138. void setSize(float width, float height);
  139. /**
  140. * Set the bounds of this control, relative to its parent container and including its
  141. * border and padding, before clipping.
  142. *
  143. * @param bounds The new bounds to set.
  144. */
  145. void setBounds(const Rectangle& bounds);
  146. /**
  147. * Get the bounds of this control, relative to its parent container and including its
  148. * border and padding, before clipping.
  149. *
  150. * @return The bounds of this control.
  151. */
  152. const Rectangle& getBounds() const;
  153. /**
  154. * Get the x coordinate of this control's bounds.
  155. *
  156. * @return The x coordinate of this control's bounds.
  157. */
  158. float getX() const;
  159. /**
  160. * Get the y coordinate of this control's bounds.
  161. *
  162. * @return The y coordinate of this control's bounds.
  163. */
  164. float getY() const;
  165. /**
  166. * Get the width of this control's bounds.
  167. *
  168. * @return The width of this control's bounds.
  169. */
  170. float getWidth() const;
  171. /**
  172. * Get the height of this control's bounds.
  173. *
  174. * @return The height of this control's bounds.
  175. */
  176. float getHeight() const;
  177. /**
  178. * Set the size of this control's border.
  179. *
  180. * @param top The height of the border's top side.
  181. * @param bottom The height of the border's bottom side.
  182. * @param left The width of the border's left side.
  183. * @param right The width of the border's right side.
  184. */
  185. void setBorder(float top, float bottom, float left, float right, unsigned char states = STATE_ALL);
  186. /**
  187. * Get the measurements of this control's border for a given state.
  188. *
  189. * @return This control's border.
  190. */
  191. const Theme::Border& getBorder(State state = NORMAL) const;
  192. /**
  193. * Set the texture region of this control's skin.
  194. *
  195. * @param region The texture region, in pixels.
  196. * @param states The states to set this property on.
  197. * One or more members of the Control::State enum, ORed together.
  198. */
  199. void setSkinRegion(const Rectangle& region, unsigned char states = STATE_ALL);
  200. /**
  201. * Get the texture region of this control's skin for a given state.
  202. *
  203. * @param state The state to get this property from.
  204. *
  205. * @return The texture region of this control's skin.
  206. */
  207. const Rectangle& getSkinRegion(State state = NORMAL) const;
  208. /**
  209. * Get the texture coordinates of an area of this control's skin for a given state.
  210. *
  211. * @param area The area of the skin to get the coordinates of.
  212. * @param state The state to get this property from.
  213. *
  214. * @return The texture coordinates of an area of this control's skin.
  215. */
  216. const Theme::UVs& getSkinUVs(Theme::Skin::SkinArea area, State state = NORMAL) const;
  217. /**
  218. * Set the blend color of this control's skin.
  219. *
  220. * @param color The new blend color.
  221. * @param states The states to set this property on.
  222. * One or more members of the Control::State enum, ORed together.
  223. */
  224. void setSkinColor(const Vector4& color, unsigned char states = STATE_ALL);
  225. /**
  226. * Get the blend color of this control's skin for a given state.
  227. *
  228. * @param state The state to get this property from.
  229. *
  230. * @return The blend color of this control's skin.
  231. */
  232. const Vector4& getSkinColor(State state = NORMAL) const;
  233. /**
  234. * Set this control's margin.
  235. *
  236. * @param top Height of top margin.
  237. * @param bottom Height of bottom margin.
  238. * @param left Width of left margin.
  239. * @param right Width of right margin.
  240. */
  241. void setMargin(float top, float bottom, float left, float right);
  242. /**
  243. * Get this control's margin.
  244. *
  245. * @return This control's margin.
  246. */
  247. const Theme::Margin& getMargin() const;
  248. /**
  249. * Set this control's padding.
  250. *
  251. * @param top Height of top padding.
  252. * @param bottom Height of bottom padding.
  253. * @param left Width of left padding.
  254. * @param right Width of right padding.
  255. */
  256. void setPadding(float top, float bottom, float left, float right);
  257. /**
  258. * Get this control's padding.
  259. *
  260. * @return This control's padding.
  261. */
  262. const Theme::Padding& getPadding() const;
  263. /**
  264. * Set the texture region of an image used by this control.
  265. *
  266. * @param id The ID of the image to modify.
  267. * @param region The new texture region of the image.
  268. * @param states The states to set this property on.
  269. * One or more members of the Control::State enum, ORed together.
  270. */
  271. void setImageRegion(const char* id, const Rectangle& region, unsigned char states = STATE_ALL);
  272. /**
  273. * Get the texture region of an image used by this control for a given state.
  274. *
  275. * @param id The ID of the image.
  276. * @param state The state to get this property from.
  277. *
  278. * @return The texture region of the specified image.
  279. */
  280. const Rectangle& getImageRegion(const char* id, State state) const;
  281. /**
  282. * Set the blend color of an image used by this control.
  283. *
  284. * @param id The ID of the image to modify.
  285. * @param color The new blend color of the image.
  286. * @param states The states to set this property on.
  287. * One or more members of the Control::State enum, ORed together.
  288. */
  289. void setImageColor(const char* id, const Vector4& color, unsigned char states = STATE_ALL);
  290. /**
  291. * Get the blend color of an image used by this control for a given state.
  292. *
  293. * @param id The ID of the image.
  294. * @param state The state to get this property from.
  295. *
  296. * @return The blend color of the specified image.
  297. */
  298. const Vector4& getImageColor(const char* id, State state) const;
  299. /**
  300. * Get the texture coordinates of an image used by this control for a given state.
  301. *
  302. * @param id The ID of the image.
  303. * @param state The state to get this property from.
  304. *
  305. * @return The texture coordinates of the specified image.
  306. */
  307. const Theme::UVs& getImageUVs(const char* id, State state) const;
  308. /**
  309. * Set the texture region of this control's cursor.
  310. *
  311. * @param states The states to set this property on.
  312. * One or more members of the Control::State enum, ORed together.
  313. */
  314. void setCursorRegion(const Rectangle& region, unsigned char states);
  315. /**
  316. * Get the texture region of this control's cursor for a given state.
  317. *
  318. * @param state The state to get this property from.
  319. *
  320. * @return The texture region of this control's cursor.
  321. */
  322. const Rectangle& getCursorRegion(State state) const;
  323. /**
  324. * Set the blend color of this control's cursor.
  325. *
  326. * @param color The new blend color.
  327. * @param states The states to set this property on.
  328. * One or more members of the Control::State enum, ORed together.
  329. */
  330. void setCursorColor(const Vector4& color, unsigned char states);
  331. /**
  332. * Get the blend color of this control's cursor for a given state.
  333. *
  334. * @param state The state to get this property from.
  335. *
  336. * @return The blend color of this control's cursor.
  337. */
  338. const Vector4& getCursorColor(State state);
  339. /**
  340. * Get the texture coordinates of this control's cursor for a given state.
  341. *
  342. * @param state The state to get this property from.
  343. *
  344. * @return The texture coordinates of this control's cursor.
  345. */
  346. const Theme::UVs& getCursorUVs(State state);
  347. /**
  348. * Set the font used by this control.
  349. *
  350. * @param font The new font to use.
  351. * @param states The states to set this property on.
  352. * One or more members of the Control::State enum, ORed together.
  353. */
  354. void setFont(Font* font, unsigned char states = STATE_ALL);
  355. /**
  356. * Get the font used by this control for a given state.
  357. *
  358. * @param state The state to get this property from.
  359. *
  360. * @return the font used by this control.
  361. */
  362. Font* getFont(State state = NORMAL) const;
  363. /**
  364. * Set this control's font size.
  365. *
  366. * @param size The new font size.
  367. * @param states The states to set this property on.
  368. * One or more members of the Control::State enum, ORed together.
  369. */
  370. void setFontSize(unsigned int size, unsigned char states = STATE_ALL);
  371. /**
  372. * Get this control's font size for a given state.
  373. *
  374. * @param state The state to get this property from.
  375. *
  376. * @return This control's font size.
  377. */
  378. unsigned int getFontSize(State state = NORMAL) const;
  379. /**
  380. * Set this control's text color.
  381. *
  382. * @param color The new text color.
  383. * @param states The states to set this property on.
  384. * One or more members of the Control::State enum, ORed together.
  385. */
  386. void setTextColor(const Vector4& color, unsigned char states = STATE_ALL);
  387. /**
  388. * Get this control's text color for a given state.
  389. *
  390. * @param state The state to get this property from.
  391. *
  392. * @return This control's text color.
  393. */
  394. const Vector4& getTextColor(State state = NORMAL) const;
  395. /**
  396. * Set this control's text alignment.
  397. *
  398. * @param alignment The new text alignment.
  399. * @param states The states to set this property on.
  400. * One or more members of the Control::State enum, ORed together.
  401. */
  402. void setTextAlignment(Font::Justify alignment, unsigned char states = STATE_ALL);
  403. /**
  404. * Get this control's text alignment for a given state.
  405. *
  406. * @param state The state to get this property from.
  407. *
  408. * @return This control's text alignment for the given state.
  409. */
  410. Font::Justify getTextAlignment(State state = NORMAL) const;
  411. /**
  412. * Set whether text is drawn from right to left within this control.
  413. *
  414. * @param rightToLeft Whether text is drawn from right to left within this control.
  415. * @param states The states to set this property on.
  416. * One or more members of the Control::State enum, ORed together.
  417. */
  418. void setTextRightToLeft(bool rightToLeft, unsigned char states = STATE_ALL);
  419. /**
  420. * Get whether text is drawn from right to left within this control, for a given state.
  421. *
  422. * @param state The state to get this property from.
  423. *
  424. * @return Whether text is drawn from right to left within this control, for the given state.
  425. */
  426. bool getTextRightToLeft(State state = NORMAL) const;
  427. /**
  428. * Set the opacity of this control.
  429. *
  430. * @param opacity The new opacity.
  431. * @param states The states to set this property on.
  432. * One or more members of the Control::State enum, ORed together.
  433. */
  434. void setOpacity(float opacity, unsigned char states = STATE_ALL);
  435. /**
  436. * Get the opacity of this control for a given state.
  437. *
  438. * @param state The state to get this property from.
  439. *
  440. * @return The opacity of this control for a given state.
  441. */
  442. float getOpacity(State state = NORMAL) const;
  443. /**
  444. * Get the bounds of this control, relative to its parent container, after clipping.
  445. *
  446. * @return The bounds of this control.
  447. */
  448. const Rectangle& getClipBounds() const;
  449. /**
  450. * Get the content area of this control, in screen coordinates, after clipping.
  451. *
  452. * @return The clipping area of this control.
  453. */
  454. const Rectangle& getClip() const;
  455. /**
  456. * Change this control's state.
  457. *
  458. * @param state The state to switch this control to.
  459. */
  460. void setState(State state);
  461. /**
  462. * Get this control's current state.
  463. *
  464. * @return This control's current state.
  465. */
  466. State getState() const;
  467. /**
  468. * Disable this control.
  469. */
  470. void disable();
  471. /**
  472. * Enable this control.
  473. */
  474. void enable();
  475. /**
  476. * Get whether this control is currently enabled.
  477. *
  478. * @return Whether this control is currently enabled.
  479. */
  480. bool isEnabled();
  481. /**
  482. * Set whether this control consumes touch events,
  483. * preventing them from being passed to the game.
  484. *
  485. * @param consume Whether this control consumes touch events.
  486. */
  487. void setConsumeTouchEvents(bool consume);
  488. /**
  489. * Get whether this control consumes touch events.
  490. *
  491. * @return Whether this control consumes touch events.
  492. */
  493. bool getConsumeTouchEvents();
  494. /**
  495. * Set the style this control will use when rendering.
  496. *
  497. * @param style The style this control will use when rendering.
  498. */
  499. void setStyle(Theme::Style* style);
  500. /**
  501. * Get this control's style.
  502. *
  503. * @return This control's style.
  504. */
  505. Theme::Style* getStyle() const;
  506. /**
  507. * Add a listener to be notified of specific events affecting
  508. * this control. Event types can be OR'ed together.
  509. * E.g. To listen to touch-press and touch-release events,
  510. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  511. * as the second parameter.
  512. *
  513. * @param listener The listener to add.
  514. * @param eventFlags The events to listen for.
  515. */
  516. virtual void addListener(Control::Listener* listener, int eventFlags);
  517. /**
  518. * @see AnimationTarget#getAnimationPropertyComponentCount
  519. */
  520. unsigned int getAnimationPropertyComponentCount(int propertyId) const;
  521. /**
  522. * @see AnimationTarget#getAnimationProperty
  523. */
  524. void getAnimationPropertyValue(int propertyId, AnimationValue* value);
  525. /**
  526. * @see AnimationTarget#setAnimationProperty
  527. */
  528. void setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight = 1.0f);
  529. protected:
  530. /**
  531. * Constructor.
  532. */
  533. Control();
  534. /**
  535. * Destructor.
  536. */
  537. virtual ~Control();
  538. /**
  539. * Get the overlay type corresponding to this control's current state.
  540. *
  541. * @return The overlay type corresponding to this control's current state.
  542. */
  543. Theme::Style::OverlayType getOverlayType() const;
  544. /**
  545. * Touch callback on touch events. Controls return true if they consume the touch event.
  546. *
  547. * @param evt The touch event that occurred.
  548. * @param x The x position of the touch in pixels. Left edge is zero.
  549. * @param y The y position of the touch in pixels. Top edge is zero.
  550. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  551. *
  552. * @return Whether the touch event was consumed by this control.
  553. *
  554. * @see Touch::TouchEvent
  555. */
  556. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  557. /**
  558. * Keyboard callback on key events.
  559. *
  560. * @param evt The key event that occured.
  561. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  562. * If evt is KEY_CHAR then key is the unicode value of the character.
  563. *
  564. * @see Keyboard::KeyEvent
  565. * @see Keyboard::Key
  566. */
  567. virtual void keyEvent(Keyboard::KeyEvent evt, int key);
  568. /**
  569. * Called when a control's properties change. Updates this control's internal rendering
  570. * properties, such as its text viewport.
  571. *
  572. * @param clip The clipping rectangle of this control's parent container.
  573. */
  574. virtual void update(const Rectangle& clip);
  575. /**
  576. * Draw the images associated with this control.
  577. *
  578. * @param spriteBatch The sprite batch containing this control's icons.
  579. * @param clip The clipping rectangle of this control's parent container.
  580. */
  581. virtual void drawImages(SpriteBatch* spriteBatch, const Rectangle& clip);
  582. /**
  583. * Draw this control's text.
  584. *
  585. * @param clip The clipping rectangle of this control's parent container.
  586. */
  587. virtual void drawText(const Rectangle& clip);
  588. /**
  589. * Initialize properties common to STATE_ALL Controls.
  590. */
  591. virtual void initialize(Theme::Style* style, Properties* properties);
  592. /**
  593. * Container and classes that extend it should implement this and return true.
  594. *
  595. * @return true if this object is of class Container, false otherwise.
  596. */
  597. virtual bool isContainer();
  598. /**
  599. * Returns whether this control has been modified and requires an update.
  600. *
  601. * @return Whether this control has been modified and requires an update.
  602. */
  603. virtual bool isDirty();
  604. /**
  605. * Get a Control::State enum from a matching string.
  606. *
  607. * @param state The string to match.
  608. *
  609. * @return The Control::State enum that matches the given string.
  610. */
  611. static State getState(const char* state);
  612. /**
  613. * Notify this control's listeners of a specific event.
  614. *
  615. * @param eventType The event to trigger.
  616. */
  617. void notifyListeners(Listener::EventType eventType);
  618. std::string _id;
  619. State _state; // Determines overlay used during draw().
  620. Rectangle _bounds; // Position, relative to parent container's clipping window, and desired size.
  621. Rectangle _clipBounds; // The position and size of this control, relative to parent container's bounds, including border and padding, after clipping.
  622. Rectangle _textBounds; // The position and size of this control's text area, before clipping. Used for text alignment.
  623. Rectangle _clip; // Clipping window of this control's content, after clipping.
  624. bool _dirty;
  625. bool _consumeTouchEvents;
  626. Theme::Style* _style;
  627. std::map<Listener::EventType, std::list<Listener*>*>* _listeners;
  628. private:
  629. static const char ANIMATION_POSITION_X_BIT = 0x01;
  630. static const char ANIMATION_POSITION_Y_BIT = 0x02;
  631. static const char ANIMATION_SIZE_WIDTH_BIT = 0x04;
  632. static const char ANIMATION_SIZE_HEIGHT_BIT = 0x08;
  633. static const char ANIMATION_OPACITY_BIT = 0x10;
  634. /*
  635. * Constructor.
  636. */
  637. Control(const Control& copy);
  638. void applyAnimationValuePositionX(float x, float blendWeight);
  639. void applyAnimationValuePositionY(float y, float blendWeight);
  640. void applyAnimationValueSizeWidth(float width, float blendWeight);
  641. void applyAnimationValueSizeHeight(float height, float blendWeight);
  642. void applyAnimationValueOpacity();
  643. Theme::Style::Overlay** getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays);
  644. Theme::Style::Overlay* getOverlay(Control::State state) const;
  645. void overrideStyle();
  646. void addSpecificListener(Control::Listener* listener, Listener::EventType eventType);
  647. /**
  648. * Draws the themed border and background of a control.
  649. *
  650. * @param spriteBatch The sprite batch containing this control's border images.
  651. * @param clip The clipping rectangle of this control's parent container.
  652. */
  653. virtual void drawBorder(SpriteBatch* spriteBatch, const Rectangle& clip);
  654. bool _styleOverridden;
  655. };
  656. }
  657. #endif