]> git.tdb.fi Git - libs/gl.git/blob - source/animation/animationplayer.h
Use C++11 features with containers
[libs/gl.git] / source / animation / animationplayer.h
1 #ifndef MSP_GL_ANIMATIONPLAYER_H_
2 #define MSP_GL_ANIMATIONPLAYER_H_
3
4 #include <msp/time/timedelta.h>
5 #include "animation.h"
6 #include "animationeventobserver.h"
7 #include "matrix.h"
8
9 namespace Msp {
10 namespace GL {
11
12 class AnimatedObject;
13
14 /**
15 The bridge between Animations and AnimatedObjects.  A single AnimationPlayer
16 can handle an arbitrary number of animations simultaneously.
17 */
18 class AnimationPlayer
19 {
20 private:
21         struct PlayingAnimation
22         {
23                 const Animation *animation;
24                 float speed;
25                 Animation::Iterator iterator;
26
27                 PlayingAnimation(const Animation &, float);
28         };
29
30         struct Target: AnimationEventObserver
31         {
32                 Placeable &placeable;
33                 AnimatedObject *object;
34                 Matrix base_matrix;
35                 const Armature *armature;
36                 std::vector<PlayingAnimation> animations;
37                 bool stacked;
38                 std::vector<AnimationEventObserver *> event_observers;
39
40                 Target(Placeable &);
41
42                 virtual void animation_event(Placeable *, const std::string &, const Variant &);
43         };
44
45         std::map<const Placeable *, Target> objects;
46
47 private:
48         Target &get_slot(Placeable &);
49
50         Target &play_(Placeable &, const Animation &, bool, float);
51 public:
52         /// Plays an animation on an object.  Any previous animations are replaced.
53         void play(AnimatedObject &, const Animation &, float = 1.0f);
54
55         void play(Placeable &, const Animation &, float = 1.0f);
56
57         /** Plays an animation, stacked with other animations.  If no animations are
58         playing yet, the object's current matrix is used as the base. */
59         void play_stacked(AnimatedObject &, const Animation &, float = 1.0f);
60
61         void play_stacked(Placeable &, const Animation &, float = 1.0f);
62
63         /// Returns the number of animations currently affecting an object.
64         unsigned get_n_active_animations(const AnimatedObject &) const;
65
66         /** Request delivery of animation events for the given object.  Events will
67         be delivered from all current and future animations until the observer is
68         removed. */
69         void observe_events(AnimatedObject &, AnimationEventObserver &);
70
71         /// Remove an event observer from one object.
72         void unobserve_events(AnimatedObject &, AnimationEventObserver &);
73
74         /// Remove an event observer from all objects.
75         void unobserve_events(AnimationEventObserver &);
76
77         /// Stops all animations affecting an object.
78         void stop(Placeable &);
79
80         /// Stops a single animation affecting an object.
81         void stop(Placeable &, const Animation &);
82
83         /** Advances all playing animations.  Should be called in a regular manner,
84         preferably just before rendering. */
85         void tick(const Time::TimeDelta &);
86
87 private:
88         void tick_single(Target &, const Time::TimeDelta &);
89         void tick_stacked(Target &, const Time::TimeDelta &);
90         static void set_object_uniform(AnimatedObject &, const std::string &, const KeyFrame::AnimatedUniform &);
91 };
92
93 } // namespace GL
94 } // namespace Msp
95
96 #endif