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