]> git.tdb.fi Git - libs/gl.git/blobdiff - source/animation/animationplayer.h
Rearrange soucre files into subdirectories
[libs/gl.git] / source / animation / animationplayer.h
diff --git a/source/animation/animationplayer.h b/source/animation/animationplayer.h
new file mode 100644 (file)
index 0000000..c45027d
--- /dev/null
@@ -0,0 +1,98 @@
+#ifndef MSP_GL_ANIMATIONPLAYER_H_
+#define MSP_GL_ANIMATIONPLAYER_H_
+
+#include <msp/time/timedelta.h>
+#include "animation.h"
+#include "animationeventobserver.h"
+#include "matrix.h"
+
+namespace Msp {
+namespace GL {
+
+class AnimatedObject;
+
+/**
+The bridge between Animations and AnimatedObjects.  A single AnimationPlayer
+can handle an arbitrary number of animations simultaneously.
+*/
+class AnimationPlayer
+{
+private:
+       struct PlayingAnimation
+       {
+               const Animation *animation;
+               float speed;
+               Animation::Iterator iterator;
+
+               PlayingAnimation(const Animation &, float);
+       };
+
+       struct Target: AnimationEventObserver
+       {
+               Placeable &placeable;
+               AnimatedObject *object;
+               Matrix base_matrix;
+               const Armature *armature;
+               std::vector<PlayingAnimation> animations;
+               bool stacked;
+               std::vector<AnimationEventObserver *> event_observers;
+
+               Target(Placeable &);
+
+               virtual void animation_event(Placeable *, const std::string &, const Variant &);
+       };
+
+       typedef std::map<const Placeable *, Target> ObjectMap;
+
+       ObjectMap objects;
+
+private:
+       Target &get_slot(Placeable &);
+
+       Target &play_(Placeable &, const Animation &, bool, float);
+public:
+       /// Plays an animation on an object.  Any previous animations are replaced.
+       void play(AnimatedObject &, const Animation &, float = 1.0f);
+
+       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 &, float = 1.0f);
+
+       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;
+
+       /** Request delivery of animation events for the given object.  Events will
+       be delivered from all current and future animations until the observer is
+       removed. */
+       void observe_events(AnimatedObject &, AnimationEventObserver &);
+
+       /// Remove an event observer from one object.
+       void unobserve_events(AnimatedObject &, AnimationEventObserver &);
+
+       /// Remove an event observer from all objects.
+       void unobserve_events(AnimationEventObserver &);
+
+       /// Stops all animations affecting an object.
+       void stop(Placeable &);
+
+       /// Stops a single animation affecting an object.
+       void stop(Placeable &, const Animation &);
+
+       /** Advances all playing animations.  Should be called in a regular manner,
+       preferably just before rendering. */
+       void tick(const Time::TimeDelta &);
+
+private:
+       void tick_single(Target &, const Time::TimeDelta &);
+       void tick_stacked(Target &, const Time::TimeDelta &);
+       static void set_object_uniform(AnimatedObject &, const std::string &, const KeyFrame::AnimatedUniform &);
+};
+
+} // namespace GL
+} // namespace Msp
+
+#endif