Path: blob/master/editor/animation/animation_state_machine_editor.cpp
21026 views
/**************************************************************************/1/* animation_state_machine_editor.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_state_machine_editor.h"3132#include "core/io/resource_loader.h"33#include "core/math/geometry_2d.h"34#include "core/os/keyboard.h"35#include "editor/editor_node.h"36#include "editor/editor_undo_redo_manager.h"37#include "editor/gui/editor_file_dialog.h"38#include "editor/settings/editor_settings.h"39#include "editor/themes/editor_scale.h"40#include "scene/animation/animation_blend_tree.h"41#include "scene/gui/line_edit.h"42#include "scene/gui/option_button.h"43#include "scene/gui/panel_container.h"44#include "scene/gui/separator.h"45#include "scene/main/viewport.h"46#include "scene/main/window.h"47#include "scene/resources/style_box_flat.h"48#include "scene/theme/theme_db.h"4950bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) {51Ref<AnimationNodeStateMachine> ansm = p_node;52return ansm.is_valid();53}5455void AnimationNodeStateMachineEditor::edit(const Ref<AnimationNode> &p_node) {56state_machine = p_node;5758read_only = false;5960if (state_machine.is_valid()) {61read_only = EditorNode::get_singleton()->is_resource_read_only(state_machine);6263selected_transition_from = StringName();64selected_transition_to = StringName();65selected_transition_index = -1;66selected_node = StringName();67selected_nodes.clear();68connected_nodes.clear();69_update_mode();70_update_graph();71}7273if (read_only) {74tool_create->set_pressed(false);75tool_connect->set_pressed(false);76}7778tool_create->set_disabled(read_only);79tool_connect->set_disabled(read_only);80}8182String AnimationNodeStateMachineEditor::_get_root_playback_path(String &r_node_directory) {83AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();84Vector<String> edited_path = AnimationTreeEditor::get_singleton()->get_edited_path();8586String base_path;87Vector<String> node_directory_path;8889bool is_playable_anodesm_found = false;9091if (edited_path.size()) {92while (!is_playable_anodesm_found) {93base_path = String("/").join(edited_path);94Ref<AnimationNodeStateMachine> anodesm = !edited_path.size() ? Ref<AnimationNode>(tree->get_root_animation_node().ptr()) : tree->get_root_animation_node()->find_node_by_path(base_path);95if (anodesm.is_null()) {96break;97} else {98if (anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {99is_playable_anodesm_found = true;100} else {101int idx = edited_path.size() - 1;102node_directory_path.push_back(edited_path[idx]);103edited_path.remove_at(idx);104}105}106}107}108109if (is_playable_anodesm_found) {110// Return Root/Nested state machine playback.111node_directory_path.reverse();112r_node_directory = String("/").join(node_directory_path);113if (node_directory_path.size()) {114r_node_directory += "/";115}116base_path = !edited_path.size() ? Animation::PARAMETERS_BASE_PATH + "playback" : Animation::PARAMETERS_BASE_PATH + base_path + "/playback";117} else {118// Hmmm, we have to return Grouped state machine playback...119// It will give the user the error that Root/Nested state machine should be retrieved, that would be kind :-)120r_node_directory = String();121base_path = AnimationTreeEditor::get_singleton()->get_base_path() + "playback";122}123124return base_path;125}126127void AnimationNodeStateMachineEditor::_reconnect_transition() {128if (reconnecting_transition_index < 0 || reconnecting_transition_target.is_empty()) {129return;130}131132StringName old_from = state_machine->get_transition_from(reconnecting_transition_index);133StringName old_to = state_machine->get_transition_to(reconnecting_transition_index);134StringName new_from;135StringName new_to;136137if (reconnecting_transition_start) {138new_from = reconnecting_transition_target;139new_to = old_to;140} else {141new_from = old_from;142new_to = reconnecting_transition_target;143}144145// Preserve transition properties.146Ref<AnimationNodeStateMachineTransition> transition = state_machine->get_transition(reconnecting_transition_index);147148updating = true;149EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();150undo_redo->create_action(TTR("Reconnect Transition"));151152// Remove old transition.153undo_redo->add_do_method(state_machine.ptr(), "remove_transition", old_from, old_to);154undo_redo->add_undo_method(state_machine.ptr(), "add_transition", old_from, old_to, transition);155156// Add new transition.157undo_redo->add_do_method(state_machine.ptr(), "add_transition", new_from, new_to, transition);158undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", new_from, new_to);159160undo_redo->add_do_method(this, "_select_transition", new_from, new_to);161undo_redo->add_undo_method(this, "_select_transition", old_from, old_to);162163undo_redo->add_do_method(this, "_update_graph");164undo_redo->add_undo_method(this, "_update_graph");165undo_redo->commit_action();166updating = false;167168selected_transition_from = new_from;169selected_transition_to = new_to;170_update_mode();171}172173void AnimationNodeStateMachineEditor::_select_transition(const StringName &p_from, const StringName &p_to) {174selected_transition_from = p_from;175selected_transition_to = p_to;176selected_transition_index = -1;177178// Find transition index.179for (int i = 0; i < state_machine->get_transition_count(); i++) {180if (state_machine->get_transition_from(i) == p_from && state_machine->get_transition_to(i) == p_to) {181selected_transition_index = i;182break;183}184}185186selected_node = StringName();187selected_nodes.clear();188189connected_nodes.clear();190connected_nodes.insert(selected_transition_from);191connected_nodes.insert(selected_transition_to);192193// Push transition to the inspector.194if (selected_transition_index >= 0) {195Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(selected_transition_index);196if (!state_machine->is_transition_across_group(selected_transition_index)) {197EditorNode::get_singleton()->push_item(tr.ptr(), "", true);198} else {199EditorNode::get_singleton()->push_item(tr.ptr(), "", true);200EditorNode::get_singleton()->push_item(nullptr, "", true);201}202}203204_update_mode();205}206207void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEvent> &p_event) {208AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();209if (!tree) {210return;211}212213String node_directory;214Ref<AnimationNodeStateMachinePlayback> playback = tree->get(_get_root_playback_path(node_directory));215if (playback.is_null()) {216return;217}218219Ref<InputEventKey> k = p_event;220if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {221if (selected_node != StringName() || !selected_nodes.is_empty() || selected_transition_to != StringName() || selected_transition_from != StringName()) {222if (!read_only) {223_erase_selected();224}225accept_event();226}227}228229Ref<InputEventMouseButton> mb = p_event;230231// Add new node232if (!read_only) {233if (mb.is_valid() && mb->is_pressed() && !box_selecting && !connecting && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == MouseButton::LEFT))) {234connecting_from = StringName();235_open_menu(mb->get_position());236}237}238239// Select node or push a field inside240if (mb.is_valid() && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {241selected_transition_from = StringName();242selected_transition_to = StringName();243selected_transition_index = -1;244selected_node = StringName();245connected_nodes.clear();246247for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order248if (node_rects[i].play.has_point(mb->get_position())) { //edit name249if (play_mode->get_selected() == 1 || !playback->is_playing()) {250// Start251playback->start(node_directory + String(node_rects[i].node_name));252} else {253// Travel254playback->travel(node_directory + String(node_rects[i].node_name));255}256state_machine_draw->queue_redraw();257return;258}259260if (!read_only) {261if (node_rects[i].name.has_point(mb->get_position()) && state_machine->can_edit_node(node_rects[i].node_name)) { // edit name262// TODO: Avoid using strings, expose a method on LineEdit.263Ref<StyleBox> line_sb = name_edit->get_theme_stylebox(CoreStringName(normal));264Rect2 edit_rect = node_rects[i].name;265edit_rect.position -= line_sb->get_offset();266edit_rect.size += line_sb->get_minimum_size();267268name_edit_popup->set_position(state_machine_draw->get_screen_position() + edit_rect.position);269name_edit_popup->set_size(edit_rect.size);270name_edit->set_text(node_rects[i].node_name);271name_edit_popup->popup();272name_edit->grab_focus();273name_edit->select_all();274275prev_name = node_rects[i].node_name;276return;277}278}279280if (node_rects[i].edit.has_point(mb->get_position())) { //edit name281callable_mp(this, &AnimationNodeStateMachineEditor::_open_editor).call_deferred(node_rects[i].node_name);282return;283}284285if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected286selected_node = node_rects[i].node_name;287288if (!selected_nodes.has(selected_node)) {289selected_nodes.clear();290}291292selected_nodes.insert(selected_node);293_update_connected_nodes(selected_node);294295Ref<AnimationNode> anode = state_machine->get_node(selected_node);296EditorNode::get_singleton()->push_item(anode.ptr(), "", true);297state_machine_draw->queue_redraw();298dragging_selected_attempt = true;299dragging_selected = false;300drag_from = mb->get_position();301snap_x = StringName();302snap_y = StringName();303_update_mode();304return;305}306}307308// Test the transition lines.309int closest = -1;310float closest_d = 1e20;311Vector<int> close_candidates;312313// First find closest lines using point-to-segment distance.314for (int i = 0; i < transition_lines.size(); i++) {315Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mb->get_position(), transition_lines[i].from, transition_lines[i].to);316float d = cpoint.distance_to(mb->get_position());317318if (d > transition_lines[i].width) {319continue;320}321322// If this is very close to our current closest distance, add it to candidates.323if (Math::abs(d - closest_d) < 2.0) { // Within 2 pixels.324close_candidates.push_back(i);325} else if (d < closest_d) {326closest_d = d;327closest = i;328close_candidates.clear();329close_candidates.push_back(i);330}331}332333// Use midpoint distance as bias.334if (close_candidates.size() > 1) {335float best_midpoint_dist = 1e20;336337for (int idx : close_candidates) {338Vector2 midpoint = (transition_lines[idx].from + transition_lines[idx].to) / 2.0;339float midpoint_dist = midpoint.distance_to(mb->get_position());340341if (midpoint_dist < best_midpoint_dist) {342best_midpoint_dist = midpoint_dist;343closest = idx;344}345}346}347348if (closest >= 0) {349_select_transition(transition_lines[closest].from_node, transition_lines[closest].to_node);350}351352// If no state or transition was selected, select host StateMachine node.353if (selected_node.is_empty() && selected_transition_index == -1) {354EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true);355}356357state_machine_draw->queue_redraw();358_update_mode();359}360361// End moving node362if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {363if (dragging_selected) {364Ref<AnimationNode> an = state_machine->get_node(selected_node);365updating = true;366367EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();368undo_redo->create_action(TTR("Move Node"));369370for (int i = 0; i < node_rects.size(); i++) {371if (!selected_nodes.has(node_rects[i].node_name)) {372continue;373}374375undo_redo->add_do_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name) + drag_ofs / EDSCALE);376undo_redo->add_undo_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name));377}378379undo_redo->add_do_method(this, "_update_graph");380undo_redo->add_undo_method(this, "_update_graph");381undo_redo->commit_action();382updating = false;383}384snap_x = StringName();385snap_y = StringName();386387dragging_selected_attempt = false;388dragging_selected = false;389state_machine_draw->queue_redraw();390}391392// Connect nodes393if (mb.is_valid() && ((tool_select->is_pressed() && mb->is_shift_pressed()) || tool_connect->is_pressed()) && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {394for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order395if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected396connecting = true;397connection_follows_cursor = true;398connecting_from = node_rects[i].node_name;399connecting_to = mb->get_position();400connecting_to_node = StringName();401return;402}403}404}405406// End connecting nodes407if (mb.is_valid() && connecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {408if (connecting_to_node != StringName()) {409Ref<AnimationNode> node = state_machine->get_node(connecting_to_node);410Ref<AnimationNodeStateMachine> anodesm = node;411Ref<AnimationNodeEndState> end_node = node;412413if (state_machine->has_transition(connecting_from, connecting_to_node) && state_machine->can_edit_node(connecting_to_node) && anodesm.is_null()) {414EditorNode::get_singleton()->show_warning(TTR("Transition exists!"));415connecting = false;416} else {417_add_transition();418}419} else {420_open_menu(mb->get_position());421}422connecting_to_node = StringName();423connection_follows_cursor = false;424state_machine_draw->queue_redraw();425}426427// Start transition reconnection.428if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {429// Check if we're clicking on a hovered transition endpoint to start dragging.430if (hovered_transition_index >= 0 && !read_only) {431reconnecting = true;432reconnecting_transition_index = hovered_transition_index;433reconnecting_transition_start = hovered_transition_start;434reconnecting_transition_pos = mb->get_position();435reconnecting_transition_target = StringName();436437StringName connected_node = reconnecting_transition_start ? transition_lines[reconnecting_transition_index].to_node : transition_lines[reconnecting_transition_index].from_node;438439reconnecting_from_node_rect_index = -1;440for (int i = 0; i < node_rects.size(); i++) {441if (node_rects[i].node_name == connected_node) {442reconnecting_from_node_rect_index = i;443break;444}445}446447// Clear other selections when starting transition drag.448selected_transition_from = StringName();449selected_transition_to = StringName();450selected_transition_index = -1;451selected_node = StringName();452selected_nodes.clear();453connected_nodes.clear();454455state_machine_draw->queue_redraw();456return;457}458}459460// End transition reconnection.461if (mb.is_valid() && reconnecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {462if (reconnecting_transition_target != StringName()) {463// Check if reconnection is valid.464StringName old_from = state_machine->get_transition_from(reconnecting_transition_index);465StringName old_to = state_machine->get_transition_to(reconnecting_transition_index);466StringName new_from = reconnecting_transition_start ? reconnecting_transition_target : old_from;467StringName new_to = reconnecting_transition_start ? old_to : reconnecting_transition_target;468469if (new_from == old_from && new_to == old_to) {470// No change.471} else if (new_from == new_to) {472EditorNode::get_singleton()->show_warning(TTR("Cannot transition to self!"));473} else if (new_to == SceneStringName(Start)) {474EditorNode::get_singleton()->show_warning(TTR("Cannot transition to \"Start\"!"));475} else if (new_from == SceneStringName(End)) {476EditorNode::get_singleton()->show_warning(TTR("Cannot transition from \"End\"!"));477} else if (state_machine->has_transition(new_from, new_to)) {478EditorNode::get_singleton()->show_warning(vformat(TTR("Transition from \"%s\" to \"%s\" already exists!"), new_from, new_to));479} else {480_reconnect_transition();481}482}483484// Reset dragging state.485reconnecting = false;486reconnecting_transition_index = -1;487reconnecting_transition_start = false;488reconnecting_transition_target = StringName();489state_machine_draw->queue_redraw();490return;491}492493// Start box selecting494if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && tool_select->is_pressed()) {495box_selecting = true;496box_selecting_from = box_selecting_to = state_machine_draw->get_local_mouse_position();497box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());498499if (mb->is_command_or_control_pressed() || mb->is_shift_pressed()) {500previous_selected = selected_nodes;501} else {502selected_nodes.clear();503previous_selected.clear();504}505}506507// End box selecting508if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {509box_selecting = false;510state_machine_draw->queue_redraw();511_update_mode();512}513514if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {515StringName clicked_node;516for (int i = node_rects.size() - 1; i >= 0; i--) {517if (node_rects[i].node.has_point(mb->get_position())) {518clicked_node = node_rects[i].node_name;519break;520}521}522523if (clicked_node != StringName()) {524if (selected_nodes.has(clicked_node) && mb->is_shift_pressed()) {525selected_nodes.erase(clicked_node);526} else {527if (!mb->is_shift_pressed()) {528selected_nodes.clear();529}530selected_nodes.insert(clicked_node);531}532selected_node = clicked_node;533} else {534// Clicked on empty space.535selected_nodes.clear();536selected_node = StringName();537}538539_update_connected_nodes(selected_node);540state_machine_draw->queue_redraw();541_update_mode();542543if (clicked_node != StringName()) {544Ref<AnimationNode> anode = state_machine->get_node(clicked_node);545EditorNode::get_singleton()->push_item(anode.ptr(), "", true);546dragging_selected_attempt = true;547dragging_selected = false;548drag_from = mb->get_position();549snap_x = StringName();550snap_y = StringName();551}552553return;554}555556Ref<InputEventMouseMotion> mm = p_event;557558// Pan window559if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {560h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);561v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);562}563564// Move mouse while connecting565if (mm.is_valid() && connecting && connection_follows_cursor && !read_only) {566connecting_to = mm->get_position();567connecting_to_node = StringName();568state_machine_draw->queue_redraw();569570for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order571if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected572connecting_to_node = node_rects[i].node_name;573return;574}575}576}577578// Move mouse while reconnecting.579if (mm.is_valid() && reconnecting && !read_only) {580reconnecting_transition_pos = mm->get_position();581reconnecting_transition_target = StringName();582reconnecting_to_node_rect_index = -1;583584for (int i = node_rects.size() - 1; i >= 0; i--) {585if (node_rects[i].node.has_point(reconnecting_transition_pos)) {586reconnecting_transition_target = node_rects[i].node_name;587reconnecting_to_node_rect_index = i;588break;589}590}591592state_machine_draw->queue_redraw();593return;594}595596// Move mouse while moving a node597if (mm.is_valid() && dragging_selected_attempt && !read_only) {598dragging_selected = true;599drag_ofs = mm->get_position() - drag_from;600snap_x = StringName();601snap_y = StringName();602{603//snap604Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE;605LocalVector<StringName> nodes = state_machine->get_node_list();606607float best_d_x = 1e20;608float best_d_y = 1e20;609610for (const StringName &E : nodes) {611if (E == selected_node) {612continue;613}614Vector2 npos = state_machine->get_node_position(E);615616float d_x = Math::abs(npos.x - cpos.x);617if (d_x < MIN(5, best_d_x)) {618drag_ofs.x -= cpos.x - npos.x;619best_d_x = d_x;620snap_x = E;621}622623float d_y = Math::abs(npos.y - cpos.y);624if (d_y < MIN(5, best_d_y)) {625drag_ofs.y -= cpos.y - npos.y;626best_d_y = d_y;627snap_y = E;628}629}630}631632state_machine_draw->queue_redraw();633}634635// Move mouse while moving box select636if (mm.is_valid() && box_selecting) {637box_selecting_to = state_machine_draw->get_local_mouse_position();638639box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());640641for (int i = 0; i < node_rects.size(); i++) {642bool in_box = node_rects[i].node.intersects(box_selecting_rect);643644if (in_box) {645if (previous_selected.has(node_rects[i].node_name)) {646selected_nodes.erase(node_rects[i].node_name);647} else {648selected_nodes.insert(node_rects[i].node_name);649}650} else {651if (previous_selected.has(node_rects[i].node_name)) {652selected_nodes.insert(node_rects[i].node_name);653} else {654selected_nodes.erase(node_rects[i].node_name);655}656}657}658659state_machine_draw->queue_redraw();660}661662if (mm.is_valid()) {663String new_hovered_node_name;664HoveredNodeArea new_hovered_node_area = HOVER_NODE_NONE;665if (tool_select->is_pressed()) {666for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.667668if (!state_machine->can_edit_node(node_rects[i].node_name)) {669continue; // start/end node can't be edited670}671672if (node_rects[i].node.has_point(mm->get_position())) {673new_hovered_node_name = node_rects[i].node_name;674if (node_rects[i].play.has_point(mm->get_position())) {675new_hovered_node_area = HOVER_NODE_PLAY;676} else if (node_rects[i].edit.has_point(mm->get_position())) {677new_hovered_node_area = HOVER_NODE_EDIT;678}679break;680}681}682}683684if (new_hovered_node_name != hovered_node_name || new_hovered_node_area != hovered_node_area) {685hovered_node_name = new_hovered_node_name;686hovered_node_area = new_hovered_node_area;687state_machine_draw->queue_redraw();688}689690// Skip if over node.691for (int i = node_rects.size() - 1; i >= 0; i--) {692if (node_rects[i].node.has_point(mm->get_position())) {693if (hovered_transition_index != -1) {694hovered_transition_index = -1;695hovered_transition_start = false;696state_machine_draw->queue_redraw();697}698state_machine_draw->set_tooltip_text("");699return;700}701}702703// Set transition tooltip or reconnection endpoint.704if (tool_select->is_pressed()) {705int closest_for_highlight = -1;706int closest_for_tooltip = -1;707float closest_d_highlight = 1e20;708float closest_d_tooltip = 1e20;709bool closest_is_start = false;710711for (int i = 0; i < transition_lines.size(); i++) {712Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mm->get_position(), transition_lines[i].from, transition_lines[i].to);713float d = cpoint.distance_to(mm->get_position());714if (d > transition_lines[i].width) {715continue;716}717718// Check for tooltip (anywhere along the line).719if (d < closest_d_tooltip) {720closest_d_tooltip = d;721closest_for_tooltip = i;722}723724// Calculate dynamic hover distance based on transition length.725float transition_length = transition_lines[i].from.distance_to(transition_lines[i].to);726float hover_distance = MIN(20.0f, transition_length * 0.2f); // 20px or 20% of length, whichever is smaller.727728// Check distance to both endpoints for highlighting.729float dist_to_start = mm->get_position().distance_to(transition_lines[i].from);730float dist_to_end = mm->get_position().distance_to(transition_lines[i].to);731732bool near_start = (dist_to_start <= hover_distance);733bool near_end = (dist_to_end <= hover_distance);734735// Determine which end is closer if both are within range.736bool is_start_closer = dist_to_start < dist_to_end;737738if ((near_start || near_end) && d < closest_d_highlight) {739StringName from_node = transition_lines[i].from_node;740StringName to_node = transition_lines[i].to_node;741742bool is_start_endpoint = near_start && (is_start_closer || !near_end);743closest_d_highlight = d;744closest_for_highlight = i;745closest_is_start = is_start_endpoint;746}747}748749// Update hovered endpoint for reconnection.750if (hovered_transition_index != closest_for_highlight || hovered_transition_start != closest_is_start) {751hovered_transition_index = closest_for_highlight;752hovered_transition_start = closest_is_start;753state_machine_draw->queue_redraw();754}755756// Set tooltip for any part of the transition line.757if (closest_for_tooltip >= 0) {758String from = transition_lines[closest_for_tooltip].from_node;759String to = transition_lines[closest_for_tooltip].to_node;760String tooltip = from + U" → " + to;761state_machine_draw->set_tooltip_text(tooltip);762} else {763state_machine_draw->set_tooltip_text("");764}765}766}767768Ref<InputEventPanGesture> pan_gesture = p_event;769if (pan_gesture.is_valid()) {770h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);771v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);772}773}774775Control::CursorShape AnimationNodeStateMachineEditor::get_cursor_shape(const Point2 &p_pos) const {776Control::CursorShape cursor_shape = get_default_cursor_shape();777if (!read_only) {778// Put ibeam (text cursor) over names to make it clearer that they are editable.779Transform2D xform = panel->get_transform() * state_machine_draw->get_transform();780Point2 pos = xform.xform_inv(p_pos);781782for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.783if (node_rects[i].node.has_point(pos)) {784if (node_rects[i].name.has_point(pos)) {785if (state_machine->can_edit_node(node_rects[i].node_name)) {786cursor_shape = Control::CURSOR_IBEAM;787}788}789break;790}791}792}793return cursor_shape;794}795796String AnimationNodeStateMachineEditor::get_tooltip(const Point2 &p_pos) const {797if (hovered_node_name == StringName()) {798return AnimationTreeNodeEditorPlugin::get_tooltip(p_pos);799}800801String tooltip_text;802if (hovered_node_area == HOVER_NODE_PLAY) {803tooltip_text = vformat(TTR("Play/Travel to %s"), hovered_node_name);804} else if (hovered_node_area == HOVER_NODE_EDIT) {805tooltip_text = vformat(TTR("Edit %s"), hovered_node_name);806} else {807tooltip_text = hovered_node_name;808}809810return tooltip_text;811}812813void AnimationNodeStateMachineEditor::_open_menu(const Vector2 &p_position) {814AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();815if (!tree) {816return;817}818819menu->clear(false);820animations_menu->clear();821animations_to_add.clear();822823List<StringName> animation_names;824tree->get_animation_list(&animation_names);825menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);826if (animation_names.is_empty()) {827menu->set_item_disabled(menu->get_item_idx_from_text(TTR("Add Animation")), true);828} else {829for (const StringName &name : animation_names) {830animations_menu->add_icon_item(theme_cache.animation_icon, name);831animations_to_add.push_back(name);832}833}834835LocalVector<StringName> classes;836ClassDB::get_inheriters_from_class("AnimationRootNode", classes);837classes.sort_custom<StringName::AlphCompare>();838839for (const StringName &class_name : classes) {840String name = String(class_name).replace_first("AnimationNode", "");841if (name == "Animation" || name == "StartState" || name == "EndState") {842continue; // nope843}844int idx = menu->get_item_count();845menu->add_item(vformat(TTR("Add %s"), name), idx);846menu->set_item_metadata(idx, class_name);847}848Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();849850if (clipb.is_valid()) {851menu->add_separator();852menu->add_item(TTR("Paste"), MENU_PASTE);853}854menu->add_separator();855menu->add_item(TTR("Load..."), MENU_LOAD_FILE);856857menu->set_position(state_machine_draw->get_screen_transform().xform(p_position));858menu->popup();859add_node_pos = p_position / EDSCALE + state_machine->get_graph_offset();860}861862bool AnimationNodeStateMachineEditor::_create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path) {863String prev_path;864865LocalVector<StringName> nodes = p_nodesm->get_node_list();866867PopupMenu *nodes_menu = memnew(PopupMenu);868nodes_menu->set_name(p_name);869nodes_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));870p_menu->add_child(nodes_menu);871872bool node_added = false;873for (const StringName &E : nodes) {874if (p_nodesm->can_edit_node(E)) {875Ref<AnimationNodeStateMachine> ansm = p_nodesm->get_node(E);876877String path = String(p_path) + "/" + E;878879if (ansm == state_machine) {880end_menu->add_item(E, nodes_to_connect.size());881nodes_to_connect.push_back(SceneStringName(End));882continue;883}884885if (ansm.is_valid()) {886state_machine_menu->add_item(E, nodes_to_connect.size());887nodes_to_connect.push_back(path);888889if (_create_submenu(nodes_menu, ansm, E, path)) {890nodes_menu->add_submenu_item(E, E);891node_added = true;892}893} else {894nodes_menu->add_item(E, nodes_to_connect.size());895nodes_to_connect.push_back(path);896node_added = true;897}898}899}900901return node_added;902}903904void AnimationNodeStateMachineEditor::_stop_connecting() {905connecting = false;906state_machine_draw->queue_redraw();907}908909void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {910file_loaded = ResourceLoader::load(p_file);911if (file_loaded.is_valid()) {912_add_menu_type(MENU_LOAD_FILE_CONFIRM);913} else {914EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));915}916}917918void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {919String base_name;920Ref<AnimationRootNode> node;921922if (p_index == MENU_LOAD_FILE) {923open_file->clear_filters();924List<String> filters;925ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);926for (const String &E : filters) {927open_file->add_filter("*." + E);928}929open_file->popup_file_dialog();930return;931} else if (p_index == MENU_LOAD_FILE_CONFIRM) {932node = file_loaded;933file_loaded.unref();934} else if (p_index == MENU_PASTE) {935node = EditorSettings::get_singleton()->get_resource_clipboard();936937} else {938String type = menu->get_item_metadata(p_index);939940Object *obj = ClassDB::instantiate(type);941ERR_FAIL_NULL(obj);942AnimationNode *an = Object::cast_to<AnimationNode>(obj);943ERR_FAIL_NULL(an);944945node = Ref<AnimationNode>(an);946base_name = type.replace_first("AnimationNode", "");947}948949if (node.is_null()) {950EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));951return;952}953954if (base_name.is_empty()) {955base_name = node->get_class().replace_first("AnimationNode", "");956}957958int base = 1;959String name = base_name;960while (state_machine->has_node(name)) {961base++;962name = base_name + " " + itos(base);963}964965updating = true;966EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();967undo_redo->create_action(TTR("Add Node and Transition"));968undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);969undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);970connecting_to_node = name;971_add_transition(true);972undo_redo->commit_action();973updating = false;974975state_machine_draw->queue_redraw();976}977978void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {979Ref<AnimationNodeAnimation> anim;980anim.instantiate();981982anim->set_animation(animations_to_add[p_index]);983984String base_name = animations_to_add[p_index].validate_node_name();985int base = 1;986String name = base_name;987while (state_machine->has_node(name)) {988base++;989name = base_name + " " + itos(base);990}991992updating = true;993EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();994undo_redo->create_action(TTR("Add Node and Transition"));995undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);996undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);997connecting_to_node = name;998_add_transition(true);999undo_redo->commit_action();1000updating = false;10011002state_machine_draw->queue_redraw();1003}10041005void AnimationNodeStateMachineEditor::_connect_to(int p_index) {1006connecting_to_node = nodes_to_connect[p_index];1007_add_transition();1008}10091010void AnimationNodeStateMachineEditor::_add_transition(const bool p_nested_action) {1011if (connecting_from != StringName() && connecting_to_node != StringName()) {1012if (connecting_to_node == SceneStringName(Start)) {1013EditorNode::get_singleton()->show_warning(TTR("Cannot transition to \"Start\"!"));1014connecting = false;1015return;1016}10171018if (connecting_from == SceneStringName(End)) {1019EditorNode::get_singleton()->show_warning(TTR("Cannot transition from \"End\"!"));1020connecting = false;1021return;1022}10231024if (state_machine->has_transition(connecting_from, connecting_to_node)) {1025EditorNode::get_singleton()->show_warning(TTR("Transition already exists!"));1026connecting = false;1027return;1028}10291030Ref<AnimationNodeStateMachineTransition> tr;1031tr.instantiate();1032tr->set_advance_mode(auto_advance->is_pressed() ? AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_AUTO : AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_ENABLED);1033tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()));10341035EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1036if (!p_nested_action) {1037updating = true;1038undo_redo->create_action(TTR("Add Transition"));1039}10401041undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr);1042undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node);1043undo_redo->add_do_method(this, "_update_graph");1044undo_redo->add_undo_method(this, "_update_graph");10451046if (!p_nested_action) {1047undo_redo->commit_action();1048updating = false;1049}10501051_select_transition(connecting_from, connecting_to_node);10521053if (selected_transition_index >= 0) {1054if (!state_machine->is_transition_across_group(selected_transition_index)) {1055EditorNode::get_singleton()->push_item(tr.ptr(), "", true);1056} else {1057EditorNode::get_singleton()->push_item(tr.ptr(), "", true);1058EditorNode::get_singleton()->push_item(nullptr, "", true);1059}1060}1061_update_mode();1062}10631064connecting = false;1065}10661067void AnimationNodeStateMachineEditor::_connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, float p_fade_ratio, bool p_auto_advance, bool p_is_across_group, float p_opacity, bool p_endpoint_hovered, bool p_endpoint_hovered_start) {1068Color line_color = p_enabled ? theme_cache.transition_color : theme_cache.transition_disabled_color;1069Color icon_color = p_enabled ? theme_cache.transition_icon_color : theme_cache.transition_icon_disabled_color;1070Color highlight_color = p_enabled ? theme_cache.highlight_color : theme_cache.highlight_disabled_color;10711072line_color.a *= p_opacity;1073icon_color.a *= p_opacity;1074highlight_color.a *= p_opacity;10751076if (p_travel) {1077line_color = highlight_color;1078}10791080// Add gradient on hovered endpoint.1081if (p_endpoint_hovered) {1082// Calculate gradient length based on transition length.1083float transition_length = p_from.distance_to(p_to);1084float gradient_distance = MIN(20.0f, transition_length * 0.2f);10851086Vector2 gradient_start;1087Vector2 gradient_end;1088if (p_endpoint_hovered_start) {1089gradient_start = p_from;1090gradient_end = p_from + (p_to - p_from).normalized() * gradient_distance;1091} else {1092gradient_end = p_to;1093gradient_start = p_to - (p_to - p_from).normalized() * gradient_distance;1094}10951096PackedVector2Array points;1097PackedColorArray colors;10981099points.push_back(gradient_start);1100points.push_back(gradient_end);11011102Color start_color = highlight_color;1103Color end_color = highlight_color;11041105if (p_endpoint_hovered_start) {1106end_color.a = 0.0f;1107} else {1108start_color.a = 0.0f;1109}11101111if (p_selected) {1112start_color = start_color.lightened(0.2f);1113end_color = end_color.lightened(0.2f);1114start_color.a *= 1.5f;1115end_color.a *= 1.5f;1116}11171118colors.push_back(start_color);1119colors.push_back(end_color);11201121float line_width = p_selected ? 10.0f : 8.0f;1122state_machine_draw->draw_polyline_colors(points, colors, line_width, true);1123}11241125if (p_selected) {1126state_machine_draw->draw_line(p_from, p_to, highlight_color, 6, true);1127}1128state_machine_draw->draw_line(p_from, p_to, line_color, 2, true);11291130if (p_fade_ratio > 0.0) {1131Color fade_line_color = highlight_color;1132fade_line_color.set_hsv(1.0, fade_line_color.get_s(), fade_line_color.get_v());1133fade_line_color.a *= p_opacity;1134state_machine_draw->draw_line(p_from, p_from.lerp(p_to, p_fade_ratio), fade_line_color, 2);1135}11361137const int ICON_COUNT = std_size(theme_cache.transition_icons);1138int icon_index = p_mode + (p_auto_advance ? ICON_COUNT / 2 : 0);1139ERR_FAIL_COND(icon_index >= ICON_COUNT);1140Ref<Texture2D> icon = theme_cache.transition_icons[icon_index];11411142Transform2D xf;1143xf.columns[0] = (p_to - p_from).normalized();1144xf.columns[1] = xf.columns[0].orthogonal();1145xf.columns[2] = (p_from + p_to) * 0.5 - xf.columns[1] * icon->get_height() * 0.5 - xf.columns[0] * icon->get_height() * 0.5;11461147state_machine_draw->draw_set_transform_matrix(xf);1148if (!p_is_across_group) {1149state_machine_draw->draw_texture(icon, Vector2(), icon_color);1150}1151state_machine_draw->draw_set_transform_matrix(Transform2D());1152}11531154void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect) {1155if (p_to == r_from) {1156return;1157}11581159//this could be optimized...1160Vector2 n = (p_to - r_from).normalized();1161while (p_rect.has_point(r_from)) {1162r_from += n;1163}1164}11651166void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect) {1167if (r_to == p_from) {1168return;1169}11701171//this could be optimized...1172Vector2 n = (r_to - p_from).normalized();1173while (p_rect.has_point(r_to)) {1174r_to -= n;1175}1176}11771178Ref<StyleBox> AnimationNodeStateMachineEditor::_adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity) {1179Ref<StyleBox> style = p_style->duplicate();1180if (style->is_class("StyleBoxFlat")) {1181Ref<StyleBoxFlat> flat_style = style;1182Color bg_color = flat_style->get_bg_color();1183Color border_color = flat_style->get_border_color();1184Color shadow_color = flat_style->get_shadow_color();11851186bg_color.a *= p_opacity;1187border_color.a *= p_opacity;1188shadow_color.a *= p_opacity;11891190flat_style->set_bg_color(bg_color);1191flat_style->set_border_color(border_color);1192flat_style->set_shadow_color(shadow_color);1193}1194return style;1195}11961197void AnimationNodeStateMachineEditor::_state_machine_draw() {1198AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1199if (!tree) {1200return;1201}12021203bool playing = false;1204StringName current;1205StringName blend_from;1206Vector<StringName> travel_path;12071208Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1209if (playback.is_valid()) {1210playing = playback->is_playing();1211current = playback->get_current_node();1212blend_from = playback->get_fading_from_node();1213travel_path = playback->get_travel_path();1214}12151216if (state_machine_draw->has_focus(true)) {1217state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), theme_cache.focus_color, false);1218}1219int sep = 3 * EDSCALE;12201221LocalVector<StringName> nodes = state_machine->get_node_list();12221223node_rects.clear();1224Rect2 scroll_range;12251226//snap lines1227if (dragging_selected) {1228Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE;1229if (snap_x != StringName()) {1230Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1231state_machine_draw->draw_line(from, to, theme_cache.guideline_color, 2);1232}1233if (snap_y != StringName()) {1234Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1235state_machine_draw->draw_line(from, to, theme_cache.guideline_color, 2);1236}1237}12381239//pre pass nodes so we know the rectangles1240for (const StringName &E : nodes) {1241String name = E;1242int name_string_size = theme_cache.node_title_font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size).width;12431244Ref<AnimationNode> anode = state_machine->get_node(name);1245bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);1246bool is_selected = selected_nodes.has(name);12471248Size2 s = (is_selected ? theme_cache.node_frame_selected : theme_cache.node_frame)->get_minimum_size();1249s.width += name_string_size;1250s.height += MAX(theme_cache.node_title_font->get_height(theme_cache.node_title_font_size), theme_cache.play_node->get_height());1251s.width += sep + theme_cache.play_node->get_width();12521253if (needs_editor) {1254s.width += sep + theme_cache.edit_node->get_width();1255}12561257Vector2 offset;1258offset += state_machine->get_node_position(E) * EDSCALE;12591260if (selected_nodes.has(E) && dragging_selected) {1261offset += drag_ofs;1262}12631264offset -= s / 2;1265offset = offset.floor();12661267//prepre rect12681269NodeRect nr;1270nr.node = Rect2(offset, s);1271nr.node_name = E;12721273scroll_range = scroll_range.merge(nr.node); //merge with range12741275//now scroll it to draw1276nr.node.position -= state_machine->get_graph_offset() * EDSCALE;12771278node_rects.push_back(nr);1279}12801281transition_lines.clear();12821283//draw connecting line for potential new transition1284if (connecting) {1285Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1286Vector2 to;1287if (connecting_to_node != StringName()) {1288to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1289} else {1290to = connecting_to;1291}12921293for (int i = 0; i < node_rects.size(); i++) {1294if (node_rects[i].node_name == connecting_from) {1295_clip_src_line_to_rect(from, to, node_rects[i].node);1296}1297if (node_rects[i].node_name == connecting_to_node) {1298_clip_dst_line_to_rect(from, to, node_rects[i].node);1299}1300}13011302_connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()), true, false, false, 0.0, false, false);1303}13041305// TransitionImmediateBig1306float tr_bidi_offset = int(theme_cache.transition_icons[0]->get_height() * 0.8);13071308//draw transition lines1309for (int i = 0; i < state_machine->get_transition_count(); i++) {1310TransitionLine tl;1311tl.transition_index = i;13121313tl.from_node = state_machine->get_transition_from(i);1314Vector2 ofs_from = (dragging_selected && selected_nodes.has(tl.from_node)) ? drag_ofs : Vector2();1315tl.from = (state_machine->get_node_position(tl.from_node) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE;13161317tl.to_node = state_machine->get_transition_to(i);1318Vector2 ofs_to = (dragging_selected && selected_nodes.has(tl.to_node)) ? drag_ofs : Vector2();1319tl.to = (state_machine->get_node_position(tl.to_node) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE;13201321Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);1322tl.disabled = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED);1323tl.auto_advance = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);1324tl.advance_condition_name = tr->get_advance_condition_name();1325tl.advance_condition_state = false;1326tl.mode = tr->get_switch_mode();1327tl.width = tr_bidi_offset;1328tl.travel = false;1329tl.fade_ratio = 0.0;1330tl.hidden = false;1331tl.is_across_group = state_machine->is_transition_across_group(i);13321333if (state_machine->has_transition(tl.to_node, tl.from_node)) { //offset if same exists1334Vector2 offset = -(tl.from - tl.to).normalized().orthogonal() * tr_bidi_offset;1335tl.from += offset;1336tl.to += offset;1337}13381339for (int j = 0; j < node_rects.size(); j++) {1340if (node_rects[j].node_name == tl.from_node) {1341_clip_src_line_to_rect(tl.from, tl.to, node_rects[j].node);1342}1343if (node_rects[j].node_name == tl.to_node) {1344_clip_dst_line_to_rect(tl.from, tl.to, node_rects[j].node);1345}1346}13471348tl.selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node;13491350if (blend_from == tl.from_node && current == tl.to_node) {1351tl.travel = true;1352tl.fade_ratio = MIN(1.0, fading_pos / fading_time);1353}13541355if (travel_path.size()) {1356if (current == tl.from_node && travel_path[0] == tl.to_node) {1357tl.travel = true;1358} else {1359for (int j = 0; j < travel_path.size() - 1; j++) {1360if (travel_path[j] == tl.from_node && travel_path[j + 1] == tl.to_node) {1361tl.travel = true;1362break;1363}1364}1365}1366}13671368StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name);1369if (tl.advance_condition_name != StringName() && bool(tree->get(fullpath))) {1370tl.advance_condition_state = true;1371tl.auto_advance = true;1372}13731374// check if already have this transition1375for (int j = 0; j < transition_lines.size(); j++) {1376if (transition_lines[j].from_node == tl.from_node && transition_lines[j].to_node == tl.to_node) {1377tl.hidden = true;1378transition_lines.write[j].disabled = transition_lines[j].disabled && tl.disabled;1379}1380}1381transition_lines.push_back(tl);1382}13831384for (int i = 0; i < transition_lines.size(); i++) {1385TransitionLine tl = transition_lines[i];13861387if (reconnecting && i == reconnecting_transition_index) {1388Vector2 transition_drag_from;1389Vector2 transition_drag_to;13901391if (reconnecting_transition_start) {1392transition_drag_from = reconnecting_transition_pos;1393transition_drag_to = (state_machine->get_node_position(tl.to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1394} else {1395transition_drag_from = (state_machine->get_node_position(tl.from_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1396transition_drag_to = reconnecting_transition_pos;1397}13981399// Check if we're attempting a self-connection.1400StringName connected_node = reconnecting_transition_start ? tl.to_node : tl.from_node;14011402// Don't draw the line if attempting self-connection.1403if (reconnecting_transition_target != connected_node) {1404if (reconnecting_from_node_rect_index >= 0) {1405if (reconnecting_transition_start) {1406_clip_dst_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_from_node_rect_index].node);1407} else {1408_clip_src_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_from_node_rect_index].node);1409}1410}14111412if (reconnecting_to_node_rect_index >= 0) {1413if (reconnecting_transition_start) {1414_clip_src_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_to_node_rect_index].node);1415} else {1416_clip_dst_line_to_rect(transition_drag_from, transition_drag_to, node_rects[reconnecting_to_node_rect_index].node);1417}1418}14191420_connection_draw(transition_drag_from, transition_drag_to, tl.mode, !tl.disabled, false, false, 0.0f, tl.auto_advance, tl.is_across_group, 0.8f, false, false);1421}14221423} else if (!tl.hidden) {1424float opacity = 0.2; // Default to reduced opacity.14251426if (selected_transition_from != StringName() && selected_transition_to != StringName()) {1427// A transition is selected.1428if ((tl.from_node == selected_transition_from && tl.to_node == selected_transition_to) || (tl.from_node == selected_transition_to && tl.to_node == selected_transition_from)) {1429opacity = 1.0; // Full opacity for the selected transition pair.1430}1431} else if (!connected_nodes.is_empty()) {1432// A node is selected.1433if (connected_nodes.has(selected_node)) {1434// Only keep full opacity for transitions directly connected to the selected node.1435if (tl.from_node == selected_node || tl.to_node == selected_node) {1436opacity = 1.0;1437}1438} else {1439// If no node is selected, all transitions are at full opacity.1440opacity = 1.0;1441}1442} else {1443// If nothing is selected, all transitions are at full opacity.1444opacity = 1.0;1445}14461447bool is_hovered = (hovered_transition_index == i);1448_connection_draw(tl.from, tl.to, tl.mode, !tl.disabled, tl.selected, tl.travel, tl.fade_ratio, tl.auto_advance, tl.is_across_group, opacity, is_hovered, hovered_transition_start);1449}1450}14511452//draw actual nodes1453for (int i = 0; i < node_rects.size(); i++) {1454String name = node_rects[i].node_name;1455int name_string_size = theme_cache.node_title_font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size).width;14561457Ref<AnimationNode> anode = state_machine->get_node(name);1458bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);1459bool is_selected = selected_nodes.has(name);14601461NodeRect &nr = node_rects.write[i];1462Vector2 offset = nr.node.position;1463int h = nr.node.size.height;14641465float opacity = 1.0;1466if (selected_transition_from != StringName() && selected_transition_to != StringName()) {1467// A transition is selected.1468if (name != selected_transition_from && name != selected_transition_to) {1469opacity = 0.2;1470}1471} else if (!connected_nodes.is_empty() && !connected_nodes.has(name)) {1472// A node is selected.1473opacity = 0.2;1474}14751476Ref<StyleBox> original_style = is_selected ? theme_cache.node_frame_selected : theme_cache.node_frame;1477Ref<StyleBox> node_style = _adjust_stylebox_opacity(original_style, opacity);14781479state_machine_draw->draw_style_box(node_style, nr.node);14801481if (!is_selected && SceneStringName(Start) == name) {1482Ref<StyleBox> start_style = _adjust_stylebox_opacity(theme_cache.node_frame_start, opacity);1483state_machine_draw->draw_style_box(start_style, nr.node);1484}1485if (!is_selected && SceneStringName(End) == name) {1486Ref<StyleBox> end_style = _adjust_stylebox_opacity(theme_cache.node_frame_end, opacity);1487state_machine_draw->draw_style_box(end_style, nr.node);1488}1489if (playing && (blend_from == name || current == name || travel_path.has(name))) {1490Ref<StyleBox> playing_style = _adjust_stylebox_opacity(theme_cache.node_frame_playing, opacity);1491state_machine_draw->draw_style_box(playing_style, nr.node);1492}14931494offset.x += original_style->get_offset().x;14951496nr.play.position = offset + Vector2(0, (h - theme_cache.play_node->get_height()) / 2).floor();1497nr.play.size = theme_cache.play_node->get_size();14981499Color color_mod = Color(1, 1, 1, opacity);1500if (hovered_node_name == name && hovered_node_area == HOVER_NODE_PLAY) {1501state_machine_draw->draw_texture(theme_cache.play_node, nr.play.position, theme_cache.highlight_color * color_mod);1502} else {1503state_machine_draw->draw_texture(theme_cache.play_node, nr.play.position, color_mod);1504}15051506offset.x += sep + theme_cache.play_node->get_width();15071508nr.name.position = offset + Vector2(0, (h - theme_cache.node_title_font->get_height(theme_cache.node_title_font_size)) / 2).floor();1509nr.name.size = Vector2(name_string_size, theme_cache.node_title_font->get_height(theme_cache.node_title_font_size));15101511Color font_color = theme_cache.node_title_font_color;1512font_color.a *= opacity;1513state_machine_draw->draw_string(theme_cache.node_title_font, nr.name.position + Vector2(0, theme_cache.node_title_font->get_ascent(theme_cache.node_title_font_size)), name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size, font_color);1514offset.x += name_string_size + sep;15151516nr.can_edit = needs_editor;1517if (needs_editor) {1518nr.edit.position = offset + Vector2(0, (h - theme_cache.edit_node->get_height()) / 2).floor();1519nr.edit.size = theme_cache.edit_node->get_size();15201521if (hovered_node_name == name && hovered_node_area == HOVER_NODE_EDIT) {1522state_machine_draw->draw_texture(theme_cache.edit_node, nr.edit.position, theme_cache.highlight_color * color_mod);1523} else {1524state_machine_draw->draw_texture(theme_cache.edit_node, nr.edit.position, color_mod);1525}1526}1527}15281529//draw box select1530if (box_selecting) {1531state_machine_draw->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));1532}15331534scroll_range.position -= state_machine_draw->get_size();1535scroll_range.size += state_machine_draw->get_size() * 2.0;15361537//adjust scrollbars1538updating = true;1539h_scroll->set_min(scroll_range.position.x);1540h_scroll->set_max(scroll_range.position.x + scroll_range.size.x);1541h_scroll->set_page(state_machine_draw->get_size().x);1542h_scroll->set_value(state_machine->get_graph_offset().x);15431544v_scroll->set_min(scroll_range.position.y);1545v_scroll->set_max(scroll_range.position.y + scroll_range.size.y);1546v_scroll->set_page(state_machine_draw->get_size().y);1547v_scroll->set_value(state_machine->get_graph_offset().y);1548updating = false;15491550state_machine_play_pos->queue_redraw();1551}15521553void AnimationNodeStateMachineEditor::_update_connected_nodes(const StringName &p_node) {1554connected_nodes.clear();1555if (p_node != StringName()) {1556connected_nodes.insert(p_node);15571558Vector<StringName> nodes_to = state_machine->get_nodes_with_transitions_to(p_node);1559for (const StringName &node_to : nodes_to) {1560connected_nodes.insert(node_to);1561}15621563Vector<StringName> nodes_from = state_machine->get_nodes_with_transitions_from(p_node);1564for (const StringName &node_from : nodes_from) {1565connected_nodes.insert(node_from);1566}1567}1568}15691570void AnimationNodeStateMachineEditor::_state_machine_pos_draw_individual(const String &p_name, float p_ratio) {1571AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1572if (!tree) {1573return;1574}15751576Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1577if (playback.is_null() || !playback->is_playing()) {1578return;1579}15801581if (p_name == SceneStringName(Start) || p_name == SceneStringName(End) || p_name.is_empty()) {1582return;1583}15841585int idx = -1;1586for (int i = 0; i < node_rects.size(); i++) {1587if (node_rects[i].node_name == p_name) {1588idx = i;1589break;1590}1591}15921593if (idx == -1) {1594return;1595}15961597const NodeRect &nr = node_rects[idx];1598if (nr.can_edit) {1599return; // It is not AnimationNodeAnimation.1600}16011602Vector2 from;1603from.x = nr.play.position.x;1604from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5;16051606Vector2 to;1607if (nr.edit.size.x) {1608to.x = nr.edit.position.x + nr.edit.size.x;1609} else {1610to.x = nr.name.position.x + nr.name.size.x;1611}1612to.y = from.y;16131614state_machine_play_pos->draw_line(from, to, theme_cache.playback_background_color, 2);1615to = from.lerp(to, p_ratio);1616state_machine_play_pos->draw_line(from, to, theme_cache.playback_color, 2);1617}16181619void AnimationNodeStateMachineEditor::_state_machine_pos_draw_all() {1620AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1621if (!tree) {1622return;1623}16241625Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1626if (playback.is_null() || !playback->is_playing()) {1627return;1628}16291630{1631float len = MAX(0.0001, current_length);1632float pos = CLAMP(current_play_pos, 0, len);1633float c = pos / len;1634_state_machine_pos_draw_individual(playback->get_current_node(), c);1635}16361637{1638float len = MAX(0.0001, fade_from_length);1639float pos = CLAMP(fade_from_current_play_pos, 0, len);1640float c = pos / len;1641_state_machine_pos_draw_individual(playback->get_fading_from_node(), c);1642}1643}16441645void AnimationNodeStateMachineEditor::_update_graph() {1646if (updating) {1647return;1648}16491650updating = true;16511652state_machine_draw->queue_redraw();16531654updating = false;1655}16561657void AnimationNodeStateMachineEditor::_notification(int p_what) {1658switch (p_what) {1659case NOTIFICATION_THEME_CHANGED: {1660panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);1661error_panel->add_theme_style_override(SceneStringName(panel), theme_cache.error_panel_style);1662error_label->add_theme_color_override(SceneStringName(font_color), theme_cache.error_color);16631664tool_select->set_button_icon(theme_cache.tool_icon_select);1665tool_create->set_button_icon(theme_cache.tool_icon_create);1666tool_connect->set_button_icon(theme_cache.tool_icon_connect);16671668switch_mode->clear();1669switch_mode->add_icon_item(theme_cache.transition_icon_immediate, TTR("Immediate"));1670switch_mode->add_icon_item(theme_cache.transition_icon_sync, TTR("Sync"));1671switch_mode->add_icon_item(theme_cache.transition_icon_end, TTR("At End"));16721673auto_advance->set_button_icon(theme_cache.play_icon_auto);16741675tool_erase->set_button_icon(theme_cache.tool_icon_erase);16761677play_mode->clear();1678play_mode->add_icon_item(theme_cache.play_icon_travel, TTR("Travel"));1679play_mode->add_icon_item(theme_cache.play_icon_start, TTR("Immediate"));1680} break;16811682case NOTIFICATION_PROCESS: {1683AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1684if (!tree) {1685return;1686}16871688String error;16891690Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");16911692if (error_time > 0) {1693error = error_text;1694error_time -= get_process_delta_time();1695} else {1696error = tree->get_editor_error_message();1697}16981699if (error.is_empty() && playback.is_null()) {1700error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1701}17021703if (error != error_label->get_text()) {1704error_label->set_text(error);1705if (!error.is_empty()) {1706error_panel->show();1707} else {1708error_panel->hide();1709}1710}17111712for (int i = 0; i < transition_lines.size(); i++) {1713int tidx = -1;1714for (int j = 0; j < state_machine->get_transition_count(); j++) {1715if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {1716tidx = j;1717break;1718}1719}17201721if (tidx == -1) { //missing transition, should redraw1722state_machine_draw->queue_redraw();1723break;1724}17251726if (transition_lines[i].disabled != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED)) {1727state_machine_draw->queue_redraw();1728break;1729}17301731if (transition_lines[i].auto_advance != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO)) {1732state_machine_draw->queue_redraw();1733break;1734}17351736if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {1737state_machine_draw->queue_redraw();1738break;1739}17401741if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {1742state_machine_draw->queue_redraw();1743break;1744}17451746bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name)));17471748if (transition_lines[i].advance_condition_state != acstate) {1749state_machine_draw->queue_redraw();1750break;1751}1752}17531754bool same_travel_path = true;1755Vector<StringName> tp;1756bool is_playing = false;1757StringName current_node;1758StringName fading_from_node;17591760current_play_pos = 0;1761current_length = 0;17621763fade_from_current_play_pos = 0;1764fade_from_length = 0;17651766fading_time = 0;1767fading_pos = 0;17681769if (playback.is_valid()) {1770tp = playback->get_travel_path();1771is_playing = playback->is_playing();1772current_node = playback->get_current_node();1773fading_from_node = playback->get_fading_from_node();1774current_play_pos = playback->get_current_play_pos();1775current_length = playback->get_current_length();1776fade_from_current_play_pos = playback->get_fading_from_play_pos();1777fade_from_length = playback->get_fading_from_length();1778fading_time = playback->get_fading_time();1779fading_pos = playback->get_fading_pos();1780}17811782{1783if (last_travel_path.size() != tp.size()) {1784same_travel_path = false;1785} else {1786for (int i = 0; i < last_travel_path.size(); i++) {1787if (last_travel_path[i] != tp[i]) {1788same_travel_path = false;1789break;1790}1791}1792}1793}17941795//redraw if travel state changed1796if (!same_travel_path ||1797last_active != is_playing ||1798last_current_node != current_node ||1799last_fading_from_node != fading_from_node ||1800last_fading_time != fading_time ||1801last_fading_pos != fading_pos) {1802state_machine_draw->queue_redraw();1803last_travel_path = tp;1804last_current_node = current_node;1805last_active = is_playing;1806last_fading_from_node = fading_from_node;1807last_fading_time = fading_time;1808last_fading_pos = fading_pos;1809state_machine_play_pos->queue_redraw();1810}18111812{1813if (current_node != StringName() && state_machine->has_node(current_node)) {1814String next = current_node;1815Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next);1816Ref<AnimationNodeStateMachinePlayback> current_node_playback;18171818while (anodesm.is_valid()) {1819current_node_playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback");1820StringName cnode = current_node_playback->get_current_node();1821next += "/" + cnode;1822if (!anodesm->has_node(cnode)) {1823break;1824}1825anodesm = anodesm->get_node(cnode);1826}18271828// when current_node is a state machine, use playback of current_node to set play_pos1829if (current_node_playback.is_valid()) {1830current_play_pos = current_node_playback->get_current_play_pos();1831current_length = current_node_playback->get_current_length();1832}1833}1834}18351836if (last_play_pos != current_play_pos || fade_from_last_play_pos != fade_from_current_play_pos) {1837last_play_pos = current_play_pos;1838fade_from_last_play_pos = fade_from_current_play_pos;1839state_machine_play_pos->queue_redraw();1840}1841} break;18421843case NOTIFICATION_VISIBILITY_CHANGED: {1844hovered_node_name = StringName();1845hovered_node_area = HOVER_NODE_NONE;1846set_process(is_visible_in_tree());1847} break;1848}1849}18501851void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {1852AnimationTreeEditor::get_singleton()->enter_editor(p_name);1853}18541855void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {1856const String &new_name = p_text;18571858ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/'));18591860if (new_name == prev_name) {1861return; // Nothing to do.1862}18631864const String &base_name = new_name;1865int base = 1;1866String name = base_name;1867while (state_machine->has_node(name)) {1868if (name == prev_name) {1869name_edit_popup->hide(); // The old name wins, the name doesn't change, just hide the popup.1870return;1871}1872base++;1873name = base_name + " " + itos(base);1874}18751876updating = true;1877EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1878undo_redo->create_action(TTR("Node Renamed"));1879undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);1880undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);1881undo_redo->add_do_method(this, "_update_graph");1882undo_redo->add_undo_method(this, "_update_graph");1883undo_redo->commit_action();1884name_edit_popup->hide();1885updating = false;18861887selected_nodes.clear();1888connected_nodes.clear();1889state_machine_draw->queue_redraw();1890}18911892void AnimationNodeStateMachineEditor::_name_edited_focus_out() {1893if (updating) {1894return;1895}18961897_name_edited(name_edit->get_text());1898}18991900void AnimationNodeStateMachineEditor::_scroll_changed(double) {1901if (updating) {1902return;1903}19041905state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));1906state_machine_draw->queue_redraw();1907}19081909void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action) {1910if (!selected_nodes.is_empty()) {1911if (!p_nested_action) {1912updating = true;1913}1914EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1915undo_redo->create_action(TTR("Node Removed"));19161917for (int i = 0; i < node_rects.size(); i++) {1918if (node_rects[i].node_name == SceneStringName(Start) || node_rects[i].node_name == SceneStringName(End)) {1919continue;1920}19211922if (!selected_nodes.has(node_rects[i].node_name)) {1923continue;1924}19251926undo_redo->add_do_method(state_machine.ptr(), "remove_node", node_rects[i].node_name);1927undo_redo->add_undo_method(state_machine.ptr(), "add_node", node_rects[i].node_name,1928state_machine->get_node(node_rects[i].node_name),1929state_machine->get_node_position(node_rects[i].node_name));19301931for (int j = 0; j < state_machine->get_transition_count(); j++) {1932String from = state_machine->get_transition_from(j);1933String to = state_machine->get_transition_to(j);1934if (from == node_rects[i].node_name || to == node_rects[i].node_name) {1935undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(j));1936}1937}1938}19391940undo_redo->add_do_method(this, "_update_graph");1941undo_redo->add_undo_method(this, "_update_graph");1942undo_redo->commit_action();19431944if (!p_nested_action) {1945updating = false;1946}19471948connected_nodes.clear();1949selected_nodes.clear();1950selected_node = StringName();19511952// Return selection to host StateMachine node.1953EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true);1954}19551956if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {1957Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));1958if (!p_nested_action) {1959updating = true;1960}1961EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1962undo_redo->create_action(TTR("Transition Removed"));1963undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);1964undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);1965undo_redo->add_do_method(this, "_update_graph");1966undo_redo->add_undo_method(this, "_update_graph");1967undo_redo->commit_action();1968if (!p_nested_action) {1969updating = false;1970}1971selected_transition_from = StringName();1972selected_transition_to = StringName();1973selected_transition_index = -1;19741975// Return selection to host StateMachine node.1976EditorNode::get_singleton()->push_item(state_machine.ptr(), "", true);1977}19781979_update_mode();1980state_machine_draw->queue_redraw();1981}19821983void AnimationNodeStateMachineEditor::_update_mode() {1984if (tool_select->is_pressed()) {1985selection_tools_hb->show();1986bool nothing_selected = selected_nodes.is_empty() && selected_transition_from == StringName() && selected_transition_to == StringName();1987bool start_end_selected = selected_nodes.size() == 1 && (*selected_nodes.begin() == SceneStringName(Start) || *selected_nodes.begin() == SceneStringName(End));1988tool_erase->set_disabled(nothing_selected || start_end_selected || read_only);1989} else {1990selection_tools_hb->hide();1991}19921993if (read_only) {1994tool_create->set_pressed(false);1995tool_connect->set_pressed(false);1996}19971998if (tool_connect->is_pressed()) {1999transition_tools_hb->show();2000} else {2001transition_tools_hb->hide();2002}2003}20042005void AnimationNodeStateMachineEditor::_bind_methods() {2006ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);2007ClassDB::bind_method("_select_transition", &AnimationNodeStateMachineEditor::_select_transition);20082009BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, panel_style, "panel", "GraphStateMachine");2010BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, error_panel_style, "error_panel", "GraphStateMachine");2011BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, error_color, "error_color", "GraphStateMachine");20122013BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_select, "ToolSelect", "EditorIcons");2014BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_create, "ToolAddNode", "EditorIcons");2015BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_connect, "ToolConnect", "EditorIcons");2016BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_erase, "Remove", "EditorIcons");20172018BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_immediate, "TransitionImmediate", "EditorIcons");2019BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_sync, "TransitionSync", "EditorIcons");2020BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_end, "TransitionEnd", "EditorIcons");20212022BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_start, "Play", "EditorIcons");2023BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_travel, "PlayTravel", "EditorIcons");2024BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_auto, "AutoPlay", "EditorIcons");20252026BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, animation_icon, "Animation", "EditorIcons");20272028BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame, "node_frame", "GraphStateMachine");2029BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_selected, "node_frame_selected", "GraphStateMachine");2030BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_playing, "node_frame_playing", "GraphStateMachine");2031BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_start, "node_frame_start", "GraphStateMachine");2032BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_end, "node_frame_end", "GraphStateMachine");20332034BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_FONT, AnimationNodeStateMachineEditor, node_title_font, "node_title_font", "GraphStateMachine");2035BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_FONT_SIZE, AnimationNodeStateMachineEditor, node_title_font_size, "node_title_font_size", "GraphStateMachine");2036BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, node_title_font_color, "node_title_font_color", "GraphStateMachine");20372038BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_node, "Play", "EditorIcons");2039BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, edit_node, "Edit", "EditorIcons");20402041BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_color, "transition_color", "GraphStateMachine");2042BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_disabled_color, "transition_disabled_color", "GraphStateMachine");2043BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_icon_color, "transition_icon_color", "GraphStateMachine");2044BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_icon_disabled_color, "transition_icon_disabled_color", "GraphStateMachine");2045BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, highlight_color, "highlight_color", "GraphStateMachine");2046BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, highlight_disabled_color, "highlight_disabled_color", "GraphStateMachine");2047BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, focus_color, "focus_color", "GraphStateMachine");2048BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, guideline_color, "guideline_color", "GraphStateMachine");20492050BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[0], "TransitionImmediateBig", "EditorIcons");2051BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[1], "TransitionSyncBig", "EditorIcons");2052BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[2], "TransitionEndBig", "EditorIcons");2053BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[3], "TransitionImmediateAutoBig", "EditorIcons");2054BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[4], "TransitionSyncAutoBig", "EditorIcons");2055BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[5], "TransitionEndAutoBig", "EditorIcons");20562057BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, playback_color, "playback_color", "GraphStateMachine");2058BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, playback_background_color, "playback_background_color", "GraphStateMachine");2059}20602061AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr;20622063AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {2064singleton = this;20652066HBoxContainer *top_hb = memnew(HBoxContainer);2067add_child(top_hb);20682069Ref<ButtonGroup> bg;2070bg.instantiate();20712072tool_select = memnew(Button);2073tool_select->set_theme_type_variation(SceneStringName(FlatButton));2074top_hb->add_child(tool_select);2075tool_select->set_toggle_mode(true);2076tool_select->set_button_group(bg);2077tool_select->set_pressed(true);2078tool_select->set_tooltip_text(TTR("Select and move nodes.\nRMB: Add node at position clicked.\nShift+LMB+Drag: Connects the selected node with another node or creates a new node if you select an area without nodes."));2079tool_select->set_accessibility_name(TTRC("Select and move nodes."));2080tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);20812082tool_create = memnew(Button);2083tool_create->set_theme_type_variation(SceneStringName(FlatButton));2084top_hb->add_child(tool_create);2085tool_create->set_toggle_mode(true);2086tool_create->set_button_group(bg);2087tool_create->set_tooltip_text(TTR("Create new nodes."));2088tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);20892090tool_connect = memnew(Button);2091tool_connect->set_theme_type_variation(SceneStringName(FlatButton));2092top_hb->add_child(tool_connect);2093tool_connect->set_toggle_mode(true);2094tool_connect->set_button_group(bg);2095tool_connect->set_tooltip_text(TTR("Connect nodes."));2096tool_connect->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);20972098// Context-sensitive selection tools:2099selection_tools_hb = memnew(HBoxContainer);2100top_hb->add_child(selection_tools_hb);2101selection_tools_hb->add_child(memnew(VSeparator));21022103tool_erase = memnew(Button);2104tool_erase->set_theme_type_variation(SceneStringName(FlatButton));2105tool_erase->set_tooltip_text(TTR("Remove selected node or transition."));2106tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected).bind(false));2107tool_erase->set_disabled(true);2108selection_tools_hb->add_child(tool_erase);21092110transition_tools_hb = memnew(HBoxContainer);2111top_hb->add_child(transition_tools_hb);2112transition_tools_hb->add_child(memnew(VSeparator));21132114transition_tools_hb->add_child(memnew(Label(TTR("Transition:"))));2115switch_mode = memnew(OptionButton);2116transition_tools_hb->add_child(switch_mode);21172118auto_advance = memnew(Button);2119auto_advance->set_theme_type_variation(SceneStringName(FlatButton));2120auto_advance->set_tooltip_text(TTR("New Transitions Should Auto Advance"));2121auto_advance->set_toggle_mode(true);2122auto_advance->set_pressed(true);2123transition_tools_hb->add_child(auto_advance);21242125//21262127top_hb->add_spacer();21282129top_hb->add_child(memnew(Label(TTR("Play Mode:"))));2130play_mode = memnew(OptionButton);2131top_hb->add_child(play_mode);21322133panel = memnew(PanelContainer);2134panel->set_clip_contents(true);2135panel->set_mouse_filter(Control::MOUSE_FILTER_PASS);2136add_child(panel);2137panel->set_v_size_flags(SIZE_EXPAND_FILL);21382139state_machine_draw = memnew(Control);2140panel->add_child(state_machine_draw);2141state_machine_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input));2142state_machine_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw));2143state_machine_draw->set_focus_mode(FOCUS_ALL);2144state_machine_draw->set_mouse_filter(Control::MOUSE_FILTER_PASS);21452146state_machine_play_pos = memnew(Control);2147state_machine_draw->add_child(state_machine_play_pos);2148state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent2149state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_FULL_RECT);2150state_machine_play_pos->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw_all));21512152v_scroll = memnew(VScrollBar);2153state_machine_draw->add_child(v_scroll);2154v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);2155v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));21562157h_scroll = memnew(HScrollBar);2158state_machine_draw->add_child(h_scroll);2159h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);2160h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE);2161h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));21622163error_panel = memnew(PanelContainer);2164add_child(error_panel);2165error_label = memnew(Label);2166error_label->set_focus_mode(FOCUS_ACCESSIBILITY);2167error_panel->add_child(error_label);2168error_panel->hide();21692170set_custom_minimum_size(Size2(0, 300 * EDSCALE));21712172menu = memnew(PopupMenu);2173add_child(menu);2174menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type));2175menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));21762177animations_menu = memnew(PopupMenu);2178animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);2179menu->add_child(animations_menu);2180animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));21812182connect_menu = memnew(PopupMenu);2183add_child(connect_menu);2184connect_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));2185connect_menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));21862187state_machine_menu = memnew(PopupMenu);2188state_machine_menu->set_name("state_machines");2189state_machine_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));2190connect_menu->add_child(state_machine_menu);21912192end_menu = memnew(PopupMenu);2193end_menu->set_name("end_nodes");2194end_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));2195connect_menu->add_child(end_menu);21962197name_edit_popup = memnew(Popup);2198add_child(name_edit_popup);2199name_edit = memnew(LineEdit);2200name_edit_popup->add_child(name_edit);2201name_edit->set_anchors_and_offsets_preset(PRESET_FULL_RECT);2202name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited));2203name_edit->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out));22042205open_file = memnew(EditorFileDialog);2206add_child(open_file);2207open_file->set_title(TTR("Open Animation Node"));2208open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);2209open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened));2210}22112212void EditorAnimationMultiTransitionEdit::add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition) {2213Transition tr;2214tr.from = p_from;2215tr.to = p_to;2216tr.transition = p_transition;2217transitions.push_back(tr);2218}22192220bool EditorAnimationMultiTransitionEdit::_set(const StringName &p_name, const Variant &p_property) {2221int index = String(p_name).get_slicec('/', 0).to_int();2222StringName prop = String(p_name).get_slicec('/', 1);22232224bool found;2225transitions.write[index].transition->set(prop, p_property, &found);2226if (found) {2227return true;2228}22292230return false;2231}22322233bool EditorAnimationMultiTransitionEdit::_get(const StringName &p_name, Variant &r_property) const {2234int index = String(p_name).get_slicec('/', 0).to_int();2235StringName prop = String(p_name).get_slicec('/', 1);22362237if (prop == "transition_path") {2238r_property = String(transitions[index].from) + U" → " + transitions[index].to;2239return true;2240}22412242bool found;2243r_property = transitions[index].transition->get(prop, &found);2244if (found) {2245return true;2246}22472248return false;2249}22502251void EditorAnimationMultiTransitionEdit::_get_property_list(List<PropertyInfo> *p_list) const {2252for (int i = 0; i < transitions.size(); i++) {2253List<PropertyInfo> plist;2254transitions[i].transition->get_property_list(&plist, true);22552256PropertyInfo prop_transition_path;2257prop_transition_path.type = Variant::STRING;2258prop_transition_path.name = itos(i) + "/" + "transition_path";2259p_list->push_back(prop_transition_path);22602261for (const PropertyInfo &pi : plist) {2262if (pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene") {2263continue;2264}22652266if (pi.usage != PROPERTY_USAGE_DEFAULT) {2267continue;2268}22692270PropertyInfo prop = pi;2271prop.name = itos(i) + "/" + prop.name;22722273p_list->push_back(prop);2274}2275}2276}227722782279