1 /*************************************************************************/
2 /* animation_cache.cpp */
3 /*************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /*************************************************************************/
30 #include "animation_cache.h"
31
_node_exit_tree(Node * p_node)32 void AnimationCache::_node_exit_tree(Node *p_node) {
33
34 //it is one shot, so it disconnects upon arrival
35
36 ERR_FAIL_COND(!connected_nodes.has(p_node));
37
38 connected_nodes.erase(p_node);
39
40 for (int i = 0; i < path_cache.size(); i++) {
41
42 if (path_cache[i].node != p_node)
43 continue;
44
45 path_cache[i].valid = false; //invalidate path cache
46 }
47 }
48
_animation_changed()49 void AnimationCache::_animation_changed() {
50
51 _clear_cache();
52 }
53
_clear_cache()54 void AnimationCache::_clear_cache() {
55
56 while (connected_nodes.size()) {
57
58 connected_nodes.front()->get()->disconnect("exit_tree", this, "_node_exit_tree");
59 connected_nodes.erase(connected_nodes.front());
60 }
61 path_cache.clear();
62 cache_valid = false;
63 cache_dirty = true;
64 }
65
_update_cache()66 void AnimationCache::_update_cache() {
67
68 cache_valid = false;
69
70 ERR_FAIL_COND(!root);
71 ERR_FAIL_COND(!root->is_inside_tree());
72 ERR_FAIL_COND(animation.is_null());
73
74 for (int i = 0; i < animation->get_track_count(); i++) {
75
76 NodePath np = animation->track_get_path(i);
77
78 Node *node = root->get_node(np);
79 if (!node) {
80
81 path_cache.push_back(Path());
82 ERR_EXPLAIN("Invalid Track Path in Animation: " + np);
83 ERR_CONTINUE(!node);
84 }
85
86 Path path;
87
88 Ref<Resource> res;
89
90 if (np.get_subname_count()) {
91
92 if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM) {
93
94 path_cache.push_back(Path());
95 ERR_EXPLAIN("Transform tracks can't have a subpath: " + np);
96 ERR_CONTINUE(animation->track_get_type(i) == Animation::TYPE_TRANSFORM);
97 }
98
99 RES res;
100
101 for (int j = 0; j < np.get_subname_count(); j++) {
102 res = j == 0 ? node->get(np.get_subname(j)) : res->get(np.get_subname(j));
103 if (res.is_null())
104 break;
105 }
106
107 if (res.is_null()) {
108
109 path_cache.push_back(Path());
110 ERR_EXPLAIN("Invalid Track SubPath in Animation: " + np);
111 ERR_CONTINUE(res.is_null());
112 }
113
114 path.resource = res;
115 path.object = res.ptr();
116
117 } else {
118
119 if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM) {
120 StringName property = np.get_property();
121 String ps = property;
122
123 Spatial *sp = node->cast_to<Spatial>();
124
125 if (!sp) {
126
127 path_cache.push_back(Path());
128 ERR_EXPLAIN("Transform track not of type Spatial: " + np);
129 ERR_CONTINUE(!sp);
130 }
131
132 if (ps != "") {
133
134 Skeleton *sk = node->cast_to<Skeleton>();
135 if (!sk) {
136
137 path_cache.push_back(Path());
138 ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton!: " + np);
139 ERR_CONTINUE(!sk);
140 }
141
142 int idx = sk->find_bone(ps);
143 if (idx == -1) {
144
145 path_cache.push_back(Path());
146 ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton Bone!: " + np);
147 ERR_CONTINUE(idx == -1);
148 }
149
150 path.bone_idx = idx;
151 path.skeleton = sk;
152 }
153
154 path.spatial = sp;
155 }
156
157 path.node = node;
158 path.object = node;
159 }
160
161 if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
162
163 if (np.get_property().operator String() == "") {
164
165 path_cache.push_back(Path());
166 ERR_EXPLAIN("Value Track lacks property: " + np);
167 ERR_CONTINUE(np.get_property().operator String() == "");
168 }
169
170 path.property = np.get_property();
171
172 } else if (animation->track_get_type(i) == Animation::TYPE_METHOD) {
173
174 if (np.get_property().operator String() != "") {
175
176 path_cache.push_back(Path());
177 ERR_EXPLAIN("Method Track has property: " + np);
178 ERR_CONTINUE(np.get_property().operator String() != "");
179 }
180 }
181
182 path.valid = true;
183
184 path_cache.push_back(path);
185
186 if (!connected_nodes.has(path.node)) {
187 connected_nodes.insert(path.node);
188 path.node->connect("exit_tree", this, "_node_exit_tree", Node::make_binds(path.node), CONNECT_ONESHOT);
189 }
190 }
191
192 cache_dirty = false;
193 cache_valid = true;
194 }
195
set_track_transform(int p_idx,const Transform & p_transform)196 void AnimationCache::set_track_transform(int p_idx, const Transform &p_transform) {
197
198 if (cache_dirty)
199 _update_cache();
200
201 ERR_FAIL_COND(!cache_valid);
202 ERR_FAIL_INDEX(p_idx, path_cache.size());
203 Path &p = path_cache[p_idx];
204 if (!p.valid)
205 return;
206
207 ERR_FAIL_COND(!p.node);
208 ERR_FAIL_COND(!p.spatial);
209
210 if (p.skeleton) {
211 p.skeleton->set_bone_pose(p.bone_idx, p_transform);
212 } else {
213 p.spatial->set_transform(p_transform);
214 }
215 }
216
set_track_value(int p_idx,const Variant & p_value)217 void AnimationCache::set_track_value(int p_idx, const Variant &p_value) {
218
219 if (cache_dirty)
220 _update_cache();
221
222 ERR_FAIL_COND(!cache_valid);
223 ERR_FAIL_INDEX(p_idx, path_cache.size());
224 Path &p = path_cache[p_idx];
225 if (!p.valid)
226 return;
227
228 ERR_FAIL_COND(!p.object);
229 p.object->set(p.property, p_value);
230 }
231
call_track(int p_idx,const StringName & p_method,const Variant ** p_args,int p_argcount,Variant::CallError & r_error)232 void AnimationCache::call_track(int p_idx, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
233
234 if (cache_dirty)
235 _update_cache();
236
237 ERR_FAIL_COND(!cache_valid);
238 ERR_FAIL_INDEX(p_idx, path_cache.size());
239 Path &p = path_cache[p_idx];
240 if (!p.valid)
241 return;
242
243 ERR_FAIL_COND(!p.object);
244 p.object->call(p_method, p_args, p_argcount, r_error);
245 }
246
set_all(float p_time,float p_delta)247 void AnimationCache::set_all(float p_time, float p_delta) {
248
249 if (cache_dirty)
250 _update_cache();
251
252 ERR_FAIL_COND(!cache_valid);
253
254 int tc = animation->get_track_count();
255 for (int i = 0; i < tc; i++) {
256
257 switch (animation->track_get_type(i)) {
258
259 case Animation::TYPE_TRANSFORM: {
260
261 Vector3 loc, scale;
262 Quat rot;
263 animation->transform_track_interpolate(i, p_time, &loc, &rot, &scale);
264 Transform tr(Matrix3(rot), loc);
265 tr.basis.scale(scale);
266
267 set_track_transform(i, tr);
268
269 } break;
270 case Animation::TYPE_VALUE: {
271
272 if (animation->value_track_get_update_mode(i) == Animation::UPDATE_CONTINUOUS || (animation->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE && p_delta == 0)) {
273 Variant v = animation->value_track_interpolate(i, p_time);
274 set_track_value(i, v);
275 } else {
276
277 List<int> indices;
278 animation->value_track_get_key_indices(i, p_time, p_delta, &indices);
279
280 for (List<int>::Element *E = indices.front(); E; E = E->next()) {
281
282 Variant v = animation->track_get_key_value(i, E->get());
283 set_track_value(i, v);
284 }
285 }
286
287 } break;
288 case Animation::TYPE_METHOD: {
289
290 List<int> indices;
291 animation->method_track_get_key_indices(i, p_time, p_delta, &indices);
292
293 for (List<int>::Element *E = indices.front(); E; E = E->next()) {
294
295 Vector<Variant> args = animation->method_track_get_params(i, E->get());
296 StringName name = animation->method_track_get_name(i, E->get());
297 Variant::CallError err;
298
299 if (!args.size()) {
300
301 call_track(i, name, NULL, 0, err);
302 } else {
303
304 Vector<Variant *> argptrs;
305 argptrs.resize(args.size());
306 for (int j = 0; j < args.size(); j++) {
307
308 argptrs[j] = &args[j];
309 }
310
311 call_track(i, name, (const Variant **)&argptrs[0], args.size(), err);
312 }
313 }
314
315 } break;
316 default: {}
317 }
318 }
319 }
320
set_animation(const Ref<Animation> & p_animation)321 void AnimationCache::set_animation(const Ref<Animation> &p_animation) {
322
323 _clear_cache();
324
325 if (animation.is_valid())
326 animation->disconnect("changed", this, "_animation_changed");
327
328 animation = p_animation;
329
330 if (animation.is_valid())
331 animation->connect("changed", this, "_animation_changed");
332 }
333
_bind_methods()334 void AnimationCache::_bind_methods() {
335
336 ObjectTypeDB::bind_method(_MD("_node_exit_tree"), &AnimationCache::_node_exit_tree);
337 ObjectTypeDB::bind_method(_MD("_animation_changed"), &AnimationCache::_animation_changed);
338 }
339
set_root(Node * p_root)340 void AnimationCache::set_root(Node *p_root) {
341
342 _clear_cache();
343 root = p_root;
344 }
345
AnimationCache()346 AnimationCache::AnimationCache() {
347
348 root = NULL;
349 cache_dirty = true;
350 cache_valid = false;
351 }
352