JugiMap Framework
jmAnimationCommon.h
1 #ifndef JUGIMAP_ANIMATION_COMMON_H
2 #define JUGIMAP_ANIMATION_COMMON_H
3 
4 #include <vector>
5 #include "jmCommonFunctions.h"
6 #include "jmColor.h"
7 
8 #include "jmGlobal.h"
9 
10 
11 
12 
13 namespace jugimap{
14 
15 
16 
17 
18 class GraphicItem;
19 class AnimationTrackState;
20 class AKMeta;
21 class SourceSprite;
22 class JugiMapBinaryLoader;
23 
24 
25 
26 
27 
28 //==================================================================================
29 
30 //Do not change content of this struct!
31 struct AnimationFrame
32 {
33  GraphicItem *texture = nullptr; // LINK
34  int msTime = 100; // in milliseconds
35  Vec2f offset;
36  Vec2i flip;
37  int dataFlags = 0;
38 };
39 
40 
41 
42 //==================================================================================
43 
44 
45 struct AnimatedProperties
46 {
47 
48  Vec2f translation;
49  Vec2f scale = Vec2f(1.0f, 1.0f);
50  float rotation = 0.0f;
51  Vec2i flip;
52  bool hidden = false;
53  float alpha = 1.0f;
54  ColorRGBA colorOverlayRGBA = 0;
55  ColorOverlayBlend colorOverlayBlend = ColorOverlayBlend::NORMAL;
56  GraphicItem *graphicItem = nullptr;
57 
58  //----
59  void Reset();
60  void Append(AnimatedProperties &ap);
61 
62  //----
63  int changeFlags = 0;
64 
65 };
66 
67 
68 //==================================================================================
69 
70 
77 {
78 
79  int startDelay = 0;
80  int loopCount = 0;
81  bool loopForever = true;
82  bool repeat = false;
85  bool startAtRepeatTime = true;
86 
87 };
88 
89 
90 
95 class Animation
96 {
97 public:
98  friend class JugiMapBinaryLoader;
99  friend class SourceSprite;
100 
101 
102 
103 
104 
106  AnimationKind GetKind(){ return kind; }
107 
108 
110  SourceSprite* GetSourceSprite(){ return sourceObject; }
111 
112 
114  std::string GetName(){return name;}
115 
116 
119 
120 
122  std::vector<Parameter>& GetParameters(){return parameters;}
123 
124 
126  int GetDataFlags(){return dataFlags;}
127 
128 
130  int GetDuration(){return duration;}
131 
132 
133 
134 protected:
135  AnimationKind kind = AnimationKind::NOT_DEFINED;
136  SourceSprite * sourceObject = nullptr; // LINK to source sprite where this animation is stored
137  std::string name;
139  int duration = 0;
140 
141  // custom data
142  unsigned char dataFlags = 0;
143  std::vector<Parameter> parameters;
144 
145 
146  //----
147  Animation(SourceSprite *_sourceObject);
148  Animation(const std::string &_nameID, SourceSprite *_sourceObject);
149  Animation(const Animation &mkaSrc);
150  Animation& operator=(const Animation &mkaSrc);
151 
153  virtual ~Animation(){}
154 
155 };
156 
157 
158 
164 {
165 public:
166 
167  virtual ~AnimationInstance(){}
168 
169 
171  AnimationKind GetKind(){ return kind;}
172 
173 
177  virtual int Update(int msTimePoint, int _flags) = 0;
178 
179 
186  virtual void UpdateAnimatedSprites(bool _resetSpriteAnimatedProperties) = 0;
187 
188 
189  virtual void ResetAnimatedProperties() = 0;
190  virtual void OnPlayingStart(){}
191  virtual void OnPlayingStop(){}
192 
193 
195  Animation * GetAnimation(){ return animation; }
196 
197 
202 
203 
207  void SetCompleteLoops(bool _completeLoops){ completeLoops = _completeLoops; }
208 
209 
211  bool GetCompleteLoops(){ return completeLoops; }
212 
213 
216  void SetSpeedFactor(float _fSpeed) { fSpeed = _fSpeed; }
217 
218 
220  float GetSpeedFactor() {return fSpeed;}
221 
222 
224  void SetStartPlayTimeOffset(int _startPlayTimeOffset){ startPlayTimeOffset = _startPlayTimeOffset; }
225 
226 
228  int GetStartPlayTimeOffset(){ return startPlayTimeOffset; }
229 
230 
231 protected:
232 
233  AnimationKind kind = AnimationKind::NOT_DEFINED;
234  Animation * animation = nullptr; // LINK to related timelineAnimation
236 
237 
238  float fSpeed = 1.0;
239  bool completeLoops = true;
240  int startPlayTimeOffset = 0;
241 
242 };
243 
244 
245 
250 {
251 public:
252 
253  int Update(int msTimePoint, int _flags) override { return 0;}
254  void UpdateAnimatedSprites(bool _resetAnimatedProperties) override {}
255  void ResetAnimatedProperties() override {}
256 
257 };
258 
259 
260 
261 
262 
263 
267 {
268 public:
269 
270  //---
271 
273  AnimationInstance *GetAnimationInstance() {return animationInstance;}
274 
275 
277  int GetCurrentAnimationTime() { return msCurrentAnimationTime;}
278 
279 
281  AnimationPlayerState GetState() {return state;}
282 
283 
285  AKMeta* GetActiveMetaKey() { return activeControllerKey; }
286 
287 
289  int Play(AnimationInstance * _animationInstance);
290 
291 
293  void Pause();
294 
295 
297  void Resume();
298 
299 
303  int Stop();
304 
305 
307  int Update();
308 
309 
314 
315 
320 
321 
322 private:
324 
326 
327  int msCurrentAnimationTime = -1;
328  int msStartPlayOffset = 0;
329  int msAnimationTimeStart = -1;
330  int countLoop = 0;
331  int msRepeat = -1;
333  int msTimeStored = 0;
334 
335  AnimationInstance *animationInstance = nullptr; // LINK
336  AKMeta* activeControllerKey = nullptr; // LINK
337 
338 
339  int UpdateAnimationInstance();
340  int GetRepeatTime(int _msCurrentTime);
341 };
342 
343 
344 
354 {
355 public:
356 
357 
359  AnimationPlayer & GetPlayer(){ return player; }
360 
361 
364 
365 
368 
369 
371  AnimationPlayerState GetState() {return player.GetState(); }
372 
373 
375  AKMeta* GetActiveMetaKey() { return player.GetActiveMetaKey(); }
376 
377 
381  int Play(AnimationInstance *_animationInstance, int _flags=0);
382 
383 
385  int Update();
386 
387 
389  void Pause() { player.Pause(); }
390 
391 
393  void Resume() { player.Resume(); }
394 
395 
399  int Stop();
400 
401 
402 private:
403  AnimationPlayer player;
404  std::vector<AnimationInstance*>animationInstances;
405 
406  int PlayNextAnimationInQueue();
407 };
408 
409 
410 extern DummyNoAnimationInstance dummyNoAnimationInstance;
411 extern AnimationPlayer *activeTimelineAnimationPlayer;
412 
413 
414 }
415 
416 
417 
418 
419 #endif // EANIMATIONCOMMON_H
jugimap::AnimationQueuePlayer::GetCurrentAnimationTime
int GetCurrentAnimationTime()
Returns the current animation time in milliseconds.
Definition: jmAnimationCommon.h:367
jugimap::AnimationPlayer::Stop
int Stop()
Stop the played animation and put the player into the idle state.
Definition: jmAnimationCommon.cpp:349
jugimap::AnimationQueuePlayer
The queue animation player for playing animations via animationInstance objects.
Definition: jmAnimationCommon.h:353
jugimap::AnimationPlayer::GetAnimationBaseParameters
AnimationBaseParameters & GetAnimationBaseParameters()
Returns a reference to the animation base parameters.
Definition: jmAnimationCommon.h:319
jugimap::AnimationPlayer::GetAnimationInstance
AnimationInstance * GetAnimationInstance()
Returns the played animation instance.
Definition: jmAnimationCommon.h:273
jugimap::Animation::GetKind
AnimationKind GetKind()
Returns the kind of this animation.
Definition: jmAnimationCommon.h:106
jugimap::AnimationInstance::Update
virtual int Update(int msTimePoint, int _flags)=0
Update the animation state for the given msTimePoint.
jugimap::AnimationBaseParameters::repeat_DelayTimeEnd
int repeat_DelayTimeEnd
The repeat delay - end point time in milliseconds. The actual repeat delay is a random value between ...
Definition: jmAnimationCommon.h:84
jugimap::Animation::GetDataFlags
int GetDataFlags()
Returns the dataFlags factor of this frame animation.
Definition: jmAnimationCommon.h:126
jugimap::Animation::GetName
std::string GetName()
Returns the name of this animation.
Definition: jmAnimationCommon.h:114
jugimap::AnimationBaseParameters::repeat_DelayTimeStart
int repeat_DelayTimeStart
The repeat delay - start point time in milliseconds.
Definition: jmAnimationCommon.h:83
jugimap::Vec2i
Vec2< int > Vec2i
Vec2 struct in integer precision.
Definition: jmCommon.h:166
jugimap::ColorOverlayBlend::NORMAL
Normal mode.
jugimap::AnimationPlayerState::IDLE
The player is idle and has assigned no animation instance.
jugimap::Animation::GetDuration
int GetDuration()
Returns duration of this animation in milliseconds. This is duration of one loop.
Definition: jmAnimationCommon.h:130
jugimap::DummyNoAnimationInstance::UpdateAnimatedSprites
void UpdateAnimatedSprites(bool _resetAnimatedProperties) override
Update the animated sprites.
Definition: jmAnimationCommon.h:254
jugimap::Animation::~Animation
virtual ~Animation()
Destructor.
Definition: jmAnimationCommon.h:153
jugimap::AnimationInstance
The base animation instance class.
Definition: jmAnimationCommon.h:163
jugimap::Animation::GetBaseParameters
AnimationBaseParameters & GetBaseParameters()
Returns a reference to the base parameters of this animation.
Definition: jmAnimationCommon.h:118
jugimap::AnimationQueuePlayer::GetActiveMetaKey
AKMeta * GetActiveMetaKey()
Returns the active meta key of the played animation or nullptr if none.
Definition: jmAnimationCommon.h:375
jugimap::AnimationInstance::UpdateAnimatedSprites
virtual void UpdateAnimatedSprites(bool _resetSpriteAnimatedProperties)=0
Update the animated sprites.
jugimap::AnimationPlayer::GetState
AnimationPlayerState GetState()
Returns the state of this player.
Definition: jmAnimationCommon.h:281
jugimap::AnimationBaseParameters::startAtRepeatTime
bool startAtRepeatTime
Start the animation after the repeat delay (overrides the starting delay).
Definition: jmAnimationCommon.h:85
jugimap::AnimationQueuePlayer::GetAnimationInstance
AnimationInstance * GetAnimationInstance()
Returns the played animation instance.
Definition: jmAnimationCommon.h:363
jugimap::AnimationQueuePlayer::Pause
void Pause()
Pause this player.
Definition: jmAnimationCommon.h:389
jugimap::AnimationInstance::GetStartPlayTimeOffset
int GetStartPlayTimeOffset()
Returns the animation starting time point offset of this animation instance.
Definition: jmAnimationCommon.h:228
jugimap::Animation::GetParameters
std::vector< Parameter > & GetParameters()
Returns a reference to the vector of stored parameters in this animation.
Definition: jmAnimationCommon.h:122
jugimap::AnimationBaseParameters::loopCount
int loopCount
The number of loops.
Definition: jmAnimationCommon.h:80
jugimap::Animation
The base animation class.
Definition: jmAnimationCommon.h:95
jugimap::AnimationInstance::GetAnimation
Animation * GetAnimation()
Returns the animation object of this animation instance.
Definition: jmAnimationCommon.h:195
jugimap::Vec2f
Vec2< float > Vec2f
Vec2 struct in float precision.
Definition: jmCommon.h:167
jugimap::AnimationPlayer::Update
int Update()
Update the player. This command must be called in every update loop of the game logic.
Definition: jmAnimationCommon.cpp:191
jugimap::AnimationInstance::SetStartPlayTimeOffset
void SetStartPlayTimeOffset(int _startPlayTimeOffset)
Set the animation starting time point offset in milliseconds.
Definition: jmAnimationCommon.h:224
jugimap::AnimationPlayerState
AnimationPlayerState
The states of animation player.
Definition: jmGlobal.h:178
jugimap::AnimationBaseParameters::loopForever
bool loopForever
The animation loops forever.
Definition: jmAnimationCommon.h:81
jugimap::AnimationInstance::GetBaseParameters
AnimationBaseParameters & GetBaseParameters()
Returns the base animation parameters of this animation instance.
Definition: jmAnimationCommon.h:201
jugimap::AnimationPlayer::Play
int Play(AnimationInstance *_animationInstance)
Play the given *_animationInstance*.
Definition: jmAnimationCommon.cpp:126
jugimap::Animation::GetSourceSprite
SourceSprite * GetSourceSprite()
Returns the source sprite to which this animation belongs.
Definition: jmAnimationCommon.h:110
jugimap::AnimationPlayer::GetActiveMetaKey
AKMeta * GetActiveMetaKey()
Returns the active meta key of the played animation or nullptr if none.
Definition: jmAnimationCommon.h:285
jugimap::AnimationBaseParameters::startDelay
int startDelay
The starting dealay of animation in milliseconds.
Definition: jmAnimationCommon.h:79
jugimap::DummyNoAnimationInstance::Update
int Update(int msTimePoint, int _flags) override
Update the animation state for the given msTimePoint.
Definition: jmAnimationCommon.h:253
jugimap::AnimationBaseParameters
The animation base parameters of Animation objects.
Definition: jmAnimationCommon.h:76
jugimap::AnimationQueuePlayer::Stop
int Stop()
Stop the played animation, clear the queue and put the player into the idle state.
Definition: jmAnimationCommon.cpp:460
jugimap::AnimationQueuePlayer::Play
int Play(AnimationInstance *_animationInstance, int _flags=0)
Play the given *_animationInstance*.
Definition: jmAnimationCommon.cpp:403
jugimap::AnimationInstance::GetCompleteLoops
bool GetCompleteLoops()
Returns the completeLoops parameter of this animation instance.
Definition: jmAnimationCommon.h:211
jugimap::AnimationQueuePlayer::GetState
AnimationPlayerState GetState()
Returns the state of this player.
Definition: jmAnimationCommon.h:371
jugimap::AnimationBaseParameters::repeat
bool repeat
The animation should repeats (if it doesn't loops forever).
Definition: jmAnimationCommon.h:82
jugimap::SourceSprite
The SourceSprite class defines the source sprite from JugiMap Editor.
Definition: jmSourceGraphics.h:221
jugimap::AnimationQueuePlayer::Resume
void Resume()
Resume this player.
Definition: jmAnimationCommon.h:393
jugimap::AnimationPlayer::GetCurrentAnimationTime
int GetCurrentAnimationTime()
Returns the current animation time in milliseconds.
Definition: jmAnimationCommon.h:277
jugimap::AnimationPlayer
The standard animation player for playing animations via AnimationInstance objects.
Definition: jmAnimationCommon.h:266
jugimap::DummyNoAnimationInstance
The DummyNoAnimationInstance is a special animation instance which can be used with animation players...
Definition: jmAnimationCommon.h:249
jugimap::AnimationQueuePlayer::GetPlayer
AnimationPlayer & GetPlayer()
Returns the wrapped AnimationPlayer object.
Definition: jmAnimationCommon.h:359
jugimap::AnimationPlayer::Pause
void Pause()
Pause this player.
Definition: jmAnimationCommon.cpp:315
jugimap::AnimationKind
AnimationKind
The kinds of animation. Used as identifier for Animation objects.
Definition: jmGlobal.h:145
jugimap::JugiMapBinaryLoader
The JugiMapBinaryLoader class loads data from jugimap binary files (.jmb).
Definition: jmMapBinaryLoader.h:37
jugimap::AnimationPlayer::SetParametersForCompletingLoop
void SetParametersForCompletingLoop()
This command modify the player parameters so that it will go into the AnimationPlayerState:STALLED st...
Definition: jmAnimationCommon.cpp:373
jugimap::AnimationInstance::GetKind
AnimationKind GetKind()
Returns the kind of this animation.
Definition: jmAnimationCommon.h:171
jugimap::AnimationInstance::GetSpeedFactor
float GetSpeedFactor()
Returns the speed factor of this animation instance.
Definition: jmAnimationCommon.h:220
jugimap::AnimationInstance::SetCompleteLoops
void SetCompleteLoops(bool _completeLoops)
Set a parameter which indicates that the animation should finish its current loop before ending.
Definition: jmAnimationCommon.h:207
jugimap::ColorOverlayBlend
ColorOverlayBlend
The blend modes for the shader based pixel blending which simulate photoshop blending modes.
Definition: jmGlobal.h:112
jugimap::AnimationPlayer::Resume
void Resume()
Resume this player.
Definition: jmAnimationCommon.cpp:332
jugimap::AnimationInstance::SetSpeedFactor
void SetSpeedFactor(float _fSpeed)
Set the speed factor of animation playback. The value 1.0 is the default speed; higher values means f...
Definition: jmAnimationCommon.h:216
jugimap::AnimationQueuePlayer::Update
int Update()
Update the player. This command must be called in every update loop of the game logic.
Definition: jmAnimationCommon.cpp:476