]> git.tdb.fi Git - libs/gl.git/blob - source/animationplayer.h
Move transform loading to ObjectInstance
[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                 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         typedef std::map<const Placeable *, Target> ObjectMap;
46
47         ObjectMap objects;
48
49 private:
50         Target &get_slot(Placeable &);
51
52         Target &play_(Placeable &, const Animation &, bool, float);
53 public:
54         /// Plays an animation on an object.  Any previous animations are replaced.
55         void play(AnimatedObject &, const Animation &, float = 1.0f);
56
57         void play(Placeable &, const Animation &, float = 1.0f);
58
59         /** Plays an animation, stacked with other animations.  If no animations are
60         playing yet, the object's current matrix is used as the base. */
61         void play_stacked(AnimatedObject &, const Animation &, float = 1.0f);
62
63         void play_stacked(Placeable &, const Animation &, float = 1.0f);
64
65         /// Returns the number of animations currently affecting an object.
66         unsigned get_n_active_animations(const AnimatedObject &) const;
67
68         /** Request delivery of animation events for the given object.  Events will
69         be delivered from all current and future animations until the observer is
70         removed. */
71         void observe_events(AnimatedObject &, AnimationEventObserver &);
72
73         /// Remove an event observer from one object.
74         void unobserve_events(AnimatedObject &, AnimationEventObserver &);
75
76         /// Remove an event observer from all objects.
77         void unobserve_events(AnimationEventObserver &);
78
79         /// Stops all animations affecting an object.
80         void stop(AnimatedObject &);
81
82         /// Stops a single animation affecting an object.
83         void stop(AnimatedObject &, const Animation &);
84
85         /** Advances all playing animations.  Should be called in a regular manner,
86         preferably just before rendering. */
87         void tick(const Time::TimeDelta &);
88
89 private:
90         void tick_single(Target &, const Time::TimeDelta &);
91         void tick_stacked(Target &, const Time::TimeDelta &);
92         static void set_object_uniform(AnimatedObject &, const std::string &, const KeyFrame::AnimatedUniform &);
93 };
94
95 } // namespace GL
96 } // namespace Msp
97
98 #endif