PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/VoltAir/Engine/graphics/AnimatedImageRenderer.h

https://github.com/ardock/VoltAir
C Header | 208 lines | 60 code | 10 blank | 138 comment | 0 complexity | eb0c6059cc5de7213e1ad752d7978ffb MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. * Copyright (C) 2014 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef ANIMATEDIMAGERENDERER_H
  17. #define ANIMATEDIMAGERENDERER_H
  18. #include "ImageRenderer.h"
  19. /**
  20. * @ingroup Engine
  21. * @ingroup QQuickItem
  22. * @brief Graphic based on ImageRenderer which displays a frame animation from a texture sheet.
  23. *
  24. * The texture sheets used by this class contain a vertical strip of equally sized frames, with
  25. * the top frame being the first frame. These frames are displayed in sequence, with a specified
  26. * delay (#frameDelay) between frames.
  27. *
  28. * This class is used for various animations in the game, including the Orb collectables, and
  29. * the Portal animations.
  30. */
  31. // TODO: Supporting grid layouts for texture sheets would allow larger animations to fit into the
  32. // texture sheets within the size limitations of more hardware.
  33. class AnimatedImageRenderer : public ImageRenderer {
  34. Q_OBJECT
  35. /**
  36. * @brief Pause this animation at the current frame.
  37. */
  38. Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged)
  39. /**
  40. * @brief Loop this animation, restarting it at the first frame after the last frame has
  41. * finished displaying.
  42. */
  43. Q_PROPERTY(bool looped READ isLooped WRITE setLooped NOTIFY loopedChanged)
  44. /**
  45. * @brief Animate this image at the speed of the game, pausing when the game is paused.
  46. */
  47. Q_PROPERTY(bool useGameTime READ isUseGameTime WRITE setUseGameTime NOTIFY useGameTimeChanged)
  48. /**
  49. * @brief Delay between frames, in seconds.
  50. */
  51. Q_PROPERTY(float frameDelay READ getFrameDelay WRITE setFrameDelay NOTIFY frameDelayChanged)
  52. /**
  53. * @brief Total number of frames in the texture sheet.
  54. */
  55. Q_PROPERTY(int frameCount READ getFrameCount WRITE setFrameCount NOTIFY frameCountChanged)
  56. /**
  57. * @brief Index of the current frame, zero being the first frame.
  58. */
  59. Q_PROPERTY(int currentFrame READ getCurrentFrame WRITE setCurrentFrame)
  60. /**
  61. * @brief Seek position into the animation, with @c 0.0f being the start, and @c 1.0f being the
  62. * end.
  63. */
  64. Q_PROPERTY(float currentPosition READ getCurrentPosition WRITE setCurrentPosition)
  65. /**
  66. * @brief Don't animate when this image is off screen or #visible is @c false.
  67. *
  68. * This reduces the amount of CPU used, but means it isn't completely in step with game time.
  69. */
  70. Q_PROPERTY(bool pauseWhenHidden READ getPauseWhenHidden WRITE setPauseWhenHidden
  71. NOTIFY pauseWhenHiddenChanged)
  72. public:
  73. /**
  74. * @brief Construct a AnimatedImageRenderer.
  75. * @param parent Parent item
  76. */
  77. explicit AnimatedImageRenderer(QQuickItem* parent = nullptr);
  78. /**
  79. * @brief Returns #paused.
  80. */
  81. bool isPaused() const { return mPaused; }
  82. /**
  83. * @brief Sets #paused.
  84. * @param value Boolean to set #paused to
  85. */
  86. void setPaused(bool value);
  87. /**
  88. * @brief Returns #looped.
  89. */
  90. bool isLooped() const { return mLooped; }
  91. /**
  92. * @brief Sets #looped.
  93. * @param value Boolean to set #looped to
  94. */
  95. void setLooped(bool value);
  96. /**
  97. * @brief Returns #useGameTime.
  98. */
  99. bool isUseGameTime() const { return mUseGameTime; }
  100. /**
  101. * @brief Sets #useGameTime.
  102. * @param value Boolean to set #useGameTime to
  103. */
  104. void setUseGameTime(bool value);
  105. /**
  106. * @brief Returns #frameDelay.
  107. */
  108. float getFrameDelay() const { return mFrameDelay; }
  109. /**
  110. * @brief Sets #frameDelay.
  111. * @param value Float to set #frameDelay to
  112. */
  113. void setFrameDelay(float value);
  114. /**
  115. * @brief Returns #frameCount.
  116. */
  117. int getFrameCount() const { return mFrameCount; }
  118. /**
  119. * @brief Sets #frameCount.
  120. * @param value Integer to set #frameCount to
  121. */
  122. void setFrameCount(int value);
  123. /**
  124. * @brief Returns #currentFrame.
  125. */
  126. int getCurrentFrame() const { return mCurrentFrame; }
  127. /**
  128. * @brief Sets #currentFrame.
  129. * @param value Integer to set #currentFrame to
  130. */
  131. void setCurrentFrame(int value);
  132. /**
  133. * @brief Returns #currentPosition.
  134. */
  135. float getCurrentPosition() const { return mCurrentPosition; }
  136. /**
  137. * @brief Sets #currentPosition.
  138. * @param value Float to set #currentPosition to
  139. */
  140. void setCurrentPosition(float value);
  141. /**
  142. * @brief Returns #pauseWhenHidden.
  143. */
  144. bool getPauseWhenHidden() const { return mPauseWhenHidden; }
  145. /**
  146. * @brief Sets #pauseWhenHidden.
  147. * @param value Float to set #pauseWhenHidden to
  148. */
  149. void setPauseWhenHidden(bool value);
  150. signals:
  151. /**
  152. * @brief Emitted when #paused changes.
  153. */
  154. void pausedChanged();
  155. /**
  156. * @brief Emitted when #looped changes.
  157. */
  158. void loopedChanged();
  159. /**
  160. * @brief Emitted when #useGameTime changes.
  161. */
  162. void useGameTimeChanged();
  163. /**
  164. * @brief Emitted when #frameDelay changes.
  165. */
  166. void frameDelayChanged();
  167. /**
  168. * @brief Emitted when #frameCount changes.
  169. */
  170. void frameCountChanged();
  171. /**
  172. * @brief Emitted when the last frame ends, or the animation has just looped.
  173. */
  174. void animationReachedEnd();
  175. /**
  176. * @brief Emitted when #pauseWhenHidden changes.
  177. */
  178. void pauseWhenHiddenChanged();
  179. protected:
  180. virtual void synchronizeForRendering(RenderList* renderList) override;
  181. virtual void computeDestTextureSize(QSizeF* textureSize) const override;
  182. virtual void computeSourceTextureRect(QRectF* textureSubRect) const override;
  183. private:
  184. void moveCurrentFrame(int frame);
  185. void updateAnimation();
  186. bool mPaused = false;
  187. bool mLooped = true;
  188. bool mUseGameTime = true;
  189. bool mPauseWhenHidden = false;
  190. bool mAtEnd = false;
  191. float mFrameDelay = 0.0f;
  192. int mFrameCount = 0;
  193. float mNormalizedInvFrameCount = 1.0f;
  194. float mFrameTimer = 0.0f;
  195. int mCurrentFrame = 0;
  196. float mCurrentPosition = 0.0f;
  197. };
  198. #endif // ANIMATEDIMAGERENDERER_H