VoltAir
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
AnimatedImageRenderer.h
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 
17 #ifndef ANIMATEDIMAGERENDERER_H
18 #define ANIMATEDIMAGERENDERER_H
19 
20 #include "ImageRenderer.h"
21 
34 // TODO: Supporting grid layouts for texture sheets would allow larger animations to fit into the
35 // texture sheets within the size limitations of more hardware.
37  Q_OBJECT
41  Q_PROPERTY(bool paused READ isPaused WRITE setPaused NOTIFY pausedChanged)
46  Q_PROPERTY(bool looped READ isLooped WRITE setLooped NOTIFY loopedChanged)
50  Q_PROPERTY(bool useGameTime READ isUseGameTime WRITE setUseGameTime NOTIFY useGameTimeChanged)
54  Q_PROPERTY(float frameDelay READ getFrameDelay WRITE setFrameDelay NOTIFY frameDelayChanged)
58  Q_PROPERTY(int frameCount READ getFrameCount WRITE setFrameCount NOTIFY frameCountChanged)
62  Q_PROPERTY(int currentFrame READ getCurrentFrame WRITE setCurrentFrame)
73  Q_PROPERTY(bool pauseWhenHidden READ getPauseWhenHidden WRITE setPauseWhenHidden
75 
76 public:
81  explicit AnimatedImageRenderer(QQuickItem* parent = nullptr);
82 
86  bool isPaused() const { return mPaused; }
91  void setPaused(bool value);
95  bool isLooped() const { return mLooped; }
100  void setLooped(bool value);
104  bool isUseGameTime() const { return mUseGameTime; }
109  void setUseGameTime(bool value);
113  float getFrameDelay() const { return mFrameDelay; }
118  void setFrameDelay(float value);
122  int getFrameCount() const { return mFrameCount; }
127  void setFrameCount(int value);
131  int getCurrentFrame() const { return mCurrentFrame; }
136  void setCurrentFrame(int value);
140  float getCurrentPosition() const { return mCurrentPosition; }
145  void setCurrentPosition(float value);
149  bool getPauseWhenHidden() const { return mPauseWhenHidden; }
154  void setPauseWhenHidden(bool value);
155 
156 signals:
160  void pausedChanged();
164  void loopedChanged();
168  void useGameTimeChanged();
172  void frameDelayChanged();
176  void frameCountChanged();
180  void animationReachedEnd();
184  void pauseWhenHiddenChanged();
185 
186 protected:
187  virtual void synchronizeForRendering(RenderList* renderList) override;
188  virtual void computeDestTextureSize(QSizeF* textureSize) const override;
189  virtual void computeSourceTextureRect(QRectF* textureSubRect) const override;
190 
191 private:
192  void moveCurrentFrame(int frame);
193  void updateAnimation();
194 
195  bool mPaused = false;
196  bool mLooped = true;
197  bool mUseGameTime = true;
198  bool mPauseWhenHidden = false;
199  bool mAtEnd = false;
200  float mFrameDelay = 0.0f;
201  int mFrameCount = 0;
202  float mNormalizedInvFrameCount = 1.0f;
203  float mFrameTimer = 0.0f;
204  int mCurrentFrame = 0;
205  float mCurrentPosition = 0.0f;
206 };
207 
208 #endif // ANIMATEDIMAGERENDERER_H
int getCurrentFrame() const
Returns currentFrame.
Definition: AnimatedImageRenderer.h:131
void setFrameDelay(float value)
Sets frameDelay.
void useGameTimeChanged()
Emitted when useGameTime changes.
virtual void synchronizeForRendering(RenderList *renderList) override
Synchronize this Graphic for rendering, adding any RenderNodes to renderList, and buffering any data ...
bool pauseWhenHidden
Don't animate when this image is off screen or visible is false.
Definition: AnimatedImageRenderer.h:74
void setPauseWhenHidden(bool value)
Sets pauseWhenHidden.
Graphic based on ImageRenderer which displays a frame animation from a texture sheet.
Definition: AnimatedImageRenderer.h:36
int getFrameCount() const
Returns frameCount.
Definition: AnimatedImageRenderer.h:122
void setLooped(bool value)
Sets looped.
bool isPaused() const
Returns paused.
Definition: AnimatedImageRenderer.h:86
int currentFrame
Index of the current frame, zero being the first frame.
Definition: AnimatedImageRenderer.h:62
void loopedChanged()
Emitted when looped changes.
void setUseGameTime(bool value)
Sets useGameTime.
bool isLooped() const
Returns looped.
Definition: AnimatedImageRenderer.h:95
bool isUseGameTime() const
Returns useGameTime.
Definition: AnimatedImageRenderer.h:104
void pauseWhenHiddenChanged()
Emitted when pauseWhenHidden changes.
bool paused
Pause this animation at the current frame.
Definition: AnimatedImageRenderer.h:41
int frameCount
Total number of frames in the texture sheet.
Definition: AnimatedImageRenderer.h:58
float getCurrentPosition() const
Returns currentPosition.
Definition: AnimatedImageRenderer.h:140
void setPaused(bool value)
Sets paused.
virtual void computeSourceTextureRect(QRectF *textureSubRect) const override
Computes the subrectangle within the source Texture to render.
float frameDelay
Delay between frames, in seconds.
Definition: AnimatedImageRenderer.h:54
bool getPauseWhenHidden() const
Returns pauseWhenHidden.
Definition: AnimatedImageRenderer.h:149
void frameCountChanged()
Emitted when frameCount changes.
void frameDelayChanged()
Emitted when frameDelay changes.
float getFrameDelay() const
Returns frameDelay.
Definition: AnimatedImageRenderer.h:113
virtual void computeDestTextureSize(QSizeF *textureSize) const override
Computes the size to treat the source Texture, for purposes of computing destination rectangle size...
void animationReachedEnd()
Emitted when the last frame ends, or the animation has just looped.
bool useGameTime
Animate this image at the speed of the game, pausing when the game is paused.
Definition: AnimatedImageRenderer.h:50
List of RenderNodes, sorted by z-depth for rendering.
Definition: RenderList.h:36
Graphic which displays a texture as an image.
Definition: ImageRenderer.h:38
void setCurrentPosition(float value)
Sets currentPosition.
bool looped
Loop this animation, restarting it at the first frame after the last frame has finished displaying...
Definition: AnimatedImageRenderer.h:46
QObject * parent() const
void setCurrentFrame(int value)
Sets currentFrame.
float currentPosition
Seek position into the animation, with 0.0f being the start, and 1.0f being the end.
Definition: AnimatedImageRenderer.h:67
void setFrameCount(int value)
Sets frameCount.
void pausedChanged()
Emitted when paused changes.