diff options
Diffstat (limited to 'scene/animation')
-rw-r--r-- | scene/animation/animation_blend_space_1d.cpp | 2 | ||||
-rw-r--r-- | scene/animation/animation_blend_space_1d.h | 2 | ||||
-rw-r--r-- | scene/animation/animation_blend_space_2d.cpp | 10 | ||||
-rw-r--r-- | scene/animation/animation_blend_space_2d.h | 3 | ||||
-rw-r--r-- | scene/animation/animation_blend_tree.cpp | 4 | ||||
-rw-r--r-- | scene/animation/animation_blend_tree.h | 5 | ||||
-rw-r--r-- | scene/animation/animation_cache.cpp | 34 | ||||
-rw-r--r-- | scene/animation/animation_node_state_machine.cpp | 129 | ||||
-rw-r--r-- | scene/animation/animation_node_state_machine.h | 3 | ||||
-rw-r--r-- | scene/animation/animation_player.cpp | 127 | ||||
-rw-r--r-- | scene/animation/animation_player.h | 58 | ||||
-rw-r--r-- | scene/animation/animation_tree.cpp | 97 | ||||
-rw-r--r-- | scene/animation/animation_tree.h | 9 | ||||
-rw-r--r-- | scene/animation/animation_tree_player.cpp | 65 | ||||
-rw-r--r-- | scene/animation/animation_tree_player.h | 8 | ||||
-rw-r--r-- | scene/animation/root_motion_view.h | 5 | ||||
-rw-r--r-- | scene/animation/skeleton_ik.cpp | 38 | ||||
-rw-r--r-- | scene/animation/skeleton_ik.h | 6 | ||||
-rw-r--r-- | scene/animation/tween.cpp | 614 | ||||
-rw-r--r-- | scene/animation/tween.h | 1 |
20 files changed, 778 insertions, 442 deletions
diff --git a/scene/animation/animation_blend_space_1d.cpp b/scene/animation/animation_blend_space_1d.cpp index dded44b990..416a291da1 100644 --- a/scene/animation/animation_blend_space_1d.cpp +++ b/scene/animation/animation_blend_space_1d.cpp @@ -266,7 +266,7 @@ float AnimationNodeBlendSpace1D::process(float p_time, bool p_seek) { // fill in weights - if (point_lower == -1) { + if (point_lower == -1 && point_higher != -1) { // we are on the left side, no other point to the left // we just play the next point. diff --git a/scene/animation/animation_blend_space_1d.h b/scene/animation/animation_blend_space_1d.h index dfac88b712..2e2d848450 100644 --- a/scene/animation/animation_blend_space_1d.h +++ b/scene/animation/animation_blend_space_1d.h @@ -34,7 +34,7 @@ #include "scene/animation/animation_tree.h" class AnimationNodeBlendSpace1D : public AnimationRootNode { - GDCLASS(AnimationNodeBlendSpace1D, AnimationRootNode) + GDCLASS(AnimationNodeBlendSpace1D, AnimationRootNode); enum { MAX_BLEND_POINTS = 64 diff --git a/scene/animation/animation_blend_space_2d.cpp b/scene/animation/animation_blend_space_2d.cpp index 1fe14e633b..75031f0149 100644 --- a/scene/animation/animation_blend_space_2d.cpp +++ b/scene/animation/animation_blend_space_2d.cpp @@ -461,9 +461,9 @@ float AnimationNodeBlendSpace2D::process(float p_time, bool p_seek) { points[j], points[(j + 1) % 3] }; - Vector2 closest = Geometry::get_closest_point_to_segment_2d(blend_pos, s); - if (first || closest.distance_to(blend_pos) < best_point.distance_to(blend_pos)) { - best_point = closest; + Vector2 closest2 = Geometry::get_closest_point_to_segment_2d(blend_pos, s); + if (first || closest2.distance_to(blend_pos) < best_point.distance_to(blend_pos)) { + best_point = closest2; blend_triangle = i; first = false; float d = s[0].distance_to(s[1]); @@ -472,7 +472,7 @@ float AnimationNodeBlendSpace2D::process(float p_time, bool p_seek) { blend_weights[(j + 1) % 3] = 0.0; blend_weights[(j + 2) % 3] = 0.0; } else { - float c = s[0].distance_to(closest) / d; + float c = s[0].distance_to(closest2) / d; blend_weights[j] = 1.0 - c; blend_weights[(j + 1) % 3] = c; @@ -527,7 +527,7 @@ float AnimationNodeBlendSpace2D::process(float p_time, bool p_seek) { } } - if (new_closest != closest) { + if (new_closest != closest && new_closest != -1) { float from = 0; if (blend_mode == BLEND_MODE_DISCRETE_CARRY && closest != -1) { diff --git a/scene/animation/animation_blend_space_2d.h b/scene/animation/animation_blend_space_2d.h index c21360beb9..66ba9b0db7 100644 --- a/scene/animation/animation_blend_space_2d.h +++ b/scene/animation/animation_blend_space_2d.h @@ -34,7 +34,8 @@ #include "scene/animation/animation_tree.h" class AnimationNodeBlendSpace2D : public AnimationRootNode { - GDCLASS(AnimationNodeBlendSpace2D, AnimationRootNode) + GDCLASS(AnimationNodeBlendSpace2D, AnimationRootNode); + public: enum BlendMode { BLEND_MODE_INTERPOLATED, diff --git a/scene/animation/animation_blend_tree.cpp b/scene/animation/animation_blend_tree.cpp index 0daa574f72..20a09696e1 100644 --- a/scene/animation/animation_blend_tree.cpp +++ b/scene/animation/animation_blend_tree.cpp @@ -363,6 +363,7 @@ AnimationNodeOneShot::AnimationNodeOneShot() { fade_out = 0.1; autorestart = false; autorestart_delay = 1; + autorestart_random_delay = 0; mix = MIX_MODE_BLEND; sync = false; @@ -857,6 +858,7 @@ AnimationNodeTransition::AnimationNodeTransition() { time = "time"; current = "current"; prev_current = "prev_current"; + xfade = 0.0; enabled_inputs = 0; for (int i = 0; i < MAX_INPUTS; i++) { @@ -1047,7 +1049,7 @@ AnimationNodeBlendTree::ConnectionError AnimationNodeBlendTree::can_connect_node return CONNECTION_ERROR_NO_INPUT; } - if (!nodes.has(p_input_node)) { + if (p_input_node == p_output_node) { return CONNECTION_ERROR_SAME_NODE; } diff --git a/scene/animation/animation_blend_tree.h b/scene/animation/animation_blend_tree.h index c16dcb1b8c..a6ef78d82e 100644 --- a/scene/animation/animation_blend_tree.h +++ b/scene/animation/animation_blend_tree.h @@ -322,7 +322,8 @@ public: }; class AnimationNodeOutput : public AnimationNode { - GDCLASS(AnimationNodeOutput, AnimationNode) + GDCLASS(AnimationNodeOutput, AnimationNode); + public: virtual String get_caption() const; virtual float process(float p_time, bool p_seek); @@ -332,7 +333,7 @@ public: ///// class AnimationNodeBlendTree : public AnimationRootNode { - GDCLASS(AnimationNodeBlendTree, AnimationRootNode) + GDCLASS(AnimationNodeBlendTree, AnimationRootNode); struct Node { Ref<AnimationNode> node; diff --git a/scene/animation/animation_cache.cpp b/scene/animation/animation_cache.cpp index 6c67af5777..e26bd5b5a1 100644 --- a/scene/animation/animation_cache.cpp +++ b/scene/animation/animation_cache.cpp @@ -80,8 +80,7 @@ void AnimationCache::_update_cache() { if (!node) { path_cache.push_back(Path()); - ERR_EXPLAIN("Invalid Track Path in Animation: " + np); - ERR_CONTINUE(!node); + ERR_CONTINUE_MSG(!node, "Invalid track path in animation: " + np + "."); } Path path; @@ -92,8 +91,7 @@ void AnimationCache::_update_cache() { if (np.get_subname_count() > 1) { path_cache.push_back(Path()); - ERR_EXPLAIN("Transform tracks can't have a subpath: " + np); - ERR_CONTINUE(animation->track_get_type(i) == Animation::TYPE_TRANSFORM); + ERR_CONTINUE_MSG(animation->track_get_type(i) == Animation::TYPE_TRANSFORM, "Transform tracks can't have a subpath: " + np + "."); } Spatial *sp = Object::cast_to<Spatial>(node); @@ -101,8 +99,7 @@ void AnimationCache::_update_cache() { if (!sp) { path_cache.push_back(Path()); - ERR_EXPLAIN("Transform track not of type Spatial: " + np); - ERR_CONTINUE(!sp); + ERR_CONTINUE_MSG(!sp, "Transform track not of type Spatial: " + np + "."); } if (np.get_subname_count() == 1) { @@ -113,15 +110,13 @@ void AnimationCache::_update_cache() { if (!sk) { path_cache.push_back(Path()); - ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton!: " + np); - ERR_CONTINUE(!sk); + ERR_CONTINUE_MSG(!sk, "Property defined in Transform track, but not a Skeleton!: " + np + "."); } int idx = sk->find_bone(ps); if (idx == -1) { path_cache.push_back(Path()); - ERR_EXPLAIN("Property defined in Transform track, but not a Skeleton Bone!: " + np); - ERR_CONTINUE(idx == -1); + ERR_CONTINUE_MSG(idx == -1, "Property defined in Transform track, but not a Skeleton Bone!: " + np + "."); } path.bone_idx = idx; @@ -133,19 +128,19 @@ void AnimationCache::_update_cache() { } else { if (np.get_subname_count() > 0) { - RES res; + RES res2; Vector<StringName> leftover_subpath; // We don't want to cache the last resource unless it is a method call bool is_method = animation->track_get_type(i) == Animation::TYPE_METHOD; - root->get_node_and_resource(np, res, leftover_subpath, is_method); + root->get_node_and_resource(np, res2, leftover_subpath, is_method); - if (res.is_valid()) { - path.resource = res; + if (res2.is_valid()) { + path.resource = res2; } else { path.node = node; } - path.object = res.is_valid() ? res.ptr() : (Object *)node; + path.object = res2.is_valid() ? res2.ptr() : (Object *)node; path.subpath = leftover_subpath; } else { @@ -161,8 +156,7 @@ void AnimationCache::_update_cache() { if (np.get_subname_count() == 0) { path_cache.push_back(Path()); - ERR_EXPLAIN("Value Track lacks property: " + np); - ERR_CONTINUE(np.get_subname_count() == 0); + ERR_CONTINUE_MSG(np.get_subname_count() == 0, "Value Track lacks property: " + np + "."); } } else if (animation->track_get_type(i) == Animation::TYPE_METHOD) { @@ -170,8 +164,7 @@ void AnimationCache::_update_cache() { if (path.subpath.size() != 0) { // Trying to call a method of a non-resource path_cache.push_back(Path()); - ERR_EXPLAIN("Method Track has property: " + np); - ERR_CONTINUE(path.subpath.size() != 0); + ERR_CONTINUE_MSG(path.subpath.size() != 0, "Method Track has property: " + np + "."); } } @@ -309,7 +302,8 @@ void AnimationCache::set_all(float p_time, float p_delta) { } } break; - default: {} + default: { + } } } } diff --git a/scene/animation/animation_node_state_machine.cpp b/scene/animation/animation_node_state_machine.cpp index 56faba27cf..65bf1e0134 100644 --- a/scene/animation/animation_node_state_machine.cpp +++ b/scene/animation/animation_node_state_machine.cpp @@ -178,11 +178,11 @@ float AnimationNodeStateMachinePlayback::get_current_length() const { return len_current; } -bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, const StringName &p_travel) { +bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *p_state_machine, const StringName &p_travel) { ERR_FAIL_COND_V(!playing, false); - ERR_FAIL_COND_V(!sm->states.has(p_travel), false); - ERR_FAIL_COND_V(!sm->states.has(current), false); + ERR_FAIL_COND_V(!p_state_machine->states.has(p_travel), false); + ERR_FAIL_COND_V(!p_state_machine->states.has(current), false); path.clear(); //a new one will be needed @@ -191,25 +191,25 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, c loops_current = 0; // reset loops, so fade does not happen immediately - Vector2 current_pos = sm->states[current].position; - Vector2 target_pos = sm->states[p_travel].position; + Vector2 current_pos = p_state_machine->states[current].position; + Vector2 target_pos = p_state_machine->states[p_travel].position; Map<StringName, AStarCost> cost_map; List<int> open_list; //build open list - for (int i = 0; i < sm->transitions.size(); i++) { - if (sm->transitions[i].from == current) { + for (int i = 0; i < p_state_machine->transitions.size(); i++) { + if (p_state_machine->transitions[i].from == current) { open_list.push_back(i); - float cost = sm->states[sm->transitions[i].to].position.distance_to(current_pos); - cost *= sm->transitions[i].transition->get_priority(); + float cost = p_state_machine->states[p_state_machine->transitions[i].to].position.distance_to(current_pos); + cost *= p_state_machine->transitions[i].transition->get_priority(); AStarCost ap; ap.prev = current; ap.distance = cost; - cost_map[sm->transitions[i].to] = ap; + cost_map[p_state_machine->transitions[i].to] = ap; - if (sm->transitions[i].to == p_travel) { //prematurely found it! :D + if (p_state_machine->transitions[i].to == p_travel) { //prematurely found it! :D path.push_back(p_travel); return true; } @@ -230,42 +230,43 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, c for (List<int>::Element *E = open_list.front(); E; E = E->next()) { - float cost = cost_map[sm->transitions[E->get()].to].distance; - cost += sm->states[sm->transitions[E->get()].to].position.distance_to(target_pos); + float cost = cost_map[p_state_machine->transitions[E->get()].to].distance; + cost += p_state_machine->states[p_state_machine->transitions[E->get()].to].position.distance_to(target_pos); if (cost < least_cost) { least_cost_transition = E; + least_cost = cost; } } - StringName transition_prev = sm->transitions[least_cost_transition->get()].from; - StringName transition = sm->transitions[least_cost_transition->get()].to; + StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].from; + StringName transition = p_state_machine->transitions[least_cost_transition->get()].to; - for (int i = 0; i < sm->transitions.size(); i++) { - if (sm->transitions[i].from != transition || sm->transitions[i].to == transition_prev) { + for (int i = 0; i < p_state_machine->transitions.size(); i++) { + if (p_state_machine->transitions[i].from != transition || p_state_machine->transitions[i].to == transition_prev) { continue; //not interested on those } - float distance = sm->states[sm->transitions[i].from].position.distance_to(sm->states[sm->transitions[i].to].position); - distance *= sm->transitions[i].transition->get_priority(); - distance += cost_map[sm->transitions[i].from].distance; + float distance = p_state_machine->states[p_state_machine->transitions[i].from].position.distance_to(p_state_machine->states[p_state_machine->transitions[i].to].position); + distance *= p_state_machine->transitions[i].transition->get_priority(); + distance += cost_map[p_state_machine->transitions[i].from].distance; - if (cost_map.has(sm->transitions[i].to)) { + if (cost_map.has(p_state_machine->transitions[i].to)) { //oh this was visited already, can we win the cost? - if (distance < cost_map[sm->transitions[i].to].distance) { - cost_map[sm->transitions[i].to].distance = distance; - cost_map[sm->transitions[i].to].prev = sm->transitions[i].from; + if (distance < cost_map[p_state_machine->transitions[i].to].distance) { + cost_map[p_state_machine->transitions[i].to].distance = distance; + cost_map[p_state_machine->transitions[i].to].prev = p_state_machine->transitions[i].from; } } else { //add to open list AStarCost ac; - ac.prev = sm->transitions[i].from; + ac.prev = p_state_machine->transitions[i].from; ac.distance = distance; - cost_map[sm->transitions[i].to] = ac; + cost_map[p_state_machine->transitions[i].to] = ac; open_list.push_back(i); - if (sm->transitions[i].to == p_travel) { + if (p_state_machine->transitions[i].to == p_travel) { found_route = true; break; } @@ -291,12 +292,12 @@ bool AnimationNodeStateMachinePlayback::_travel(AnimationNodeStateMachine *sm, c return true; } -float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, float p_time, bool p_seek) { +float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, float p_time, bool p_seek) { //if not playing and it can restart, then restart if (!playing && start_request == StringName()) { - if (!stop_request && sm->start_node) { - start(sm->start_node); + if (!stop_request && p_state_machine->start_node) { + start(p_state_machine->start_node); } else { return 0; } @@ -314,12 +315,12 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, if (start_request_travel) { if (!playing) { + String node_name = start_request; start_request = StringName(); - ERR_EXPLAIN("Can't travel to '" + String(start_request) + "' if state machine is not active."); - ERR_FAIL_V(0); + ERR_FAIL_V_MSG(0, "Can't travel to '" + node_name + "' if state machine is not playing."); } - if (!_travel(sm, start_request)) { + if (!_travel(p_state_machine, start_request)) { //can't travel, then teleport path.clear(); current = start_request; @@ -338,16 +339,16 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, if (do_start) { - if (sm->start_node != StringName() && p_seek && p_time == 0) { - current = sm->start_node; + if (p_state_machine->start_node != StringName() && p_seek && p_time == 0) { + current = p_state_machine->start_node; } - len_current = sm->blend_node(current, sm->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false); + len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 1.0, AnimationNode::FILTER_IGNORE, false); pos_current = 0; loops_current = 0; } - if (!sm->states.has(current)) { + if (!p_state_machine->states.has(current)) { playing = false; //current does not exist current = StringName(); return 0; @@ -356,7 +357,7 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, if (fading_from != StringName()) { - if (!sm->states.has(fading_from)) { + if (!p_state_machine->states.has(fading_from)) { fading_from = StringName(); } else { if (!p_seek) { @@ -369,11 +370,11 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, } } - float rem = sm->blend_node(current, sm->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false); + float rem = p_state_machine->blend_node(current, p_state_machine->states[current].node, p_time, p_seek, fade_blend, AnimationNode::FILTER_IGNORE, false); if (fading_from != StringName()) { - sm->blend_node(current, sm->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false); + p_state_machine->blend_node(fading_from, p_state_machine->states[fading_from].node, p_time, p_seek, 1.0 - fade_blend, AnimationNode::FILTER_IGNORE, false); } //guess playback position @@ -398,39 +399,40 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, if (path.size()) { - for (int i = 0; i < sm->transitions.size(); i++) { - if (sm->transitions[i].from == current && sm->transitions[i].to == path[0]) { - next_xfade = sm->transitions[i].transition->get_xfade_time(); - switch_mode = sm->transitions[i].transition->get_switch_mode(); + for (int i = 0; i < p_state_machine->transitions.size(); i++) { + if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) { + next_xfade = p_state_machine->transitions[i].transition->get_xfade_time(); + switch_mode = p_state_machine->transitions[i].transition->get_switch_mode(); next = path[0]; } } } else { float priority_best = 1e20; int auto_advance_to = -1; - for (int i = 0; i < sm->transitions.size(); i++) { + for (int i = 0; i < p_state_machine->transitions.size(); i++) { bool auto_advance = false; - if (sm->transitions[i].transition->has_auto_advance()) { + if (p_state_machine->transitions[i].transition->has_auto_advance()) { auto_advance = true; } - StringName advance_condition_name = sm->transitions[i].transition->get_advance_condition_name(); - if (advance_condition_name != StringName() && bool(sm->get_parameter(advance_condition_name))) { + StringName advance_condition_name = p_state_machine->transitions[i].transition->get_advance_condition_name(); + if (advance_condition_name != StringName() && bool(p_state_machine->get_parameter(advance_condition_name))) { auto_advance = true; } - if (sm->transitions[i].from == current && auto_advance) { + if (p_state_machine->transitions[i].from == current && auto_advance) { - if (sm->transitions[i].transition->get_priority() < priority_best) { + if (p_state_machine->transitions[i].transition->get_priority() <= priority_best) { + priority_best = p_state_machine->transitions[i].transition->get_priority(); auto_advance_to = i; } } } if (auto_advance_to != -1) { - next = sm->transitions[auto_advance_to].to; - next_xfade = sm->transitions[auto_advance_to].transition->get_xfade_time(); - switch_mode = sm->transitions[auto_advance_to].transition->get_switch_mode(); + next = p_state_machine->transitions[auto_advance_to].to; + next_xfade = p_state_machine->transitions[auto_advance_to].transition->get_xfade_time(); + switch_mode = p_state_machine->transitions[auto_advance_to].transition->get_switch_mode(); } } @@ -439,13 +441,13 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, bool goto_next = false; - if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE) { - goto_next = fading_from == StringName(); - } else { + if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) { goto_next = next_xfade >= (len_current - pos_current) || loops_current > 0; if (loops_current > 0) { next_xfade = 0; } + } else { + goto_next = fading_from == StringName(); } if (goto_next) { //loops should be used because fade time may be too small or zero and animation may have looped @@ -465,12 +467,12 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, } current = next; if (switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) { - len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false); + len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false); pos_current = MIN(pos_current, len_current); - sm->blend_node(current, sm->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false); + p_state_machine->blend_node(current, p_state_machine->states[current].node, pos_current, true, 0, AnimationNode::FILTER_IGNORE, false); } else { - len_current = sm->blend_node(current, sm->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false); + len_current = p_state_machine->blend_node(current, p_state_machine->states[current].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false); pos_current = 0; } @@ -480,9 +482,9 @@ float AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *sm, } //compute time left for transitions by using the end node - if (sm->end_node != StringName() && sm->end_node != current) { + if (p_state_machine->end_node != StringName() && p_state_machine->end_node != current) { - rem = sm->blend_node(sm->end_node, sm->states[sm->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false); + rem = p_state_machine->blend_node(p_state_machine->end_node, p_state_machine->states[p_state_machine->end_node].node, 0, true, 0, AnimationNode::FILTER_IGNORE, false); } return rem; @@ -597,6 +599,9 @@ void AnimationNodeStateMachine::remove_node(const StringName &p_name) { { Ref<AnimationNode> node = states[p_name].node; + + ERR_FAIL_COND(node.is_null()); + node->disconnect("tree_changed", this, "_tree_changed"); } @@ -959,7 +964,7 @@ void AnimationNodeStateMachine::_bind_methods() { ClassDB::bind_method(D_METHOD("set_end_node", "name"), &AnimationNodeStateMachine::set_end_node); ClassDB::bind_method(D_METHOD("get_end_node"), &AnimationNodeStateMachine::get_end_node); - ClassDB::bind_method(D_METHOD("set_graph_offset", "name"), &AnimationNodeStateMachine::set_graph_offset); + ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset); ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset); ClassDB::bind_method(D_METHOD("_tree_changed"), &AnimationNodeStateMachine::_tree_changed); diff --git a/scene/animation/animation_node_state_machine.h b/scene/animation/animation_node_state_machine.h index e47b940c35..26909a326e 100644 --- a/scene/animation/animation_node_state_machine.h +++ b/scene/animation/animation_node_state_machine.h @@ -34,7 +34,8 @@ #include "scene/animation/animation_tree.h" class AnimationNodeStateMachineTransition : public Resource { - GDCLASS(AnimationNodeStateMachineTransition, Resource) + GDCLASS(AnimationNodeStateMachineTransition, Resource); + public: enum SwitchMode { SWITCH_MODE_IMMEDIATE, diff --git a/scene/animation/animation_player.cpp b/scene/animation/animation_player.cpp index 1d2001c30d..051f832882 100644 --- a/scene/animation/animation_player.cpp +++ b/scene/animation/animation_player.cpp @@ -34,12 +34,23 @@ #include "core/message_queue.h" #include "scene/scene_string_names.h" #include "servers/audio/audio_stream.h" + #ifdef TOOLS_ENABLED +#include "editor/editor_settings.h" +#include "scene/2d/skeleton_2d.h" + void AnimatedValuesBackup::update_skeletons() { for (int i = 0; i < entries.size(); i++) { if (entries[i].bone_idx != -1) { + // 3D bone Object::cast_to<Skeleton>(entries[i].object)->notification(Skeleton::NOTIFICATION_UPDATE_SKELETON); + } else { + Bone2D *bone = Object::cast_to<Bone2D>(entries[i].object); + if (bone && bone->skeleton) { + // 2D bone + bone->skeleton->_update_transform(); + } } } } @@ -237,10 +248,7 @@ void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { RES resource; Vector<StringName> leftover_path; Node *child = parent->get_node_and_resource(a->track_get_path(i), resource, leftover_path); - if (!child) { - ERR_EXPLAIN("On Animation: '" + p_anim->name + "', couldn't resolve track: '" + String(a->track_get_path(i)) + "'"); - } - ERR_CONTINUE(!child); // couldn't find the child node + ERR_CONTINUE_MSG(!child, "On Animation: '" + p_anim->name + "', couldn't resolve track: '" + String(a->track_get_path(i)) + "'."); // couldn't find the child node uint32_t id = resource.is_valid() ? resource->get_instance_id() : child->get_instance_id(); int bone_idx = -1; @@ -287,7 +295,6 @@ void AnimationPlayer::_ensure_node_caches(AnimationData *p_anim) { // broken track (nonexistent bone) p_anim->node_cache[i]->skeleton = NULL; p_anim->node_cache[i]->spatial = NULL; - printf("bone is %ls\n", String(bone_name).c_str()); ERR_CONTINUE(p_anim->node_cache[i]->bone_idx < 0); } } else { @@ -344,10 +351,16 @@ void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, float for (int i = 0; i < a->get_track_count(); i++) { + // If an animation changes this animation (or it animates itself) + // we need to recreate our animation cache + if (p_anim->node_cache.size() != a->get_track_count()) { + _ensure_node_caches(p_anim); + } + TrackNodeCache *nc = p_anim->node_cache[i]; - if (!nc) // no node cache for this track, skip it - continue; + if (!nc) + continue; // no node cache for this track, skip it if (!a->track_is_enabled(i)) continue; // do nothing if the track is disabled @@ -545,14 +558,24 @@ void AnimationPlayer::_animation_process_animation(AnimationData *p_anim, float #endif if (can_call) { - MessageQueue::get_singleton()->push_call( - nc->node, - method, - s >= 1 ? params[0] : Variant(), - s >= 2 ? params[1] : Variant(), - s >= 3 ? params[2] : Variant(), - s >= 4 ? params[3] : Variant(), - s >= 5 ? params[4] : Variant()); + if (method_call_mode == ANIMATION_METHOD_CALL_DEFERRED) { + MessageQueue::get_singleton()->push_call( + nc->node, + method, + s >= 1 ? params[0] : Variant(), + s >= 2 ? params[1] : Variant(), + s >= 3 ? params[2] : Variant(), + s >= 4 ? params[3] : Variant(), + s >= 5 ? params[4] : Variant()); + } else { + nc->node->call( + method, + s >= 1 ? params[0] : Variant(), + s >= 2 ? params[1] : Variant(), + s >= 3 ? params[2] : Variant(), + s >= 4 ? params[3] : Variant(), + s >= 5 ? params[4] : Variant()); + } } } @@ -947,8 +970,7 @@ void AnimationPlayer::_animation_process(float p_delta) { Error AnimationPlayer::add_animation(const StringName &p_name, const Ref<Animation> &p_animation) { #ifdef DEBUG_ENABLED - ERR_EXPLAIN("Invalid animation name: " + String(p_name)); - ERR_FAIL_COND_V(String(p_name).find("/") != -1 || String(p_name).find(":") != -1 || String(p_name).find(",") != -1 || String(p_name).find("[") != -1, ERR_INVALID_PARAMETER); + ERR_FAIL_COND_V_MSG(String(p_name).find("/") != -1 || String(p_name).find(":") != -1 || String(p_name).find(",") != -1 || String(p_name).find("[") != -1, ERR_INVALID_PARAMETER, "Invalid animation name: " + String(p_name) + "."); #endif ERR_FAIL_COND_V(p_animation.is_null(), ERR_INVALID_PARAMETER); @@ -985,25 +1007,12 @@ void AnimationPlayer::remove_animation(const StringName &p_name) { void AnimationPlayer::_ref_anim(const Ref<Animation> &p_anim) { - if (used_anims.has(p_anim)) - used_anims[p_anim]++; - else { - used_anims[p_anim] = 1; - Ref<Animation>(p_anim)->connect("changed", this, "_animation_changed"); - } + Ref<Animation>(p_anim)->connect(SceneStringNames::get_singleton()->tracks_changed, this, "_animation_changed", varray(), CONNECT_REFERENCE_COUNTED); } void AnimationPlayer::_unref_anim(const Ref<Animation> &p_anim) { - ERR_FAIL_COND(!used_anims.has(p_anim)); - - int &n = used_anims[p_anim]; - n--; - if (n == 0) { - - Ref<Animation>(p_anim)->disconnect("changed", this, "_animation_changed"); - used_anims.erase(p_anim); - } + Ref<Animation>(p_anim)->disconnect(SceneStringNames::get_singleton()->tracks_changed, this, "_animation_changed"); } void AnimationPlayer::rename_animation(const StringName &p_name, const StringName &p_new_name) { @@ -1140,17 +1149,12 @@ void AnimationPlayer::play_backwards(const StringName &p_name, float p_custom_bl void AnimationPlayer::play(const StringName &p_name, float p_custom_blend, float p_custom_scale, bool p_from_end) { - //printf("animation is %ls\n", String(p_name).c_str()); - //ERR_FAIL_COND(!is_inside_scene()); StringName name = p_name; if (String(name) == "") name = playback.assigned; - if (!animation_set.has(name)) { - ERR_EXPLAIN("Animation not found: " + name); - ERR_FAIL(); - } + ERR_FAIL_COND_MSG(!animation_set.has(name), "Animation not found: " + name + "."); Playback &c = playback; @@ -1196,12 +1200,26 @@ void AnimationPlayer::play(const StringName &p_name, float p_custom_blend, float } } - _stop_playing_caches(); + if (get_current_animation() != p_name) { + _stop_playing_caches(); + } c.current.from = &animation_set[name]; - c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0; + + if (c.assigned != name) { // reset + c.current.pos = p_from_end ? c.current.from->animation->get_length() : 0; + } else { + if (p_from_end && c.current.pos == 0) { + // Animation reset BUT played backwards, set position to the end + c.current.pos = c.current.from->animation->get_length(); + } else if (!p_from_end && c.current.pos == c.current.from->animation->get_length()) { + // Animation resumed but already ended, set position to the beginning + c.current.pos = 0; + } + } + c.current.speed_scale = p_custom_scale; - c.assigned = p_name; + c.assigned = name; c.seeked = false; c.started = true; @@ -1280,6 +1298,7 @@ void AnimationPlayer::stop(bool p_reset) { if (p_reset) { c.current.from = NULL; c.current.speed_scale = 1; + c.current.pos = 0; } _set_process(false); queued.clear(); @@ -1456,6 +1475,16 @@ AnimationPlayer::AnimationProcessMode AnimationPlayer::get_animation_process_mod return animation_process_mode; } +void AnimationPlayer::set_method_call_mode(AnimationMethodCallMode p_mode) { + + method_call_mode = p_mode; +} + +AnimationPlayer::AnimationMethodCallMode AnimationPlayer::get_method_call_mode() const { + + return method_call_mode; +} + void AnimationPlayer::_set_process(bool p_process, bool p_force) { if (processing == p_process && !p_force) @@ -1507,13 +1536,19 @@ NodePath AnimationPlayer::get_root() const { void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const { +#ifdef TOOLS_ENABLED + const String quote_style = EDITOR_DEF("text_editor/completion/use_single_quotes", 0) ? "'" : "\""; +#else + const String quote_style = "\""; +#endif + String pf = p_function; if (p_function == "play" || p_function == "play_backwards" || p_function == "remove_animation" || p_function == "has_animation" || p_function == "queue") { List<StringName> al; get_animation_list(&al); for (List<StringName>::Element *E = al.front(); E; E = E->next()) { - r_options->push_back("\"" + String(E->get()) + "\""); + r_options->push_back(quote_style + String(E->get()) + quote_style); } } Node::get_argument_options(p_function, p_idx, r_options); @@ -1637,6 +1672,9 @@ void AnimationPlayer::_bind_methods() { ClassDB::bind_method(D_METHOD("set_animation_process_mode", "mode"), &AnimationPlayer::set_animation_process_mode); ClassDB::bind_method(D_METHOD("get_animation_process_mode"), &AnimationPlayer::get_animation_process_mode); + ClassDB::bind_method(D_METHOD("set_method_call_mode", "mode"), &AnimationPlayer::set_method_call_mode); + ClassDB::bind_method(D_METHOD("get_method_call_mode"), &AnimationPlayer::get_method_call_mode); + ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position); ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length); @@ -1655,6 +1693,7 @@ void AnimationPlayer::_bind_methods() { ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01"), "set_default_blend_time", "get_default_blend_time"); ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_active", PROPERTY_HINT_NONE, "", 0), "set_active", "is_active"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale"); + ADD_PROPERTY(PropertyInfo(Variant::INT, "method_call_mode", PROPERTY_HINT_ENUM, "Deferred,Immediate"), "set_method_call_mode", "get_method_call_mode"); ADD_SIGNAL(MethodInfo("animation_finished", PropertyInfo(Variant::STRING, "anim_name"))); ADD_SIGNAL(MethodInfo("animation_changed", PropertyInfo(Variant::STRING, "old_name"), PropertyInfo(Variant::STRING, "new_name"))); @@ -1664,6 +1703,9 @@ void AnimationPlayer::_bind_methods() { BIND_ENUM_CONSTANT(ANIMATION_PROCESS_PHYSICS); BIND_ENUM_CONSTANT(ANIMATION_PROCESS_IDLE); BIND_ENUM_CONSTANT(ANIMATION_PROCESS_MANUAL); + + BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_DEFERRED); + BIND_ENUM_CONSTANT(ANIMATION_METHOD_CALL_IMMEDIATE); } AnimationPlayer::AnimationPlayer() { @@ -1676,6 +1718,7 @@ AnimationPlayer::AnimationPlayer() { end_reached = false; end_notify = false; animation_process_mode = ANIMATION_PROCESS_IDLE; + method_call_mode = ANIMATION_METHOD_CALL_DEFERRED; processing = false; default_blend_time = 0; root = SceneStringNames::get_singleton()->path_pp; diff --git a/scene/animation/animation_player.h b/scene/animation/animation_player.h index 175072edcf..320fd2084c 100644 --- a/scene/animation/animation_player.h +++ b/scene/animation/animation_player.h @@ -35,9 +35,6 @@ #include "scene/3d/skeleton.h" #include "scene/3d/spatial.h" #include "scene/resources/animation.h" -/** - @author Juan Linietsky <reduzio@gmail.com> -*/ #ifdef TOOLS_ENABLED // To save/restore animated values @@ -68,6 +65,11 @@ public: ANIMATION_PROCESS_MANUAL, }; + enum AnimationMethodCallMode { + ANIMATION_METHOD_CALL_DEFERRED, + ANIMATION_METHOD_CALL_IMMEDIATE, + }; + private: enum { @@ -114,10 +116,12 @@ private: Variant value_accum; uint64_t accum_pass; Variant capture; - PropertyAnim() { - accum_pass = 0; - object = NULL; - } + + PropertyAnim() : + owner(NULL), + special(SP_NONE), + object(NULL), + accum_pass(0) {} }; Map<StringName, PropertyAnim> property_anim; @@ -129,25 +133,28 @@ private: float bezier_accum; Object *object; uint64_t accum_pass; - BezierAnim() { - accum_pass = 0; - bezier_accum = 0; - object = NULL; - } + + BezierAnim() : + owner(NULL), + bezier_accum(0.0), + object(NULL), + accum_pass(0) {} }; Map<StringName, BezierAnim> bezier_anim; - TrackNodeCache() { - skeleton = NULL; - spatial = NULL; - node = NULL; - accum_pass = 0; - bone_idx = -1; - node_2d = NULL; - audio_playing = false; - animation_playing = false; - } + TrackNodeCache() : + id(0), + node(NULL), + spatial(NULL), + node_2d(NULL), + skeleton(NULL), + bone_idx(-1), + accum_pass(0), + audio_playing(false), + audio_start(0.0), + audio_len(0.0), + animation_playing(false) {} }; struct TrackNodeCacheKey { @@ -176,8 +183,6 @@ private: int cache_update_bezier_size; Set<TrackNodeCache *> playing_caches; - Map<Ref<Animation>, int> used_anims; - uint64_t accum_pass; float speed_scale; float default_blend_time; @@ -243,6 +248,7 @@ private: String autoplay; AnimationProcessMode animation_process_mode; + AnimationMethodCallMode method_call_mode; bool processing; bool active; @@ -335,6 +341,9 @@ public: void set_animation_process_mode(AnimationProcessMode p_mode); AnimationProcessMode get_animation_process_mode() const; + void set_method_call_mode(AnimationMethodCallMode p_mode); + AnimationMethodCallMode get_method_call_mode() const; + void seek(float p_time, bool p_update = false); void seek_delta(float p_time, float p_delta); float get_current_animation_position() const; @@ -360,5 +369,6 @@ public: }; VARIANT_ENUM_CAST(AnimationPlayer::AnimationProcessMode); +VARIANT_ENUM_CAST(AnimationPlayer::AnimationMethodCallMode); #endif diff --git a/scene/animation/animation_tree.cpp b/scene/animation/animation_tree.cpp index dc022bcd70..bb7c400cfe 100644 --- a/scene/animation/animation_tree.cpp +++ b/scene/animation/animation_tree.cpp @@ -316,7 +316,7 @@ String AnimationNode::get_caption() const { void AnimationNode::add_input(const String &p_name) { //root nodes can't add inputs - ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != NULL) + ERR_FAIL_COND(Object::cast_to<AnimationRootNode>(this) != NULL); Input input; ERR_FAIL_COND(p_name.find(".") != -1 || p_name.find("/") != -1); input.name = p_name; @@ -686,7 +686,7 @@ bool AnimationTree::_update_caches(AnimationPlayer *player) { } break; default: { - ERR_PRINT("Animation corrupted (invalid track type"); + ERR_PRINT("Animation corrupted (invalid track type)"); continue; } } @@ -883,16 +883,16 @@ void AnimationTree::_process_graph(float p_delta) { TrackCacheTransform *t = static_cast<TrackCacheTransform *>(track); - if (t->process_pass != process_pass) { + if (track->root_motion) { - t->process_pass = process_pass; - t->loc = Vector3(); - t->rot = Quat(); - t->rot_blend_accum = 0; - t->scale = Vector3(); - } + if (t->process_pass != process_pass) { - if (track->root_motion) { + t->process_pass = process_pass; + t->loc = Vector3(); + t->rot = Quat(); + t->rot_blend_accum = 0; + t->scale = Vector3(1, 1, 1); + } float prev_time = time - delta; if (prev_time < 0) { @@ -946,7 +946,14 @@ void AnimationTree::_process_graph(float p_delta) { Error err = a->transform_track_interpolate(i, time, &loc, &rot, &scale); //ERR_CONTINUE(err!=OK); //used for testing, should be removed - scale -= Vector3(1.0, 1.0, 1.0); //helps make it work properly with Add nodes + if (t->process_pass != process_pass) { + + t->process_pass = process_pass; + t->loc = loc; + t->rot = rot; + t->rot_blend_accum = 0; + t->scale = scale; + } if (err != OK) continue; @@ -978,8 +985,7 @@ void AnimationTree::_process_graph(float p_delta) { continue; if (t->process_pass != process_pass) { - Variant::CallError ce; - t->value = Variant::construct(value.get_type(), NULL, 0, ce); //reset + t->value = value; t->process_pass = process_pass; } @@ -1009,10 +1015,10 @@ void AnimationTree::_process_graph(float p_delta) { a->method_track_get_key_indices(i, time, delta, &indices); - for (List<int>::Element *E = indices.front(); E; E = E->next()) { + for (List<int>::Element *F = indices.front(); F; F = F->next()) { - StringName method = a->method_track_get_name(i, E->get()); - Vector<Variant> params = a->method_track_get_params(i, E->get()); + StringName method = a->method_track_get_name(i, F->get()); + Vector<Variant> params = a->method_track_get_params(i, F->get()); int s = params.size(); @@ -1036,7 +1042,7 @@ void AnimationTree::_process_graph(float p_delta) { float bezier = a->bezier_track_interpolate(i, time); if (t->process_pass != process_pass) { - t->value = 0; + t->value = bezier; t->process_pass = process_pass; } @@ -1151,9 +1157,9 @@ void AnimationTree::_process_graph(float p_delta) { TrackCacheAnimation *t = static_cast<TrackCacheAnimation *>(track); - AnimationPlayer *player = Object::cast_to<AnimationPlayer>(t->object); + AnimationPlayer *player2 = Object::cast_to<AnimationPlayer>(t->object); - if (!player) + if (!player2) continue; if (delta == 0 || seeked) { @@ -1165,10 +1171,10 @@ void AnimationTree::_process_graph(float p_delta) { float pos = a->track_get_key_time(i, idx); StringName anim_name = a->animation_track_get_key_animation(i, idx); - if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) + if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) continue; - Ref<Animation> anim = player->get_animation(anim_name); + Ref<Animation> anim = player2->get_animation(anim_name); float at_anim_pos; @@ -1178,14 +1184,14 @@ void AnimationTree::_process_graph(float p_delta) { at_anim_pos = MAX(anim->get_length(), time - pos); //seek to end } - if (player->is_playing() || seeked) { - player->play(anim_name); - player->seek(at_anim_pos); + if (player2->is_playing() || seeked) { + player2->play(anim_name); + player2->seek(at_anim_pos); t->playing = true; playing_caches.insert(t); } else { - player->set_assigned_animation(anim_name); - player->seek(at_anim_pos, true); + player2->set_assigned_animation(anim_name); + player2->seek(at_anim_pos, true); } } else { //find stuff to play @@ -1195,15 +1201,15 @@ void AnimationTree::_process_graph(float p_delta) { int idx = to_play.back()->get(); StringName anim_name = a->animation_track_get_key_animation(i, idx); - if (String(anim_name) == "[stop]" || !player->has_animation(anim_name)) { + if (String(anim_name) == "[stop]" || !player2->has_animation(anim_name)) { if (playing_caches.has(t)) { playing_caches.erase(t); - player->stop(); + player2->stop(); t->playing = false; } } else { - player->play(anim_name); + player2->play(anim_name); t->playing = true; playing_caches.insert(t); } @@ -1233,8 +1239,6 @@ void AnimationTree::_process_graph(float p_delta) { Transform xform; xform.origin = t->loc; - t->scale += Vector3(1.0, 1.0, 1.0); //helps make it work properly with Add nodes and root motion - xform.basis.set_quat_scale(t->rot, t->scale); if (t->root_motion) { @@ -1268,7 +1272,8 @@ void AnimationTree::_process_graph(float p_delta) { t->object->set_indexed(t->subpath, t->value); } break; - default: {} //the rest don't matter + default: { + } //the rest don't matter } } } @@ -1293,9 +1298,17 @@ void AnimationTree::_notification(int p_what) { _clear_caches(); if (last_animation_player) { - Object *old_player = ObjectDB::get_instance(last_animation_player); - if (old_player) { - old_player->disconnect("caches_cleared", this, "_clear_caches"); + Object *player = ObjectDB::get_instance(last_animation_player); + if (player) { + player->disconnect("caches_cleared", this, "_clear_caches"); + } + } + } else if (p_what == NOTIFICATION_ENTER_TREE) { + if (last_animation_player) { + + Object *player = ObjectDB::get_instance(last_animation_player); + if (player) { + player->connect("caches_cleared", this, "_clear_caches"); } } } @@ -1329,15 +1342,15 @@ String AnimationTree::get_configuration_warning() const { if (!root.is_valid()) { if (warning != String()) { - warning += "\n"; + warning += "\n\n"; } - warning += TTR("A root AnimationNode for the graph is not set."); + warning += TTR("No root AnimationNode for the graph is set."); } if (!has_node(animation_player)) { if (warning != String()) { - warning += "\n"; + warning += "\n\n"; } warning += TTR("Path to an AnimationPlayer node containing animations is not set."); @@ -1348,7 +1361,7 @@ String AnimationTree::get_configuration_warning() const { if (!player) { if (warning != String()) { - warning += "\n"; + warning += "\n\n"; } warning += TTR("Path set for AnimationPlayer does not lead to an AnimationPlayer node."); @@ -1357,10 +1370,10 @@ String AnimationTree::get_configuration_warning() const { if (!player->has_node(player->get_root())) { if (warning != String()) { - warning += "\n"; + warning += "\n\n"; } - warning += TTR("AnimationPlayer root is not a valid node."); + warning += TTR("The AnimationPlayer root node is not a valid node."); return warning; } @@ -1399,6 +1412,7 @@ void AnimationTree::_update_properties_for_node(const String &p_base_path, Ref<A Vector<Activity> activity; for (int i = 0; i < node->get_input_count(); i++) { Activity a; + a.activity = 0; a.last_pass = 0; activity.push_back(a); } @@ -1567,6 +1581,7 @@ AnimationTree::AnimationTree() { active = false; cache_valid = false; setup_pass = 1; + process_pass = 1; started = true; properties_dirty = true; last_animation_player = 0; diff --git a/scene/animation/animation_tree.h b/scene/animation/animation_tree.h index 4c65b2a92c..e22d6e4c2d 100644 --- a/scene/animation/animation_tree.h +++ b/scene/animation/animation_tree.h @@ -41,7 +41,8 @@ class AnimationPlayer; class AnimationTree; class AnimationNode : public Resource { - GDCLASS(AnimationNode, Resource) + GDCLASS(AnimationNode, Resource); + public: enum FilterAction { FILTER_IGNORE, @@ -155,13 +156,15 @@ VARIANT_ENUM_CAST(AnimationNode::FilterAction) //root node does not allow inputs class AnimationRootNode : public AnimationNode { - GDCLASS(AnimationRootNode, AnimationNode) + GDCLASS(AnimationRootNode, AnimationNode); + public: AnimationRootNode() {} }; class AnimationTree : public Node { - GDCLASS(AnimationTree, Node) + GDCLASS(AnimationTree, Node); + public: enum AnimationProcessMode { ANIMATION_PROCESS_PHYSICS, diff --git a/scene/animation/animation_tree_player.cpp b/scene/animation/animation_tree_player.cpp index e3a21d8b46..8f6d53c21c 100644 --- a/scene/animation/animation_tree_player.cpp +++ b/scene/animation/animation_tree_player.cpp @@ -136,9 +136,9 @@ bool AnimationTreePlayer::_set(const StringName &p_name, const Variant &p_value) else animation_node_set_animation(id, node.get_valid("animation")); Array filters = node.get_valid("filter"); - for (int i = 0; i < filters.size(); i++) { + for (int j = 0; j < filters.size(); j++) { - animation_node_set_filter_path(id, filters[i], true); + animation_node_set_filter_path(id, filters[j], true); } } break; case NODE_ONESHOT: { @@ -150,9 +150,9 @@ bool AnimationTreePlayer::_set(const StringName &p_name, const Variant &p_value) oneshot_node_set_autorestart_delay(id, node.get_valid("autorestart_delay")); oneshot_node_set_autorestart_random_delay(id, node.get_valid("autorestart_random_delay")); Array filters = node.get_valid("filter"); - for (int i = 0; i < filters.size(); i++) { + for (int j = 0; j < filters.size(); j++) { - oneshot_node_set_filter_path(id, filters[i], true); + oneshot_node_set_filter_path(id, filters[j], true); } } break; @@ -162,9 +162,9 @@ bool AnimationTreePlayer::_set(const StringName &p_name, const Variant &p_value) case NODE_BLEND2: { blend2_node_set_amount(id, node.get_valid("blend")); Array filters = node.get_valid("filter"); - for (int i = 0; i < filters.size(); i++) { + for (int j = 0; j < filters.size(); j++) { - blend2_node_set_filter_path(id, filters[i], true); + blend2_node_set_filter_path(id, filters[j], true); } } break; case NODE_BLEND3: { @@ -193,7 +193,8 @@ bool AnimationTreePlayer::_set(const StringName &p_name, const Variant &p_value) } } break; - default: {}; + default: { + }; } } @@ -278,8 +279,8 @@ bool AnimationTreePlayer::_get(const StringName &p_name, Variant &r_ret) const { an->filter.get_key_list(&keys); k.resize(keys.size()); int i = 0; - for (List<NodePath>::Element *E = keys.front(); E; E = E->next()) { - k[i++] = E->get(); + for (List<NodePath>::Element *F = keys.front(); F; F = F->next()) { + k[i++] = F->get(); } node["filter"] = k; } break; @@ -297,8 +298,8 @@ bool AnimationTreePlayer::_get(const StringName &p_name, Variant &r_ret) const { osn->filter.get_key_list(&keys); k.resize(keys.size()); int i = 0; - for (List<NodePath>::Element *E = keys.front(); E; E = E->next()) { - k[i++] = E->get(); + for (List<NodePath>::Element *F = keys.front(); F; F = F->next()) { + k[i++] = F->get(); } node["filter"] = k; @@ -315,8 +316,8 @@ bool AnimationTreePlayer::_get(const StringName &p_name, Variant &r_ret) const { bn->filter.get_key_list(&keys); k.resize(keys.size()); int i = 0; - for (List<NodePath>::Element *E = keys.front(); E; E = E->next()) { - k[i++] = E->get(); + for (List<NodePath>::Element *F = keys.front(); F; F = F->next()) { + k[i++] = F->get(); } node["filter"] = k; @@ -352,7 +353,8 @@ bool AnimationTreePlayer::_get(const StringName &p_name, Variant &r_ret) const { node["transitions"] = transitions; } break; - default: {}; + default: { + }; } nodes.push_back(node); @@ -401,8 +403,7 @@ void AnimationTreePlayer::_notification(int p_what) { case NOTIFICATION_ENTER_TREE: { - ERR_EXPLAIN("AnimationTreePlayer has been deprecated. Use AnimationTree instead."); - WARN_DEPRECATED + WARN_DEPRECATED_MSG("AnimationTreePlayer has been deprecated. Use AnimationTree instead."); if (!processing) { //make sure that a previous process state was not saved @@ -772,7 +773,8 @@ float AnimationTreePlayer::_process_node(const StringName &p_node, AnimationNode } } break; - default: {} + default: { + } } return 0; @@ -874,15 +876,16 @@ void AnimationTreePlayer::_process_animation(float p_delta) { List<int> indices; a->method_track_get_key_indices(tr.local_track, anim_list->time, anim_list->step, &indices); - for (List<int>::Element *E = indices.front(); E; E = E->next()) { + for (List<int>::Element *F = indices.front(); F; F = F->next()) { - StringName method = a->method_track_get_name(tr.local_track, E->get()); - Vector<Variant> args = a->method_track_get_params(tr.local_track, E->get()); + StringName method = a->method_track_get_name(tr.local_track, F->get()); + Vector<Variant> args = a->method_track_get_params(tr.local_track, F->get()); args.resize(VARIANT_ARG_MAX); tr.track->object->call(method, args[0], args[1], args[2], args[3], args[4]); } } break; - default: {} + default: { + } } } } @@ -969,7 +972,8 @@ void AnimationTreePlayer::add_node(NodeType p_type, const StringName &p_node) { n = memnew(TransitionNode); } break; - default: {} + default: { + } } //n->name+=" "+itos(p_node); @@ -988,10 +992,9 @@ int AnimationTreePlayer::node_get_input_count(const StringName &p_node) const { ERR_FAIL_COND_V(!node_map.has(p_node), -1); return node_map[p_node]->inputs.size(); } -#define GET_NODE(m_type, m_cast) \ - ERR_FAIL_COND(!node_map.has(p_node)); \ - ERR_EXPLAIN("Invalid parameter for node type."); \ - ERR_FAIL_COND(node_map[p_node]->type != m_type); \ +#define GET_NODE(m_type, m_cast) \ + ERR_FAIL_COND(!node_map.has(p_node)); \ + ERR_FAIL_COND_MSG(node_map[p_node]->type != m_type, "Invalid parameter for node type."); \ m_cast *n = static_cast<m_cast *>(node_map[p_node]); void AnimationTreePlayer::animation_node_set_animation(const StringName &p_node, const Ref<Animation> &p_animation) { @@ -1204,10 +1207,9 @@ Point2 AnimationTreePlayer::node_get_position(const StringName &p_node) const { return node_map[p_node]->pos; } -#define GET_NODE_V(m_type, m_cast, m_ret) \ - ERR_FAIL_COND_V(!node_map.has(p_node), m_ret); \ - ERR_EXPLAIN("Invalid parameter for node type."); \ - ERR_FAIL_COND_V(node_map[p_node]->type != m_type, m_ret); \ +#define GET_NODE_V(m_type, m_cast, m_ret) \ + ERR_FAIL_COND_V(!node_map.has(p_node), m_ret); \ + ERR_FAIL_COND_V_MSG(node_map[p_node]->type != m_type, m_ret, "Invalid parameter for node type."); \ m_cast *n = static_cast<m_cast *>(node_map[p_node]); Ref<Animation> AnimationTreePlayer::animation_node_get_animation(const StringName &p_node) const { @@ -1362,8 +1364,7 @@ void AnimationTreePlayer::get_node_list(List<StringName> *p_node_list) const { void AnimationTreePlayer::remove_node(const StringName &p_node) { ERR_FAIL_COND(!node_map.has(p_node)); - ERR_EXPLAIN("Node 0 (output) can't be removed."); - ERR_FAIL_COND(p_node == out_name); + ERR_FAIL_COND_MSG(p_node == out_name, "Node 0 (output) can't be removed."); for (Map<StringName, NodeBase *>::Element *E = node_map.front(); E; E = E->next()) { diff --git a/scene/animation/animation_tree_player.h b/scene/animation/animation_tree_player.h index 12e514f7d5..9ec6325969 100644 --- a/scene/animation/animation_tree_player.h +++ b/scene/animation/animation_tree_player.h @@ -109,6 +109,14 @@ private: Variant value; bool skip; + + Track() : + id(0), + object(NULL), + spatial(NULL), + skeleton(NULL), + bone_idx(-1), + skip(false) {} }; typedef Map<TrackKey, Track> TrackMap; diff --git a/scene/animation/root_motion_view.h b/scene/animation/root_motion_view.h index bca265b1f0..b30b06229e 100644 --- a/scene/animation/root_motion_view.h +++ b/scene/animation/root_motion_view.h @@ -34,7 +34,8 @@ #include "scene/3d/visual_instance.h" class RootMotionView : public VisualInstance { - GDCLASS(RootMotionView, VisualInstance) + GDCLASS(RootMotionView, VisualInstance); + public: RID immediate; NodePath path; @@ -55,7 +56,7 @@ public: void set_animation_path(const NodePath &p_path); NodePath get_animation_path() const; - void set_color(const Color &p_path); + void set_color(const Color &p_color); Color get_color() const; void set_cell_size(float p_size); diff --git a/scene/animation/skeleton_ik.cpp b/scene/animation/skeleton_ik.cpp index 88fb2d5bfc..7a1b10792b 100644 --- a/scene/animation/skeleton_ik.cpp +++ b/scene/animation/skeleton_ik.cpp @@ -37,20 +37,20 @@ #ifndef _3D_DISABLED FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::find_child(const BoneId p_bone_id) { - for (int i = childs.size() - 1; 0 <= i; --i) { - if (p_bone_id == childs[i].bone) { - return &childs.write[i]; + for (int i = children.size() - 1; 0 <= i; --i) { + if (p_bone_id == children[i].bone) { + return &children.write[i]; } } return NULL; } FabrikInverseKinematic::ChainItem *FabrikInverseKinematic::ChainItem::add_child(const BoneId p_bone_id) { - const int infant_child_id = childs.size(); - childs.resize(infant_child_id + 1); - childs.write[infant_child_id].bone = p_bone_id; - childs.write[infant_child_id].parent_item = this; - return &childs.write[infant_child_id]; + const int infant_child_id = children.size(); + children.resize(infant_child_id + 1); + children.write[infant_child_id].bone = p_bone_id; + children.write[infant_child_id].parent_item = this; + return &children.write[infant_child_id]; } /// Build a chain that starts from the root to tip @@ -144,8 +144,8 @@ void FabrikInverseKinematic::update_chain(const Skeleton *p_sk, ChainItem *p_cha p_chain_item->initial_transform = p_sk->get_bone_global_pose(p_chain_item->bone); p_chain_item->current_pos = p_chain_item->initial_transform.origin; - for (int i = p_chain_item->childs.size() - 1; 0 <= i; --i) { - update_chain(p_sk, &p_chain_item->childs.write[i]); + for (int i = p_chain_item->children.size() - 1; 0 <= i; --i) { + update_chain(p_sk, &p_chain_item->children.write[i]); } } @@ -210,9 +210,9 @@ void FabrikInverseKinematic::solve_simple_forwards(Chain &r_chain, bool p_solve_ while (sub_chain_root) { // Reach the tip sub_chain_root->current_pos = origin; - if (!sub_chain_root->childs.empty()) { + if (!sub_chain_root->children.empty()) { - ChainItem &child(sub_chain_root->childs.write[0]); + ChainItem &child(sub_chain_root->children.write[0]); // Is not tip // So calculate next origin location @@ -302,10 +302,10 @@ void FabrikInverseKinematic::solve(Task *p_task, real_t blending_delta, bool ove Transform new_bone_pose(ci->initial_transform); new_bone_pose.origin = ci->current_pos; - if (!ci->childs.empty()) { + if (!ci->children.empty()) { /// Rotate basis - const Vector3 initial_ori((ci->childs[0].initial_transform.origin - ci->initial_transform.origin).normalized()); + const Vector3 initial_ori((ci->children[0].initial_transform.origin - ci->initial_transform.origin).normalized()); const Vector3 rot_axis(initial_ori.cross(ci->current_ori).normalized()); if (rot_axis[0] != 0 && rot_axis[1] != 0 && rot_axis[2] != 0) { @@ -322,8 +322,8 @@ void FabrikInverseKinematic::solve(Task *p_task, real_t blending_delta, bool ove p_task->skeleton->set_bone_global_pose(ci->bone, new_bone_pose); - if (!ci->childs.empty()) - ci = &ci->childs.write[0]; + if (!ci->children.empty()) + ci = &ci->children.write[0]; else ci = NULL; } @@ -335,7 +335,7 @@ void SkeletonIK::_validate_property(PropertyInfo &property) const { if (skeleton) { - String names; + String names("--,"); for (int i = 0; i < skeleton->get_bone_count(); i++) { if (i > 0) names += ","; @@ -406,6 +406,7 @@ void SkeletonIK::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { skeleton = Object::cast_to<Skeleton>(get_parent()); + set_process_priority(1); reload_chain(); } break; case NOTIFICATION_INTERNAL_PROCESS: { @@ -423,7 +424,6 @@ void SkeletonIK::_notification(int p_what) { } SkeletonIK::SkeletonIK() : - Node(), interpolation(1), override_tip_basis(true), use_magnet(false), @@ -432,8 +432,6 @@ SkeletonIK::SkeletonIK() : skeleton(NULL), target_node_override(NULL), task(NULL) { - - set_process_priority(1); } SkeletonIK::~SkeletonIK() { diff --git a/scene/animation/skeleton_ik.h b/scene/animation/skeleton_ik.h index 228184a2df..d2c5f56ace 100644 --- a/scene/animation/skeleton_ik.h +++ b/scene/animation/skeleton_ik.h @@ -49,7 +49,7 @@ class FabrikInverseKinematic { struct ChainItem { - Vector<ChainItem> childs; + Vector<ChainItem> children; ChainItem *parent_item; // Bone info @@ -165,7 +165,7 @@ protected: _validate_property(PropertyInfo &property) const; static void _bind_methods(); - virtual void _notification(int p_notification); + virtual void _notification(int p_what); public: SkeletonIK(); @@ -192,7 +192,7 @@ public: void set_use_magnet(bool p_use); bool is_using_magnet() const; - void set_magnet_position(const Vector3 &p_constraint); + void set_magnet_position(const Vector3 &p_local_position); const Vector3 &get_magnet_position() const; void set_min_distance(real_t p_min_distance); diff --git a/scene/animation/tween.cpp b/scene/animation/tween.cpp index 6a5d7839f4..2609924f33 100644 --- a/scene/animation/tween.cpp +++ b/scene/animation/tween.cpp @@ -34,10 +34,14 @@ void Tween::_add_pending_command(StringName p_key, const Variant &p_arg1, const Variant &p_arg2, const Variant &p_arg3, const Variant &p_arg4, const Variant &p_arg5, const Variant &p_arg6, const Variant &p_arg7, const Variant &p_arg8, const Variant &p_arg9, const Variant &p_arg10) { + // Add a new pending command and reference it pending_commands.push_back(PendingCommand()); PendingCommand &cmd = pending_commands.back()->get(); + // Update the command with the target key cmd.key = p_key; + + // Determine command argument count int &count = cmd.args; if (p_arg10.get_type() != Variant::NIL) count = 10; @@ -59,6 +63,9 @@ void Tween::_add_pending_command(StringName p_key, const Variant &p_arg1, const count = 2; else if (p_arg1.get_type() != Variant::NIL) count = 1; + + // Add the specified arguments to the command + // TODO: Make this a switch statement? if (count > 0) cmd.arg[0] = p_arg1; if (count > 1) @@ -83,10 +90,14 @@ void Tween::_add_pending_command(StringName p_key, const Variant &p_arg1, const void Tween::_process_pending_commands() { + // For each pending command... for (List<PendingCommand>::Element *E = pending_commands.front(); E; E = E->next()) { + // Get the command PendingCommand &cmd = E->get(); Variant::CallError err; + + // Grab all of the arguments for the command Variant *arg[10] = { &cmd.arg[0], &cmd.arg[1], @@ -99,16 +110,20 @@ void Tween::_process_pending_commands() { &cmd.arg[8], &cmd.arg[9], }; + + // Execute the command (and retrieve any errors) this->call(cmd.key, (const Variant **)arg, cmd.args, err); } + + // Clear the pending commands pending_commands.clear(); } bool Tween::_set(const StringName &p_name, const Variant &p_value) { + // Set the correct attribute based on the given name String name = p_name; - - if (name == "playback/speed" || name == "speed") { //bw compatibility + if (name == "playback/speed" || name == "speed") { // Backwards compatibility set_speed_scale(p_value); } else if (name == "playback/active") { @@ -122,69 +137,78 @@ bool Tween::_set(const StringName &p_name, const Variant &p_value) { bool Tween::_get(const StringName &p_name, Variant &r_ret) const { + // Get the correct attribute based on the given name String name = p_name; - - if (name == "playback/speed") { //bw compatibility - + if (name == "playback/speed") { // Backwards compatibility r_ret = speed_scale; - } else if (name == "playback/active") { + } else if (name == "playback/active") { r_ret = is_active(); - } else if (name == "playback/repeat") { + } else if (name == "playback/repeat") { r_ret = is_repeat(); } - return true; } void Tween::_get_property_list(List<PropertyInfo> *p_list) const { - + // Add the property info for the Tween object p_list->push_back(PropertyInfo(Variant::BOOL, "playback/active", PROPERTY_HINT_NONE, "")); p_list->push_back(PropertyInfo(Variant::BOOL, "playback/repeat", PROPERTY_HINT_NONE, "")); p_list->push_back(PropertyInfo(Variant::REAL, "playback/speed", PROPERTY_HINT_RANGE, "-64,64,0.01")); } void Tween::_notification(int p_what) { - + // What notification did we receive? switch (p_what) { case NOTIFICATION_ENTER_TREE: { - + // Are we not already active? if (!is_active()) { - //make sure that a previous process state was not saved - //only process if "processing" is set + // Make sure that a previous process state was not saved + // Only process if "processing" is set set_physics_process_internal(false); set_process_internal(false); } } break; - case NOTIFICATION_READY: { + case NOTIFICATION_READY: { + // Do nothing } break; + case NOTIFICATION_INTERNAL_PROCESS: { + // Are we processing during physics time? if (tween_process_mode == TWEEN_PROCESS_PHYSICS) + // Do nothing since we aren't aligned with physics when we should be break; + // Should we update? if (is_active()) + // Update the tweens _tween_process(get_process_delta_time()); } break; - case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { + case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: { + // Are we processing during 'regular' time? if (tween_process_mode == TWEEN_PROCESS_IDLE) + // Do nothing since we whould only process during idle time break; + // Should we update? if (is_active()) + // Update the tweens _tween_process(get_physics_process_delta_time()); } break; - case NOTIFICATION_EXIT_TREE: { + case NOTIFICATION_EXIT_TREE: { + // We've left the tree. Stop all tweens stop_all(); } break; } } void Tween::_bind_methods() { - + // Bind getters and setters ClassDB::bind_method(D_METHOD("is_active"), &Tween::is_active); ClassDB::bind_method(D_METHOD("set_active", "active"), &Tween::set_active); @@ -197,6 +221,7 @@ void Tween::_bind_methods() { ClassDB::bind_method(D_METHOD("set_tween_process_mode", "mode"), &Tween::set_tween_process_mode); ClassDB::bind_method(D_METHOD("get_tween_process_mode"), &Tween::get_tween_process_mode); + // Bind the various Tween control methods ClassDB::bind_method(D_METHOD("start"), &Tween::start); ClassDB::bind_method(D_METHOD("reset", "object", "key"), &Tween::reset, DEFVAL("")); ClassDB::bind_method(D_METHOD("reset_all"), &Tween::reset_all); @@ -211,6 +236,7 @@ void Tween::_bind_methods() { ClassDB::bind_method(D_METHOD("tell"), &Tween::tell); ClassDB::bind_method(D_METHOD("get_runtime"), &Tween::get_runtime); + // Bind interpolation and follow methods ClassDB::bind_method(D_METHOD("interpolate_property", "object", "property", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_property, DEFVAL(0)); ClassDB::bind_method(D_METHOD("interpolate_method", "object", "method", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::interpolate_method, DEFVAL(0)); ClassDB::bind_method(D_METHOD("interpolate_callback", "object", "duration", "callback", "arg1", "arg2", "arg3", "arg4", "arg5"), &Tween::interpolate_callback, DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant()), DEFVAL(Variant())); @@ -220,17 +246,22 @@ void Tween::_bind_methods() { ClassDB::bind_method(D_METHOD("targeting_property", "object", "property", "initial", "initial_val", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_property, DEFVAL(0)); ClassDB::bind_method(D_METHOD("targeting_method", "object", "method", "initial", "initial_method", "final_val", "duration", "trans_type", "ease_type", "delay"), &Tween::targeting_method, DEFVAL(0)); + // Add the Tween signals ADD_SIGNAL(MethodInfo("tween_started", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"))); ADD_SIGNAL(MethodInfo("tween_step", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"), PropertyInfo(Variant::REAL, "elapsed"), PropertyInfo(Variant::OBJECT, "value"))); ADD_SIGNAL(MethodInfo("tween_completed", PropertyInfo(Variant::OBJECT, "object"), PropertyInfo(Variant::NODE_PATH, "key"))); + ADD_SIGNAL(MethodInfo("tween_all_completed")); + // Add the properties and tie them to the getters and setters ADD_PROPERTY(PropertyInfo(Variant::BOOL, "repeat"), "set_repeat", "is_repeat"); ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_tween_process_mode", "get_tween_process_mode"); ADD_PROPERTY(PropertyInfo(Variant::REAL, "playback_speed", PROPERTY_HINT_RANGE, "-64,64,0.01"), "set_speed_scale", "get_speed_scale"); + // Bind Idle vs Physics process BIND_ENUM_CONSTANT(TWEEN_PROCESS_PHYSICS); BIND_ENUM_CONSTANT(TWEEN_PROCESS_IDLE); + // Bind the Transition type constants BIND_ENUM_CONSTANT(TRANS_LINEAR); BIND_ENUM_CONSTANT(TRANS_SINE); BIND_ENUM_CONSTANT(TRANS_QUINT); @@ -243,6 +274,7 @@ void Tween::_bind_methods() { BIND_ENUM_CONSTANT(TRANS_BOUNCE); BIND_ENUM_CONSTANT(TRANS_BACK); + // Bind the easing constants BIND_ENUM_CONSTANT(EASE_IN); BIND_ENUM_CONSTANT(EASE_OUT); BIND_ENUM_CONSTANT(EASE_IN_OUT); @@ -251,27 +283,30 @@ void Tween::_bind_methods() { Variant &Tween::_get_initial_val(InterpolateData &p_data) { + // What type of data are we interpolating? switch (p_data.type) { case INTER_PROPERTY: case INTER_METHOD: case FOLLOW_PROPERTY: case FOLLOW_METHOD: + // Simply use the given initial value return p_data.initial_val; case TARGETING_PROPERTY: case TARGETING_METHOD: { - + // Get the object that is being targeted Object *object = ObjectDB::get_instance(p_data.target_id); ERR_FAIL_COND_V(object == NULL, p_data.initial_val); + // Are we targeting a property or a method? static Variant initial_val; if (p_data.type == TARGETING_PROPERTY) { - + // Get the property from the target object bool valid = false; initial_val = object->get_indexed(p_data.target_key, &valid); ERR_FAIL_COND_V(!valid, p_data.initial_val); } else { - + // Call the method and get the initial value from it Variant::CallError error; initial_val = object->call(p_data.target_key[0], NULL, 0, error); ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val); @@ -280,64 +315,75 @@ Variant &Tween::_get_initial_val(InterpolateData &p_data) { } case INTER_CALLBACK: + // Callback does not have a special initial value break; } + // If we've made it here, just return the delta value as the initial value return p_data.delta_val; } Variant &Tween::_get_delta_val(InterpolateData &p_data) { + // What kind of data are we interpolating? switch (p_data.type) { case INTER_PROPERTY: case INTER_METHOD: + // Simply return the given delta value return p_data.delta_val; case FOLLOW_PROPERTY: case FOLLOW_METHOD: { - + // We're following an object, so grab that instance Object *target = ObjectDB::get_instance(p_data.target_id); ERR_FAIL_COND_V(target == NULL, p_data.initial_val); + // We want to figure out the final value Variant final_val; - if (p_data.type == FOLLOW_PROPERTY) { - + // Read the property as-is bool valid = false; final_val = target->get_indexed(p_data.target_key, &valid); ERR_FAIL_COND_V(!valid, p_data.initial_val); } else { - + // We're looking at a method. Call the method on the target object Variant::CallError error; final_val = target->call(p_data.target_key[0], NULL, 0, error); ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, p_data.initial_val); } - // convert INT to REAL is better for interpolaters + // If we're looking at an INT value, instead convert it to a REAL + // This is better for interpolation if (final_val.get_type() == Variant::INT) final_val = final_val.operator real_t(); + + // Calculate the delta based on the initial value and the final value _calc_delta_val(p_data.initial_val, final_val, p_data.delta_val); return p_data.delta_val; } case TARGETING_PROPERTY: case TARGETING_METHOD: { - + // Grab the initial value from the data to calculate delta Variant initial_val = _get_initial_val(p_data); - // convert INT to REAL is better for interpolaters + + // If we're looking at an INT value, instead convert it to a REAL + // This is better for interpolation if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t(); - //_calc_delta_val(p_data.initial_val, p_data.final_val, p_data.delta_val); + // Calculate the delta based on the initial value and the final value _calc_delta_val(initial_val, p_data.final_val, p_data.delta_val); return p_data.delta_val; } case INTER_CALLBACK: + // Callbacks have no special delta break; } + // If we've made it here, use the initial value as the delta return p_data.initial_val; } Variant Tween::_run_equation(InterpolateData &p_data) { - + // Get the initial and delta values from the data Variant &initial_val = _get_initial_val(p_data); Variant &delta_val = _get_delta_val(p_data); Variant result; @@ -345,48 +391,59 @@ Variant Tween::_run_equation(InterpolateData &p_data) { #define APPLY_EQUATION(element) \ r.element = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, i.element, d.element, p_data.duration); + // What type of data are we interpolating? switch (initial_val.get_type()) { case Variant::BOOL: + // Run the boolean specific equation (checking if it is at least 0.5) result = (_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, initial_val, delta_val, p_data.duration)) >= 0.5; break; case Variant::INT: + // Run the integer specific equation result = (int)_run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (int)initial_val, (int)delta_val, p_data.duration); break; case Variant::REAL: + // Run the REAL specific equation result = _run_equation(p_data.trans_type, p_data.ease_type, p_data.elapsed - p_data.delay, (real_t)initial_val, (real_t)delta_val, p_data.duration); break; case Variant::VECTOR2: { + // Get vectors for initial and delta values Vector2 i = initial_val; Vector2 d = delta_val; Vector2 r; + // Execute the equation and mutate the r vector + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(x); APPLY_EQUATION(y); - result = r; } break; case Variant::VECTOR3: { + // Get vectors for initial and delta values Vector3 i = initial_val; Vector3 d = delta_val; Vector3 r; + // Execute the equation and mutate the r vector + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(x); APPLY_EQUATION(y); APPLY_EQUATION(z); - result = r; } break; case Variant::BASIS: { + // Get the basis for initial and delta values Basis i = initial_val; Basis d = delta_val; Basis r; + // Execute the equation on all the basis and mutate the r basis + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(elements[0][0]); APPLY_EQUATION(elements[0][1]); APPLY_EQUATION(elements[0][2]); @@ -396,55 +453,63 @@ Variant Tween::_run_equation(InterpolateData &p_data) { APPLY_EQUATION(elements[2][0]); APPLY_EQUATION(elements[2][1]); APPLY_EQUATION(elements[2][2]); - result = r; } break; case Variant::TRANSFORM2D: { + // Get the transforms for initial and delta values Transform2D i = initial_val; Transform2D d = delta_val; Transform2D r; + // Execute the equation on the transforms and mutate the r transform + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(elements[0][0]); APPLY_EQUATION(elements[0][1]); APPLY_EQUATION(elements[1][0]); APPLY_EQUATION(elements[1][1]); APPLY_EQUATION(elements[2][0]); APPLY_EQUATION(elements[2][1]); - result = r; } break; case Variant::QUAT: { + // Get the quaternian for the initial and delta values Quat i = initial_val; Quat d = delta_val; Quat r; + // Execute the equation on the quaternian values and mutate the r quaternian + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(x); APPLY_EQUATION(y); APPLY_EQUATION(z); APPLY_EQUATION(w); - result = r; } break; case Variant::AABB: { + // Get the AABB's for the initial and delta values AABB i = initial_val; AABB d = delta_val; AABB r; + // Execute the equation for the position and size of the AABB's and mutate the r AABB + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(position.x); APPLY_EQUATION(position.y); APPLY_EQUATION(position.z); APPLY_EQUATION(size.x); APPLY_EQUATION(size.y); APPLY_EQUATION(size.z); - result = r; } break; case Variant::TRANSFORM: { + // Get the transforms for the initial and delta values Transform i = initial_val; Transform d = delta_val; Transform r; + // Execute the equation for each of the transforms and their origin and mutate the r transform + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(basis.elements[0][0]); APPLY_EQUATION(basis.elements[0][1]); APPLY_EQUATION(basis.elements[0][2]); @@ -457,40 +522,45 @@ Variant Tween::_run_equation(InterpolateData &p_data) { APPLY_EQUATION(origin.x); APPLY_EQUATION(origin.y); APPLY_EQUATION(origin.z); - result = r; } break; case Variant::COLOR: { + // Get the Color for initial and delta value Color i = initial_val; Color d = delta_val; Color r; + // Apply the equation on the Color RGBA, and mutate the r color + // This uses the custom APPLY_EQUATION macro defined above APPLY_EQUATION(r); APPLY_EQUATION(g); APPLY_EQUATION(b); APPLY_EQUATION(a); - result = r; } break; default: { + // If unknown, just return the initial value result = initial_val; } break; }; #undef APPLY_EQUATION - + // Return the result that was computed return result; } bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) { + // Get the object we want to apply the new value to Object *object = ObjectDB::get_instance(p_data.id); ERR_FAIL_COND_V(object == NULL, false); + // What kind of data are we mutating? switch (p_data.type) { case INTER_PROPERTY: case FOLLOW_PROPERTY: case TARGETING_PROPERTY: { + // Simply set the property on the object bool valid = false; object->set_indexed(p_data.key, value, &valid); return valid; @@ -499,85 +569,110 @@ bool Tween::_apply_tween_value(InterpolateData &p_data, Variant &value) { case INTER_METHOD: case FOLLOW_METHOD: case TARGETING_METHOD: { + // We want to call the method on the target object Variant::CallError error; + + // Do we have a non-nil value passed in? if (value.get_type() != Variant::NIL) { + // Pass it as an argument to the function call Variant *arg[1] = { &value }; object->call(p_data.key[0], (const Variant **)arg, 1, error); } else { + // Don't pass any argument object->call(p_data.key[0], NULL, 0, error); } - if (error.error == Variant::CallError::CALL_OK) - return true; - return false; + // Did we get an error from the function call? + return error.error == Variant::CallError::CALL_OK; } case INTER_CALLBACK: + // Nothing to apply for a callback break; }; + // No issues found! return true; } void Tween::_tween_process(float p_delta) { - + // Process all of the pending commands _process_pending_commands(); + // If the scale is 0, make no progress on the tweens if (speed_scale == 0) return; - p_delta *= speed_scale; + // Update the delta and whether we are pending an update + p_delta *= speed_scale; pending_update++; - // if repeat and all interpolates was finished then reset all interpolates - bool all_finished = true; - if (repeat) { + // Are we repeating the interpolations? + if (repeat) { + // For each interpolation... + bool repeats_finished = true; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Get the data from it InterpolateData &data = E->get(); + // Is not finished? if (!data.finish) { - all_finished = false; + // We aren't finished yet, no need to check the rest + repeats_finished = false; break; } } - if (all_finished) + // If we are all finished, we can reset all of the tweens + if (repeats_finished) reset_all(); } - all_finished = true; + // Are all of the tweens complete? + bool all_finished = true; + + // For each tween we wish to interpolate... for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { + // Get the data from it InterpolateData &data = E->get(); + + // Track if we hit one that isn't finished yet all_finished = all_finished && data.finish; + // Is the data not active or already finished? No need to go any further if (!data.active || data.finish) continue; + // Get the target object for this interpolation Object *object = ObjectDB::get_instance(data.id); if (object == NULL) continue; + // Are we still delaying this tween? bool prev_delaying = data.elapsed <= data.delay; data.elapsed += p_delta; if (data.elapsed < data.delay) continue; else if (prev_delaying) { - + // We can apply the tween's value to the data and emit that the tween has started _apply_tween_value(data, data.initial_val); emit_signal("tween_started", object, NodePath(Vector<StringName>(), data.key, false)); } + // Are we at the end of the tween? if (data.elapsed > (data.delay + data.duration)) { - + // Set the elapsed time to the end and mark this one as finished data.elapsed = data.delay + data.duration; data.finish = true; } + // Are we interpolating a callback? if (data.type == INTER_CALLBACK) { + // Is the tween completed? if (data.finish) { + // Are we calling this callback deferred or immediately? if (data.call_deferred) { - + // Run the deferred function callback, applying the correct number of arguments switch (data.args) { case 0: object->call_deferred(data.key[0]); @@ -599,6 +694,7 @@ void Tween::_tween_process(float p_delta) { break; } } else { + // Call the function directly with the arguments Variant::CallError error; Variant *arg[5] = { &data.arg[0], @@ -611,98 +707,111 @@ void Tween::_tween_process(float p_delta) { } } } else { + // We can apply the value directly Variant result = _run_equation(data); _apply_tween_value(data, result); + + // Emit that the tween has taken a step emit_signal("tween_step", object, NodePath(Vector<StringName>(), data.key, false), data.elapsed, result); } + // Is the tween now finished? if (data.finish) { + // Set it to the final value directly _apply_tween_value(data, data.final_val); + + // Mark the tween as completed and emit the signal data.elapsed = 0; emit_signal("tween_completed", object, NodePath(Vector<StringName>(), data.key, false)); - // not repeat mode, remove completed action + + // If we are not repeating the tween, remove it if (!repeat) call_deferred("_remove_by_uid", data.uid); - } else if (!repeat) + } else if (!repeat) { + // Check whether all tweens are finished all_finished = all_finished && data.finish; + } } + // One less update left to go pending_update--; - if (all_finished) + // If all tweens are completed, we no longer need to be active + if (all_finished) { set_active(false); + emit_signal("tween_all_completed"); + } } void Tween::set_tween_process_mode(TweenProcessMode p_mode) { - tween_process_mode = p_mode; } Tween::TweenProcessMode Tween::get_tween_process_mode() const { - return tween_process_mode; } bool Tween::is_active() const { - return is_processing_internal() || is_physics_processing_internal(); } void Tween::set_active(bool p_active) { - + // Do nothing if it's the same active mode that we currently are if (is_active() == p_active) return; + // Depending on physics or idle, set processing switch (tween_process_mode) { - case TWEEN_PROCESS_IDLE: set_process_internal(p_active); break; case TWEEN_PROCESS_PHYSICS: set_physics_process_internal(p_active); break; } } bool Tween::is_repeat() const { - return repeat; } void Tween::set_repeat(bool p_repeat) { - repeat = p_repeat; } void Tween::set_speed_scale(float p_speed) { - speed_scale = p_speed; } float Tween::get_speed_scale() const { - return speed_scale; } bool Tween::start() { + // Are there any pending updates? if (pending_update != 0) { + // Start the tweens after deferring call_deferred("start"); return true; } + // We want to be activated set_active(true); return true; } bool Tween::reset(Object *p_object, StringName p_key) { - + // Find all interpolations that use the same object and target string pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Get the target object InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); if (object == NULL) continue; + // Do we have the correct object and key? if (object == p_object && (data.concatenated_key == p_key || p_key == "")) { - + // Reset the tween to the initial state data.elapsed = 0; data.finish = false; + + // Also apply the initial state if there isn't a delay if (data.delay == 0) _apply_tween_value(data, data.initial_val); } @@ -712,13 +821,15 @@ bool Tween::reset(Object *p_object, StringName p_key) { } bool Tween::reset_all() { - + // Go through all interpolations pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Get the target data and set it back to the initial state InterpolateData &data = E->get(); data.elapsed = 0; data.finish = false; + + // If there isn't a delay, apply the value to the object if (data.delay == 0) _apply_tween_value(data, data.initial_val); } @@ -727,15 +838,19 @@ bool Tween::reset_all() { } bool Tween::stop(Object *p_object, StringName p_key) { - + // Find the tween that has the given target object and string key pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { + // Get the object the tween is targeting InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); if (object == NULL) continue; + + // Is this the correct object and does it have the given key? if (object == p_object && (data.concatenated_key == p_key || p_key == "")) + // Disable the tween data.active = false; } pending_update--; @@ -743,12 +858,13 @@ bool Tween::stop(Object *p_object, StringName p_key) { } bool Tween::stop_all() { - + // We no longer need to be active since all tweens have been stopped set_active(false); + // For each interpolation... pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Simply set it inactive InterpolateData &data = E->get(); data.active = false; } @@ -757,16 +873,20 @@ bool Tween::stop_all() { } bool Tween::resume(Object *p_object, StringName p_key) { - + // We need to be activated + // TODO: What if no tween is found?? set_active(true); + // Find the tween that uses the given target object and string key pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Grab the object InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); if (object == NULL) continue; + + // If the object and string key match, activate it if (object == p_object && (data.concatenated_key == p_key || p_key == "")) data.active = true; } @@ -775,12 +895,14 @@ bool Tween::resume(Object *p_object, StringName p_key) { } bool Tween::resume_all() { - + // Set ourselves active so we can process tweens + // TODO: What if there are no tweens? We get set to active for no reason! set_active(true); + // For each interpolation... pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Simply grab it and set it to active InterpolateData &data = E->get(); data.active = true; } @@ -789,35 +911,46 @@ bool Tween::resume_all() { } bool Tween::remove(Object *p_object, StringName p_key) { + // If we are still updating, call this function again later if (pending_update != 0) { call_deferred("remove", p_object, p_key); return true; } + + // For each interpolation... List<List<InterpolateData>::Element *> for_removal; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Get the target object InterpolateData &data = E->get(); Object *object = ObjectDB::get_instance(data.id); if (object == NULL) continue; + + // If the target object and string key match, queue it for removal if (object == p_object && (data.concatenated_key == p_key || p_key == "")) { for_removal.push_back(E); } } + + // For each interpolation we wish to remove... for (List<List<InterpolateData>::Element *>::Element *E = for_removal.front(); E; E = E->next()) { + // Erase it interpolates.erase(E->get()); } return true; } void Tween::_remove_by_uid(int uid) { + // If we are still updating, call this function again later if (pending_update != 0) { call_deferred("_remove_by_uid", uid); return; } + // Find the interpolation that matches the given UID for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { if (uid == E->get().uid) { + // It matches, erase it and stop looking E->erase(); break; } @@ -826,49 +959,61 @@ void Tween::_remove_by_uid(int uid) { void Tween::_push_interpolate_data(InterpolateData &p_data) { pending_update++; + + // Add the new interpolation p_data.uid = ++uid; interpolates.push_back(p_data); + pending_update--; } bool Tween::remove_all() { - + // If we are still updating, call this function again later if (pending_update != 0) { call_deferred("remove_all"); return true; } + // We no longer need to be active set_active(false); + + // Clear out all interpolations and reset the uid interpolates.clear(); uid = 0; + return true; } bool Tween::seek(real_t p_time) { - + // Go through each interpolation... pending_update++; for (List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Get the target data InterpolateData &data = E->get(); + // Update the elapsed data to be set to the target time data.elapsed = p_time; - if (data.elapsed < data.delay) { + // Are we at the end? + if (data.elapsed < data.delay) { + // There is still time left to go data.finish = false; continue; } else if (data.elapsed >= (data.delay + data.duration)) { - - data.finish = true; + // We are past the end of it, set the elapsed time to the end and mark as finished data.elapsed = (data.delay + data.duration); + data.finish = true; } else { + // We are not finished with this interpolation yet data.finish = false; } + // If we are a callback, do nothing special if (data.type == INTER_CALLBACK) { continue; } + // Run the equation on the data and apply the value Variant result = _run_equation(data); - _apply_tween_value(data, result); } pending_update--; @@ -876,13 +1021,16 @@ bool Tween::seek(real_t p_time) { } real_t Tween::tell() const { - + // We want to grab the position of the furthest along tween pending_update++; real_t pos = 0; - for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { + // For each interpolation... + for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { + // Get the data and figure out if it's position is further along than the previous ones const InterpolateData &data = E->get(); if (data.elapsed > pos) + // Save it if so pos = data.elapsed; } pending_update--; @@ -890,55 +1038,63 @@ real_t Tween::tell() const { } real_t Tween::get_runtime() const { - + // If the tween isn't moving, it'll last forever if (speed_scale == 0) { return INFINITY; } pending_update++; + + // For each interpolation... real_t runtime = 0; for (const List<InterpolateData>::Element *E = interpolates.front(); E; E = E->next()) { - + // Get the tween data and see if it's runtime is greater than the previous tweens const InterpolateData &data = E->get(); real_t t = data.delay + data.duration; if (t > runtime) + // This is the longest running tween runtime = t; } pending_update--; + // Adjust the runtime for the current speed scale return runtime / speed_scale; } bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final_val, Variant &p_delta_val) { + // Get the initial, final, and delta values const Variant &initial_val = p_initial_val; const Variant &final_val = p_final_val; Variant &delta_val = p_delta_val; + // What kind of data are we interpolating? switch (initial_val.get_type()) { case Variant::BOOL: - //delta_val = p_final_val; - delta_val = (int)p_final_val - (int)p_initial_val; - break; - + // We'll treat booleans just like integers case Variant::INT: + // Compute the integer delta delta_val = (int)final_val - (int)initial_val; break; case Variant::REAL: + // Convert to REAL and find the delta delta_val = (real_t)final_val - (real_t)initial_val; break; case Variant::VECTOR2: + // Convert to Vectors and find the delta delta_val = final_val.operator Vector2() - initial_val.operator Vector2(); break; case Variant::VECTOR3: + // Convert to Vectors and find the delta delta_val = final_val.operator Vector3() - initial_val.operator Vector3(); break; case Variant::BASIS: { + // Build a new basis which is the delta between the initial and final values Basis i = initial_val; Basis f = final_val; delta_val = Basis(f.elements[0][0] - i.elements[0][0], @@ -953,6 +1109,7 @@ bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final } break; case Variant::TRANSFORM2D: { + // Build a new transform which is the difference between the initial and final values Transform2D i = initial_val; Transform2D f = final_val; Transform2D d = Transform2D(); @@ -964,15 +1121,21 @@ bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final d[2][1] = f.elements[2][1] - i.elements[2][1]; delta_val = d; } break; + case Variant::QUAT: + // Convert to quaternianls and find the delta delta_val = final_val.operator Quat() - initial_val.operator Quat(); break; + case Variant::AABB: { + // Build a new AABB and use the new position and sizes to make a delta AABB i = initial_val; AABB f = final_val; delta_val = AABB(f.position - i.position, f.size - i.size); } break; + case Variant::TRANSFORM: { + // Build a new transform which is the difference between the initial and final values Transform i = initial_val; Transform f = final_val; Transform d; @@ -991,124 +1154,148 @@ bool Tween::_calc_delta_val(const Variant &p_initial_val, const Variant &p_final delta_val = d; } break; + case Variant::COLOR: { + // Make a new color which is the difference between each the color's RGBA attributes Color i = initial_val; Color f = final_val; delta_val = Color(f.r - i.r, f.g - i.g, f.b - i.b, f.a - i.a); } break; default: + // TODO: Should move away from a 'magic string'? ERR_PRINT("Invalid param type, except(int/real/vector2/vector/matrix/matrix32/quat/aabb/transform/color)"); return false; }; return true; } -bool Tween::interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - if (pending_update != 0) { - _add_pending_command("interpolate_property", p_object, p_property, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); - return true; - } - p_property = p_property.get_as_property_path(); - - if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames()); - - // convert INT to REAL is better for interpolaters - if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); - if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); - - ERR_FAIL_COND_V(p_object == NULL, false); - ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); - ERR_FAIL_COND_V(p_initial_val.get_type() != p_final_val.get_type(), false); - ERR_FAIL_COND_V(p_duration <= 0, false); - ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); - ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); - ERR_FAIL_COND_V(p_delay < 0, false); +bool Tween::_build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - bool prop_valid = false; - p_object->get_indexed(p_property.get_subnames(), &prop_valid); - ERR_FAIL_COND_V(!prop_valid, false); + // TODO: Add initialization+implementation for remaining interpolation types + // TODO: Fix this method's organization to take advantage of the type + // Make a new interpolation data InterpolateData data; data.active = true; - data.type = INTER_PROPERTY; + data.type = p_interpolation_type; data.finish = false; data.elapsed = 0; + // Validate and apply interpolation data + + // Give it the object + ERR_FAIL_COND_V_MSG(p_object == NULL, false, "Invalid object provided to Tween."); + ERR_FAIL_COND_V_MSG(!ObjectDB::instance_validate(p_object), false, "Invalid object provided to Tween."); data.id = p_object->get_instance_id(); - data.key = p_property.get_subnames(); - data.concatenated_key = p_property.get_concatenated_subnames(); + + // Validate the initial and final values + ERR_FAIL_COND_V_MSG(p_initial_val.get_type() != p_final_val.get_type(), false, "Initial value type '" + Variant::get_type_name(p_initial_val.get_type()) + "' does not match final value type '" + Variant::get_type_name(p_final_val.get_type()) + "'."); data.initial_val = p_initial_val; data.final_val = p_final_val; + + // Check the Duration + ERR_FAIL_COND_V_MSG(p_duration < 0, false, "Only non-negative duration values allowed in Tweens."); data.duration = p_duration; + + // Tween Delay + ERR_FAIL_COND_V_MSG(p_delay < 0, false, "Only non-negative delay values allowed in Tweens."); + data.delay = p_delay; + + // Transition type + ERR_FAIL_COND_V_MSG(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false, "Invalid transition type provided to Tween."); data.trans_type = p_trans_type; + + // Easing type + ERR_FAIL_COND_V_MSG(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false, "Invalid easing type provided to Tween."); data.ease_type = p_ease_type; - data.delay = p_delay; + // Is the property defined? + if (p_property) { + // Check that the object actually contains the given property + bool prop_valid = false; + p_object->get_indexed(p_property->get_subnames(), &prop_valid); + ERR_FAIL_COND_V_MSG(!prop_valid, false, "Tween target object has no property named: " + p_property->get_concatenated_subnames() + "."); + + data.key = p_property->get_subnames(); + data.concatenated_key = p_property->get_concatenated_subnames(); + } + + // Is the method defined? + if (p_method) { + // Does the object even have the requested method? + ERR_FAIL_COND_V_MSG(!p_object->has_method(*p_method), false, "Tween target object has no method named: " + *p_method + "."); + + data.key.push_back(*p_method); + data.concatenated_key = *p_method; + } + + // Is there not a valid delta? if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) return false; + // Add this interpolation to the total _push_interpolate_data(data); return true; } -bool Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { +bool Tween::interpolate_property(Object *p_object, NodePath p_property, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { + // If we are busy updating, call this function again later if (pending_update != 0) { - _add_pending_command("interpolate_method", p_object, p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + _add_pending_command("interpolate_property", p_object, p_property, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); return true; } - // convert INT to REAL is better for interpolaters - if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); - if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); - ERR_FAIL_COND_V(p_object == NULL, false); - ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); - ERR_FAIL_COND_V(p_initial_val.get_type() != p_final_val.get_type(), false); - ERR_FAIL_COND_V(p_duration <= 0, false); - ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); - ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); - ERR_FAIL_COND_V(p_delay < 0, false); + // Get the property from the node path + p_property = p_property.get_as_property_path(); - ERR_EXPLAIN("Object has no method named: %s" + p_method); - ERR_FAIL_COND_V(!p_object->has_method(p_method), false); + // If no initial value given, grab the initial value from the object + // TODO: Is this documented? This is very useful and removes a lot of clutter from tweens! + if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames()); - InterpolateData data; - data.active = true; - data.type = INTER_METHOD; - data.finish = false; - data.elapsed = 0; + // Convert any integers into REALs as they are better for interpolation + if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); - data.id = p_object->get_instance_id(); - data.key.push_back(p_method); - data.concatenated_key = p_method; - data.initial_val = p_initial_val; - data.final_val = p_final_val; - data.duration = p_duration; - data.trans_type = p_trans_type; - data.ease_type = p_ease_type; - data.delay = p_delay; + // Build the interpolation data + bool result = _build_interpolation(INTER_PROPERTY, p_object, &p_property, NULL, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + return result; +} - if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) - return false; +bool Tween::interpolate_method(Object *p_object, StringName p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { + // If we are busy updating, call this function again later + if (pending_update != 0) { + _add_pending_command("interpolate_method", p_object, p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + return true; + } - _push_interpolate_data(data); - return true; + // Convert any integers into REALs as they are better for interpolation + if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + + // Build the interpolation data + bool result = _build_interpolation(INTER_METHOD, p_object, NULL, &p_method, p_initial_val, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); + return result; } bool Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) { - + // If we are already updating, call this function again later if (pending_update != 0) { _add_pending_command("interpolate_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5); return true; } + // Check that the target object is valid ERR_FAIL_COND_V(p_object == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); + + // Duration cannot be negative ERR_FAIL_COND_V(p_duration < 0, false); - ERR_EXPLAIN("Object has no callback named: %s" + p_callback); - ERR_FAIL_COND_V(!p_object->has_method(p_callback), false); + // Check whether the object even has the callback + ERR_FAIL_COND_V_MSG(!p_object->has_method(p_callback), false, "Object has no callback named: " + p_callback + "."); + // Build a new InterpolationData InterpolateData data; data.active = true; data.type = INTER_CALLBACK; @@ -1116,12 +1303,14 @@ bool Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_c data.call_deferred = false; data.elapsed = 0; + // Give the data it's configuration data.id = p_object->get_instance_id(); data.key.push_back(p_callback); data.concatenated_key = p_callback; data.duration = p_duration; data.delay = 0; + // Add arguments to the interpolation int args = 0; if (p_arg5.get_type() != Variant::NIL) args = 5; @@ -1143,23 +1332,29 @@ bool Tween::interpolate_callback(Object *p_object, real_t p_duration, String p_c data.arg[3] = p_arg4; data.arg[4] = p_arg5; + // Add the new interpolation _push_interpolate_data(data); return true; } bool Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, String p_callback, VARIANT_ARG_DECLARE) { - + // If we are already updating, call this function again later if (pending_update != 0) { _add_pending_command("interpolate_deferred_callback", p_object, p_duration, p_callback, p_arg1, p_arg2, p_arg3, p_arg4, p_arg5); return true; } + + // Check that the target object is valid ERR_FAIL_COND_V(p_object == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); + + // No negative durations allowed ERR_FAIL_COND_V(p_duration < 0, false); - ERR_EXPLAIN("Object has no callback named: %s" + p_callback); - ERR_FAIL_COND_V(!p_object->has_method(p_callback), false); + // Confirm the callback exists on the object + ERR_FAIL_COND_V_MSG(!p_object->has_method(p_callback), false, "Object has no callback named: " + p_callback + "."); + // Create a new InterpolateData for the callback InterpolateData data; data.active = true; data.type = INTER_CALLBACK; @@ -1167,12 +1362,14 @@ bool Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, S data.call_deferred = true; data.elapsed = 0; + // Give the data it's configuration data.id = p_object->get_instance_id(); data.key.push_back(p_callback); data.concatenated_key = p_callback; data.duration = p_duration; data.delay = 0; + // Collect arguments for the callback int args = 0; if (p_arg5.get_type() != Variant::NIL) args = 5; @@ -1194,32 +1391,46 @@ bool Tween::interpolate_deferred_callback(Object *p_object, real_t p_duration, S data.arg[3] = p_arg4; data.arg[4] = p_arg5; + // Add the new interpolation _push_interpolate_data(data); return true; } bool Tween::follow_property(Object *p_object, NodePath p_property, Variant p_initial_val, Object *p_target, NodePath p_target_property, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { + // If we are already updating, call this function again later if (pending_update != 0) { _add_pending_command("follow_property", p_object, p_property, p_initial_val, p_target, p_target_property, p_duration, p_trans_type, p_ease_type, p_delay); return true; } + + // Get the two properties from their paths p_property = p_property.get_as_property_path(); p_target_property = p_target_property.get_as_property_path(); + // If no initial value is given, grab it from the source object + // TODO: Is this documented? It's really helpful for decluttering tweens if (p_initial_val.get_type() == Variant::NIL) p_initial_val = p_object->get_indexed(p_property.get_subnames()); - // convert INT to REAL is better for interpolaters + // Convert initial INT values to REAL as they are better for interpolation if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + // Confirm the source and target objects are valid ERR_FAIL_COND_V(p_object == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); ERR_FAIL_COND_V(p_target == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_target), false); - ERR_FAIL_COND_V(p_duration <= 0, false); + + // No negative durations + ERR_FAIL_COND_V(p_duration < 0, false); + + // Ensure transition and easing types are valid ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + + // No negative delays ERR_FAIL_COND_V(p_delay < 0, false); + // Confirm the source and target objects have the desired properties bool prop_valid = false; p_object->get_indexed(p_property.get_subnames(), &prop_valid); ERR_FAIL_COND_V(!prop_valid, false); @@ -1228,16 +1439,20 @@ bool Tween::follow_property(Object *p_object, NodePath p_property, Variant p_ini Variant target_val = p_target->get_indexed(p_target_property.get_subnames(), &target_prop_valid); ERR_FAIL_COND_V(!target_prop_valid, false); - // convert INT to REAL is better for interpolaters + // Convert target INT to REAL since it is better for interpolation if (target_val.get_type() == Variant::INT) target_val = target_val.operator real_t(); + + // Verify that the target value and initial value are the same type ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false); + // Create a new InterpolateData InterpolateData data; data.active = true; data.type = FOLLOW_PROPERTY; data.finish = false; data.elapsed = 0; + // Give the InterpolateData it's configuration data.id = p_object->get_instance_id(); data.key = p_property.get_subnames(); data.concatenated_key = p_property.get_concatenated_subnames(); @@ -1249,46 +1464,57 @@ bool Tween::follow_property(Object *p_object, NodePath p_property, Variant p_ini data.ease_type = p_ease_type; data.delay = p_delay; + // Add the interpolation _push_interpolate_data(data); return true; } bool Tween::follow_method(Object *p_object, StringName p_method, Variant p_initial_val, Object *p_target, StringName p_target_method, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { + // If we are currently updating, call this function again later if (pending_update != 0) { _add_pending_command("follow_method", p_object, p_method, p_initial_val, p_target, p_target_method, p_duration, p_trans_type, p_ease_type, p_delay); return true; } - // convert INT to REAL is better for interpolaters + // Convert initial INT values to REAL as they are better for interpolation if (p_initial_val.get_type() == Variant::INT) p_initial_val = p_initial_val.operator real_t(); + // Verify the source and target objects are valid ERR_FAIL_COND_V(p_object == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); ERR_FAIL_COND_V(p_target == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_target), false); - ERR_FAIL_COND_V(p_duration <= 0, false); + + // No negative durations + ERR_FAIL_COND_V(p_duration < 0, false); + + // Ensure that the transition and ease types are valid ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + + // No negative delays ERR_FAIL_COND_V(p_delay < 0, false); - ERR_EXPLAIN("Object has no method named: %s" + p_method); - ERR_FAIL_COND_V(!p_object->has_method(p_method), false); - ERR_EXPLAIN("Target has no method named: %s" + p_target_method); - ERR_FAIL_COND_V(!p_target->has_method(p_target_method), false); + // Confirm both objects have the target methods + ERR_FAIL_COND_V_MSG(!p_object->has_method(p_method), false, "Object has no method named: " + p_method + "."); + ERR_FAIL_COND_V_MSG(!p_target->has_method(p_target_method), false, "Target has no method named: " + p_target_method + "."); + // Call the method to get the target value Variant::CallError error; Variant target_val = p_target->call(p_target_method, NULL, 0, error); ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false); - // convert INT to REAL is better for interpolaters + // Convert target INT values to REAL as they are better for interpolation if (target_val.get_type() == Variant::INT) target_val = target_val.operator real_t(); ERR_FAIL_COND_V(target_val.get_type() != p_initial_val.get_type(), false); + // Make the new InterpolateData for the method follow InterpolateData data; data.active = true; data.type = FOLLOW_METHOD; data.finish = false; data.elapsed = 0; + // Give the data it's configuration data.id = p_object->get_instance_id(); data.key.push_back(p_method); data.concatenated_key = p_method; @@ -1300,31 +1526,41 @@ bool Tween::follow_method(Object *p_object, StringName p_method, Variant p_initi data.ease_type = p_ease_type; data.delay = p_delay; + // Add the new interpolation _push_interpolate_data(data); return true; } bool Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_initial, NodePath p_initial_property, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { - + // If we are currently updating, call this function again later if (pending_update != 0) { _add_pending_command("targeting_property", p_object, p_property, p_initial, p_initial_property, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); return true; } + // Grab the target property and the target property p_property = p_property.get_as_property_path(); p_initial_property = p_initial_property.get_as_property_path(); - // convert INT to REAL is better for interpolaters + // Convert the initial INT values to REAL as they are better for Interpolation if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + // Verify both objects are valid ERR_FAIL_COND_V(p_object == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); ERR_FAIL_COND_V(p_initial == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_initial), false); - ERR_FAIL_COND_V(p_duration <= 0, false); + + // No negative durations + ERR_FAIL_COND_V(p_duration < 0, false); + + // Ensure transition and easing types are valid ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + + // No negative delays ERR_FAIL_COND_V(p_delay < 0, false); + // Ensure the initial and target properties exist on their objects bool prop_valid = false; p_object->get_indexed(p_property.get_subnames(), &prop_valid); ERR_FAIL_COND_V(!prop_valid, false); @@ -1333,16 +1569,18 @@ bool Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_ Variant initial_val = p_initial->get_indexed(p_initial_property.get_subnames(), &initial_prop_valid); ERR_FAIL_COND_V(!initial_prop_valid, false); - // convert INT to REAL is better for interpolaters + // Convert the initial INT value to REAL as it is better for interpolation if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t(); ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false); + // Build the InterpolateData object InterpolateData data; data.active = true; data.type = TARGETING_PROPERTY; data.finish = false; data.elapsed = 0; + // Give the data it's configuration data.id = p_object->get_instance_id(); data.key = p_property.get_subnames(); data.concatenated_key = p_property.get_concatenated_subnames(); @@ -1355,49 +1593,62 @@ bool Tween::targeting_property(Object *p_object, NodePath p_property, Object *p_ data.ease_type = p_ease_type; data.delay = p_delay; + // Ensure there is a valid delta if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) return false; + // Add the interpolation _push_interpolate_data(data); return true; } bool Tween::targeting_method(Object *p_object, StringName p_method, Object *p_initial, StringName p_initial_method, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay) { + // If we are currently updating, call this function again later if (pending_update != 0) { _add_pending_command("targeting_method", p_object, p_method, p_initial, p_initial_method, p_final_val, p_duration, p_trans_type, p_ease_type, p_delay); return true; } - // convert INT to REAL is better for interpolaters + + // Convert final INT values to REAL as they are better for interpolation if (p_final_val.get_type() == Variant::INT) p_final_val = p_final_val.operator real_t(); + // Make sure the given objects are valid ERR_FAIL_COND_V(p_object == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_object), false); ERR_FAIL_COND_V(p_initial == NULL, false); ERR_FAIL_COND_V(!ObjectDB::instance_validate(p_initial), false); - ERR_FAIL_COND_V(p_duration <= 0, false); + + // No negative durations + ERR_FAIL_COND_V(p_duration < 0, false); + + // Ensure transition and easing types are valid ERR_FAIL_COND_V(p_trans_type < 0 || p_trans_type >= TRANS_COUNT, false); ERR_FAIL_COND_V(p_ease_type < 0 || p_ease_type >= EASE_COUNT, false); + + // No negative delays ERR_FAIL_COND_V(p_delay < 0, false); - ERR_EXPLAIN("Object has no method named: %s" + p_method); - ERR_FAIL_COND_V(!p_object->has_method(p_method), false); - ERR_EXPLAIN("Initial Object has no method named: %s" + p_initial_method); - ERR_FAIL_COND_V(!p_initial->has_method(p_initial_method), false); + // Make sure both objects have the given method + ERR_FAIL_COND_V_MSG(!p_object->has_method(p_method), false, "Object has no method named: " + p_method + "."); + ERR_FAIL_COND_V_MSG(!p_initial->has_method(p_initial_method), false, "Initial Object has no method named: " + p_initial_method + "."); + // Call the method to get the initial value Variant::CallError error; Variant initial_val = p_initial->call(p_initial_method, NULL, 0, error); ERR_FAIL_COND_V(error.error != Variant::CallError::CALL_OK, false); - // convert INT to REAL is better for interpolaters + // Convert initial INT values to REAL as they aer better for interpolation if (initial_val.get_type() == Variant::INT) initial_val = initial_val.operator real_t(); ERR_FAIL_COND_V(initial_val.get_type() != p_final_val.get_type(), false); + // Build the new InterpolateData object InterpolateData data; data.active = true; data.type = TARGETING_METHOD; data.finish = false; data.elapsed = 0; + // Configure the data data.id = p_object->get_instance_id(); data.key.push_back(p_method); data.concatenated_key = p_method; @@ -1410,16 +1661,17 @@ bool Tween::targeting_method(Object *p_object, StringName p_method, Object *p_in data.ease_type = p_ease_type; data.delay = p_delay; + // Ensure there is a valid delta if (!_calc_delta_val(data.initial_val, data.final_val, data.delta_val)) return false; + // Add the interpolation _push_interpolate_data(data); return true; } Tween::Tween() { - - //String autoplay; + // Initialize tween attributes tween_process_mode = TWEEN_PROCESS_IDLE; repeat = false; speed_scale = 1; diff --git a/scene/animation/tween.h b/scene/animation/tween.h index 6fe3bffdbe..64ce099ecd 100644 --- a/scene/animation/tween.h +++ b/scene/animation/tween.h @@ -135,6 +135,7 @@ private: void _tween_process(float p_delta); void _remove_by_uid(int uid); void _push_interpolate_data(InterpolateData &p_data); + bool _build_interpolation(InterpolateType p_interpolation_type, Object *p_object, NodePath *p_property, StringName *p_method, Variant p_initial_val, Variant p_final_val, real_t p_duration, TransitionType p_trans_type, EaseType p_ease_type, real_t p_delay); protected: bool _set(const StringName &p_name, const Variant &p_value); |