Path: blob/master/editor/animation/animation_blend_tree_editor_plugin.cpp
9896 views
/**************************************************************************/1/* animation_blend_tree_editor_plugin.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_blend_tree_editor_plugin.h"3132#include "core/config/project_settings.h"33#include "core/io/resource_loader.h"34#include "editor/editor_node.h"35#include "editor/editor_string_names.h"36#include "editor/editor_undo_redo_manager.h"37#include "editor/gui/editor_file_dialog.h"38#include "editor/inspector/editor_inspector.h"39#include "editor/inspector/editor_properties.h"40#include "editor/settings/editor_settings.h"41#include "editor/themes/editor_scale.h"42#include "scene/3d/skeleton_3d.h"43#include "scene/gui/check_box.h"44#include "scene/gui/grid_container.h"45#include "scene/gui/line_edit.h"46#include "scene/gui/menu_button.h"47#include "scene/gui/option_button.h"48#include "scene/gui/progress_bar.h"49#include "scene/gui/separator.h"50#include "scene/gui/view_panner.h"51#include "scene/main/window.h"5253void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {54for (int i = 0; i < add_options.size(); i++) {55ERR_FAIL_COND(add_options[i].script == p_script);56}5758AddOption ao;59ao.name = p_name;60ao.script = p_script;61add_options.push_back(ao);6263_update_options_menu();64}6566void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {67for (int i = 0; i < add_options.size(); i++) {68if (add_options[i].script == p_script) {69add_options.remove_at(i);70return;71}72}7374_update_options_menu();75}7677void AnimationNodeBlendTreeEditor::_update_options_menu(bool p_has_input_ports) {78add_node->get_popup()->clear();79add_node->get_popup()->reset_size();80for (int i = 0; i < add_options.size(); i++) {81if (p_has_input_ports && add_options[i].input_port_count == 0) {82continue;83}84add_node->get_popup()->add_item(add_options[i].name, i);85}8687Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();88if (clipb.is_valid()) {89add_node->get_popup()->add_separator();90add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);91}92add_node->get_popup()->add_separator();93add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);94use_position_from_popup_menu = false;95}9697Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {98return Size2(10, 200);99}100101void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {102AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();103if (!tree) {104return;105}106updating = true;107EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();108undo_redo->create_action(vformat(TTR("Parameter Changed: %s"), p_property), UndoRedo::MERGE_ENDS);109undo_redo->add_do_property(tree, p_property, p_value);110undo_redo->add_undo_property(tree, p_property, tree->get(p_property));111undo_redo->add_do_method(this, "update_graph");112undo_redo->add_undo_method(this, "update_graph");113undo_redo->commit_action();114updating = false;115}116117void AnimationNodeBlendTreeEditor::update_graph() {118if (updating || blend_tree.is_null()) {119return;120}121122AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();123if (!tree) {124return;125}126127visible_properties.clear();128129graph->set_scroll_offset(blend_tree->get_graph_offset() * EDSCALE);130131graph->clear_connections();132//erase all nodes133for (int i = 0; i < graph->get_child_count(); i++) {134if (Object::cast_to<GraphNode>(graph->get_child(i))) {135memdelete(graph->get_child(i));136i--;137}138}139140animations.clear();141142LocalVector<StringName> nodes = blend_tree->get_node_list();143144for (const StringName &E : nodes) {145GraphNode *node = memnew(GraphNode);146graph->add_child(node);147148node->set_draggable(!read_only);149150Ref<AnimationNode> agnode = blend_tree->get_node(E);151ERR_CONTINUE(agnode.is_null());152153node->set_position_offset(blend_tree->get_node_position(E) * EDSCALE);154155node->set_title(agnode->get_caption());156node->set_name(E);157158int base = 0;159if (E != SceneStringName(output)) {160LineEdit *name = memnew(LineEdit);161name->set_text(E);162name->set_editable(!read_only);163name->set_expand_to_text_length_enabled(true);164name->set_custom_minimum_size(Vector2(100, 0) * EDSCALE);165node->add_child(name);166node->set_slot(0, false, 0, Color(), true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")));167name->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode), CONNECT_DEFERRED);168name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode), CONNECT_DEFERRED);169name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed), CONNECT_DEFERRED);170base = 1;171agnode->set_deletable(true);172173if (!read_only) {174Button *delete_button = memnew(Button);175delete_button->set_flat(true);176delete_button->set_focus_mode(FOCUS_ACCESSIBILITY);177delete_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));178delete_button->set_accessibility_name(TTRC("Delete"));179delete_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_node_request).bind(E), CONNECT_DEFERRED);180node->get_titlebar_hbox()->add_child(delete_button);181}182}183184for (int i = 0; i < agnode->get_input_count(); i++) {185Label *in_name = memnew(Label);186node->add_child(in_name);187in_name->set_text(agnode->get_input_name(i));188node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")), false, 0, Color());189}190191List<PropertyInfo> pinfo;192agnode->get_parameter_list(&pinfo);193for (const PropertyInfo &F : pinfo) {194if (!(F.usage & PROPERTY_USAGE_EDITOR)) {195continue;196}197String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;198EditorProperty *prop = EditorInspector::instantiate_property_editor(tree, F.type, base_path, F.hint, F.hint_string, F.usage);199Vector<String> path = F.name.split("/");200float ratio = 0.0f;201if (prop) {202prop->set_read_only(read_only || (F.usage & PROPERTY_USAGE_READ_ONLY));203prop->set_object_and_property(tree, base_path);204if (path.size() >= 2 && path[0] == "conditions") {205prop->set_draw_label(true);206prop->set_label(path[1]);207ratio = 0.9;208}209prop->set_name_split_ratio(ratio);210prop->update_property();211prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));212213if (F.hint == PROPERTY_HINT_RESOURCE_TYPE) {214// Give the resource editor some more space to make the inside readable.215prop->set_custom_minimum_size(Vector2(180, 0) * EDSCALE);216// Align the size of the node with the resource editor, its un-expanding does not trigger a resize.217prop->connect(SceneStringName(resized), Callable(node, "reset_size"));218}219220node->add_child(prop);221visible_properties.push_back(prop);222}223}224225node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));226227if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {228node->add_child(memnew(HSeparator));229Button *open_in_editor = memnew(Button);230open_in_editor->set_text(TTR("Open Editor"));231open_in_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));232node->add_child(open_in_editor);233open_in_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor).bind(E), CONNECT_DEFERRED);234open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);235}236237if (agnode->has_filter()) {238node->add_child(memnew(HSeparator));239Button *inspect_filters = memnew(Button);240if (read_only) {241inspect_filters->set_text(TTR("Inspect Filters"));242} else {243inspect_filters->set_text(TTR("Edit Filters"));244}245inspect_filters->set_button_icon(get_editor_theme_icon(SNAME("AnimationFilter")));246node->add_child(inspect_filters);247inspect_filters->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_inspect_filters).bind(E), CONNECT_DEFERRED);248inspect_filters->set_h_size_flags(SIZE_SHRINK_CENTER);249}250251Ref<AnimationNodeAnimation> anim = agnode;252if (anim.is_valid()) {253MenuButton *mb = memnew(MenuButton);254mb->set_text(anim->get_animation());255mb->set_button_icon(get_editor_theme_icon(SNAME("Animation")));256mb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);257mb->set_disabled(read_only);258Array options;259260node->add_child(memnew(HSeparator));261node->add_child(mb);262263ProgressBar *pb = memnew(ProgressBar);264265List<StringName> anims;266tree->get_animation_list(&anims);267268for (const StringName &F : anims) {269mb->get_popup()->add_item(F);270options.push_back(F);271}272273pb->set_show_percentage(false);274pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);275animations[E] = pb;276node->add_child(pb);277278mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);279}280281Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), "GraphNode")->duplicate();282if (sb_panel.is_valid()) {283sb_panel->set_content_margin(SIDE_TOP, 12 * EDSCALE);284sb_panel->set_content_margin(SIDE_BOTTOM, 12 * EDSCALE);285node->add_theme_style_override(SceneStringName(panel), sb_panel);286}287288node->add_theme_constant_override("separation", 4 * EDSCALE);289}290291List<AnimationNodeBlendTree::NodeConnection> node_connections;292blend_tree->get_node_connections(&node_connections);293294for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {295StringName from = E.output_node;296StringName to = E.input_node;297int to_idx = E.input_index;298299graph->connect_node(from, 0, to, to_idx);300}301302float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");303graph->set_minimap_opacity(graph_minimap_opacity);304float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");305graph->set_connection_lines_curvature(graph_lines_curvature);306}307308void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {309file_loaded = ResourceLoader::load(p_file);310if (file_loaded.is_valid()) {311_add_node(MENU_LOAD_FILE_CONFIRM);312} else {313EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));314}315}316317void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {318Ref<AnimationNode> anode;319320String base_name;321322if (p_idx == MENU_LOAD_FILE) {323open_file->clear_filters();324List<String> ext_filters;325ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &ext_filters);326for (const String &E : ext_filters) {327open_file->add_filter("*." + E);328}329open_file->popup_file_dialog();330return;331} else if (p_idx == MENU_LOAD_FILE_CONFIRM) {332anode = file_loaded;333file_loaded.unref();334base_name = anode->get_class();335} else if (p_idx == MENU_PASTE) {336anode = EditorSettings::get_singleton()->get_resource_clipboard();337ERR_FAIL_COND(anode.is_null());338base_name = anode->get_class();339} else if (!add_options[p_idx].type.is_empty()) {340AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));341ERR_FAIL_NULL(an);342anode = Ref<AnimationNode>(an);343base_name = add_options[p_idx].name;344} else {345ERR_FAIL_COND(add_options[p_idx].script.is_null());346StringName base_type = add_options[p_idx].script->get_instance_base_type();347AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));348ERR_FAIL_NULL(an);349anode = Ref<AnimationNode>(an);350anode->set_script(add_options[p_idx].script);351base_name = add_options[p_idx].name;352}353354Ref<AnimationNodeOutput> out = anode;355if (out.is_valid()) {356EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));357return;358}359360if (!from_node.is_empty() && anode->get_input_count() == 0) {361from_node = "";362return;363}364365Point2 instance_pos = graph->get_scroll_offset();366if (use_position_from_popup_menu) {367instance_pos += position_from_popup_menu;368} else {369instance_pos += graph->get_size() * 0.5;370}371372instance_pos /= graph->get_zoom();373374int base = 1;375String name = base_name;376while (blend_tree->has_node(name)) {377base++;378name = base_name + " " + itos(base);379}380381EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();382undo_redo->create_action(TTR("Add Node to BlendTree"));383undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);384undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);385386if (!from_node.is_empty()) {387undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);388from_node = "";389}390if (!to_node.is_empty() && to_slot != -1) {391undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);392to_node = "";393to_slot = -1;394}395396undo_redo->add_do_method(this, "update_graph");397undo_redo->add_undo_method(this, "update_graph");398undo_redo->commit_action();399}400401void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_node_position) {402_update_options_menu(p_has_input_ports);403use_position_from_popup_menu = true;404position_from_popup_menu = p_node_position;405add_node->get_popup()->set_position(graph->get_screen_position() + graph->get_local_mouse_position());406add_node->get_popup()->reset_size();407add_node->get_popup()->popup();408}409410void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {411if (read_only) {412return;413}414415_popup(false, p_position);416}417418void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {419if (read_only) {420return;421}422423Ref<AnimationNode> node = blend_tree->get_node(p_from);424if (node.is_valid()) {425from_node = p_from;426_popup(true, p_release_position);427}428}429430void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {431if (read_only) {432return;433}434435Ref<AnimationNode> node = blend_tree->get_node(p_to);436if (node.is_valid()) {437to_node = p_to;438to_slot = p_to_slot;439_popup(false, p_release_position);440}441}442443void AnimationNodeBlendTreeEditor::_popup_hide() {444to_node = "";445to_slot = -1;446}447448void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {449updating = true;450EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();451undo_redo->create_action(TTR("Node Moved"));452undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);453undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);454undo_redo->add_do_method(this, "update_graph");455undo_redo->add_undo_method(this, "update_graph");456undo_redo->commit_action();457updating = false;458}459460void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {461if (read_only) {462return;463}464465AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);466467if (err == AnimationNodeBlendTree::CONNECTION_ERROR_CONNECTION_EXISTS) {468blend_tree->disconnect_node(p_to, p_to_index);469err = blend_tree->can_connect_node(p_to, p_to_index, p_from);470}471472if (err != AnimationNodeBlendTree::CONNECTION_OK) {473EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));474return;475}476477EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();478undo_redo->create_action(TTR("Nodes Connected"));479undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);480undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);481undo_redo->add_do_method(this, "update_graph");482undo_redo->add_undo_method(this, "update_graph");483undo_redo->commit_action();484}485486void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {487if (read_only) {488return;489}490491graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);492493updating = true;494EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();495undo_redo->create_action(TTR("Nodes Disconnected"));496undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);497undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);498undo_redo->add_do_method(this, "update_graph");499undo_redo->add_undo_method(this, "update_graph");500undo_redo->commit_action();501updating = false;502}503504void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, const Array &p_options, const String &p_node) {505String option = p_options[p_index];506507Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);508ERR_FAIL_COND(anim.is_null());509510EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();511undo_redo->create_action(TTR("Set Animation"));512undo_redo->add_do_method(anim.ptr(), "set_animation", option);513undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());514undo_redo->add_do_method(this, "update_graph");515undo_redo->add_undo_method(this, "update_graph");516undo_redo->commit_action();517}518519void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {520if (read_only) {521return;522}523524EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();525undo_redo->create_action(TTR("Delete Node"));526undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);527undo_redo->add_undo_method(blend_tree.ptr(), "add_node", p_which, blend_tree->get_node(p_which), blend_tree.ptr()->get_node_position(p_which));528529List<AnimationNodeBlendTree::NodeConnection> conns;530blend_tree->get_node_connections(&conns);531532for (const AnimationNodeBlendTree::NodeConnection &E : conns) {533if (E.output_node == p_which || E.input_node == p_which) {534undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);535}536}537538undo_redo->add_do_method(this, "update_graph");539undo_redo->add_undo_method(this, "update_graph");540undo_redo->commit_action();541}542543void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {544if (read_only) {545return;546}547548List<StringName> to_erase;549550if (p_nodes.is_empty()) {551for (int i = 0; i < graph->get_child_count(); i++) {552GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));553if (gn && gn->is_selected()) {554Ref<AnimationNode> anode = blend_tree->get_node(gn->get_name());555if (anode->is_deletable()) {556to_erase.push_back(gn->get_name());557}558}559}560} else {561for (int i = 0; i < p_nodes.size(); i++) {562Ref<AnimationNode> anode = blend_tree->get_node(p_nodes[i]);563if (anode->is_deletable()) {564to_erase.push_back(p_nodes[i]);565}566}567}568569if (to_erase.is_empty()) {570return;571}572573EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();574undo_redo->create_action(TTR("Delete Node(s)"));575576for (const StringName &F : to_erase) {577_delete_node_request(F);578}579580undo_redo->commit_action();581}582583void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {584if (read_only) {585return;586}587588GraphNode *gn = Object::cast_to<GraphNode>(p_node);589ERR_FAIL_NULL(gn);590591String name = gn->get_name();592593Ref<AnimationNode> anode = blend_tree->get_node(name);594ERR_FAIL_COND(anode.is_null());595596EditorNode::get_singleton()->push_item(anode.ptr(), "", true);597}598599void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {600Ref<AnimationNode> an = blend_tree->get_node(p_which);601ERR_FAIL_COND(an.is_null());602AnimationTreeEditor::get_singleton()->enter_editor(p_which);603}604605void AnimationNodeBlendTreeEditor::_filter_toggled() {606updating = true;607EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();608undo_redo->create_action(TTR("Toggle Filter On/Off"));609undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());610undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());611undo_redo->add_do_method(this, "_update_filters", _filter_edit);612undo_redo->add_undo_method(this, "_update_filters", _filter_edit);613undo_redo->commit_action();614updating = false;615}616617void AnimationNodeBlendTreeEditor::_filter_edited() {618TreeItem *edited = filters->get_edited();619ERR_FAIL_NULL(edited);620621NodePath edited_path = edited->get_metadata(0);622bool filtered = edited->is_checked(0);623624updating = true;625EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();626undo_redo->create_action(TTR("Change Filter"));627undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);628undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));629undo_redo->add_do_method(this, "_update_filters", _filter_edit);630undo_redo->add_undo_method(this, "_update_filters", _filter_edit);631undo_redo->commit_action();632updating = false;633}634635void AnimationNodeBlendTreeEditor::_filter_fill_selection() {636TreeItem *ti = filters->get_root();637if (!ti) {638return;639}640641updating = true;642EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();643undo_redo->create_action(TTR("Fill Selected Filter Children"));644645_filter_fill_selection_recursive(undo_redo, ti, false);646647undo_redo->add_do_method(this, "_update_filters", _filter_edit);648undo_redo->add_undo_method(this, "_update_filters", _filter_edit);649undo_redo->commit_action();650updating = false;651652_update_filters(_filter_edit);653}654655void AnimationNodeBlendTreeEditor::_filter_invert_selection() {656TreeItem *ti = filters->get_root();657if (!ti) {658return;659}660661updating = true;662EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();663undo_redo->create_action(TTR("Invert Filter Selection"));664665_filter_invert_selection_recursive(undo_redo, ti);666667undo_redo->add_do_method(this, "_update_filters", _filter_edit);668undo_redo->add_undo_method(this, "_update_filters", _filter_edit);669undo_redo->commit_action();670updating = false;671672_update_filters(_filter_edit);673}674675void AnimationNodeBlendTreeEditor::_filter_clear_selection() {676TreeItem *ti = filters->get_root();677if (!ti) {678return;679}680681updating = true;682EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();683undo_redo->create_action(TTR("Clear Filter Selection"));684685_filter_clear_selection_recursive(undo_redo, ti);686687undo_redo->add_do_method(this, "_update_filters", _filter_edit);688undo_redo->add_undo_method(this, "_update_filters", _filter_edit);689undo_redo->commit_action();690updating = false;691692_update_filters(_filter_edit);693}694695void AnimationNodeBlendTreeEditor::_filter_fill_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item, bool p_parent_filtered) {696TreeItem *ti = p_item->get_first_child();697bool parent_filtered = p_parent_filtered;698while (ti) {699NodePath item_path = ti->get_metadata(0);700bool filtered = _filter_edit->is_path_filtered(item_path);701parent_filtered |= filtered;702703p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, parent_filtered);704p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);705706_filter_fill_selection_recursive(p_undo_redo, ti, parent_filtered);707ti = ti->get_next();708parent_filtered = p_parent_filtered;709}710}711712void AnimationNodeBlendTreeEditor::_filter_invert_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {713TreeItem *ti = p_item->get_first_child();714while (ti) {715NodePath item_path = ti->get_metadata(0);716bool filtered = _filter_edit->is_path_filtered(item_path);717718p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, !filtered);719p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);720721_filter_invert_selection_recursive(p_undo_redo, ti);722ti = ti->get_next();723}724}725726void AnimationNodeBlendTreeEditor::_filter_clear_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {727TreeItem *ti = p_item->get_first_child();728while (ti) {729NodePath item_path = ti->get_metadata(0);730bool filtered = _filter_edit->is_path_filtered(item_path);731732p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, false);733p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);734735_filter_clear_selection_recursive(p_undo_redo, ti);736ti = ti->get_next();737}738}739740bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {741if (updating || _filter_edit != anode) {742return false;743}744745AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();746if (!tree) {747return false;748}749750Node *base = tree->get_node(tree->get_root_node());751if (!base) {752EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));753return false;754}755756updating = true;757758HashSet<String> paths;759HashMap<String, RBSet<String>> types;760{761List<StringName> animation_list;762tree->get_animation_list(&animation_list);763764for (const StringName &E : animation_list) {765Ref<Animation> anim = tree->get_animation(E);766for (int i = 0; i < anim->get_track_count(); i++) {767String track_path = String(anim->track_get_path(i));768paths.insert(track_path);769770String track_type_name;771Animation::TrackType track_type = anim->track_get_type(i);772switch (track_type) {773case Animation::TrackType::TYPE_ANIMATION: {774track_type_name = TTR("Anim Clips");775} break;776case Animation::TrackType::TYPE_AUDIO: {777track_type_name = TTR("Audio Clips");778} break;779case Animation::TrackType::TYPE_METHOD: {780track_type_name = TTR("Functions");781} break;782default: {783} break;784}785if (!track_type_name.is_empty()) {786types[track_path].insert(track_type_name);787}788}789}790}791792filter_enabled->set_pressed(anode->is_filter_enabled());793filters->clear();794TreeItem *root = filters->create_item();795796HashMap<String, TreeItem *> parenthood;797798for (const String &E : paths) {799NodePath path = E;800TreeItem *ti = nullptr;801String accum;802for (int i = 0; i < path.get_name_count(); i++) {803String name = path.get_name(i);804if (!accum.is_empty()) {805accum += "/";806}807accum += name;808if (!parenthood.has(accum)) {809if (ti) {810ti = filters->create_item(ti);811} else {812ti = filters->create_item(root);813}814parenthood[accum] = ti;815ti->set_text(0, name);816ti->set_selectable(0, false);817ti->set_editable(0, false);818819if (base->has_node(accum)) {820Node *node = base->get_node(accum);821ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));822}823824} else {825ti = parenthood[accum];826}827}828829Node *node = nullptr;830if (base->has_node(accum)) {831node = base->get_node(accum);832}833if (!node) {834continue; //no node, can't edit835}836837if (path.get_subname_count()) {838String concat = path.get_concatenated_subnames();839840Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);841if (skeleton && skeleton->find_bone(concat) != -1) {842//path in skeleton843const String &bone = concat;844int idx = skeleton->find_bone(bone);845List<String> bone_path;846while (idx != -1) {847bone_path.push_front(skeleton->get_bone_name(idx));848idx = skeleton->get_bone_parent(idx);849}850851accum += ":";852for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {853if (F != bone_path.front()) {854accum += "/";855}856857accum += F->get();858if (!parenthood.has(accum)) {859ti = filters->create_item(ti);860parenthood[accum] = ti;861ti->set_text(0, F->get());862ti->set_selectable(0, false);863ti->set_editable(0, false);864ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));865} else {866ti = parenthood[accum];867}868}869870ti->set_editable(0, !read_only);871ti->set_selectable(0, true);872ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);873ti->set_text(0, concat);874ti->set_checked(0, anode->is_path_filtered(path));875ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));876ti->set_metadata(0, path);877878} else {879//just a property880ti = filters->create_item(ti);881ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);882ti->set_text(0, concat);883ti->set_editable(0, !read_only);884ti->set_selectable(0, true);885ti->set_checked(0, anode->is_path_filtered(path));886ti->set_metadata(0, path);887}888} else {889if (ti) {890//just a node, not a property track891String types_text = "[";892if (types.has(String(path))) {893RBSet<String>::Iterator F = types[String(path)].begin();894types_text += *F;895while (F) {896types_text += " / " + *F;897;898++F;899}900}901types_text += "]";902ti = filters->create_item(ti);903ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);904ti->set_text(0, types_text);905ti->set_editable(0, !read_only);906ti->set_selectable(0, true);907ti->set_checked(0, anode->is_path_filtered(path));908ti->set_metadata(0, path);909}910}911}912913updating = false;914915return true;916}917918void AnimationNodeBlendTreeEditor::_inspect_filters(const String &p_which) {919if (read_only) {920filter_dialog->set_title(TTR("Inspect Filtered Tracks:"));921} else {922filter_dialog->set_title(TTR("Edit Filtered Tracks:"));923}924925filter_enabled->set_disabled(read_only);926927Ref<AnimationNode> anode = blend_tree->get_node(p_which);928ERR_FAIL_COND(anode.is_null());929930_filter_edit = anode;931if (!_update_filters(anode)) {932return;933}934935filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);936}937938void AnimationNodeBlendTreeEditor::_update_editor_settings() {939graph->get_panner()->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/sub_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));940graph->set_warped_panning(EDITOR_GET("editors/panning/warped_mouse_panning"));941}942943void AnimationNodeBlendTreeEditor::_notification(int p_what) {944switch (p_what) {945case NOTIFICATION_ENTER_TREE: {946_update_editor_settings();947} break;948949case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {950if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {951_update_editor_settings();952}953} break;954955case NOTIFICATION_THEME_CHANGED: {956error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));957error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));958959if (is_visible_in_tree()) {960update_graph();961}962} break;963964case NOTIFICATION_PROCESS: {965AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();966if (!tree) {967return; // Node has been changed.968}969970String error;971972if (!tree->is_active()) {973error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");974} else if (tree->is_state_invalid()) {975error = tree->get_invalid_state_reason();976}977978if (error != error_label->get_text()) {979error_label->set_text(error);980if (!error.is_empty()) {981error_panel->show();982} else {983error_panel->hide();984}985}986987List<AnimationNodeBlendTree::NodeConnection> conns;988blend_tree->get_node_connections(&conns);989for (const AnimationNodeBlendTree::NodeConnection &E : conns) {990float activity = 0;991StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;992if (!tree->is_state_invalid()) {993activity = tree->get_connection_activity(path, E.input_index);994}995graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);996}997998for (const KeyValue<StringName, ProgressBar *> &E : animations) {999Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);1000if (an.is_valid()) {1001if (tree->has_animation(an->get_animation())) {1002Ref<Animation> anim = tree->get_animation(an->get_animation());1003if (anim.is_valid()) {1004//StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;1005StringName length_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_length";1006StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_position";1007E.value->set_max(tree->get(length_path));1008E.value->set_value(tree->get(time_path));1009}1010}1011}1012}10131014for (int i = 0; i < visible_properties.size(); i++) {1015visible_properties[i]->update_property();1016}1017} break;10181019case NOTIFICATION_VISIBILITY_CHANGED: {1020set_process(is_visible_in_tree());1021} break;1022}1023}10241025void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {1026if (read_only) {1027return;1028}10291030if (updating) {1031return;1032}10331034if (blend_tree.is_null()) {1035return;1036}10371038updating = true;1039blend_tree->set_graph_offset(p_scroll / EDSCALE);1040updating = false;1041}10421043void AnimationNodeBlendTreeEditor::_bind_methods() {1044ClassDB::bind_method("update_graph", &AnimationNodeBlendTreeEditor::update_graph);1045ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);1046}10471048AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;10491050// AnimationNode's "node_changed" signal means almost update_input.1051void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name) {1052// TODO:1053// Here is executed during the commit of EditorNode::undo_redo, it is not possible to create an undo_redo action here.1054// The disconnect when the number of enabled inputs decreases is done in AnimationNodeBlendTree and update_graph().1055// This means that there is no place to register undo_redo actions.1056// In order to implement undo_redo correctly, we may need to implement AnimationNodeEdit such as AnimationTrackKeyEdit1057// and add it to _node_selected() with EditorNode::get_singleton()->push_item(AnimationNodeEdit).1058update_graph();1059}10601061void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {1062if (blend_tree.is_null()) {1063return;1064}10651066AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1067if (!tree) {1068return;1069}10701071String prev_name = blend_tree->get_node_name(p_node);1072ERR_FAIL_COND(prev_name.is_empty());1073GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));1074ERR_FAIL_NULL(gn);10751076const String &new_name = p_text;10771078ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/'));10791080if (new_name == prev_name) {1081return; //nothing to do1082}10831084const String &base_name = new_name;1085int base = 1;1086String name = base_name;1087while (blend_tree->has_node(name)) {1088base++;1089name = base_name + " " + itos(base);1090}10911092String base_path = AnimationTreeEditor::get_singleton()->get_base_path();10931094updating = true;1095EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1096undo_redo->create_action(TTR("Node Renamed"));1097undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);1098undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);1099undo_redo->add_do_method(this, "update_graph");1100undo_redo->add_undo_method(this, "update_graph");1101undo_redo->commit_action();1102updating = false;1103gn->set_name(new_name);1104gn->set_size(gn->get_minimum_size());11051106//change editors accordingly1107for (int i = 0; i < visible_properties.size(); i++) {1108String pname = visible_properties[i]->get_edited_property().operator String();1109if (pname.begins_with(base_path + prev_name)) {1110String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);1111visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);1112}1113}11141115//recreate connections1116graph->clear_connections();11171118List<AnimationNodeBlendTree::NodeConnection> node_connections;1119blend_tree->get_node_connections(&node_connections);11201121for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {1122StringName from = E.output_node;1123StringName to = E.input_node;1124int to_idx = E.input_index;11251126graph->connect_node(from, 0, to, to_idx);1127}11281129//update animations1130for (const KeyValue<StringName, ProgressBar *> &E : animations) {1131if (E.key == prev_name) {1132animations[new_name] = animations[prev_name];1133animations.erase(prev_name);1134break;1135}1136}11371138update_graph(); // Needed to update the signal connections with the new name.1139current_node_rename_text = String();1140}11411142void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {1143if (current_node_rename_text.is_empty()) {1144return; // The text_submitted signal triggered the graph update and freed the LineEdit.1145}1146_node_renamed(current_node_rename_text, p_node);1147}11481149void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {1150current_node_rename_text = p_text;1151}11521153bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {1154Ref<AnimationNodeBlendTree> bt = p_node;1155return bt.is_valid();1156}11571158void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {1159if (blend_tree.is_valid()) {1160blend_tree->disconnect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));1161}11621163blend_tree = p_node;11641165read_only = false;11661167if (blend_tree.is_null()) {1168hide();1169} else {1170read_only = EditorNode::get_singleton()->is_resource_read_only(blend_tree);11711172blend_tree->connect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));11731174update_graph();1175}11761177add_node->set_disabled(read_only);1178graph->set_show_arrange_button(!read_only);1179}11801181AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {1182singleton = this;1183updating = false;1184use_position_from_popup_menu = false;11851186graph = memnew(GraphEdit);1187add_child(graph);1188graph->add_valid_right_disconnect_type(0);1189graph->add_valid_left_disconnect_type(0);1190graph->set_v_size_flags(SIZE_EXPAND_FILL);1191graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);1192graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);1193graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));1194graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));1195graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));1196graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));1197graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));1198graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));1199float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");1200graph->set_minimap_opacity(graph_minimap_opacity);1201float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");1202graph->set_connection_lines_curvature(graph_lines_curvature);12031204VSeparator *vs = memnew(VSeparator);1205graph->get_menu_hbox()->add_child(vs);1206graph->get_menu_hbox()->move_child(vs, 0);12071208add_node = memnew(MenuButton);1209graph->get_menu_hbox()->add_child(add_node);1210add_node->set_text(TTR("Add Node..."));1211graph->get_menu_hbox()->move_child(add_node, 0);1212add_node->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));1213add_node->get_popup()->connect("popup_hide", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_hide), CONNECT_DEFERRED);1214add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu).bind(false));1215add_node->set_disabled(read_only);12161217add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));1218add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));1219add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));1220add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));1221add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));1222add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));1223add_options.push_back(AddOption("Sub2", "AnimationNodeSub2", 2));1224add_options.push_back(AddOption("TimeSeek", "AnimationNodeTimeSeek", 1));1225add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));1226add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));1227add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));1228add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));1229add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));1230add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));1231_update_options_menu();12321233error_panel = memnew(PanelContainer);1234add_child(error_panel);1235error_label = memnew(Label);1236error_label->set_focus_mode(FOCUS_ACCESSIBILITY);1237error_panel->add_child(error_label);1238error_label->set_text("eh");12391240filter_dialog = memnew(AcceptDialog);1241add_child(filter_dialog);1242filter_dialog->set_title(TTR("Edit Filtered Tracks:"));12431244VBoxContainer *filter_vbox = memnew(VBoxContainer);1245filter_dialog->add_child(filter_vbox);12461247HBoxContainer *filter_hbox = memnew(HBoxContainer);1248filter_vbox->add_child(filter_hbox);12491250filter_enabled = memnew(CheckBox);1251filter_enabled->set_text(TTR("Enable Filtering"));1252filter_enabled->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));1253filter_hbox->add_child(filter_enabled);12541255filter_fill_selection = memnew(Button);1256filter_fill_selection->set_text(TTR("Fill Selected Children"));1257filter_fill_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_fill_selection));1258filter_hbox->add_child(filter_fill_selection);12591260filter_invert_selection = memnew(Button);1261filter_invert_selection->set_text(TTR("Invert"));1262filter_invert_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_invert_selection));1263filter_hbox->add_child(filter_invert_selection);12641265filter_clear_selection = memnew(Button);1266filter_clear_selection->set_text(TTR("Clear"));1267filter_clear_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_clear_selection));1268filter_hbox->add_child(filter_clear_selection);12691270filters = memnew(Tree);1271filter_vbox->add_child(filters);1272filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1273filters->set_v_size_flags(SIZE_EXPAND_FILL);1274filters->set_hide_root(true);1275filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));12761277open_file = memnew(EditorFileDialog);1278add_child(open_file);1279open_file->set_title(TTR("Open Animation Node"));1280open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);1281open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));12821283animation_node_inspector_plugin.instantiate();1284EditorInspector::add_inspector_plugin(animation_node_inspector_plugin);1285}12861287// EditorPluginAnimationNodeAnimation12881289void AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog() {1290AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();1291StringName anim_name = animation_node_animation->get_animation();1292PackedStringArray markers = tree->has_animation(anim_name) ? tree->get_animation(anim_name)->get_marker_names() : PackedStringArray();12931294dialog->select_start->clear();1295dialog->select_start->add_icon_item(get_editor_theme_icon(SNAME("PlayStart")), TTR("Start of Animation"));1296dialog->select_start->add_separator();1297dialog->select_end->clear();1298dialog->select_end->add_icon_item(get_editor_theme_icon(SNAME("PlayStartBackwards")), TTR("End of Animation"));1299dialog->select_end->add_separator();13001301for (const String &marker : markers) {1302dialog->select_start->add_item(marker);1303dialog->select_end->add_item(marker);1304}13051306// Because the default selections are always valid, and marker times won't change during the dialog, we can ensure that the user can only select valid markers.1307// This invariant is maintained by _validate_markers.1308dialog->select_start->select(0);1309dialog->select_end->select(0);13101311dialog->popup_centered(Size2(200, 0) * EDSCALE);1312}13131314void AnimationNodeAnimationEditor::_validate_markers(int p_id) {1315// Note: p_id is ignored. It is included because OptionButton's item_changed signal always passes it.1316int start_id = dialog->select_start->get_selected_id();1317int end_id = dialog->select_end->get_selected_id();13181319StringName anim_name = animation_node_animation->get_animation();1320Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(anim_name);1321ERR_FAIL_COND(animation.is_null());13221323double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));1324double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));13251326// p_start and p_end have the same item count.1327for (int i = 2; i < dialog->select_start->get_item_count(); i++) {1328String start_marker = dialog->select_start->get_item_text(i);1329String end_marker = dialog->select_end->get_item_text(i);1330dialog->select_start->set_item_disabled(i, end_id >= 2 && (i == end_id || animation->get_marker_time(start_marker) > end_time));1331dialog->select_end->set_item_disabled(i, start_id >= 2 && (i == start_id || start_time > animation->get_marker_time(end_marker)));1332}1333}13341335void AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog() {1336int start_id = dialog->select_start->get_selected_id();1337int end_id = dialog->select_end->get_selected_id();13381339Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(animation_node_animation->get_animation());1340ERR_FAIL_COND(animation.is_null());1341double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));1342double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));1343double length = end_time - start_time;13441345EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();1346undo_redo->create_action(TTR("Set Custom Timeline from Marker"));1347undo_redo->add_do_method(*animation_node_animation, "set_start_offset", start_time);1348undo_redo->add_undo_method(*animation_node_animation, "set_start_offset", animation_node_animation->get_start_offset());1349undo_redo->add_do_method(*animation_node_animation, "set_stretch_time_scale", false);1350undo_redo->add_undo_method(*animation_node_animation, "set_stretch_time_scale", animation_node_animation->is_stretching_time_scale());1351undo_redo->add_do_method(*animation_node_animation, "set_timeline_length", length);1352undo_redo->add_undo_method(*animation_node_animation, "set_timeline_length", animation_node_animation->get_timeline_length());1353undo_redo->add_do_method(*animation_node_animation, "notify_property_list_changed");1354undo_redo->add_undo_method(*animation_node_animation, "notify_property_list_changed");1355undo_redo->commit_action();1356}13571358AnimationNodeAnimationEditor::AnimationNodeAnimationEditor(Ref<AnimationNodeAnimation> p_animation_node_animation) {1359animation_node_animation = p_animation_node_animation;13601361dialog = memnew(AnimationNodeAnimationEditorDialog);1362add_child(dialog);1363dialog->set_hide_on_ok(false);1364dialog->select_start->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));1365dialog->select_end->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));1366dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog));13671368Control *top_spacer = memnew(Control);1369add_child(top_spacer);1370top_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);13711372button = memnew(Button);1373add_child(button);1374button->set_text(TTR("Set Custom Timeline from Marker"));1375button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);1376button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog));13771378Control *bottom_spacer = memnew(Control);1379add_child(bottom_spacer);1380bottom_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);1381}13821383void AnimationNodeAnimationEditor::_notification(int p_what) {1384switch (p_what) {1385case NOTIFICATION_THEME_CHANGED: {1386button->set_theme_type_variation(SNAME("InspectorActionButton"));1387button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));1388} break;1389}1390}13911392bool EditorInspectorPluginAnimationNodeAnimation::can_handle(Object *p_object) {1393Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));1394return ana.is_valid() && ana->is_using_custom_timeline();1395}13961397bool EditorInspectorPluginAnimationNodeAnimation::parse_property(Object *p_object, const Variant::Type p_type, const String &p_path, const PropertyHint p_hint, const String &p_hint_text, const BitField<PropertyUsageFlags> p_usage, const bool p_wide) {1398Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));1399ERR_FAIL_COND_V(ana.is_null(), false);14001401if (p_path == "timeline_length") {1402add_custom_control(memnew(AnimationNodeAnimationEditor(ana)));1403}14041405return false;1406}14071408AnimationNodeAnimationEditorDialog::AnimationNodeAnimationEditorDialog() {1409set_title(TTR("Select Markers"));14101411GridContainer *grid = memnew(GridContainer);1412grid->set_columns(2);1413grid->set_offsets_preset(Control::PRESET_FULL_RECT);1414add_child(grid);14151416Label *label_start = memnew(Label(TTR("Start Marker")));1417grid->add_child(label_start);1418label_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);1419label_start->set_stretch_ratio(1);1420select_start = memnew(OptionButton);1421select_start->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1422select_start->set_accessibility_name(TTRC("Start Marker"));1423grid->add_child(select_start);1424select_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);1425select_start->set_stretch_ratio(2);14261427Label *label_end = memnew(Label(TTR("End Marker")));1428grid->add_child(label_end);1429label_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);1430label_end->set_stretch_ratio(1);1431select_end = memnew(OptionButton);1432select_end->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);1433select_end->set_accessibility_name(TTRC("End Marker"));1434grid->add_child(select_end);1435select_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);1436select_end->set_stretch_ratio(2);1437}143814391440