2014-02-10 02:10:30 +01:00
|
|
|
/*************************************************************************/
|
|
|
|
/* animation_cache.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 22:01:57 +01:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2014-02-10 02:10:30 +01:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
|
|
|
#include "animation_cache.h"
|
|
|
|
|
2014-11-06 01:20:42 +01:00
|
|
|
void AnimationCache::_node_exit_tree(Node *p_node) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
//it is one shot, so it disconnects upon arrival
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!connected_nodes.has(p_node));
|
|
|
|
|
|
|
|
connected_nodes.erase(p_node);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < path_cache.size(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (path_cache[i].node != p_node)
|
2014-02-10 02:10:30 +01:00
|
|
|
continue;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path_cache[i].valid = false; //invalidate path cache
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_animation_changed() {
|
|
|
|
|
|
|
|
_clear_cache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_clear_cache() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
while (connected_nodes.size()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
connected_nodes.front()->get()->disconnect("exit_tree", this, "_node_exit_tree");
|
2014-02-10 02:10:30 +01:00
|
|
|
connected_nodes.erase(connected_nodes.front());
|
|
|
|
}
|
2017-01-14 18:03:38 +01:00
|
|
|
path_cache.clear();
|
2017-03-19 00:36:26 +01:00
|
|
|
cache_valid = false;
|
|
|
|
cache_dirty = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_update_cache() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
cache_valid = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
ERR_FAIL_COND(!root);
|
2014-11-06 01:20:42 +01:00
|
|
|
ERR_FAIL_COND(!root->is_inside_tree());
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_FAIL_COND(animation.is_null());
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < animation->get_track_count(); i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
NodePath np = animation->track_get_path(i);
|
|
|
|
|
|
|
|
Node *node = root->get_node(np);
|
|
|
|
if (!node) {
|
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Invalid Track Path in Animation: " + np);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!node);
|
|
|
|
}
|
|
|
|
|
|
|
|
Path path;
|
|
|
|
|
|
|
|
Ref<Resource> res;
|
|
|
|
|
|
|
|
if (np.get_subname_count()) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Transform tracks can't have a subpath: " + np);
|
|
|
|
ERR_CONTINUE(animation->track_get_type(i) == Animation::TYPE_TRANSFORM);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RES res;
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int j = 0; j < np.get_subname_count(); j++) {
|
|
|
|
res = j == 0 ? node->get(np.get_subname(j)) : res->get(np.get_subname(j));
|
2014-02-10 02:10:30 +01:00
|
|
|
if (res.is_null())
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res.is_null()) {
|
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Invalid Track SubPath in Animation: " + np);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(res.is_null());
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path.resource = res;
|
|
|
|
path.object = res.ptr();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (animation->track_get_type(i) == Animation::TYPE_TRANSFORM) {
|
2014-02-10 02:10:30 +01:00
|
|
|
StringName property = np.get_property();
|
|
|
|
String ps = property;
|
|
|
|
|
|
|
|
Spatial *sp = node->cast_to<Spatial>();
|
|
|
|
|
|
|
|
if (!sp) {
|
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Transform track not of type Spatial: " + np);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!sp);
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (ps != "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
Skeleton *sk = node->cast_to<Skeleton>();
|
|
|
|
if (!sk) {
|
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton!: " + np);
|
2014-02-10 02:10:30 +01:00
|
|
|
ERR_CONTINUE(!sk);
|
|
|
|
}
|
|
|
|
|
|
|
|
int idx = sk->find_bone(ps);
|
2017-03-19 00:36:26 +01:00
|
|
|
if (idx == -1) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton Bone!: " + np);
|
|
|
|
ERR_CONTINUE(idx == -1);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path.bone_idx = idx;
|
|
|
|
path.skeleton = sk;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path.spatial = sp;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path.node = node;
|
|
|
|
path.object = node;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (np.get_property().operator String() == "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Value Track lacks property: " + np);
|
|
|
|
ERR_CONTINUE(np.get_property().operator String() == "");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path.property = np.get_property();
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
} else if (animation->track_get_type(i) == Animation::TYPE_METHOD) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (np.get_property().operator String() != "") {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
path_cache.push_back(Path());
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_EXPLAIN("Method Track has property: " + np);
|
|
|
|
ERR_CONTINUE(np.get_property().operator String() != "");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
path.valid = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
path_cache.push_back(path);
|
|
|
|
|
|
|
|
if (!connected_nodes.has(path.node)) {
|
|
|
|
connected_nodes.insert(path.node);
|
2017-03-19 00:36:26 +01:00
|
|
|
path.node->connect("exit_tree", this, "_node_exit_tree", Node::make_binds(path.node), CONNECT_ONESHOT);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
cache_dirty = false;
|
|
|
|
cache_valid = true;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void AnimationCache::set_track_transform(int p_idx, const Transform &p_transform) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (cache_dirty)
|
|
|
|
_update_cache();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_idx, path_cache.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
Path &p = path_cache[p_idx];
|
|
|
|
if (!p.valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!p.node);
|
|
|
|
ERR_FAIL_COND(!p.spatial);
|
|
|
|
|
|
|
|
if (p.skeleton) {
|
2017-03-19 00:36:26 +01:00
|
|
|
p.skeleton->set_bone_pose(p.bone_idx, p_transform);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
p.spatial->set_transform(p_transform);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void AnimationCache::set_track_value(int p_idx, const Variant &p_value) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (cache_dirty)
|
|
|
|
_update_cache();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_idx, path_cache.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
Path &p = path_cache[p_idx];
|
|
|
|
if (!p.valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!p.object);
|
2017-03-19 00:36:26 +01:00
|
|
|
p.object->set(p.property, p_value);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void AnimationCache::call_track(int p_idx, const StringName &p_method, const Variant **p_args, int p_argcount, Variant::CallError &r_error) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (cache_dirty)
|
|
|
|
_update_cache();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
2017-03-19 00:36:26 +01:00
|
|
|
ERR_FAIL_INDEX(p_idx, path_cache.size());
|
2014-02-10 02:10:30 +01:00
|
|
|
Path &p = path_cache[p_idx];
|
|
|
|
if (!p.valid)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!p.object);
|
2017-03-19 00:36:26 +01:00
|
|
|
p.object->call(p_method, p_args, p_argcount, r_error);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::set_all(float p_time, float p_delta) {
|
|
|
|
|
|
|
|
if (cache_dirty)
|
|
|
|
_update_cache();
|
|
|
|
|
|
|
|
ERR_FAIL_COND(!cache_valid);
|
|
|
|
|
|
|
|
int tc = animation->get_track_count();
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int i = 0; i < tc; i++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
switch (animation->track_get_type(i)) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
case Animation::TYPE_TRANSFORM: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector3 loc, scale;
|
2014-02-10 02:10:30 +01:00
|
|
|
Quat rot;
|
2017-03-19 00:36:26 +01:00
|
|
|
animation->transform_track_interpolate(i, p_time, &loc, &rot, &scale);
|
|
|
|
Transform tr(Matrix3(rot), loc);
|
2014-02-10 02:10:30 +01:00
|
|
|
tr.basis.scale(scale);
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
set_track_transform(i, tr);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
} break;
|
|
|
|
case Animation::TYPE_VALUE: {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
if (animation->value_track_get_update_mode(i) == Animation::UPDATE_CONTINUOUS || (animation->value_track_get_update_mode(i) == Animation::UPDATE_DISCRETE && p_delta == 0)) {
|
|
|
|
Variant v = animation->value_track_interpolate(i, p_time);
|
|
|
|
set_track_value(i, v);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
|
|
|
List<int> indices;
|
2017-03-19 00:36:26 +01:00
|
|
|
animation->value_track_get_key_indices(i, p_time, p_delta, &indices);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<int>::Element *E = indices.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Variant v = animation->track_get_key_value(i, E->get());
|
|
|
|
set_track_value(i, v);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
case Animation::TYPE_METHOD: {
|
|
|
|
|
|
|
|
List<int> indices;
|
2017-03-19 00:36:26 +01:00
|
|
|
animation->method_track_get_key_indices(i, p_time, p_delta, &indices);
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
for (List<int>::Element *E = indices.front(); E; E = E->next()) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<Variant> args = animation->method_track_get_params(i, E->get());
|
|
|
|
StringName name = animation->method_track_get_name(i, E->get());
|
2014-02-10 02:10:30 +01:00
|
|
|
Variant::CallError err;
|
|
|
|
|
|
|
|
if (!args.size()) {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
call_track(i, name, NULL, 0, err);
|
2014-02-10 02:10:30 +01:00
|
|
|
} else {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
Vector<Variant *> argptrs;
|
2014-02-10 02:10:30 +01:00
|
|
|
argptrs.resize(args.size());
|
2017-03-19 00:36:26 +01:00
|
|
|
for (int j = 0; j < args.size(); j++) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
argptrs[j] = &args[j];
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
call_track(i, name, (const Variant **)&argptrs[0], args.size(), err);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} break;
|
|
|
|
default: {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void AnimationCache::set_animation(const Ref<Animation> &p_animation) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_clear_cache();
|
|
|
|
|
|
|
|
if (animation.is_valid())
|
2017-03-19 00:36:26 +01:00
|
|
|
animation->disconnect("changed", this, "_animation_changed");
|
2014-02-10 02:10:30 +01:00
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
animation = p_animation;
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
if (animation.is_valid())
|
2017-03-19 00:36:26 +01:00
|
|
|
animation->connect("changed", this, "_animation_changed");
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationCache::_bind_methods() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
ObjectTypeDB::bind_method(_MD("_node_exit_tree"), &AnimationCache::_node_exit_tree);
|
|
|
|
ObjectTypeDB::bind_method(_MD("_animation_changed"), &AnimationCache::_animation_changed);
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
void AnimationCache::set_root(Node *p_root) {
|
2014-02-10 02:10:30 +01:00
|
|
|
|
|
|
|
_clear_cache();
|
2017-03-19 00:36:26 +01:00
|
|
|
root = p_root;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AnimationCache::AnimationCache() {
|
|
|
|
|
2017-03-19 00:36:26 +01:00
|
|
|
root = NULL;
|
|
|
|
cache_dirty = true;
|
|
|
|
cache_valid = false;
|
2014-02-10 02:10:30 +01:00
|
|
|
}
|