Control.h 33 KB

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