Gorgon Game Engine
Animation.h
Go to the documentation of this file.
1 
4 #pragma once
5 
6 #include <stdexcept>
7 #include <iostream>
8 
9 #include "Event.h"
10 #include "Containers/Collection.h"
11 
12 namespace Gorgon {
14  namespace Animation {
15 
16  class Base;
17  class ControllerBase;
18 
26  class Governor {
27  friend class ControllerBase;
28  public:
29 
31  virtual ~Governor();
32 
34  virtual void Activate() {
35  active = this;
36  }
37 
41  virtual void Animate() const;
42 
44  static Governor &Default() {
45  static Governor def;
46 
47  return def;
48  }
49 
52  static Governor &Active() {
53  if(active)
54  return *active;
55  else
56  return Default();
57  }
58 
59  private:
60  static Governor *active;
62  };
63 
66  public:
69 
71  explicit ControllerBase(Governor &governor);
72 
74  virtual ~ControllerBase();
75 
77  virtual void Progress(unsigned timepassed) = 0;
78 
80  virtual void Add(Base &animation);
81 
83  virtual void Remove(Base &animation);
84 
86  virtual void Delete(Base &animation);
87 
89  virtual unsigned GetProgress() const = 0;
90 
96  virtual bool IsControlled() const = 0;
97 
100  void AutoDestruct() { collectable=true; }
101 
104  void Keep() { collectable=false; }
105 
108  virtual void Reset() = 0;
109 
111  virtual void SetGovernor(Governor &governor);
112 
113  protected:
115  bool collectable = false;
116 
117  Governor *governor = nullptr;
118 
121  };
122 
126  class Timer : public ControllerBase {
127  public:
128 
129  virtual ~Timer() { }
130 
132  virtual void Progress(unsigned timepassed) override final;
133 
135  virtual void Reset() override {
136  progress=0;
137  }
138 
140  virtual void SetProgress(unsigned progress) {
141  this->progress=progress;
142  }
143 
145  virtual unsigned GetProgress() const override final {
146  return progress;
147  }
148 
154  virtual bool IsControlled() const override final {
155  return false;
156  }
157 
158  protected:
160  unsigned progress = 0;
161  };
162 
169  class Controller : public ControllerBase {
170  public:
171 
173  Controller(double progress = 0.0);
174 
175  virtual ~Controller() {}
176 
179 
181  virtual void Progress(unsigned timepassed) override final;
182 
184  virtual void SetProgress(unsigned progress) { this->progress=progress; }
185 
189  void SetProgress(double progress) { this->progress=progress; }
190 
193  virtual void Reset() override;
195 
198 
203  virtual void Play();
204 
208  virtual void Loop() { Play(); islooping=true; }
209 
211  virtual void Pause();
212 
214  virtual void Resume() { ispaused=false; }
215 
219  virtual void SetSpeed(float speed) { this->speed=speed; }
220 
222  virtual void Reverse() { speed=-speed; }
223 
226  virtual void SetLength(unsigned length) { this->length=length; }
228 
231 
233  virtual float GetSpeed() { return speed; }
234 
237  bool IsPaused() const { return ispaused; }
238 
241  bool IsFinished() const { return isfinished; }
242 
245  bool IsPlaying() const { return !ispaused && !isfinished; }
246 
249  bool IsLooping() const { return (speed>=0 ? islooping : islooping && length!=0); }
250 
251  virtual bool IsControlled() const override final { return true; }
253 
256 
261 
262  protected:
264  bool ispaused = false;
265 
267  bool islooping = false;
268 
270  float speed = 1.0f;
271 
273  bool isfinished = false;
274 
276  double progress = 0.0;
277 
279  unsigned length=0;
280  };
281 
283  class Provider {
284  public:
286 
287 
289  virtual ~Provider() { }
290 
293  virtual Provider &MoveOutProvider() = 0;
294 
296  virtual Base &CreateAnimation(ControllerBase &timer) const = 0;
297 
301  virtual Base &CreateAnimation(bool create=true) const = 0;
302  };
303 
306  class Base {
307  friend class ControllerBase;
308  public:
312  }
313 
315  Base(const Base &base) {
316  if(base.HasController())
318  }
319 
325  explicit Base(bool create=false) {
326  if(create) {
327  auto timer = new Timer;
328  timer->AutoDestruct();
329  SetController(*timer);
330  }
331  }
332 
335  if(&controller==this->controller) return;
336  if(this->controller) {
337  this->controller->Remove(*this);
338  }
339  this->controller=&controller;
340  controller.Add(*this);
341  }
342 
344  virtual bool HasController() const { return controller!=nullptr; }
345 
347  virtual ControllerBase &GetController() const {
348 #ifndef NDEBUG
349  if(!controller) {
350  throw std::runtime_error("Animation does not have a controller");
351  }
352 #endif
353  return *controller;
354  }
355 
357  virtual void RemoveController() {
358  if(controller) {
359  controller->Remove(*this);
360  }
361  controller=nullptr;
362  }
363 
369  virtual bool Progress(unsigned &leftover) = 0;
370 
374  virtual int GetDuration() const = 0;
375 
379  virtual void DeleteAnimation() const {
380  delete this;
381  }
382 
383  protected:
385  virtual ~Base();
386 
389  };
390  }
391 }
Gorgon::Animation::Base::GetDuration
virtual int GetDuration() const =0
Returns the duration of the animation if it is a known apriori.
Gorgon::Animation::Timer::SetProgress
virtual void SetProgress(unsigned progress)
Sets the progress of the timer to the given value.
Definition: Animation.h:140
Gorgon::Animation::Base::Base
Base(ControllerBase &controller)
Sets the controller for this animation to the given controller.
Definition: Animation.h:310
Gorgon::Animation::Controller::Resume
virtual void Resume()
Resumes the controller. This method will not have any effect if the animation is finished.
Definition: Animation.h:214
Gorgon::Animation::ControllerBase::~ControllerBase
virtual ~ControllerBase()
Destructor.
Definition: Animation.cpp:54
Gorgon::Animation::Governor::~Governor
virtual ~Governor()
Destroys this governor. If it is the active governor, default governor will be activated.
Definition: Animation.cpp:21
Gorgon::Animation::Controller::IsPlaying
bool IsPlaying() const
Returns whether the controller is playing its animations right now.
Definition: Animation.h:245
Gorgon::Animation::Controller::SetSpeed
virtual void SetSpeed(float speed)
Changes the speed of the controller.
Definition: Animation.h:219
Gorgon::Animation::Controller::islooping
bool islooping
Looping state.
Definition: Animation.h:267
Gorgon::Animation::ControllerBase::governor
Governor * governor
Definition: Animation.h:117
Gorgon::Animation::Controller::progress
double progress
Floating point progress to avoid precision loss due to speed.
Definition: Animation.h:276
Gorgon::Animation::ControllerBase::Reset
virtual void Reset()=0
Resets the animation to the start.
Gorgon::Event
This class provides event mechanism.
Definition: Event.h:134
Collection.h
contains collection, a vector of references.
Gorgon::Animation::Base::Base
Base(bool create=false)
This constructor creates a new controller depending on the create parameter.
Definition: Animation.h:325
Gorgon::Animation::ControllerBase::GetProgress
virtual unsigned GetProgress() const =0
Returns the current progress of the timer.
Gorgon::Animation::Controller::IsPaused
bool IsPaused() const
Returns whether the controller is paused.
Definition: Animation.h:237
Gorgon::Animation::ControllerBase
Controllers are required to progress animations.
Definition: Animation.h:65
Gorgon::Animation::Controller::FinishedEvent
Event< Controller > FinishedEvent
Will be fired when the controller reaches the finished state.
Definition: Animation.h:259
Gorgon::Animation::Controller::Controller
Controller(double progress=0.0)
Default constructor.
Definition: Animation.cpp:114
Gorgon::Animation::ControllerBase::Remove
virtual void Remove(Base &animation)
Removes the given animation.
Definition: Animation.cpp:78
Gorgon::Animation::Controller::ispaused
bool ispaused
Paused state.
Definition: Animation.h:264
Gorgon::Animation::Timer::~Timer
virtual ~Timer()
Definition: Animation.h:129
Gorgon::Animation::Provider::CreateAnimation
virtual Base & CreateAnimation(bool create=true) const =0
This function should create an animation and depending on the create parameter, it should create a ti...
Gorgon::Animation::Controller::length
unsigned length
Length of the animations controlled by this controller.
Definition: Animation.h:279
Gorgon::Animation::Controller::Progress
virtual void Progress(unsigned timepassed) override final
Progresses this controller by the given time.
Definition: Animation.cpp:118
Gorgon::Animation::ControllerBase::Delete
virtual void Delete(Base &animation)
Deletes the given animation.
Definition: Animation.cpp:90
Gorgon::Animation::ControllerBase::IsControlled
virtual bool IsControlled() const =0
This method allows clients to determine if the progress is controlled.
Gorgon::Animation::Timer::GetProgress
virtual unsigned GetProgress() const override final
Returns the current progress of the timer.
Definition: Animation.h:145
Gorgon::Animation::Controller::SetLength
virtual void SetLength(unsigned length)
Informs controller about the length of the animations its controlling.
Definition: Animation.h:226
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Animation::Timer::Progress
virtual void Progress(unsigned timepassed) override final
Progresses this timer by moving the timer timepassed milliseconds forwards.
Definition: Animation.cpp:98
Gorgon::Animation::Provider
This interface marks a class as animation provider.
Definition: Animation.h:283
Gorgon::Animation::Controller::GetSpeed
virtual float GetSpeed()
Returns the current speed of the controller.
Definition: Animation.h:233
Gorgon::Animation::Base
This is the base class for all animations.
Definition: Animation.h:306
Gorgon::Animation::ControllerBase::Progress
virtual void Progress(unsigned timepassed)=0
Progresses this timer by moving the timer timepassed milliseconds forwards.
Gorgon::Animation::Controller::speed
float speed
Current speed.
Definition: Animation.h:270
Gorgon::Animation::Controller
This class allows finer control for the animations, allowing loop, stopping at the end,...
Definition: Animation.h:169
Gorgon::Animation::Controller::SetProgress
void SetProgress(double progress)
Sets the current progress of the controller.
Definition: Animation.h:189
Gorgon::Containers::Collection
Collection is a container for reference typed objects.
Definition: Collection.h:21
Gorgon::Animation::Timer::IsControlled
virtual bool IsControlled() const override final
This method allows clients to determine if the progress is controlled.
Definition: Animation.h:154
Gorgon::Animation::Controller::IsFinished
bool IsFinished() const
Whether the controller is finished either by reaching to the end while the speed is positive or reach...
Definition: Animation.h:241
Gorgon::Animation::Controller::Pause
virtual void Pause()
Pauses the controller, until a Resume or Reset is issued.
Definition: Animation.cpp:212
Gorgon::Animation::Controller::Loop
virtual void Loop()
Starts this controller in looping mode.
Definition: Animation.h:208
Gorgon::Animation::Governor
This class governs the progress of animations.
Definition: Animation.h:26
Gorgon::Animation::Timer::progress
unsigned progress
Amount of time passed since the start of the animation.
Definition: Animation.h:160
Gorgon::Resource::GID::Animation
constexpr Type Animation
Definition: GID.h:187
Gorgon::Animation::ControllerBase::collectable
bool collectable
Whether this controller should be collected by the garbage collector when its task is finished.
Definition: Animation.h:115
Gorgon::Animation::Controller::IsControlled
virtual bool IsControlled() const override final
This method allows clients to determine if the progress is controlled.
Definition: Animation.h:251
Gorgon::Animation::Governor::Animate
virtual void Animate() const
Animates the animations within this governor.
Definition: Animation.cpp:35
Gorgon::Animation::Controller::Reset
virtual void Reset() override
Resets the controller to start from the beginning.
Definition: Animation.cpp:204
Gorgon::Animation::Governor::Activate
virtual void Activate()
Activates this governor, replacing current one.
Definition: Animation.h:34
Gorgon::Animation::Base::~Base
virtual ~Base()
Virtual destructor.
Definition: Animation.cpp:216
Gorgon::Animation::Base::DeleteAnimation
virtual void DeleteAnimation() const
Deletes this animation.
Definition: Animation.h:379
Gorgon::Animation::Timer
This class is the most basic controller and does not support any operations.
Definition: Animation.h:126
Gorgon::Animation::Controller::Reverse
virtual void Reverse()
Reverses the animation direction by negating the speed.
Definition: Animation.h:222
Gorgon::Animation::Controller::Play
virtual void Play()
Starts this controller to run once.
Definition: Animation.cpp:172
Event.h
contains event distribution mechanism
Gorgon::Animation::Provider::CreateAnimation
virtual Base & CreateAnimation(ControllerBase &timer) const =0
This function should create a new animation with the given controller.
Gorgon::Animation::ControllerBase::Add
virtual void Add(Base &animation)
This function attaches the given animation to this controller.
Definition: Animation.cpp:73
Gorgon::Animation::ControllerBase::Keep
void Keep()
Resets the flag that will automatically destroy this controller whenever it has no animations left to...
Definition: Animation.h:104
Gorgon::Animation::Timer::Reset
virtual void Reset() override
Resets the timer, basically starting the animation from the start.
Definition: Animation.h:135
Gorgon::Animation::Base::HasController
virtual bool HasController() const
Returns whether this animation has a controller.
Definition: Animation.h:344
Gorgon::Animation::ControllerBase::ControllerBase
ControllerBase()
Default constructor.
Definition: Animation.cpp:44
Gorgon::Animation::Base::RemoveController
virtual void RemoveController()
Removes the controller of this animation.
Definition: Animation.h:357
Gorgon::Animation::Controller::~Controller
virtual ~Controller()
Definition: Animation.h:175
Gorgon::Animation::Base::Progress
virtual bool Progress(unsigned &leftover)=0
This function should progress the animation.
Gorgon::Animation::Controller::IsLooping
bool IsLooping() const
Checks whether the controller is in loop mode.
Definition: Animation.h:249
Gorgon::Animation::Governor::Default
static Governor & Default()
Returns the default governor.
Definition: Animation.h:44
Gorgon::Animation::Governor::Active
static Governor & Active()
Returns the current governor.
Definition: Animation.h:52
Gorgon::Animation::Base::SetController
virtual void SetController(ControllerBase &controller)
Sets the controller to the given controller.
Definition: Animation.h:334
Gorgon::Animation::ControllerBase::SetGovernor
virtual void SetGovernor(Governor &governor)
Changes the governor of this controller.
Definition: Animation.cpp:63
Gorgon::Animation::Provider::MoveOutProvider
virtual Provider & MoveOutProvider()=0
This function moves this animation provider into a new provider.
Gorgon::Animation::Controller::SetProgress
virtual void SetProgress(unsigned progress)
Sets the current progress of the controller.
Definition: Animation.h:184
Gorgon::Animation::ControllerBase::animations
Containers::Collection< Base > animations
List of animations this controller holds.
Definition: Animation.h:120
Gorgon::Animation::Base::controller
ControllerBase * controller
Controller of this animation.
Definition: Animation.h:388
Gorgon::Animation::Base::Base
Base(const Base &base)
Copies the animation.
Definition: Animation.h:315
Gorgon::Animation::ControllerBase::AutoDestruct
void AutoDestruct()
Set a flag that will automatically destroy this controller whenever it has no animations left to cont...
Definition: Animation.h:100
Gorgon::Animation::Controller::isfinished
bool isfinished
Whether the controller is finished.
Definition: Animation.h:273
Gorgon::Animation::Provider::~Provider
virtual ~Provider()
Virtual destructor.
Definition: Animation.h:289
Gorgon::Animation::Base::GetController
virtual ControllerBase & GetController() const
Returns the controller of this animation.
Definition: Animation.h:347