]> git.tdb.fi Git - libs/gl.git/blob - source/animation.h
Restructure Animation to make matrix interpolation code more reusable
[libs/gl.git] / source / animation.h
1 #ifndef MSP_GL_ANIMATION_H_
2 #define MSP_GL_ANIMATION_H_
3
4 #include <msp/core/refptr.h>
5 #include <msp/datafile/objectloader.h>
6 #include <msp/time/timedelta.h>
7
8 namespace Msp {
9 namespace GL {
10
11 class KeyFrame;
12 class Matrix;
13
14 /**
15 An Animation is a sequence of KeyFrames combined with timing information.  The
16 state at any point in the animation can be interpolated from the keyframes.
17 */
18 class Animation
19 {
20 public:
21         class Loader: public DataFile::CollectionObjectLoader<Animation>
22         {
23         private:
24                 Time::TimeDelta current_time;
25
26         public:
27                 Loader(Animation &);
28                 Loader(Animation &, Collection &);
29         private:
30                 void init();
31
32                 void keyframe(const std::string &);
33                 void keyframe_inline();
34                 void interval(float);
35         };
36
37 private:
38         struct AxisInterpolation
39         {
40                 float slope;
41                 float scale;
42
43                 AxisInterpolation();
44                 AxisInterpolation(const double *, const double *);
45         };
46
47         struct MatrixInterpolation
48         {
49                 const Matrix *matrix1;
50                 const Matrix *matrix2;
51                 AxisInterpolation axes[3];
52
53                 MatrixInterpolation();
54                 MatrixInterpolation(const Matrix &, const Matrix &);
55
56                 Matrix get(float) const;
57         };
58
59         struct TimedKeyFrame
60         {
61                 const Animation &animation;
62                 const TimedKeyFrame *prev;
63                 Time::TimeDelta time;
64                 Time::TimeDelta delta_t;
65                 RefPtr<const KeyFrame> keyframe;
66                 MatrixInterpolation matrix;
67
68                 TimedKeyFrame(const Animation &);
69                 void prepare();
70         };
71
72         typedef std::list<TimedKeyFrame> KeyFrameList;
73
74 public:
75         class Iterator
76         {
77         private:
78                 const Animation &animation;
79                 KeyFrameList::const_iterator iter;
80                 Time::TimeDelta time_since_keyframe;
81                 bool end;
82
83         public:
84                 Iterator(const Animation &);
85
86                 Iterator &operator+=(const Time::TimeDelta &);
87
88                 bool is_end() const { return end; }
89                 Matrix get_matrix() const;
90         };
91
92 private:
93         KeyFrameList keyframes;
94         bool looping;
95
96 public:
97         Animation();
98
99         void add_keyframe(const Time::TimeDelta &, const KeyFrame &);
100 private:
101         void prepare_keyframe(TimedKeyFrame &);
102
103 public:
104         void set_looping(bool);
105 };
106
107 } // namespace GL
108 } // namespace Msp
109
110 #endif