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) 2007-2020 Juan Linietsky, Ariel Manzur. */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */
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/io/resource_loader.h"
34 #include "core/os/input.h"
35 #include "core/os/keyboard.h"
36 #include "core/project_settings.h"
37 #include "editor/editor_inspector.h"
38 #include "editor/editor_scale.h"
39 #include "scene/animation/animation_player.h"
40 #include "scene/gui/menu_button.h"
41 #include "scene/gui/panel.h"
42 #include "scene/gui/progress_bar.h"
43 #include "scene/main/viewport.h"
44
add_custom_type(const String & p_name,const Ref<Script> & p_script)45 void AnimationNodeBlendTreeEditor::add_custom_type(const String &p_name, const Ref<Script> &p_script) {
46
47 for (int i = 0; i < add_options.size(); i++) {
48 ERR_FAIL_COND(add_options[i].script == p_script);
49 }
50
51 AddOption ao;
52 ao.name = p_name;
53 ao.script = p_script;
54 add_options.push_back(ao);
55
56 _update_options_menu();
57 }
58
remove_custom_type(const Ref<Script> & p_script)59 void AnimationNodeBlendTreeEditor::remove_custom_type(const Ref<Script> &p_script) {
60
61 for (int i = 0; i < add_options.size(); i++) {
62 if (add_options[i].script == p_script) {
63 add_options.remove(i);
64 return;
65 }
66 }
67
68 _update_options_menu();
69 }
70
_update_options_menu()71 void AnimationNodeBlendTreeEditor::_update_options_menu() {
72
73 add_node->get_popup()->clear();
74 for (int i = 0; i < add_options.size(); i++) {
75 add_node->get_popup()->add_item(add_options[i].name, i);
76 }
77
78 Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
79 if (clipb.is_valid()) {
80 add_node->get_popup()->add_separator();
81 add_node->get_popup()->add_item(TTR("Paste"), MENU_PASTE);
82 }
83 add_node->get_popup()->add_separator();
84 add_node->get_popup()->add_item(TTR("Load..."), MENU_LOAD_FILE);
85 use_popup_menu_position = false;
86 }
87
get_minimum_size() const88 Size2 AnimationNodeBlendTreeEditor::get_minimum_size() const {
89
90 return Size2(10, 200);
91 }
92
_property_changed(const StringName & p_property,const Variant & p_value,const String & p_field,bool p_changing)93 void AnimationNodeBlendTreeEditor::_property_changed(const StringName &p_property, const Variant &p_value, const String &p_field, bool p_changing) {
94
95 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_tree();
96 updating = true;
97 undo_redo->create_action(TTR("Parameter Changed") + ": " + String(p_property), UndoRedo::MERGE_ENDS);
98 undo_redo->add_do_property(tree, p_property, p_value);
99 undo_redo->add_undo_property(tree, p_property, tree->get(p_property));
100 undo_redo->add_do_method(this, "_update_graph");
101 undo_redo->add_undo_method(this, "_update_graph");
102 undo_redo->commit_action();
103 updating = false;
104 }
105
_update_graph()106 void AnimationNodeBlendTreeEditor::_update_graph() {
107
108 if (updating)
109 return;
110
111 visible_properties.clear();
112
113 graph->set_scroll_ofs(blend_tree->get_graph_offset() * EDSCALE);
114
115 graph->clear_connections();
116 //erase all nodes
117 for (int i = 0; i < graph->get_child_count(); i++) {
118
119 if (Object::cast_to<GraphNode>(graph->get_child(i))) {
120 memdelete(graph->get_child(i));
121 i--;
122 }
123 }
124
125 animations.clear();
126
127 List<StringName> nodes;
128 blend_tree->get_node_list(&nodes);
129
130 for (List<StringName>::Element *E = nodes.front(); E; E = E->next()) {
131
132 GraphNode *node = memnew(GraphNode);
133 graph->add_child(node);
134
135 Ref<AnimationNode> agnode = blend_tree->get_node(E->get());
136
137 node->set_offset(blend_tree->get_node_position(E->get()) * EDSCALE);
138
139 node->set_title(agnode->get_caption());
140 node->set_name(E->get());
141
142 int base = 0;
143 if (String(E->get()) != "output") {
144 LineEdit *name = memnew(LineEdit);
145 name->set_text(E->get());
146 name->set_expand_to_text_length(true);
147 node->add_child(name);
148 node->set_slot(0, false, 0, Color(), true, 0, get_color("font_color", "Label"));
149 name->connect("text_entered", this, "_node_renamed", varray(agnode));
150 name->connect("focus_exited", this, "_node_renamed_focus_out", varray(name, agnode), CONNECT_DEFERRED);
151 base = 1;
152 node->set_show_close_button(true);
153 node->connect("close_request", this, "_delete_request", varray(E->get()), CONNECT_DEFERRED);
154 }
155
156 for (int i = 0; i < agnode->get_input_count(); i++) {
157 Label *in_name = memnew(Label);
158 node->add_child(in_name);
159 in_name->set_text(agnode->get_input_name(i));
160 node->set_slot(base + i, true, 0, get_color("font_color", "Label"), false, 0, Color());
161 }
162
163 List<PropertyInfo> pinfo;
164 agnode->get_parameter_list(&pinfo);
165 for (List<PropertyInfo>::Element *F = pinfo.front(); F; F = F->next()) {
166
167 if (!(F->get().usage & PROPERTY_USAGE_EDITOR)) {
168 continue;
169 }
170 String base_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E->get()) + "/" + F->get().name;
171 EditorProperty *prop = EditorInspector::instantiate_property_editor(AnimationTreeEditor::get_singleton()->get_tree(), F->get().type, base_path, F->get().hint, F->get().hint_string, F->get().usage);
172 if (prop) {
173 prop->set_object_and_property(AnimationTreeEditor::get_singleton()->get_tree(), base_path);
174 prop->update_property();
175 prop->set_name_split_ratio(0);
176 prop->connect("property_changed", this, "_property_changed");
177 node->add_child(prop);
178 visible_properties.push_back(prop);
179 }
180 }
181
182 node->connect("dragged", this, "_node_dragged", varray(E->get()));
183
184 if (AnimationTreeEditor::get_singleton()->can_edit(agnode)) {
185 node->add_child(memnew(HSeparator));
186 Button *open_in_editor = memnew(Button);
187 open_in_editor->set_text(TTR("Open Editor"));
188 open_in_editor->set_icon(get_icon("Edit", "EditorIcons"));
189 node->add_child(open_in_editor);
190 open_in_editor->connect("pressed", this, "_open_in_editor", varray(E->get()), CONNECT_DEFERRED);
191 open_in_editor->set_h_size_flags(SIZE_SHRINK_CENTER);
192 }
193
194 if (agnode->has_filter()) {
195
196 node->add_child(memnew(HSeparator));
197 Button *edit_filters = memnew(Button);
198 edit_filters->set_text(TTR("Edit Filters"));
199 edit_filters->set_icon(get_icon("AnimationFilter", "EditorIcons"));
200 node->add_child(edit_filters);
201 edit_filters->connect("pressed", this, "_edit_filters", varray(E->get()), CONNECT_DEFERRED);
202 edit_filters->set_h_size_flags(SIZE_SHRINK_CENTER);
203 }
204
205 Ref<AnimationNodeAnimation> anim = agnode;
206 if (anim.is_valid()) {
207
208 MenuButton *mb = memnew(MenuButton);
209 mb->set_text(anim->get_animation());
210 mb->set_icon(get_icon("Animation", "EditorIcons"));
211 Array options;
212
213 node->add_child(memnew(HSeparator));
214 node->add_child(mb);
215
216 ProgressBar *pb = memnew(ProgressBar);
217
218 AnimationTree *player = AnimationTreeEditor::get_singleton()->get_tree();
219 if (player->has_node(player->get_animation_player())) {
220 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(player->get_node(player->get_animation_player()));
221 if (ap) {
222 List<StringName> anims;
223 ap->get_animation_list(&anims);
224
225 for (List<StringName>::Element *F = anims.front(); F; F = F->next()) {
226 mb->get_popup()->add_item(F->get());
227 options.push_back(F->get());
228 }
229
230 if (ap->has_animation(anim->get_animation())) {
231 pb->set_max(ap->get_animation(anim->get_animation())->get_length());
232 }
233 }
234 }
235
236 pb->set_percent_visible(false);
237 pb->set_custom_minimum_size(Vector2(0, 14) * EDSCALE);
238 animations[E->get()] = pb;
239 node->add_child(pb);
240
241 mb->get_popup()->connect("index_pressed", this, "_anim_selected", varray(options, E->get()), CONNECT_DEFERRED);
242 }
243
244 if (EditorSettings::get_singleton()->get("interface/theme/use_graph_node_headers")) {
245 Ref<StyleBoxFlat> sb = node->get_stylebox("frame", "GraphNode");
246 Color c = sb->get_border_color();
247 Color mono_color = ((c.r + c.g + c.b) / 3) < 0.7 ? Color(1.0, 1.0, 1.0) : Color(0.0, 0.0, 0.0);
248 mono_color.a = 0.85;
249 c = mono_color;
250
251 node->add_color_override("title_color", c);
252 c.a = 0.7;
253 node->add_color_override("close_color", c);
254 node->add_color_override("resizer_color", c);
255 }
256 }
257
258 List<AnimationNodeBlendTree::NodeConnection> connections;
259 blend_tree->get_node_connections(&connections);
260
261 for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = connections.front(); E; E = E->next()) {
262
263 StringName from = E->get().output_node;
264 StringName to = E->get().input_node;
265 int to_idx = E->get().input_index;
266
267 graph->connect_node(from, 0, to, to_idx);
268 }
269 }
270
_file_opened(const String & p_file)271 void AnimationNodeBlendTreeEditor::_file_opened(const String &p_file) {
272
273 file_loaded = ResourceLoader::load(p_file);
274 if (file_loaded.is_valid()) {
275 _add_node(MENU_LOAD_FILE_CONFIRM);
276 }
277 }
278
_add_node(int p_idx)279 void AnimationNodeBlendTreeEditor::_add_node(int p_idx) {
280
281 Ref<AnimationNode> anode;
282
283 String base_name;
284
285 if (p_idx == MENU_LOAD_FILE) {
286
287 open_file->clear_filters();
288 List<String> filters;
289 ResourceLoader::get_recognized_extensions_for_type("AnimationNode", &filters);
290 for (List<String>::Element *E = filters.front(); E; E = E->next()) {
291 open_file->add_filter("*." + E->get());
292 }
293 open_file->popup_centered_ratio();
294 return;
295 } else if (p_idx == MENU_LOAD_FILE_CONFIRM) {
296 anode = file_loaded;
297 file_loaded.unref();
298 base_name = anode->get_class();
299 } else if (p_idx == MENU_PASTE) {
300
301 anode = EditorSettings::get_singleton()->get_resource_clipboard();
302 ERR_FAIL_COND(!anode.is_valid());
303 base_name = anode->get_class();
304 } else if (add_options[p_idx].type != String()) {
305 AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instance(add_options[p_idx].type));
306 ERR_FAIL_COND(!an);
307 anode = Ref<AnimationNode>(an);
308 base_name = add_options[p_idx].name;
309 } else {
310 ERR_FAIL_COND(add_options[p_idx].script.is_null());
311 String base_type = add_options[p_idx].script->get_instance_base_type();
312 AnimationNode *an = Object::cast_to<AnimationNode>(ClassDB::instance(base_type));
313 ERR_FAIL_COND(!an);
314 anode = Ref<AnimationNode>(an);
315 anode->set_script(add_options[p_idx].script.get_ref_ptr());
316 base_name = add_options[p_idx].name;
317 }
318
319 Ref<AnimationNodeOutput> out = anode;
320 if (out.is_valid()) {
321 EditorNode::get_singleton()->show_warning(TTR("Output node can't be added to the blend tree."));
322 return;
323 }
324
325 Point2 instance_pos = graph->get_scroll_ofs();
326 if (use_popup_menu_position) {
327 instance_pos += popup_menu_position;
328 } else {
329 instance_pos += graph->get_size() * 0.5;
330 }
331
332 instance_pos /= graph->get_zoom();
333
334 int base = 1;
335 String name = base_name;
336 while (blend_tree->has_node(name)) {
337 base++;
338 name = base_name + " " + itos(base);
339 }
340
341 undo_redo->create_action(TTR("Add Node to BlendTree"));
342 undo_redo->add_do_method(blend_tree.ptr(), "add_node", name, anode, instance_pos / EDSCALE);
343 undo_redo->add_undo_method(blend_tree.ptr(), "remove_node", name);
344 undo_redo->add_do_method(this, "_update_graph");
345 undo_redo->add_undo_method(this, "_update_graph");
346 undo_redo->commit_action();
347 }
348
_node_dragged(const Vector2 & p_from,const Vector2 & p_to,const StringName & p_which)349 void AnimationNodeBlendTreeEditor::_node_dragged(const Vector2 &p_from, const Vector2 &p_to, const StringName &p_which) {
350
351 updating = true;
352 undo_redo->create_action(TTR("Node Moved"));
353 undo_redo->add_do_method(blend_tree.ptr(), "set_node_position", p_which, p_to / EDSCALE);
354 undo_redo->add_undo_method(blend_tree.ptr(), "set_node_position", p_which, p_from / EDSCALE);
355 undo_redo->add_do_method(this, "_update_graph");
356 undo_redo->add_undo_method(this, "_update_graph");
357 undo_redo->commit_action();
358 updating = false;
359 }
360
_connection_request(const String & p_from,int p_from_index,const String & p_to,int p_to_index)361 void AnimationNodeBlendTreeEditor::_connection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
362
363 AnimationNodeBlendTree::ConnectionError err = blend_tree->can_connect_node(p_to, p_to_index, p_from);
364
365 if (err != AnimationNodeBlendTree::CONNECTION_OK) {
366 EditorNode::get_singleton()->show_warning(TTR("Unable to connect, port may be in use or connection may be invalid."));
367 return;
368 }
369
370 undo_redo->create_action(TTR("Nodes Connected"));
371 undo_redo->add_do_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
372 undo_redo->add_undo_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
373 undo_redo->add_do_method(this, "_update_graph");
374 undo_redo->add_undo_method(this, "_update_graph");
375 undo_redo->commit_action();
376 }
377
_disconnection_request(const String & p_from,int p_from_index,const String & p_to,int p_to_index)378 void AnimationNodeBlendTreeEditor::_disconnection_request(const String &p_from, int p_from_index, const String &p_to, int p_to_index) {
379
380 graph->disconnect_node(p_from, p_from_index, p_to, p_to_index);
381
382 updating = true;
383 undo_redo->create_action(TTR("Nodes Disconnected"));
384 undo_redo->add_do_method(blend_tree.ptr(), "disconnect_node", p_to, p_to_index);
385 undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", p_to, p_to_index, p_from);
386 undo_redo->add_do_method(this, "_update_graph");
387 undo_redo->add_undo_method(this, "_update_graph");
388 undo_redo->commit_action();
389 updating = false;
390 }
391
_anim_selected(int p_index,Array p_options,const String & p_node)392 void AnimationNodeBlendTreeEditor::_anim_selected(int p_index, Array p_options, const String &p_node) {
393
394 String option = p_options[p_index];
395
396 Ref<AnimationNodeAnimation> anim = blend_tree->get_node(p_node);
397 ERR_FAIL_COND(!anim.is_valid());
398
399 undo_redo->create_action(TTR("Set Animation"));
400 undo_redo->add_do_method(anim.ptr(), "set_animation", option);
401 undo_redo->add_undo_method(anim.ptr(), "set_animation", anim->get_animation());
402 undo_redo->add_do_method(this, "_update_graph");
403 undo_redo->add_undo_method(this, "_update_graph");
404 undo_redo->commit_action();
405 }
406
_delete_request(const String & p_which)407 void AnimationNodeBlendTreeEditor::_delete_request(const String &p_which) {
408
409 undo_redo->create_action(TTR("Delete Node"));
410 undo_redo->add_do_method(blend_tree.ptr(), "remove_node", p_which);
411 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));
412
413 List<AnimationNodeBlendTree::NodeConnection> conns;
414 blend_tree->get_node_connections(&conns);
415
416 for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = conns.front(); E; E = E->next()) {
417 if (E->get().output_node == p_which || E->get().input_node == p_which) {
418 undo_redo->add_undo_method(blend_tree.ptr(), "connect_node", E->get().input_node, E->get().input_index, E->get().output_node);
419 }
420 }
421
422 undo_redo->add_do_method(this, "_update_graph");
423 undo_redo->add_undo_method(this, "_update_graph");
424 undo_redo->commit_action();
425 }
426
_delete_nodes_request()427 void AnimationNodeBlendTreeEditor::_delete_nodes_request() {
428
429 List<StringName> to_erase;
430
431 for (int i = 0; i < graph->get_child_count(); i++) {
432 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_child(i));
433 if (gn) {
434 if (gn->is_selected() && gn->is_close_button_visible()) {
435 to_erase.push_back(gn->get_name());
436 }
437 }
438 }
439
440 if (to_erase.empty())
441 return;
442
443 undo_redo->create_action(TTR("Delete Node(s)"));
444
445 for (List<StringName>::Element *F = to_erase.front(); F; F = F->next()) {
446 _delete_request(F->get());
447 }
448
449 undo_redo->commit_action();
450 }
451
_popup_request(const Vector2 & p_position)452 void AnimationNodeBlendTreeEditor::_popup_request(const Vector2 &p_position) {
453
454 _update_options_menu();
455 use_popup_menu_position = true;
456 popup_menu_position = graph->get_local_mouse_position();
457 add_node->get_popup()->set_position(p_position);
458 add_node->get_popup()->popup();
459 }
460
_node_selected(Object * p_node)461 void AnimationNodeBlendTreeEditor::_node_selected(Object *p_node) {
462
463 GraphNode *gn = Object::cast_to<GraphNode>(p_node);
464 ERR_FAIL_COND(!gn);
465
466 String name = gn->get_name();
467
468 Ref<AnimationNode> anode = blend_tree->get_node(name);
469 ERR_FAIL_COND(!anode.is_valid());
470
471 EditorNode::get_singleton()->push_item(anode.ptr(), "", true);
472 }
473
_open_in_editor(const String & p_which)474 void AnimationNodeBlendTreeEditor::_open_in_editor(const String &p_which) {
475
476 Ref<AnimationNode> an = blend_tree->get_node(p_which);
477 ERR_FAIL_COND(!an.is_valid());
478 AnimationTreeEditor::get_singleton()->enter_editor(p_which);
479 }
480
_filter_toggled()481 void AnimationNodeBlendTreeEditor::_filter_toggled() {
482
483 updating = true;
484 undo_redo->create_action(TTR("Toggle Filter On/Off"));
485 undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_enabled", filter_enabled->is_pressed());
486 undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_enabled", _filter_edit->is_filter_enabled());
487 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
488 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
489 undo_redo->commit_action();
490 updating = false;
491 }
492
_filter_edited()493 void AnimationNodeBlendTreeEditor::_filter_edited() {
494
495 TreeItem *edited = filters->get_edited();
496 ERR_FAIL_COND(!edited);
497
498 NodePath edited_path = edited->get_metadata(0);
499 bool filtered = edited->is_checked(0);
500
501 updating = true;
502 undo_redo->create_action(TTR("Change Filter"));
503 undo_redo->add_do_method(_filter_edit.ptr(), "set_filter_path", edited_path, filtered);
504 undo_redo->add_undo_method(_filter_edit.ptr(), "set_filter_path", edited_path, _filter_edit->is_path_filtered(edited_path));
505 undo_redo->add_do_method(this, "_update_filters", _filter_edit);
506 undo_redo->add_undo_method(this, "_update_filters", _filter_edit);
507 undo_redo->commit_action();
508 updating = false;
509 }
510
_update_filters(const Ref<AnimationNode> & anode)511 bool AnimationNodeBlendTreeEditor::_update_filters(const Ref<AnimationNode> &anode) {
512
513 if (updating || _filter_edit != anode)
514 return false;
515
516 NodePath player_path = AnimationTreeEditor::get_singleton()->get_tree()->get_animation_player();
517
518 if (!AnimationTreeEditor::get_singleton()->get_tree()->has_node(player_path)) {
519 EditorNode::get_singleton()->show_warning(TTR("No animation player set, so unable to retrieve track names."));
520 return false;
521 }
522
523 AnimationPlayer *player = Object::cast_to<AnimationPlayer>(AnimationTreeEditor::get_singleton()->get_tree()->get_node(player_path));
524 if (!player) {
525 EditorNode::get_singleton()->show_warning(TTR("Player path set is invalid, so unable to retrieve track names."));
526 return false;
527 }
528
529 Node *base = player->get_node(player->get_root());
530
531 if (!base) {
532 EditorNode::get_singleton()->show_warning(TTR("Animation player has no valid root node path, so unable to retrieve track names."));
533 return false;
534 }
535
536 updating = true;
537
538 Set<String> paths;
539 HashMap<String, Set<String> > types;
540 {
541 List<StringName> animations;
542 player->get_animation_list(&animations);
543
544 for (List<StringName>::Element *E = animations.front(); E; E = E->next()) {
545
546 Ref<Animation> anim = player->get_animation(E->get());
547 for (int i = 0; i < anim->get_track_count(); i++) {
548 String track_path = anim->track_get_path(i);
549 paths.insert(track_path);
550
551 String track_type_name;
552 Animation::TrackType track_type = anim->track_get_type(i);
553 switch (track_type) {
554 case Animation::TrackType::TYPE_ANIMATION: {
555 track_type_name = TTR("Anim Clips");
556 } break;
557 case Animation::TrackType::TYPE_AUDIO: {
558 track_type_name = TTR("Audio Clips");
559 } break;
560 case Animation::TrackType::TYPE_METHOD: {
561 track_type_name = TTR("Functions");
562 } break;
563 default: {
564 } break;
565 }
566 if (!track_type_name.empty()) {
567 types[track_path].insert(track_type_name);
568 }
569 }
570 }
571 }
572
573 filter_enabled->set_pressed(anode->is_filter_enabled());
574 filters->clear();
575 TreeItem *root = filters->create_item();
576
577 Map<String, TreeItem *> parenthood;
578
579 for (Set<String>::Element *E = paths.front(); E; E = E->next()) {
580
581 NodePath path = E->get();
582 TreeItem *ti = NULL;
583 String accum;
584 for (int i = 0; i < path.get_name_count(); i++) {
585 String name = path.get_name(i);
586 if (accum != String()) {
587 accum += "/";
588 }
589 accum += name;
590 if (!parenthood.has(accum)) {
591 if (ti) {
592 ti = filters->create_item(ti);
593 } else {
594 ti = filters->create_item(root);
595 }
596 parenthood[accum] = ti;
597 ti->set_text(0, name);
598 ti->set_selectable(0, false);
599 ti->set_editable(0, false);
600
601 if (base->has_node(accum)) {
602 Node *node = base->get_node(accum);
603 ti->set_icon(0, EditorNode::get_singleton()->get_object_icon(node, "Node"));
604 }
605
606 } else {
607 ti = parenthood[accum];
608 }
609 }
610
611 Node *node = NULL;
612 if (base->has_node(accum)) {
613 node = base->get_node(accum);
614 }
615 if (!node)
616 continue; //no node, can't edit
617
618 if (path.get_subname_count()) {
619
620 String concat = path.get_concatenated_subnames();
621
622 Skeleton *skeleton = Object::cast_to<Skeleton>(node);
623 if (skeleton && skeleton->find_bone(concat) != -1) {
624 //path in skeleton
625 const String &bone = concat;
626 int idx = skeleton->find_bone(bone);
627 List<String> bone_path;
628 while (idx != -1) {
629 bone_path.push_front(skeleton->get_bone_name(idx));
630 idx = skeleton->get_bone_parent(idx);
631 }
632
633 accum += ":";
634 for (List<String>::Element *F = bone_path.front(); F; F = F->next()) {
635 if (F != bone_path.front()) {
636 accum += "/";
637 }
638
639 accum += F->get();
640 if (!parenthood.has(accum)) {
641 ti = filters->create_item(ti);
642 parenthood[accum] = ti;
643 ti->set_text(0, F->get());
644 ti->set_selectable(0, false);
645 ti->set_editable(0, false);
646 ti->set_icon(0, get_icon("BoneAttachment", "EditorIcons"));
647 } else {
648 ti = parenthood[accum];
649 }
650 }
651
652 ti->set_editable(0, true);
653 ti->set_selectable(0, true);
654 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
655 ti->set_text(0, concat);
656 ti->set_checked(0, anode->is_path_filtered(path));
657 ti->set_icon(0, get_icon("BoneAttachment", "EditorIcons"));
658 ti->set_metadata(0, path);
659
660 } else {
661 //just a property
662 ti = filters->create_item(ti);
663 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
664 ti->set_text(0, concat);
665 ti->set_editable(0, true);
666 ti->set_selectable(0, true);
667 ti->set_checked(0, anode->is_path_filtered(path));
668 ti->set_metadata(0, path);
669 }
670 } else {
671 if (ti) {
672 //just a node, not a property track
673 String types_text = "[";
674 if (types.has(path)) {
675 Set<String>::Element *F = types[path].front();
676 types_text += F->get();
677 while (F->next()) {
678 F = F->next();
679 types_text += " / " + F->get();
680 }
681 }
682 types_text += "]";
683 ti = filters->create_item(ti);
684 ti->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
685 ti->set_text(0, types_text);
686 ti->set_editable(0, true);
687 ti->set_selectable(0, true);
688 ti->set_checked(0, anode->is_path_filtered(path));
689 ti->set_metadata(0, path);
690 }
691 }
692 }
693
694 updating = false;
695
696 return true;
697 }
698
_edit_filters(const String & p_which)699 void AnimationNodeBlendTreeEditor::_edit_filters(const String &p_which) {
700
701 Ref<AnimationNode> anode = blend_tree->get_node(p_which);
702 ERR_FAIL_COND(!anode.is_valid());
703
704 _filter_edit = anode;
705 if (!_update_filters(anode))
706 return;
707
708 filter_dialog->popup_centered_minsize(Size2(500, 500) * EDSCALE);
709 }
710
_removed_from_graph()711 void AnimationNodeBlendTreeEditor::_removed_from_graph() {
712 if (is_visible()) {
713 EditorNode::get_singleton()->edit_item(NULL);
714 }
715 }
716
_notification(int p_what)717 void AnimationNodeBlendTreeEditor::_notification(int p_what) {
718
719 if (p_what == NOTIFICATION_ENTER_TREE || p_what == NOTIFICATION_THEME_CHANGED) {
720
721 error_panel->add_style_override("panel", get_stylebox("bg", "Tree"));
722 error_label->add_color_override("font_color", get_color("error_color", "Editor"));
723
724 if (p_what == NOTIFICATION_THEME_CHANGED && is_visible_in_tree())
725 _update_graph();
726 }
727
728 if (p_what == NOTIFICATION_PROCESS) {
729
730 String error;
731
732 if (!AnimationTreeEditor::get_singleton()->get_tree()->is_active()) {
733 error = TTR("AnimationTree is inactive.\nActivate to enable playback, check node warnings if activation fails.");
734 } else if (AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
735 error = AnimationTreeEditor::get_singleton()->get_tree()->get_invalid_state_reason();
736 }
737
738 if (error != error_label->get_text()) {
739 error_label->set_text(error);
740 if (error != String()) {
741 error_panel->show();
742 } else {
743 error_panel->hide();
744 }
745 }
746
747 List<AnimationNodeBlendTree::NodeConnection> conns;
748 blend_tree->get_node_connections(&conns);
749 for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = conns.front(); E; E = E->next()) {
750 float activity = 0;
751 StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E->get().input_node;
752 if (AnimationTreeEditor::get_singleton()->get_tree() && !AnimationTreeEditor::get_singleton()->get_tree()->is_state_invalid()) {
753 activity = AnimationTreeEditor::get_singleton()->get_tree()->get_connection_activity(path, E->get().input_index);
754 }
755 graph->set_connection_activity(E->get().output_node, 0, E->get().input_node, E->get().input_index, activity);
756 }
757
758 AnimationTree *graph_player = AnimationTreeEditor::get_singleton()->get_tree();
759 AnimationPlayer *player = NULL;
760 if (graph_player->has_node(graph_player->get_animation_player())) {
761 player = Object::cast_to<AnimationPlayer>(graph_player->get_node(graph_player->get_animation_player()));
762 }
763
764 if (player) {
765 for (Map<StringName, ProgressBar *>::Element *E = animations.front(); E; E = E->next()) {
766 Ref<AnimationNodeAnimation> an = blend_tree->get_node(E->key());
767 if (an.is_valid()) {
768 if (player->has_animation(an->get_animation())) {
769 Ref<Animation> anim = player->get_animation(an->get_animation());
770 if (anim.is_valid()) {
771 E->get()->set_max(anim->get_length());
772 //StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + E->get().input_node;
773 StringName time_path = AnimationTreeEditor::get_singleton()->get_base_path() + String(E->key()) + "/time";
774 E->get()->set_value(AnimationTreeEditor::get_singleton()->get_tree()->get(time_path));
775 }
776 }
777 }
778 }
779 }
780
781 for (int i = 0; i < visible_properties.size(); i++) {
782 visible_properties[i]->update_property();
783 }
784 }
785
786 if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
787 set_process(is_visible_in_tree());
788 }
789 }
790
_scroll_changed(const Vector2 & p_scroll)791 void AnimationNodeBlendTreeEditor::_scroll_changed(const Vector2 &p_scroll) {
792 if (updating)
793 return;
794 updating = true;
795 blend_tree->set_graph_offset(p_scroll / EDSCALE);
796 updating = false;
797 }
798
_bind_methods()799 void AnimationNodeBlendTreeEditor::_bind_methods() {
800
801 ClassDB::bind_method("_update_graph", &AnimationNodeBlendTreeEditor::_update_graph);
802 ClassDB::bind_method("_add_node", &AnimationNodeBlendTreeEditor::_add_node);
803 ClassDB::bind_method("_node_dragged", &AnimationNodeBlendTreeEditor::_node_dragged);
804 ClassDB::bind_method("_node_renamed", &AnimationNodeBlendTreeEditor::_node_renamed);
805 ClassDB::bind_method("_node_renamed_focus_out", &AnimationNodeBlendTreeEditor::_node_renamed_focus_out);
806 ClassDB::bind_method("_connection_request", &AnimationNodeBlendTreeEditor::_connection_request);
807 ClassDB::bind_method("_disconnection_request", &AnimationNodeBlendTreeEditor::_disconnection_request);
808 ClassDB::bind_method("_node_selected", &AnimationNodeBlendTreeEditor::_node_selected);
809 ClassDB::bind_method("_open_in_editor", &AnimationNodeBlendTreeEditor::_open_in_editor);
810 ClassDB::bind_method("_scroll_changed", &AnimationNodeBlendTreeEditor::_scroll_changed);
811 ClassDB::bind_method("_delete_request", &AnimationNodeBlendTreeEditor::_delete_request);
812 ClassDB::bind_method("_delete_nodes_request", &AnimationNodeBlendTreeEditor::_delete_nodes_request);
813 ClassDB::bind_method("_popup_request", &AnimationNodeBlendTreeEditor::_popup_request);
814 ClassDB::bind_method("_edit_filters", &AnimationNodeBlendTreeEditor::_edit_filters);
815 ClassDB::bind_method("_update_filters", &AnimationNodeBlendTreeEditor::_update_filters);
816 ClassDB::bind_method("_filter_edited", &AnimationNodeBlendTreeEditor::_filter_edited);
817 ClassDB::bind_method("_filter_toggled", &AnimationNodeBlendTreeEditor::_filter_toggled);
818 ClassDB::bind_method("_removed_from_graph", &AnimationNodeBlendTreeEditor::_removed_from_graph);
819 ClassDB::bind_method("_property_changed", &AnimationNodeBlendTreeEditor::_property_changed);
820 ClassDB::bind_method("_file_opened", &AnimationNodeBlendTreeEditor::_file_opened);
821 ClassDB::bind_method("_update_options_menu", &AnimationNodeBlendTreeEditor::_update_options_menu);
822
823 ClassDB::bind_method("_anim_selected", &AnimationNodeBlendTreeEditor::_anim_selected);
824 }
825
826 AnimationNodeBlendTreeEditor *AnimationNodeBlendTreeEditor::singleton = NULL;
827
_node_renamed(const String & p_text,Ref<AnimationNode> p_node)828 void AnimationNodeBlendTreeEditor::_node_renamed(const String &p_text, Ref<AnimationNode> p_node) {
829
830 String prev_name = blend_tree->get_node_name(p_node);
831 ERR_FAIL_COND(prev_name == String());
832 GraphNode *gn = Object::cast_to<GraphNode>(graph->get_node(prev_name));
833 ERR_FAIL_COND(!gn);
834
835 const String &new_name = p_text;
836
837 ERR_FAIL_COND(new_name == "" || new_name.find(".") != -1 || new_name.find("/") != -1);
838
839 if (new_name == prev_name) {
840 return; //nothing to do
841 }
842
843 const String &base_name = new_name;
844 int base = 1;
845 String name = base_name;
846 while (blend_tree->has_node(name)) {
847 base++;
848 name = base_name + " " + itos(base);
849 }
850
851 String base_path = AnimationTreeEditor::get_singleton()->get_base_path();
852
853 updating = true;
854 undo_redo->create_action(TTR("Node Renamed"));
855 undo_redo->add_do_method(blend_tree.ptr(), "rename_node", prev_name, name);
856 undo_redo->add_undo_method(blend_tree.ptr(), "rename_node", name, prev_name);
857 undo_redo->add_do_method(AnimationTreeEditor::get_singleton()->get_tree(), "rename_parameter", base_path + prev_name, base_path + name);
858 undo_redo->add_undo_method(AnimationTreeEditor::get_singleton()->get_tree(), "rename_parameter", base_path + name, base_path + prev_name);
859 undo_redo->add_do_method(this, "_update_graph");
860 undo_redo->add_undo_method(this, "_update_graph");
861 undo_redo->commit_action();
862 updating = false;
863 gn->set_name(new_name);
864 gn->set_size(gn->get_minimum_size());
865
866 //change editors accordingly
867 for (int i = 0; i < visible_properties.size(); i++) {
868 String pname = visible_properties[i]->get_edited_property().operator String();
869 if (pname.begins_with(base_path + prev_name)) {
870 String new_name2 = pname.replace_first(base_path + prev_name, base_path + name);
871 visible_properties[i]->set_object_and_property(visible_properties[i]->get_edited_object(), new_name2);
872 }
873 }
874
875 //recreate connections
876 graph->clear_connections();
877
878 List<AnimationNodeBlendTree::NodeConnection> connections;
879 blend_tree->get_node_connections(&connections);
880
881 for (List<AnimationNodeBlendTree::NodeConnection>::Element *E = connections.front(); E; E = E->next()) {
882
883 StringName from = E->get().output_node;
884 StringName to = E->get().input_node;
885 int to_idx = E->get().input_index;
886
887 graph->connect_node(from, 0, to, to_idx);
888 }
889
890 //update animations
891 for (Map<StringName, ProgressBar *>::Element *E = animations.front(); E; E = E->next()) {
892 if (E->key() == prev_name) {
893 animations[new_name] = animations[prev_name];
894 animations.erase(prev_name);
895 break;
896 }
897 }
898
899 _update_graph(); // Needed to update the signal connections with the new name.
900 }
901
_node_renamed_focus_out(Node * le,Ref<AnimationNode> p_node)902 void AnimationNodeBlendTreeEditor::_node_renamed_focus_out(Node *le, Ref<AnimationNode> p_node) {
903 _node_renamed(le->call("get_text"), p_node);
904 }
905
can_edit(const Ref<AnimationNode> & p_node)906 bool AnimationNodeBlendTreeEditor::can_edit(const Ref<AnimationNode> &p_node) {
907 Ref<AnimationNodeBlendTree> bt = p_node;
908 return bt.is_valid();
909 }
910
edit(const Ref<AnimationNode> & p_node)911 void AnimationNodeBlendTreeEditor::edit(const Ref<AnimationNode> &p_node) {
912
913 if (blend_tree.is_valid()) {
914 blend_tree->disconnect("removed_from_graph", this, "_removed_from_graph");
915 }
916
917 blend_tree = p_node;
918
919 if (blend_tree.is_null()) {
920 hide();
921 } else {
922 blend_tree->connect("removed_from_graph", this, "_removed_from_graph");
923
924 _update_graph();
925 }
926 }
927
AnimationNodeBlendTreeEditor()928 AnimationNodeBlendTreeEditor::AnimationNodeBlendTreeEditor() {
929
930 singleton = this;
931 updating = false;
932 use_popup_menu_position = false;
933
934 graph = memnew(GraphEdit);
935 add_child(graph);
936 graph->add_valid_right_disconnect_type(0);
937 graph->add_valid_left_disconnect_type(0);
938 graph->set_v_size_flags(SIZE_EXPAND_FILL);
939 graph->connect("connection_request", this, "_connection_request", varray(), CONNECT_DEFERRED);
940 graph->connect("disconnection_request", this, "_disconnection_request", varray(), CONNECT_DEFERRED);
941 graph->connect("node_selected", this, "_node_selected");
942 graph->connect("scroll_offset_changed", this, "_scroll_changed");
943 graph->connect("delete_nodes_request", this, "_delete_nodes_request");
944 graph->connect("popup_request", this, "_popup_request");
945
946 VSeparator *vs = memnew(VSeparator);
947 graph->get_zoom_hbox()->add_child(vs);
948 graph->get_zoom_hbox()->move_child(vs, 0);
949
950 add_node = memnew(MenuButton);
951 graph->get_zoom_hbox()->add_child(add_node);
952 add_node->set_text(TTR("Add Node..."));
953 graph->get_zoom_hbox()->move_child(add_node, 0);
954 add_node->get_popup()->connect("id_pressed", this, "_add_node");
955 add_node->connect("about_to_show", this, "_update_options_menu");
956
957 add_options.push_back(AddOption("Animation", "AnimationNodeAnimation"));
958 add_options.push_back(AddOption("OneShot", "AnimationNodeOneShot"));
959 add_options.push_back(AddOption("Add2", "AnimationNodeAdd2"));
960 add_options.push_back(AddOption("Add3", "AnimationNodeAdd3"));
961 add_options.push_back(AddOption("Blend2", "AnimationNodeBlend2"));
962 add_options.push_back(AddOption("Blend3", "AnimationNodeBlend3"));
963 add_options.push_back(AddOption("Seek", "AnimationNodeTimeSeek"));
964 add_options.push_back(AddOption("TimeScale", "AnimationNodeTimeScale"));
965 add_options.push_back(AddOption("Transition", "AnimationNodeTransition"));
966 add_options.push_back(AddOption("BlendTree", "AnimationNodeBlendTree"));
967 add_options.push_back(AddOption("BlendSpace1D", "AnimationNodeBlendSpace1D"));
968 add_options.push_back(AddOption("BlendSpace2D", "AnimationNodeBlendSpace2D"));
969 add_options.push_back(AddOption("StateMachine", "AnimationNodeStateMachine"));
970 _update_options_menu();
971
972 error_panel = memnew(PanelContainer);
973 add_child(error_panel);
974 error_label = memnew(Label);
975 error_panel->add_child(error_label);
976 error_label->set_text("eh");
977
978 filter_dialog = memnew(AcceptDialog);
979 add_child(filter_dialog);
980 filter_dialog->set_title(TTR("Edit Filtered Tracks:"));
981
982 VBoxContainer *filter_vbox = memnew(VBoxContainer);
983 filter_dialog->add_child(filter_vbox);
984
985 filter_enabled = memnew(CheckBox);
986 filter_enabled->set_text(TTR("Enable Filtering"));
987 filter_enabled->connect("pressed", this, "_filter_toggled");
988 filter_vbox->add_child(filter_enabled);
989
990 filters = memnew(Tree);
991 filter_vbox->add_child(filters);
992 filters->set_v_size_flags(SIZE_EXPAND_FILL);
993 filters->set_hide_root(true);
994 filters->connect("item_edited", this, "_filter_edited");
995
996 open_file = memnew(EditorFileDialog);
997 add_child(open_file);
998 open_file->set_title(TTR("Open Animation Node"));
999 open_file->set_mode(EditorFileDialog::MODE_OPEN_FILE);
1000 open_file->connect("file_selected", this, "_file_opened");
1001 undo_redo = EditorNode::get_undo_redo();
1002 }
1003