UiAnimation.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 <OgreString.h>
  17. #include <Ogre.h>
  18. #include <vector>
  19. class UiWidget;
  20. /**
  21. * A keyframe value.
  22. */
  23. struct UiKeyFrameFloat{
  24. /**
  25. * Keyframe time, in seconds.
  26. */
  27. float time;
  28. /**
  29. * Value for the keyframe.
  30. */
  31. float value;
  32. };
  33. /**
  34. * Coordinate keyframe.
  35. */
  36. struct UiKeyFrameVector2{
  37. /**
  38. * Keyframe time, in seconds.
  39. */
  40. float time;
  41. /**
  42. * Position for the keyframe.
  43. */
  44. Ogre::Vector2 value;
  45. };
  46. /**
  47. * An UI element animation.
  48. */
  49. class UiAnimation{
  50. public:
  51. /**
  52. * Constructor.
  53. *
  54. * @param name[in] Animation name.
  55. * @param widget[in] Widget to animate.
  56. */
  57. UiAnimation(const Ogre::String& name, UiWidget* widget);
  58. /**
  59. * Destructor.
  60. */
  61. virtual ~UiAnimation();
  62. /**
  63. * Animation state.
  64. */
  65. enum State{
  66. /**
  67. * @todo Understand and document.
  68. */
  69. DEFAULT,
  70. /**
  71. * Animate only once.
  72. */
  73. ONCE
  74. };
  75. /**
  76. * Adds time to the animation and updates it.
  77. *
  78. * @param time[in] Time to add, in seconds.
  79. */
  80. void AddTime(const float time);
  81. /**
  82. * Retrieves the animation name.
  83. *
  84. * @return The animation name.
  85. */
  86. const Ogre::String& GetName() const;
  87. /**
  88. * Sets the time of the animation and updates it.
  89. *
  90. * @param time[i] The time of the animation.
  91. */
  92. void SetTime(const float time);
  93. /**
  94. * Retrieves the animation current time.
  95. *
  96. * @return The animation current time.
  97. */
  98. float GetTime() const;
  99. /**
  100. * Sets the animation length.
  101. *
  102. * @param time[in] The animation length, in seconds.
  103. */
  104. void SetLength(const float time);
  105. /**
  106. * Retrieves the animation length.
  107. *
  108. * @return The animation length, in seconds.
  109. */
  110. float GetLength() const;
  111. /**
  112. * Adds a scale keyframe to the animation.
  113. *
  114. * @param key_frame[in] The keyframe to add.
  115. */
  116. void AddScaleKeyFrame(const UiKeyFrameVector2& key_frame);
  117. /**
  118. * Adds a X position keyframe to the animation.
  119. *
  120. * @param key_frame[in] The keyframe to add.
  121. */
  122. void AddXKeyFrame(const UiKeyFrameVector2& key_frame);
  123. /**
  124. * Adds a Y position keyframe to the animation.
  125. *
  126. * @param key_frame[in] The keyframe to add.
  127. */
  128. void AddYKeyFrame(const UiKeyFrameVector2& key_frame);
  129. /**
  130. * Adds a width keyframe to the animation.
  131. *
  132. * @param key_frame[in] The keyframe to add.
  133. */
  134. void AddWidthKeyFrame(const UiKeyFrameVector2& key_frame);
  135. /**
  136. * Adds a height keyframe to the animation.
  137. *
  138. * @param key_frame[in] The keyframe to add.
  139. */
  140. void AddHeightKeyFrame(const UiKeyFrameVector2& key_frame);
  141. /**
  142. * Adds a rotation keyframe to the animation.
  143. *
  144. * @param key_frame[in] The keyframe to add.
  145. */
  146. void AddRotationKeyFrame(const UiKeyFrameFloat& key_frame);
  147. /**
  148. * Adds an alpha keyframe to the animation.
  149. *
  150. * @param key_frame[in] The keyframe to add.
  151. */
  152. void AddAlphaKeyFrame(const UiKeyFrameFloat& key_frame);
  153. /**
  154. * Adds an scissor keyframe to the animation.
  155. *
  156. * @param x1[in] @todo Understand and document.
  157. * @param y1[in] @todo Understand and document.
  158. * @param x2[in] @todo Understand and document.
  159. * @param y2[in] @todo Understand and document.
  160. * @todo What is an scissor keyframe?.
  161. */
  162. void AddScissorKeyFrame(
  163. const UiKeyFrameVector2& x1, const UiKeyFrameVector2& y1,
  164. const UiKeyFrameVector2& x2, const UiKeyFrameVector2& y2
  165. );
  166. private:
  167. /**
  168. * Constructor.
  169. */
  170. UiAnimation();
  171. /**
  172. * Gets the value from a @{see UiKeyFrameFloat}.
  173. *
  174. * @return The value of the keyframe.
  175. */
  176. float KeyFrameGetValue(std::vector<UiKeyFrameFloat>& data);
  177. /**
  178. * Gets the value from a @{see UiKeyFrameVector2}.
  179. *
  180. * @return The value of the keyframe.
  181. */
  182. Ogre::Vector2 KeyFrameGetValue(std::vector<UiKeyFrameVector2>& data);
  183. /**
  184. * The UI animation name.
  185. */
  186. Ogre::String name_;
  187. /**
  188. * The UI widget.
  189. */
  190. UiWidget* widget_;
  191. /**
  192. * The animation's current time.
  193. */
  194. float time_;
  195. /**
  196. * The animation length.
  197. */
  198. float length_;
  199. /**
  200. * List of scale keyframes.
  201. */
  202. std::vector<UiKeyFrameVector2> scale_;
  203. /**
  204. * List of X position keyframes.
  205. */
  206. std::vector<UiKeyFrameVector2> x_;
  207. /**
  208. * List of Y position keyframes.
  209. */
  210. std::vector<UiKeyFrameVector2> y_;
  211. /**
  212. * List of width keyframes.
  213. */
  214. std::vector<UiKeyFrameVector2> width_;
  215. /**
  216. * List of height keyframes.
  217. */
  218. std::vector<UiKeyFrameVector2> height_;
  219. /**
  220. * List of rotation keyframes.
  221. */
  222. std::vector<UiKeyFrameFloat> rotation_;
  223. /**
  224. * List of alpha keyframes.
  225. */
  226. std::vector<UiKeyFrameFloat> alpha_;
  227. /**
  228. * @todo Understand and document.
  229. */
  230. std::vector<UiKeyFrameVector2> scissor_x_top_;
  231. /**
  232. * @todo Understand and document.
  233. */
  234. std::vector<UiKeyFrameVector2> scissor_y_left_;
  235. /**
  236. * @todo Understand and document.
  237. */
  238. std::vector<UiKeyFrameVector2> scissor_x_bottom_;
  239. /**
  240. * @todo Understand and document.
  241. */
  242. std::vector<UiKeyFrameVector2> scissor_y_right_;
  243. };