]> git.tdb.fi Git - libs/gl.git/blob - source/animation/keyframe.cpp
Use constructor delegation instead of init functions when possible
[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, Collection *c):
51         DataFile::CollectionObjectLoader<KeyFrame>(k, c)
52 {
53         add("pose", &Loader::pose);
54         add("pose", &Loader::pose_inline);
55         add("transform", &Loader::transform);
56         add("uniforms", &Loader::uniforms);
57
58         // Deprecated; use the transform statement instead
59         add("position", &Loader::position);
60         add("rotation", &Loader::rotation);
61         add("scaling", &Loader::scaling_uniform);
62         add("scaling", &Loader::scaling);
63 }
64
65 void KeyFrame::Loader::set_inline_base_name(const string &n)
66 {
67         inline_base_name = n;
68 }
69
70 void KeyFrame::Loader::pose(const string &n)
71 {
72         obj.pose = &get_collection().get<Pose>(n);
73 }
74
75 void KeyFrame::Loader::pose_inline()
76 {
77         RefPtr<Pose> p = new Pose;
78         load_sub(*p, get_collection());
79         get_collection().add((inline_base_name.empty() ? FS::basename(get_source()) : inline_base_name)+".pose", p.get());
80         obj.pose = p.release();
81 }
82
83 void KeyFrame::Loader::position(float x, float y, float z)
84 {
85         obj.transform.set_position(Vector3(x, y, z));
86 }
87
88 void KeyFrame::Loader::rotation(float a, float x, float y, float z)
89 {
90         obj.transform.set_rotation(Transform::Angle::from_degrees(a), Vector3(x, y, z));
91 }
92
93 void KeyFrame::Loader::scaling_uniform(float s)
94 {
95         obj.transform.set_scale(s);
96 }
97
98 void KeyFrame::Loader::scaling(float x, float y, float z)
99 {
100         obj.transform.set_scale(Vector3(x, y, z));
101 }
102
103 void KeyFrame::Loader::transform()
104 {
105         load_sub(obj.transform);
106 }
107
108 void KeyFrame::Loader::uniforms()
109 {
110         UniformsLoader ldr(obj);
111         load_sub_with(ldr);
112 }
113
114
115 KeyFrame::UniformsLoader::UniformsLoader(KeyFrame &k):
116         DataFile::ObjectLoader<KeyFrame>(k)
117 {
118         add("uniform", &UniformsLoader::uniform1f);
119         add("uniform", &UniformsLoader::uniform2f);
120         add("uniform", &UniformsLoader::uniform3f);
121         add("uniform", &UniformsLoader::uniform4f);
122
123         // Deprecated
124         add("uniform1f", &UniformsLoader::uniform1f);
125         add("uniform2f", &UniformsLoader::uniform2f);
126         add("uniform3f", &UniformsLoader::uniform3f);
127         add("uniform4f", &UniformsLoader::uniform4f);
128 }
129
130 void KeyFrame::UniformsLoader::uniform1f(const string &n, float v)
131 {
132         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(1, v)));
133 }
134
135 void KeyFrame::UniformsLoader::uniform2f(const string &n, float v0, float v1)
136 {
137         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(2, v0, v1)));
138 }
139
140 void KeyFrame::UniformsLoader::uniform3f(const string &n, float v0, float v1, float v2)
141 {
142         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(3, v0, v1, v2)));
143 }
144
145 void KeyFrame::UniformsLoader::uniform4f(const string &n, float v0, float v1, float v2, float v3)
146 {
147         obj.uniforms.insert(UniformMap::value_type(n, AnimatedUniform(4, v0, v1, v2, v3)));
148 }
149
150 } // namespace GL
151 } // namespace Msp