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