]> git.tdb.fi Git - libs/gl.git/commitdiff
Add a speed parameter for animation playback
authorMikko Rasa <tdb@tdb.fi>
Sun, 1 Jul 2018 22:10:41 +0000 (01:10 +0300)
committerMikko Rasa <tdb@tdb.fi>
Sun, 1 Jul 2018 22:10:41 +0000 (01:10 +0300)
source/animationplayer.cpp
source/animationplayer.h

index 13bd7af034a70aa59b4c9b445cb8161640ba969a..cbcb6b596f07addcdac87d3f7d6ab8bd3b992af4 100644 (file)
@@ -18,7 +18,7 @@ AnimationPlayer::Target &AnimationPlayer::get_slot(Placeable &obj)
        return objects.insert(ObjectMap::value_type(&obj, Target(obj))).first->second;
 }
 
-AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked)
+AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation &anim, bool stacked, float speed)
 {
        Target &target = get_slot(obj);
        if(!stacked)
@@ -31,30 +31,30 @@ AnimationPlayer::Target &AnimationPlayer::play_(Placeable &obj, const Animation
        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(AnimatedObject &obj, const Animation &anim)
+void AnimationPlayer::play(AnimatedObject &obj, const Animation &anim, float speed)
 {
-       Target &target = play_(obj, anim, false);
+       Target &target = play_(obj, anim, false, speed);
        target.object = &obj;
 }
 
-void AnimationPlayer::play(Placeable &obj, const Animation &anim)
+void AnimationPlayer::play(Placeable &obj, const Animation &anim, float speed)
 {
-       play_(obj, anim, false);
+       play_(obj, anim, false, speed);
 }
 
-void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim)
+void AnimationPlayer::play_stacked(AnimatedObject &obj, const Animation &anim, float speed)
 {
-       Target &target = play_(obj, anim, true);
+       Target &target = play_(obj, anim, true, speed);
        target.object = &obj;
 }
 
-void AnimationPlayer::play_stacked(Placeable &obj, const Animation &anim)
+void AnimationPlayer::play_stacked(Placeable &obj, const Animation &anim, float speed)
 {
-       play_(obj, anim, true);
+       play_(obj, anim, true, speed);
 }
 
 unsigned AnimationPlayer::get_n_active_animations(const AnimatedObject &obj) const
@@ -132,7 +132,7 @@ 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;
+       anim.iterator += dt*anim.speed;
        target.placeable.set_matrix(anim.iterator.get_matrix());
 
        if(target.object)
@@ -160,7 +160,7 @@ 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();
 
                if(target.object)
@@ -216,8 +216,9 @@ void AnimationPlayer::set_object_uniform(AnimatedObject &obj, const string &name
 }
 
 
-AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a):
+AnimationPlayer::PlayingAnimation::PlayingAnimation(const Animation &a, float s):
        animation(&a),
+       speed(s),
        iterator(*animation)
 { }
 
index 4158627dbaa66fc26cceee5bb9d4dfb93600decf..b6575304f4072d03f489590e599b370ad0ea816b 100644 (file)
@@ -21,9 +21,10 @@ private:
        struct PlayingAnimation
        {
                const Animation *animation;
+               float speed;
                Animation::Iterator iterator;
 
-               PlayingAnimation(const Animation &);
+               PlayingAnimation(const Animation &, float);
        };
 
        struct Target: AnimationEventObserver
@@ -48,18 +49,18 @@ private:
 private:
        Target &get_slot(Placeable &);
 
-       Target &play_(Placeable &, const Animation &, bool);
+       Target &play_(Placeable &, const Animation &, bool, float);
 public:
        /// Plays an animation on an object.  Any previous animations are replaced.
-       void play(AnimatedObject &, const Animation &);
+       void play(AnimatedObject &, const Animation &, float = 1.0f);
 
-       void play(Placeable &, const Animation &);
+       void play(Placeable &, const Animation &, float = 1.0f);
 
        /** Plays an animation, stacked with other animations.  If no animations are
        playing yet, the object's current matrix is used as the base. */
-       void play_stacked(AnimatedObject &, const Animation &);
+       void play_stacked(AnimatedObject &, const Animation &, float = 1.0f);
 
-       void play_stacked(Placeable &, const Animation &);
+       void play_stacked(Placeable &, const Animation &, float = 1.0f);
 
        /// Returns the number of animations currently affecting an object.
        unsigned get_n_active_animations(const AnimatedObject &) const;