]> git.tdb.fi Git - libs/gl.git/blob - source/animationplayer.h
Support slopes in keyframe interpolation
[libs/gl.git] / source / 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                 Animation::Iterator iterator;
25
26                 PlayingAnimation(const Animation &);
27         };
28
29         struct Target: AnimationEventObserver
30         {
31                 Placeable &placeable;
32                 AnimatedObject *object;
33                 Matrix base_matrix;
34                 const Armature *armature;
35                 std::vector<PlayingAnimation> animations;
36                 bool stacked;
37                 std::vector<AnimationEventObserver *> event_observers;
38
39                 Target(Placeable &);
40
41                 virtual void animation_event(Placeable *, const std::string &, const Variant &);
42         };
43
44         typedef std::map<const Placeable *, Target> ObjectMap;
45
46         ObjectMap objects;
47
48 private:
49         Target &get_slot(Placeable &);
50
51         Target &play_(Placeable &, const Animation &, bool);
52 public:
53         /// Plays an animation on an object.  Any previous animations are replaced.
54         void play(AnimatedObject &, const Animation &);
55
56         void play(Placeable &, const Animation &);
57
58         /** Plays an animation, stacked with other animations.  If no animations are
59         playing yet, the object's current matrix is used as the base. */
60         void play_stacked(AnimatedObject &, const Animation &);
61
62         void play_stacked(Placeable &, const Animation &);
63
64         /// Returns the number of animations currently affecting an object.
65         unsigned get_n_active_animations(const AnimatedObject &) const;
66
67         /** Request delivery of animation events for the given object.  Events will
68         be delivered from all current and future animations until the observer is
69         removed. */
70         void observe_events(AnimatedObject &, AnimationEventObserver &);
71
72         /// Remove an event observer from one object.
73         void unobserve_events(AnimatedObject &, AnimationEventObserver &);
74
75         /// Remove an event observer from all objects.
76         void unobserve_events(AnimationEventObserver &);
77
78         /// Stops all animations affecting an object.
79         void stop(AnimatedObject &);
80
81         /// Stops a single animation affecting an object.
82         void stop(AnimatedObject &, const Animation &);
83
84         /** Advances all playing animations.  Should be called in a regular manner,
85         preferably just before rendering. */
86         void tick(const Time::TimeDelta &);
87
88 private:
89         void tick_single(Target &, const Time::TimeDelta &);
90         void tick_stacked(Target &, const Time::TimeDelta &);
91         static void set_object_uniform(AnimatedObject &, const std::string &, const KeyFrame::AnimatedUniform &);
92 };
93
94 } // namespace GL
95 } // namespace Msp
96
97 #endif