Debian Codegraph @

editor/animation/animation_bezier_editor.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* animation_bezier_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_bezier_editor.h"
32
33 #include "core/string/translation_server.h"
34 #include "editor/animation/animation_player_editor_plugin.h"
35 #include "editor/editor_node.h"
36 #include "editor/editor_string_names.h"
37 #include "editor/editor_undo_redo_manager.h"
38 #include "editor/gui/editor_spin_slider.h"
39 #include "editor/settings/editor_settings.h"
40 #include "editor/themes/editor_scale.h"
41 #include "scene/gui/option_button.h"
42 #include "scene/gui/view_panner.h"
43 #include "scene/resources/text_line.h"
44
45 #include <climits>
46
47 float AnimationBezierTrackEdit::_bezier_h_to_pixel(float p_h) {
48 float h = p_h;
49 h = (h - timeline_v_scroll) / timeline_v_zoom;
50 h = (get_size().height / 2.0) - h;
51 return h;
52 }
53
54 void AnimationBezierTrackEdit::_draw_track(int p_track, const Color &p_color) {
55 float scale = timeline->get_zoom_scale();
56
57 int limit = timeline->get_name_limit();
58 int right_limit = get_size().width;
59
60 // Selection may have altered the order of keys.
61 RBMap<real_t, int> key_order;
62
63 for (int i = 0; i < animation->track_get_key_count(p_track); i++) {
64 real_t ofs = animation->track_get_key_time(p_track, i);
65 if (selection.has(IntPair(p_track, i))) {
66 if (moving_selection) {
67 ofs += moving_selection_offset.x;
68 } else if (scaling_selection) {
69 ofs += -scaling_selection_offset.x + (ofs - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
70 }
71 }
72
73 key_order[ofs] = i;
74 }
75
76 for (RBMap<real_t, int>::Element *E = key_order.front(); E; E = E->next()) {
77 int i = E->get();
78
79 if (!E->next()) {
80 break;
81 }
82
83 int i_n = E->next()->get();
84
85 float offset = animation->track_get_key_time(p_track, i);
86 float height = animation->bezier_track_get_key_value(p_track, i);
87 Vector2 out_handle = animation->bezier_track_get_key_out_handle(p_track, i);
88 if (p_track == moving_handle_track && (moving_handle == -1 || moving_handle == 1) && moving_handle_key == i) {
89 out_handle = moving_handle_right;
90 }
91
92 if (selection.has(IntPair(p_track, i))) {
93 if (moving_selection) {
94 offset += moving_selection_offset.x;
95 height += moving_selection_offset.y;
96 } else if (scaling_selection) {
97 offset += -scaling_selection_offset.x + (offset - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
98 height += -scaling_selection_offset.y + (height - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
99 }
100 }
101
102 float offset_n = animation->track_get_key_time(p_track, i_n);
103 float height_n = animation->bezier_track_get_key_value(p_track, i_n);
104 Vector2 in_handle = animation->bezier_track_get_key_in_handle(p_track, i_n);
105 if (p_track == moving_handle_track && (moving_handle == -1 || moving_handle == 1) && moving_handle_key == i_n) {
106 in_handle = moving_handle_left;
107 }
108
109 if (selection.has(IntPair(p_track, i_n))) {
110 if (moving_selection) {
111 offset_n += moving_selection_offset.x;
112 height_n += moving_selection_offset.y;
113 } else if (scaling_selection) {
114 offset_n += -scaling_selection_offset.x + (offset_n - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
115 height_n += -scaling_selection_offset.y + (height_n - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
116 }
117 }
118
119 if (moving_inserted_key && moving_selection_from_track == p_track) {
120 if (moving_selection_from_key == i) {
121 Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(p_track, i);
122 if (handle_mode != Animation::HANDLE_MODE_FREE) {
123 float offset_p = offset;
124 float height_p = height;
125 if (E->prev()) {
126 int i_p = E->prev()->get();
127 offset_p = animation->track_get_key_time(p_track, i_p);
128 height_p = animation->bezier_track_get_key_value(p_track, i_p);
129 }
130
131 animation->bezier_track_calculate_handles(offset, offset_p, height_p, offset_n, height_n, handle_mode, Animation::HANDLE_SET_MODE_AUTO, nullptr, &out_handle);
132 }
133 } else if (moving_selection_from_key == i_n) {
134 Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(p_track, i_n);
135 if (handle_mode != Animation::HANDLE_MODE_FREE) {
136 float offset_nn = offset_n;
137 float height_nn = height_n;
138 if (E->next()->next()) {
139 int i_nn = E->next()->next()->get();
140 offset_nn = animation->track_get_key_time(p_track, i_nn);
141 height_nn = animation->bezier_track_get_key_value(p_track, i_nn);
142 }
143
144 animation->bezier_track_calculate_handles(offset_n, offset, height, offset_nn, height_nn, handle_mode, Animation::HANDLE_SET_MODE_AUTO, &in_handle, nullptr);
145 }
146 }
147 }
148
149 out_handle += Vector2(offset, height);
150 in_handle += Vector2(offset_n, height_n);
151
152 Vector2 start(offset, height);
153 Vector2 end(offset_n, height_n);
154
155 int from_x = (offset - timeline->get_value()) * scale + limit;
156 int point_start = from_x;
157 int to_x = (offset_n - timeline->get_value()) * scale + limit;
158 int point_end = to_x;
159
160 if (from_x > right_limit) { // Not visible.
161 continue;
162 }
163
164 if (to_x < limit) { // Not visible.
165 continue;
166 }
167
168 from_x = MAX(from_x, limit);
169 to_x = MIN(to_x, right_limit);
170
171 Vector<Vector2> lines;
172
173 Vector2 prev_pos;
174
175 for (int j = from_x; j <= to_x; j++) {
176 float t = (j - limit) / scale + timeline->get_value();
177
178 float h;
179
180 if (j == point_end) {
181 h = end.y; // Make sure it always connects.
182 } else if (j == point_start) {
183 h = start.y; // Make sure it always connects.
184 } else { // Custom interpolation, used because it needs to show paths affected by moving the selection or handles.
185 int iterations = 10;
186 float low = 0;
187 float high = 1;
188
189 // Narrow high and low as much as possible.
190 for (int k = 0; k < iterations; k++) {
191 float middle = (low + high) / 2.0;
192
193 Vector2 interp = start.bezier_interpolate(out_handle, in_handle, end, middle);
194
195 if (interp.x < t) {
196 low = middle;
197 } else {
198 high = middle;
199 }
200 }
201
202 // Interpolate the result.
203 Vector2 low_pos = start.bezier_interpolate(out_handle, in_handle, end, low);
204 Vector2 high_pos = start.bezier_interpolate(out_handle, in_handle, end, high);
205
206 float c = (t - low_pos.x) / (high_pos.x - low_pos.x);
207
208 h = low_pos.lerp(high_pos, c).y;
209 }
210
211 h = _bezier_h_to_pixel(h);
212
213 Vector2 pos(j, h);
214
215 if (j > from_x) {
216 lines.push_back(prev_pos);
217 lines.push_back(pos);
218 }
219 prev_pos = pos;
220 }
221
222 if (lines.size() >= 2) {
223 draw_multiline(lines, p_color, Math::round(EDSCALE), true);
224 }
225 }
226 }
227
228 void AnimationBezierTrackEdit::_draw_line_clipped(const Vector2 &p_from, const Vector2 &p_to, const Color &p_color, int p_clip_left, int p_clip_right) {
229 Vector2 from = p_from;
230 Vector2 to = p_to;
231
232 if (from.x == to.x && from.y == to.y) {
233 return;
234 }
235 if (to.x < from.x) {
236 SWAP(to, from);
237 }
238
239 if (to.x < p_clip_left) {
240 return;
241 }
242
243 if (from.x > p_clip_right) {
244 return;
245 }
246
247 if (to.x > p_clip_right) {
248 float c = (p_clip_right - from.x) / (to.x - from.x);
249 to = from.lerp(to, c);
250 }
251
252 if (from.x < p_clip_left) {
253 float c = (p_clip_left - from.x) / (to.x - from.x);
254 from = from.lerp(to, c);
255 }
256
257 draw_line(from, to, p_color, Math::round(EDSCALE), true);
258 }
259
260 void AnimationBezierTrackEdit::_notification(int p_what) {
261 switch (p_what) {
262 case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
263 if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/panning")) {
264 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")));
265 panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
266 }
267 } break;
268
269 case NOTIFICATION_READY: {
270 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")));
271 panner->setup_warped_panning(get_viewport(), EDITOR_GET("editors/panning/warped_mouse_panning"));
272 } break;
273 case NOTIFICATION_THEME_CHANGED: {
274 bezier_icon = get_editor_theme_icon(SNAME("KeyBezierPoint"));
275 bezier_handle_icon = get_editor_theme_icon(SNAME("KeyBezierHandle"));
276 selected_icon = get_editor_theme_icon(SNAME("KeyBezierSelected"));
277 } break;
278
279 case NOTIFICATION_ACCESSIBILITY_UPDATE: {
280 RID ae = get_accessibility_element();
281 ERR_FAIL_COND(ae.is_null());
282
283 //TODO
284 DisplayServer::get_singleton()->accessibility_update_set_role(ae, DisplayServer::AccessibilityRole::ROLE_STATIC_TEXT);
285 DisplayServer::get_singleton()->accessibility_update_set_value(ae, TTR(vformat("The %s is not accessible at this time.", "Animation bezier track editor")));
286 } break;
287
288 case NOTIFICATION_DRAW: {
289 if (animation.is_null()) {
290 return;
291 }
292
293 int limit = timeline->get_name_limit();
294
295 const Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
296 const int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
297 const Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
298
299 const Color h_line_color = get_theme_color(SNAME("h_line_color"), SNAME("AnimationBezierTrackEdit"));
300 const Color v_line_color = get_theme_color(SNAME("v_line_color"), SNAME("AnimationBezierTrackEdit"));
301 const Color focus_color = get_theme_color(SNAME("focus_color"), SNAME("AnimationBezierTrackEdit"));
302 const Color track_focus_color = get_theme_color(SNAME("track_focus_color"), SNAME("AnimationBezierTrackEdit"));
303
304 const int h_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationBezierTrackEdit"));
305 const int v_separation = get_theme_constant(SNAME("h_separation"), SNAME("AnimationBezierTrackEdit"));
306
307 const String &lang = _get_locale();
308
309 if (has_focus(true)) {
310 draw_rect(Rect2(Point2(1, 1), get_size() - Point2(1, 1)), focus_color, false, Math::round(EDSCALE));
311 }
312
313 int right_limit = get_size().width;
314
315 // Unavailable timeline.
316 {
317 int px = (editor->get_current_animation()->get_length() - timeline->get_value()) * timeline->get_zoom_scale() + timeline->get_name_limit();
318 px = MAX(px, timeline->get_name_limit());
319 Rect2 rect = Rect2(px, 0, right_limit - px, get_size().height);
320 if (rect.size.width > 0) {
321 draw_rect(rect, Color(0, 0, 0, 0.2));
322 }
323 }
324
325 draw_line(Point2(limit, 0), Point2(limit, get_size().height), v_line_color, Math::round(EDSCALE));
326
327 track_v_scroll_max = v_separation;
328
329 int vofs = v_separation + track_v_scroll;
330 int margin = 0;
331
332 RBMap<int, Color> subtrack_colors;
333 Color selected_track_color;
334 subtracks.clear();
335 node_icons.clear();
336 subtrack_icons.clear();
337
338 // Marker sections.
339 {
340 float scale = timeline->get_zoom_scale();
341 int limit_end = get_size().width - timeline->get_buttons_width();
342
343 PackedStringArray section = editor->get_selected_section();
344 if (section.size() == 2) {
345 StringName start_marker = section[0];
346 StringName end_marker = section[1];
347 double start_time = animation->get_marker_time(start_marker);
348 double end_time = animation->get_marker_time(end_marker);
349
350 // When AnimationPlayer is playing, don't move the preview rect, so it still indicates the playback section.
351 AnimationPlayer *player = AnimationPlayerEditor::get_singleton()->get_player();
352 if (editor->is_marker_moving_selection() && !(player && player->is_playing())) {
353 start_time += editor->get_marker_moving_selection_offset();
354 end_time += editor->get_marker_moving_selection_offset();
355 }
356
357 if (start_time < animation->get_length() && end_time >= 0) {
358 float start_ofs = MAX(0, start_time) - timeline->get_value();
359 float end_ofs = MIN(animation->get_length(), end_time) - timeline->get_value();
360 start_ofs = start_ofs * scale + limit;
361 end_ofs = end_ofs * scale + limit;
362 start_ofs = MAX(start_ofs, limit);
363 end_ofs = MIN(end_ofs, limit_end);
364 Rect2 rect;
365 rect.set_position(Vector2(start_ofs, 0));
366 rect.set_size(Vector2(end_ofs - start_ofs, get_size().height));
367
368 draw_rect(rect, Color(1, 0.1, 0.1, 0.2));
369 }
370 }
371 }
372
373 // Marker overlays.
374 {
375 float scale = timeline->get_zoom_scale();
376 PackedStringArray markers = animation->get_marker_names();
377 for (const StringName marker : markers) {
378 double time = animation->get_marker_time(marker);
379 if (editor->is_marker_selected(marker) && editor->is_marker_moving_selection()) {
380 time += editor->get_marker_moving_selection_offset();
381 }
382 if (time >= 0) {
383 float offset = time - timeline->get_value();
384 offset = offset * scale + limit;
385 Color marker_color = animation->get_marker_color(marker);
386 marker_color.a = 0.2;
387 draw_line(Point2(offset, 0), Point2(offset, get_size().height), marker_color, Math::round(EDSCALE));
388 }
389 }
390 }
391
392 RBMap<String, Vector<int>> track_indices;
393 int track_count = animation->get_track_count();
394 for (int i = 0; i < track_count; ++i) {
395 if (!_is_track_displayed(i)) {
396 continue;
397 }
398
399 String base_path = String(animation->track_get_path(i));
400 int end = base_path.find_char(':');
401 if (end != -1) {
402 base_path = base_path.substr(0, end + 1);
403 }
404 Vector<int> indices = track_indices.has(base_path) ? track_indices[base_path] : Vector<int>();
405 indices.push_back(i);
406 track_indices[base_path] = indices;
407 }
408
409 const Color dc = get_theme_color(SNAME("font_disabled_color"), EditorStringName(Editor));
410
411 Ref<Texture2D> remove = get_editor_theme_icon(SNAME("Remove"));
412 Ref<Texture2D> visibility_visible = get_editor_theme_icon(SNAME("GuiVisibilityVisible"));
413 Ref<Texture2D> visibility_hidden = get_editor_theme_icon(SNAME("GuiVisibilityHidden"));
414 Ref<Texture2D> lock = get_editor_theme_icon(SNAME("Lock"));
415 Ref<Texture2D> unlock = get_editor_theme_icon(SNAME("Unlock"));
416 Ref<Texture2D> solo = get_editor_theme_icon(SNAME("AudioBusSolo"));
417
418 for (const KeyValue<String, Vector<int>> &E : track_indices) {
419 String base_path = E.key;
420
421 Vector<int> tracks = E.value;
422
423 // Names and icon.
424 {
425 NodePath path = animation->track_get_path(tracks[0]);
426
427 Node *node = nullptr;
428
429 if (root && root->has_node(path)) {
430 node = root->get_node(path);
431 }
432
433 if (node) {
434 int ofs = h_separation;
435
436 Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(node);
437
438 TextLine text_buf = TextLine(node->get_name(), font, font_size);
439 int total_icon_width = icon->get_width() + solo->get_width() + visibility_visible->get_width() + lock->get_width() + remove->get_width();
440 int total_icon_separation = h_separation * 5;
441 text_buf.set_width(limit - ofs - total_icon_width - total_icon_separation);
442
443 int h = MAX(text_buf.get_size().y, icon->get_height());
444
445 draw_texture(icon, Point2(ofs, vofs + int(h - icon->get_height()) / 2.0));
446 ofs += icon->get_width() + h_separation;
447
448 margin = icon->get_width();
449
450 Vector2 string_pos = Point2(ofs, vofs);
451 string_pos = string_pos.floor();
452 text_buf.draw(get_canvas_item(), string_pos, color);
453
454 Rect2 remove_rect(limit - h_separation - remove->get_width(), vofs, remove->get_width(), remove->get_height());
455 if (read_only) {
456 draw_texture(remove, remove_rect.position, dc);
457 } else {
458 draw_texture(remove, remove_rect.position);
459 }
460
461 bool all_tracks_locked = true;
462 for (int track : tracks) {
463 if (!locked_tracks.has(track)) {
464 all_tracks_locked = false;
465 break;
466 }
467 }
468
469 Rect2 lock_rect(remove_rect.position.x - h_separation - lock->get_width(), vofs, lock->get_width(), lock->get_height());
470
471 if (all_tracks_locked) {
472 draw_texture(lock, lock_rect.position);
473 } else {
474 draw_texture(unlock, lock_rect.position);
475 }
476
477 bool all_tracks_hidden = true;
478 for (int track : tracks) {
479 if (!hidden_tracks.has(track)) {
480 all_tracks_hidden = false;
481 break;
482 }
483 }
484
485 Rect2 visibility_rect(lock_rect.position.x - h_separation - visibility_visible->get_width(), vofs, visibility_visible->get_width(), visibility_visible->get_height());
486
487 if (all_tracks_hidden) {
488 draw_texture(visibility_hidden, visibility_rect.position);
489 } else {
490 draw_texture(visibility_visible, visibility_rect.position);
491 }
492
493 Rect2 solo_rect(visibility_rect.position.x - h_separation - solo->get_width(), vofs, solo->get_width(), solo->get_height());
494 draw_texture(solo, solo_rect.position);
495
496 RBMap<int, Rect2> icon_rects;
497 icon_rects[REMOVE_ICON] = remove_rect;
498 icon_rects[LOCK_ICON] = lock_rect;
499 icon_rects[VISIBILITY_ICON] = visibility_rect;
500 icon_rects[SOLO_ICON] = solo_rect;
501
502 node_icons[base_path.trim_suffix(":")] = icon_rects;
503
504 vofs += h + v_separation;
505 track_v_scroll_max += h + v_separation;
506 }
507 }
508
509 float remove_hpos = limit - h_separation - remove->get_width();
510 float lock_hpos = remove_hpos - h_separation - lock->get_width();
511 float visibility_hpos = lock_hpos - h_separation - visibility_visible->get_width();
512 float solo_hpos = visibility_hpos - h_separation - solo->get_width();
513
514 float buttons_width = remove->get_width() + lock->get_width() + visibility_visible->get_width() + solo->get_width() + h_separation * 3;
515
516 for (int i = 0; i < tracks.size(); ++i) {
517 // Related track titles.
518
519 int current_track = tracks[i];
520
521 String path = String(animation->track_get_path(current_track));
522 path = path.replace_first(base_path, "");
523
524 Color cc = color;
525 TextLine text_buf = TextLine(path, font, font_size);
526 text_buf.set_width(limit - margin - buttons_width - h_separation * 2);
527
528 Rect2 rect = Rect2(margin, vofs, solo_hpos - h_separation - solo->get_width(), text_buf.get_size().y + v_separation);
529
530 cc.a *= 0.7;
531 float h;
532 if (path.ends_with(":x")) {
533 h = 0;
534 } else if (path.ends_with(":y")) {
535 h = 0.33f;
536 } else if (path.ends_with(":z")) {
537 h = 0.66f;
538 } else {
539 uint32_t hash = path.hash();
540 hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
541 hash = ((hash >> 16) ^ hash) * 0x45d9f3b;
542 hash = (hash >> 16) ^ hash;
543 h = (hash % 65535) / 65536.0;
544 }
545
546 if (current_track != selected_track) {
547 Color track_color;
548 if (locked_tracks.has(current_track)) {
549 track_color.set_hsv(h, 0, 0.4);
550 } else {
551 track_color.set_hsv(h, 0.2, 0.8);
552 }
553 track_color.a = 0.5;
554 draw_rect(Rect2(0, vofs, margin - h_separation, text_buf.get_size().y * 0.8), track_color);
555 subtrack_colors[current_track] = track_color;
556
557 subtracks[current_track] = rect;
558 } else {
559 draw_rect(rect, track_focus_color);
560 if (locked_tracks.has(selected_track)) {
561 selected_track_color.set_hsv(h, 0.0, 0.4);
562 } else {
563 selected_track_color.set_hsv(h, 0.8, 0.8);
564 }
565 }
566
567 Vector2 string_pos = Point2(margin + h_separation, vofs);
568 text_buf.draw(get_canvas_item(), string_pos, cc);
569
570 Rect2 remove_rect = Rect2(remove_hpos, vofs, remove->get_width(), remove->get_height());
571 static const Color texture_modulate = Color(1, 1, 1, .75);
572
573 if (read_only) {
574 draw_texture(remove, remove_rect.position, dc);
575 } else {
576 draw_texture(remove, remove_rect.position, texture_modulate);
577 }
578
579 Rect2 lock_rect = Rect2(lock_hpos, vofs, lock->get_width(), lock->get_height());
580 if (locked_tracks.has(current_track)) {
581 draw_texture(lock, lock_rect.position, texture_modulate);
582 } else {
583 draw_texture(unlock, lock_rect.position, texture_modulate);
584 }
585
586 Rect2 visible_rect = Rect2(visibility_hpos, vofs, visibility_visible->get_width(), visibility_visible->get_height());
587 if (hidden_tracks.has(current_track)) {
588 draw_texture(visibility_hidden, visible_rect.position, texture_modulate);
589 } else {
590 draw_texture(visibility_visible, visible_rect.position, texture_modulate);
591 }
592
593 Rect2 solo_rect = Rect2(solo_hpos, vofs, solo->get_width(), solo->get_height());
594 draw_texture(solo, solo_rect.position, texture_modulate);
595
596 RBMap<int, Rect2> track_icons;
597 track_icons[REMOVE_ICON] = remove_rect;
598 track_icons[LOCK_ICON] = lock_rect;
599 track_icons[VISIBILITY_ICON] = visible_rect;
600 track_icons[SOLO_ICON] = solo_rect;
601
602 subtrack_icons[current_track] = track_icons;
603
604 vofs += text_buf.get_size().y + v_separation;
605 track_v_scroll_max += text_buf.get_size().y + v_separation;
606 }
607 }
608
609 const Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
610
611 // Guides.
612 {
613 float min_left_scale = font->get_height(font_size) + v_separation;
614
615 float scale = (min_left_scale * 2) * timeline_v_zoom;
616 float step = Math::pow(10.0, Math::round(Math::log(scale / 5.0) / Math::log(10.0))) * 5.0;
617 scale = Math::snapped(scale, step);
618
619 while (scale / timeline_v_zoom < min_left_scale * 2) {
620 scale += step;
621 }
622
623 bool first = true;
624 int prev_iv = 0;
625 for (int i = font->get_height(font_size); i < get_size().height; i++) {
626 float ofs = get_size().height / 2.0 - i;
627 ofs *= timeline_v_zoom;
628 ofs += timeline_v_scroll;
629
630 int iv = int(ofs / scale);
631 if (ofs < 0) {
632 iv -= 1;
633 }
634 if (!first && iv != prev_iv) {
635 Color lc = h_line_color;
636 lc.a *= 0.5;
637 draw_line(Point2(limit, i), Point2(right_limit, i), lc, Math::round(EDSCALE));
638 Color c = color;
639 c.a *= 0.5;
640
641 const String &formatted = TranslationServer::get_singleton()->format_number(rtos(Math::snapped((iv + 1) * scale, step)), lang);
642 draw_string(font, Point2(limit + 8, i - 2), formatted, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, c);
643 }
644
645 first = false;
646 prev_iv = iv;
647 }
648 }
649
650 // Draw other curves.
651 {
652 float scale = timeline->get_zoom_scale();
653 Ref<Texture2D> point = get_editor_theme_icon(SNAME("KeyValue"));
654 for (const KeyValue<int, Color> &E : subtrack_colors) {
655 if (hidden_tracks.has(E.key)) {
656 continue;
657 }
658 _draw_track(E.key, E.value);
659
660 for (int i = 0; i < animation->track_get_key_count(E.key); i++) {
661 float offset = animation->track_get_key_time(E.key, i);
662 float value = animation->bezier_track_get_key_value(E.key, i);
663
664 Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value));
665
666 if (pos.x >= limit && pos.x <= right_limit) {
667 draw_texture(point, pos - point->get_size() / 2.0, E.value);
668 }
669 }
670 }
671
672 if (selected_track >= 0 && track_count > 0 && !hidden_tracks.has(selected_track)) {
673 // Draw edited curve.
674 _draw_track(selected_track, selected_track_color);
675 }
676 }
677
678 const bool draw_selection_handles = selection.size() > 1;
679 LocalVector<Point2> selected_pos;
680
681 // Draw editor handles.
682 {
683 edit_points.clear();
684 float scale = timeline->get_zoom_scale();
685
686 for (int i = 0; i < track_count; ++i) {
687 bool draw_track = _is_track_curves_displayed(i) && !locked_tracks.has(i);
688 if (!draw_selection_handles && !draw_track) {
689 continue;
690 }
691
692 int key_count = animation->track_get_key_count(i);
693 for (int j = 0; j < key_count; ++j) {
694 float offset = animation->track_get_key_time(i, j);
695 float value = animation->bezier_track_get_key_value(i, j);
696 bool is_selected = selection.has(IntPair(i, j));
697
698 if (is_selected) {
699 if (moving_selection) {
700 offset += moving_selection_offset.x;
701 value += moving_selection_offset.y;
702 } else if (scaling_selection) {
703 offset += -scaling_selection_offset.x + (offset - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
704 value += -scaling_selection_offset.y + (value - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
705 }
706 }
707
708 Vector2 pos((offset - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value));
709
710 if (draw_selection_handles && is_selected) {
711 selected_pos.push_back(pos);
712 }
713
714 if (!draw_track) {
715 continue;
716 }
717
718 Vector2 in_vec = animation->bezier_track_get_key_in_handle(i, j);
719 Vector2 out_vec = animation->bezier_track_get_key_out_handle(i, j);
720
721 if ((moving_handle == 1 || moving_handle == -1) && moving_handle_track == i && moving_handle_key == j) {
722 in_vec = moving_handle_left;
723 }
724
725 if ((moving_handle == 1 || moving_handle == -1) && moving_handle_track == i && moving_handle_key == j) {
726 out_vec = moving_handle_right;
727 }
728
729 if (moving_inserted_key && moving_selection_from_key == j) {
730 Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(i, j);
731 if (handle_mode != Animation::HANDLE_MODE_FREE) {
732 int key_prev = 0;
733 int key_next = moving_selection_from_key;
734 for (int k = 0; k < key_count; k++) {
735 if (k == moving_selection_from_key) {
736 continue;
737 }
738
739 if (animation->track_get_key_time(i, k) < offset) {
740 key_prev = k;
741 } else {
742 key_next = k;
743 break;
744 }
745 }
746
747 float prev_time = offset;
748 float prev_value = value;
749 if (key_prev != moving_selection_from_key) {
750 prev_time = animation->track_get_key_time(i, key_prev);
751 prev_value = animation->bezier_track_get_key_value(i, key_prev);
752 }
753
754 float next_time = offset;
755 float next_value = value;
756 if (key_next != moving_selection_from_key) {
757 next_time = animation->track_get_key_time(i, key_next);
758 next_value = animation->bezier_track_get_key_value(i, key_next);
759 }
760
761 animation->bezier_track_calculate_handles(offset, prev_time, prev_value, next_time, next_value, handle_mode, Animation::HANDLE_SET_MODE_AUTO, &in_vec, &out_vec);
762 }
763 }
764
765 Vector2 pos_in(((offset + in_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + in_vec.y));
766 Vector2 pos_out(((offset + out_vec.x) - timeline->get_value()) * scale + limit, _bezier_h_to_pixel(value + out_vec.y));
767
768 if (i == selected_track || is_selected) {
769 _draw_line_clipped(pos, pos_in, accent, limit, right_limit);
770 _draw_line_clipped(pos, pos_out, accent, limit, right_limit);
771 }
772
773 EditPoint ep;
774 ep.track = i;
775 ep.key = j;
776 if (pos.x >= limit && pos.x <= right_limit) {
777 ep.point_rect.position = (pos - bezier_icon->get_size() / 2.0).floor();
778 ep.point_rect.size = bezier_icon->get_size();
779 if (is_selected) {
780 draw_texture(selected_icon, ep.point_rect.position);
781
782 const String &formatted_offset = TranslationServer::get_singleton()->format_number(rtos(Math::snapped(offset, 0.0001)), lang);
783 draw_string(font, ep.point_rect.position + Vector2(8, -font->get_height(font_size) - 8), TTR("Time:") + " " + formatted_offset, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent);
784
785 const String &formatted_value = TranslationServer::get_singleton()->format_number(rtos(Math::snapped(value, 0.001)), lang);
786 draw_string(font, ep.point_rect.position + Vector2(8, -8), TTR("Value:") + " " + formatted_value, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, accent);
787 } else {
788 Color track_color = Color(1, 1, 1, 1);
789 if (i != selected_track) {
790 track_color = subtrack_colors[i];
791 }
792 draw_texture(bezier_icon, ep.point_rect.position, track_color);
793 }
794 ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5);
795 }
796 ep.point_rect = ep.point_rect.grow(ep.point_rect.size.width * 0.5);
797
798 if (i == selected_track || is_selected) {
799 if (animation->bezier_track_get_key_handle_mode(i, j) != Animation::HANDLE_MODE_LINEAR) {
800 if (pos_in.x >= limit && pos_in.x <= right_limit) {
801 ep.in_rect.position = (pos_in - bezier_handle_icon->get_size() / 2.0).floor();
802 ep.in_rect.size = bezier_handle_icon->get_size();
803 draw_texture(bezier_handle_icon, ep.in_rect.position);
804 ep.in_rect = ep.in_rect.grow(ep.in_rect.size.width * 0.5);
805 }
806 if (pos_out.x >= limit && pos_out.x <= right_limit) {
807 ep.out_rect.position = (pos_out - bezier_handle_icon->get_size() / 2.0).floor();
808 ep.out_rect.size = bezier_handle_icon->get_size();
809 draw_texture(bezier_handle_icon, ep.out_rect.position);
810 ep.out_rect = ep.out_rect.grow(ep.out_rect.size.width * 0.5);
811 }
812 }
813 }
814 if (!locked_tracks.has(i)) {
815 edit_points.push_back(ep);
816 }
817 }
818 }
819
820 for (int i = 0; i < edit_points.size(); ++i) {
821 if (edit_points[i].track == selected_track) {
822 EditPoint ep = edit_points[i];
823 edit_points.remove_at(i);
824 edit_points.insert(0, ep);
825 }
826 }
827 }
828
829 selection_rect = Rect2();
830 selection_handles_rect = Rect2();
831 // Draw scale handles.
832 if (draw_selection_handles) {
833 selection_rect.position = selected_pos[0];
834 selected_pos.remove_at(0);
835 for (const Point2 &pos : selected_pos) {
836 selection_rect = selection_rect.expand(pos);
837 }
838
839 const int outer_ofs = Math::round(12 * EDSCALE);
840 const int inner_ofs = Math::round(outer_ofs / 2.0);
841
842 // Draw horizontal handles.
843 if (selection_rect.size.height > CMP_EPSILON) {
844 _draw_line_clipped(selection_rect.position - Vector2(inner_ofs, inner_ofs), selection_rect.position + Vector2(selection_rect.size.width + inner_ofs, -inner_ofs), accent, limit, right_limit);
845 _draw_line_clipped(selection_rect.position + Vector2(-inner_ofs, selection_rect.size.height + inner_ofs), selection_rect.position + selection_rect.size + Vector2(inner_ofs, inner_ofs), accent, limit, right_limit);
846 }
847 // Draw vertical handles.
848 if (selection_rect.size.width > CMP_EPSILON) {
849 _draw_line_clipped(selection_rect.position - Vector2(inner_ofs, inner_ofs), selection_rect.position + Vector2(-inner_ofs, selection_rect.size.height + inner_ofs), accent, limit, right_limit);
850 _draw_line_clipped(selection_rect.position + Vector2(selection_rect.size.width + inner_ofs, -inner_ofs), selection_rect.position + selection_rect.size + Vector2(inner_ofs, inner_ofs), accent, limit, right_limit);
851 }
852
853 selection_handles_rect.position = selection_rect.position - Vector2(outer_ofs, outer_ofs);
854 selection_handles_rect.size = selection_rect.size + Vector2(outer_ofs, outer_ofs) * 2;
855 }
856
857 if (box_selecting) {
858 Vector2 bs_from = box_selection_from;
859 Vector2 bs_to = box_selection_to;
860 if (bs_from.x > bs_to.x) {
861 SWAP(bs_from.x, bs_to.x);
862 }
863 if (bs_from.y > bs_to.y) {
864 SWAP(bs_from.y, bs_to.y);
865 }
866 draw_rect(
867 Rect2(bs_from, bs_to - bs_from),
868 get_theme_color(SNAME("box_selection_fill_color"), EditorStringName(Editor)));
869 draw_rect(
870 Rect2(bs_from, bs_to - bs_from),
871 get_theme_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor)),
872 false,
873 Math::round(EDSCALE));
874 }
875 } break;
876 }
877 }
878
879 // Check if a track is displayed in the bezier editor (track type = bezier and track not filtered).
880 bool AnimationBezierTrackEdit::_is_track_displayed(int p_track_index) {
881 if (animation->track_get_type(p_track_index) != Animation::TrackType::TYPE_BEZIER) {
882 return false;
883 }
884
885 if (is_filtered) {
886 String path = String(animation->track_get_path(p_track_index));
887 if (root && root->has_node(path)) {
888 Node *node = root->get_node(path);
889 if (!node) {
890 return false; // No node, no filter.
891 }
892 if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
893 return false; // Skip track due to not selected.
894 }
895 }
896 }
897
898 return true;
899 }
900
901 // Check if the curves for a track are displayed in the editor (not hidden). Includes the check on the track visibility.
902 bool AnimationBezierTrackEdit::_is_track_curves_displayed(int p_track_index) {
903 // Is the track is visible in the editor?
904 if (!_is_track_displayed(p_track_index)) {
905 return false;
906 }
907
908 // And curves visible?
909 if (hidden_tracks.has(p_track_index)) {
910 return false;
911 }
912
913 return true;
914 }
915
916 Ref<Animation> AnimationBezierTrackEdit::get_animation() const {
917 return animation;
918 }
919
920 void AnimationBezierTrackEdit::set_animation_and_track(const Ref<Animation> &p_animation, int p_track, bool p_read_only) {
921 if (p_animation != animation) {
922 selection.clear();
923 }
924 animation = p_animation;
925 read_only = p_read_only;
926 selected_track = p_track;
927 queue_redraw();
928 }
929
930 Size2 AnimationBezierTrackEdit::get_minimum_size() const {
931 return Vector2(1, 1);
932 }
933
934 Control::CursorShape AnimationBezierTrackEdit::get_cursor_shape(const Point2 &p_pos) const {
935 // Box selecting or moving a handle
936 if (box_selecting || Math::abs(moving_handle) == 1) {
937 return get_default_cursor_shape();
938 }
939 // Hovering a handle
940 if (!read_only) {
941 for (const EditPoint &edit_point : edit_points) {
942 if (edit_point.in_rect.has_point(p_pos) || edit_point.out_rect.has_point(p_pos)) {
943 return get_default_cursor_shape();
944 }
945 }
946 }
947 // Currently box scaling
948 if (scaling_selection) {
949 if (scaling_selection_handles == Vector2i(1, 1) || scaling_selection_handles == Vector2i(-1, -1)) {
950 return CURSOR_FDIAGSIZE;
951 } else if (scaling_selection_handles == Vector2i(1, -1) || scaling_selection_handles == Vector2i(-1, 1)) {
952 return CURSOR_BDIAGSIZE;
953 } else if (abs(scaling_selection_handles.x) == 1) {
954 return CURSOR_HSIZE;
955 } else if (abs(scaling_selection_handles.y) == 1) {
956 return CURSOR_VSIZE;
957 }
958 }
959 // Hovering the scaling box
960 const Vector2i rel_pos = p_pos - selection_rect.position;
961 if (selection_handles_rect.has_point(p_pos)) {
962 if ((rel_pos.x < 0 && rel_pos.y < 0) || (rel_pos.x > selection_rect.size.width && rel_pos.y > selection_rect.size.height)) {
963 return CURSOR_FDIAGSIZE;
964 } else if ((rel_pos.x < 0 && rel_pos.y > selection_rect.size.height) || (rel_pos.x > selection_rect.size.width && rel_pos.y < 0)) {
965 return CURSOR_BDIAGSIZE;
966 } else if (rel_pos.x < 0 || rel_pos.x > selection_rect.size.width) {
967 return CURSOR_HSIZE;
968 } else if (rel_pos.y < 0 || rel_pos.y > selection_rect.size.height) {
969 return CURSOR_VSIZE;
970 }
971 return CURSOR_MOVE;
972 }
973 return get_default_cursor_shape();
974 }
975
976 void AnimationBezierTrackEdit::set_timeline(AnimationTimelineEdit *p_timeline) {
977 timeline = p_timeline;
978 timeline->connect("zoom_changed", callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed));
979 timeline->connect("name_limit_changed", callable_mp(this, &AnimationBezierTrackEdit::_zoom_changed));
980 }
981
982 void AnimationBezierTrackEdit::set_editor(AnimationTrackEditor *p_editor) {
983 editor = p_editor;
984 connect("clear_selection", callable_mp(editor, &AnimationTrackEditor::_clear_selection).bind(false));
985 connect("select_key", callable_mp(editor, &AnimationTrackEditor::_key_selected), CONNECT_DEFERRED);
986 connect("deselect_key", callable_mp(editor, &AnimationTrackEditor::_key_deselected), CONNECT_DEFERRED);
987 }
988
989 void AnimationBezierTrackEdit::_play_position_draw() {
990 if (animation.is_null() || play_position_pos < 0) {
991 return;
992 }
993
994 float scale = timeline->get_zoom_scale();
995 int h = get_size().height;
996
997 int limit = timeline->get_name_limit();
998
999 int px = (-timeline->get_value() + play_position_pos) * scale + limit;
1000
1001 if (px >= limit && px < (get_size().width)) {
1002 const Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
1003 play_position->draw_line(Point2(px, 0), Point2(px, h), color, Math::round(2 * EDSCALE));
1004 }
1005 }
1006
1007 void AnimationBezierTrackEdit::set_play_position(real_t p_pos) {
1008 play_position_pos = p_pos;
1009 play_position->queue_redraw();
1010 }
1011
1012 void AnimationBezierTrackEdit::update_play_position() {
1013 play_position->queue_redraw();
1014 }
1015
1016 void AnimationBezierTrackEdit::set_root(Node *p_root) {
1017 root = p_root;
1018 }
1019
1020 void AnimationBezierTrackEdit::set_filtered(bool p_filtered) {
1021 is_filtered = p_filtered;
1022 if (animation.is_null()) {
1023 return;
1024 }
1025 String base_path = String(animation->track_get_path(selected_track));
1026 if (is_filtered) {
1027 if (root && root->has_node(base_path)) {
1028 Node *node = root->get_node(base_path);
1029 if (!node || !EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
1030 for (int i = 0; i < animation->get_track_count(); ++i) {
1031 if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER) {
1032 continue;
1033 }
1034
1035 base_path = String(animation->track_get_path(i));
1036 if (root && root->has_node(base_path)) {
1037 node = root->get_node(base_path);
1038 if (!node) {
1039 continue; // No node, no filter.
1040 }
1041 if (!EditorNode::get_singleton()->get_editor_selection()->is_selected(node)) {
1042 continue; // Skip track due to not selected.
1043 }
1044
1045 set_animation_and_track(animation, i, read_only);
1046 break;
1047 }
1048 }
1049 }
1050 }
1051 }
1052 queue_redraw();
1053 }
1054
1055 void AnimationBezierTrackEdit::auto_fit_vertically() {
1056 int track_count = animation->get_track_count();
1057 real_t minimum_value = Math::INF;
1058 real_t maximum_value = -Math::INF;
1059
1060 int nb_track_visible = 0;
1061 for (int i = 0; i < track_count; ++i) {
1062 if (!_is_track_curves_displayed(i) || locked_tracks.has(i)) {
1063 continue;
1064 }
1065
1066 int key_count = animation->track_get_key_count(i);
1067
1068 for (int j = 0; j < key_count; ++j) {
1069 real_t value = animation->bezier_track_get_key_value(i, j);
1070
1071 minimum_value = MIN(value, minimum_value);
1072 maximum_value = MAX(value, maximum_value);
1073
1074 // We also want to includes the handles...
1075 Vector2 in_vec = animation->bezier_track_get_key_in_handle(i, j);
1076 Vector2 out_vec = animation->bezier_track_get_key_out_handle(i, j);
1077
1078 minimum_value = MIN(value + in_vec.y, minimum_value);
1079 maximum_value = MAX(value + in_vec.y, maximum_value);
1080 minimum_value = MIN(value + out_vec.y, minimum_value);
1081 maximum_value = MAX(value + out_vec.y, maximum_value);
1082 }
1083
1084 nb_track_visible++;
1085 }
1086
1087 if (nb_track_visible == 0) {
1088 // No visible track... we will not adjust the vertical zoom
1089 return;
1090 }
1091
1092 if (Math::is_finite(minimum_value) && Math::is_finite(maximum_value)) {
1093 _zoom_vertically(minimum_value, maximum_value);
1094 queue_redraw();
1095 }
1096 }
1097
1098 void AnimationBezierTrackEdit::_zoom_vertically(real_t p_minimum_value, real_t p_maximum_value) {
1099 real_t target_height = p_maximum_value - p_minimum_value;
1100 if (target_height <= CMP_EPSILON) {
1101 timeline_v_scroll = p_maximum_value;
1102 return;
1103 }
1104
1105 timeline_v_scroll = (p_maximum_value + p_minimum_value) / 2.0;
1106 timeline_v_zoom = target_height / ((get_size().height - timeline->get_size().height) * 0.9);
1107 }
1108
1109 void AnimationBezierTrackEdit::_zoom_changed() {
1110 queue_redraw();
1111 play_position->queue_redraw();
1112 }
1113
1114 void AnimationBezierTrackEdit::_update_locked_tracks_after(int p_track) {
1115 _unlock_track(p_track);
1116
1117 Vector<int> updated_locked_tracks;
1118 for (const int &E : locked_tracks) {
1119 updated_locked_tracks.push_back(E);
1120 }
1121 locked_tracks.clear();
1122 for (int i = 0; i < updated_locked_tracks.size(); ++i) {
1123 if (updated_locked_tracks[i] > p_track) {
1124 locked_tracks.insert(updated_locked_tracks[i] - 1);
1125 } else {
1126 locked_tracks.insert(updated_locked_tracks[i]);
1127 }
1128 }
1129 }
1130
1131 void AnimationBezierTrackEdit::_update_hidden_tracks_after(int p_track) {
1132 _show_track(p_track);
1133
1134 Vector<int> updated_hidden_tracks;
1135 for (const int &E : hidden_tracks) {
1136 updated_hidden_tracks.push_back(E);
1137 }
1138 hidden_tracks.clear();
1139 for (int i = 0; i < updated_hidden_tracks.size(); ++i) {
1140 if (updated_hidden_tracks[i] > p_track) {
1141 hidden_tracks.insert(updated_hidden_tracks[i] - 1);
1142 } else {
1143 hidden_tracks.insert(updated_hidden_tracks[i]);
1144 }
1145 }
1146 }
1147
1148 bool AnimationBezierTrackEdit::_lock_track(int p_track) {
1149 locked_tracks.insert(p_track);
1150 if (selected_track == p_track) {
1151 for (int i = 0; i < animation->get_track_count(); ++i) {
1152 if (!locked_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
1153 set_animation_and_track(animation, i, read_only);
1154 return true;
1155 }
1156 }
1157 }
1158
1159 return false;
1160 }
1161
1162 bool AnimationBezierTrackEdit::_unlock_track(int p_track) {
1163 return locked_tracks.erase(p_track);
1164 }
1165
1166 bool AnimationBezierTrackEdit::_hide_track(int p_track) {
1167 hidden_tracks.insert(p_track);
1168 if (selected_track == p_track) {
1169 for (int i = 0; i < animation->get_track_count(); ++i) {
1170 if (!hidden_tracks.has(i) && animation->track_get_type(i) == Animation::TrackType::TYPE_BEZIER) {
1171 set_animation_and_track(animation, i, read_only);
1172 return true;
1173 }
1174 }
1175 }
1176
1177 return false;
1178 }
1179
1180 bool AnimationBezierTrackEdit::_show_track(int p_track) {
1181 return hidden_tracks.erase(p_track);
1182 }
1183
1184 String AnimationBezierTrackEdit::get_tooltip(const Point2 &p_pos) const {
1185 return Control::get_tooltip(p_pos);
1186 }
1187
1188 void AnimationBezierTrackEdit::_clear_selection() {
1189 selection.clear();
1190 emit_signal(SNAME("clear_selection"));
1191 queue_redraw();
1192 }
1193
1194 void AnimationBezierTrackEdit::_change_selected_keys_handle_mode(Animation::HandleMode p_mode, bool p_auto) {
1195 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1196 undo_redo->create_action(TTR("Update Selected Key Handles"), UndoRedo::MERGE_DISABLE, animation.ptr());
1197 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1198 const IntPair track_key_pair = E->get();
1199 undo_redo->add_undo_method(editor, "_bezier_track_set_key_handle_mode", animation.ptr(), track_key_pair.first, track_key_pair.second, animation->bezier_track_get_key_handle_mode(track_key_pair.first, track_key_pair.second), Animation::HANDLE_SET_MODE_NONE);
1200 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", track_key_pair.first, track_key_pair.second, animation->bezier_track_get_key_in_handle(track_key_pair.first, track_key_pair.second));
1201 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", track_key_pair.first, track_key_pair.second, animation->bezier_track_get_key_out_handle(track_key_pair.first, track_key_pair.second));
1202 undo_redo->add_do_method(editor, "_bezier_track_set_key_handle_mode", animation.ptr(), track_key_pair.first, track_key_pair.second, p_mode, p_auto ? Animation::HANDLE_SET_MODE_AUTO : Animation::HANDLE_SET_MODE_RESET);
1203 }
1204 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
1205 if (ape) {
1206 undo_redo->add_do_method(ape, "_animation_update_key_frame");
1207 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
1208 }
1209 undo_redo->commit_action();
1210 }
1211
1212 void AnimationBezierTrackEdit::_clear_selection_for_anim(const Ref<Animation> &p_anim) {
1213 if (!(animation == p_anim) || !is_visible()) {
1214 return;
1215 }
1216 _clear_selection();
1217 }
1218
1219 void AnimationBezierTrackEdit::_select_at_anim(const Ref<Animation> &p_anim, int p_track, real_t p_pos, bool p_single) {
1220 if (!(animation == p_anim) || !is_visible()) {
1221 return;
1222 }
1223
1224 int idx = animation->track_find_key(p_track, p_pos, Animation::FIND_MODE_APPROX);
1225 ERR_FAIL_COND(idx < 0);
1226
1227 selection.insert(IntPair(p_track, idx));
1228 emit_signal(SNAME("select_key"), idx, p_single, p_track);
1229 queue_redraw();
1230 }
1231
1232 void AnimationBezierTrackEdit::gui_input(const Ref<InputEvent> &p_event) {
1233 ERR_FAIL_COND(p_event.is_null());
1234
1235 if (panner->gui_input(p_event)) {
1236 accept_event();
1237 return;
1238 }
1239
1240 if (p_event->is_pressed()) {
1241 if (ED_IS_SHORTCUT("animation_editor/duplicate_selected_keys", p_event)) {
1242 if (!read_only) {
1243 duplicate_selected_keys(-1.0, false);
1244 }
1245 accept_event();
1246 }
1247 if (ED_IS_SHORTCUT("animation_editor/cut_selected_keys", p_event)) {
1248 if (!read_only) {
1249 copy_selected_keys(true);
1250 }
1251 accept_event();
1252 }
1253 if (ED_IS_SHORTCUT("animation_editor/copy_selected_keys", p_event)) {
1254 if (!read_only) {
1255 copy_selected_keys(false);
1256 }
1257 accept_event();
1258 }
1259 if (ED_IS_SHORTCUT("animation_editor/paste_keys", p_event)) {
1260 if (!read_only) {
1261 paste_keys(-1.0, false);
1262 }
1263 accept_event();
1264 }
1265 if (ED_IS_SHORTCUT("animation_editor/delete_selection", p_event)) {
1266 if (!read_only) {
1267 delete_selection();
1268 }
1269 accept_event();
1270 }
1271 }
1272
1273 Ref<InputEventKey> key_press = p_event;
1274
1275 if (key_press.is_valid() && key_press->is_pressed()) {
1276 if (ED_IS_SHORTCUT("animation_bezier_editor/focus", p_event)) {
1277 SelectionSet focused_keys;
1278 if (selection.is_empty()) {
1279 for (int i = 0; i < edit_points.size(); ++i) {
1280 IntPair key_pair = IntPair(edit_points[i].track, edit_points[i].key);
1281 focused_keys.insert(key_pair);
1282 }
1283 } else {
1284 for (const IntPair &E : selection) {
1285 focused_keys.insert(E);
1286 if (E.second > 0) {
1287 IntPair previous_key = IntPair(E.first, E.second - 1);
1288 focused_keys.insert(previous_key);
1289 }
1290 if (E.second < animation->track_get_key_count(E.first) - 1) {
1291 IntPair next_key = IntPair(E.first, E.second + 1);
1292 focused_keys.insert(next_key);
1293 }
1294 }
1295 }
1296 if (focused_keys.is_empty()) {
1297 accept_event();
1298 return;
1299 }
1300
1301 real_t minimum_time = Math::INF;
1302 real_t maximum_time = -Math::INF;
1303 real_t minimum_value = Math::INF;
1304 real_t maximum_value = -Math::INF;
1305
1306 for (const IntPair &E : focused_keys) {
1307 IntPair key_pair = E;
1308
1309 real_t time = animation->track_get_key_time(key_pair.first, key_pair.second);
1310 real_t value = animation->bezier_track_get_key_value(key_pair.first, key_pair.second);
1311
1312 minimum_time = MIN(time, minimum_time);
1313 maximum_time = MAX(time, maximum_time);
1314 minimum_value = MIN(value, minimum_value);
1315 maximum_value = MAX(value, maximum_value);
1316 }
1317
1318 float width = get_size().width - timeline->get_name_limit() - timeline->get_buttons_width();
1319 float padding = width * 0.1;
1320 float desired_scale = (width - padding / 2.0) / (maximum_time - minimum_time);
1321 minimum_time = MAX(0, minimum_time - (padding / 2.0) / desired_scale);
1322
1323 float zv = Math::pow(100 / desired_scale, 0.125f);
1324 if (zv < 1) {
1325 zv = Math::pow(desired_scale / 100, 0.125f) - 1;
1326 zv = 1 - zv;
1327 }
1328 float zoom_value = timeline->get_zoom()->get_max() - zv;
1329
1330 if (Math::is_finite(minimum_time) && Math::is_finite(maximum_time) && maximum_time - minimum_time > CMP_EPSILON) {
1331 timeline->get_zoom()->set_value(zoom_value);
1332 callable_mp((Range *)timeline, &Range::set_value).call_deferred(minimum_time);
1333 }
1334
1335 if (Math::is_finite(minimum_value) && Math::is_finite(maximum_value)) {
1336 _zoom_vertically(minimum_value, maximum_value);
1337 }
1338
1339 queue_redraw();
1340 accept_event();
1341 return;
1342 } else if (ED_IS_SHORTCUT("animation_bezier_editor/select_all_keys", p_event)) {
1343 for (int i = 0; i < edit_points.size(); ++i) {
1344 _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), i == 0);
1345 }
1346
1347 queue_redraw();
1348 accept_event();
1349 return;
1350 } else if (ED_IS_SHORTCUT("animation_bezier_editor/deselect_all_keys", p_event)) {
1351 selection.clear();
1352 emit_signal(SNAME("clear_selection"));
1353
1354 queue_redraw();
1355 accept_event();
1356 return;
1357 }
1358 }
1359
1360 Ref<InputEventMouseButton> mb = p_event;
1361 int limit = timeline->get_name_limit();
1362 if (mb.is_valid() && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
1363 menu_insert_key = mb->get_position();
1364 if (menu_insert_key.x >= limit && menu_insert_key.x <= get_size().width) {
1365 if (!read_only) {
1366 Vector2 popup_pos = get_screen_position() + mb->get_position();
1367
1368 bool selected = _try_select_at_ui_pos(mb->get_position(), mb->is_shift_pressed(), false);
1369
1370 menu->clear();
1371 menu->add_icon_item(bezier_icon, TTR("Insert Key Here"), MENU_KEY_INSERT);
1372 if (selected || selection.size()) {
1373 menu->add_separator();
1374 menu->add_icon_item(get_editor_theme_icon(SNAME("Duplicate")), TTR("Duplicate Selected Key(s)"), MENU_KEY_DUPLICATE);
1375 menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCut")), TTR("Cut Selected Key(s)"), MENU_KEY_CUT);
1376 menu->add_icon_item(get_editor_theme_icon(SNAME("ActionCopy")), TTR("Copy Selected Key(s)"), MENU_KEY_COPY);
1377 }
1378
1379 if (editor->is_key_clipboard_active()) {
1380 menu->add_icon_item(get_editor_theme_icon(SNAME("ActionPaste")), TTR("Paste Key(s)"), MENU_KEY_PASTE);
1381 }
1382
1383 if (selected || selection.size()) {
1384 menu->add_separator();
1385 menu->add_icon_item(get_editor_theme_icon(SNAME("Remove")), TTR("Delete Selected Key(s)"), MENU_KEY_DELETE);
1386 menu->add_separator();
1387 menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesFree")), TTR("Make Handles Free"), MENU_KEY_SET_HANDLE_FREE);
1388 menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesLinear")), TTR("Make Handles Linear"), MENU_KEY_SET_HANDLE_LINEAR);
1389 menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesBalanced")), TTR("Make Handles Balanced"), MENU_KEY_SET_HANDLE_BALANCED);
1390 menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesMirror")), TTR("Make Handles Mirrored"), MENU_KEY_SET_HANDLE_MIRRORED);
1391 menu->add_separator();
1392 menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesBalanced")), TTR("Make Handles Balanced (Auto Tangent)"), MENU_KEY_SET_HANDLE_AUTO_BALANCED);
1393 menu->add_icon_item(get_editor_theme_icon(SNAME("BezierHandlesMirror")), TTR("Make Handles Mirrored (Auto Tangent)"), MENU_KEY_SET_HANDLE_AUTO_MIRRORED);
1394 }
1395
1396 if (menu->get_item_count()) {
1397 menu->reset_size();
1398 menu->set_position(popup_pos);
1399 menu->popup();
1400 }
1401 }
1402 }
1403 }
1404
1405 if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1406 Point2 pos = mb->get_position();
1407 bool no_mod_key_pressed = !mb->is_alt_pressed() && !mb->is_shift_pressed() && !mb->is_command_or_control_pressed();
1408 if (mb->is_double_click() && !moving_selection && no_mod_key_pressed) {
1409 int x = pos.x - timeline->get_name_limit();
1410 float ofs = x / timeline->get_zoom_scale() + timeline->get_value();
1411 emit_signal(SNAME("timeline_changed"), ofs, false);
1412 }
1413 for (const KeyValue<int, Rect2> &E : subtracks) {
1414 if (E.value.has_point(mb->get_position())) {
1415 if (!locked_tracks.has(E.key) && !hidden_tracks.has(E.key)) {
1416 set_animation_and_track(animation, E.key, read_only);
1417 _clear_selection();
1418 }
1419 return;
1420 }
1421 }
1422
1423 for (const KeyValue<int, RBMap<int, Rect2>> &E : subtrack_icons) {
1424 int track = E.key;
1425 for (const KeyValue<int, Rect2> &I : E.value) {
1426 if (I.value.has_point(mb->get_position())) {
1427 if (I.key == REMOVE_ICON) {
1428 if (!read_only) {
1429 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1430 undo_redo->create_action("Remove Bezier Track", UndoRedo::MERGE_DISABLE, animation.ptr());
1431
1432 undo_redo->add_do_method(this, "_update_locked_tracks_after", track);
1433 undo_redo->add_do_method(this, "_update_hidden_tracks_after", track);
1434
1435 undo_redo->add_do_method(animation.ptr(), "remove_track", track);
1436
1437 undo_redo->add_undo_method(animation.ptr(), "add_track", Animation::TrackType::TYPE_BEZIER, track);
1438 undo_redo->add_undo_method(animation.ptr(), "track_set_path", track, animation->track_get_path(track));
1439
1440 if (locked_tracks.has(track)) {
1441 undo_redo->add_undo_method(this, "_lock_track", track);
1442 }
1443
1444 if (hidden_tracks.has(track)) {
1445 undo_redo->add_undo_method(this, "_hide_track", track);
1446 }
1447
1448 for (int i = 0; i < animation->track_get_key_count(track); ++i) {
1449 undo_redo->add_undo_method(
1450 this,
1451 "_bezier_track_insert_key_at_anim",
1452 animation,
1453 track,
1454 animation->track_get_key_time(track, i),
1455 animation->bezier_track_get_key_value(track, i),
1456 animation->bezier_track_get_key_in_handle(track, i),
1457 animation->bezier_track_get_key_out_handle(track, i),
1458 animation->bezier_track_get_key_handle_mode(track, i));
1459 }
1460
1461 undo_redo->commit_action();
1462
1463 for (selected_track = MIN(selected_track, animation->get_track_count() - 1); selected_track >= 0; --selected_track) {
1464 if (animation->track_get_type(selected_track) == Animation::TYPE_BEZIER) {
1465 break;
1466 }
1467 }
1468 }
1469 return;
1470 } else if (I.key == LOCK_ICON) {
1471 if (!_unlock_track(track)) {
1472 _lock_track(track);
1473 }
1474 queue_redraw();
1475 return;
1476 } else if (I.key == VISIBILITY_ICON) {
1477 if (!_show_track(track)) {
1478 _hide_track(track);
1479 }
1480 queue_redraw();
1481 return;
1482 } else if (I.key == SOLO_ICON) {
1483 bool show_other_tracks = true;
1484 for (int i = 0; i < animation->get_track_count(); ++i) {
1485 if (i != track && animation->track_get_type(i) == Animation::TYPE_BEZIER && !hidden_tracks.has(i)) {
1486 show_other_tracks = false;
1487 break;
1488 }
1489 }
1490
1491 if (_show_track(track)) {
1492 show_other_tracks = false;
1493 }
1494
1495 for (int i = 0; i < animation->get_track_count(); ++i) {
1496 if (i != track) {
1497 if (show_other_tracks) {
1498 _show_track(i);
1499 } else {
1500 _hide_track(i);
1501 }
1502 }
1503 }
1504 queue_redraw();
1505 return;
1506 }
1507 return;
1508 }
1509 }
1510 }
1511
1512 for (const KeyValue<String, RBMap<int, Rect2>> &E : node_icons) {
1513 for (const KeyValue<int, Rect2> &I : E.value) {
1514 if (I.value.has_point(mb->get_position())) {
1515 if (I.key == REMOVE_ICON) {
1516 if (!read_only) {
1517 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1518 undo_redo->create_action("Remove Bezier Track", UndoRedo::MERGE_DISABLE, animation.ptr(), true);
1519
1520 for (int i = animation->get_track_count() - 1; i >= 0; --i) {
1521 if (animation->track_get_path(i).get_concatenated_names() != E.key) {
1522 continue;
1523 }
1524
1525 if (animation->track_get_type(i) != Animation::TYPE_BEZIER) {
1526 continue;
1527 }
1528
1529 undo_redo->add_do_method(this, "_update_locked_tracks_after", i);
1530 undo_redo->add_do_method(this, "_update_hidden_tracks_after", i);
1531 undo_redo->add_do_method(animation.ptr(), "remove_track", i);
1532
1533 for (int j = animation->track_get_key_count(i) - 1; j >= 0; --j) {
1534 undo_redo->add_undo_method(
1535 this,
1536 "_bezier_track_insert_key_at_anim",
1537 animation,
1538 i,
1539 animation->track_get_key_time(i, j),
1540 animation->bezier_track_get_key_value(i, j),
1541 animation->bezier_track_get_key_in_handle(i, j),
1542 animation->bezier_track_get_key_out_handle(i, j),
1543 animation->bezier_track_get_key_handle_mode(i, j));
1544 }
1545
1546 if (hidden_tracks.has(i)) {
1547 undo_redo->add_undo_method(this, "_hide_track", i);
1548 }
1549
1550 if (locked_tracks.has(i)) {
1551 undo_redo->add_undo_method(this, "_lock_track", i);
1552 }
1553
1554 undo_redo->add_undo_method(animation.ptr(), "track_set_path", i, animation->track_get_path(i));
1555 undo_redo->add_undo_method(animation.ptr(), "add_track", Animation::TrackType::TYPE_BEZIER, i);
1556 }
1557
1558 undo_redo->commit_action();
1559
1560 for (selected_track = MIN(selected_track, animation->get_track_count() - 1); selected_track >= 0; --selected_track) {
1561 if (animation->track_get_type(selected_track) == Animation::TYPE_BEZIER) {
1562 break;
1563 }
1564 }
1565 }
1566 return;
1567 } else if (I.key == LOCK_ICON) {
1568 bool unlock_tracks = true;
1569
1570 for (int i = 0; i < animation->get_track_count(); ++i) {
1571 if (animation->track_get_path(i).get_concatenated_names() == E.key) {
1572 if (animation->track_get_type(i) == Animation::TYPE_BEZIER && !locked_tracks.has(i)) {
1573 unlock_tracks = false;
1574 break;
1575 }
1576 }
1577 }
1578
1579 for (int i = 0; i < animation->get_track_count(); ++i) {
1580 if (animation->track_get_path(i).get_concatenated_names() == E.key) {
1581 if (unlock_tracks) {
1582 _unlock_track(i);
1583 } else {
1584 _lock_track(i);
1585 }
1586 }
1587 }
1588
1589 queue_redraw();
1590 return;
1591 } else if (I.key == VISIBILITY_ICON) {
1592 bool show_tracks = true;
1593
1594 for (int i = 0; i < animation->get_track_count(); ++i) {
1595 if (animation->track_get_path(i).get_concatenated_names() == E.key) {
1596 if (animation->track_get_type(i) == Animation::TYPE_BEZIER && !hidden_tracks.has(i)) {
1597 show_tracks = false;
1598 break;
1599 }
1600 }
1601 }
1602
1603 for (int i = 0; i < animation->get_track_count(); ++i) {
1604 if (animation->track_get_path(i).get_concatenated_names() == E.key) {
1605 if (show_tracks) {
1606 _show_track(i);
1607 } else {
1608 _hide_track(i);
1609 }
1610 }
1611 }
1612
1613 queue_redraw();
1614 return;
1615 } else if (I.key == SOLO_ICON) {
1616 bool show_other_tracks = true;
1617
1618 for (int i = 0; i < animation->get_track_count(); ++i) {
1619 if (animation->track_get_path(i).get_concatenated_names() != E.key) {
1620 if (animation->track_get_type(i) == Animation::TYPE_BEZIER && !hidden_tracks.has(i)) {
1621 show_other_tracks = false;
1622 break;
1623 }
1624 }
1625 }
1626
1627 bool show_own_tracks = true;
1628
1629 for (int i = 0; i < animation->get_track_count(); ++i) {
1630 if (animation->track_get_path(i).get_concatenated_names() == E.key) {
1631 if (animation->track_get_type(i) == Animation::TYPE_BEZIER && !hidden_tracks.has(i)) {
1632 show_own_tracks = false;
1633 break;
1634 }
1635 }
1636 }
1637
1638 if (show_own_tracks) {
1639 show_other_tracks = false;
1640 }
1641
1642 for (int i = 0; i < animation->get_track_count(); ++i) {
1643 if (animation->track_get_path(i).get_concatenated_names() == E.key) {
1644 if (show_own_tracks) {
1645 _show_track(i);
1646 }
1647 } else {
1648 if (show_other_tracks) {
1649 _show_track(i);
1650 } else {
1651 _hide_track(i);
1652 }
1653 }
1654 }
1655
1656 queue_redraw();
1657 return;
1658 }
1659 return;
1660 }
1661 }
1662 }
1663
1664 // Check this first, to allow manipulating key handles while ignoring keyframes before scaling/moving.
1665 bool inside_selection_handles_rect = !read_only && selection_handles_rect.has_point(mb->get_position());
1666
1667 // First, check keyframe.
1668 // Command/Control makes it ignore the keyframe, so control point editors can be force-edited.
1669 if (!inside_selection_handles_rect && !mb->is_command_or_control_pressed()) {
1670 if (_try_select_at_ui_pos(mb->get_position(), mb->is_shift_pressed(), true)) {
1671 return;
1672 }
1673 }
1674 // Second, check key handles.
1675 for (int i = 0; i < edit_points.size(); i++) {
1676 if (!read_only) {
1677 if (edit_points[i].in_rect.has_point(mb->get_position())) {
1678 moving_handle = -1;
1679 moving_handle_key = edit_points[i].key;
1680 moving_handle_track = edit_points[i].track;
1681 moving_handle_left = animation->bezier_track_get_key_in_handle(edit_points[i].track, edit_points[i].key);
1682 moving_handle_right = animation->bezier_track_get_key_out_handle(edit_points[i].track, edit_points[i].key);
1683 queue_redraw();
1684 return;
1685 }
1686
1687 if (edit_points[i].out_rect.has_point(mb->get_position())) {
1688 moving_handle = 1;
1689 moving_handle_key = edit_points[i].key;
1690 moving_handle_track = edit_points[i].track;
1691 moving_handle_left = animation->bezier_track_get_key_in_handle(edit_points[i].track, edit_points[i].key);
1692 moving_handle_right = animation->bezier_track_get_key_out_handle(edit_points[i].track, edit_points[i].key);
1693 queue_redraw();
1694 return;
1695 }
1696 }
1697 }
1698
1699 // Box scaling/movement.
1700 if (inside_selection_handles_rect) {
1701 const Vector2i rel_pos = mb->get_position() - selection_rect.position;
1702 scaling_selection_handles = Vector2i();
1703
1704 // Check which scaling handles are available.
1705 if (selection_rect.size.width > CMP_EPSILON) {
1706 if (rel_pos.x <= 0) {
1707 scaling_selection_handles.x = -1;
1708 } else if (rel_pos.x >= selection_rect.size.width) {
1709 scaling_selection_handles.x = 1;
1710 }
1711 }
1712 if (selection_rect.size.height > CMP_EPSILON) {
1713 if (rel_pos.y <= 0) {
1714 scaling_selection_handles.y = -1;
1715 } else if (rel_pos.y >= selection_rect.size.height) {
1716 scaling_selection_handles.y = 1;
1717 }
1718 }
1719
1720 if (scaling_selection_handles != Vector2i()) {
1721 scaling_selection = true;
1722
1723 const float time = ((selection_rect.position.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
1724 const float h = (get_size().height / 2.0 - selection_rect.position.y) * timeline_v_zoom + timeline_v_scroll;
1725 scaling_selection_pivot = Point2(time, h);
1726
1727 return;
1728 }
1729
1730 // If not scaling, that means we're moving.
1731 moving_selection_attempt = true;
1732 moving_selection = false;
1733 moving_selection_mouse_begin = mb->get_position();
1734 // The pivot will be from the mouse click location, not a specific key.
1735 moving_selection_from_key = -1;
1736 moving_selection_from_track = selected_track;
1737 moving_selection_offset = Vector2();
1738 select_single_attempt = IntPair(-1, -1);
1739
1740 return;
1741 }
1742
1743 // Insert new point.
1744 if (mb->get_position().x >= limit && mb->get_position().x < get_size().width && mb->is_command_or_control_pressed()) {
1745 float h = (get_size().height / 2.0 - mb->get_position().y) * timeline_v_zoom + timeline_v_scroll;
1746 Array new_point = animation->make_default_bezier_key(h);
1747
1748 real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
1749 while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
1750 time += 0.0001;
1751 }
1752
1753 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1754 undo_redo->create_action(TTR("Add Bezier Point"));
1755 undo_redo->add_do_method(animation.ptr(), "bezier_track_insert_key", selected_track, time, new_point[0], Vector2(new_point[1], new_point[2]), Vector2(new_point[3], new_point[4]));
1756 undo_redo->add_do_method(editor, "_bezier_track_set_key_handle_mode_at_time", animation.ptr(), selected_track, time, (Animation::HandleMode)editor->bezier_key_mode->get_selected_id(), Animation::HANDLE_SET_MODE_AUTO);
1757 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, time);
1758 undo_redo->commit_action();
1759
1760 // Then attempt to move.
1761 int index = animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX);
1762 ERR_FAIL_COND(index == -1);
1763 _clear_selection();
1764 _select_at_anim(animation, selected_track, animation->track_get_key_time(selected_track, index), true);
1765
1766 moving_selection_attempt = true;
1767 moving_inserted_key = true;
1768 moving_selection = false;
1769 moving_selection_mouse_begin = mb->get_position();
1770 moving_selection_from_key = index;
1771 moving_selection_from_track = selected_track;
1772 moving_selection_offset = Vector2();
1773 select_single_attempt = IntPair(-1, -1);
1774 queue_redraw();
1775
1776 return;
1777 }
1778
1779 // Box select.
1780 if (mb->get_position().x >= limit && mb->get_position().x < get_size().width) {
1781 box_selecting_attempt = true;
1782 box_selecting = false;
1783 box_selecting_add = false;
1784 box_selection_from = mb->get_position();
1785 return;
1786 }
1787 }
1788
1789 if (box_selecting_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1790 if (box_selecting) {
1791 // Do actual select.
1792 if (!box_selecting_add) {
1793 _clear_selection();
1794 }
1795
1796 Vector2 bs_from = box_selection_from;
1797 Vector2 bs_to = box_selection_to;
1798 if (bs_from.x > bs_to.x) {
1799 SWAP(bs_from.x, bs_to.x);
1800 }
1801 if (bs_from.y > bs_to.y) {
1802 SWAP(bs_from.y, bs_to.y);
1803 }
1804 Rect2 rect(bs_from, bs_to - bs_from);
1805
1806 bool track_set = false;
1807 int j = 0;
1808 for (int i = 0; i < edit_points.size(); i++) {
1809 if (edit_points[i].point_rect.intersects(rect)) {
1810 _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), j == 0 && !box_selecting_add);
1811 if (!track_set) {
1812 track_set = true;
1813 set_animation_and_track(animation, edit_points[i].track, read_only);
1814 }
1815 j++;
1816 }
1817 }
1818 } else {
1819 _clear_selection(); // Clicked and nothing happened, so clear the selection.
1820
1821 // Select by clicking on curve.
1822 int track_count = animation->get_track_count();
1823
1824 real_t animation_length = animation->get_length();
1825 animation->set_length(real_t(INT_MAX)); // bezier_track_interpolate doesn't find keys if they exist beyond anim length.
1826
1827 real_t time = ((mb->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
1828
1829 for (int i = 0; i < track_count; ++i) {
1830 if (animation->track_get_type(i) != Animation::TrackType::TYPE_BEZIER || hidden_tracks.has(i) || locked_tracks.has(i)) {
1831 continue;
1832 }
1833
1834 float track_h = animation->bezier_track_interpolate(i, time);
1835 float track_height = _bezier_h_to_pixel(track_h);
1836
1837 if (std::abs(mb->get_position().y - track_height) < 10) {
1838 set_animation_and_track(animation, i, read_only);
1839 break;
1840 }
1841 }
1842
1843 animation->set_length(animation_length);
1844 }
1845
1846 box_selecting_attempt = false;
1847 box_selecting = false;
1848 queue_redraw();
1849 }
1850
1851 if (moving_selection_attempt && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1852 if (!read_only) {
1853 if (moving_selection && (std::abs(moving_selection_offset.x) > CMP_EPSILON || std::abs(moving_selection_offset.y) > CMP_EPSILON)) {
1854 // Commit it.
1855 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1856 undo_redo->create_action(TTR("Move Bezier Points"));
1857
1858 List<AnimMoveRestore> to_restore;
1859 List<Animation::HandleMode> to_restore_handle_modes;
1860 // 1 - Remove the keys.
1861 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1862 undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
1863 }
1864 // 2 - Remove overlapped keys.
1865 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1866 real_t newtime = animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x;
1867
1868 int idx = animation->track_find_key(E->get().first, newtime, Animation::FIND_MODE_APPROX);
1869 if (idx == -1) {
1870 continue;
1871 }
1872
1873 if (selection.has(IntPair(E->get().first, idx))) {
1874 continue; // Already in selection, don't save.
1875 }
1876
1877 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newtime);
1878 AnimMoveRestore amr;
1879
1880 amr.key = animation->track_get_key_value(E->get().first, idx);
1881 amr.track = E->get().first;
1882 amr.time = newtime;
1883
1884 to_restore.push_back(amr);
1885 to_restore_handle_modes.push_back(animation->bezier_track_get_key_handle_mode(E->get().first, idx));
1886 }
1887
1888 // 3 - Move the keys (re-insert them).
1889 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1890 real_t newpos = animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x;
1891 Array key = animation->track_get_key_value(E->get().first, E->get().second);
1892 real_t h = key[0];
1893 h += moving_selection_offset.y;
1894 key[0] = h;
1895
1896 Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second);
1897 Animation::HandleSetMode handle_set_mode = Animation::HANDLE_SET_MODE_NONE;
1898 if (moving_inserted_key) {
1899 handle_mode = (Animation::HandleMode)editor->bezier_key_mode->get_selected_id();
1900 handle_set_mode = Animation::HANDLE_SET_MODE_AUTO;
1901 }
1902
1903 undo_redo->add_do_method(
1904 this,
1905 "_bezier_track_insert_key_at_anim",
1906 animation,
1907 E->get().first,
1908 newpos,
1909 key[0],
1910 Vector2(key[1], key[2]),
1911 Vector2(key[3], key[4]),
1912 handle_mode,
1913 handle_set_mode);
1914 }
1915
1916 // 4 - (undo) Remove inserted keys.
1917 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1918 real_t newpos = animation->track_get_key_time(E->get().first, E->get().second) + moving_selection_offset.x;
1919 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newpos);
1920 }
1921
1922 // 5 - (undo) Reinsert keys.
1923 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1924 real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
1925 Array key = animation->track_get_key_value(E->get().first, E->get().second);
1926 undo_redo->add_undo_method(
1927 this,
1928 "_bezier_track_insert_key_at_anim",
1929 animation,
1930 E->get().first,
1931 oldpos,
1932 key[0],
1933 Vector2(key[1], key[2]),
1934 Vector2(key[3], key[4]),
1935 animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
1936 }
1937
1938 // 6 - (undo) Reinsert overlapped keys.
1939 List<AnimMoveRestore>::ConstIterator restore_itr = to_restore.begin();
1940 List<Animation::HandleMode>::ConstIterator handle_itr = to_restore_handle_modes.begin();
1941 for (; restore_itr != to_restore.end() && handle_itr != to_restore_handle_modes.end(); ++restore_itr, ++handle_itr) {
1942 const AnimMoveRestore &amr = *restore_itr;
1943 Array key = amr.key;
1944 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, 1);
1945 undo_redo->add_undo_method(
1946 this,
1947 "_bezier_track_insert_key_at_anim",
1948 animation,
1949 amr.track,
1950 amr.time,
1951 key[0],
1952 Vector2(key[1], key[2]),
1953 Vector2(key[3], key[4]),
1954 *handle_itr);
1955 }
1956
1957 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
1958 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
1959
1960 // 7 - Reselect.
1961 int i = 0;
1962 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
1963 real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
1964 real_t newpos = oldpos + moving_selection_offset.x;
1965
1966 undo_redo->add_do_method(this, "_select_at_anim", animation, E->get().first, newpos, i == 0);
1967 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, oldpos, i == 0);
1968 i++;
1969 }
1970
1971 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
1972 if (ape) {
1973 undo_redo->add_do_method(ape, "_animation_update_key_frame");
1974 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
1975 }
1976 undo_redo->commit_action();
1977
1978 } else if (select_single_attempt != IntPair(-1, -1)) {
1979 selection.clear();
1980 set_animation_and_track(animation, select_single_attempt.first, read_only);
1981 _select_at_anim(animation, select_single_attempt.first, animation->track_get_key_time(select_single_attempt.first, select_single_attempt.second), true);
1982 }
1983
1984 moving_selection = false;
1985 moving_selection_attempt = false;
1986 moving_inserted_key = false;
1987 moving_selection_mouse_begin = Point2();
1988 queue_redraw();
1989 }
1990 }
1991
1992 if (scaling_selection && mb.is_valid() && !read_only && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1993 if (std::abs(scaling_selection_scale.x - 1) > CMP_EPSILON || std::abs(scaling_selection_scale.y - 1) > CMP_EPSILON) {
1994 // Scale it.
1995 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1996 undo_redo->create_action(TTR("Scale Bezier Points"));
1997
1998 List<AnimMoveRestore> to_restore;
1999 List<Animation::HandleMode> to_restore_handle_modes;
2000 // 1 - Remove the keys.
2001 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2002 undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
2003 }
2004 // 2 - Remove overlapped keys.
2005 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2006 real_t newtime = animation->track_get_key_time(E->get().first, E->get().second);
2007 newtime += -scaling_selection_offset.x + (newtime - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
2008
2009 int idx = animation->track_find_key(E->get().first, newtime, Animation::FIND_MODE_APPROX);
2010 if (idx == -1) {
2011 continue;
2012 }
2013
2014 if (selection.has(IntPair(E->get().first, idx))) {
2015 continue; // Already in selection, don't save.
2016 }
2017
2018 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newtime);
2019 AnimMoveRestore amr;
2020
2021 amr.key = animation->track_get_key_value(E->get().first, idx);
2022 amr.track = E->get().first;
2023 amr.time = newtime;
2024
2025 to_restore.push_back(amr);
2026 to_restore_handle_modes.push_back(animation->bezier_track_get_key_handle_mode(E->get().first, idx));
2027 }
2028
2029 // 3 - Scale the keys (re-insert them).
2030 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2031 real_t newpos = animation->track_get_key_time(E->get().first, E->get().second);
2032 newpos += -scaling_selection_offset.x + (newpos - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
2033
2034 Array key = animation->track_get_key_value(E->get().first, E->get().second);
2035 real_t h = key[0];
2036 h += -scaling_selection_offset.y + (h - scaling_selection_pivot.y) * (scaling_selection_scale.y - 1);
2037 key[0] = h;
2038
2039 undo_redo->add_do_method(
2040 this,
2041 "_bezier_track_insert_key_at_anim",
2042 animation,
2043 E->get().first,
2044 newpos,
2045 key[0],
2046 Vector2(key[1], key[2]),
2047 Vector2(key[3], key[4]),
2048 animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
2049 }
2050
2051 // 4 - (undo) Remove inserted keys.
2052 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2053 real_t newpos = animation->track_get_key_time(E->get().first, E->get().second);
2054 newpos += -scaling_selection_offset.x + (newpos - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
2055 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, newpos);
2056 }
2057
2058 // 5 - (undo) Reinsert keys.
2059 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2060 real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
2061 Array key = animation->track_get_key_value(E->get().first, E->get().second);
2062 undo_redo->add_undo_method(
2063 this,
2064 "_bezier_track_insert_key_at_anim",
2065 animation,
2066 E->get().first,
2067 oldpos,
2068 key[0],
2069 Vector2(key[1], key[2]),
2070 Vector2(key[3], key[4]),
2071 animation->bezier_track_get_key_handle_mode(E->get().first, E->get().second));
2072 }
2073
2074 // 6 - (undo) Reinsert overlapped keys.
2075 List<AnimMoveRestore>::ConstIterator restore_itr = to_restore.begin();
2076 List<Animation::HandleMode>::ConstIterator handle_itr = to_restore_handle_modes.begin();
2077 for (; restore_itr != to_restore.end() && handle_itr != to_restore_handle_modes.end(); ++restore_itr, ++handle_itr) {
2078 const AnimMoveRestore &amr = *restore_itr;
2079 Array key = amr.key;
2080 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", amr.track, amr.time, amr.key, 1);
2081 undo_redo->add_undo_method(
2082 this,
2083 "_bezier_track_insert_key_at_anim",
2084 animation,
2085 amr.track,
2086 amr.time,
2087 key[0],
2088 Vector2(key[1], key[2]),
2089 Vector2(key[3], key[4]),
2090 *handle_itr);
2091 }
2092
2093 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
2094 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
2095
2096 // 7 - Reselect.
2097 int i = 0;
2098 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2099 real_t oldpos = animation->track_get_key_time(E->get().first, E->get().second);
2100 real_t newpos = animation->track_get_key_time(E->get().first, E->get().second);
2101 newpos += -scaling_selection_offset.x + (newpos - scaling_selection_pivot.x) * (scaling_selection_scale.x - 1);
2102
2103 undo_redo->add_do_method(this, "_select_at_anim", animation, E->get().first, newpos, i == 0);
2104 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, oldpos, i == 0);
2105 i++;
2106 }
2107
2108 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2109 if (ape) {
2110 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2111 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2112 }
2113 undo_redo->commit_action();
2114 }
2115
2116 scaling_selection = false;
2117 scaling_selection_scale = Vector2(1, 1);
2118 scaling_selection_offset = Vector2();
2119 queue_redraw();
2120 }
2121
2122 Ref<InputEventMouseMotion> mm = p_event;
2123 if (moving_selection_attempt && mm.is_valid()) {
2124 Point2 new_pos = mm->get_position();
2125 if (mm->is_alt_pressed()) { // Axis snap key move when alt is pressed
2126 if (Math::abs(new_pos.x - moving_selection_mouse_begin.x) > Math::abs(new_pos.y - moving_selection_mouse_begin.y)) {
2127 new_pos.y = moving_selection_mouse_begin.y;
2128 } else {
2129 new_pos.x = moving_selection_mouse_begin.x;
2130 }
2131 }
2132
2133 if (!moving_selection) {
2134 moving_selection = true;
2135 select_single_attempt = IntPair(-1, -1);
2136 }
2137
2138 if (!read_only) {
2139 float y = (get_size().height / 2.0 - new_pos.y) * timeline_v_zoom + timeline_v_scroll;
2140 float moving_selection_begin_time = ((moving_selection_mouse_begin.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
2141 float new_time = ((new_pos.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
2142 float moving_selection_pivot = moving_selection_from_key != -1 ? animation->track_get_key_time(moving_selection_from_track, moving_selection_from_key) : 0;
2143 float time_delta = new_time - moving_selection_begin_time;
2144
2145 float snapped_time = editor->snap_time(moving_selection_pivot + time_delta);
2146 float time_offset = 0.0;
2147 if (std::abs(moving_selection_offset.x) > CMP_EPSILON || (snapped_time > moving_selection_pivot && time_delta > CMP_EPSILON) || (snapped_time < moving_selection_pivot && time_delta < -CMP_EPSILON)) {
2148 time_offset = snapped_time - moving_selection_pivot;
2149 }
2150
2151 float moving_selection_begin_value;
2152 if (moving_selection_from_key == -1) {
2153 moving_selection_begin_value = (get_size().height / 2.0 - moving_selection_mouse_begin.y) * timeline_v_zoom + timeline_v_scroll;
2154 } else {
2155 moving_selection_begin_value = animation->bezier_track_get_key_value(moving_selection_from_track, moving_selection_from_key);
2156 }
2157
2158 float y_offset = y - moving_selection_begin_value;
2159 moving_selection_offset = Vector2(time_offset, y_offset);
2160 }
2161
2162 additional_moving_handle_lefts.clear();
2163 additional_moving_handle_rights.clear();
2164
2165 queue_redraw();
2166 }
2167
2168 if (box_selecting_attempt && mm.is_valid()) {
2169 if (!box_selecting) {
2170 box_selecting = true;
2171 box_selecting_add = mm->is_shift_pressed();
2172 }
2173
2174 box_selection_to = mm->get_position();
2175 queue_redraw();
2176 }
2177
2178 if (scaling_selection && mm.is_valid() && !read_only) {
2179 Point2 mp = mm->get_position();
2180 const int handle_length = Math::round((selection_handles_rect.size.width - selection_rect.size.width) / 4.0);
2181 Point2 rel_pos;
2182
2183 // Calculate the scale according with the distance between the mouse's position (adjusted so that the cursor appears inside the handles)
2184 // and the opposite end of the `selection_rect`.
2185
2186 if (scaling_selection_handles.x != 0) {
2187 if (scaling_selection_handles.x == 1) { // Right Handle
2188 const int handle_adjust = Math::round(mp.x - (scaling_selection_scale.x >= 0 ? selection_rect.position.x : (selection_rect.position.x + selection_rect.size.width)));
2189 mp.x -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.x * SIGN(handle_adjust);
2190
2191 if (editor->is_snap_keys_enabled()) {
2192 mp.x = editor->snap_time((mp.x - limit) / timeline->get_zoom_scale(), true) + timeline->get_value();
2193 mp.x = (mp.x - timeline->get_value()) * timeline->get_zoom_scale() + limit;
2194 }
2195
2196 rel_pos.x = scaling_selection_scale.x >= 0 ? (mp.x - selection_rect.position.x) : selection_rect.position.x + selection_rect.size.width - mp.x;
2197 } else { // Left Handle
2198 const int handle_adjust = Math::round((scaling_selection_scale.x >= 0 ? (selection_rect.position.x + selection_rect.size.width) : selection_rect.position.x) - mp.x);
2199 mp.x -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.x * SIGN(handle_adjust);
2200
2201 const float x = editor->snap_time((mp.x - limit) / timeline->get_zoom_scale(), true) + timeline->get_value();
2202 if (editor->is_snap_keys_enabled()) {
2203 mp.x = (x - timeline->get_value()) * timeline->get_zoom_scale() + limit;
2204 }
2205
2206 rel_pos.x = scaling_selection_scale.x >= 0 ? (selection_rect.position.x + selection_rect.size.width - mp.x) : (mp.x - selection_rect.position.x);
2207 scaling_selection_offset.x = scaling_selection_pivot.x - x;
2208 }
2209
2210 scaling_selection_scale.x *= rel_pos.x / selection_rect.size.width;
2211 if (scaling_selection_scale.x == 0) {
2212 scaling_selection_scale.x = CMP_EPSILON;
2213 }
2214 }
2215
2216 if (scaling_selection_handles.y != 0) {
2217 if (scaling_selection_handles.y == 1) { // Bottom Handle
2218 const int handle_adjust = Math::round(mp.y - (scaling_selection_scale.y >= 0 ? selection_rect.position.y : (selection_rect.position.y + selection_rect.size.height)));
2219 mp.y -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.y * SIGN(handle_adjust);
2220
2221 if (scaling_selection_scale.y >= 0) {
2222 rel_pos.y = mp.y - selection_rect.position.y;
2223 } else {
2224 rel_pos.y = selection_rect.position.y + selection_rect.size.height - mp.y;
2225 }
2226 } else { // Top Handle
2227 const int handle_adjust = Math::round((scaling_selection_scale.y >= 0 ? (selection_rect.position.y + selection_rect.size.height) : selection_rect.position.y) - mp.y);
2228 mp.y -= MIN(Math::abs(handle_adjust), handle_length) * scaling_selection_handles.y * SIGN(handle_adjust);
2229
2230 if (scaling_selection_scale.y >= 0) {
2231 rel_pos.y = selection_rect.position.y + selection_rect.size.height - mp.y;
2232 } else {
2233 rel_pos.y = mp.y - selection_rect.position.y;
2234 }
2235
2236 const float h = (get_size().height / 2.0 - mp.y) * timeline_v_zoom + timeline_v_scroll;
2237 scaling_selection_offset.y = scaling_selection_pivot.y - h;
2238 }
2239
2240 scaling_selection_scale.y *= rel_pos.y / selection_rect.size.height;
2241 if (scaling_selection_scale.y == 0) {
2242 scaling_selection_scale.y = CMP_EPSILON;
2243 }
2244 }
2245
2246 queue_redraw();
2247 }
2248
2249 if ((moving_handle == 1 || moving_handle == -1) && mm.is_valid()) {
2250 float y = (get_size().height / 2.0 - mm->get_position().y) * timeline_v_zoom + timeline_v_scroll;
2251 float x = editor->snap_time((mm->get_position().x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
2252
2253 Vector2 key_pos = Vector2(animation->track_get_key_time(moving_handle_track, moving_handle_key), animation->bezier_track_get_key_value(moving_handle_track, moving_handle_key));
2254
2255 Vector2 moving_handle_value = Vector2(x, y) - key_pos;
2256
2257 moving_handle_left = animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key);
2258 moving_handle_right = animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key);
2259
2260 if (moving_handle == -1) {
2261 moving_handle_left = moving_handle_value;
2262
2263 Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);
2264
2265 if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
2266 real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
2267 Transform2D xform;
2268 xform.set_scale(Vector2(1.0, 1.0 / ratio));
2269
2270 Vector2 vec_out = xform.xform(moving_handle_right);
2271 Vector2 vec_in = xform.xform(moving_handle_left);
2272
2273 moving_handle_right = xform.affine_inverse().xform(-vec_in.normalized() * vec_out.length());
2274 } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) {
2275 moving_handle_right = -moving_handle_left;
2276 }
2277 } else if (moving_handle == 1) {
2278 moving_handle_right = moving_handle_value;
2279
2280 Animation::HandleMode handle_mode = animation->bezier_track_get_key_handle_mode(moving_handle_track, moving_handle_key);
2281
2282 if (handle_mode == Animation::HANDLE_MODE_BALANCED) {
2283 real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
2284 Transform2D xform;
2285 xform.set_scale(Vector2(1.0, 1.0 / ratio));
2286
2287 Vector2 vec_in = xform.xform(moving_handle_left);
2288 Vector2 vec_out = xform.xform(moving_handle_right);
2289
2290 moving_handle_left = xform.affine_inverse().xform(-vec_out.normalized() * vec_in.length());
2291 } else if (handle_mode == Animation::HANDLE_MODE_MIRRORED) {
2292 moving_handle_left = -moving_handle_right;
2293 }
2294 }
2295 queue_redraw();
2296 }
2297
2298 if ((moving_handle == -1 || moving_handle == 1) && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
2299 if (!read_only) {
2300 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2301 undo_redo->create_action(TTR("Move Bezier Points"));
2302 if (moving_handle == -1) {
2303 real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
2304 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_in_handle", moving_handle_track, moving_handle_key, moving_handle_left, ratio);
2305 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_in_handle", moving_handle_track, moving_handle_key, animation->bezier_track_get_key_in_handle(moving_handle_track, moving_handle_key), ratio);
2306 } else if (moving_handle == 1) {
2307 real_t ratio = timeline->get_zoom_scale() * timeline_v_zoom;
2308 undo_redo->add_do_method(animation.ptr(), "bezier_track_set_key_out_handle", moving_handle_track, moving_handle_key, moving_handle_right, ratio);
2309 undo_redo->add_undo_method(animation.ptr(), "bezier_track_set_key_out_handle", moving_handle_track, moving_handle_key, animation->bezier_track_get_key_out_handle(moving_handle_track, moving_handle_key), ratio);
2310 }
2311 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2312 if (ape) {
2313 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2314 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2315 }
2316 undo_redo->commit_action();
2317 moving_handle = 0;
2318 queue_redraw();
2319 }
2320 }
2321 }
2322
2323 bool AnimationBezierTrackEdit::_try_select_at_ui_pos(const Point2 &p_pos, bool p_aggregate, bool p_deselectable) {
2324 for (int i = 0; i < edit_points.size(); i++) {
2325 // Path 2D editing in the 3D and 2D editors works the same way. (?)
2326 if (edit_points[i].point_rect.has_point(p_pos)) {
2327 IntPair pair = IntPair(edit_points[i].track, edit_points[i].key);
2328 if (p_aggregate) {
2329 // Add to selection.
2330 if (selection.has(pair)) {
2331 if (p_deselectable) {
2332 selection.erase(pair);
2333 emit_signal(SNAME("deselect_key"), edit_points[i].key, edit_points[i].track);
2334 }
2335 } else {
2336 _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), false);
2337 }
2338 queue_redraw();
2339 select_single_attempt = IntPair(-1, -1);
2340 } else {
2341 if (p_deselectable) {
2342 moving_selection_attempt = true;
2343 moving_selection_from_key = pair.second;
2344 moving_selection_from_track = pair.first;
2345 moving_selection_mouse_begin = p_pos;
2346 moving_selection_offset = Vector2();
2347 moving_handle_track = pair.first;
2348 moving_handle_left = animation->bezier_track_get_key_in_handle(pair.first, pair.second);
2349 moving_handle_right = animation->bezier_track_get_key_out_handle(pair.first, pair.second);
2350
2351 if (selection.has(pair)) {
2352 moving_selection = false;
2353 } else {
2354 moving_selection = true;
2355 }
2356 select_single_attempt = pair;
2357 }
2358
2359 set_animation_and_track(animation, pair.first, read_only);
2360 if (!selection.has(pair)) {
2361 selection.clear();
2362 _select_at_anim(animation, edit_points[i].track, animation->track_get_key_time(edit_points[i].track, edit_points[i].key), true);
2363 }
2364 }
2365 return true;
2366 }
2367 }
2368 return false;
2369 }
2370
2371 void AnimationBezierTrackEdit::_pan_callback(Vector2 p_scroll_vec, Ref<InputEvent> p_event) {
2372 Ref<InputEventMouseMotion> mm = p_event;
2373 if (mm.is_valid()) {
2374 if (mm->get_position().x > timeline->get_name_limit()) {
2375 timeline_v_scroll += p_scroll_vec.y * timeline_v_zoom;
2376 timeline_v_scroll = CLAMP(timeline_v_scroll, -100000, 100000);
2377 timeline->set_value(timeline->get_value() - p_scroll_vec.x / timeline->get_zoom_scale());
2378 } else {
2379 track_v_scroll += p_scroll_vec.y;
2380 if (track_v_scroll < -track_v_scroll_max) {
2381 track_v_scroll = -track_v_scroll_max;
2382 } else if (track_v_scroll > 0) {
2383 track_v_scroll = 0;
2384 }
2385 }
2386 queue_redraw();
2387 }
2388 }
2389
2390 void AnimationBezierTrackEdit::_zoom_callback(float p_zoom_factor, Vector2 p_origin, Ref<InputEvent> p_event) {
2391 const float v_zoom_orig = timeline_v_zoom;
2392 Ref<InputEventWithModifiers> iewm = p_event;
2393 if (iewm.is_valid() && iewm->is_alt_pressed()) {
2394 // Alternate zoom (doesn't affect timeline).
2395 timeline_v_zoom = CLAMP(timeline_v_zoom / p_zoom_factor, 0.000001, 100000);
2396 } else {
2397 float zoom_factor = p_zoom_factor > 1.0 ? AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_IN : AnimationTimelineEdit::SCROLL_ZOOM_FACTOR_OUT;
2398 timeline->_zoom_callback(zoom_factor, p_origin, p_event);
2399 }
2400 timeline_v_scroll = timeline_v_scroll + (p_origin.y - get_size().y / 2.0) * (timeline_v_zoom - v_zoom_orig);
2401 queue_redraw();
2402 }
2403
2404 float AnimationBezierTrackEdit::get_bezier_key_value(Array p_bezier_key_array) {
2405 return p_bezier_key_array[0];
2406 }
2407
2408 void AnimationBezierTrackEdit::_menu_selected(int p_index) {
2409 int limit = timeline->get_name_limit();
2410
2411 real_t time = ((menu_insert_key.x - limit) / timeline->get_zoom_scale()) + timeline->get_value();
2412
2413 switch (p_index) {
2414 case MENU_KEY_INSERT: {
2415 if (animation->get_track_count() > 0) {
2416 if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
2417 time = editor->snap_time(time);
2418 }
2419 while (animation->track_find_key(selected_track, time, Animation::FIND_MODE_APPROX) != -1) {
2420 time += 0.001;
2421 }
2422 float h = (get_size().height / 2.0 - menu_insert_key.y) * timeline_v_zoom + timeline_v_scroll;
2423 Array new_point = animation->make_default_bezier_key(h);
2424 Animation::HandleMode handle_mode = (Animation::HandleMode)editor->bezier_key_mode->get_selected_id();
2425 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2426 undo_redo->create_action(TTR("Add Bezier Point"));
2427 undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, time, new_point);
2428 undo_redo->add_do_method(editor, "_bezier_track_set_key_handle_mode_at_time", animation.ptr(), selected_track, time, handle_mode, Animation::HANDLE_SET_MODE_AUTO);
2429 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
2430 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, time);
2431 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2432 if (ape) {
2433 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2434 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2435 }
2436 undo_redo->commit_action();
2437 queue_redraw();
2438 }
2439 } break;
2440 case MENU_KEY_DUPLICATE: {
2441 duplicate_selected_keys(time, true);
2442 } break;
2443 case MENU_KEY_DELETE: {
2444 delete_selection();
2445 } break;
2446 case MENU_KEY_CUT: {
2447 copy_selected_keys(true);
2448 } break;
2449 case MENU_KEY_COPY: {
2450 copy_selected_keys(false);
2451 } break;
2452 case MENU_KEY_PASTE: {
2453 paste_keys(time, true);
2454 } break;
2455 case MENU_KEY_SET_HANDLE_FREE: {
2456 _change_selected_keys_handle_mode(Animation::HANDLE_MODE_FREE);
2457 } break;
2458 case MENU_KEY_SET_HANDLE_LINEAR: {
2459 _change_selected_keys_handle_mode(Animation::HANDLE_MODE_LINEAR);
2460 } break;
2461 case MENU_KEY_SET_HANDLE_BALANCED: {
2462 _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED);
2463 } break;
2464 case MENU_KEY_SET_HANDLE_MIRRORED: {
2465 _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED);
2466 } break;
2467 case MENU_KEY_SET_HANDLE_AUTO_BALANCED: {
2468 _change_selected_keys_handle_mode(Animation::HANDLE_MODE_BALANCED, true);
2469 } break;
2470 case MENU_KEY_SET_HANDLE_AUTO_MIRRORED: {
2471 _change_selected_keys_handle_mode(Animation::HANDLE_MODE_MIRRORED, true);
2472 } break;
2473 }
2474 }
2475
2476 void AnimationBezierTrackEdit::duplicate_selected_keys(real_t p_ofs, bool p_ofs_valid) {
2477 if (selection.is_empty()) {
2478 return;
2479 }
2480
2481 real_t top_time = 1e10;
2482 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2483 real_t t = animation->track_get_key_time(E->get().first, E->get().second);
2484 if (t < top_time) {
2485 top_time = t;
2486 }
2487 }
2488
2489 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2490 undo_redo->create_action(TTR("Animation Duplicate Keys"));
2491
2492 List<Pair<int, real_t>> new_selection_values;
2493
2494 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2495 real_t t = animation->track_get_key_time(E->get().first, E->get().second);
2496 real_t insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
2497
2498 if (p_ofs_valid) {
2499 if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
2500 insert_pos = editor->snap_time(insert_pos);
2501 }
2502 }
2503
2504 real_t dst_time = t + (insert_pos - top_time);
2505 int existing_idx = animation->track_find_key(E->get().first, dst_time, Animation::FIND_MODE_APPROX);
2506
2507 undo_redo->add_do_method(animation.ptr(), "track_insert_key", E->get().first, dst_time, animation->track_get_key_value(E->get().first, E->get().second), animation->track_get_key_transition(E->get().first, E->get().second));
2508 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", E->get().first, dst_time);
2509
2510 Pair<int, real_t> p;
2511 p.first = E->get().first;
2512 p.second = dst_time;
2513 new_selection_values.push_back(p);
2514
2515 if (existing_idx != -1) {
2516 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->get().first, dst_time, animation->track_get_key_value(E->get().first, existing_idx), animation->track_get_key_transition(E->get().first, existing_idx));
2517 }
2518 }
2519
2520 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
2521 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
2522
2523 // Reselect duplicated.
2524 int i = 0;
2525 for (const Pair<int, real_t> &E : new_selection_values) {
2526 undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second, i == 0);
2527 i++;
2528 }
2529 i = 0;
2530 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2531 real_t time = animation->track_get_key_time(E->get().first, E->get().second);
2532 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, time, i == 0);
2533 i++;
2534 }
2535
2536 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2537 if (ape) {
2538 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2539 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2540 }
2541 undo_redo->add_do_method(this, "queue_redraw");
2542 undo_redo->add_undo_method(this, "queue_redraw");
2543 undo_redo->commit_action();
2544 }
2545
2546 void AnimationBezierTrackEdit::copy_selected_keys(bool p_cut) {
2547 if (selection.is_empty()) {
2548 return;
2549 }
2550
2551 float top_time = 1e10;
2552 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2553 float t = animation->track_get_key_time(E->get().first, E->get().second);
2554 if (t < top_time) {
2555 top_time = t;
2556 }
2557 }
2558
2559 RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo> keys;
2560 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2561 AnimationTrackEditor::SelectedKey sk;
2562 AnimationTrackEditor::KeyInfo ki;
2563 sk.track = E->get().first;
2564 sk.key = E->get().second;
2565 ki.pos = animation->track_get_key_time(E->get().first, E->get().second);
2566 keys.insert(sk, ki);
2567 }
2568 editor->_set_key_clipboard(selected_track, top_time, keys);
2569
2570 if (p_cut) {
2571 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2572 undo_redo->create_action(TTR("Animation Cut Keys"), UndoRedo::MERGE_DISABLE, animation.ptr());
2573 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
2574 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
2575 int i = 0;
2576 for (RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo>::Element *E = keys.back(); E; E = E->prev()) {
2577 int track_idx = E->key().track;
2578 int key_idx = E->key().key;
2579 float time = E->value().pos;
2580 undo_redo->add_do_method(animation.ptr(), "track_remove_key_at_time", track_idx, time);
2581 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));
2582 undo_redo->add_undo_method(this, "_select_at_anim", animation, track_idx, time, i == 0);
2583 i++;
2584 }
2585 i = 0;
2586 for (RBMap<AnimationTrackEditor::SelectedKey, AnimationTrackEditor::KeyInfo>::Element *E = keys.back(); E; E = E->prev()) {
2587 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->key().track, E->value().pos, i == 0);
2588 i++;
2589 }
2590
2591 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2592 if (ape) {
2593 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2594 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2595 }
2596 undo_redo->add_do_method(this, "queue_redraw");
2597 undo_redo->add_undo_method(this, "queue_redraw");
2598
2599 undo_redo->commit_action();
2600 }
2601 }
2602
2603 void AnimationBezierTrackEdit::paste_keys(real_t p_ofs, bool p_ofs_valid) {
2604 if (editor->is_key_clipboard_active() && animation.is_valid() && (selected_track >= 0 && selected_track < animation->get_track_count())) {
2605 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2606 undo_redo->create_action(TTR("Animation Paste Keys"));
2607
2608 bool same_track = true;
2609 bool all_compatible = true;
2610
2611 for (int i = 0; i < editor->key_clipboard.keys.size(); i++) {
2612 const AnimationTrackEditor::KeyClipboard::Key key = editor->key_clipboard.keys[i];
2613
2614 if (key.track != 0) {
2615 same_track = false;
2616 break;
2617 }
2618
2619 if (!editor->_is_track_compatible(selected_track, key.value.get_type(), key.track_type)) {
2620 all_compatible = false;
2621 break;
2622 }
2623 }
2624
2625 ERR_FAIL_COND_MSG(!all_compatible, "Paste failed: Not all animation keys were compatible with their target tracks");
2626 if (!same_track) {
2627 WARN_PRINT("Pasted animation keys from multiple tracks into single Bezier track");
2628 }
2629
2630 List<Pair<int, float>> new_selection_values;
2631 for (int i = 0; i < editor->key_clipboard.keys.size(); i++) {
2632 const AnimationTrackEditor::KeyClipboard::Key key = editor->key_clipboard.keys[i];
2633
2634 float insert_pos = p_ofs_valid ? p_ofs : timeline->get_play_position();
2635 if (p_ofs_valid) {
2636 if (editor->snap_keys->is_pressed() && editor->step->get_value() != 0) {
2637 insert_pos = editor->snap_time(insert_pos);
2638 }
2639 }
2640 float dst_time = key.time + insert_pos;
2641
2642 int existing_idx = animation->track_find_key(selected_track, dst_time, Animation::FIND_MODE_APPROX);
2643
2644 Variant value = key.value;
2645 if (key.track_type != Animation::TYPE_BEZIER) {
2646 value = animation->make_default_bezier_key(key.value);
2647 }
2648
2649 undo_redo->add_do_method(animation.ptr(), "track_insert_key", selected_track, dst_time, value, key.transition);
2650 undo_redo->add_undo_method(animation.ptr(), "track_remove_key_at_time", selected_track, dst_time);
2651
2652 Pair<int, float> p;
2653 p.first = selected_track;
2654 p.second = dst_time;
2655 new_selection_values.push_back(p);
2656
2657 if (existing_idx != -1) {
2658 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", selected_track, dst_time, animation->track_get_key_value(selected_track, existing_idx), animation->track_get_key_transition(selected_track, existing_idx));
2659 }
2660 }
2661
2662 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
2663 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
2664
2665 // Reselect pasted.
2666 int i = 0;
2667 for (const Pair<int, float> &E : new_selection_values) {
2668 undo_redo->add_do_method(this, "_select_at_anim", animation, E.first, E.second, i == 0);
2669 i++;
2670 }
2671 i = 0;
2672 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2673 undo_redo->add_undo_method(this, "_select_at_anim", animation, E->get().first, animation->track_get_key_time(E->get().first, E->get().second), i == 0);
2674 i++;
2675 }
2676
2677 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2678 if (ape) {
2679 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2680 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2681 }
2682 undo_redo->add_do_method(this, "queue_redraw");
2683 undo_redo->add_undo_method(this, "queue_redraw");
2684
2685 undo_redo->commit_action();
2686 }
2687 }
2688
2689 void AnimationBezierTrackEdit::delete_selection() {
2690 if (selection.size()) {
2691 EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
2692 undo_redo->create_action(TTR("Animation Delete Keys"));
2693
2694 for (SelectionSet::Element *E = selection.back(); E; E = E->prev()) {
2695 undo_redo->add_do_method(animation.ptr(), "track_remove_key", E->get().first, E->get().second);
2696 undo_redo->add_undo_method(animation.ptr(), "track_insert_key", E->get().first, animation->track_get_key_time(E->get().first, E->get().second), animation->track_get_key_value(E->get().first, E->get().second), 1);
2697 }
2698 undo_redo->add_do_method(this, "_clear_selection_for_anim", animation);
2699 undo_redo->add_undo_method(this, "_clear_selection_for_anim", animation);
2700 AnimationPlayerEditor *ape = AnimationPlayerEditor::get_singleton();
2701 if (ape) {
2702 undo_redo->add_do_method(ape, "_animation_update_key_frame");
2703 undo_redo->add_undo_method(ape, "_animation_update_key_frame");
2704 }
2705 undo_redo->commit_action();
2706
2707 //selection.clear();
2708 }
2709 }
2710
2711 void AnimationBezierTrackEdit::_bezier_track_insert_key_at_anim(const Ref<Animation> &p_anim, int p_track, double p_time, real_t p_value, const Vector2 &p_in_handle, const Vector2 &p_out_handle, const Animation::HandleMode p_handle_mode, Animation::HandleSetMode p_handle_set_mode) {
2712 int idx = p_anim->bezier_track_insert_key(p_track, p_time, p_value, p_in_handle, p_out_handle);
2713 p_anim->bezier_track_set_key_handle_mode(p_track, idx, p_handle_mode, p_handle_set_mode);
2714 }
2715
2716 void AnimationBezierTrackEdit::_bind_methods() {
2717 ClassDB::bind_method(D_METHOD("_clear_selection"), &AnimationBezierTrackEdit::_clear_selection);
2718 ClassDB::bind_method(D_METHOD("_clear_selection_for_anim"), &AnimationBezierTrackEdit::_clear_selection_for_anim);
2719 ClassDB::bind_method(D_METHOD("_select_at_anim"), &AnimationBezierTrackEdit::_select_at_anim);
2720 ClassDB::bind_method(D_METHOD("_update_hidden_tracks_after"), &AnimationBezierTrackEdit::_update_hidden_tracks_after);
2721 ClassDB::bind_method(D_METHOD("_update_locked_tracks_after"), &AnimationBezierTrackEdit::_update_locked_tracks_after);
2722 ClassDB::bind_method(D_METHOD("_lock_track"), &AnimationBezierTrackEdit::_lock_track);
2723 ClassDB::bind_method(D_METHOD("_hide_track"), &AnimationBezierTrackEdit::_hide_track);
2724 ClassDB::bind_method(D_METHOD("_bezier_track_insert_key_at_anim"), &AnimationBezierTrackEdit::_bezier_track_insert_key_at_anim, DEFVAL(Animation::HANDLE_SET_MODE_NONE));
2725
2726 ADD_SIGNAL(MethodInfo("select_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::BOOL, "single"), PropertyInfo(Variant::INT, "track")));
2727 ADD_SIGNAL(MethodInfo("deselect_key", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::INT, "track")));
2728 ADD_SIGNAL(MethodInfo("clear_selection"));
2729 ADD_SIGNAL(MethodInfo("timeline_changed", PropertyInfo(Variant::FLOAT, "position"), PropertyInfo(Variant::BOOL, "timeline_only")));
2730 }
2731
2732 AnimationBezierTrackEdit::AnimationBezierTrackEdit() {
2733 panner.instantiate();
2734 panner->set_callbacks(callable_mp(this, &AnimationBezierTrackEdit::_pan_callback), callable_mp(this, &AnimationBezierTrackEdit::_zoom_callback));
2735
2736 play_position = memnew(Control);
2737 play_position->set_mouse_filter(MOUSE_FILTER_PASS);
2738 add_child(play_position);
2739 play_position->set_anchors_and_offsets_preset(PRESET_FULL_RECT);
2740 play_position->connect(SceneStringName(draw), callable_mp(this, &AnimationBezierTrackEdit::_play_position_draw));
2741 set_focus_mode(FOCUS_CLICK);
2742
2743 set_clip_contents(true);
2744
2745 ED_SHORTCUT("animation_bezier_editor/focus", TTRC("Focus"), Key::F);
2746 ED_SHORTCUT("animation_bezier_editor/select_all_keys", TTRC("Select All Keys"), KeyModifierMask::CMD_OR_CTRL | Key::A);
2747 ED_SHORTCUT("animation_bezier_editor/deselect_all_keys", TTRC("Deselect All Keys"), KeyModifierMask::CMD_OR_CTRL | KeyModifierMask::SHIFT | Key::A);
2748
2749 menu = memnew(PopupMenu);
2750 add_child(menu);
2751 menu->connect(SceneStringName(id_pressed), callable_mp(this, &AnimationBezierTrackEdit::_menu_selected));
2752 }