]> git.tdb.fi Git - libs/gl.git/blob - source/animation/animationplayer.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / animation / animationplayer.cpp
1 #include <msp/core/algorithm.h>
2 #include "animatedobject.h"
3 #include "animationplayer.h"
4 #include "armature.h"
5 #include "programdata.h"
6
7 using namespace std;
8
9 namespace Msp {
10 namespace GL {
11
12 AnimationPlayer::Target &AnimationPlayer::get_slot(Placeable &obj)
13 {
14         auto i = objects.find(&obj);
15         if(i!=objects.end())
16                 return i->second;
17
18         return objects.insert(make_pair(&obj, Target(obj))).first->second;
19 }
20
21 AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked, float speed)
22 {
23         Target &target = get_slot(obj);
24         if(!stacked)
25         {
26                 target.animations.clear();
27                 target.base_matrix = Matrix();
28         }
29         else if(target.animations.empty())
30                 target.base_matrix = *obj.get_matrix();
31         target.stacked = stacked;
32         // TODO check for incompatible armature
33         target.armature = anim.get_armature();
34         target.animations.emplace_back(anim, speed);
35         return target;
36 }
37
38 void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim, float speed)
39 {
40         Target &target = play_(obj, anim, false, speed);
41         target.object = &obj;
42 }
43
44 void AnimationPlayer::play(Placeable &obj, const Animation &anim, float speed)
45 {
46         play_(obj, anim, false, speed);
47 }
48
49 void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim, float speed)
50 {
51         Target &target = play_(obj, anim, true, speed);
52         target.object = &obj;
53 }
54
55 void AnimationPlayer::play_stacked(Placeable &obj, const Animation &anim, float speed)
56 {
57         play_(obj, anim, true, speed);
58 }
59
60 unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
61 {
62         auto i = objects.find(&obj);
63         return (i!=objects.end() ? i->second.animations.size() : 0);
64 }
65
66 void AnimationPlayer::observe_events(AnimatedObject &obj, AnimationEventObserver &observer)
67 {
68         Target &target = get_slot(obj);
69         if(find(target.event_observers, &observer)==target.event_observers.end())
70                 target.event_observers.push_back(&observer);
71 }
72
73 void AnimationPlayer::unobserve_events(AnimatedObject &obj, AnimationEventObserver &observer)
74 {
75         auto i = objects.find(&obj);
76         if(i==objects.end())
77                 return;
78
79         auto j = find(i->second.event_observers, &observer);
80         if(j!=i->second.event_observers.end())
81                 i->second.event_observers.erase(j);
82 }
83
84 void AnimationPlayer::unobserve_events(AnimationEventObserver &observer)
85 {
86         for(auto &kvp: objects)
87         {
88                 auto j = find(kvp.second.event_observers, &observer);
89                 if(j!=kvp.second.event_observers.end())
90                         kvp.second.event_observers.erase(j);
91         }
92 }
93
94 void AnimationPlayer::stop(Placeable &obj)
95 {
96         objects.erase(&obj);
97 }
98
99 void AnimationPlayer::stop(Placeable &obj, const Animation &anim)
100 {
101         auto i = objects.find(&obj);
102         if(i==objects.end())
103                 return;
104
105         auto j = find_member(i->second.animations, &anim, &PlayingAnimation::animation);
106         if(j!=i->second.animations.end())
107                 i->second.animations.erase(j);
108
109         if(i->second.animations.empty())
110                 objects.erase(i);
111 }
112
113 void AnimationPlayer::tick(const Time::TimeDelta &dt)
114 {
115         for(auto i=objects.begin(); i!=objects.end(); )
116         {
117                 if(i->second.stacked)
118                         tick_stacked(i->second, dt);
119                 else if(!i->second.animations.empty())
120                         tick_single(i->second, dt);
121
122                 if(i->second.animations.empty() && i->second.event_observers.empty())
123                         objects.erase(i++);
124                 else
125                         ++i;
126         }
127 }
128
129 void AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt)
130 {
131         PlayingAnimation &anim = target.animations.front();
132         anim.iterator += dt*anim.speed;
133         target.placeable.set_matrix(anim.iterator.get_matrix());
134
135         if(target.object)
136         {
137                 unsigned n_uniforms = anim.animation->get_n_uniforms();
138                 for(unsigned i=0; i<n_uniforms; ++i)
139                         set_object_uniform(*target.object, anim.animation->get_uniform_name(i), anim.iterator.get_uniform(i));
140
141                 if(target.armature)
142                 {
143                         unsigned max_index = target.armature->get_max_link_index();
144                         for(unsigned i=0; i<=max_index; ++i)
145                                 target.object->set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
146                 }
147         }
148
149         anim.iterator.dispatch_events(target);
150
151         if(anim.iterator.is_end())
152                 target.animations.clear();
153 }
154
155 void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt)
156 {
157         Matrix matrix = target.base_matrix;
158         for(PlayingAnimation &a: target.animations)
159         {
160                 a.iterator += dt*a.speed;
161                 matrix *= a.iterator.get_matrix();
162
163                 if(target.object)
164                 {
165                         unsigned n_uniforms = a.animation->get_n_uniforms();
166                         for(unsigned j=0; j<n_uniforms; ++j)
167                                 set_object_uniform(*target.object, a.animation->get_uniform_name(j), a.iterator.get_uniform(j));
168                 }
169         }
170         target.placeable.set_matrix(matrix);
171
172         if(target.object && target.armature)
173         {
174                 unsigned max_index = target.armature->get_max_link_index();
175                 for(unsigned i=0; i<=max_index; ++i)
176                 {
177                         matrix = Matrix();
178                         /* XXX This is in all likelihood incorrect.  The stacking should be
179                         performed on local matrices. */
180                         for(const PlayingAnimation &a: target.animations)
181                                 if(a.animation->get_armature())
182                                         matrix *= a.iterator.get_pose_matrix(i);
183                         target.object->set_pose_matrix(i, matrix);
184                 }
185         }
186
187         for(auto i=target.animations.begin(); i!=target.animations.end(); )
188         {
189                 i->iterator.dispatch_events(target);
190
191                 if(i->iterator.is_end())
192                         i = target.animations.erase(i);
193                 else
194                         ++i;
195         }
196
197         if(target.animations.empty())
198                 target.stacked = false;
199 }
200
201 void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name, const KeyFrame::AnimatedUniform &uni)
202 {
203         ProgramData &shdata = obj.get_shader_data();
204
205         if(uni.size==1)
206                 shdata.uniform(name, uni.values[0]);
207         else if(uni.size==2)
208                 shdata.uniform2(name, uni.values);
209         else if(uni.size==3)
210                 shdata.uniform3(name, uni.values);
211         else if(uni.size==4)
212                 shdata.uniform4(name, uni.values);
213 }
214
215
216 AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a, float s):
217         animation(&a),
218         speed(s),
219         iterator(*animation)
220 { }
221
222
223 AnimationPlayer::Target::Target(Placeable &p):
224         placeable(p),
225         object(0),
226         armature(0),
227         stacked(false)
228 { }
229
230 void AnimationPlayer::Target::animation_event(Placeable *, const string &name, const Variant &value)
231 {
232         for(AnimationEventObserver *o: event_observers)
233                 o->animation_event(&placeable, name, value);
234 }
235
236 } // namespace GL
237 } // namespace Msp