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