Debian Codegraph @

editor/animation/animation_track_editor.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* animation_track_editor.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "animation_track_editor.h"
32
33 #include "animation_track_editor_plugins.h"
34 #include "core/config/project_settings.h"
35 #include "core/error/error_macros.h"
36 #include "core/input/input.h"
37 #include "core/string/translation_server.h"
38 #include "editor/animation/animation_bezier_editor.h"
39 #include "editor/animation/animation_player_editor_plugin.h"
40 #include "editor/docks/inspector_dock.h"
41 #include "editor/editor_node.h"
42 #include "editor/editor_string_names.h"
43 #include "editor/editor_undo_redo_manager.h"
44 #include "editor/gui/editor_spin_slider.h"
45 #include "editor/gui/editor_validation_panel.h"
46 #include "editor/inspector/multi_node_edit.h"
47 #include "editor/scene/scene_tree_editor.h"
48 #include "editor/script/script_editor_plugin.h"
49 #include "editor/settings/editor_settings.h"
50 #include "editor/themes/editor_scale.h"
51 #include "scene/3d/mesh_instance_3d.h"
52 #include "scene/animation/animation_player.h"
53 #include "scene/animation/tween.h"
54 #include "scene/gui/check_box.h"
55 #include "scene/gui/color_picker.h"
56 #include "scene/gui/flow_container.h"
57 #include "scene/gui/grid_container.h"
58 #include "scene/gui/option_button.h"
59 #include "scene/gui/panel_container.h"
60 #include "scene/gui/separator.h"
61 #include "scene/gui/slider.h"
62 #include "scene/gui/spin_box.h"
63 #include "scene/gui/texture_rect.h"
64 #include "scene/gui/view_panner.h"
65 #include "scene/main/window.h"
66 #include "servers/audio/audio_stream.h"
67
68 constexpr double FPS_DECIMAL = 1.0;
69 constexpr double SECOND_DECIMAL = 0.0001;
70
71 void AnimationTrackKeyEdit::_bind_methods() {
72 ClassDB::bind_method(D_METHOD("_update_obj"), &AnimationTrackKeyEdit::_update_obj);
73 ClassDB::bind_method(D_METHOD("_key_ofs_changed"), &AnimationTrackKeyEdit::_key_ofs_changed);
74 ClassDB::bind_method(D_METHOD("_hide_script_from_inspector"), &AnimationTrackKeyEdit::_hide_script_from_inspector);
75 ClassDB::bind_method(D_METHOD("_hide_metadata_from_inspector"), &AnimationTrackKeyEdit::_hide_metadata_from_inspector);
76 ClassDB::bind_method(D_METHOD("get_root_path"), &AnimationTrackKeyEdit::get_root_path);
77 ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &AnimationTrackKeyEdit::_dont_undo_redo);
78 ClassDB::bind_method(D_METHOD("_is_read_only"), &AnimationTrackKeyEdit::_is_read_only);
79 }
80
81 void AnimationTrackKeyEdit::_fix_node_path(Variant &value) {
82 NodePath np = value;
83
84 if (np == NodePath()) {
85 return;
86 }
87
88 Node *root = EditorNode::get_singleton()->get_tree()->get_root();
89
90 Node *np_node = root->get_node_or_null(np);
91 ERR_FAIL_NULL(np_node);
92
93 Node *edited_node = root->get_node_or_null(base);
94 ERR_FAIL_NULL(edited_node);
95
96 value = edited_node->get_path_to(np_node);
97 }
98
99 void AnimationTrackKeyEdit::_update_obj(const Ref<Animation> &p_anim) {
100 if (setting || animation != p_anim) {
101 return;
102 }
103
104 notify_change();
105 }
106
107 void AnimationTrackKeyEdit::_key_ofs_changed(const Ref<Animation> &p_anim, float from, float to) {
108 if (animation != p_anim || from != key_ofs) {
109 return;
110 }
111
112 key_ofs = to;
113
114 if (setting) {
115 return;
116 }
117
118 notify_change();
119 }
120
121 bool AnimationTrackKeyEdit::_set(const StringName &p_name, const Variant &p_value) {
122 int key = animation->track_find_key(track, key_ofs, Animation::FIND_MODE_APPROX);
123 ERR_FAIL_COND_V(key == -1, false);
124
125 String name = p_name;
126 if (name == "easing") {
127 float val = p_value;
128 float prev_val = animation->track_get_key_transition(track, key);
129 setting = true;
130
131 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
132 undo_redo->create_action(TTR("Animation Change Transition"), UndoRedo::MERGE_ENDS);
133 undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", track, key, val);
134 undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", track, key, prev_val);
135 undo_redo->add_do_method(this, "_update_obj", animation);
136 undo_redo->add_undo_method(this, "_update_obj", animation);
137 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
138 if (ape) {
139 undo_redo->add_do_method(ape, "_animation_update_key_frame");
140 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
141 }
142 undo_redo->commit_action();
143
144 setting = false;
145 return true;
146 }
147
148 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
149 switch (animation->track_get_type(track)) {
150 case Animation::TYPE_POSITION_3D:
151 case Animation::TYPE_ROTATION_3D:
152 case Animation::TYPE_SCALE_3D: {
153 if (name == "position" || name == "rotation" || name == "scale") {
154 Variant old = animation->track_get_key_value(track, key);
155 setting = true;
156 String action_name;
157 switch (animation->track_get_type(track)) {
158 case Animation::TYPE_POSITION_3D:
159 action_name = TTR("Animation Change Position3D");
160 break;
161 case Animation::TYPE_ROTATION_3D:
162 action_name = TTR("Animation Change Rotation3D");
163 break;
164 case Animation::TYPE_SCALE_3D:
165 action_name = TTR("Animation Change Scale3D");
166 break;
167 default: {
168 }
169 }
170
171 undo_redo->create_action(action_name);
172 undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, p_value);
173 undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, old);
174 undo_redo->add_do_method(this, "_update_obj", animation);
175 undo_redo->add_undo_method(this, "_update_obj", animation);
176 undo_redo->commit_action();
177
178 setting = false;
179 return true;
180 }
181
182 } break;
183 case Animation::TYPE_BLEND_SHAPE:
184 case Animation::TYPE_VALUE: {
185 if (name == "value") {
186 Variant value = p_value;
187
188 if (value.get_type() == Variant::NODE_PATH) {
189 _fix_node_path(value);
190 }
191
192 setting = true;
193
194 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
195 Variant prev = animation->track_get_key_value(track, key);
196 undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, value);
197 undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, prev);
198 undo_redo->add_do_method(this, "_update_obj", animation);
199 undo_redo->add_undo_method(this, "_update_obj", animation);
200 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
201 if (ape) {
202 undo_redo->add_do_method(ape, "_animation_update_key_frame");
203 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
204 }
205 undo_redo->commit_action();
206
207 setting = false;
208 return true;
209 }
210 } break;
211 case Animation::TYPE_METHOD: {
212 Dictionary d_old = animation->track_get_key_value(track, key);
213 Dictionary d_new = d_old.duplicate();
214
215 bool change_notify_deserved = false;
216 bool mergeable = false;
217
218 if (name == "name") {
219 d_new["method"] = p_value;
220 } else if (name == "arg_count") {
221 Vector<Variant> args = d_old["args"];
222 args.resize(p_value);
223 d_new["args"] = args;
224 change_notify_deserved = true;
225 } else if (name.begins_with("args/")) {
226 Vector<Variant> args = d_old["args"];
227 int idx = name.get_slicec('/', 1).to_int();
228 ERR_FAIL_INDEX_V(idx, args.size(), false);
229
230 String what = name.get_slicec('/', 2);
231 if (what == "type") {
232 Variant::Type t = Variant::Type(int(p_value));
233
234 if (t != args[idx].get_type()) {
235 Callable::CallError err;
236 if (Variant::can_convert_strict(args[idx].get_type(), t)) {
237 Variant old = args[idx];
238 Variant *ptrs[1] = { &old };
239 Variant::construct(t, args.write[idx], (const Variant **)ptrs, 1, err);
240 } else {
241 Variant::construct(t, args.write[idx], nullptr, 0, err);
242 }
243 change_notify_deserved = true;
244 d_new["args"] = args;
245 }
246 } else if (what == "value") {
247 Variant value = p_value;
248 if (value.get_type() == Variant::NODE_PATH) {
249 _fix_node_path(value);
250 }
251
252 args.write[idx] = value;
253 d_new["args"] = args;
254 mergeable = true;
255 }
256 }
257
258 if (mergeable) {
259 undo_redo->create_action(TTR("Animation Change Call"), UndoRedo::MERGE_ENDS);
260 } else {
261 undo_redo->create_action(TTR("Animation Change Call"));
262 }
263
264 setting = true;
265 undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, d_new);
266 undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, d_old);
267 undo_redo->add_do_method(this, "_update_obj", animation);
268 undo_redo->add_undo_method(this, "_update_obj", animation);
269 undo_redo->commit_action();
270
271 setting = false;
272 if (change_notify_deserved) {
273 notify_change();
274 }
275 return true;
276 } break;
277 case Animation::TYPE_BEZIER: {
278 if (name == "value") {
279 const Variant &value = p_value;
280
281 setting = true;
282 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
283 float prev = animation->bezier_track_get_key_value(track, key);
284 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_value", track, key, value);
285 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_value", track, key, prev);
286 undo_redo->add_do_method(this, "_update_obj", animation);
287 undo_redo->add_undo_method(this, "_update_obj", animation);
288 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
289 if (ape) {
290 undo_redo->add_do_method(ape, "_animation_update_key_frame");
291 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
292 }
293 undo_redo->commit_action();
294
295 setting = false;
296 return true;
297 }
298
299 if (name == "in_handle") {
300 const Variant &value = p_value;
301
302 setting = true;
303 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
304 Vector2 prev = animation->bezier_track_get_key_in_handle(track, key);
305 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", track, key, value);
306 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", track, key, prev);
307 undo_redo->add_do_method(this, "_update_obj", animation);
308 undo_redo->add_undo_method(this, "_update_obj", animation);
309 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
310 if (ape) {
311 undo_redo->add_do_method(ape, "_animation_update_key_frame");
312 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
313 }
314 undo_redo->commit_action();
315
316 setting = false;
317 return true;
318 }
319
320 if (name == "out_handle") {
321 const Variant &value = p_value;
322
323 setting = true;
324 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
325 Vector2 prev = animation->bezier_track_get_key_out_handle(track, key);
326 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", track, key, value);
327 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", track, key, prev);
328 undo_redo->add_do_method(this, "_update_obj", animation);
329 undo_redo->add_undo_method(this, "_update_obj", animation);
330 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
331 if (ape) {
332 undo_redo->add_do_method(ape, "_animation_update_key_frame");
333 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
334 }
335 undo_redo->commit_action();
336
337 setting = false;
338 return true;
339 }
340
341 if (name == "handle_mode") {
342 const Variant &value = p_value;
343
344 setting = true;
345 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS, animation.ptr());
346 int prev_mode = animation->bezier_track_get_key_handle_mode(track, key);
347 Vector2 prev_in_handle = animation->bezier_track_get_key_in_handle(track, key);
348 Vector2 prev_out_handle = animation->bezier_track_get_key_out_handle(track, key);
349 undo_redo->add_do_method(editor, "_bezier_track_set_key_handle_mode", animation.ptr(), track, key, value);
350 undo_redo->add_do_method(this, "_update_obj", animation);
351 undo_redo->add_undo_method(editor, "_bezier_track_set_key_handle_mode", animation.ptr(), track, key, prev_mode);
352 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", track, key, prev_in_handle);
353 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", track, key, prev_out_handle);
354 undo_redo->add_undo_method(this, "_update_obj", animation);
355 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
356 if (ape) {
357 undo_redo->add_do_method(ape, "_animation_update_key_frame");
358 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
359 }
360 undo_redo->commit_action();
361
362 setting = false;
363 return true;
364 }
365 } break;
366 case Animation::TYPE_AUDIO: {
367 if (name == "stream") {
368 Ref<AudioStream> stream = p_value;
369
370 setting = true;
371 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
372 Ref<Resource> prev = animation->audio_track_get_key_stream(track, key);
373 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_stream", track, key, stream);
374 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_stream", track, key, prev);
375 undo_redo->add_do_method(this, "_update_obj", animation);
376 undo_redo->add_undo_method(this, "_update_obj", animation);
377 undo_redo->commit_action();
378
379 setting = false;
380 notify_change(); // To update limits for `start_offset`/`end_offset` sliders (they depend on the stream length).
381 return true;
382 }
383
384 if (name == "start_offset") {
385 float value = p_value;
386
387 setting = true;
388 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
389 float prev = animation->audio_track_get_key_start_offset(track, key);
390 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_start_offset", track, key, value);
391 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_start_offset", track, key, prev);
392 undo_redo->add_do_method(this, "_update_obj", animation);
393 undo_redo->add_undo_method(this, "_update_obj", animation);
394 undo_redo->commit_action();
395
396 setting = false;
397 return true;
398 }
399
400 if (name == "end_offset") {
401 float value = p_value;
402
403 setting = true;
404 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
405 float prev = animation->audio_track_get_key_end_offset(track, key);
406 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_end_offset", track, key, value);
407 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_end_offset", track, key, prev);
408 undo_redo->add_do_method(this, "_update_obj", animation);
409 undo_redo->add_undo_method(this, "_update_obj", animation);
410 undo_redo->commit_action();
411
412 setting = false;
413 return true;
414 }
415 } break;
416 case Animation::TYPE_ANIMATION: {
417 if (name == "animation") {
418 StringName anim_name = p_value;
419
420 setting = true;
421 undo_redo->create_action(TTR("Animation Change Keyframe Value"), UndoRedo::MERGE_ENDS);
422 StringName prev = animation->animation_track_get_key_animation(track, key);
423 undo_redo->add_do_method(animation.ptr(), "animation_track_set_key_animation", track, key, anim_name);
424 undo_redo->add_undo_method(animation.ptr(), "animation_track_set_key_animation", track, key, prev);
425 undo_redo->add_do_method(this, "_update_obj", animation);
426 undo_redo->add_undo_method(this, "_update_obj", animation);
427 undo_redo->commit_action();
428
429 setting = false;
430 return true;
431 }
432 } break;
433 }
434
435 return false;
436 }
437
438 bool AnimationTrackKeyEdit::_get(const StringName &p_name, Variant &r_ret) const {
439 int key = animation->track_find_key(track, key_ofs, Animation::FIND_MODE_APPROX);
440 ERR_FAIL_COND_V(key == -1, false);
441
442 String name = p_name;
443 if (name == "easing") {
444 r_ret = animation->track_get_key_transition(track, key);
445 return true;
446 }
447
448 switch (animation->track_get_type(track)) {
449 case Animation::TYPE_POSITION_3D:
450 case Animation::TYPE_ROTATION_3D:
451 case Animation::TYPE_SCALE_3D: {
452 if (name == "position" || name == "rotation" || name == "scale") {
453 r_ret = animation->track_get_key_value(track, key);
454 return true;
455 }
456 } break;
457 case Animation::TYPE_BLEND_SHAPE:
458 case Animation::TYPE_VALUE: {
459 if (name == "value") {
460 r_ret = animation->track_get_key_value(track, key);
461 return true;
462 }
463
464 } break;
465 case Animation::TYPE_METHOD: {
466 Dictionary d = animation->track_get_key_value(track, key);
467
468 if (name == "name") {
469 ERR_FAIL_COND_V(!d.has("method"), false);
470 r_ret = d["method"];
471 return true;
472 }
473
474 ERR_FAIL_COND_V(!d.has("args"), false);
475
476 Vector<Variant> args = d["args"];
477
478 if (name == "arg_count") {
479 r_ret = args.size();
480 return true;
481 }
482
483 if (name.begins_with("args/")) {
484 int idx = name.get_slicec('/', 1).to_int();
485 ERR_FAIL_INDEX_V(idx, args.size(), false);
486
487 String what = name.get_slicec('/', 2);
488 if (what == "type") {
489 r_ret = args[idx].get_type();
490 return true;
491 }
492
493 if (what == "value") {
494 r_ret = args[idx];
495 return true;
496 }
497 }
498
499 } break;
500 case Animation::TYPE_BEZIER: {
501 if (name == "value") {
502 r_ret = animation->bezier_track_get_key_value(track, key);
503 return true;
504 }
505
506 if (name == "in_handle") {
507 r_ret = animation->bezier_track_get_key_in_handle(track, key);
508 return true;
509 }
510
511 if (name == "out_handle") {
512 r_ret = animation->bezier_track_get_key_out_handle(track, key);
513 return true;
514 }
515
516 if (name == "handle_mode") {
517 r_ret = animation->bezier_track_get_key_handle_mode(track, key);
518 return true;
519 }
520
521 } break;
522 case Animation::TYPE_AUDIO: {
523 if (name == "stream") {
524 r_ret = animation->audio_track_get_key_stream(track, key);
525 return true;
526 }
527
528 if (name == "start_offset") {
529 r_ret = animation->audio_track_get_key_start_offset(track, key);
530 return true;
531 }
532
533 if (name == "end_offset") {
534 r_ret = animation->audio_track_get_key_end_offset(track, key);
535 return true;
536 }
537
538 } break;
539 case Animation::TYPE_ANIMATION: {
540 if (name == "animation") {
541 r_ret = animation->animation_track_get_key_animation(track, key);
542 return true;
543 }
544
545 } break;
546 }
547
548 return false;
549 }
550
551 void AnimationTrackKeyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
552 if (animation.is_null()) {
553 return;
554 }
555
556 ERR_FAIL_INDEX(track, animation->get_track_count());
557 int key = animation->track_find_key(track, key_ofs, Animation::FIND_MODE_APPROX);
558 ERR_FAIL_COND(key == -1);
559
560 switch (animation->track_get_type(track)) {
561 case Animation::TYPE_POSITION_3D: {
562 p_list->push_back(PropertyInfo(Variant::VECTOR3, PNAME("position")));
563 } break;
564 case Animation::TYPE_ROTATION_3D: {
565 p_list->push_back(PropertyInfo(Variant::QUATERNION, PNAME("rotation")));
566 } break;
567 case Animation::TYPE_SCALE_3D: {
568 p_list->push_back(PropertyInfo(Variant::VECTOR3, PNAME("scale")));
569 } break;
570 case Animation::TYPE_BLEND_SHAPE: {
571 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("value")));
572 } break;
573 case Animation::TYPE_VALUE: {
574 Variant v = animation->track_get_key_value(track, key);
575
576 if (hint.type != Variant::NIL) {
577 PropertyInfo pi = hint;
578 pi.name = PNAME("value");
579 p_list->push_back(pi);
580 } else {
581 PropertyHint val_hint = PROPERTY_HINT_NONE;
582 String val_hint_string;
583
584 if (v.get_type() == Variant::OBJECT) {
585 // Could actually check the object property if exists..? Yes I will!
586 Ref<Resource> res = v;
587 if (res.is_valid()) {
588 val_hint = PROPERTY_HINT_RESOURCE_TYPE;
589 val_hint_string = res->get_class();
590 }
591 }
592
593 if (v.get_type() != Variant::NIL) {
594 p_list->push_back(PropertyInfo(v.get_type(), PNAME("value"), val_hint, val_hint_string));
595 }
596 }
597
598 } break;
599 case Animation::TYPE_METHOD: {
600 p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("name")));
601 p_list->push_back(PropertyInfo(Variant::INT, PNAME("arg_count"), PROPERTY_HINT_RANGE, "0,32,1,or_greater"));
602
603 Dictionary d = animation->track_get_key_value(track, key);
604 ERR_FAIL_COND(!d.has("args"));
605 Vector<Variant> args = d["args"];
606 String vtypes;
607 for (int i = 0; i < Variant::VARIANT_MAX; i++) {
608 if (i > 0) {
609 vtypes += ",";
610 }
611 vtypes += Variant::get_type_name(Variant::Type(i));
612 }
613
614 for (int i = 0; i < args.size(); i++) {
615 p_list->push_back(PropertyInfo(Variant::INT, vformat("%s/%d/%s", PNAME("args"), i, PNAME("type")), PROPERTY_HINT_ENUM, vtypes));
616 if (args[i].get_type() != Variant::NIL) {
617 p_list->push_back(PropertyInfo(args[i].get_type(), vformat("%s/%d/%s", PNAME("args"), i, PNAME("value"))));
618 }
619 }
620
621 } break;
622 case Animation::TYPE_BEZIER: {
623 Animation::HandleMode hm = animation->bezier_track_get_key_handle_mode(track, key);
624 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("value")));
625 if (hm == Animation::HANDLE_MODE_LINEAR) {
626 p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("in_handle"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY));
627 p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("out_handle"), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_READ_ONLY));
628 } else {
629 p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("in_handle")));
630 p_list->push_back(PropertyInfo(Variant::VECTOR2, PNAME("out_handle")));
631 }
632 p_list->push_back(PropertyInfo(Variant::INT, PNAME("handle_mode"), PROPERTY_HINT_ENUM, "Free,Linear,Balanced,Mirrored"));
633
634 } break;
635 case Animation::TYPE_AUDIO: {
636 p_list->push_back(PropertyInfo(Variant::OBJECT, PNAME("stream"), PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"));
637 Ref<AudioStream> audio_stream = animation->audio_track_get_key_stream(track, key);
638 String hint_string = vformat("0,%.4f,0.0001,or_greater", audio_stream.is_valid() ? audio_stream->get_length() : 3600.0);
639 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("start_offset"), PROPERTY_HINT_RANGE, hint_string));
640 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("end_offset"), PROPERTY_HINT_RANGE, hint_string));
641
642 } break;
643 case Animation::TYPE_ANIMATION: {
644 String animations;
645
646 if (root_path) {
647 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(root_path->get_node_or_null(animation->track_get_path(track)));
648 if (ap) {
649 List<StringName> anims;
650 ap->get_animation_list(&anims);
651 for (const StringName &E : anims) {
652 if (!animations.is_empty()) {
653 animations += ",";
654 }
655
656 animations += String(E);
657 }
658 }
659 }
660
661 if (!animations.is_empty()) {
662 animations += ",";
663 }
664 animations += "[stop]";
665
666 p_list->push_back(PropertyInfo(Variant::STRING_NAME, PNAME("animation"), PROPERTY_HINT_ENUM, animations));
667
668 } break;
669 }
670
671 if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
672 p_list->push_back(PropertyInfo(Variant::FLOAT, PNAME("easing"), PROPERTY_HINT_EXP_EASING));
673 }
674 }
675
676 void AnimationTrackKeyEdit::notify_change() {
677 notify_property_list_changed();
678 }
679
680 Node *AnimationTrackKeyEdit::get_root_path() {
681 return root_path;
682 }
683
684 void AnimationTrackKeyEdit::set_use_fps(bool p_enable) {
685 use_fps = p_enable;
686 notify_property_list_changed();
687 }
688
689 void AnimationMultiTrackKeyEdit::_bind_methods() {
690 ClassDB::bind_method(D_METHOD("_update_obj"), &AnimationMultiTrackKeyEdit::_update_obj);
691 ClassDB::bind_method(D_METHOD("_key_ofs_changed"), &AnimationMultiTrackKeyEdit::_key_ofs_changed);
692 ClassDB::bind_method(D_METHOD("_hide_script_from_inspector"), &AnimationMultiTrackKeyEdit::_hide_script_from_inspector);
693 ClassDB::bind_method(D_METHOD("_hide_metadata_from_inspector"), &AnimationMultiTrackKeyEdit::_hide_metadata_from_inspector);
694 ClassDB::bind_method(D_METHOD("get_root_path"), &AnimationMultiTrackKeyEdit::get_root_path);
695 ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &AnimationMultiTrackKeyEdit::_dont_undo_redo);
696 ClassDB::bind_method(D_METHOD("_is_read_only"), &AnimationMultiTrackKeyEdit::_is_read_only);
697 }
698
699 void AnimationMultiTrackKeyEdit::_fix_node_path(Variant &value, NodePath &base) {
700 NodePath np = value;
701
702 if (np == NodePath()) {
703 return;
704 }
705
706 Node *root = EditorNode::get_singleton()->get_tree()->get_root();
707
708 Node *np_node = root->get_node_or_null(np);
709 ERR_FAIL_NULL(np_node);
710
711 Node *edited_node = root->get_node_or_null(base);
712 ERR_FAIL_NULL(edited_node);
713
714 value = edited_node->get_path_to(np_node);
715 }
716
717 void AnimationMultiTrackKeyEdit::_update_obj(const Ref<Animation> &p_anim) {
718 if (setting || animation != p_anim) {
719 return;
720 }
721
722 notify_change();
723 }
724
725 void AnimationMultiTrackKeyEdit::_key_ofs_changed(const Ref<Animation> &p_anim, float from, float to) {
726 if (animation != p_anim) {
727 return;
728 }
729
730 for (const KeyValue<int, List<float>> &E : key_ofs_map) {
731 int key = 0;
732 for (const float &key_ofs : E.value) {
733 if (from != key_ofs) {
734 key++;
735 continue;
736 }
737
738 int track = E.key;
739 key_ofs_map[track].get(key) = to;
740
741 if (setting) {
742 return;
743 }
744
745 notify_change();
746
747 return;
748 }
749 }
750 }
751
752 bool AnimationMultiTrackKeyEdit::_set(const StringName &p_name, const Variant &p_value) {
753 bool update_obj = false;
754 bool change_notify_deserved = false;
755 for (const KeyValue<int, List<float>> &E : key_ofs_map) {
756 int track = E.key;
757 for (const float &key_ofs : E.value) {
758 int key = animation->track_find_key(track, key_ofs, Animation::FIND_MODE_APPROX);
759 ERR_FAIL_COND_V(key == -1, false);
760
761 String name = p_name;
762 if (name == "easing") {
763 float val = p_value;
764 float prev_val = animation->track_get_key_transition(track, key);
765
766 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
767 if (!setting) {
768 setting = true;
769 undo_redo->create_action(TTR("Animation Multi Change Transition"), UndoRedo::MERGE_ENDS);
770 }
771 undo_redo->add_do_method(animation.ptr(), "track_set_key_transition", track, key, val);
772 undo_redo->add_undo_method(animation.ptr(), "track_set_key_transition", track, key, prev_val);
773 update_obj = true;
774 }
775
776 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
777 switch (animation->track_get_type(track)) {
778 case Animation::TYPE_POSITION_3D:
779 case Animation::TYPE_ROTATION_3D:
780 case Animation::TYPE_SCALE_3D: {
781 Variant old = animation->track_get_key_value(track, key);
782 if (!setting) {
783 String action_name;
784 switch (animation->track_get_type(track)) {
785 case Animation::TYPE_POSITION_3D:
786 action_name = TTR("Animation Multi Change Position3D");
787 break;
788 case Animation::TYPE_ROTATION_3D:
789 action_name = TTR("Animation Multi Change Rotation3D");
790 break;
791 case Animation::TYPE_SCALE_3D:
792 action_name = TTR("Animation Multi Change Scale3D");
793 break;
794 default: {
795 }
796 }
797
798 setting = true;
799 undo_redo->create_action(action_name);
800 }
801 undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, p_value);
802 undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, old);
803 update_obj = true;
804 } break;
805 case Animation::TYPE_BLEND_SHAPE:
806 case Animation::TYPE_VALUE: {
807 if (name == "value") {
808 Variant value = p_value;
809
810 if (value.get_type() == Variant::NODE_PATH) {
811 _fix_node_path(value, base_map[track]);
812 }
813
814 if (!setting) {
815 setting = true;
816 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
817 }
818 Variant prev = animation->track_get_key_value(track, key);
819 undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, value);
820 undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, prev);
821 update_obj = true;
822 }
823 } break;
824 case Animation::TYPE_METHOD: {
825 Dictionary d_old = animation->track_get_key_value(track, key);
826 Dictionary d_new = d_old.duplicate();
827
828 bool mergeable = false;
829
830 if (name == "name") {
831 d_new["method"] = p_value;
832 } else if (name == "arg_count") {
833 Vector<Variant> args = d_old["args"];
834 args.resize(p_value);
835 d_new["args"] = args;
836 change_notify_deserved = true;
837 } else if (name.begins_with("args/")) {
838 Vector<Variant> args = d_old["args"];
839 int idx = name.get_slicec('/', 1).to_int();
840 ERR_FAIL_INDEX_V(idx, args.size(), false);
841
842 String what = name.get_slicec('/', 2);
843 if (what == "type") {
844 Variant::Type t = Variant::Type(int(p_value));
845
846 if (t != args[idx].get_type()) {
847 Callable::CallError err;
848 if (Variant::can_convert_strict(args[idx].get_type(), t)) {
849 Variant old = args[idx];
850 Variant *ptrs[1] = { &old };
851 Variant::construct(t, args.write[idx], (const Variant **)ptrs, 1, err);
852 } else {
853 Variant::construct(t, args.write[idx], nullptr, 0, err);
854 }
855 change_notify_deserved = true;
856 d_new["args"] = args;
857 }
858 } else if (what == "value") {
859 Variant value = p_value;
860 if (value.get_type() == Variant::NODE_PATH) {
861 _fix_node_path(value, base_map[track]);
862 }
863
864 args.write[idx] = value;
865 d_new["args"] = args;
866 mergeable = true;
867 }
868 }
869
870 Variant prev = animation->track_get_key_value(track, key);
871
872 if (!setting) {
873 if (mergeable) {
874 undo_redo->create_action(TTR("Animation Multi Change Call"), UndoRedo::MERGE_ENDS);
875 } else {
876 undo_redo->create_action(TTR("Animation Multi Change Call"));
877 }
878
879 setting = true;
880 }
881
882 undo_redo->add_do_method(animation.ptr(), "track_set_key_value", track, key, d_new);
883 undo_redo->add_undo_method(animation.ptr(), "track_set_key_value", track, key, d_old);
884 update_obj = true;
885 } break;
886 case Animation::TYPE_BEZIER: {
887 if (name == "value") {
888 const Variant &value = p_value;
889
890 if (!setting) {
891 setting = true;
892 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
893 }
894 float prev = animation->bezier_track_get_key_value(track, key);
895 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_value", track, key, value);
896 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_value", track, key, prev);
897 update_obj = true;
898 } else if (name == "in_handle") {
899 const Variant &value = p_value;
900
901 if (!setting) {
902 setting = true;
903 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
904 }
905 Vector2 prev = animation->bezier_track_get_key_in_handle(track, key);
906 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", track, key, value);
907 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", track, key, prev);
908 update_obj = true;
909 } else if (name == "out_handle") {
910 const Variant &value = p_value;
911
912 if (!setting) {
913 setting = true;
914 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
915 }
916 Vector2 prev = animation->bezier_track_get_key_out_handle(track, key);
917 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", track, key, value);
918 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", track, key, prev);
919 update_obj = true;
920 } else if (name == "handle_mode") {
921 const Variant &value = p_value;
922
923 if (!setting) {
924 setting = true;
925 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS, animation.ptr());
926 }
927 int prev_mode = animation->bezier_track_get_key_handle_mode(track, key);
928 Vector2 prev_in_handle = animation->bezier_track_get_key_in_handle(track, key);
929 Vector2 prev_out_handle = animation->bezier_track_get_key_out_handle(track, key);
930 undo_redo->add_do_method(editor, "_bezier_track_set_key_handle_mode", animation.ptr(), track, key, value);
931 undo_redo->add_undo_method(editor, "_bezier_track_set_key_handle_mode", animation.ptr(), track, key, prev_mode);
932 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", track, key, prev_in_handle);
933 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", track, key, prev_out_handle);
934 update_obj = true;
935 }
936 } break;
937 case Animation::TYPE_AUDIO: {
938 if (name == "stream") {
939 Ref<AudioStream> stream = p_value;
940
941 if (!setting) {
942 setting = true;
943 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
944 }
945 Ref<Resource> prev = animation->audio_track_get_key_stream(track, key);
946 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_stream", track, key, stream);
947 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_stream", track, key, prev);
948 update_obj = true;
949 } else if (name == "start_offset") {
950 float value = p_value;
951
952 if (!setting) {
953 setting = true;
954 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
955 }
956 float prev = animation->audio_track_get_key_start_offset(track, key);
957 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_start_offset", track, key, value);
958 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_start_offset", track, key, prev);
959 update_obj = true;
960 } else if (name == "end_offset") {
961 float value = p_value;
962
963 if (!setting) {
964 setting = true;
965 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
966 }
967 float prev = animation->audio_track_get_key_end_offset(track, key);
968 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_end_offset", track, key, value);
969 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_end_offset", track, key, prev);
970 update_obj = true;
971 }
972 } break;
973 case Animation::TYPE_ANIMATION: {
974 if (name == "animation") {
975 StringName anim_name = p_value;
976
977 if (!setting) {
978 setting = true;
979 undo_redo->create_action(TTR("Animation Multi Change Keyframe Value"), UndoRedo::MERGE_ENDS);
980 }
981 StringName prev = animation->animation_track_get_key_animation(track, key);
982 undo_redo->add_do_method(animation.ptr(), "animation_track_set_key_animation", track, key, anim_name);
983 undo_redo->add_undo_method(animation.ptr(), "animation_track_set_key_animation", track, key, prev);
984 update_obj = true;
985 }
986 } break;
987 }
988 }
989 }
990
991 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
992 if (setting) {
993 if (update_obj) {
994 undo_redo->add_do_method(this, "_update_obj", animation);
995 undo_redo->add_undo_method(this, "_update_obj", animation);
996 }
997
998 undo_redo->commit_action();
999 setting = false;
1000
1001 if (change_notify_deserved) {
1002 notify_change();
1003 }
1004
1005 return true;
1006 }
1007
1008 return false;
1009 }
1010
1011 bool AnimationMultiTrackKeyEdit::_get(const StringName &p_name, Variant &r_ret) const {
1012 for (const KeyValue<int, List<float>> &E : key_ofs_map) {
1013 int track = E.key;
1014 for (const float &key_ofs : E.value) {
1015 int key = animation->track_find_key(track, key_ofs, Animation::FIND_MODE_APPROX);
1016 ERR_CONTINUE(key == -1);
1017
1018 String name = p_name;
1019 if (name == "easing") {
1020 r_ret = animation->track_get_key_transition(track, key);
1021 return true;
1022 }
1023
1024 switch (animation->track_get_type(track)) {
1025 case Animation::TYPE_POSITION_3D:
1026 case Animation::TYPE_ROTATION_3D:
1027 case Animation::TYPE_SCALE_3D: {
1028 if (name == "position" || name == "rotation" || name == "scale") {
1029 r_ret = animation->track_get_key_value(track, key);
1030 return true;
1031 }
1032
1033 } break;
1034 case Animation::TYPE_BLEND_SHAPE:
1035 case Animation::TYPE_VALUE: {
1036 if (name == "value") {
1037 r_ret = animation->track_get_key_value(track, key);
1038 return true;
1039 }
1040
1041 } break;
1042 case Animation::TYPE_METHOD: {
1043 Dictionary d = animation->track_get_key_value(track, key);
1044
1045 if (name == "name") {
1046 ERR_FAIL_COND_V(!d.has("method"), false);
1047 r_ret = d["method"];
1048 return true;
1049 }
1050
1051 ERR_FAIL_COND_V(!d.has("args"), false);
1052
1053 Vector<Variant> args = d["args"];
1054
1055 if (name == "arg_count") {
1056 r_ret = args.size();
1057 return true;
1058 }
1059
1060 if (name.begins_with("args/")) {
1061 int idx = name.get_slicec('/', 1).to_int();
1062 ERR_FAIL_INDEX_V(idx, args.size(), false);
1063
1064 String what = name.get_slicec('/', 2);
1065 if (what == "type") {
1066 r_ret = args[idx].get_type();
1067 return true;
1068 }
1069
1070 if (what == "value") {
1071 r_ret = args[idx];
1072 return true;
1073 }
1074 }
1075
1076 } break;
1077 case Animation::TYPE_BEZIER: {
1078 if (name == "value") {
1079 r_ret = animation->bezier_track_get_key_value(track, key);
1080 return true;
1081 }
1082
1083 if (name == "in_handle") {
1084 r_ret = animation->bezier_track_get_key_in_handle(track, key);
1085 return true;
1086 }
1087
1088 if (name == "out_handle") {
1089 r_ret = animation->bezier_track_get_key_out_handle(track, key);
1090 return true;
1091 }
1092
1093 if (name == "handle_mode") {
1094 r_ret = animation->bezier_track_get_key_handle_mode(track, key);
1095 return true;
1096 }
1097
1098 } break;
1099 case Animation::TYPE_AUDIO: {
1100 if (name == "stream") {
1101 r_ret = animation->audio_track_get_key_stream(track, key);
1102 return true;
1103 }
1104
1105 if (name == "start_offset") {
1106 r_ret = animation->audio_track_get_key_start_offset(track, key);
1107 return true;
1108 }
1109
1110 if (name == "end_offset") {
1111 r_ret = animation->audio_track_get_key_end_offset(track, key);
1112 return true;
1113 }
1114
1115 } break;
1116 case Animation::TYPE_ANIMATION: {
1117 if (name == "animation") {
1118 r_ret = animation->animation_track_get_key_animation(track, key);
1119 return true;
1120 }
1121
1122 } break;
1123 }
1124 }
1125 }
1126
1127 return false;
1128 }
1129
1130 void AnimationMultiTrackKeyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
1131 if (animation.is_null()) {
1132 return;
1133 }
1134
1135 int first_track = -1;
1136 float first_key = -1.0;
1137
1138 bool same_track_type = true;
1139 bool same_key_type = true;
1140 for (const KeyValue<int, List<float>> &E : key_ofs_map) {
1141 int track = E.key;
1142 ERR_FAIL_INDEX(track, animation->get_track_count());
1143
1144 if (first_track < 0) {
1145 first_track = track;
1146 }
1147
1148 if (same_track_type) {
1149 if (animation->track_get_type(first_track) != animation->track_get_type(track)) {
1150 same_track_type = false;
1151 same_key_type = false;
1152 }
1153
1154 for (const float &F : E.value) {
1155 int key = animation->track_find_key(track, F, Animation::FIND_MODE_APPROX);
1156 ERR_FAIL_COND(key == -1);
1157 if (first_key < 0) {
1158 first_key = key;
1159 }
1160
1161 if (animation->track_get_key_value(first_track, first_key).get_type() != animation->track_get_key_value(track, key).get_type()) {
1162 same_key_type = false;
1163 }
1164 }
1165 }
1166 }
1167
1168 if (same_track_type) {
1169 switch (animation->track_get_type(first_track)) {
1170 case Animation::TYPE_POSITION_3D: {
1171 p_list->push_back(PropertyInfo(Variant::VECTOR3, "position"));
1172 } break;
1173 case Animation::TYPE_ROTATION_3D: {
1174 p_list->push_back(PropertyInfo(Variant::QUATERNION, "rotation"));
1175 } break;
1176 case Animation::TYPE_SCALE_3D: {
1177 p_list->push_back(PropertyInfo(Variant::VECTOR3, "scale"));
1178 } break;
1179 case Animation::TYPE_BLEND_SHAPE: {
1180 p_list->push_back(PropertyInfo(Variant::FLOAT, "value"));
1181 } break;
1182 case Animation::TYPE_VALUE: {
1183 if (same_key_type) {
1184 Variant v = animation->track_get_key_value(first_track, first_key);
1185
1186 if (hint.type != Variant::NIL) {
1187 PropertyInfo pi = hint;
1188 pi.name = "value";
1189 p_list->push_back(pi);
1190 } else {
1191 PropertyHint val_hint = PROPERTY_HINT_NONE;
1192 String val_hint_string;
1193
1194 if (v.get_type() == Variant::OBJECT) {
1195 // Could actually check the object property if exists..? Yes I will!
1196 Ref<Resource> res = v;
1197 if (res.is_valid()) {
1198 val_hint = PROPERTY_HINT_RESOURCE_TYPE;
1199 val_hint_string = res->get_class();
1200 }
1201 }
1202
1203 if (v.get_type() != Variant::NIL) {
1204 p_list->push_back(PropertyInfo(v.get_type(), "value", val_hint, val_hint_string));
1205 }
1206 }
1207 }
1208
1209 p_list->push_back(PropertyInfo(Variant::FLOAT, "easing", PROPERTY_HINT_EXP_EASING));
1210 } break;
1211 case Animation::TYPE_METHOD: {
1212 p_list->push_back(PropertyInfo(Variant::STRING_NAME, "name"));
1213
1214 p_list->push_back(PropertyInfo(Variant::INT, "arg_count", PROPERTY_HINT_RANGE, "0,32,1,or_greater"));
1215
1216 Dictionary d = animation->track_get_key_value(first_track, first_key);
1217 ERR_FAIL_COND(!d.has("args"));
1218 Vector<Variant> args = d["args"];
1219 String vtypes;
1220 for (int i = 0; i < Variant::VARIANT_MAX; i++) {
1221 if (i > 0) {
1222 vtypes += ",";
1223 }
1224 vtypes += Variant::get_type_name(Variant::Type(i));
1225 }
1226
1227 for (int i = 0; i < args.size(); i++) {
1228 p_list->push_back(PropertyInfo(Variant::INT, "args/" + itos(i) + "/type", PROPERTY_HINT_ENUM, vtypes));
1229 if (args[i].get_type() != Variant::NIL) {
1230 p_list->push_back(PropertyInfo(args[i].get_type(), "args/" + itos(i) + "/value"));
1231 }
1232 }
1233 } break;
1234 case Animation::TYPE_BEZIER: {
1235 p_list->push_back(PropertyInfo(Variant::FLOAT, "value"));
1236 p_list->push_back(PropertyInfo(Variant::VECTOR2, "in_handle"));
1237 p_list->push_back(PropertyInfo(Variant::VECTOR2, "out_handle"));
1238 p_list->push_back(PropertyInfo(Variant::INT, "handle_mode", PROPERTY_HINT_ENUM, "Free,Linear,Balanced,Mirrored"));
1239 } break;
1240 case Animation::TYPE_AUDIO: {
1241 p_list->push_back(PropertyInfo(Variant::OBJECT, "stream", PROPERTY_HINT_RESOURCE_TYPE, "AudioStream"));
1242 p_list->push_back(PropertyInfo(Variant::FLOAT, "start_offset", PROPERTY_HINT_RANGE, "0,3600,0.0001,or_greater"));
1243 p_list->push_back(PropertyInfo(Variant::FLOAT, "end_offset", PROPERTY_HINT_RANGE, "0,3600,0.0001,or_greater"));
1244 } break;
1245 case Animation::TYPE_ANIMATION: {
1246 if (key_ofs_map.size() > 1) {
1247 break;
1248 }
1249
1250 String animations;
1251
1252 if (root_path) {
1253 AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(root_path->get_node_or_null(animation->track_get_path(first_track)));
1254 if (ap) {
1255 List<StringName> anims;
1256 ap->get_animation_list(&anims);
1257 for (const StringName &anim : anims) {
1258 if (!animations.is_empty()) {
1259 animations += ",";
1260 }
1261
1262 animations += String(anim);
1263 }
1264 }
1265 }
1266
1267 if (!animations.is_empty()) {
1268 animations += ",";
1269 }
1270 animations += "[stop]";
1271
1272 p_list->push_back(PropertyInfo(Variant::STRING_NAME, "animation", PROPERTY_HINT_ENUM, animations));
1273 } break;
1274 }
1275 }
1276 }
1277
1278 void AnimationMultiTrackKeyEdit::notify_change() {
1279 notify_property_list_changed();
1280 }
1281
1282 Node *AnimationMultiTrackKeyEdit::get_root_path() {
1283 return root_path;
1284 }
1285
1286 void AnimationMultiTrackKeyEdit::set_use_fps(bool p_enable) {
1287 use_fps = p_enable;
1288 notify_property_list_changed();
1289 }
1290
1291 void AnimationTimelineEdit::_zoom_changed(double) {
1292 double zoom_pivot = 0; // Point on timeline to stay fixed.
1293 double zoom_pivot_delta = 0; // Delta seconds from left-most point on timeline to zoom pivot.
1294
1295 int timeline_width_pixels = get_size().width - get_buttons_width() - get_name_limit();
1296 double timeline_width_seconds = timeline_width_pixels / last_zoom_scale; // Length (in seconds) of visible part of timeline before zoom.
1297 double updated_timeline_width_seconds = timeline_width_pixels / get_zoom_scale(); // Length after zoom.
1298 double updated_timeline_half_width = updated_timeline_width_seconds / 2.0;
1299 bool zooming = updated_timeline_width_seconds < timeline_width_seconds;
1300
1301 double timeline_left = get_value();
1302 double timeline_right = timeline_left + timeline_width_seconds;
1303 double timeline_center = timeline_left + timeline_width_seconds / 2.0;
1304
1305 if (zoom_callback_occurred) { // Zooming with scroll wheel will focus on the position of the mouse.
1306 double zoom_scroll_origin_norm = (zoom_scroll_origin.x - get_name_limit()) / timeline_width_pixels;
1307 zoom_scroll_origin_norm = MAX(zoom_scroll_origin_norm, 0);
1308 zoom_pivot = timeline_left + timeline_width_seconds * zoom_scroll_origin_norm;
1309 zoom_pivot_delta = updated_timeline_width_seconds * zoom_scroll_origin_norm;
1310 zoom_callback_occurred = false;
1311 } else { // Zooming with slider will depend on the current play position.
1312 // If the play position is not in range, or exactly in the center, zoom in on the center.
1313 if (get_play_position() < timeline_left || get_play_position() > timeline_left + timeline_width_seconds || get_play_position() == timeline_center) {
1314 zoom_pivot = timeline_center;
1315 zoom_pivot_delta = updated_timeline_half_width;
1316 }
1317 // Zoom from right if play position is right of center,
1318 // and shrink from right if play position is left of center.
1319 else if ((get_play_position() > timeline_center) == zooming) {
1320 // If play position crosses to other side of center, center it.
1321 bool center_passed = (get_play_position() < timeline_right - updated_timeline_half_width) == zooming;
1322 zoom_pivot = center_passed ? get_play_position() : timeline_right;
1323 double center_offset = CMP_EPSILON * (zooming ? 1 : -1); // Small offset to prevent crossover.
1324 zoom_pivot_delta = center_passed ? updated_timeline_half_width + center_offset : updated_timeline_width_seconds;
1325 }
1326 // Zoom from left if play position is left of center,
1327 // and shrink from left if play position is right of center.
1328 else if ((get_play_position() <= timeline_center) == zooming) {
1329 // If play position crosses to other side of center, center it.
1330 bool center_passed = (get_play_position() > timeline_left + updated_timeline_half_width) == zooming;
1331 zoom_pivot = center_passed ? get_play_position() : timeline_left;
1332 double center_offset = CMP_EPSILON * (zooming ? -1 : 1); // Small offset to prevent crossover.
1333 zoom_pivot_delta = center_passed ? updated_timeline_half_width + center_offset : 0;
1334 }
1335 }
1336
1337 double hscroll_pos = zoom_pivot - zoom_pivot_delta;
1338 hscroll_pos = CLAMP(hscroll_pos, hscroll->get_min(), hscroll->get_max());
1339
1340 hscroll->set_value(hscroll_pos);
1341 hscroll_on_zoom_buffer = hscroll_pos; // In case of page update.
1342 last_zoom_scale = get_zoom_scale();
1343
1344 queue_redraw();
1345 play_position->queue_redraw();
1346 emit_signal(SNAME("zoom_changed"));
1347 }
1348
1349 float AnimationTimelineEdit::get_zoom_scale() const {
1350 return _get_zoom_scale(zoom->get_value());
1351 }
1352
1353 float AnimationTimelineEdit::_get_zoom_scale(double p_zoom_value) const {
1354 float zv = zoom->get_max() - p_zoom_value;
1355 if (zv < 1) {
1356 zv = 1.0 - zv;
1357 return Math::pow(1.0f + zv, 8.0f) * 100;
1358 } else {
1359 return 1.0 / Math::pow(zv, 8.0f) * 100;
1360 }
1361 }
1362
1363 void AnimationTimelineEdit::_anim_length_changed(double p_new_len) {
1364 if (editing) {
1365 return;
1366 }
1367
1368 p_new_len = MAX(SECOND_DECIMAL, p_new_len);
1369 if (use_fps && animation->get_step() > 0) {
1370 p_new_len *= animation->get_step();
1371 }
1372
1373 editing = true;
1374 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1375 undo_redo->create_action(TTR("Change Animation Length"), UndoRedo::MERGE_ENDS);
1376 undo_redo->add_do_method(animation.ptr(), "set_length", p_new_len);
1377 undo_redo->add_undo_method(animation.ptr(), "set_length", animation->get_length());
1378 undo_redo->commit_action();
1379 editing = false;
1380 queue_redraw();
1381
1382 emit_signal(SNAME("length_changed"), p_new_len);
1383 }
1384
1385 void AnimationTimelineEdit::_anim_loop_pressed() {
1386 if (!read_only) {
1387 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1388 undo_redo->create_action(TTR("Change Animation Loop"));
1389 switch (animation->get_loop_mode()) {
1390 case Animation::LOOP_NONE: {
1391 undo_redo->add_do_method(animation.ptr(), "set_loop_mode", Animation::LOOP_LINEAR);
1392 } break;
1393 case Animation::LOOP_LINEAR: {
1394 undo_redo->add_do_method(animation.ptr(), "set_loop_mode", Animation::LOOP_PINGPONG);
1395 } break;
1396 case Animation::LOOP_PINGPONG: {
1397 undo_redo->add_do_method(animation.ptr(), "set_loop_mode", Animation::LOOP_NONE);
1398 } break;
1399 default:
1400 break;
1401 }
1402 undo_redo->add_do_method(this, "update_values");
1403 undo_redo->add_undo_method(animation.ptr(), "set_loop_mode", animation->get_loop_mode());
1404 undo_redo->add_undo_method(this, "update_values");
1405 undo_redo->commit_action();
1406 } else {
1407 String base = animation->get_path();
1408 int srpos = base.find("::");
1409 if (srpos != -1) {
1410 base = animation->get_path().substr(0, srpos);
1411 }
1412
1413 if (FileAccess::exists(base + ".import")) {
1414 if (ResourceLoader::get_resource_type(base) == "PackedScene") {
1415 EditorNode::get_singleton()->show_warning(TTR("Can't change loop mode on animation instanced from an imported scene.\n\nTo change this animation's loop mode, navigate to the scene's Advanced Import settings and select the animation.\nYou can then change the loop mode from the inspector menu."));
1416 } else {
1417 EditorNode::get_singleton()->show_warning(TTR("Can't change loop mode on animation instanced from an imported resource."));
1418 }
1419 } else {
1420 if (ResourceLoader::get_resource_type(base) == "PackedScene") {
1421 EditorNode::get_singleton()->show_warning(TTR("Can't change loop mode on animation embedded in another scene.\n\nYou must open this scene and change the animation's loop mode from there."));
1422 } else {
1423 EditorNode::get_singleton()->show_warning(TTR("Can't change loop mode on animation embedded in another resource."));
1424 }
1425 }
1426
1427 update_values();
1428 }
1429 }
1430
1431 int AnimationTimelineEdit::get_buttons_width() const {
1432 const Ref<Texture2D> interp_mode = get_editor_theme_icon(SNAME("TrackContinuous"));
1433 const Ref<Texture2D> interp_type = get_editor_theme_icon(SNAME("InterpRaw"));
1434 const Ref<Texture2D> loop_type = get_editor_theme_icon(SNAME("InterpWrapClamp"));
1435 const Ref<Texture2D> remove_icon = get_editor_theme_icon(SNAME("Remove"));
1436 const Ref<Texture2D> down_icon = get_theme_icon(SNAME("select_arrow"), SNAME("Tree"));
1437
1438 const int h_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationTrackEdit"));
1439 const int outer_margin = get_theme_constant(SNAME("outer_margin"), SNAME("AnimationTrackEdit"));
1440
1441 int total_w = interp_mode->get_width() + interp_type->get_width() + loop_type->get_width() + remove_icon->get_width() + outer_margin;
1442 total_w += (down_icon->get_width() + h_separation) * 4;
1443
1444 return total_w;
1445 }
1446
1447 int AnimationTimelineEdit::get_name_limit() const {
1448 Ref<Texture2D> hsize_icon = get_editor_theme_icon(SNAME("Hsize"));
1449
1450 int filter_track_width = filter_track->is_visible() ? filter_track->get_custom_minimum_size().width : 0;
1451 int limit = MAX(name_limit, add_track->get_minimum_size().width + hsize_icon->get_width() + filter_track_width + 16 * EDSCALE);
1452
1453 limit = MIN(limit, get_size().width - get_buttons_width() - 1);
1454
1455 return limit;
1456 }
1457
1458 void AnimationTimelineEdit::_notification(int p_what) {
1459 switch (p_what) {
1460 case NOTIFICATION_THEME_CHANGED: {
1461 add_track->set_button_icon(get_editor_theme_icon(SNAME("Add")));
1462 loop->set_button_icon(get_editor_theme_icon(SNAME("Loop")));
1463 time_icon->set_texture(get_editor_theme_icon(SNAME("Time")));
1464 filter_track->set_right_icon(get_editor_theme_icon(SNAME("Search")));
1465
1466 add_track->get_popup()->clear();
1467 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyValue")), TTR("Property Track..."));
1468 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyXPosition")), TTR("3D Position Track..."));
1469 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyXRotation")), TTR("3D Rotation Track..."));
1470 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyXScale")), TTR("3D Scale Track..."));
1471 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyBlendShape")), TTR("Blend Shape Track..."));
1472 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyCall")), TTR("Call Method Track..."));
1473 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyBezier")), TTR("Bezier Curve Track..."));
1474 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyAudio")), TTR("Audio Playback Track..."));
1475 add_track->get_popup()->add_icon_item(get_editor_theme_icon(SNAME("KeyAnimation")), TTR("Animation Playback Track..."));
1476
1477 timeline_resize_rect.size = get_editor_theme_icon(SNAME("TimelineHandle"))->get_size();
1478 } break;
1479
1480 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
1481 if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
1482 break;
1483 }
1484 [[fallthrough]];
1485 }
1486 case NOTIFICATION_READY: {
1487 panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
1488 panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
1489 } break;
1490
1491 case NOTIFICATION_RESIZED: {
1492 len_hb->set_position(Vector2(get_size().width - get_buttons_width(), 0));
1493 len_hb->set_size(Size2(get_buttons_width(), get_size().height));
1494 int hsize_icon_width = get_editor_theme_icon(SNAME("Hsize"))->get_width();
1495 add_track_hb->set_size(Size2(name_limit - ((hsize_icon_width + 16) * EDSCALE), 0));
1496 } break;
1497
1498 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
1499 RID ae = get_accessibility_element();
1500 ERR_FAIL_COND(ae.is_null());
1501
1502 //TODO
1503 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
1504 DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Animation timeline editor")));
1505 } break;
1506
1507 case NOTIFICATION_DRAW: {
1508 if (animation.is_null()) {
1509 return;
1510 }
1511
1512 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1513 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1514
1515 const Ref<StyleBox> &stylebox_time_unavailable = get_theme_stylebox(SNAME("time_unavailable"), SNAME("AnimationTimelineEdit"));
1516 const Ref<StyleBox> &stylebox_time_available = get_theme_stylebox(SNAME("time_available"), SNAME("AnimationTimelineEdit"));
1517
1518 const Color v_line_primary_color = get_theme_color(SNAME("v_line_primary_color"), SNAME("AnimationTimelineEdit"));
1519 const Color v_line_secondary_color = get_theme_color(SNAME("v_line_secondary_color"), SNAME("AnimationTimelineEdit"));
1520 const Color h_line_color = get_theme_color(SNAME("h_line_color"), SNAME("AnimationTimelineEdit"));
1521 const Color font_primary_color = get_theme_color(SNAME("font_primary_color"), SNAME("AnimationTimelineEdit"));
1522 const Color font_secondary_color = get_theme_color(SNAME("font_secondary_color"), SNAME("AnimationTimelineEdit"));
1523
1524 const int v_line_primary_margin = get_theme_constant(SNAME("v_line_primary_margin"), SNAME("AnimationTimelineEdit"));
1525 const int v_line_secondary_margin = get_theme_constant(SNAME("v_line_secondary_margin"), SNAME("AnimationTimelineEdit"));
1526 const int v_line_primary_width = get_theme_constant(SNAME("v_line_primary_width"), SNAME("AnimationTimelineEdit"));
1527 const int v_line_secondary_width = get_theme_constant(SNAME("v_line_secondary_width"), SNAME("AnimationTimelineEdit"));
1528 const int text_primary_margin = get_theme_constant(SNAME("text_primary_margin"), SNAME("AnimationTimelineEdit"));
1529 const int text_secondary_margin = get_theme_constant(SNAME("text_secondary_margin"), SNAME("AnimationTimelineEdit"));
1530
1531 int zoomw = get_size().width - get_buttons_width() - get_name_limit();
1532 float scale = get_zoom_scale();
1533 int h = get_size().height;
1534
1535 float l = animation->get_length();
1536 if (l <= 0) {
1537 l = SECOND_DECIMAL; // Avoid crashor.
1538 }
1539
1540 Ref<Texture2D> hsize_icon = get_editor_theme_icon(SNAME("Hsize"));
1541 hsize_rect = Rect2(get_name_limit() - hsize_icon->get_width() - 8 * EDSCALE, (get_size().height - hsize_icon->get_height()) / 2, hsize_icon->get_width(), hsize_icon->get_height());
1542 draw_texture(hsize_icon, hsize_rect.position);
1543
1544 {
1545 float time_min = 0;
1546 float time_max = animation->get_length();
1547 for (int i = 0; i < animation->get_track_count(); i++) {
1548 if (animation->track_get_key_count(i) > 0) {
1549 float beg = animation->track_get_key_time(i, 0);
1550
1551 if (beg < time_min) {
1552 time_min = beg;
1553 }
1554
1555 float end = animation->track_get_key_time(i, animation->track_get_key_count(i) - 1);
1556
1557 if (end > time_max) {
1558 time_max = end;
1559 }
1560 }
1561 }
1562
1563 PackedStringArray markers = animation->get_marker_names();
1564 if (markers.size() > 0) {
1565 float min_marker = animation->get_marker_time(markers[0]);
1566 float max_marker = animation->get_marker_time(markers[markers.size() - 1]);
1567 if (min_marker < time_min) {
1568 time_min = min_marker;
1569 }
1570 if (max_marker > time_max) {
1571 time_max = max_marker;
1572 }
1573 }
1574
1575 float extra = (zoomw / scale) * 0.5;
1576
1577 time_max += extra;
1578 set_min(time_min);
1579 set_max(time_max);
1580
1581 if (zoomw / scale < (time_max - time_min)) {
1582 hscroll->show();
1583
1584 } else {
1585 hscroll->hide();
1586 }
1587 }
1588
1589 set_page(zoomw / scale);
1590
1591 if (hscroll->is_visible() && hscroll_on_zoom_buffer >= 0) {
1592 hscroll->set_value(hscroll_on_zoom_buffer);
1593 hscroll_on_zoom_buffer = -1.0;
1594 }
1595
1596 int end_px = (l - get_value()) * scale;
1597 int begin_px = -get_value() * scale;
1598
1599 draw_style_box(stylebox_time_unavailable, Rect2(Point2(get_name_limit(), 0), Point2(zoomw - 1, h)));
1600
1601 if (begin_px < zoomw && end_px > 0) {
1602 if (begin_px < 0) {
1603 begin_px = 0;
1604 }
1605 if (end_px > zoomw) {
1606 end_px = zoomw;
1607 }
1608
1609 draw_style_box(stylebox_time_available, Rect2(Point2(get_name_limit() + begin_px, 0), Point2(end_px - begin_px, h)));
1610 }
1611
1612 #define SC_ADJ 100
1613 int dec = 1;
1614 int step = 1;
1615 int decimals = 2;
1616 bool step_found = false;
1617
1618 const float period_width = font->get_char_size('.', font_size).width;
1619 float max_digit_width = font->get_char_size('0', font_size).width;
1620 for (int i = 1; i <= 9; i++) {
1621 const float digit_width = font->get_char_size('0' + i, font_size).width;
1622 max_digit_width = MAX(digit_width, max_digit_width);
1623 }
1624 const int max_sc = int(Math::ceil(zoomw / scale));
1625 const int max_sc_width = String::num(max_sc).length() * Math::ceil(max_digit_width);
1626
1627 const int min_margin = MAX(text_secondary_margin, text_primary_margin);
1628
1629 while (!step_found) {
1630 int min = max_sc_width;
1631 if (decimals > 0) {
1632 min += Math::ceil(period_width + max_digit_width * decimals);
1633 }
1634
1635 min += (min_margin * 2);
1636
1637 static const int _multp[3] = { 1, 2, 5 };
1638 for (int i = 0; i < 3; i++) {
1639 step = (_multp[i] * dec);
1640 if (step * scale / SC_ADJ > min) {
1641 step_found = true;
1642 break;
1643 }
1644 }
1645 if (step_found) {
1646 break;
1647 }
1648 dec *= 10;
1649 decimals--;
1650 if (decimals < 0) {
1651 decimals = 0;
1652 }
1653 }
1654
1655 if (use_fps) {
1656 float step_size = animation->get_step();
1657 if (step_size > 0) {
1658 int prev_frame_ofs = -10000000;
1659
1660 for (int i = 0; i < zoomw; i++) {
1661 float pos = get_value() + double(i) / scale;
1662 float prev = get_value() + (double(i) - 1.0) / scale;
1663
1664 int frame = pos / step_size;
1665 int prev_frame = prev / step_size;
1666
1667 bool sub = Math::floor(prev) == Math::floor(pos);
1668
1669 if (frame != prev_frame && i >= prev_frame_ofs) {
1670 int line_margin = sub ? v_line_secondary_margin : v_line_primary_margin;
1671 int line_width = sub ? v_line_secondary_width : v_line_primary_width;
1672 Color line_color = sub ? v_line_secondary_color : v_line_primary_color;
1673
1674 draw_line(Point2(get_name_limit() + i, 0 + line_margin), Point2(get_name_limit() + i, h - line_margin), line_color, line_width);
1675
1676 int text_margin = sub ? text_secondary_margin : text_primary_margin;
1677
1678 draw_string(font, Point2(get_name_limit() + i + text_margin, (h - font->get_height(font_size)) / 2 + font->get_ascent(font_size)).floor(), itos(frame), HORIZONTAL_ALIGNMENT_LEFT, zoomw - i, font_size, sub ? font_secondary_color : font_primary_color);
1679
1680 prev_frame_ofs = i + font->get_string_size(itos(frame), HORIZONTAL_ALIGNMENT_LEFT, -1, font_size).x + text_margin;
1681 }
1682 }
1683 }
1684
1685 } else {
1686 for (int i = 0; i < zoomw; i++) {
1687 float pos = get_value() + double(i) / scale;
1688 float prev = get_value() + (double(i) - 1.0) / scale;
1689
1690 int sc = int(Math::floor(pos * SC_ADJ));
1691 int prev_sc = int(Math::floor(prev * SC_ADJ));
1692
1693 if ((sc / step) != (prev_sc / step) || (prev_sc < 0 && sc >= 0)) {
1694 int scd = sc < 0 ? prev_sc : sc;
1695 bool sub = (((scd - (scd % step)) % (dec * 10)) != 0);
1696
1697 int line_margin = sub ? v_line_secondary_margin : v_line_primary_margin;
1698 int line_width = sub ? v_line_secondary_width : v_line_primary_width;
1699 Color line_color = sub ? v_line_secondary_color : v_line_primary_color;
1700
1701 draw_line(Point2(get_name_limit() + i, 0 + line_margin), Point2(get_name_limit() + i, h - line_margin), line_color, line_width);
1702
1703 int text_margin = sub ? text_secondary_margin : text_primary_margin;
1704
1705 draw_string(font, Point2(get_name_limit() + i + text_margin, (h - font->get_height(font_size)) / 2 + font->get_ascent(font_size)).floor(), String::num((scd - (scd % step)) / double(SC_ADJ), decimals), HORIZONTAL_ALIGNMENT_LEFT, zoomw - i, font_size, sub ? font_secondary_color : font_primary_color);
1706 }
1707 }
1708 }
1709
1710 draw_line(Vector2(0, get_size().height), get_size(), h_line_color, Math::round(EDSCALE));
1711
1712 int px = get_name_limit() + end_px - timeline_resize_rect.size.width * 0.5;
1713 timeline_resize_rect.position.x = px;
1714 if (px >= get_name_limit() && px < zoomw + get_buttons_width()) {
1715 draw_texture(get_editor_theme_icon(SNAME("TimelineHandle")), Point2(timeline_resize_rect.position.x, 0));
1716 }
1717
1718 update_values();
1719 } break;
1720
1721 case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
1722 if (resizing_timeline) {
1723 _commit_timeline_resize();
1724 } else if (dragging_hsize || dragging_timeline) {
1725 _stop_dragging();
1726 }
1727 } break;
1728 }
1729 }
1730
1731 void AnimationTimelineEdit::set_animation(const Ref<Animation> &p_animation, bool p_read_only) {
1732 animation = p_animation;
1733 read_only = p_read_only;
1734
1735 length->set_read_only(read_only);
1736
1737 if (animation.is_valid()) {
1738 len_hb->show();
1739 filter_track->show();
1740 if (read_only) {
1741 add_track->hide();
1742 } else {
1743 add_track->show();
1744 }
1745 play_position->show();
1746 } else {
1747 len_hb->hide();
1748 filter_track->hide();
1749 add_track->hide();
1750 play_position->hide();
1751 }
1752 queue_redraw();
1753 }
1754
1755 Size2 AnimationTimelineEdit::get_minimum_size() const {
1756 Size2 ms = filter_track->get_minimum_size();
1757 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1758 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1759 ms.height = MAX(ms.height, font->get_height(font_size));
1760 ms.width = get_buttons_width() + add_track->get_minimum_size().width + get_editor_theme_icon(SNAME("Hsize"))->get_width() + 2 + 8 * EDSCALE;
1761 return ms;
1762 }
1763
1764 void AnimationTimelineEdit::set_zoom(Range *p_zoom) {
1765 zoom = p_zoom;
1766 zoom->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTimelineEdit::_zoom_changed));
1767 }
1768
1769 void AnimationTimelineEdit::auto_fit() {
1770 if (animation.is_null()) {
1771 return;
1772 }
1773
1774 float anim_end = animation->get_length();
1775 float anim_start = 0;
1776
1777 // Search for keyframe outside animation boundaries to include keyframes before animation start and after animation length.
1778 int track_count = animation->get_track_count();
1779 for (int track = 0; track < track_count; ++track) {
1780 for (int i = 0; i < animation->track_get_key_count(track); i++) {
1781 float key_time = animation->track_get_key_time(track, i);
1782 if (key_time > anim_end) {
1783 anim_end = key_time;
1784 }
1785 if (key_time < anim_start) {
1786 anim_start = key_time;
1787 }
1788 }
1789 }
1790
1791 float anim_length = anim_end - anim_start;
1792 int timeline_width_pixels = get_size().width - get_buttons_width() - get_name_limit();
1793
1794 // I want a little buffer at the end... (5% looks nice and we should keep some space for the bezier handles)
1795 timeline_width_pixels *= 0.95;
1796
1797 // The technique is to reuse the _get_zoom_scale function directly to be sure that the auto_fit is always calculated
1798 // the same way as the zoom slider. It's a little bit more calculation then doing the inverse of get_zoom_scale but
1799 // it's really easier to understand and should always be accurate.
1800 float new_zoom = zoom->get_max();
1801 while (true) {
1802 double test_zoom_scale = _get_zoom_scale(new_zoom);
1803
1804 if (anim_length * test_zoom_scale <= timeline_width_pixels) {
1805 // It fits...
1806 break;
1807 }
1808
1809 new_zoom -= zoom->get_step();
1810
1811 if (new_zoom <= zoom->get_min()) {
1812 new_zoom = zoom->get_min();
1813 break;
1814 }
1815 }
1816
1817 // Horizontal scroll to get_min which should include keyframes that are before the animation start.
1818 hscroll->set_value(hscroll->get_min());
1819 // Set the zoom value... the signal value_changed will be emitted and the timeline will be refreshed correctly!
1820 zoom->set_value(new_zoom);
1821 // The new zoom value must be applied correctly so the scrollbar are updated before we move the scrollbar to
1822 // the beginning of the animation, hence the call deferred.
1823 callable_mp(this, &AnimationTimelineEdit::_scroll_to_start).call_deferred();
1824 }
1825
1826 void AnimationTimelineEdit::_scroll_to_start() {
1827 // Horizontal scroll to get_min which should include keyframes that are before the animation start.
1828 hscroll->set_value(hscroll->get_min());
1829 }
1830
1831 void AnimationTimelineEdit::set_track_edit(AnimationTrackEdit *p_track_edit) {
1832 track_edit = p_track_edit;
1833 }
1834
1835 void AnimationTimelineEdit::set_editor(AnimationTrackEditor *p_editor) {
1836 editor = p_editor;
1837 }
1838
1839 void AnimationTimelineEdit::set_play_position(float p_pos) {
1840 play_position_pos = p_pos;
1841 play_position->queue_redraw();
1842 }
1843
1844 float AnimationTimelineEdit::get_play_position() const {
1845 return play_position_pos;
1846 }
1847
1848 void AnimationTimelineEdit::update_play_position() {
1849 play_position->queue_redraw();
1850 }
1851
1852 void AnimationTimelineEdit::update_values() {
1853 if (animation.is_null() || editing) {
1854 return;
1855 }
1856
1857 editing = true;
1858 if (use_fps && animation->get_step() > 0.0) {
1859 length->set_step(FPS_DECIMAL);
1860 length->set_value(animation->get_length() / animation->get_step());
1861 length->set_tooltip_text(TTR("Animation length (frames)"));
1862 time_icon->set_tooltip_text(TTR("Animation length (frames)"));
1863 if (track_edit) {
1864 track_edit->editor->_update_key_edit();
1865 track_edit->editor->marker_edit->_update_key_edit();
1866 }
1867 } else {
1868 length->set_step(SECOND_DECIMAL);
1869 length->set_value(animation->get_length());
1870 length->set_tooltip_text(TTR("Animation length (seconds)"));
1871 time_icon->set_tooltip_text(TTR("Animation length (seconds)"));
1872 }
1873
1874 switch (animation->get_loop_mode()) {
1875 case Animation::LOOP_NONE: {
1876 loop->set_button_icon(get_editor_theme_icon(SNAME("Loop")));
1877 loop->set_pressed(false);
1878 } break;
1879 case Animation::LOOP_LINEAR: {
1880 loop->set_button_icon(get_editor_theme_icon(SNAME("Loop")));
1881 loop->set_pressed(true);
1882 } break;
1883 case Animation::LOOP_PINGPONG: {
1884 loop->set_button_icon(get_editor_theme_icon(SNAME("PingPongLoop")));
1885 loop->set_pressed(true);
1886 } break;
1887 default:
1888 break;
1889 }
1890
1891 editing = false;
1892 }
1893
1894 void AnimationTimelineEdit::_play_position_draw() {
1895 if (animation.is_null() || play_position_pos < 0) {
1896 return;
1897 }
1898
1899 float scale = get_zoom_scale();
1900 int px = (-get_value() + play_position_pos) * scale + get_name_limit();
1901
1902 if (px >= get_name_limit() && px < (play_position->get_size().width - get_buttons_width())) {
1903 int h = editor->box_selection_container->get_global_position().y - get_global_position().y;
1904 Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
1905
1906 play_position->draw_line(Point2(px, 0), Point2(px, h), color, Math::round(2 * EDSCALE));
1907 play_position->draw_texture(
1908 get_editor_theme_icon(SNAME("TimelineIndicator")),
1909 Point2(px - get_editor_theme_icon(SNAME("TimelineIndicator"))->get_width() * 0.5, 0),
1910 color);
1911 }
1912 }
1913
1914 void AnimationTimelineEdit::gui_input(const Ref<InputEvent> &p_event) {
1915 ERR_FAIL_COND(p_event.is_null());
1916
1917 if (panner->gui_input(p_event, get_global_rect())) {
1918 accept_event();
1919 return;
1920 }
1921
1922 const Ref<InputEventMouseButton> mb = p_event;
1923
1924 if (mb.is_valid()) {
1925 if (mb->is_pressed() && mb->is_alt_pressed() && mb->get_button_index() == MouseButton::WHEEL_UP) {
1926 if (track_edit) {
1927 track_edit->get_editor()->goto_prev_step(true);
1928 }
1929 accept_event();
1930 }
1931
1932 if (mb->is_pressed() && mb->is_alt_pressed() && mb->get_button_index() == MouseButton::WHEEL_DOWN) {
1933 if (track_edit) {
1934 track_edit->get_editor()->goto_next_step(true);
1935 }
1936 accept_event();
1937 }
1938
1939 if (!resizing_timeline && !dragging_hsize && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1940 if (timeline_resize_rect.has_point(mb->get_position())) {
1941 resizing_timeline = true;
1942 timeline_resize_from = timeline_resize_rect.position.x + timeline_resize_rect.size.width / 2 - mb->get_position().x;
1943 timeline_resize_at = animation->get_length();
1944 } else if (hsize_rect.has_point(mb->get_position())) {
1945 dragging_hsize = true;
1946 dragging_hsize_from = mb->get_position().x;
1947 dragging_hsize_at = name_limit;
1948 }
1949 }
1950
1951 if (!resizing_timeline && !panner->is_panning() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT &&
1952 mb->get_position().x > get_name_limit() && mb->get_position().x < (get_size().width - get_buttons_width())) {
1953 int x = mb->get_position().x - get_name_limit();
1954
1955 float ofs = x / get_zoom_scale() + get_value();
1956 emit_signal(SNAME("timeline_changed"), ofs, mb->is_alt_pressed());
1957 dragging_timeline = true;
1958 }
1959
1960 if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1961 if (resizing_timeline) {
1962 _commit_timeline_resize();
1963 } else if (dragging_hsize || dragging_timeline) {
1964 _stop_dragging();
1965 }
1966 }
1967
1968 if (resizing_timeline && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
1969 resizing_timeline = false;
1970 // Undo changes.
1971 animation->set_length(timeline_resize_at);
1972 }
1973 }
1974
1975 Ref<InputEventMouseMotion> mm = p_event;
1976
1977 if (mm.is_valid()) {
1978 if (dragging_hsize) {
1979 int ofs = mm->get_position().x - dragging_hsize_from;
1980 name_limit = dragging_hsize_at + ofs;
1981 // Make sure name_limit is clamped to the range that UI allows.
1982 name_limit = get_name_limit();
1983 int hsize_icon_width = get_editor_theme_icon(SNAME("Hsize"))->get_width();
1984 add_track_hb->set_size(Size2(name_limit - ((hsize_icon_width + 16) * EDSCALE), 0));
1985 queue_redraw();
1986 emit_signal(SNAME("name_limit_changed"));
1987 play_position->queue_redraw();
1988 } else if (resizing_timeline) {
1989 int x = mm->get_position().x + timeline_resize_from - get_name_limit();
1990 float ofs = x / get_zoom_scale() + get_value();
1991 animation->set_length(editor->snap_time(ofs));
1992 } else if (dragging_timeline) {
1993 int x = mm->get_position().x - get_name_limit();
1994 float ofs = x / get_zoom_scale() + get_value();
1995 emit_signal(SNAME("timeline_changed"), ofs, mm->is_alt_pressed());
1996 }
1997 }
1998 }
1999
2000 void AnimationTimelineEdit::_commit_timeline_resize() {
2001 if (animation->get_length() != timeline_resize_at) {
2002 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2003 undo_redo->create_action(TTR("Change Animation Length"));
2004 undo_redo->add_do_method(animation.ptr(), "set_length", animation->get_length());
2005 undo_redo->add_undo_method(animation.ptr(), "set_length", timeline_resize_at);
2006 undo_redo->commit_action(false);
2007 }
2008 resizing_timeline = false;
2009 }
2010
2011 void AnimationTimelineEdit::_stop_dragging() {
2012 dragging_hsize = false;
2013 dragging_timeline = false;
2014 }
2015
2016 Control::CursorShape AnimationTimelineEdit::get_cursor_shape(const Point2 &p_pos) const {
2017 if (dragging_hsize || resizing_timeline || hsize_rect.has_point(p_pos) || timeline_resize_rect.has_point(p_pos)) {
2018 // Indicate that the track name column's width and the timeline length can be adjusted.
2019 return Control::CURSOR_HSIZE;
2020 } else {
2021 return get_default_cursor_shape();
2022 }
2023 }
2024
2025 void AnimationTimelineEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
2026 set_value(get_value() - p_scroll_vec.x / get_zoom_scale());
2027 }
2028
2029 void AnimationTimelineEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
2030 double current_zoom_value = get_zoom()->get_value();
2031 zoom_scroll_origin = p_origin;
2032 zoom_callback_occurred = true;
2033 get_zoom()->set_value(MAX(0.01, current_zoom_value - (1.0 - p_zoom_factor)));
2034 }
2035
2036 void AnimationTimelineEdit::set_use_fps(bool p_use_fps) {
2037 use_fps = p_use_fps;
2038 queue_redraw();
2039 }
2040
2041 bool AnimationTimelineEdit::is_using_fps() const {
2042 return use_fps;
2043 }
2044
2045 void AnimationTimelineEdit::set_hscroll(HScrollBar *p_hscroll) {
2046 hscroll = p_hscroll;
2047 }
2048
2049 void AnimationTimelineEdit::_track_added(int p_track) {
2050 emit_signal(SNAME("track_added"), p_track);
2051 }
2052
2053 void AnimationTimelineEdit::_bind_methods() {
2054 ADD_SIGNAL(MethodInfo("zoom_changed"));
2055 ADD_SIGNAL(MethodInfo("name_limit_changed"));
2056 ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::FLOAT, "position"), PropertyInfo(Variant::BOOL, "timeline_only")));
2057 ADD_SIGNAL(MethodInfo("track_added", PropertyInfo(Variant::INT, "track")));
2058 ADD_SIGNAL(MethodInfo("length_changed", PropertyInfo(Variant::FLOAT, "size")));
2059 ADD_SIGNAL(MethodInfo("filter_changed"));
2060
2061 ClassDB::bind_method(D_METHOD("update_values"), &AnimationTimelineEdit::update_values);
2062 }
2063
2064 AnimationTimelineEdit::AnimationTimelineEdit() {
2065 name_limit = 150 * EDSCALE;
2066
2067 play_position = memnew(Control);
2068 play_position->set_mouse_filter(MOUSE_FILTER_PASS);
2069 add_child(play_position);
2070 play_position->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
2071 play_position->connect(SceneStringName(draw), callable_mp(this, &AnimationTimelineEdit::_play_position_draw));
2072
2073 add_track_hb = memnew(HBoxContainer);
2074 add_child(add_track_hb);
2075
2076 add_track = memnew(MenuButton);
2077 add_track->set_tooltip_text(TTR("Select a new track by type to add to this animation."));
2078 add_track->set_position(Vector2(0, 0));
2079 add_track_hb->add_child(add_track);
2080 filter_track = memnew(LineEdit);
2081 filter_track->set_h_size_flags(SIZE_EXPAND_FILL);
2082 filter_track->set_custom_minimum_size(Vector2(120 * EDSCALE, 0));
2083 filter_track->set_placeholder(TTR("Filter Tracks"));
2084 filter_track->set_tooltip_text(TTR("Filter tracks by entering part of their node name or property."));
2085 filter_track->connect(SceneStringName(text_changed), callable_mp((AnimationTrackEditor *)this, &AnimationTrackEditor::_on_filter_updated));
2086 filter_track->set_clear_button_enabled(true);
2087 filter_track->hide();
2088 add_track_hb->add_child(filter_track);
2089
2090 len_hb = memnew(HBoxContainer);
2091
2092 Control *expander = memnew(Control);
2093 expander->set_h_size_flags(SIZE_EXPAND_FILL);
2094 expander->set_mouse_filter(MOUSE_FILTER_IGNORE);
2095 len_hb->add_child(expander);
2096
2097 time_icon = memnew(TextureRect);
2098 time_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
2099 time_icon->set_tooltip_text(TTR("Animation length (seconds)"));
2100 len_hb->add_child(time_icon);
2101
2102 length = memnew(EditorSpinSlider);
2103 length->set_min(SECOND_DECIMAL);
2104 length->set_max(36000);
2105 length->set_step(SECOND_DECIMAL);
2106 length->set_allow_greater(true);
2107 length->set_custom_minimum_size(Vector2(70 * EDSCALE, 0));
2108 length->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
2109 length->set_tooltip_text(TTR("Animation length (seconds)"));
2110 length->set_accessibility_name(TTRC("Animation length (seconds)"));
2111 length->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTimelineEdit::_anim_length_changed));
2112 len_hb->add_child(length);
2113
2114 loop = memnew(Button);
2115 loop->set_flat(true);
2116 loop->set_tooltip_text(TTR("Animation Looping"));
2117 loop->connect(SceneStringName(pressed), callable_mp(this, &AnimationTimelineEdit::_anim_loop_pressed));
2118 loop->set_toggle_mode(true);
2119 len_hb->add_child(loop);
2120 add_child(len_hb);
2121
2122 add_track->hide();
2123 add_track->get_popup()->connect("index_pressed", callable_mp(this, &AnimationTimelineEdit::_track_added));
2124 len_hb->hide();
2125
2126 panner.instantiate();
2127 panner->set_scroll_zoom_factor(SCROLL_ZOOM_FACTOR_IN);
2128 panner->set_callbacks(callable_mp(this, &AnimationTimelineEdit::_pan_callback), callable_mp(this, &AnimationTimelineEdit::_zoom_callback));
2129 panner->set_pan_axis(ViewPanner::PAN_AXIS_HORIZONTAL);
2130
2131 set_layout_direction(Control::LAYOUT_DIRECTION_LTR);
2132 }
2133
2134 ////////////////////////////////////
2135
2136 void AnimationTrackEdit::_notification(int p_what) {
2137 switch (p_what) {
2138 case NOTIFICATION_THEME_CHANGED: {
2139 if (animation.is_null()) {
2140 return;
2141 }
2142 ERR_FAIL_INDEX(track, animation->get_track_count());
2143
2144 type_icon = _get_key_type_icon();
2145 selected_icon = get_editor_theme_icon(SNAME("KeySelected"));
2146 } break;
2147
2148 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
2149 RID ae = get_accessibility_element();
2150 ERR_FAIL_COND(ae.is_null());
2151
2152 //TODO
2153 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
2154 DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Animation track editor")));
2155 } break;
2156
2157 case NOTIFICATION_DRAW: {
2158 if (animation.is_null() || animation->get_track_count() == 0) {
2159 return;
2160 }
2161 ERR_FAIL_INDEX(track, animation->get_track_count());
2162
2163 int limit = timeline->get_name_limit();
2164
2165 const Ref<StyleBox> &stylebox_odd = get_theme_stylebox(SNAME("odd"), SNAME("AnimationTrackEdit"));
2166 const Ref<StyleBox> &stylebox_focus = get_theme_stylebox(SNAME("focus"), SNAME("AnimationTrackEdit"));
2167 const Ref<StyleBox> &stylebox_hover = get_theme_stylebox(SceneStringName(hover), SNAME("AnimationTrackEdit"));
2168
2169 const Color h_line_color = get_theme_color(SNAME("h_line_color"), SNAME("AnimationTrackEdit"));
2170 const int h_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationTrackEdit"));
2171 const int outer_margin = get_theme_constant(SNAME("outer_margin"), SNAME("AnimationTrackEdit"));
2172
2173 if (track % 2 == 1) {
2174 // Draw a background over odd lines to make long lists of tracks easier to read.
2175 draw_style_box(stylebox_odd, Rect2(Point2(1 * EDSCALE, 0), get_size() - Size2(1 * EDSCALE, 0)));
2176 }
2177
2178 if (hovered) {
2179 // Draw hover feedback.
2180 draw_style_box(stylebox_hover, Rect2(Point2(1 * EDSCALE, 0), get_size() - Size2(1 * EDSCALE, 0)));
2181 }
2182
2183 if (has_focus()) {
2184 // Offside so the horizontal sides aren't cutoff.
2185 draw_style_box(stylebox_focus, Rect2(Point2(1 * EDSCALE, 0), get_size() - Size2(1 * EDSCALE, 0)));
2186 }
2187
2188 int limit_end = get_size().width - timeline->get_buttons_width();
2189
2190 // Unavailable timeline.
2191
2192 {
2193 int px = (animation->get_length() - timeline->get_value()) * timeline->get_zoom_scale() + timeline->get_name_limit();
2194 px = MAX(px, timeline->get_name_limit());
2195 Rect2 rect = Rect2(px, 0, limit_end - px, get_size().height);
2196 if (rect.size.width > 0) {
2197 draw_rect(rect, Color(0, 0, 0, 0.2));
2198 }
2199 }
2200
2201 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
2202 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
2203 const Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
2204
2205 const Color dc = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
2206
2207 // Names and icons.
2208
2209 {
2210 Ref<Texture2D> check = animation->track_is_enabled(track) ? get_theme_icon(SNAME("checked"), SNAME("CheckBox")) : get_theme_icon(SNAME("unchecked"), SNAME("CheckBox"));
2211
2212 int ofs = in_group ? outer_margin : 0;
2213
2214 check_rect = Rect2(Point2(ofs, (get_size().height - check->get_height()) / 2).round(), check->get_size());
2215 draw_texture(check, check_rect.position);
2216 ofs += check->get_width() + h_separation;
2217
2218 Ref<Texture2D> key_type_icon = _get_key_type_icon();
2219 draw_texture(key_type_icon, Point2(ofs, (get_size().height - key_type_icon->get_height()) / 2).round());
2220 ofs += key_type_icon->get_width() + h_separation;
2221
2222 NodePath anim_path = animation->track_get_path(track);
2223 Node *node = nullptr;
2224 if (root) {
2225 node = root->get_node_or_null(anim_path);
2226 }
2227
2228 String text;
2229 Color text_color = color;
2230 if (node && EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
2231 text_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
2232 }
2233
2234 if (in_group) {
2235 if (animation->track_get_type(track) == Animation::TYPE_METHOD) {
2236 text = TTR("Functions:");
2237 } else if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
2238 text = TTR("Audio Clips:");
2239 } else if (animation->track_get_type(track) == Animation::TYPE_ANIMATION) {
2240 text = TTR("Animation Clips:");
2241 } else {
2242 text += anim_path.get_concatenated_subnames();
2243 }
2244 text_color.a *= 0.7;
2245 } else if (node) {
2246 Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(node);
2247 const Vector2 icon_size = Vector2(1, 1) * get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
2248
2249 icon_rect = Rect2(Point2(ofs, (get_size().height - check->get_height()) / 2).round(), icon_size);
2250 draw_texture_rect(icon, icon_rect);
2251 icon_cache = icon;
2252
2253 text = String() + node->get_name() + ":" + anim_path.get_concatenated_subnames();
2254 ofs += h_separation;
2255 ofs += icon_size.x;
2256
2257 } else {
2258 icon_cache = key_type_icon;
2259
2260 text = String(anim_path);
2261 }
2262
2263 path_cache = text;
2264
2265 path_rect = Rect2(ofs, 0, limit - ofs - h_separation, get_size().height);
2266
2267 Vector2 string_pos = Point2(ofs, (get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size));
2268 string_pos = string_pos.floor();
2269 draw_string(font, string_pos, text, HORIZONTAL_ALIGNMENT_LEFT, limit - ofs - h_separation, font_size, text_color);
2270
2271 draw_line(Point2(limit, 0), Point2(limit, get_size().height), h_line_color, Math::round(EDSCALE));
2272 }
2273
2274 // Marker sections.
2275
2276 {
2277 float scale = timeline->get_zoom_scale();
2278
2279 PackedStringArray section = editor->get_selected_section();
2280 if (section.size() == 2) {
2281 StringName start_marker = section[0];
2282 StringName end_marker = section[1];
2283 double start_time = animation->get_marker_time(start_marker);
2284 double end_time = animation->get_marker_time(end_marker);
2285
2286 // When AnimationPlayer is playing, don't move the preview rect, so it still indicates the playback section.
2287 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
2288 if (editor->is_marker_moving_selection() && !(player && player->is_playing())) {
2289 start_time += editor->get_marker_moving_selection_offset();
2290 end_time += editor->get_marker_moving_selection_offset();
2291 }
2292
2293 if (start_time < animation->get_length() && end_time >= 0) {
2294 float start_ofs = MAX(0, start_time) - timeline->get_value();
2295 float end_ofs = MIN(animation->get_length(), end_time) - timeline->get_value();
2296 start_ofs = start_ofs * scale + limit;
2297 end_ofs = end_ofs * scale + limit;
2298 start_ofs = MAX(start_ofs, limit);
2299 end_ofs = MIN(end_ofs, limit_end);
2300 Rect2 rect;
2301 rect.set_position(Vector2(start_ofs, 0));
2302 rect.set_size(Vector2(end_ofs - start_ofs, get_size().height));
2303
2304 draw_rect(rect, Color(1, 0.1, 0.1, 0.2));
2305 }
2306 }
2307 }
2308
2309 // Marker overlays.
2310
2311 {
2312 float scale = timeline->get_zoom_scale();
2313 PackedStringArray markers = animation->get_marker_names();
2314 for (const StringName marker : markers) {
2315 double time = animation->get_marker_time(marker);
2316 if (editor->is_marker_selected(marker) && editor->is_marker_moving_selection()) {
2317 time += editor->get_marker_moving_selection_offset();
2318 }
2319 if (time >= 0) {
2320 float offset = time - timeline->get_value();
2321 offset = offset * scale + limit;
2322 if (offset >= timeline->get_name_limit() && offset < limit_end) {
2323 Color marker_color = animation->get_marker_color(marker);
2324 marker_color.a = 0.2;
2325 draw_line(Point2(offset, 0), Point2(offset, get_size().height), marker_color, Math::round(EDSCALE));
2326 }
2327 }
2328 }
2329 }
2330
2331 // Keyframes.
2332
2333 draw_bg(limit, limit_end);
2334
2335 {
2336 float scale = timeline->get_zoom_scale();
2337
2338 // Pre-calculate the actual order of the keys. This is needed as a move might be happening
2339 // which might cause the keys to be in a different order than their current indices.
2340 Vector<Pair<float, int>> sorted_keys;
2341
2342 for (int i = 0; i < animation->track_get_key_count(track); i++) {
2343 float time_offset = animation->track_get_key_time(track, i) - timeline->get_value();
2344 if (editor->is_key_selected(track, i) && editor->is_moving_selection()) {
2345 time_offset += editor->get_moving_selection_offset();
2346 }
2347
2348 float screen_pos = time_offset * scale + limit;
2349 sorted_keys.push_back(Pair<float, int>(screen_pos, i));
2350 }
2351
2352 sorted_keys.sort();
2353
2354 for (int i = 0; i < sorted_keys.size(); i++) {
2355 float offset = sorted_keys[i].first;
2356 int original_index = sorted_keys[i].second;
2357 bool selected = editor->is_key_selected(track, original_index);
2358
2359 if (i < sorted_keys.size() - 1) {
2360 float offset_n = sorted_keys[i + 1].first;
2361
2362 int next_original_index = sorted_keys[i + 1].second;
2363
2364 draw_key_link(original_index, next_original_index, scale, int(offset), int(offset_n), limit, limit_end);
2365 draw_key(original_index, scale, int(offset), selected, limit, MIN(offset_n, limit_end));
2366 } else {
2367 draw_key(original_index, scale, int(offset), selected, limit, limit_end);
2368 }
2369 }
2370 }
2371
2372 draw_fg(limit, limit_end);
2373
2374 // Buttons.
2375
2376 {
2377 Ref<Texture2D> wrap_icon[2] = {
2378 get_editor_theme_icon(SNAME("InterpWrapClamp")),
2379 get_editor_theme_icon(SNAME("InterpWrapLoop")),
2380 };
2381 Ref<Texture2D> interp_icon[5] = {
2382 get_editor_theme_icon(SNAME("InterpRaw")),
2383 get_editor_theme_icon(SNAME("InterpLinear")),
2384 get_editor_theme_icon(SNAME("InterpCubic")),
2385 get_editor_theme_icon(SNAME("InterpLinearAngle")),
2386 get_editor_theme_icon(SNAME("InterpCubicAngle")),
2387 };
2388 Ref<Texture2D> cont_icon[3] = {
2389 get_editor_theme_icon(SNAME("TrackContinuous")),
2390 get_editor_theme_icon(SNAME("TrackDiscrete")),
2391 get_editor_theme_icon(SNAME("TrackCapture"))
2392 };
2393 Ref<Texture2D> blend_icon[2] = {
2394 get_editor_theme_icon(SNAME("UseBlendEnable")),
2395 get_editor_theme_icon(SNAME("UseBlendDisable")),
2396 };
2397
2398 int ofs = limit_end;
2399
2400 const Ref<Texture2D> down_icon = get_theme_icon(SNAME("select_arrow"), SNAME("Tree"));
2401
2402 draw_line(Point2(ofs, 0), Point2(ofs, get_size().height), h_line_color, Math::round(EDSCALE));
2403
2404 ofs += h_separation;
2405 {
2406 // Callmode.
2407
2408 Animation::UpdateMode update_mode;
2409
2410 if (animation->track_get_type(track) == Animation::TYPE_VALUE) {
2411 update_mode = animation->value_track_get_update_mode(track);
2412 } else {
2413 update_mode = Animation::UPDATE_CONTINUOUS;
2414 }
2415
2416 Ref<Texture2D> update_icon = cont_icon[update_mode];
2417
2418 update_mode_rect.position.x = ofs;
2419 update_mode_rect.position.y = Math::round((get_size().height - update_icon->get_height()) / 2);
2420 update_mode_rect.size = update_icon->get_size();
2421
2422 if (!animation->track_is_compressed(track) && animation->track_get_type(track) == Animation::TYPE_VALUE) {
2423 draw_texture(update_icon, update_mode_rect.position);
2424 }
2425 if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
2426 Ref<Texture2D> use_blend_icon = blend_icon[animation->audio_track_is_use_blend(track) ? 0 : 1];
2427 Vector2 use_blend_icon_pos = update_mode_rect.position + (update_mode_rect.size - use_blend_icon->get_size()) / 2;
2428 draw_texture(use_blend_icon, use_blend_icon_pos);
2429 }
2430 // Make it easier to click.
2431 update_mode_rect.position.y = 0;
2432 update_mode_rect.size.y = get_size().height;
2433
2434 ofs += update_icon->get_width() + h_separation / 2;
2435 update_mode_rect.size.x += h_separation / 2;
2436
2437 if (!read_only) {
2438 if (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_AUDIO) {
2439 draw_texture(down_icon, Vector2(ofs, (get_size().height - down_icon->get_height()) / 2).round());
2440 update_mode_rect.size.x += down_icon->get_width();
2441 } else if (animation->track_get_type(track) == Animation::TYPE_BEZIER) {
2442 update_mode_rect.size.x += down_icon->get_width();
2443 update_mode_rect = Rect2();
2444 } else {
2445 update_mode_rect = Rect2();
2446 }
2447 } else {
2448 update_mode_rect = Rect2();
2449 }
2450
2451 ofs += down_icon->get_width();
2452 draw_line(Point2(ofs + h_separation * 0.5, 0), Point2(ofs + h_separation * 0.5, get_size().height), h_line_color, Math::round(EDSCALE));
2453 ofs += h_separation;
2454 }
2455
2456 {
2457 // Interp.
2458
2459 Animation::InterpolationType interp_mode = animation->track_get_interpolation_type(track);
2460
2461 Ref<Texture2D> icon = interp_icon[interp_mode];
2462
2463 interp_mode_rect.position.x = ofs;
2464 interp_mode_rect.position.y = Math::round((get_size().height - icon->get_height()) / 2);
2465 interp_mode_rect.size = icon->get_size();
2466
2467 if (!animation->track_is_compressed(track) && (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_BLEND_SHAPE || animation->track_get_type(track) == Animation::TYPE_POSITION_3D || animation->track_get_type(track) == Animation::TYPE_SCALE_3D || animation->track_get_type(track) == Animation::TYPE_ROTATION_3D)) {
2468 draw_texture(icon, interp_mode_rect.position);
2469 }
2470 // Make it easier to click.
2471 interp_mode_rect.position.y = 0;
2472 interp_mode_rect.size.y = get_size().height;
2473
2474 ofs += icon->get_width() + h_separation / 2;
2475 interp_mode_rect.size.x += h_separation / 2;
2476
2477 if (!read_only && !animation->track_is_compressed(track) && (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_BLEND_SHAPE || animation->track_get_type(track) == Animation::TYPE_POSITION_3D || animation->track_get_type(track) == Animation::TYPE_SCALE_3D || animation->track_get_type(track) == Animation::TYPE_ROTATION_3D)) {
2478 draw_texture(down_icon, Vector2(ofs, (get_size().height - down_icon->get_height()) / 2).round());
2479 interp_mode_rect.size.x += down_icon->get_width();
2480 } else {
2481 interp_mode_rect = Rect2();
2482 }
2483
2484 ofs += down_icon->get_width();
2485 draw_line(Point2(ofs + h_separation * 0.5, 0), Point2(ofs + h_separation * 0.5, get_size().height), h_line_color, Math::round(EDSCALE));
2486 ofs += h_separation;
2487 }
2488
2489 {
2490 // Loop.
2491
2492 bool loop_wrap = animation->track_get_interpolation_loop_wrap(track);
2493
2494 Ref<Texture2D> icon = wrap_icon[loop_wrap ? 1 : 0];
2495
2496 loop_wrap_rect.position.x = ofs;
2497 loop_wrap_rect.position.y = Math::round((get_size().height - icon->get_height()) / 2);
2498 loop_wrap_rect.size = icon->get_size();
2499
2500 if (!animation->track_is_compressed(track) && (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_BLEND_SHAPE || animation->track_get_type(track) == Animation::TYPE_POSITION_3D || animation->track_get_type(track) == Animation::TYPE_SCALE_3D || animation->track_get_type(track) == Animation::TYPE_ROTATION_3D)) {
2501 draw_texture(icon, loop_wrap_rect.position);
2502 }
2503
2504 loop_wrap_rect.position.y = 0;
2505 loop_wrap_rect.size.y = get_size().height;
2506
2507 ofs += icon->get_width() + h_separation / 2;
2508 loop_wrap_rect.size.x += h_separation / 2;
2509
2510 if (!read_only && !animation->track_is_compressed(track) && (animation->track_get_type(track) == Animation::TYPE_VALUE || animation->track_get_type(track) == Animation::TYPE_BLEND_SHAPE || animation->track_get_type(track) == Animation::TYPE_POSITION_3D || animation->track_get_type(track) == Animation::TYPE_SCALE_3D || animation->track_get_type(track) == Animation::TYPE_ROTATION_3D)) {
2511 draw_texture(down_icon, Vector2(ofs, (get_size().height - down_icon->get_height()) / 2).round());
2512 loop_wrap_rect.size.x += down_icon->get_width();
2513 } else {
2514 loop_wrap_rect = Rect2();
2515 }
2516
2517 ofs += down_icon->get_width();
2518 draw_line(Point2(ofs + h_separation * 0.5, 0), Point2(ofs + h_separation * 0.5, get_size().height), h_line_color, Math::round(EDSCALE));
2519 ofs += h_separation;
2520 }
2521
2522 {
2523 // Erase.
2524
2525 Ref<Texture2D> icon = get_editor_theme_icon(animation->track_is_compressed(track) ? SNAME("Lock") : SNAME("Remove"));
2526
2527 remove_rect.position.x = ofs + ((get_size().width - ofs) - icon->get_width()) - outer_margin;
2528 remove_rect.position.y = Math::round((get_size().height - icon->get_height()) / 2);
2529 remove_rect.size = icon->get_size();
2530
2531 if (read_only) {
2532 draw_texture(icon, remove_rect.position, dc);
2533 } else {
2534 draw_texture(icon, remove_rect.position);
2535 }
2536 }
2537 }
2538
2539 if (in_group) {
2540 draw_line(Vector2(timeline->get_name_limit(), get_size().height), get_size(), h_line_color, Math::round(EDSCALE));
2541 } else {
2542 draw_line(Vector2(0, get_size().height), get_size(), h_line_color, Math::round(EDSCALE));
2543 }
2544
2545 if (dropping_at != 0) {
2546 Color drop_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
2547 if (dropping_at < 0) {
2548 draw_line(Vector2(0, 0), Vector2(get_size().width, 0), drop_color, Math::round(EDSCALE));
2549 } else {
2550 draw_line(Vector2(0, get_size().height), get_size(), drop_color, Math::round(EDSCALE));
2551 }
2552 }
2553 } break;
2554
2555 case NOTIFICATION_MOUSE_ENTER:
2556 hovered = true;
2557 queue_redraw();
2558 break;
2559 case NOTIFICATION_MOUSE_EXIT:
2560 hovered = false;
2561 // When the mouse cursor exits the track, we're no longer hovering any keyframe.
2562 hovering_key_idx = -1;
2563 queue_redraw();
2564 [[fallthrough]];
2565 case NOTIFICATION_DRAG_END: {
2566 cancel_drop();
2567 } break;
2568 }
2569 }
2570
2571 int AnimationTrackEdit::get_key_height() const {
2572 if (animation.is_null()) {
2573 return 0;
2574 }
2575
2576 return type_icon->get_height();
2577 }
2578
2579 Rect2 AnimationTrackEdit::get_key_rect(int p_index, float p_pixels_sec) {
2580 if (animation.is_null()) {
2581 return Rect2();
2582 }
2583 Rect2 rect = Rect2(-type_icon->get_width() / 2, 0, type_icon->get_width(), get_size().height);
2584
2585 // Make it a big easier to click.
2586 rect.position.x -= rect.size.x * 0.5;
2587 rect.size.x *= 2;
2588 return rect;
2589 }
2590
2591 bool AnimationTrackEdit::is_key_selectable_by_distance() const {
2592 return true;
2593 }
2594
2595 void AnimationTrackEdit::draw_key_link(int p_index_from, int p_index_to, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right) {
2596 if (p_next_x < p_clip_left) {
2597 return;
2598 }
2599 if (p_x > p_clip_right) {
2600 return;
2601 }
2602
2603 Variant current = animation->track_get_key_value(get_track(), p_index_from);
2604 Variant next = animation->track_get_key_value(get_track(), p_index_to);
2605 if (current != next || animation->track_get_type(get_track()) == Animation::TrackType::TYPE_METHOD) {
2606 return;
2607 }
2608
2609 Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
2610 color.a = 0.5;
2611
2612 int from_x = MAX(p_x, p_clip_left);
2613 int to_x = MIN(p_next_x, p_clip_right);
2614
2615 draw_line(Point2(from_x + 1, get_size().height / 2), Point2(to_x, get_size().height / 2), color, Math::round(2 * EDSCALE));
2616 }
2617
2618 void AnimationTrackEdit::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
2619 if (animation.is_null()) {
2620 return;
2621 }
2622
2623 if (p_x < p_clip_left || p_x > p_clip_right) {
2624 return;
2625 }
2626
2627 Ref<Texture2D> icon_to_draw = p_selected ? selected_icon : type_icon;
2628
2629 if (animation->track_get_type(track) == Animation::TYPE_VALUE && !Math::is_equal_approx(animation->track_get_key_transition(track, p_index), real_t(1.0))) {
2630 // Use a different icon for keys with non-linear easing.
2631 icon_to_draw = get_editor_theme_icon(p_selected ? SNAME("KeyEasedSelected") : SNAME("KeyValueEased"));
2632 }
2633
2634 // Override type icon for invalid value keys, unless selected.
2635 if (!p_selected && animation->track_get_type(track) == Animation::TYPE_VALUE) {
2636 const Variant &v = animation->track_get_key_value(track, p_index);
2637 Variant::Type valid_type = Variant::NIL;
2638 if (!_is_value_key_valid(v, valid_type)) {
2639 icon_to_draw = get_editor_theme_icon(SNAME("KeyInvalid"));
2640 }
2641 }
2642
2643 Vector2 ofs(p_x - icon_to_draw->get_width() / 2, (get_size().height - icon_to_draw->get_height()) / 2);
2644
2645 if (animation->track_get_type(track) == Animation::TYPE_METHOD) {
2646 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
2647 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
2648 Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
2649 color.a = 0.5;
2650
2651 Dictionary d = animation->track_get_key_value(track, p_index);
2652 String text;
2653
2654 if (d.has("method")) {
2655 text += String(d["method"]);
2656 }
2657 text += "(";
2658 Vector<Variant> args;
2659 if (d.has("args")) {
2660 args = d["args"];
2661 }
2662 for (int i = 0; i < args.size(); i++) {
2663 if (i > 0) {
2664 text += ", ";
2665 }
2666 text += args[i].get_construct_string();
2667 }
2668 text += ")";
2669
2670 int limit = editor->is_function_name_pressed() ? 0 : MAX(0, p_clip_right - p_x - icon_to_draw->get_width() * 2);
2671
2672 if (limit > 0) {
2673 draw_string(font, Vector2(p_x + icon_to_draw->get_width(), int(get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size)), text, HORIZONTAL_ALIGNMENT_LEFT, limit, font_size, color);
2674 }
2675 }
2676
2677 // Use a different color for the currently hovered key.
2678 // The color multiplier is chosen to work with both dark and light editor themes,
2679 // and on both unselected and selected key icons.
2680 draw_texture(
2681 icon_to_draw,
2682 ofs,
2683 p_index == hovering_key_idx ? get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")) : Color(1, 1, 1));
2684 }
2685
2686 // Helper.
2687 void AnimationTrackEdit::draw_rect_clipped(const Rect2 &p_rect, const Color &p_color, bool p_filled) {
2688 int clip_left = timeline->get_name_limit();
2689 int clip_right = get_size().width - timeline->get_buttons_width();
2690
2691 if (p_rect.position.x > clip_right) {
2692 return;
2693 }
2694 if (p_rect.position.x + p_rect.size.x < clip_left) {
2695 return;
2696 }
2697 Rect2 clip = Rect2(clip_left, 0, clip_right - clip_left, get_size().height);
2698 draw_rect(clip.intersection(p_rect), p_color, p_filled);
2699 }
2700
2701 void AnimationTrackEdit::draw_bg(int p_clip_left, int p_clip_right) {
2702 }
2703
2704 void AnimationTrackEdit::draw_fg(int p_clip_left, int p_clip_right) {
2705 }
2706
2707 void AnimationTrackEdit::draw_texture_region_clipped(const Ref<Texture2D> &p_texture, const Rect2 &p_rect, const Rect2 &p_region) {
2708 int clip_left = timeline->get_name_limit();
2709 int clip_right = get_size().width - timeline->get_buttons_width();
2710
2711 // Clip left and right.
2712 if (clip_left > p_rect.position.x + p_rect.size.x) {
2713 return;
2714 }
2715 if (clip_right < p_rect.position.x) {
2716 return;
2717 }
2718
2719 Rect2 rect = p_rect;
2720 Rect2 region = p_region;
2721
2722 if (clip_left > rect.position.x) {
2723 int rect_pixels = (clip_left - rect.position.x);
2724 int region_pixels = rect_pixels * region.size.x / rect.size.x;
2725
2726 rect.position.x += rect_pixels;
2727 rect.size.x -= rect_pixels;
2728
2729 region.position.x += region_pixels;
2730 region.size.x -= region_pixels;
2731 }
2732
2733 if (clip_right < rect.position.x + rect.size.x) {
2734 int rect_pixels = rect.position.x + rect.size.x - clip_right;
2735 int region_pixels = rect_pixels * region.size.x / rect.size.x;
2736
2737 rect.size.x -= rect_pixels;
2738 region.size.x -= region_pixels;
2739 }
2740
2741 draw_texture_rect_region(p_texture, rect, region);
2742 }
2743
2744 int AnimationTrackEdit::get_track() const {
2745 return track;
2746 }
2747
2748 Ref<Animation> AnimationTrackEdit::get_animation() const {
2749 return animation;
2750 }
2751
2752 void AnimationTrackEdit::set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only) {
2753 animation = p_animation;
2754 read_only = p_read_only;
2755
2756 track = p_track;
2757 queue_redraw();
2758
2759 ERR_FAIL_INDEX(track, animation->get_track_count());
2760
2761 node_path = animation->track_get_path(p_track);
2762 type_icon = _get_key_type_icon();
2763 selected_icon = get_editor_theme_icon(SNAME("KeySelected"));
2764 }
2765
2766 NodePath AnimationTrackEdit::get_path() const {
2767 return node_path;
2768 }
2769
2770 Size2 AnimationTrackEdit::get_minimum_size() const {
2771 Ref<Texture2D> texture = get_editor_theme_icon(SNAME("Object"));
2772 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
2773 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
2774 const int separation = get_theme_constant(SNAME("v_separation"), SNAME("ItemList"));
2775
2776 int max_h = MAX(texture->get_height(), font->get_height(font_size));
2777 max_h = MAX(max_h, get_key_height());
2778
2779 return Vector2(1, max_h + separation);
2780 }
2781
2782 void AnimationTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
2783 timeline = p_timeline;
2784 timeline->set_track_edit(this);
2785 timeline->connect("zoom_changed", callable_mp(this, &AnimationTrackEdit::_zoom_changed));
2786 timeline->connect("name_limit_changed", callable_mp(this, &AnimationTrackEdit::_zoom_changed));
2787 }
2788
2789 void AnimationTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
2790 editor = p_editor;
2791 }
2792
2793 void AnimationTrackEdit::_play_position_draw() {
2794 if (animation.is_null() || play_position_pos < 0) {
2795 return;
2796 }
2797
2798 float scale = timeline->get_zoom_scale();
2799 int h = get_size().height;
2800
2801 int px = (-timeline->get_value() + play_position_pos) * scale + timeline->get_name_limit();
2802
2803 if (px >= timeline->get_name_limit() && px < (get_size().width - timeline->get_buttons_width())) {
2804 Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
2805 play_position->draw_line(Point2(px, 0), Point2(px, h), color, Math::round(2 * EDSCALE));
2806 }
2807 }
2808
2809 void AnimationTrackEdit::set_play_position(float p_pos) {
2810 play_position_pos = p_pos;
2811 play_position->queue_redraw();
2812 }
2813
2814 void AnimationTrackEdit::update_play_position() {
2815 play_position->queue_redraw();
2816 }
2817
2818 void AnimationTrackEdit::set_root(Node *p_root) {
2819 root = p_root;
2820 }
2821
2822 void AnimationTrackEdit::_zoom_changed() {
2823 queue_redraw();
2824 play_position->queue_redraw();
2825 }
2826
2827 void AnimationTrackEdit::_path_submitted(const String &p_text) {
2828 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2829 undo_redo->create_action(TTR("Change Track Path"));
2830 undo_redo->add_do_method(animation.ptr(), "track_set_path", track, p_text);
2831 undo_redo->add_undo_method(animation.ptr(), "track_set_path", track, animation->track_get_path(track));
2832 undo_redo->commit_action();
2833 path_popup->hide();
2834 }
2835
2836 bool AnimationTrackEdit::_is_value_key_valid(const Variant &p_key_value, Variant::Type &r_valid_type) const {
2837 if (root == nullptr || !root->has_node_and_resource(animation->track_get_path(track))) {
2838 return false;
2839 }
2840 Ref<Resource> res;
2841 Vector<StringName> leftover_path;
2842 Node *node = root->get_node_and_resource(animation->track_get_path(track), res, leftover_path);
2843
2844 Object *obj = nullptr;
2845 if (res.is_valid()) {
2846 obj = res.ptr();
2847 } else if (node) {
2848 obj = node;
2849 }
2850
2851 bool prop_exists = false;
2852 if (obj) {
2853 r_valid_type = obj->get_static_property_type_indexed(leftover_path, &prop_exists);
2854 }
2855
2856 return (!prop_exists || Variant::can_convert(p_key_value.get_type(), r_valid_type));
2857 }
2858
2859 Ref<Texture2D> AnimationTrackEdit::_get_key_type_icon() const {
2860 const Ref<Texture2D> type_icons[9] = {
2861 get_editor_theme_icon(SNAME("KeyValue")),
2862 get_editor_theme_icon(SNAME("KeyTrackPosition")),
2863 get_editor_theme_icon(SNAME("KeyTrackRotation")),
2864 get_editor_theme_icon(SNAME("KeyTrackScale")),
2865 get_editor_theme_icon(SNAME("KeyTrackBlendShape")),
2866 get_editor_theme_icon(SNAME("KeyCall")),
2867 get_editor_theme_icon(SNAME("KeyBezier")),
2868 get_editor_theme_icon(SNAME("KeyAudio")),
2869 get_editor_theme_icon(SNAME("KeyAnimation"))
2870 };
2871 return type_icons[animation->track_get_type(track)];
2872 }
2873
2874 Control::CursorShape AnimationTrackEdit::get_cursor_shape(const Point2 &p_pos) const {
2875 if (command_or_control_pressed && animation->track_get_type(track) == Animation::TYPE_METHOD && hovering_key_idx != -1) {
2876 return Control::CURSOR_POINTING_HAND;
2877 }
2878 return get_default_cursor_shape();
2879 }
2880
2881 String AnimationTrackEdit::get_tooltip(const Point2 &p_pos) const {
2882 if (check_rect.has_point(p_pos)) {
2883 return TTR("Toggle this track on/off.");
2884 }
2885
2886 if (icon_rect.has_point(p_pos)) {
2887 return TTR("Select node in scene.");
2888 }
2889
2890 // Don't overlap track keys if they start at 0.
2891 if (path_rect.has_point(p_pos + Size2(type_icon->get_width(), 0))) {
2892 return String(animation->track_get_path(track));
2893 }
2894
2895 if (update_mode_rect.has_point(p_pos)) {
2896 if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
2897 return TTR("Use Blend");
2898 } else {
2899 return TTR("Update Mode (How this property is set)");
2900 }
2901 }
2902
2903 if (interp_mode_rect.has_point(p_pos)) {
2904 return TTR("Interpolation Mode");
2905 }
2906
2907 if (loop_wrap_rect.has_point(p_pos)) {
2908 return TTR("Loop Wrap Mode (Interpolate end with beginning on loop)");
2909 }
2910
2911 if (remove_rect.has_point(p_pos)) {
2912 return TTR("Remove this track.");
2913 }
2914
2915 int limit = timeline->get_name_limit();
2916 int limit_end = get_size().width - timeline->get_buttons_width();
2917 // Left Border including space occupied by keyframes on t=0.
2918 int limit_start_hitbox = limit - type_icon->get_width();
2919
2920 if (p_pos.x >= limit_start_hitbox && p_pos.x <= limit_end) {
2921 int key_idx = -1;
2922 float key_distance = 1e20;
2923
2924 // Select should happen in the opposite order of drawing for more accurate overlap select.
2925 for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) {
2926 Rect2 rect = const_cast<AnimationTrackEdit *>(this)->get_key_rect(i, timeline->get_zoom_scale());
2927 float offset = animation->track_get_key_time(track, i) - timeline->get_value();
2928 offset = offset * timeline->get_zoom_scale() + limit;
2929 rect.position.x += offset;
2930
2931 if (rect.has_point(p_pos)) {
2932 if (const_cast<AnimationTrackEdit *>(this)->is_key_selectable_by_distance()) {
2933 float distance = Math::abs(offset - p_pos.x);
2934 if (key_idx == -1 || distance < key_distance) {
2935 key_idx = i;
2936 key_distance = distance;
2937 }
2938 } else {
2939 // First one does it.
2940 break;
2941 }
2942 }
2943 }
2944
2945 if (key_idx != -1) {
2946 String text = TTR("Time (s):") + " " + TranslationServer::get_singleton()->format_number(rtos(Math::snapped(animation->track_get_key_time(track, key_idx), SECOND_DECIMAL)), _get_locale()) + "\n";
2947 switch (animation->track_get_type(track)) {
2948 case Animation::TYPE_POSITION_3D: {
2949 Vector3 t = animation->track_get_key_value(track, key_idx);
2950 text += TTR("Position:") + " " + String(t);
2951 } break;
2952 case Animation::TYPE_ROTATION_3D: {
2953 Quaternion t = animation->track_get_key_value(track, key_idx);
2954 text += TTR("Rotation:") + " " + String(t);
2955 } break;
2956 case Animation::TYPE_SCALE_3D: {
2957 Vector3 t = animation->track_get_key_value(track, key_idx);
2958 text += TTR("Scale:") + " " + String(t);
2959 } break;
2960 case Animation::TYPE_BLEND_SHAPE: {
2961 float t = animation->track_get_key_value(track, key_idx);
2962 text += TTR("Blend Shape:") + " " + itos(t);
2963 } break;
2964 case Animation::TYPE_VALUE: {
2965 const Variant &v = animation->track_get_key_value(track, key_idx);
2966 text += TTR("Type:") + " " + Variant::get_type_name(v.get_type()) + "\n";
2967 Variant::Type valid_type = Variant::NIL;
2968 text += TTR("Value:") + " " + String(v);
2969 if (!_is_value_key_valid(v, valid_type)) {
2970 text += " " + vformat(TTR("(Invalid, expected type: %s)"), Variant::get_type_name(valid_type));
2971 }
2972 text += "\n" + TTR("Easing:") + " " + rtos(animation->track_get_key_transition(track, key_idx));
2973
2974 } break;
2975 case Animation::TYPE_METHOD: {
2976 Dictionary d = animation->track_get_key_value(track, key_idx);
2977 if (d.has("method")) {
2978 text += String(d["method"]);
2979 }
2980 text += "(";
2981 Vector<Variant> args;
2982 if (d.has("args")) {
2983 args = d["args"];
2984 }
2985 for (int i = 0; i < args.size(); i++) {
2986 if (i > 0) {
2987 text += ", ";
2988 }
2989 text += args[i].get_construct_string();
2990 }
2991 text += ")";
2992
2993 } break;
2994 case Animation::TYPE_BEZIER: {
2995 float h = animation->bezier_track_get_key_value(track, key_idx);
2996 text += TTR("Value:") + " " + rtos(h) + "\n";
2997 Vector2 ih = animation->bezier_track_get_key_in_handle(track, key_idx);
2998 text += TTR("In-Handle:") + " " + String(ih) + "\n";
2999 Vector2 oh = animation->bezier_track_get_key_out_handle(track, key_idx);
3000 text += TTR("Out-Handle:") + " " + String(oh) + "\n";
3001
3002 String handle_mode;
3003 int hm = animation->bezier_track_get_key_handle_mode(track, key_idx);
3004 switch (hm) {
3005 case Animation::HANDLE_MODE_FREE: {
3006 handle_mode = TTR("Free", "Bezier Handle Mode");
3007 } break;
3008 case Animation::HANDLE_MODE_LINEAR: {
3009 handle_mode = TTR("Linear", "Bezier Handle Mode");
3010 } break;
3011 case Animation::HANDLE_MODE_BALANCED: {
3012 handle_mode = TTR("Balanced", "Bezier Handle Mode");
3013 } break;
3014 case Animation::HANDLE_MODE_MIRRORED: {
3015 handle_mode = TTR("Mirrored", "Bezier Handle Mode");
3016 } break;
3017 default: {
3018 // Unknown modes may occur when editing a file from a newer version of Godot.
3019 handle_mode = itos(hm);
3020 } break;
3021 }
3022 text += vformat(TTR("Handle mode: %s"), handle_mode);
3023 } break;
3024 case Animation::TYPE_AUDIO: {
3025 String stream_name = "null";
3026 Ref<Resource> stream = animation->audio_track_get_key_stream(track, key_idx);
3027 if (stream.is_valid()) {
3028 if (stream->get_path().is_resource_file()) {
3029 stream_name = stream->get_path().get_file();
3030 } else if (!stream->get_name().is_empty()) {
3031 stream_name = stream->get_name();
3032 } else {
3033 stream_name = stream->get_class();
3034 }
3035 }
3036
3037 text += TTR("Stream:") + " " + stream_name + "\n";
3038 float so = animation->audio_track_get_key_start_offset(track, key_idx);
3039 text += TTR("Start (s):") + " " + rtos(so) + "\n";
3040 float eo = animation->audio_track_get_key_end_offset(track, key_idx);
3041 text += TTR("End (s):") + " " + rtos(eo);
3042 } break;
3043 case Animation::TYPE_ANIMATION: {
3044 String name = animation->animation_track_get_key_animation(track, key_idx);
3045 text += TTR("Animation Clip:") + " " + name;
3046 } break;
3047 }
3048 return text;
3049 }
3050 }
3051
3052 return Control::get_tooltip(p_pos);
3053 }
3054
3055 void AnimationTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
3056 ERR_FAIL_COND(p_event.is_null());
3057
3058 if (p_event->is_pressed()) {
3059 if (ED_IS_SHORTCUT("animation_editor/duplicate_selected_keys", p_event)) {
3060 if (!read_only) {
3061 emit_signal(SNAME("duplicate_request"), -1.0, false);
3062 }
3063 accept_event();
3064 }
3065 if (ED_IS_SHORTCUT("animation_editor/cut_selected_keys", p_event)) {
3066 if (!read_only) {
3067 emit_signal(SNAME("cut_request"));
3068 }
3069 accept_event();
3070 }
3071 if (ED_IS_SHORTCUT("animation_editor/copy_selected_keys", p_event)) {
3072 if (!read_only) {
3073 emit_signal(SNAME("copy_request"));
3074 }
3075 accept_event();
3076 }
3077
3078 if (ED_IS_SHORTCUT("animation_editor/paste_keys", p_event)) {
3079 if (!read_only) {
3080 emit_signal(SNAME("paste_request"), -1.0, false);
3081 }
3082 accept_event();
3083 }
3084
3085 if (ED_IS_SHORTCUT("animation_editor/delete_selection", p_event)) {
3086 if (!read_only) {
3087 emit_signal(SNAME("delete_request"));
3088 }
3089 accept_event();
3090 }
3091 }
3092
3093 Ref<InputEventMouseButton> mb = p_event;
3094 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
3095 Point2 pos = mb->get_position();
3096 bool no_mod_key_pressed = !mb->is_alt_pressed() && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed();
3097 if (mb->is_double_click() && !moving_selection && no_mod_key_pressed) {
3098 int x = pos.x - timeline->get_name_limit();
3099 float ofs = x / timeline->get_zoom_scale() + timeline->get_value();
3100 emit_signal(SNAME("timeline_changed"), ofs, false);
3101 }
3102
3103 if (!read_only) {
3104 if (check_rect.has_point(pos)) {
3105 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
3106 undo_redo->create_action(TTR("Toggle Track Enabled"));
3107 undo_redo->add_do_method(animation.ptr(), "track_set_enabled", track, !animation->track_is_enabled(track));
3108 undo_redo->add_undo_method(animation.ptr(), "track_set_enabled", track, animation->track_is_enabled(track));
3109 undo_redo->commit_action();
3110 queue_redraw();
3111 accept_event();
3112 }
3113
3114 if (icon_rect.has_point(pos)) {
3115 EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
3116 editor_selection->clear();
3117 Node *n = root->get_node_or_null(node_path);
3118 if (n) {
3119 editor_selection->add_node(n);
3120 }
3121 }
3122
3123 // Don't overlap track keys if they start at 0.
3124 if (path_rect.has_point(pos + Size2(type_icon->get_width(), 0))) {
3125 clicking_on_name = true;
3126 accept_event();
3127 }
3128
3129 if (update_mode_rect.has_point(pos)) {
3130 if (!menu) {
3131 menu = memnew(PopupMenu);
3132 add_child(menu);
3133 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationTrackEdit::_menu_selected));
3134 }
3135 menu->clear();
3136 if (animation->track_get_type(track) == Animation::TYPE_AUDIO) {
3137 menu->add_icon_item(get_editor_theme_icon(SNAME("UseBlendEnable")), TTR("Use Blend"), MENU_USE_BLEND_ENABLED);
3138 menu->add_icon_item(get_editor_theme_icon(SNAME("UseBlendDisable")), TTR("Don't Use Blend"), MENU_USE_BLEND_DISABLED);
3139 } else {
3140 menu->add_icon_item(get_editor_theme_icon(SNAME("TrackContinuous")), TTR("Continuous"), MENU_CALL_MODE_CONTINUOUS);
3141 menu->add_icon_item(get_editor_theme_icon(SNAME("TrackDiscrete")), TTR("Discrete"), MENU_CALL_MODE_DISCRETE);
3142 menu->add_icon_item(get_editor_theme_icon(SNAME("TrackCapture")), TTR("Capture"), MENU_CALL_MODE_CAPTURE);
3143 }
3144 menu->reset_size();
3145
3146 moving_selection_attempt = false;
3147 moving_selection = false;
3148
3149 Vector2 popup_pos = get_screen_position() + update_mode_rect.position + Vector2(0, update_mode_rect.size.height);
3150 menu->set_position(popup_pos);
3151 menu->popup();
3152 accept_event();
3153 }
3154
3155 if (interp_mode_rect.has_point(pos)) {
3156 if (!menu) {
3157 menu = memnew(PopupMenu);
3158 add_child(menu);
3159 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationTrackEdit::_menu_selected));
3160 }
3161 menu->clear();
3162 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpRaw")), TTR("Nearest"), MENU_INTERPOLATION_NEAREST);
3163 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpLinear")), TTR("Linear"), MENU_INTERPOLATION_LINEAR);
3164 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpCubic")), TTR("Cubic"), MENU_INTERPOLATION_CUBIC);
3165 // Check whether it is angle property.
3166 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
3167 if (ape) {
3168 AnimationPlayer *ap = ape->get_player();
3169 if (ap) {
3170 NodePath npath = animation->track_get_path(track);
3171 Node *a_ap_root_node = ap->get_node_or_null(ap->get_root_node());
3172 Node *nd = nullptr;
3173 // We must test that we have a valid a_ap_root_node before trying to access its content to init the nd Node.
3174 if (a_ap_root_node) {
3175 nd = a_ap_root_node->get_node_or_null(NodePath(npath.get_concatenated_names()));
3176 }
3177 if (nd) {
3178 StringName prop = npath.get_concatenated_subnames();
3179 PropertyInfo prop_info;
3180 ClassDB::get_property_info(nd->get_class(), prop, &prop_info);
3181 #ifdef DISABLE_DEPRECATED
3182 bool is_angle = prop_info.type == Variant::FLOAT && prop_info.hint_string.contains("radians_as_degrees");
3183 #else
3184 bool is_angle = prop_info.type == Variant::FLOAT && prop_info.hint_string.contains("radians");
3185 #endif // DISABLE_DEPRECATED
3186 if (is_angle) {
3187 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpLinearAngle")), TTR("Linear Angle"), MENU_INTERPOLATION_LINEAR_ANGLE);
3188 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpCubicAngle")), TTR("Cubic Angle"), MENU_INTERPOLATION_CUBIC_ANGLE);
3189 }
3190 }
3191 }
3192 }
3193 menu->reset_size();
3194
3195 moving_selection_attempt = false;
3196 moving_selection = false;
3197
3198 Vector2 popup_pos = get_screen_position() + interp_mode_rect.position + Vector2(0, interp_mode_rect.size.height);
3199 menu->set_position(popup_pos);
3200 menu->popup();
3201 accept_event();
3202 }
3203
3204 if (loop_wrap_rect.has_point(pos)) {
3205 if (!menu) {
3206 menu = memnew(PopupMenu);
3207 add_child(menu);
3208 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationTrackEdit::_menu_selected));
3209 }
3210 menu->clear();
3211 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpWrapClamp")), TTR("Clamp Loop Interp"), MENU_LOOP_CLAMP);
3212 menu->add_icon_item(get_editor_theme_icon(SNAME("InterpWrapLoop")), TTR("Wrap Loop Interp"), MENU_LOOP_WRAP);
3213 menu->reset_size();
3214
3215 moving_selection_attempt = false;
3216 moving_selection = false;
3217
3218 Vector2 popup_pos = get_screen_position() + loop_wrap_rect.position + Vector2(0, loop_wrap_rect.size.height);
3219 menu->set_position(popup_pos);
3220 menu->popup();
3221 accept_event();
3222 }
3223
3224 if (remove_rect.has_point(pos)) {
3225 emit_signal(SNAME("remove_request"), track);
3226 accept_event();
3227 return;
3228 }
3229 }
3230
3231 if (mb->is_command_or_control_pressed() && _lookup_key(hovering_key_idx)) {
3232 accept_event();
3233 return;
3234 }
3235
3236 if (_try_select_at_ui_pos(pos, mb->is_command_or_control_pressed() || mb->is_shift_pressed(), true)) {
3237 accept_event();
3238 }
3239 }
3240
3241 if (!moving_selection && mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
3242 Point2 pos = mb->get_position();
3243 if (pos.x >= timeline->get_name_limit() && pos.x <= get_size().width - timeline->get_buttons_width()) {
3244 // Can do something with menu too! show insert key.
3245 float offset = (pos.x - timeline->get_name_limit()) / timeline->get_zoom_scale();
3246 if (!read_only) {
3247 if (!menu) {
3248 menu = memnew(PopupMenu);
3249 add_child(menu);
3250 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationTrackEdit::_menu_selected));
3251 }
3252
3253 bool selected = _try_select_at_ui_pos(pos, mb->is_command_or_control_pressed() || mb->is_shift_pressed(), false);
3254 moving_selection_attempt = false;
3255
3256 menu->clear();
3257 if (animation->track_get_type(track) == Animation::TYPE_METHOD) {
3258 if (hovering_key_idx != -1) {
3259 lookup_key_idx = hovering_key_idx;
3260 menu->add_icon_item(get_editor_theme_icon(SNAME("Help")), vformat("%s (%s)", TTR("Go to Definition"), animation->method_track_get_name(track, lookup_key_idx)), MENU_KEY_LOOKUP);
3261 menu->add_separator();
3262 }
3263 }
3264 menu->add_icon_item(get_editor_theme_icon(SNAME("Key")), TTR("Insert Key..."), MENU_KEY_INSERT);
3265 if (selected || editor->is_selection_active()) {
3266 menu->add_separator();
3267 menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Duplicate Key(s)"), MENU_KEY_DUPLICATE);
3268 menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCut")), TTR("Cut Key(s)"), MENU_KEY_CUT);
3269 menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCopy")), TTR("Copy Key(s)"), MENU_KEY_COPY);
3270 }
3271 if (editor->is_key_clipboard_active()) {
3272 menu->add_icon_item(get_editor_theme_icon(SNAME("ActionPaste")), TTR("Paste Key(s)"), MENU_KEY_PASTE);
3273 }
3274 if (selected || editor->is_selection_active()) {
3275 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
3276 if (ape) {
3277 AnimationPlayer *ap = ape->get_player();
3278 if (ap && editor->can_add_reset_key() && animation != ap->get_animation(SceneStringName(RESET))) {
3279 menu->add_separator();
3280 menu->add_icon_item(get_editor_theme_icon(SNAME("MoveUp")), TTR("Send Key(s) to RESET"), MENU_KEY_ADD_RESET);
3281 }
3282 }
3283 menu->add_separator();
3284 menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete Key(s)"), MENU_KEY_DELETE);
3285 }
3286 menu->reset_size();
3287
3288 menu->set_position(get_screen_position() + get_local_mouse_position());
3289 menu->popup();
3290
3291 insert_at_pos = offset + timeline->get_value();
3292 accept_event();
3293 }
3294 }
3295 }
3296
3297 if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && clicking_on_name) {
3298 if (!path) {
3299 path_popup = memnew(Popup);
3300 path_popup->set_wrap_controls(true);
3301 add_child(path_popup);
3302 path = memnew(LineEdit);
3303 path_popup->add_child(path);
3304 path->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
3305 path->connect(SceneStringName(text_submitted), callable_mp(this, &AnimationTrackEdit::_path_submitted));
3306 }
3307
3308 path->set_text(String(animation->track_get_path(track)));
3309 const Vector2 theme_ofs = path->get_theme_stylebox(CoreStringName(normal), SNAME("LineEdit"))->get_offset();
3310
3311 moving_selection_attempt = false;
3312 moving_selection = false;
3313
3314 path_popup->set_position(get_screen_position() + path_rect.position - theme_ofs);
3315 path_popup->set_size(path_rect.size);
3316 path_popup->popup();
3317 path->grab_focus();
3318 path->set_caret_column(path->get_text().length());
3319 clicking_on_name = false;
3320 }
3321
3322 if (mb.is_valid() && moving_selection_attempt) {
3323 if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
3324 moving_selection_attempt = false;
3325 if (moving_selection && moving_selection_effective) {
3326 if (std::abs(editor->get_moving_selection_offset()) > CMP_EPSILON) {
3327 emit_signal(SNAME("move_selection_commit"));
3328 }
3329 } else if (select_single_attempt != -1) {
3330 emit_signal(SNAME("select_key"), select_single_attempt, true);
3331 }
3332 moving_selection = false;
3333 select_single_attempt = -1;
3334 }
3335
3336 if (moving_selection && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
3337 moving_selection_attempt = false;
3338 moving_selection = false;
3339 emit_signal(SNAME("move_selection_cancel"));
3340 }
3341 }
3342
3343 Ref<InputEventMouseMotion> mm = p_event;
3344 if (mm.is_valid()) {
3345 const int previous_hovering_key_idx = hovering_key_idx;
3346
3347 command_or_control_pressed = mm->is_command_or_control_pressed();
3348
3349 // Hovering compressed keyframes for editing is not possible.
3350 if (!animation->track_is_compressed(track)) {
3351 const float scale = timeline->get_zoom_scale();
3352 const int limit = timeline->get_name_limit();
3353 const int limit_end = get_size().width - timeline->get_buttons_width();
3354 // Left Border including space occupied by keyframes on t=0.
3355 const int limit_start_hitbox = limit - type_icon->get_width();
3356 const Point2 pos = mm->get_position();
3357
3358 if (pos.x >= limit_start_hitbox && pos.x <= limit_end) {
3359 // Use the same logic as key selection to ensure that hovering accurately represents
3360 // which key will be selected when clicking.
3361 int key_idx = -1;
3362 float key_distance = 1e20;
3363
3364 hovering_key_idx = -1;
3365
3366 // Hovering should happen in the opposite order of drawing for more accurate overlap hovering.
3367 for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) {
3368 Rect2 rect = get_key_rect(i, scale);
3369 float offset = animation->track_get_key_time(track, i) - timeline->get_value();
3370 offset = offset * scale + limit;
3371 rect.position.x += offset;
3372
3373 if (rect.has_point(pos)) {
3374 if (is_key_selectable_by_distance()) {
3375 const float distance = Math::abs(offset - pos.x);
3376 if (key_idx == -1 || distance < key_distance) {
3377 key_idx = i;
3378 key_distance = distance;
3379 hovering_key_idx = i;
3380 }
3381 } else {
3382 // First one does it.
3383 hovering_key_idx = i;
3384 break;
3385 }
3386 }
3387 }
3388
3389 if (hovering_key_idx != previous_hovering_key_idx) {
3390 // Required to draw keyframe hover feedback on the correct keyframe.
3391 queue_redraw();
3392 }
3393 }
3394 }
3395 }
3396
3397 if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::LEFT) && moving_selection_attempt) {
3398 if (!moving_selection) {
3399 moving_selection = true;
3400 emit_signal(SNAME("move_selection_begin"));
3401 }
3402
3403 float moving_begin_time = ((moving_selection_mouse_begin_x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
3404 float new_time = ((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
3405 float delta = new_time - moving_begin_time;
3406 float snapped_time = editor->snap_time(moving_selection_pivot + delta);
3407
3408 float offset = 0.0;
3409 if (std::abs(editor->get_moving_selection_offset()) > CMP_EPSILON || (snapped_time > moving_selection_pivot && delta > CMP_EPSILON) || (snapped_time < moving_selection_pivot && delta < -CMP_EPSILON)) {
3410 offset = snapped_time - moving_selection_pivot;
3411 moving_selection_effective = true;
3412 }
3413
3414 emit_signal(SNAME("move_selection"), offset);
3415 }
3416 }
3417
3418 bool AnimationTrackEdit::_try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable) {
3419 if (!animation->track_is_compressed(track)) { // Selecting compressed keyframes for editing is not possible.
3420 float scale = timeline->get_zoom_scale();
3421 int limit = timeline->get_name_limit();
3422 int limit_end = get_size().width - timeline->get_buttons_width();
3423 // Left Border including space occupied by keyframes on t=0.
3424 int limit_start_hitbox = limit - type_icon->get_width();
3425
3426 if (p_pos.x >= limit_start_hitbox && p_pos.x <= limit_end) {
3427 int key_idx = -1;
3428 float key_distance = 1e20;
3429
3430 // Select should happen in the opposite order of drawing for more accurate overlap select.
3431 for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) {
3432 Rect2 rect = get_key_rect(i, scale);
3433 float offset = animation->track_get_key_time(track, i) - timeline->get_value();
3434 offset = offset * scale + limit;
3435 rect.position.x += offset;
3436
3437 if (rect.has_point(p_pos)) {
3438 if (is_key_selectable_by_distance()) {
3439 float distance = Math::abs(offset - p_pos.x);
3440 if (key_idx == -1 || distance < key_distance) {
3441 key_idx = i;
3442 key_distance = distance;
3443 }
3444 } else {
3445 // First one does it.
3446 key_idx = i;
3447 break;
3448 }
3449 }
3450 }
3451
3452 if (key_idx != -1) {
3453 if (p_aggregate) {
3454 if (editor->is_key_selected(track, key_idx)) {
3455 if (p_deselectable) {
3456 emit_signal(SNAME("deselect_key"), key_idx);
3457 moving_selection_pivot = 0.0f;
3458 moving_selection_mouse_begin_x = 0.0f;
3459 }
3460 } else {
3461 emit_signal(SNAME("select_key"), key_idx, false);
3462 moving_selection_attempt = true;
3463 moving_selection_effective = false;
3464 select_single_attempt = -1;
3465 moving_selection_pivot = animation->track_get_key_time(track, key_idx);
3466 moving_selection_mouse_begin_x = p_pos.x;
3467 }
3468 } else {
3469 if (!editor->is_key_selected(track, key_idx)) {
3470 emit_signal(SNAME("select_key"), key_idx, true);
3471 select_single_attempt = -1;
3472 } else {
3473 select_single_attempt = key_idx;
3474 }
3475
3476 moving_selection_attempt = true;
3477 moving_selection_effective = false;
3478 moving_selection_pivot = animation->track_get_key_time(track, key_idx);
3479 moving_selection_mouse_begin_x = p_pos.x;
3480 }
3481
3482 if (read_only) {
3483 moving_selection_attempt = false;
3484 moving_selection_pivot = 0.0f;
3485 moving_selection_mouse_begin_x = 0.0f;
3486 }
3487 return true;
3488 }
3489 }
3490 }
3491 return false;
3492 }
3493
3494 bool AnimationTrackEdit::_lookup_key(int p_key_idx) const {
3495 if (p_key_idx < 0 || p_key_idx >= animation->track_get_key_count(track)) {
3496 return false;
3497 }
3498
3499 if (animation->track_get_type(track) == Animation::TYPE_METHOD) {
3500 Node *target = root->get_node_or_null(animation->track_get_path(track));
3501 if (target) {
3502 StringName method = animation->method_track_get_name(track, p_key_idx);
3503 // First, check every script in the inheritance chain.
3504 bool found_in_script = false;
3505 Ref<Script> target_script_ref = target->get_script();
3506 Script *target_script = target_script_ref.ptr();
3507 while (target_script) {
3508 if (target_script->has_method(method)) {
3509 found_in_script = true;
3510 // Tell ScriptEditor to show the method's line.
3511 ScriptEditor::get_singleton()->script_goto_method(target_script, animation->method_track_get_name(track, p_key_idx));
3512 break;
3513 }
3514 target_script = target_script->get_base_script().ptr();
3515 }
3516
3517 if (!found_in_script) {
3518 // Not found in script, so it must be a native method.
3519 if (ClassDB::has_method(target->get_class_name(), method)) {
3520 // Show help page instead.
3521 ScriptEditor::get_singleton()->goto_help(vformat("class_method:%s:%s", target->get_class_name(), method));
3522 } else {
3523 // Still not found, which means the target doesn't have this method. Warn the user.
3524 WARN_PRINT_ED(TTR(vformat("Failed to lookup method: \"%s\"", method)));
3525 }
3526 }
3527 return true;
3528 }
3529 }
3530 return false;
3531 }
3532
3533 Variant AnimationTrackEdit::get_drag_data(const Point2 &p_point) {
3534 if (!clicking_on_name || (get_editor()->is_sorting_alphabetically() && !get_editor()->is_grouping_tracks())) {
3535 return Variant();
3536 }
3537
3538 Dictionary drag_data;
3539 drag_data["type"] = "animation_track";
3540 String base_path = String(animation->track_get_path(track));
3541 base_path = base_path.get_slicec(':', 0); // Remove sub-path.
3542 drag_data["group"] = base_path;
3543 drag_data["index"] = track;
3544
3545 Button *tb = memnew(Button);
3546 tb->set_flat(true);
3547 tb->set_text(path_cache);
3548 tb->set_button_icon(icon_cache);
3549 tb->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
3550 tb->add_theme_constant_override("icon_max_width", get_theme_constant("class_icon_size", EditorStringName(Editor)));
3551 set_drag_preview(tb);
3552
3553 clicking_on_name = false;
3554
3555 return drag_data;
3556 }
3557
3558 bool AnimationTrackEdit::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
3559 Dictionary d = p_data;
3560 if (!d.has("type")) {
3561 return false;
3562 }
3563
3564 String type = d["type"];
3565 if (type != "animation_track") {
3566 return false;
3567 }
3568
3569 // Don't allow moving tracks outside their groups.
3570 if (get_editor()->is_grouping_tracks()) {
3571 String base_path = String(animation->track_get_path(track));
3572 base_path = base_path.get_slicec(':', 0); // Remove sub-path.
3573 if (d["group"] != base_path) {
3574 return false;
3575 }
3576 }
3577
3578 if (p_point.y < get_size().height / 2) {
3579 dropping_at = -1;
3580 } else {
3581 dropping_at = 1;
3582 }
3583
3584 const_cast<AnimationTrackEdit *>(this)->queue_redraw();
3585
3586 return true;
3587 }
3588
3589 void AnimationTrackEdit::drop_data(const Point2 &p_point, const Variant &p_data) {
3590 Dictionary d = p_data;
3591 if (!d.has("type")) {
3592 return;
3593 }
3594
3595 String type = d["type"];
3596 if (type != "animation_track") {
3597 return;
3598 }
3599
3600 // Don't allow moving tracks outside their groups.
3601 if (get_editor()->is_grouping_tracks()) {
3602 String base_path = String(animation->track_get_path(track));
3603 base_path = base_path.get_slicec(':', 0); // Remove sub-path.
3604 if (d["group"] != base_path) {
3605 return;
3606 }
3607 }
3608
3609 int from_track = d["index"];
3610
3611 if (dropping_at < 0) {
3612 emit_signal(SNAME("dropped"), from_track, track);
3613 } else {
3614 emit_signal(SNAME("dropped"), from_track, track + 1);
3615 }
3616 }
3617
3618 void AnimationTrackEdit::_menu_selected(int p_index) {
3619 switch (p_index) {
3620 case MENU_CALL_MODE_CONTINUOUS:
3621 case MENU_CALL_MODE_DISCRETE:
3622 case MENU_CALL_MODE_CAPTURE: {
3623 Animation::UpdateMode update_mode = Animation::UpdateMode(p_index);
3624 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
3625 undo_redo->create_action(TTR("Change Animation Update Mode"));
3626 undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", track, update_mode);
3627 undo_redo->add_undo_method(animation.ptr(), "value_track_set_update_mode", track, animation->value_track_get_update_mode(track));
3628 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
3629 if (ape) {
3630 undo_redo->add_do_method(ape, "_animation_update_key_frame");
3631 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
3632 }
3633 undo_redo->commit_action();
3634 queue_redraw();
3635
3636 } break;
3637 case MENU_INTERPOLATION_NEAREST:
3638 case MENU_INTERPOLATION_LINEAR:
3639 case MENU_INTERPOLATION_CUBIC:
3640 case MENU_INTERPOLATION_LINEAR_ANGLE:
3641 case MENU_INTERPOLATION_CUBIC_ANGLE: {
3642 Animation::InterpolationType interp_mode = Animation::InterpolationType(p_index - MENU_INTERPOLATION_NEAREST);
3643 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
3644 undo_redo->create_action(TTR("Change Animation Interpolation Mode"));
3645 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", track, interp_mode);
3646 undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", track, animation->track_get_interpolation_type(track));
3647 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
3648 if (ape) {
3649 undo_redo->add_do_method(ape, "_animation_update_key_frame");
3650 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
3651 }
3652 undo_redo->commit_action();
3653 queue_redraw();
3654 } break;
3655 case MENU_LOOP_WRAP:
3656 case MENU_LOOP_CLAMP: {
3657 bool loop_wrap = p_index == MENU_LOOP_WRAP;
3658 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
3659 undo_redo->create_action(TTR("Change Animation Loop Mode"));
3660 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", track, loop_wrap);
3661 undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_loop_wrap", track, animation->track_get_interpolation_loop_wrap(track));
3662 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
3663 if (ape) {
3664 undo_redo->add_do_method(ape, "_animation_update_key_frame");
3665 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
3666 }
3667 undo_redo->commit_action();
3668 queue_redraw();
3669
3670 } break;
3671 case MENU_KEY_INSERT: {
3672 emit_signal(SNAME("insert_key"), insert_at_pos);
3673 } break;
3674 case MENU_KEY_DUPLICATE: {
3675 emit_signal(SNAME("duplicate_request"), insert_at_pos, !editor->is_insert_at_current_time_enabled());
3676 } break;
3677 case MENU_KEY_CUT: {
3678 emit_signal(SNAME("cut_request"));
3679 } break;
3680 case MENU_KEY_COPY: {
3681 emit_signal(SNAME("copy_request"));
3682 } break;
3683 case MENU_KEY_PASTE: {
3684 emit_signal(SNAME("paste_request"), insert_at_pos, !editor->is_insert_at_current_time_enabled());
3685 } break;
3686 case MENU_KEY_ADD_RESET: {
3687 emit_signal(SNAME("create_reset_request"));
3688 } break;
3689 case MENU_KEY_DELETE: {
3690 emit_signal(SNAME("delete_request"));
3691 } break;
3692 case MENU_KEY_LOOKUP: {
3693 _lookup_key(lookup_key_idx);
3694 } break;
3695 case MENU_USE_BLEND_ENABLED:
3696 case MENU_USE_BLEND_DISABLED: {
3697 bool use_blend = p_index == MENU_USE_BLEND_ENABLED;
3698 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
3699 undo_redo->create_action(TTR("Change Animation Use Blend"));
3700 undo_redo->add_do_method(animation.ptr(), "audio_track_set_use_blend", track, use_blend);
3701 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_use_blend", track, animation->audio_track_is_use_blend(track));
3702 undo_redo->commit_action();
3703 queue_redraw();
3704 } break;
3705 }
3706 }
3707
3708 void AnimationTrackEdit::cancel_drop() {
3709 if (dropping_at != 0) {
3710 dropping_at = 0;
3711 queue_redraw();
3712 }
3713 }
3714
3715 void AnimationTrackEdit::set_in_group(bool p_enable) {
3716 in_group = p_enable;
3717 queue_redraw();
3718 }
3719
3720 void AnimationTrackEdit::append_to_selection(const Rect2 &p_box, bool p_deselection) {
3721 if (animation->track_is_compressed(track)) {
3722 return; // Compressed keyframes can't be edited
3723 }
3724 // Left Border including space occupied by keyframes on t=0.
3725 int limit_start_hitbox = timeline->get_name_limit() - type_icon->get_width();
3726 Rect2 select_rect(limit_start_hitbox, 0, get_size().width - timeline->get_name_limit() - timeline->get_buttons_width(), get_size().height);
3727 select_rect = select_rect.intersection(p_box);
3728
3729 // Select should happen in the opposite order of drawing for more accurate overlap select.
3730 for (int i = animation->track_get_key_count(track) - 1; i >= 0; i--) {
3731 Rect2 rect = const_cast<AnimationTrackEdit *>(this)->get_key_rect(i, timeline->get_zoom_scale());
3732 float offset = animation->track_get_key_time(track, i) - timeline->get_value();
3733 offset = offset * timeline->get_zoom_scale() + timeline->get_name_limit();
3734 rect.position.x += offset;
3735
3736 if (select_rect.intersects(rect)) {
3737 if (p_deselection) {
3738 emit_signal(SNAME("deselect_key"), i);
3739 } else {
3740 emit_signal(SNAME("select_key"), i, false);
3741 }
3742 }
3743 }
3744 }
3745
3746 void AnimationTrackEdit::_bind_methods() {
3747 ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::FLOAT, "position"), PropertyInfo(Variant::BOOL, "timeline_only")));
3748 ADD_SIGNAL(MethodInfo("remove_request", PropertyInfo(Variant::INT, "track")));
3749 ADD_SIGNAL(MethodInfo("dropped", PropertyInfo(Variant::INT, "from_track"), PropertyInfo(Variant::INT, "to_track")));
3750 ADD_SIGNAL(MethodInfo("insert_key", PropertyInfo(Variant::FLOAT, "offset")));
3751 ADD_SIGNAL(MethodInfo("select_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "single")));
3752 ADD_SIGNAL(MethodInfo("deselect_key", PropertyInfo(Variant::INT, "index")));
3753
3754 ADD_SIGNAL(MethodInfo("move_selection_begin"));
3755 ADD_SIGNAL(MethodInfo("move_selection", PropertyInfo(Variant::FLOAT, "offset")));
3756 ADD_SIGNAL(MethodInfo("move_selection_commit"));
3757 ADD_SIGNAL(MethodInfo("move_selection_cancel"));
3758
3759 ADD_SIGNAL(MethodInfo("duplicate_request", PropertyInfo(Variant::FLOAT, "offset"), PropertyInfo(Variant::BOOL, "is_offset_valid")));
3760 ADD_SIGNAL(MethodInfo("create_reset_request"));
3761 ADD_SIGNAL(MethodInfo("copy_request"));
3762 ADD_SIGNAL(MethodInfo("cut_request"));
3763 ADD_SIGNAL(MethodInfo("paste_request", PropertyInfo(Variant::FLOAT, "offset"), PropertyInfo(Variant::BOOL, "is_offset_valid")));
3764 ADD_SIGNAL(MethodInfo("delete_request"));
3765 }
3766
3767 AnimationTrackEdit::AnimationTrackEdit() {
3768 play_position = memnew(Control);
3769 play_position->set_mouse_filter(MOUSE_FILTER_PASS);
3770 add_child(play_position);
3771 play_position->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
3772 play_position->connect(SceneStringName(draw), callable_mp(this, &AnimationTrackEdit::_play_position_draw));
3773 set_focus_mode(FOCUS_CLICK);
3774 set_mouse_filter(MOUSE_FILTER_PASS); // Scroll has to work too for selection.
3775 }
3776
3777 //////////////////////////////////////
3778
3779 AnimationTrackEdit *AnimationTrackEditPlugin::create_value_track_edit(Object *p_object, Variant::Type p_type, const String &p_property, PropertyHint p_hint, const String &p_hint_string, int p_usage) {
3780 if (get_script_instance()) {
3781 return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_value_track_edit", p_object, p_type, p_property, p_hint, p_hint_string, p_usage));
3782 }
3783 return nullptr;
3784 }
3785
3786 AnimationTrackEdit *AnimationTrackEditPlugin::create_audio_track_edit() {
3787 if (get_script_instance()) {
3788 return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_audio_track_edit").operator Object *());
3789 }
3790 return nullptr;
3791 }
3792
3793 AnimationTrackEdit *AnimationTrackEditPlugin::create_animation_track_edit(Object *p_object) {
3794 if (get_script_instance()) {
3795 return Object::cast_to<AnimationTrackEdit>(get_script_instance()->call("create_animation_track_edit", p_object).operator Object *());
3796 }
3797 return nullptr;
3798 }
3799
3800 ///////////////////////////////////////
3801
3802 void AnimationTrackEditGroup::_notification(int p_what) {
3803 switch (p_what) {
3804 case NOTIFICATION_THEME_CHANGED: {
3805 icon_size = Vector2(1, 1) * get_theme_constant(SNAME("class_icon_size"), EditorStringName(Editor));
3806 } break;
3807
3808 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
3809 RID ae = get_accessibility_element();
3810 ERR_FAIL_COND(ae.is_null());
3811
3812 //TODO
3813 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
3814 DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Animation track group")));
3815 } break;
3816
3817 case NOTIFICATION_DRAW: {
3818 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
3819 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
3820 Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
3821
3822 const Ref<StyleBox> &stylebox_header = get_theme_stylebox(SNAME("header"), SNAME("AnimationTrackEditGroup"));
3823 float v_margin_offset = stylebox_header->get_content_margin(SIDE_TOP) - stylebox_header->get_content_margin(SIDE_BOTTOM);
3824
3825 const Color h_line_color = get_theme_color(SNAME("h_line_color"), SNAME("AnimationTrackEditGroup"));
3826 const Color v_line_color = get_theme_color(SNAME("v_line_color"), SNAME("AnimationTrackEditGroup"));
3827 const int h_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationTrackEditGroup"));
3828
3829 const Ref<StyleBox> &stylebox_hover = get_theme_stylebox(SceneStringName(hover), SNAME("AnimationTrackEditGroup"));
3830
3831 if (root) {
3832 Node *n = root->get_node_or_null(node);
3833 if (n && EditorNode::get_singleton()->get_editor_selection()->is_selected(n)) {
3834 color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
3835 }
3836 }
3837
3838 draw_style_box(stylebox_header, Rect2(Point2(), get_size()));
3839
3840 if (hovered) {
3841 // Draw hover feedback for AnimationTrackEditGroup.
3842 // Add a limit to just show hover over portion with text.
3843 int limit = timeline->get_name_limit();
3844 draw_style_box(stylebox_hover, Rect2(Point2(1 * EDSCALE, 0), Size2(limit - 1 * EDSCALE, get_size().height)));
3845 }
3846
3847 int limit = timeline->get_name_limit();
3848 int limit_end = get_size().width - timeline->get_buttons_width();
3849
3850 // Unavailable timeline.
3851
3852 {
3853 int px = (editor->get_current_animation()->get_length() - timeline->get_value()) * timeline->get_zoom_scale() + timeline->get_name_limit();
3854 px = MAX(px, timeline->get_name_limit());
3855 Rect2 rect = Rect2(px, 0, limit_end - px, get_size().height);
3856 if (rect.size.width > 0) {
3857 draw_rect(rect, Color(0, 0, 0, 0.2));
3858 }
3859 }
3860
3861 // Section preview.
3862
3863 {
3864 float scale = timeline->get_zoom_scale();
3865
3866 PackedStringArray section = editor->get_selected_section();
3867 if (section.size() == 2) {
3868 StringName start_marker = section[0];
3869 StringName end_marker = section[1];
3870 double start_time = editor->get_current_animation()->get_marker_time(start_marker);
3871 double end_time = editor->get_current_animation()->get_marker_time(end_marker);
3872
3873 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
3874 // When AnimationPlayer is playing, don't move the preview rect, so it still indicates the playback section.
3875 if (editor->is_marker_moving_selection() && !(player && player->is_playing())) {
3876 start_time += editor->get_marker_moving_selection_offset();
3877 end_time += editor->get_marker_moving_selection_offset();
3878 }
3879
3880 if (start_time < editor->get_current_animation()->get_length() && end_time >= 0) {
3881 float start_ofs = MAX(0, start_time) - timeline->get_value();
3882 float end_ofs = MIN(editor->get_current_animation()->get_length(), end_time) - timeline->get_value();
3883 start_ofs = start_ofs * scale + limit;
3884 end_ofs = end_ofs * scale + limit;
3885 start_ofs = MAX(start_ofs, limit);
3886 end_ofs = MIN(end_ofs, limit_end);
3887 Rect2 rect;
3888 rect.set_position(Vector2(start_ofs, 0));
3889 rect.set_size(Vector2(end_ofs - start_ofs, get_size().height));
3890
3891 draw_rect(rect, Color(1, 0.1, 0.1, 0.2));
3892 }
3893 }
3894 }
3895
3896 // Marker overlays.
3897
3898 {
3899 float scale = timeline->get_zoom_scale();
3900 PackedStringArray markers = editor->get_current_animation()->get_marker_names();
3901 for (const StringName marker : markers) {
3902 double time = editor->get_current_animation()->get_marker_time(marker);
3903 if (editor->is_marker_selected(marker) && editor->is_marker_moving_selection()) {
3904 time += editor->get_marker_moving_selection_offset();
3905 }
3906 if (time >= 0) {
3907 float offset = time - timeline->get_value();
3908 offset = offset * scale + limit;
3909 if (offset >= timeline->get_name_limit() && offset < limit_end) {
3910 Color marker_color = editor->get_current_animation()->get_marker_color(marker);
3911 marker_color.a = 0.2;
3912 draw_line(Point2(offset, 0), Point2(offset, get_size().height), marker_color, Math::round(EDSCALE));
3913 }
3914 }
3915 }
3916 }
3917
3918 draw_line(Point2(), Point2(get_size().width, 0), h_line_color, Math::round(EDSCALE));
3919 draw_line(Point2(timeline->get_name_limit(), 0), Point2(timeline->get_name_limit(), get_size().height), v_line_color, Math::round(EDSCALE));
3920 draw_line(Point2(get_size().width - timeline->get_buttons_width(), 0), Point2(get_size().width - timeline->get_buttons_width(), get_size().height), v_line_color, Math::round(EDSCALE));
3921
3922 int ofs = stylebox_header->get_margin(SIDE_LEFT);
3923 draw_texture_rect(icon, Rect2(Point2(ofs, (get_size().height - icon_size.y) / 2 + v_margin_offset).round(), icon_size));
3924 ofs += h_separation + icon_size.x;
3925 draw_string(font, Point2(ofs, (get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size) + v_margin_offset).round(), node_name, HORIZONTAL_ALIGNMENT_LEFT, timeline->get_name_limit() - ofs, font_size, color);
3926
3927 int px = (-timeline->get_value() + timeline->get_play_position()) * timeline->get_zoom_scale() + timeline->get_name_limit();
3928 if (px >= timeline->get_name_limit() && px < limit_end) {
3929 const Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
3930 draw_line(Point2(px, 0), Point2(px, get_size().height), accent, Math::round(2 * EDSCALE));
3931 }
3932 } break;
3933
3934 case NOTIFICATION_MOUSE_EXIT: {
3935 if (hovered) {
3936 hovered = false;
3937 // When the mouse cursor exits the AnimationTrackEditGroup, we're no longer hovering the group.
3938 queue_redraw();
3939 }
3940 } break;
3941 }
3942 }
3943
3944 void AnimationTrackEditGroup::gui_input(const Ref<InputEvent> &p_event) {
3945 ERR_FAIL_COND(p_event.is_null());
3946
3947 Ref<InputEventMouseButton> mb = p_event;
3948 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
3949 Point2 pos = mb->get_position();
3950 Rect2 node_name_rect = Rect2(0, 0, timeline->get_name_limit(), get_size().height);
3951
3952 if (node_name_rect.has_point(pos)) {
3953 EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
3954 editor_selection->clear();
3955 Node *n = root->get_node_or_null(node);
3956 if (n) {
3957 editor_selection->add_node(n);
3958 }
3959 }
3960 }
3961 Ref<InputEventMouseMotion> mm = p_event;
3962 if (mm.is_valid()) {
3963 Point2 pos = mm->get_position();
3964 Rect2 node_name_rect = Rect2(0, 0, timeline->get_name_limit(), get_size().height);
3965
3966 bool was_hovered = hovered;
3967 hovered = node_name_rect.has_point(pos);
3968
3969 if (was_hovered != hovered) {
3970 queue_redraw();
3971 }
3972 }
3973 }
3974
3975 void AnimationTrackEditGroup::set_type_and_name(const Ref<Texture2D> &p_type, const String &p_name, const NodePath &p_node) {
3976 icon = p_type;
3977 node_name = p_name;
3978 node = p_node;
3979 queue_redraw();
3980 update_minimum_size();
3981 }
3982
3983 Size2 AnimationTrackEditGroup::get_minimum_size() const {
3984 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
3985 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
3986 const int separation = get_theme_constant(SNAME("v_separation"), SNAME("ItemList"));
3987
3988 const Ref<StyleBox> &header_style = get_theme_stylebox(SNAME("header"), SNAME("AnimationTrackEditGroup"));
3989 const int content_margin = header_style->get_content_margin(SIDE_TOP) + header_style->get_content_margin(SIDE_BOTTOM);
3990
3991 return Vector2(0, MAX(font->get_height(font_size), icon_size.y) + separation + content_margin);
3992 }
3993
3994 String AnimationTrackEditGroup::get_node_name() const {
3995 return node_name;
3996 }
3997
3998 void AnimationTrackEditGroup::set_timeline(AnimationTimelineEdit *p_timeline) {
3999 timeline = p_timeline;
4000 timeline->connect("zoom_changed", callable_mp(this, &AnimationTrackEditGroup::_zoom_changed));
4001 timeline->connect("name_limit_changed", callable_mp(this, &AnimationTrackEditGroup::_zoom_changed));
4002 }
4003
4004 void AnimationTrackEditGroup::set_root(Node *p_root) {
4005 root = p_root;
4006 queue_redraw();
4007 }
4008
4009 void AnimationTrackEditGroup::set_editor(AnimationTrackEditor *p_editor) {
4010 editor = p_editor;
4011 }
4012
4013 void AnimationTrackEditGroup::_zoom_changed() {
4014 queue_redraw();
4015 }
4016
4017 AnimationTrackEditGroup::AnimationTrackEditGroup() {
4018 set_mouse_filter(MOUSE_FILTER_PASS);
4019 }
4020
4021 //////////////////////////////////////
4022
4023 void AnimationTrackEditor::add_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin) {
4024 if (track_edit_plugins.has(p_plugin)) {
4025 return;
4026 }
4027 track_edit_plugins.push_back(p_plugin);
4028 }
4029
4030 void AnimationTrackEditor::remove_track_edit_plugin(const Ref<AnimationTrackEditPlugin> &p_plugin) {
4031 track_edit_plugins.erase(p_plugin);
4032 }
4033
4034 void AnimationTrackEditor::set_animation(const Ref<Animation> &p_anim, bool p_read_only) {
4035 if (animation != p_anim) {
4036 for (int i = 0; i < track_edits.size(); i++) {
4037 if (track_edits[i]->has_focus()) {
4038 track_edits[i]->release_focus();
4039 break;
4040 }
4041 }
4042 }
4043 if (animation.is_valid()) {
4044 animation->disconnect_changed(callable_mp(this, &AnimationTrackEditor::_animation_changed));
4045 _clear_selection();
4046 }
4047 animation = p_anim;
4048 read_only = p_read_only;
4049 timeline->set_animation(p_anim, read_only);
4050
4051 marker_edit->set_animation(p_anim, read_only);
4052 marker_edit->set_play_position(timeline->get_play_position());
4053
4054 _check_bezier_exist();
4055 _update_tracks();
4056
4057 if (animation.is_valid()) {
4058 animation->connect_changed(callable_mp(this, &AnimationTrackEditor::_animation_changed));
4059
4060 hscroll->show();
4061 edit->set_disabled(read_only);
4062 step->set_block_signals(true);
4063
4064 _update_step_spinbox();
4065 step->set_block_signals(false);
4066 step->set_read_only(false);
4067 snap_keys->set_disabled(false);
4068 snap_timeline->set_disabled(false);
4069 insert_at_current_time->set_disabled(false);
4070 fps_compat->set_disabled(false);
4071 snap_mode->set_disabled(false);
4072 auto_fit->set_disabled(false);
4073 auto_fit_bezier->set_disabled(false);
4074
4075 imported_anim_warning->hide();
4076 for (int i = 0; i < animation->get_track_count(); i++) {
4077 if (animation->track_is_imported(i)) {
4078 imported_anim_warning->show();
4079 break;
4080 }
4081 }
4082
4083 if (bezier_edit->is_visible()) {
4084 for (int i = 0; i < animation->get_track_count(); ++i) {
4085 if (animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
4086 _bezier_edit(i);
4087 break;
4088 }
4089 }
4090 }
4091 } else {
4092 hscroll->hide();
4093 edit->set_disabled(true);
4094 step->set_block_signals(true);
4095 step->set_value(0);
4096 step->set_block_signals(false);
4097 step->set_read_only(true);
4098 snap_keys->set_disabled(true);
4099 snap_timeline->set_disabled(true);
4100 insert_at_current_time->set_disabled(true);
4101 fps_compat->set_disabled(true);
4102 snap_mode->set_disabled(true);
4103 bezier_edit_icon->set_disabled(true);
4104 auto_fit->set_disabled(true);
4105 auto_fit_bezier->set_disabled(true);
4106 }
4107 }
4108
4109 void AnimationTrackEditor::_check_bezier_exist() {
4110 bool is_exist = false;
4111 if (animation.is_valid()) {
4112 for (int i = 0; i < animation->get_track_count(); i++) {
4113 if (animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
4114 is_exist = true;
4115 break;
4116 }
4117 }
4118 }
4119 if (is_exist) {
4120 bezier_edit_icon->set_disabled(false);
4121 } else {
4122 if (bezier_edit->is_visible()) {
4123 _cancel_bezier_edit();
4124 }
4125 bezier_edit_icon->set_disabled(true);
4126 }
4127 }
4128
4129 Ref<Animation> AnimationTrackEditor::get_current_animation() const {
4130 return animation;
4131 }
4132
4133 void AnimationTrackEditor::_root_removed() {
4134 root = nullptr;
4135 }
4136
4137 void AnimationTrackEditor::set_root(Node *p_root) {
4138 if (root) {
4139 root->disconnect(SceneStringName(tree_exiting), callable_mp(this, &AnimationTrackEditor::_root_removed));
4140 }
4141
4142 root = p_root;
4143
4144 if (root) {
4145 root->connect(SceneStringName(tree_exiting), callable_mp(this, &AnimationTrackEditor::_root_removed), CONNECT_ONE_SHOT);
4146 }
4147
4148 _update_tracks();
4149 }
4150
4151 Node *AnimationTrackEditor::get_root() const {
4152 return root;
4153 }
4154
4155 void AnimationTrackEditor::update_keying() {
4156 bool keying_enabled = false;
4157
4158 EditorSelectionHistory *editor_history = EditorNode::get_singleton()->get_editor_selection_history();
4159 if (is_visible_in_tree() && animation.is_valid() && editor_history->get_path_size() > 0) {
4160 Object *obj = ObjectDB::get_instance(editor_history->get_path_object(0));
4161 keying_enabled = Object::cast_to<Node>(obj) != nullptr || Object::cast_to<MultiNodeEdit>(obj) != nullptr;
4162 }
4163
4164 if (keying_enabled == keying) {
4165 return;
4166 }
4167
4168 keying = keying_enabled;
4169
4170 emit_signal(SNAME("keying_changed"));
4171 }
4172
4173 bool AnimationTrackEditor::has_keying() const {
4174 return keying;
4175 }
4176
4177 Dictionary AnimationTrackEditor::get_state() const {
4178 Dictionary state;
4179 state["fps_mode"] = timeline->is_using_fps();
4180 state["fps_compat"] = fps_compat->is_pressed();
4181 state["zoom"] = zoom->get_value();
4182 state["offset"] = timeline->get_value();
4183 state["v_scroll"] = scroll->get_v_scroll_bar()->get_value();
4184 return state;
4185 }
4186
4187 void AnimationTrackEditor::set_state(const Dictionary &p_state) {
4188 if (p_state.has("fps_mode")) {
4189 bool fps_mode = p_state["fps_mode"];
4190 if (fps_mode) {
4191 snap_mode->select(1);
4192 } else {
4193 snap_mode->select(0);
4194 }
4195 _snap_mode_changed(snap_mode->get_selected());
4196 }
4197
4198 if (p_state.has("fps_compat")) {
4199 fps_compat->set_pressed(p_state["fps_compat"]);
4200 }
4201
4202 if (p_state.has("zoom")) {
4203 zoom->set_value(p_state["zoom"]);
4204 }
4205
4206 if (p_state.has("offset")) {
4207 timeline->set_value(p_state["offset"]);
4208 }
4209
4210 if (p_state.has("v_scroll")) {
4211 scroll->get_v_scroll_bar()->set_value(p_state["v_scroll"]);
4212 }
4213 }
4214
4215 void AnimationTrackEditor::clear() {
4216 snap_mode->select(EDITOR_GET("editors/animation/default_fps_mode"));
4217 _snap_mode_changed(snap_mode->get_selected());
4218 fps_compat->set_pressed(EDITOR_GET("editors/animation/default_fps_compatibility"));
4219 zoom->set_value(1.0);
4220 timeline->set_value(0);
4221 scroll->get_v_scroll_bar()->set_value(0);
4222 }
4223
4224 void AnimationTrackEditor::cleanup() {
4225 set_animation(Ref<Animation>(), read_only);
4226 }
4227
4228 void AnimationTrackEditor::_name_limit_changed() {
4229 _redraw_tracks();
4230 }
4231
4232 void AnimationTrackEditor::_timeline_changed(float p_new_pos, bool p_timeline_only) {
4233 emit_signal(SNAME("timeline_changed"), p_new_pos, p_timeline_only, false);
4234 }
4235
4236 void AnimationTrackEditor::_track_remove_request(int p_track) {
4237 _animation_track_remove_request(p_track, animation);
4238 }
4239
4240 void AnimationTrackEditor::_animation_track_remove_request(int p_track, Ref<Animation> p_from_animation) {
4241 if (p_from_animation->track_is_compressed(p_track)) {
4242 EditorNode::get_singleton()->show_warning(TTR("Compressed tracks can't be edited or removed. Re-import the animation with compression disabled in order to edit."));
4243 return;
4244 }
4245 int idx = p_track;
4246 if (idx >= 0 && idx < p_from_animation->get_track_count()) {
4247 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
4248 undo_redo->create_action(TTR("Remove Anim Track"), UndoRedo::MERGE_DISABLE, p_from_animation.ptr());
4249
4250 // Remove corresponding reset tracks if they are no longer needed.
4251 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
4252 if (player->has_animation(SceneStringName(RESET))) {
4253 Ref<Animation> reset = player->get_animation(SceneStringName(RESET));
4254 if (reset != p_from_animation) {
4255 for (int i = 0; i < reset->get_track_count(); i++) {
4256 if (reset->track_get_path(i) == p_from_animation->track_get_path(p_track)) {
4257 // Check if the reset track isn't used by other animations.
4258 bool used = false;
4259 List<StringName> animation_list;
4260 player->get_animation_list(&animation_list);
4261
4262 for (const StringName &anim_name : animation_list) {
4263 Ref<Animation> anim = player->get_animation(anim_name);
4264 if (anim == p_from_animation || anim == reset) {
4265 continue;
4266 }
4267
4268 for (int j = 0; j < anim->get_track_count(); j++) {
4269 if (anim->track_get_path(j) == reset->track_get_path(i)) {
4270 used = true;
4271 break;
4272 }
4273 }
4274
4275 if (used) {
4276 break;
4277 }
4278 }
4279
4280 if (!used) {
4281 _animation_track_remove_request(i, reset);
4282 }
4283 break;
4284 }
4285 }
4286 }
4287 }
4288
4289 undo_redo->add_do_method(this, "_clear_selection", false);
4290 undo_redo->add_do_method(p_from_animation.ptr(), "remove_track", idx);
4291 undo_redo->add_undo_method(p_from_animation.ptr(), "add_track", p_from_animation->track_get_type(idx), idx);
4292 undo_redo->add_undo_method(p_from_animation.ptr(), "track_set_path", idx, p_from_animation->track_get_path(idx));
4293
4294 // TODO interpolation.
4295 for (int i = 0; i < p_from_animation->track_get_key_count(idx); i++) {
4296 Variant v = p_from_animation->track_get_key_value(idx, i);
4297 float time = p_from_animation->track_get_key_time(idx, i);
4298 float trans = p_from_animation->track_get_key_transition(idx, i);
4299
4300 undo_redo->add_undo_method(p_from_animation.ptr(), "track_insert_key", idx, time, v);
4301 undo_redo->add_undo_method(p_from_animation.ptr(), "track_set_key_transition", idx, i, trans);
4302 }
4303
4304 undo_redo->add_undo_method(p_from_animation.ptr(), "track_set_interpolation_type", idx, p_from_animation->track_get_interpolation_type(idx));
4305 if (p_from_animation->track_get_type(idx) == Animation::TYPE_VALUE) {
4306 undo_redo->add_undo_method(p_from_animation.ptr(), "value_track_set_update_mode", idx, p_from_animation->value_track_get_update_mode(idx));
4307 }
4308 if (animation->track_get_type(idx) == Animation::TYPE_AUDIO) {
4309 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_use_blend", idx, animation->audio_track_is_use_blend(idx));
4310 }
4311
4312 undo_redo->commit_action();
4313 }
4314 }
4315
4316 void AnimationTrackEditor::_track_grab_focus(int p_track) {
4317 // Don't steal focus if not working with the track editor.
4318 if (Object::cast_to<AnimationTrackEdit>(get_viewport()->gui_get_focus_owner())) {
4319 for (int i = 0; i < track_edits.size(); i++) {
4320 if (track_edits[i]->get_track() == p_track) {
4321 track_edits[i]->grab_focus();
4322 }
4323 }
4324 }
4325 }
4326
4327 void AnimationTrackEditor::set_anim_pos(float p_pos) {
4328 timeline->set_play_position(p_pos);
4329 marker_edit->set_play_position(p_pos);
4330 for (int i = 0; i < track_edits.size(); i++) {
4331 track_edits[i]->set_play_position(p_pos);
4332 }
4333 _redraw_groups();
4334 bezier_edit->set_play_position(p_pos);
4335 emit_signal(SNAME("timeline_changed"), p_pos, true, true);
4336 }
4337
4338 static bool track_type_is_resettable(Animation::TrackType p_type) {
4339 switch (p_type) {
4340 case Animation::TYPE_VALUE:
4341 [[fallthrough]];
4342 case Animation::TYPE_BLEND_SHAPE:
4343 [[fallthrough]];
4344 case Animation::TYPE_BEZIER:
4345 [[fallthrough]];
4346 case Animation::TYPE_POSITION_3D:
4347 [[fallthrough]];
4348 case Animation::TYPE_ROTATION_3D:
4349 [[fallthrough]];
4350 case Animation::TYPE_SCALE_3D:
4351 return true;
4352 default:
4353 return false;
4354 }
4355 }
4356
4357 bool AnimationTrackEditor::is_read_only() const {
4358 return read_only;
4359 }
4360
4361 void AnimationTrackEditor::make_insert_queue() {
4362 insert_data.clear();
4363 insert_queue = true;
4364 }
4365
4366 void AnimationTrackEditor::commit_insert_queue() {
4367 bool reset_allowed = true;
4368 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
4369 if (is_global_library_read_only() || (player->has_animation(SceneStringName(RESET)) && player->get_animation(SceneStringName(RESET)) == animation)) {
4370 // Avoid messing with the reset animation itself.
4371 reset_allowed = false;
4372 } else {
4373 bool some_resettable = false;
4374 for (const AnimationTrackEditor::InsertData &E : insert_data) {
4375 if (track_type_is_resettable(E.type)) {
4376 some_resettable = true;
4377 break;
4378 }
4379 }
4380 if (!some_resettable) {
4381 reset_allowed = false;
4382 }
4383 }
4384
4385 // Organize insert data.
4386 int num_tracks = 0;
4387 String last_track_query;
4388 bool all_bezier = true;
4389 for (const AnimationTrackEditor::InsertData &E : insert_data) {
4390 if (E.type != Animation::TYPE_VALUE && E.type != Animation::TYPE_BEZIER) {
4391 all_bezier = false;
4392 }
4393
4394 if (E.track_idx == -1) {
4395 ++num_tracks;
4396 last_track_query = E.query;
4397 }
4398
4399 if (E.type != Animation::TYPE_VALUE) {
4400 continue;
4401 }
4402
4403 switch (E.value.get_type()) {
4404 case Variant::INT:
4405 case Variant::FLOAT:
4406 case Variant::VECTOR2:
4407 case Variant::VECTOR3:
4408 case Variant::QUATERNION:
4409 case Variant::PLANE:
4410 case Variant::COLOR: {
4411 // Valid.
4412 } break;
4413 default: {
4414 all_bezier = false;
4415 }
4416 }
4417 }
4418
4419 // Skip the confirmation dialog if the user holds Shift while clicking the key icon.
4420 // If `confirm_insert_track` editor setting is disabled, the behavior is reversed.
4421 bool confirm_insert = EDITOR_GET("editors/animation/confirm_insert_track");
4422 if ((Input::get_singleton()->is_key_pressed(Key::SHIFT) != confirm_insert) && num_tracks > 0) {
4423 String dialog_text;
4424
4425 // Potentially a new key, does not exist.
4426 if (num_tracks == 1) {
4427 // TRANSLATORS: %s will be replaced by a phrase describing the target of track.
4428 dialog_text = vformat(TTR("Create new track for %s and insert key?"), last_track_query);
4429 } else {
4430 dialog_text = vformat(TTR("Create %d new tracks and insert keys?"), num_tracks);
4431 }
4432
4433 if (confirm_insert) {
4434 dialog_text += +"\n\n" + TTR("Hold Shift when clicking the key icon to skip this dialog.");
4435 }
4436 insert_confirm_text->set_text(dialog_text);
4437
4438 insert_confirm_bezier->set_visible(all_bezier);
4439 insert_confirm_reset->set_visible(reset_allowed);
4440
4441 insert_confirm->set_ok_button_text(TTR("Create"));
4442 insert_confirm->popup_centered();
4443 } else {
4444 _insert_track(reset_allowed && EDITOR_GET("editors/animation/default_create_reset_tracks"), all_bezier && EDITOR_GET("editors/animation/default_create_bezier_tracks"));
4445 }
4446
4447 insert_queue = false;
4448 }
4449
4450 void AnimationTrackEditor::_query_insert(const InsertData &p_id) {
4451 ERR_FAIL_COND_EDMSG(read_only, "Animation is read-only."); // Should have been prevented by dialog, but for safety.
4452
4453 if (!insert_queue) {
4454 insert_data.clear();
4455 }
4456
4457 for (const InsertData &E : insert_data) {
4458 // Prevent insertion of multiple tracks.
4459 if (E.path == p_id.path && E.type == p_id.type) {
4460 return; // Already inserted a track this frame.
4461 }
4462 }
4463
4464 insert_data.push_back(p_id);
4465
4466 // Without queue, commit immediately.
4467 if (!insert_queue) {
4468 commit_insert_queue();
4469 }
4470 }
4471
4472 void AnimationTrackEditor::_insert_track(bool p_reset_wanted, bool p_create_beziers) {
4473 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
4474 undo_redo->create_action(TTR("Animation Insert Key"));
4475
4476 Ref<Animation> reset_anim;
4477 if (p_reset_wanted) {
4478 reset_anim = _create_and_get_reset_animation();
4479 }
4480
4481 TrackIndices next_tracks(animation.ptr(), reset_anim.ptr());
4482 bool advance = false;
4483 while (insert_data.size()) {
4484 if (insert_data.front()->get().advance) {
4485 advance = true;
4486 }
4487 next_tracks = _confirm_insert(insert_data.front()->get(), next_tracks, p_reset_wanted, reset_anim, p_create_beziers);
4488 insert_data.pop_front();
4489 }
4490
4491 undo_redo->commit_action();
4492
4493 if (advance) {
4494 _edit_menu_pressed(EDIT_GOTO_NEXT_STEP_TIMELINE_ONLY);
4495 }
4496 }
4497
4498 void AnimationTrackEditor::insert_transform_key(Node3D *p_node, const String &p_sub, const Animation::TrackType p_type, const Variant &p_value) {
4499 if (read_only) {
4500 popup_read_only_dialog();
4501 return;
4502 }
4503
4504 ERR_FAIL_NULL(root);
4505 ERR_FAIL_COND_MSG(
4506 (p_type != Animation::TYPE_POSITION_3D && p_type != Animation::TYPE_ROTATION_3D && p_type != Animation::TYPE_SCALE_3D),
4507 "Track type must be Position/Rotation/Scale 3D.");
4508 if (!keying) {
4509 return;
4510 }
4511 if (animation.is_null()) {
4512 return;
4513 }
4514
4515 // Let's build a node path.
4516 String path = String(root->get_path_to(p_node, true));
4517 if (!p_sub.is_empty()) {
4518 path += ":" + p_sub;
4519 }
4520
4521 NodePath np = path;
4522
4523 int track_idx = -1;
4524
4525 for (int i = 0; i < animation->get_track_count(); i++) {
4526 if (animation->track_get_path(i) != np) {
4527 continue;
4528 }
4529 if (animation->track_get_type(i) != p_type) {
4530 continue;
4531 }
4532 track_idx = i;
4533 }
4534
4535 InsertData id;
4536 id.path = np;
4537 // TRANSLATORS: This describes the target of new animation track, will be inserted into another string.
4538 id.query = vformat(TTR("node '%s'"), p_node->get_name());
4539 id.advance = false;
4540 id.track_idx = track_idx;
4541 id.value = p_value;
4542 id.type = p_type;
4543 _query_insert(id);
4544 }
4545
4546 bool AnimationTrackEditor::has_track(Node3D *p_node, const String &p_sub, const Animation::TrackType p_type) {
4547 ERR_FAIL_NULL_V(root, false);
4548 if (!keying) {
4549 return false;
4550 }
4551 if (animation.is_null()) {
4552 return false;
4553 }
4554
4555 // Let's build a node path.
4556 String path = String(root->get_path_to(p_node, true));
4557 if (!p_sub.is_empty()) {
4558 path += ":" + p_sub;
4559 }
4560
4561 int track_id = animation->find_track(path, p_type);
4562 if (track_id >= 0) {
4563 return true;
4564 }
4565 return false;
4566 }
4567
4568 void AnimationTrackEditor::_insert_animation_key(NodePath p_path, const Variant &p_value) {
4569 if (read_only) {
4570 popup_read_only_dialog();
4571 return;
4572 }
4573
4574 String path = String(p_path);
4575
4576 // Animation property is a special case, always creates an animation track.
4577 for (int i = 0; i < animation->get_track_count(); i++) {
4578 String np = String(animation->track_get_path(i));
4579
4580 if (path == np && animation->track_get_type(i) == Animation::TYPE_ANIMATION) {
4581 // Exists.
4582 InsertData id;
4583 id.path = path;
4584 id.track_idx = i;
4585 id.value = p_value;
4586 id.type = Animation::TYPE_ANIMATION;
4587 // TRANSLATORS: This describes the target of new animation track, will be inserted into another string.
4588 id.query = TTR("animation");
4589 id.advance = false;
4590 // Dialog insert.
4591 _query_insert(id);
4592 return;
4593 }
4594 }
4595
4596 InsertData id;
4597 id.path = path;
4598 id.track_idx = -1;
4599 id.value = p_value;
4600 id.type = Animation::TYPE_ANIMATION;
4601 id.query = TTR("animation");
4602 id.advance = false;
4603 // Dialog insert.
4604 _query_insert(id);
4605 }
4606
4607 void AnimationTrackEditor::insert_node_value_key(Node *p_node, const String &p_property, bool p_only_if_exists, bool p_advance) {
4608 if (read_only) {
4609 popup_read_only_dialog();
4610 return;
4611 }
4612
4613 ERR_FAIL_NULL(root);
4614
4615 // Let's build a node path.
4616 String path = String(root->get_path_to(p_node, true));
4617
4618 // Get the value from the subpath.
4619 Vector<StringName> subpath = NodePath(p_property).get_as_property_path().get_subnames();
4620 Variant value = p_node->get_indexed(subpath);
4621
4622 if (Object::cast_to<AnimationPlayer>(p_node) && p_property == "current_animation") {
4623 if (p_node == AnimationPlayerEditor::get_singleton()->get_player()) {
4624 EditorNode::get_singleton()->show_warning(TTR("AnimationPlayer can't animate itself, only other players."));
4625 return;
4626 }
4627 _insert_animation_key(path, value);
4628 return;
4629 }
4630
4631 EditorSelectionHistory *history = EditorNode::get_singleton()->get_editor_selection_history();
4632 for (int i = 1; i < history->get_path_size(); i++) {
4633 String prop = history->get_path_property(i);
4634 ERR_FAIL_COND(prop.is_empty());
4635 path += ":" + prop;
4636 }
4637
4638 path += ":" + p_property;
4639
4640 NodePath np = path;
4641
4642 // Locate track.
4643
4644 bool inserted = false;
4645
4646 for (int i = 0; i < animation->get_track_count(); i++) {
4647 if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
4648 if (animation->track_get_path(i) != np) {
4649 continue;
4650 }
4651
4652 InsertData id;
4653 id.path = np;
4654 id.track_idx = i;
4655 id.value = value;
4656 id.type = Animation::TYPE_VALUE;
4657 // TRANSLATORS: This describes the target of new animation track, will be inserted into another string.
4658 id.query = vformat(TTR("property '%s'"), p_property);
4659 id.advance = p_advance;
4660 // Dialog insert.
4661 _query_insert(id);
4662 inserted = true;
4663 } else if (animation->track_get_type(i) == Animation::TYPE_BEZIER) {
4664 Variant actual_value;
4665 String track_path = String(animation->track_get_path(i));
4666 if (track_path == String(np)) {
4667 actual_value = value; // All good.
4668 } else {
4669 int sep = track_path.rfind_char(':');
4670 if (sep != -1) {
4671 String base_path = track_path.substr(0, sep);
4672 if (base_path == String(np)) {
4673 String value_name = track_path.substr(sep + 1);
4674 actual_value = value.get(value_name);
4675 } else {
4676 continue;
4677 }
4678 } else {
4679 continue;
4680 }
4681 }
4682
4683 InsertData id;
4684 id.path = animation->track_get_path(i);
4685 id.track_idx = i;
4686 id.value = actual_value;
4687 id.type = Animation::TYPE_BEZIER;
4688 id.query = vformat(TTR("property '%s'"), p_property);
4689 id.advance = p_advance;
4690 // Dialog insert.
4691 _query_insert(id);
4692 inserted = true;
4693 }
4694 }
4695
4696 if (inserted || p_only_if_exists) {
4697 return;
4698 }
4699 InsertData id;
4700 id.path = np;
4701 id.track_idx = -1;
4702 id.value = value;
4703 id.type = Animation::TYPE_VALUE;
4704 id.query = vformat(TTR("property '%s'"), p_property);
4705 id.advance = p_advance;
4706 // Dialog insert.
4707 _query_insert(id);
4708 }
4709
4710 PackedStringArray AnimationTrackEditor::get_selected_section() const {
4711 return marker_edit->get_selected_section();
4712 }
4713
4714 bool AnimationTrackEditor::is_marker_selected(const StringName &p_marker) const {
4715 return marker_edit->is_marker_selected(p_marker);
4716 }
4717
4718 bool AnimationTrackEditor::is_marker_moving_selection() const {
4719 return marker_edit->is_moving_selection();
4720 }
4721
4722 float AnimationTrackEditor::get_marker_moving_selection_offset() const {
4723 return marker_edit->get_moving_selection_offset();
4724 }
4725
4726 void AnimationTrackEditor::insert_value_key(const String &p_property, bool p_advance) {
4727 if (read_only) {
4728 popup_read_only_dialog();
4729 return;
4730 }
4731
4732 EditorSelectionHistory *history = EditorNode::get_singleton()->get_editor_selection_history();
4733
4734 ERR_FAIL_NULL(root);
4735 ERR_FAIL_COND(history->get_path_size() == 0);
4736 Object *obj = ObjectDB::get_instance(history->get_path_object(0));
4737
4738 Ref<MultiNodeEdit> multi_node_edit(obj);
4739 if (multi_node_edit.is_valid()) {
4740 Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
4741 ERR_FAIL_NULL(edited_scene);
4742
4743 make_insert_queue();
4744
4745 for (int i = 0; i < multi_node_edit->get_node_count(); ++i) {
4746 Node *node = edited_scene->get_node(multi_node_edit->get_node(i));
4747 insert_node_value_key(node, p_property, false, p_advance);
4748 }
4749
4750 commit_insert_queue();
4751 } else {
4752 Node *node = Object::cast_to<Node>(obj);
4753 ERR_FAIL_NULL(node);
4754
4755 make_insert_queue();
4756 insert_node_value_key(node, p_property, false, p_advance);
4757 commit_insert_queue();
4758 }
4759 }
4760
4761 bool AnimationTrackEditor::is_global_library_read_only() const {
4762 Ref<AnimationLibrary> al;
4763 AnimationMixer *mixer = AnimationPlayerEditor::get_singleton()->fetch_mixer_for_library();
4764 if (mixer) {
4765 if (!mixer->has_animation_library("")) {
4766 return false;
4767 } else {
4768 al = mixer->get_animation_library("");
4769 }
4770 }
4771 if (al.is_valid()) {
4772 String base = al->get_path();
4773 int srpos = base.find("::");
4774 if (srpos != -1) {
4775 base = base.substr(0, srpos);
4776 }
4777 if (FileAccess::exists(base + ".import")) {
4778 return true;
4779 }
4780 }
4781 return false;
4782 }
4783
4784 Ref<Animation> AnimationTrackEditor::_create_and_get_reset_animation() {
4785 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
4786 if (player->has_animation(SceneStringName(RESET))) {
4787 return player->get_animation(SceneStringName(RESET));
4788 } else {
4789 Ref<AnimationLibrary> al;
4790 AnimationMixer *mixer = AnimationPlayerEditor::get_singleton()->fetch_mixer_for_library();
4791 if (mixer) {
4792 if (!mixer->has_animation_library("")) {
4793 al.instantiate();
4794 mixer->add_animation_library("", al);
4795 } else {
4796 al = mixer->get_animation_library("");
4797 }
4798 }
4799 Ref<Animation> reset_anim;
4800 reset_anim.instantiate();
4801 reset_anim->set_length(ANIM_MIN_LENGTH);
4802 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
4803 undo_redo->add_do_method(al.ptr(), "add_animation", SceneStringName(RESET), reset_anim);
4804 undo_redo->add_do_method(AnimationPlayerEditor::get_singleton(), "_animation_player_changed", player);
4805 undo_redo->add_undo_method(al.ptr(), "remove_animation", SceneStringName(RESET));
4806 undo_redo->add_undo_method(AnimationPlayerEditor::get_singleton(), "_animation_player_changed", player);
4807 return reset_anim;
4808 }
4809 }
4810
4811 void AnimationTrackEditor::_confirm_insert_list() {
4812 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
4813 undo_redo->create_action(TTR("Animation Insert Key"));
4814
4815 bool create_reset = insert_confirm_reset->is_visible() && insert_confirm_reset->is_pressed();
4816 Ref<Animation> reset_anim;
4817 if (create_reset) {
4818 reset_anim = _create_and_get_reset_animation();
4819 }
4820
4821 TrackIndices next_tracks(animation.ptr(), reset_anim.ptr());
4822 bool advance = false;
4823 while (insert_data.size()) {
4824 if (insert_data.front()->get().advance) {
4825 advance = true;
4826 }
4827 next_tracks = _confirm_insert(insert_data.front()->get(), next_tracks, create_reset, reset_anim, insert_confirm_bezier->is_pressed());
4828 insert_data.pop_front();
4829 }
4830
4831 undo_redo->commit_action();
4832
4833 if (advance) {
4834 _edit_menu_pressed(EDIT_GOTO_NEXT_STEP_TIMELINE_ONLY);
4835 }
4836 }
4837
4838 PropertyInfo AnimationTrackEditor::_find_hint_for_track(int p_idx, NodePath &r_base_path, Variant *r_current_val) {
4839 r_base_path = NodePath();
4840 ERR_FAIL_COND_V(animation.is_null(), PropertyInfo());
4841 ERR_FAIL_INDEX_V(p_idx, animation->get_track_count(), PropertyInfo());
4842
4843 if (!root) {
4844 return PropertyInfo();
4845 }
4846
4847 NodePath path = animation->track_get_path(p_idx);
4848
4849 if (!root->has_node_and_resource(path)) {
4850 return PropertyInfo();
4851 }
4852
4853 Ref<Resource> res;
4854 Vector<StringName> leftover_path;
4855 Node *node = root->get_node_and_resource(path, res, leftover_path, true);
4856
4857 if (node) {
4858 r_base_path = node->get_path();
4859 }
4860
4861 if (leftover_path.is_empty()) {
4862 if (r_current_val) {
4863 if (res.is_valid()) {
4864 *r_current_val = res;
4865 } else if (node) {
4866 *r_current_val = node;
4867 }
4868 }
4869 return PropertyInfo();
4870 }
4871
4872 Variant property_info_base;
4873 if (res.is_valid()) {
4874 property_info_base = res;
4875 if (r_current_val) {
4876 *r_current_val = res->get_indexed(leftover_path);
4877 }
4878 } else if (node) {
4879 property_info_base = node;
4880 if (r_current_val) {
4881 *r_current_val = node->get_indexed(leftover_path);
4882 }
4883 }
4884
4885 if (property_info_base.is_null()) {
4886 WARN_PRINT(vformat("Could not determine track hint for '%s:%s' because its base property is null.",
4887 String(path.get_concatenated_names()), String(path.get_concatenated_subnames())));
4888 return PropertyInfo();
4889 }
4890
4891 List<PropertyInfo> pinfo;
4892 property_info_base.get_property_list(&pinfo);
4893
4894 for (const PropertyInfo &E : pinfo) {
4895 if (E.name == leftover_path[leftover_path.size() - 1]) {
4896 return E;
4897 }
4898 }
4899
4900 return PropertyInfo();
4901 }
4902
4903 static Vector<String> _get_bezier_subindices_for_type(Variant::Type p_type, bool *r_valid = nullptr) {
4904 Vector<String> subindices;
4905 if (r_valid) {
4906 *r_valid = true;
4907 }
4908 switch (p_type) {
4909 case Variant::INT: {
4910 subindices.push_back("");
4911 } break;
4912 case Variant::FLOAT: {
4913 subindices.push_back("");
4914 } break;
4915 case Variant::VECTOR2: {
4916 subindices.push_back(":x");
4917 subindices.push_back(":y");
4918 } break;
4919 case Variant::VECTOR3: {
4920 subindices.push_back(":x");
4921 subindices.push_back(":y");
4922 subindices.push_back(":z");
4923 } break;
4924 case Variant::QUATERNION: {
4925 subindices.push_back(":x");
4926 subindices.push_back(":y");
4927 subindices.push_back(":z");
4928 subindices.push_back(":w");
4929 } break;
4930 case Variant::COLOR: {
4931 subindices.push_back(":r");
4932 subindices.push_back(":g");
4933 subindices.push_back(":b");
4934 subindices.push_back(":a");
4935 } break;
4936 case Variant::PLANE: {
4937 subindices.push_back(":x");
4938 subindices.push_back(":y");
4939 subindices.push_back(":z");
4940 subindices.push_back(":d");
4941 } break;
4942 case Variant::NIL: {
4943 subindices.push_back(""); // Hack: it is probably float since non-numeric types are filtered in the selection window.
4944 } break;
4945 default: {
4946 if (r_valid) {
4947 *r_valid = false;
4948 }
4949 }
4950 }
4951
4952 return subindices;
4953 }
4954
4955 AnimationTrackEditor::TrackIndices AnimationTrackEditor::_confirm_insert(InsertData p_id, TrackIndices p_next_tracks, bool p_reset_wanted, Ref<Animation> p_reset_anim, bool p_create_beziers) {
4956 bool created = false;
4957
4958 bool create_normal_track = p_id.track_idx < 0;
4959 bool create_reset_track = p_reset_wanted && track_type_is_resettable(p_id.type);
4960
4961 Animation::UpdateMode update_mode = Animation::UPDATE_DISCRETE;
4962 Animation::InterpolationType interp_type = Animation::INTERPOLATION_LINEAR;
4963 bool loop_wrap = true;
4964 if (create_normal_track || create_reset_track) {
4965 if (p_id.type == Animation::TYPE_VALUE || p_id.type == Animation::TYPE_BEZIER) {
4966 _fetch_value_track_options(p_id.path, &update_mode, &interp_type, &loop_wrap);
4967 }
4968 }
4969
4970 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
4971 if (create_normal_track) {
4972 if (p_create_beziers) {
4973 bool valid;
4974 Vector<String> subindices = _get_bezier_subindices_for_type(p_id.value.get_type(), &valid);
4975 if (valid) {
4976 for (int i = 0; i < subindices.size(); i++) {
4977 InsertData id = p_id;
4978 id.type = Animation::TYPE_BEZIER;
4979 id.value = subindices[i].is_empty() ? p_id.value : p_id.value.get(subindices[i].substr(1));
4980 id.path = String(p_id.path) + subindices[i];
4981 p_next_tracks = _confirm_insert(id, p_next_tracks, p_reset_wanted, p_reset_anim, false);
4982 }
4983
4984 return p_next_tracks;
4985 }
4986 }
4987 created = true;
4988
4989 p_id.track_idx = p_next_tracks.normal;
4990
4991 undo_redo->add_do_method(animation.ptr(), "add_track", p_id.type);
4992 undo_redo->add_do_method(animation.ptr(), "track_set_path", p_id.track_idx, p_id.path);
4993 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", p_id.track_idx, interp_type);
4994 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", p_id.track_idx, loop_wrap);
4995 if (p_id.type == Animation::TYPE_VALUE) {
4996 undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", p_id.track_idx, update_mode);
4997 }
4998 }
4999
5000 float time = p_id.time == FLT_MAX ? timeline->get_play_position() : p_id.time;
5001 Variant value;
5002
5003 switch (p_id.type) {
5004 case Animation::TYPE_POSITION_3D:
5005 case Animation::TYPE_ROTATION_3D:
5006 case Animation::TYPE_SCALE_3D:
5007 case Animation::TYPE_BLEND_SHAPE:
5008 case Animation::TYPE_VALUE:
5009 case Animation::TYPE_AUDIO:
5010 case Animation::TYPE_ANIMATION: {
5011 value = p_id.value;
5012
5013 } break;
5014 case Animation::TYPE_BEZIER: {
5015 int existing = -1;
5016 if (p_id.track_idx < animation->get_track_count()) {
5017 existing = animation->track_find_key(p_id.track_idx, time, Animation::FIND_MODE_APPROX);
5018 }
5019
5020 if (existing != -1) {
5021 Array arr = animation->track_get_key_value(p_id.track_idx, existing);
5022 arr[0] = p_id.value;
5023 value = arr;
5024 } else {
5025 value = animation->make_default_bezier_key(p_id.value);
5026 }
5027 bezier_edit_icon->set_disabled(false);
5028 } break;
5029 default: {
5030 // Other track types shouldn't use this code path.
5031 DEV_ASSERT(false);
5032 }
5033 }
5034
5035 undo_redo->add_do_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, value);
5036 if (!created && p_id.type == Animation::TYPE_BEZIER) {
5037 undo_redo->add_do_method(this, "_bezier_track_set_key_handle_mode_at_time", animation.ptr(), p_id.track_idx, time, (Animation::HandleMode)bezier_key_mode->get_selected_id(), Animation::HANDLE_SET_MODE_AUTO);
5038 }
5039
5040 if (created) {
5041 // Just remove the track.
5042 undo_redo->add_undo_method(this, "_clear_selection", false);
5043 undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
5044 p_next_tracks.normal++;
5045 } else {
5046 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", p_id.track_idx, time);
5047 int existing = animation->track_find_key(p_id.track_idx, time, Animation::FIND_MODE_APPROX);
5048 if (existing != -1) {
5049 Variant v = animation->track_get_key_value(p_id.track_idx, existing);
5050 float trans = animation->track_get_key_transition(p_id.track_idx, existing);
5051 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", p_id.track_idx, time, v, trans);
5052 }
5053 }
5054
5055 if (create_reset_track) {
5056 Animation *reset_anim = p_reset_anim.ptr();
5057 for (int i = 0; i < reset_anim->get_track_count(); i++) {
5058 if (reset_anim->track_get_path(i) == p_id.path) {
5059 create_reset_track = false;
5060 break;
5061 }
5062 }
5063 if (create_reset_track) {
5064 undo_redo->add_do_method(reset_anim, "add_track", p_id.type);
5065 undo_redo->add_do_method(reset_anim, "track_set_path", p_next_tracks.reset, p_id.path);
5066 if (p_id.type == Animation::TYPE_VALUE) {
5067 undo_redo->add_do_method(reset_anim, "value_track_set_update_mode", p_next_tracks.reset, update_mode);
5068 }
5069 undo_redo->add_do_method(reset_anim, "track_set_interpolation_type", p_next_tracks.reset, interp_type);
5070 undo_redo->add_do_method(reset_anim, "track_insert_key", p_next_tracks.reset, 0.0f, value);
5071 undo_redo->add_undo_method(reset_anim, "remove_track", reset_anim->get_track_count());
5072 p_next_tracks.reset++;
5073 }
5074 }
5075
5076 return p_next_tracks;
5077 }
5078
5079 void AnimationTrackEditor::show_select_node_warning(bool p_show) {
5080 info_message_vbox->set_visible(p_show);
5081 }
5082
5083 void AnimationTrackEditor::show_dummy_player_warning(bool p_show) {
5084 dummy_player_warning->set_visible(p_show);
5085 }
5086
5087 void AnimationTrackEditor::show_inactive_player_warning(bool p_show) {
5088 inactive_player_warning->set_visible(p_show);
5089 }
5090
5091 bool AnimationTrackEditor::is_key_selected(int p_track, int p_key) const {
5092 SelectedKey sk;
5093 sk.key = p_key;
5094 sk.track = p_track;
5095
5096 return selection.has(sk);
5097 }
5098
5099 bool AnimationTrackEditor::is_selection_active() const {
5100 return selection.size();
5101 }
5102
5103 bool AnimationTrackEditor::is_key_clipboard_active() const {
5104 return key_clipboard.keys.size();
5105 }
5106
5107 bool AnimationTrackEditor::is_snap_timeline_enabled() const {
5108 return snap_timeline->is_pressed() ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
5109 }
5110
5111 bool AnimationTrackEditor::is_snap_keys_enabled() const {
5112 return snap_keys->is_pressed() ^ Input::get_singleton()->is_key_pressed(Key::CMD_OR_CTRL);
5113 }
5114
5115 bool AnimationTrackEditor::is_insert_at_current_time_enabled() const {
5116 return insert_at_current_time->is_pressed();
5117 }
5118
5119 void AnimationTrackEditor::resolve_insertion_offset(float &r_offset) const {
5120 if (is_insert_at_current_time_enabled()) {
5121 r_offset = timeline->get_play_position();
5122 }
5123 }
5124
5125 bool AnimationTrackEditor::is_bezier_editor_active() const {
5126 return bezier_edit->is_visible();
5127 }
5128
5129 bool AnimationTrackEditor::can_add_reset_key() const {
5130 if (is_global_library_read_only()) {
5131 return false;
5132 }
5133 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
5134 const Animation::TrackType track_type = animation->track_get_type(E.key.track);
5135 if (track_type != Animation::TYPE_ANIMATION && track_type != Animation::TYPE_AUDIO && track_type != Animation::TYPE_METHOD) {
5136 return true;
5137 }
5138 }
5139 return false;
5140 }
5141
5142 void AnimationTrackEditor::_on_filter_updated(const String &p_filter) {
5143 emit_signal(SNAME("filter_changed"));
5144 }
5145
5146 void AnimationTrackEditor::_update_tracks() {
5147 int selected = _get_track_selected();
5148
5149 while (track_vbox->get_child_count()) {
5150 memdelete(track_vbox->get_child(0));
5151 }
5152
5153 timeline->set_track_edit(nullptr);
5154
5155 track_edits.clear();
5156 groups.clear();
5157
5158 if (animation.is_null()) {
5159 return;
5160 }
5161
5162 bool file_read_only = false;
5163 if (!animation->get_path().is_resource_file()) {
5164 int srpos = animation->get_path().find("::");
5165 if (srpos != -1) {
5166 String base = animation->get_path().substr(0, srpos);
5167 if (ResourceLoader::get_resource_type(base) == "PackedScene") {
5168 if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) {
5169 file_read_only = true;
5170 }
5171 } else {
5172 if (FileAccess::exists(base + ".import")) {
5173 file_read_only = true;
5174 }
5175 }
5176 }
5177 } else {
5178 if (FileAccess::exists(animation->get_path() + ".import")) {
5179 file_read_only = true;
5180 }
5181 }
5182
5183 RBMap<String, VBoxContainer *> group_sort;
5184 LocalVector<VBoxContainer *> group_containers;
5185
5186 bool use_grouping = !view_group->is_pressed();
5187 bool use_filter = selected_filter->is_pressed();
5188 bool use_alphabetic_sorting = alphabetic_sorting->is_pressed();
5189
5190 AnimationTrackEdit *selected_track_edit = nullptr;
5191
5192 for (int i = 0; i < animation->get_track_count(); i++) {
5193 AnimationTrackEdit *track_edit = nullptr;
5194
5195 // Find hint and info for plugin.
5196
5197 if (use_filter) {
5198 NodePath path = animation->track_get_path(i);
5199
5200 if (root) {
5201 Node *node = root->get_node_or_null(path);
5202 if (!node) {
5203 continue; // No node, no filter.
5204 }
5205 if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
5206 continue; // Skip track due to not selected.
5207 }
5208 }
5209 }
5210
5211 String filter_text = timeline->filter_track->get_text();
5212
5213 if (!filter_text.is_empty()) {
5214 String target = String(animation->track_get_path(i));
5215 if (!target.containsn(filter_text)) {
5216 continue;
5217 }
5218 }
5219
5220 if (animation->track_get_type(i) == Animation::TYPE_VALUE) {
5221 NodePath path = animation->track_get_path(i);
5222
5223 if (root && root->has_node_and_resource(path)) {
5224 Ref<Resource> res;
5225 NodePath base_path;
5226 Vector<StringName> leftover_path;
5227 Node *node = root->get_node_and_resource(path, res, leftover_path, true);
5228 PropertyInfo pinfo = _find_hint_for_track(i, base_path);
5229
5230 Object *object = node;
5231 if (res.is_valid()) {
5232 object = res.ptr();
5233 }
5234
5235 if (object && !leftover_path.is_empty()) {
5236 if (pinfo.name.is_empty()) {
5237 pinfo.name = leftover_path[leftover_path.size() - 1];
5238 }
5239
5240 for (int j = 0; j < track_edit_plugins.size(); j++) {
5241 track_edit = track_edit_plugins.write[j]->create_value_track_edit(object, pinfo.type, pinfo.name, pinfo.hint, pinfo.hint_string, pinfo.usage);
5242 if (track_edit) {
5243 break;
5244 }
5245 }
5246 }
5247 }
5248 }
5249 if (animation->track_get_type(i) == Animation::TYPE_AUDIO) {
5250 for (int j = 0; j < track_edit_plugins.size(); j++) {
5251 track_edit = track_edit_plugins.write[j]->create_audio_track_edit();
5252 if (track_edit) {
5253 break;
5254 }
5255 }
5256 }
5257
5258 if (animation->track_get_type(i) == Animation::TYPE_ANIMATION) {
5259 NodePath path = animation->track_get_path(i);
5260
5261 Node *node = nullptr;
5262 if (root) {
5263 node = root->get_node_or_null(path);
5264 }
5265
5266 if (node && Object::cast_to<AnimationPlayer>(node)) {
5267 for (int j = 0; j < track_edit_plugins.size(); j++) {
5268 track_edit = track_edit_plugins.write[j]->create_animation_track_edit(node);
5269 if (track_edit) {
5270 break;
5271 }
5272 }
5273 }
5274 }
5275
5276 if (track_edit == nullptr) {
5277 // No valid plugin_found.
5278 track_edit = memnew(AnimationTrackEdit);
5279 }
5280
5281 track_edits.push_back(track_edit);
5282
5283 if (use_grouping) {
5284 String base_path = String(animation->track_get_path(i));
5285 base_path = base_path.get_slicec(':', 0); // Remove sub-path.
5286
5287 if (!group_sort.has(base_path)) {
5288 AnimationTrackEditGroup *g = memnew(AnimationTrackEditGroup);
5289 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Node"));
5290 String name = base_path;
5291 String tooltip;
5292 if (root) {
5293 Node *n = root->get_node_or_null(base_path);
5294 if (n) {
5295 icon = EditorNode::get_singleton()->get_object_icon(n);
5296 name = n->get_name();
5297 tooltip = String(root->get_path_to(n));
5298 }
5299 }
5300
5301 g->set_type_and_name(icon, name, animation->track_get_path(i));
5302 g->set_root(root);
5303 g->set_tooltip_text(tooltip);
5304 g->set_timeline(timeline);
5305 g->set_editor(this);
5306 groups.push_back(g);
5307 VBoxContainer *vb = memnew(VBoxContainer);
5308 vb->add_theme_constant_override("separation", 0);
5309 vb->add_child(g);
5310 group_sort[base_path] = vb;
5311 group_containers.push_back(vb);
5312 }
5313
5314 track_edit->set_in_group(true);
5315 group_sort[base_path]->add_child(track_edit);
5316
5317 } else {
5318 track_edit->set_in_group(false);
5319 }
5320
5321 track_edit->set_timeline(timeline);
5322 track_edit->set_root(root);
5323 track_edit->set_animation_and_track(animation, i, file_read_only);
5324 track_edit->set_play_position(timeline->get_play_position());
5325 track_edit->set_editor(this);
5326
5327 if (selected == i) {
5328 selected_track_edit = track_edit;
5329 }
5330
5331 track_edit->connect("timeline_changed", callable_mp(this, &AnimationTrackEditor::_timeline_changed));
5332 track_edit->connect("remove_request", callable_mp(this, &AnimationTrackEditor::_track_remove_request), CONNECT_DEFERRED);
5333 track_edit->connect("dropped", callable_mp(this, &AnimationTrackEditor::_dropped_track), CONNECT_DEFERRED);
5334 track_edit->connect("insert_key", callable_mp(this, &AnimationTrackEditor::_insert_key_from_track).bind(i), CONNECT_DEFERRED);
5335 track_edit->connect("select_key", callable_mp(this, &AnimationTrackEditor::_key_selected).bind(i), CONNECT_DEFERRED);
5336 track_edit->connect("deselect_key", callable_mp(this, &AnimationTrackEditor::_key_deselected).bind(i), CONNECT_DEFERRED);
5337 track_edit->connect("move_selection_begin", callable_mp(this, &AnimationTrackEditor::_move_selection_begin));
5338 track_edit->connect("move_selection", callable_mp(this, &AnimationTrackEditor::_move_selection));
5339 track_edit->connect("move_selection_commit", callable_mp(this, &AnimationTrackEditor::_move_selection_commit));
5340 track_edit->connect("move_selection_cancel", callable_mp(this, &AnimationTrackEditor::_move_selection_cancel));
5341
5342 track_edit->connect("duplicate_request", callable_mp(this, &AnimationTrackEditor::_anim_duplicate_keys).bind(i), CONNECT_DEFERRED);
5343 track_edit->connect("cut_request", callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_CUT_KEYS), CONNECT_DEFERRED);
5344 track_edit->connect("copy_request", callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_COPY_KEYS), CONNECT_DEFERRED);
5345 track_edit->connect("paste_request", callable_mp(this, &AnimationTrackEditor::_anim_paste_keys).bind(i), CONNECT_DEFERRED);
5346 track_edit->connect("create_reset_request", callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_ADD_RESET_KEY), CONNECT_DEFERRED);
5347 track_edit->connect("delete_request", callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_DELETE_SELECTION), CONNECT_DEFERRED);
5348 }
5349
5350 if (use_grouping) {
5351 if (use_alphabetic_sorting) {
5352 struct GroupAlphaCompare {
5353 bool operator()(const VBoxContainer *p_lhs, const VBoxContainer *p_rhs) const {
5354 String lhs_node_name = Object::cast_to<AnimationTrackEditGroup>(p_lhs->get_child(0))->get_node_name();
5355 String rhs_node_name = Object::cast_to<AnimationTrackEditGroup>(p_rhs->get_child(0))->get_node_name();
5356 return lhs_node_name < rhs_node_name;
5357 }
5358 };
5359
5360 group_containers.sort_custom<GroupAlphaCompare>();
5361 }
5362
5363 for (VBoxContainer *vb : group_containers) {
5364 track_vbox->add_child(vb);
5365 }
5366
5367 } else {
5368 if (use_alphabetic_sorting) {
5369 struct TrackAlphaCompare {
5370 bool operator()(const AnimationTrackEdit *p_lhs, const AnimationTrackEdit *p_rhs) const {
5371 String lhs_leaf = (String)p_lhs->get_path().slice(-p_lhs->get_path().get_subname_count() - 1);
5372 String rhs_leaf = (String)p_rhs->get_path().slice(-p_rhs->get_path().get_subname_count() - 1);
5373 return lhs_leaf < rhs_leaf;
5374 }
5375 };
5376
5377 track_edits.sort_custom<TrackAlphaCompare>();
5378 }
5379
5380 for (AnimationTrackEdit *track_edit : track_edits) {
5381 track_vbox->add_child(track_edit);
5382 }
5383 }
5384
5385 if (selected_track_edit != nullptr) {
5386 selected_track_edit->grab_focus();
5387 }
5388 }
5389
5390 void AnimationTrackEditor::_redraw_tracks() {
5391 for (int i = 0; i < track_edits.size(); i++) {
5392 track_edits[i]->queue_redraw();
5393 }
5394 }
5395
5396 void AnimationTrackEditor::_redraw_groups() {
5397 for (int i = 0; i < groups.size(); i++) {
5398 groups[i]->queue_redraw();
5399 }
5400 }
5401
5402 void AnimationTrackEditor::_animation_changed() {
5403 if (animation_changing_awaiting_update) {
5404 return; // All will be updated, don't bother with anything.
5405 }
5406
5407 _check_bezier_exist();
5408
5409 if (key_edit) {
5410 if (key_edit->setting) {
5411 // If editing a key, just redraw the edited track, makes refresh less costly.
5412 if (key_edit->track < track_edits.size()) {
5413 if (animation->track_get_type(key_edit->track) == Animation::TYPE_BEZIER) {
5414 bezier_edit->queue_redraw();
5415 } else {
5416 track_edits[key_edit->track]->queue_redraw();
5417 }
5418 }
5419 return;
5420 } else {
5421 _update_key_edit();
5422 }
5423 }
5424
5425 animation_changing_awaiting_update = true;
5426 callable_mp(this, &AnimationTrackEditor::_animation_update).call_deferred();
5427 }
5428
5429 void AnimationTrackEditor::_snap_mode_changed(int p_mode) {
5430 bool use_fps = p_mode == 1;
5431 timeline->set_use_fps(use_fps);
5432 if (key_edit) {
5433 key_edit->set_use_fps(use_fps);
5434 }
5435 marker_edit->set_use_fps(use_fps);
5436 // To ensure that the conversion results are consistent between serialization and load, the value is snapped with 0.0625 to be a rational number when FPS mode is used.
5437 step->set_step(use_fps ? FPS_DECIMAL : SECOND_DECIMAL);
5438 if (use_fps) {
5439 fps_compat->hide();
5440 } else {
5441 fps_compat->show();
5442 }
5443 _update_step_spinbox();
5444 }
5445
5446 void AnimationTrackEditor::_update_step_spinbox() {
5447 if (animation.is_null()) {
5448 return;
5449 }
5450 step->set_block_signals(true);
5451
5452 if (timeline->is_using_fps()) {
5453 if (animation->get_step() == 0.0) {
5454 step->set_value(0.0);
5455 } else {
5456 step->set_value(1.0 / animation->get_step());
5457 }
5458 } else {
5459 step->set_value(animation->get_step());
5460 }
5461
5462 step->set_block_signals(false);
5463 _update_snap_unit();
5464 }
5465
5466 void AnimationTrackEditor::_store_snap_states() {
5467 EditorSettings::get_singleton()->set_project_metadata("animation_track_editor", "snap_timeline", snap_timeline->is_pressed());
5468 EditorSettings::get_singleton()->set_project_metadata("animation_track_editor", "snap_keys", snap_keys->is_pressed());
5469 }
5470
5471 void AnimationTrackEditor::_update_fps_compat_mode(bool p_enabled) {
5472 _update_snap_unit();
5473 }
5474
5475 void AnimationTrackEditor::_update_nearest_fps_label() {
5476 bool is_fps_invalid = nearest_fps == 0;
5477 if (is_fps_invalid) {
5478 nearest_fps_label->hide();
5479 } else {
5480 nearest_fps_label->show();
5481 nearest_fps_label->set_text(vformat(TTR("Nearest FPS: %d"), nearest_fps));
5482 }
5483 }
5484
5485 void AnimationTrackEditor::_animation_update() {
5486 timeline->queue_redraw();
5487 timeline->update_values();
5488
5489 bool same = true;
5490
5491 if (animation.is_null()) {
5492 return;
5493 }
5494
5495 if (track_edits.size() == animation->get_track_count()) {
5496 // Check tracks are the same.
5497
5498 for (int i = 0; i < track_edits.size(); i++) {
5499 if (track_edits[i]->get_path() != animation->track_get_path(i)) {
5500 same = false;
5501 break;
5502 }
5503 }
5504 } else {
5505 same = false;
5506 }
5507
5508 if (same) {
5509 _redraw_tracks();
5510 _redraw_groups();
5511 } else {
5512 _update_tracks();
5513 }
5514
5515 bezier_edit->queue_redraw();
5516
5517 _update_step_spinbox();
5518 emit_signal(SNAME("animation_step_changed"), animation->get_step());
5519 emit_signal(SNAME("animation_len_changed"), animation->get_length());
5520
5521 animation_changing_awaiting_update = false;
5522 }
5523
5524 MenuButton *AnimationTrackEditor::get_edit_menu() {
5525 return edit;
5526 }
5527
5528 void AnimationTrackEditor::_notification(int p_what) {
5529 switch (p_what) {
5530 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
5531 if (!EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
5532 break;
5533 }
5534
5535 panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
5536 panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
5537 } break;
5538
5539 case NOTIFICATION_THEME_CHANGED: {
5540 add_animation_player->set_button_icon(get_editor_theme_icon(SNAME("Add")));
5541 zoom_icon->set_texture(get_editor_theme_icon(SNAME("Zoom")));
5542 bezier_edit_icon->set_button_icon(get_editor_theme_icon(SNAME("EditBezier")));
5543 snap_timeline->set_button_icon(get_editor_theme_icon(SNAME("SnapTimeline")));
5544 snap_keys->set_button_icon(get_editor_theme_icon(SNAME("SnapKeys")));
5545 insert_at_current_time->set_button_icon(get_editor_theme_icon(SNAME("InsertAtCurrentTime")));
5546 fps_compat->set_button_icon(get_editor_theme_icon(SNAME("FPS")));
5547 view_group->set_button_icon(get_editor_theme_icon(view_group->is_pressed() ? SNAME("AnimationTrackList") : SNAME("AnimationTrackGroup")));
5548 function_name_toggler->set_button_icon(get_editor_theme_icon(SNAME("MemberMethod")));
5549 selected_filter->set_button_icon(get_editor_theme_icon(SNAME("AnimationFilter")));
5550 alphabetic_sorting->set_button_icon(get_editor_theme_icon(SNAME("Sort")));
5551 imported_anim_warning->set_button_icon(get_editor_theme_icon(SNAME("NodeWarning")));
5552 dummy_player_warning->set_button_icon(get_editor_theme_icon(SNAME("NodeWarning")));
5553 inactive_player_warning->set_button_icon(get_editor_theme_icon(SNAME("NodeWarning")));
5554
5555 Ref<StyleBox> panel_style = get_theme_stylebox(SceneStringName(panel), SNAME("Tree"))->duplicate();
5556 panel_style->set_content_margin(SIDE_TOP, get_theme_constant("base_margin", EditorStringName(Editor)) * EDSCALE);
5557 main_panel->add_theme_style_override(SceneStringName(panel), panel_style);
5558
5559 edit->get_popup()->set_item_icon(edit->get_popup()->get_item_index(EDIT_ADD_RESET_KEY), get_editor_theme_icon(SNAME("MoveUp")));
5560 edit->get_popup()->set_item_icon(edit->get_popup()->get_item_index(EDIT_APPLY_RESET), get_editor_theme_icon(SNAME("Reload")));
5561 auto_fit->set_button_icon(get_editor_theme_icon(SNAME("AnimationAutoFit")));
5562 auto_fit_bezier->set_button_icon(get_editor_theme_icon(SNAME("AnimationAutoFitBezier")));
5563
5564 const int timeline_separation = get_theme_constant(SNAME("timeline_v_separation"), SNAME("AnimationTrackEditor"));
5565 timeline_vbox->add_theme_constant_override("separation", timeline_separation);
5566
5567 const int track_separation = get_theme_constant(SNAME("track_v_separation"), SNAME("AnimationTrackEditor"));
5568 track_vbox->add_theme_constant_override("separation", track_separation);
5569
5570 function_name_toggler->add_theme_color_override("icon_pressed_color", get_theme_color("icon_disabled_color", EditorStringName(Editor)));
5571
5572 bezier_key_mode->set_item_icon(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_FREE), get_editor_theme_icon(SNAME("BezierHandlesFree")));
5573 bezier_key_mode->set_item_icon(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_LINEAR), get_editor_theme_icon(SNAME("BezierHandlesLinear")));
5574 bezier_key_mode->set_item_icon(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_BALANCED), get_editor_theme_icon(SNAME("BezierHandlesBalanced")));
5575 bezier_key_mode->set_item_icon(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_MIRRORED), get_editor_theme_icon(SNAME("BezierHandlesMirror")));
5576
5577 _update_timeline_margins();
5578 } break;
5579
5580 case NOTIFICATION_READY: {
5581 Node *scene_root = EditorNode::get_singleton()->get_scene_root();
5582 scene_root->connect("child_entered_tree", callable_mp(this, &AnimationTrackEditor::_root_node_changed).bind(false));
5583 scene_root->connect("child_exiting_tree", callable_mp(this, &AnimationTrackEditor::_root_node_changed).bind(true));
5584
5585 EditorNode::get_singleton()->connect("scene_changed", callable_mp(this, &AnimationTrackEditor::_scene_changed));
5586 EditorNode::get_singleton()->get_editor_selection()->connect("selection_changed", callable_mp(this, &AnimationTrackEditor::_selection_changed));
5587
5588 panner->setup((ViewPanner::ControlScheme)EDITOR_GET("editors/panning/animation_editors_panning_scheme").operator int(), ED_GET_SHORTCUT("canvas_item_editor/pan_view"), bool(EDITOR_GET("editors/panning/simple_panning")));
5589 panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
5590 } break;
5591
5592 case NOTIFICATION_VISIBILITY_CHANGED: {
5593 update_keying();
5594 } break;
5595
5596 case NOTIFICATION_TRANSLATION_CHANGED: {
5597 bezier_key_mode->set_item_text(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_FREE), TTR("Free", "Bezier Handle Mode"));
5598 bezier_key_mode->set_item_text(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_LINEAR), TTR("Linear", "Bezier Handle Mode"));
5599 bezier_key_mode->set_item_text(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_BALANCED), TTR("Balanced", "Bezier Handle Mode"));
5600 bezier_key_mode->set_item_text(bezier_key_mode->get_item_index(Animation::HANDLE_MODE_MIRRORED), TTR("Mirrored", "Bezier Handle Mode"));
5601 bezier_key_mode->set_tooltip_text(TTR("Bezier Default Mode") + "\n" + TTR("Set the default handle mode of new bezier keys."));
5602 } break;
5603 }
5604 }
5605
5606 void AnimationTrackEditor::_update_scroll(double) {
5607 _redraw_tracks();
5608 _redraw_groups();
5609 marker_edit->queue_redraw();
5610 }
5611
5612 void AnimationTrackEditor::_update_step(double p_new_step) {
5613 if (animation.is_null()) {
5614 return;
5615 }
5616
5617 _update_snap_unit();
5618
5619 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5620 undo_redo->create_action(TTR("Change Animation Step"));
5621 double step_value = p_new_step;
5622 if (timeline->is_using_fps()) {
5623 if (step_value != 0.0) {
5624 // A step_value should be less than or equal to 1000 to ensure that no error accumulates due to interactions with retrieving values from inner range.
5625 step_value = 1.0 / MIN(1000.0, p_new_step);
5626 }
5627 timeline->queue_redraw();
5628 }
5629 undo_redo->add_do_method(animation.ptr(), "set_step", step_value);
5630 undo_redo->add_undo_method(animation.ptr(), "set_step", animation->get_step());
5631 step->set_block_signals(true);
5632 undo_redo->commit_action();
5633 step->set_block_signals(false);
5634 emit_signal(SNAME("animation_step_changed"), step_value);
5635 }
5636
5637 void AnimationTrackEditor::_update_length(double p_new_len) {
5638 emit_signal(SNAME("animation_len_changed"), p_new_len);
5639 }
5640
5641 void AnimationTrackEditor::_dropped_track(int p_from_track, int p_to_track) {
5642 if (p_from_track == p_to_track || p_from_track == p_to_track - 1) {
5643 return;
5644 }
5645
5646 _clear_selection(true);
5647 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5648 undo_redo->create_action(TTR("Rearrange Tracks"));
5649 undo_redo->add_do_method(animation.ptr(), "track_move_to", p_from_track, p_to_track);
5650 // Take into account that the position of the tracks that come after the one removed will change.
5651 int to_track_real = p_to_track > p_from_track ? p_to_track - 1 : p_to_track;
5652 undo_redo->add_undo_method(animation.ptr(), "track_move_to", to_track_real, p_to_track > p_from_track ? p_from_track : p_from_track + 1);
5653 undo_redo->add_do_method(this, "_track_grab_focus", to_track_real);
5654 undo_redo->add_undo_method(this, "_track_grab_focus", p_from_track);
5655 undo_redo->commit_action();
5656 }
5657
5658 void AnimationTrackEditor::_new_track_node_selected(NodePath p_path) {
5659 ERR_FAIL_NULL(root);
5660 Node *node = get_node_or_null(p_path);
5661 ERR_FAIL_NULL(node);
5662 NodePath path_to = root->get_path_to(node, true);
5663
5664 if (adding_track_type == Animation::TYPE_BLEND_SHAPE && !node->is_class("MeshInstance3D")) {
5665 EditorNode::get_singleton()->show_warning(TTR("Blend Shape tracks only apply to MeshInstance3D nodes."));
5666 return;
5667 }
5668
5669 if ((adding_track_type == Animation::TYPE_POSITION_3D || adding_track_type == Animation::TYPE_ROTATION_3D || adding_track_type == Animation::TYPE_SCALE_3D) && !node->is_class("Node3D")) {
5670 EditorNode::get_singleton()->show_warning(TTR("Position/Rotation/Scale 3D tracks only apply to 3D-based nodes."));
5671 return;
5672 }
5673
5674 switch (adding_track_type) {
5675 case Animation::TYPE_VALUE: {
5676 adding_track_path = path_to;
5677 prop_selector->set_type_filter(Vector<Variant::Type>());
5678 prop_selector->select_property_from_instance(node);
5679 } break;
5680 case Animation::TYPE_BLEND_SHAPE: {
5681 adding_track_path = path_to;
5682 Vector<Variant::Type> filter;
5683 filter.push_back(Variant::FLOAT);
5684 prop_selector->set_type_filter(filter);
5685 prop_selector->select_property_from_instance(node);
5686 } break;
5687 case Animation::TYPE_POSITION_3D:
5688 case Animation::TYPE_ROTATION_3D:
5689 case Animation::TYPE_SCALE_3D:
5690 case Animation::TYPE_METHOD: {
5691 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5692 undo_redo->create_action(TTR("Add Track"));
5693 undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
5694 undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path_to);
5695 undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
5696 undo_redo->commit_action();
5697
5698 } break;
5699 case Animation::TYPE_BEZIER: {
5700 Vector<Variant::Type> filter;
5701 filter.push_back(Variant::INT);
5702 filter.push_back(Variant::FLOAT);
5703 filter.push_back(Variant::VECTOR2);
5704 filter.push_back(Variant::VECTOR3);
5705 filter.push_back(Variant::QUATERNION);
5706 filter.push_back(Variant::PLANE);
5707 filter.push_back(Variant::COLOR);
5708
5709 adding_track_path = path_to;
5710 prop_selector->set_type_filter(filter);
5711 prop_selector->select_property_from_instance(node);
5712 } break;
5713 case Animation::TYPE_AUDIO: {
5714 if (!node->is_class("AudioStreamPlayer") && !node->is_class("AudioStreamPlayer2D") && !node->is_class("AudioStreamPlayer3D")) {
5715 EditorNode::get_singleton()->show_warning(TTR("Audio tracks can only point to nodes of type:\n-AudioStreamPlayer\n-AudioStreamPlayer2D\n-AudioStreamPlayer3D"));
5716 return;
5717 }
5718
5719 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5720 undo_redo->create_action(TTR("Add Track"));
5721 undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
5722 undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path_to);
5723 undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
5724 undo_redo->commit_action();
5725
5726 } break;
5727 case Animation::TYPE_ANIMATION: {
5728 if (!node->is_class("AnimationPlayer")) {
5729 EditorNode::get_singleton()->show_warning(TTR("Animation tracks can only point to AnimationPlayer nodes."));
5730 return;
5731 }
5732
5733 if (node == AnimationPlayerEditor::get_singleton()->get_player()) {
5734 EditorNode::get_singleton()->show_warning(TTR("AnimationPlayer can't animate itself, only other players."));
5735 return;
5736 }
5737
5738 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5739 undo_redo->create_action(TTR("Add Track"));
5740 undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
5741 undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), path_to);
5742 undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
5743 undo_redo->commit_action();
5744
5745 } break;
5746 }
5747 }
5748
5749 void AnimationTrackEditor::_add_track(int p_type) {
5750 AnimationPlayer *ap = AnimationPlayerEditor::get_singleton()->get_player();
5751 if (!ap) {
5752 ERR_FAIL_EDMSG("No AnimationPlayer is currently being edited.");
5753 }
5754 Node *root_node = ap->get_node_or_null(ap->get_root_node());
5755 if (!root_node) {
5756 EditorNode::get_singleton()->show_warning(TTR("Not possible to add a new track without a root"));
5757 return;
5758 }
5759 adding_track_type = p_type;
5760 Vector<StringName> valid_types;
5761 switch (adding_track_type) {
5762 case Animation::TYPE_BLEND_SHAPE: {
5763 // Blend Shape is a property of MeshInstance3D.
5764 valid_types.push_back(SNAME("MeshInstance3D"));
5765 } break;
5766 case Animation::TYPE_POSITION_3D:
5767 case Animation::TYPE_ROTATION_3D:
5768 case Animation::TYPE_SCALE_3D: {
5769 // 3D Properties come from nodes inheriting Node3D.
5770 valid_types.push_back(SNAME("Node3D"));
5771 } break;
5772 case Animation::TYPE_AUDIO: {
5773 valid_types.push_back(SNAME("AudioStreamPlayer"));
5774 valid_types.push_back(SNAME("AudioStreamPlayer2D"));
5775 valid_types.push_back(SNAME("AudioStreamPlayer3D"));
5776 } break;
5777 case Animation::TYPE_ANIMATION: {
5778 valid_types.push_back(SNAME("AnimationPlayer"));
5779 } break;
5780 }
5781 pick_track->set_valid_types(valid_types);
5782 pick_track->popup_scenetree_dialog(nullptr, root_node);
5783 pick_track->get_filter_line_edit()->clear();
5784 pick_track->get_filter_line_edit()->grab_focus();
5785 }
5786
5787 void AnimationTrackEditor::_fetch_value_track_options(const NodePath &p_path, Animation::UpdateMode *r_update_mode, Animation::InterpolationType *r_interpolation_type, bool *r_loop_wrap) {
5788 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
5789 if (player->has_animation(SceneStringName(RESET))) {
5790 Ref<Animation> reset_anim = player->get_animation(SceneStringName(RESET));
5791 int rt = reset_anim->find_track(p_path, Animation::TrackType::TYPE_VALUE);
5792 if (rt >= 0) {
5793 *r_update_mode = reset_anim->value_track_get_update_mode(rt);
5794 *r_interpolation_type = reset_anim->track_get_interpolation_type(rt);
5795 *r_loop_wrap = reset_anim->track_get_interpolation_loop_wrap(rt);
5796 return;
5797 }
5798 rt = reset_anim->find_track(p_path, Animation::TrackType::TYPE_BEZIER);
5799 if (rt >= 0) {
5800 *r_interpolation_type = reset_anim->track_get_interpolation_type(rt);
5801 *r_loop_wrap = reset_anim->track_get_interpolation_loop_wrap(rt);
5802 return;
5803 }
5804 }
5805
5806 // Hack.
5807 NodePath np;
5808 animation->add_track(Animation::TYPE_VALUE);
5809 animation->track_set_path(animation->get_track_count() - 1, p_path);
5810 PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
5811 animation->remove_track(animation->get_track_count() - 1); // Hack.
5812 switch (h.type) {
5813 case Variant::FLOAT:
5814 case Variant::VECTOR2:
5815 case Variant::RECT2:
5816 case Variant::VECTOR3:
5817 case Variant::TRANSFORM2D:
5818 case Variant::VECTOR4:
5819 case Variant::PLANE:
5820 case Variant::QUATERNION:
5821 case Variant::AABB:
5822 case Variant::BASIS:
5823 case Variant::TRANSFORM3D:
5824 case Variant::PROJECTION:
5825 case Variant::COLOR:
5826 case Variant::PACKED_FLOAT32_ARRAY:
5827 case Variant::PACKED_FLOAT64_ARRAY:
5828 case Variant::PACKED_VECTOR2_ARRAY:
5829 case Variant::PACKED_VECTOR3_ARRAY:
5830 case Variant::PACKED_COLOR_ARRAY:
5831 case Variant::PACKED_VECTOR4_ARRAY: {
5832 *r_update_mode = Animation::UPDATE_CONTINUOUS;
5833 } break;
5834 default: {
5835 }
5836 }
5837 }
5838
5839 void AnimationTrackEditor::_new_track_property_selected(const String &p_name) {
5840 String full_path = String(adding_track_path) + ":" + p_name;
5841
5842 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
5843
5844 Animation::UpdateMode update_mode = Animation::UPDATE_DISCRETE;
5845 Animation::InterpolationType interp_type = Animation::INTERPOLATION_LINEAR;
5846 bool loop_wrap = true;
5847 _fetch_value_track_options(full_path, &update_mode, &interp_type, &loop_wrap);
5848 if (adding_track_type == Animation::TYPE_BEZIER) {
5849 Vector<String> subindices;
5850 {
5851 // Hack.
5852 NodePath np;
5853 animation->add_track(Animation::TYPE_VALUE);
5854 animation->track_set_path(animation->get_track_count() - 1, full_path);
5855 PropertyInfo h = _find_hint_for_track(animation->get_track_count() - 1, np);
5856 animation->remove_track(animation->get_track_count() - 1); // Hack.
5857 bool valid;
5858 subindices = _get_bezier_subindices_for_type(h.type, &valid);
5859 if (!valid) {
5860 EditorNode::get_singleton()->show_warning(TTR("Invalid track for Bezier (no suitable sub-properties)"));
5861 return;
5862 }
5863 }
5864
5865 undo_redo->create_action(TTR("Add Bezier Track"));
5866 int base_track = animation->get_track_count();
5867 for (int i = 0; i < subindices.size(); i++) {
5868 int track_idx = base_track + i;
5869 undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
5870 undo_redo->add_do_method(animation.ptr(), "track_set_path", track_idx, full_path + subindices[i]);
5871 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", track_idx, interp_type);
5872 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", track_idx, loop_wrap);
5873 undo_redo->add_undo_method(animation.ptr(), "remove_track", base_track);
5874 }
5875 undo_redo->commit_action();
5876 } else {
5877 bool is_blend_shape = adding_track_type == Animation::TYPE_BLEND_SHAPE;
5878 if (is_blend_shape) {
5879 PackedStringArray split = p_name.split("/");
5880 if (!split.is_empty()) {
5881 full_path = String(adding_track_path) + ":" + split[split.size() - 1];
5882 }
5883 }
5884 undo_redo->create_action(TTR("Add Track"));
5885 undo_redo->add_do_method(animation.ptr(), "add_track", adding_track_type);
5886 undo_redo->add_do_method(animation.ptr(), "track_set_path", animation->get_track_count(), full_path);
5887 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", animation->get_track_count(), interp_type);
5888 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", animation->get_track_count(), loop_wrap);
5889 if (!is_blend_shape) {
5890 undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", animation->get_track_count(), update_mode);
5891 }
5892 undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
5893 undo_redo->commit_action();
5894 }
5895 }
5896
5897 void AnimationTrackEditor::_timeline_value_changed(double) {
5898 timeline->update_play_position();
5899
5900 _redraw_tracks();
5901 for (int i = 0; i < track_edits.size(); i++) {
5902 track_edits[i]->update_play_position();
5903 }
5904 _redraw_groups();
5905
5906 bezier_edit->queue_redraw();
5907 bezier_edit->update_play_position();
5908
5909 marker_edit->update_play_position();
5910 }
5911
5912 int AnimationTrackEditor::_get_track_selected() {
5913 for (int i = 0; i < track_edits.size(); i++) {
5914 if (track_edits[i]->has_focus()) {
5915 return track_edits[i]->get_track();
5916 }
5917 }
5918
5919 return -1;
5920 }
5921
5922 void AnimationTrackEditor::_insert_key_from_track(float p_ofs, int p_track) {
5923 if (read_only) {
5924 popup_read_only_dialog();
5925 return;
5926 }
5927
5928 ERR_FAIL_INDEX(p_track, animation->get_track_count());
5929
5930 if (snap_keys->is_pressed() && step->get_value() != 0) {
5931 p_ofs = snap_time(p_ofs);
5932 }
5933
5934 resolve_insertion_offset(p_ofs);
5935
5936 while (animation->track_find_key(p_track, p_ofs, Animation::FIND_MODE_APPROX) != -1) { // Make sure insertion point is valid.
5937 p_ofs += SECOND_DECIMAL;
5938 }
5939
5940 Node *node = root->get_node_or_null(animation->track_get_path(p_track));
5941 if (!node) {
5942 EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a key."));
5943 return;
5944 }
5945
5946 // Special handling for this one.
5947 if (animation->track_get_type(p_track) == Animation::TYPE_METHOD) {
5948 method_selector->select_method_from_instance(node);
5949
5950 insert_key_from_track_call_ofs = p_ofs;
5951 insert_key_from_track_call_track = p_track;
5952 return;
5953 }
5954
5955 InsertData id;
5956 id.path = animation->track_get_path(p_track);
5957 id.advance = false;
5958 id.track_idx = p_track;
5959 id.type = animation->track_get_type(p_track);
5960 // TRANSLATORS: This describes the target of new animation track, will be inserted into another string.
5961 id.query = vformat(TTR("node '%s'"), node->get_name());
5962 id.time = p_ofs;
5963 // id.value is filled in each case handled below.
5964
5965 switch (animation->track_get_type(p_track)) {
5966 case Animation::TYPE_POSITION_3D: {
5967 Node3D *base = Object::cast_to<Node3D>(node);
5968
5969 if (!base) {
5970 EditorNode::get_singleton()->show_warning(TTR("Track is not of type Node3D, can't insert key"));
5971 return;
5972 }
5973
5974 id.value = base->get_position();
5975 } break;
5976 case Animation::TYPE_ROTATION_3D: {
5977 Node3D *base = Object::cast_to<Node3D>(node);
5978
5979 if (!base) {
5980 EditorNode::get_singleton()->show_warning(TTR("Track is not of type Node3D, can't insert key"));
5981 return;
5982 }
5983
5984 id.value = base->get_transform().basis.operator Quaternion();
5985 } break;
5986 case Animation::TYPE_SCALE_3D: {
5987 Node3D *base = Object::cast_to<Node3D>(node);
5988
5989 if (!base) {
5990 EditorNode::get_singleton()->show_warning(TTR("Track is not of type Node3D, can't insert key"));
5991 return;
5992 }
5993
5994 id.value = base->get_scale();
5995 } break;
5996 case Animation::TYPE_BLEND_SHAPE: {
5997 MeshInstance3D *base = Object::cast_to<MeshInstance3D>(node);
5998
5999 if (!base) {
6000 EditorNode::get_singleton()->show_warning(TTR("Track is not of type MeshInstance3D, can't insert key"));
6001 return;
6002 }
6003
6004 id.value = base->get_blend_shape_value(base->find_blend_shape_by_name(id.path.get_subname(0)));
6005 } break;
6006 case Animation::TYPE_VALUE: {
6007 NodePath bp;
6008 _find_hint_for_track(p_track, bp, &id.value);
6009 } break;
6010 case Animation::TYPE_METHOD: {
6011 Node *base = root->get_node_or_null(animation->track_get_path(p_track));
6012 ERR_FAIL_NULL(base);
6013
6014 method_selector->select_method_from_instance(base);
6015
6016 insert_key_from_track_call_ofs = p_ofs;
6017 insert_key_from_track_call_track = p_track;
6018
6019 } break;
6020 case Animation::TYPE_BEZIER: {
6021 NodePath bp;
6022 Variant value;
6023 _find_hint_for_track(p_track, bp, &value);
6024 id.value = animation->make_default_bezier_key(value);
6025 } break;
6026 case Animation::TYPE_AUDIO: {
6027 Dictionary ak;
6028 ak["stream"] = Ref<Resource>();
6029 ak["start_offset"] = 0;
6030 ak["end_offset"] = 0;
6031
6032 id.value = ak;
6033 } break;
6034 case Animation::TYPE_ANIMATION: {
6035 id.value = StringName("[stop]");
6036 } break;
6037 default: {
6038 // All track types should be handled by now.
6039 DEV_ASSERT(false);
6040 }
6041 }
6042
6043 _query_insert(id);
6044 }
6045
6046 void AnimationTrackEditor::_add_method_key(const String &p_method) {
6047 if (!root->has_node(animation->track_get_path(insert_key_from_track_call_track))) {
6048 EditorNode::get_singleton()->show_warning(TTR("Track path is invalid, so can't add a method key."));
6049 return;
6050 }
6051 Node *base = root->get_node_or_null(animation->track_get_path(insert_key_from_track_call_track));
6052 ERR_FAIL_NULL(base);
6053
6054 List<MethodInfo> minfo;
6055 base->get_method_list(&minfo);
6056
6057 for (const MethodInfo &E : minfo) {
6058 if (E.name == p_method) {
6059 Dictionary d;
6060 d["method"] = p_method;
6061 Array params;
6062 int64_t first_defarg = E.arguments.size() - E.default_arguments.size();
6063
6064 for (int64_t i = 0; i < E.arguments.size(); ++i) {
6065 if (i >= first_defarg) {
6066 Variant arg = E.default_arguments[i - first_defarg];
6067 params.push_back(arg);
6068 } else {
6069 Callable::CallError ce;
6070 Variant arg;
6071 Variant::construct(E.arguments[i].type, arg, nullptr, 0, ce);
6072 params.push_back(arg);
6073 }
6074 }
6075 d["args"] = params;
6076
6077 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
6078 undo_redo->create_action(TTR("Add Method Track Key"));
6079 undo_redo->add_do_method(animation.ptr(), "track_insert_key", insert_key_from_track_call_track, insert_key_from_track_call_ofs, d);
6080 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
6081 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", insert_key_from_track_call_track, insert_key_from_track_call_ofs);
6082 undo_redo->commit_action();
6083
6084 return;
6085 }
6086 }
6087
6088 EditorNode::get_singleton()->show_warning(TTR("Method not found in object:") + " " + p_method);
6089 }
6090
6091 void AnimationTrackEditor::_key_selected(int p_key, bool p_single, int p_track) {
6092 ERR_FAIL_INDEX(p_track, animation->get_track_count());
6093 ERR_FAIL_INDEX(p_key, animation->track_get_key_count(p_track));
6094
6095 SelectedKey sk;
6096 sk.key = p_key;
6097 sk.track = p_track;
6098
6099 if (p_single) {
6100 _clear_selection();
6101 }
6102
6103 KeyInfo ki;
6104 ki.pos = animation->track_get_key_time(p_track, p_key);
6105 selection[sk] = ki;
6106
6107 _redraw_tracks();
6108 _update_key_edit();
6109
6110 marker_edit->_clear_selection(marker_edit->is_selection_active());
6111 }
6112
6113 void AnimationTrackEditor::_key_deselected(int p_key, int p_track) {
6114 ERR_FAIL_INDEX(p_track, animation->get_track_count());
6115 ERR_FAIL_INDEX(p_key, animation->track_get_key_count(p_track));
6116
6117 SelectedKey sk;
6118 sk.key = p_key;
6119 sk.track = p_track;
6120
6121 selection.erase(sk);
6122
6123 _redraw_tracks();
6124 _update_key_edit();
6125 }
6126
6127 void AnimationTrackEditor::_move_selection_begin() {
6128 moving_selection = true;
6129 moving_selection_offset = 0;
6130 }
6131
6132 void AnimationTrackEditor::_move_selection(float p_offset) {
6133 moving_selection_offset = p_offset;
6134 _redraw_tracks();
6135 }
6136
6137 struct _AnimMoveRestore {
6138 int track = 0;
6139 float time = 0;
6140 Variant key;
6141 float transition = 0;
6142 };
6143 // Used for undo/redo.
6144
6145 void AnimationTrackEditor::_clear_key_edit() {
6146 if (key_edit) {
6147 // If key edit is the object being inspected, remove it first.
6148 if (InspectorDock::get_inspector_singleton()->get_edited_object() == key_edit) {
6149 EditorNode::get_singleton()->push_item(nullptr);
6150 }
6151
6152 // Then actually delete it.
6153 memdelete(key_edit);
6154 key_edit = nullptr;
6155 }
6156
6157 if (multi_key_edit) {
6158 if (InspectorDock::get_inspector_singleton()->get_edited_object() == multi_key_edit) {
6159 EditorNode::get_singleton()->push_item(nullptr);
6160 }
6161
6162 memdelete(multi_key_edit);
6163 multi_key_edit = nullptr;
6164 }
6165 }
6166
6167 void AnimationTrackEditor::_clear_selection(bool p_update) {
6168 selection.clear();
6169
6170 if (p_update) {
6171 _redraw_tracks();
6172 }
6173
6174 _clear_key_edit();
6175 }
6176
6177 void AnimationTrackEditor::_update_key_edit() {
6178 _clear_key_edit();
6179 if (animation.is_null()) {
6180 return;
6181 }
6182
6183 if (selection.size() == 1) {
6184 key_edit = memnew(AnimationTrackKeyEdit);
6185 key_edit->animation = animation;
6186 key_edit->animation_read_only = read_only;
6187 key_edit->track = selection.front()->key().track;
6188 key_edit->use_fps = timeline->is_using_fps();
6189 key_edit->editor = this;
6190
6191 int key_id = selection.front()->key().key;
6192 if (key_id >= animation->track_get_key_count(key_edit->track)) {
6193 _clear_key_edit();
6194 return; // Probably in the process of rearranging the keys.
6195 }
6196 float ofs = animation->track_get_key_time(key_edit->track, key_id);
6197 key_edit->key_ofs = ofs;
6198 key_edit->root_path = root;
6199
6200 NodePath np;
6201 key_edit->hint = _find_hint_for_track(key_edit->track, np);
6202 key_edit->base = np;
6203
6204 EditorNode::get_singleton()->push_item(key_edit);
6205 } else if (selection.size() > 1) {
6206 multi_key_edit = memnew(AnimationMultiTrackKeyEdit);
6207 multi_key_edit->animation = animation;
6208 multi_key_edit->animation_read_only = read_only;
6209 multi_key_edit->editor = this;
6210
6211 RBMap<int, List<float>> key_ofs_map;
6212 RBMap<int, NodePath> base_map;
6213 int first_track = -1;
6214 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
6215 int track = E.key.track;
6216 if (first_track < 0) {
6217 first_track = track;
6218 }
6219
6220 if (!key_ofs_map.has(track)) {
6221 key_ofs_map[track] = List<float>();
6222 base_map[track] = NodePath();
6223 }
6224
6225 int key_id = E.key.key;
6226 if (key_id >= animation->track_get_key_count(track)) {
6227 _clear_key_edit();
6228 return; // Probably in the process of rearranging the keys.
6229 }
6230 key_ofs_map[track].push_back(animation->track_get_key_time(track, E.key.key));
6231 }
6232 multi_key_edit->key_ofs_map = key_ofs_map;
6233 multi_key_edit->base_map = base_map;
6234 multi_key_edit->hint = _find_hint_for_track(first_track, base_map[first_track]);
6235 multi_key_edit->use_fps = timeline->is_using_fps();
6236 multi_key_edit->root_path = root;
6237
6238 EditorNode::get_singleton()->push_item(multi_key_edit);
6239 }
6240 }
6241
6242 void AnimationTrackEditor::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
6243 if (animation != p_anim) {
6244 return;
6245 }
6246
6247 _clear_selection();
6248 }
6249
6250 void AnimationTrackEditor::_select_at_anim(const Ref<Animation> &p_anim, int p_track, float p_pos) {
6251 if (animation != p_anim) {
6252 return;
6253 }
6254
6255 int idx = animation->track_find_key(p_track, p_pos, Animation::FIND_MODE_APPROX);
6256 ERR_FAIL_COND(idx < 0);
6257
6258 SelectedKey sk;
6259 sk.track = p_track;
6260 sk.key = idx;
6261 KeyInfo ki;
6262 ki.pos = p_pos;
6263
6264 selection.insert(sk, ki);
6265 _update_key_edit();
6266
6267 marker_edit->_clear_selection(marker_edit->is_selection_active());
6268 }
6269
6270 void AnimationTrackEditor::_move_selection_commit() {
6271 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
6272 undo_redo->create_action(TTR("Animation Move Keys"));
6273
6274 List<_AnimMoveRestore> to_restore;
6275
6276 float motion = moving_selection_offset;
6277 // 1 - remove the keys.
6278 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6279 undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
6280 }
6281 // 2 - Remove overlapped keys.
6282 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6283 float newtime = E->get().pos + motion;
6284 int idx = animation->track_find_key(E->key().track, newtime, Animation::FIND_MODE_APPROX);
6285 if (idx == -1) {
6286 continue;
6287 }
6288 SelectedKey sk;
6289 sk.key = idx;
6290 sk.track = E->key().track;
6291 if (selection.has(sk)) {
6292 continue; // Already in selection, don't save.
6293 }
6294
6295 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", E->key().track, newtime);
6296 _AnimMoveRestore amr;
6297
6298 amr.key = animation->track_get_key_value(E->key().track, idx);
6299 amr.track = E->key().track;
6300 amr.time = newtime;
6301 amr.transition = animation->track_get_key_transition(E->key().track, idx);
6302
6303 to_restore.push_back(amr);
6304 }
6305
6306 // 3 - Move the keys (Reinsert them).
6307 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6308 float newpos = E->get().pos + motion;
6309 undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->key().track, newpos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
6310 }
6311
6312 // 4 - (Undo) Remove inserted keys.
6313 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6314 float newpos = E->get().pos + motion;
6315 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->key().track, newpos);
6316 }
6317
6318 // 5 - (Undo) Reinsert keys.
6319 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6320 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
6321 }
6322
6323 // 6 - (Undo) Reinsert overlapped keys.
6324 for (_AnimMoveRestore &amr : to_restore) {
6325 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
6326 }
6327
6328 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
6329 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
6330
6331 // 7 - Reselect.
6332 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6333 float oldpos = E->get().pos;
6334 float newpos = oldpos + motion;
6335
6336 undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
6337 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
6338 }
6339
6340 moving_selection = false;
6341 undo_redo->add_do_method(this, "_redraw_tracks");
6342 undo_redo->add_undo_method(this, "_redraw_tracks");
6343
6344 // Update key frame.
6345 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
6346 if (ape) {
6347 undo_redo->add_do_method(ape, "_animation_update_key_frame");
6348 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
6349 }
6350
6351 undo_redo->commit_action();
6352 }
6353
6354 void AnimationTrackEditor::_move_selection_cancel() {
6355 moving_selection = false;
6356 _redraw_tracks();
6357 }
6358
6359 bool AnimationTrackEditor::is_moving_selection() const {
6360 return moving_selection;
6361 }
6362
6363 float AnimationTrackEditor::get_moving_selection_offset() const {
6364 return moving_selection_offset;
6365 }
6366
6367 void AnimationTrackEditor::_box_selection_draw() {
6368 const Rect2 selection_rect = Rect2(Point2(), box_selection->get_size());
6369 box_selection->draw_rect(selection_rect, get_theme_color(SNAME("box_selection_fill_color"), EditorStringName(Editor)));
6370 box_selection->draw_rect(selection_rect, get_theme_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor)), false, Math::round(EDSCALE));
6371 }
6372
6373 void AnimationTrackEditor::_scroll_input(const Ref<InputEvent> &p_event) {
6374 if (!box_selecting) {
6375 if (panner->gui_input(p_event, scroll->get_global_rect())) {
6376 scroll->accept_event();
6377 return;
6378 }
6379 }
6380
6381 Ref<InputEventMouseButton> mb = p_event;
6382
6383 if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
6384 if (mb->is_pressed()) {
6385 box_selecting = true;
6386 box_selecting_from = scroll->get_global_transform().xform(mb->get_position());
6387 box_select_rect = Rect2();
6388 } else if (box_selecting) {
6389 if (box_selection->is_visible_in_tree()) {
6390 // Only if moved.
6391 for (int i = 0; i < track_edits.size(); i++) {
6392 Rect2 local_rect = box_select_rect;
6393 local_rect.position -= track_edits[i]->get_global_position();
6394 track_edits[i]->append_to_selection(local_rect, mb->is_command_or_control_pressed());
6395 }
6396
6397 if (_get_track_selected() == -1 && track_edits.size() > 0) { // Minimal hack to make shortcuts work.
6398 track_edits[track_edits.size() - 1]->grab_focus();
6399 }
6400 } else if (!mb->is_command_or_control_pressed() && !mb->is_shift_pressed()) {
6401 _clear_selection(true); // Clear it.
6402 }
6403
6404 box_selection->hide();
6405 box_selecting = false;
6406 }
6407 }
6408
6409 Ref<InputEventMouseMotion> mm = p_event;
6410
6411 if (mm.is_valid() && box_selecting) {
6412 if (!mm->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
6413 // No longer.
6414 box_selection->hide();
6415 box_selecting = false;
6416 return;
6417 }
6418
6419 if (!box_selection->is_visible_in_tree()) {
6420 if (!mm->is_command_or_control_pressed() && !mm->is_shift_pressed()) {
6421 _clear_selection(true);
6422 }
6423 box_selection->show();
6424 }
6425
6426 Vector2 from = box_selecting_from;
6427 Vector2 to = scroll->get_global_transform().xform(mm->get_position());
6428
6429 box_selecting_to = to;
6430
6431 if (from.x > to.x) {
6432 SWAP(from.x, to.x);
6433 }
6434
6435 if (from.y > to.y) {
6436 SWAP(from.y, to.y);
6437 }
6438
6439 Rect2 rect(from, to - from);
6440 box_selection->set_rect(Rect2(from - scroll->get_global_position(), rect.get_size()));
6441 box_select_rect = rect;
6442 }
6443 }
6444
6445 void AnimationTrackEditor::_toggle_bezier_edit() {
6446 if (bezier_edit->is_visible()) {
6447 _cancel_bezier_edit();
6448 } else {
6449 int track_count = animation->get_track_count();
6450 for (int i = 0; i < track_count; ++i) {
6451 if (animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
6452 _bezier_edit(i);
6453 return;
6454 }
6455 }
6456 }
6457 }
6458
6459 void AnimationTrackEditor::_scroll_changed(const Vector2 &p_val) {
6460 if (box_selecting) {
6461 const Vector2 scroll_difference = p_val - prev_scroll_position;
6462
6463 Vector2 from = box_selecting_from - scroll_difference;
6464 Vector2 to = box_selecting_to;
6465
6466 box_selecting_from = from;
6467
6468 if (from.x > to.x) {
6469 SWAP(from.x, to.x);
6470 }
6471
6472 if (from.y > to.y) {
6473 SWAP(from.y, to.y);
6474 }
6475
6476 Rect2 rect(from, to - from);
6477 box_selection->set_rect(Rect2(from - scroll->get_global_position(), rect.get_size()));
6478 box_select_rect = rect;
6479 }
6480
6481 prev_scroll_position = p_val;
6482 }
6483
6484 void AnimationTrackEditor::_v_scroll_changed(float p_val) {
6485 _scroll_changed(Vector2(prev_scroll_position.x, p_val));
6486 }
6487
6488 void AnimationTrackEditor::_h_scroll_changed(float p_val) {
6489 _scroll_changed(Vector2(p_val, prev_scroll_position.y));
6490 }
6491
6492 void AnimationTrackEditor::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
6493 Ref<InputEventWithModifiers> iewm = p_event;
6494 if (iewm.is_valid() && iewm->is_alt_pressed()) {
6495 if (p_scroll_vec.x < 0 || p_scroll_vec.y < 0) {
6496 goto_prev_step(true);
6497 } else {
6498 goto_next_step(true);
6499 }
6500 } else {
6501 timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale());
6502 scroll->set_v_scroll(scroll->get_v_scroll() - p_scroll_vec.y);
6503 }
6504 }
6505
6506 void AnimationTrackEditor::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
6507 timeline->_zoom_callback(p_zoom_factor, p_origin, p_event);
6508 }
6509
6510 void AnimationTrackEditor::_cancel_bezier_edit() {
6511 bezier_edit->hide();
6512 box_selection_container->show();
6513 bezier_edit_icon->set_pressed(false);
6514 auto_fit->show();
6515 auto_fit_bezier->hide();
6516 }
6517
6518 void AnimationTrackEditor::_bezier_edit(int p_for_track) {
6519 _clear_selection(); // Bezier probably wants to use a separate selection mode.
6520 bezier_edit->set_root(root);
6521 bezier_edit->set_animation_and_track(animation, p_for_track, read_only);
6522 box_selection_container->hide();
6523 bezier_edit->show();
6524 auto_fit->hide();
6525 auto_fit_bezier->show();
6526 // Search everything within the track and curve - edit it.
6527 }
6528
6529 void AnimationTrackEditor::_bezier_track_set_key_handle_mode(Animation *p_anim, int p_track, int p_index, Animation::HandleMode p_mode, Animation::HandleSetMode p_set_mode) {
6530 ERR_FAIL_NULL(p_anim);
6531 p_anim->bezier_track_set_key_handle_mode(p_track, p_index, p_mode, p_set_mode);
6532 }
6533
6534 void AnimationTrackEditor::_bezier_track_set_key_handle_mode_at_time(Animation *p_anim, int p_track, float p_time, Animation::HandleMode p_mode, Animation::HandleSetMode p_set_mode) {
6535 ERR_FAIL_NULL(p_anim);
6536 int index = p_anim->track_find_key(p_track, p_time, Animation::FIND_MODE_APPROX);
6537 ERR_FAIL_COND(index < 0);
6538 _bezier_track_set_key_handle_mode(p_anim, p_track, index, p_mode, p_set_mode);
6539 }
6540
6541 void AnimationTrackEditor::_anim_duplicate_keys(float p_ofs, bool p_ofs_valid, int p_track) {
6542 if (selection.size() && animation.is_valid()) {
6543 int top_track = 0x7FFFFFFF;
6544 float top_time = 1e10;
6545 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6546 const SelectedKey &sk = E->key();
6547
6548 float t = animation->track_get_key_time(sk.track, sk.key);
6549 if (t < top_time) {
6550 top_time = t;
6551 }
6552 if (sk.track < top_track) {
6553 top_track = sk.track;
6554 }
6555 }
6556 ERR_FAIL_COND(top_track == 0x7FFFFFFF || top_time == 1e10);
6557
6558 int start_track = p_track;
6559 if (p_track == -1) { // Duplicating from shortcut or Edit menu.
6560 bool is_valid_track_selected = _get_track_selected() >= 0 && _get_track_selected() < animation->get_track_count();
6561 start_track = is_valid_track_selected ? _get_track_selected() : top_track;
6562 }
6563
6564 bool all_compatible = true;
6565
6566 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6567 const SelectedKey &sk = E->key();
6568 int dst_track = sk.track + (start_track - top_track);
6569
6570 if (dst_track < 0 || dst_track >= animation->get_track_count()) {
6571 all_compatible = false;
6572 break;
6573 }
6574
6575 Variant::Type value_type = animation->track_get_key_value(sk.track, sk.key).get_type();
6576 Animation::TrackType track_type = animation->track_get_type(sk.track);
6577 if (!_is_track_compatible(dst_track, value_type, track_type)) {
6578 all_compatible = false;
6579 break;
6580 }
6581 }
6582
6583 ERR_FAIL_COND_MSG(!all_compatible, "Duplicate failed: Not all animation keys were compatible with their target tracks");
6584
6585 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
6586 undo_redo->create_action(TTR("Animation Duplicate Keys"));
6587
6588 List<Pair<int, float>> new_selection_values;
6589
6590 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6591 const SelectedKey &sk = E->key();
6592
6593 float t = animation->track_get_key_time(sk.track, sk.key);
6594 float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
6595
6596 if (p_ofs_valid) {
6597 if (snap_keys->is_pressed() && step->get_value() != 0) {
6598 insert_pos = snap_time(insert_pos);
6599 }
6600 }
6601
6602 float dst_time = t + (insert_pos - top_time);
6603 int dst_track = sk.track + (start_track - top_track);
6604
6605 if (dst_track < 0 || dst_track >= animation->get_track_count()) {
6606 continue;
6607 }
6608
6609 int existing_idx = animation->track_find_key(dst_track, dst_time, Animation::FIND_MODE_APPROX);
6610
6611 Variant value = animation->track_get_key_value(sk.track, sk.key);
6612 bool key_is_bezier = animation->track_get_type(sk.track) == Animation::TYPE_BEZIER;
6613 bool track_is_bezier = animation->track_get_type(dst_track) == Animation::TYPE_BEZIER;
6614 if (key_is_bezier && !track_is_bezier) {
6615 value = AnimationBezierTrackEdit::get_bezier_key_value(value);
6616 } else if (!key_is_bezier && track_is_bezier) {
6617 value = animation->make_default_bezier_key(value);
6618 }
6619
6620 undo_redo->add_do_method(animation.ptr(), "track_insert_key", dst_track, dst_time, value, animation->track_get_key_transition(E->key().track, E->key().key));
6621 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", dst_track, dst_time);
6622
6623 Pair<int, float> p;
6624 p.first = dst_track;
6625 p.second = dst_time;
6626 new_selection_values.push_back(p);
6627
6628 if (existing_idx != -1) {
6629 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", dst_track, dst_time, animation->track_get_key_value(dst_track, existing_idx), animation->track_get_key_transition(dst_track, existing_idx));
6630 }
6631 }
6632
6633 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
6634 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
6635
6636 // Reselect duplicated.
6637 RBMap<SelectedKey, KeyInfo> new_selection;
6638 for (const Pair<int, float> &E : new_selection_values) {
6639 undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second);
6640 }
6641 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6642 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, E->get().pos);
6643 }
6644
6645 undo_redo->add_do_method(this, "_redraw_tracks");
6646 undo_redo->add_undo_method(this, "_redraw_tracks");
6647 undo_redo->commit_action();
6648 }
6649 }
6650
6651 void AnimationTrackEditor::_anim_copy_keys(bool p_cut) {
6652 if (is_selection_active() && animation.is_valid()) {
6653 int top_track = 0x7FFFFFFF;
6654 float top_time = 1e10;
6655
6656 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6657 const SelectedKey &sk = E->key();
6658
6659 float t = animation->track_get_key_time(sk.track, sk.key);
6660 if (t < top_time) {
6661 top_time = t;
6662 }
6663 if (sk.track < top_track) {
6664 top_track = sk.track;
6665 }
6666 }
6667
6668 ERR_FAIL_COND(top_track == 0x7FFFFFFF || top_time == 1e10);
6669
6670 _set_key_clipboard(top_track, top_time, selection);
6671
6672 if (p_cut) {
6673 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
6674 undo_redo->create_action(TTR("Animation Cut Keys"), UndoRedo::MERGE_DISABLE, animation.ptr());
6675 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
6676 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
6677 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6678 int track_idx = E->key().track;
6679 int key_idx = E->key().key;
6680 float time = E->value().pos;
6681 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", track_idx, time);
6682 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track_idx, time, animation->track_get_key_value(track_idx, key_idx), animation->track_get_key_transition(track_idx, key_idx));
6683 }
6684 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6685 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, E->value().pos);
6686 }
6687 undo_redo->commit_action();
6688 }
6689 }
6690 }
6691
6692 void AnimationTrackEditor::_set_key_clipboard(int p_top_track, float p_top_time, RBMap<SelectedKey, KeyInfo> &p_keys) {
6693 key_clipboard.keys.clear();
6694 key_clipboard.top_track = p_top_track;
6695 for (RBMap<SelectedKey, KeyInfo>::Element *E = p_keys.back(); E; E = E->prev()) {
6696 KeyClipboard::Key k;
6697 k.value = animation->track_get_key_value(E->key().track, E->key().key);
6698 k.transition = animation->track_get_key_transition(E->key().track, E->key().key);
6699 k.time = E->value().pos - p_top_time;
6700 k.track = E->key().track - p_top_track;
6701 k.track_type = animation->track_get_type(E->key().track);
6702
6703 key_clipboard.keys.push_back(k);
6704 }
6705 }
6706
6707 void AnimationTrackEditor::_anim_paste_keys(float p_ofs, bool p_ofs_valid, int p_track) {
6708 if (is_key_clipboard_active() && animation.is_valid()) {
6709 int start_track = p_track;
6710 if (p_track == -1) { // Pasting from shortcut or Edit menu.
6711 bool is_valid_track_selected = _get_track_selected() >= 0 && _get_track_selected() < animation->get_track_count();
6712 start_track = is_valid_track_selected ? _get_track_selected() : key_clipboard.top_track;
6713 }
6714
6715 bool all_compatible = true;
6716
6717 for (int i = 0; i < key_clipboard.keys.size(); i++) {
6718 const KeyClipboard::Key key = key_clipboard.keys[i];
6719
6720 int dst_track = key.track + start_track;
6721
6722 if (dst_track < 0 || dst_track >= animation->get_track_count()) {
6723 all_compatible = false;
6724 break;
6725 }
6726
6727 if (!_is_track_compatible(dst_track, key.value.get_type(), key.track_type)) {
6728 all_compatible = false;
6729 break;
6730 }
6731 }
6732
6733 ERR_FAIL_COND_MSG(!all_compatible, "Paste failed: Not all animation keys were compatible with their target tracks");
6734
6735 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
6736 undo_redo->create_action(TTR("Animation Paste Keys"));
6737 List<Pair<int, float>> new_selection_values;
6738
6739 for (int i = 0; i < key_clipboard.keys.size(); i++) {
6740 const KeyClipboard::Key key = key_clipboard.keys[i];
6741
6742 float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
6743
6744 if (p_ofs_valid) {
6745 if (snap_keys->is_pressed() && step->get_value() != 0) {
6746 insert_pos = snap_time(insert_pos);
6747 }
6748 }
6749
6750 float dst_time = key.time + insert_pos;
6751 int dst_track = key.track + start_track;
6752
6753 int existing_idx = animation->track_find_key(dst_track, dst_time, Animation::FIND_MODE_APPROX);
6754
6755 Variant value = key.value;
6756 bool key_is_bezier = key.track_type == Animation::TYPE_BEZIER;
6757 bool track_is_bezier = animation->track_get_type(dst_track) == Animation::TYPE_BEZIER;
6758 if (key_is_bezier && !track_is_bezier) {
6759 value = AnimationBezierTrackEdit::get_bezier_key_value(value);
6760 } else if (!key_is_bezier && track_is_bezier) {
6761 value = animation->make_default_bezier_key(value);
6762 }
6763
6764 undo_redo->add_do_method(animation.ptr(), "track_insert_key", dst_track, dst_time, value, key.transition);
6765 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", dst_track, dst_time);
6766
6767 Pair<int, float> p;
6768 p.first = dst_track;
6769 p.second = dst_time;
6770 new_selection_values.push_back(p);
6771
6772 if (existing_idx != -1) {
6773 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", dst_track, dst_time, animation->track_get_key_value(dst_track, existing_idx), animation->track_get_key_transition(dst_track, existing_idx));
6774 }
6775 }
6776
6777 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
6778 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
6779
6780 // Reselect pasted.
6781 for (const Pair<int, float> &E : new_selection_values) {
6782 undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second);
6783 }
6784 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
6785 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, E->get().pos);
6786 }
6787
6788 undo_redo->add_do_method(this, "_redraw_tracks");
6789 undo_redo->add_undo_method(this, "_redraw_tracks");
6790 undo_redo->commit_action();
6791 }
6792 }
6793
6794 bool AnimationTrackEditor::_is_track_compatible(int p_target_track_idx, Variant::Type p_source_value_type, Animation::TrackType p_source_track_type) {
6795 if (animation.is_valid()) {
6796 Animation::TrackType target_track_type = animation->track_get_type(p_target_track_idx);
6797 bool track_types_equal = target_track_type == p_source_track_type;
6798 bool is_source_vector3_type = p_source_track_type == Animation::TYPE_POSITION_3D || p_source_track_type == Animation::TYPE_SCALE_3D || p_source_track_type == Animation::TYPE_ROTATION_3D;
6799 bool is_source_bezier = p_source_track_type == Animation::TYPE_BEZIER;
6800 switch (target_track_type) {
6801 case Animation::TYPE_POSITION_3D:
6802 case Animation::TYPE_SCALE_3D:
6803 return p_source_value_type == Variant::VECTOR3;
6804 case Animation::TYPE_ROTATION_3D:
6805 return p_source_value_type == Variant::QUATERNION;
6806 case Animation::TYPE_BEZIER:
6807 return track_types_equal || p_source_value_type == Variant::FLOAT;
6808 case Animation::TYPE_VALUE:
6809 if (track_types_equal || is_source_vector3_type || is_source_bezier) {
6810 bool path_valid = false;
6811 Variant::Type property_type = Variant::NIL;
6812
6813 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
6814 if (ape) {
6815 AnimationPlayer *ap = ape->get_player();
6816 if (ap) {
6817 NodePath npath = animation->track_get_path(p_target_track_idx);
6818 Node *a_ap_root_node = ap->get_node(ap->get_root_node());
6819 Node *nd = nullptr;
6820 // We must test that we have a valid a_ap_root_node before trying to access its content to init the nd Node.
6821 if (a_ap_root_node) {
6822 nd = a_ap_root_node->get_node(NodePath(npath.get_concatenated_names()));
6823 }
6824 if (nd) {
6825 StringName prop = npath.get_concatenated_subnames();
6826 PropertyInfo prop_info;
6827 path_valid = ClassDB::get_property_info(nd->get_class(), prop, &prop_info);
6828 property_type = prop_info.type;
6829 }
6830 }
6831 }
6832
6833 if (path_valid) {
6834 if (is_source_bezier) {
6835 p_source_value_type = Variant::FLOAT;
6836 }
6837 return property_type == p_source_value_type;
6838 } else {
6839 if (animation->track_get_key_count(p_target_track_idx) > 0) {
6840 Variant::Type first_key_type = animation->track_get_key_value(p_target_track_idx, 0).get_type();
6841 return first_key_type == p_source_value_type;
6842 }
6843 return true; // Type is Undefined.
6844 }
6845 }
6846 return false;
6847 default: // Works for TYPE_ANIMATION; TYPE_AUDIO; TYPE_CALL_METHOD; BLEND_SHAPE.
6848 return track_types_equal;
6849 }
6850 }
6851 return false;
6852 }
6853
6854 void AnimationTrackEditor::_edit_menu_about_to_popup() {
6855 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
6856 edit->get_popup()->set_item_disabled(edit->get_popup()->get_item_index(EDIT_ADD_RESET_KEY), !can_add_reset_key() || animation == player->get_animation(SceneStringName(RESET)));
6857 edit->get_popup()->set_item_disabled(edit->get_popup()->get_item_index(EDIT_APPLY_RESET), !player->can_apply_reset());
6858
6859 bool has_length = false;
6860 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
6861 if (animation->track_get_type(E.key.track) == Animation::TYPE_AUDIO && animation->audio_track_get_key_stream(E.key.track, E.key.key).is_valid()) {
6862 has_length = true;
6863 break;
6864 }
6865 }
6866 edit->get_popup()->set_item_disabled(edit->get_popup()->get_item_index(EDIT_SET_START_OFFSET), !has_length);
6867 edit->get_popup()->set_item_disabled(edit->get_popup()->get_item_index(EDIT_SET_END_OFFSET), !has_length);
6868 }
6869
6870 void AnimationTrackEditor::goto_prev_step(bool p_from_mouse_event) {
6871 if (animation.is_null()) {
6872 return;
6873 }
6874 float anim_step = animation->get_step();
6875 if (anim_step == 0.0) {
6876 anim_step = 1.0;
6877 }
6878 if (p_from_mouse_event && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
6879 // Use more precise snapping when holding Shift.
6880 // This is used when scrobbling the timeline using Alt + Mouse wheel.
6881 anim_step *= 0.25;
6882 }
6883
6884 float pos = timeline->get_play_position();
6885 pos = Math::snapped(pos - anim_step, anim_step);
6886 if (pos < 0.0) {
6887 pos = 0.0;
6888 }
6889 set_anim_pos(pos);
6890 _timeline_changed(pos, false);
6891 }
6892
6893 void AnimationTrackEditor::goto_next_step(bool p_from_mouse_event, bool p_timeline_only) {
6894 if (animation.is_null()) {
6895 return;
6896 }
6897 float anim_step = animation->get_step();
6898 if (anim_step == 0.0) {
6899 anim_step = 1.0;
6900 }
6901 if (p_from_mouse_event && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
6902 // Use more precise snapping when holding Shift.
6903 // This is used when scrobbling the timeline using Alt + Mouse wheel.
6904 // Do not use precise snapping when using the menu action or keyboard shortcut,
6905 // as the default keyboard shortcut requires pressing Shift.
6906 anim_step *= 0.25;
6907 }
6908
6909 float pos = timeline->get_play_position();
6910
6911 pos = Math::snapped(pos + anim_step, anim_step);
6912 if (pos > animation->get_length()) {
6913 pos = animation->get_length();
6914 }
6915 set_anim_pos(pos);
6916
6917 _timeline_changed(pos, p_timeline_only);
6918 }
6919
6920 void AnimationTrackEditor::_edit_menu_pressed(int p_option) {
6921 switch (p_option) {
6922 case EDIT_COPY_TRACKS: {
6923 track_copy_select->clear();
6924 TreeItem *troot = track_copy_select->create_item();
6925
6926 for (int i = 0; i < animation->get_track_count(); i++) {
6927 NodePath path = animation->track_get_path(i);
6928 Node *node = nullptr;
6929
6930 if (root) {
6931 node = root->get_node_or_null(path);
6932 }
6933
6934 String text;
6935 Ref<Texture2D> icon = get_editor_theme_icon(SNAME("Node"));
6936 if (node) {
6937 if (has_theme_icon(node->get_class(), EditorStringName(EditorIcons))) {
6938 icon = get_editor_theme_icon(node->get_class());
6939 }
6940
6941 text = node->get_name();
6942 Vector<StringName> sn = path.get_subnames();
6943 for (int j = 0; j < sn.size(); j++) {
6944 text += ".";
6945 text += sn[j];
6946 }
6947
6948 path = NodePath(node->get_path().get_names(), path.get_subnames(), true); // Store full path instead for copying.
6949 } else {
6950 text = String(path);
6951 int sep = text.find_char(':');
6952 if (sep != -1) {
6953 text = text.substr(sep + 1);
6954 }
6955 }
6956
6957 String track_type;
6958 switch (animation->track_get_type(i)) {
6959 case Animation::TYPE_POSITION_3D:
6960 track_type = TTR("Position");
6961 break;
6962 case Animation::TYPE_ROTATION_3D:
6963 track_type = TTR("Rotation");
6964 break;
6965 case Animation::TYPE_SCALE_3D:
6966 track_type = TTR("Scale");
6967 break;
6968 case Animation::TYPE_BLEND_SHAPE:
6969 track_type = TTR("BlendShape");
6970 break;
6971 case Animation::TYPE_METHOD:
6972 track_type = TTR("Methods");
6973 break;
6974 case Animation::TYPE_BEZIER:
6975 track_type = TTR("Bezier");
6976 break;
6977 case Animation::TYPE_AUDIO:
6978 track_type = TTR("Audio");
6979 break;
6980 default: {
6981 };
6982 }
6983 if (!track_type.is_empty()) {
6984 text += vformat(" (%s)", track_type);
6985 }
6986
6987 TreeItem *it = track_copy_select->create_item(troot);
6988 it->set_editable(0, true);
6989 it->set_selectable(0, true);
6990 it->set_cell_mode(0, TreeItem::CELL_MODE_CHECK);
6991 it->set_icon(0, icon);
6992 it->set_text(0, text);
6993 Dictionary md;
6994 md["track_idx"] = i;
6995 md["path"] = path;
6996 it->set_metadata(0, md);
6997 }
6998
6999 track_copy_dialog->popup_centered(Size2(350, 500) * EDSCALE);
7000 } break;
7001 case EDIT_COPY_TRACKS_CONFIRM: {
7002 track_clipboard.clear();
7003 TreeItem *tree_root = track_copy_select->get_root();
7004 if (tree_root) {
7005 TreeItem *it = tree_root->get_first_child();
7006 while (it) {
7007 Dictionary md = it->get_metadata(0);
7008 int idx = md["track_idx"];
7009 if (it->is_checked(0) && idx >= 0 && idx < animation->get_track_count()) {
7010 TrackClipboard tc;
7011 tc.base_path = animation->track_get_path(idx);
7012 tc.full_path = md["path"];
7013 tc.track_type = animation->track_get_type(idx);
7014 tc.interp_type = animation->track_get_interpolation_type(idx);
7015 if (tc.track_type == Animation::TYPE_VALUE) {
7016 tc.update_mode = animation->value_track_get_update_mode(idx);
7017 }
7018 if (tc.track_type == Animation::TYPE_AUDIO) {
7019 tc.use_blend = animation->audio_track_is_use_blend(idx);
7020 }
7021 tc.loop_wrap = animation->track_get_interpolation_loop_wrap(idx);
7022 tc.enabled = animation->track_is_enabled(idx);
7023 for (int i = 0; i < animation->track_get_key_count(idx); i++) {
7024 TrackClipboard::Key k;
7025 k.time = animation->track_get_key_time(idx, i);
7026 k.value = animation->track_get_key_value(idx, i);
7027 k.transition = animation->track_get_key_transition(idx, i);
7028 tc.keys.push_back(k);
7029 }
7030 track_clipboard.push_back(tc);
7031 }
7032 it = it->get_next();
7033 }
7034 }
7035 } break;
7036 case EDIT_PASTE_TRACKS: {
7037 if (track_clipboard.is_empty()) {
7038 EditorNode::get_singleton()->show_warning(TTR("Clipboard is empty!"));
7039 break;
7040 }
7041
7042 int base_track = animation->get_track_count();
7043 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7044 undo_redo->create_action(TTR("Paste Tracks"));
7045 for (int i = 0; i < track_clipboard.size(); i++) {
7046 undo_redo->add_do_method(animation.ptr(), "add_track", track_clipboard[i].track_type);
7047 Node *exists = nullptr;
7048 NodePath path = track_clipboard[i].base_path;
7049
7050 if (root) {
7051 NodePath np = track_clipboard[i].full_path;
7052 exists = root->get_node_or_null(np);
7053 if (exists) {
7054 path = NodePath(root->get_path_to(exists).get_names(), track_clipboard[i].full_path.get_subnames(), false);
7055 }
7056 }
7057
7058 undo_redo->add_do_method(animation.ptr(), "track_set_path", base_track, path);
7059 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", base_track, track_clipboard[i].interp_type);
7060 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_loop_wrap", base_track, track_clipboard[i].loop_wrap);
7061 undo_redo->add_do_method(animation.ptr(), "track_set_enabled", base_track, track_clipboard[i].enabled);
7062 if (track_clipboard[i].track_type == Animation::TYPE_VALUE) {
7063 undo_redo->add_do_method(animation.ptr(), "value_track_set_update_mode", base_track, track_clipboard[i].update_mode);
7064 }
7065 if (track_clipboard[i].track_type == Animation::TYPE_AUDIO) {
7066 undo_redo->add_do_method(animation.ptr(), "audio_track_set_use_blend", base_track, track_clipboard[i].use_blend);
7067 }
7068
7069 for (int j = 0; j < track_clipboard[i].keys.size(); j++) {
7070 undo_redo->add_do_method(animation.ptr(), "track_insert_key", base_track, track_clipboard[i].keys[j].time, track_clipboard[i].keys[j].value, track_clipboard[i].keys[j].transition);
7071 }
7072
7073 undo_redo->add_undo_method(animation.ptr(), "remove_track", animation->get_track_count());
7074
7075 base_track++;
7076 }
7077
7078 undo_redo->commit_action();
7079 } break;
7080 case EDIT_SCALE_SELECTION: {
7081 scale_dialog->popup_centered(Size2(200, 100) * EDSCALE);
7082 scale->get_line_edit()->grab_focus();
7083 scale_from_cursor = false;
7084 } break;
7085 case EDIT_SCALE_FROM_CURSOR: {
7086 scale_dialog->popup_centered(Size2(200, 100) * EDSCALE);
7087 scale->get_line_edit()->grab_focus();
7088 scale_from_cursor = true;
7089 } break;
7090 case EDIT_SCALE_CONFIRM: {
7091 if (selection.is_empty()) {
7092 return;
7093 }
7094
7095 float from_t = 1e20;
7096 float to_t = -1e20;
7097 float len = -1e20;
7098 float pivot = 0;
7099
7100 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7101 float t = animation->track_get_key_time(E.key.track, E.key.key);
7102 if (t < from_t) {
7103 from_t = t;
7104 }
7105 if (t > to_t) {
7106 to_t = t;
7107 }
7108 }
7109
7110 len = to_t - from_t;
7111 if (scale_from_cursor) {
7112 pivot = timeline->get_play_position();
7113 } else {
7114 pivot = from_t;
7115 }
7116
7117 float s = scale->get_value();
7118 ERR_FAIL_COND_MSG(s == 0, "Can't scale to 0.");
7119
7120 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7121 undo_redo->create_action(TTR("Animation Scale Keys"));
7122
7123 List<_AnimMoveRestore> to_restore;
7124
7125 // 1 - Remove the keys.
7126 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7127 undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
7128 }
7129 // 2 - Remove overlapped keys.
7130 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7131 float newtime = (E->get().pos - from_t) * s + from_t;
7132 int idx = animation->track_find_key(E->key().track, newtime, Animation::FIND_MODE_APPROX);
7133 if (idx == -1) {
7134 continue;
7135 }
7136 SelectedKey sk;
7137 sk.key = idx;
7138 sk.track = E->key().track;
7139 if (selection.has(sk)) {
7140 continue; // Already in selection, don't save.
7141 }
7142
7143 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", E->key().track, newtime);
7144 _AnimMoveRestore amr;
7145
7146 amr.key = animation->track_get_key_value(E->key().track, idx);
7147 amr.track = E->key().track;
7148 amr.time = newtime;
7149 amr.transition = animation->track_get_key_transition(E->key().track, idx);
7150
7151 to_restore.push_back(amr);
7152 }
7153
7154 #define NEW_POS(m_ofs) (((s > 0) ? m_ofs : from_t + (len - (m_ofs - from_t))) - pivot) * Math::abs(s) + pivot
7155 // 3 - Move the keys (re insert them).
7156 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7157 float newpos = NEW_POS(E->get().pos);
7158 undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->key().track, newpos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
7159 }
7160
7161 // 4 - (Undo) Remove inserted keys.
7162 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7163 float newpos = NEW_POS(E->get().pos);
7164 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->key().track, newpos);
7165 }
7166
7167 // 5 - (Undo) Reinsert keys.
7168 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7169 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
7170 }
7171
7172 // 6 - (Undo) Reinsert overlapped keys.
7173 for (_AnimMoveRestore &amr : to_restore) {
7174 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, amr.transition);
7175 }
7176
7177 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7178 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7179
7180 // 7 - Reselect.
7181 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7182 float oldpos = E->get().pos;
7183 float newpos = NEW_POS(oldpos);
7184 if (newpos >= 0) {
7185 undo_redo->add_do_method(this, "_select_at_anim", animation, E->key().track, newpos);
7186 }
7187 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, oldpos);
7188 }
7189 #undef NEW_POS
7190
7191 undo_redo->add_do_method(this, "_redraw_tracks");
7192 undo_redo->add_undo_method(this, "_redraw_tracks");
7193 undo_redo->commit_action();
7194 } break;
7195
7196 case EDIT_SET_START_OFFSET: {
7197 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7198 undo_redo->create_action(TTR("Animation Set Start Offset"), UndoRedo::MERGE_ENDS);
7199 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7200 if (animation->track_get_type(E.key.track) != Animation::TYPE_AUDIO) {
7201 continue;
7202 }
7203 Ref<AudioStream> stream = animation->audio_track_get_key_stream(E.key.track, E.key.key);
7204 if (stream.is_null()) {
7205 continue;
7206 }
7207 double len = stream->get_length() - animation->audio_track_get_key_end_offset(E.key.track, E.key.key);
7208 real_t prev_offset = animation->audio_track_get_key_start_offset(E.key.track, E.key.key);
7209 double prev_time = animation->track_get_key_time(E.key.track, E.key.key);
7210 float cur_time = timeline->get_play_position();
7211 float diff = prev_offset + cur_time - prev_time;
7212 float destination = cur_time - MIN(0, diff);
7213 if (diff >= len || animation->track_find_key(E.key.track, destination, Animation::FIND_MODE_EXACT) >= 0) {
7214 continue;
7215 }
7216 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_start_offset", E.key.track, E.key.key, diff);
7217 undo_redo->add_do_method(animation.ptr(), "track_set_key_time", E.key.track, E.key.key, destination);
7218 undo_redo->add_undo_method(animation.ptr(), "track_set_key_time", E.key.track, E.key.key, prev_time);
7219 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_start_offset", E.key.track, E.key.key, prev_offset);
7220 }
7221 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7222 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7223 undo_redo->commit_action();
7224 } break;
7225 case EDIT_SET_END_OFFSET: {
7226 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7227 undo_redo->create_action(TTR("Animation Set End Offset"), UndoRedo::MERGE_ENDS);
7228 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7229 if (animation->track_get_type(E.key.track) != Animation::TYPE_AUDIO) {
7230 continue;
7231 }
7232 Ref<AudioStream> stream = animation->audio_track_get_key_stream(E.key.track, E.key.key);
7233 if (stream.is_null()) {
7234 continue;
7235 }
7236 double len = stream->get_length() - animation->audio_track_get_key_start_offset(E.key.track, E.key.key);
7237 real_t prev_offset = animation->audio_track_get_key_end_offset(E.key.track, E.key.key);
7238 double prev_time = animation->track_get_key_time(E.key.track, E.key.key);
7239 float cur_time = timeline->get_play_position();
7240 float diff = prev_time + len - cur_time;
7241 if (diff >= len) {
7242 continue;
7243 }
7244 undo_redo->add_do_method(animation.ptr(), "audio_track_set_key_end_offset", E.key.track, E.key.key, diff);
7245 undo_redo->add_undo_method(animation.ptr(), "audio_track_set_key_end_offset", E.key.track, E.key.key, prev_offset);
7246 }
7247 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7248 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7249 undo_redo->commit_action();
7250 } break;
7251
7252 case EDIT_EASE_SELECTION: {
7253 ease_dialog->popup_centered(Size2(200, 100) * EDSCALE);
7254 } break;
7255 case EDIT_EASE_CONFIRM: {
7256 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7257 undo_redo->create_action(TTR("Make Easing Keys"));
7258
7259 Tween::TransitionType transition_type = static_cast<Tween::TransitionType>(transition_selection->get_selected_id());
7260 Tween::EaseType ease_type = static_cast<Tween::EaseType>(ease_selection->get_selected_id());
7261 float fps = ease_fps->get_value();
7262 double dur_step = 1.0 / fps;
7263
7264 // Organize track and key.
7265 HashMap<int, Vector<int>> keymap;
7266 Vector<int> tracks;
7267 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7268 if (!tracks.has(E.key.track)) {
7269 tracks.append(E.key.track);
7270 }
7271 }
7272 for (int i = 0; i < tracks.size(); i++) {
7273 switch (animation->track_get_type(tracks[i])) {
7274 case Animation::TYPE_VALUE:
7275 case Animation::TYPE_POSITION_3D:
7276 case Animation::TYPE_ROTATION_3D:
7277 case Animation::TYPE_SCALE_3D:
7278 case Animation::TYPE_BLEND_SHAPE: {
7279 Vector<int> keys;
7280 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7281 if (E.key.track == tracks[i]) {
7282 keys.append(E.key.key);
7283 }
7284 }
7285 keys.sort();
7286 keymap.insert(tracks[i], keys);
7287 } break;
7288 default: {
7289 } break;
7290 }
7291 }
7292
7293 // Make easing.
7294 HashMap<int, Vector<int>>::Iterator E = keymap.begin();
7295 while (E) {
7296 int track = E->key;
7297 Vector<int> keys = E->value;
7298 int len = keys.size() - 1;
7299
7300 // Special case for angle interpolation.
7301 bool is_using_angle = animation->track_get_interpolation_type(track) == Animation::INTERPOLATION_LINEAR_ANGLE || animation->track_get_interpolation_type(track) == Animation::INTERPOLATION_CUBIC_ANGLE;
7302
7303 // Make insert queue.
7304 Vector<Pair<real_t, Variant>> insert_queue_new;
7305 for (int i = 0; i < len; i++) {
7306 // Check neighboring keys.
7307 if (keys[i] + 1 == keys[i + 1]) {
7308 double from_t = animation->track_get_key_time(track, keys[i]);
7309 double to_t = animation->track_get_key_time(track, keys[i + 1]);
7310 Variant from_v = animation->track_get_key_value(track, keys[i]);
7311 Variant to_v = animation->track_get_key_value(track, keys[i + 1]);
7312 if (is_using_angle) {
7313 real_t a = from_v;
7314 real_t b = to_v;
7315 real_t to_diff = std::fmod(b - a, Math::TAU);
7316 to_v = a + std::fmod(2.0 * to_diff, Math::TAU) - to_diff;
7317 }
7318 Variant delta_v = Animation::subtract_variant(to_v, from_v);
7319 double duration = to_t - from_t;
7320 double fixed_duration = duration - UNIT_EPSILON; // Prevent to overwrap keys...
7321 for (double delta_t = dur_step; delta_t < fixed_duration; delta_t += dur_step) {
7322 Pair<real_t, Variant> keydata;
7323 keydata.first = from_t + delta_t;
7324 keydata.second = Tween::interpolate_variant(from_v, delta_v, delta_t, duration, transition_type, ease_type);
7325 insert_queue_new.append(keydata);
7326 }
7327 }
7328 }
7329
7330 // Do insertion.
7331 for (int i = 0; i < insert_queue_new.size(); i++) {
7332 undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, insert_queue_new[i].first, insert_queue_new[i].second);
7333 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", track, insert_queue_new[i].first);
7334 }
7335
7336 ++E;
7337 }
7338
7339 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7340 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7341 undo_redo->add_do_method(this, "_redraw_tracks");
7342 undo_redo->add_undo_method(this, "_redraw_tracks");
7343 undo_redo->commit_action();
7344 _update_key_edit();
7345
7346 } break;
7347
7348 case EDIT_DUPLICATE_SELECTED_KEYS: {
7349 if (bezier_edit->is_visible()) {
7350 bezier_edit->duplicate_selected_keys(-1.0, false);
7351 break;
7352 }
7353 _anim_duplicate_keys(-1.0, false, -1.0);
7354 } break;
7355 case EDIT_CUT_KEYS: {
7356 if (bezier_edit->is_visible()) {
7357 bezier_edit->copy_selected_keys(true);
7358 break;
7359 }
7360 _anim_copy_keys(true);
7361 } break;
7362 case EDIT_COPY_KEYS: {
7363 if (bezier_edit->is_visible()) {
7364 bezier_edit->copy_selected_keys(false);
7365 break;
7366 }
7367 _anim_copy_keys(false);
7368 } break;
7369 case EDIT_PASTE_KEYS: {
7370 _anim_paste_keys(-1.0, false, -1.0);
7371 } break;
7372 case EDIT_MOVE_FIRST_SELECTED_KEY_TO_CURSOR: {
7373 if (moving_selection || selection.is_empty()) {
7374 break;
7375 }
7376 real_t from_t = 1e20;
7377 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7378 real_t t = animation->track_get_key_time(E.key.track, E.key.key);
7379 if (t < from_t) {
7380 from_t = t;
7381 }
7382 }
7383 _move_selection_begin();
7384 _move_selection(timeline->get_play_position() - from_t);
7385 _move_selection_commit();
7386 } break;
7387 case EDIT_MOVE_LAST_SELECTED_KEY_TO_CURSOR: {
7388 if (moving_selection || selection.is_empty()) {
7389 break;
7390 }
7391 real_t to_t = -1e20;
7392 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7393 real_t t = animation->track_get_key_time(E.key.track, E.key.key);
7394 if (t > to_t) {
7395 to_t = t;
7396 }
7397 }
7398 _move_selection_begin();
7399 _move_selection(timeline->get_play_position() - to_t);
7400 _move_selection_commit();
7401 } break;
7402 case EDIT_ADD_RESET_KEY: {
7403 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7404 undo_redo->create_action(TTR("Animation Add RESET Keys"));
7405
7406 Ref<Animation> reset = _create_and_get_reset_animation();
7407 int reset_tracks = reset->get_track_count();
7408 HashSet<int> tracks_added;
7409
7410 for (const KeyValue<SelectedKey, KeyInfo> &E : selection) {
7411 const SelectedKey &sk = E.key;
7412
7413 const Animation::TrackType track_type = animation->track_get_type(E.key.track);
7414 if (track_type == Animation::TYPE_ANIMATION || track_type == Animation::TYPE_AUDIO || track_type == Animation::TYPE_METHOD) {
7415 continue;
7416 }
7417
7418 // Only add one key per track.
7419 if (tracks_added.has(sk.track)) {
7420 continue;
7421 }
7422 tracks_added.insert(sk.track);
7423
7424 int dst_track = -1;
7425
7426 const NodePath &path = animation->track_get_path(sk.track);
7427 for (int i = 0; i < reset->get_track_count(); i++) {
7428 if (reset->track_get_path(i) == path) {
7429 dst_track = i;
7430 break;
7431 }
7432 }
7433
7434 int existing_idx = -1;
7435 if (dst_track == -1) {
7436 // If adding multiple tracks, make sure that correct track is referenced.
7437 dst_track = reset_tracks;
7438 reset_tracks++;
7439
7440 undo_redo->add_do_method(reset.ptr(), "add_track", animation->track_get_type(sk.track));
7441 undo_redo->add_do_method(reset.ptr(), "track_set_path", dst_track, path);
7442 undo_redo->add_undo_method(reset.ptr(), "remove_track", dst_track);
7443 } else {
7444 existing_idx = reset->track_find_key(dst_track, 0, Animation::FIND_MODE_APPROX);
7445 }
7446
7447 if (animation->track_get_type(sk.track) == Animation::TYPE_VALUE) {
7448 undo_redo->add_do_method(reset.ptr(), "value_track_set_update_mode", dst_track, animation->value_track_get_update_mode(sk.track));
7449 }
7450 if (animation->track_get_type(sk.track) == Animation::TYPE_AUDIO) {
7451 undo_redo->add_do_method(reset.ptr(), "audio_track_set_use_blend", dst_track, animation->audio_track_is_use_blend(sk.track));
7452 }
7453 undo_redo->add_do_method(reset.ptr(), "track_set_interpolation_type", dst_track, animation->track_get_interpolation_type(sk.track));
7454 undo_redo->add_do_method(reset.ptr(), "track_set_interpolation_loop_wrap", dst_track, animation->track_get_interpolation_loop_wrap(sk.track));
7455
7456 undo_redo->add_do_method(reset.ptr(), "track_insert_key", dst_track, 0, animation->track_get_key_value(sk.track, sk.key), animation->track_get_key_transition(sk.track, sk.key));
7457 undo_redo->add_undo_method(reset.ptr(), "track_remove_key_at_time", dst_track, 0);
7458
7459 if (existing_idx != -1) {
7460 undo_redo->add_undo_method(reset.ptr(), "track_insert_key", dst_track, 0, reset->track_get_key_value(dst_track, existing_idx), reset->track_get_key_transition(dst_track, existing_idx));
7461 }
7462 }
7463
7464 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7465 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7466 undo_redo->add_do_method(this, "_redraw_tracks");
7467 undo_redo->add_undo_method(this, "_redraw_tracks");
7468 undo_redo->commit_action();
7469
7470 } break;
7471 case EDIT_DELETE_SELECTION: {
7472 if (bezier_edit->is_visible()) {
7473 bezier_edit->delete_selection();
7474 break;
7475 }
7476
7477 if (selection.size()) {
7478 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7479 undo_redo->create_action(TTR("Animation Delete Keys"));
7480
7481 for (RBMap<SelectedKey, KeyInfo>::Element *E = selection.back(); E; E = E->prev()) {
7482 undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->key().track, E->key().key);
7483 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->key().track, E->get().pos, animation->track_get_key_value(E->key().track, E->key().key), animation->track_get_key_transition(E->key().track, E->key().key));
7484 }
7485 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7486 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7487 undo_redo->add_do_method(this, "_redraw_tracks");
7488 undo_redo->add_undo_method(this, "_redraw_tracks");
7489 undo_redo->commit_action();
7490 _update_key_edit();
7491 }
7492 } break;
7493 case EDIT_GOTO_NEXT_STEP_TIMELINE_ONLY:
7494 case EDIT_GOTO_NEXT_STEP: {
7495 goto_next_step(false, p_option == EDIT_GOTO_NEXT_STEP_TIMELINE_ONLY);
7496 } break;
7497 case EDIT_GOTO_PREV_STEP: {
7498 goto_prev_step(false);
7499 } break;
7500
7501 case EDIT_GOTO_NEXT_KEYFRAME: {
7502 AnimationPlayerEditor::get_singleton()->go_to_nearest_keyframe(false);
7503 } break;
7504 case EDIT_GOTO_PREV_KEYFRAME: {
7505 AnimationPlayerEditor::get_singleton()->go_to_nearest_keyframe(true);
7506 } break;
7507
7508 case EDIT_APPLY_RESET: {
7509 AnimationPlayerEditor::get_singleton()->get_player()->apply_reset(true);
7510 } break;
7511
7512 case EDIT_BAKE_ANIMATION: {
7513 bake_dialog->popup_centered(Size2(200, 100) * EDSCALE);
7514 } break;
7515 case EDIT_BAKE_ANIMATION_CONFIRM: {
7516 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7517 undo_redo->create_action(TTR("Bake Animation as Linear Keys"));
7518
7519 int track_len = animation->get_track_count();
7520 bool b_trs = bake_trs->is_pressed();
7521 bool b_bs = bake_blendshape->is_pressed();
7522 bool b_v = bake_value->is_pressed();
7523
7524 double anim_len = animation->get_length() + CMP_EPSILON; // For end key.
7525 float fps = bake_fps->get_value();
7526 double dur_step = 1.0 / fps;
7527
7528 for (int i = 0; i < track_len; i++) {
7529 bool do_bake = false;
7530 Animation::TrackType type = animation->track_get_type(i);
7531 do_bake |= b_trs && (type == Animation::TYPE_POSITION_3D || type == Animation::TYPE_ROTATION_3D || type == Animation::TYPE_SCALE_3D);
7532 do_bake |= b_bs && type == Animation::TYPE_BLEND_SHAPE;
7533 do_bake |= b_v && type == Animation::TYPE_VALUE;
7534 if (do_bake && !animation->track_is_compressed(i)) {
7535 Animation::InterpolationType it = animation->track_get_interpolation_type(i);
7536 if (it == Animation::INTERPOLATION_NEAREST) {
7537 continue; // Nearest interpolation cannot be baked.
7538 }
7539
7540 // Special case for angle interpolation.
7541 bool is_using_angle = it == Animation::INTERPOLATION_LINEAR_ANGLE || it == Animation::INTERPOLATION_CUBIC_ANGLE;
7542
7543 // Make insert queue.
7544 Vector<Pair<real_t, Variant>> insert_queue_new;
7545
7546 switch (type) {
7547 case Animation::TYPE_POSITION_3D: {
7548 for (double delta_t = 0.0; delta_t <= anim_len; delta_t += dur_step) {
7549 Pair<real_t, Variant> keydata;
7550 keydata.first = delta_t;
7551 Vector3 v;
7552 animation->try_position_track_interpolate(i, delta_t, &v);
7553 keydata.second = v;
7554 insert_queue_new.append(keydata);
7555 }
7556 } break;
7557 case Animation::TYPE_ROTATION_3D: {
7558 for (double delta_t = 0.0; delta_t <= anim_len; delta_t += dur_step) {
7559 Pair<real_t, Variant> keydata;
7560 keydata.first = delta_t;
7561 Quaternion v;
7562 animation->try_rotation_track_interpolate(i, delta_t, &v);
7563 keydata.second = v;
7564 insert_queue_new.append(keydata);
7565 }
7566 } break;
7567 case Animation::TYPE_SCALE_3D: {
7568 for (double delta_t = 0.0; delta_t <= anim_len; delta_t += dur_step) {
7569 Pair<real_t, Variant> keydata;
7570 keydata.first = delta_t;
7571 Vector3 v;
7572 animation->try_scale_track_interpolate(i, delta_t, &v);
7573 keydata.second = v;
7574 insert_queue_new.append(keydata);
7575 }
7576 } break;
7577 case Animation::TYPE_BLEND_SHAPE: {
7578 for (double delta_t = 0.0; delta_t <= anim_len; delta_t += dur_step) {
7579 Pair<real_t, Variant> keydata;
7580 keydata.first = delta_t;
7581 float v;
7582 animation->try_blend_shape_track_interpolate(i, delta_t, &v);
7583 keydata.second = v;
7584 insert_queue_new.append(keydata);
7585 }
7586 } break;
7587 case Animation::TYPE_VALUE: {
7588 for (double delta_t = 0.0; delta_t < anim_len; delta_t += dur_step) {
7589 Pair<real_t, Variant> keydata;
7590 keydata.first = delta_t;
7591 keydata.second = animation->value_track_interpolate(i, delta_t);
7592 insert_queue_new.append(keydata);
7593 }
7594 } break;
7595 default: {
7596 } break;
7597 }
7598
7599 // Cleanup keys.
7600 int key_len = animation->track_get_key_count(i);
7601 for (int j = key_len - 1; j >= 0; j--) {
7602 undo_redo->add_do_method(animation.ptr(), "track_remove_key", i, j);
7603 }
7604
7605 // Insert keys.
7606 undo_redo->add_do_method(animation.ptr(), "track_set_interpolation_type", i, is_using_angle ? Animation::INTERPOLATION_LINEAR_ANGLE : Animation::INTERPOLATION_LINEAR);
7607 for (int j = insert_queue_new.size() - 1; j >= 0; j--) {
7608 undo_redo->add_do_method(animation.ptr(), "track_insert_key", i, insert_queue_new[j].first, insert_queue_new[j].second);
7609 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", i, insert_queue_new[j].first);
7610 }
7611
7612 // Undo methods.
7613 undo_redo->add_undo_method(animation.ptr(), "track_set_interpolation_type", i, animation->track_get_interpolation_type(i));
7614 for (int j = key_len - 1; j >= 0; j--) {
7615 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", i, animation->track_get_key_time(i, j), animation->track_get_key_value(i, j), animation->track_get_key_transition(i, j));
7616 }
7617 }
7618 }
7619
7620 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
7621 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
7622 undo_redo->add_do_method(this, "_redraw_tracks");
7623 undo_redo->add_undo_method(this, "_redraw_tracks");
7624 undo_redo->commit_action();
7625 _update_key_edit();
7626
7627 } break;
7628
7629 case EDIT_OPTIMIZE_ANIMATION: {
7630 optimize_dialog->popup_centered(Size2(250, 180) * EDSCALE);
7631
7632 } break;
7633 case EDIT_OPTIMIZE_ANIMATION_CONFIRM: {
7634 animation->optimize(optimize_velocity_error->get_value(), optimize_angular_error->get_value(), optimize_precision_error->get_value());
7635 _redraw_tracks();
7636 _update_key_edit();
7637 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7638 undo_redo->clear_history(undo_redo->get_history_id_for_object(animation.ptr()));
7639 undo_redo->clear_history(undo_redo->get_history_id_for_object(this));
7640
7641 } break;
7642 case EDIT_CLEAN_UP_ANIMATION: {
7643 cleanup_dialog->popup_centered(Size2(300, 0) * EDSCALE);
7644
7645 } break;
7646 case EDIT_CLEAN_UP_ANIMATION_CONFIRM: {
7647 if (cleanup_all->is_pressed()) {
7648 List<StringName> names;
7649 AnimationPlayerEditor::get_singleton()->get_player()->get_animation_list(&names);
7650 for (const StringName &E : names) {
7651 _cleanup_animation(AnimationPlayerEditor::get_singleton()->get_player()->get_animation(E));
7652 }
7653 } else {
7654 _cleanup_animation(animation);
7655 }
7656
7657 } break;
7658 }
7659 }
7660
7661 void AnimationTrackEditor::_cleanup_animation(Ref<Animation> p_animation) {
7662 _clear_selection();
7663 for (int i = 0; i < p_animation->get_track_count(); i++) {
7664 if (!root->has_node_and_resource(p_animation->track_get_path(i))) {
7665 continue;
7666 }
7667 Ref<Resource> res;
7668 Vector<StringName> leftover_path;
7669 Node *node = root->get_node_and_resource(p_animation->track_get_path(i), res, leftover_path);
7670
7671 bool prop_exists = false;
7672 Variant::Type valid_type = Variant::NIL;
7673 Object *obj = nullptr;
7674
7675 if (res.is_valid()) {
7676 obj = res.ptr();
7677 } else if (node) {
7678 obj = node;
7679 }
7680
7681 if (obj && p_animation->track_get_type(i) == Animation::TYPE_VALUE) {
7682 valid_type = obj->get_static_property_type_indexed(leftover_path, &prop_exists);
7683 }
7684
7685 if (!obj && cleanup_tracks->is_pressed()) {
7686 p_animation->remove_track(i);
7687 i--;
7688 continue;
7689 }
7690
7691 if (cleanup_keys_with_trimming_head->is_pressed()) {
7692 // Check is necessary because if there is already a key in position 0, it should not be replaced.
7693 if (p_animation->track_get_type(i) == Animation::TYPE_AUDIO && p_animation->track_find_key(i, 0, Animation::FIND_MODE_EXACT) < 0) {
7694 for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
7695 double t = p_animation->track_get_key_time(i, j);
7696 if (t < 0) {
7697 if (j == p_animation->track_get_key_count(i) - 1 || (j + 1 < p_animation->track_get_key_count(i) && p_animation->track_get_key_time(i, j + 1) > 0)) {
7698 Ref<AudioStream> stream = p_animation->audio_track_get_key_stream(i, j);
7699 double len = stream->get_length() - p_animation->audio_track_get_key_end_offset(i, j);
7700 double prev_offset = p_animation->audio_track_get_key_start_offset(i, j);
7701 double prev_time = p_animation->track_get_key_time(i, j);
7702 double diff = prev_offset - prev_time;
7703 if (diff >= len) {
7704 p_animation->track_remove_key(i, j);
7705 j--;
7706 continue;
7707 }
7708 p_animation->audio_track_set_key_start_offset(i, j, diff);
7709 p_animation->track_set_key_time(i, j, 0);
7710 } else {
7711 p_animation->track_remove_key(i, j);
7712 j--;
7713 }
7714 }
7715 }
7716 } else {
7717 for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
7718 double t = p_animation->track_get_key_time(i, j);
7719 if (t < 0) {
7720 p_animation->track_remove_key(i, j);
7721 j--;
7722 }
7723 }
7724 }
7725 }
7726
7727 if (cleanup_keys_with_trimming_end->is_pressed()) {
7728 if (p_animation->track_get_type(i) == Animation::TYPE_AUDIO) {
7729 for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
7730 double t = p_animation->track_get_key_time(i, j);
7731 if (t <= p_animation->get_length() && (j == p_animation->track_get_key_count(i) - 1 || (j + 1 < p_animation->track_get_key_count(i) && p_animation->track_get_key_time(i, j + 1) > p_animation->get_length()))) {
7732 Ref<AudioStream> stream = animation->audio_track_get_key_stream(i, j);
7733 double len = stream->get_length() - animation->audio_track_get_key_start_offset(i, j);
7734 if (t + len < p_animation->get_length()) {
7735 continue;
7736 }
7737 double prev_time = animation->track_get_key_time(i, j);
7738 double diff = prev_time + len - p_animation->get_length();
7739 if (diff >= len) {
7740 p_animation->track_remove_key(i, j);
7741 j--;
7742 continue;
7743 }
7744 p_animation->audio_track_set_key_end_offset(i, j, diff);
7745 } else if (t > p_animation->get_length()) {
7746 p_animation->track_remove_key(i, j);
7747 j--;
7748 }
7749 }
7750 } else {
7751 for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
7752 double t = p_animation->track_get_key_time(i, j);
7753 if (t > p_animation->get_length()) {
7754 p_animation->track_remove_key(i, j);
7755 j--;
7756 }
7757 }
7758 }
7759 }
7760
7761 if (!prop_exists || p_animation->track_get_type(i) != Animation::TYPE_VALUE || !cleanup_keys->is_pressed()) {
7762 continue;
7763 }
7764
7765 for (int j = 0; j < p_animation->track_get_key_count(i); j++) {
7766 Variant v = p_animation->track_get_key_value(i, j);
7767
7768 if (!Variant::can_convert(v.get_type(), valid_type)) {
7769 p_animation->track_remove_key(i, j);
7770 j--;
7771 }
7772 }
7773
7774 if (p_animation->track_get_key_count(i) == 0 && cleanup_tracks->is_pressed()) {
7775 p_animation->remove_track(i);
7776 i--;
7777 }
7778 }
7779
7780 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7781 undo_redo->clear_history(undo_redo->get_history_id_for_object(animation.ptr()));
7782 undo_redo->clear_history(undo_redo->get_history_id_for_object(this));
7783 _update_tracks();
7784 }
7785
7786 void AnimationTrackEditor::_toggle_function_names() {
7787 _redraw_tracks();
7788 }
7789
7790 void AnimationTrackEditor::_view_group_toggle() {
7791 _update_tracks();
7792 view_group->set_button_icon(get_editor_theme_icon(view_group->is_pressed() ? SNAME("AnimationTrackList") : SNAME("AnimationTrackGroup")));
7793 bezier_edit->set_filtered(selected_filter->is_pressed());
7794 }
7795
7796 bool AnimationTrackEditor::is_grouping_tracks() {
7797 if (!view_group) {
7798 return false;
7799 }
7800
7801 return !view_group->is_pressed();
7802 }
7803
7804 bool AnimationTrackEditor::is_sorting_alphabetically() {
7805 return alphabetic_sorting->is_pressed();
7806 }
7807
7808 bool AnimationTrackEditor::is_function_name_pressed() {
7809 return function_name_toggler->is_pressed();
7810 }
7811
7812 void AnimationTrackEditor::_auto_fit() {
7813 timeline->auto_fit();
7814 }
7815
7816 void AnimationTrackEditor::_auto_fit_bezier() {
7817 timeline->auto_fit();
7818
7819 if (bezier_edit->is_visible()) {
7820 bezier_edit->auto_fit_vertically();
7821 }
7822 }
7823
7824 void AnimationTrackEditor::_root_node_changed(Node *p_node, bool p_removed) {
7825 add_animation_player->set_disabled(p_removed);
7826 }
7827
7828 void AnimationTrackEditor::_scene_changed() {
7829 add_animation_player->set_disabled(EditorNode::get_singleton()->get_edited_scene() == nullptr);
7830 }
7831
7832 void AnimationTrackEditor::_selection_changed() {
7833 if (selected_filter->is_pressed()) {
7834 _update_tracks(); // Needs updating.
7835 } else {
7836 _redraw_tracks();
7837 _redraw_groups();
7838 }
7839 }
7840
7841 void AnimationTrackEditor::_update_snap_unit() {
7842 nearest_fps = 0;
7843
7844 if (step->get_value() <= 0) {
7845 snap_unit = 0;
7846 _update_nearest_fps_label();
7847 return; // Avoid zero div.
7848 }
7849
7850 if (timeline->is_using_fps()) {
7851 snap_unit = 1.0 / step->get_value();
7852 } else {
7853 if (fps_compat->is_pressed()) {
7854 snap_unit = CLAMP(step->get_value(), 0.0, 1.0);
7855 if (!Math::is_zero_approx(snap_unit)) {
7856 real_t fps = Math::round(1.0 / snap_unit);
7857 nearest_fps = int(fps);
7858 snap_unit = 1.0 / fps;
7859 }
7860 } else {
7861 snap_unit = step->get_value();
7862 }
7863 }
7864 _update_nearest_fps_label();
7865 }
7866
7867 float AnimationTrackEditor::snap_time(float p_value, bool p_relative) {
7868 if (is_snap_keys_enabled()) {
7869 double current_snap = snap_unit;
7870 if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
7871 // Use more precise snapping when holding Shift.
7872 current_snap *= 0.25;
7873 }
7874
7875 if (p_relative) {
7876 double rel = Math::fmod(timeline->get_value(), current_snap);
7877 p_value = Math::snapped(p_value + rel, current_snap) - rel;
7878 } else {
7879 p_value = Math::snapped(p_value, current_snap);
7880 }
7881 }
7882
7883 return p_value;
7884 }
7885
7886 float AnimationTrackEditor::get_snap_unit() {
7887 return snap_unit;
7888 }
7889
7890 void AnimationTrackEditor::_update_timeline_margins() {
7891 int margin_left = timeline_mc->get_theme_constant(SNAME("margin_left"), SNAME("AnimationTrackMargins"));
7892 int margin_right = timeline_mc->get_theme_constant(SNAME("margin_right"), SNAME("AnimationTrackMargins"));
7893
7894 // Prevent the timeline cursor from misaligning with the tracks on the right-to-left layout.
7895 if (scroll->get_v_scroll_bar()->is_visible() && is_layout_rtl()) {
7896 margin_left += scroll->get_v_scroll_bar()->get_minimum_size().width;
7897 }
7898
7899 timeline_mc->add_theme_constant_override(SNAME("margin_left"), margin_left);
7900 timeline_mc->add_theme_constant_override(SNAME("margin_right"), margin_right);
7901 }
7902
7903 void AnimationTrackEditor::_add_animation_player() {
7904 EditorData &editor_data = EditorNode::get_editor_data();
7905 Node *scene = editor_data.get_edited_scene_root();
7906
7907 ERR_FAIL_NULL_EDMSG(scene, "Cannot add AnimationPlayer without root node in scene");
7908
7909 AnimationPlayer *animation_player = memnew(AnimationPlayer);
7910 editor_data.instantiate_object_properties(animation_player);
7911
7912 String new_name = scene->validate_child_name(animation_player);
7913 if (GLOBAL_GET("editor/naming/node_name_casing").operator int() != NAME_CASING_PASCAL_CASE) {
7914 new_name = adjust_name_casing(new_name);
7915 }
7916 animation_player->set_name(new_name);
7917
7918 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
7919 undo_redo->create_action_for_history(TTR("Create Node"), editor_data.get_current_edited_scene_history_id());
7920
7921 undo_redo->add_do_method(scene, "add_child", animation_player, true);
7922 undo_redo->add_do_method(animation_player, "set_owner", scene);
7923 undo_redo->add_do_reference(animation_player);
7924 undo_redo->add_undo_method(scene, "remove_child", animation_player);
7925
7926 undo_redo->commit_action();
7927
7928 EditorSelection *editor_selection = EditorNode::get_singleton()->get_editor_selection();
7929 editor_selection->clear();
7930 editor_selection->add_node(animation_player);
7931 }
7932
7933 void AnimationTrackEditor::_show_imported_anim_warning() {
7934 // It looks terrible on a single line but the TTR extractor doesn't support line breaks yet.
7935 EditorNode::get_singleton()->show_warning(
7936 TTR("This animation belongs to an imported scene, so changes to imported tracks will not be saved.\n\nTo modify this animation, navigate to the scene's Advanced Import settings and select the animation.\nSome options, including looping, are available here. To add custom tracks, enable \"Save To File\" and\n\"Keep Custom Tracks\"."));
7937 }
7938
7939 void AnimationTrackEditor::_show_dummy_player_warning() {
7940 EditorNode::get_singleton()->show_warning(
7941 TTR("Some AnimationPlayerEditor's options are disabled since this is the dummy AnimationPlayer for preview.\n\nThe dummy player is forced active, non-deterministic and doesn't have the root motion track. Furthermore, the original node is inactive temporary."));
7942 }
7943
7944 void AnimationTrackEditor::_show_inactive_player_warning() {
7945 EditorNode::get_singleton()->show_warning(
7946 TTR("AnimationPlayer is inactive. The playback will not be processed."));
7947 }
7948
7949 void AnimationTrackEditor::_select_all_tracks_for_copy() {
7950 TreeItem *track = track_copy_select->get_root()->get_first_child();
7951 if (!track) {
7952 return;
7953 }
7954
7955 bool all_selected = true;
7956 while (track) {
7957 if (!track->is_checked(0)) {
7958 all_selected = false;
7959 }
7960
7961 track = track->get_next();
7962 }
7963
7964 track = track_copy_select->get_root()->get_first_child();
7965 while (track) {
7966 track->set_checked(0, !all_selected);
7967 track = track->get_next();
7968 }
7969 }
7970
7971 void AnimationTrackEditor::_bind_methods() {
7972 ClassDB::bind_method("_track_grab_focus", &AnimationTrackEditor::_track_grab_focus);
7973 ClassDB::bind_method("_redraw_tracks", &AnimationTrackEditor::_redraw_tracks);
7974 ClassDB::bind_method("_clear_selection_for_anim", &AnimationTrackEditor::_clear_selection_for_anim);
7975 ClassDB::bind_method("_select_at_anim", &AnimationTrackEditor::_select_at_anim);
7976 ClassDB::bind_method("_clear_selection", &AnimationTrackEditor::_clear_selection);
7977
7978 ClassDB::bind_method(D_METHOD("_bezier_track_set_key_handle_mode", "animation", "track_idx", "key_idx", "key_handle_mode", "key_handle_set_mode"), &AnimationTrackEditor::_bezier_track_set_key_handle_mode, DEFVAL(Animation::HANDLE_SET_MODE_NONE));
7979 ClassDB::bind_method(D_METHOD("_bezier_track_set_key_handle_mode_at_time", "animation", "track_idx", "time", "key_handle_mode", "key_handle_set_mode"), &AnimationTrackEditor::_bezier_track_set_key_handle_mode_at_time, DEFVAL(Animation::HANDLE_SET_MODE_NONE));
7980
7981 ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::FLOAT, "position"), PropertyInfo(Variant::BOOL, "timeline_only"), PropertyInfo(Variant::BOOL, "update_position_only")));
7982 ADD_SIGNAL(MethodInfo("keying_changed"));
7983 ADD_SIGNAL(MethodInfo("animation_len_changed", PropertyInfo(Variant::FLOAT, "len")));
7984 ADD_SIGNAL(MethodInfo("animation_step_changed", PropertyInfo(Variant::FLOAT, "step")));
7985 }
7986
7987 void AnimationTrackEditor::_pick_track_filter_text_changed(const String &p_newtext) {
7988 TreeItem *root_item = pick_track->get_scene_tree()->get_scene_tree()->get_root();
7989
7990 Vector<Node *> select_candidates;
7991 Node *to_select = nullptr;
7992
7993 String filter = pick_track->get_filter_line_edit()->get_text();
7994
7995 _pick_track_select_recursive(root_item, filter, select_candidates);
7996
7997 if (!select_candidates.is_empty()) {
7998 for (int i = 0; i < select_candidates.size(); ++i) {
7999 Node *candidate = select_candidates[i];
8000
8001 if (((String)candidate->get_name()).to_lower().begins_with(filter.to_lower())) {
8002 to_select = candidate;
8003 break;
8004 }
8005 }
8006
8007 if (!to_select) {
8008 to_select = select_candidates[0];
8009 }
8010 }
8011
8012 pick_track->get_scene_tree()->set_selected(to_select);
8013 }
8014
8015 void AnimationTrackEditor::_pick_track_select_recursive(TreeItem *p_item, const String &p_filter, Vector<Node *> &p_select_candidates) {
8016 if (!p_item) {
8017 return;
8018 }
8019
8020 NodePath np = p_item->get_metadata(0);
8021 Node *node = get_node_or_null(np);
8022
8023 if (node && !p_filter.is_empty() && ((String)node->get_name()).containsn(p_filter)) {
8024 p_select_candidates.push_back(node);
8025 }
8026
8027 TreeItem *c = p_item->get_first_child();
8028
8029 while (c) {
8030 _pick_track_select_recursive(c, p_filter, p_select_candidates);
8031 c = c->get_next();
8032 }
8033 }
8034
8035 void AnimationTrackEditor::popup_read_only_dialog() {
8036 read_only_dialog->popup_centered(Size2(200, 100) * EDSCALE);
8037 }
8038
8039 AnimationTrackEditor::AnimationTrackEditor() {
8040 MarginContainer *mc = memnew(MarginContainer);
8041 mc->set_theme_type_variation("NoBorderAnimation");
8042 mc->set_v_size_flags(SIZE_EXPAND_FILL);
8043 add_child(mc);
8044
8045 main_panel = memnew(PanelContainer);
8046 main_panel->set_focus_mode(FOCUS_ALL); // Allow panel to have focus so that shortcuts work as expected.
8047 mc->add_child(main_panel);
8048 HBoxContainer *timeline_scroll = memnew(HBoxContainer);
8049 main_panel->add_child(timeline_scroll);
8050 timeline_scroll->set_v_size_flags(SIZE_EXPAND_FILL);
8051
8052 timeline_vbox = memnew(VBoxContainer);
8053 timeline_scroll->add_child(timeline_vbox);
8054 timeline_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
8055 timeline_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
8056
8057 info_message_vbox = memnew(VBoxContainer);
8058 main_panel->add_child(info_message_vbox);
8059 info_message_vbox->set_alignment(AlignmentMode::ALIGNMENT_CENTER);
8060 info_message_vbox->set_v_size_flags(SIZE_EXPAND_FILL);
8061 info_message_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
8062
8063 info_message = memnew(Label);
8064 info_message_vbox->add_child(info_message);
8065 info_message->set_focus_mode(FOCUS_ACCESSIBILITY);
8066 info_message->set_text(TTR("Select an AnimationPlayer node to create and edit animations."));
8067 info_message->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
8068 info_message->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
8069 info_message->set_autowrap_mode(TextServer::AUTOWRAP_WORD_SMART);
8070 info_message->set_custom_minimum_size(Size2(100 * EDSCALE, 0));
8071 info_message->set_anchors_and_offsets_preset(PRESET_FULL_RECT, PRESET_MODE_KEEP_SIZE, 8 * EDSCALE);
8072
8073 add_animation_player = memnew(Button);
8074 info_message_vbox->add_child(add_animation_player);
8075 add_animation_player->set_text(TTR("Add AnimationPlayer"));
8076 add_animation_player->set_tooltip_text(TTR("Add a new AnimationPlayer node to the scene."));
8077 add_animation_player->set_h_size_flags(SIZE_SHRINK_CENTER);
8078 add_animation_player->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_add_animation_player));
8079
8080 timeline_mc = memnew(MarginContainer);
8081 timeline_mc->set_h_size_flags(SIZE_EXPAND_FILL);
8082 timeline_vbox->add_child(timeline_mc);
8083
8084 timeline = memnew(AnimationTimelineEdit);
8085 timeline_mc->add_child(timeline);
8086 timeline->set_editor(this);
8087 timeline->connect("timeline_changed", callable_mp(this, &AnimationTrackEditor::_timeline_changed));
8088 timeline->connect("name_limit_changed", callable_mp(this, &AnimationTrackEditor::_name_limit_changed));
8089 timeline->connect("track_added", callable_mp(this, &AnimationTrackEditor::_add_track));
8090 timeline->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTrackEditor::_timeline_value_changed));
8091 timeline->connect("length_changed", callable_mp(this, &AnimationTrackEditor::_update_length));
8092 timeline->connect("filter_changed", callable_mp(this, &AnimationTrackEditor::_update_tracks));
8093
8094 panner.instantiate();
8095 panner->set_scroll_zoom_factor(AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_IN);
8096 panner->set_callbacks(callable_mp(this, &AnimationTrackEditor::_pan_callback), callable_mp(this, &AnimationTrackEditor::_zoom_callback));
8097
8098 box_selection_container = memnew(Control);
8099 box_selection_container->set_v_size_flags(SIZE_EXPAND_FILL);
8100 box_selection_container->set_clip_contents(true);
8101 timeline_vbox->add_child(box_selection_container);
8102
8103 bezier_edit = memnew(AnimationBezierTrackEdit);
8104 timeline_vbox->add_child(bezier_edit);
8105 bezier_edit->set_editor(this);
8106 bezier_edit->set_timeline(timeline);
8107 bezier_edit->hide();
8108 bezier_edit->set_v_size_flags(SIZE_EXPAND_FILL);
8109 bezier_edit->connect("timeline_changed", callable_mp(this, &AnimationTrackEditor::_timeline_changed));
8110
8111 marker_edit = memnew(AnimationMarkerEdit);
8112 timeline->get_child(0)->add_child(marker_edit);
8113 // Prevents the play position from being drawn at the wrong place in specific cases.
8114 timeline->get_child(0)->connect(SceneStringName(resized), callable_mp(marker_edit, &AnimationMarkerEdit::update_play_position));
8115 marker_edit->set_editor(this);
8116 marker_edit->set_timeline(timeline);
8117 marker_edit->set_anchors_and_offsets_preset(Control::LayoutPreset::PRESET_FULL_RECT);
8118 marker_edit->set_z_index(1); // Ensure marker appears over the animation track editor.
8119 marker_edit->connect(SceneStringName(draw), callable_mp(this, &AnimationTrackEditor::_redraw_groups));
8120 marker_edit->connect(SceneStringName(draw), callable_mp(this, &AnimationTrackEditor::_redraw_tracks));
8121 marker_edit->connect(SceneStringName(draw), callable_mp((CanvasItem *)bezier_edit, &CanvasItem::queue_redraw));
8122
8123 scroll = memnew(ScrollContainer);
8124 scroll->set_scroll_hint_mode(ScrollContainer::SCROLL_HINT_MODE_ALL);
8125 box_selection_container->add_child(scroll);
8126 scroll->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
8127
8128 scroll->set_focus_mode(FOCUS_CLICK);
8129 scroll->connect(SceneStringName(gui_input), callable_mp(this, &AnimationTrackEditor::_scroll_input));
8130 scroll->connect(SceneStringName(focus_exited), callable_mp(panner.ptr(), &ViewPanner::release_pan_key));
8131
8132 // Must be updated from here, so it guarantees that the scrollbar theme has already changed.
8133 scroll->connect(SceneStringName(theme_changed), callable_mp(this, &AnimationTrackEditor::_update_timeline_margins), CONNECT_DEFERRED);
8134 scroll->get_v_scroll_bar()->connect(SceneStringName(visibility_changed), callable_mp(this, &AnimationTrackEditor::_update_timeline_margins));
8135 scroll->get_v_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTrackEditor::_v_scroll_changed));
8136 scroll->get_h_scroll_bar()->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTrackEditor::_h_scroll_changed));
8137
8138 timeline_vbox->set_custom_minimum_size(Size2(0, 150) * EDSCALE);
8139
8140 hscroll = memnew(HScrollBar);
8141 hscroll->share(timeline);
8142 hscroll->hide();
8143 hscroll->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTrackEditor::_update_scroll));
8144 timeline_vbox->add_child(hscroll);
8145 timeline->set_hscroll(hscroll);
8146
8147 mc = memnew(MarginContainer);
8148 mc->set_h_size_flags(SIZE_EXPAND_FILL);
8149 mc->set_theme_type_variation("AnimationTrackMargins");
8150 scroll->add_child(mc);
8151
8152 track_vbox = memnew(VBoxContainer);
8153 mc->add_child(track_vbox);
8154 scroll->set_horizontal_scroll_mode(ScrollContainer::SCROLL_MODE_DISABLED);
8155
8156 HFlowContainer *bottom_hf = memnew(HFlowContainer);
8157 add_child(bottom_hf);
8158
8159 imported_anim_warning = memnew(Button);
8160 imported_anim_warning->hide();
8161 imported_anim_warning->set_text(TTR("Imported Animation"));
8162 imported_anim_warning->set_tooltip_text(TTR("Warning: Editing imported animation"));
8163 imported_anim_warning->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_show_imported_anim_warning));
8164 bottom_hf->add_child(imported_anim_warning);
8165
8166 dummy_player_warning = memnew(Button);
8167 dummy_player_warning->hide();
8168 dummy_player_warning->set_text(TTR("Dummy Player"));
8169 dummy_player_warning->set_tooltip_text(TTR("Warning: Editing dummy AnimationPlayer"));
8170 dummy_player_warning->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_show_dummy_player_warning));
8171 bottom_hf->add_child(dummy_player_warning);
8172
8173 inactive_player_warning = memnew(Button);
8174 inactive_player_warning->hide();
8175 inactive_player_warning->set_text(TTR("Inactive Player"));
8176 inactive_player_warning->set_tooltip_text(TTR("Warning: AnimationPlayer is inactive"));
8177 inactive_player_warning->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_show_inactive_player_warning));
8178 bottom_hf->add_child(inactive_player_warning);
8179
8180 Control *spacer = memnew(Control);
8181 spacer->set_mouse_filter(MOUSE_FILTER_PASS);
8182 spacer->set_h_size_flags(SIZE_EXPAND_FILL);
8183 bottom_hf->add_child(spacer);
8184
8185 bezier_key_mode = memnew(OptionButton);
8186 bezier_key_mode->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
8187 bezier_key_mode->add_item(String(), Animation::HANDLE_MODE_FREE);
8188 bezier_key_mode->add_item(String(), Animation::HANDLE_MODE_LINEAR);
8189 bezier_key_mode->add_item(String(), Animation::HANDLE_MODE_BALANCED);
8190 bezier_key_mode->add_item(String(), Animation::HANDLE_MODE_MIRRORED);
8191 bezier_key_mode->select(Animation::HANDLE_MODE_BALANCED);
8192 bezier_key_mode->set_accessibility_name(TTRC("Bezier Default Mode"));
8193
8194 bottom_hf->add_child(bezier_key_mode);
8195 bottom_hf->add_child(memnew(VSeparator));
8196
8197 bezier_edit_icon = memnew(Button);
8198 bezier_edit_icon->set_flat(true);
8199 bezier_edit_icon->set_disabled(true);
8200 bezier_edit_icon->set_toggle_mode(true);
8201 bezier_edit_icon->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_toggle_bezier_edit));
8202 bezier_edit_icon->set_tooltip_text(TTR("Toggle between the bezier curve editor and track editor."));
8203
8204 bottom_hf->add_child(bezier_edit_icon);
8205
8206 function_name_toggler = memnew(Button);
8207 function_name_toggler->set_flat(true);
8208 function_name_toggler->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_toggle_function_names));
8209 function_name_toggler->set_shortcut(ED_SHORTCUT("animation_editor/toggle_function_names", TTRC("Toggle method names")));
8210 function_name_toggler->set_toggle_mode(true);
8211 function_name_toggler->set_shortcut_in_tooltip(false);
8212 function_name_toggler->set_tooltip_text(TTRC("Toggle function names in the track editor."));
8213
8214 bottom_hf->add_child(function_name_toggler);
8215
8216 selected_filter = memnew(Button);
8217 selected_filter->set_flat(true);
8218 selected_filter->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_view_group_toggle)); // Same function works the same.
8219 selected_filter->set_toggle_mode(true);
8220 selected_filter->set_tooltip_text(TTR("Only show tracks from nodes selected in tree."));
8221
8222 bottom_hf->add_child(selected_filter);
8223
8224 alphabetic_sorting = memnew(Button);
8225 alphabetic_sorting->set_flat(true);
8226 alphabetic_sorting->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_update_tracks));
8227 alphabetic_sorting->set_toggle_mode(true);
8228 alphabetic_sorting->set_tooltip_text(TTR("Sort tracks/groups alphabetically.\nIf disabled, tracks are shown in the order they are added and can be reordered using drag-and-drop."));
8229
8230 bottom_hf->add_child(alphabetic_sorting);
8231
8232 view_group = memnew(Button);
8233 view_group->set_flat(true);
8234 view_group->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_view_group_toggle));
8235 view_group->set_toggle_mode(true);
8236 view_group->set_tooltip_text(TTR("Group tracks by node or display them as plain list."));
8237
8238 bottom_hf->add_child(view_group);
8239
8240 insert_at_current_time = memnew(Button);
8241 insert_at_current_time->set_flat(true);
8242 bottom_hf->add_child(insert_at_current_time);
8243 insert_at_current_time->set_disabled(true);
8244 insert_at_current_time->set_toggle_mode(true);
8245 insert_at_current_time->set_pressed(EDITOR_GET("editors/animation/insert_at_current_time"));
8246 insert_at_current_time->set_tooltip_text(TTRC("Insert at current time."));
8247
8248 bottom_hf->add_child(memnew(VSeparator));
8249
8250 snap_timeline = memnew(Button);
8251 snap_timeline->set_flat(true);
8252 snap_timeline->set_disabled(true);
8253 snap_timeline->set_toggle_mode(true);
8254 snap_timeline->set_tooltip_text(TTR("Apply snapping to timeline cursor."));
8255 snap_timeline->set_pressed(EditorSettings::get_singleton()->get_project_metadata("animation_track_editor", "snap_timeline", false));
8256 bottom_hf->add_child(snap_timeline);
8257 snap_timeline->connect(SceneStringName(toggled), callable_mp(this, &AnimationTrackEditor::_store_snap_states).unbind(1));
8258
8259 snap_keys = memnew(Button);
8260 snap_keys->set_flat(true);
8261 snap_keys->set_disabled(true);
8262 snap_keys->set_toggle_mode(true);
8263 snap_keys->set_tooltip_text(TTR("Apply snapping to selected key(s)."));
8264 snap_keys->set_pressed(EditorSettings::get_singleton()->get_project_metadata("animation_track_editor", "snap_keys", true));
8265 bottom_hf->add_child(snap_keys);
8266 snap_keys->connect(SceneStringName(toggled), callable_mp(this, &AnimationTrackEditor::_store_snap_states).unbind(1));
8267
8268 fps_compat = memnew(Button);
8269 fps_compat->set_flat(true);
8270 bottom_hf->add_child(fps_compat);
8271 fps_compat->set_disabled(true);
8272 fps_compat->set_toggle_mode(true);
8273 fps_compat->set_pressed(true);
8274 fps_compat->set_tooltip_text(TTR("Apply snapping to the nearest integer FPS."));
8275 fps_compat->connect(SceneStringName(toggled), callable_mp(this, &AnimationTrackEditor::_update_fps_compat_mode));
8276
8277 nearest_fps_label = memnew(Label);
8278 nearest_fps_label->set_focus_mode(FOCUS_ACCESSIBILITY);
8279 nearest_fps_label->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
8280 bottom_hf->add_child(nearest_fps_label);
8281
8282 step = memnew(EditorSpinSlider);
8283 step->set_min(0);
8284 step->set_max(1000000);
8285 step->set_step(SECOND_DECIMAL);
8286 step->set_control_state(EditorSpinSlider::CONTROL_STATE_HIDE);
8287 step->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
8288 step->set_tooltip_text(TTR("Animation step value."));
8289 step->set_accessibility_name(TTRC("Animation step value."));
8290 bottom_hf->add_child(step);
8291 step->connect(SceneStringName(value_changed), callable_mp(this, &AnimationTrackEditor::_update_step));
8292 step->set_read_only(true);
8293
8294 snap_mode = memnew(OptionButton);
8295 snap_mode->add_item(TTR("Seconds"));
8296 snap_mode->add_item(TTR("FPS"));
8297 snap_mode->set_accessibility_name(TTRC("Snap Mode"));
8298 snap_mode->set_disabled(true);
8299 bottom_hf->add_child(snap_mode);
8300 snap_mode->connect(SceneStringName(item_selected), callable_mp(this, &AnimationTrackEditor::_snap_mode_changed));
8301
8302 bottom_hf->add_child(memnew(VSeparator));
8303
8304 HBoxContainer *zoom_hb = memnew(HBoxContainer);
8305 zoom_icon = memnew(TextureRect);
8306 zoom_icon->set_v_size_flags(SIZE_SHRINK_CENTER);
8307 zoom_hb->add_child(zoom_icon);
8308 zoom = memnew(HSlider);
8309 zoom->set_step(0.01);
8310 zoom->set_min(0.0);
8311 zoom->set_max(2.0);
8312 zoom->set_value(1.0);
8313 zoom->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
8314 zoom->set_v_size_flags(SIZE_SHRINK_CENTER);
8315 zoom->set_accessibility_name(TTRC("Zoom"));
8316 zoom_hb->add_child(zoom);
8317 bottom_hf->add_child(zoom_hb);
8318 timeline->set_zoom(zoom);
8319
8320 ED_SHORTCUT("animation_editor/auto_fit", TTRC("Fit to panel"), KeyModifierMask::ALT | Key::F);
8321
8322 auto_fit = memnew(Button);
8323 auto_fit->set_flat(true);
8324 auto_fit->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_auto_fit));
8325 auto_fit->set_shortcut(ED_GET_SHORTCUT("animation_editor/auto_fit"));
8326 auto_fit->set_accessibility_name(TTRC("Auto Fit"));
8327 bottom_hf->add_child(auto_fit);
8328
8329 auto_fit_bezier = memnew(Button);
8330 auto_fit_bezier->set_flat(true);
8331 auto_fit_bezier->set_visible(false);
8332 auto_fit_bezier->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_auto_fit_bezier));
8333 auto_fit_bezier->set_shortcut(ED_GET_SHORTCUT("animation_editor/auto_fit"));
8334 auto_fit_bezier->set_accessibility_name(TTRC("Auto Fit Bezier"));
8335 bottom_hf->add_child(auto_fit_bezier);
8336
8337 edit = memnew(MenuButton);
8338 edit->set_shortcut_context(this);
8339 edit->set_text(TTR("Edit"));
8340 edit->set_flat(false);
8341 edit->set_disabled(true);
8342 edit->set_tooltip_text(TTR("Animation properties."));
8343 edit->set_accessibility_name(TTRC("Animation properties."));
8344 edit->get_popup()->add_item(TTR("Copy Tracks..."), EDIT_COPY_TRACKS);
8345 edit->get_popup()->add_item(TTR("Paste Tracks"), EDIT_PASTE_TRACKS);
8346 edit->get_popup()->add_separator();
8347 edit->get_popup()->add_item(TTR("Scale Selection..."), EDIT_SCALE_SELECTION);
8348 edit->get_popup()->add_item(TTR("Scale From Cursor..."), EDIT_SCALE_FROM_CURSOR);
8349 edit->get_popup()->add_separator();
8350 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/set_start_offset", TTRC("Set Start Offset (Audio)"), KeyModifierMask::CMD_OR_CTRL | Key::BRACKETLEFT), EDIT_SET_START_OFFSET);
8351 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/set_end_offset", TTRC("Set End Offset (Audio)"), KeyModifierMask::CMD_OR_CTRL | Key::BRACKETRIGHT), EDIT_SET_END_OFFSET);
8352 edit->get_popup()->add_separator();
8353 edit->get_popup()->add_item(TTR("Make Easing Selection..."), EDIT_EASE_SELECTION);
8354 edit->get_popup()->add_separator();
8355 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/duplicate_selected_keys", TTRC("Duplicate Selected Keys"), KeyModifierMask::CMD_OR_CTRL | Key::D), EDIT_DUPLICATE_SELECTED_KEYS);
8356 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/cut_selected_keys", TTRC("Cut Selected Keys"), KeyModifierMask::CMD_OR_CTRL | Key::X), EDIT_CUT_KEYS);
8357 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/copy_selected_keys", TTRC("Copy Selected Keys"), KeyModifierMask::CMD_OR_CTRL | Key::C), EDIT_COPY_KEYS);
8358 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/paste_keys", TTRC("Paste Keys"), KeyModifierMask::CMD_OR_CTRL | Key::V), EDIT_PASTE_KEYS);
8359
8360 edit->get_popup()->add_separator();
8361 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/move_first_selected_key_to_cursor", TTRC("Move First Selected Key to Cursor"), Key::BRACKETLEFT), EDIT_MOVE_FIRST_SELECTED_KEY_TO_CURSOR);
8362 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/move_last_selected_key_to_cursor", TTRC("Move Last Selected Key to Cursor"), Key::BRACKETRIGHT), EDIT_MOVE_LAST_SELECTED_KEY_TO_CURSOR);
8363 edit->get_popup()->add_separator();
8364 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/delete_selection", TTRC("Delete Selection"), Key::KEY_DELETE), EDIT_DELETE_SELECTION);
8365
8366 edit->get_popup()->add_separator();
8367 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/goto_next_step", TTRC("Go to Next Step"), KeyModifierMask::CMD_OR_CTRL | Key::RIGHT), EDIT_GOTO_NEXT_STEP);
8368 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/goto_prev_step", TTRC("Go to Previous Step"), KeyModifierMask::CMD_OR_CTRL | Key::LEFT), EDIT_GOTO_PREV_STEP);
8369 edit->get_popup()->add_separator();
8370 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/go_to_next_keyframe", TTRC("Go to Next Keyframe"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::D), EDIT_GOTO_NEXT_KEYFRAME);
8371 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/go_to_previous_keyframe", TTRC("Go to Previous Keyframe"), KeyModifierMask::SHIFT | KeyModifierMask::ALT | Key::A), EDIT_GOTO_PREV_KEYFRAME);
8372 edit->get_popup()->add_separator();
8373
8374 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/add_reset_value", TTRC("Add tracks to RESET")), EDIT_ADD_RESET_KEY);
8375 edit->get_popup()->add_shortcut(ED_SHORTCUT("animation_editor/apply_reset", TTRC("Apply RESET")), EDIT_APPLY_RESET);
8376 edit->get_popup()->add_separator();
8377 edit->get_popup()->add_item(TTR("Bake Animation..."), EDIT_BAKE_ANIMATION);
8378 edit->get_popup()->add_item(TTR("Optimize Animation (no undo)..."), EDIT_OPTIMIZE_ANIMATION);
8379 edit->get_popup()->add_item(TTR("Clean-Up Animation (no undo)..."), EDIT_CLEAN_UP_ANIMATION);
8380
8381 edit->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed));
8382 edit->get_popup()->connect("about_to_popup", callable_mp(this, &AnimationTrackEditor::_edit_menu_about_to_popup));
8383
8384 pick_track = memnew(SceneTreeDialog);
8385 add_child(pick_track);
8386 pick_track->set_title(TTR("Pick a node to animate:"));
8387 pick_track->connect("selected", callable_mp(this, &AnimationTrackEditor::_new_track_node_selected));
8388 pick_track->get_filter_line_edit()->connect(SceneStringName(text_changed), callable_mp(this, &AnimationTrackEditor::_pick_track_filter_text_changed));
8389
8390 prop_selector = memnew(PropertySelector);
8391 add_child(prop_selector);
8392 prop_selector->connect("selected", callable_mp(this, &AnimationTrackEditor::_new_track_property_selected));
8393 prop_selector->set_accessibility_name(TTRC("Track Property"));
8394
8395 method_selector = memnew(PropertySelector);
8396 add_child(method_selector);
8397 method_selector->connect("selected", callable_mp(this, &AnimationTrackEditor::_add_method_key));
8398 method_selector->set_accessibility_name(TTRC("Method Key"));
8399
8400 insert_confirm = memnew(ConfirmationDialog);
8401 add_child(insert_confirm);
8402 insert_confirm->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_confirm_insert_list));
8403 VBoxContainer *icvb = memnew(VBoxContainer);
8404 insert_confirm->add_child(icvb);
8405 insert_confirm_text = memnew(Label);
8406 insert_confirm_text->set_focus_mode(FOCUS_ACCESSIBILITY);
8407 icvb->add_child(insert_confirm_text);
8408 HBoxContainer *ichb = memnew(HBoxContainer);
8409 icvb->add_child(ichb);
8410 insert_confirm_bezier = memnew(CheckBox);
8411 insert_confirm_bezier->set_text(TTR("Use Bezier Curves"));
8412 insert_confirm_bezier->set_pressed(EDITOR_GET("editors/animation/default_create_bezier_tracks"));
8413 ichb->add_child(insert_confirm_bezier);
8414 insert_confirm_reset = memnew(CheckBox);
8415 insert_confirm_reset->set_text(TTR("Create RESET Track(s)", ""));
8416 insert_confirm_reset->set_pressed(EDITOR_GET("editors/animation/default_create_reset_tracks"));
8417 ichb->add_child(insert_confirm_reset);
8418
8419 box_selection = memnew(Control);
8420 box_selection_container->add_child(box_selection);
8421 box_selection->set_mouse_filter(MOUSE_FILTER_IGNORE);
8422 box_selection->hide();
8423 box_selection->connect(SceneStringName(draw), callable_mp(this, &AnimationTrackEditor::_box_selection_draw));
8424
8425 // Default Plugins.
8426
8427 Ref<AnimationTrackEditDefaultPlugin> def_plugin;
8428 def_plugin.instantiate();
8429 add_track_edit_plugin(def_plugin);
8430
8431 // Dialogs.
8432
8433 optimize_dialog = memnew(ConfirmationDialog);
8434 add_child(optimize_dialog);
8435 optimize_dialog->set_title(TTR("Animation Optimizer"));
8436 VBoxContainer *optimize_vb = memnew(VBoxContainer);
8437 optimize_dialog->add_child(optimize_vb);
8438
8439 optimize_velocity_error = memnew(SpinBox);
8440 optimize_velocity_error->set_max(1.0);
8441 optimize_velocity_error->set_min(0.001);
8442 optimize_velocity_error->set_step(0.001);
8443 optimize_velocity_error->set_value(0.01);
8444 optimize_velocity_error->set_accessibility_name(TTRC("Max Velocity Error:"));
8445 optimize_vb->add_margin_child(TTR("Max Velocity Error:"), optimize_velocity_error);
8446 optimize_angular_error = memnew(SpinBox);
8447 optimize_angular_error->set_max(1.0);
8448 optimize_angular_error->set_min(0.001);
8449 optimize_angular_error->set_step(0.001);
8450 optimize_angular_error->set_value(0.01);
8451 optimize_angular_error->set_accessibility_name(TTRC("Max Angular Error:"));
8452 optimize_vb->add_margin_child(TTR("Max Angular Error:"), optimize_angular_error);
8453 optimize_precision_error = memnew(SpinBox);
8454 optimize_precision_error->set_max(6);
8455 optimize_precision_error->set_min(1);
8456 optimize_precision_error->set_step(1);
8457 optimize_precision_error->set_value(3);
8458 optimize_precision_error->set_accessibility_name(TTRC("Max Precision Error:"));
8459 optimize_vb->add_margin_child(TTR("Max Precision Error:"), optimize_precision_error);
8460
8461 optimize_dialog->set_ok_button_text(TTR("Optimize"));
8462 optimize_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_OPTIMIZE_ANIMATION_CONFIRM));
8463
8464 //
8465 cleanup_dialog = memnew(ConfirmationDialog);
8466 add_child(cleanup_dialog);
8467 VBoxContainer *cleanup_vb = memnew(VBoxContainer);
8468 cleanup_dialog->add_child(cleanup_vb);
8469
8470 cleanup_keys_with_trimming_head = memnew(CheckBox);
8471 cleanup_keys_with_trimming_head->set_text(TTR("Trim keys placed in negative time"));
8472 cleanup_keys_with_trimming_head->set_pressed(true);
8473 cleanup_vb->add_child(cleanup_keys_with_trimming_head);
8474
8475 cleanup_keys_with_trimming_end = memnew(CheckBox);
8476 cleanup_keys_with_trimming_end->set_text(TTR("Trim keys placed exceed the animation length"));
8477 cleanup_keys_with_trimming_end->set_pressed(true);
8478 cleanup_vb->add_child(cleanup_keys_with_trimming_end);
8479
8480 cleanup_keys = memnew(CheckBox);
8481 cleanup_keys->set_text(TTR("Remove invalid keys"));
8482 cleanup_keys->set_pressed(true);
8483 cleanup_vb->add_child(cleanup_keys);
8484
8485 cleanup_tracks = memnew(CheckBox);
8486 cleanup_tracks->set_text(TTR("Remove unresolved and empty tracks"));
8487 cleanup_tracks->set_pressed(true);
8488 cleanup_vb->add_child(cleanup_tracks);
8489
8490 cleanup_all = memnew(CheckBox);
8491 cleanup_all->set_text(TTR("Clean-up all animations"));
8492 cleanup_vb->add_child(cleanup_all);
8493
8494 cleanup_dialog->set_title(TTR("Clean-Up Animation(s) (NO UNDO!)"));
8495 cleanup_dialog->set_ok_button_text(TTR("Clean-Up"));
8496
8497 cleanup_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_CLEAN_UP_ANIMATION_CONFIRM));
8498
8499 //
8500 scale_dialog = memnew(ConfirmationDialog);
8501 VBoxContainer *vbc = memnew(VBoxContainer);
8502 scale_dialog->add_child(vbc);
8503
8504 scale = memnew(SpinBox);
8505 scale->set_min(-99999);
8506 scale->set_max(99999);
8507 scale->set_step(0.001);
8508 scale->set_select_all_on_focus(true);
8509 scale->set_accessibility_name(TTRC("Scale Ratio"));
8510 vbc->add_margin_child(TTR("Scale Ratio:"), scale);
8511 scale_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_SCALE_CONFIRM), CONNECT_DEFERRED);
8512 add_child(scale_dialog);
8513
8514 scale_dialog->register_text_enter(scale->get_line_edit());
8515
8516 //
8517 ease_dialog = memnew(ConfirmationDialog);
8518 ease_dialog->set_title(TTR("Select Transition and Easing"));
8519 ease_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_EASE_CONFIRM));
8520 add_child(ease_dialog);
8521 GridContainer *ease_grid = memnew(GridContainer);
8522 ease_grid->set_columns(2);
8523 ease_dialog->add_child(ease_grid);
8524 transition_selection = memnew(OptionButton);
8525 transition_selection->set_accessibility_name(TTRC("Transition Type:"));
8526 transition_selection->add_item(TTR("Linear", "Transition Type"), Tween::TRANS_LINEAR);
8527 transition_selection->add_item(TTR("Sine", "Transition Type"), Tween::TRANS_SINE);
8528 transition_selection->add_item(TTR("Quint", "Transition Type"), Tween::TRANS_QUINT);
8529 transition_selection->add_item(TTR("Quart", "Transition Type"), Tween::TRANS_QUART);
8530 transition_selection->add_item(TTR("Quad", "Transition Type"), Tween::TRANS_QUAD);
8531 transition_selection->add_item(TTR("Expo", "Transition Type"), Tween::TRANS_EXPO);
8532 transition_selection->add_item(TTR("Elastic", "Transition Type"), Tween::TRANS_ELASTIC);
8533 transition_selection->add_item(TTR("Cubic", "Transition Type"), Tween::TRANS_CUBIC);
8534 transition_selection->add_item(TTR("Circ", "Transition Type"), Tween::TRANS_CIRC);
8535 transition_selection->add_item(TTR("Bounce", "Transition Type"), Tween::TRANS_BOUNCE);
8536 transition_selection->add_item(TTR("Back", "Transition Type"), Tween::TRANS_BACK);
8537 transition_selection->add_item(TTR("Spring", "Transition Type"), Tween::TRANS_SPRING);
8538 transition_selection->select(Tween::TRANS_LINEAR); // Default
8539 transition_selection->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Translation context is needed.
8540 ease_selection = memnew(OptionButton);
8541 ease_selection->set_accessibility_name(TTRC("Ease Type:"));
8542 ease_selection->add_item(TTR("Ease In", "Ease Type"), Tween::EASE_IN);
8543 ease_selection->add_item(TTR("Ease Out", "Ease Type"), Tween::EASE_OUT);
8544 ease_selection->add_item(TTR("Ease In-Out", "Ease Type"), Tween::EASE_IN_OUT);
8545 ease_selection->add_item(TTR("Ease Out-In", "Ease Type"), Tween::EASE_OUT_IN);
8546 ease_selection->select(Tween::EASE_IN_OUT); // Default
8547 ease_selection->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); // Translation context is needed.
8548 ease_fps = memnew(SpinBox);
8549 ease_fps->set_min(FPS_DECIMAL);
8550 ease_fps->set_max(999);
8551 ease_fps->set_step(FPS_DECIMAL);
8552 ease_fps->set_value(30); // Default
8553 ease_fps->set_accessibility_name(TTRC("FPS:"));
8554 ease_grid->add_child(memnew(Label(TTR("Transition Type:"))));
8555 ease_grid->add_child(transition_selection);
8556 ease_grid->add_child(memnew(Label(TTR("Ease Type:"))));
8557 ease_grid->add_child(ease_selection);
8558 ease_grid->add_child(memnew(Label(TTR("FPS:"))));
8559 ease_grid->add_child(ease_fps);
8560
8561 //
8562 bake_dialog = memnew(ConfirmationDialog);
8563 bake_dialog->set_title(TTR("Animation Baker"));
8564 bake_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_BAKE_ANIMATION_CONFIRM));
8565 add_child(bake_dialog);
8566 GridContainer *bake_grid = memnew(GridContainer);
8567 bake_grid->set_columns(2);
8568 bake_dialog->add_child(bake_grid);
8569 bake_trs = memnew(CheckBox);
8570 bake_trs->set_accessibility_name(TTRC("3D Pos/Rot/Scl Track:"));
8571 bake_trs->set_pressed(true);
8572 bake_blendshape = memnew(CheckBox);
8573 bake_blendshape->set_accessibility_name(TTRC("Blendshape Track:"));
8574 bake_blendshape->set_pressed(true);
8575 bake_value = memnew(CheckBox);
8576 bake_value->set_accessibility_name(TTRC("Value Track:"));
8577 bake_value->set_pressed(true);
8578 bake_fps = memnew(SpinBox);
8579 bake_fps->set_accessibility_name(TTRC("FPS:"));
8580 bake_fps->set_min(FPS_DECIMAL);
8581 bake_fps->set_max(999);
8582 bake_fps->set_step(FPS_DECIMAL);
8583 bake_fps->set_value(30); // Default
8584 bake_grid->add_child(memnew(Label(TTR("3D Pos/Rot/Scl Track:"))));
8585 bake_grid->add_child(bake_trs);
8586 bake_grid->add_child(memnew(Label(TTR("Blendshape Track:"))));
8587 bake_grid->add_child(bake_blendshape);
8588 bake_grid->add_child(memnew(Label(TTR("Value Track:"))));
8589 bake_grid->add_child(bake_value);
8590 bake_grid->add_child(memnew(Label(TTR("FPS:"))));
8591 bake_grid->add_child(bake_fps);
8592
8593 track_copy_dialog = memnew(ConfirmationDialog);
8594 add_child(track_copy_dialog);
8595 track_copy_dialog->set_title(TTR("Select Tracks to Copy"));
8596 track_copy_dialog->set_ok_button_text(TTR("Copy"));
8597
8598 VBoxContainer *track_copy_vbox = memnew(VBoxContainer);
8599 track_copy_dialog->add_child(track_copy_vbox);
8600
8601 Button *select_all_button = memnew(Button);
8602 select_all_button->set_text(TTR("Select All/None"));
8603 select_all_button->connect(SceneStringName(pressed), callable_mp(this, &AnimationTrackEditor::_select_all_tracks_for_copy));
8604 track_copy_vbox->add_child(select_all_button);
8605
8606 track_copy_select = memnew(Tree);
8607 track_copy_select->set_accessibility_name(TTRC("Copy Selection"));
8608 track_copy_select->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED);
8609 track_copy_select->set_h_size_flags(SIZE_EXPAND_FILL);
8610 track_copy_select->set_v_size_flags(SIZE_EXPAND_FILL);
8611 track_copy_select->set_hide_root(true);
8612 track_copy_vbox->add_child(track_copy_select);
8613 track_copy_dialog->connect(SceneStringName(confirmed), callable_mp(this, &AnimationTrackEditor::_edit_menu_pressed).bind(EDIT_COPY_TRACKS_CONFIRM));
8614
8615 read_only_dialog = memnew(AcceptDialog);
8616 read_only_dialog->set_title(TTRC("Key Insertion Error"));
8617 read_only_dialog->set_text(TTRC("Imported Animation cannot be edited!"));
8618 add_child(read_only_dialog);
8619 }
8620
8621 AnimationTrackEditor::~AnimationTrackEditor() {
8622 if (key_edit) {
8623 memdelete(key_edit);
8624 }
8625 if (multi_key_edit) {
8626 memdelete(multi_key_edit);
8627 }
8628 }
8629
8630 // AnimationTrackKeyEditEditorPlugin.
8631
8632 void AnimationTrackKeyEditEditor::_time_edit_spun() {
8633 _time_edit_entered();
8634 _time_edit_exited();
8635 }
8636
8637 void AnimationTrackKeyEditEditor::_time_edit_entered() {
8638 int key = animation->track_find_key(track, key_ofs, Animation::FIND_MODE_APPROX);
8639 if (key == -1) {
8640 return;
8641 }
8642 key_data_cache.time = animation->track_get_key_time(track, key);
8643 key_data_cache.transition = animation->track_get_key_transition(track, key);
8644 key_data_cache.value = animation->track_get_key_value(track, key);
8645 }
8646
8647 void AnimationTrackKeyEditEditor::_time_edit_exited() {
8648 real_t new_time = spinner->get_value();
8649
8650 if (use_fps) {
8651 real_t fps = animation->get_step();
8652 if (fps > 0) {
8653 fps = 1.0 / fps;
8654 }
8655 new_time /= fps;
8656 }
8657
8658 if (Math::is_equal_approx(new_time, key_data_cache.time)) {
8659 return; // No change.
8660 }
8661
8662 int existing = animation->track_find_key(track, new_time, Animation::FIND_MODE_APPROX);
8663 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
8664 undo_redo->create_action(TTR("Animation Change Keyframe Time"));
8665
8666 if (existing != -1) {
8667 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", track, animation->track_get_key_time(track, existing));
8668 }
8669 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", track, key_data_cache.time);
8670 undo_redo->add_do_method(animation.ptr(), "track_insert_key", track, new_time, key_data_cache.value, key_data_cache.transition);
8671 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", track, new_time);
8672 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, key_data_cache.time, key_data_cache.value, key_data_cache.transition);
8673 if (existing != -1) {
8674 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", track, animation->track_get_key_time(track, existing), animation->track_get_key_value(track, existing), animation->track_get_key_transition(track, existing));
8675 }
8676
8677 // Reselect key.
8678 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
8679 if (ape) {
8680 AnimationTrackEditor *ate = ape->get_track_editor();
8681 if (ate) {
8682 undo_redo->add_do_method(ate, "_clear_selection_for_anim", animation);
8683 undo_redo->add_undo_method(ate, "_clear_selection_for_anim", animation);
8684 undo_redo->add_do_method(ate, "_select_at_anim", animation, track, new_time);
8685 undo_redo->add_undo_method(ate, "_select_at_anim", animation, track, key_data_cache.time);
8686 }
8687 undo_redo->add_do_method(ape, "_animation_update_key_frame");
8688 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
8689 }
8690
8691 undo_redo->commit_action();
8692 }
8693
8694 AnimationTrackKeyEditEditor::AnimationTrackKeyEditEditor(Ref<Animation> p_animation, int p_track, real_t p_key_ofs, bool p_use_fps) {
8695 if (p_animation.is_null()) {
8696 return;
8697 }
8698
8699 animation = p_animation;
8700 track = p_track;
8701 key_ofs = p_key_ofs;
8702 use_fps = p_use_fps;
8703
8704 set_label("Time");
8705
8706 spinner = memnew(EditorSpinSlider);
8707 spinner->set_focus_mode(Control::FOCUS_CLICK);
8708 spinner->set_min(0);
8709 spinner->set_allow_greater(true);
8710 spinner->set_allow_lesser(true);
8711 add_child(spinner);
8712
8713 if (use_fps) {
8714 spinner->set_step(FPS_DECIMAL);
8715 real_t fps = animation->get_step();
8716 if (fps > 0) {
8717 fps = 1.0 / fps;
8718 }
8719 spinner->set_value(key_ofs * fps);
8720 spinner->connect("updown_pressed", callable_mp(this, &AnimationTrackKeyEditEditor::_time_edit_spun), CONNECT_DEFERRED);
8721 } else {
8722 spinner->set_step(SECOND_DECIMAL);
8723 spinner->set_value(key_ofs);
8724 spinner->set_max(animation->get_length());
8725 }
8726
8727 spinner->connect("grabbed", callable_mp(this, &AnimationTrackKeyEditEditor::_time_edit_entered), CONNECT_DEFERRED);
8728 spinner->connect("ungrabbed", callable_mp(this, &AnimationTrackKeyEditEditor::_time_edit_exited), CONNECT_DEFERRED);
8729 spinner->connect("value_focus_entered", callable_mp(this, &AnimationTrackKeyEditEditor::_time_edit_entered), CONNECT_DEFERRED);
8730 spinner->connect("value_focus_exited", callable_mp(this, &AnimationTrackKeyEditEditor::_time_edit_exited), CONNECT_DEFERRED);
8731 }
8732
8733 void AnimationMarkerEdit::_zoom_changed() {
8734 queue_redraw();
8735 play_position->queue_redraw();
8736 }
8737
8738 void AnimationMarkerEdit::_menu_selected(int p_index) {
8739 switch (p_index) {
8740 case MENU_KEY_INSERT: {
8741 _insert_marker(insert_at_pos);
8742 } break;
8743 case MENU_KEY_RENAME: {
8744 if (selection.size() > 0) {
8745 _rename_marker(*selection.last());
8746 }
8747 } break;
8748 case MENU_KEY_DELETE: {
8749 _delete_selected_markers();
8750 } break;
8751 case MENU_KEY_TOGGLE_MARKER_NAMES: {
8752 should_show_all_marker_names = !should_show_all_marker_names;
8753 queue_redraw();
8754 } break;
8755 }
8756 }
8757
8758 void AnimationMarkerEdit::_play_position_draw() {
8759 if (animation.is_null() || play_position_pos < 0) {
8760 return;
8761 }
8762
8763 float scale = timeline->get_zoom_scale();
8764 int px = (play_position_pos - timeline->get_value()) * scale + timeline->get_name_limit();
8765
8766 if (px >= timeline->get_name_limit() && px < (get_size().width - timeline->get_buttons_width())) {
8767 Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
8768 play_position->draw_line(Point2(px, 0), Point2(px, get_size().height), color, Math::round(2 * EDSCALE));
8769 }
8770 }
8771
8772 bool AnimationMarkerEdit::_try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable) {
8773 int limit = timeline->get_name_limit();
8774 int limit_end = get_size().width - timeline->get_buttons_width();
8775 // Left Border including space occupied by keyframes on t=0.
8776 int limit_start_hitbox = limit - type_icon->get_width();
8777
8778 if (p_pos.x >= limit_start_hitbox && p_pos.x <= limit_end) {
8779 int key_idx = -1;
8780 float key_distance = 1e20;
8781 PackedStringArray names = animation->get_marker_names();
8782 for (int i = 0; i < names.size(); i++) {
8783 Rect2 rect = const_cast<AnimationMarkerEdit *>(this)->get_key_rect(timeline->get_zoom_scale());
8784 float offset = animation->get_marker_time(names[i]) - timeline->get_value();
8785 offset = offset * timeline->get_zoom_scale() + limit;
8786 rect.position.x += offset;
8787 if (rect.has_point(p_pos)) {
8788 if (const_cast<AnimationMarkerEdit *>(this)->is_key_selectable_by_distance()) {
8789 float distance = Math::abs(offset - p_pos.x);
8790 if (key_idx == -1 || distance < key_distance) {
8791 key_idx = i;
8792 key_distance = distance;
8793 }
8794 } else {
8795 // First one does it.
8796 break;
8797 }
8798 }
8799 }
8800
8801 if (key_idx != -1) {
8802 if (p_aggregate) {
8803 StringName name = names[key_idx];
8804 if (selection.has(name)) {
8805 if (p_deselectable) {
8806 call_deferred("_deselect_key", name);
8807 moving_selection_pivot = 0.0f;
8808 moving_selection_mouse_begin_x = 0.0f;
8809 }
8810 } else {
8811 call_deferred("_select_key", name, false);
8812 moving_selection_attempt = true;
8813 moving_selection_effective = false;
8814 select_single_attempt = StringName();
8815 moving_selection_pivot = animation->get_marker_time(name);
8816 moving_selection_mouse_begin_x = p_pos.x;
8817 }
8818
8819 } else {
8820 StringName name = names[key_idx];
8821 if (!selection.has(name)) {
8822 call_deferred("_select_key", name, true);
8823 select_single_attempt = StringName();
8824 } else {
8825 select_single_attempt = name;
8826 }
8827
8828 moving_selection_attempt = true;
8829 moving_selection_effective = false;
8830 moving_selection_pivot = animation->get_marker_time(name);
8831 moving_selection_mouse_begin_x = p_pos.x;
8832 }
8833
8834 if (read_only) {
8835 moving_selection_attempt = false;
8836 moving_selection_pivot = 0.0f;
8837 moving_selection_mouse_begin_x = 0.0f;
8838 }
8839 return true;
8840 }
8841 }
8842
8843 return false;
8844 }
8845
8846 bool AnimationMarkerEdit::_is_ui_pos_in_current_section(const Point2 &p_pos) {
8847 int limit = timeline->get_name_limit();
8848 int limit_end = get_size().width - timeline->get_buttons_width();
8849
8850 if (p_pos.x >= limit && p_pos.x <= limit_end) {
8851 PackedStringArray section = get_selected_section();
8852 if (!section.is_empty()) {
8853 StringName start_marker = section[0];
8854 StringName end_marker = section[1];
8855 float start_offset = (animation->get_marker_time(start_marker) - timeline->get_value()) * timeline->get_zoom_scale() + limit;
8856 float end_offset = (animation->get_marker_time(end_marker) - timeline->get_value()) * timeline->get_zoom_scale() + limit;
8857 return p_pos.x >= start_offset && p_pos.x <= end_offset;
8858 }
8859 }
8860
8861 return false;
8862 }
8863
8864 HBoxContainer *AnimationMarkerEdit::_create_hbox_labeled_control(const String &p_text, Control *p_control) const {
8865 HBoxContainer *hbox = memnew(HBoxContainer);
8866 Label *label = memnew(Label);
8867 label->set_text(p_text);
8868 hbox->add_child(label);
8869 hbox->add_child(p_control);
8870 hbox->set_h_size_flags(SIZE_EXPAND_FILL);
8871 label->set_h_size_flags(SIZE_EXPAND_FILL);
8872 label->set_stretch_ratio(1.0);
8873 p_control->set_h_size_flags(SIZE_EXPAND_FILL);
8874 p_control->set_stretch_ratio(1.0);
8875 return hbox;
8876 }
8877
8878 void AnimationMarkerEdit::_update_key_edit() {
8879 _clear_key_edit();
8880 if (animation.is_null()) {
8881 return;
8882 }
8883
8884 if (selection.size() == 1) {
8885 key_edit = memnew(AnimationMarkerKeyEdit);
8886 key_edit->animation = animation;
8887 key_edit->animation_read_only = read_only;
8888 key_edit->marker_name = *selection.begin();
8889 key_edit->use_fps = timeline->is_using_fps();
8890 key_edit->marker_edit = this;
8891
8892 EditorNode::get_singleton()->push_item(key_edit);
8893
8894 InspectorDock::get_singleton()->set_info(TTR("Marker name is read-only in the inspector."), TTR("A marker's name can only be changed by right-clicking it in the animation editor and selecting \"Rename Marker\", in order to make sure that marker names are all unique."), true);
8895 } else if (selection.size() > 1) {
8896 multi_key_edit = memnew(AnimationMultiMarkerKeyEdit);
8897 multi_key_edit->animation = animation;
8898 multi_key_edit->animation_read_only = read_only;
8899 multi_key_edit->marker_edit = this;
8900 for (const StringName &name : selection) {
8901 multi_key_edit->marker_names.push_back(name);
8902 }
8903
8904 EditorNode::get_singleton()->push_item(multi_key_edit);
8905 }
8906 }
8907
8908 void AnimationMarkerEdit::_clear_key_edit() {
8909 if (key_edit) {
8910 // If key edit is the object being inspected, remove it first.
8911 if (InspectorDock::get_inspector_singleton()->get_edited_object() == key_edit) {
8912 EditorNode::get_singleton()->push_item(nullptr);
8913 }
8914
8915 // Then actually delete it.
8916 memdelete(key_edit);
8917 key_edit = nullptr;
8918 }
8919
8920 if (multi_key_edit) {
8921 if (InspectorDock::get_inspector_singleton()->get_edited_object() == multi_key_edit) {
8922 EditorNode::get_singleton()->push_item(nullptr);
8923 }
8924
8925 memdelete(multi_key_edit);
8926 multi_key_edit = nullptr;
8927 }
8928 }
8929
8930 void AnimationMarkerEdit::_bind_methods() {
8931 ClassDB::bind_method("_clear_selection_for_anim", &AnimationMarkerEdit::_clear_selection_for_anim);
8932 ClassDB::bind_method("_select_key", &AnimationMarkerEdit::_select_key);
8933 ClassDB::bind_method("_deselect_key", &AnimationMarkerEdit::_deselect_key);
8934 }
8935
8936 void AnimationMarkerEdit::_notification(int p_what) {
8937 switch (p_what) {
8938 case NOTIFICATION_THEME_CHANGED: {
8939 if (animation.is_null()) {
8940 return;
8941 }
8942
8943 type_icon = get_editor_theme_icon(SNAME("Marker"));
8944 selected_icon = get_editor_theme_icon(SNAME("MarkerSelected"));
8945 } break;
8946
8947 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
8948 RID ae = get_accessibility_element();
8949 ERR_FAIL_COND(ae.is_null());
8950
8951 //TODO
8952 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
8953 DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Animation marker editor")));
8954 } break;
8955
8956 case NOTIFICATION_DRAW: {
8957 if (animation.is_null()) {
8958 return;
8959 }
8960
8961 int limit = timeline->get_name_limit();
8962 int limit_end = get_size().width - timeline->get_buttons_width();
8963 float scale = timeline->get_zoom_scale();
8964
8965 Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
8966 Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
8967
8968 // SECTION PREVIEW //
8969
8970 {
8971 PackedStringArray section = get_selected_section();
8972 if (section.size() == 2) {
8973 StringName start_marker = section[0];
8974 StringName end_marker = section[1];
8975 double start_time = animation->get_marker_time(start_marker);
8976 double end_time = animation->get_marker_time(end_marker);
8977
8978 // When AnimationPlayer is playing, don't move the preview rect, so it still indicates the playback section.
8979 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
8980 if (moving_selection && !(player && player->is_playing())) {
8981 start_time += moving_selection_offset;
8982 end_time += moving_selection_offset;
8983 }
8984
8985 if (start_time < animation->get_length() && end_time >= 0) {
8986 float start_ofs = MAX(0, start_time) - timeline->get_value();
8987 float end_ofs = MIN(animation->get_length(), end_time) - timeline->get_value();
8988 start_ofs = start_ofs * scale + limit;
8989 end_ofs = end_ofs * scale + limit;
8990 start_ofs = MAX(start_ofs, limit);
8991 end_ofs = MIN(end_ofs, limit_end);
8992 Rect2 rect;
8993 rect.set_position(Vector2(start_ofs, 0));
8994 rect.set_size(Vector2(end_ofs - start_ofs, get_size().height));
8995
8996 draw_rect(rect, Color(1, 0.1, 0.1, 0.2));
8997 }
8998 }
8999 }
9000
9001 // KEYFRAMES //
9002
9003 draw_bg(limit, get_size().width - timeline->get_buttons_width());
9004
9005 {
9006 PackedStringArray names = animation->get_marker_names();
9007 for (int i = 0; i < names.size(); i++) {
9008 StringName name = names[i];
9009 bool is_selected = selection.has(name);
9010 float offset = animation->get_marker_time(name) - timeline->get_value();
9011 if (is_selected && moving_selection) {
9012 offset += moving_selection_offset;
9013 }
9014
9015 offset = offset * scale + limit;
9016
9017 draw_key(name, scale, int(offset), is_selected, limit, limit_end);
9018
9019 const int font_size = 12 * EDSCALE;
9020 Size2 string_size = font->get_string_size(name, HORIZONTAL_ALIGNMENT_LEFT, -1.0, font_size);
9021 if (int(offset) <= limit_end && int(offset) >= limit && should_show_all_marker_names) {
9022 float bottom = get_size().height + string_size.y - font->get_descent(font_size);
9023 float extrusion = MAX(0, offset + string_size.x - limit_end); // How much the string would extrude outside limit_end if unadjusted.
9024 Color marker_color = animation->get_marker_color(name);
9025 float margin = 4 * EDSCALE;
9026 Point2 pos = Point2(offset - extrusion + margin, bottom + margin);
9027 draw_string(font, pos, name, HORIZONTAL_ALIGNMENT_LEFT, -1.0, font_size, marker_color);
9028 draw_string_outline(font, pos, name, HORIZONTAL_ALIGNMENT_LEFT, -1.0, font_size, 1, color);
9029 }
9030 }
9031 }
9032
9033 draw_fg(limit, limit_end);
9034 } break;
9035
9036 case NOTIFICATION_MOUSE_ENTER:
9037 hovered = true;
9038 queue_redraw();
9039 break;
9040 case NOTIFICATION_MOUSE_EXIT:
9041 hovered = false;
9042 // When the mouse cursor exits the track, we're no longer hovering any keyframe.
9043 hovering_marker = StringName();
9044 queue_redraw();
9045 break;
9046 }
9047 }
9048
9049 void AnimationMarkerEdit::gui_input(const Ref<InputEvent> &p_event) {
9050 ERR_FAIL_COND(p_event.is_null());
9051
9052 if (animation.is_null()) {
9053 return;
9054 }
9055
9056 if (p_event->is_pressed()) {
9057 if (ED_IS_SHORTCUT("animation_marker_edit/rename_marker", p_event)) {
9058 if (!read_only) {
9059 _menu_selected(MENU_KEY_RENAME);
9060 }
9061 }
9062
9063 if (ED_IS_SHORTCUT("animation_marker_edit/delete_selection", p_event)) {
9064 if (!read_only) {
9065 _menu_selected(MENU_KEY_DELETE);
9066 }
9067 }
9068
9069 if (ED_IS_SHORTCUT("animation_marker_edit/toggle_marker_names", p_event)) {
9070 if (!read_only) {
9071 _menu_selected(MENU_KEY_TOGGLE_MARKER_NAMES);
9072 }
9073 }
9074 }
9075
9076 Ref<InputEventMouseButton> mb = p_event;
9077
9078 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
9079 Point2 pos = mb->get_position();
9080 if (_try_select_at_ui_pos(pos, mb->is_command_or_control_pressed() || mb->is_shift_pressed(), true)) {
9081 accept_event();
9082 } else if (!_is_ui_pos_in_current_section(pos)) {
9083 _clear_selection_for_anim(animation);
9084 }
9085 }
9086
9087 // Check if moving markers or resizing the timeline first, so they can be canceled instead.
9088 if (!moving_selection && !timeline->resizing_timeline && mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
9089 Point2 pos = mb->get_position();
9090 if (pos.x >= timeline->get_name_limit() && pos.x <= get_size().width - timeline->get_buttons_width()) {
9091 // Can do something with menu too! show insert key.
9092 float offset = (pos.x - timeline->get_name_limit()) / timeline->get_zoom_scale();
9093 if (!read_only) {
9094 bool selected = _try_select_at_ui_pos(pos, mb->is_command_or_control_pressed() || mb->is_shift_pressed(), false);
9095 moving_selection_attempt = false;
9096
9097 menu->clear();
9098 menu->add_icon_item(get_editor_theme_icon(SNAME("Key")), TTR("Insert Marker..."), MENU_KEY_INSERT);
9099
9100 if (selected || selection.size() > 0) {
9101 menu->add_icon_item(get_editor_theme_icon(SNAME("Edit")), TTR("Rename Marker"), MENU_KEY_RENAME);
9102 menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete Marker(s)"), MENU_KEY_DELETE);
9103 }
9104
9105 menu->add_icon_item(get_editor_theme_icon(should_show_all_marker_names ? SNAME("GuiChecked") : SNAME("GuiUnchecked")), TTR("Show All Marker Names"), MENU_KEY_TOGGLE_MARKER_NAMES);
9106 menu->reset_size();
9107
9108 menu->set_position(get_screen_position() + get_local_mouse_position());
9109 menu->popup();
9110
9111 insert_at_pos = offset + timeline->get_value();
9112 accept_event();
9113
9114 timeline->_stop_dragging();
9115 }
9116 }
9117 }
9118
9119 if (mb.is_valid() && moving_selection_attempt) {
9120 if (!mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
9121 moving_selection_attempt = false;
9122 if (moving_selection && moving_selection_effective) {
9123 if (Math::abs(moving_selection_offset) > CMP_EPSILON) {
9124 _move_selection_commit();
9125 accept_event(); // So play position doesn't snap to the end of move selection.
9126 }
9127 } else if (select_single_attempt) {
9128 call_deferred("_select_key", select_single_attempt, true);
9129
9130 // First select click should not affect play position.
9131 if (!selection.has(select_single_attempt)) {
9132 accept_event();
9133 } else {
9134 // Second click and onwards should snap to marker time.
9135 double ofs = animation->get_marker_time(select_single_attempt);
9136 timeline->set_play_position(ofs);
9137 timeline->emit_signal(SNAME("timeline_changed"), ofs, mb->is_alt_pressed());
9138 accept_event();
9139 }
9140 } else {
9141 // First select click should not affect play position.
9142 if (!selection.has(select_single_attempt)) {
9143 accept_event();
9144 }
9145 }
9146
9147 moving_selection = false;
9148 select_single_attempt = StringName();
9149 }
9150
9151 if (moving_selection && mb->is_pressed() && mb->get_button_index() == MouseButton::RIGHT) {
9152 moving_selection_attempt = false;
9153 moving_selection = false;
9154 _move_selection_cancel();
9155 }
9156 }
9157
9158 Ref<InputEventMouseMotion> mm = p_event;
9159
9160 if (mm.is_valid()) {
9161 const StringName previous_hovering_marker = hovering_marker;
9162
9163 // Hovering compressed keyframes for editing is not possible.
9164 const float scale = timeline->get_zoom_scale();
9165 const int limit = timeline->get_name_limit();
9166 const int limit_end = get_size().width - timeline->get_buttons_width();
9167 // Left Border including space occupied by keyframes on t=0.
9168 const int limit_start_hitbox = limit - type_icon->get_width();
9169 const Point2 pos = mm->get_position();
9170
9171 if (pos.x >= limit_start_hitbox && pos.x <= limit_end) {
9172 // Use the same logic as key selection to ensure that hovering accurately represents
9173 // which key will be selected when clicking.
9174 int key_idx = -1;
9175 float key_distance = 1e20;
9176
9177 hovering_marker = StringName();
9178
9179 PackedStringArray names = animation->get_marker_names();
9180
9181 // Hovering should happen in the opposite order of drawing for more accurate overlap hovering.
9182 for (int i = names.size() - 1; i >= 0; i--) {
9183 StringName name = names[i];
9184 Rect2 rect = get_key_rect(scale);
9185 float offset = animation->get_marker_time(name) - timeline->get_value();
9186 offset = offset * scale + limit;
9187 rect.position.x += offset;
9188
9189 if (rect.has_point(pos)) {
9190 if (is_key_selectable_by_distance()) {
9191 const float distance = Math::abs(offset - pos.x);
9192 if (key_idx == -1 || distance < key_distance) {
9193 key_idx = i;
9194 key_distance = distance;
9195 hovering_marker = name;
9196 }
9197 } else {
9198 // First one does it.
9199 hovering_marker = name;
9200 break;
9201 }
9202 }
9203 }
9204
9205 if (hovering_marker != previous_hovering_marker) {
9206 // Required to draw keyframe hover feedback on the correct keyframe.
9207 queue_redraw();
9208 }
9209 }
9210 }
9211
9212 if (mm.is_valid() && mm->get_button_mask().has_flag(MouseButtonMask::LEFT) && moving_selection_attempt) {
9213 if (!moving_selection) {
9214 moving_selection = true;
9215 _move_selection_begin();
9216 }
9217
9218 float moving_begin_time = ((moving_selection_mouse_begin_x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
9219 float new_time = ((mm->get_position().x - timeline->get_name_limit()) / timeline->get_zoom_scale()) + timeline->get_value();
9220 float delta = new_time - moving_begin_time;
9221 float snapped_time = editor->snap_time(moving_selection_pivot + delta);
9222
9223 float offset = 0.0;
9224 if (Math::abs(editor->get_moving_selection_offset()) > CMP_EPSILON || (snapped_time > moving_selection_pivot && delta > CMP_EPSILON) || (snapped_time < moving_selection_pivot && delta < -CMP_EPSILON)) {
9225 offset = snapped_time - moving_selection_pivot;
9226 moving_selection_effective = true;
9227 }
9228
9229 _move_selection(offset);
9230 }
9231 }
9232
9233 String AnimationMarkerEdit::get_tooltip(const Point2 &p_pos) const {
9234 if (animation.is_null()) {
9235 return Control::get_tooltip(p_pos);
9236 }
9237
9238 int limit = timeline->get_name_limit();
9239 int limit_end = get_size().width - timeline->get_buttons_width();
9240 // Left Border including space occupied by keyframes on t=0.
9241 int limit_start_hitbox = limit - type_icon->get_width();
9242
9243 if (p_pos.x >= limit_start_hitbox && p_pos.x <= limit_end) {
9244 int key_idx = -1;
9245 float key_distance = 1e20;
9246
9247 PackedStringArray names = animation->get_marker_names();
9248
9249 // Select should happen in the opposite order of drawing for more accurate overlap select.
9250 for (int i = names.size() - 1; i >= 0; i--) {
9251 StringName name = names[i];
9252 Rect2 rect = const_cast<AnimationMarkerEdit *>(this)->get_key_rect(timeline->get_zoom_scale());
9253 float offset = animation->get_marker_time(name) - timeline->get_value();
9254 offset = offset * timeline->get_zoom_scale() + limit;
9255 rect.position.x += offset;
9256
9257 if (rect.has_point(p_pos)) {
9258 if (const_cast<AnimationMarkerEdit *>(this)->is_key_selectable_by_distance()) {
9259 float distance = Math::abs(offset - p_pos.x);
9260 if (key_idx == -1 || distance < key_distance) {
9261 key_idx = i;
9262 key_distance = distance;
9263 }
9264 } else {
9265 // First one does it.
9266 break;
9267 }
9268 }
9269 }
9270
9271 if (key_idx != -1) {
9272 String name = names[key_idx];
9273 String text = TTR("Time (s):") + " " + TranslationServer::get_singleton()->format_number(rtos(Math::snapped(animation->get_marker_time(name), 0.0001)), _get_locale()) + "\n";
9274 text += TTR("Marker:") + " " + name + "\n";
9275 return text;
9276 }
9277 }
9278
9279 return Control::get_tooltip(p_pos);
9280 }
9281
9282 int AnimationMarkerEdit::get_key_height() const {
9283 if (animation.is_null()) {
9284 return 0;
9285 }
9286
9287 return type_icon->get_height();
9288 }
9289
9290 Rect2 AnimationMarkerEdit::get_key_rect(float p_pixels_sec) const {
9291 if (animation.is_null()) {
9292 return Rect2();
9293 }
9294
9295 Rect2 rect = Rect2(-type_icon->get_width() / 2, get_size().height - type_icon->get_size().height, type_icon->get_width(), type_icon->get_size().height);
9296
9297 // Make it a big easier to click.
9298 rect.position.x -= rect.size.x * 0.5;
9299 rect.size.x *= 2;
9300 return rect;
9301 }
9302
9303 PackedStringArray AnimationMarkerEdit::get_selected_section() const {
9304 if (selection.size() >= 2) {
9305 PackedStringArray arr;
9306 arr.push_back(""); // Marker with smallest time.
9307 arr.push_back(""); // Marker with largest time.
9308 double min_time = Math::INF;
9309 double max_time = -Math::INF;
9310 for (const StringName &marker_name : selection) {
9311 double time = animation->get_marker_time(marker_name);
9312 if (time < min_time) {
9313 arr.set(0, marker_name);
9314 min_time = time;
9315 }
9316 if (time > max_time) {
9317 arr.set(1, marker_name);
9318 max_time = time;
9319 }
9320 }
9321 return arr;
9322 }
9323
9324 return PackedStringArray();
9325 }
9326
9327 bool AnimationMarkerEdit::is_marker_selected(const StringName &p_marker) const {
9328 return selection.has(p_marker);
9329 }
9330
9331 bool AnimationMarkerEdit::is_key_selectable_by_distance() const {
9332 return true;
9333 }
9334
9335 void AnimationMarkerEdit::draw_key(const StringName &p_name, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
9336 if (animation.is_null()) {
9337 return;
9338 }
9339
9340 if (p_x < p_clip_left || p_x > p_clip_right) {
9341 return;
9342 }
9343
9344 Ref<Texture2D> icon_to_draw = p_selected ? selected_icon : type_icon;
9345
9346 Vector2 ofs(p_x - icon_to_draw->get_width() / 2, int(get_size().height - icon_to_draw->get_height()));
9347
9348 // Don't apply custom marker color when the key is selected.
9349 Color marker_color = p_selected ? Color(1, 1, 1) : animation->get_marker_color(p_name);
9350
9351 // Use a different color for the currently hovered key.
9352 // The color multiplier is chosen to work with both dark and light editor themes,
9353 // and on both unselected and selected key icons.
9354 draw_texture(
9355 icon_to_draw,
9356 ofs,
9357 p_name == hovering_marker ? get_theme_color(SNAME("folder_icon_color"), SNAME("FileDialog")) : marker_color);
9358 }
9359
9360 void AnimationMarkerEdit::draw_bg(int p_clip_left, int p_clip_right) {
9361 }
9362
9363 void AnimationMarkerEdit::draw_fg(int p_clip_left, int p_clip_right) {
9364 }
9365
9366 Ref<Animation> AnimationMarkerEdit::get_animation() const {
9367 return animation;
9368 }
9369
9370 void AnimationMarkerEdit::set_animation(const Ref<Animation> &p_animation, bool p_read_only) {
9371 if (animation.is_valid()) {
9372 _clear_selection_for_anim(animation);
9373 }
9374 animation = p_animation;
9375 read_only = p_read_only;
9376 type_icon = get_editor_theme_icon(SNAME("Marker"));
9377 selected_icon = get_editor_theme_icon(SNAME("MarkerSelected"));
9378
9379 queue_redraw();
9380 }
9381
9382 Size2 AnimationMarkerEdit::get_minimum_size() const {
9383 Ref<Texture2D> texture = get_editor_theme_icon(SNAME("Object"));
9384 Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
9385 int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
9386 int separation = get_theme_constant(SNAME("v_separation"), SNAME("ItemList"));
9387
9388 int max_h = MAX(texture->get_height(), font->get_height(font_size));
9389 max_h = MAX(max_h, get_key_height());
9390
9391 return Vector2(1, max_h + separation);
9392 }
9393
9394 void AnimationMarkerEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
9395 timeline = p_timeline;
9396 timeline->connect("zoom_changed", callable_mp(this, &AnimationMarkerEdit::_zoom_changed));
9397 timeline->connect("name_limit_changed", callable_mp(this, &AnimationMarkerEdit::_zoom_changed));
9398 }
9399
9400 void AnimationMarkerEdit::set_editor(AnimationTrackEditor *p_editor) {
9401 editor = p_editor;
9402 }
9403
9404 void AnimationMarkerEdit::set_play_position(float p_pos) {
9405 play_position_pos = p_pos;
9406 play_position->queue_redraw();
9407 }
9408
9409 void AnimationMarkerEdit::update_play_position() {
9410 play_position->queue_redraw();
9411 }
9412
9413 void AnimationMarkerEdit::set_use_fps(bool p_use_fps) {
9414 if (key_edit) {
9415 key_edit->use_fps = p_use_fps;
9416 key_edit->notify_property_list_changed();
9417 }
9418 }
9419
9420 void AnimationMarkerEdit::_move_selection_begin() {
9421 moving_selection = true;
9422 moving_selection_offset = 0;
9423 }
9424
9425 void AnimationMarkerEdit::_move_selection(float p_offset) {
9426 moving_selection_offset = p_offset;
9427 queue_redraw();
9428 }
9429
9430 void AnimationMarkerEdit::_move_selection_commit() {
9431 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9432 undo_redo->create_action(TTR("Animation Move Markers"));
9433
9434 for (HashSet<StringName>::Iterator E = selection.last(); E; --E) {
9435 StringName name = *E;
9436 double time = animation->get_marker_time(name);
9437 float newpos = time + moving_selection_offset;
9438 undo_redo->add_do_method(animation.ptr(), "remove_marker", name);
9439 undo_redo->add_do_method(animation.ptr(), "add_marker", name, newpos);
9440 undo_redo->add_do_method(animation.ptr(), "set_marker_color", name, animation->get_marker_color(name));
9441 undo_redo->add_undo_method(animation.ptr(), "remove_marker", name);
9442 undo_redo->add_undo_method(animation.ptr(), "add_marker", name, time);
9443 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", name, animation->get_marker_color(name));
9444
9445 // add_marker will overwrite the overlapped key on the redo pass, so we add it back on the undo pass.
9446 if (StringName overlap = animation->get_marker_at_time(newpos)) {
9447 if (select_single_attempt == overlap) {
9448 select_single_attempt = "";
9449 }
9450 undo_redo->add_undo_method(animation.ptr(), "add_marker", overlap, newpos);
9451 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", overlap, animation->get_marker_color(overlap));
9452 }
9453 }
9454
9455 moving_selection = false;
9456 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
9457 if (player) {
9458 PackedStringArray selected_section = get_selected_section();
9459 if (selected_section.size() >= 2) {
9460 undo_redo->add_do_method(player, "set_section_with_markers", selected_section[0], selected_section[1]);
9461 undo_redo->add_undo_method(player, "set_section_with_markers", selected_section[0], selected_section[1]);
9462 }
9463 }
9464 undo_redo->add_do_method(timeline, "queue_redraw");
9465 undo_redo->add_undo_method(timeline, "queue_redraw");
9466 undo_redo->add_do_method(this, "queue_redraw");
9467 undo_redo->add_undo_method(this, "queue_redraw");
9468 undo_redo->commit_action();
9469 _update_key_edit();
9470 }
9471
9472 void AnimationMarkerEdit::_delete_selected_markers() {
9473 if (selection.size()) {
9474 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9475 undo_redo->create_action(TTR("Animation Delete Markers"));
9476 for (const StringName &name : selection) {
9477 double time = animation->get_marker_time(name);
9478 undo_redo->add_do_method(animation.ptr(), "remove_marker", name);
9479 undo_redo->add_undo_method(animation.ptr(), "add_marker", name, time);
9480 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", name, animation->get_marker_color(name));
9481 }
9482 _clear_selection_for_anim(animation);
9483
9484 undo_redo->add_do_method(this, "queue_redraw");
9485 undo_redo->add_undo_method(this, "queue_redraw");
9486 undo_redo->commit_action();
9487 _update_key_edit();
9488 }
9489 }
9490
9491 void AnimationMarkerEdit::_move_selection_cancel() {
9492 moving_selection = false;
9493 queue_redraw();
9494 }
9495
9496 void AnimationMarkerEdit::_clear_selection(bool p_update) {
9497 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
9498 if (player) {
9499 player->reset_section();
9500 }
9501
9502 selection.clear();
9503
9504 if (p_update) {
9505 queue_redraw();
9506 }
9507
9508 _clear_key_edit();
9509 }
9510
9511 void AnimationMarkerEdit::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
9512 if (animation != p_anim) {
9513 return;
9514 }
9515
9516 _clear_selection(true);
9517 }
9518
9519 void AnimationMarkerEdit::_select_key(const StringName &p_name, bool is_single) {
9520 if (is_single) {
9521 _clear_selection(false);
9522 }
9523
9524 selection.insert(p_name);
9525
9526 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
9527 if (player) {
9528 if (selection.size() >= 2) {
9529 PackedStringArray selected_section = get_selected_section();
9530 double start_time = animation->get_marker_time(selected_section[0]);
9531 double end_time = animation->get_marker_time(selected_section[1]);
9532 player->set_section(start_time, end_time);
9533 } else {
9534 player->reset_section();
9535 }
9536 }
9537
9538 queue_redraw();
9539 _update_key_edit();
9540
9541 editor->_clear_selection(editor->is_selection_active());
9542 }
9543
9544 void AnimationMarkerEdit::_deselect_key(const StringName &p_name) {
9545 selection.erase(p_name);
9546
9547 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
9548 if (player) {
9549 if (selection.size() >= 2) {
9550 PackedStringArray selected_section = get_selected_section();
9551 double start_time = animation->get_marker_time(selected_section[0]);
9552 double end_time = animation->get_marker_time(selected_section[1]);
9553 player->set_section(start_time, end_time);
9554 } else {
9555 player->reset_section();
9556 }
9557 }
9558
9559 queue_redraw();
9560 _update_key_edit();
9561 }
9562
9563 void AnimationMarkerEdit::_insert_marker(float p_ofs) {
9564 if (editor->is_snap_timeline_enabled()) {
9565 p_ofs = editor->snap_time(p_ofs);
9566 }
9567
9568 editor->resolve_insertion_offset(p_ofs);
9569
9570 marker_insert_confirm->popup_centered(Size2(200, 100) * EDSCALE);
9571 marker_insert_color->set_pick_color(Color(1, 1, 1));
9572
9573 String base = "new_marker";
9574 int count = 1;
9575 while (true) {
9576 String attempt = base;
9577 if (count > 1) {
9578 attempt += vformat("_%d", count);
9579 }
9580 if (animation->has_marker(attempt)) {
9581 count++;
9582 continue;
9583 }
9584 base = attempt;
9585 break;
9586 }
9587
9588 marker_insert_new_name->set_text(base);
9589 _marker_insert_new_name_changed(base);
9590 marker_insert_ofs = p_ofs;
9591 }
9592
9593 void AnimationMarkerEdit::_rename_marker(const StringName &p_name) {
9594 marker_rename_confirm->popup_centered(Size2i(200, 0) * EDSCALE);
9595 marker_rename_prev_name = p_name;
9596 marker_rename_new_name->set_text(p_name);
9597 }
9598
9599 void AnimationMarkerEdit::_marker_insert_confirmed() {
9600 StringName name = marker_insert_new_name->get_text();
9601
9602 if (animation->has_marker(name)) {
9603 marker_insert_error_dialog->set_text(vformat(TTR("Marker '%s' already exists!"), name));
9604 marker_insert_error_dialog->popup_centered();
9605 return;
9606 }
9607
9608 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9609
9610 undo_redo->create_action(TTR("Add Marker Key"));
9611 undo_redo->add_do_method(animation.ptr(), "add_marker", name, marker_insert_ofs);
9612 undo_redo->add_undo_method(animation.ptr(), "remove_marker", name);
9613 StringName existing_marker = animation->get_marker_at_time(marker_insert_ofs);
9614 if (existing_marker) {
9615 undo_redo->add_undo_method(animation.ptr(), "add_marker", existing_marker, marker_insert_ofs);
9616 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", existing_marker, animation->get_marker_color(existing_marker));
9617 }
9618 undo_redo->add_do_method(animation.ptr(), "set_marker_color", name, marker_insert_color->get_pick_color());
9619
9620 undo_redo->add_do_method(this, "queue_redraw");
9621 undo_redo->add_undo_method(this, "queue_redraw");
9622
9623 undo_redo->commit_action();
9624
9625 marker_insert_confirm->hide();
9626 }
9627
9628 void AnimationMarkerEdit::_marker_insert_new_name_changed(const String &p_text) {
9629 marker_insert_confirm->get_ok_button()->set_disabled(p_text.is_empty());
9630 }
9631
9632 void AnimationMarkerEdit::_marker_rename_confirmed() {
9633 StringName new_name = marker_rename_new_name->get_text();
9634 StringName prev_name = marker_rename_prev_name;
9635
9636 if (new_name == StringName()) {
9637 marker_rename_error_dialog->set_text(TTR("Empty marker names are not allowed."));
9638 marker_rename_error_dialog->popup_centered();
9639 return;
9640 }
9641
9642 if (new_name != prev_name && animation->has_marker(new_name)) {
9643 marker_rename_error_dialog->set_text(vformat(TTR("Marker '%s' already exists!"), new_name));
9644 marker_rename_error_dialog->popup_centered();
9645 return;
9646 }
9647
9648 if (prev_name != new_name) {
9649 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9650 undo_redo->create_action(TTR("Rename Marker"));
9651 undo_redo->add_do_method(animation.ptr(), "remove_marker", prev_name);
9652 undo_redo->add_do_method(animation.ptr(), "add_marker", new_name, animation->get_marker_time(prev_name));
9653 undo_redo->add_do_method(animation.ptr(), "set_marker_color", new_name, animation->get_marker_color(prev_name));
9654 undo_redo->add_undo_method(animation.ptr(), "remove_marker", new_name);
9655 undo_redo->add_undo_method(animation.ptr(), "add_marker", prev_name, animation->get_marker_time(prev_name));
9656 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", prev_name, animation->get_marker_color(prev_name));
9657 undo_redo->add_do_method(this, "_select_key", new_name, true);
9658 undo_redo->add_undo_method(this, "_select_key", prev_name, true);
9659 undo_redo->commit_action();
9660 select_single_attempt = StringName();
9661 }
9662 marker_rename_confirm->hide();
9663 }
9664
9665 void AnimationMarkerEdit::_marker_rename_new_name_changed(const String &p_text) {
9666 marker_rename_confirm->get_ok_button()->set_disabled(p_text.is_empty());
9667 }
9668
9669 AnimationMarkerEdit::AnimationMarkerEdit() {
9670 play_position = memnew(Control);
9671 play_position->set_mouse_filter(MOUSE_FILTER_PASS);
9672 add_child(play_position);
9673 play_position->connect(SceneStringName(draw), callable_mp(this, &AnimationMarkerEdit::_play_position_draw));
9674 set_focus_mode(FOCUS_CLICK);
9675 set_mouse_filter(MOUSE_FILTER_PASS); // Scroll has to work too for selection.
9676
9677 menu = memnew(PopupMenu);
9678 add_child(menu);
9679 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationMarkerEdit::_menu_selected));
9680 menu->add_shortcut(ED_SHORTCUT("animation_marker_edit/rename_marker", TTRC("Rename Marker"), Key::R), MENU_KEY_RENAME);
9681 menu->add_shortcut(ED_SHORTCUT("animation_marker_edit/delete_selection", TTRC("Delete Marker(s)"), Key::KEY_DELETE), MENU_KEY_DELETE);
9682 menu->add_shortcut(ED_SHORTCUT("animation_marker_edit/toggle_marker_names", TTRC("Show All Marker Names"), Key::M), MENU_KEY_TOGGLE_MARKER_NAMES);
9683
9684 marker_insert_confirm = memnew(ConfirmationDialog);
9685 marker_insert_confirm->set_title(TTR("Insert Marker"));
9686 marker_insert_confirm->set_hide_on_ok(false);
9687 marker_insert_confirm->connect(SceneStringName(confirmed), callable_mp(this, &AnimationMarkerEdit::_marker_insert_confirmed));
9688 add_child(marker_insert_confirm);
9689 VBoxContainer *marker_insert_vbox = memnew(VBoxContainer);
9690 marker_insert_vbox->set_anchors_and_offsets_preset(Control::LayoutPreset::PRESET_FULL_RECT);
9691 marker_insert_confirm->add_child(marker_insert_vbox);
9692 marker_insert_new_name = memnew(LineEdit);
9693 marker_insert_new_name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationMarkerEdit::_marker_insert_new_name_changed));
9694 marker_insert_confirm->register_text_enter(marker_insert_new_name);
9695 marker_insert_vbox->add_child(_create_hbox_labeled_control(TTR("Marker Name"), marker_insert_new_name));
9696 marker_insert_color = memnew(ColorPickerButton);
9697 marker_insert_color->set_edit_alpha(false);
9698 marker_insert_color->get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(marker_insert_color->get_picker()));
9699 marker_insert_vbox->add_child(_create_hbox_labeled_control(TTR("Marker Color"), marker_insert_color));
9700 marker_insert_error_dialog = memnew(AcceptDialog);
9701 marker_insert_error_dialog->set_ok_button_text(TTR("Close"));
9702 marker_insert_error_dialog->set_title(TTR("Error!"));
9703 marker_insert_confirm->add_child(marker_insert_error_dialog);
9704
9705 marker_rename_confirm = memnew(ConfirmationDialog);
9706 marker_rename_confirm->set_title(TTR("Rename Marker"));
9707 marker_rename_confirm->set_hide_on_ok(false);
9708 marker_rename_confirm->connect(SceneStringName(confirmed), callable_mp(this, &AnimationMarkerEdit::_marker_rename_confirmed));
9709 add_child(marker_rename_confirm);
9710 VBoxContainer *marker_rename_vbox = memnew(VBoxContainer);
9711 marker_rename_vbox->set_anchors_and_offsets_preset(Control::LayoutPreset::PRESET_FULL_RECT);
9712 marker_rename_confirm->add_child(marker_rename_vbox);
9713 Label *marker_rename_new_name_label = memnew(Label);
9714 marker_rename_new_name_label->set_text(TTR("Change Marker Name:"));
9715 marker_rename_vbox->add_child(marker_rename_new_name_label);
9716 marker_rename_new_name = memnew(LineEdit);
9717 marker_rename_new_name->set_accessibility_name(TTRC("Change Marker Name:"));
9718 marker_rename_new_name->connect(SceneStringName(text_changed), callable_mp(this, &AnimationMarkerEdit::_marker_rename_new_name_changed));
9719 marker_rename_confirm->register_text_enter(marker_rename_new_name);
9720 marker_rename_vbox->add_child(marker_rename_new_name);
9721
9722 marker_rename_error_dialog = memnew(AcceptDialog);
9723 marker_rename_error_dialog->set_ok_button_text(TTR("Close"));
9724 marker_rename_error_dialog->set_title(TTR("Error!"));
9725 marker_rename_confirm->add_child(marker_rename_error_dialog);
9726 }
9727
9728 float AnimationMarkerKeyEdit::get_time() const {
9729 return animation->get_marker_time(marker_name);
9730 }
9731
9732 void AnimationMarkerKeyEdit::_bind_methods() {
9733 ClassDB::bind_method(D_METHOD("_hide_script_from_inspector"), &AnimationMarkerKeyEdit::_hide_script_from_inspector);
9734 ClassDB::bind_method(D_METHOD("_hide_metadata_from_inspector"), &AnimationMarkerKeyEdit::_hide_metadata_from_inspector);
9735 ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &AnimationMarkerKeyEdit::_dont_undo_redo);
9736 ClassDB::bind_method(D_METHOD("_is_read_only"), &AnimationMarkerKeyEdit::_is_read_only);
9737 ClassDB::bind_method(D_METHOD("_set_marker_name"), &AnimationMarkerKeyEdit::_set_marker_name);
9738 }
9739
9740 void AnimationMarkerKeyEdit::_set_marker_name(const StringName &p_name) {
9741 marker_name = p_name;
9742 }
9743
9744 bool AnimationMarkerKeyEdit::_set(const StringName &p_name, const Variant &p_value) {
9745 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9746
9747 if (p_name == "color") {
9748 Color color = p_value;
9749 Color prev_color = animation->get_marker_color(marker_name);
9750 if (color != prev_color) {
9751 undo_redo->create_action(TTR("Edit Marker Color"), UndoRedo::MERGE_ENDS);
9752 undo_redo->add_do_method(animation.ptr(), "set_marker_color", marker_name, color);
9753 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", marker_name, prev_color);
9754 undo_redo->add_do_method(marker_edit, "queue_redraw");
9755 undo_redo->add_undo_method(marker_edit, "queue_redraw");
9756 undo_redo->commit_action();
9757 }
9758 return true;
9759 }
9760
9761 return false;
9762 }
9763
9764 bool AnimationMarkerKeyEdit::_get(const StringName &p_name, Variant &r_ret) const {
9765 if (p_name == "name") {
9766 r_ret = marker_name;
9767 return true;
9768 }
9769
9770 if (p_name == "color") {
9771 r_ret = animation->get_marker_color(marker_name);
9772 return true;
9773 }
9774
9775 return false;
9776 }
9777
9778 void AnimationMarkerKeyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
9779 if (animation.is_null()) {
9780 return;
9781 }
9782
9783 p_list->push_back(PropertyInfo(Variant::STRING_NAME, "name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_READ_ONLY | PROPERTY_USAGE_EDITOR));
9784 p_list->push_back(PropertyInfo(Variant::COLOR, "color", PROPERTY_HINT_COLOR_NO_ALPHA));
9785 }
9786
9787 void AnimationMultiMarkerKeyEdit::_bind_methods() {
9788 ClassDB::bind_method(D_METHOD("_hide_script_from_inspector"), &AnimationMultiMarkerKeyEdit::_hide_script_from_inspector);
9789 ClassDB::bind_method(D_METHOD("_hide_metadata_from_inspector"), &AnimationMultiMarkerKeyEdit::_hide_metadata_from_inspector);
9790 ClassDB::bind_method(D_METHOD("_dont_undo_redo"), &AnimationMultiMarkerKeyEdit::_dont_undo_redo);
9791 ClassDB::bind_method(D_METHOD("_is_read_only"), &AnimationMultiMarkerKeyEdit::_is_read_only);
9792 }
9793
9794 bool AnimationMultiMarkerKeyEdit::_set(const StringName &p_name, const Variant &p_value) {
9795 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9796 if (p_name == "color") {
9797 Color color = p_value;
9798
9799 undo_redo->create_action(TTR("Multi Edit Marker Color"), UndoRedo::MERGE_ENDS);
9800
9801 for (const StringName &marker_name : marker_names) {
9802 undo_redo->add_do_method(animation.ptr(), "set_marker_color", marker_name, color);
9803 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", marker_name, animation->get_marker_color(marker_name));
9804 }
9805
9806 undo_redo->add_do_method(marker_edit, "queue_redraw");
9807 undo_redo->add_undo_method(marker_edit, "queue_redraw");
9808 undo_redo->commit_action();
9809
9810 return true;
9811 }
9812
9813 return false;
9814 }
9815
9816 bool AnimationMultiMarkerKeyEdit::_get(const StringName &p_name, Variant &r_ret) const {
9817 if (p_name == "color") {
9818 r_ret = animation->get_marker_color(marker_names[0]);
9819 return true;
9820 }
9821
9822 return false;
9823 }
9824
9825 void AnimationMultiMarkerKeyEdit::_get_property_list(List<PropertyInfo> *p_list) const {
9826 if (animation.is_null()) {
9827 return;
9828 }
9829
9830 p_list->push_back(PropertyInfo(Variant::COLOR, "color", PROPERTY_HINT_COLOR_NO_ALPHA));
9831 }
9832
9833 // AnimationMarkerKeyEditEditorPlugin
9834
9835 void AnimationMarkerKeyEditEditor::_time_edit_exited() {
9836 real_t new_time = spinner->get_value();
9837
9838 if (use_fps) {
9839 real_t fps = animation->get_step();
9840 if (fps > 0) {
9841 fps = 1.0 / fps;
9842 }
9843 new_time /= fps;
9844 }
9845
9846 real_t prev_time = animation->get_marker_time(marker_name);
9847
9848 if (Math::is_equal_approx(new_time, prev_time)) {
9849 return; // No change.
9850 }
9851
9852 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
9853 undo_redo->create_action(TTR("Animation Change Marker Time"));
9854
9855 Color color = animation->get_marker_color(marker_name);
9856 undo_redo->add_do_method(animation.ptr(), "add_marker", marker_name, new_time);
9857 undo_redo->add_do_method(animation.ptr(), "set_marker_color", marker_name, color);
9858 undo_redo->add_undo_method(animation.ptr(), "remove_marker", marker_name);
9859 undo_redo->add_undo_method(animation.ptr(), "add_marker", marker_name, prev_time);
9860 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", marker_name, color);
9861 StringName existing_marker = animation->get_marker_at_time(new_time);
9862 if (existing_marker) {
9863 undo_redo->add_undo_method(animation.ptr(), "add_marker", existing_marker, animation->get_marker_time(existing_marker));
9864 undo_redo->add_undo_method(animation.ptr(), "set_marker_color", existing_marker, animation->get_marker_color(existing_marker));
9865 }
9866 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
9867 if (ape) {
9868 AnimationTrackEditor *ate = ape->get_track_editor();
9869 if (ate) {
9870 AnimationMarkerEdit *ame = ate->marker_edit;
9871 undo_redo->add_do_method(ame, "queue_redraw");
9872 undo_redo->add_undo_method(ame, "queue_redraw");
9873 }
9874 }
9875 undo_redo->commit_action();
9876 }
9877
9878 AnimationMarkerKeyEditEditor::AnimationMarkerKeyEditEditor(Ref<Animation> p_animation, const StringName &p_name, bool p_use_fps) {
9879 if (p_animation.is_null()) {
9880 return;
9881 }
9882
9883 animation = p_animation;
9884 use_fps = p_use_fps;
9885 marker_name = p_name;
9886
9887 set_label("Time");
9888
9889 spinner = memnew(EditorSpinSlider);
9890 spinner->set_focus_mode(Control::FOCUS_CLICK);
9891 spinner->set_min(0);
9892 spinner->set_allow_greater(true);
9893 spinner->set_allow_lesser(true);
9894 add_child(spinner);
9895
9896 float time = animation->get_marker_time(marker_name);
9897
9898 if (use_fps) {
9899 spinner->set_step(FPS_DECIMAL);
9900 real_t fps = animation->get_step();
9901 if (fps > 0) {
9902 fps = 1.0 / fps;
9903 }
9904 spinner->set_value(time * fps);
9905 spinner->connect("updown_pressed", callable_mp(this, &AnimationMarkerKeyEditEditor::_time_edit_exited), CONNECT_DEFERRED);
9906 } else {
9907 spinner->set_step(SECOND_DECIMAL);
9908 spinner->set_value(time);
9909 spinner->set_max(animation->get_length());
9910 }
9911
9912 spinner->connect("ungrabbed", callable_mp(this, &AnimationMarkerKeyEditEditor::_time_edit_exited), CONNECT_DEFERRED);
9913 spinner->connect("value_focus_exited", callable_mp(this, &AnimationMarkerKeyEditEditor::_time_edit_exited), CONNECT_DEFERRED);
9914 }