From 5e8625bd980129891f1e98543a8a2e0886057b2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 24 Jan 2013 13:08:27 +0100 Subject: [PATCH] SceneGraph: don't pass duration in Animable constructor. It gets lost in the whole parameter list, using protected function setDuration() instead. Default is `0.0f`, which means infinite duration. --- src/SceneGraph/Animable.h | 30 +++++++++++++++++++--------- src/SceneGraph/Animable.hpp | 2 +- src/SceneGraph/Test/AnimableTest.cpp | 12 +++++++---- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/src/SceneGraph/Animable.h b/src/SceneGraph/Animable.h index ae2414fd8..a0e51de2e 100644 --- a/src/SceneGraph/Animable.h +++ b/src/SceneGraph/Animable.h @@ -60,8 +60,8 @@ which takes care of running the animations. First thing is add Animable feature to some object and implement animationStep(). You can do it conveniently using multiple inheritance (see -@ref scenegraph-features for introduction). You need to pass animation -duration to constructor, animationStep() provides both absolute animation +@ref scenegraph-features for introduction). Override animationStep() to +implement your animation, the function provides both absolute animation time and time delta. Example: @code typedef SceneGraph::Object> Object3D; @@ -69,7 +69,8 @@ typedef SceneGraph::Scene> Scene3D; class AnimableObject: public Object3D, SceneGraph::Animable3D<> { public: - AnimableObject(Object* parent = nullptr, SceneGraph::DrawableGroup3D<>* group = nullptr): Object3D(parent), SceneGraph::Animable3D<>(this, 10.0f, group) { + AnimableObject(Object* parent = nullptr, SceneGraph::DrawableGroup3D<>* group = nullptr): Object3D(parent), SceneGraph::Animable3D<>(this, group) { + setDuration(10.0f); // ... } @@ -141,15 +142,13 @@ class MAGNUM_SCENEGRAPH_EXPORT Animable: public AbstractGroupedFeature* object, GLfloat duration, AnimableGroup* group = nullptr); + explicit Animable(AbstractObject* object, AnimableGroup* group = nullptr); /** @brief Animation duration */ inline GLfloat duration() const { return _duration; } @@ -213,6 +212,19 @@ class MAGNUM_SCENEGRAPH_EXPORT Animable: public AbstractGroupedFeature* group() const; protected: + /** + * @brief Set animation duration + * @return Pointer to self (for method chaining) + * + * Sets duration of the animation cycle in seconds. Set to `0.0f` for + * infinite non-repeating animation. Default is `0.0f`. + */ + /* Protected so only animation implementer can change it */ + inline Animable* setDuration(GLfloat duration) { + _duration = duration; + return this; + } + /** * @brief Perform animation step * @param time Time from start of the animation @@ -294,7 +306,7 @@ class MAGNUM_SCENEGRAPH_EXPORT Animable: public AbstractGroupedFeature Animable::Animable(AbstractObject* object, GLfloat duration, AnimableGroup* group): AbstractGroupedFeature, T>(object, group), _duration(duration), startTime(std::numeric_limits::infinity()), pauseTime(-std::numeric_limits::infinity()), previousState(AnimationState::Stopped), currentState(AnimationState::Stopped), _repeated(false), _repeatCount(0), repeats(0) {} +template Animable::Animable(AbstractObject* object, AnimableGroup* group): AbstractGroupedFeature, T>(object, group), _duration(0.0f), startTime(std::numeric_limits::infinity()), pauseTime(-std::numeric_limits::infinity()), previousState(AnimationState::Stopped), currentState(AnimationState::Stopped), _repeated(false), _repeatCount(0), repeats(0) {} template Animable* Animable::setState(AnimationState state) { if(currentState == state) return this; diff --git a/src/SceneGraph/Test/AnimableTest.cpp b/src/SceneGraph/Test/AnimableTest.cpp index 600f815d4..1c4840fb1 100644 --- a/src/SceneGraph/Test/AnimableTest.cpp +++ b/src/SceneGraph/Test/AnimableTest.cpp @@ -51,7 +51,9 @@ AnimableTest::AnimableTest() { void AnimableTest::state() { class StateTrackingAnimable: public SceneGraph::Animable<3> { public: - StateTrackingAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, 1.0f, group) {} + StateTrackingAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, group) { + setDuration(1.0f); + } std::string trackedState; @@ -139,7 +141,8 @@ void AnimableTest::state() { class OneShotAnimable: public SceneGraph::Animable<3> { public: - OneShotAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, 10.0f, group), time(-1.0f) { + OneShotAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, group), time(-1.0f) { + setDuration(10.0f); setState(AnimationState::Running); } @@ -163,7 +166,7 @@ class OneShotAnimable: public SceneGraph::Animable<3> { void AnimableTest::step() { class InifiniteAnimable: public SceneGraph::Animable<3> { public: - InifiniteAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, 0.0f, group), time(-1.0f), delta(0.0f) {} + InifiniteAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, group), time(-1.0f), delta(0.0f) {} GLfloat time, delta; @@ -222,7 +225,8 @@ void AnimableTest::duration() { void AnimableTest::repeat() { class RepeatingAnimable: public SceneGraph::Animable<3> { public: - RepeatingAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, 10.0f, group), time(-1.0f) { + RepeatingAnimable(AbstractObject<3>* object, AnimableGroup<3>* group = nullptr): SceneGraph::Animable<3>(object, group), time(-1.0f) { + setDuration(10.0f); setState(AnimationState::Running); setRepeated(true); }