]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animationplayer.cpp
Change AnimationPlayer::stop to take a Placeable
[libs/gl.git] / source / animationplayer.cpp
index 6b5a481f6a74f8da98b138067d803d8aacfbe785..ac16321874a7abd612ad6cf39fdb69cf1fdb8cf4 100644 (file)
@@ -9,7 +9,7 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-AnimationPlayer::Target &AnimationPlayer::get_slot(AnimatedObject &obj)
+AnimationPlayer::Target &AnimationPlayer::get_slot(Placeable &obj)
 {
        ObjectMap::iterator i = objects.find(&obj);
        if(i!=objects.end())
@@ -18,25 +18,43 @@ AnimationPlayer::Target &AnimationPlayer::get_slot(AnimatedObject &obj)
        return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second;
 }
 
-void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim)
+AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked, float speed)
 {
        Target &target = get_slot(obj);
-       target.animations.clear();
-       target.base_matrix = Matrix();
-       target.stacked = false;
+       if(!stacked)
+       {
+               target.animations.clear();
+               target.base_matrix = Matrix();
+       }
+       else if(target.animations.empty())
+               target.base_matrix = *obj.get_matrix();
+       target.stacked = stacked;
+       // TODO check for incompatible armature
        target.armature = anim.get_armature();
-       target.animations.push_back(PlayingAnimation(anim));
+       target.animations.push_back(PlayingAnimation(anim, speed));
+       return target;
 }
 
-void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim)
+void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim, float speed)
 {
-       Target &target = get_slot(obj);
-       if(target.animations.empty())
-               target.base_matrix = *obj.get_matrix();
-       // TODO check for incompatible armature
-       target.stacked = true;
-       target.armature = anim.get_armature();
-       target.animations.push_back(PlayingAnimation(anim));
+       Target &target = play_(obj, anim, false, speed);
+       target.object = &obj;
+}
+
+void AnimationPlayer::play(Placeable &obj, const Animation &anim, float speed)
+{
+       play_(obj, anim, false, speed);
+}
+
+void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim, float speed)
+{
+       Target &target = play_(obj, anim, true, speed);
+       target.object = &obj;
+}
+
+void AnimationPlayer::play_stacked(Placeable &obj, const Animation &anim, float speed)
+{
+       play_(obj, anim, true, speed);
 }
 
 unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
@@ -73,12 +91,12 @@ void AnimationPlayer::unobserve_events(AnimationEventObserver &observer)
        }
 }
 
-void AnimationPlayer::stop(AnimatedObject &obj)
+void AnimationPlayer::stop(Placeable &obj)
 {
        objects.erase(&obj);
 }
 
-void AnimationPlayer::stop(AnimatedObject &obj, const Animation &anim)
+void AnimationPlayer::stop(Placeable &obj, const Animation &anim)
 {
        ObjectMap::iterator i = objects.find(&obj);
        if(i==objects.end())
@@ -114,23 +132,26 @@ void AnimationPlayer::tick(const Time::TimeDelta &dt)
 void AnimationPlayer::tick_single(Target &target, const Time::TimeDelta &dt)
 {
        PlayingAnimation &anim = target.animations.front();
-       anim.iterator += dt;
-       target.object.set_matrix(anim.iterator.get_matrix());
+       anim.iterator += dt*anim.speed;
+       target.placeable.set_matrix(anim.iterator.get_matrix());
 
-       unsigned n_uniforms = anim.animation->get_n_uniforms();
-       for(unsigned i=0; i<n_uniforms; ++i)
-               set_object_uniform(target.object, anim.animation->get_uniform_name(i), anim.iterator.get_uniform(i));
-
-       if(target.armature)
+       if(target.object)
        {
-               unsigned max_index = target.armature->get_max_link_index();
-               for(unsigned i=0; i<=max_index; ++i)
-                       target.object.set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
+               unsigned n_uniforms = anim.animation->get_n_uniforms();
+               for(unsigned i=0; i<n_uniforms; ++i)
+                       set_object_uniform(*target.object, anim.animation->get_uniform_name(i), anim.iterator.get_uniform(i));
+
+               if(target.armature)
+               {
+                       unsigned max_index = target.armature->get_max_link_index();
+                       for(unsigned i=0; i<=max_index; ++i)
+                               target.object->set_pose_matrix(i, anim.iterator.get_pose_matrix(i));
+               }
        }
 
        anim.iterator.dispatch_events(target);
 
-       if(!anim.iterator.is_end())
+       if(anim.iterator.is_end())
                target.animations.clear();
 }
 
@@ -139,16 +160,19 @@ void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt)
        Matrix matrix = target.base_matrix;
        for(vector<PlayingAnimation>::iterator i=target.animations.begin(); i!=target.animations.end(); ++i)
        {
-               i->iterator += dt;
+               i->iterator += dt*i->speed;
                matrix *= i->iterator.get_matrix();
 
-               unsigned n_uniforms = i->animation->get_n_uniforms();
-               for(unsigned j=0; j<n_uniforms; ++j)
-                       set_object_uniform(target.object, i->animation->get_uniform_name(j), i->iterator.get_uniform(j));
+               if(target.object)
+               {
+                       unsigned n_uniforms = i->animation->get_n_uniforms();
+                       for(unsigned j=0; j<n_uniforms; ++j)
+                               set_object_uniform(*target.object, i->animation->get_uniform_name(j), i->iterator.get_uniform(j));
+               }
        }
-       target.object.set_matrix(matrix);
+       target.placeable.set_matrix(matrix);
 
-       if(target.armature)
+       if(target.object && target.armature)
        {
                unsigned max_index = target.armature->get_max_link_index();
                for(unsigned i=0; i<=max_index; ++i)
@@ -159,7 +183,7 @@ void AnimationPlayer::tick_stacked(Target &target, const Time::TimeDelta &dt)
                        for(vector<PlayingAnimation>::iterator j=target.animations.begin(); j!=target.animations.end(); ++j)
                                if(j->animation->get_armature())
                                        matrix *= j->iterator.get_pose_matrix(i);
-                       target.object.set_pose_matrix(i, matrix);
+                       target.object->set_pose_matrix(i, matrix);
                }
        }
 
@@ -185,30 +209,32 @@ void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name
                shdata.uniform(name, uni.values[0]);
        else if(uni.size==2)
                shdata.uniform2(name, uni.values);
-       else if(uni.size==2)
+       else if(uni.size==3)
                shdata.uniform3(name, uni.values);
        else if(uni.size==4)
                shdata.uniform4(name, uni.values);
 }
 
 
-AnimationPlayer::Target::Target(AnimatedObject &o):
-       object(o),
+AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a, float s):
+       animation(&a),
+       speed(s),
+       iterator(*animation)
+{ }
+
+
+AnimationPlayer::Target::Target(Placeable &p):
+       placeable(p),
+       object(0),
        armature(0),
        stacked(false)
 { }
 
-void AnimationPlayer::Target::animation_event(AnimatedObject *, const string &name, const Variant &value)
+void AnimationPlayer::Target::animation_event(Placeable *, const string &name, const Variant &value)
 {
        for(vector<AnimationEventObserver *>::const_iterator i=event_observers.begin(); i!=event_observers.end(); ++i)
-               (*i)->animation_event(&object, name, value);
+               (*i)->animation_event(&placeable, name, value);
 }
 
-
-AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a):
-       animation(&a),
-       iterator(*animation)
-{ }
-
 } // namespace GL
 } // namespace Msp