1 /*************************************************************************/
2 /*  animation_cache.cpp                                                  */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 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 
31 #include "animation_cache.h"
32 
_node_exit_tree(Node * p_node)33 void AnimationCache::_node_exit_tree(Node *p_node) {
34 
35 	//it is one shot, so it disconnects upon arrival
36 
37 	ERR_FAIL_COND(!connected_nodes.has(p_node));
38 
39 	connected_nodes.erase(p_node);
40 
41 	for (int i = 0; i < path_cache.size(); i++) {
42 
43 		if (path_cache[i].node != p_node)
44 			continue;
45 
46 		path_cache.write[i].valid = false; //invalidate path cache
47 	}
48 }
49 
_animation_changed()50 void AnimationCache::_animation_changed() {
51 
52 	_clear_cache();
53 }
54 
_clear_cache()55 void AnimationCache::_clear_cache() {
56 
57 	while (connected_nodes.size()) {
58 
59 		connected_nodes.front()->get()->disconnect("tree_exiting", this, "_node_exit_tree");
60 		connected_nodes.erase(connected_nodes.front());
61 	}
62 	path_cache.clear();
63 	cache_valid = false;
64 	cache_dirty = true;
65 }
66 
_update_cache()67 void AnimationCache::_update_cache() {
68 
69 	cache_valid = false;
70 
71 	ERR_FAIL_COND(!root);
72 	ERR_FAIL_COND(!root->is_inside_tree());
73 	ERR_FAIL_COND(animation.is_null());
74 
75 	for (int i = 0; i < animation->get_track_count(); i++) {
76 
77 		NodePath np = animation->track_get_path(i);
78 
79 		Node *node = root->get_node(np);
80 		if (!node) {
81 
82 			path_cache.push_back(Path());
83 			ERR_CONTINUE_MSG(!node, "Invalid track path in animation '" + np + "'.");
84 		}
85 
86 		Path path;
87 
88 		Ref<Resource> res;
89 
90 		if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM) {
91 
92 			if (np.get_subname_count() > 1) {
93 				path_cache.push_back(Path());
94 				ERR_CONTINUE_MSG(animation->track_get_type(i) == Animation::TYPE_TRANSFORM, "Transform tracks can't have a subpath '" + np + "'.");
95 			}
96 
97 			Spatial *sp = Object::cast_to<Spatial>(node);
98 
99 			if (!sp) {
100 
101 				path_cache.push_back(Path());
102 				ERR_CONTINUE_MSG(!sp, "Transform track not of type Spatial '" + np + "'.");
103 			}
104 
105 			if (np.get_subname_count() == 1) {
106 				StringName property = np.get_subname(0);
107 				String ps = property;
108 
109 				Skeleton *sk = Object::cast_to<Skeleton>(node);
110 				if (!sk) {
111 
112 					path_cache.push_back(Path());
113 					ERR_CONTINUE_MSG(!sk, "Property defined in Transform track, but not a Skeleton! '" + np + "'.");
114 				}
115 
116 				int idx = sk->find_bone(ps);
117 				if (idx == -1) {
118 					path_cache.push_back(Path());
119 					ERR_CONTINUE_MSG(idx == -1, "Property defined in Transform track, but not a Skeleton Bone! '" + np + "'.");
120 				}
121 
122 				path.bone_idx = idx;
123 				path.skeleton = sk;
124 			}
125 
126 			path.spatial = sp;
127 
128 		} else {
129 			if (np.get_subname_count() > 0) {
130 
131 				RES res2;
132 				Vector<StringName> leftover_subpath;
133 
134 				// We don't want to cache the last resource unless it is a method call
135 				bool is_method = animation->track_get_type(i) == Animation::TYPE_METHOD;
136 				root->get_node_and_resource(np, res2, leftover_subpath, is_method);
137 
138 				if (res2.is_valid()) {
139 					path.resource = res2;
140 				} else {
141 					path.node = node;
142 				}
143 				path.object = res2.is_valid() ? res2.ptr() : (Object *)node;
144 				path.subpath = leftover_subpath;
145 
146 			} else {
147 
148 				path.node = node;
149 				path.object = node;
150 				path.subpath = np.get_subnames();
151 			}
152 		}
153 
154 		if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
155 
156 			if (np.get_subname_count() == 0) {
157 
158 				path_cache.push_back(Path());
159 				ERR_CONTINUE_MSG(np.get_subname_count() == 0, "Value Track lacks property: " + np + ".");
160 			}
161 
162 		} else if (animation->track_get_type(i) == Animation::TYPE_METHOD) {
163 
164 			if (path.subpath.size() != 0) { // Trying to call a method of a non-resource
165 
166 				path_cache.push_back(Path());
167 				ERR_CONTINUE_MSG(path.subpath.size() != 0, "Method Track has property: " + np + ".");
168 			}
169 		}
170 
171 		path.valid = true;
172 
173 		path_cache.push_back(path);
174 
175 		if (!connected_nodes.has(path.node)) {
176 			connected_nodes.insert(path.node);
177 			path.node->connect("tree_exiting", this, "_node_exit_tree", Node::make_binds(path.node), CONNECT_ONESHOT);
178 		}
179 	}
180 
181 	cache_dirty = false;
182 	cache_valid = true;
183 }
184 
set_track_transform(int p_idx,const Transform & p_transform)185 void AnimationCache::set_track_transform(int p_idx, const Transform &p_transform) {
186 
187 	if (cache_dirty)
188 		_update_cache();
189 
190 	ERR_FAIL_COND(!cache_valid);
191 	ERR_FAIL_INDEX(p_idx, path_cache.size());
192 	Path &p = path_cache.write[p_idx];
193 	if (!p.valid)
194 		return;
195 
196 	ERR_FAIL_COND(!p.node);
197 	ERR_FAIL_COND(!p.spatial);
198 
199 	if (p.skeleton) {
200 		p.skeleton->set_bone_pose(p.bone_idx, p_transform);
201 	} else {
202 		p.spatial->set_transform(p_transform);
203 	}
204 }
205 
set_track_value(int p_idx,const Variant & p_value)206 void AnimationCache::set_track_value(int p_idx, const Variant &p_value) {
207 
208 	if (cache_dirty)
209 		_update_cache();
210 
211 	ERR_FAIL_COND(!cache_valid);
212 	ERR_FAIL_INDEX(p_idx, path_cache.size());
213 	Path &p = path_cache.write[p_idx];
214 	if (!p.valid)
215 		return;
216 
217 	ERR_FAIL_COND(!p.object);
218 	p.object->set_indexed(p.subpath, p_value);
219 }
220 
call_track(int p_idx,const StringName & p_method,const Variant ** p_args,int p_argcount,Variant::CallError & r_error)221 void AnimationCache::call_track(int p_idx, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
222 
223 	if (cache_dirty)
224 		_update_cache();
225 
226 	ERR_FAIL_COND(!cache_valid);
227 	ERR_FAIL_INDEX(p_idx, path_cache.size());
228 	Path &p = path_cache.write[p_idx];
229 	if (!p.valid)
230 		return;
231 
232 	ERR_FAIL_COND(!p.object);
233 	p.object->call(p_method, p_args, p_argcount, r_error);
234 }
235 
set_all(float p_time,float p_delta)236 void AnimationCache::set_all(float p_time, float p_delta) {
237 
238 	if (cache_dirty)
239 		_update_cache();
240 
241 	ERR_FAIL_COND(!cache_valid);
242 
243 	int tc = animation->get_track_count();
244 	for (int i = 0; i < tc; i++) {
245 
246 		switch (animation->track_get_type(i)) {
247 
248 			case Animation::TYPE_TRANSFORM: {
249 
250 				Vector3 loc, scale;
251 				Quat rot;
252 				animation->transform_track_interpolate(i, p_time, &loc, &rot, &scale);
253 				Transform tr(Basis(rot), loc);
254 				tr.basis.scale(scale);
255 
256 				set_track_transform(i, tr);
257 
258 			} break;
259 			case Animation::TYPE_VALUE: {
260 
261 				if (animation->value_track_get_update_mode(i) == Animation::UPDATE_CONTINUOUS || (animation->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE && p_delta == 0)) {
262 					Variant v = animation->value_track_interpolate(i, p_time);
263 					set_track_value(i, v);
264 				} else {
265 
266 					List<int> indices;
267 					animation->value_track_get_key_indices(i, p_time, p_delta, &indices);
268 
269 					for (List<int>::Element *E = indices.front(); E; E = E->next()) {
270 
271 						Variant v = animation->track_get_key_value(i, E->get());
272 						set_track_value(i, v);
273 					}
274 				}
275 
276 			} break;
277 			case Animation::TYPE_METHOD: {
278 
279 				List<int> indices;
280 				animation->method_track_get_key_indices(i, p_time, p_delta, &indices);
281 
282 				for (List<int>::Element *E = indices.front(); E; E = E->next()) {
283 
284 					Vector<Variant> args = animation->method_track_get_params(i, E->get());
285 					StringName name = animation->method_track_get_name(i, E->get());
286 					Variant::CallError err;
287 
288 					if (!args.size()) {
289 
290 						call_track(i, name, NULL, 0, err);
291 					} else {
292 
293 						Vector<const Variant *> argptrs;
294 						argptrs.resize(args.size());
295 						for (int j = 0; j < args.size(); j++) {
296 
297 							argptrs.write[j] = &args.write[j];
298 						}
299 
300 						call_track(i, name, (const Variant **)&argptrs[0], args.size(), err);
301 					}
302 				}
303 
304 			} break;
305 			default: {
306 			}
307 		}
308 	}
309 }
310 
set_animation(const Ref<Animation> & p_animation)311 void AnimationCache::set_animation(const Ref<Animation> &p_animation) {
312 
313 	_clear_cache();
314 
315 	if (animation.is_valid())
316 		animation->disconnect("changed", this, "_animation_changed");
317 
318 	animation = p_animation;
319 
320 	if (animation.is_valid())
321 		animation->connect("changed", this, "_animation_changed");
322 }
323 
_bind_methods()324 void AnimationCache::_bind_methods() {
325 
326 	ClassDB::bind_method(D_METHOD("_node_exit_tree"), &AnimationCache::_node_exit_tree);
327 	ClassDB::bind_method(D_METHOD("_animation_changed"), &AnimationCache::_animation_changed);
328 }
329 
set_root(Node * p_root)330 void AnimationCache::set_root(Node *p_root) {
331 
332 	_clear_cache();
333 	root = p_root;
334 }
335 
AnimationCache()336 AnimationCache::AnimationCache() {
337 
338 	root = NULL;
339 	cache_dirty = true;
340 	cache_valid = false;
341 }
342