UGDK  0.5.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
animationplayer.h
Go to the documentation of this file.
1 #ifndef UGDK_ACTION_ANIMATIONPLAYER_H_
2 #define UGDK_ACTION_ANIMATIONPLAYER_H_
3 
7 
8 #include <string>
9 #include <vector>
10 #include <functional>
11 
12 namespace ugdk {
13 namespace action {
14 
15 template<class Frame>
16 class AnimationPlayer : public MediaPlayer {
17  public:
20  using FrameChangedCallback = std::function<void(const Frame&)>;
21 
23  : current_animation_(nullptr)
24  , elapsed_time_(0.0)
25  , table_(table)
26  , notified_(false)
27  {}
28 
30  frame_change_callback_ = callback;
31  }
32 
33  const Animation* current_animation() const {
34  return current_animation_;
35  }
36 
37  const Frame& current_animation_frame() const {
38  return *current_animation_->frames()[current_frame_];
39  }
40 
41  void Update(double dt) {
42  if (!current_animation_) return;
43  elapsed_time_ += dt;
44 
45  double frame_period = current_animation_frame().period();
46  if (elapsed_time_ >= frame_period) {
47  elapsed_time_ -= frame_period;
48  ChangeToNextFrame();
49  }
50  }
51 
54  notified_ = false;
55  elapsed_time_ = 0.0;
56  current_frame_ = 0;
57  ExecuteFrameChangeCallback();
58  }
59 
61 
62  void Select(const std::string& name) {
63  if(table_)
64  this->set_current_animation(table_->Search(name));
65  }
66 
68 
69  void Select(int index) {
70  if(table_)
71  this->set_current_animation(table_->Get(index));
72  }
73 
75  void Refresh() {
76  if (current_animation_)
77  ExecuteFrameChangeCallback();
78  }
79 
80  private:
81  const Animation* current_animation_;
82  int current_frame_;
83  double elapsed_time_;
84  const AnimationTable* table_;
85  FrameChangedCallback frame_change_callback_;
86  bool notified_;
87 
88  void set_current_animation(const Animation* anim) {
89  auto previous_anim = current_animation_;
90  current_animation_ = anim;
91  if (anim != previous_anim)
93  }
94 
95  void ExecuteFrameChangeCallback() {
96  if (frame_change_callback_)
97  frame_change_callback_(current_animation_frame());
98  }
99 
100  void ChangeToNextFrame() {
101  bool notify = false;
102 
103  ++current_frame_;
104  if(current_frame_ >= current_animation_->frames().size()) {
105  if (current_animation_->repeat() == Animation::AnimationRepeat::LOOP) {
106  current_frame_ = 0;
107  notified_ = false;
108  }
109  else {
110  --current_frame_;
111  }
112  notify = true;
113  }
114  ExecuteFrameChangeCallback();
115 
116  if (notify && !notified_) {
118  notified_ = true;
119  }
120  }
121 };
122 
123 } /* namespace action */
124 } /* namespace ugdk */
125 
126 #endif /* UGDK_ACTION_SPECIALIZEDANIMATIONMANAGER_H_ */
127 
128 
const Frame & current_animation_frame() const
Definition: animationplayer.h:37
Definition: indexabletable.h:15
structure::IndexableTable< Animation > AnimationTable
Definition: animationplayer.h:19
const Animation * current_animation() const
Definition: animationplayer.h:33
Definition: mediaplayer.h:30
Definition: animation.h:11
void Refresh()
Calls the frame changed callback with the current frame.
Definition: animationplayer.h:75
Definition: animationplayer.h:16
Vector & frames()
Definition: animation.h:31
T * Get(int index) const
Instant access to an element, using a pre-generated ID.
Definition: indexabletable.h:58
void RestartAnimation()
Restarts the current animation from the first frame.
Definition: animationplayer.h:53
AnimationPlayer(const AnimationTable *table)
Definition: animationplayer.h:22
void set_frame_change_callback(const FrameChangedCallback &callback)
Definition: animationplayer.h:29
Definition: animation.h:15
T * Search(const std::string &name) const
Searches for the element with the given name.
Definition: indexabletable.h:47
void Select(const std::string &name)
Change the current animation to a new animation from the previously selected AnimationSet.
Definition: animationplayer.h:62
std::function< void(const Frame &)> FrameChangedCallback
Definition: animationplayer.h:20
void Select(int index)
Change the current animation to a new animation from the previo2usly selected AnimationSet.
Definition: animationplayer.h:69
AnimationRepeat repeat() const
Definition: animation.h:34
void Update(double dt)
Definition: animationplayer.h:41