1/**************************************************************************/
2/* animation_state_machine_editor.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_state_machine_editor.h"
32
33#include "core/config/project_settings.h"
34#include "core/input/input.h"
35#include "core/io/resource_loader.h"
36#include "core/math/geometry_2d.h"
37#include "core/os/keyboard.h"
38#include "editor/editor_node.h"
39#include "editor/editor_scale.h"
40#include "editor/editor_settings.h"
41#include "editor/editor_string_names.h"
42#include "editor/editor_undo_redo_manager.h"
43#include "editor/gui/editor_file_dialog.h"
44#include "scene/animation/animation_blend_tree.h"
45#include "scene/animation/animation_player.h"
46#include "scene/gui/menu_button.h"
47#include "scene/gui/option_button.h"
48#include "scene/gui/panel.h"
49#include "scene/gui/panel_container.h"
50#include "scene/gui/separator.h"
51#include "scene/gui/tree.h"
52#include "scene/main/viewport.h"
53#include "scene/main/window.h"
54#include "scene/resources/style_box_flat.h"
55#include "scene/scene_string_names.h"
56
57bool AnimationNodeStateMachineEditor::can_edit(const Ref<AnimationNode> &p_node) {
58 Ref<AnimationNodeStateMachine> ansm = p_node;
59 return ansm.is_valid();
60}
61
62void AnimationNodeStateMachineEditor::edit(const Ref<AnimationNode> &p_node) {
63 state_machine = p_node;
64
65 read_only = false;
66
67 if (state_machine.is_valid()) {
68 read_only = EditorNode::get_singleton()->is_resource_read_only(state_machine);
69
70 selected_transition_from = StringName();
71 selected_transition_to = StringName();
72 selected_transition_index = -1;
73 selected_node = StringName();
74 selected_nodes.clear();
75 _update_mode();
76 _update_graph();
77 }
78
79 tool_create->set_disabled(read_only);
80 tool_connect->set_disabled(read_only);
81}
82
83String AnimationNodeStateMachineEditor::_get_root_playback_path(String &r_node_directory) {
84 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
85 Vector<String> edited_path = AnimationTreeEditor::get_singleton()->get_edited_path();
86
87 String base_path;
88 Vector<String> node_directory_path;
89
90 bool is_playable_anodesm_found = false;
91
92 if (edited_path.size()) {
93 while (!is_playable_anodesm_found) {
94 base_path = String("/").join(edited_path);
95 Ref<AnimationNodeStateMachine> anodesm = !edited_path.size() ? tree->get_tree_root() : tree->get_tree_root()->find_node_by_path(base_path);
96 if (!anodesm.is_valid()) {
97 break;
98 } else {
99 if (anodesm->get_state_machine_type() != AnimationNodeStateMachine::STATE_MACHINE_TYPE_GROUPED) {
100 is_playable_anodesm_found = true;
101 } else {
102 int idx = edited_path.size() - 1;
103 node_directory_path.push_back(edited_path[idx]);
104 edited_path.remove_at(idx);
105 }
106 }
107 }
108 }
109
110 if (is_playable_anodesm_found) {
111 // Return Root/Nested state machine playback.
112 node_directory_path.reverse();
113 r_node_directory = String("/").join(node_directory_path);
114 if (node_directory_path.size()) {
115 r_node_directory += "/";
116 }
117 base_path = !edited_path.size() ? String(SceneStringNames::get_singleton()->parameters_base_path) + "playback" : String(SceneStringNames::get_singleton()->parameters_base_path) + base_path + "/playback";
118 } else {
119 // Hmmm, we have to return Grouped state machine playback...
120 // It will give the user the error that Root/Nested state machine should be retrieved, that would be kind :-)
121 r_node_directory = String();
122 base_path = AnimationTreeEditor::get_singleton()->get_base_path() + "playback";
123 }
124
125 return base_path;
126}
127
128void AnimationNodeStateMachineEditor::_state_machine_gui_input(const Ref<InputEvent> &p_event) {
129 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
130 if (!tree) {
131 return;
132 }
133
134 String node_directory;
135 Ref<AnimationNodeStateMachinePlayback> playback = tree->get(_get_root_playback_path(node_directory));
136 if (!playback.is_valid()) {
137 return;
138 }
139
140 Ref<InputEventKey> k = p_event;
141 if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
142 if (selected_node != StringName() || !selected_nodes.is_empty() || selected_transition_to != StringName() || selected_transition_from != StringName()) {
143 if (!read_only) {
144 _erase_selected();
145 }
146 accept_event();
147 }
148 }
149
150 Ref<InputEventMouseButton> mb = p_event;
151
152 // Add new node
153 if (!read_only) {
154 if (mb.is_valid() && mb->is_pressed() && !box_selecting && !connecting && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (tool_create->is_pressed() && mb->get_button_index() == MouseButton::LEFT))) {
155 connecting_from = StringName();
156 _open_menu(mb->get_position());
157 }
158 }
159
160 // Select node or push a field inside
161 if (mb.is_valid() && !mb->is_shift_pressed() && !mb->is_ctrl_pressed() && mb->is_pressed() && tool_select->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
162 selected_transition_from = StringName();
163 selected_transition_to = StringName();
164 selected_transition_index = -1;
165 selected_node = StringName();
166
167 for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
168 if (node_rects[i].play.has_point(mb->get_position())) { //edit name
169 if (play_mode->get_selected() == 1 || !playback->is_playing()) {
170 // Start
171 playback->start(node_directory + String(node_rects[i].node_name));
172 } else {
173 // Travel
174 playback->travel(node_directory + String(node_rects[i].node_name));
175 }
176 state_machine_draw->queue_redraw();
177 return;
178 }
179
180 if (!read_only) {
181 if (node_rects[i].name.has_point(mb->get_position()) && state_machine->can_edit_node(node_rects[i].node_name)) { // edit name
182 Ref<StyleBox> line_sb = get_theme_stylebox(SNAME("normal"), SNAME("LineEdit"));
183
184 Rect2 edit_rect = node_rects[i].name;
185 edit_rect.position -= line_sb->get_offset();
186 edit_rect.size += line_sb->get_minimum_size();
187
188 name_edit_popup->set_position(state_machine_draw->get_screen_position() + edit_rect.position);
189 name_edit_popup->set_size(edit_rect.size);
190 name_edit->set_text(node_rects[i].node_name);
191 name_edit_popup->popup();
192 name_edit->grab_focus();
193 name_edit->select_all();
194
195 prev_name = node_rects[i].node_name;
196 return;
197 }
198 }
199
200 if (node_rects[i].edit.has_point(mb->get_position())) { //edit name
201 call_deferred(SNAME("_open_editor"), node_rects[i].node_name);
202 return;
203 }
204
205 if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
206 selected_node = node_rects[i].node_name;
207
208 if (!selected_nodes.has(selected_node)) {
209 selected_nodes.clear();
210 }
211
212 selected_nodes.insert(selected_node);
213
214 Ref<AnimationNode> anode = state_machine->get_node(selected_node);
215 EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
216 state_machine_draw->queue_redraw();
217 dragging_selected_attempt = true;
218 dragging_selected = false;
219 drag_from = mb->get_position();
220 snap_x = StringName();
221 snap_y = StringName();
222 _update_mode();
223 return;
224 }
225 }
226
227 //test the lines now
228 int closest = -1;
229 float closest_d = 1e20;
230 for (int i = 0; i < transition_lines.size(); i++) {
231 Vector2 s[2] = {
232 transition_lines[i].from,
233 transition_lines[i].to
234 };
235 Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mb->get_position(), s);
236 float d = cpoint.distance_to(mb->get_position());
237 if (d > transition_lines[i].width) {
238 continue;
239 }
240
241 if (d < closest_d) {
242 closest = i;
243 closest_d = d;
244 }
245 }
246
247 if (closest >= 0) {
248 selected_transition_from = transition_lines[closest].from_node;
249 selected_transition_to = transition_lines[closest].to_node;
250 selected_transition_index = closest;
251
252 Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(closest);
253 if (!state_machine->is_transition_across_group(closest)) {
254 EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
255 } else {
256 EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
257 EditorNode::get_singleton()->push_item(nullptr, "", true);
258 }
259 }
260
261 state_machine_draw->queue_redraw();
262 _update_mode();
263 }
264
265 // End moving node
266 if (mb.is_valid() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
267 if (dragging_selected) {
268 Ref<AnimationNode> an = state_machine->get_node(selected_node);
269 updating = true;
270
271 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
272 undo_redo->create_action(TTR("Move Node"));
273
274 for (int i = 0; i < node_rects.size(); i++) {
275 if (!selected_nodes.has(node_rects[i].node_name)) {
276 continue;
277 }
278
279 undo_redo->add_do_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name) + drag_ofs / EDSCALE);
280 undo_redo->add_undo_method(state_machine.ptr(), "set_node_position", node_rects[i].node_name, state_machine->get_node_position(node_rects[i].node_name));
281 }
282
283 undo_redo->add_do_method(this, "_update_graph");
284 undo_redo->add_undo_method(this, "_update_graph");
285 undo_redo->commit_action();
286 updating = false;
287 }
288 snap_x = StringName();
289 snap_y = StringName();
290
291 dragging_selected_attempt = false;
292 dragging_selected = false;
293 state_machine_draw->queue_redraw();
294 }
295
296 // Connect nodes
297 if (mb.is_valid() && ((tool_select->is_pressed() && mb->is_shift_pressed()) || tool_connect->is_pressed()) && mb->get_button_index() == MouseButton::LEFT && mb->is_pressed()) {
298 for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
299 if (node_rects[i].node.has_point(mb->get_position())) { //select node since nothing else was selected
300 connecting = true;
301 connection_follows_cursor = true;
302 connecting_from = node_rects[i].node_name;
303 connecting_to = mb->get_position();
304 connecting_to_node = StringName();
305 return;
306 }
307 }
308 }
309
310 // End connecting nodes
311 if (mb.is_valid() && connecting && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed()) {
312 if (connecting_to_node != StringName()) {
313 Ref<AnimationNode> node = state_machine->get_node(connecting_to_node);
314 Ref<AnimationNodeStateMachine> anodesm = node;
315 Ref<AnimationNodeEndState> end_node = node;
316
317 if (state_machine->has_transition(connecting_from, connecting_to_node) && state_machine->can_edit_node(connecting_to_node) && !anodesm.is_valid()) {
318 EditorNode::get_singleton()->show_warning(TTR("Transition exists!"));
319 connecting = false;
320 } else {
321 _add_transition();
322 }
323 } else {
324 _open_menu(mb->get_position());
325 }
326 connecting_to_node = StringName();
327 connection_follows_cursor = false;
328 state_machine_draw->queue_redraw();
329 }
330
331 // Start box selecting
332 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && tool_select->is_pressed()) {
333 box_selecting = true;
334 box_selecting_from = box_selecting_to = state_machine_draw->get_local_mouse_position();
335 box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
336 MIN(box_selecting_from.y, box_selecting_to.y),
337 ABS(box_selecting_from.x - box_selecting_to.x),
338 ABS(box_selecting_from.y - box_selecting_to.y));
339
340 if (mb->is_ctrl_pressed() || mb->is_shift_pressed()) {
341 previous_selected = selected_nodes;
342 } else {
343 selected_nodes.clear();
344 previous_selected.clear();
345 }
346 }
347
348 // End box selecting
349 if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT && !mb->is_pressed() && box_selecting) {
350 box_selecting = false;
351 state_machine_draw->queue_redraw();
352 _update_mode();
353 }
354
355 Ref<InputEventMouseMotion> mm = p_event;
356
357 // Pan window
358 if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
359 h_scroll->set_value(h_scroll->get_value() - mm->get_relative().x);
360 v_scroll->set_value(v_scroll->get_value() - mm->get_relative().y);
361 }
362
363 // Move mouse while connecting
364 if (mm.is_valid() && connecting && connection_follows_cursor && !read_only) {
365 connecting_to = mm->get_position();
366 connecting_to_node = StringName();
367 state_machine_draw->queue_redraw();
368
369 for (int i = node_rects.size() - 1; i >= 0; i--) { //inverse to draw order
370 if (node_rects[i].node_name != connecting_from && node_rects[i].node.has_point(connecting_to)) { //select node since nothing else was selected
371 connecting_to_node = node_rects[i].node_name;
372 return;
373 }
374 }
375 }
376
377 // Move mouse while moving a node
378 if (mm.is_valid() && dragging_selected_attempt && !read_only) {
379 dragging_selected = true;
380 drag_ofs = mm->get_position() - drag_from;
381 snap_x = StringName();
382 snap_y = StringName();
383 {
384 //snap
385 Vector2 cpos = state_machine->get_node_position(selected_node) + drag_ofs / EDSCALE;
386 List<StringName> nodes;
387 state_machine->get_node_list(&nodes);
388
389 float best_d_x = 1e20;
390 float best_d_y = 1e20;
391
392 for (const StringName &E : nodes) {
393 if (E == selected_node) {
394 continue;
395 }
396 Vector2 npos = state_machine->get_node_position(E);
397
398 float d_x = ABS(npos.x - cpos.x);
399 if (d_x < MIN(5, best_d_x)) {
400 drag_ofs.x -= cpos.x - npos.x;
401 best_d_x = d_x;
402 snap_x = E;
403 }
404
405 float d_y = ABS(npos.y - cpos.y);
406 if (d_y < MIN(5, best_d_y)) {
407 drag_ofs.y -= cpos.y - npos.y;
408 best_d_y = d_y;
409 snap_y = E;
410 }
411 }
412 }
413
414 state_machine_draw->queue_redraw();
415 }
416
417 // Move mouse while moving box select
418 if (mm.is_valid() && box_selecting) {
419 box_selecting_to = state_machine_draw->get_local_mouse_position();
420
421 box_selecting_rect = Rect2(MIN(box_selecting_from.x, box_selecting_to.x),
422 MIN(box_selecting_from.y, box_selecting_to.y),
423 ABS(box_selecting_from.x - box_selecting_to.x),
424 ABS(box_selecting_from.y - box_selecting_to.y));
425
426 for (int i = 0; i < node_rects.size(); i++) {
427 bool in_box = node_rects[i].node.intersects(box_selecting_rect);
428
429 if (in_box) {
430 if (previous_selected.has(node_rects[i].node_name)) {
431 selected_nodes.erase(node_rects[i].node_name);
432 } else {
433 selected_nodes.insert(node_rects[i].node_name);
434 }
435 } else {
436 if (previous_selected.has(node_rects[i].node_name)) {
437 selected_nodes.insert(node_rects[i].node_name);
438 } else {
439 selected_nodes.erase(node_rects[i].node_name);
440 }
441 }
442 }
443
444 state_machine_draw->queue_redraw();
445 }
446
447 if (mm.is_valid()) {
448 state_machine_draw->grab_focus();
449
450 String new_over_node;
451 int new_over_node_what = -1;
452 if (tool_select->is_pressed()) {
453 for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.
454
455 if (!state_machine->can_edit_node(node_rects[i].node_name)) {
456 continue; // start/end node can't be edited
457 }
458
459 if (node_rects[i].node.has_point(mm->get_position())) {
460 new_over_node = node_rects[i].node_name;
461 if (node_rects[i].play.has_point(mm->get_position())) {
462 new_over_node_what = 0;
463 } else if (node_rects[i].edit.has_point(mm->get_position())) {
464 new_over_node_what = 1;
465 }
466 break;
467 }
468 }
469 }
470
471 if (new_over_node != over_node || new_over_node_what != over_node_what) {
472 over_node = new_over_node;
473 over_node_what = new_over_node_what;
474 state_machine_draw->queue_redraw();
475 }
476
477 // set tooltip for transition
478 if (tool_select->is_pressed()) {
479 int closest = -1;
480 float closest_d = 1e20;
481 for (int i = 0; i < transition_lines.size(); i++) {
482 Vector2 s[2] = {
483 transition_lines[i].from,
484 transition_lines[i].to
485 };
486 Vector2 cpoint = Geometry2D::get_closest_point_to_segment(mm->get_position(), s);
487 float d = cpoint.distance_to(mm->get_position());
488 if (d > transition_lines[i].width) {
489 continue;
490 }
491
492 if (d < closest_d) {
493 closest = i;
494 closest_d = d;
495 }
496 }
497
498 if (closest >= 0) {
499 String from = String(transition_lines[closest].from_node);
500 String to = String(transition_lines[closest].to_node);
501 String tooltip = from + " -> " + to;
502 state_machine_draw->set_tooltip_text(tooltip);
503 } else {
504 state_machine_draw->set_tooltip_text("");
505 }
506 }
507 }
508
509 Ref<InputEventPanGesture> pan_gesture = p_event;
510 if (pan_gesture.is_valid()) {
511 h_scroll->set_value(h_scroll->get_value() + h_scroll->get_page() * pan_gesture->get_delta().x / 8);
512 v_scroll->set_value(v_scroll->get_value() + v_scroll->get_page() * pan_gesture->get_delta().y / 8);
513 }
514}
515
516Control::CursorShape AnimationNodeStateMachineEditor::get_cursor_shape(const Point2 &p_pos) const {
517 Control::CursorShape cursor_shape = get_default_cursor_shape();
518 if (!read_only) {
519 // Put ibeam (text cursor) over names to make it clearer that they are editable.
520 Transform2D xform = panel->get_transform() * state_machine_draw->get_transform();
521 Point2 pos = xform.xform_inv(p_pos);
522
523 for (int i = node_rects.size() - 1; i >= 0; i--) { // Inverse to draw order.
524 if (node_rects[i].node.has_point(pos)) {
525 if (node_rects[i].name.has_point(pos)) {
526 if (state_machine->can_edit_node(node_rects[i].node_name)) {
527 cursor_shape = Control::CURSOR_IBEAM;
528 }
529 }
530 break;
531 }
532 }
533 }
534 return cursor_shape;
535}
536
537void AnimationNodeStateMachineEditor::_open_menu(const Vector2 &p_position) {
538 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
539 if (!tree) {
540 return;
541 }
542
543 menu->clear();
544 animations_menu->clear();
545 animations_to_add.clear();
546 List<StringName> classes;
547 classes.sort_custom<StringName::AlphCompare>();
548
549 ClassDB::get_inheriters_from_class("AnimationRootNode", &classes);
550 menu->add_submenu_item(TTR("Add Animation"), "animations");
551
552 if (tree->has_node(tree->get_animation_player())) {
553 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(tree->get_node(tree->get_animation_player()));
554 if (ap) {
555 List<StringName> names;
556 ap->get_animation_list(&names);
557 for (List<StringName>::Element *E = names.front(); E; E = E->next()) {
558 animations_menu->add_icon_item(get_editor_theme_icon("Animation"), E->get());
559 animations_to_add.push_back(E->get());
560 }
561 }
562 }
563
564 for (List<StringName>::Element *E = classes.front(); E; E = E->next()) {
565 String name = String(E->get()).replace_first("AnimationNode", "");
566 if (name == "Animation" || name == "StartState" || name == "EndState") {
567 continue; // nope
568 }
569 int idx = menu->get_item_count();
570 menu->add_item(vformat(TTR("Add %s"), name), idx);
571 menu->set_item_metadata(idx, E->get());
572 }
573 Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
574
575 if (clipb.is_valid()) {
576 menu->add_separator();
577 menu->add_item(TTR("Paste"), MENU_PASTE);
578 }
579 menu->add_separator();
580 menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
581
582 menu->set_position(state_machine_draw->get_screen_transform().xform(p_position));
583 menu->popup();
584 add_node_pos = p_position / EDSCALE + state_machine->get_graph_offset();
585}
586
587bool AnimationNodeStateMachineEditor::_create_submenu(PopupMenu *p_menu, Ref<AnimationNodeStateMachine> p_nodesm, const StringName &p_name, const StringName &p_path) {
588 String prev_path;
589
590 List<StringName> nodes;
591 p_nodesm->get_node_list(&nodes);
592
593 PopupMenu *nodes_menu = memnew(PopupMenu);
594 nodes_menu->set_name(p_name);
595 nodes_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
596 p_menu->add_child(nodes_menu);
597
598 bool node_added = false;
599 for (const StringName &E : nodes) {
600 if (p_nodesm->can_edit_node(E)) {
601 Ref<AnimationNodeStateMachine> ansm = p_nodesm->get_node(E);
602
603 String path = String(p_path) + "/" + E;
604
605 if (ansm == state_machine) {
606 end_menu->add_item(E, nodes_to_connect.size());
607 nodes_to_connect.push_back(state_machine->end_node);
608 continue;
609 }
610
611 if (ansm.is_valid()) {
612 state_machine_menu->add_item(E, nodes_to_connect.size());
613 nodes_to_connect.push_back(path);
614
615 if (_create_submenu(nodes_menu, ansm, E, path)) {
616 nodes_menu->add_submenu_item(E, E);
617 node_added = true;
618 }
619 } else {
620 nodes_menu->add_item(E, nodes_to_connect.size());
621 nodes_to_connect.push_back(path);
622 node_added = true;
623 }
624 }
625 }
626
627 return node_added;
628}
629
630void AnimationNodeStateMachineEditor::_stop_connecting() {
631 connecting = false;
632 state_machine_draw->queue_redraw();
633}
634
635void AnimationNodeStateMachineEditor::_delete_selected() {
636 TreeItem *item = delete_tree->get_next_selected(nullptr);
637 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
638 while (item) {
639 if (!updating) {
640 updating = true;
641 undo_redo->create_action("Transition(s) Removed");
642 }
643
644 Vector<String> path = item->get_text(0).split(" -> ");
645
646 selected_transition_from = path[0];
647 selected_transition_to = path[1];
648 _erase_selected(true);
649
650 item = delete_tree->get_next_selected(item);
651 }
652
653 if (updating) {
654 undo_redo->commit_action();
655 updating = false;
656 }
657}
658
659void AnimationNodeStateMachineEditor::_delete_all() {
660 updating = true;
661 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
662 undo_redo->create_action("Transition(s) Removed");
663 _erase_selected(true);
664 undo_redo->commit_action();
665 updating = false;
666
667 delete_window->hide();
668}
669
670void AnimationNodeStateMachineEditor::_delete_tree_draw() {
671 TreeItem *item = delete_tree->get_next_selected(nullptr);
672 while (item) {
673 delete_window->get_cancel_button()->set_disabled(false);
674 return;
675 }
676 delete_window->get_cancel_button()->set_disabled(true);
677}
678
679void AnimationNodeStateMachineEditor::_file_opened(const String &p_file) {
680 file_loaded = ResourceLoader::load(p_file);
681 if (file_loaded.is_valid()) {
682 _add_menu_type(MENU_LOAD_FILE_CONFIRM);
683 } else {
684 EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
685 }
686}
687
688void AnimationNodeStateMachineEditor::_add_menu_type(int p_index) {
689 String base_name;
690 Ref<AnimationRootNode> node;
691
692 if (p_index == MENU_LOAD_FILE) {
693 open_file->clear_filters();
694 List<String> filters;
695 ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
696 for (const String &E : filters) {
697 open_file->add_filter("*." + E);
698 }
699 open_file->popup_file_dialog();
700 return;
701 } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
702 node = file_loaded;
703 file_loaded.unref();
704 } else if (p_index == MENU_PASTE) {
705 node = EditorSettings::get_singleton()->get_resource_clipboard();
706
707 } else {
708 String type = menu->get_item_metadata(p_index);
709
710 Object *obj = ClassDB::instantiate(type);
711 ERR_FAIL_NULL(obj);
712 AnimationNode *an = Object::cast_to<AnimationNode>(obj);
713 ERR_FAIL_NULL(an);
714
715 node = Ref<AnimationNode>(an);
716 base_name = type.replace_first("AnimationNode", "");
717 }
718
719 if (!node.is_valid()) {
720 EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
721 return;
722 }
723
724 if (base_name.is_empty()) {
725 base_name = node->get_class().replace_first("AnimationNode", "");
726 }
727
728 int base = 1;
729 String name = base_name;
730 while (state_machine->has_node(name)) {
731 base++;
732 name = base_name + " " + itos(base);
733 }
734
735 updating = true;
736 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
737 undo_redo->create_action(TTR("Add Node and Transition"));
738 undo_redo->add_do_method(state_machine.ptr(), "add_node", name, node, add_node_pos);
739 undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
740 connecting_to_node = name;
741 _add_transition(true);
742 undo_redo->commit_action();
743 updating = false;
744
745 state_machine_draw->queue_redraw();
746}
747
748void AnimationNodeStateMachineEditor::_add_animation_type(int p_index) {
749 Ref<AnimationNodeAnimation> anim;
750 anim.instantiate();
751
752 anim->set_animation(animations_to_add[p_index]);
753
754 String base_name = animations_to_add[p_index].validate_node_name();
755 int base = 1;
756 String name = base_name;
757 while (state_machine->has_node(name)) {
758 base++;
759 name = base_name + " " + itos(base);
760 }
761
762 updating = true;
763 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
764 undo_redo->create_action(TTR("Add Node and Transition"));
765 undo_redo->add_do_method(state_machine.ptr(), "add_node", name, anim, add_node_pos);
766 undo_redo->add_undo_method(state_machine.ptr(), "remove_node", name);
767 connecting_to_node = name;
768 _add_transition(true);
769 undo_redo->commit_action();
770 updating = false;
771
772 state_machine_draw->queue_redraw();
773}
774
775void AnimationNodeStateMachineEditor::_connect_to(int p_index) {
776 connecting_to_node = nodes_to_connect[p_index];
777 _add_transition();
778}
779
780void AnimationNodeStateMachineEditor::_add_transition(const bool p_nested_action) {
781 if (connecting_from != StringName() && connecting_to_node != StringName()) {
782 if (state_machine->has_transition(connecting_from, connecting_to_node)) {
783 EditorNode::get_singleton()->show_warning("Transition exists!");
784 connecting = false;
785 return;
786 }
787
788 Ref<AnimationNodeStateMachineTransition> tr;
789 tr.instantiate();
790 tr->set_advance_mode(auto_advance->is_pressed() ? AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_AUTO : AnimationNodeStateMachineTransition::AdvanceMode::ADVANCE_MODE_ENABLED);
791 tr->set_switch_mode(AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()));
792
793 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
794 if (!p_nested_action) {
795 updating = true;
796 undo_redo->create_action(TTR("Add Transition"));
797 }
798
799 undo_redo->add_do_method(state_machine.ptr(), "add_transition", connecting_from, connecting_to_node, tr);
800 undo_redo->add_undo_method(state_machine.ptr(), "remove_transition", connecting_from, connecting_to_node);
801 undo_redo->add_do_method(this, "_update_graph");
802 undo_redo->add_undo_method(this, "_update_graph");
803
804 if (!p_nested_action) {
805 undo_redo->commit_action();
806 updating = false;
807 }
808
809 selected_transition_from = connecting_from;
810 selected_transition_to = connecting_to_node;
811 selected_transition_index = transition_lines.size();
812
813 if (!state_machine->is_transition_across_group(selected_transition_index)) {
814 EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
815 } else {
816 EditorNode::get_singleton()->push_item(tr.ptr(), "", true);
817 EditorNode::get_singleton()->push_item(nullptr, "", true);
818 }
819 _update_mode();
820 }
821
822 connecting = false;
823}
824
825void AnimationNodeStateMachineEditor::_connection_draw(const Vector2 &p_from, const Vector2 &p_to, AnimationNodeStateMachineTransition::SwitchMode p_mode, bool p_enabled, bool p_selected, bool p_travel, float p_fade_ratio, bool p_auto_advance, bool p_is_across_group) {
826 Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
827 Color icon_color(1, 1, 1);
828 Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
829
830 if (!p_enabled) {
831 linecolor.a *= 0.2;
832 icon_color.a *= 0.2;
833 accent.a *= 0.6;
834 }
835
836 const Ref<Texture2D> icons[] = {
837 get_editor_theme_icon(SNAME("TransitionImmediateBig")),
838 get_editor_theme_icon(SNAME("TransitionSyncBig")),
839 get_editor_theme_icon(SNAME("TransitionEndBig")),
840 get_editor_theme_icon(SNAME("TransitionImmediateAutoBig")),
841 get_editor_theme_icon(SNAME("TransitionSyncAutoBig")),
842 get_editor_theme_icon(SNAME("TransitionEndAutoBig"))
843 };
844 const int ICON_COUNT = sizeof(icons) / sizeof(*icons);
845
846 if (p_selected) {
847 state_machine_draw->draw_line(p_from, p_to, accent, 6);
848 }
849
850 if (p_travel) {
851 linecolor = accent;
852 }
853
854 state_machine_draw->draw_line(p_from, p_to, linecolor, 2);
855
856 if (p_fade_ratio > 0.0) {
857 Color fade_linecolor = accent;
858 fade_linecolor.set_hsv(1.0, fade_linecolor.get_s(), fade_linecolor.get_v());
859 state_machine_draw->draw_line(p_from, p_from.lerp(p_to, p_fade_ratio), fade_linecolor, 2);
860 }
861 int icon_index = p_mode + (p_auto_advance ? ICON_COUNT / 2 : 0);
862 ERR_FAIL_COND(icon_index >= ICON_COUNT);
863 Ref<Texture2D> icon = icons[icon_index];
864
865 Transform2D xf;
866 xf.columns[0] = (p_to - p_from).normalized();
867 xf.columns[1] = xf.columns[0].orthogonal();
868 xf.columns[2] = (p_from + p_to) * 0.5 - xf.columns[1] * icon->get_height() * 0.5 - xf.columns[0] * icon->get_height() * 0.5;
869
870 state_machine_draw->draw_set_transform_matrix(xf);
871 if (!p_is_across_group) {
872 state_machine_draw->draw_texture(icon, Vector2(), icon_color);
873 }
874 state_machine_draw->draw_set_transform_matrix(Transform2D());
875}
876
877void AnimationNodeStateMachineEditor::_clip_src_line_to_rect(Vector2 &r_from, const Vector2 &p_to, const Rect2 &p_rect) {
878 if (p_to == r_from) {
879 return;
880 }
881
882 //this could be optimized...
883 Vector2 n = (p_to - r_from).normalized();
884 while (p_rect.has_point(r_from)) {
885 r_from += n;
886 }
887}
888
889void AnimationNodeStateMachineEditor::_clip_dst_line_to_rect(const Vector2 &p_from, Vector2 &r_to, const Rect2 &p_rect) {
890 if (r_to == p_from) {
891 return;
892 }
893
894 //this could be optimized...
895 Vector2 n = (r_to - p_from).normalized();
896 while (p_rect.has_point(r_to)) {
897 r_to -= n;
898 }
899}
900
901void AnimationNodeStateMachineEditor::_state_machine_draw() {
902 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
903 if (!tree) {
904 return;
905 }
906
907 Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
908
909 Ref<StyleBoxFlat> style = get_theme_stylebox(SNAME("state_machine_frame"), SNAME("GraphNode"));
910 Ref<StyleBoxFlat> style_selected = get_theme_stylebox(SNAME("state_machine_selected_frame"), SNAME("GraphNode"));
911
912 Ref<Font> font = get_theme_font(SNAME("title_font"), SNAME("GraphNode"));
913 int font_size = get_theme_font_size(SNAME("title_font_size"), SNAME("GraphNode"));
914 Color font_color = get_theme_color(SNAME("title_color"), SNAME("GraphNode"));
915 Ref<Texture2D> play = get_editor_theme_icon(SNAME("Play"));
916 Ref<Texture2D> edit = get_editor_theme_icon(SNAME("Edit"));
917 Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
918 Color linecolor = get_theme_color(SNAME("font_color"), SNAME("Label"));
919 linecolor.a *= 0.3;
920 Ref<StyleBox> playing_overlay = get_theme_stylebox(SNAME("position"), SNAME("GraphNode"));
921
922 Ref<StyleBoxFlat> start_overlay = style->duplicate();
923 start_overlay->set_border_width_all(1 * EDSCALE);
924 start_overlay->set_border_color(Color::html("#80f6cf"));
925
926 Ref<StyleBoxFlat> end_overlay = style->duplicate();
927 end_overlay->set_border_width_all(1 * EDSCALE);
928 end_overlay->set_border_color(Color::html("#f26661"));
929
930 bool playing = false;
931 StringName current;
932 StringName blend_from;
933 Vector<StringName> travel_path;
934
935 if (playback.is_valid()) {
936 playing = playback->is_playing();
937 current = playback->get_current_node();
938 blend_from = playback->get_fading_from_node();
939 travel_path = playback->get_travel_path();
940 }
941
942 if (state_machine_draw->has_focus()) {
943 state_machine_draw->draw_rect(Rect2(Point2(), state_machine_draw->get_size()), accent, false);
944 }
945 int sep = 3 * EDSCALE;
946
947 List<StringName> nodes;
948 state_machine->get_node_list(&nodes);
949
950 node_rects.clear();
951 Rect2 scroll_range;
952
953 //snap lines
954 if (dragging_selected) {
955 Vector2 from = (state_machine->get_node_position(selected_node) * EDSCALE) + drag_ofs - state_machine->get_graph_offset() * EDSCALE;
956 if (snap_x != StringName()) {
957 Vector2 to = (state_machine->get_node_position(snap_x) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
958 state_machine_draw->draw_line(from, to, linecolor, 2);
959 }
960 if (snap_y != StringName()) {
961 Vector2 to = (state_machine->get_node_position(snap_y) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
962 state_machine_draw->draw_line(from, to, linecolor, 2);
963 }
964 }
965
966 //pre pass nodes so we know the rectangles
967 for (const StringName &E : nodes) {
968 Ref<AnimationNode> anode = state_machine->get_node(E);
969 String name = E;
970 bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);
971 Ref<StyleBox> sb = selected_nodes.has(E) ? style_selected : style;
972
973 Size2 s = sb->get_minimum_size();
974 int strsize = font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
975 s.width += strsize;
976 s.height += MAX(font->get_height(font_size), play->get_height());
977 s.width += sep + play->get_width();
978
979 if (needs_editor) {
980 s.width += sep + edit->get_width();
981 }
982
983 Vector2 offset;
984 offset += state_machine->get_node_position(E) * EDSCALE;
985
986 if (selected_nodes.has(E) && dragging_selected) {
987 offset += drag_ofs;
988 }
989
990 offset -= s / 2;
991 offset = offset.floor();
992
993 //prepre rect
994
995 NodeRect nr;
996 nr.node = Rect2(offset, s);
997 nr.node_name = E;
998
999 scroll_range = scroll_range.merge(nr.node); //merge with range
1000
1001 //now scroll it to draw
1002 nr.node.position -= state_machine->get_graph_offset() * EDSCALE;
1003
1004 node_rects.push_back(nr);
1005 }
1006
1007 transition_lines.clear();
1008
1009 //draw connecting line for potential new transition
1010 if (connecting) {
1011 Vector2 from = (state_machine->get_node_position(connecting_from) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
1012 Vector2 to;
1013 if (connecting_to_node != StringName()) {
1014 to = (state_machine->get_node_position(connecting_to_node) * EDSCALE) - state_machine->get_graph_offset() * EDSCALE;
1015 } else {
1016 to = connecting_to;
1017 }
1018
1019 for (int i = 0; i < node_rects.size(); i++) {
1020 if (node_rects[i].node_name == connecting_from) {
1021 _clip_src_line_to_rect(from, to, node_rects[i].node);
1022 }
1023 if (node_rects[i].node_name == connecting_to_node) {
1024 _clip_dst_line_to_rect(from, to, node_rects[i].node);
1025 }
1026 }
1027
1028 _connection_draw(from, to, AnimationNodeStateMachineTransition::SwitchMode(switch_mode->get_selected()), true, false, false, 0.0, false, false);
1029 }
1030
1031 Ref<Texture2D> tr_reference_icon = get_editor_theme_icon(SNAME("TransitionImmediateBig"));
1032 float tr_bidi_offset = int(tr_reference_icon->get_height() * 0.8);
1033
1034 //draw transition lines
1035 for (int i = 0; i < state_machine->get_transition_count(); i++) {
1036 TransitionLine tl;
1037 tl.transition_index = i;
1038
1039 tl.from_node = state_machine->get_transition_from(i);
1040 Vector2 ofs_from = (dragging_selected && selected_nodes.has(tl.from_node)) ? drag_ofs : Vector2();
1041 tl.from = (state_machine->get_node_position(tl.from_node) * EDSCALE) + ofs_from - state_machine->get_graph_offset() * EDSCALE;
1042
1043 tl.to_node = state_machine->get_transition_to(i);
1044 Vector2 ofs_to = (dragging_selected && selected_nodes.has(tl.to_node)) ? drag_ofs : Vector2();
1045 tl.to = (state_machine->get_node_position(tl.to_node) * EDSCALE) + ofs_to - state_machine->get_graph_offset() * EDSCALE;
1046
1047 Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(i);
1048 tl.disabled = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED);
1049 tl.auto_advance = bool(tr->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO);
1050 tl.advance_condition_name = tr->get_advance_condition_name();
1051 tl.advance_condition_state = false;
1052 tl.mode = tr->get_switch_mode();
1053 tl.width = tr_bidi_offset;
1054 tl.travel = false;
1055 tl.fade_ratio = 0.0;
1056 tl.hidden = false;
1057 tl.is_across_group = state_machine->is_transition_across_group(i);
1058
1059 if (state_machine->has_transition(tl.to_node, tl.from_node)) { //offset if same exists
1060 Vector2 offset = -(tl.from - tl.to).normalized().orthogonal() * tr_bidi_offset;
1061 tl.from += offset;
1062 tl.to += offset;
1063 }
1064
1065 for (int j = 0; j < node_rects.size(); j++) {
1066 if (node_rects[j].node_name == tl.from_node) {
1067 _clip_src_line_to_rect(tl.from, tl.to, node_rects[j].node);
1068 }
1069 if (node_rects[j].node_name == tl.to_node) {
1070 _clip_dst_line_to_rect(tl.from, tl.to, node_rects[j].node);
1071 }
1072 }
1073
1074 tl.selected = selected_transition_from == tl.from_node && selected_transition_to == tl.to_node;
1075
1076 if (blend_from == tl.from_node && current == tl.to_node) {
1077 tl.travel = true;
1078 tl.fade_ratio = MIN(1.0, fading_pos / fading_time);
1079 }
1080
1081 if (travel_path.size()) {
1082 if (current == tl.from_node && travel_path[0] == tl.to_node) {
1083 tl.travel = true;
1084 } else {
1085 for (int j = 0; j < travel_path.size() - 1; j++) {
1086 if (travel_path[j] == tl.from_node && travel_path[j + 1] == tl.to_node) {
1087 tl.travel = true;
1088 break;
1089 }
1090 }
1091 }
1092 }
1093
1094 StringName fullpath = AnimationTreeEditor::get_singleton()->get_base_path() + String(tl.advance_condition_name);
1095 if (tl.advance_condition_name != StringName() && bool(tree->get(fullpath))) {
1096 tl.advance_condition_state = true;
1097 tl.auto_advance = true;
1098 }
1099
1100 // check if already have this transition
1101 for (int j = 0; j < transition_lines.size(); j++) {
1102 if (transition_lines[j].from_node == tl.from_node && transition_lines[j].to_node == tl.to_node) {
1103 tl.hidden = true;
1104 transition_lines.write[j].disabled = transition_lines[j].disabled && tl.disabled;
1105 }
1106 }
1107 transition_lines.push_back(tl);
1108 }
1109
1110 for (int i = 0; i < transition_lines.size(); i++) {
1111 TransitionLine tl = transition_lines[i];
1112 if (!tl.hidden) {
1113 _connection_draw(tl.from, tl.to, tl.mode, !tl.disabled, tl.selected, tl.travel, tl.fade_ratio, tl.auto_advance, tl.is_across_group);
1114 }
1115 }
1116
1117 //draw actual nodes
1118 for (int i = 0; i < node_rects.size(); i++) {
1119 String name = node_rects[i].node_name;
1120 Ref<AnimationNode> anode = state_machine->get_node(name);
1121 bool needs_editor = AnimationTreeEditor::get_singleton()->can_edit(anode);
1122 Ref<StyleBox> sb = selected_nodes.has(name) ? style_selected : style;
1123 int strsize = font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).width;
1124 NodeRect &nr = node_rects.write[i];
1125
1126 Vector2 offset = nr.node.position;
1127 int h = nr.node.size.height;
1128
1129 //prepre rect
1130
1131 //now scroll it to draw
1132 state_machine_draw->draw_style_box(sb, nr.node);
1133
1134 if (state_machine->start_node == name) {
1135 state_machine_draw->draw_style_box(sb == style_selected ? style_selected : start_overlay, nr.node);
1136 }
1137
1138 if (state_machine->end_node == name) {
1139 state_machine_draw->draw_style_box(sb == style_selected ? style_selected : end_overlay, nr.node);
1140 }
1141
1142 if (playing && (blend_from == name || current == name || travel_path.has(name))) {
1143 state_machine_draw->draw_style_box(playing_overlay, nr.node);
1144 }
1145
1146 offset.x += sb->get_offset().x;
1147
1148 nr.play.position = offset + Vector2(0, (h - play->get_height()) / 2).floor();
1149 nr.play.size = play->get_size();
1150
1151 Ref<Texture2D> play_tex = play;
1152
1153 if (over_node == name && over_node_what == 0) {
1154 state_machine_draw->draw_texture(play_tex, nr.play.position, accent);
1155 } else {
1156 state_machine_draw->draw_texture(play_tex, nr.play.position);
1157 }
1158
1159 offset.x += sep + play->get_width();
1160
1161 nr.name.position = offset + Vector2(0, (h - font->get_height(font_size)) / 2).floor();
1162 nr.name.size = Vector2(strsize, font->get_height(font_size));
1163
1164 state_machine_draw->draw_string(font, nr.name.position + Vector2(0, font->get_ascent(font_size)), name, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, font_color);
1165 offset.x += strsize + sep;
1166
1167 nr.can_edit = needs_editor;
1168 if (needs_editor) {
1169 nr.edit.position = offset + Vector2(0, (h - edit->get_height()) / 2).floor();
1170 nr.edit.size = edit->get_size();
1171
1172 if (over_node == name && over_node_what == 1) {
1173 state_machine_draw->draw_texture(edit, nr.edit.position, accent);
1174 } else {
1175 state_machine_draw->draw_texture(edit, nr.edit.position);
1176 }
1177 }
1178 }
1179
1180 //draw box select
1181 if (box_selecting) {
1182 state_machine_draw->draw_rect(box_selecting_rect, Color(0.7, 0.7, 1.0, 0.3));
1183 }
1184
1185 scroll_range.position -= state_machine_draw->get_size();
1186 scroll_range.size += state_machine_draw->get_size() * 2.0;
1187
1188 //adjust scrollbars
1189 updating = true;
1190 h_scroll->set_min(scroll_range.position.x);
1191 h_scroll->set_max(scroll_range.position.x + scroll_range.size.x);
1192 h_scroll->set_page(state_machine_draw->get_size().x);
1193 h_scroll->set_value(state_machine->get_graph_offset().x);
1194
1195 v_scroll->set_min(scroll_range.position.y);
1196 v_scroll->set_max(scroll_range.position.y + scroll_range.size.y);
1197 v_scroll->set_page(state_machine_draw->get_size().y);
1198 v_scroll->set_value(state_machine->get_graph_offset().y);
1199 updating = false;
1200
1201 state_machine_play_pos->queue_redraw();
1202}
1203
1204void AnimationNodeStateMachineEditor::_state_machine_pos_draw_individual(String p_name, float p_ratio) {
1205 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
1206 if (!tree) {
1207 return;
1208 }
1209
1210 Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
1211 if (!playback.is_valid() || !playback->is_playing()) {
1212 return;
1213 }
1214
1215 if (p_name == state_machine->start_node || p_name == state_machine->end_node || p_name.is_empty()) {
1216 return;
1217 }
1218
1219 int idx = -1;
1220 for (int i = 0; i < node_rects.size(); i++) {
1221 if (node_rects[i].node_name == p_name) {
1222 idx = i;
1223 break;
1224 }
1225 }
1226
1227 if (idx == -1) {
1228 return;
1229 }
1230
1231 const NodeRect &nr = node_rects[idx];
1232
1233 if (nr.can_edit) {
1234 return; // It is not AnimationNodeAnimation.
1235 }
1236
1237 Vector2 from;
1238 from.x = nr.play.position.x;
1239 from.y = (nr.play.position.y + nr.play.size.y + nr.node.position.y + nr.node.size.y) * 0.5;
1240
1241 Vector2 to;
1242 if (nr.edit.size.x) {
1243 to.x = nr.edit.position.x + nr.edit.size.x;
1244 } else {
1245 to.x = nr.name.position.x + nr.name.size.x;
1246 }
1247 to.y = from.y;
1248
1249 float c = p_ratio;
1250 Color fg = get_theme_color(SNAME("font_color"), SNAME("Label"));
1251 Color bg = fg;
1252 bg.a *= 0.3;
1253
1254 state_machine_play_pos->draw_line(from, to, bg, 2);
1255
1256 to = from.lerp(to, c);
1257
1258 state_machine_play_pos->draw_line(from, to, fg, 2);
1259}
1260
1261void AnimationNodeStateMachineEditor::_state_machine_pos_draw_all() {
1262 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
1263 if (!tree) {
1264 return;
1265 }
1266
1267 Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
1268 if (!playback.is_valid() || !playback->is_playing()) {
1269 return;
1270 }
1271
1272 {
1273 float len = MAX(0.0001, current_length);
1274 float pos = CLAMP(current_play_pos, 0, len);
1275 float c = current_length == HUGE_LENGTH ? 1 : (pos / len);
1276 _state_machine_pos_draw_individual(playback->get_current_node(), c);
1277 }
1278
1279 {
1280 float len = MAX(0.0001, fade_from_length);
1281 float pos = CLAMP(fade_from_current_play_pos, 0, len);
1282 float c = fade_from_length == HUGE_LENGTH ? 1 : (pos / len);
1283 _state_machine_pos_draw_individual(playback->get_fading_from_node(), c);
1284 }
1285}
1286
1287void AnimationNodeStateMachineEditor::_update_graph() {
1288 if (updating) {
1289 return;
1290 }
1291
1292 updating = true;
1293
1294 state_machine_draw->queue_redraw();
1295
1296 updating = false;
1297}
1298
1299void AnimationNodeStateMachineEditor::_notification(int p_what) {
1300 switch (p_what) {
1301 case NOTIFICATION_ENTER_TREE:
1302 case NOTIFICATION_THEME_CHANGED:
1303 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
1304 case NOTIFICATION_TRANSLATION_CHANGED: {
1305 error_panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
1306 error_label->add_theme_color_override("font_color", get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
1307 panel->add_theme_style_override("panel", get_theme_stylebox(SNAME("panel"), SNAME("Tree")));
1308
1309 tool_select->set_icon(get_editor_theme_icon(SNAME("ToolSelect")));
1310 tool_create->set_icon(get_editor_theme_icon(SNAME("ToolAddNode")));
1311 tool_connect->set_icon(get_editor_theme_icon(SNAME("ToolConnect")));
1312
1313 switch_mode->clear();
1314 switch_mode->add_icon_item(get_editor_theme_icon(SNAME("TransitionImmediate")), TTR("Immediate"));
1315 switch_mode->add_icon_item(get_editor_theme_icon(SNAME("TransitionSync")), TTR("Sync"));
1316 switch_mode->add_icon_item(get_editor_theme_icon(SNAME("TransitionEnd")), TTR("At End"));
1317
1318 auto_advance->set_icon(get_editor_theme_icon(SNAME("AutoPlay")));
1319
1320 tool_erase->set_icon(get_editor_theme_icon(SNAME("Remove")));
1321
1322 play_mode->clear();
1323 play_mode->add_icon_item(get_editor_theme_icon(SNAME("PlayTravel")), TTR("Travel"));
1324 play_mode->add_icon_item(get_editor_theme_icon(SNAME("Play")), TTR("Immediate"));
1325 } break;
1326
1327 case NOTIFICATION_PROCESS: {
1328 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
1329 if (!tree) {
1330 return;
1331 }
1332
1333 String error;
1334
1335 Ref<AnimationNodeStateMachinePlayback> playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
1336
1337 if (error_time > 0) {
1338 error = error_text;
1339 error_time -= get_process_delta_time();
1340 } else if (!tree->is_active()) {
1341 error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
1342 } else if (tree->is_state_invalid()) {
1343 error = tree->get_invalid_state_reason();
1344 } else if (playback.is_null()) {
1345 error = vformat(TTR("No playback resource set at path: %s."), AnimationTreeEditor::get_singleton()->get_base_path() + "playback");
1346 }
1347
1348 if (error != error_label->get_text()) {
1349 error_label->set_text(error);
1350 if (!error.is_empty()) {
1351 error_panel->show();
1352 } else {
1353 error_panel->hide();
1354 }
1355 }
1356
1357 for (int i = 0; i < transition_lines.size(); i++) {
1358 int tidx = -1;
1359 for (int j = 0; j < state_machine->get_transition_count(); j++) {
1360 if (transition_lines[i].from_node == state_machine->get_transition_from(j) && transition_lines[i].to_node == state_machine->get_transition_to(j)) {
1361 tidx = j;
1362 break;
1363 }
1364 }
1365
1366 if (tidx == -1) { //missing transition, should redraw
1367 state_machine_draw->queue_redraw();
1368 break;
1369 }
1370
1371 if (transition_lines[i].disabled != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_DISABLED)) {
1372 state_machine_draw->queue_redraw();
1373 break;
1374 }
1375
1376 if (transition_lines[i].auto_advance != bool(state_machine->get_transition(tidx)->get_advance_mode() == AnimationNodeStateMachineTransition::ADVANCE_MODE_AUTO)) {
1377 state_machine_draw->queue_redraw();
1378 break;
1379 }
1380
1381 if (transition_lines[i].advance_condition_name != state_machine->get_transition(tidx)->get_advance_condition_name()) {
1382 state_machine_draw->queue_redraw();
1383 break;
1384 }
1385
1386 if (transition_lines[i].mode != state_machine->get_transition(tidx)->get_switch_mode()) {
1387 state_machine_draw->queue_redraw();
1388 break;
1389 }
1390
1391 bool acstate = transition_lines[i].advance_condition_name != StringName() && bool(tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + String(transition_lines[i].advance_condition_name)));
1392
1393 if (transition_lines[i].advance_condition_state != acstate) {
1394 state_machine_draw->queue_redraw();
1395 break;
1396 }
1397 }
1398
1399 bool same_travel_path = true;
1400 Vector<StringName> tp;
1401 bool is_playing = false;
1402 StringName current_node;
1403 StringName fading_from_node;
1404
1405 current_play_pos = 0;
1406 current_length = 0;
1407
1408 fade_from_current_play_pos = 0;
1409 fade_from_length = 0;
1410
1411 fading_time = 0;
1412 fading_pos = 0;
1413
1414 if (playback.is_valid()) {
1415 tp = playback->get_travel_path();
1416 is_playing = playback->is_playing();
1417 current_node = playback->get_current_node();
1418 fading_from_node = playback->get_fading_from_node();
1419 current_play_pos = playback->get_current_play_pos();
1420 current_length = playback->get_current_length();
1421 fade_from_current_play_pos = playback->get_fade_from_play_pos();
1422 fade_from_length = playback->get_fade_from_length();
1423 fading_time = playback->get_fading_time();
1424 fading_pos = playback->get_fading_pos();
1425 }
1426
1427 {
1428 if (last_travel_path.size() != tp.size()) {
1429 same_travel_path = false;
1430 } else {
1431 for (int i = 0; i < last_travel_path.size(); i++) {
1432 if (last_travel_path[i] != tp[i]) {
1433 same_travel_path = false;
1434 break;
1435 }
1436 }
1437 }
1438 }
1439
1440 //redraw if travel state changed
1441 if (!same_travel_path ||
1442 last_active != is_playing ||
1443 last_current_node != current_node ||
1444 last_fading_from_node != fading_from_node ||
1445 last_fading_time != fading_time ||
1446 last_fading_pos != fading_pos) {
1447 state_machine_draw->queue_redraw();
1448 last_travel_path = tp;
1449 last_current_node = current_node;
1450 last_active = is_playing;
1451 last_fading_from_node = fading_from_node;
1452 last_fading_time = fading_time;
1453 last_fading_pos = fading_pos;
1454 state_machine_play_pos->queue_redraw();
1455 }
1456
1457 {
1458 if (current_node != StringName() && state_machine->has_node(current_node)) {
1459 String next = current_node;
1460 Ref<AnimationNodeStateMachine> anodesm = state_machine->get_node(next);
1461 Ref<AnimationNodeStateMachinePlayback> current_node_playback;
1462
1463 while (anodesm.is_valid()) {
1464 current_node_playback = tree->get(AnimationTreeEditor::get_singleton()->get_base_path() + next + "/playback");
1465 StringName cnode = current_node_playback->get_current_node();
1466 next += "/" + cnode;
1467 if (!anodesm->has_node(cnode)) {
1468 break;
1469 }
1470 anodesm = anodesm->get_node(cnode);
1471 }
1472
1473 // when current_node is a state machine, use playback of current_node to set play_pos
1474 if (current_node_playback.is_valid()) {
1475 current_play_pos = current_node_playback->get_current_play_pos();
1476 current_length = current_node_playback->get_current_length();
1477 }
1478 }
1479 }
1480
1481 if (last_play_pos != current_play_pos || fade_from_last_play_pos != fade_from_current_play_pos) {
1482 last_play_pos = current_play_pos;
1483 fade_from_last_play_pos = fade_from_current_play_pos;
1484 state_machine_play_pos->queue_redraw();
1485 }
1486 } break;
1487
1488 case NOTIFICATION_VISIBILITY_CHANGED: {
1489 over_node = StringName();
1490 set_process(is_visible_in_tree());
1491 } break;
1492 }
1493}
1494
1495void AnimationNodeStateMachineEditor::_open_editor(const String &p_name) {
1496 AnimationTreeEditor::get_singleton()->enter_editor(p_name);
1497}
1498
1499void AnimationNodeStateMachineEditor::_name_edited(const String &p_text) {
1500 const String &new_name = p_text;
1501
1502 ERR_FAIL_COND(new_name.is_empty() || new_name.contains(".") || new_name.contains("/"));
1503
1504 if (new_name == prev_name) {
1505 return; // Nothing to do.
1506 }
1507
1508 const String &base_name = new_name;
1509 int base = 1;
1510 String name = base_name;
1511 while (state_machine->has_node(name)) {
1512 if (name == prev_name) {
1513 name_edit_popup->hide(); // The old name wins, the name doesn't change, just hide the popup.
1514 return;
1515 }
1516 base++;
1517 name = base_name + " " + itos(base);
1518 }
1519
1520 updating = true;
1521 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1522 undo_redo->create_action(TTR("Node Renamed"));
1523 undo_redo->add_do_method(state_machine.ptr(), "rename_node", prev_name, name);
1524 undo_redo->add_undo_method(state_machine.ptr(), "rename_node", name, prev_name);
1525 undo_redo->add_do_method(this, "_update_graph");
1526 undo_redo->add_undo_method(this, "_update_graph");
1527 undo_redo->commit_action();
1528 name_edit_popup->hide();
1529 updating = false;
1530
1531 state_machine_draw->queue_redraw();
1532}
1533
1534void AnimationNodeStateMachineEditor::_name_edited_focus_out() {
1535 if (updating) {
1536 return;
1537 }
1538
1539 _name_edited(name_edit->get_text());
1540}
1541
1542void AnimationNodeStateMachineEditor::_scroll_changed(double) {
1543 if (updating) {
1544 return;
1545 }
1546
1547 state_machine->set_graph_offset(Vector2(h_scroll->get_value(), v_scroll->get_value()));
1548 state_machine_draw->queue_redraw();
1549}
1550
1551void AnimationNodeStateMachineEditor::_erase_selected(const bool p_nested_action) {
1552 if (!selected_nodes.is_empty()) {
1553 if (!p_nested_action) {
1554 updating = true;
1555 }
1556 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1557 undo_redo->create_action(TTR("Node Removed"));
1558
1559 for (int i = 0; i < node_rects.size(); i++) {
1560 if (node_rects[i].node_name == state_machine->start_node || node_rects[i].node_name == state_machine->end_node) {
1561 continue;
1562 }
1563
1564 if (!selected_nodes.has(node_rects[i].node_name)) {
1565 continue;
1566 }
1567
1568 undo_redo->add_do_method(state_machine.ptr(), "remove_node", node_rects[i].node_name);
1569 undo_redo->add_undo_method(state_machine.ptr(), "add_node", node_rects[i].node_name,
1570 state_machine->get_node(node_rects[i].node_name),
1571 state_machine->get_node_position(node_rects[i].node_name));
1572
1573 for (int j = 0; j < state_machine->get_transition_count(); j++) {
1574 String from = state_machine->get_transition_from(j);
1575 String to = state_machine->get_transition_to(j);
1576 if (from == node_rects[i].node_name || to == node_rects[i].node_name) {
1577 undo_redo->add_undo_method(state_machine.ptr(), "add_transition", from, to, state_machine->get_transition(j));
1578 }
1579 }
1580 }
1581
1582 undo_redo->add_do_method(this, "_update_graph");
1583 undo_redo->add_undo_method(this, "_update_graph");
1584 undo_redo->commit_action();
1585
1586 if (!p_nested_action) {
1587 updating = false;
1588 }
1589
1590 selected_nodes.clear();
1591 }
1592
1593 if (selected_transition_to != StringName() && selected_transition_from != StringName() && state_machine->has_transition(selected_transition_from, selected_transition_to)) {
1594 Ref<AnimationNodeStateMachineTransition> tr = state_machine->get_transition(state_machine->find_transition(selected_transition_from, selected_transition_to));
1595 if (!p_nested_action) {
1596 updating = true;
1597 }
1598 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1599 undo_redo->create_action(TTR("Transition Removed"));
1600 undo_redo->add_do_method(state_machine.ptr(), "remove_transition", selected_transition_from, selected_transition_to);
1601 undo_redo->add_undo_method(state_machine.ptr(), "add_transition", selected_transition_from, selected_transition_to, tr);
1602 undo_redo->add_do_method(this, "_update_graph");
1603 undo_redo->add_undo_method(this, "_update_graph");
1604 undo_redo->commit_action();
1605 if (!p_nested_action) {
1606 updating = false;
1607 }
1608 selected_transition_from = StringName();
1609 selected_transition_to = StringName();
1610 selected_transition_index = -1;
1611 }
1612
1613 state_machine_draw->queue_redraw();
1614}
1615
1616void AnimationNodeStateMachineEditor::_update_mode() {
1617 if (tool_select->is_pressed()) {
1618 selection_tools_hb->show();
1619 bool nothing_selected = selected_nodes.is_empty() && selected_transition_from == StringName() && selected_transition_to == StringName();
1620 bool start_end_selected = selected_nodes.size() == 1 && (*selected_nodes.begin() == state_machine->start_node || *selected_nodes.begin() == state_machine->end_node);
1621 tool_erase->set_disabled(nothing_selected || start_end_selected || read_only);
1622 } else {
1623 selection_tools_hb->hide();
1624 }
1625
1626 if (tool_connect->is_pressed()) {
1627 transition_tools_hb->show();
1628 } else {
1629 transition_tools_hb->hide();
1630 }
1631}
1632
1633void AnimationNodeStateMachineEditor::_bind_methods() {
1634 ClassDB::bind_method("_update_graph", &AnimationNodeStateMachineEditor::_update_graph);
1635 ClassDB::bind_method("_open_editor", &AnimationNodeStateMachineEditor::_open_editor);
1636 ClassDB::bind_method("_connect_to", &AnimationNodeStateMachineEditor::_connect_to);
1637 ClassDB::bind_method("_stop_connecting", &AnimationNodeStateMachineEditor::_stop_connecting);
1638 ClassDB::bind_method("_delete_selected", &AnimationNodeStateMachineEditor::_delete_selected);
1639 ClassDB::bind_method("_delete_all", &AnimationNodeStateMachineEditor::_delete_all);
1640 ClassDB::bind_method("_delete_tree_draw", &AnimationNodeStateMachineEditor::_delete_tree_draw);
1641}
1642
1643AnimationNodeStateMachineEditor *AnimationNodeStateMachineEditor::singleton = nullptr;
1644
1645AnimationNodeStateMachineEditor::AnimationNodeStateMachineEditor() {
1646 singleton = this;
1647
1648 HBoxContainer *top_hb = memnew(HBoxContainer);
1649 add_child(top_hb);
1650
1651 Ref<ButtonGroup> bg;
1652 bg.instantiate();
1653
1654 tool_select = memnew(Button);
1655 tool_select->set_flat(true);
1656 top_hb->add_child(tool_select);
1657 tool_select->set_toggle_mode(true);
1658 tool_select->set_button_group(bg);
1659 tool_select->set_pressed(true);
1660 tool_select->set_tooltip_text(TTR("Select and move nodes.\nRMB: Add node at position clicked.\nShift+LMB+Drag: Connects the selected node with another node or creates a new node if you select an area without nodes."));
1661 tool_select->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);
1662
1663 tool_create = memnew(Button);
1664 tool_create->set_flat(true);
1665 top_hb->add_child(tool_create);
1666 tool_create->set_toggle_mode(true);
1667 tool_create->set_button_group(bg);
1668 tool_create->set_tooltip_text(TTR("Create new nodes."));
1669 tool_create->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);
1670
1671 tool_connect = memnew(Button);
1672 tool_connect->set_flat(true);
1673 top_hb->add_child(tool_connect);
1674 tool_connect->set_toggle_mode(true);
1675 tool_connect->set_button_group(bg);
1676 tool_connect->set_tooltip_text(TTR("Connect nodes."));
1677 tool_connect->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_update_mode), CONNECT_DEFERRED);
1678
1679 // Context-sensitive selection tools:
1680 selection_tools_hb = memnew(HBoxContainer);
1681 top_hb->add_child(selection_tools_hb);
1682 selection_tools_hb->add_child(memnew(VSeparator));
1683
1684 tool_erase = memnew(Button);
1685 tool_erase->set_flat(true);
1686 tool_erase->set_tooltip_text(TTR("Remove selected node or transition."));
1687 tool_erase->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_erase_selected).bind(false));
1688 tool_erase->set_disabled(true);
1689 selection_tools_hb->add_child(tool_erase);
1690
1691 transition_tools_hb = memnew(HBoxContainer);
1692 top_hb->add_child(transition_tools_hb);
1693 transition_tools_hb->add_child(memnew(VSeparator));
1694
1695 transition_tools_hb->add_child(memnew(Label(TTR("Transition:"))));
1696 switch_mode = memnew(OptionButton);
1697 transition_tools_hb->add_child(switch_mode);
1698
1699 auto_advance = memnew(Button);
1700 auto_advance->set_flat(true);
1701 auto_advance->set_tooltip_text(TTR("New Transitions Should Auto Advance"));
1702 auto_advance->set_toggle_mode(true);
1703 auto_advance->set_pressed(true);
1704 transition_tools_hb->add_child(auto_advance);
1705
1706 //
1707
1708 top_hb->add_spacer();
1709
1710 top_hb->add_child(memnew(Label(TTR("Play Mode:"))));
1711 play_mode = memnew(OptionButton);
1712 top_hb->add_child(play_mode);
1713
1714 panel = memnew(PanelContainer);
1715 panel->set_clip_contents(true);
1716 panel->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1717 add_child(panel);
1718 panel->set_v_size_flags(SIZE_EXPAND_FILL);
1719
1720 state_machine_draw = memnew(Control);
1721 panel->add_child(state_machine_draw);
1722 state_machine_draw->connect("gui_input", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_gui_input));
1723 state_machine_draw->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_draw));
1724 state_machine_draw->set_focus_mode(FOCUS_ALL);
1725 state_machine_draw->set_mouse_filter(Control::MOUSE_FILTER_PASS);
1726
1727 state_machine_play_pos = memnew(Control);
1728 state_machine_draw->add_child(state_machine_play_pos);
1729 state_machine_play_pos->set_mouse_filter(MOUSE_FILTER_PASS); //pass all to parent
1730 state_machine_play_pos->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
1731 state_machine_play_pos->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_state_machine_pos_draw_all));
1732
1733 v_scroll = memnew(VScrollBar);
1734 state_machine_draw->add_child(v_scroll);
1735 v_scroll->set_anchors_and_offsets_preset(PRESET_RIGHT_WIDE);
1736 v_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
1737
1738 h_scroll = memnew(HScrollBar);
1739 state_machine_draw->add_child(h_scroll);
1740 h_scroll->set_anchors_and_offsets_preset(PRESET_BOTTOM_WIDE);
1741 h_scroll->set_offset(SIDE_RIGHT, -v_scroll->get_size().x * EDSCALE);
1742 h_scroll->connect("value_changed", callable_mp(this, &AnimationNodeStateMachineEditor::_scroll_changed));
1743
1744 error_panel = memnew(PanelContainer);
1745 add_child(error_panel);
1746 error_label = memnew(Label);
1747 error_panel->add_child(error_label);
1748 error_panel->hide();
1749
1750 set_custom_minimum_size(Size2(0, 300 * EDSCALE));
1751
1752 menu = memnew(PopupMenu);
1753 add_child(menu);
1754 menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_menu_type));
1755 menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));
1756
1757 animations_menu = memnew(PopupMenu);
1758 menu->add_child(animations_menu);
1759 animations_menu->set_name("animations");
1760 animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_add_animation_type));
1761
1762 connect_menu = memnew(PopupMenu);
1763 add_child(connect_menu);
1764 connect_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
1765 connect_menu->connect("popup_hide", callable_mp(this, &AnimationNodeStateMachineEditor::_stop_connecting));
1766
1767 state_machine_menu = memnew(PopupMenu);
1768 state_machine_menu->set_name("state_machines");
1769 state_machine_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
1770 connect_menu->add_child(state_machine_menu);
1771
1772 end_menu = memnew(PopupMenu);
1773 end_menu->set_name("end_nodes");
1774 end_menu->connect("id_pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_connect_to));
1775 connect_menu->add_child(end_menu);
1776
1777 name_edit_popup = memnew(Popup);
1778 add_child(name_edit_popup);
1779 name_edit = memnew(LineEdit);
1780 name_edit_popup->add_child(name_edit);
1781 name_edit->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
1782 name_edit->connect("text_submitted", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited));
1783 name_edit->connect("focus_exited", callable_mp(this, &AnimationNodeStateMachineEditor::_name_edited_focus_out));
1784
1785 open_file = memnew(EditorFileDialog);
1786 add_child(open_file);
1787 open_file->set_title(TTR("Open Animation Node"));
1788 open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
1789 open_file->connect("file_selected", callable_mp(this, &AnimationNodeStateMachineEditor::_file_opened));
1790
1791 delete_window = memnew(ConfirmationDialog);
1792 delete_window->set_flag(Window::FLAG_RESIZE_DISABLED, true);
1793 add_child(delete_window);
1794
1795 delete_tree = memnew(Tree);
1796 delete_tree->set_hide_root(true);
1797 delete_tree->connect("draw", callable_mp(this, &AnimationNodeStateMachineEditor::_delete_tree_draw));
1798 delete_window->add_child(delete_tree);
1799
1800 Button *ok = delete_window->get_cancel_button();
1801 ok->set_text(TTR("Delete Selected"));
1802 ok->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_delete_selected));
1803
1804 Button *delete_all = delete_window->add_button(TTR("Delete All"), true);
1805 delete_all->connect("pressed", callable_mp(this, &AnimationNodeStateMachineEditor::_delete_all));
1806
1807 over_node_what = -1;
1808 dragging_selected_attempt = false;
1809 connecting = false;
1810 selected_transition_index = -1;
1811
1812 last_active = false;
1813
1814 error_time = 0;
1815}
1816
1817void EditorAnimationMultiTransitionEdit::add_transition(const StringName &p_from, const StringName &p_to, Ref<AnimationNodeStateMachineTransition> p_transition) {
1818 Transition tr;
1819 tr.from = p_from;
1820 tr.to = p_to;
1821 tr.transition = p_transition;
1822 transitions.push_back(tr);
1823}
1824
1825bool EditorAnimationMultiTransitionEdit::_set(const StringName &p_name, const Variant &p_property) {
1826 int index = String(p_name).get_slicec('/', 0).to_int();
1827 StringName prop = String(p_name).get_slicec('/', 1);
1828
1829 bool found;
1830 transitions.write[index].transition->set(prop, p_property, &found);
1831 if (found) {
1832 return true;
1833 }
1834
1835 return false;
1836}
1837
1838bool EditorAnimationMultiTransitionEdit::_get(const StringName &p_name, Variant &r_property) const {
1839 int index = String(p_name).get_slicec('/', 0).to_int();
1840 StringName prop = String(p_name).get_slicec('/', 1);
1841
1842 if (prop == "transition_path") {
1843 r_property = String(transitions[index].from) + " -> " + transitions[index].to;
1844 return true;
1845 }
1846
1847 bool found;
1848 r_property = transitions[index].transition->get(prop, &found);
1849 if (found) {
1850 return true;
1851 }
1852
1853 return false;
1854}
1855
1856void EditorAnimationMultiTransitionEdit::_get_property_list(List<PropertyInfo> *p_list) const {
1857 for (int i = 0; i < transitions.size(); i++) {
1858 List<PropertyInfo> plist;
1859 transitions[i].transition->get_property_list(&plist, true);
1860
1861 PropertyInfo prop_transition_path;
1862 prop_transition_path.type = Variant::STRING;
1863 prop_transition_path.name = itos(i) + "/" + "transition_path";
1864 p_list->push_back(prop_transition_path);
1865
1866 for (List<PropertyInfo>::Element *F = plist.front(); F; F = F->next()) {
1867 if (F->get().name == "script" || F->get().name == "resource_name" || F->get().name == "resource_path" || F->get().name == "resource_local_to_scene") {
1868 continue;
1869 }
1870
1871 if (F->get().usage != PROPERTY_USAGE_DEFAULT) {
1872 continue;
1873 }
1874
1875 PropertyInfo prop = F->get();
1876 prop.name = itos(i) + "/" + prop.name;
1877
1878 p_list->push_back(prop);
1879 }
1880 }
1881}
1882