]> git.tdb.fi Git - libs/gl.git/blob - source/animation/keyframe.cpp
Add inline data items to the collection
[libs/gl.git] / source / animation / keyframe.cpp
1 #include <msp/datafile/collection.h>
2 #include <msp/fs/utils.h>
3 #include "keyframe.h"
4 #include "pose.h"
5
6 using namespace std;
7
8 namespace Msp {
9 namespace GL {
10
11 // Avoid synthesizing RefPtr c'tor and d'tor in files including keyframe.h
12 KeyFrame::KeyFrame()
13 { }
14
15 KeyFrame::~KeyFrame()
16 { }
17
18 void KeyFrame::set_transform(const Transform &t)
19 {
20         transform = t;
21 }
22
23 void KeyFrame::set_matrix(const Matrix &m)
24 {
25         transform = Transform::from_matrix(m);
26 }
27
28 void KeyFrame::set_uniform(const string &n, const AnimatedUniform &u)
29 {
30         uniforms.erase(n);
31         uniforms.insert(UniformMap::value_type(n, u));
32 }
33
34 void KeyFrame::set_pose(const Pose &p)
35 {
36         pose = &p;
37 }
38
39
40 KeyFrame::AnimatedUniform::AnimatedUniform(unsigned s, float v0, float v1, float v2, float v3):
41         size(s)
42 {
43         values[0] = v0;
44         values[1] = v1;
45         values[2] = v2;
46         values[3] = v3;
47 }
48
49
50 KeyFrame::Loader::Loader(KeyFrame &k):
51         DataFile::CollectionObjectLoader<KeyFrame>(k, 0)
52 {
53         init();
54 }
55
56 KeyFrame::Loader::Loader(KeyFrame &k, Collection &c):
57         DataFile::CollectionObjectLoader<KeyFrame>(k, &c)
58 {
59         init();
60 }
61
62 void KeyFrame::Loader::init()
63 {
64         add("pose", &Loader::pose);
65         add("pose", &Loader::pose_inline);
66         add("transform", &Loader::transform);
67         add("uniforms", &Loader::uniforms);
68
69         // Deprecated; use the transform statement instead
70         add("position", &Loader::position);
71         add("rotation", &Loader::rotation);
72         add("scaling", &Loader::scaling_uniform);
73         add("scaling", &Loader::scaling);
74 }
75
76 void KeyFrame::Loader::set_inline_base_name(const string &n)
77 {
78         inline_base_name = n;
79 }
80
81 void KeyFrame::Loader::pose(const string &n)
82 {
83         obj.pose = &get_collection().get<Pose>(n);
84 }
85
86 void KeyFrame::Loader::pose_inline()
87 {
88         RefPtr<Pose> p = new Pose;
89         load_sub(*p, get_collection());
90         get_collection().add((inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name)+".pose", p.get());
91         obj.pose = p.release();
92 }
93
94 void KeyFrame::Loader::position(float x, float y, float z)
95 {
96         obj.transform.set_position(Vector3(x, y, z));
97 }
98
99 void KeyFrame::Loader::rotation(float a, float x, float y, float z)
100 {
101         obj.transform.set_rotation(Transform::Angle::from_degrees(a), Vector3(x, y, z));
102 }
103
104 void KeyFrame::Loader::scaling_uniform(float s)
105 {
106         obj.transform.set_scale(s);
107 }
108
109 void KeyFrame::Loader::scaling(float x, float y, float z)
110 {
111         obj.transform.set_scale(Vector3(x, y, z));
112 }
113
114 void KeyFrame::Loader::transform()
115 {
116         load_sub(obj.transform);
117 }
118
119 void KeyFrame::Loader::uniforms()
120 {
121         UniformsLoader ldr(obj);
122         load_sub_with(ldr);
123 }
124
125
126 KeyFrame::UniformsLoader::UniformsLoader(KeyFrame &k):
127         DataFile::ObjectLoader<KeyFrame>(k)
128 {
129         add("uniform", &UniformsLoader::uniform1f);
130         add("uniform", &UniformsLoader::uniform2f);
131         add("uniform", &UniformsLoader::uniform3f);
132         add("uniform", &UniformsLoader::uniform4f);
133
134         // Deprecated
135         add("uniform1f", &UniformsLoader::uniform1f);
136         add("uniform2f", &UniformsLoader::uniform2f);
137         add("uniform3f", &UniformsLoader::uniform3f);
138         add("uniform4f", &UniformsLoader::uniform4f);
139 }
140
141 void KeyFrame::UniformsLoader::uniform1f(const string &n, float v)
142 {
143         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(1, v)));
144 }
145
146 void KeyFrame::UniformsLoader::uniform2f(const string &n, float v0, float v1)
147 {
148         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(2, v0, v1)));
149 }
150
151 void KeyFrame::UniformsLoader::uniform3f(const string &n, float v0, float v1, float v2)
152 {
153         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(3, v0, v1, v2)));
154 }
155
156 void KeyFrame::UniformsLoader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
157 {
158         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(4, v0, v1, v2, v3)));
159 }
160
161 } // namespace GL
162 } // namespace Msp