Control.h 43 KB

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