Path: blob/master/editor/animation/animation_state_machine_editor.cpp
9896 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::_state_machine_gui_input(const Ref<InputEvent> &p_event) {128AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();129if (!tree) {130return;131}132133String node_directory;134Ref<AnimationNodeStateMachinePlayback> playback = tree->get(_get_root_playback_path(node_directory));135if (playback.is_null()) {136return;137}138139Ref<InputEventKey> k = p_event;140if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {141if (selected_node != StringName() || !selected_nodes.is_empty() || selected_transition_to != StringName() || selected_transition_from != StringName()) {142if (!read_only) {143_erase_selected();144}145accept_event();146}147}148149Ref<InputEventMouseButton> mb = p_event;150151// Add new node152if (!read_only) {153if (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))) {154connecting_from = StringName();155_open_menu(mb->get_position());156}157}158159// Select node or push a field inside160if (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) {161selected_transition_from = StringName();162selected_transition_to = StringName();163selected_transition_index = -1;164selected_node = StringName();165166for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order167if (node_rects[i].play.has_point(mb->get_position())) { //edit name168if (play_mode->get_selected() == 1 || !playback->is_playing()) {169// Start170playback->start(node_directory + String(node_rects[i].node_name));171} else {172// Travel173playback->travel(node_directory + String(node_rects[i].node_name));174}175state_machine_draw->queue_redraw();176return;177}178179if (!read_only) {180if (node_rects[i].name.has_point(mb->get_position()) && state_machine->can_edit_node(node_rects[i].node_name)) { // edit name181// TODO: Avoid using strings, expose a method on LineEdit.182Ref<StyleBox> line_sb = name_edit->get_theme_stylebox(CoreStringName(normal));183Rect2 edit_rect = node_rects[i].name;184edit_rect.position -= line_sb->get_offset();185edit_rect.size += line_sb->get_minimum_size();186187name_edit_popup->set_position(state_machine_draw->get_screen_position() + edit_rect.position);188name_edit_popup->set_size(edit_rect.size);189name_edit->set_text(node_rects[i].node_name);190name_edit_popup->popup();191name_edit->grab_focus();192name_edit->select_all();193194prev_name = node_rects[i].node_name;195return;196}197}198199if (node_rects[i].edit.has_point(mb->get_position())) { //edit name200callable_mp(this, &AnimationNodeStateMachineEditor::_open_editor).call_deferred(node_rects[i].node_name);201return;202}203204if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected205selected_node = node_rects[i].node_name;206207if (!selected_nodes.has(selected_node)) {208selected_nodes.clear();209}210211selected_nodes.insert(selected_node);212_update_connected_nodes(selected_node);213214Ref<AnimationNode> anode = state_machine->get_node(selected_node);215EditorNode::get_singleton()->push_item(anode.ptr(), "", true);216state_machine_draw->queue_redraw();217dragging_selected_attempt = true;218dragging_selected = false;219drag_from = mb->get_position();220snap_x = StringName();221snap_y = StringName();222_update_mode();223return;224}225}226227// Test the transition lines.228int closest = -1;229float closest_d = 1e20;230Vector<int> close_candidates;231232// First find closest lines using point-to-segment distance.233for (int i = 0; i < transition_lines.size(); i++) {234Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mb->get_position(), transition_lines[i].from, transition_lines[i].to);235float d = cpoint.distance_to(mb->get_position());236237if (d > transition_lines[i].width) {238continue;239}240241// If this is very close to our current closest distance, add it to candidates.242if (Math::abs(d - closest_d) < 2.0) { // Within 2 pixels.243close_candidates.push_back(i);244} else if (d < closest_d) {245closest_d = d;246closest = i;247close_candidates.clear();248close_candidates.push_back(i);249}250}251252// Use midpoint distance as bias.253if (close_candidates.size() > 1) {254float best_midpoint_dist = 1e20;255256for (int idx : close_candidates) {257Vector2 midpoint = (transition_lines[idx].from + transition_lines[idx].to) / 2.0;258float midpoint_dist = midpoint.distance_to(mb->get_position());259260if (midpoint_dist < best_midpoint_dist) {261best_midpoint_dist = midpoint_dist;262closest = idx;263}264}265}266267if (closest >= 0) {268selected_transition_from = transition_lines[closest].from_node;269selected_transition_to = transition_lines[closest].to_node;270selected_transition_index = closest;271272// Update connected_nodes for the selected transition.273connected_nodes.clear();274connected_nodes.insert(selected_transition_from);275connected_nodes.insert(selected_transition_to);276277Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(closest);278if (!state_machine->is_transition_across_group(closest)) {279EditorNode::get_singleton()->push_item(tr.ptr(), "", true);280} else {281EditorNode::get_singleton()->push_item(tr.ptr(), "", true);282EditorNode::get_singleton()->push_item(nullptr, "", true);283}284}285286state_machine_draw->queue_redraw();287_update_mode();288}289290// End moving node291if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {292if (dragging_selected) {293Ref<AnimationNode> an = state_machine->get_node(selected_node);294updating = true;295296EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();297undo_redo->create_action(TTR("Move Node"));298299for (int i = 0; i < node_rects.size(); i++) {300if (!selected_nodes.has(node_rects[i].node_name)) {301continue;302}303304undo_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);305undo_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));306}307308undo_redo->add_do_method(this, "_update_graph");309undo_redo->add_undo_method(this, "_update_graph");310undo_redo->commit_action();311updating = false;312}313snap_x = StringName();314snap_y = StringName();315316dragging_selected_attempt = false;317dragging_selected = false;318state_machine_draw->queue_redraw();319}320321// Connect nodes322if (mb.is_valid() && ((tool_select->is_pressed() && mb->is_shift_pressed()) || tool_connect->is_pressed()) && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {323for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order324if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected325connecting = true;326connection_follows_cursor = true;327connecting_from = node_rects[i].node_name;328connecting_to = mb->get_position();329connecting_to_node = StringName();330return;331}332}333}334335// End connecting nodes336if (mb.is_valid() && connecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {337if (connecting_to_node != StringName()) {338Ref<AnimationNode> node = state_machine->get_node(connecting_to_node);339Ref<AnimationNodeStateMachine> anodesm = node;340Ref<AnimationNodeEndState> end_node = node;341342if (state_machine->has_transition(connecting_from, connecting_to_node) && state_machine->can_edit_node(connecting_to_node) && anodesm.is_null()) {343EditorNode::get_singleton()->show_warning(TTR("Transition exists!"));344connecting = false;345} else {346_add_transition();347}348} else {349_open_menu(mb->get_position());350}351connecting_to_node = StringName();352connection_follows_cursor = false;353state_machine_draw->queue_redraw();354}355356// Start box selecting357if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && tool_select->is_pressed()) {358box_selecting = true;359box_selecting_from = box_selecting_to = state_machine_draw->get_local_mouse_position();360box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());361362if (mb->is_command_or_control_pressed() || mb->is_shift_pressed()) {363previous_selected = selected_nodes;364} else {365selected_nodes.clear();366previous_selected.clear();367}368}369370// End box selecting371if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {372box_selecting = false;373state_machine_draw->queue_redraw();374_update_mode();375}376377if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {378StringName clicked_node;379for (int i = node_rects.size() - 1; i >= 0; i--) {380if (node_rects[i].node.has_point(mb->get_position())) {381clicked_node = node_rects[i].node_name;382break;383}384}385386if (clicked_node != StringName()) {387if (selected_nodes.has(clicked_node) && mb->is_shift_pressed()) {388selected_nodes.erase(clicked_node);389} else {390if (!mb->is_shift_pressed()) {391selected_nodes.clear();392}393selected_nodes.insert(clicked_node);394}395selected_node = clicked_node;396} else {397// Clicked on empty space.398selected_nodes.clear();399selected_node = StringName();400}401402_update_connected_nodes(selected_node);403state_machine_draw->queue_redraw();404_update_mode();405406if (clicked_node != StringName()) {407Ref<AnimationNode> anode = state_machine->get_node(clicked_node);408EditorNode::get_singleton()->push_item(anode.ptr(), "", true);409dragging_selected_attempt = true;410dragging_selected = false;411drag_from = mb->get_position();412snap_x = StringName();413snap_y = StringName();414}415416return;417}418419Ref<InputEventMouseMotion> mm = p_event;420421// Pan window422if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {423h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);424v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);425}426427// Move mouse while connecting428if (mm.is_valid() && connecting && connection_follows_cursor && !read_only) {429connecting_to = mm->get_position();430connecting_to_node = StringName();431state_machine_draw->queue_redraw();432433for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order434if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected435connecting_to_node = node_rects[i].node_name;436return;437}438}439}440441// Move mouse while moving a node442if (mm.is_valid() && dragging_selected_attempt && !read_only) {443dragging_selected = true;444drag_ofs = mm->get_position() - drag_from;445snap_x = StringName();446snap_y = StringName();447{448//snap449Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE;450LocalVector<StringName> nodes = state_machine->get_node_list();451452float best_d_x = 1e20;453float best_d_y = 1e20;454455for (const StringName &E : nodes) {456if (E == selected_node) {457continue;458}459Vector2 npos = state_machine->get_node_position(E);460461float d_x = Math::abs(npos.x - cpos.x);462if (d_x < MIN(5, best_d_x)) {463drag_ofs.x -= cpos.x - npos.x;464best_d_x = d_x;465snap_x = E;466}467468float d_y = Math::abs(npos.y - cpos.y);469if (d_y < MIN(5, best_d_y)) {470drag_ofs.y -= cpos.y - npos.y;471best_d_y = d_y;472snap_y = E;473}474}475}476477state_machine_draw->queue_redraw();478}479480// Move mouse while moving box select481if (mm.is_valid() && box_selecting) {482box_selecting_to = state_machine_draw->get_local_mouse_position();483484box_selecting_rect = Rect2(box_selecting_from.min(box_selecting_to), (box_selecting_from - box_selecting_to).abs());485486for (int i = 0; i < node_rects.size(); i++) {487bool in_box = node_rects[i].node.intersects(box_selecting_rect);488489if (in_box) {490if (previous_selected.has(node_rects[i].node_name)) {491selected_nodes.erase(node_rects[i].node_name);492} else {493selected_nodes.insert(node_rects[i].node_name);494}495} else {496if (previous_selected.has(node_rects[i].node_name)) {497selected_nodes.insert(node_rects[i].node_name);498} else {499selected_nodes.erase(node_rects[i].node_name);500}501}502}503504state_machine_draw->queue_redraw();505}506507if (mm.is_valid()) {508String new_hovered_node_name;509HoveredNodeArea new_hovered_node_area = HOVER_NODE_NONE;510if (tool_select->is_pressed()) {511for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.512513if (!state_machine->can_edit_node(node_rects[i].node_name)) {514continue; // start/end node can't be edited515}516517if (node_rects[i].node.has_point(mm->get_position())) {518new_hovered_node_name = node_rects[i].node_name;519if (node_rects[i].play.has_point(mm->get_position())) {520new_hovered_node_area = HOVER_NODE_PLAY;521} else if (node_rects[i].edit.has_point(mm->get_position())) {522new_hovered_node_area = HOVER_NODE_EDIT;523}524break;525}526}527}528529if (new_hovered_node_name != hovered_node_name || new_hovered_node_area != hovered_node_area) {530hovered_node_name = new_hovered_node_name;531hovered_node_area = new_hovered_node_area;532state_machine_draw->queue_redraw();533}534535// set tooltip for transition536if (tool_select->is_pressed()) {537int closest = -1;538float closest_d = 1e20;539for (int i = 0; i < transition_lines.size(); i++) {540Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mm->get_position(), transition_lines[i].from, transition_lines[i].to);541float d = cpoint.distance_to(mm->get_position());542if (d > transition_lines[i].width) {543continue;544}545546if (d < closest_d) {547closest = i;548closest_d = d;549}550}551552if (closest >= 0) {553String from = String(transition_lines[closest].from_node);554String to = String(transition_lines[closest].to_node);555String tooltip = from + " -> " + to;556state_machine_draw->set_tooltip_text(tooltip);557} else {558state_machine_draw->set_tooltip_text("");559}560}561}562563Ref<InputEventPanGesture> pan_gesture = p_event;564if (pan_gesture.is_valid()) {565h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);566v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);567}568}569570Control::CursorShape AnimationNodeStateMachineEditor::get_cursor_shape(const Point2 &p_pos) const {571Control::CursorShape cursor_shape = get_default_cursor_shape();572if (!read_only) {573// Put ibeam (text cursor) over names to make it clearer that they are editable.574Transform2D xform = panel->get_transform() * state_machine_draw->get_transform();575Point2 pos = xform.xform_inv(p_pos);576577for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.578if (node_rects[i].node.has_point(pos)) {579if (node_rects[i].name.has_point(pos)) {580if (state_machine->can_edit_node(node_rects[i].node_name)) {581cursor_shape = Control::CURSOR_IBEAM;582}583}584break;585}586}587}588return cursor_shape;589}590591String AnimationNodeStateMachineEditor::get_tooltip(const Point2 &p_pos) const {592if (hovered_node_name == StringName()) {593return AnimationTreeNodeEditorPlugin::get_tooltip(p_pos);594}595596String tooltip_text;597if (hovered_node_area == HOVER_NODE_PLAY) {598tooltip_text = vformat(TTR("Play/Travel to %s"), hovered_node_name);599} else if (hovered_node_area == HOVER_NODE_EDIT) {600tooltip_text = vformat(TTR("Edit %s"), hovered_node_name);601} else {602tooltip_text = hovered_node_name;603}604605return tooltip_text;606}607608void AnimationNodeStateMachineEditor::_open_menu(const Vector2 &p_position) {609AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();610if (!tree) {611return;612}613614menu->clear(false);615animations_menu->clear();616animations_to_add.clear();617618List<StringName> animation_names;619tree->get_animation_list(&animation_names);620menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);621if (animation_names.is_empty()) {622menu->set_item_disabled(menu->get_item_idx_from_text(TTR("Add Animation")), true);623} else {624for (const StringName &name : animation_names) {625animations_menu->add_icon_item(theme_cache.animation_icon, name);626animations_to_add.push_back(name);627}628}629630LocalVector<StringName> classes;631ClassDB::get_inheriters_from_class("AnimationRootNode", classes);632classes.sort_custom<StringName::AlphCompare>();633634for (const StringName &class_name : classes) {635String name = String(class_name).replace_first("AnimationNode", "");636if (name == "Animation" || name == "StartState" || name == "EndState") {637continue; // nope638}639int idx = menu->get_item_count();640menu->add_item(vformat(TTR("Add %s"), name), idx);641menu->set_item_metadata(idx, class_name);642}643Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();644645if (clipb.is_valid()) {646menu->add_separator();647menu->add_item(TTR("Paste"), MENU_PASTE);648}649menu->add_separator();650menu->add_item(TTR("Load..."), MENU_LOAD_FILE);651652menu->set_position(state_machine_draw->get_screen_transform().xform(p_position));653menu->popup();654add_node_pos = p_position / EDSCALE + state_machine->get_graph_offset();655}656657bool AnimationNodeStateMachineEditor::_create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path) {658String prev_path;659660LocalVector<StringName> nodes = p_nodesm->get_node_list();661662PopupMenu *nodes_menu = memnew(PopupMenu);663nodes_menu->set_name(p_name);664nodes_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));665p_menu->add_child(nodes_menu);666667bool node_added = false;668for (const StringName &E : nodes) {669if (p_nodesm->can_edit_node(E)) {670Ref<AnimationNodeStateMachine> ansm = p_nodesm->get_node(E);671672String path = String(p_path) + "/" + E;673674if (ansm == state_machine) {675end_menu->add_item(E, nodes_to_connect.size());676nodes_to_connect.push_back(SceneStringName(End));677continue;678}679680if (ansm.is_valid()) {681state_machine_menu->add_item(E, nodes_to_connect.size());682nodes_to_connect.push_back(path);683684if (_create_submenu(nodes_menu, ansm, E, path)) {685nodes_menu->add_submenu_item(E, E);686node_added = true;687}688} else {689nodes_menu->add_item(E, nodes_to_connect.size());690nodes_to_connect.push_back(path);691node_added = true;692}693}694}695696return node_added;697}698699void AnimationNodeStateMachineEditor::_stop_connecting() {700connecting = false;701state_machine_draw->queue_redraw();702}703704void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {705file_loaded = ResourceLoader::load(p_file);706if (file_loaded.is_valid()) {707_add_menu_type(MENU_LOAD_FILE_CONFIRM);708} else {709EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));710}711}712713void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {714String base_name;715Ref<AnimationRootNode> node;716717if (p_index == MENU_LOAD_FILE) {718open_file->clear_filters();719List<String> filters;720ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);721for (const String &E : filters) {722open_file->add_filter("*." + E);723}724open_file->popup_file_dialog();725return;726} else if (p_index == MENU_LOAD_FILE_CONFIRM) {727node = file_loaded;728file_loaded.unref();729} else if (p_index == MENU_PASTE) {730node = EditorSettings::get_singleton()->get_resource_clipboard();731732} else {733String type = menu->get_item_metadata(p_index);734735Object *obj = ClassDB::instantiate(type);736ERR_FAIL_NULL(obj);737AnimationNode *an = Object::cast_to<AnimationNode>(obj);738ERR_FAIL_NULL(an);739740node = Ref<AnimationNode>(an);741base_name = type.replace_first("AnimationNode", "");742}743744if (node.is_null()) {745EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));746return;747}748749if (base_name.is_empty()) {750base_name = node->get_class().replace_first("AnimationNode", "");751}752753int base = 1;754String name = base_name;755while (state_machine->has_node(name)) {756base++;757name = base_name + " " + itos(base);758}759760updating = true;761EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();762undo_redo->create_action(TTR("Add Node and Transition"));763undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);764undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);765connecting_to_node = name;766_add_transition(true);767undo_redo->commit_action();768updating = false;769770state_machine_draw->queue_redraw();771}772773void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {774Ref<AnimationNodeAnimation> anim;775anim.instantiate();776777anim->set_animation(animations_to_add[p_index]);778779String base_name = animations_to_add[p_index].validate_node_name();780int base = 1;781String name = base_name;782while (state_machine->has_node(name)) {783base++;784name = base_name + " " + itos(base);785}786787updating = true;788EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();789undo_redo->create_action(TTR("Add Node and Transition"));790undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);791undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);792connecting_to_node = name;793_add_transition(true);794undo_redo->commit_action();795updating = false;796797state_machine_draw->queue_redraw();798}799800void AnimationNodeStateMachineEditor::_connect_to(int p_index) {801connecting_to_node = nodes_to_connect[p_index];802_add_transition();803}804805void AnimationNodeStateMachineEditor::_add_transition(const bool p_nested_action) {806if (connecting_from != StringName() && connecting_to_node != StringName()) {807if (state_machine->has_transition(connecting_from, connecting_to_node)) {808EditorNode::get_singleton()->show_warning("Transition exists!");809connecting = false;810return;811}812813Ref<AnimationNodeStateMachineTransition> tr;814tr.instantiate();815tr->set_advance_mode(auto_advance->is_pressed() ? AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_AUTO : AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_ENABLED);816tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()));817818EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();819if (!p_nested_action) {820updating = true;821undo_redo->create_action(TTR("Add Transition"));822}823824undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr);825undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node);826undo_redo->add_do_method(this, "_update_graph");827undo_redo->add_undo_method(this, "_update_graph");828829if (!p_nested_action) {830undo_redo->commit_action();831updating = false;832}833834selected_transition_from = connecting_from;835selected_transition_to = connecting_to_node;836selected_transition_index = transition_lines.size();837838if (!state_machine->is_transition_across_group(selected_transition_index)) {839EditorNode::get_singleton()->push_item(tr.ptr(), "", true);840} else {841EditorNode::get_singleton()->push_item(tr.ptr(), "", true);842EditorNode::get_singleton()->push_item(nullptr, "", true);843}844_update_mode();845}846847connecting = false;848}849850void 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) {851Color line_color = p_enabled ? theme_cache.transition_color : theme_cache.transition_disabled_color;852Color icon_color = p_enabled ? theme_cache.transition_icon_color : theme_cache.transition_icon_disabled_color;853Color highlight_color = p_enabled ? theme_cache.highlight_color : theme_cache.highlight_disabled_color;854855line_color.a *= p_opacity;856icon_color.a *= p_opacity;857highlight_color.a *= p_opacity;858859if (p_travel) {860line_color = highlight_color;861}862863if (p_selected) {864state_machine_draw->draw_line(p_from, p_to, highlight_color, 6, true);865}866state_machine_draw->draw_line(p_from, p_to, line_color, 2, true);867868if (p_fade_ratio > 0.0) {869Color fade_line_color = highlight_color;870fade_line_color.set_hsv(1.0, fade_line_color.get_s(), fade_line_color.get_v());871fade_line_color.a *= p_opacity;872state_machine_draw->draw_line(p_from, p_from.lerp(p_to, p_fade_ratio), fade_line_color, 2);873}874875const int ICON_COUNT = std::size(theme_cache.transition_icons);876int icon_index = p_mode + (p_auto_advance ? ICON_COUNT / 2 : 0);877ERR_FAIL_COND(icon_index >= ICON_COUNT);878Ref<Texture2D> icon = theme_cache.transition_icons[icon_index];879880Transform2D xf;881xf.columns[0] = (p_to - p_from).normalized();882xf.columns[1] = xf.columns[0].orthogonal();883xf.columns[2] = (p_from + p_to) * 0.5 - xf.columns[1] * icon->get_height() * 0.5 - xf.columns[0] * icon->get_height() * 0.5;884885state_machine_draw->draw_set_transform_matrix(xf);886if (!p_is_across_group) {887state_machine_draw->draw_texture(icon, Vector2(), icon_color);888}889state_machine_draw->draw_set_transform_matrix(Transform2D());890}891892void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect) {893if (p_to == r_from) {894return;895}896897//this could be optimized...898Vector2 n = (p_to - r_from).normalized();899while (p_rect.has_point(r_from)) {900r_from += n;901}902}903904void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect) {905if (r_to == p_from) {906return;907}908909//this could be optimized...910Vector2 n = (r_to - p_from).normalized();911while (p_rect.has_point(r_to)) {912r_to -= n;913}914}915916Ref<StyleBox> AnimationNodeStateMachineEditor::_adjust_stylebox_opacity(Ref<StyleBox> p_style, float p_opacity) {917Ref<StyleBox> style = p_style->duplicate();918if (style->is_class("StyleBoxFlat")) {919Ref<StyleBoxFlat> flat_style = style;920Color bg_color = flat_style->get_bg_color();921Color border_color = flat_style->get_border_color();922Color shadow_color = flat_style->get_shadow_color();923924bg_color.a *= p_opacity;925border_color.a *= p_opacity;926shadow_color.a *= p_opacity;927928flat_style->set_bg_color(bg_color);929flat_style->set_border_color(border_color);930flat_style->set_shadow_color(shadow_color);931}932return style;933}934935void AnimationNodeStateMachineEditor::_state_machine_draw() {936AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();937if (!tree) {938return;939}940941bool playing = false;942StringName current;943StringName blend_from;944Vector<StringName> travel_path;945946Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");947if (playback.is_valid()) {948playing = playback->is_playing();949current = playback->get_current_node();950blend_from = playback->get_fading_from_node();951travel_path = playback->get_travel_path();952}953954if (state_machine_draw->has_focus()) {955state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), theme_cache.focus_color, false);956}957int sep = 3 * EDSCALE;958959LocalVector<StringName> nodes = state_machine->get_node_list();960961node_rects.clear();962Rect2 scroll_range;963964//snap lines965if (dragging_selected) {966Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE;967if (snap_x != StringName()) {968Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;969state_machine_draw->draw_line(from, to, theme_cache.guideline_color, 2);970}971if (snap_y != StringName()) {972Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;973state_machine_draw->draw_line(from, to, theme_cache.guideline_color, 2);974}975}976977//pre pass nodes so we know the rectangles978for (const StringName &E : nodes) {979String name = E;980int name_string_size = theme_cache.node_title_font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size).width;981982Ref<AnimationNode> anode = state_machine->get_node(name);983bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);984bool is_selected = selected_nodes.has(name);985986Size2 s = (is_selected ? theme_cache.node_frame_selected : theme_cache.node_frame)->get_minimum_size();987s.width += name_string_size;988s.height += MAX(theme_cache.node_title_font->get_height(theme_cache.node_title_font_size), theme_cache.play_node->get_height());989s.width += sep + theme_cache.play_node->get_width();990991if (needs_editor) {992s.width += sep + theme_cache.edit_node->get_width();993}994995Vector2 offset;996offset += state_machine->get_node_position(E) * EDSCALE;997998if (selected_nodes.has(E) && dragging_selected) {999offset += drag_ofs;1000}10011002offset -= s / 2;1003offset = offset.floor();10041005//prepre rect10061007NodeRect nr;1008nr.node = Rect2(offset, s);1009nr.node_name = E;10101011scroll_range = scroll_range.merge(nr.node); //merge with range10121013//now scroll it to draw1014nr.node.position -= state_machine->get_graph_offset() * EDSCALE;10151016node_rects.push_back(nr);1017}10181019transition_lines.clear();10201021//draw connecting line for potential new transition1022if (connecting) {1023Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1024Vector2 to;1025if (connecting_to_node != StringName()) {1026to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;1027} else {1028to = connecting_to;1029}10301031for (int i = 0; i < node_rects.size(); i++) {1032if (node_rects[i].node_name == connecting_from) {1033_clip_src_line_to_rect(from, to, node_rects[i].node);1034}1035if (node_rects[i].node_name == connecting_to_node) {1036_clip_dst_line_to_rect(from, to, node_rects[i].node);1037}1038}10391040_connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()), true, false, false, 0.0, false, false);1041}10421043// TransitionImmediateBig1044float tr_bidi_offset = int(theme_cache.transition_icons[0]->get_height() * 0.8);10451046//draw transition lines1047for (int i = 0; i < state_machine->get_transition_count(); i++) {1048TransitionLine tl;1049tl.transition_index = i;10501051tl.from_node = state_machine->get_transition_from(i);1052Vector2 ofs_from = (dragging_selected && selected_nodes.has(tl.from_node)) ? drag_ofs : Vector2();1053tl.from = (state_machine->get_node_position(tl.from_node) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE;10541055tl.to_node = state_machine->get_transition_to(i);1056Vector2 ofs_to = (dragging_selected && selected_nodes.has(tl.to_node)) ? drag_ofs : Vector2();1057tl.to = (state_machine->get_node_position(tl.to_node) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE;10581059Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);1060tl.disabled = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED);1061tl.auto_advance = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);1062tl.advance_condition_name = tr->get_advance_condition_name();1063tl.advance_condition_state = false;1064tl.mode = tr->get_switch_mode();1065tl.width = tr_bidi_offset;1066tl.travel = false;1067tl.fade_ratio = 0.0;1068tl.hidden = false;1069tl.is_across_group = state_machine->is_transition_across_group(i);10701071if (state_machine->has_transition(tl.to_node, tl.from_node)) { //offset if same exists1072Vector2 offset = -(tl.from - tl.to).normalized().orthogonal() * tr_bidi_offset;1073tl.from += offset;1074tl.to += offset;1075}10761077for (int j = 0; j < node_rects.size(); j++) {1078if (node_rects[j].node_name == tl.from_node) {1079_clip_src_line_to_rect(tl.from, tl.to, node_rects[j].node);1080}1081if (node_rects[j].node_name == tl.to_node) {1082_clip_dst_line_to_rect(tl.from, tl.to, node_rects[j].node);1083}1084}10851086tl.selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node;10871088if (blend_from == tl.from_node && current == tl.to_node) {1089tl.travel = true;1090tl.fade_ratio = MIN(1.0, fading_pos / fading_time);1091}10921093if (travel_path.size()) {1094if (current == tl.from_node && travel_path[0] == tl.to_node) {1095tl.travel = true;1096} else {1097for (int j = 0; j < travel_path.size() - 1; j++) {1098if (travel_path[j] == tl.from_node && travel_path[j + 1] == tl.to_node) {1099tl.travel = true;1100break;1101}1102}1103}1104}11051106StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name);1107if (tl.advance_condition_name != StringName() && bool(tree->get(fullpath))) {1108tl.advance_condition_state = true;1109tl.auto_advance = true;1110}11111112// check if already have this transition1113for (int j = 0; j < transition_lines.size(); j++) {1114if (transition_lines[j].from_node == tl.from_node && transition_lines[j].to_node == tl.to_node) {1115tl.hidden = true;1116transition_lines.write[j].disabled = transition_lines[j].disabled && tl.disabled;1117}1118}1119transition_lines.push_back(tl);1120}11211122for (int i = 0; i < transition_lines.size(); i++) {1123TransitionLine tl = transition_lines[i];1124if (!tl.hidden) {1125float opacity = 0.2; // Default to reduced opacity.11261127if (selected_transition_from != StringName() && selected_transition_to != StringName()) {1128// A transition is selected.1129if ((tl.from_node == selected_transition_from && tl.to_node == selected_transition_to) || (tl.from_node == selected_transition_to && tl.to_node == selected_transition_from)) {1130opacity = 1.0; // Full opacity for the selected transition pair.1131}1132} else if (!connected_nodes.is_empty()) {1133// A node is selected.1134if (connected_nodes.has(selected_node)) {1135// Only keep full opacity for transitions directly connected to the selected node.1136if (tl.from_node == selected_node || tl.to_node == selected_node) {1137opacity = 1.0;1138}1139} else {1140// If no node is selected, all transitions are at full opacity.1141opacity = 1.0;1142}1143} else {1144// If nothing is selected, all transitions are at full opacity.1145opacity = 1.0;1146}11471148_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);1149}1150}11511152//draw actual nodes1153for (int i = 0; i < node_rects.size(); i++) {1154String name = node_rects[i].node_name;1155int name_string_size = theme_cache.node_title_font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.node_title_font_size).width;11561157Ref<AnimationNode> anode = state_machine->get_node(name);1158bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);1159bool is_selected = selected_nodes.has(name);11601161NodeRect &nr = node_rects.write[i];1162Vector2 offset = nr.node.position;1163int h = nr.node.size.height;11641165float opacity = 1.0;1166if (selected_transition_from != StringName() && selected_transition_to != StringName()) {1167// A transition is selected.1168if (name != selected_transition_from && name != selected_transition_to) {1169opacity = 0.2;1170}1171} else if (!connected_nodes.is_empty() && !connected_nodes.has(name)) {1172// A node is selected.1173opacity = 0.2;1174}11751176Ref<StyleBox> original_style = is_selected ? theme_cache.node_frame_selected : theme_cache.node_frame;1177Ref<StyleBox> node_style = _adjust_stylebox_opacity(original_style, opacity);11781179state_machine_draw->draw_style_box(node_style, nr.node);11801181if (!is_selected && SceneStringName(Start) == name) {1182Ref<StyleBox> start_style = _adjust_stylebox_opacity(theme_cache.node_frame_start, opacity);1183state_machine_draw->draw_style_box(start_style, nr.node);1184}1185if (!is_selected && SceneStringName(End) == name) {1186Ref<StyleBox> end_style = _adjust_stylebox_opacity(theme_cache.node_frame_end, opacity);1187state_machine_draw->draw_style_box(end_style, nr.node);1188}1189if (playing && (blend_from == name || current == name || travel_path.has(name))) {1190Ref<StyleBox> playing_style = _adjust_stylebox_opacity(theme_cache.node_frame_playing, opacity);1191state_machine_draw->draw_style_box(playing_style, nr.node);1192}11931194offset.x += original_style->get_offset().x;11951196nr.play.position = offset + Vector2(0, (h - theme_cache.play_node->get_height()) / 2).floor();1197nr.play.size = theme_cache.play_node->get_size();11981199Color color_mod = Color(1, 1, 1, opacity);1200if (hovered_node_name == name && hovered_node_area == HOVER_NODE_PLAY) {1201state_machine_draw->draw_texture(theme_cache.play_node, nr.play.position, theme_cache.highlight_color * color_mod);1202} else {1203state_machine_draw->draw_texture(theme_cache.play_node, nr.play.position, color_mod);1204}12051206offset.x += sep + theme_cache.play_node->get_width();12071208nr.name.position = offset + Vector2(0, (h - theme_cache.node_title_font->get_height(theme_cache.node_title_font_size)) / 2).floor();1209nr.name.size = Vector2(name_string_size, theme_cache.node_title_font->get_height(theme_cache.node_title_font_size));12101211Color font_color = theme_cache.node_title_font_color;1212font_color.a *= opacity;1213state_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);1214offset.x += name_string_size + sep;12151216nr.can_edit = needs_editor;1217if (needs_editor) {1218nr.edit.position = offset + Vector2(0, (h - theme_cache.edit_node->get_height()) / 2).floor();1219nr.edit.size = theme_cache.edit_node->get_size();12201221if (hovered_node_name == name && hovered_node_area == HOVER_NODE_EDIT) {1222state_machine_draw->draw_texture(theme_cache.edit_node, nr.edit.position, theme_cache.highlight_color * color_mod);1223} else {1224state_machine_draw->draw_texture(theme_cache.edit_node, nr.edit.position, color_mod);1225}1226}1227}12281229//draw box select1230if (box_selecting) {1231state_machine_draw->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));1232}12331234scroll_range.position -= state_machine_draw->get_size();1235scroll_range.size += state_machine_draw->get_size() * 2.0;12361237//adjust scrollbars1238updating = true;1239h_scroll->set_min(scroll_range.position.x);1240h_scroll->set_max(scroll_range.position.x + scroll_range.size.x);1241h_scroll->set_page(state_machine_draw->get_size().x);1242h_scroll->set_value(state_machine->get_graph_offset().x);12431244v_scroll->set_min(scroll_range.position.y);1245v_scroll->set_max(scroll_range.position.y + scroll_range.size.y);1246v_scroll->set_page(state_machine_draw->get_size().y);1247v_scroll->set_value(state_machine->get_graph_offset().y);1248updating = false;12491250state_machine_play_pos->queue_redraw();1251}12521253void AnimationNodeStateMachineEditor::_update_connected_nodes(const StringName &p_node) {1254connected_nodes.clear();1255if (p_node != StringName()) {1256connected_nodes.insert(p_node);12571258Vector<StringName> nodes_to = state_machine->get_nodes_with_transitions_to(p_node);1259for (const StringName &node_to : nodes_to) {1260connected_nodes.insert(node_to);1261}12621263Vector<StringName> nodes_from = state_machine->get_nodes_with_transitions_from(p_node);1264for (const StringName &node_from : nodes_from) {1265connected_nodes.insert(node_from);1266}1267}1268}12691270void AnimationNodeStateMachineEditor::_state_machine_pos_draw_individual(const String &p_name, float p_ratio) {1271AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1272if (!tree) {1273return;1274}12751276Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1277if (playback.is_null() || !playback->is_playing()) {1278return;1279}12801281if (p_name == SceneStringName(Start) || p_name == SceneStringName(End) || p_name.is_empty()) {1282return;1283}12841285int idx = -1;1286for (int i = 0; i < node_rects.size(); i++) {1287if (node_rects[i].node_name == p_name) {1288idx = i;1289break;1290}1291}12921293if (idx == -1) {1294return;1295}12961297const NodeRect &nr = node_rects[idx];1298if (nr.can_edit) {1299return; // It is not AnimationNodeAnimation.1300}13011302Vector2 from;1303from.x = nr.play.position.x;1304from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5;13051306Vector2 to;1307if (nr.edit.size.x) {1308to.x = nr.edit.position.x + nr.edit.size.x;1309} else {1310to.x = nr.name.position.x + nr.name.size.x;1311}1312to.y = from.y;13131314state_machine_play_pos->draw_line(from, to, theme_cache.playback_background_color, 2);1315to = from.lerp(to, p_ratio);1316state_machine_play_pos->draw_line(from, to, theme_cache.playback_color, 2);1317}13181319void AnimationNodeStateMachineEditor::_state_machine_pos_draw_all() {1320AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1321if (!tree) {1322return;1323}13241325Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1326if (playback.is_null() || !playback->is_playing()) {1327return;1328}13291330{1331float len = MAX(0.0001, current_length);1332float pos = CLAMP(current_play_pos, 0, len);1333float c = pos / len;1334_state_machine_pos_draw_individual(playback->get_current_node(), c);1335}13361337{1338float len = MAX(0.0001, fade_from_length);1339float pos = CLAMP(fade_from_current_play_pos, 0, len);1340float c = pos / len;1341_state_machine_pos_draw_individual(playback->get_fading_from_node(), c);1342}1343}13441345void AnimationNodeStateMachineEditor::_update_graph() {1346if (updating) {1347return;1348}13491350updating = true;13511352state_machine_draw->queue_redraw();13531354updating = false;1355}13561357void AnimationNodeStateMachineEditor::_notification(int p_what) {1358switch (p_what) {1359case NOTIFICATION_THEME_CHANGED: {1360panel->add_theme_style_override(SceneStringName(panel), theme_cache.panel_style);1361error_panel->add_theme_style_override(SceneStringName(panel), theme_cache.error_panel_style);1362error_label->add_theme_color_override(SceneStringName(font_color), theme_cache.error_color);13631364tool_select->set_button_icon(theme_cache.tool_icon_select);1365tool_create->set_button_icon(theme_cache.tool_icon_create);1366tool_connect->set_button_icon(theme_cache.tool_icon_connect);13671368switch_mode->clear();1369switch_mode->add_icon_item(theme_cache.transition_icon_immediate, TTR("Immediate"));1370switch_mode->add_icon_item(theme_cache.transition_icon_sync, TTR("Sync"));1371switch_mode->add_icon_item(theme_cache.transition_icon_end, TTR("At End"));13721373auto_advance->set_button_icon(theme_cache.play_icon_auto);13741375tool_erase->set_button_icon(theme_cache.tool_icon_erase);13761377play_mode->clear();1378play_mode->add_icon_item(theme_cache.play_icon_travel, TTR("Travel"));1379play_mode->add_icon_item(theme_cache.play_icon_start, TTR("Immediate"));1380} break;13811382case NOTIFICATION_PROCESS: {1383AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1384if (!tree) {1385return;1386}13871388String error;13891390Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");13911392if (error_time > 0) {1393error = error_text;1394error_time -= get_process_delta_time();1395} else if (!tree->is_active()) {1396error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");1397} else if (tree->is_state_invalid()) {1398error = tree->get_invalid_state_reason();1399} else if (playback.is_null()) {1400error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");1401}14021403if (error != error_label->get_text()) {1404error_label->set_text(error);1405if (!error.is_empty()) {1406error_panel->show();1407} else {1408error_panel->hide();1409}1410}14111412for (int i = 0; i < transition_lines.size(); i++) {1413int tidx = -1;1414for (int j = 0; j < state_machine->get_transition_count(); j++) {1415if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {1416tidx = j;1417break;1418}1419}14201421if (tidx == -1) { //missing transition, should redraw1422state_machine_draw->queue_redraw();1423break;1424}14251426if (transition_lines[i].disabled != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED)) {1427state_machine_draw->queue_redraw();1428break;1429}14301431if (transition_lines[i].auto_advance != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO)) {1432state_machine_draw->queue_redraw();1433break;1434}14351436if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {1437state_machine_draw->queue_redraw();1438break;1439}14401441if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {1442state_machine_draw->queue_redraw();1443break;1444}14451446bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name)));14471448if (transition_lines[i].advance_condition_state != acstate) {1449state_machine_draw->queue_redraw();1450break;1451}1452}14531454bool same_travel_path = true;1455Vector<StringName> tp;1456bool is_playing = false;1457StringName current_node;1458StringName fading_from_node;14591460current_play_pos = 0;1461current_length = 0;14621463fade_from_current_play_pos = 0;1464fade_from_length = 0;14651466fading_time = 0;1467fading_pos = 0;14681469if (playback.is_valid()) {1470tp = playback->get_travel_path();1471is_playing = playback->is_playing();1472current_node = playback->get_current_node();1473fading_from_node = playback->get_fading_from_node();1474current_play_pos = playback->get_current_play_pos();1475current_length = playback->get_current_length();1476fade_from_current_play_pos = playback->get_fade_from_play_pos();1477fade_from_length = playback->get_fade_from_length();1478fading_time = playback->get_fading_time();1479fading_pos = playback->get_fading_pos();1480}14811482{1483if (last_travel_path.size() != tp.size()) {1484same_travel_path = false;1485} else {1486for (int i = 0; i < last_travel_path.size(); i++) {1487if (last_travel_path[i] != tp[i]) {1488same_travel_path = false;1489break;1490}1491}1492}1493}14941495//redraw if travel state changed1496if (!same_travel_path ||1497last_active != is_playing ||1498last_current_node != current_node ||1499last_fading_from_node != fading_from_node ||1500last_fading_time != fading_time ||1501last_fading_pos != fading_pos) {1502state_machine_draw->queue_redraw();1503last_travel_path = tp;1504last_current_node = current_node;1505last_active = is_playing;1506last_fading_from_node = fading_from_node;1507last_fading_time = fading_time;1508last_fading_pos = fading_pos;1509state_machine_play_pos->queue_redraw();1510}15111512{1513if (current_node != StringName() && state_machine->has_node(current_node)) {1514String next = current_node;1515Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next);1516Ref<AnimationNodeStateMachinePlayback> current_node_playback;15171518while (anodesm.is_valid()) {1519current_node_playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback");1520StringName cnode = current_node_playback->get_current_node();1521next += "/" + cnode;1522if (!anodesm->has_node(cnode)) {1523break;1524}1525anodesm = anodesm->get_node(cnode);1526}15271528// when current_node is a state machine, use playback of current_node to set play_pos1529if (current_node_playback.is_valid()) {1530current_play_pos = current_node_playback->get_current_play_pos();1531current_length = current_node_playback->get_current_length();1532}1533}1534}15351536if (last_play_pos != current_play_pos || fade_from_last_play_pos != fade_from_current_play_pos) {1537last_play_pos = current_play_pos;1538fade_from_last_play_pos = fade_from_current_play_pos;1539state_machine_play_pos->queue_redraw();1540}1541} break;15421543case NOTIFICATION_VISIBILITY_CHANGED: {1544hovered_node_name = StringName();1545hovered_node_area = HOVER_NODE_NONE;1546set_process(is_visible_in_tree());1547} break;1548}1549}15501551void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {1552AnimationTreeEditor::get_singleton()->enter_editor(p_name);1553}15541555void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {1556const String &new_name = p_text;15571558ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/'));15591560if (new_name == prev_name) {1561return; // Nothing to do.1562}15631564const String &base_name = new_name;1565int base = 1;1566String name = base_name;1567while (state_machine->has_node(name)) {1568if (name == prev_name) {1569name_edit_popup->hide(); // The old name wins, the name doesn't change, just hide the popup.1570return;1571}1572base++;1573name = base_name + " " + itos(base);1574}15751576updating = true;1577EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1578undo_redo->create_action(TTR("Node Renamed"));1579undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);1580undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);1581undo_redo->add_do_method(this, "_update_graph");1582undo_redo->add_undo_method(this, "_update_graph");1583undo_redo->commit_action();1584name_edit_popup->hide();1585updating = false;15861587selected_nodes.clear();1588connected_nodes.clear();1589state_machine_draw->queue_redraw();1590}15911592void AnimationNodeStateMachineEditor::_name_edited_focus_out() {1593if (updating) {1594return;1595}15961597_name_edited(name_edit->get_text());1598}15991600void AnimationNodeStateMachineEditor::_scroll_changed(double) {1601if (updating) {1602return;1603}16041605state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));1606state_machine_draw->queue_redraw();1607}16081609void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action) {1610if (!selected_nodes.is_empty()) {1611if (!p_nested_action) {1612updating = true;1613}1614EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1615undo_redo->create_action(TTR("Node Removed"));16161617for (int i = 0; i < node_rects.size(); i++) {1618if (node_rects[i].node_name == SceneStringName(Start) || node_rects[i].node_name == SceneStringName(End)) {1619continue;1620}16211622if (!selected_nodes.has(node_rects[i].node_name)) {1623continue;1624}16251626undo_redo->add_do_method(state_machine.ptr(), "remove_node", node_rects[i].node_name);1627undo_redo->add_undo_method(state_machine.ptr(), "add_node", node_rects[i].node_name,1628state_machine->get_node(node_rects[i].node_name),1629state_machine->get_node_position(node_rects[i].node_name));16301631for (int j = 0; j < state_machine->get_transition_count(); j++) {1632String from = state_machine->get_transition_from(j);1633String to = state_machine->get_transition_to(j);1634if (from == node_rects[i].node_name || to == node_rects[i].node_name) {1635undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(j));1636}1637}1638}16391640undo_redo->add_do_method(this, "_update_graph");1641undo_redo->add_undo_method(this, "_update_graph");1642undo_redo->commit_action();16431644if (!p_nested_action) {1645updating = false;1646}16471648connected_nodes.clear();1649selected_nodes.clear();1650}16511652if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {1653Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));1654if (!p_nested_action) {1655updating = true;1656}1657EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1658undo_redo->create_action(TTR("Transition Removed"));1659undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);1660undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);1661undo_redo->add_do_method(this, "_update_graph");1662undo_redo->add_undo_method(this, "_update_graph");1663undo_redo->commit_action();1664if (!p_nested_action) {1665updating = false;1666}1667selected_transition_from = StringName();1668selected_transition_to = StringName();1669selected_transition_index = -1;1670}16711672state_machine_draw->queue_redraw();1673}16741675void AnimationNodeStateMachineEditor::_update_mode() {1676if (tool_select->is_pressed()) {1677selection_tools_hb->show();1678bool nothing_selected = selected_nodes.is_empty() && selected_transition_from == StringName() && selected_transition_to == StringName();1679bool start_end_selected = selected_nodes.size() == 1 && (*selected_nodes.begin() == SceneStringName(Start) || *selected_nodes.begin() == SceneStringName(End));1680tool_erase->set_disabled(nothing_selected || start_end_selected || read_only);1681} else {1682selection_tools_hb->hide();1683}16841685if (read_only) {1686tool_create->set_pressed(false);1687tool_connect->set_pressed(false);1688}16891690if (tool_connect->is_pressed()) {1691transition_tools_hb->show();1692} else {1693transition_tools_hb->hide();1694}1695}16961697void AnimationNodeStateMachineEditor::_bind_methods() {1698ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);16991700BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, panel_style, "panel", "GraphStateMachine");1701BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, error_panel_style, "error_panel", "GraphStateMachine");1702BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, error_color, "error_color", "GraphStateMachine");17031704BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_select, "ToolSelect", "EditorIcons");1705BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_create, "ToolAddNode", "EditorIcons");1706BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_connect, "ToolConnect", "EditorIcons");1707BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, tool_icon_erase, "Remove", "EditorIcons");17081709BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_immediate, "TransitionImmediate", "EditorIcons");1710BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_sync, "TransitionSync", "EditorIcons");1711BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icon_end, "TransitionEnd", "EditorIcons");17121713BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_start, "Play", "EditorIcons");1714BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_travel, "PlayTravel", "EditorIcons");1715BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_icon_auto, "AutoPlay", "EditorIcons");17161717BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, animation_icon, "Animation", "EditorIcons");17181719BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame, "node_frame", "GraphStateMachine");1720BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_selected, "node_frame_selected", "GraphStateMachine");1721BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_playing, "node_frame_playing", "GraphStateMachine");1722BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_start, "node_frame_start", "GraphStateMachine");1723BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, AnimationNodeStateMachineEditor, node_frame_end, "node_frame_end", "GraphStateMachine");17241725BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_FONT, AnimationNodeStateMachineEditor, node_title_font, "node_title_font", "GraphStateMachine");1726BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_FONT_SIZE, AnimationNodeStateMachineEditor, node_title_font_size, "node_title_font_size", "GraphStateMachine");1727BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, node_title_font_color, "node_title_font_color", "GraphStateMachine");17281729BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, play_node, "Play", "EditorIcons");1730BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, edit_node, "Edit", "EditorIcons");17311732BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_color, "transition_color", "GraphStateMachine");1733BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_disabled_color, "transition_disabled_color", "GraphStateMachine");1734BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_icon_color, "transition_icon_color", "GraphStateMachine");1735BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, transition_icon_disabled_color, "transition_icon_disabled_color", "GraphStateMachine");1736BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, highlight_color, "highlight_color", "GraphStateMachine");1737BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, highlight_disabled_color, "highlight_disabled_color", "GraphStateMachine");1738BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, focus_color, "focus_color", "GraphStateMachine");1739BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, guideline_color, "guideline_color", "GraphStateMachine");17401741BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[0], "TransitionImmediateBig", "EditorIcons");1742BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[1], "TransitionSyncBig", "EditorIcons");1743BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[2], "TransitionEndBig", "EditorIcons");1744BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[3], "TransitionImmediateAutoBig", "EditorIcons");1745BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[4], "TransitionSyncAutoBig", "EditorIcons");1746BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_ICON, AnimationNodeStateMachineEditor, transition_icons[5], "TransitionEndAutoBig", "EditorIcons");17471748BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, playback_color, "playback_color", "GraphStateMachine");1749BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, AnimationNodeStateMachineEditor, playback_background_color, "playback_background_color", "GraphStateMachine");1750}17511752AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr;17531754AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {1755singleton = this;17561757HBoxContainer *top_hb = memnew(HBoxContainer);1758add_child(top_hb);17591760Ref<ButtonGroup> bg;1761bg.instantiate();17621763tool_select = memnew(Button);1764tool_select->set_theme_type_variation(SceneStringName(FlatButton));1765top_hb->add_child(tool_select);1766tool_select->set_toggle_mode(true);1767tool_select->set_button_group(bg);1768tool_select->set_pressed(true);1769tool_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."));1770tool_select->set_accessibility_name(TTRC("Select and move nodes."));1771tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);17721773tool_create = memnew(Button);1774tool_create->set_theme_type_variation(SceneStringName(FlatButton));1775top_hb->add_child(tool_create);1776tool_create->set_toggle_mode(true);1777tool_create->set_button_group(bg);1778tool_create->set_tooltip_text(TTR("Create new nodes."));1779tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);17801781tool_connect = memnew(Button);1782tool_connect->set_theme_type_variation(SceneStringName(FlatButton));1783top_hb->add_child(tool_connect);1784tool_connect->set_toggle_mode(true);1785tool_connect->set_button_group(bg);1786tool_connect->set_tooltip_text(TTR("Connect nodes."));1787tool_connect->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);17881789// Context-sensitive selection tools:1790selection_tools_hb = memnew(HBoxContainer);1791top_hb->add_child(selection_tools_hb);1792selection_tools_hb->add_child(memnew(VSeparator));17931794tool_erase = memnew(Button);1795tool_erase->set_theme_type_variation(SceneStringName(FlatButton));1796tool_erase->set_tooltip_text(TTR("Remove selected node or transition."));1797tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected).bind(false));1798tool_erase->set_disabled(true);1799selection_tools_hb->add_child(tool_erase);18001801transition_tools_hb = memnew(HBoxContainer);1802top_hb->add_child(transition_tools_hb);1803transition_tools_hb->add_child(memnew(VSeparator));18041805transition_tools_hb->add_child(memnew(Label(TTR("Transition:"))));1806switch_mode = memnew(OptionButton);1807transition_tools_hb->add_child(switch_mode);18081809auto_advance = memnew(Button);1810auto_advance->set_theme_type_variation(SceneStringName(FlatButton));1811auto_advance->set_tooltip_text(TTR("New Transitions Should Auto Advance"));1812auto_advance->set_toggle_mode(true);1813auto_advance->set_pressed(true);1814transition_tools_hb->add_child(auto_advance);18151816//18171818top_hb->add_spacer();18191820top_hb->add_child(memnew(Label(TTR("Play Mode:"))));1821play_mode = memnew(OptionButton);1822top_hb->add_child(play_mode);18231824panel = memnew(PanelContainer);1825panel->set_clip_contents(true);1826panel->set_mouse_filter(Control::MOUSE_FILTER_PASS);1827add_child(panel);1828panel->set_v_size_flags(SIZE_EXPAND_FILL);18291830state_machine_draw = memnew(Control);1831panel->add_child(state_machine_draw);1832state_machine_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input));1833state_machine_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw));1834state_machine_draw->set_focus_mode(FOCUS_ALL);1835state_machine_draw->set_mouse_filter(Control::MOUSE_FILTER_PASS);18361837state_machine_play_pos = memnew(Control);1838state_machine_draw->add_child(state_machine_play_pos);1839state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent1840state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_FULL_RECT);1841state_machine_play_pos->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw_all));18421843v_scroll = memnew(VScrollBar);1844state_machine_draw->add_child(v_scroll);1845v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);1846v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));18471848h_scroll = memnew(HScrollBar);1849state_machine_draw->add_child(h_scroll);1850h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);1851h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE);1852h_scroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));18531854error_panel = memnew(PanelContainer);1855add_child(error_panel);1856error_label = memnew(Label);1857error_label->set_focus_mode(FOCUS_ACCESSIBILITY);1858error_panel->add_child(error_label);1859error_panel->hide();18601861set_custom_minimum_size(Size2(0, 300 * EDSCALE));18621863menu = memnew(PopupMenu);1864add_child(menu);1865menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type));1866menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));18671868animations_menu = memnew(PopupMenu);1869animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1870menu->add_child(animations_menu);1871animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));18721873connect_menu = memnew(PopupMenu);1874add_child(connect_menu);1875connect_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));1876connect_menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));18771878state_machine_menu = memnew(PopupMenu);1879state_machine_menu->set_name("state_machines");1880state_machine_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));1881connect_menu->add_child(state_machine_menu);18821883end_menu = memnew(PopupMenu);1884end_menu->set_name("end_nodes");1885end_menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));1886connect_menu->add_child(end_menu);18871888name_edit_popup = memnew(Popup);1889add_child(name_edit_popup);1890name_edit = memnew(LineEdit);1891name_edit_popup->add_child(name_edit);1892name_edit->set_anchors_and_offsets_preset(PRESET_FULL_RECT);1893name_edit->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited));1894name_edit->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out));18951896open_file = memnew(EditorFileDialog);1897add_child(open_file);1898open_file->set_title(TTR("Open Animation Node"));1899open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);1900open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened));1901}19021903void EditorAnimationMultiTransitionEdit::add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition) {1904Transition tr;1905tr.from = p_from;1906tr.to = p_to;1907tr.transition = p_transition;1908transitions.push_back(tr);1909}19101911bool EditorAnimationMultiTransitionEdit::_set(const StringName &p_name, const Variant &p_property) {1912int index = String(p_name).get_slicec('/', 0).to_int();1913StringName prop = String(p_name).get_slicec('/', 1);19141915bool found;1916transitions.write[index].transition->set(prop, p_property, &found);1917if (found) {1918return true;1919}19201921return false;1922}19231924bool EditorAnimationMultiTransitionEdit::_get(const StringName &p_name, Variant &r_property) const {1925int index = String(p_name).get_slicec('/', 0).to_int();1926StringName prop = String(p_name).get_slicec('/', 1);19271928if (prop == "transition_path") {1929r_property = String(transitions[index].from) + " -> " + transitions[index].to;1930return true;1931}19321933bool found;1934r_property = transitions[index].transition->get(prop, &found);1935if (found) {1936return true;1937}19381939return false;1940}19411942void EditorAnimationMultiTransitionEdit::_get_property_list(List<PropertyInfo> *p_list) const {1943for (int i = 0; i < transitions.size(); i++) {1944List<PropertyInfo> plist;1945transitions[i].transition->get_property_list(&plist, true);19461947PropertyInfo prop_transition_path;1948prop_transition_path.type = Variant::STRING;1949prop_transition_path.name = itos(i) + "/" + "transition_path";1950p_list->push_back(prop_transition_path);19511952for (const PropertyInfo &pi : plist) {1953if (pi.name == "script" || pi.name == "resource_name" || pi.name == "resource_path" || pi.name == "resource_local_to_scene") {1954continue;1955}19561957if (pi.usage != PROPERTY_USAGE_DEFAULT) {1958continue;1959}19601961PropertyInfo prop = pi;1962prop.name = itos(i) + "/" + prop.name;19631964p_list->push_back(prop);1965}1966}1967}196819691970