Control.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392
  1. #ifndef CONTROL_H_
  2. #define CONTROL_H_
  3. #include "Ref.h"
  4. #include "Rectangle.h"
  5. #include "Vector2.h"
  6. #include "Theme.h"
  7. #include "ThemeStyle.h"
  8. #include "Touch.h"
  9. #include "Keyboard.h"
  10. #include "Mouse.h"
  11. #include "ScriptTarget.h"
  12. #include "Gamepad.h"
  13. namespace gameplay
  14. {
  15. class Container;
  16. class Form;
  17. /**
  18. * Defines the base class for all controls.
  19. *
  20. * @see http://blackberry.github.io/GamePlay/docs/file-formats.html#wiki-UI_Forms
  21. */
  22. class Control : public Ref, public AnimationTarget, public ScriptTarget
  23. {
  24. friend class Form;
  25. friend class Container;
  26. friend class Layout;
  27. friend class AbsoluteLayout;
  28. friend class VerticalLayout;
  29. friend class FlowLayout;
  30. public:
  31. /**
  32. * The possible states a control can be in.
  33. */
  34. enum State
  35. {
  36. /**
  37. * State of an enabled but inactive control.
  38. */
  39. NORMAL = 0x01,
  40. /**
  41. * State of a control when it is currently in focus.
  42. */
  43. FOCUS = 0x02,
  44. /**
  45. * State of a control that is currently being acted on,
  46. * e.g. through touch or mouse-click events.
  47. */
  48. ACTIVE = 0x04,
  49. /**
  50. * State of a control that has been disabled.
  51. */
  52. DISABLED = 0x08,
  53. /**
  54. * When a mouse is in use, the state of a control the cursor is over.
  55. */
  56. HOVER = 0x10,
  57. };
  58. /**
  59. * Defines the set of alignments for positioning controls and justifying text.
  60. */
  61. enum Alignment
  62. {
  63. // Specify horizontal alignment, use default vertical alignment (ALIGN_TOP).
  64. ALIGN_LEFT = 0x01,
  65. ALIGN_HCENTER = 0x02,
  66. ALIGN_RIGHT = 0x04,
  67. // Specify vertical alignment, use default horizontal alignment (ALIGN_LEFT).
  68. ALIGN_TOP = 0x10,
  69. ALIGN_VCENTER = 0x20,
  70. ALIGN_BOTTOM = 0x40,
  71. // Specify both vertical and horizontal alignment.
  72. ALIGN_TOP_LEFT = ALIGN_TOP | ALIGN_LEFT,
  73. ALIGN_VCENTER_LEFT = ALIGN_VCENTER | ALIGN_LEFT,
  74. ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT,
  75. ALIGN_TOP_HCENTER = ALIGN_TOP | ALIGN_HCENTER,
  76. ALIGN_VCENTER_HCENTER = ALIGN_VCENTER | ALIGN_HCENTER,
  77. ALIGN_BOTTOM_HCENTER = ALIGN_BOTTOM | ALIGN_HCENTER,
  78. ALIGN_TOP_RIGHT = ALIGN_TOP | ALIGN_RIGHT,
  79. ALIGN_VCENTER_RIGHT = ALIGN_VCENTER | ALIGN_RIGHT,
  80. ALIGN_BOTTOM_RIGHT = ALIGN_BOTTOM | ALIGN_RIGHT
  81. };
  82. /**
  83. * Defines supported auto sizing modes for controls.
  84. */
  85. enum AutoSize
  86. {
  87. /**
  88. * No auto sizing is applied.
  89. */
  90. AUTO_SIZE_NONE = 0x00,
  91. /**
  92. * The control's size is stretched to fill the content area of its parent container.
  93. */
  94. AUTO_SIZE_STRETCH = 0x01,
  95. /**
  96. * The control's size is set to tightly fit its contents.
  97. *
  98. * Not all controls support this auto sizing mode.
  99. */
  100. AUTO_SIZE_FIT = 0x02
  101. };
  102. /**
  103. * Implement Control::Listener and call Control::addListener()
  104. * in order to listen for events on controls.
  105. */
  106. class Listener
  107. {
  108. public:
  109. /**
  110. * Defines the Listener's event types.
  111. */
  112. enum EventType
  113. {
  114. /**
  115. * Mouse-down or touch-press event.
  116. */
  117. PRESS = 0x01,
  118. /**
  119. * Mouse-up or touch-release event.
  120. */
  121. RELEASE = 0x02,
  122. /**
  123. * Event triggered after consecutive PRESS and RELEASE events take place
  124. * within the bounds of a control.
  125. */
  126. CLICK = 0x04,
  127. /**
  128. * Event triggered when the value of a slider, check box, or radio button
  129. * changes.
  130. */
  131. VALUE_CHANGED = 0x08,
  132. /**
  133. * Event triggered when the contents of a text box are modified.
  134. */
  135. TEXT_CHANGED = 0x10,
  136. /**
  137. * Event triggered when a control is clicked with the middle mouse button.
  138. */
  139. MIDDLE_CLICK = 0x20,
  140. /**
  141. * Event triggered when a control is clicked with the right mouse button.
  142. */
  143. RIGHT_CLICK = 0x40,
  144. /**
  145. * Event triggered when a mouse cursor enters a control.
  146. */
  147. ENTER = 0x80,
  148. /**
  149. * Event triggered when a mouse cursor leaves a control.
  150. */
  151. LEAVE = 0x100,
  152. /**
  153. * Event triggered when a control gains focus.
  154. */
  155. FOCUS_GAINED = 0x200,
  156. /**
  157. * Event triggered when a control loses focus.
  158. */
  159. FOCUS_LOST = 0x400
  160. };
  161. /*
  162. * Destructor.
  163. */
  164. virtual ~Listener() { }
  165. /**
  166. * Method called by controls when an event is triggered.
  167. *
  168. * @param control The control triggering the event.
  169. * @param evt The event triggered.
  170. */
  171. virtual void controlEvent(Control* control, EventType evt) = 0;
  172. };
  173. /**
  174. * @script{ignore}
  175. * A constant used for setting themed attributes on all control states simultaneously.
  176. */
  177. static const unsigned char STATE_ALL = NORMAL | ACTIVE | FOCUS | DISABLED | HOVER;
  178. /**
  179. * Position animation property. Data = x, y
  180. */
  181. static const int ANIMATE_POSITION = 1;
  182. /**
  183. * Position x animation property. Data = x
  184. */
  185. static const int ANIMATE_POSITION_X = 2;
  186. /**
  187. * Position y animation property. Data = y
  188. */
  189. static const int ANIMATE_POSITION_Y = 3;
  190. /**
  191. * Size animation property. Data = width, height
  192. */
  193. static const int ANIMATE_SIZE = 4;
  194. /**
  195. * Size width animation property. Data = width
  196. */
  197. static const int ANIMATE_SIZE_WIDTH = 5;
  198. /**
  199. * Size height animation property. Data = height
  200. */
  201. static const int ANIMATE_SIZE_HEIGHT = 6;
  202. /**
  203. * Opacity property. Data = opacity
  204. */
  205. static const int ANIMATE_OPACITY = 7;
  206. /**
  207. * Get this control's ID string.
  208. *
  209. * @return This control's ID.
  210. */
  211. const char* getId() const;
  212. /**
  213. * Sets this control's ID string.
  214. *
  215. * @param id The new control ID.
  216. */
  217. void setId(const char* id);
  218. /**
  219. * Get the x coordinate of this control.
  220. *
  221. * @return The x coordinate of this control.
  222. */
  223. float getX() const;
  224. /**
  225. * Sets the X coordinate for the control.
  226. *
  227. * If the value is passed as a percentage of its parent container's clip region, it is interpreted as a value
  228. * between 0-1, where 1 equals the full size of it's parent.
  229. *
  230. * @param x The new X coordinate.
  231. * @param percentage True if the value should be interpreted as a percentage (0-1), false if it is regular number.
  232. */
  233. void setX(float x, bool percentage = false);
  234. /**
  235. * Determines if the X coordinate of this control computed as a percentage of its parent container.
  236. *
  237. * @return True if the X value is computed as a percentage of its parent container.
  238. */
  239. bool isXPercentage() const;
  240. /**
  241. * Get the y coordinate of this control.
  242. *
  243. * @return The y coordinate of this control.
  244. */
  245. float getY() const;
  246. /**
  247. * Sets the Y coordinate for the control.
  248. *
  249. * If the value is passed as a percentage of its parent container's clip region, it is interpreted as a value
  250. * between 0-1, where 1 equals the full size of it's parent.
  251. *
  252. * @param y The new Y coordinate.
  253. * @param percentage True if the value should be interpreted as a percentage (0-1), false if it is regular number.
  254. */
  255. void setY(float y, bool percentage = false);
  256. /**
  257. * Determines if the Y coordinate of this control is computed as a percentage of its parent container.
  258. *
  259. * @return True if the Y value is computed as a percentage of its parent container.
  260. */
  261. bool isYPercentage() const;
  262. /**
  263. * Get the width of this control.
  264. *
  265. * @return The width of this control.
  266. */
  267. float getWidth() const;
  268. /**
  269. * Set the desired width of the control, including it's border and padding, before clipping.
  270. *
  271. * If the value is passed as a percentage of its parent container's clip region, it is interpreted as a value
  272. * between 0-1, where 1 equals the full size of it's parent.
  273. *
  274. * @param width The width.
  275. * @param percentage True if the value should be interpreted as a percentage (0-1), false if it is regular number.
  276. */
  277. void setWidth(float width, bool percentage = false);
  278. /**
  279. * Determines if the width of this control is computed as a percentage of its parent container.
  280. *
  281. * @return True if the width is computed as a percentage of its parent container.
  282. */
  283. bool isWidthPercentage() const;
  284. /**
  285. * Get the height of this control.
  286. *
  287. * @return The height of this control.
  288. */
  289. float getHeight() const;
  290. /**
  291. * Set the desired height of the control, including it's border and padding, before clipping.
  292. *
  293. * If the value is passed as a percentage of its parent container's clip region, it is interpreted as a value
  294. * between 0-1, where 1 equals the full size of it's parent.
  295. *
  296. * @param height The height.
  297. * @param percentage True if the value should be interpreted as a percentage (0-1), false if it is regular number.
  298. */
  299. void setHeight(float height, bool percentage = false);
  300. /**
  301. * Determines if the height of this control is computed as a percentage of its parent container.
  302. *
  303. * @return True if the height is computed as a percentage of its parent container.
  304. */
  305. bool isHeightPercentage() const;
  306. /**
  307. * Set the position of this control relative to its parent container.
  308. *
  309. * This method sets the local position of the control, relative to its container.
  310. * Setting percetage values is not supported with this method, use setX
  311. * and setY instead.
  312. *
  313. * @param x The x coordinate.
  314. * @param y The y coordinate.
  315. */
  316. void setPosition(float x, float y);
  317. /**
  318. * Set the desired size of this control, including its border and padding, before clipping.
  319. *
  320. * This method sets the size of the control, relative to its container.
  321. * Setting percetage values is not supported with this method, use setWidth
  322. * and setHeight instead.
  323. *
  324. * @param width The width.
  325. * @param height The height.
  326. */
  327. void setSize(float width, float height);
  328. /**
  329. * Get the bounds of this control, relative to its parent container and including its
  330. * border and padding, before clipping.
  331. *
  332. * @return The bounds of this control.
  333. */
  334. const Rectangle& getBounds() const;
  335. /**
  336. * Set the bounds of this control, relative to its parent container and including its
  337. * border and padding, before clipping.
  338. *
  339. * This method sets the local bounds of the control, relative to its container.
  340. * Setting percetage values is not supported with this method, use setX,
  341. * setY, setWidth and setHeight instead.
  342. *
  343. * @param bounds The new bounds to set.
  344. */
  345. void setBounds(const Rectangle& bounds);
  346. /**
  347. * Get the absolute bounds of this control, in pixels, including border and padding,
  348. * before clipping.
  349. *
  350. * The absolute bounds of a control represents its final computed bounds after all
  351. * alignment, auto sizing, relative position and sizing has been computed. The
  352. * returned bounds is in absolute coordinates, relative to the control's top-most
  353. * parent container (usually its form).
  354. *
  355. * @return The absolute bounds of this control.
  356. */
  357. const Rectangle& getAbsoluteBounds() const;
  358. /**
  359. * Get the bounds of this control, relative to its parent container, after clipping.
  360. *
  361. * @return The bounds of this control.
  362. */
  363. const Rectangle& getClipBounds() const;
  364. /**
  365. * Get the content area of this control, in screen coordinates, after clipping.
  366. *
  367. * @return The clipping area of this control.
  368. */
  369. const Rectangle& getClip() const;
  370. /**
  371. * Returns the auto sizing mode for this control's width.
  372. *
  373. * @return The auto size mode for this control's width.
  374. */
  375. AutoSize getAutoWidth() const;
  376. /**
  377. * Enables or disables auto sizing for this control's width.
  378. *
  379. * This method is a simplified version of setAutoWidth(AutoSize) left intact for legacy reasons.
  380. * It enables or disables the AUTO_SIZE_STRETCH mode for the control's width.
  381. *
  382. * @param autoWidth True to enable AUTO_SIZE_STRETCH for this control's width.
  383. */
  384. void setAutoWidth(bool autoWidth);
  385. /**
  386. * Sets the auto size mode for this control's width.
  387. *
  388. * @param mode The new auto size mode for this control's width.
  389. */
  390. void setAutoWidth(AutoSize mode);
  391. /**
  392. * Returns the auto sizing mode for this control's height.
  393. *
  394. * @return The auto size mode for this control's height.
  395. */
  396. AutoSize getAutoHeight() const;
  397. /**
  398. * Enables or disables auto sizing for this control's height.
  399. *
  400. * This method is a simplified version of setAutoHeight(AutoSize) left intact for legacy reasons.
  401. * It enables or disables the AUTO_SIZE_STRETCH mode for the control's height.
  402. *
  403. * @param autoHeight True to enable AUTO_SIZE_STRETCH for this control's height.
  404. */
  405. void setAutoHeight(bool autoHeight);
  406. /**
  407. * Sets the auto size mode for this control's height.
  408. *
  409. * @param mode The new auto size mode for this control's height.
  410. */
  411. void setAutoHeight(AutoSize mode);
  412. /**
  413. * Set the alignment of this control within its parent container.
  414. *
  415. * @param alignment This control's alignment.
  416. */
  417. void setAlignment(Alignment alignment);
  418. /**
  419. * Get the alignment of this control within its parent container.
  420. *
  421. * @return The alignment of this control within its parent container.
  422. */
  423. Alignment getAlignment() const;
  424. /**
  425. * Set the size of this control's border.
  426. *
  427. * @param top The height of the border's top side.
  428. * @param bottom The height of the border's bottom side.
  429. * @param left The width of the border's left side.
  430. * @param right The width of the border's right side.
  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 setBorder(float top, float bottom, float left, float right, unsigned char states = STATE_ALL);
  435. /**
  436. * Get the measurements of this control's border for a given state.
  437. *
  438. * @return This control's border.
  439. */
  440. const Theme::Border& getBorder(State state = NORMAL) const;
  441. /**
  442. * Set the texture region of this control's skin.
  443. *
  444. * @param region The texture region, in pixels.
  445. * @param states The states to set this property on.
  446. * One or more members of the Control::State enum, ORed together.
  447. */
  448. void setSkinRegion(const Rectangle& region, unsigned char states = STATE_ALL);
  449. /**
  450. * Get the texture region of this control's skin for a given state.
  451. *
  452. * @param state The state to get this property from.
  453. *
  454. * @return The texture region of this control's skin.
  455. */
  456. const Rectangle& getSkinRegion(State state = NORMAL) const;
  457. /**
  458. * Set the blend color of this control's skin.
  459. *
  460. * @param color The new blend color.
  461. * @param states The states to set this property on.
  462. * One or more members of the Control::State enum, ORed together.
  463. */
  464. void setSkinColor(const Vector4& color, unsigned char states = STATE_ALL);
  465. /**
  466. * Get the blend color of this control's skin for a given state.
  467. *
  468. * @param state The state to get this property from.
  469. *
  470. * @return The blend color of this control's skin.
  471. */
  472. const Vector4& getSkinColor(State state = NORMAL) const;
  473. /**
  474. * Set this control's margin.
  475. *
  476. * @param top Height of top margin.
  477. * @param bottom Height of bottom margin.
  478. * @param left Width of left margin.
  479. * @param right Width of right margin.
  480. */
  481. void setMargin(float top, float bottom, float left, float right);
  482. /**
  483. * Get this control's margin.
  484. *
  485. * @return This control's margin.
  486. */
  487. const Theme::Margin& getMargin() const;
  488. /**
  489. * Set this control's padding.
  490. *
  491. * @param top Height of top padding.
  492. * @param bottom Height of bottom padding.
  493. * @param left Width of left padding.
  494. * @param right Width of right padding.
  495. */
  496. void setPadding(float top, float bottom, float left, float right);
  497. /**
  498. * Get this control's padding.
  499. *
  500. * @return This control's padding.
  501. */
  502. const Theme::Padding& getPadding() const;
  503. /**
  504. * Set the texture region of an image used by this control.
  505. *
  506. * @param id The ID of the image to modify.
  507. * @param region The new texture region of the image.
  508. * @param states The states to set this property on.
  509. * One or more members of the Control::State enum, ORed together.
  510. */
  511. void setImageRegion(const char* id, const Rectangle& region, unsigned char states = STATE_ALL);
  512. /**
  513. * Get the texture region of an image used by this control for a given state.
  514. *
  515. * @param id The ID of the image.
  516. * @param state The state to get this property from.
  517. *
  518. * @return The texture region of the specified image.
  519. */
  520. const Rectangle& getImageRegion(const char* id, State state) const;
  521. /**
  522. * Set the blend color of an image used by this control.
  523. *
  524. * @param id The ID of the image to modify.
  525. * @param color The new blend color of the image.
  526. * @param states The states to set this property on.
  527. * One or more members of the Control::State enum, ORed together.
  528. */
  529. void setImageColor(const char* id, const Vector4& color, unsigned char states = STATE_ALL);
  530. /**
  531. * Get the blend color of an image used by this control for a given state.
  532. *
  533. * @param id The ID of the image.
  534. * @param state The state to get this property from.
  535. *
  536. * @return The blend color of the specified image.
  537. */
  538. const Vector4& getImageColor(const char* id, State state) const;
  539. /**
  540. * Get the texture coordinates of an image used by this control for a given state.
  541. *
  542. * @param id The ID of the image.
  543. * @param state The state to get this property from.
  544. *
  545. * @return The texture coordinates of the specified image.
  546. */
  547. const Theme::UVs& getImageUVs(const char* id, State state) const;
  548. /**
  549. * Set the texture region of this control's cursor.
  550. *
  551. * @param region The cursor region.
  552. * @param states The states to set this property on.
  553. * One or more members of the Control::State enum, ORed together.
  554. */
  555. void setCursorRegion(const Rectangle& region, unsigned char states);
  556. /**
  557. * Get the texture region of this control's cursor for a given state.
  558. *
  559. * @param state The state to get this property from.
  560. *
  561. * @return The texture region of this control's cursor.
  562. */
  563. const Rectangle& getCursorRegion(State state) const;
  564. /**
  565. * Set the blend color of this control's cursor.
  566. *
  567. * @param color The new blend color.
  568. * @param states The states to set this property on.
  569. * One or more members of the Control::State enum, ORed together.
  570. */
  571. void setCursorColor(const Vector4& color, unsigned char states);
  572. /**
  573. * Get the blend color of this control's cursor for a given state.
  574. *
  575. * @param state The state to get this property from.
  576. *
  577. * @return The blend color of this control's cursor.
  578. */
  579. const Vector4& getCursorColor(State state);
  580. /**
  581. * Get the texture coordinates of this control's cursor for a given state.
  582. *
  583. * @param state The state to get this property from.
  584. *
  585. * @return The texture coordinates of this control's cursor.
  586. */
  587. const Theme::UVs& getCursorUVs(State state);
  588. /**
  589. * Set the font used by this control.
  590. *
  591. * @param font The new font to use.
  592. * @param states The states to set this property on.
  593. * One or more members of the Control::State enum, ORed together.
  594. */
  595. void setFont(Font* font, unsigned char states = STATE_ALL);
  596. /**
  597. * Get the font used by this control for a given state.
  598. *
  599. * @param state The state to get this property from.
  600. *
  601. * @return the font used by this control.
  602. */
  603. Font* getFont(State state = NORMAL) const;
  604. /**
  605. * Set this control's font size.
  606. *
  607. * @param size The new font size.
  608. * @param states The states to set this property on.
  609. * One or more members of the Control::State enum, ORed together.
  610. */
  611. void setFontSize(unsigned int size, unsigned char states = STATE_ALL);
  612. /**
  613. * Get this control's font size for a given state.
  614. *
  615. * @param state The state to get this property from.
  616. *
  617. * @return This control's font size.
  618. */
  619. unsigned int getFontSize(State state = NORMAL) const;
  620. /**
  621. * Set this control's text color.
  622. *
  623. * @param color The new text color.
  624. * @param states The states to set this property on.
  625. * One or more members of the Control::State enum, ORed together.
  626. */
  627. void setTextColor(const Vector4& color, unsigned char states = STATE_ALL);
  628. /**
  629. * Get this control's text color for a given state.
  630. *
  631. * @param state The state to get this property from.
  632. *
  633. * @return This control's text color.
  634. */
  635. const Vector4& getTextColor(State state = NORMAL) const;
  636. /**
  637. * Set this control's text alignment.
  638. *
  639. * @param alignment The new text alignment.
  640. * @param states The states to set this property on.
  641. * One or more members of the Control::State enum, ORed together.
  642. */
  643. void setTextAlignment(Font::Justify alignment, unsigned char states = STATE_ALL);
  644. /**
  645. * Get this control's text alignment for a given state.
  646. *
  647. * @param state The state to get this property from.
  648. *
  649. * @return This control's text alignment for the given state.
  650. */
  651. Font::Justify getTextAlignment(State state = NORMAL) const;
  652. /**
  653. * Set whether text is drawn from right to left within this control.
  654. *
  655. * @param rightToLeft Whether text is drawn from right to left within this control.
  656. * @param states The states to set this property on.
  657. * One or more members of the Control::State enum, ORed together.
  658. */
  659. void setTextRightToLeft(bool rightToLeft, unsigned char states = STATE_ALL);
  660. /**
  661. * Get whether text is drawn from right to left within this control, for a given state.
  662. *
  663. * @param state The state to get this property from.
  664. *
  665. * @return Whether text is drawn from right to left within this control, for the given state.
  666. */
  667. bool getTextRightToLeft(State state = NORMAL) const;
  668. /**
  669. * Sets the visibility of a control.
  670. *
  671. * This is a quick way to hide a control without having to remove it from a form.
  672. *
  673. * @param visible true if the control is visible and enabled; false if not-visible and disabled.
  674. */
  675. void setVisible(bool visible);
  676. /**
  677. * Get the visibility of a control.
  678. *
  679. * @return true if the control is visible; false if not visible.
  680. */
  681. bool isVisible() const;
  682. /**
  683. * Determines if this control is visible in its hierarchy.
  684. *
  685. * A control is visible in its hierarchy if it is visible and all of its parents
  686. * are also visible.
  687. */
  688. bool isVisibleInHierarchy() const;
  689. /**
  690. * Set the opacity of this control.
  691. *
  692. * @param opacity The new opacity.
  693. * @param states The states to set this property on. One or more members of the Control::State enum, OR'ed together.
  694. */
  695. void setOpacity(float opacity, unsigned char states = STATE_ALL);
  696. /**
  697. * Get the opacity of this control for a given state.
  698. *
  699. * @param state The state to get this property from.
  700. *
  701. * @return The opacity of this control for a given state.
  702. */
  703. float getOpacity(State state = NORMAL) const;
  704. /**
  705. * Enables/Disables a control.
  706. *
  707. * @param enabled true if the control is enabled; false if disabled.
  708. */
  709. virtual void setEnabled(bool enabled);
  710. /**
  711. * Get whether this control is currently enabled.
  712. *
  713. * @return Whether this control is currently enabled.
  714. */
  715. bool isEnabled() const;
  716. /**
  717. * Determines if this control is enabled in its hierarchy.
  718. *
  719. * A control is enabled in its hierarchy if it is enabled and all of its parents
  720. * are also enabled.
  721. */
  722. bool isEnabledInHierarchy() const;
  723. /**
  724. * Get this control's current state.
  725. *
  726. * @return This control's current state.
  727. */
  728. State getState() const;
  729. /**
  730. * Set whether this control consumes input events,
  731. * preventing them from being passed to the game.
  732. *
  733. * @param consume Whether this control consumes input events.
  734. */
  735. void setConsumeInputEvents(bool consume);
  736. /**
  737. * Get whether this control consumes touch events.
  738. *
  739. * @return Whether this control consumes touch events.
  740. */
  741. bool getConsumeInputEvents();
  742. /**
  743. * Get this control's style.
  744. *
  745. * @return This control's style.
  746. */
  747. Theme::Style* getStyle() const;
  748. /**
  749. * Set the style this control will use when rendering.
  750. *
  751. * @param style The style this control will use when rendering.
  752. */
  753. void setStyle(Theme::Style* style);
  754. /**
  755. * Returns the theme for this control.
  756. */
  757. Theme* getTheme() const;
  758. /**
  759. * Get this control's z-index.
  760. *
  761. * @return This control's z-index.
  762. */
  763. int getZIndex() const;
  764. /**
  765. * Set this control's z-index.
  766. *
  767. * @param zIndex The new z-index.
  768. */
  769. void setZIndex(int zIndex);
  770. /**
  771. * Determines if this control accepts focus.
  772. *
  773. * @return True if this control accepts focus, false if it does not.
  774. */
  775. bool canFocus() const;
  776. /**
  777. * Sets whether or not the control accepts input focus.
  778. *
  779. * @param acceptsFocus True if the control should accept input focus, false otherwise.
  780. */
  781. void setCanFocus(bool acceptsFocus);
  782. /**
  783. * Determines if this control is currently in focus.
  784. *
  785. * @return True if the control is currently in focus.
  786. */
  787. bool hasFocus() const;
  788. /**
  789. * Sets input focus to this control.
  790. *
  791. * If this control accepts focus (the hasFocus method returns true), input focus
  792. * is set to this control. If this control is a container, the first focusable
  793. * control within it gains focus.
  794. *
  795. * @return True if this control or one of its children successfully gained focus,
  796. * false otherwise.
  797. */
  798. virtual bool setFocus();
  799. /**
  800. * Get this control's focus index.
  801. *
  802. * @return This control's focus index.
  803. */
  804. int getFocusIndex() const;
  805. /**
  806. * Set this control's focus index.
  807. *
  808. * Focus indexes control the order in which input focus changes between controls
  809. * when using the focus change controls such as the TAB key.
  810. *
  811. * Valid focus indexes should be zero or greater, with a negative number indicating
  812. * an unset focus index.
  813. *
  814. * @param focusIndex The new focus index.
  815. */
  816. void setFocusIndex(int focusIndex);
  817. /**
  818. * Returns whether this Control object is a Container or not.
  819. *
  820. * @return true if this object is of class Container, false otherwise.
  821. */
  822. virtual bool isContainer() const;
  823. /**
  824. * Gets the type of the Control and returns it as a string.
  825. *
  826. * @return The string of the Control type, all in lower-case.
  827. */
  828. virtual const char* getType() const;
  829. /**
  830. * Returns this control's parent, or NULL if this control does not have a parent.
  831. *
  832. * @return This control's parent.
  833. */
  834. Control* getParent() const;
  835. /**
  836. * Determines if this control is a child (at any level of hierarchy) of the
  837. * specified control.
  838. *
  839. * @param control The control to check.
  840. * @return True if this control is a direct or indirect child of the specified control.
  841. */
  842. bool isChild(Control* control) const;
  843. /**
  844. * Returns this control's top level form, or NULL if this control does not belong to a form.
  845. *
  846. * @return this control's form.
  847. */
  848. Form* getTopLevelForm() const;
  849. /**
  850. * Adds a listener to be notified of specific events affecting
  851. * this control. Event types can be OR'ed together.
  852. * E.g. To listen to touch-press and touch-release events,
  853. * pass <code>Control::Listener::TOUCH | Control::Listener::RELEASE</code>
  854. * as the second parameter.
  855. *
  856. * @param listener The listener to add.
  857. * @param eventFlags The events to listen for.
  858. */
  859. virtual void addListener(Control::Listener* listener, int eventFlags);
  860. /**
  861. * Removes a listener from this control.
  862. *
  863. * @param listener The listener to remove.
  864. */
  865. virtual void removeListener(Control::Listener* listener);
  866. /**
  867. * @see AnimationTarget::getAnimationPropertyComponentCount
  868. */
  869. virtual unsigned int getAnimationPropertyComponentCount(int propertyId) const;
  870. /**
  871. * @see AnimationTarget::getAnimationProperty
  872. */
  873. virtual void getAnimationPropertyValue(int propertyId, AnimationValue* value);
  874. /**
  875. * @see AnimationTarget::setAnimationProperty
  876. */
  877. virtual void setAnimationPropertyValue(int propertyId, AnimationValue* value, float blendWeight = 1.0f);
  878. protected:
  879. /**
  880. * Constant value representing an unset or invalid contact index.
  881. */
  882. static const int INVALID_CONTACT_INDEX = -1;
  883. /**
  884. * Constructor.
  885. */
  886. Control();
  887. /**
  888. * Destructor.
  889. */
  890. virtual ~Control();
  891. /**
  892. * Hidden copy assignment operator.
  893. */
  894. Control& operator=(const Control&);
  895. /**
  896. * Get the overlay type corresponding to this control's current state.
  897. *
  898. * @return The overlay type corresponding to this control's current state.
  899. */
  900. Theme::Style::OverlayType getOverlayType() const;
  901. /**
  902. * Touch callback on touch events. Controls return true if they consume the touch event.
  903. *
  904. * @param evt The touch event that occurred.
  905. * @param x The x position of the touch in pixels. Left edge is zero.
  906. * @param y The y position of the touch in pixels. Top edge is zero.
  907. * @param contactIndex The order of occurrence for multiple touch contacts starting at zero.
  908. *
  909. * @return Whether the touch event was consumed by this control.
  910. *
  911. * @see Touch::TouchEvent
  912. */
  913. virtual bool touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
  914. /**
  915. * Keyboard callback on key events.
  916. *
  917. * @param evt The key event that occurred.
  918. * @param key If evt is KEY_PRESS or KEY_RELEASE then key is the key code from Keyboard::Key.
  919. * If evt is KEY_CHAR then key is the unicode value of the character.
  920. *
  921. * @return Whether the key event was consumed by this control.
  922. *
  923. * @see Keyboard::KeyEvent
  924. * @see Keyboard::Key
  925. */
  926. virtual bool keyEvent(Keyboard::KeyEvent evt, int key);
  927. /**
  928. * Mouse callback on mouse events.
  929. *
  930. * @param evt The mouse event that occurred.
  931. * @param x The x position of the mouse in pixels. Left edge is zero.
  932. * @param y The y position of the mouse in pixels. Top edge is zero.
  933. * @param wheelDelta The number of mouse wheel ticks. Positive is up (forward), negative is down (backward).
  934. *
  935. * @return True if the mouse event is consumed or false if it is not consumed.
  936. *
  937. * @see Mouse::MouseEvent
  938. */
  939. virtual bool mouseEvent(Mouse::MouseEvent evt, int x, int y, int wheelDelta);
  940. /**
  941. * Gamepad callback on gamepad events.
  942. *
  943. * @param gamepad The gamepad whose state changed.
  944. * @param evt The gamepad event that occurred.
  945. * @param analogIndex If evt is JOYSTICK_EVENT or TRIGGER_EVENT, this will be the index of the corresponding control.
  946. */
  947. virtual bool gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int analogIndex);
  948. /**
  949. * Called when a control's properties change. Updates this control's internal rendering
  950. * properties, such as its text viewport.
  951. *
  952. * @param container This control's parent container.
  953. * @param offset Positioning offset to add to the control's position.
  954. */
  955. virtual void update(const Control* container, const Vector2& offset);
  956. /**
  957. * Indicates that a control will begin drawing into the specified batch.
  958. *
  959. * When drawing is finshed (before any other batch can be drawn into), the
  960. * finishBatch method should be called.
  961. *
  962. * @param form The form beign drawn.
  963. * @param batch The sprite batch to be drawn into.
  964. */
  965. void startBatch(Form* form, SpriteBatch* batch);
  966. /**
  967. * Called after a batch has been drawn into and before any other batch is used.
  968. *
  969. * @param form The form being drawn.
  970. * @param batch The batch that was previously started (via Control::startBatch).
  971. */
  972. void finishBatch(Form* form, SpriteBatch* batch);
  973. /**
  974. * Draws the control.
  975. *
  976. * Implementations of Control are expected to perform all drawing into a SpriteBatch.
  977. * Batches should not be explicitly started or finished, but instead should be passed
  978. * to Control::prepare(Form*, SpriteBatch*). This will handle automatically starting
  979. * and finishing the batch when neccessary.
  980. *
  981. * @param form The top level form being drawn.
  982. * @param clip The clipping rectangle.
  983. *
  984. * @return The number of draw calls issued.
  985. */
  986. virtual unsigned int draw(Form* form, const Rectangle& clip);
  987. /**
  988. * Draws the themed border and background of a control.
  989. *
  990. * Implementations of Control are expected to perform all drawing into a SpriteBatch.
  991. * Batches should not be explicitly started or finished, but instead should be passed
  992. * to Control::prepare(Form*, SpriteBatch*). This will handle automatically starting
  993. * and finishing the batch when neccessary.
  994. *
  995. * @param form The top level form being drawn.
  996. * @param clip The clipping rectangle of this control's parent container.
  997. *
  998. * @return The number of draw calls issued.
  999. */
  1000. virtual unsigned int drawBorder(Form* form, const Rectangle& clip);
  1001. /**
  1002. * Draw the images associated with this control.
  1003. *
  1004. * Implementations of Control are expected to perform all drawing into a SpriteBatch.
  1005. * Batches should not be explicitly started or finished, but instead should be passed
  1006. * to Control::prepare(Form*, SpriteBatch*). This will handle automatically starting
  1007. * and finishing the batch when neccessary.
  1008. *
  1009. * @param form The top level form being drawn.
  1010. * @param clip The clipping rectangle of this control's parent container.
  1011. *
  1012. * @return The number of draw calls issued.
  1013. */
  1014. virtual unsigned int drawImages(Form* form, const Rectangle& clip);
  1015. /**
  1016. * Draw this control's text.
  1017. *
  1018. * Implementations of Control are expected to perform all drawing into a SpriteBatch.
  1019. * Batches should not be explicitly started or finished, but instead should be passed
  1020. * to Control::prepare(Form*, SpriteBatch*). This will handle automatically starting
  1021. * and finishing the batch when neccessary.
  1022. *
  1023. * @param form The top level form being drawn.
  1024. * @param clip The clipping rectangle of this control's parent container.
  1025. *
  1026. * @return The number of draw calls issued.
  1027. */
  1028. virtual unsigned int drawText(Form* form, const Rectangle& clip);
  1029. /**
  1030. * Initializes the control.
  1031. *
  1032. * @param typeName The type name of the control being initalized.
  1033. * @param style The style to apply to this control (optional).
  1034. * @param properties The properties to set on this control (optional).
  1035. */
  1036. virtual void initialize(const char* typeName, Theme::Style* style, Properties* properties);
  1037. /**
  1038. * Returns whether this control has been modified and requires an update.
  1039. *
  1040. * @return Whether this control has been modified and requires an update.
  1041. */
  1042. virtual bool isDirty();
  1043. /**
  1044. * Get a Control::State enum from a matching string.
  1045. *
  1046. * @param state The string to match.
  1047. *
  1048. * @return The Control::State enum that matches the given string.
  1049. */
  1050. static State getState(const char* state);
  1051. /**
  1052. * Get a Theme::ThemeImage from its ID, for a given state.
  1053. *
  1054. * @param id The ID of the image to retrieve.
  1055. * @param state The state to get this image from.
  1056. *
  1057. * @return The requested Theme::ThemeImage, or NULL if none was found.
  1058. */
  1059. Theme::ThemeImage* getImage(const char* id, State state);
  1060. /**
  1061. * Notify this control's listeners of a specific event.
  1062. *
  1063. * @param eventType The event to trigger.
  1064. */
  1065. void notifyListeners(Control::Listener::EventType eventType);
  1066. /**
  1067. * Called when a control event is fired for this control, before external
  1068. * listeners are notified of the event.
  1069. *
  1070. * @param evt The event type.
  1071. */
  1072. virtual void controlEvent(Control::Listener::EventType evt);
  1073. /**
  1074. * Gets the Alignment by string.
  1075. *
  1076. * @param alignment The string representation of the Alignment type.
  1077. * @return The Alignment enum value corresponding to the given string.
  1078. */
  1079. static Alignment getAlignment(const char* alignment);
  1080. /**
  1081. * The Control's ID.
  1082. */
  1083. std::string _id;
  1084. /**
  1085. * Whether the control is enabled.
  1086. */
  1087. bool _enabled;
  1088. /**
  1089. * Bits indicating whether bounds values are absolute values or percentages.
  1090. */
  1091. int _boundsBits;
  1092. /**
  1093. * Local bounds, relative to parent container's clipping window, possibly stored as percentages (see _boundsBits).
  1094. */
  1095. Rectangle _relativeBounds;
  1096. /**
  1097. * Local bounds, relative to parent container's clipping window, and desired size.
  1098. */
  1099. Rectangle _bounds;
  1100. /**
  1101. * Local bounds, relative to parent container's clipping window, including border and padding, after clipping.
  1102. */
  1103. Rectangle _clipBounds;
  1104. /**
  1105. * Absolute bounds, including border and padding, before clipping.
  1106. */
  1107. Rectangle _absoluteBounds;
  1108. /**
  1109. * Absolute bounds, including border and padding, after clipping.
  1110. */
  1111. Rectangle _absoluteClipBounds;
  1112. /**
  1113. * Absolute bounds of content area (i.e. without border and padding), before clipping.
  1114. */
  1115. Rectangle _viewportBounds;
  1116. /**
  1117. * Absolute bounds of content area (i.e. without border and padding), after clipping.
  1118. */
  1119. Rectangle _viewportClipBounds;
  1120. /**
  1121. * If the control is dirty and need updating.
  1122. */
  1123. bool _dirty;
  1124. /**
  1125. * Flag for whether the Control consumes input events.
  1126. */
  1127. bool _consumeInputEvents;
  1128. /**
  1129. * The Control's Alignment
  1130. */
  1131. Alignment _alignment;
  1132. /**
  1133. * Whether the Control's alignment has been set programmatically.
  1134. */
  1135. bool _isAlignmentSet;
  1136. /**
  1137. * Whether the Control's width is auto-sized.
  1138. */
  1139. AutoSize _autoWidth;
  1140. /**
  1141. * Whether the Control's height is auto-sized.
  1142. */
  1143. AutoSize _autoHeight;
  1144. /**
  1145. * Listeners map of EventType's to a list of Listeners.
  1146. */
  1147. //std::map<Listener::EventType, std::list<Listener*>*>* _listeners;
  1148. std::map<Control::Listener::EventType, std::list<Control::Listener*>*>* _listeners;
  1149. /**
  1150. * The Control's Theme::Style.
  1151. */
  1152. Theme::Style* _style;
  1153. /**
  1154. * The control is not visible and _state become DISABLED if false.
  1155. */
  1156. bool _visible;
  1157. /**
  1158. * The current opacity of the control.
  1159. */
  1160. float _opacity;
  1161. /**
  1162. * The z-order of the control.
  1163. */
  1164. int _zIndex;
  1165. /**
  1166. * The contact index assigned to this control.
  1167. */
  1168. int _contactIndex;
  1169. /**
  1170. * The focus order of the control.
  1171. */
  1172. int _focusIndex;
  1173. /**
  1174. * Whether or not the control accepts input focus.
  1175. */
  1176. bool _canFocus;
  1177. /**
  1178. * The control's parent container.
  1179. */
  1180. Container* _parent;
  1181. private:
  1182. /*
  1183. * Constructor.
  1184. */
  1185. Control(const Control& copy);
  1186. Theme::Style::Overlay** getOverlays(unsigned char overlayTypes, Theme::Style::Overlay** overlays);
  1187. Theme::Style::Overlay* getOverlay(Control::State state) const;
  1188. void overrideStyle();
  1189. void overrideThemedProperties(Properties* properties, unsigned char states);
  1190. void setImageList(Theme::ImageList* imageList, unsigned char states = STATE_ALL);
  1191. void setCursor(Theme::ThemeImage* cursor, unsigned char states = STATE_ALL);
  1192. void setSkin(Theme::Skin* skin, unsigned char states = STATE_ALL);
  1193. Theme::Skin* getSkin(State state);
  1194. void addSpecificListener(Control::Listener* listener, Control::Listener::EventType eventType);
  1195. bool _styleOverridden;
  1196. Theme::Skin* _skin;
  1197. };
  1198. }
  1199. #endif