Debian Codegraph @

editor/animation/animation_blend_tree_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* animation_blend_tree_editor_plugin.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "animation_blend_tree_editor_plugin.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/io/resource_loader.h"
35 #include "editor/editor_node.h"
36 #include "editor/editor_string_names.h"
37 #include "editor/editor_undo_redo_manager.h"
38 #include "editor/gui/editor_file_dialog.h"
39 #include "editor/inspector/editor_inspector.h"
40 #include "editor/inspector/editor_properties.h"
41 #include "editor/settings/editor_settings.h"
42 #include "editor/themes/editor_scale.h"
43 #include "scene/3d/skeleton_3d.h"
44 #include "scene/gui/check_box.h"
45 #include "scene/gui/grid_container.h"
46 #include "scene/gui/line_edit.h"
47 #include "scene/gui/menu_button.h"
48 #include "scene/gui/option_button.h"
49 #include "scene/gui/progress_bar.h"
50 #include "scene/gui/separator.h"
51 #include "scene/gui/view_panner.h"
52 #include "scene/main/window.h"
53
54 void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {
55 for (int i = 0; i < add_options.size(); i++) {
56 ERR_FAIL_COND(add_options[i].script == p_script);
57 }
58
59 AddOption ao;
60 ao.name = p_name;
61 ao.script = p_script;
62 add_options.push_back(ao);
63
64 _update_options_menu();
65 }
66
67 void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {
68 for (int i = 0; i < add_options.size(); i++) {
69 if (add_options[i].script == p_script) {
70 add_options.remove_at(i);
71 return;
72 }
73 }
74
75 _update_options_menu();
76 }
77
78 void AnimationNodeBlendTreeEditor::_update_options_menu(bool p_has_input_ports) {
79 add_node->get_popup()->clear();
80 add_node->get_popup()->reset_size();
81 for (int i = 0; i < add_options.size(); i++) {
82 if (p_has_input_ports && add_options[i].input_port_count == 0) {
83 continue;
84 }
85 add_node->get_popup()->add_item(add_options[i].name, i);
86 }
87
88 Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
89 if (clipb.is_valid()) {
90 add_node->get_popup()->add_separator();
91 add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);
92 }
93 add_node->get_popup()->add_separator();
94 add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);
95 use_position_from_popup_menu = false;
96 }
97
98 Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {
99 return Size2(10, 200);
100 }
101
102 void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
103 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
104 if (!tree) {
105 return;
106 }
107 updating = true;
108 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
109 undo_redo->create_action(vformat(TTR("Parameter Changed: %s"), p_property), UndoRedo::MERGE_ENDS);
110 undo_redo->add_do_property(tree, p_property, p_value);
111 undo_redo->add_undo_property(tree, p_property, tree->get(p_property));
112 undo_redo->add_do_method(this, "update_graph");
113 undo_redo->add_undo_method(this, "update_graph");
114 undo_redo->commit_action();
115 updating = false;
116 }
117
118 void AnimationNodeBlendTreeEditor::update_graph() {
119 if (updating || blend_tree.is_null()) {
120 return;
121 }
122
123 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
124 if (!tree) {
125 return;
126 }
127
128 visible_properties.clear();
129
130 // Store selected nodes before clearing the graph.
131 List<StringName> selected_nodes;
132 for (int i = 0; i < graph->get_child_count(); i++) {
133 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
134 if (gn && gn->is_selected()) {
135 selected_nodes.push_back(gn->get_name());
136 }
137 }
138
139 graph->set_scroll_offset(blend_tree->get_graph_offset() * EDSCALE);
140
141 graph->clear_connections();
142 //erase all nodes
143 for (int i = 0; i < graph->get_child_count(); i++) {
144 if (Object::cast_to<GraphNode>(graph->get_child(i))) {
145 memdelete(graph->get_child(i));
146 i--;
147 }
148 }
149
150 animations.clear();
151
152 LocalVector<StringName> nodes = blend_tree->get_node_list();
153
154 for (const StringName &E : nodes) {
155 GraphNode *node = memnew(GraphNode);
156 graph->add_child(node);
157
158 node->set_draggable(!read_only);
159
160 Ref<AnimationNode> agnode = blend_tree->get_node(E);
161 ERR_CONTINUE(agnode.is_null());
162
163 node->set_position_offset(blend_tree->get_node_position(E) * EDSCALE);
164
165 node->set_title(agnode->get_caption());
166 node->set_name(E);
167
168 int base = 0;
169 if (E != SceneStringName(output)) {
170 LineEdit *name = memnew(LineEdit);
171 name->set_text(E);
172 name->set_editable(!read_only);
173 name->set_expand_to_text_length_enabled(true);
174 name->set_custom_minimum_size(Vector2(100, 0) * EDSCALE);
175 node->add_child(name);
176 node->set_slot(0, false, 0, Color(), true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")));
177 name->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed).bind(agnode), CONNECT_DEFERRED);
178 name->connect(SceneStringName(focus_exited), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_renamed_focus_out).bind(agnode), CONNECT_DEFERRED);
179 name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed), CONNECT_DEFERRED);
180 base = 1;
181 agnode->set_deletable(true);
182
183 if (!read_only) {
184 Button *delete_button = memnew(Button);
185 delete_button->set_flat(true);
186 delete_button->set_focus_mode(FOCUS_ACCESSIBILITY);
187 delete_button->set_button_icon(get_editor_theme_icon(SNAME("Close")));
188 delete_button->set_accessibility_name(TTRC("Delete"));
189 delete_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_node_request).bind(E), CONNECT_DEFERRED);
190 node->get_titlebar_hbox()->add_child(delete_button);
191 }
192 }
193
194 for (int i = 0; i < agnode->get_input_count(); i++) {
195 Label *in_name = memnew(Label);
196 node->add_child(in_name);
197 in_name->set_text(agnode->get_input_name(i));
198 node->set_slot(base + i, true, read_only ? -1 : 0, get_theme_color(SceneStringName(font_color), SNAME("Label")), false, 0, Color());
199 }
200
201 List<PropertyInfo> pinfo;
202 agnode->get_parameter_list(&pinfo);
203 for (const PropertyInfo &F : pinfo) {
204 if (!(F.usage & PROPERTY_USAGE_EDITOR)) {
205 continue;
206 }
207 String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E) + "/" + F.name;
208 EditorProperty *prop = EditorInspector::instantiate_property_editor(tree, F.type, base_path, F.hint, F.hint_string, F.usage);
209 Vector<String> path = F.name.split("/");
210 float ratio = 0.0f;
211 if (prop) {
212 prop->set_read_only(read_only || (F.usage & PROPERTY_USAGE_READ_ONLY));
213 prop->set_object_and_property(tree, base_path);
214 if (path.size() >= 2 && path[0] == "conditions") {
215 prop->set_draw_label(true);
216 prop->set_label(path[1]);
217 ratio = 0.9;
218 }
219 prop->set_name_split_ratio(ratio);
220 prop->update_property();
221 prop->connect("property_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_property_changed));
222
223 if (F.hint == PROPERTY_HINT_RESOURCE_TYPE) {
224 // Give the resource editor some more space to make the inside readable.
225 prop->set_custom_minimum_size(Vector2(180, 0) * EDSCALE);
226 // Align the size of the node with the resource editor, its un-expanding does not trigger a resize.
227 prop->connect(SceneStringName(resized), Callable(node, "reset_size"));
228 }
229
230 node->add_child(prop);
231 visible_properties.push_back(prop);
232 }
233 }
234
235 node->connect("dragged", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_dragged).bind(E));
236
237 if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
238 node->add_child(memnew(HSeparator));
239 Button *open_in_editor = memnew(Button);
240 open_in_editor->set_text(TTR("Open Editor"));
241 open_in_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
242 node->add_child(open_in_editor);
243 open_in_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_open_in_editor).bind(E), CONNECT_DEFERRED);
244 open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
245 }
246
247 if (agnode->has_filter()) {
248 node->add_child(memnew(HSeparator));
249 Button *inspect_filters = memnew(Button);
250 if (read_only) {
251 inspect_filters->set_text(TTR("Inspect Filters"));
252 } else {
253 inspect_filters->set_text(TTR("Edit Filters"));
254 }
255 inspect_filters->set_button_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
256 node->add_child(inspect_filters);
257 inspect_filters->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_inspect_filters).bind(E), CONNECT_DEFERRED);
258 inspect_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
259 }
260
261 Ref<AnimationNodeAnimation> anim = agnode;
262 if (anim.is_valid()) {
263 MenuButton *mb = memnew(MenuButton);
264 mb->set_text(anim->get_animation());
265 mb->set_button_icon(get_editor_theme_icon(SNAME("Animation")));
266 mb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
267 mb->set_disabled(read_only);
268 Array options;
269
270 node->add_child(memnew(HSeparator));
271 node->add_child(mb);
272
273 ProgressBar *pb = memnew(ProgressBar);
274
275 List<StringName> anims;
276 tree->get_animation_list(&anims);
277
278 for (const StringName &F : anims) {
279 mb->get_popup()->add_item(F);
280 options.push_back(F);
281 }
282
283 pb->set_show_percentage(false);
284 pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
285 animations[E] = pb;
286 node->add_child(pb);
287
288 mb->get_popup()->connect("index_pressed", callable_mp(this, &AnimationNodeBlendTreeEditor::_anim_selected).bind(options, E), CONNECT_DEFERRED);
289 }
290
291 Ref<StyleBox> sb_panel = node->get_theme_stylebox(SceneStringName(panel), "GraphNode")->duplicate();
292 if (sb_panel.is_valid()) {
293 sb_panel->set_content_margin(SIDE_TOP, 12 * EDSCALE);
294 sb_panel->set_content_margin(SIDE_BOTTOM, 12 * EDSCALE);
295 node->add_theme_style_override(SceneStringName(panel), sb_panel);
296 }
297
298 node->add_theme_constant_override("separation", 4 * EDSCALE);
299 }
300
301 List<AnimationNodeBlendTree::NodeConnection> node_connections;
302 blend_tree->get_node_connections(&node_connections);
303
304 for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
305 StringName from = E.output_node;
306 StringName to = E.input_node;
307 int to_idx = E.input_index;
308
309 graph->connect_node(from, 0, to, to_idx);
310 }
311
312 float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
313 graph->set_minimap_opacity(graph_minimap_opacity);
314 float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
315 graph->set_connection_lines_curvature(graph_lines_curvature);
316
317 // Restore selected nodes after graph reconstruction.
318 for (const StringName &name : selected_nodes) {
319 for (int i = 0; i < graph->get_child_count(); i++) {
320 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
321 if (gn && gn->get_name() == name) {
322 gn->set_selected(true);
323 break;
324 }
325 }
326 }
327 }
328
329 void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
330 file_loaded = ResourceLoader::load(p_file);
331 if (file_loaded.is_valid()) {
332 _add_node(MENU_LOAD_FILE_CONFIRM);
333 } else {
334 EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
335 }
336 }
337
338 void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
339 Ref<AnimationNode> anode;
340
341 String base_name;
342
343 if (p_idx == MENU_LOAD_FILE) {
344 open_file->clear_filters();
345 List<String> ext_filters;
346 ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &ext_filters);
347 for (const String &E : ext_filters) {
348 open_file->add_filter("*." + E);
349 }
350 open_file->popup_file_dialog();
351 return;
352 } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
353 anode = file_loaded;
354 file_loaded.unref();
355 base_name = anode->get_class();
356 } else if (p_idx == MENU_PASTE) {
357 anode = EditorSettings::get_singleton()->get_resource_clipboard();
358 ERR_FAIL_COND(anode.is_null());
359 base_name = anode->get_class();
360 } else if (!add_options[p_idx].type.is_empty()) {
361 AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(add_options[p_idx].type));
362 ERR_FAIL_NULL(an);
363 anode = Ref<AnimationNode>(an);
364 base_name = add_options[p_idx].name;
365 } else {
366 ERR_FAIL_COND(add_options[p_idx].script.is_null());
367 StringName base_type = add_options[p_idx].script->get_instance_base_type();
368 AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instantiate(base_type));
369 ERR_FAIL_NULL(an);
370 anode = Ref<AnimationNode>(an);
371 anode->set_script(add_options[p_idx].script);
372 base_name = add_options[p_idx].name;
373 }
374
375 Ref<AnimationNodeOutput> out = anode;
376 if (out.is_valid()) {
377 EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
378 return;
379 }
380
381 if (!from_node.is_empty() && anode->get_input_count() == 0) {
382 from_node = "";
383 return;
384 }
385
386 Point2 instance_pos = graph->get_scroll_offset();
387 if (use_position_from_popup_menu) {
388 instance_pos += position_from_popup_menu;
389 } else {
390 instance_pos += graph->get_size() * 0.5;
391 }
392
393 instance_pos /= graph->get_zoom();
394
395 int base = 1;
396 String name = base_name;
397 while (blend_tree->has_node(name)) {
398 base++;
399 name = base_name + " " + itos(base);
400 }
401
402 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
403 undo_redo->create_action(TTR("Add Node to BlendTree"));
404 undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
405 undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
406
407 if (!from_node.is_empty()) {
408 undo_redo->add_do_method(blend_tree.ptr(), "connect_node", name, 0, from_node);
409 from_node = "";
410 }
411 if (!to_node.is_empty() && to_slot != -1) {
412 undo_redo->add_do_method(blend_tree.ptr(), "connect_node", to_node, to_slot, name);
413 to_node = "";
414 to_slot = -1;
415 }
416
417 undo_redo->add_do_method(this, "update_graph");
418 undo_redo->add_undo_method(this, "update_graph");
419 undo_redo->commit_action();
420 }
421
422 void AnimationNodeBlendTreeEditor::_popup(bool p_has_input_ports, const Vector2 &p_node_position) {
423 _update_options_menu(p_has_input_ports);
424 use_position_from_popup_menu = true;
425 position_from_popup_menu = p_node_position;
426 add_node->get_popup()->set_position(graph->get_screen_position() + graph->get_local_mouse_position());
427 add_node->get_popup()->reset_size();
428 add_node->get_popup()->popup();
429 }
430
431 void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
432 if (read_only) {
433 return;
434 }
435
436 _popup(false, p_position);
437 }
438
439 void AnimationNodeBlendTreeEditor::_connection_to_empty(const String &p_from, int p_from_slot, const Vector2 &p_release_position) {
440 if (read_only) {
441 return;
442 }
443
444 Ref<AnimationNode> node = blend_tree->get_node(p_from);
445 if (node.is_valid()) {
446 from_node = p_from;
447 _popup(true, p_release_position);
448 }
449 }
450
451 void AnimationNodeBlendTreeEditor::_connection_from_empty(const String &p_to, int p_to_slot, const Vector2 &p_release_position) {
452 if (read_only) {
453 return;
454 }
455
456 Ref<AnimationNode> node = blend_tree->get_node(p_to);
457 if (node.is_valid()) {
458 to_node = p_to;
459 to_slot = p_to_slot;
460 _popup(false, p_release_position);
461 }
462 }
463
464 void AnimationNodeBlendTreeEditor::_popup_hide() {
465 to_node = "";
466 to_slot = -1;
467 }
468
469 void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
470 updating = true;
471 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
472 undo_redo->create_action(TTR("Node Moved"));
473 undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
474 undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
475 undo_redo->add_do_method(this, "update_graph");
476 undo_redo->add_undo_method(this, "update_graph");
477 undo_redo->commit_action();
478 updating = false;
479 }
480
481 void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
482 if (read_only) {
483 return;
484 }
485
486 AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
487
488 if (err == AnimationNodeBlendTree::CONNECTION_ERROR_CONNECTION_EXISTS) {
489 blend_tree->disconnect_node(p_to, p_to_index);
490 err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
491 }
492
493 if (err != AnimationNodeBlendTree::CONNECTION_OK) {
494 EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
495 return;
496 }
497
498 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
499 undo_redo->create_action(TTR("Nodes Connected"));
500 undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
501 undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
502 undo_redo->add_do_method(this, "update_graph");
503 undo_redo->add_undo_method(this, "update_graph");
504 undo_redo->commit_action();
505 }
506
507 void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
508 if (read_only) {
509 return;
510 }
511
512 graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
513
514 updating = true;
515 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
516 undo_redo->create_action(TTR("Nodes Disconnected"));
517 undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
518 undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
519 undo_redo->add_do_method(this, "update_graph");
520 undo_redo->add_undo_method(this, "update_graph");
521 undo_redo->commit_action();
522 updating = false;
523 }
524
525 void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, const Array &p_options, const String &p_node) {
526 String option = p_options[p_index];
527
528 Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
529 ERR_FAIL_COND(anim.is_null());
530
531 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
532 undo_redo->create_action(TTR("Set Animation"));
533 undo_redo->add_do_method(anim.ptr(), "set_animation", option);
534 undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
535 undo_redo->add_do_method(this, "update_graph");
536 undo_redo->add_undo_method(this, "update_graph");
537 undo_redo->commit_action();
538 }
539
540 void AnimationNodeBlendTreeEditor::_delete_node_request(const String &p_which) {
541 if (read_only) {
542 return;
543 }
544
545 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
546 undo_redo->create_action(TTR("Delete Node"));
547 undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
548 undo_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));
549
550 List<AnimationNodeBlendTree::NodeConnection> conns;
551 blend_tree->get_node_connections(&conns);
552
553 for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
554 if (E.output_node == p_which || E.input_node == p_which) {
555 undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E.input_node, E.input_index, E.output_node);
556 }
557 }
558
559 undo_redo->add_do_method(this, "update_graph");
560 undo_redo->add_undo_method(this, "update_graph");
561 undo_redo->commit_action();
562
563 // Return selection to host BlendTree node.
564 EditorNode::get_singleton()->push_item(blend_tree.ptr(), "", true);
565 }
566
567 void AnimationNodeBlendTreeEditor::_delete_nodes_request(const TypedArray<StringName> &p_nodes) {
568 if (read_only) {
569 return;
570 }
571
572 List<StringName> to_erase;
573
574 if (p_nodes.is_empty()) {
575 for (int i = 0; i < graph->get_child_count(); i++) {
576 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
577 if (gn && gn->is_selected()) {
578 Ref<AnimationNode> anode = blend_tree->get_node(gn->get_name());
579 if (anode->is_deletable()) {
580 to_erase.push_back(gn->get_name());
581 }
582 }
583 }
584 } else {
585 for (int i = 0; i < p_nodes.size(); i++) {
586 Ref<AnimationNode> anode = blend_tree->get_node(p_nodes[i]);
587 if (anode->is_deletable()) {
588 to_erase.push_back(p_nodes[i]);
589 }
590 }
591 }
592
593 if (to_erase.is_empty()) {
594 return;
595 }
596
597 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
598 undo_redo->create_action(TTR("Delete Node(s)"));
599
600 for (const StringName &F : to_erase) {
601 _delete_node_request(F);
602 }
603
604 undo_redo->commit_action();
605 }
606
607 void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
608 if (read_only) {
609 return;
610 }
611
612 GraphNode *gn = Object::cast_to<GraphNode>(p_node);
613 ERR_FAIL_NULL(gn);
614
615 String name = gn->get_name();
616
617 Ref<AnimationNode> anode = blend_tree->get_node(name);
618 ERR_FAIL_COND(anode.is_null());
619
620 EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
621 }
622
623 void AnimationNodeBlendTreeEditor::_node_deselected(Object *p_node) {
624 // Check if no nodes are selected, return selection to host BlendTree node.
625 bool any_selected = false;
626 for (int i = 0; i < graph->get_child_count(); i++) {
627 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
628 if (gn && gn->is_selected()) {
629 any_selected = true;
630 break;
631 }
632 }
633
634 if (!any_selected) {
635 EditorNode::get_singleton()->push_item(blend_tree.ptr(), "", true);
636 }
637 }
638
639 void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
640 Ref<AnimationNode> an = blend_tree->get_node(p_which);
641 ERR_FAIL_COND(an.is_null());
642 AnimationTreeEditor::get_singleton()->enter_editor(p_which);
643 }
644
645 void AnimationNodeBlendTreeEditor::_filter_toggled() {
646 updating = true;
647 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
648 undo_redo->create_action(TTR("Toggle Filter On/Off"));
649 undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
650 undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
651 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
652 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
653 undo_redo->commit_action();
654 updating = false;
655 }
656
657 void AnimationNodeBlendTreeEditor::_filter_edited() {
658 TreeItem *edited = filters->get_edited();
659 ERR_FAIL_NULL(edited);
660
661 NodePath edited_path = edited->get_metadata(0);
662 bool filtered = edited->is_checked(0);
663
664 updating = true;
665 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
666 undo_redo->create_action(TTR("Change Filter"));
667 undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
668 undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
669 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
670 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
671 undo_redo->commit_action();
672 updating = false;
673 }
674
675 void AnimationNodeBlendTreeEditor::_filter_fill_selection() {
676 TreeItem *ti = filters->get_root();
677 if (!ti) {
678 return;
679 }
680
681 updating = true;
682 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
683 undo_redo->create_action(TTR("Fill Selected Filter Children"));
684
685 _filter_fill_selection_recursive(undo_redo, ti, false);
686
687 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
688 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
689 undo_redo->commit_action();
690 updating = false;
691
692 _update_filters(_filter_edit);
693 }
694
695 void AnimationNodeBlendTreeEditor::_filter_invert_selection() {
696 TreeItem *ti = filters->get_root();
697 if (!ti) {
698 return;
699 }
700
701 updating = true;
702 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
703 undo_redo->create_action(TTR("Invert Filter Selection"));
704
705 _filter_invert_selection_recursive(undo_redo, ti);
706
707 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
708 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
709 undo_redo->commit_action();
710 updating = false;
711
712 _update_filters(_filter_edit);
713 }
714
715 void AnimationNodeBlendTreeEditor::_filter_clear_selection() {
716 TreeItem *ti = filters->get_root();
717 if (!ti) {
718 return;
719 }
720
721 updating = true;
722 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
723 undo_redo->create_action(TTR("Clear Filter Selection"));
724
725 _filter_clear_selection_recursive(undo_redo, ti);
726
727 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
728 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
729 undo_redo->commit_action();
730 updating = false;
731
732 _update_filters(_filter_edit);
733 }
734
735 void AnimationNodeBlendTreeEditor::_filter_fill_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item, bool p_parent_filtered) {
736 TreeItem *ti = p_item->get_first_child();
737 bool parent_filtered = p_parent_filtered;
738 while (ti) {
739 NodePath item_path = ti->get_metadata(0);
740 bool filtered = _filter_edit->is_path_filtered(item_path);
741 parent_filtered |= filtered;
742
743 p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, parent_filtered);
744 p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
745
746 _filter_fill_selection_recursive(p_undo_redo, ti, parent_filtered);
747 ti = ti->get_next();
748 parent_filtered = p_parent_filtered;
749 }
750 }
751
752 void AnimationNodeBlendTreeEditor::_filter_invert_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
753 TreeItem *ti = p_item->get_first_child();
754 while (ti) {
755 NodePath item_path = ti->get_metadata(0);
756 bool filtered = _filter_edit->is_path_filtered(item_path);
757
758 p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, !filtered);
759 p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
760
761 _filter_invert_selection_recursive(p_undo_redo, ti);
762 ti = ti->get_next();
763 }
764 }
765
766 void AnimationNodeBlendTreeEditor::_filter_clear_selection_recursive(EditorUndoRedoManager *p_undo_redo, TreeItem *p_item) {
767 TreeItem *ti = p_item->get_first_child();
768 while (ti) {
769 NodePath item_path = ti->get_metadata(0);
770 bool filtered = _filter_edit->is_path_filtered(item_path);
771
772 p_undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", item_path, false);
773 p_undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", item_path, filtered);
774
775 _filter_clear_selection_recursive(p_undo_redo, ti);
776 ti = ti->get_next();
777 }
778 }
779
780 bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
781 if (updating || _filter_edit != anode) {
782 return false;
783 }
784
785 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
786 if (!tree) {
787 return false;
788 }
789
790 Node *base = tree->get_node(tree->get_root_node());
791 if (!base) {
792 EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
793 return false;
794 }
795
796 updating = true;
797
798 HashSet<String> paths;
799 HashMap<String, RBSet<String>> types;
800 {
801 List<StringName> animation_list;
802 tree->get_animation_list(&animation_list);
803
804 for (const StringName &E : animation_list) {
805 Ref<Animation> anim = tree->get_animation(E);
806 for (int i = 0; i < anim->get_track_count(); i++) {
807 String track_path = String(anim->track_get_path(i));
808 paths.insert(track_path);
809
810 String track_type_name;
811 Animation::TrackType track_type = anim->track_get_type(i);
812 switch (track_type) {
813 case Animation::TrackType::TYPE_ANIMATION: {
814 track_type_name = TTR("Anim Clips");
815 } break;
816 case Animation::TrackType::TYPE_AUDIO: {
817 track_type_name = TTR("Audio Clips");
818 } break;
819 case Animation::TrackType::TYPE_METHOD: {
820 track_type_name = TTR("Functions");
821 } break;
822 default: {
823 } break;
824 }
825 if (!track_type_name.is_empty()) {
826 types[track_path].insert(track_type_name);
827 }
828 }
829 }
830 }
831
832 filter_enabled->set_pressed(anode->is_filter_enabled());
833 filters->clear();
834 TreeItem *root = filters->create_item();
835
836 HashMap<String, TreeItem *> parenthood;
837
838 for (const String &E : paths) {
839 NodePath path = E;
840 TreeItem *ti = nullptr;
841 String accum;
842 for (int i = 0; i < path.get_name_count(); i++) {
843 String name = path.get_name(i);
844 if (!accum.is_empty()) {
845 accum += "/";
846 }
847 accum += name;
848 if (!parenthood.has(accum)) {
849 if (ti) {
850 ti = filters->create_item(ti);
851 } else {
852 ti = filters->create_item(root);
853 }
854 parenthood[accum] = ti;
855 ti->set_text(0, name);
856 ti->set_selectable(0, false);
857 ti->set_editable(0, false);
858
859 if (base->has_node(accum)) {
860 Node *node = base->get_node(accum);
861 ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node));
862 }
863
864 } else {
865 ti = parenthood[accum];
866 }
867 }
868
869 Node *node = nullptr;
870 if (base->has_node(accum)) {
871 node = base->get_node(accum);
872 }
873 if (!node) {
874 continue; //no node, can't edit
875 }
876
877 if (path.get_subname_count()) {
878 String concat = path.get_concatenated_subnames();
879
880 Skeleton3D *skeleton = Object::cast_to<Skeleton3D>(node);
881 if (skeleton && skeleton->find_bone(concat) != -1) {
882 //path in skeleton
883 const String &bone = concat;
884 int idx = skeleton->find_bone(bone);
885 List<String> bone_path;
886 while (idx != -1) {
887 bone_path.push_front(skeleton->get_bone_name(idx));
888 idx = skeleton->get_bone_parent(idx);
889 }
890
891 accum += ":";
892 for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
893 if (F != bone_path.front()) {
894 accum += "/";
895 }
896
897 accum += F->get();
898 if (!parenthood.has(accum)) {
899 ti = filters->create_item(ti);
900 parenthood[accum] = ti;
901 ti->set_text(0, F->get());
902 ti->set_selectable(0, false);
903 ti->set_editable(0, false);
904 ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
905 } else {
906 ti = parenthood[accum];
907 }
908 }
909
910 ti->set_editable(0, !read_only);
911 ti->set_selectable(0, true);
912 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
913 ti->set_text(0, concat);
914 ti->set_checked(0, anode->is_path_filtered(path));
915 ti->set_icon(0, get_editor_theme_icon(SNAME("Bone")));
916 ti->set_metadata(0, path);
917
918 } else {
919 //just a property
920 ti = filters->create_item(ti);
921 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
922 ti->set_text(0, concat);
923 ti->set_editable(0, !read_only);
924 ti->set_selectable(0, true);
925 ti->set_checked(0, anode->is_path_filtered(path));
926 ti->set_metadata(0, path);
927 }
928 } else {
929 if (ti) {
930 //just a node, not a property track
931 String types_text = "[";
932 if (types.has(String(path))) {
933 RBSet<String>::Iterator F = types[String(path)].begin();
934 types_text += *F;
935 while (F) {
936 types_text += " / " + *F;
937 ;
938 ++F;
939 }
940 }
941 types_text += "]";
942 ti = filters->create_item(ti);
943 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
944 ti->set_text(0, types_text);
945 ti->set_editable(0, !read_only);
946 ti->set_selectable(0, true);
947 ti->set_checked(0, anode->is_path_filtered(path));
948 ti->set_metadata(0, path);
949 }
950 }
951 }
952
953 updating = false;
954
955 return true;
956 }
957
958 void AnimationNodeBlendTreeEditor::_inspect_filters(const String &p_which) {
959 if (read_only) {
960 filter_dialog->set_title(TTR("Inspect Filtered Tracks:"));
961 } else {
962 filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
963 }
964
965 filter_enabled->set_disabled(read_only);
966
967 Ref<AnimationNode> anode = blend_tree->get_node(p_which);
968 ERR_FAIL_COND(anode.is_null());
969
970 _filter_edit = anode;
971 if (!_update_filters(anode)) {
972 return;
973 }
974
975 filter_dialog->popup_centered(Size2(500, 500) * EDSCALE);
976 }
977
978 void AnimationNodeBlendTreeEditor::_update_editor_settings() {
979 graph->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")));
980 graph->set_warped_panning(EDITOR_GET("editors/panning/warped_mouse_panning"));
981 }
982
983 void AnimationNodeBlendTreeEditor::_notification(int p_what) {
984 switch (p_what) {
985 case NOTIFICATION_READY: {
986 _update_editor_settings();
987 } break;
988
989 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
990 if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
991 _update_editor_settings();
992 }
993 } break;
994
995 case NOTIFICATION_THEME_CHANGED: {
996 error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
997 error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
998
999 if (is_visible_in_tree()) {
1000 update_graph();
1001 }
1002 } break;
1003
1004 case NOTIFICATION_PROCESS: {
1005 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
1006 if (!tree) {
1007 return; // Node has been changed.
1008 }
1009
1010 String error;
1011
1012 error = tree->get_editor_error_message();
1013
1014 if (error != error_label->get_text()) {
1015 error_label->set_text(error);
1016 if (!error.is_empty()) {
1017 error_panel->show();
1018 } else {
1019 error_panel->hide();
1020 }
1021 }
1022
1023 List<AnimationNodeBlendTree::NodeConnection> conns;
1024 blend_tree->get_node_connections(&conns);
1025 for (const AnimationNodeBlendTree::NodeConnection &E : conns) {
1026 float activity = 0;
1027 StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
1028 if (!tree->is_state_invalid()) {
1029 activity = tree->get_connection_activity(path, E.input_index);
1030 }
1031 graph->set_connection_activity(E.output_node, 0, E.input_node, E.input_index, activity);
1032 }
1033
1034 for (const KeyValue<StringName, ProgressBar *> &E : animations) {
1035 Ref<AnimationNodeAnimation> an = blend_tree->get_node(E.key);
1036 if (an.is_valid()) {
1037 if (tree->has_animation(an->get_animation())) {
1038 Ref<Animation> anim = tree->get_animation(an->get_animation());
1039 if (anim.is_valid()) {
1040 //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E.input_node;
1041 StringName length_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_length";
1042 StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E.key) + "/current_position";
1043 E.value->set_max(tree->get(length_path));
1044 E.value->set_value(tree->get(time_path));
1045 }
1046 }
1047 }
1048 }
1049
1050 for (int i = 0; i < visible_properties.size(); i++) {
1051 visible_properties[i]->update_property();
1052 }
1053 } break;
1054
1055 case NOTIFICATION_VISIBILITY_CHANGED: {
1056 set_process(is_visible_in_tree());
1057 } break;
1058 }
1059 }
1060
1061 void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
1062 if (read_only) {
1063 return;
1064 }
1065
1066 if (updating) {
1067 return;
1068 }
1069
1070 if (blend_tree.is_null()) {
1071 return;
1072 }
1073
1074 updating = true;
1075 blend_tree->set_graph_offset(p_scroll / EDSCALE);
1076 updating = false;
1077 }
1078
1079 void AnimationNodeBlendTreeEditor::_bind_methods() {
1080 ClassDB::bind_method("update_graph", &AnimationNodeBlendTreeEditor::update_graph);
1081 ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
1082 }
1083
1084 AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = nullptr;
1085
1086 // AnimationNode's "node_changed" signal means almost update_input.
1087 void AnimationNodeBlendTreeEditor::_node_changed(const StringName &p_node_name) {
1088 // TODO:
1089 // Here is executed during the commit of EditorNode::undo_redo, it is not possible to create an undo_redo action here.
1090 // The disconnect when the number of enabled inputs decreases is done in AnimationNodeBlendTree and update_graph().
1091 // This means that there is no place to register undo_redo actions.
1092 // In order to implement undo_redo correctly, we may need to implement AnimationNodeEdit such as AnimationTrackKeyEdit
1093 // and add it to _node_selected() with EditorNode::get_singleton()->push_item(AnimationNodeEdit).
1094 update_graph();
1095 }
1096
1097 void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
1098 if (blend_tree.is_null()) {
1099 return;
1100 }
1101
1102 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
1103 if (!tree) {
1104 return;
1105 }
1106
1107 String prev_name = blend_tree->get_node_name(p_node);
1108 ERR_FAIL_COND(prev_name.is_empty());
1109 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
1110 ERR_FAIL_NULL(gn);
1111
1112 const String &new_name = p_text;
1113
1114 ERR_FAIL_COND(new_name.is_empty() || new_name.contains_char('.') || new_name.contains_char('/'));
1115
1116 if (new_name == prev_name) {
1117 return; //nothing to do
1118 }
1119
1120 const String &base_name = new_name;
1121 int base = 1;
1122 String name = base_name;
1123 while (blend_tree->has_node(name)) {
1124 base++;
1125 name = base_name + " " + itos(base);
1126 }
1127
1128 String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
1129
1130 updating = true;
1131 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1132 undo_redo->create_action(TTR("Node Renamed"));
1133 undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
1134 undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
1135 undo_redo->add_do_method(this, "update_graph");
1136 undo_redo->add_undo_method(this, "update_graph");
1137 undo_redo->commit_action();
1138 updating = false;
1139 gn->set_name(new_name);
1140 gn->set_size(gn->get_minimum_size());
1141
1142 //change editors accordingly
1143 for (int i = 0; i < visible_properties.size(); i++) {
1144 String pname = visible_properties[i]->get_edited_property().operator String();
1145 if (pname.begins_with(base_path + prev_name)) {
1146 String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
1147 visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
1148 }
1149 }
1150
1151 //recreate connections
1152 graph->clear_connections();
1153
1154 List<AnimationNodeBlendTree::NodeConnection> node_connections;
1155 blend_tree->get_node_connections(&node_connections);
1156
1157 for (const AnimationNodeBlendTree::NodeConnection &E : node_connections) {
1158 StringName from = E.output_node;
1159 StringName to = E.input_node;
1160 int to_idx = E.input_index;
1161
1162 graph->connect_node(from, 0, to, to_idx);
1163 }
1164
1165 //update animations
1166 for (const KeyValue<StringName, ProgressBar *> &E : animations) {
1167 if (E.key == prev_name) {
1168 animations[new_name] = animations[prev_name];
1169 animations.erase(prev_name);
1170 break;
1171 }
1172 }
1173
1174 update_graph(); // Needed to update the signal connections with the new name.
1175 current_node_rename_text = String();
1176 }
1177
1178 void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Ref<AnimationNode> p_node) {
1179 if (current_node_rename_text.is_empty()) {
1180 return; // The text_submitted signal triggered the graph update and freed the LineEdit.
1181 }
1182 _node_renamed(current_node_rename_text, p_node);
1183 }
1184
1185 void AnimationNodeBlendTreeEditor::_node_rename_lineedit_changed(const String &p_text) {
1186 current_node_rename_text = p_text;
1187 }
1188
1189 bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
1190 Ref<AnimationNodeBlendTree> bt = p_node;
1191 return bt.is_valid();
1192 }
1193
1194 void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
1195 if (blend_tree.is_valid()) {
1196 blend_tree->disconnect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
1197 }
1198
1199 blend_tree = p_node;
1200
1201 read_only = false;
1202
1203 if (blend_tree.is_null()) {
1204 hide();
1205 } else {
1206 read_only = EditorNode::get_singleton()->is_resource_read_only(blend_tree);
1207
1208 blend_tree->connect("node_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_changed));
1209
1210 update_graph();
1211 }
1212
1213 add_node->set_disabled(read_only);
1214 graph->set_show_arrange_button(!read_only);
1215 }
1216
1217 AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
1218 singleton = this;
1219 updating = false;
1220 use_position_from_popup_menu = false;
1221
1222 graph = memnew(GraphEdit);
1223 add_child(graph);
1224 graph->add_valid_right_disconnect_type(0);
1225 graph->add_valid_left_disconnect_type(0);
1226 graph->set_v_size_flags(SIZE_EXPAND_FILL);
1227 graph->connect("connection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_request), CONNECT_DEFERRED);
1228 graph->connect("disconnection_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_disconnection_request), CONNECT_DEFERRED);
1229 graph->connect("node_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_selected));
1230 graph->connect("node_deselected", callable_mp(this, &AnimationNodeBlendTreeEditor::_node_deselected));
1231 graph->connect("scroll_offset_changed", callable_mp(this, &AnimationNodeBlendTreeEditor::_scroll_changed));
1232 graph->connect("delete_nodes_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_delete_nodes_request));
1233 graph->connect("popup_request", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_request));
1234 graph->connect("connection_to_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_to_empty));
1235 graph->connect("connection_from_empty", callable_mp(this, &AnimationNodeBlendTreeEditor::_connection_from_empty));
1236 float graph_minimap_opacity = EDITOR_GET("editors/visual_editors/minimap_opacity");
1237 graph->set_minimap_opacity(graph_minimap_opacity);
1238 float graph_lines_curvature = EDITOR_GET("editors/visual_editors/lines_curvature");
1239 graph->set_connection_lines_curvature(graph_lines_curvature);
1240
1241 VSeparator *vs = memnew(VSeparator);
1242 graph->get_menu_hbox()->add_child(vs);
1243 graph->get_menu_hbox()->move_child(vs, 0);
1244
1245 add_node = memnew(MenuButton);
1246 graph->get_menu_hbox()->add_child(add_node);
1247 add_node->set_text(TTR("Add Node..."));
1248 graph->get_menu_hbox()->move_child(add_node, 0);
1249 add_node->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_add_node));
1250 add_node->get_popup()->connect("popup_hide", callable_mp(this, &AnimationNodeBlendTreeEditor::_popup_hide), CONNECT_DEFERRED);
1251 add_node->connect("about_to_popup", callable_mp(this, &AnimationNodeBlendTreeEditor::_update_options_menu).bind(false));
1252 add_node->set_disabled(read_only);
1253
1254 add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
1255 add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot", 2));
1256 add_options.push_back(AddOption("Add2", "AnimationNodeAdd2", 2));
1257 add_options.push_back(AddOption("Add3", "AnimationNodeAdd3", 3));
1258 add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2", 2));
1259 add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3", 3));
1260 add_options.push_back(AddOption("Sub2", "AnimationNodeSub2", 2));
1261 add_options.push_back(AddOption("TimeSeek", "AnimationNodeTimeSeek", 1));
1262 add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale", 1));
1263 add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
1264 add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
1265 add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
1266 add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
1267 add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
1268 _update_options_menu();
1269
1270 error_panel = memnew(PanelContainer);
1271 add_child(error_panel);
1272 error_label = memnew(Label);
1273 error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
1274 error_panel->add_child(error_label);
1275 error_label->set_text("eh");
1276
1277 filter_dialog = memnew(AcceptDialog);
1278 add_child(filter_dialog);
1279 filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
1280
1281 VBoxContainer *filter_vbox = memnew(VBoxContainer);
1282 filter_dialog->add_child(filter_vbox);
1283
1284 HBoxContainer *filter_hbox = memnew(HBoxContainer);
1285 filter_vbox->add_child(filter_hbox);
1286
1287 filter_enabled = memnew(CheckBox);
1288 filter_enabled->set_text(TTR("Enable Filtering"));
1289 filter_enabled->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_toggled));
1290 filter_hbox->add_child(filter_enabled);
1291
1292 filter_fill_selection = memnew(Button);
1293 filter_fill_selection->set_text(TTR("Fill Selected Children"));
1294 filter_fill_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_fill_selection));
1295 filter_hbox->add_child(filter_fill_selection);
1296
1297 filter_invert_selection = memnew(Button);
1298 filter_invert_selection->set_text(TTR("Invert"));
1299 filter_invert_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_invert_selection));
1300 filter_hbox->add_child(filter_invert_selection);
1301
1302 filter_clear_selection = memnew(Button);
1303 filter_clear_selection->set_text(TTR("Clear"));
1304 filter_clear_selection->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_clear_selection));
1305 filter_hbox->add_child(filter_clear_selection);
1306
1307 filters = memnew(Tree);
1308 filter_vbox->add_child(filters);
1309 filters->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1310 filters->set_v_size_flags(SIZE_EXPAND_FILL);
1311 filters->set_hide_root(true);
1312 filters->connect("item_edited", callable_mp(this, &AnimationNodeBlendTreeEditor::_filter_edited));
1313
1314 open_file = memnew(EditorFileDialog);
1315 add_child(open_file);
1316 open_file->set_title(TTR("Open Animation Node"));
1317 open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
1318 open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendTreeEditor::_file_opened));
1319
1320 animation_node_inspector_plugin.instantiate();
1321 EditorInspector::add_inspector_plugin(animation_node_inspector_plugin);
1322 }
1323
1324 // EditorPluginAnimationNodeAnimation
1325
1326 void AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog() {
1327 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
1328 StringName anim_name = animation_node_animation->get_animation();
1329 PackedStringArray markers = tree->has_animation(anim_name) ? tree->get_animation(anim_name)->get_marker_names() : PackedStringArray();
1330
1331 dialog->select_start->clear();
1332 dialog->select_start->add_icon_item(get_editor_theme_icon(SNAME("PlayStart")), TTR("Start of Animation"));
1333 dialog->select_start->add_separator();
1334 dialog->select_end->clear();
1335 dialog->select_end->add_icon_item(get_editor_theme_icon(SNAME("PlayStartBackwards")), TTR("End of Animation"));
1336 dialog->select_end->add_separator();
1337
1338 for (const String &marker : markers) {
1339 dialog->select_start->add_item(marker);
1340 dialog->select_end->add_item(marker);
1341 }
1342
1343 // 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.
1344 // This invariant is maintained by _validate_markers.
1345 dialog->select_start->select(0);
1346 dialog->select_end->select(0);
1347
1348 dialog->popup_centered(Size2(200, 0) * EDSCALE);
1349 }
1350
1351 void AnimationNodeAnimationEditor::_validate_markers(int p_id) {
1352 // Note: p_id is ignored. It is included because OptionButton's item_changed signal always passes it.
1353 int start_id = dialog->select_start->get_selected_id();
1354 int end_id = dialog->select_end->get_selected_id();
1355
1356 StringName anim_name = animation_node_animation->get_animation();
1357 Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(anim_name);
1358 ERR_FAIL_COND(animation.is_null());
1359
1360 double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));
1361 double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));
1362
1363 // p_start and p_end have the same item count.
1364 for (int i = 2; i < dialog->select_start->get_item_count(); i++) {
1365 String start_marker = dialog->select_start->get_item_text(i);
1366 String end_marker = dialog->select_end->get_item_text(i);
1367 dialog->select_start->set_item_disabled(i, end_id >= 2 && (i == end_id || animation->get_marker_time(start_marker) > end_time));
1368 dialog->select_end->set_item_disabled(i, start_id >= 2 && (i == start_id || start_time > animation->get_marker_time(end_marker)));
1369 }
1370 }
1371
1372 void AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog() {
1373 int start_id = dialog->select_start->get_selected_id();
1374 int end_id = dialog->select_end->get_selected_id();
1375
1376 Ref<Animation> animation = AnimationTreeEditor::get_singleton()->get_animation_tree()->get_animation(animation_node_animation->get_animation());
1377 ERR_FAIL_COND(animation.is_null());
1378 double start_time = start_id < 2 ? 0 : animation->get_marker_time(dialog->select_start->get_item_text(start_id));
1379 double end_time = end_id < 2 ? animation->get_length() : animation->get_marker_time(dialog->select_end->get_item_text(end_id));
1380 double length = end_time - start_time;
1381
1382 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1383 undo_redo->create_action(TTR("Set Custom Timeline from Marker"));
1384 undo_redo->add_do_method(*animation_node_animation, "set_start_offset", start_time);
1385 undo_redo->add_undo_method(*animation_node_animation, "set_start_offset", animation_node_animation->get_start_offset());
1386 undo_redo->add_do_method(*animation_node_animation, "set_stretch_time_scale", false);
1387 undo_redo->add_undo_method(*animation_node_animation, "set_stretch_time_scale", animation_node_animation->is_stretching_time_scale());
1388 undo_redo->add_do_method(*animation_node_animation, "set_timeline_length", length);
1389 undo_redo->add_undo_method(*animation_node_animation, "set_timeline_length", animation_node_animation->get_timeline_length());
1390 undo_redo->add_do_method(*animation_node_animation, "notify_property_list_changed");
1391 undo_redo->add_undo_method(*animation_node_animation, "notify_property_list_changed");
1392 undo_redo->commit_action();
1393 }
1394
1395 AnimationNodeAnimationEditor::AnimationNodeAnimationEditor(Ref<AnimationNodeAnimation> p_animation_node_animation) {
1396 animation_node_animation = p_animation_node_animation;
1397
1398 dialog = memnew(AnimationNodeAnimationEditorDialog);
1399 add_child(dialog);
1400 dialog->set_hide_on_ok(false);
1401 dialog->select_start->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));
1402 dialog->select_end->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeAnimationEditor::_validate_markers));
1403 dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationNodeAnimationEditor::_confirm_set_custom_timeline_from_marker_dialog));
1404
1405 Control *top_spacer = memnew(Control);
1406 add_child(top_spacer);
1407 top_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);
1408
1409 button = memnew(Button);
1410 add_child(button);
1411 button->set_text(TTR("Set Custom Timeline from Marker"));
1412 button->set_h_size_flags(Control::SIZE_SHRINK_CENTER);
1413 button->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeAnimationEditor::_open_set_custom_timeline_from_marker_dialog));
1414
1415 Control *bottom_spacer = memnew(Control);
1416 add_child(bottom_spacer);
1417 bottom_spacer->set_custom_minimum_size(Size2(0, 2) * EDSCALE);
1418 }
1419
1420 void AnimationNodeAnimationEditor::_notification(int p_what) {
1421 switch (p_what) {
1422 case NOTIFICATION_THEME_CHANGED: {
1423 button->set_theme_type_variation(SNAME("InspectorActionButton"));
1424 button->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
1425 } break;
1426 }
1427 }
1428
1429 bool EditorInspectorPluginAnimationNodeAnimation::can_handle(Object *p_object) {
1430 Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
1431 return ana.is_valid() && ana->is_using_custom_timeline();
1432 }
1433
1434 bool 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) {
1435 Ref<AnimationNodeAnimation> ana(Object::cast_to<AnimationNodeAnimation>(p_object));
1436 ERR_FAIL_COND_V(ana.is_null(), false);
1437
1438 if (p_path == "timeline_length") {
1439 add_custom_control(memnew(AnimationNodeAnimationEditor(ana)));
1440 }
1441
1442 return false;
1443 }
1444
1445 AnimationNodeAnimationEditorDialog::AnimationNodeAnimationEditorDialog() {
1446 set_title(TTR("Select Markers"));
1447
1448 GridContainer *grid = memnew(GridContainer);
1449 grid->set_columns(2);
1450 grid->set_offsets_preset(Control::PRESET_FULL_RECT);
1451 add_child(grid);
1452
1453 Label *label_start = memnew(Label(TTR("Start Marker")));
1454 grid->add_child(label_start);
1455 label_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1456 label_start->set_stretch_ratio(1);
1457 select_start = memnew(OptionButton);
1458 select_start->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1459 select_start->set_accessibility_name(TTRC("Start Marker"));
1460 grid->add_child(select_start);
1461 select_start->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1462 select_start->set_stretch_ratio(2);
1463
1464 Label *label_end = memnew(Label(TTR("End Marker")));
1465 grid->add_child(label_end);
1466 label_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1467 label_end->set_stretch_ratio(1);
1468 select_end = memnew(OptionButton);
1469 select_end->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
1470 select_end->set_accessibility_name(TTRC("End Marker"));
1471 grid->add_child(select_end);
1472 select_end->set_h_size_flags(Control::SIZE_EXPAND_FILL);
1473 select_end->set_stretch_ratio(2);
1474 }