Path: blob/master/scene/animation/animation_node_state_machine.cpp
9896 views
/**************************************************************************/1/* animation_node_state_machine.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "animation_node_state_machine.h"3132/////////////////////////////////////////////////3334void AnimationNodeStateMachineTransition::set_switch_mode(SwitchMode p_mode) {35switch_mode = p_mode;36}3738AnimationNodeStateMachineTransition::SwitchMode AnimationNodeStateMachineTransition::get_switch_mode() const {39return switch_mode;40}4142void AnimationNodeStateMachineTransition::set_advance_mode(AdvanceMode p_mode) {43advance_mode = p_mode;44}4546AnimationNodeStateMachineTransition::AdvanceMode AnimationNodeStateMachineTransition::get_advance_mode() const {47return advance_mode;48}4950void AnimationNodeStateMachineTransition::set_advance_condition(const StringName &p_condition) {51String cs = p_condition;52ERR_FAIL_COND(cs.contains_char('/') || cs.contains_char(':'));53advance_condition = p_condition;54if (!cs.is_empty()) {55advance_condition_name = "conditions/" + cs;56} else {57advance_condition_name = StringName();58}59emit_signal(SNAME("advance_condition_changed"));60}6162StringName AnimationNodeStateMachineTransition::get_advance_condition() const {63return advance_condition;64}6566StringName AnimationNodeStateMachineTransition::get_advance_condition_name() const {67return advance_condition_name;68}6970void AnimationNodeStateMachineTransition::set_advance_expression(const String &p_expression) {71advance_expression = p_expression;7273String advance_expression_stripped = advance_expression.strip_edges();74if (advance_expression_stripped == String()) {75expression.unref();76return;77}7879if (expression.is_null()) {80expression.instantiate();81}8283expression->parse(advance_expression_stripped);84}8586String AnimationNodeStateMachineTransition::get_advance_expression() const {87return advance_expression;88}8990void AnimationNodeStateMachineTransition::set_xfade_time(float p_xfade) {91ERR_FAIL_COND(p_xfade < 0);92xfade_time = p_xfade;93emit_changed();94}9596float AnimationNodeStateMachineTransition::get_xfade_time() const {97return xfade_time;98}99100void AnimationNodeStateMachineTransition::set_xfade_curve(const Ref<Curve> &p_curve) {101xfade_curve = p_curve;102emit_changed();103}104105Ref<Curve> AnimationNodeStateMachineTransition::get_xfade_curve() const {106return xfade_curve;107}108109void AnimationNodeStateMachineTransition::set_break_loop_at_end(bool p_enable) {110break_loop_at_end = p_enable;111emit_changed();112}113114bool AnimationNodeStateMachineTransition::is_loop_broken_at_end() const {115return break_loop_at_end;116}117118void AnimationNodeStateMachineTransition::set_reset(bool p_reset) {119reset = p_reset;120emit_changed();121}122123bool AnimationNodeStateMachineTransition::is_reset() const {124return reset;125}126127void AnimationNodeStateMachineTransition::set_priority(int p_priority) {128priority = p_priority;129emit_changed();130}131132int AnimationNodeStateMachineTransition::get_priority() const {133return priority;134}135136void AnimationNodeStateMachineTransition::_bind_methods() {137ClassDB::bind_method(D_METHOD("set_switch_mode", "mode"), &AnimationNodeStateMachineTransition::set_switch_mode);138ClassDB::bind_method(D_METHOD("get_switch_mode"), &AnimationNodeStateMachineTransition::get_switch_mode);139140ClassDB::bind_method(D_METHOD("set_advance_mode", "mode"), &AnimationNodeStateMachineTransition::set_advance_mode);141ClassDB::bind_method(D_METHOD("get_advance_mode"), &AnimationNodeStateMachineTransition::get_advance_mode);142143ClassDB::bind_method(D_METHOD("set_advance_condition", "name"), &AnimationNodeStateMachineTransition::set_advance_condition);144ClassDB::bind_method(D_METHOD("get_advance_condition"), &AnimationNodeStateMachineTransition::get_advance_condition);145146ClassDB::bind_method(D_METHOD("set_xfade_time", "secs"), &AnimationNodeStateMachineTransition::set_xfade_time);147ClassDB::bind_method(D_METHOD("get_xfade_time"), &AnimationNodeStateMachineTransition::get_xfade_time);148149ClassDB::bind_method(D_METHOD("set_xfade_curve", "curve"), &AnimationNodeStateMachineTransition::set_xfade_curve);150ClassDB::bind_method(D_METHOD("get_xfade_curve"), &AnimationNodeStateMachineTransition::get_xfade_curve);151152ClassDB::bind_method(D_METHOD("set_break_loop_at_end", "enable"), &AnimationNodeStateMachineTransition::set_break_loop_at_end);153ClassDB::bind_method(D_METHOD("is_loop_broken_at_end"), &AnimationNodeStateMachineTransition::is_loop_broken_at_end);154155ClassDB::bind_method(D_METHOD("set_reset", "reset"), &AnimationNodeStateMachineTransition::set_reset);156ClassDB::bind_method(D_METHOD("is_reset"), &AnimationNodeStateMachineTransition::is_reset);157158ClassDB::bind_method(D_METHOD("set_priority", "priority"), &AnimationNodeStateMachineTransition::set_priority);159ClassDB::bind_method(D_METHOD("get_priority"), &AnimationNodeStateMachineTransition::get_priority);160161ClassDB::bind_method(D_METHOD("set_advance_expression", "text"), &AnimationNodeStateMachineTransition::set_advance_expression);162ClassDB::bind_method(D_METHOD("get_advance_expression"), &AnimationNodeStateMachineTransition::get_advance_expression);163164ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "xfade_time", PROPERTY_HINT_RANGE, "0,240,0.01,suffix:s"), "set_xfade_time", "get_xfade_time");165ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "xfade_curve", PROPERTY_HINT_RESOURCE_TYPE, "Curve"), "set_xfade_curve", "get_xfade_curve");166167ADD_PROPERTY(PropertyInfo(Variant::BOOL, "break_loop_at_end"), "set_break_loop_at_end", "is_loop_broken_at_end");168ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset"), "set_reset", "is_reset");169170ADD_PROPERTY(PropertyInfo(Variant::INT, "priority", PROPERTY_HINT_RANGE, "0,32,1"), "set_priority", "get_priority");171ADD_GROUP("Switch", "");172ADD_PROPERTY(PropertyInfo(Variant::INT, "switch_mode", PROPERTY_HINT_ENUM, "Immediate,Sync,At End"), "set_switch_mode", "get_switch_mode");173ADD_GROUP("Advance", "advance_");174ADD_PROPERTY(PropertyInfo(Variant::INT, "advance_mode", PROPERTY_HINT_ENUM, "Disabled,Enabled,Auto"), "set_advance_mode", "get_advance_mode");175ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "advance_condition"), "set_advance_condition", "get_advance_condition");176ADD_PROPERTY(PropertyInfo(Variant::STRING, "advance_expression", PROPERTY_HINT_EXPRESSION, ""), "set_advance_expression", "get_advance_expression");177178BIND_ENUM_CONSTANT(SWITCH_MODE_IMMEDIATE);179BIND_ENUM_CONSTANT(SWITCH_MODE_SYNC);180BIND_ENUM_CONSTANT(SWITCH_MODE_AT_END);181182BIND_ENUM_CONSTANT(ADVANCE_MODE_DISABLED);183BIND_ENUM_CONSTANT(ADVANCE_MODE_ENABLED);184BIND_ENUM_CONSTANT(ADVANCE_MODE_AUTO);185186ADD_SIGNAL(MethodInfo("advance_condition_changed"));187}188189AnimationNodeStateMachineTransition::AnimationNodeStateMachineTransition() {190}191192////////////////////////////////////////////////////////193194void AnimationNodeStateMachinePlayback::_set_current(AnimationNodeStateMachine *p_state_machine, const StringName &p_state) {195current = p_state;196if (current == StringName()) {197group_start_transition = Ref<AnimationNodeStateMachineTransition>();198group_end_transition = Ref<AnimationNodeStateMachineTransition>();199return;200}201202AnimationTree *tree = p_state_machine->process_state ? p_state_machine->process_state->tree : nullptr;203Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(current);204if (anodesm.is_null()) {205group_start_transition = Ref<AnimationNodeStateMachineTransition>();206group_end_transition = Ref<AnimationNodeStateMachineTransition>();207_signal_state_change(tree, current, true);208return;209}210211Vector<int> indices = p_state_machine->find_transition_to(current);212int group_start_size = indices.size();213if (group_start_size) {214group_start_transition = p_state_machine->get_transition(indices[0]);215} else {216group_start_transition = Ref<AnimationNodeStateMachineTransition>();217}218219indices = p_state_machine->find_transition_from(current);220int group_end_size = indices.size();221if (group_end_size) {222group_end_transition = p_state_machine->get_transition(indices[0]);223} else {224group_end_transition = Ref<AnimationNodeStateMachineTransition>();225}226227// Validation.228if (anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {229indices = anodesm->find_transition_from(SceneStringName(Start));230int anodesm_start_size = indices.size();231indices = anodesm->find_transition_to(SceneStringName(End));232int anodesm_end_size = indices.size();233if (group_start_size > 1) {234WARN_PRINT_ED("There are two or more transitions to the Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + ", which may result in unintended transitions.");235}236if (group_end_size > 1) {237WARN_PRINT_ED("There are two or more transitions from the Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + ", which may result in unintended transitions.");238}239if (anodesm_start_size > 1) {240WARN_PRINT_ED("There are two or more transitions from the Start of Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + current + ", which may result in unintended transitions.");241}242if (anodesm_end_size > 1) {243WARN_PRINT_ED("There are two or more transitions to the End of Grouped AnimationNodeStateMachine in AnimationNodeStateMachine: " + base_path + current + ", which may result in unintended transitions.");244}245if (anodesm_start_size != group_start_size) {246ERR_PRINT_ED("There is a mismatch in the number of start transitions in and out of the Grouped AnimationNodeStateMachine on AnimationNodeStateMachine: " + base_path + current + ".");247}248if (anodesm_end_size != group_end_size) {249ERR_PRINT_ED("There is a mismatch in the number of end transitions in and out of the Grouped AnimationNodeStateMachine on AnimationNodeStateMachine: " + base_path + current + ".");250}251} else {252_signal_state_change(tree, current, true);253}254}255256void AnimationNodeStateMachinePlayback::_set_grouped(bool p_is_grouped) {257is_grouped = p_is_grouped;258}259260void AnimationNodeStateMachinePlayback::travel(const StringName &p_state, bool p_reset_on_teleport) {261ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");262ERR_FAIL_COND_EDMSG(String(p_state).contains("/Start") || String(p_state).contains("/End"), "Grouped AnimationNodeStateMachinePlayback doesn't allow to play Start/End directly. Instead, play the prev or next state of group in the parent AnimationNodeStateMachine.");263_travel_main(p_state, p_reset_on_teleport);264}265266void AnimationNodeStateMachinePlayback::start(const StringName &p_state, bool p_reset) {267ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");268ERR_FAIL_COND_EDMSG(String(p_state).contains("/Start") || String(p_state).contains("/End"), "Grouped AnimationNodeStateMachinePlayback doesn't allow to play Start/End directly. Instead, play the prev or next state of group in the parent AnimationNodeStateMachine.");269_start_main(p_state, p_reset);270}271272void AnimationNodeStateMachinePlayback::next() {273ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");274_next_main();275}276277void AnimationNodeStateMachinePlayback::stop() {278ERR_FAIL_COND_EDMSG(is_grouped, "Grouped AnimationNodeStateMachinePlayback must be handled by parent AnimationNodeStateMachinePlayback. You need to retrieve the parent Root/Nested AnimationNodeStateMachine.");279_stop_main();280}281282void AnimationNodeStateMachinePlayback::_travel_main(const StringName &p_state, bool p_reset_on_teleport) {283travel_request = p_state;284reset_request_on_teleport = p_reset_on_teleport;285stop_request = false;286}287288void AnimationNodeStateMachinePlayback::_start_main(const StringName &p_state, bool p_reset) {289travel_request = StringName();290path.clear();291reset_request = p_reset;292start_request = p_state;293stop_request = false;294}295296void AnimationNodeStateMachinePlayback::_next_main() {297next_request = true;298}299300void AnimationNodeStateMachinePlayback::_stop_main() {301stop_request = true;302}303304bool AnimationNodeStateMachinePlayback::is_playing() const {305return playing;306}307308bool AnimationNodeStateMachinePlayback::is_end() const {309return current == SceneStringName(End) && fading_from == StringName();310}311312StringName AnimationNodeStateMachinePlayback::get_current_node() const {313return current;314}315316StringName AnimationNodeStateMachinePlayback::get_fading_from_node() const {317return fading_from;318}319320Vector<StringName> AnimationNodeStateMachinePlayback::get_travel_path() const {321return path;322}323324TypedArray<StringName> AnimationNodeStateMachinePlayback::_get_travel_path() const {325return Variant(get_travel_path()).operator Array();326}327328float AnimationNodeStateMachinePlayback::get_current_play_pos() const {329return current_nti.position;330}331332float AnimationNodeStateMachinePlayback::get_current_length() const {333return current_nti.length;334}335336float AnimationNodeStateMachinePlayback::get_fade_from_play_pos() const {337return fadeing_from_nti.position;338}339340float AnimationNodeStateMachinePlayback::get_fade_from_length() const {341return fadeing_from_nti.length;342}343344float AnimationNodeStateMachinePlayback::get_fading_time() const {345return fading_time;346}347348float AnimationNodeStateMachinePlayback::get_fading_pos() const {349return fading_pos;350}351352bool _is_grouped_state_machine(const Ref<AnimationNodeStateMachine> p_node) {353return p_node.is_valid() && p_node->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED;354}355356void AnimationNodeStateMachinePlayback::_clear_fading(AnimationNodeStateMachine *p_state_machine, const StringName &p_state) {357if (!p_state.is_empty() && !_is_grouped_state_machine(p_state_machine->get_node(p_state))) {358_signal_state_change(p_state_machine->get_animation_tree(), p_state, false);359}360fading_from = StringName();361fadeing_from_nti = AnimationNode::NodeTimeInfo();362}363364void AnimationNodeStateMachinePlayback::_signal_state_change(AnimationTree *p_animation_tree, const StringName &p_state, bool p_started) {365if (is_grouped && p_animation_tree && p_state != SceneStringName(Start) && p_state != SceneStringName(End)) {366AnimationNodeStateMachinePlayback *parent_playback = *_get_parent_playback(p_animation_tree);367if (parent_playback) {368String prefix = base_path.substr(base_path.rfind_char('/', base_path.length() - 2) + 1);369parent_playback->_signal_state_change(p_animation_tree, prefix + p_state, p_started);370}371}372emit_signal(p_started ? SceneStringName(state_started) : SceneStringName(state_finished), p_state);373}374375void AnimationNodeStateMachinePlayback::_clear_path_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_test_only) {376List<AnimationNode::ChildNode> child_nodes;377p_state_machine->get_child_nodes(&child_nodes);378for (const AnimationNode::ChildNode &child_node : child_nodes) {379Ref<AnimationNodeStateMachine> anodesm = child_node.node;380if (_is_grouped_state_machine(anodesm)) {381Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + child_node.name + "/playback");382ERR_FAIL_COND(playback.is_null());383playback->_set_base_path(base_path + child_node.name + "/");384if (p_test_only) {385playback = playback->duplicate();386}387playback->path.clear();388playback->_clear_path_children(p_tree, anodesm.ptr(), p_test_only);389if (current != child_node.name) {390playback->_start(anodesm.ptr()); // Can restart.391}392}393}394}395396void AnimationNodeStateMachinePlayback::_start_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_test_only) {397if (p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {398return; // This function must be fired only by the top state machine, do nothing in child state machine.399}400Vector<String> temp_path = p_path.split("/");401if (temp_path.size() > 1) {402for (int i = 1; i < temp_path.size(); i++) {403String concatenated;404for (int j = 0; j < i; j++) {405concatenated += temp_path[j] + (j == i - 1 ? "" : "/");406}407Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(concatenated);408if (anodesm.is_valid() && anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {409ERR_FAIL_MSG("Root/Nested AnimationNodeStateMachine can't have path from parent AnimationNodeStateMachine.");410}411Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + concatenated + "/playback");412ERR_FAIL_COND(playback.is_null());413playback->_set_base_path(base_path + concatenated + "/");414if (p_test_only) {415playback = playback->duplicate();416}417playback->_start_main(temp_path[i], i == temp_path.size() - 1 ? reset_request : false);418}419reset_request = false;420}421}422423bool AnimationNodeStateMachinePlayback::_travel_children(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const String &p_path, bool p_is_allow_transition_to_self, bool p_is_parent_same_state, bool p_test_only) {424if (p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {425return false; // This function must be fired only by the top state machine, do nothing in child state machine.426}427Vector<String> temp_path = p_path.split("/");428Vector<ChildStateMachineInfo> children;429430bool found_route = true;431bool is_parent_same_state = p_is_parent_same_state;432if (temp_path.size() > 1) {433for (int i = 1; i < temp_path.size(); i++) {434String concatenated;435for (int j = 0; j < i; j++) {436concatenated += temp_path[j] + (j == i - 1 ? "" : "/");437}438439Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(concatenated);440if (anodesm.is_valid() && anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {441ERR_FAIL_V_MSG(false, "Root/Nested AnimationNodeStateMachine can't have path from parent AnimationNodeStateMachine.");442}443Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + concatenated + "/playback");444ERR_FAIL_COND_V(playback.is_null(), false);445playback->_set_base_path(base_path + concatenated + "/");446if (p_test_only) {447playback = playback->duplicate();448}449if (!playback->is_playing()) {450playback->_start(anodesm.ptr());451}452ChildStateMachineInfo child_info;453child_info.playback = playback;454455// Process for the case that parent state is changed.456bool child_found_route = true;457bool is_current_same_state = temp_path[i] == playback->get_current_node();458if (!is_parent_same_state) {459// Force travel to end current child state machine.460String child_path = "/" + playback->get_current_node();461while (true) {462Ref<AnimationNodeStateMachine> child_anodesm = p_state_machine->find_node_by_path(concatenated + child_path);463if (child_anodesm.is_null() || child_anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {464break;465}466Ref<AnimationNodeStateMachinePlayback> child_playback = p_tree->get(base_path + concatenated + child_path + "/playback");467ERR_FAIL_COND_V(child_playback.is_null(), false);468child_playback->_set_base_path(base_path + concatenated + "/");469if (p_test_only) {470child_playback = child_playback->duplicate();471}472child_playback->_travel_main(SceneStringName(End));473child_found_route &= child_playback->_travel(p_tree, child_anodesm.ptr(), false, p_test_only);474child_path += "/" + child_playback->get_current_node();475}476// Force restart target state machine.477playback->_start(anodesm.ptr());478}479is_parent_same_state = is_current_same_state;480481bool is_deepest_state = i == temp_path.size() - 1;482child_info.is_reset = is_deepest_state ? reset_request_on_teleport : false;483playback->_travel_main(temp_path[i], child_info.is_reset);484if (playback->_make_travel_path(p_tree, anodesm.ptr(), is_deepest_state ? p_is_allow_transition_to_self : false, child_info.path, p_test_only)) {485found_route &= child_found_route;486} else {487child_info.path.push_back(temp_path[i]);488found_route = false;489}490children.push_back(child_info);491}492reset_request_on_teleport = false;493}494495if (found_route) {496for (int i = 0; i < children.size(); i++) {497children.write[i].playback->clear_path();498for (int j = 0; j < children[i].path.size(); j++) {499children.write[i].playback->push_path(children[i].path[j]);500}501}502} else {503for (int i = 0; i < children.size(); i++) {504children.write[i].playback->_travel_main(StringName(), children[i].is_reset); // Clear travel.505if (children[i].path.size()) {506children.write[i].playback->_start_main(children[i].path[children[i].path.size() - 1], children[i].is_reset);507}508}509}510return found_route;511}512513void AnimationNodeStateMachinePlayback::_start(AnimationNodeStateMachine *p_state_machine) {514playing = true;515_set_current(p_state_machine, start_request != StringName() ? start_request : SceneStringName(Start));516teleport_request = true;517stop_request = false;518start_request = StringName();519}520521bool AnimationNodeStateMachinePlayback::_travel(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, bool p_test_only) {522return _make_travel_path(p_tree, p_state_machine, p_is_allow_transition_to_self, path, p_test_only);523}524525String AnimationNodeStateMachinePlayback::_validate_path(AnimationNodeStateMachine *p_state_machine, const String &p_path) {526if (p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {527return p_path; // Grouped state machine doesn't allow validate-able request.528}529String target = p_path;530Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(target);531while (anodesm.is_valid() && anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {532Vector<int> indices = anodesm->find_transition_from(SceneStringName(Start));533if (indices.size()) {534target = target + "/" + anodesm->get_transition_to(indices[0]); // Find next state of Start.535} else {536break; // There is no transition in Start state of grouped state machine.537}538anodesm = p_state_machine->find_node_by_path(target);539}540return target;541}542543bool AnimationNodeStateMachinePlayback::_make_travel_path(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, bool p_is_allow_transition_to_self, Vector<StringName> &r_path, bool p_test_only) {544StringName travel = travel_request;545travel_request = StringName();546547if (!playing) {548_start(p_state_machine);549}550551ERR_FAIL_COND_V(!p_state_machine->states.has(travel), false);552ERR_FAIL_COND_V(!p_state_machine->states.has(current), false);553554if (current == travel) {555return !p_is_allow_transition_to_self;556}557558Vector<StringName> new_path;559560Vector2 current_pos = p_state_machine->states[current].position;561Vector2 target_pos = p_state_machine->states[travel].position;562563bool found_route = false;564HashMap<StringName, AStarCost> cost_map;565566List<int> open_list;567568// Build open list.569for (int i = 0; i < p_state_machine->transitions.size(); i++) {570if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {571continue;572}573574if (p_state_machine->transitions[i].from == current) {575open_list.push_back(i);576float cost = p_state_machine->states[p_state_machine->transitions[i].to].position.distance_to(current_pos);577cost *= p_state_machine->transitions[i].transition->get_priority();578AStarCost ap;579ap.prev = current;580ap.distance = cost;581cost_map[p_state_machine->transitions[i].to] = ap;582583if (p_state_machine->transitions[i].to == travel) { // Prematurely found it! :D584found_route = true;585break;586}587}588}589590// Begin astar.591while (!found_route) {592if (open_list.is_empty()) {593break; // No path found.594}595596// Find the last cost transition.597List<int>::Element *least_cost_transition = nullptr;598float least_cost = 1e20;599600for (List<int>::Element *E = open_list.front(); E; E = E->next()) {601float cost = cost_map[p_state_machine->transitions[E->get()].to].distance;602cost += p_state_machine->states[p_state_machine->transitions[E->get()].to].position.distance_to(target_pos);603604if (cost < least_cost) {605least_cost_transition = E;606least_cost = cost;607}608}609610StringName transition_prev = p_state_machine->transitions[least_cost_transition->get()].from;611StringName transition = p_state_machine->transitions[least_cost_transition->get()].to;612613for (int i = 0; i < p_state_machine->transitions.size(); i++) {614if (p_state_machine->transitions[i].transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {615continue;616}617618if (p_state_machine->transitions[i].from != transition || p_state_machine->transitions[i].to == transition_prev) {619continue; // Not interested on those.620}621622float 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);623distance *= p_state_machine->transitions[i].transition->get_priority();624distance += cost_map[p_state_machine->transitions[i].from].distance;625626if (cost_map.has(p_state_machine->transitions[i].to)) {627// Oh this was visited already, can we win the cost?628if (distance < cost_map[p_state_machine->transitions[i].to].distance) {629cost_map[p_state_machine->transitions[i].to].distance = distance;630cost_map[p_state_machine->transitions[i].to].prev = p_state_machine->transitions[i].from;631}632} else {633// Add to open list.634AStarCost ac;635ac.prev = p_state_machine->transitions[i].from;636ac.distance = distance;637cost_map[p_state_machine->transitions[i].to] = ac;638639open_list.push_back(i);640641if (p_state_machine->transitions[i].to == travel) {642found_route = true;643break;644}645}646}647648if (found_route) {649break;650}651652open_list.erase(least_cost_transition);653}654655// Check child grouped state machine.656if (found_route) {657// Make path.658StringName at = travel;659while (at != current) {660new_path.push_back(at);661at = cost_map[at].prev;662}663new_path.reverse();664665// Check internal paths of child grouped state machine.666// For example:667// [current - End] - [Start - End] - [Start - End] - [Start - target]668String current_path = current;669int len = new_path.size() + 1;670for (int i = 0; i < len; i++) {671Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(current_path);672if (anodesm.is_valid() && anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {673Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + current_path + "/playback");674ERR_FAIL_COND_V(playback.is_null(), false);675playback->_set_base_path(base_path + current_path + "/");676if (p_test_only) {677playback = playback->duplicate();678}679if (i > 0) {680playback->_start(anodesm.ptr());681}682if (i >= new_path.size()) {683break; // Tracing has been finished, needs to break.684}685playback->_travel_main(SceneStringName(End));686if (!playback->_travel(p_tree, anodesm.ptr(), false, p_test_only)) {687found_route = false;688break;689}690}691if (i >= new_path.size()) {692break; // Tracing has been finished, needs to break.693}694current_path = new_path[i];695}696}697698// Finally, rewrite path if route is found.699if (found_route) {700r_path = new_path;701return true;702} else {703return false;704}705}706707AnimationNode::NodeTimeInfo AnimationNodeStateMachinePlayback::process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {708AnimationNode::NodeTimeInfo nti = _process(p_state_machine, p_playback_info, p_test_only);709start_request = StringName();710next_request = false;711stop_request = false;712reset_request_on_teleport = false;713return nti;714}715716AnimationNode::NodeTimeInfo AnimationNodeStateMachinePlayback::_process(AnimationNodeStateMachine *p_state_machine, const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {717AnimationTree *tree = p_state_machine->process_state->tree;718719double p_time = p_playback_info.time;720double p_delta = p_playback_info.delta;721bool p_seek = p_playback_info.seeked;722bool p_is_external_seeking = p_playback_info.is_external_seeking;723724// Check seek to 0 (means reset) by parent AnimationNode.725if (Math::is_zero_approx(p_time) && p_seek && !p_is_external_seeking) {726if (p_state_machine->state_machine_type != AnimationNodeStateMachine::STATE_MACHINE_TYPE_NESTED || is_end() || !playing) {727// Restart state machine.728if (p_state_machine->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {729path.clear();730_clear_path_children(tree, p_state_machine, p_test_only);731}732_start(p_state_machine);733reset_request = true;734} else {735// Reset current state.736reset_request = true;737teleport_request = true;738}739}740741if (stop_request) {742start_request = StringName();743travel_request = StringName();744path.clear();745playing = false;746return AnimationNode::NodeTimeInfo();747}748749if (!playing && start_request != StringName() && travel_request != StringName()) {750return AnimationNode::NodeTimeInfo();751}752753// Process start/travel request.754if (start_request != StringName() || travel_request != StringName()) {755if (p_state_machine->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {756_clear_path_children(tree, p_state_machine, p_test_only);757}758}759760if (start_request != StringName()) {761path.clear();762String start_target = _validate_path(p_state_machine, start_request);763Vector<String> start_path = String(start_target).split("/");764start_request = start_path[0];765if (start_path.size()) {766_start_children(tree, p_state_machine, start_target, p_test_only);767}768// Teleport to start.769if (p_state_machine->states.has(start_request)) {770_start(p_state_machine);771} else {772StringName node = start_request;773ERR_FAIL_V_MSG(AnimationNode::NodeTimeInfo(), "No such node: '" + node + "'");774}775}776777if (travel_request != StringName()) {778// Fix path.779String travel_target = _validate_path(p_state_machine, travel_request);780Vector<String> travel_path = travel_target.split("/");781travel_request = travel_path[0];782StringName temp_travel_request = travel_request; // For the case that can't travel.783// Process children.784Vector<StringName> new_path;785bool can_travel = _make_travel_path(tree, p_state_machine, travel_path.size() <= 1 ? p_state_machine->is_allow_transition_to_self() : false, new_path, p_test_only);786if (travel_path.size()) {787if (can_travel) {788can_travel = _travel_children(tree, p_state_machine, travel_target, p_state_machine->is_allow_transition_to_self(), travel_path[0] == current, p_test_only);789} else {790_start_children(tree, p_state_machine, travel_target, p_test_only);791}792}793794// Process to travel.795if (can_travel) {796path = new_path;797} else {798// Can't travel, then teleport.799if (p_state_machine->states.has(temp_travel_request)) {800path.clear();801if (current != temp_travel_request || reset_request_on_teleport) {802_set_current(p_state_machine, temp_travel_request);803reset_request = reset_request_on_teleport;804teleport_request = true;805}806} else {807ERR_FAIL_V_MSG(AnimationNode::NodeTimeInfo(), "No such node: '" + temp_travel_request + "'");808}809}810}811812AnimationMixer::PlaybackInfo pi = p_playback_info;813814if (teleport_request) {815teleport_request = false;816// Clear fading on teleport.817fading_from = StringName();818fadeing_from_nti = AnimationNode::NodeTimeInfo();819fading_pos = 0;820// Init current length.821pi.time = 0;822pi.seeked = true;823pi.is_external_seeking = false;824pi.weight = 0;825current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, true);826// Don't process first node if not necessary, instead process next node.827_transition_to_next_recursive(tree, p_state_machine, p_delta, p_test_only);828}829830// Check current node existence.831if (!p_state_machine->states.has(current)) {832playing = false; // Current does not exist.833_set_current(p_state_machine, StringName());834return AnimationNode::NodeTimeInfo();835}836837// Special case for grouped state machine Start/End to make priority with parent blend (means don't treat Start and End states as RESET animations).838bool is_start_of_group = false;839bool is_end_of_group = false;840if (!p_state_machine->are_ends_reset() || p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {841is_start_of_group = fading_from == SceneStringName(Start);842is_end_of_group = current == SceneStringName(End);843}844845// Calc blend amount by cross-fade.846float fade_blend = 1.0;847if (fading_time && fading_from != StringName()) {848if (!p_state_machine->states.has(fading_from)) {849fading_from = StringName();850} else {851if (!p_seek) {852fading_pos += Math::abs(p_delta);853}854fade_blend = MIN(1.0, fading_pos / fading_time);855}856}857if (current_curve.is_valid()) {858fade_blend = current_curve->sample(fade_blend);859}860fade_blend = Math::is_zero_approx(fade_blend) ? CMP_EPSILON : fade_blend;861if (is_start_of_group) {862fade_blend = 1.0;863} else if (is_end_of_group) {864fade_blend = 0.0;865}866867// Main process.868pi = p_playback_info;869pi.weight = fade_blend;870if (reset_request) {871reset_request = false;872pi.time = 0;873pi.seeked = true;874}875current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.876877// Cross-fade process.878if (fading_from != StringName()) {879double fade_blend_inv = 1.0 - fade_blend;880fade_blend_inv = Math::is_zero_approx(fade_blend_inv) ? CMP_EPSILON : fade_blend_inv;881if (is_start_of_group) {882fade_blend_inv = 0.0;883} else if (is_end_of_group) {884fade_blend_inv = 1.0;885}886887pi = p_playback_info;888pi.weight = fade_blend_inv;889if (_reset_request_for_fading_from) {890_reset_request_for_fading_from = false;891pi.time = 0;892pi.seeked = true;893}894fadeing_from_nti = p_state_machine->blend_node(p_state_machine->states[fading_from].node, fading_from, pi, AnimationNode::FILTER_IGNORE, true, p_test_only); // Blend values must be more than CMP_EPSILON to process discrete keys in edge.895896if (Animation::is_greater_or_equal_approx(fading_pos, fading_time)) {897// Finish fading.898_clear_fading(p_state_machine, fading_from);899}900}901902// Find next and see when to transition.903bool will_end = _transition_to_next_recursive(tree, p_state_machine, p_delta, p_test_only) || current == SceneStringName(End);904905// Predict remaining time.906if (will_end || ((p_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_NESTED) && !p_state_machine->has_transition_from(current))) {907// There is no next transition.908if (fading_from != StringName()) {909return Animation::is_greater_approx(current_nti.get_remain(), fadeing_from_nti.get_remain()) ? current_nti : fadeing_from_nti;910}911return current_nti;912}913914if (!is_end()) {915current_nti.is_infinity = true;916}917918return current_nti;919}920921bool AnimationNodeStateMachinePlayback::_transition_to_next_recursive(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, double p_delta, bool p_test_only) {922_reset_request_for_fading_from = false;923924AnimationMixer::PlaybackInfo pi;925pi.delta = p_delta;926NextInfo next;927Vector<StringName> transition_path;928transition_path.push_back(current);929while (true) {930next = _find_next(p_tree, p_state_machine);931932if (!_can_transition_to_next(p_tree, p_state_machine, next, p_test_only)) {933break; // Finish transition.934}935936if (transition_path.has(next.node)) {937WARN_PRINT_ONCE_ED("AnimationNodeStateMachinePlayback: " + base_path + "playback has detected one or more looped transitions in a single frame and aborted to prevent an infinite loop. You may need to check the transition settings.");938break; // Maybe infinity loop, do nothing more.939}940941transition_path.push_back(next.node);942943// Setting for fading.944if (next.xfade) {945// Time to fade.946fading_from = current;947fading_time = next.xfade;948fading_pos = 0;949} else {950if (reset_request) {951// There is no possibility of processing doubly. Now we can apply reset actually in here.952pi.time = 0;953pi.seeked = true;954pi.is_external_seeking = false;955pi.weight = 0;956p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, p_test_only);957}958_clear_fading(p_state_machine, current);959fading_time = 0;960fading_pos = 0;961}962963// If it came from path, remove path.964if (path.size()) {965path.remove_at(0);966}967968// Update current status.969_set_current(p_state_machine, next.node);970current_curve = next.curve;971972if (current == SceneStringName(End)) {973break;974}975976_reset_request_for_fading_from = reset_request; // To avoid processing doubly, it must be reset in the fading process within _process().977reset_request = next.is_reset;978979fadeing_from_nti = current_nti;980981if (next.switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_SYNC) {982pi.time = current_nti.position;983pi.seeked = true;984pi.is_external_seeking = false;985pi.weight = 0;986p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, p_test_only);987}988989// Just get length to find next recursive.990pi.time = 0;991pi.is_external_seeking = false;992pi.weight = 0;993pi.seeked = next.is_reset;994current_nti = p_state_machine->blend_node(p_state_machine->states[current].node, current, pi, AnimationNode::FILTER_IGNORE, true, true); // Just retrieve remain length, don't process.995996// Fading must be processed.997if (fading_time) {998break;999}1000}10011002return next.node == SceneStringName(End);1003}10041005bool AnimationNodeStateMachinePlayback::_can_transition_to_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, NextInfo p_next, bool p_test_only) {1006if (p_next.node == StringName()) {1007return false;1008}10091010if (next_request) {1011// Process request only once.1012next_request = false;1013// Next request must be applied to only deepest state machine.1014Ref<AnimationNodeStateMachine> anodesm = p_state_machine->find_node_by_path(current);1015if (anodesm.is_valid() && anodesm->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {1016Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(base_path + current + "/playback");1017ERR_FAIL_COND_V(playback.is_null(), false);1018playback->_set_base_path(base_path + current + "/");1019if (p_test_only) {1020playback = playback->duplicate();1021}1022playback->_next_main();1023// Then, fading should end.1024_clear_fading(p_state_machine, fading_from);1025fading_pos = 0;1026} else {1027return true;1028}1029}10301031if (fading_from != StringName()) {1032return false;1033}10341035if (current != SceneStringName(Start) && p_next.switch_mode == AnimationNodeStateMachineTransition::SWITCH_MODE_AT_END) {1036return Animation::is_less_or_equal_approx(current_nti.get_remain(p_next.break_loop_at_end), p_next.xfade);1037}1038return true;1039}10401041Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachinePlayback::_check_group_transition(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine, const AnimationNodeStateMachine::Transition &p_transition, Ref<AnimationNodeStateMachine> &r_state_machine, bool &r_bypass) const {1042Ref<AnimationNodeStateMachineTransition> temp_transition;1043Ref<AnimationNodeStateMachinePlayback> parent_playback;1044if (r_state_machine->get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {1045if (p_transition.from == SceneStringName(Start)) {1046parent_playback = _get_parent_playback(p_tree);1047if (parent_playback.is_valid()) {1048r_bypass = true;1049temp_transition = parent_playback->_get_group_start_transition();1050}1051} else if (p_transition.to == SceneStringName(End)) {1052parent_playback = _get_parent_playback(p_tree);1053if (parent_playback.is_valid()) {1054temp_transition = parent_playback->_get_group_end_transition();1055}1056}1057if (temp_transition.is_valid()) {1058r_state_machine = _get_parent_state_machine(p_tree);1059return temp_transition;1060}1061}1062return p_transition.transition;1063}10641065AnimationNodeStateMachinePlayback::NextInfo AnimationNodeStateMachinePlayback::_find_next(AnimationTree *p_tree, AnimationNodeStateMachine *p_state_machine) const {1066NextInfo next;1067if (path.size()) {1068for (int i = 0; i < p_state_machine->transitions.size(); i++) {1069Ref<AnimationNodeStateMachine> anodesm = p_state_machine;1070bool bypass = false;1071Ref<AnimationNodeStateMachineTransition> ref_transition = _check_group_transition(p_tree, p_state_machine, p_state_machine->transitions[i], anodesm, bypass);1072if (ref_transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {1073continue;1074}1075if (p_state_machine->transitions[i].from == current && p_state_machine->transitions[i].to == path[0]) {1076next.node = path[0];1077next.xfade = ref_transition->get_xfade_time();1078next.curve = ref_transition->get_xfade_curve();1079next.switch_mode = ref_transition->get_switch_mode();1080next.is_reset = ref_transition->is_reset();1081next.break_loop_at_end = ref_transition->is_loop_broken_at_end();1082}1083}1084} else {1085int auto_advance_to = -1;1086float priority_best = 1e20;1087for (int i = 0; i < p_state_machine->transitions.size(); i++) {1088Ref<AnimationNodeStateMachine> anodesm = p_state_machine;1089bool bypass = false;1090Ref<AnimationNodeStateMachineTransition> ref_transition = _check_group_transition(p_tree, p_state_machine, p_state_machine->transitions[i], anodesm, bypass);1091if (ref_transition->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED) {1092continue;1093}1094if (p_state_machine->transitions[i].from == current && (_check_advance_condition(anodesm, ref_transition) || bypass)) {1095if (ref_transition->get_priority() <= priority_best) {1096priority_best = ref_transition->get_priority();1097auto_advance_to = i;1098}1099}1100}11011102if (auto_advance_to != -1) {1103next.node = p_state_machine->transitions[auto_advance_to].to;1104Ref<AnimationNodeStateMachine> anodesm = p_state_machine;1105bool bypass = false;1106Ref<AnimationNodeStateMachineTransition> ref_transition = _check_group_transition(p_tree, p_state_machine, p_state_machine->transitions[auto_advance_to], anodesm, bypass);1107next.xfade = ref_transition->get_xfade_time();1108next.curve = ref_transition->get_xfade_curve();1109next.switch_mode = ref_transition->get_switch_mode();1110next.is_reset = ref_transition->is_reset();1111next.break_loop_at_end = ref_transition->is_loop_broken_at_end();1112}1113}11141115return next;1116}11171118bool AnimationNodeStateMachinePlayback::_check_advance_condition(const Ref<AnimationNodeStateMachine> state_machine, const Ref<AnimationNodeStateMachineTransition> transition) const {1119if (transition->get_advance_mode() != AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO) {1120return false;1121}11221123StringName advance_condition_name = transition->get_advance_condition_name();11241125if (advance_condition_name != StringName() && !bool(state_machine->get_parameter(advance_condition_name))) {1126return false;1127}11281129if (transition->expression.is_valid()) {1130AnimationTree *tree_base = state_machine->get_animation_tree();1131ERR_FAIL_NULL_V(tree_base, false);11321133NodePath advance_expression_base_node_path = tree_base->get_advance_expression_base_node();1134Node *expression_base = tree_base->get_node_or_null(advance_expression_base_node_path);11351136if (expression_base) {1137Ref<Expression> exp = transition->expression;1138bool ret = exp->execute(Array(), expression_base, false, Engine::get_singleton()->is_editor_hint()); // Avoids allowing the user to crash the system with an expression by only allowing const calls.1139if (exp->has_execute_failed() || !ret) {1140return false;1141}1142} else {1143WARN_PRINT_ONCE("Animation transition has a valid expression, but no expression base node was set on its AnimationTree.");1144}1145}11461147return true;1148}11491150void AnimationNodeStateMachinePlayback::clear_path() {1151path.clear();1152}11531154void AnimationNodeStateMachinePlayback::push_path(const StringName &p_state) {1155path.push_back(p_state);1156}11571158void AnimationNodeStateMachinePlayback::_set_base_path(const String &p_base_path) {1159base_path = p_base_path;1160}11611162Ref<AnimationNodeStateMachinePlayback> AnimationNodeStateMachinePlayback::_get_parent_playback(AnimationTree *p_tree) const {1163if (base_path.is_empty()) {1164return Ref<AnimationNodeStateMachinePlayback>();1165}1166Vector<String> split = base_path.split("/");1167ERR_FAIL_COND_V_MSG(split.size() < 2, Ref<AnimationNodeStateMachinePlayback>(), "Path is too short.");1168StringName self_path = split[split.size() - 2];1169split.remove_at(split.size() - 2);1170String playback_path = String("/").join(split) + "playback";1171Ref<AnimationNodeStateMachinePlayback> playback = p_tree->get(playback_path);1172if (playback.is_null()) {1173ERR_PRINT_ONCE("Can't get parent AnimationNodeStateMachinePlayback with path: " + playback_path + ". Maybe there is no Root/Nested AnimationNodeStateMachine in the parent of the Grouped AnimationNodeStateMachine.");1174return Ref<AnimationNodeStateMachinePlayback>();1175}1176if (playback->get_current_node() != self_path) {1177return Ref<AnimationNodeStateMachinePlayback>();1178}1179return playback;1180}11811182Ref<AnimationNodeStateMachine> AnimationNodeStateMachinePlayback::_get_parent_state_machine(AnimationTree *p_tree) const {1183if (base_path.is_empty()) {1184return Ref<AnimationNodeStateMachine>();1185}1186Vector<String> split = base_path.split("/");1187ERR_FAIL_COND_V_MSG(split.size() < 3, Ref<AnimationNodeStateMachine>(), "Path is too short.");1188split = split.slice(1, split.size() - 2);1189Ref<AnimationNode> root = p_tree->get_root_animation_node();1190ERR_FAIL_COND_V_MSG(root.is_null(), Ref<AnimationNodeStateMachine>(), "There is no root AnimationNode in AnimationTree: " + String(p_tree->get_name()));1191String anodesm_path = String("/").join(split);1192Ref<AnimationNodeStateMachine> anodesm = !anodesm_path.size() ? root : root->find_node_by_path(anodesm_path);1193ERR_FAIL_COND_V_MSG(anodesm.is_null(), Ref<AnimationNodeStateMachine>(), "Can't get state machine with path: " + anodesm_path);1194return anodesm;1195}11961197Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachinePlayback::_get_group_start_transition() const {1198ERR_FAIL_COND_V_MSG(group_start_transition.is_null(), Ref<AnimationNodeStateMachineTransition>(), "Group start transition is null.");1199return group_start_transition;1200}12011202Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachinePlayback::_get_group_end_transition() const {1203ERR_FAIL_COND_V_MSG(group_end_transition.is_null(), Ref<AnimationNodeStateMachineTransition>(), "Group end transition is null.");1204return group_end_transition;1205}12061207void AnimationNodeStateMachinePlayback::_bind_methods() {1208ClassDB::bind_method(D_METHOD("travel", "to_node", "reset_on_teleport"), &AnimationNodeStateMachinePlayback::travel, DEFVAL(true));1209ClassDB::bind_method(D_METHOD("start", "node", "reset"), &AnimationNodeStateMachinePlayback::start, DEFVAL(true));1210ClassDB::bind_method(D_METHOD("next"), &AnimationNodeStateMachinePlayback::next);1211ClassDB::bind_method(D_METHOD("stop"), &AnimationNodeStateMachinePlayback::stop);1212ClassDB::bind_method(D_METHOD("is_playing"), &AnimationNodeStateMachinePlayback::is_playing);1213ClassDB::bind_method(D_METHOD("get_current_node"), &AnimationNodeStateMachinePlayback::get_current_node);1214ClassDB::bind_method(D_METHOD("get_current_play_position"), &AnimationNodeStateMachinePlayback::get_current_play_pos);1215ClassDB::bind_method(D_METHOD("get_current_length"), &AnimationNodeStateMachinePlayback::get_current_length);1216ClassDB::bind_method(D_METHOD("get_fading_from_node"), &AnimationNodeStateMachinePlayback::get_fading_from_node);1217ClassDB::bind_method(D_METHOD("get_travel_path"), &AnimationNodeStateMachinePlayback::_get_travel_path);12181219ADD_SIGNAL(MethodInfo(SceneStringName(state_started), PropertyInfo(Variant::STRING_NAME, "state")));1220ADD_SIGNAL(MethodInfo(SceneStringName(state_finished), PropertyInfo(Variant::STRING_NAME, "state")));1221}12221223AnimationNodeStateMachinePlayback::AnimationNodeStateMachinePlayback() {1224set_local_to_scene(true); // Only one per instantiated scene.1225default_transition.instantiate();1226default_transition->set_xfade_time(0);1227default_transition->set_reset(true);1228default_transition->set_advance_mode(AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);1229default_transition->set_switch_mode(AnimationNodeStateMachineTransition::SWITCH_MODE_IMMEDIATE);1230}12311232///////////////////////////////////////////////////////12331234void AnimationNodeStateMachine::get_parameter_list(List<PropertyInfo> *r_list) const {1235AnimationNode::get_parameter_list(r_list);1236r_list->push_back(PropertyInfo(Variant::OBJECT, playback, PROPERTY_HINT_RESOURCE_TYPE, "AnimationNodeStateMachinePlayback", PROPERTY_USAGE_EDITOR | PROPERTY_USAGE_ALWAYS_DUPLICATE)); // Don't store this object in .tres, it always needs to be made as unique object.1237List<StringName> advance_conditions;1238for (int i = 0; i < transitions.size(); i++) {1239StringName ac = transitions[i].transition->get_advance_condition_name();1240if (ac != StringName() && advance_conditions.find(ac) == nullptr) {1241advance_conditions.push_back(ac);1242}1243}12441245advance_conditions.sort_custom<StringName::AlphCompare>();1246for (const StringName &E : advance_conditions) {1247r_list->push_back(PropertyInfo(Variant::BOOL, E));1248}1249}12501251Variant AnimationNodeStateMachine::get_parameter_default_value(const StringName &p_parameter) const {1252Variant ret = AnimationNode::get_parameter_default_value(p_parameter);1253if (ret != Variant()) {1254return ret;1255}12561257if (p_parameter == playback) {1258Ref<AnimationNodeStateMachinePlayback> p;1259p.instantiate();1260return p;1261} else {1262return false; // Advance condition.1263}1264}12651266bool AnimationNodeStateMachine::is_parameter_read_only(const StringName &p_parameter) const {1267if (AnimationNode::is_parameter_read_only(p_parameter)) {1268return true;1269}12701271if (p_parameter == playback) {1272return true;1273}1274return false;1275}12761277void AnimationNodeStateMachine::add_node(const StringName &p_name, Ref<AnimationNode> p_node, const Vector2 &p_position) {1278ERR_FAIL_COND(states.has(p_name));1279ERR_FAIL_COND(p_node.is_null());1280ERR_FAIL_COND(String(p_name).contains_char('/'));12811282State state_new;1283state_new.node = p_node;1284state_new.position = p_position;12851286states[p_name] = state_new;12871288emit_changed();1289emit_signal(SNAME("tree_changed"));12901291p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);1292p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);1293p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);1294}12951296void AnimationNodeStateMachine::replace_node(const StringName &p_name, Ref<AnimationNode> p_node) {1297ERR_FAIL_COND(states.has(p_name) == false);1298ERR_FAIL_COND(p_node.is_null());1299ERR_FAIL_COND(String(p_name).contains_char('/'));13001301{1302Ref<AnimationNode> node = states[p_name].node;1303if (node.is_valid()) {1304node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));1305node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));1306node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));1307}1308}13091310states[p_name].node = p_node;13111312emit_changed();1313emit_signal(SNAME("tree_changed"));13141315p_node->connect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);1316p_node->connect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed), CONNECT_REFERENCE_COUNTED);1317p_node->connect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed), CONNECT_REFERENCE_COUNTED);1318}13191320void AnimationNodeStateMachine::set_state_machine_type(StateMachineType p_state_machine_type) {1321state_machine_type = p_state_machine_type;1322emit_changed();1323emit_signal(SNAME("tree_changed"));1324notify_property_list_changed();1325}13261327AnimationNodeStateMachine::StateMachineType AnimationNodeStateMachine::get_state_machine_type() const {1328return state_machine_type;1329}13301331void AnimationNodeStateMachine::set_allow_transition_to_self(bool p_enable) {1332allow_transition_to_self = p_enable;1333}13341335bool AnimationNodeStateMachine::is_allow_transition_to_self() const {1336return allow_transition_to_self;1337}13381339void AnimationNodeStateMachine::set_reset_ends(bool p_enable) {1340reset_ends = p_enable;1341}13421343bool AnimationNodeStateMachine::are_ends_reset() const {1344return reset_ends;1345}13461347bool AnimationNodeStateMachine::can_edit_node(const StringName &p_name) const {1348if (states.has(p_name)) {1349const AnimationNode *anode = states[p_name].node.ptr();1350return !(Object::cast_to<AnimationNodeStartState>(anode) || Object::cast_to<AnimationNodeEndState>(anode));1351}13521353return true;1354}13551356Ref<AnimationNode> AnimationNodeStateMachine::get_node(const StringName &p_name) const {1357ERR_FAIL_COND_V_EDMSG(!states.has(p_name), Ref<AnimationNode>(), String(p_name) + " is not found current state.");13581359return states[p_name].node;1360}13611362StringName AnimationNodeStateMachine::get_node_name(const Ref<AnimationNode> &p_node) const {1363for (const KeyValue<StringName, State> &E : states) {1364if (E.value.node == p_node) {1365return E.key;1366}1367}13681369ERR_FAIL_V(StringName());1370}13711372void AnimationNodeStateMachine::get_child_nodes(List<ChildNode> *r_child_nodes) {1373Vector<StringName> nodes;13741375for (const KeyValue<StringName, State> &E : states) {1376nodes.push_back(E.key);1377}13781379nodes.sort_custom<StringName::AlphCompare>();13801381for (int i = 0; i < nodes.size(); i++) {1382ChildNode cn;1383cn.name = nodes[i];1384cn.node = states[cn.name].node;1385r_child_nodes->push_back(cn);1386}1387}13881389bool AnimationNodeStateMachine::has_node(const StringName &p_name) const {1390return states.has(p_name);1391}13921393void AnimationNodeStateMachine::remove_node(const StringName &p_name) {1394ERR_FAIL_COND(!states.has(p_name));13951396if (!can_edit_node(p_name)) {1397return;1398}13991400for (int i = 0; i < transitions.size(); i++) {1401if (transitions[i].from == p_name || transitions[i].to == p_name) {1402remove_transition_by_index(i);1403i--;1404}1405}14061407{1408Ref<AnimationNode> node = states[p_name].node;1409ERR_FAIL_COND(node.is_null());1410node->disconnect("tree_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));1411node->disconnect("animation_node_renamed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_renamed));1412node->disconnect("animation_node_removed", callable_mp(this, &AnimationNodeStateMachine::_animation_node_removed));1413}14141415states.erase(p_name);14161417emit_signal(SNAME("animation_node_removed"), get_instance_id(), p_name);1418emit_changed();1419emit_signal(SNAME("tree_changed"));1420}14211422void AnimationNodeStateMachine::rename_node(const StringName &p_name, const StringName &p_new_name) {1423ERR_FAIL_COND(!states.has(p_name));1424ERR_FAIL_COND(states.has(p_new_name));1425ERR_FAIL_COND(!can_edit_node(p_name));14261427states[p_new_name] = states[p_name];1428states.erase(p_name);14291430_rename_transitions(p_name, p_new_name);14311432emit_signal(SNAME("animation_node_renamed"), get_instance_id(), p_name, p_new_name);1433emit_changed();1434emit_signal(SNAME("tree_changed"));1435}14361437void AnimationNodeStateMachine::_rename_transitions(const StringName &p_name, const StringName &p_new_name) {1438if (updating_transitions) {1439return;1440}14411442updating_transitions = true;1443for (int i = 0; i < transitions.size(); i++) {1444if (transitions[i].from == p_name) {1445transitions.write[i].from = p_new_name;1446}1447if (transitions[i].to == p_name) {1448transitions.write[i].to = p_new_name;1449}1450}1451updating_transitions = false;1452}14531454LocalVector<StringName> AnimationNodeStateMachine::get_node_list() const {1455LocalVector<StringName> nodes;1456nodes.reserve(states.size());1457for (const KeyValue<StringName, State> &E : states) {1458nodes.push_back(E.key);1459}1460nodes.sort_custom<StringName::AlphCompare>();1461return nodes;1462}14631464TypedArray<StringName> AnimationNodeStateMachine::get_node_list_as_typed_array() const {1465TypedArray<StringName> typed_arr;1466LocalVector<StringName> vec = get_node_list();1467typed_arr.resize(vec.size());1468for (uint32_t i = 0; i < vec.size(); i++) {1469typed_arr[i] = vec[i];1470}1471return typed_arr;1472}14731474bool AnimationNodeStateMachine::has_transition(const StringName &p_from, const StringName &p_to) const {1475for (int i = 0; i < transitions.size(); i++) {1476if (transitions[i].from == p_from && transitions[i].to == p_to) {1477return true;1478}1479}1480return false;1481}14821483bool AnimationNodeStateMachine::has_transition_from(const StringName &p_from) const {1484for (int i = 0; i < transitions.size(); i++) {1485if (transitions[i].from == p_from) {1486return true;1487}1488}1489return false;1490}14911492bool AnimationNodeStateMachine::has_transition_to(const StringName &p_to) const {1493for (int i = 0; i < transitions.size(); i++) {1494if (transitions[i].to == p_to) {1495return true;1496}1497}1498return false;1499}15001501int AnimationNodeStateMachine::find_transition(const StringName &p_from, const StringName &p_to) const {1502for (int i = 0; i < transitions.size(); i++) {1503if (transitions[i].from == p_from && transitions[i].to == p_to) {1504return i;1505}1506}1507return -1;1508}15091510Vector<int> AnimationNodeStateMachine::find_transition_from(const StringName &p_from) const {1511Vector<int> ret;1512for (int i = 0; i < transitions.size(); i++) {1513if (transitions[i].from == p_from) {1514ret.push_back(i);1515}1516}1517return ret;1518}15191520Vector<int> AnimationNodeStateMachine::find_transition_to(const StringName &p_to) const {1521Vector<int> ret;1522for (int i = 0; i < transitions.size(); i++) {1523if (transitions[i].to == p_to) {1524ret.push_back(i);1525}1526}1527return ret;1528}15291530bool AnimationNodeStateMachine::_can_connect(const StringName &p_name) {1531if (states.has(p_name)) {1532return true;1533}15341535String node_name = p_name;1536Vector<String> path = node_name.split("/");15371538if (path.size() < 2) {1539return false;1540}15411542return false;1543}15441545void AnimationNodeStateMachine::add_transition(const StringName &p_from, const StringName &p_to, const Ref<AnimationNodeStateMachineTransition> &p_transition) {1546if (updating_transitions) {1547return;1548}15491550ERR_FAIL_COND(p_from == SceneStringName(End) || p_to == SceneStringName(Start));1551ERR_FAIL_COND(p_from == p_to);1552ERR_FAIL_COND(!_can_connect(p_from));1553ERR_FAIL_COND(!_can_connect(p_to));1554ERR_FAIL_COND(p_transition.is_null());15551556for (int i = 0; i < transitions.size(); i++) {1557ERR_FAIL_COND(transitions[i].from == p_from && transitions[i].to == p_to);1558}15591560updating_transitions = true;15611562Transition tr;1563tr.from = p_from;1564tr.to = p_to;1565tr.transition = p_transition;15661567tr.transition->connect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed), CONNECT_REFERENCE_COUNTED);15681569transitions.push_back(tr);15701571updating_transitions = false;1572}15731574Ref<AnimationNodeStateMachineTransition> AnimationNodeStateMachine::get_transition(int p_transition) const {1575ERR_FAIL_INDEX_V(p_transition, transitions.size(), Ref<AnimationNodeStateMachineTransition>());1576return transitions[p_transition].transition;1577}15781579StringName AnimationNodeStateMachine::get_transition_from(int p_transition) const {1580ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());1581return transitions[p_transition].from;1582}15831584StringName AnimationNodeStateMachine::get_transition_to(int p_transition) const {1585ERR_FAIL_INDEX_V(p_transition, transitions.size(), StringName());1586return transitions[p_transition].to;1587}15881589bool AnimationNodeStateMachine::is_transition_across_group(int p_transition) const {1590ERR_FAIL_INDEX_V(p_transition, transitions.size(), false);1591if (get_state_machine_type() == AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {1592if (transitions[p_transition].from == SceneStringName(Start) || transitions[p_transition].to == SceneStringName(End)) {1593return true;1594}1595}1596return false;1597}15981599int AnimationNodeStateMachine::get_transition_count() const {1600return transitions.size();1601}16021603void AnimationNodeStateMachine::remove_transition(const StringName &p_from, const StringName &p_to) {1604for (int i = 0; i < transitions.size(); i++) {1605if (transitions[i].from == p_from && transitions[i].to == p_to) {1606remove_transition_by_index(i);1607return;1608}1609}1610}16111612void AnimationNodeStateMachine::remove_transition_by_index(const int p_transition) {1613ERR_FAIL_INDEX(p_transition, transitions.size());1614transitions.write[p_transition].transition->disconnect("advance_condition_changed", callable_mp(this, &AnimationNodeStateMachine::_tree_changed));1615transitions.remove_at(p_transition);1616}16171618void AnimationNodeStateMachine::_remove_transition(const Ref<AnimationNodeStateMachineTransition> p_transition) {1619for (int i = 0; i < transitions.size(); i++) {1620if (transitions[i].transition == p_transition) {1621remove_transition_by_index(i);1622return;1623}1624}1625}16261627void AnimationNodeStateMachine::set_graph_offset(const Vector2 &p_offset) {1628graph_offset = p_offset;1629}16301631Vector2 AnimationNodeStateMachine::get_graph_offset() const {1632return graph_offset;1633}16341635AnimationNode::NodeTimeInfo AnimationNodeStateMachine::_process(const AnimationMixer::PlaybackInfo p_playback_info, bool p_test_only) {1636Ref<AnimationNodeStateMachinePlayback> playback_new = get_parameter(playback);1637ERR_FAIL_COND_V(playback_new.is_null(), AnimationNode::NodeTimeInfo());1638playback_new->_set_base_path(node_state.get_base_path());1639playback_new->_set_grouped(state_machine_type == STATE_MACHINE_TYPE_GROUPED);1640if (p_test_only) {1641playback_new = playback_new->duplicate(); // Don't process original when testing.1642}16431644return playback_new->process(this, p_playback_info, p_test_only);1645}16461647String AnimationNodeStateMachine::get_caption() const {1648return "StateMachine";1649}16501651Ref<AnimationNode> AnimationNodeStateMachine::get_child_by_name(const StringName &p_name) const {1652return get_node(p_name);1653}16541655bool AnimationNodeStateMachine::_set(const StringName &p_name, const Variant &p_value) {1656String prop_name = p_name;1657if (prop_name.begins_with("states/")) {1658String node_name = prop_name.get_slicec('/', 1);1659String what = prop_name.get_slicec('/', 2);16601661if (what == "node") {1662Ref<AnimationNode> anode = p_value;1663if (anode.is_valid()) {1664add_node(node_name, p_value);1665}1666return true;1667}16681669if (what == "position") {1670if (states.has(node_name)) {1671states[node_name].position = p_value;1672}1673return true;1674}1675} else if (prop_name == "transitions") {1676Array trans = p_value;1677ERR_FAIL_COND_V(trans.size() % 3 != 0, false);16781679for (int i = 0; i < trans.size(); i += 3) {1680add_transition(trans[i], trans[i + 1], trans[i + 2]);1681}1682return true;1683} else if (prop_name == "graph_offset") {1684set_graph_offset(p_value);1685return true;1686}16871688return false;1689}16901691bool AnimationNodeStateMachine::_get(const StringName &p_name, Variant &r_ret) const {1692String prop_name = p_name;1693if (prop_name.begins_with("states/")) {1694String node_name = prop_name.get_slicec('/', 1);1695String what = prop_name.get_slicec('/', 2);16961697if (what == "node") {1698if (states.has(node_name) && can_edit_node(node_name)) {1699r_ret = states[node_name].node;1700return true;1701}1702}17031704if (what == "position") {1705if (states.has(node_name)) {1706r_ret = states[node_name].position;1707return true;1708}1709}1710} else if (prop_name == "transitions") {1711Array trans;1712for (int i = 0; i < transitions.size(); i++) {1713String from = transitions[i].from;1714String to = transitions[i].to;17151716trans.push_back(from);1717trans.push_back(to);1718trans.push_back(transitions[i].transition);1719}17201721r_ret = trans;1722return true;1723} else if (prop_name == "graph_offset") {1724r_ret = get_graph_offset();1725return true;1726}17271728return false;1729}17301731void AnimationNodeStateMachine::_get_property_list(List<PropertyInfo> *p_list) const {1732LocalVector<StringName> names;1733names.reserve(states.size());1734for (const KeyValue<StringName, State> &E : states) {1735names.push_back(E.key);1736}1737names.sort_custom<StringName::AlphCompare>();17381739for (const StringName &prop_name : names) {1740p_list->push_back(PropertyInfo(Variant::OBJECT, "states/" + prop_name + "/node", PROPERTY_HINT_RESOURCE_TYPE, "AnimationNode", PROPERTY_USAGE_NO_EDITOR));1741p_list->push_back(PropertyInfo(Variant::VECTOR2, "states/" + prop_name + "/position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));1742}17431744p_list->push_back(PropertyInfo(Variant::ARRAY, "transitions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));1745p_list->push_back(PropertyInfo(Variant::VECTOR2, "graph_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR));1746}17471748void AnimationNodeStateMachine::_validate_property(PropertyInfo &p_property) const {1749if (p_property.name == "allow_transition_to_self" || p_property.name == "reset_ends") {1750if (state_machine_type == STATE_MACHINE_TYPE_GROUPED) {1751p_property.usage = PROPERTY_USAGE_NONE;1752}1753}1754}17551756void AnimationNodeStateMachine::reset_state() {1757states.clear();1758transitions.clear();1759playback = "playback";1760graph_offset = Vector2();17611762Ref<AnimationNodeStartState> s;1763s.instantiate();1764State start;1765start.node = s;1766start.position = Vector2(200, 100);1767states[SceneStringName(Start)] = start;17681769Ref<AnimationNodeEndState> e;1770e.instantiate();1771State end;1772end.node = e;1773end.position = Vector2(900, 100);1774states[SceneStringName(End)] = end;17751776emit_changed();1777emit_signal(SNAME("tree_changed"));1778}17791780void AnimationNodeStateMachine::set_node_position(const StringName &p_name, const Vector2 &p_position) {1781ERR_FAIL_COND(!states.has(p_name));1782states[p_name].position = p_position;1783}17841785Vector2 AnimationNodeStateMachine::get_node_position(const StringName &p_name) const {1786ERR_FAIL_COND_V(!states.has(p_name), Vector2());1787return states[p_name].position;1788}17891790void AnimationNodeStateMachine::_tree_changed() {1791emit_changed();1792AnimationRootNode::_tree_changed();1793}17941795void AnimationNodeStateMachine::_animation_node_renamed(const ObjectID &p_oid, const String &p_old_name, const String &p_new_name) {1796AnimationRootNode::_animation_node_renamed(p_oid, p_old_name, p_new_name);1797}17981799void AnimationNodeStateMachine::_animation_node_removed(const ObjectID &p_oid, const StringName &p_node) {1800AnimationRootNode::_animation_node_removed(p_oid, p_node);1801}18021803#ifdef TOOLS_ENABLED1804void AnimationNodeStateMachine::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {1805const String pf = p_function;1806bool add_state_options = false;1807if (p_idx == 0) {1808add_state_options = (pf == "get_node" || pf == "has_node" || pf == "rename_node" || pf == "remove_node" || pf == "replace_node" || pf == "set_node_position" || pf == "get_node_position");1809} else if (p_idx <= 1) {1810add_state_options = (pf == "has_transition" || pf == "add_transition" || pf == "remove_transition");1811}1812if (add_state_options) {1813for (const KeyValue<StringName, State> &E : states) {1814r_options->push_back(String(E.key).quote());1815}1816}1817AnimationRootNode::get_argument_options(p_function, p_idx, r_options);1818}1819#endif18201821void AnimationNodeStateMachine::_bind_methods() {1822ClassDB::bind_method(D_METHOD("add_node", "name", "node", "position"), &AnimationNodeStateMachine::add_node, DEFVAL(Vector2()));1823ClassDB::bind_method(D_METHOD("replace_node", "name", "node"), &AnimationNodeStateMachine::replace_node);1824ClassDB::bind_method(D_METHOD("get_node", "name"), &AnimationNodeStateMachine::get_node);1825ClassDB::bind_method(D_METHOD("remove_node", "name"), &AnimationNodeStateMachine::remove_node);1826ClassDB::bind_method(D_METHOD("rename_node", "name", "new_name"), &AnimationNodeStateMachine::rename_node);1827ClassDB::bind_method(D_METHOD("has_node", "name"), &AnimationNodeStateMachine::has_node);1828ClassDB::bind_method(D_METHOD("get_node_name", "node"), &AnimationNodeStateMachine::get_node_name);1829ClassDB::bind_method(D_METHOD("get_node_list"), &AnimationNodeStateMachine::get_node_list_as_typed_array);18301831ClassDB::bind_method(D_METHOD("set_node_position", "name", "position"), &AnimationNodeStateMachine::set_node_position);1832ClassDB::bind_method(D_METHOD("get_node_position", "name"), &AnimationNodeStateMachine::get_node_position);18331834ClassDB::bind_method(D_METHOD("has_transition", "from", "to"), &AnimationNodeStateMachine::has_transition);1835ClassDB::bind_method(D_METHOD("add_transition", "from", "to", "transition"), &AnimationNodeStateMachine::add_transition);1836ClassDB::bind_method(D_METHOD("get_transition", "idx"), &AnimationNodeStateMachine::get_transition);1837ClassDB::bind_method(D_METHOD("get_transition_from", "idx"), &AnimationNodeStateMachine::get_transition_from);1838ClassDB::bind_method(D_METHOD("get_transition_to", "idx"), &AnimationNodeStateMachine::get_transition_to);1839ClassDB::bind_method(D_METHOD("get_transition_count"), &AnimationNodeStateMachine::get_transition_count);1840ClassDB::bind_method(D_METHOD("remove_transition_by_index", "idx"), &AnimationNodeStateMachine::remove_transition_by_index);1841ClassDB::bind_method(D_METHOD("remove_transition", "from", "to"), &AnimationNodeStateMachine::remove_transition);18421843ClassDB::bind_method(D_METHOD("set_graph_offset", "offset"), &AnimationNodeStateMachine::set_graph_offset);1844ClassDB::bind_method(D_METHOD("get_graph_offset"), &AnimationNodeStateMachine::get_graph_offset);18451846ClassDB::bind_method(D_METHOD("set_state_machine_type", "state_machine_type"), &AnimationNodeStateMachine::set_state_machine_type);1847ClassDB::bind_method(D_METHOD("get_state_machine_type"), &AnimationNodeStateMachine::get_state_machine_type);18481849ClassDB::bind_method(D_METHOD("set_allow_transition_to_self", "enable"), &AnimationNodeStateMachine::set_allow_transition_to_self);1850ClassDB::bind_method(D_METHOD("is_allow_transition_to_self"), &AnimationNodeStateMachine::is_allow_transition_to_self);18511852ClassDB::bind_method(D_METHOD("set_reset_ends", "enable"), &AnimationNodeStateMachine::set_reset_ends);1853ClassDB::bind_method(D_METHOD("are_ends_reset"), &AnimationNodeStateMachine::are_ends_reset);18541855ADD_PROPERTY(PropertyInfo(Variant::INT, "state_machine_type", PROPERTY_HINT_ENUM, "Root,Nested,Grouped"), "set_state_machine_type", "get_state_machine_type");1856ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_transition_to_self"), "set_allow_transition_to_self", "is_allow_transition_to_self");1857ADD_PROPERTY(PropertyInfo(Variant::BOOL, "reset_ends"), "set_reset_ends", "are_ends_reset");18581859BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_ROOT);1860BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_NESTED);1861BIND_ENUM_CONSTANT(STATE_MACHINE_TYPE_GROUPED);1862}18631864Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_from(const StringName &p_node) const {1865Vector<StringName> result;1866for (const Transition &transition : transitions) {1867if (transition.from == p_node) {1868result.push_back(transition.to);1869}1870}1871return result;1872}18731874Vector<StringName> AnimationNodeStateMachine::get_nodes_with_transitions_to(const StringName &p_node) const {1875Vector<StringName> result;1876for (const Transition &transition : transitions) {1877if (transition.to == p_node) {1878result.push_back(transition.from);1879}1880}1881return result;1882}18831884AnimationNodeStateMachine::AnimationNodeStateMachine() {1885Ref<AnimationNodeStartState> s;1886s.instantiate();1887State start;1888start.node = s;1889start.position = Vector2(200, 100);1890states[SceneStringName(Start)] = start;18911892Ref<AnimationNodeEndState> e;1893e.instantiate();1894State end;1895end.node = e;1896end.position = Vector2(900, 100);1897states[SceneStringName(End)] = end;1898}189919001901