Debian Codegraph @

editor/animation/animation_blend_space_1d_editor.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* animation_blend_space_1d_editor.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "animation_blend_space_1d_editor.h"
32
33 #include "core/os/keyboard.h"
34 #include "editor/editor_node.h"
35 #include "editor/editor_string_names.h"
36 #include "editor/editor_undo_redo_manager.h"
37 #include "editor/gui/editor_file_dialog.h"
38 #include "editor/settings/editor_settings.h"
39 #include "editor/themes/editor_scale.h"
40 #include "scene/animation/animation_blend_tree.h"
41 #include "scene/gui/button.h"
42 #include "scene/gui/check_box.h"
43 #include "scene/gui/line_edit.h"
44 #include "scene/gui/option_button.h"
45 #include "scene/gui/panel_container.h"
46 #include "scene/gui/separator.h"
47 #include "scene/gui/spin_box.h"
48
49 StringName AnimationNodeBlendSpace1DEditor::get_blend_position_path() const {
50 StringName path = AnimationTreeEditor::get_singleton()->get_base_path() + "blend_position";
51 return path;
52 }
53
54 void AnimationNodeBlendSpace1DEditor::_blend_space_gui_input(const Ref<InputEvent> &p_event) {
55 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
56 if (!tree) {
57 return;
58 }
59
60 Ref<InputEventKey> k = p_event;
61
62 if (tool_select->is_pressed() && k.is_valid() && k->is_pressed() && k->get_keycode() == Key::KEY_DELETE && !k->is_echo()) {
63 if (selected_point != -1) {
64 if (!read_only) {
65 _erase_selected();
66 }
67 accept_event();
68 }
69 }
70
71 Ref<InputEventMouseButton> mb = p_event;
72
73 if (mb.is_valid() && mb->is_pressed() && ((tool_select->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) || (mb->get_button_index() == MouseButton::LEFT && tool_create->is_pressed()))) {
74 if (!read_only) {
75 menu->clear(false);
76 animations_menu->clear();
77 animations_to_add.clear();
78
79 LocalVector<StringName> classes;
80 ClassDB::get_inheriters_from_class("AnimationRootNode", classes);
81 classes.sort_custom<StringName::AlphCompare>();
82
83 menu->add_submenu_node_item(TTR("Add Animation"), animations_menu);
84
85 List<StringName> names;
86 tree->get_animation_list(&names);
87
88 for (const StringName &E : names) {
89 animations_menu->add_icon_item(get_editor_theme_icon(SNAME("Animation")), E);
90 animations_to_add.push_back(E);
91 }
92
93 for (const StringName &E : classes) {
94 String name = String(E).replace_first("AnimationNode", "");
95 if (name == "Animation" || name == "StartState" || name == "EndState") {
96 continue;
97 }
98
99 int idx = menu->get_item_count();
100 menu->add_item(vformat(TTR("Add %s"), name), idx);
101 menu->set_item_metadata(idx, E);
102 }
103
104 Ref<AnimationNode> clipb = EditorSettings::get_singleton()->get_resource_clipboard();
105 if (clipb.is_valid()) {
106 menu->add_separator();
107 menu->add_item(TTR("Paste"), MENU_PASTE);
108 }
109 menu->add_separator();
110 menu->add_item(TTR("Load..."), MENU_LOAD_FILE);
111
112 menu->set_position(blend_space_draw->get_screen_position() + mb->get_position());
113 menu->reset_size();
114 menu->popup();
115
116 add_point_pos = (mb->get_position() / blend_space_draw->get_size()).x;
117 add_point_pos *= (blend_space->get_max_space() - blend_space->get_min_space());
118 add_point_pos += blend_space->get_min_space();
119
120 if (snap->is_pressed()) {
121 add_point_pos = Math::snapped(add_point_pos, blend_space->get_snap());
122 }
123 }
124 }
125
126 if (mb.is_valid() && mb->is_pressed() && tool_select->is_pressed() && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed() && mb->get_button_index() == MouseButton::LEFT) {
127 blend_space_draw->queue_redraw(); // why not
128
129 // try to see if a point can be selected
130 selected_point = -1;
131 _update_tool_erase();
132
133 for (int i = 0; i < points.size(); i++) {
134 if (Math::abs(float(points[i] - mb->get_position().x)) < 10 * EDSCALE) {
135 selected_point = i;
136
137 Ref<AnimationNode> node = blend_space->get_blend_point_node(i);
138 EditorNode::get_singleton()->push_item(node.ptr(), "", true);
139
140 if (mb->is_double_click() && AnimationTreeEditor::get_singleton()->can_edit(node)) {
141 _open_editor();
142 return;
143 }
144
145 dragging_selected_attempt = true;
146 drag_from = mb->get_position();
147 _update_tool_erase();
148 _update_edited_point_pos();
149 return;
150 }
151 }
152
153 // If no point was selected, select host BlendSpace1D node.
154 if (selected_point == -1) {
155 EditorNode::get_singleton()->push_item(blend_space.ptr(), "", true);
156 }
157 }
158
159 if (mb.is_valid() && !mb->is_pressed() && dragging_selected_attempt && mb->get_button_index() == MouseButton::LEFT) {
160 if (!read_only) {
161 if (dragging_selected) {
162 // move
163 float point = blend_space->get_blend_point_position(selected_point);
164 point += drag_ofs.x;
165
166 if (snap->is_pressed()) {
167 point = Math::snapped(point, blend_space->get_snap());
168 }
169
170 updating = true;
171 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
172 undo_redo->create_action(TTR("Move Node Point"));
173 undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, point);
174 undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
175 undo_redo->add_do_method(this, "_update_space");
176 undo_redo->add_undo_method(this, "_update_space");
177 undo_redo->add_do_method(this, "_update_edited_point_pos");
178 undo_redo->add_undo_method(this, "_update_edited_point_pos");
179 undo_redo->commit_action();
180 updating = false;
181 }
182
183 dragging_selected_attempt = false;
184 dragging_selected = false;
185 blend_space_draw->queue_redraw();
186 }
187 }
188
189 // *set* the blend
190 if (mb.is_valid() && mb->is_pressed() && !dragging_selected_attempt && ((tool_select->is_pressed() && mb->is_shift_pressed()) || tool_blend->is_pressed()) && mb->get_button_index() == MouseButton::LEFT) {
191 float blend_pos = mb->get_position().x / blend_space_draw->get_size().x;
192 blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
193 blend_pos += blend_space->get_min_space();
194
195 tree->set(get_blend_position_path(), blend_pos);
196
197 dragging_blend_position = true;
198 blend_space_draw->queue_redraw();
199 }
200
201 if (mb.is_valid() && !mb->is_pressed() && dragging_blend_position && mb->get_button_index() == MouseButton::LEFT) {
202 dragging_blend_position = false;
203 }
204
205 Ref<InputEventMouseMotion> mm = p_event;
206
207 if (mm.is_valid() && dragging_selected_attempt) {
208 dragging_selected = true;
209 drag_ofs = ((mm->get_position() - drag_from) / blend_space_draw->get_size()) * ((blend_space->get_max_space() - blend_space->get_min_space()) * Vector2(1, 0));
210 blend_space_draw->queue_redraw();
211 _update_edited_point_pos();
212 }
213
214 if (mm.is_valid() && dragging_blend_position && !dragging_selected_attempt && ((tool_select->is_pressed() && mm->is_shift_pressed()) || tool_blend->is_pressed()) && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
215 float blend_pos = mm->get_position().x / blend_space_draw->get_size().x;
216 blend_pos *= blend_space->get_max_space() - blend_space->get_min_space();
217 blend_pos += blend_space->get_min_space();
218
219 tree->set(get_blend_position_path(), blend_pos);
220
221 blend_space_draw->queue_redraw();
222 }
223 }
224
225 void AnimationNodeBlendSpace1DEditor::_blend_space_draw() {
226 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
227 if (!tree) {
228 return;
229 }
230
231 Color linecolor = get_theme_color(SceneStringName(font_color), SNAME("Label"));
232 Color linecolor_soft = linecolor;
233 linecolor_soft.a *= 0.5;
234
235 Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
236 int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
237 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("KeyValue"));
238 Ref<Texture2D> icon_selected = get_editor_theme_icon(SNAME("KeySelected"));
239
240 Size2 s = blend_space_draw->get_size();
241
242 if (blend_space_draw->has_focus()) {
243 Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
244 blend_space_draw->draw_rect(Rect2(Point2(), s), color, false);
245 }
246
247 blend_space_draw->draw_line(Point2(1, s.height - 1), Point2(s.width - 1, s.height - 1), linecolor, Math::round(EDSCALE));
248
249 if (blend_space->get_min_space() < 0) {
250 float point = 0.0;
251 point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
252 point *= s.width;
253
254 float x = point;
255
256 blend_space_draw->draw_line(Point2(x, s.height - 1), Point2(x, s.height - 5 * EDSCALE), linecolor, Math::round(EDSCALE));
257 blend_space_draw->draw_string(font, Point2(x + 2 * EDSCALE, s.height - 2 * EDSCALE - font->get_height(font_size) + font->get_ascent(font_size)), "0", HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, linecolor);
258 blend_space_draw->draw_line(Point2(x, s.height - 5 * EDSCALE), Point2(x, 0), linecolor_soft, Math::round(EDSCALE));
259 }
260
261 if (snap->is_pressed()) {
262 linecolor_soft.a = linecolor.a * 0.1;
263
264 if (blend_space->get_snap() > 0) {
265 int prev_idx = -1;
266
267 for (int i = 0; i < s.x; i++) {
268 float v = blend_space->get_min_space() + i * (blend_space->get_max_space() - blend_space->get_min_space()) / s.x;
269 int idx = int(v / blend_space->get_snap());
270
271 if (i > 0 && prev_idx != idx) {
272 blend_space_draw->draw_line(Point2(i, 0), Point2(i, s.height), linecolor_soft, Math::round(EDSCALE));
273 }
274
275 prev_idx = idx;
276 }
277 }
278 }
279
280 points.clear();
281
282 for (int i = 0; i < blend_space->get_blend_point_count(); i++) {
283 float point = blend_space->get_blend_point_position(i);
284
285 if (!read_only) {
286 if (dragging_selected && selected_point == i) {
287 point += drag_ofs.x;
288 if (snap->is_pressed()) {
289 point = Math::snapped(point, blend_space->get_snap());
290 }
291 }
292 }
293
294 point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
295 point *= s.width;
296
297 points.push_back(point);
298
299 Vector2 gui_point = Vector2(point, s.height / 2.0);
300
301 gui_point -= (icon->get_size() / 2.0);
302
303 gui_point = gui_point.floor();
304
305 if (i == selected_point) {
306 blend_space_draw->draw_texture(icon_selected, gui_point);
307 } else {
308 blend_space_draw->draw_texture(icon, gui_point);
309 }
310 }
311
312 // blend position
313 {
314 Color color;
315 if (tool_blend->is_pressed()) {
316 color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
317 } else {
318 color = linecolor;
319 color.a *= 0.5;
320 }
321
322 float point = tree->get(get_blend_position_path());
323
324 point = (point - blend_space->get_min_space()) / (blend_space->get_max_space() - blend_space->get_min_space());
325 point *= s.width;
326
327 Vector2 gui_point = Vector2(point, s.height / 2.0);
328
329 float mind = 5 * EDSCALE;
330 float maxd = 15 * EDSCALE;
331 blend_space_draw->draw_line(gui_point + Vector2(mind, 0), gui_point + Vector2(maxd, 0), color, Math::round(2 * EDSCALE));
332 blend_space_draw->draw_line(gui_point + Vector2(-mind, 0), gui_point + Vector2(-maxd, 0), color, Math::round(2 * EDSCALE));
333 blend_space_draw->draw_line(gui_point + Vector2(0, mind), gui_point + Vector2(0, maxd), color, Math::round(2 * EDSCALE));
334 blend_space_draw->draw_line(gui_point + Vector2(0, -mind), gui_point + Vector2(0, -maxd), color, Math::round(2 * EDSCALE));
335 }
336 }
337
338 void AnimationNodeBlendSpace1DEditor::_update_space() {
339 if (updating) {
340 return;
341 }
342
343 updating = true;
344
345 max_value->set_value(blend_space->get_max_space());
346 min_value->set_value(blend_space->get_min_space());
347
348 sync->set_pressed(blend_space->is_using_sync());
349 interpolation->select(blend_space->get_blend_mode());
350
351 label_value->set_text(blend_space->get_value_label());
352
353 snap_value->set_value(blend_space->get_snap());
354
355 blend_space_draw->queue_redraw();
356
357 updating = false;
358 }
359
360 void AnimationNodeBlendSpace1DEditor::_config_changed(double) {
361 if (updating) {
362 return;
363 }
364
365 updating = true;
366 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
367 undo_redo->create_action(TTR("Change BlendSpace1D Config"));
368 undo_redo->add_do_method(blend_space.ptr(), "set_max_space", max_value->get_value());
369 undo_redo->add_undo_method(blend_space.ptr(), "set_max_space", blend_space->get_max_space());
370 undo_redo->add_do_method(blend_space.ptr(), "set_min_space", min_value->get_value());
371 undo_redo->add_undo_method(blend_space.ptr(), "set_min_space", blend_space->get_min_space());
372 undo_redo->add_do_method(blend_space.ptr(), "set_snap", snap_value->get_value());
373 undo_redo->add_undo_method(blend_space.ptr(), "set_snap", blend_space->get_snap());
374 undo_redo->add_do_method(blend_space.ptr(), "set_use_sync", sync->is_pressed());
375 undo_redo->add_undo_method(blend_space.ptr(), "set_use_sync", blend_space->is_using_sync());
376 undo_redo->add_do_method(blend_space.ptr(), "set_blend_mode", interpolation->get_selected());
377 undo_redo->add_undo_method(blend_space.ptr(), "set_blend_mode", blend_space->get_blend_mode());
378 undo_redo->add_do_method(this, "_update_space");
379 undo_redo->add_undo_method(this, "_update_space");
380 undo_redo->commit_action();
381 updating = false;
382
383 blend_space_draw->queue_redraw();
384 }
385
386 void AnimationNodeBlendSpace1DEditor::_labels_changed(String) {
387 if (updating) {
388 return;
389 }
390
391 updating = true;
392 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
393 undo_redo->create_action(TTR("Change BlendSpace1D Labels"), UndoRedo::MERGE_ENDS);
394 undo_redo->add_do_method(blend_space.ptr(), "set_value_label", label_value->get_text());
395 undo_redo->add_undo_method(blend_space.ptr(), "set_value_label", blend_space->get_value_label());
396 undo_redo->add_do_method(this, "_update_space");
397 undo_redo->add_undo_method(this, "_update_space");
398 undo_redo->commit_action();
399 updating = false;
400 }
401
402 void AnimationNodeBlendSpace1DEditor::_snap_toggled() {
403 blend_space_draw->queue_redraw();
404 }
405
406 void AnimationNodeBlendSpace1DEditor::_file_opened(const String &p_file) {
407 file_loaded = ResourceLoader::load(p_file);
408 if (file_loaded.is_valid()) {
409 _add_menu_type(MENU_LOAD_FILE_CONFIRM);
410 } else {
411 EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only animation nodes are allowed."));
412 }
413 }
414
415 void AnimationNodeBlendSpace1DEditor::_add_menu_type(int p_index) {
416 Ref<AnimationRootNode> node;
417 if (p_index == MENU_LOAD_FILE) {
418 open_file->clear_filters();
419 List<String> filters;
420 ResourceLoader::get_recognized_extensions_for_type("AnimationRootNode", &filters);
421 for (const String &E : filters) {
422 open_file->add_filter("*." + E);
423 }
424 open_file->popup_file_dialog();
425 return;
426 } else if (p_index == MENU_LOAD_FILE_CONFIRM) {
427 node = file_loaded;
428 file_loaded.unref();
429 } else if (p_index == MENU_PASTE) {
430 node = EditorSettings::get_singleton()->get_resource_clipboard();
431 } else {
432 String type = menu->get_item_metadata(p_index);
433
434 Object *obj = ClassDB::instantiate(type);
435 ERR_FAIL_NULL(obj);
436 AnimationNode *an = Object::cast_to<AnimationNode>(obj);
437 ERR_FAIL_NULL(an);
438
439 node = Ref<AnimationNode>(an);
440 }
441
442 if (node.is_null()) {
443 EditorNode::get_singleton()->show_warning(TTR("This type of node can't be used. Only root nodes are allowed."));
444 return;
445 }
446
447 updating = true;
448 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
449 undo_redo->create_action(TTR("Add Node Point"));
450 undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", node, add_point_pos);
451 undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
452 undo_redo->add_do_method(this, "_update_space");
453 undo_redo->add_undo_method(this, "_update_space");
454 undo_redo->commit_action();
455 updating = false;
456
457 blend_space_draw->queue_redraw();
458 }
459
460 void AnimationNodeBlendSpace1DEditor::_add_animation_type(int p_index) {
461 Ref<AnimationNodeAnimation> anim;
462 anim.instantiate();
463
464 anim->set_animation(animations_to_add[p_index]);
465
466 updating = true;
467 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
468 undo_redo->create_action(TTR("Add Animation Point"));
469 undo_redo->add_do_method(blend_space.ptr(), "add_blend_point", anim, add_point_pos);
470 undo_redo->add_undo_method(blend_space.ptr(), "remove_blend_point", blend_space->get_blend_point_count());
471 undo_redo->add_do_method(this, "_update_space");
472 undo_redo->add_undo_method(this, "_update_space");
473 undo_redo->commit_action();
474 updating = false;
475
476 blend_space_draw->queue_redraw();
477 }
478
479 void AnimationNodeBlendSpace1DEditor::_tool_switch(int p_tool) {
480 if (p_tool == 0) {
481 tool_erase->show();
482 tool_erase_sep->show();
483 } else {
484 tool_erase->hide();
485 tool_erase_sep->hide();
486 }
487
488 _update_tool_erase();
489 blend_space_draw->queue_redraw();
490 }
491
492 void AnimationNodeBlendSpace1DEditor::_update_edited_point_pos() {
493 if (updating) {
494 return;
495 }
496
497 if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
498 float pos = blend_space->get_blend_point_position(selected_point);
499
500 if (dragging_selected) {
501 pos += drag_ofs.x;
502
503 if (snap->is_pressed()) {
504 pos = Math::snapped(pos, blend_space->get_snap());
505 }
506 }
507
508 updating = true;
509 edit_value->set_value(pos);
510 updating = false;
511 }
512 }
513
514 void AnimationNodeBlendSpace1DEditor::_update_tool_erase() {
515 bool point_valid = selected_point >= 0 && selected_point < blend_space->get_blend_point_count();
516 tool_erase->set_disabled(!point_valid || read_only);
517
518 if (point_valid) {
519 Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
520
521 if (AnimationTreeEditor::get_singleton()->can_edit(an)) {
522 open_editor->show();
523 } else {
524 open_editor->hide();
525 }
526
527 if (!read_only) {
528 edit_hb->show();
529 } else {
530 edit_hb->hide();
531 }
532 } else {
533 edit_hb->hide();
534 }
535 }
536
537 void AnimationNodeBlendSpace1DEditor::_erase_selected() {
538 if (selected_point != -1) {
539 updating = true;
540
541 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
542 undo_redo->create_action(TTR("Remove BlendSpace1D Point"));
543 undo_redo->add_do_method(blend_space.ptr(), "remove_blend_point", selected_point);
544 undo_redo->add_undo_method(blend_space.ptr(), "add_blend_point", blend_space->get_blend_point_node(selected_point), blend_space->get_blend_point_position(selected_point), selected_point);
545 undo_redo->add_do_method(this, "_update_space");
546 undo_redo->add_undo_method(this, "_update_space");
547 undo_redo->commit_action();
548
549 // Return selection to host BlendSpace1D node.
550 EditorNode::get_singleton()->push_item(blend_space.ptr(), "", true);
551
552 updating = false;
553 _update_tool_erase();
554
555 blend_space_draw->queue_redraw();
556 }
557 }
558
559 void AnimationNodeBlendSpace1DEditor::_edit_point_pos(double) {
560 if (updating) {
561 return;
562 }
563
564 updating = true;
565 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
566 undo_redo->create_action(TTR("Move BlendSpace1D Node Point"));
567 undo_redo->add_do_method(blend_space.ptr(), "set_blend_point_position", selected_point, edit_value->get_value());
568 undo_redo->add_undo_method(blend_space.ptr(), "set_blend_point_position", selected_point, blend_space->get_blend_point_position(selected_point));
569 undo_redo->add_do_method(this, "_update_space");
570 undo_redo->add_undo_method(this, "_update_space");
571 undo_redo->add_do_method(this, "_update_edited_point_pos");
572 undo_redo->add_undo_method(this, "_update_edited_point_pos");
573 undo_redo->commit_action();
574 updating = false;
575
576 blend_space_draw->queue_redraw();
577 }
578
579 void AnimationNodeBlendSpace1DEditor::_open_editor() {
580 if (selected_point >= 0 && selected_point < blend_space->get_blend_point_count()) {
581 Ref<AnimationNode> an = blend_space->get_blend_point_node(selected_point);
582 ERR_FAIL_COND(an.is_null());
583 AnimationTreeEditor::get_singleton()->enter_editor(itos(selected_point));
584 }
585 }
586
587 void AnimationNodeBlendSpace1DEditor::_notification(int p_what) {
588 switch (p_what) {
589 case NOTIFICATION_THEME_CHANGED: {
590 error_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
591 error_label->add_theme_color_override(SceneStringName(font_color), get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
592 panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SceneStringName(panel), SNAME("Tree")));
593 tool_blend->set_button_icon(get_editor_theme_icon(SNAME("EditPivot")));
594 tool_select->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
595 tool_create->set_button_icon(get_editor_theme_icon(SNAME("EditKey")));
596 tool_erase->set_button_icon(get_editor_theme_icon(SNAME("Remove")));
597 snap->set_button_icon(get_editor_theme_icon(SNAME("SnapGrid")));
598 open_editor->set_button_icon(get_editor_theme_icon(SNAME("Edit")));
599 interpolation->clear();
600 interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), 0);
601 interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), 1);
602 interpolation->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), 2);
603 } break;
604
605 case NOTIFICATION_PROCESS: {
606 AnimationTree *tree = AnimationTreeEditor::get_singleton()->get_animation_tree();
607 if (!tree) {
608 return;
609 }
610
611 String error;
612
613 error = tree->get_editor_error_message();
614
615 if (error != error_label->get_text()) {
616 error_label->set_text(error);
617 if (!error.is_empty()) {
618 error_panel->show();
619 } else {
620 error_panel->hide();
621 }
622 }
623 } break;
624
625 case NOTIFICATION_VISIBILITY_CHANGED: {
626 set_process(is_visible_in_tree());
627 } break;
628 }
629 }
630
631 void AnimationNodeBlendSpace1DEditor::_bind_methods() {
632 ClassDB::bind_method("_update_space", &AnimationNodeBlendSpace1DEditor::_update_space);
633 ClassDB::bind_method("_update_tool_erase", &AnimationNodeBlendSpace1DEditor::_update_tool_erase);
634
635 ClassDB::bind_method("_update_edited_point_pos", &AnimationNodeBlendSpace1DEditor::_update_edited_point_pos);
636 }
637
638 bool AnimationNodeBlendSpace1DEditor::can_edit(const Ref<AnimationNode> &p_node) {
639 Ref<AnimationNodeBlendSpace1D> b1d = p_node;
640 return b1d.is_valid();
641 }
642
643 void AnimationNodeBlendSpace1DEditor::edit(const Ref<AnimationNode> &p_node) {
644 blend_space = p_node;
645 read_only = false;
646
647 if (blend_space.is_valid()) {
648 read_only = EditorNode::get_singleton()->is_resource_read_only(blend_space);
649
650 _update_space();
651 }
652
653 tool_create->set_disabled(read_only);
654 edit_value->set_editable(!read_only);
655 label_value->set_editable(!read_only);
656 min_value->set_editable(!read_only);
657 max_value->set_editable(!read_only);
658 sync->set_disabled(read_only);
659 interpolation->set_disabled(read_only);
660 }
661
662 AnimationNodeBlendSpace1DEditor *AnimationNodeBlendSpace1DEditor::singleton = nullptr;
663
664 AnimationNodeBlendSpace1DEditor::AnimationNodeBlendSpace1DEditor() {
665 singleton = this;
666
667 HBoxContainer *top_hb = memnew(HBoxContainer);
668 add_child(top_hb);
669
670 Ref<ButtonGroup> bg;
671 bg.instantiate();
672
673 tool_select = memnew(Button);
674 tool_select->set_theme_type_variation(SceneStringName(FlatButton));
675 tool_select->set_toggle_mode(true);
676 tool_select->set_button_group(bg);
677 top_hb->add_child(tool_select);
678 tool_select->set_pressed(true);
679 tool_select->set_tooltip_text(TTR("Select and move points.\nRMB: Create point at position clicked.\nShift+LMB+Drag: Set the blending position within the space."));
680 tool_select->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(0));
681
682 tool_create = memnew(Button);
683 tool_create->set_theme_type_variation(SceneStringName(FlatButton));
684 tool_create->set_toggle_mode(true);
685 tool_create->set_button_group(bg);
686 top_hb->add_child(tool_create);
687 tool_create->set_tooltip_text(TTR("Create points."));
688 tool_create->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(1));
689
690 tool_blend = memnew(Button);
691 tool_blend->set_theme_type_variation(SceneStringName(FlatButton));
692 tool_blend->set_toggle_mode(true);
693 tool_blend->set_button_group(bg);
694 top_hb->add_child(tool_blend);
695 tool_blend->set_tooltip_text(TTR("Set the blending position within the space."));
696 tool_blend->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_tool_switch).bind(2));
697
698 tool_erase_sep = memnew(VSeparator);
699 top_hb->add_child(tool_erase_sep);
700 tool_erase = memnew(Button);
701 tool_erase->set_theme_type_variation(SceneStringName(FlatButton));
702 top_hb->add_child(tool_erase);
703 tool_erase->set_tooltip_text(TTR("Erase points."));
704 tool_erase->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_erase_selected));
705
706 top_hb->add_child(memnew(VSeparator));
707
708 snap = memnew(Button);
709 snap->set_theme_type_variation(SceneStringName(FlatButton));
710 snap->set_toggle_mode(true);
711 top_hb->add_child(snap);
712 snap->set_pressed(true);
713 snap->set_tooltip_text(TTR("Enable snap and show grid."));
714 snap->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_snap_toggled));
715
716 snap_value = memnew(SpinBox);
717 top_hb->add_child(snap_value);
718 snap_value->set_min(0.01);
719 snap_value->set_step(0.01);
720 snap_value->set_max(1000);
721 snap_value->set_accessibility_name(TTRC("Grid Step"));
722
723 top_hb->add_child(memnew(VSeparator));
724 top_hb->add_child(memnew(Label(TTR("Sync:"))));
725 sync = memnew(CheckBox);
726 top_hb->add_child(sync);
727 sync->connect(SceneStringName(toggled), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
728
729 top_hb->add_child(memnew(VSeparator));
730
731 top_hb->add_child(memnew(Label(TTR("Blend:"))));
732 interpolation = memnew(OptionButton);
733 top_hb->add_child(interpolation);
734 interpolation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
735
736 edit_hb = memnew(HBoxContainer);
737 top_hb->add_child(edit_hb);
738 edit_hb->add_child(memnew(VSeparator));
739 edit_hb->add_child(memnew(Label(TTR("Point"))));
740
741 edit_value = memnew(SpinBox);
742 edit_hb->add_child(edit_value);
743 edit_value->set_min(-1000);
744 edit_value->set_max(1000);
745 edit_value->set_step(0.01);
746 edit_value->set_accessibility_name(TTRC("Blend Value"));
747 edit_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_edit_point_pos));
748
749 open_editor = memnew(Button);
750 edit_hb->add_child(open_editor);
751 open_editor->set_text(TTR("Open Editor"));
752 open_editor->connect(SceneStringName(pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_open_editor), CONNECT_DEFERRED);
753
754 edit_hb->hide();
755 open_editor->hide();
756
757 VBoxContainer *main_vb = memnew(VBoxContainer);
758 add_child(main_vb);
759 main_vb->set_v_size_flags(SIZE_EXPAND_FILL);
760
761 panel = memnew(PanelContainer);
762 panel->set_clip_contents(true);
763 main_vb->add_child(panel);
764 panel->set_h_size_flags(SIZE_EXPAND_FILL);
765 panel->set_v_size_flags(SIZE_EXPAND_FILL);
766
767 blend_space_draw = memnew(Control);
768 blend_space_draw->connect(SceneStringName(gui_input), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_gui_input));
769 blend_space_draw->connect(SceneStringName(draw), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_blend_space_draw));
770 blend_space_draw->set_focus_mode(FOCUS_ALL);
771
772 panel->add_child(blend_space_draw);
773
774 {
775 HBoxContainer *bottom_hb = memnew(HBoxContainer);
776 main_vb->add_child(bottom_hb);
777 bottom_hb->set_h_size_flags(SIZE_EXPAND_FILL);
778
779 min_value = memnew(SpinBox);
780 min_value->set_min(-10000);
781 min_value->set_max(0);
782 min_value->set_step(0.01);
783 min_value->set_accessibility_name(TTRC("Min"));
784
785 max_value = memnew(SpinBox);
786 max_value->set_min(0.01);
787 max_value->set_max(10000);
788 max_value->set_step(0.01);
789 max_value->set_accessibility_name(TTRC("Max"));
790
791 label_value = memnew(LineEdit);
792 label_value->set_expand_to_text_length_enabled(true);
793 label_value->set_accessibility_name(TTRC("Value"));
794
795 // now add
796
797 bottom_hb->add_child(min_value);
798 bottom_hb->add_spacer();
799 bottom_hb->add_child(label_value);
800 bottom_hb->add_spacer();
801 bottom_hb->add_child(max_value);
802 }
803
804 snap_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
805 min_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
806 max_value->connect(SceneStringName(value_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_config_changed));
807 label_value->connect(SceneStringName(text_changed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_labels_changed));
808
809 error_panel = memnew(PanelContainer);
810 add_child(error_panel);
811
812 error_label = memnew(Label);
813 error_label->set_focus_mode(FOCUS_ACCESSIBILITY);
814 error_panel->add_child(error_label);
815 error_panel->hide();
816
817 menu = memnew(PopupMenu);
818 add_child(menu);
819 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_menu_type));
820
821 animations_menu = memnew(PopupMenu);
822 animations_menu->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
823 menu->add_child(animations_menu);
824 animations_menu->connect("index_pressed", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_add_animation_type));
825
826 open_file = memnew(EditorFileDialog);
827 add_child(open_file);
828 open_file->set_title(TTR("Open Animation Node"));
829 open_file->set_file_mode(EditorFileDialog::FILE_MODE_OPEN_FILE);
830 open_file->connect("file_selected", callable_mp(this, &AnimationNodeBlendSpace1DEditor::_file_opened));
831
832 set_custom_minimum_size(Size2(0, 150 * EDSCALE));
833 }