UiWidget.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #pragma once
  16. #include <OgreColourValue.h>
  17. #include <OgreString.h>
  18. #include <Ogre.h>
  19. #include <vector>
  20. #include "ScriptManager.h"
  21. #include "UiAnimation.h"
  22. /**
  23. * An UI widget.
  24. */
  25. class UiWidget{
  26. public:
  27. /**
  28. * Constructor.
  29. *
  30. * @param name[in] Name for the widget.
  31. */
  32. UiWidget(const Ogre::String& name);
  33. /**
  34. * Constructor.
  35. *
  36. * @param name[in] Name for the widget.
  37. * @param path_name[in] @todo Understand and document.
  38. * @param parent[in] Widget to be made parent of the new one.
  39. */
  40. UiWidget(
  41. const Ogre::String& name, const Ogre::String& path_name,
  42. UiWidget* parent
  43. );
  44. /**
  45. * Destructor.
  46. */
  47. virtual ~UiWidget();
  48. /**
  49. * Initializes the widget.
  50. *
  51. * It sets the widget geometry.
  52. */
  53. void Initialise();
  54. /**
  55. * Updates the widget status.
  56. */
  57. virtual void Update();
  58. /**
  59. * Handles resizing events.
  60. *
  61. * Recalculates the widget geometry and calls OnResize for all the
  62. * widget children.
  63. */
  64. void OnResize();
  65. /**
  66. * Renders the widget and it's children.
  67. */
  68. virtual void Render();
  69. /**
  70. * Toggles the widget visibility.
  71. *
  72. * @param vivible[in] True to make the widget visible, false to make it
  73. * invisible.
  74. */
  75. void SetVisible(const bool visible);
  76. /**
  77. * Checks the widget visibility.
  78. *
  79. * @return True if the widget is visible, false if it is invisible.
  80. */
  81. bool IsVisible() const;
  82. /**
  83. * Retrieves the widget name.
  84. *
  85. * @return The widget name.
  86. */
  87. const Ogre::String& GetName() const;
  88. /**
  89. * Add other widget as the widget's child.
  90. *
  91. * @param widget[in] Widget to be made children of this one.
  92. */
  93. void AddChild(UiWidget* widget);
  94. /**
  95. * Retrieves a children by name.
  96. *
  97. * @param name[in] Name of the children widget to retrieve.
  98. * @return The children widget by the specified name. NULL if the
  99. * widgets has no direct children by that name.
  100. */
  101. UiWidget* GetChild(const Ogre::String& name);
  102. /**
  103. * Retrieves a children by ID.
  104. *
  105. * @param name[in] ID of the children widget to retrieve.
  106. * @return The children widget by the specified name. NULL if the
  107. * widgets has no direct children by that ID.
  108. */
  109. UiWidget* GetChild(const unsigned int id);
  110. /**
  111. * Counts the widget's children.
  112. *
  113. * @return The number of direct children.
  114. */
  115. unsigned int GetNumberOfChildren();
  116. /**
  117. * Removes all children widgets.
  118. */
  119. void RemoveAllChildren();
  120. /**
  121. * Adds an animation to the widget.
  122. *
  123. * @param animation[in] The animation to add.
  124. */
  125. void AddAnimation(UiAnimation* animation);
  126. /**
  127. * Retrieves the current animation name.
  128. *
  129. * @return The current animation name. A blankstring if there is no set
  130. * default animation.
  131. */
  132. const Ogre::String& GetCurrentAnimationName() const;
  133. /**
  134. * Retrieves the current animation state.
  135. *
  136. * @return The current animation state.
  137. */
  138. UiAnimation::State GetAnimationState() const;
  139. /**
  140. * Plays an animation.
  141. *
  142. * @param animation[in] Animation to play.
  143. * @param state[in] Initial animation state.
  144. * @param start[in] Seconds at which to start the animation.
  145. * @param start[in] Seconds at which to end the animation. -1 to play
  146. * it in full.
  147. */
  148. void PlayAnimation(
  149. const Ogre::String& animation, UiAnimation::State state,
  150. const float start, const float end
  151. );
  152. /**
  153. * Plays an animation.
  154. *
  155. * The animation will be played from the start for it's full length.
  156. *
  157. * @param animation[in] The name of the animation to play.
  158. * @todo It uses the parameter UiAnimation::DEFAULT. Whats does it do?
  159. * Does it play the animation in a loop, or uses a default value of the
  160. * specific animation.
  161. */
  162. void ScriptPlayAnimation(const char* name);
  163. /**
  164. * Plays an animation.
  165. *
  166. * The animation will be played from the start for it's full length,
  167. * only once.
  168. *
  169. * @param animation[in] The name of the animation to play.
  170. */
  171. void ScriptPlayAnimationStop(const char* name);
  172. /**
  173. * @param name[in] Name of the animation to play.
  174. * @param start[in] Seconds at which to start the animation.
  175. * @param start[in] Seconds at which to end the animation. -1 to play
  176. * it in full.
  177. * @todo It uses the parameter UiAnimation::DEFAULT. Whats does it do?
  178. * Does it play the animation in a loop, or uses a default value of the
  179. * specific animation.
  180. */
  181. void ScriptPlayAnimation(
  182. const char* name, const float start, const float end
  183. );
  184. /**
  185. * Plays an animation.
  186. *
  187. * The animation will be played only once.
  188. *
  189. * @param animation[in] The name of the animation to play.
  190. */
  191. void ScriptPlayAnimationStop(
  192. const char* name, const float start, const float end
  193. );
  194. /**
  195. * Sets the default animation for the widget.
  196. *
  197. * @param animation[in] The name of the default animation.
  198. */
  199. void ScriptSetDefaultAnimation(const char* animation);
  200. /**
  201. * Synchronizes an animation.
  202. *
  203. * @return Always -1.
  204. * @todo Understand and document better.
  205. */
  206. int ScriptAnimationSync();
  207. /**
  208. * Marks the widget as transformed.
  209. *
  210. * It means that it's geometry must be updated in the next pass. It
  211. * also marks the direct children as transformed.
  212. */
  213. void SetUpdateTransformation();
  214. /**
  215. * Recalculates the widget geometry.
  216. */
  217. virtual void UpdateTransformation();
  218. /**
  219. * Horizontal alignment of the widget.
  220. */
  221. enum Align{
  222. /**
  223. * Left alignment.
  224. */
  225. LEFT,
  226. /**
  227. * Right alignment.
  228. */
  229. RIGHT,
  230. /**
  231. * Centered horizontally.
  232. */
  233. CENTER
  234. };
  235. /**
  236. * Vertical alignment of the widget.
  237. */
  238. enum VerticalAlign{
  239. /**
  240. * Top alignment.
  241. */
  242. TOP,
  243. /**
  244. * Bottom alignment.
  245. */
  246. BOTTOM,
  247. /**
  248. * Centered vertically.
  249. */
  250. MIDDLE
  251. };
  252. /**
  253. * Sets the widget horizontal alignment.
  254. *
  255. * @poaram align[in] Widget alignment.
  256. */
  257. void SetAlign(const UiWidget::Align align);
  258. /**
  259. * Sets the widget vertical alignment.
  260. *
  261. * @poaram align[in] Widget alignment.
  262. */
  263. void SetVerticalAlign(const UiWidget::VerticalAlign valign);
  264. /**
  265. * Retrieves the final Z-index of the widget after a transformation.
  266. *
  267. * @return The final Z-index.
  268. */
  269. float GetFinalZ() const;
  270. /**
  271. * Retrieves the coordinates of the widget after a transformation.
  272. *
  273. * @return The final coordinater.
  274. */
  275. Ogre::Vector2 GetFinalOrigin() const;
  276. /**
  277. * Retrieves the final translation of the widget after a
  278. * transformation.
  279. *
  280. * @return The final translation.
  281. * @todo What is a translation here? Just a movement?
  282. */
  283. Ogre::Vector2 GetFinalTranslate() const;
  284. /**
  285. * Retrieves the final size of the widget after a transformation.
  286. *
  287. * @return The final size.
  288. */
  289. Ogre::Vector2 GetFinalSize() const;
  290. /**
  291. * Retrieves the final scale of the widget after a transformation.
  292. *
  293. * @return The final scale.
  294. */
  295. Ogre::Vector2 GetFinalScale() const;
  296. /**
  297. * Retrieves the final scissor of the widget after a transformation.
  298. *
  299. * @return The final scissor.
  300. * @todo What is a scissor?
  301. */
  302. Ogre::Vector4 GetFinalScissor(bool& scissor) const;
  303. float GetFinalRotation() const;
  304. /**
  305. * Sets the X coordinate origin for the widget.
  306. *
  307. * @param percent[in] @todo
  308. * @param x[in] Origin X coordinate.
  309. */
  310. void SetOriginX(const float percent, const float x);
  311. /**
  312. * Sets the Y coordinate origin for the widget.
  313. *
  314. * @param percent[in] @todo
  315. * @param y[in] Origin Y coordinate.
  316. */
  317. void SetOriginY(const float percent, const float y);
  318. /**
  319. * Sets the X coordinate for the widget.
  320. *
  321. * @param percent[in] @todo
  322. * @param x[in] The X coordinate.
  323. */
  324. void SetX(const float percent, const float x);
  325. /**
  326. * Retrieves the X coordinate for the widget.
  327. *
  328. * @param percent[out] @todo
  329. * @param x[out] The X coordinate will be loaded here.
  330. */
  331. void GetX(float& percent, float& x);
  332. /**
  333. * Sets the Y coordinate for the widget.
  334. *
  335. * @param percent[in] @todo
  336. * @param y[in] The Y coordinate.
  337. */
  338. void SetY(const float percent, const float y);
  339. /**
  340. * Retrieves the Y coordinate for the widget.
  341. *
  342. * @param percent[out] @todo
  343. * @param y[out] The Y coordinate will be loaded here.
  344. */
  345. void GetY(float& percent, float& y);
  346. /**
  347. * Sets the widget Z-index.
  348. *
  349. * @param z[in] The Z-index.
  350. */
  351. void SetZ(const float z);
  352. /**
  353. * Sets the width the widget.
  354. *
  355. * @param percent[in] @todo
  356. * @param width[in] The widget width.
  357. */
  358. void SetWidth(const float percent, const float width);
  359. /**
  360. * Retrieves the width of the widget.
  361. *
  362. * @param percent[out] @todo
  363. * @param width[out] The width will be loaded here.
  364. */
  365. void GetWidth(float& percent, float& width);
  366. /**
  367. * Sets the height the widget.
  368. *
  369. * @param percent[in] @todo
  370. * @param height[in] The widget height.
  371. */
  372. void SetHeight(const float percent, const float height);
  373. /**
  374. * Retrieves the height of the widget.
  375. *
  376. * @param percent[out] @todo
  377. * @param height[out] The height will be loaded here.
  378. */
  379. void GetHeight(float& percent, float& height);
  380. /**
  381. * Sets the widget scale.
  382. *
  383. * @param scale[in] The new widget scale.
  384. */
  385. void SetScale(const Ogre::Vector2& scale);
  386. /**
  387. * Sets the widget rotation.
  388. *
  389. * @param scale[in] The new widget rotation.
  390. */
  391. void SetRotation(const float degree);
  392. /**
  393. * Sets the widget scissor area.
  394. *
  395. * @param percent_x1[in] @todo Undersnd and document.
  396. * @param x1[in] @todo Undersnd and document.
  397. * @param percent_y1[in] @todo Undersnd and document.
  398. * @param y1[in] @todo Undersnd and document.
  399. * @param percent_x2[in] @todo Undersnd and document.
  400. * @param x2[in] @todo Undersnd and document.
  401. * @param percent_y2[in] @todo Undersnd and document.
  402. * @param y2[in] @todo Undersnd and document.
  403. */
  404. void SetScissorArea(
  405. const float percent_x1, const float x1, const float percent_y1,
  406. const float y1, const float percent_x2, const float x2,
  407. const float percent_y2, const float y2
  408. );
  409. /**
  410. * @todo Understand and document.
  411. *
  412. * @param global[in] @todo Understand and document.
  413. */
  414. void SetGlobalScissor(const bool global);
  415. /**
  416. * Sets the widget colour.
  417. *
  418. * It set the same colour for all corners.
  419. *
  420. * @param r[in] Colour red component (0-255).
  421. * @param g[in] Colour green component (0-255).
  422. * @param b[in] Colour blue component (0-255).
  423. */
  424. void SetColour(const float r, const float g, const float b);
  425. /**
  426. * Sets the widget colour.
  427. *
  428. * It set the same colours for each corner, and blends it in the
  429. * interior.
  430. *
  431. * @param r1[in] Colour red component for the top-left corner (0-255).
  432. * @param g1[in] Colour green component for the top-left corner
  433. * (0-255).
  434. * @param b1[in] Colour blue component for the top-left corner (0-255).
  435. * @param r2[in] Colour red component for the top-right corner (0-255).
  436. * @param g2[in] Colour green component for the top-right corner
  437. * (0-255).
  438. * @param b2[in] Colour blue component for the top-right corner (0-255).
  439. * @param r3[in] Colour red component for the bottom-right corner (0-255).
  440. * @param g3[in] Colour green component for the bottom-right corner
  441. * (0-255).
  442. * @param b3[in] Colour blue component for the bottom-right corner
  443. * (0-255).
  444. * @param r4[in] Colour red component for the bottom-left corner
  445. * (0-255).
  446. * @param g4[in] Colour green component for the bottom-left corner
  447. * (0-255).
  448. * @param b4[in] Colour blue component for the bottom-left corner
  449. * (0-255).
  450. */
  451. void SetColours(
  452. const float r1, const float g1, const float b1,
  453. const float r2, const float g2, const float b2,
  454. const float r3, const float g3, const float b3,
  455. const float r4, const float g4, const float b4
  456. );
  457. /**
  458. * Sets the widget transparency.
  459. *
  460. * @param a[in] Alpha value (0 opaque, 255 fully transparent).
  461. */
  462. void SetAlpha(const float a);
  463. protected:
  464. /**
  465. * The widget name.
  466. */
  467. Ogre::String name_;
  468. /**
  469. * @todo Understand and document.
  470. */
  471. Ogre::String path_name_;
  472. /**
  473. * The parent widget.
  474. */
  475. UiWidget* parent_;
  476. /**
  477. * The list of children widgets.
  478. */
  479. std::vector<UiWidget*> children_;
  480. /**
  481. * Width, in game screen units.
  482. *
  483. * @todo Understand and document.
  484. */
  485. float screen_width_;
  486. /**
  487. * Height, in game screen units.
  488. *
  489. * @todo Understand and document.
  490. */
  491. float screen_height_;
  492. /**
  493. * Indicates if the widget is visible.
  494. */
  495. bool visible_;
  496. /**
  497. * Top-left corner colour.
  498. */
  499. Ogre::ColourValue colour_1_;
  500. /**
  501. * Top-right corner colour.
  502. */
  503. Ogre::ColourValue colour_2_;
  504. /**
  505. * Bottom-right corner colour.
  506. */
  507. Ogre::ColourValue colour_3_;
  508. /**
  509. * Bottom-left corner colour.
  510. */
  511. Ogre::ColourValue colour_4_;
  512. /**
  513. * The vertical alignment.
  514. */
  515. Align align_;
  516. /**
  517. * The evrtical alignment.
  518. */
  519. VerticalAlign vertical_align_;
  520. /**
  521. * Indicates if the widget is marked as transformed.
  522. */
  523. bool update_transformation_;
  524. /**
  525. * Final origin point after a transformation.
  526. */
  527. Ogre::Vector2 final_origin_;
  528. /**
  529. * Final translation after a transformation.
  530. *
  531. * @todo Understand and document.
  532. */
  533. Ogre::Vector2 final_translate_;
  534. /**
  535. * Final Z-index after a transformation.
  536. */
  537. float final_z_;
  538. /**
  539. * Final size after a transformation.
  540. */
  541. Ogre::Vector2 final_size_;
  542. /**
  543. * Final scale after a transformation.
  544. */
  545. Ogre::Vector2 final_scale_;
  546. /**
  547. * Final rotation after a transformation.
  548. */
  549. float final_rotation_;
  550. /**
  551. * @todo Understand and document.
  552. */
  553. float origin_x_percent_;
  554. /**
  555. * The origin X coordinate.
  556. */
  557. float origin_x_;
  558. /**
  559. * @todo Understand and document.
  560. */
  561. float origin_y_percent_;
  562. /**
  563. * The origin Y coordinate.
  564. */
  565. float origin_y_;
  566. /**
  567. * @todo Understand and document.
  568. */
  569. float x_percent_;
  570. /**
  571. * The widget X coordinate.
  572. */
  573. float x_;
  574. /**
  575. * @todo Understand and document.
  576. */
  577. float y_percent_;
  578. /**
  579. * The widget Y coordinate.
  580. */
  581. float y_;
  582. /**
  583. * The widget Z coordinate.
  584. */
  585. float z_;
  586. /**
  587. * @todo Understand and document.
  588. */
  589. float width_percent_;
  590. /**
  591. * The widget width.
  592. */
  593. float width_;
  594. /**
  595. * @todo Understand and document.
  596. */
  597. float height_percent_;
  598. /**
  599. * The widget height.
  600. */
  601. float height_;
  602. /**
  603. * The widget scale.
  604. */
  605. Ogre::Vector2 scale_;
  606. /**
  607. * The widget rotation.
  608. */
  609. float rotation_;
  610. /**
  611. * @todo Understand and document.
  612. */
  613. bool scissor_;
  614. /**
  615. * @todo Understand and document.
  616. */
  617. bool local_scissor_;
  618. /**
  619. * @todo Understand and document.
  620. */
  621. bool global_scissor_;
  622. /**
  623. * @todo Understand and document.
  624. */
  625. int scissor_top_;
  626. /**
  627. * @todo Understand and document.
  628. */
  629. float scissor_x_percent_top_;
  630. /**
  631. * @todo Understand and document.
  632. */
  633. float scissor_x_top_;
  634. /**
  635. * @todo Understand and document.
  636. */
  637. int scissor_bottom_;
  638. /**
  639. * @todo Understand and document.
  640. */
  641. float scissor_x_percent_bottom_;
  642. /**
  643. * @todo Understand and document.
  644. */
  645. float scissor_x_bottom_;
  646. /**
  647. * @todo Understand and document.
  648. */
  649. int scissor_left_;
  650. /**
  651. * @todo Understand and document.
  652. */
  653. float scissor_y_percent_left_;
  654. /**
  655. * @todo Understand and document.
  656. */
  657. float scissor_y_left_;
  658. /**
  659. * @todo Understand and document.
  660. */
  661. int scissor_right_;
  662. /**
  663. * @todo Understand and document.
  664. */
  665. float scissor_y_percent_right_;
  666. /**
  667. * @todo Understand and document.
  668. */
  669. float scissor_y_right_;
  670. /**
  671. * The current animation.
  672. */
  673. UiAnimation* animation_current_;
  674. /**
  675. * The animation stack.
  676. *
  677. * @todo Understand and document better.
  678. */
  679. std::vector<ScriptId> animation_sync_;
  680. /**
  681. * The current animation state.
  682. */
  683. UiAnimation::State animation_state_;
  684. /**
  685. * The name of the default animation.
  686. */
  687. Ogre::String animation_default_;
  688. /**
  689. * Time for the current animation to end.
  690. */
  691. float animation_end_time_;
  692. /**
  693. * The list of animations.
  694. */
  695. std::vector<UiAnimation*> animations_;
  696. private:
  697. /**
  698. * Constructor.
  699. */
  700. UiWidget();
  701. };