Debian Codegraph @

editor/animation/animation_player_editor_plugin.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* animation_player_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_player_editor_plugin.h"
32
33 #include "core/config/project_settings.h"
34 #include "core/input/input.h"
35 #include "core/os/keyboard.h"
36 #include "editor/animation/animation_tree_editor_plugin.h"
37 #include "editor/docks/editor_dock_manager.h"
38 #include "editor/docks/inspector_dock.h"
39 #include "editor/docks/scene_tree_dock.h"
40 #include "editor/editor_node.h"
41 #include "editor/editor_undo_redo_manager.h"
42 #include "editor/gui/editor_file_dialog.h"
43 #include "editor/gui/editor_validation_panel.h"
44 #include "editor/scene/3d/node_3d_editor_plugin.h" // For onion skinning.
45 #include "editor/scene/canvas_item_editor_plugin.h" // For onion skinning.
46 #include "editor/settings/editor_command_palette.h"
47 #include "editor/settings/editor_settings.h"
48 #include "editor/themes/editor_scale.h"
49 #include "editor/themes/editor_theme_manager.h"
50 #include "scene/animation/animation_tree.h"
51 #include "scene/gui/separator.h"
52 #include "scene/main/window.h"
53 #include "scene/resources/animation.h"
54 #include "scene/resources/image_texture.h"
55 #include "servers/rendering/rendering_server.h"
56
57 ///////////////////////////////////
58
59 void AnimationPlayerEditor::_find_player() {
60 if (!is_visible() || player) {
61 return;
62 }
63
64 Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
65
66 if (!edited_scene) {
67 return;
68 }
69
70 TypedArray<Node> players = edited_scene->find_children("", "AnimationPlayer");
71
72 if (players.size() == 1) {
73 // Replicating EditorNode::_plugin_over_edit to ensure an identical setup as when selecting manually.
74 plugin->edit(players.front());
75 plugin->make_visible(true);
76 }
77 }
78
79 void AnimationPlayerEditor::_node_removed(Node *p_node) {
80 if (player && original_node == p_node) {
81 if (is_dummy) {
82 plugin->_clear_dummy_player();
83 }
84
85 player = nullptr;
86
87 set_process(false);
88
89 track_editor->set_animation(Ref<Animation>(), true);
90 track_editor->set_root(nullptr);
91 track_editor->show_select_node_warning(true);
92 _update_player();
93
94 _ensure_dummy_player();
95
96 pin->set_pressed(false);
97 }
98 }
99
100 void AnimationPlayerEditor::_notification(int p_what) {
101 switch (p_what) {
102 case NOTIFICATION_PROCESS: {
103 finishing = false;
104 if (!player || is_dummy) {
105 track_editor->show_inactive_player_warning(false);
106 } else {
107 track_editor->show_inactive_player_warning(!player->is_active());
108 }
109
110 if (!player) {
111 return;
112 }
113
114 updating = true;
115
116 if (player->is_playing()) {
117 {
118 StringName animname = player->get_assigned_animation();
119
120 if (player->has_animation(animname)) {
121 Ref<Animation> anim = player->get_animation(animname);
122 if (anim.is_valid()) {
123 frame->set_max((double)anim->get_length());
124 }
125 }
126 }
127 frame->set_value(player->get_current_animation_position());
128 track_editor->set_anim_pos(player->get_current_animation_position());
129 } else {
130 if (!player->is_valid()) {
131 // Reset timeline when the player has been stopped externally
132 frame->set_value(0);
133 } else if (last_active) {
134 // Need the last frame after it stopped.
135 frame->set_value(player->get_current_animation_position());
136 track_editor->set_anim_pos(player->get_current_animation_position());
137 }
138 stop->set_button_icon(stop_icon);
139 }
140
141 last_active = player->is_playing();
142
143 updating = false;
144 } break;
145
146 case NOTIFICATION_ENTER_TREE: {
147 get_tree()->connect(SNAME("node_removed"), callable_mp(this, &AnimationPlayerEditor::_node_removed));
148 } break;
149
150 case NOTIFICATION_EXIT_TREE: {
151 get_tree()->disconnect(SNAME("node_removed"), callable_mp(this, &AnimationPlayerEditor::_node_removed));
152 } break;
153
154 case NOTIFICATION_READY: {
155 EditorNode::get_singleton()->connect("scene_changed", callable_mp(this, &AnimationPlayerEditor::_find_player));
156
157 add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SceneStringName(panel), SNAME("Panel")));
158
159 _update_playback_tooltips();
160 } break;
161
162 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
163 if (EditorThemeManager::is_generated_theme_outdated()) {
164 add_theme_style_override(SceneStringName(panel), EditorNode::get_singleton()->get_editor_theme()->get_stylebox(SceneStringName(panel), SNAME("Panel")));
165 }
166
167 _update_playback_tooltips();
168 } break;
169
170 case NOTIFICATION_TRANSLATION_CHANGED:
171 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
172 case NOTIFICATION_THEME_CHANGED: {
173 stop_icon = get_editor_theme_icon(SNAME("Stop"));
174 pause_icon = get_editor_theme_icon(SNAME("Pause"));
175 if (player && player->is_playing()) {
176 stop->set_button_icon(pause_icon);
177 } else {
178 stop->set_button_icon(stop_icon);
179 }
180
181 autoplay->set_button_icon(get_editor_theme_icon(SNAME("AutoPlay")));
182 play->set_button_icon(get_editor_theme_icon(SNAME("PlayStart")));
183 play_from->set_button_icon(get_editor_theme_icon(SNAME("Play")));
184 play_bw->set_button_icon(get_editor_theme_icon(SNAME("PlayStartBackwards")));
185 play_bw_from->set_button_icon(get_editor_theme_icon(SNAME("PlayBackwards")));
186
187 autoplay_icon = get_editor_theme_icon(SNAME("AutoPlay"));
188 reset_icon = get_editor_theme_icon(SNAME("Reload"));
189 {
190 Ref<Image> autoplay_img = autoplay_icon->get_image();
191 Ref<Image> reset_img = reset_icon->get_image();
192 Size2 icon_size = autoplay_img->get_size();
193 Ref<Image> autoplay_reset_img = Image::create_empty(icon_size.x * 2, icon_size.y, false, autoplay_img->get_format());
194 autoplay_reset_img->blit_rect(autoplay_img, Rect2i(Point2i(), icon_size), Point2i());
195 autoplay_reset_img->blit_rect(reset_img, Rect2i(Point2i(), icon_size), Point2i(icon_size.x, 0));
196 autoplay_reset_icon = ImageTexture::create_from_image(autoplay_reset_img);
197 }
198
199 onion_toggle->set_button_icon(get_editor_theme_icon(SNAME("Onion")));
200 onion_skinning->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
201
202 pin->set_button_icon(get_editor_theme_icon(SNAME("Pin")));
203
204 #define ITEM_ICON(m_item, m_icon) tool_anim->get_popup()->set_item_icon(tool_anim->get_popup()->get_item_index(m_item), get_editor_theme_icon(SNAME(m_icon)))
205
206 ITEM_ICON(TOOL_NEW_ANIM, "New");
207 ITEM_ICON(TOOL_ANIM_LIBRARY, "AnimationLibrary");
208 ITEM_ICON(TOOL_DUPLICATE_ANIM, "Duplicate");
209 ITEM_ICON(TOOL_RENAME_ANIM, "Rename");
210 ITEM_ICON(TOOL_EDIT_TRANSITIONS, "Blend");
211 ITEM_ICON(TOOL_EDIT_RESOURCE, "Edit");
212 ITEM_ICON(TOOL_REMOVE_ANIM, "Remove");
213
214 _update_animation_list_icons();
215 } break;
216
217 case NOTIFICATION_VISIBILITY_CHANGED: {
218 _find_player();
219 _ensure_dummy_player();
220 } break;
221 }
222 }
223
224 void AnimationPlayerEditor::_autoplay_pressed() {
225 if (updating) {
226 return;
227 }
228 if (animation->has_selectable_items() == 0) {
229 return;
230 }
231
232 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
233 String current = animation->get_item_text(animation->get_selected());
234 if (player->get_autoplay() == current) {
235 //unset
236 undo_redo->create_action(TTR("Toggle Autoplay"));
237 undo_redo->add_do_method(player, "set_autoplay", StringName());
238 undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
239 undo_redo->add_do_method(this, "_animation_player_changed", player);
240 undo_redo->add_undo_method(this, "_animation_player_changed", player);
241 undo_redo->commit_action();
242
243 } else {
244 //set
245 undo_redo->create_action(TTR("Toggle Autoplay"));
246 undo_redo->add_do_method(player, "set_autoplay", StringName(current));
247 undo_redo->add_undo_method(player, "set_autoplay", player->get_autoplay());
248 undo_redo->add_do_method(this, "_animation_player_changed", player);
249 undo_redo->add_undo_method(this, "_animation_player_changed", player);
250 undo_redo->commit_action();
251 }
252 }
253
254 void AnimationPlayerEditor::go_to_nearest_keyframe(bool p_backward) {
255 if (_get_current().is_empty()) {
256 return;
257 }
258
259 Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
260
261 double current_time = player->get_current_animation_position();
262 // Offset the time to avoid finding the same keyframe with Animation::track_find_key().
263 double time_offset = MAX(CMP_EPSILON * 2, current_time * CMP_EPSILON * 2);
264 double current_time_offset = current_time + (p_backward ? -time_offset : time_offset);
265
266 float nearest_key_time = p_backward ? 0 : anim->get_length();
267 int track_count = anim->get_track_count();
268 bool bezier_active = track_editor->is_bezier_editor_active();
269
270 Node *root = get_tree()->get_edited_scene_root();
271 EditorSelection *selection = EditorNode::get_singleton()->get_editor_selection();
272
273 Vector<int> selected_tracks;
274 for (int i = 0; i < track_count; ++i) {
275 if (selection->is_selected(root->get_node_or_null(anim->track_get_path(i)))) {
276 selected_tracks.push_back(i);
277 }
278 }
279
280 // Find the nearest keyframe in selection if the scene has selected nodes
281 // or the nearest keyframe in the entire animation otherwise.
282 if (selected_tracks.size() > 0) {
283 for (int track : selected_tracks) {
284 if (bezier_active && anim->track_get_type(track) != Animation::TYPE_BEZIER) {
285 continue;
286 }
287 int key = anim->track_find_key(track, current_time_offset, Animation::FIND_MODE_NEAREST, false, !p_backward);
288 if (key == -1) {
289 continue;
290 }
291 double key_time = anim->track_get_key_time(track, key);
292 if ((p_backward && key_time > nearest_key_time) || (!p_backward && key_time < nearest_key_time)) {
293 nearest_key_time = key_time;
294 }
295 }
296 } else {
297 for (int track = 0; track < track_count; ++track) {
298 if (bezier_active && anim->track_get_type(track) != Animation::TYPE_BEZIER) {
299 continue;
300 }
301 int key = anim->track_find_key(track, current_time_offset, Animation::FIND_MODE_NEAREST, false, !p_backward);
302 if (key == -1) {
303 continue;
304 }
305 double key_time = anim->track_get_key_time(track, key);
306 if ((p_backward && key_time > nearest_key_time) || (!p_backward && key_time < nearest_key_time)) {
307 nearest_key_time = key_time;
308 }
309 }
310 }
311
312 player->seek_internal(nearest_key_time, true, true, true);
313 frame->set_value(nearest_key_time);
314 track_editor->set_anim_pos(nearest_key_time);
315 }
316
317 void AnimationPlayerEditor::_play_pressed() {
318 String current = _get_current();
319
320 if (!current.is_empty()) {
321 if (current == player->get_assigned_animation()) {
322 player->stop(); // So it won't blend with itself.
323 }
324 ERR_FAIL_COND_EDMSG(!_validate_tracks(player->get_animation(current)), "Animation tracks may have any invalid key, abort playing.");
325 PackedStringArray markers = track_editor->get_selected_section();
326 if (markers.size() == 2) {
327 StringName start_marker = markers[0];
328 StringName end_marker = markers[1];
329 player->play_section_with_markers(current, start_marker, end_marker);
330 } else {
331 player->play(current);
332 }
333 }
334
335 //unstop
336 stop->set_button_icon(pause_icon);
337 }
338
339 void AnimationPlayerEditor::_play_from_pressed() {
340 String current = _get_current();
341
342 if (!current.is_empty()) {
343 if (!player->is_valid()) {
344 _play_pressed(); // Fallback.
345 return;
346 }
347 double time = player->get_current_animation_position();
348 if (current == player->get_assigned_animation() && player->is_playing()) {
349 player->clear_caches(); // So it won't blend with itself.
350 }
351 ERR_FAIL_COND_EDMSG(!_validate_tracks(player->get_animation(current)), "Animation tracks may have any invalid key, abort playing.");
352 player->seek_internal(time, true, true, true);
353 PackedStringArray markers = track_editor->get_selected_section();
354 if (markers.size() == 2) {
355 StringName start_marker = markers[0];
356 StringName end_marker = markers[1];
357 player->play_section_with_markers(current, start_marker, end_marker);
358 } else {
359 player->play(current);
360 }
361 }
362
363 //unstop
364 stop->set_button_icon(pause_icon);
365 }
366
367 String AnimationPlayerEditor::_get_current() const {
368 String current;
369 if (animation->get_selected() >= 0 && animation->get_selected() < animation->get_item_count() && !animation->is_item_separator(animation->get_selected())) {
370 current = animation->get_item_text(animation->get_selected());
371 }
372 return current;
373 }
374 void AnimationPlayerEditor::_play_bw_pressed() {
375 String current = _get_current();
376 if (!current.is_empty()) {
377 if (current == player->get_assigned_animation()) {
378 player->stop(); // So it won't blend with itself.
379 }
380 ERR_FAIL_COND_EDMSG(!_validate_tracks(player->get_animation(current)), "Animation tracks may have any invalid key, abort playing.");
381 PackedStringArray markers = track_editor->get_selected_section();
382 if (markers.size() == 2) {
383 StringName start_marker = markers[0];
384 StringName end_marker = markers[1];
385 player->play_section_with_markers_backwards(current, start_marker, end_marker);
386 } else {
387 player->play_backwards(current);
388 }
389 }
390
391 //unstop
392 stop->set_button_icon(pause_icon);
393 }
394
395 void AnimationPlayerEditor::_play_bw_from_pressed() {
396 String current = _get_current();
397
398 if (!current.is_empty()) {
399 if (!player->is_valid()) {
400 _play_bw_pressed(); // Fallback.
401 return;
402 }
403 double time = player->get_current_animation_position();
404 if (current == player->get_assigned_animation() && player->is_playing()) {
405 player->clear_caches(); // So it won't blend with itself.
406 }
407 ERR_FAIL_COND_EDMSG(!_validate_tracks(player->get_animation(current)), "Animation tracks may have any invalid key, abort playing.");
408 player->seek_internal(time, true, true, true);
409 PackedStringArray markers = track_editor->get_selected_section();
410 if (markers.size() == 2) {
411 StringName start_marker = markers[0];
412 StringName end_marker = markers[1];
413 player->play_section_with_markers_backwards(current, start_marker, end_marker);
414 } else {
415 player->play_backwards(current);
416 }
417 }
418
419 //unstop
420 stop->set_button_icon(pause_icon);
421 }
422
423 void AnimationPlayerEditor::_stop_pressed() {
424 if (!player) {
425 return;
426 }
427
428 if (player->is_playing()) {
429 player->pause();
430 } else {
431 String current = _get_current();
432 player->stop();
433 player->set_assigned_animation(current);
434 frame->set_value(0);
435 track_editor->set_anim_pos(0);
436 }
437 stop->set_button_icon(stop_icon);
438 }
439
440 void AnimationPlayerEditor::_animation_selected(int p_which) {
441 if (updating) {
442 return;
443 }
444
445 #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), true)
446 ITEM_CHECK_DISABLED(TOOL_RENAME_ANIM);
447 ITEM_CHECK_DISABLED(TOOL_DUPLICATE_ANIM);
448 ITEM_CHECK_DISABLED(TOOL_REMOVE_ANIM);
449
450 ITEM_CHECK_DISABLED(TOOL_EDIT_TRANSITIONS);
451 ITEM_CHECK_DISABLED(TOOL_EDIT_RESOURCE);
452 #undef ITEM_CHECK_DISABLED
453
454 // when selecting an animation, the idea is that the only interesting behavior
455 // ui-wise is that it should play/blend the next one if currently playing
456 String current = _get_current();
457
458 if (!current.is_empty()) {
459 player->set_assigned_animation(current);
460
461 Ref<Animation> anim = player->get_animation(current);
462 ERR_FAIL_COND(anim.is_null());
463 {
464 bool animation_is_readonly = EditorNode::get_singleton()->is_resource_read_only(anim);
465
466 track_editor->set_animation(anim, animation_is_readonly);
467 Node *root = player->get_node_or_null(player->get_root_node());
468
469 // Player shouldn't access parent if it's the scene root.
470 if (!root || (player == get_tree()->get_edited_scene_root() && player->get_root_node() == SceneStringName(path_pp))) {
471 NodePath cached_root_path = player->get_path_to(get_cached_root_node());
472 if (player->get_node_or_null(cached_root_path) != nullptr) {
473 player->set_root_node(cached_root_path);
474 } else {
475 player->set_root_node(SceneStringName(path_pp)); // No other choice, preventing crash.
476 }
477 } else {
478 cached_root_node_id = root->get_instance_id(); // Caching as `track_editor` can lose track of player's root node.
479 track_editor->set_root(root);
480 }
481 }
482 frame->set_max((double)anim->get_length());
483 autoplay->set_pressed(current == player->get_autoplay());
484 player->stop();
485 } else {
486 track_editor->set_animation(Ref<Animation>(), true);
487 track_editor->set_root(nullptr);
488 autoplay->set_pressed(false);
489 }
490
491 AnimationPlayerEditor::get_singleton()->get_track_editor()->update_keying();
492 _animation_key_editor_seek(timeline_position);
493
494 emit_signal("animation_selected", current);
495 }
496
497 void AnimationPlayerEditor::_animation_new() {
498 int count = 1;
499 String base = "new_animation";
500 String current_library_name = "";
501 if (animation->has_selectable_items()) {
502 String current_animation_name = animation->get_item_text(animation->get_selected());
503 Ref<Animation> current_animation = player->get_animation(current_animation_name);
504 if (current_animation.is_valid()) {
505 current_library_name = player->find_animation_library(current_animation);
506 }
507 }
508 String attempt_prefix = (current_library_name == "") ? "" : current_library_name + "/";
509 while (true) {
510 String attempt = base;
511 if (count > 1) {
512 attempt += vformat("_%d", count);
513 }
514 if (player->has_animation(attempt_prefix + attempt)) {
515 count++;
516 continue;
517 }
518 base = attempt;
519 break;
520 }
521
522 _update_name_dialog_library_dropdown();
523
524 name_dialog_op = TOOL_NEW_ANIM;
525 name_dialog->set_title(TTR("Create New Animation"));
526 name_dialog->popup_centered(Size2(300, 90));
527 name_title->set_text(TTR("New Animation Name:"));
528 name->set_text(base);
529 name->select_all();
530 name->grab_focus();
531 }
532
533 void AnimationPlayerEditor::_animation_rename() {
534 if (!animation->has_selectable_items()) {
535 return;
536 }
537 int selected = animation->get_selected();
538 String selected_name = animation->get_item_text(selected);
539
540 // Remove library prefix if present.
541 if (selected_name.contains_char('/')) {
542 selected_name = selected_name.get_slicec('/', 1);
543 }
544
545 name_dialog->set_title(TTR("Rename Animation"));
546 name_title->set_text(TTR("Change Animation Name:"));
547 name->set_text(selected_name);
548 name_dialog_op = TOOL_RENAME_ANIM;
549 name_dialog->popup_centered(Size2(300, 90));
550 name->select_all();
551 name->grab_focus();
552 library->hide();
553 }
554
555 void AnimationPlayerEditor::_animation_remove() {
556 if (!animation->has_selectable_items()) {
557 return;
558 }
559
560 String current = animation->get_item_text(animation->get_selected());
561
562 delete_dialog->set_text(vformat(TTR("Delete Animation '%s'?"), current));
563 delete_dialog->popup_centered();
564 }
565
566 void AnimationPlayerEditor::_animation_remove_confirmed() {
567 String current = animation->get_item_text(animation->get_selected());
568 Ref<Animation> anim = player->get_animation(current);
569
570 Ref<AnimationLibrary> al = player->get_animation_library(player->find_animation_library(anim));
571 ERR_FAIL_COND(al.is_null());
572
573 // For names of form lib_name/anim_name, remove library name prefix.
574 if (current.contains_char('/')) {
575 current = current.get_slicec('/', 1);
576 }
577 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
578 undo_redo->create_action(TTR("Remove Animation"));
579 if (player->get_autoplay() == current) {
580 undo_redo->add_do_method(player, "set_autoplay", StringName());
581 undo_redo->add_undo_method(player, "set_autoplay", StringName(current));
582 // Avoid having the autoplay icon linger around if there is only one animation in the player.
583 undo_redo->add_do_method(this, "_animation_player_changed", player);
584 }
585 undo_redo->add_do_method(al.ptr(), "remove_animation", current);
586 undo_redo->add_undo_method(al.ptr(), "add_animation", current, anim);
587 undo_redo->add_do_method(this, "_animation_player_changed", player);
588 undo_redo->add_undo_method(this, "_animation_player_changed", player);
589 if (animation->has_selectable_items() && animation->get_selectable_item(false) == animation->get_selectable_item(true)) { // Last item remaining.
590 undo_redo->add_do_method(this, "_stop_onion_skinning");
591 undo_redo->add_undo_method(this, "_start_onion_skinning");
592 }
593 undo_redo->commit_action();
594 }
595
596 void AnimationPlayerEditor::_select_anim_by_name(const String &p_anim) {
597 int idx = -1;
598 for (int i = 0; i < animation->get_item_count(); i++) {
599 if (animation->get_item_text(i) == p_anim) {
600 idx = i;
601 break;
602 }
603 }
604
605 ERR_FAIL_COND(idx == -1);
606
607 animation->select(idx);
608
609 _animation_selected(idx);
610 }
611
612 float AnimationPlayerEditor::_get_editor_step() const {
613 const StringName current = player->get_assigned_animation();
614 const Ref<Animation> anim = player->get_animation(current);
615 ERR_FAIL_COND_V(anim.is_null(), 0.0);
616
617 float step = track_editor->get_snap_unit();
618
619 // Use more precise snapping when holding Shift
620 return Input::get_singleton()->is_key_pressed(Key::SHIFT) ? step * 0.25 : step;
621 }
622
623 void AnimationPlayerEditor::_animation_name_edited() {
624 if (player->is_playing()) {
625 player->stop();
626 }
627
628 String new_name = name->get_text();
629 if (!AnimationLibrary::is_valid_animation_name(new_name)) {
630 error_dialog->set_text(TTR("Invalid animation name!"));
631 error_dialog->popup_centered();
632 return;
633 }
634
635 if (name_dialog_op == TOOL_RENAME_ANIM && animation->has_selectable_items() && animation->get_item_text(animation->get_selected()) == new_name) {
636 name_dialog->hide();
637 return;
638 }
639
640 String test_name_prefix = "";
641 if (library->is_visible() && library->get_selected_id() != -1) {
642 test_name_prefix = library->get_item_metadata(library->get_selected_id());
643 test_name_prefix += (test_name_prefix != "") ? "/" : "";
644 }
645
646 if (player->has_animation(test_name_prefix + new_name)) {
647 error_dialog->set_text(vformat(TTR("Animation '%s' already exists!"), test_name_prefix + new_name));
648 error_dialog->popup_centered();
649 return;
650 }
651
652 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
653 switch (name_dialog_op) {
654 case TOOL_RENAME_ANIM: {
655 String current = animation->get_item_text(animation->get_selected());
656 Ref<Animation> anim = player->get_animation(current);
657
658 Ref<AnimationLibrary> al = player->get_animation_library(player->find_animation_library(anim));
659 ERR_FAIL_COND(al.is_null());
660
661 // Extract library prefix if present.
662 String new_library_prefix = "";
663 if (current.contains_char('/')) {
664 new_library_prefix = current.get_slicec('/', 0) + "/";
665 current = current.get_slicec('/', 1);
666 }
667
668 undo_redo->create_action(TTR("Rename Animation"));
669 undo_redo->add_do_method(al.ptr(), "rename_animation", current, new_name);
670 undo_redo->add_do_method(anim.ptr(), "set_name", new_name);
671 undo_redo->add_undo_method(al.ptr(), "rename_animation", new_name, current);
672 undo_redo->add_undo_method(anim.ptr(), "set_name", current);
673 undo_redo->add_do_method(this, "_animation_player_changed", player);
674 undo_redo->add_undo_method(this, "_animation_player_changed", player);
675 undo_redo->commit_action();
676
677 if (is_dummy) {
678 plugin->_update_dummy_player(original_node);
679 }
680 _select_anim_by_name(new_library_prefix + new_name);
681 } break;
682
683 case TOOL_NEW_ANIM: {
684 Ref<Animation> new_anim = Ref<Animation>(memnew(Animation));
685 new_anim->set_name(new_name);
686
687 if (animation->get_item_count() > 0) {
688 String current = animation->get_item_text(animation->get_selected());
689 Ref<Animation> current_anim = player->get_animation(current);
690
691 if (current_anim.is_valid()) {
692 new_anim->set_step(current_anim->get_step());
693 }
694 } else {
695 new_anim->set_step(EDITOR_GET("editors/animation/default_animation_step"));
696 }
697
698 String library_name;
699 Ref<AnimationLibrary> al;
700 library_name = library->get_item_metadata(library->get_selected());
701 // It's possible that [Global] was selected, but doesn't exist yet.
702 if (player->has_animation_library(library_name)) {
703 al = player->get_animation_library(library_name);
704 }
705
706 undo_redo->create_action(TTR("Add Animation"));
707
708 bool lib_added = false;
709 if (al.is_null()) {
710 al.instantiate();
711 lib_added = true;
712 undo_redo->add_do_method(fetch_mixer_for_library(), "add_animation_library", "", al);
713 library_name = "";
714 }
715
716 undo_redo->add_do_method(al.ptr(), "add_animation", new_name, new_anim);
717 undo_redo->add_undo_method(al.ptr(), "remove_animation", new_name);
718 undo_redo->add_do_method(this, "_animation_player_changed", player);
719 undo_redo->add_undo_method(this, "_animation_player_changed", player);
720 if (!animation->has_selectable_items()) {
721 undo_redo->add_do_method(this, "_start_onion_skinning");
722 undo_redo->add_undo_method(this, "_stop_onion_skinning");
723 }
724 if (lib_added) {
725 undo_redo->add_undo_method(fetch_mixer_for_library(), "remove_animation_library", "");
726 }
727 undo_redo->commit_action();
728
729 if (library_name != "") {
730 library_name = library_name + "/";
731 }
732
733 if (is_dummy) {
734 plugin->_update_dummy_player(original_node);
735 }
736 _select_anim_by_name(library_name + new_name);
737
738 } break;
739
740 case TOOL_DUPLICATE_ANIM: {
741 String current = animation->get_item_text(animation->get_selected());
742 Ref<Animation> anim = player->get_animation(current);
743
744 Ref<Animation> new_anim = _animation_clone(anim);
745 new_anim->set_name(new_name);
746
747 String library_name;
748 Ref<AnimationLibrary> al;
749 if (library->is_visible()) {
750 library_name = library->get_item_metadata(library->get_selected());
751 // It's possible that [Global] was selected, but doesn't exist yet.
752 if (player->has_animation_library(library_name)) {
753 al = player->get_animation_library(library_name);
754 }
755 } else {
756 if (player->has_animation_library("")) {
757 al = player->get_animation_library("");
758 library_name = "";
759 }
760 }
761
762 undo_redo->create_action(TTR("Duplicate Animation"));
763
764 bool lib_added = false;
765 if (al.is_null()) {
766 al.instantiate();
767 lib_added = true;
768 undo_redo->add_do_method(player, "add_animation_library", "", al);
769 library_name = "";
770 }
771
772 undo_redo->add_do_method(al.ptr(), "add_animation", new_name, new_anim);
773 undo_redo->add_undo_method(al.ptr(), "remove_animation", new_name);
774 undo_redo->add_do_method(this, "_animation_player_changed", player);
775 undo_redo->add_undo_method(this, "_animation_player_changed", player);
776 if (lib_added) {
777 undo_redo->add_undo_method(player, "remove_animation_library", "");
778 }
779 undo_redo->commit_action();
780
781 if (library_name != "") {
782 library_name = library_name + "/";
783 }
784
785 if (is_dummy) {
786 plugin->_update_dummy_player(original_node);
787 }
788 _select_anim_by_name(library_name + new_name);
789 } break;
790 }
791
792 name_dialog->hide();
793 }
794
795 void AnimationPlayerEditor::_blend_editor_next_changed(const int p_idx) {
796 if (!animation->has_selectable_items()) {
797 return;
798 }
799
800 String current = animation->get_item_text(animation->get_selected());
801
802 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
803 undo_redo->create_action(TTR("Blend Next Changed"));
804 undo_redo->add_do_method(player, "animation_set_next", current, blend_editor.next->get_item_text(p_idx));
805 undo_redo->add_undo_method(player, "animation_set_next", current, player->animation_get_next(current));
806 undo_redo->add_do_method(this, "_animation_player_changed", player);
807 undo_redo->add_undo_method(this, "_animation_player_changed", player);
808 undo_redo->commit_action();
809 }
810
811 void AnimationPlayerEditor::_edit_animation_blend() {
812 if (updating_blends || !animation->has_selectable_items()) {
813 return;
814 }
815
816 blend_editor.dialog->popup_centered(Size2(400, 400) * EDSCALE);
817 _update_animation_blend();
818 }
819
820 void AnimationPlayerEditor::_update_animation_blend() {
821 if (updating_blends || !animation->has_selectable_items()) {
822 return;
823 }
824
825 blend_editor.tree->clear();
826
827 String current = animation->get_item_text(animation->get_selected());
828
829 List<StringName> anims;
830 player->get_animation_list(&anims);
831 TreeItem *root = blend_editor.tree->create_item();
832 updating_blends = true;
833
834 int i = 0;
835 bool anim_found = false;
836 blend_editor.next->clear();
837 blend_editor.next->add_item("", i);
838
839 for (const StringName &to : anims) {
840 TreeItem *blend = blend_editor.tree->create_item(root);
841 blend->set_editable(0, false);
842 blend->set_editable(1, true);
843 blend->set_text(0, to);
844 blend->set_cell_mode(1, TreeItem::CELL_MODE_RANGE);
845 blend->set_range_config(1, 0, 3600, 0.001);
846 blend->set_range(1, player->get_blend_time(current, to));
847
848 i++;
849 blend_editor.next->add_item(to, i);
850 if (to == player->animation_get_next(current)) {
851 blend_editor.next->select(i);
852 anim_found = true;
853 }
854 }
855
856 // make sure we reset it else it becomes out of sync and could contain a deleted animation
857 if (!anim_found) {
858 blend_editor.next->select(0);
859 player->animation_set_next(current, blend_editor.next->get_item_text(0));
860 }
861
862 updating_blends = false;
863 }
864
865 void AnimationPlayerEditor::_blend_edited() {
866 if (updating_blends || !animation->has_selectable_items()) {
867 return;
868 }
869
870 TreeItem *selected = blend_editor.tree->get_edited();
871 if (!selected) {
872 return;
873 }
874
875 String current = animation->get_item_text(animation->get_selected());
876
877 updating_blends = true;
878 String to = selected->get_text(0);
879 float blend_time = selected->get_range(1);
880 float prev_blend_time = player->get_blend_time(current, to);
881
882 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
883 undo_redo->create_action(TTR("Change Blend Time"));
884 undo_redo->add_do_method(player, "set_blend_time", current, to, blend_time);
885 undo_redo->add_undo_method(player, "set_blend_time", current, to, prev_blend_time);
886 undo_redo->add_do_method(this, "_animation_player_changed", player);
887 undo_redo->add_undo_method(this, "_animation_player_changed", player);
888 undo_redo->commit_action();
889 updating_blends = false;
890 }
891
892 void AnimationPlayerEditor::ensure_visibility() {
893 if (player) {
894 return; // another player is pinned, don't reset
895 }
896
897 _animation_edit();
898 }
899
900 Dictionary AnimationPlayerEditor::get_state() const {
901 Dictionary d;
902
903 if (!is_dummy) {
904 d["visible"] = is_visible_in_tree();
905 if (EditorNode::get_singleton()->get_edited_scene() && is_visible_in_tree() && player) {
906 d["player"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(player);
907 d["animation"] = player->get_assigned_animation();
908 d["track_editor_state"] = track_editor->get_state();
909 }
910 }
911
912 return d;
913 }
914
915 void AnimationPlayerEditor::set_state(const Dictionary &p_state) {
916 if (!p_state.has("visible") || !p_state["visible"]) {
917 return;
918 }
919 if (!EditorNode::get_singleton()->get_edited_scene()) {
920 return;
921 }
922
923 if (p_state.has("player")) {
924 Node *n = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["player"]);
925 if (Object::cast_to<AnimationPlayer>(n) && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
926 if (player) {
927 if (player->is_connected(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
928 player->disconnect(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated));
929 }
930 if (player->is_connected(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed))) {
931 player->disconnect(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed));
932 }
933 }
934 player = Object::cast_to<AnimationPlayer>(n);
935 if (player) {
936 if (!player->is_connected(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
937 player->connect(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated), CONNECT_DEFERRED);
938 }
939 if (!player->is_connected(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed))) {
940 player->connect(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed));
941 }
942 }
943
944 _update_player();
945 make_visible();
946 set_process(true);
947 ensure_visibility();
948
949 if (p_state.has("animation")) {
950 String anim = p_state["animation"];
951 if (!anim.is_empty() && player->has_animation(anim)) {
952 _select_anim_by_name(anim);
953 _animation_edit();
954 }
955 }
956 }
957 }
958
959 if (p_state.has("track_editor_state")) {
960 track_editor->set_state(p_state["track_editor_state"]);
961 }
962 }
963
964 void AnimationPlayerEditor::clear() {
965 track_editor->clear();
966 }
967
968 void AnimationPlayerEditor::_animation_resource_edit() {
969 String current = _get_current();
970 if (current != String()) {
971 Ref<Animation> anim = player->get_animation(current);
972 EditorNode::get_singleton()->edit_resource(anim);
973 }
974 }
975
976 void AnimationPlayerEditor::_animation_edit() {
977 String current = _get_current();
978 if (current != String()) {
979 Ref<Animation> anim = player->get_animation(current);
980
981 track_editor->set_animation(anim, EditorNode::get_singleton()->is_resource_read_only(anim));
982
983 Node *root = player->get_node_or_null(player->get_root_node());
984 if (root) {
985 track_editor->set_root(root);
986 }
987 } else {
988 track_editor->set_animation(Ref<Animation>(), true);
989 track_editor->set_root(nullptr);
990 }
991 }
992
993 void AnimationPlayerEditor::_scale_changed(const String &p_scale) {
994 player->set_speed_scale(p_scale.to_float());
995 }
996
997 void AnimationPlayerEditor::_update_animation() {
998 // the purpose of _update_animation is to reflect the current state
999 // of the animation player in the current editor..
1000
1001 updating = true;
1002
1003 if (player->is_playing()) {
1004 stop->set_button_icon(pause_icon);
1005 } else {
1006 stop->set_button_icon(stop_icon);
1007 }
1008
1009 scale->set_text(String::num(player->get_speed_scale(), 2));
1010 String current = player->get_assigned_animation();
1011
1012 for (int i = 0; i < animation->get_item_count(); i++) {
1013 if (animation->get_item_text(i) == current) {
1014 animation->select(i);
1015 break;
1016 }
1017 }
1018
1019 updating = false;
1020 }
1021
1022 void AnimationPlayerEditor::_update_player() {
1023 updating = true;
1024
1025 animation->clear();
1026
1027 tool_anim->set_disabled(player == nullptr);
1028 pin->set_disabled(player == nullptr);
1029 _set_controls_disabled(player == nullptr);
1030
1031 if (!player) {
1032 AnimationPlayerEditor::get_singleton()->get_track_editor()->update_keying();
1033 return;
1034 }
1035
1036 List<StringName> libraries;
1037 player->get_animation_library_list(&libraries);
1038
1039 int active_idx = -1;
1040 bool no_anims_found = true;
1041 bool global_animation_library_is_readonly = false;
1042 bool all_animation_libraries_are_readonly = libraries.size() > 0;
1043
1044 for (const StringName &K : libraries) {
1045 if (K != StringName()) {
1046 animation->add_separator(K);
1047 }
1048
1049 // Check if the global library is read-only since we want to disable options for adding/remove/renaming animations if it is.
1050 Ref<AnimationLibrary> anim_library = player->get_animation_library(K);
1051 bool is_animation_library_read_only = EditorNode::get_singleton()->is_resource_read_only(anim_library);
1052 if (!is_animation_library_read_only) {
1053 all_animation_libraries_are_readonly = false;
1054 } else {
1055 if (K == "") {
1056 global_animation_library_is_readonly = true;
1057 }
1058 }
1059
1060 List<StringName> animlist;
1061 anim_library->get_animation_list(&animlist);
1062
1063 for (const StringName &E : animlist) {
1064 String path = K;
1065 if (path != "") {
1066 path += "/";
1067 }
1068 path += E;
1069 animation->add_item(path);
1070 if (player->get_assigned_animation() == path) {
1071 active_idx = animation->get_selectable_item(true);
1072 }
1073 no_anims_found = false;
1074 }
1075 }
1076 #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), all_animation_libraries_are_readonly || (no_anims_found && global_animation_library_is_readonly))
1077 ITEM_CHECK_DISABLED(TOOL_NEW_ANIM);
1078 #undef ITEM_CHECK_DISABLED
1079
1080 _update_animation_list_icons();
1081
1082 updating = false;
1083 if (active_idx != -1) {
1084 animation->select(active_idx);
1085 autoplay->set_pressed(animation->get_item_text(active_idx) == player->get_autoplay());
1086 _animation_selected(active_idx);
1087 } else if (animation->has_selectable_items()) {
1088 int item = animation->get_selectable_item();
1089 animation->select(item);
1090 autoplay->set_pressed(animation->get_item_text(item) == player->get_autoplay());
1091 _animation_selected(item);
1092 } else {
1093 _animation_selected(0);
1094 }
1095
1096 if (no_anims_found) {
1097 _set_controls_disabled(true);
1098 } else {
1099 String current = animation->get_item_text(animation->get_selected());
1100 Ref<Animation> anim = player->get_animation(current);
1101
1102 bool animation_library_is_readonly = EditorNode::get_singleton()->is_resource_read_only(anim);
1103
1104 track_editor->set_animation(anim, animation_library_is_readonly);
1105 Node *root = player->get_node_or_null(player->get_root_node());
1106 if (root) {
1107 track_editor->set_root(root);
1108 }
1109 }
1110
1111 _update_animation();
1112 }
1113
1114 void AnimationPlayerEditor::_set_controls_disabled(bool p_disabled) {
1115 frame->set_editable(!p_disabled);
1116
1117 stop->set_disabled(p_disabled);
1118 play->set_disabled(p_disabled);
1119 play_bw->set_disabled(p_disabled);
1120 play_bw_from->set_disabled(p_disabled);
1121 play_from->set_disabled(p_disabled);
1122 animation->set_disabled(p_disabled);
1123 autoplay->set_disabled(p_disabled);
1124 onion_toggle->set_disabled(p_disabled);
1125 onion_skinning->set_disabled(p_disabled);
1126 }
1127
1128 void AnimationPlayerEditor::_update_animation_list_icons() {
1129 for (int i = 0; i < animation->get_item_count(); i++) {
1130 String anim_name = animation->get_item_text(i);
1131 if (animation->is_item_disabled(i) || animation->is_item_separator(i)) {
1132 continue;
1133 }
1134
1135 Ref<Texture2D> icon;
1136 if (anim_name == player->get_autoplay()) {
1137 if (anim_name == SceneStringName(RESET)) {
1138 icon = autoplay_reset_icon;
1139 } else {
1140 icon = autoplay_icon;
1141 }
1142 } else if (anim_name == SceneStringName(RESET)) {
1143 icon = reset_icon;
1144 }
1145
1146 animation->set_item_icon(i, icon);
1147 }
1148 }
1149
1150 void AnimationPlayerEditor::_update_name_dialog_library_dropdown() {
1151 StringName current_library_name;
1152 if (animation->has_selectable_items()) {
1153 String current_animation_name = animation->get_item_text(animation->get_selected());
1154 Ref<Animation> current_animation = player->get_animation(current_animation_name);
1155 if (current_animation.is_valid()) {
1156 current_library_name = player->find_animation_library(current_animation);
1157 }
1158 }
1159
1160 List<StringName> libraries;
1161 player->get_animation_library_list(&libraries);
1162 library->clear();
1163
1164 int valid_library_count = 0;
1165
1166 // When [Global] isn't present, but other libraries are, add option of creating [Global].
1167 int index_offset = 0;
1168 if (!player->has_animation_library(StringName())) {
1169 library->add_item(String(TTR("[Global] (create)")));
1170 library->set_item_metadata(0, "");
1171 if (!libraries.is_empty()) {
1172 index_offset = 1;
1173 }
1174 valid_library_count++;
1175 }
1176
1177 int current_lib_id = index_offset; // Don't default to [Global] if it doesn't exist yet.
1178 for (const StringName &library_name : libraries) {
1179 if (!EditorNode::get_singleton()->is_resource_read_only(player->get_animation_library(library_name))) {
1180 library->add_item((library_name == StringName()) ? String(TTR("[Global]")) : String(library_name));
1181 library->set_item_metadata(valid_library_count, String(library_name));
1182 // Default to duplicating into same library.
1183 if (library_name == current_library_name) {
1184 current_library_name = library_name;
1185 current_lib_id = valid_library_count;
1186 }
1187 valid_library_count++;
1188 }
1189 }
1190
1191 // If our library name is empty, but we have valid libraries, we can check here to auto assign the first
1192 // one which isn't a read-only library.
1193 bool auto_assigning_non_global_library = false;
1194 if (current_library_name == StringName() && valid_library_count > 0) {
1195 for (const StringName &library_name : libraries) {
1196 if (!EditorNode::get_singleton()->is_resource_read_only(player->get_animation_library(library_name))) {
1197 current_library_name = library_name;
1198 current_lib_id = 0;
1199 if (library_name != StringName()) {
1200 auto_assigning_non_global_library = true;
1201 }
1202 break;
1203 }
1204 }
1205 }
1206
1207 if (library->get_item_count() > 0) {
1208 library->select(current_lib_id);
1209 if (library->get_item_count() > 1 || auto_assigning_non_global_library) {
1210 library->show();
1211 library->set_disabled(auto_assigning_non_global_library && library->get_item_count() == 1);
1212 } else {
1213 library->hide();
1214 }
1215 }
1216 }
1217
1218 void AnimationPlayerEditor::_update_playback_tooltips() {
1219 stop->set_tooltip_text(TTR("Pause/Stop Animation") + " (" + ED_GET_SHORTCUT("animation_editor/stop_animation")->get_as_text() + ")");
1220 play->set_tooltip_text(TTR("Play Animation from Start") + " (" + ED_GET_SHORTCUT("animation_editor/play_animation_from_start")->get_as_text() + ")");
1221 play_from->set_tooltip_text(TTR("Play Animation") + " (" + ED_GET_SHORTCUT("animation_editor/play_animation")->get_as_text() + ")");
1222 play_bw_from->set_tooltip_text(TTR("Play Animation Backwards") + " (" + ED_GET_SHORTCUT("animation_editor/play_animation_backwards")->get_as_text() + ")");
1223 play_bw->set_tooltip_text(TTR("Play Animation Backwards from End") + " (" + ED_GET_SHORTCUT("animation_editor/play_animation_from_end")->get_as_text() + ")");
1224 }
1225
1226 void AnimationPlayerEditor::_ensure_dummy_player() {
1227 bool dummy_exists = is_dummy && player && original_node;
1228 if (dummy_exists) {
1229 if (is_visible()) {
1230 player->set_active(true);
1231 original_node->set_editing(true);
1232 } else {
1233 player->set_active(false);
1234 original_node->set_editing(false);
1235 }
1236 }
1237
1238 int selected = animation->get_selected();
1239 autoplay->set_disabled(selected != -1 ? (animation->get_item_text(selected).is_empty() ? true : dummy_exists) : true);
1240
1241 // Show warning.
1242 if (track_editor) {
1243 track_editor->show_dummy_player_warning(dummy_exists);
1244 }
1245 }
1246
1247 void AnimationPlayerEditor::edit(AnimationMixer *p_node, AnimationPlayer *p_player, bool p_is_dummy) {
1248 if (player && pin->is_pressed()) {
1249 return; // Ignore, pinned.
1250 }
1251
1252 if (player) {
1253 if (player->is_connected(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
1254 player->disconnect(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated));
1255 }
1256 if (player->is_connected(SceneStringName(animation_finished), callable_mp(this, &AnimationPlayerEditor::_animation_finished))) {
1257 player->disconnect(SceneStringName(animation_finished), callable_mp(this, &AnimationPlayerEditor::_animation_finished));
1258 }
1259 if (player->is_connected(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed))) {
1260 player->disconnect(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed));
1261 }
1262 }
1263
1264 AnimationTree *tree = Object::cast_to<AnimationTree>(p_node);
1265
1266 if (tree) {
1267 if (tree->is_connected(SNAME("animation_player_changed"), callable_mp(this, &AnimationPlayerEditor::unpin))) {
1268 tree->disconnect(SNAME("animation_player_changed"), callable_mp(this, &AnimationPlayerEditor::unpin));
1269 }
1270 }
1271
1272 original_node = p_node;
1273 player = p_player;
1274 is_dummy = p_is_dummy;
1275
1276 if (tree) {
1277 if (!tree->is_connected(SNAME("animation_player_changed"), callable_mp(this, &AnimationPlayerEditor::unpin))) {
1278 tree->connect(SNAME("animation_player_changed"), callable_mp(this, &AnimationPlayerEditor::unpin));
1279 }
1280 }
1281
1282 if (player) {
1283 if (!player->is_connected(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated))) {
1284 player->connect(SNAME("animation_list_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_libraries_updated), CONNECT_DEFERRED);
1285 }
1286 if (!player->is_connected(SceneStringName(animation_finished), callable_mp(this, &AnimationPlayerEditor::_animation_finished))) {
1287 player->connect(SceneStringName(animation_finished), callable_mp(this, &AnimationPlayerEditor::_animation_finished));
1288 }
1289 if (!player->is_connected(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed))) {
1290 player->connect(SNAME("current_animation_changed"), callable_mp(this, &AnimationPlayerEditor::_current_animation_changed));
1291 }
1292 _update_player();
1293
1294 if (onion.enabled) {
1295 if (animation->has_selectable_items()) {
1296 _start_onion_skinning();
1297 } else {
1298 _stop_onion_skinning();
1299 }
1300 }
1301
1302 track_editor->show_select_node_warning(false);
1303 } else {
1304 if (onion.enabled) {
1305 _stop_onion_skinning();
1306 }
1307
1308 track_editor->show_select_node_warning(true);
1309 }
1310
1311 library_editor->set_animation_mixer(fetch_mixer_for_library());
1312
1313 _ensure_dummy_player();
1314 }
1315
1316 void AnimationPlayerEditor::forward_force_draw_over_viewport(Control *p_overlay) {
1317 if (!onion.can_overlay) {
1318 return;
1319 }
1320
1321 // Can happen on viewport resize, at least.
1322 if (!_are_onion_layers_valid()) {
1323 return;
1324 }
1325
1326 RID ci = p_overlay->get_canvas_item();
1327 Rect2 src_rect = p_overlay->get_global_rect();
1328 // Re-flip since captures are already flipped.
1329 src_rect.position.y = onion.capture_size.y - (src_rect.position.y + src_rect.size.y);
1330 src_rect.size.y *= -1;
1331
1332 Rect2 dst_rect = Rect2(Point2(), p_overlay->get_size());
1333
1334 float alpha_step = 1.0 / (onion.steps + 1);
1335
1336 uint32_t capture_idx = 0;
1337 if (onion.past) {
1338 float alpha = 0.0f;
1339 do {
1340 alpha += alpha_step;
1341
1342 if (onion.captures_valid[capture_idx]) {
1343 RS::get_singleton()->canvas_item_add_texture_rect_region(
1344 ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[capture_idx]), src_rect, Color(1, 1, 1, alpha));
1345 }
1346
1347 capture_idx++;
1348 } while (capture_idx < onion.steps);
1349 }
1350 if (onion.future) {
1351 float alpha = 1.0f;
1352 uint32_t base_cidx = capture_idx;
1353 do {
1354 alpha -= alpha_step;
1355
1356 if (onion.captures_valid[capture_idx]) {
1357 RS::get_singleton()->canvas_item_add_texture_rect_region(
1358 ci, dst_rect, RS::get_singleton()->viewport_get_texture(onion.captures[capture_idx]), src_rect, Color(1, 1, 1, alpha));
1359 }
1360
1361 capture_idx++;
1362 } while (capture_idx < base_cidx + onion.steps); // In case there's the present capture at the end, skip it.
1363 }
1364 }
1365
1366 void AnimationPlayerEditor::_animation_duplicate() {
1367 if (!animation->has_selectable_items()) {
1368 return;
1369 }
1370
1371 String current = animation->get_item_text(animation->get_selected());
1372 Ref<Animation> anim = player->get_animation(current);
1373 if (anim.is_null()) {
1374 return;
1375 }
1376
1377 int count = 2;
1378 String new_name = current;
1379 PackedStringArray split = new_name.split("_");
1380 int last_index = split.size() - 1;
1381 if (last_index > 0 && split[last_index].is_valid_int() && split[last_index].to_int() >= 0) {
1382 count = split[last_index].to_int();
1383 split.remove_at(last_index);
1384 new_name = String("_").join(split);
1385 }
1386 while (true) {
1387 String attempt = new_name;
1388 attempt += vformat("_%d", count);
1389 if (player->has_animation(attempt)) {
1390 count++;
1391 continue;
1392 }
1393 new_name = attempt;
1394 break;
1395 }
1396
1397 if (new_name.contains_char('/')) {
1398 // Discard library prefix.
1399 new_name = new_name.get_slicec('/', 1);
1400 }
1401
1402 _update_name_dialog_library_dropdown();
1403
1404 name_dialog_op = TOOL_DUPLICATE_ANIM;
1405 name_dialog->set_title(TTR("Duplicate Animation"));
1406 // TRANSLATORS: This is a label for the new name field in the "Duplicate Animation" dialog.
1407 name_title->set_text(TTR("Duplicated Animation Name:"));
1408 name->set_text(new_name);
1409 name_dialog->popup_centered(Size2(300, 90));
1410 name->select_all();
1411 name->grab_focus();
1412 }
1413
1414 Ref<Animation> AnimationPlayerEditor::_animation_clone(Ref<Animation> p_anim) {
1415 Ref<Animation> new_anim = memnew(Animation);
1416 List<PropertyInfo> plist;
1417 p_anim->get_property_list(&plist);
1418
1419 for (const PropertyInfo &E : plist) {
1420 if (E.usage & PROPERTY_USAGE_STORAGE) {
1421 new_anim->set(E.name, p_anim->get(E.name));
1422 }
1423 }
1424 new_anim->set_path("");
1425
1426 return new_anim;
1427 }
1428
1429 void AnimationPlayerEditor::_seek_value_changed(float p_value, bool p_timeline_only) {
1430 if (updating || !player || player->is_playing()) {
1431 return;
1432 };
1433
1434 updating = true;
1435 StringName current = player->get_assigned_animation();
1436 if (current.is_empty() || !player->has_animation(current)) {
1437 updating = false;
1438 current = "";
1439 return;
1440 };
1441
1442 Ref<Animation> anim;
1443 anim = player->get_animation(current);
1444
1445 double pos = CLAMP((double)anim->get_length() * (p_value / frame->get_max()), 0, (double)anim->get_length());
1446 if (track_editor->is_snap_timeline_enabled()) {
1447 pos = Math::snapped(pos, _get_editor_step());
1448 }
1449 pos = CLAMP(pos, 0, (double)anim->get_length() - CMP_EPSILON2); // Hack: Avoid fposmod with LOOP_LINEAR.
1450
1451 if (!p_timeline_only && anim.is_valid() && (!player->is_valid() || !Math::is_equal_approx(pos, player->get_current_animation_position()))) {
1452 player->seek_internal(pos, true, true, false);
1453 }
1454
1455 track_editor->set_anim_pos(pos);
1456 }
1457
1458 void AnimationPlayerEditor::_animation_player_changed(Object *p_pl) {
1459 _update_player();
1460
1461 if (blend_editor.dialog->is_visible()) {
1462 _update_animation_blend(); // Update.
1463 }
1464
1465 if (library_editor->is_visible()) {
1466 library_editor->update_tree();
1467 }
1468 }
1469
1470 void AnimationPlayerEditor::_animation_libraries_updated() {
1471 _animation_player_changed(player);
1472 }
1473
1474 void AnimationPlayerEditor::_list_changed() {
1475 if (is_visible_in_tree()) {
1476 _update_player();
1477 }
1478 }
1479
1480 void AnimationPlayerEditor::_animation_finished(const String &p_name) {
1481 finishing = true;
1482 }
1483
1484 void AnimationPlayerEditor::_current_animation_changed(const StringName &p_name) {
1485 if (is_visible_in_tree()) {
1486 if (finishing) {
1487 finishing = false; // Maybe redundant since it will be false in the AnimationPlayerEditor::_process(), but for safety.
1488 return;
1489 } else if (p_name.is_empty()) {
1490 // Means [stop].
1491 frame->set_value(0);
1492 track_editor->set_anim_pos(0);
1493 _update_animation();
1494 return;
1495 }
1496 Ref<Animation> anim = player->get_animation(p_name);
1497 if (anim.is_null()) {
1498 return;
1499 }
1500
1501 // Determine the read-only status of the animation's library and the libraries as a whole.
1502 List<StringName> libraries;
1503 player->get_animation_library_list(&libraries);
1504
1505 bool current_animation_library_is_readonly = false;
1506 bool all_animation_libraries_are_readonly = true;
1507 for (const StringName &K : libraries) {
1508 Ref<AnimationLibrary> anim_library = player->get_animation_library(K);
1509 bool animation_library_is_readonly = EditorNode::get_singleton()->is_resource_read_only(anim_library);
1510 if (!animation_library_is_readonly) {
1511 all_animation_libraries_are_readonly = false;
1512 }
1513
1514 List<StringName> animlist;
1515 anim_library->get_animation_list(&animlist);
1516 bool animation_found = false;
1517 for (const StringName &E : animlist) {
1518 String path = K;
1519 if (path != "") {
1520 path += "/";
1521 }
1522 path += E;
1523 if (p_name == path) {
1524 current_animation_library_is_readonly = animation_library_is_readonly;
1525 break;
1526 }
1527 }
1528 if (animation_found) {
1529 break;
1530 }
1531 }
1532
1533 StringName library_name = player->find_animation_library(anim);
1534
1535 bool animation_is_readonly = EditorNode::get_singleton()->is_resource_read_only(anim);
1536
1537 track_editor->set_animation(anim, animation_is_readonly);
1538 _update_animation();
1539
1540 #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), false)
1541 ITEM_CHECK_DISABLED(TOOL_EDIT_TRANSITIONS);
1542 ITEM_CHECK_DISABLED(TOOL_EDIT_RESOURCE);
1543 #undef ITEM_CHECK_DISABLED
1544
1545 #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), current_animation_library_is_readonly)
1546 ITEM_CHECK_DISABLED(TOOL_RENAME_ANIM);
1547 ITEM_CHECK_DISABLED(TOOL_REMOVE_ANIM);
1548 #undef ITEM_CHECK_DISABLED
1549
1550 #define ITEM_CHECK_DISABLED(m_item) tool_anim->get_popup()->set_item_disabled(tool_anim->get_popup()->get_item_index(m_item), all_animation_libraries_are_readonly)
1551 ITEM_CHECK_DISABLED(TOOL_DUPLICATE_ANIM);
1552 #undef ITEM_CHECK_DISABLED
1553 }
1554 }
1555
1556 void AnimationPlayerEditor::_animation_key_editor_anim_len_changed(float p_len) {
1557 frame->set_max(p_len);
1558 }
1559
1560 void AnimationPlayerEditor::_animation_key_editor_seek(float p_pos, bool p_timeline_only, bool p_update_position_only) {
1561 timeline_position = p_pos;
1562
1563 if (!is_visible_in_tree() ||
1564 p_update_position_only ||
1565 !player ||
1566 player->is_playing() ||
1567 !player->has_animation(player->get_assigned_animation())) {
1568 return;
1569 }
1570
1571 updating = true;
1572 frame->set_value(track_editor->is_snap_timeline_enabled() ? Math::snapped(p_pos, _get_editor_step()) : p_pos);
1573 updating = false;
1574 _seek_value_changed(p_pos, p_timeline_only);
1575 }
1576
1577 void AnimationPlayerEditor::_animation_update_key_frame() {
1578 if (player) {
1579 player->advance(0);
1580 }
1581 }
1582
1583 void AnimationPlayerEditor::_animation_tool_menu(int p_option) {
1584 String current = _get_current();
1585
1586 Ref<Animation> anim;
1587 if (!current.is_empty()) {
1588 anim = player->get_animation(current);
1589 }
1590
1591 switch (p_option) {
1592 case TOOL_NEW_ANIM: {
1593 _animation_new();
1594 } break;
1595 case TOOL_ANIM_LIBRARY: {
1596 library_editor->set_animation_mixer(fetch_mixer_for_library());
1597 library_editor->show_dialog();
1598 } break;
1599 case TOOL_DUPLICATE_ANIM: {
1600 _animation_duplicate();
1601 } break;
1602 case TOOL_RENAME_ANIM: {
1603 _animation_rename();
1604 } break;
1605 case TOOL_EDIT_TRANSITIONS: {
1606 _edit_animation_blend();
1607 } break;
1608 case TOOL_REMOVE_ANIM: {
1609 _animation_remove();
1610 } break;
1611 case TOOL_EDIT_RESOURCE: {
1612 if (anim.is_valid()) {
1613 EditorNode::get_singleton()->edit_resource(anim);
1614 }
1615 } break;
1616 }
1617 }
1618
1619 void AnimationPlayerEditor::_onion_skinning_menu(int p_option) {
1620 PopupMenu *menu = onion_skinning->get_popup();
1621 int idx = menu->get_item_index(p_option);
1622
1623 switch (p_option) {
1624 case ONION_SKINNING_ENABLE: {
1625 onion.enabled = !onion.enabled;
1626
1627 if (onion.enabled) {
1628 if (get_player() && !get_player()->has_animation(SceneStringName(RESET))) {
1629 EditorNode::get_singleton()->show_warning(TTR("Onion skinning requires a RESET animation."));
1630 }
1631 _start_onion_skinning(); // It will check for RESET animation anyway.
1632 } else {
1633 _stop_onion_skinning();
1634 }
1635
1636 } break;
1637 case ONION_SKINNING_PAST: {
1638 // Ensure at least one of past/future is checked.
1639 onion.past = onion.future ? !onion.past : true;
1640 menu->set_item_checked(idx, onion.past);
1641 } break;
1642 case ONION_SKINNING_FUTURE: {
1643 // Ensure at least one of past/future is checked.
1644 onion.future = onion.past ? !onion.future : true;
1645 menu->set_item_checked(idx, onion.future);
1646 } break;
1647 case ONION_SKINNING_1_STEP: // Fall-through.
1648 case ONION_SKINNING_2_STEPS:
1649 case ONION_SKINNING_3_STEPS: {
1650 onion.steps = (p_option - ONION_SKINNING_1_STEP) + 1;
1651 int one_frame_idx = menu->get_item_index(ONION_SKINNING_1_STEP);
1652 for (int i = 0; i <= ONION_SKINNING_LAST_STEPS_OPTION - ONION_SKINNING_1_STEP; i++) {
1653 menu->set_item_checked(one_frame_idx + i, (int)onion.steps == i + 1);
1654 }
1655 } break;
1656 case ONION_SKINNING_DIFFERENCES_ONLY: {
1657 onion.differences_only = !onion.differences_only;
1658 menu->set_item_checked(idx, onion.differences_only);
1659 } break;
1660 case ONION_SKINNING_FORCE_WHITE_MODULATE: {
1661 onion.force_white_modulate = !onion.force_white_modulate;
1662 menu->set_item_checked(idx, onion.force_white_modulate);
1663 } break;
1664 case ONION_SKINNING_INCLUDE_GIZMOS: {
1665 onion.include_gizmos = !onion.include_gizmos;
1666 menu->set_item_checked(idx, onion.include_gizmos);
1667 } break;
1668 }
1669 }
1670
1671 void AnimationPlayerEditor::shortcut_input(const Ref<InputEvent> &p_ev) {
1672 ERR_FAIL_COND(p_ev.is_null());
1673
1674 Ref<InputEventKey> k = p_ev;
1675 if (is_visible_in_tree() && k.is_valid() && k->is_pressed() && !k->is_echo()) {
1676 if (ED_IS_SHORTCUT("animation_editor/stop_animation", p_ev)) {
1677 _stop_pressed();
1678 accept_event();
1679 } else if (ED_IS_SHORTCUT("animation_editor/play_animation", p_ev)) {
1680 _play_from_pressed();
1681 accept_event();
1682 } else if (ED_IS_SHORTCUT("animation_editor/play_animation_backwards", p_ev)) {
1683 _play_bw_from_pressed();
1684 accept_event();
1685 } else if (ED_IS_SHORTCUT("animation_editor/play_animation_from_start", p_ev)) {
1686 _play_pressed();
1687 accept_event();
1688 } else if (ED_IS_SHORTCUT("animation_editor/play_animation_from_end", p_ev)) {
1689 _play_bw_pressed();
1690 accept_event();
1691 } else if (ED_IS_SHORTCUT("animation_editor/go_to_next_keyframe", p_ev)) {
1692 go_to_nearest_keyframe(false);
1693 accept_event();
1694 } else if (ED_IS_SHORTCUT("animation_editor/go_to_previous_keyframe", p_ev)) {
1695 go_to_nearest_keyframe(true);
1696 accept_event();
1697 }
1698 }
1699 }
1700
1701 void AnimationPlayerEditor::_editor_visibility_changed() {
1702 if (is_visible() && animation->has_selectable_items()) {
1703 _start_onion_skinning();
1704 }
1705 }
1706
1707 bool AnimationPlayerEditor::_are_onion_layers_valid() {
1708 ERR_FAIL_COND_V(!onion.past && !onion.future, false);
1709
1710 Size2 capture_size = DisplayServer::get_singleton()->window_get_size(DisplayServer::MAIN_WINDOW_ID);
1711 return onion.captures.size() == onion.get_capture_count() && onion.capture_size == capture_size;
1712 }
1713
1714 void AnimationPlayerEditor::_allocate_onion_layers() {
1715 _free_onion_layers();
1716
1717 int captures = onion.get_capture_count();
1718 Size2 capture_size = DisplayServer::get_singleton()->window_get_size(DisplayServer::MAIN_WINDOW_ID);
1719
1720 onion.captures.resize(captures);
1721 onion.captures_valid.resize(captures);
1722
1723 for (int i = 0; i < captures; i++) {
1724 bool is_present = onion.differences_only && i == captures - 1;
1725
1726 // Each capture is a viewport with a canvas item attached that renders a full-size rect with the contents of the main viewport.
1727 onion.captures[i] = RS::get_singleton()->viewport_create();
1728
1729 RS::get_singleton()->viewport_set_size(onion.captures[i], capture_size.width, capture_size.height);
1730 RS::get_singleton()->viewport_set_update_mode(onion.captures[i], RS::VIEWPORT_UPDATE_ALWAYS);
1731 RS::get_singleton()->viewport_set_transparent_background(onion.captures[i], !is_present);
1732 RS::get_singleton()->viewport_attach_canvas(onion.captures[i], onion.capture.canvas);
1733 }
1734
1735 // Reset the capture canvas item to the current root viewport texture (defensive).
1736 RS::get_singleton()->canvas_item_clear(onion.capture.canvas_item);
1737 RS::get_singleton()->canvas_item_add_texture_rect(onion.capture.canvas_item, Rect2(Point2(), Point2(capture_size.x, -capture_size.y)), get_tree()->get_root()->get_texture()->get_rid());
1738
1739 onion.capture_size = capture_size;
1740 }
1741
1742 void AnimationPlayerEditor::_free_onion_layers() {
1743 for (uint32_t i = 0; i < onion.captures.size(); i++) {
1744 if (onion.captures[i].is_valid()) {
1745 RS::get_singleton()->free_rid(onion.captures[i]);
1746 }
1747 }
1748 onion.captures.clear();
1749 onion.captures_valid.clear();
1750 }
1751
1752 void AnimationPlayerEditor::_prepare_onion_layers_1() {
1753 // This would be called per viewport and we want to act once only.
1754 int64_t cur_frame = get_tree()->get_frame();
1755 if (cur_frame == onion.last_frame) {
1756 return;
1757 }
1758
1759 if (!onion.enabled || !is_visible() || !get_player() || !get_player()->has_animation(SceneStringName(RESET))) {
1760 _stop_onion_skinning();
1761 return;
1762 }
1763
1764 onion.last_frame = cur_frame;
1765
1766 // Refresh viewports with no onion layers overlaid.
1767 onion.can_overlay = false;
1768 plugin->update_overlays();
1769
1770 if (player->is_playing()) {
1771 return;
1772 }
1773
1774 // And go to next step afterwards.
1775 callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_2_prolog).call_deferred();
1776 }
1777
1778 void AnimationPlayerEditor::_prepare_onion_layers_2_prolog() {
1779 Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
1780 if (anim.is_null()) {
1781 return;
1782 }
1783
1784 if (!_are_onion_layers_valid()) {
1785 _allocate_onion_layers();
1786 }
1787
1788 // Hide superfluous elements that would make the overlay unnecessary cluttered.
1789 if (Node3DEditor::get_singleton()->is_visible()) {
1790 // 3D
1791 onion.temp.spatial_edit_state = Node3DEditor::get_singleton()->get_state();
1792 Dictionary new_state = onion.temp.spatial_edit_state.duplicate();
1793 new_state["show_grid"] = false;
1794 new_state["show_origin"] = false;
1795 Array orig_vp = onion.temp.spatial_edit_state["viewports"];
1796 Array vp;
1797 vp.resize(4);
1798 for (int i = 0; i < vp.size(); i++) {
1799 Dictionary d = ((Dictionary)orig_vp[i]).duplicate();
1800 d["use_environment"] = false;
1801 d["doppler"] = false;
1802 d["listener"] = false;
1803 d["gizmos"] = onion.include_gizmos ? d["gizmos"] : Variant(false);
1804 d["information"] = false;
1805 vp[i] = d;
1806 }
1807 new_state["viewports"] = vp;
1808 // TODO: Save/restore only affected entries.
1809 Node3DEditor::get_singleton()->set_state(new_state);
1810 } else {
1811 // CanvasItemEditor.
1812 onion.temp.canvas_edit_state = CanvasItemEditor::get_singleton()->get_state();
1813 Dictionary new_state = onion.temp.canvas_edit_state.duplicate();
1814 new_state["show_origin"] = false;
1815 new_state["show_grid"] = false;
1816 new_state["show_rulers"] = false;
1817 new_state["show_guides"] = false;
1818 new_state["show_helpers"] = false;
1819 new_state["show_zoom_control"] = false;
1820 new_state["show_edit_locks"] = false;
1821 new_state["grid_visibility"] = 2; // TODO: Expose CanvasItemEditor::GRID_VISIBILITY_HIDE somehow and use it.
1822 new_state["show_transformation_gizmos"] = onion.include_gizmos ? new_state["gizmos"] : Variant(false);
1823 // TODO: Save/restore only affected entries.
1824 CanvasItemEditor::get_singleton()->set_state(new_state);
1825 }
1826
1827 // Tweak the root viewport to ensure it's rendered before our target.
1828 RID root_vp = get_tree()->get_root()->get_viewport_rid();
1829 onion.temp.screen_rect = Rect2(Vector2(), DisplayServer::get_singleton()->window_get_size(DisplayServer::MAIN_WINDOW_ID));
1830 RS::get_singleton()->viewport_attach_to_screen(root_vp, Rect2(), DisplayServer::INVALID_WINDOW_ID);
1831 RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_ALWAYS);
1832
1833 RID present_rid;
1834 if (onion.differences_only) {
1835 // Capture present scene as it is.
1836 RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, RID());
1837 present_rid = onion.captures[onion.captures.size() - 1];
1838 RS::get_singleton()->viewport_set_active(present_rid, true);
1839 RS::get_singleton()->viewport_set_parent_viewport(root_vp, present_rid);
1840 RS::get_singleton()->draw(false);
1841 RS::get_singleton()->viewport_set_active(present_rid, false);
1842 }
1843
1844 // Backup current animation state.
1845 onion.temp.anim_values_backup = player->make_backup();
1846 onion.temp.anim_player_position = player->get_current_animation_position();
1847
1848 // Render every past/future step with the capture shader.
1849
1850 RS::get_singleton()->canvas_item_set_material(onion.capture.canvas_item, onion.capture.material->get_rid());
1851 onion.capture.material->set_shader_parameter("bkg_color", GLOBAL_GET("rendering/environment/defaults/default_clear_color"));
1852 onion.capture.material->set_shader_parameter("differences_only", onion.differences_only);
1853 onion.capture.material->set_shader_parameter("present", onion.differences_only ? RS::get_singleton()->viewport_get_texture(present_rid) : RID());
1854 onion.capture.material->set_shader_parameter("dir_color", onion.force_white_modulate ? Color(1, 1, 1) : Color(EDITOR_GET("editors/animation/onion_layers_past_color")));
1855
1856 uint32_t p_capture_idx = 0;
1857 int first_step_offset = onion.past ? -(int)onion.steps : 0;
1858 _prepare_onion_layers_2_step_prepare(first_step_offset, p_capture_idx);
1859 }
1860
1861 void AnimationPlayerEditor::_prepare_onion_layers_2_step_prepare(int p_step_offset, uint32_t p_capture_idx) {
1862 uint32_t next_capture_idx = p_capture_idx;
1863 if (p_step_offset == 0) {
1864 // Skip present step and switch to the color of future.
1865 if (!onion.force_white_modulate) {
1866 onion.capture.material->set_shader_parameter("dir_color", EDITOR_GET("editors/animation/onion_layers_future_color"));
1867 }
1868 } else {
1869 Ref<Animation> anim = player->get_animation(player->get_assigned_animation());
1870 double pos = onion.temp.anim_player_position + p_step_offset * anim->get_step();
1871
1872 bool valid = anim->get_loop_mode() != Animation::LOOP_NONE || (pos >= 0 && pos <= anim->get_length());
1873 onion.captures_valid[p_capture_idx] = valid;
1874 if (valid) {
1875 player->seek_internal(pos, true, true, false);
1876 OS::get_singleton()->get_main_loop()->process(0);
1877 // This is the key: process the frame and let all callbacks/updates/notifications happen
1878 // so everything (transforms, skeletons, etc.) is up-to-date visually.
1879 callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_2_step_capture).call_deferred(p_step_offset, p_capture_idx);
1880 return;
1881 } else {
1882 next_capture_idx++;
1883 }
1884 }
1885
1886 int last_step_offset = onion.future ? onion.steps : 0;
1887 if (p_step_offset < last_step_offset) {
1888 _prepare_onion_layers_2_step_prepare(p_step_offset + 1, next_capture_idx);
1889 } else {
1890 _prepare_onion_layers_2_epilog();
1891 }
1892 }
1893
1894 void AnimationPlayerEditor::_prepare_onion_layers_2_step_capture(int p_step_offset, uint32_t p_capture_idx) {
1895 DEV_ASSERT(p_step_offset != 0);
1896 DEV_ASSERT(onion.captures_valid[p_capture_idx]);
1897
1898 RID root_vp = get_tree()->get_root()->get_viewport_rid();
1899 RS::get_singleton()->viewport_set_active(onion.captures[p_capture_idx], true);
1900 RS::get_singleton()->viewport_set_parent_viewport(root_vp, onion.captures[p_capture_idx]);
1901 RS::get_singleton()->draw(false);
1902 RS::get_singleton()->viewport_set_active(onion.captures[p_capture_idx], false);
1903
1904 int last_step_offset = onion.future ? onion.steps : 0;
1905 if (p_step_offset < last_step_offset) {
1906 _prepare_onion_layers_2_step_prepare(p_step_offset + 1, p_capture_idx + 1);
1907 } else {
1908 _prepare_onion_layers_2_epilog();
1909 }
1910 }
1911
1912 void AnimationPlayerEditor::_prepare_onion_layers_2_epilog() {
1913 // Restore root viewport.
1914 RID root_vp = get_tree()->get_root()->get_viewport_rid();
1915 RS::get_singleton()->viewport_set_parent_viewport(root_vp, RID());
1916 RS::get_singleton()->viewport_attach_to_screen(root_vp, onion.temp.screen_rect, DisplayServer::MAIN_WINDOW_ID);
1917 RS::get_singleton()->viewport_set_update_mode(root_vp, RS::VIEWPORT_UPDATE_WHEN_VISIBLE);
1918
1919 // Restore animation state.
1920 // Here we're combine the power of seeking back to the original position and
1921 // restoring the values backup. In most cases they will bring the same value back,
1922 // but there are cases handled by one that the other can't.
1923 // Namely:
1924 // - Seeking won't restore any values that may have been modified by the user
1925 // in the node after the last time the AnimationPlayer updated it.
1926 // - Restoring the backup won't account for values that are not directly involved
1927 // in the animation but a consequence of them (e.g., SkeletonModification2DLookAt).
1928 // FIXME: Since backup of values is based on the reset animation, only values
1929 // backed by a proper reset animation will work correctly with onion
1930 // skinning and the possibility to restore the values mentioned in the
1931 // first point above is gone. Still good enough.
1932 player->seek_internal(onion.temp.anim_player_position, true, true, false);
1933 player->restore(onion.temp.anim_values_backup);
1934
1935 // Restore state of main editors.
1936 if (Node3DEditor::get_singleton()->is_visible()) {
1937 // 3D
1938 Node3DEditor::get_singleton()->set_state(onion.temp.spatial_edit_state);
1939 } else { // CanvasItemEditor
1940 // 2D
1941 CanvasItemEditor::get_singleton()->set_state(onion.temp.canvas_edit_state);
1942 }
1943
1944 // Update viewports with skin layers overlaid for the actual engine loop render.
1945 onion.can_overlay = true;
1946 plugin->update_overlays();
1947 }
1948
1949 void AnimationPlayerEditor::_start_onion_skinning() {
1950 if (get_player() && !get_player()->has_animation(SceneStringName(RESET))) {
1951 onion.enabled = false;
1952 onion_toggle->set_pressed_no_signal(false);
1953 return;
1954 }
1955 if (!get_tree()->is_connected(SNAME("process_frame"), callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1))) {
1956 get_tree()->connect(SNAME("process_frame"), callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1));
1957 }
1958 }
1959
1960 void AnimationPlayerEditor::_stop_onion_skinning() {
1961 if (get_tree()->is_connected(SNAME("process_frame"), callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1))) {
1962 get_tree()->disconnect(SNAME("process_frame"), callable_mp(this, &AnimationPlayerEditor::_prepare_onion_layers_1));
1963
1964 _free_onion_layers();
1965
1966 // Clean up.
1967 onion.can_overlay = false;
1968 plugin->update_overlays();
1969 onion.temp = {};
1970 }
1971 }
1972
1973 void AnimationPlayerEditor::_pin_pressed() {
1974 SceneTreeDock::get_singleton()->get_tree_editor()->update_tree();
1975 }
1976
1977 AnimationMixer *AnimationPlayerEditor::fetch_mixer_for_library() const {
1978 if (!original_node) {
1979 return nullptr;
1980 }
1981 // Does AnimationTree have AnimationPlayer?
1982 if (original_node->is_class("AnimationTree")) {
1983 AnimationTree *src_tree = Object::cast_to<AnimationTree>(original_node);
1984 Node *src_player = src_tree->get_node_or_null(src_tree->get_animation_player());
1985 if (src_player) {
1986 return Object::cast_to<AnimationMixer>(src_player);
1987 }
1988 }
1989 return original_node;
1990 }
1991
1992 Node *AnimationPlayerEditor::get_cached_root_node() const {
1993 return ObjectDB::get_instance<Node>(cached_root_node_id);
1994 }
1995
1996 bool AnimationPlayerEditor::_validate_tracks(const Ref<Animation> p_anim) {
1997 bool is_valid = true;
1998 if (p_anim.is_null()) {
1999 return true; // There is a problem outside of the animation track.
2000 }
2001 int len = p_anim->get_track_count();
2002 for (int i = 0; i < len; i++) {
2003 Animation::TrackType ttype = p_anim->track_get_type(i);
2004 if (ttype == Animation::TYPE_ROTATION_3D) {
2005 int key_len = p_anim->track_get_key_count(i);
2006 for (int j = 0; j < key_len; j++) {
2007 Quaternion q;
2008 p_anim->rotation_track_get_key(i, j, &q);
2009 ERR_BREAK_EDMSG(!q.is_normalized(), "AnimationPlayer: '" + player->get_name() + "', Animation: '" + player->get_current_animation() + "', 3D Rotation Track: '" + String(p_anim->track_get_path(i)) + "' contains unnormalized Quaternion key.");
2010 }
2011 } else if (ttype == Animation::TYPE_VALUE) {
2012 int key_len = p_anim->track_get_key_count(i);
2013 if (key_len == 0) {
2014 continue;
2015 }
2016 switch (p_anim->track_get_key_value(i, 0).get_type()) {
2017 case Variant::QUATERNION: {
2018 for (int j = 0; j < key_len; j++) {
2019 Quaternion q = Quaternion(p_anim->track_get_key_value(i, j));
2020 if (!q.is_normalized()) {
2021 is_valid = false;
2022 ERR_BREAK_EDMSG(true, "AnimationPlayer: '" + player->get_name() + "', Animation: '" + player->get_current_animation() + "', Value Track: '" + String(p_anim->track_get_path(i)) + "' contains unnormalized Quaternion key.");
2023 }
2024 }
2025 } break;
2026 case Variant::TRANSFORM3D: {
2027 for (int j = 0; j < key_len; j++) {
2028 Transform3D t = Transform3D(p_anim->track_get_key_value(i, j));
2029 if (!t.basis.orthonormalized().is_rotation()) {
2030 is_valid = false;
2031 ERR_BREAK_EDMSG(true, "AnimationPlayer: '" + player->get_name() + "', Animation: '" + player->get_current_animation() + "', Value Track: '" + String(p_anim->track_get_path(i)) + "' contains corrupted basis (some axes are too close other axis or scaled by zero) Transform3D key.");
2032 }
2033 }
2034 } break;
2035 default: {
2036 } break;
2037 }
2038 }
2039 }
2040 return is_valid;
2041 }
2042
2043 void AnimationPlayerEditor::_bind_methods() {
2044 // Needed for UndoRedo.
2045 ClassDB::bind_method(D_METHOD("_animation_player_changed"), &AnimationPlayerEditor::_animation_player_changed);
2046 ClassDB::bind_method(D_METHOD("_animation_update_key_frame"), &AnimationPlayerEditor::_animation_update_key_frame);
2047 ClassDB::bind_method(D_METHOD("_start_onion_skinning"), &AnimationPlayerEditor::_start_onion_skinning);
2048 ClassDB::bind_method(D_METHOD("_stop_onion_skinning"), &AnimationPlayerEditor::_stop_onion_skinning);
2049
2050 ADD_SIGNAL(MethodInfo("animation_selected", PropertyInfo(Variant::STRING, "name")));
2051 }
2052
2053 AnimationPlayerEditor *AnimationPlayerEditor::singleton = nullptr;
2054
2055 AnimationPlayer *AnimationPlayerEditor::get_player() const {
2056 return player;
2057 }
2058
2059 AnimationMixer *AnimationPlayerEditor::get_editing_node() const {
2060 return original_node;
2061 }
2062
2063 AnimationPlayerEditor::AnimationPlayerEditor(AnimationPlayerEditorPlugin *p_plugin) {
2064 plugin = p_plugin;
2065 singleton = this;
2066
2067 set_name(TTRC("Animation"));
2068 set_icon_name("Animation");
2069 set_dock_shortcut(ED_SHORTCUT_AND_COMMAND("bottom_panels/toggle_animation_bottom_panel", TTRC("Toggle Animation Dock"), KeyModifierMask::ALT | Key::N));
2070 set_default_slot(EditorDock::DOCK_SLOT_BOTTOM);
2071 set_available_layouts(EditorDock::DOCK_LAYOUT_HORIZONTAL | EditorDock::DOCK_LAYOUT_FLOATING);
2072
2073 set_focus_mode(FOCUS_ALL);
2074 set_process_shortcut_input(true);
2075
2076 VBoxContainer *main_vbox_container = memnew(VBoxContainer);
2077 add_child(main_vbox_container);
2078 HBoxContainer *hb = memnew(HBoxContainer);
2079 main_vbox_container->add_child(hb);
2080
2081 HBoxContainer *playback_container = memnew(HBoxContainer);
2082 playback_container->set_layout_direction(LAYOUT_DIRECTION_LTR);
2083 hb->add_child(playback_container);
2084
2085 play_bw_from = memnew(Button);
2086 play_bw_from->set_theme_type_variation(SceneStringName(FlatButton));
2087 playback_container->add_child(play_bw_from);
2088
2089 play_bw = memnew(Button);
2090 play_bw->set_theme_type_variation(SceneStringName(FlatButton));
2091 playback_container->add_child(play_bw);
2092
2093 stop = memnew(Button);
2094 stop->set_theme_type_variation(SceneStringName(FlatButton));
2095 playback_container->add_child(stop);
2096
2097 play = memnew(Button);
2098 play->set_theme_type_variation(SceneStringName(FlatButton));
2099 playback_container->add_child(play);
2100
2101 play_from = memnew(Button);
2102 play_from->set_theme_type_variation(SceneStringName(FlatButton));
2103 playback_container->add_child(play_from);
2104
2105 frame = memnew(SpinBox);
2106 hb->add_child(frame);
2107 frame->set_custom_minimum_size(Size2(80, 0) * EDSCALE);
2108 frame->set_stretch_ratio(2);
2109 frame->set_step(0.0001);
2110 frame->set_tooltip_text(TTR("Animation position (in seconds)."));
2111
2112 hb->add_child(memnew(VSeparator));
2113
2114 scale = memnew(LineEdit);
2115 hb->add_child(scale);
2116 scale->set_h_size_flags(SIZE_EXPAND_FILL);
2117 scale->set_stretch_ratio(1);
2118 scale->set_tooltip_text(TTR("Scale animation playback globally for the node."));
2119 scale->hide();
2120
2121 delete_dialog = memnew(ConfirmationDialog);
2122 add_child(delete_dialog);
2123 delete_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationPlayerEditor::_animation_remove_confirmed));
2124
2125 tool_anim = memnew(MenuButton);
2126 tool_anim->set_shortcut_context(this);
2127 tool_anim->set_flat(false);
2128 tool_anim->set_tooltip_text(TTR("Animation Tools"));
2129 tool_anim->set_text(TTR("Animation"));
2130 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/new_animation", TTRC("New...")), TOOL_NEW_ANIM);
2131 tool_anim->get_popup()->add_separator();
2132 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/animation_libraries", TTRC("Manage Animations...")), TOOL_ANIM_LIBRARY);
2133 tool_anim->get_popup()->add_separator();
2134 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/duplicate_animation", TTRC("Duplicate...")), TOOL_DUPLICATE_ANIM);
2135 tool_anim->get_popup()->add_separator();
2136 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/rename_animation", TTRC("Rename...")), TOOL_RENAME_ANIM);
2137 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/edit_transitions", TTRC("Edit Transitions...")), TOOL_EDIT_TRANSITIONS);
2138 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/open_animation_in_inspector", TTRC("Open in Inspector")), TOOL_EDIT_RESOURCE);
2139 tool_anim->get_popup()->add_separator();
2140 tool_anim->get_popup()->add_shortcut(ED_SHORTCUT("animation_player_editor/remove_animation", TTRC("Remove")), TOOL_REMOVE_ANIM);
2141 tool_anim->set_disabled(true);
2142 tool_anim->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationPlayerEditor::_animation_tool_menu));
2143 hb->add_child(tool_anim);
2144
2145 animation = memnew(OptionButton);
2146 hb->add_child(animation);
2147 animation->set_accessibility_name(TTRC("Animation"));
2148 animation->set_h_size_flags(SIZE_EXPAND_FILL);
2149 animation->set_tooltip_text(TTR("Display list of animations in player."));
2150 animation->set_clip_text(true);
2151 animation->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2152
2153 autoplay = memnew(Button);
2154 autoplay->set_theme_type_variation(SceneStringName(FlatButton));
2155 hb->add_child(autoplay);
2156 autoplay->set_tooltip_text(TTR("Autoplay on Load"));
2157
2158 hb->add_child(memnew(VSeparator));
2159
2160 track_editor = memnew(AnimationTrackEditor);
2161 hb->add_child(track_editor->get_edit_menu());
2162
2163 hb->add_child(memnew(VSeparator));
2164
2165 onion_toggle = memnew(Button);
2166 onion_toggle->set_theme_type_variation(SceneStringName(FlatButton));
2167 onion_toggle->set_toggle_mode(true);
2168 onion_toggle->set_tooltip_text(TTR("Enable Onion Skinning"));
2169 onion_toggle->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu).bind(ONION_SKINNING_ENABLE));
2170 hb->add_child(onion_toggle);
2171
2172 onion_skinning = memnew(MenuButton);
2173 onion_skinning->set_accessibility_name(TTRC("Onion Skinning Options"));
2174 onion_skinning->set_flat(false);
2175 onion_skinning->set_theme_type_variation("FlatMenuButton");
2176 onion_skinning->set_tooltip_text(TTR("Onion Skinning Options"));
2177 onion_skinning->get_popup()->add_separator(TTR("Directions"));
2178 // TRANSLATORS: Opposite of "Future", refers to a direction in animation onion skinning.
2179 onion_skinning->get_popup()->add_check_item(TTR("Past"), ONION_SKINNING_PAST);
2180 onion_skinning->get_popup()->set_item_checked(-1, true);
2181 // TRANSLATORS: Opposite of "Past", refers to a direction in animation onion skinning.
2182 onion_skinning->get_popup()->add_check_item(TTR("Future"), ONION_SKINNING_FUTURE);
2183 onion_skinning->get_popup()->add_separator(TTR("Depth"));
2184 onion_skinning->get_popup()->add_radio_check_item(TTR("1 step"), ONION_SKINNING_1_STEP);
2185 onion_skinning->get_popup()->set_item_checked(-1, true);
2186 onion_skinning->get_popup()->add_radio_check_item(TTR("2 steps"), ONION_SKINNING_2_STEPS);
2187 onion_skinning->get_popup()->add_radio_check_item(TTR("3 steps"), ONION_SKINNING_3_STEPS);
2188 onion_skinning->get_popup()->add_separator();
2189 onion_skinning->get_popup()->add_check_item(TTR("Differences Only"), ONION_SKINNING_DIFFERENCES_ONLY);
2190 onion_skinning->get_popup()->add_check_item(TTR("Force White Modulate"), ONION_SKINNING_FORCE_WHITE_MODULATE);
2191 onion_skinning->get_popup()->add_check_item(TTR("Include Gizmos (3D)"), ONION_SKINNING_INCLUDE_GIZMOS);
2192 onion_skinning->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationPlayerEditor::_onion_skinning_menu));
2193 hb->add_child(onion_skinning);
2194
2195 hb->add_child(memnew(VSeparator));
2196
2197 pin = memnew(Button);
2198 pin->set_theme_type_variation(SceneStringName(FlatButton));
2199 pin->set_toggle_mode(true);
2200 pin->set_tooltip_text(TTR("Pin AnimationPlayer"));
2201 hb->add_child(pin);
2202 pin->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_pin_pressed));
2203
2204 file = memnew(EditorFileDialog);
2205 add_child(file);
2206
2207 name_dialog = memnew(ConfirmationDialog);
2208 name_dialog->set_title(TTR("Create New Animation"));
2209 name_dialog->set_hide_on_ok(false);
2210 add_child(name_dialog);
2211 VBoxContainer *vb = memnew(VBoxContainer);
2212 name_dialog->add_child(vb);
2213
2214 name_title = memnew(Label(TTR("Animation Name:")));
2215 vb->add_child(name_title);
2216
2217 HBoxContainer *name_hb = memnew(HBoxContainer);
2218 name = memnew(LineEdit);
2219 name_hb->add_child(name);
2220 name->set_h_size_flags(SIZE_EXPAND_FILL);
2221 library = memnew(OptionButton);
2222 name_hb->add_child(library);
2223 library->hide();
2224 vb->add_child(name_hb);
2225 name_dialog->register_text_enter(name);
2226
2227 error_dialog = memnew(AcceptDialog);
2228 error_dialog->set_ok_button_text(TTR("Close"));
2229 error_dialog->set_title(TTR("Error!"));
2230 name_dialog->add_child(error_dialog);
2231
2232 name_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationPlayerEditor::_animation_name_edited));
2233
2234 blend_editor.dialog = memnew(AcceptDialog);
2235 blend_editor.dialog->set_title(TTR("Cross-Animation Blend Times"));
2236 blend_editor.dialog->set_ok_button_text(TTR("Close"));
2237 blend_editor.dialog->set_hide_on_ok(true);
2238 add_child(blend_editor.dialog);
2239
2240 VBoxContainer *blend_vb = memnew(VBoxContainer);
2241 blend_editor.dialog->add_child(blend_vb);
2242
2243 blend_editor.tree = memnew(Tree);
2244 blend_editor.tree->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2245 blend_editor.tree->set_hide_root(true);
2246 blend_editor.tree->set_columns(2);
2247 blend_editor.tree->set_column_expand_ratio(0, 10);
2248 blend_editor.tree->set_column_clip_content(0, true);
2249 blend_editor.tree->set_column_expand_ratio(1, 3);
2250 blend_editor.tree->set_column_clip_content(1, true);
2251 blend_vb->add_margin_child(TTR("Blend Times:"), blend_editor.tree, true);
2252 blend_editor.tree->connect(SNAME("item_edited"), callable_mp(this, &AnimationPlayerEditor::_blend_edited));
2253
2254 blend_editor.next = memnew(OptionButton);
2255 blend_editor.next->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
2256 blend_editor.next->connect(SceneStringName(item_selected), callable_mp(this, &AnimationPlayerEditor::_blend_editor_next_changed));
2257 blend_vb->add_margin_child(TTR("Next (Auto Queue):"), blend_editor.next);
2258
2259 autoplay->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_autoplay_pressed));
2260 autoplay->set_toggle_mode(true);
2261 play->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_play_pressed));
2262 play_from->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_play_from_pressed));
2263 play_bw->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_play_bw_pressed));
2264 play_bw_from->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_play_bw_from_pressed));
2265 stop->connect(SceneStringName(pressed), callable_mp(this, &AnimationPlayerEditor::_stop_pressed));
2266
2267 animation->connect(SceneStringName(item_selected), callable_mp(this, &AnimationPlayerEditor::_animation_selected));
2268
2269 frame->connect(SceneStringName(value_changed), callable_mp(this, &AnimationPlayerEditor::_seek_value_changed).bind(false));
2270 scale->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationPlayerEditor::_scale_changed));
2271
2272 main_vbox_container->add_child(track_editor);
2273 track_editor->set_v_size_flags(SIZE_EXPAND_FILL);
2274 track_editor->connect(SNAME("timeline_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_seek));
2275 track_editor->connect(SNAME("animation_len_changed"), callable_mp(this, &AnimationPlayerEditor::_animation_key_editor_anim_len_changed));
2276
2277 _update_player();
2278
2279 library_editor = memnew(AnimationLibraryEditor);
2280 add_child(library_editor);
2281 library_editor->connect(SNAME("update_editor"), callable_mp(this, &AnimationPlayerEditor::_animation_player_changed));
2282
2283 // Onion skinning.
2284
2285 track_editor->connect(SceneStringName(visibility_changed), callable_mp(this, &AnimationPlayerEditor::_editor_visibility_changed));
2286
2287 onion.capture.canvas = RS::get_singleton()->canvas_create();
2288 onion.capture.canvas_item = RS::get_singleton()->canvas_item_create();
2289 RS::get_singleton()->canvas_item_set_parent(onion.capture.canvas_item, onion.capture.canvas);
2290
2291 onion.capture.material.instantiate();
2292
2293 onion.capture.shader.instantiate();
2294 onion.capture.shader->set_code(R"(
2295 // Animation editor onion skinning shader.
2296
2297 shader_type canvas_item;
2298
2299 uniform vec4 bkg_color;
2300 uniform vec4 dir_color;
2301 uniform bool differences_only;
2302 uniform sampler2D present;
2303
2304 float zero_if_equal(vec4 a, vec4 b) {
2305 return smoothstep(0.0, 0.005, length(a.rgb - b.rgb) / sqrt(3.0));
2306 }
2307
2308 void fragment() {
2309 vec4 capture_samp = texture(TEXTURE, UV);
2310 float bkg_mask = zero_if_equal(capture_samp, bkg_color);
2311 float diff_mask = 1.0;
2312 if (differences_only) {
2313 // FIXME: If Y-flips across render target, canvas item, etc. was handled correctly,
2314 // this would not be as convoluted in the shader.
2315 vec4 capture_samp2 = texture(TEXTURE, vec2(UV.x, 1.0 - UV.y));
2316 vec4 present_samp = texture(present, vec2(UV.x, 1.0 - UV.y));
2317 diff_mask = 1.0 - zero_if_equal(present_samp, bkg_color);
2318 }
2319 COLOR = vec4(capture_samp.rgb * dir_color.rgb, bkg_mask * diff_mask);
2320 }
2321 )");
2322 RS::get_singleton()->material_set_shader(onion.capture.material->get_rid(), onion.capture.shader->get_rid());
2323
2324 ED_SHORTCUT("animation_editor/stop_animation", TTRC("Pause/Stop Animation"), Key::S, true);
2325 ED_SHORTCUT("animation_editor/play_animation", TTRC("Play Animation"), Key::D, true);
2326 ED_SHORTCUT("animation_editor/play_animation_backwards", TTRC("Play Animation Backwards"), Key::A, true);
2327 ED_SHORTCUT("animation_editor/play_animation_from_start", TTRC("Play Animation from Start"), KeyModifierMask::SHIFT + Key::D, true);
2328 ED_SHORTCUT("animation_editor/play_animation_from_end", TTRC("Play Animation Backwards from End"), KeyModifierMask::SHIFT + Key::A, true);
2329 }
2330
2331 AnimationPlayerEditor::~AnimationPlayerEditor() {
2332 _free_onion_layers();
2333 RS::get_singleton()->free_rid(onion.capture.canvas);
2334 RS::get_singleton()->free_rid(onion.capture.canvas_item);
2335 onion.capture = {};
2336 }
2337
2338 void AnimationPlayerEditorPlugin::_notification(int p_what) {
2339 switch (p_what) {
2340 case NOTIFICATION_READY: {
2341 Node3DEditor::get_singleton()->connect(SNAME("transform_key_request"), callable_mp(this, &AnimationPlayerEditorPlugin::_transform_key_request));
2342 InspectorDock::get_inspector_singleton()->connect(SNAME("property_keyed"), callable_mp(this, &AnimationPlayerEditorPlugin::_property_keyed));
2343 anim_editor->get_track_editor()->connect(SNAME("keying_changed"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_keying));
2344 InspectorDock::get_inspector_singleton()->connect(SNAME("edited_object_changed"), callable_mp(anim_editor->get_track_editor(), &AnimationTrackEditor::update_keying));
2345 set_force_draw_over_forwarding_enabled();
2346 } break;
2347 }
2348 }
2349
2350 void AnimationPlayerEditorPlugin::_property_keyed(const String &p_keyed, const Variant &p_value, bool p_advance) {
2351 AnimationTrackEditor *te = anim_editor->get_track_editor();
2352 if (!te || !te->has_keying()) {
2353 return;
2354 }
2355 te->_clear_selection();
2356 te->insert_value_key(p_keyed, p_advance);
2357 }
2358
2359 void AnimationPlayerEditorPlugin::_transform_key_request(Object *sp, const String &p_sub, const Transform3D &p_key) {
2360 if (!anim_editor->get_track_editor()->has_keying()) {
2361 return;
2362 }
2363 Node3D *s = Object::cast_to<Node3D>(sp);
2364 if (!s) {
2365 return;
2366 }
2367 anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_POSITION_3D, p_key.origin);
2368 anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_ROTATION_3D, p_key.basis.get_rotation_quaternion());
2369 anim_editor->get_track_editor()->insert_transform_key(s, p_sub, Animation::TYPE_SCALE_3D, p_key.basis.get_scale());
2370 }
2371
2372 void AnimationPlayerEditorPlugin::_update_keying() {
2373 InspectorDock::get_inspector_singleton()->set_keying(anim_editor->get_track_editor()->has_keying());
2374 }
2375
2376 void AnimationPlayerEditorPlugin::edit(Object *p_object) {
2377 if (player && anim_editor && anim_editor->is_pinned()) {
2378 return; // Ignore, pinned.
2379 }
2380
2381 player = nullptr;
2382 if (!p_object) {
2383 return;
2384 }
2385 last_mixer = p_object->get_instance_id();
2386
2387 AnimationMixer *src_node = Object::cast_to<AnimationMixer>(p_object);
2388 bool is_dummy = false;
2389 if (!p_object->is_class("AnimationPlayer")) {
2390 // If it needs dummy AnimationPlayer, assign original AnimationMixer to LibraryEditor.
2391 _update_dummy_player(src_node);
2392
2393 is_dummy = true;
2394
2395 if (!src_node->is_connected(SNAME("mixer_updated"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_dummy_player))) {
2396 src_node->connect(SNAME("mixer_updated"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_dummy_player).bind(src_node), CONNECT_DEFERRED);
2397 }
2398 if (!src_node->is_connected(SNAME("animation_libraries_updated"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_dummy_player))) {
2399 src_node->connect(SNAME("animation_libraries_updated"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_dummy_player).bind(src_node), CONNECT_DEFERRED);
2400 }
2401 } else {
2402 _clear_dummy_player();
2403 player = Object::cast_to<AnimationPlayer>(p_object);
2404 }
2405 player->set_dummy(is_dummy);
2406
2407 anim_editor->edit(src_node, player, is_dummy);
2408 }
2409
2410 void AnimationPlayerEditorPlugin::_clear_dummy_player() {
2411 if (!dummy_player) {
2412 return;
2413 }
2414 Node *parent = dummy_player->get_parent();
2415 if (parent) {
2416 callable_mp(parent, &Node::remove_child).call_deferred(dummy_player);
2417 }
2418 dummy_player->queue_free();
2419 dummy_player = nullptr;
2420 }
2421
2422 void AnimationPlayerEditorPlugin::_update_dummy_player(AnimationMixer *p_mixer) {
2423 // Check current editing object.
2424 if (p_mixer->get_instance_id() != last_mixer && p_mixer->is_connected(SNAME("mixer_updated"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_dummy_player))) {
2425 p_mixer->disconnect(SNAME("mixer_updated"), callable_mp(this, &AnimationPlayerEditorPlugin::_update_dummy_player));
2426 return;
2427 }
2428
2429 // Add dummy player to scene.
2430 if (!dummy_player) {
2431 Node *parent = p_mixer->get_parent();
2432 ERR_FAIL_NULL(parent);
2433 dummy_player = memnew(AnimationPlayer);
2434 dummy_player->set_active(false); // Inactive as default, it will be activated if the AnimationPlayerEditor visibility is changed.
2435 parent->add_child(dummy_player);
2436 }
2437 player = dummy_player;
2438
2439 // Convert AnimationTree (AnimationMixer) to AnimationPlayer.
2440 AnimationMixer *default_node = memnew(AnimationMixer);
2441 List<PropertyInfo> pinfo;
2442 default_node->get_property_list(&pinfo);
2443 for (const PropertyInfo &E : pinfo) {
2444 if (!(E.usage & PROPERTY_USAGE_STORAGE)) {
2445 continue;
2446 }
2447 if (E.name != "script" && E.name != "active" && E.name != "deterministic" && E.name != "root_motion_track") {
2448 dummy_player->set(E.name, p_mixer->get(E.name));
2449 }
2450 }
2451 memdelete(default_node);
2452
2453 // Library list is dynamically added to property list, should be copied explicitly.
2454 List<StringName> existing_libs;
2455 dummy_player->get_animation_library_list(&existing_libs);
2456 for (const StringName &K : existing_libs) {
2457 dummy_player->remove_animation_library(K);
2458 }
2459
2460 List<StringName> libraries;
2461 p_mixer->get_animation_library_list(&libraries);
2462 for (const StringName &K : libraries) {
2463 dummy_player->add_animation_library(K, p_mixer->get_animation_library(K));
2464 }
2465
2466 if (anim_editor) {
2467 anim_editor->_update_player();
2468 }
2469 }
2470
2471 bool AnimationPlayerEditorPlugin::handles(Object *p_object) const {
2472 return p_object->is_class("AnimationPlayer") || p_object->is_class("AnimationTree") || p_object->is_class("AnimationMixer");
2473 }
2474
2475 void AnimationPlayerEditorPlugin::make_visible(bool p_visible) {
2476 if (p_visible) {
2477 // if AnimationTree editor is visible, do not occupy the bottom panel
2478 if (AnimationTreeEditor::get_singleton() && AnimationTreeEditor::get_singleton()->is_visible_in_tree()) {
2479 return;
2480 }
2481 anim_editor->make_visible();
2482 anim_editor->set_process(true);
2483 anim_editor->ensure_visibility();
2484 }
2485 }
2486
2487 AnimationPlayerEditorPlugin::AnimationPlayerEditorPlugin() {
2488 anim_editor = memnew(AnimationPlayerEditor(this));
2489 EditorDockManager::get_singleton()->add_dock(anim_editor);
2490 }
2491
2492 AnimationPlayerEditorPlugin::~AnimationPlayerEditorPlugin() {
2493 }
2494
2495 // AnimationTrackKeyEditEditorPlugin
2496
2497 bool EditorInspectorPluginAnimationTrackKeyEdit::can_handle(Object *p_object) {
2498 return Object::cast_to<AnimationTrackKeyEdit>(p_object) != nullptr;
2499 }
2500
2501 void EditorInspectorPluginAnimationTrackKeyEdit::parse_begin(Object *p_object) {
2502 AnimationTrackKeyEdit *atk = Object::cast_to<AnimationTrackKeyEdit>(p_object);
2503 ERR_FAIL_NULL(atk);
2504
2505 atk_editor = memnew(AnimationTrackKeyEditEditor(atk->animation, atk->track, atk->key_ofs, atk->use_fps));
2506 add_custom_control(atk_editor);
2507 }
2508
2509 AnimationTrackKeyEditEditorPlugin::AnimationTrackKeyEditEditorPlugin() {
2510 atk_plugin = memnew(EditorInspectorPluginAnimationTrackKeyEdit);
2511 EditorInspector::add_inspector_plugin(atk_plugin);
2512 }
2513
2514 bool AnimationTrackKeyEditEditorPlugin::handles(Object *p_object) const {
2515 return p_object->is_class("AnimationTrackKeyEdit");
2516 }
2517
2518 bool EditorInspectorPluginAnimationMarkerKeyEdit::can_handle(Object *p_object) {
2519 return Object::cast_to<AnimationMarkerKeyEdit>(p_object) != nullptr;
2520 }
2521
2522 void EditorInspectorPluginAnimationMarkerKeyEdit::parse_begin(Object *p_object) {
2523 AnimationMarkerKeyEdit *amk = Object::cast_to<AnimationMarkerKeyEdit>(p_object);
2524 ERR_FAIL_NULL(amk);
2525
2526 amk_editor = memnew(AnimationMarkerKeyEditEditor(amk->animation, amk->marker_name, amk->use_fps));
2527 add_custom_control(amk_editor);
2528 }
2529
2530 AnimationMarkerKeyEditEditorPlugin::AnimationMarkerKeyEditEditorPlugin() {
2531 amk_plugin = memnew(EditorInspectorPluginAnimationMarkerKeyEdit);
2532 EditorInspector::add_inspector_plugin(amk_plugin);
2533 }
2534
2535 bool AnimationMarkerKeyEditEditorPlugin::handles(Object *p_object) const {
2536 return p_object->is_class("AnimationMarkerKeyEdit");
2537 }