From 42c1534d95e1551c37e64a1dae288e8b75e8d8ba Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 2 Jul 2018 01:10:41 +0300 Subject: [PATCH] Add a speed parameter for animation playback --- source/animationplayer.cpp | 27 ++++++++++++++------------- source/animationplayer.h | 13 +++++++------ 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/source/animationplayer.cpp b/source/animationplayer.cpp index 13bd7af0..cbcb6b59 100644 --- a/source/animationplayer.cpp +++ b/source/animationplayer.cpp @@ -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::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) { } diff --git a/source/animationplayer.h b/source/animationplayer.h index 4158627d..b6575304 100644 --- a/source/animationplayer.h +++ b/source/animationplayer.h @@ -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; -- 2.45.2