Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/animation/animation_track_editor_plugins.cpp
9898 views
1
/**************************************************************************/
2
/* animation_track_editor_plugins.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "animation_track_editor_plugins.h"
32
33
#include "editor/audio/audio_stream_preview.h"
34
#include "editor/editor_string_names.h"
35
#include "editor/editor_undo_redo_manager.h"
36
#include "editor/inspector/editor_resource_preview.h"
37
#include "editor/themes/editor_scale.h"
38
#include "scene/2d/animated_sprite_2d.h"
39
#include "scene/2d/sprite_2d.h"
40
#include "scene/3d/sprite_3d.h"
41
#include "scene/animation/animation_player.h"
42
#include "servers/audio/audio_stream.h"
43
44
/// BOOL ///
45
int AnimationTrackEditBool::get_key_height() const {
46
Ref<Texture2D> checked = get_theme_icon(SNAME("checked"), SNAME("CheckBox"));
47
return checked->get_height();
48
}
49
50
Rect2 AnimationTrackEditBool::get_key_rect(int p_index, float p_pixels_sec) {
51
Ref<Texture2D> checked = get_theme_icon(SNAME("checked"), SNAME("CheckBox"));
52
return Rect2(-checked->get_width() / 2, 0, checked->get_width(), get_size().height);
53
}
54
55
bool AnimationTrackEditBool::is_key_selectable_by_distance() const {
56
return false;
57
}
58
59
void AnimationTrackEditBool::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
60
bool checked = get_animation()->track_get_key_value(get_track(), p_index);
61
Ref<Texture2D> icon = get_theme_icon(checked ? "checked" : "unchecked", "CheckBox");
62
63
Vector2 ofs(p_x - icon->get_width() / 2, int(get_size().height - icon->get_height()) / 2);
64
65
if (ofs.x + icon->get_width() / 2 < p_clip_left) {
66
return;
67
}
68
69
if (ofs.x + icon->get_width() / 2 > p_clip_right) {
70
return;
71
}
72
73
draw_texture(icon, ofs);
74
75
if (p_selected) {
76
Color color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
77
draw_rect_clipped(Rect2(ofs, icon->get_size()), color, false);
78
}
79
}
80
81
/// COLOR ///
82
83
int AnimationTrackEditColor::get_key_height() const {
84
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
85
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
86
return font->get_height(font_size) * 0.8;
87
}
88
89
Rect2 AnimationTrackEditColor::get_key_rect(int p_index, float p_pixels_sec) {
90
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
91
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
92
int fh = font->get_height(font_size) * 0.8;
93
return Rect2(-fh / 2, 0, fh, get_size().height);
94
}
95
96
bool AnimationTrackEditColor::is_key_selectable_by_distance() const {
97
return false;
98
}
99
100
void AnimationTrackEditColor::draw_key_link(int p_index, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right) {
101
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
102
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
103
int fh = (font->get_height(font_size) * 0.8);
104
105
fh /= 3;
106
107
int x_from = p_x + fh / 2 - 1;
108
int x_to = p_next_x - fh / 2 + 1;
109
x_from = MAX(x_from, p_clip_left);
110
x_to = MIN(x_to, p_clip_right);
111
112
int y_from = (get_size().height - fh) / 2;
113
114
if (x_from > p_clip_right || x_to < p_clip_left) {
115
return;
116
}
117
118
Vector<Color> color_samples;
119
color_samples.append(get_animation()->track_get_key_value(get_track(), p_index));
120
121
if (get_animation()->track_get_type(get_track()) == Animation::TYPE_VALUE) {
122
if (get_animation()->track_get_interpolation_type(get_track()) != Animation::INTERPOLATION_NEAREST &&
123
(get_animation()->value_track_get_update_mode(get_track()) == Animation::UPDATE_CONTINUOUS ||
124
get_animation()->value_track_get_update_mode(get_track()) == Animation::UPDATE_CAPTURE) &&
125
!Math::is_zero_approx(get_animation()->track_get_key_transition(get_track(), p_index))) {
126
float start_time = get_animation()->track_get_key_time(get_track(), p_index);
127
float end_time = get_animation()->track_get_key_time(get_track(), p_index + 1);
128
129
Color color_next = get_animation()->value_track_interpolate(get_track(), end_time);
130
131
if (!color_samples[0].is_equal_approx(color_next)) {
132
color_samples.resize(1 + (x_to - x_from) / 64); // Make a color sample every 64 px.
133
for (int i = 1; i < color_samples.size(); i++) {
134
float j = i;
135
color_samples.write[i] = get_animation()->value_track_interpolate(
136
get_track(),
137
Math::lerp(start_time, end_time, j / color_samples.size()));
138
}
139
}
140
color_samples.append(color_next);
141
} else {
142
color_samples.append(color_samples[0]);
143
}
144
} else {
145
color_samples.append(get_animation()->track_get_key_value(get_track(), p_index + 1));
146
}
147
148
for (int i = 0; i < color_samples.size() - 1; i++) {
149
Vector<Vector2> points = {
150
Vector2(Math::lerp(x_from, x_to, float(i) / (color_samples.size() - 1)), y_from),
151
Vector2(Math::lerp(x_from, x_to, float(i + 1) / (color_samples.size() - 1)), y_from),
152
Vector2(Math::lerp(x_from, x_to, float(i + 1) / (color_samples.size() - 1)), y_from + fh),
153
Vector2(Math::lerp(x_from, x_to, float(i) / (color_samples.size() - 1)), y_from + fh)
154
};
155
156
Vector<Color> colors = {
157
color_samples[i],
158
color_samples[i + 1],
159
color_samples[i + 1],
160
color_samples[i]
161
};
162
163
draw_primitive(points, colors, Vector<Vector2>());
164
}
165
}
166
167
void AnimationTrackEditColor::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
168
Color color = get_animation()->track_get_key_value(get_track(), p_index);
169
170
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
171
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
172
int fh = font->get_height(font_size) * 0.8;
173
174
Rect2 rect(Vector2(p_x - fh / 2, int(get_size().height - fh) / 2), Size2(fh, fh));
175
176
draw_rect_clipped(Rect2(rect.position, rect.size / 2), Color(0.4, 0.4, 0.4));
177
draw_rect_clipped(Rect2(rect.position + rect.size / 2, rect.size / 2), Color(0.4, 0.4, 0.4));
178
draw_rect_clipped(Rect2(rect.position + Vector2(rect.size.x / 2, 0), rect.size / 2), Color(0.6, 0.6, 0.6));
179
draw_rect_clipped(Rect2(rect.position + Vector2(0, rect.size.y / 2), rect.size / 2), Color(0.6, 0.6, 0.6));
180
draw_rect_clipped(rect, color);
181
182
if (p_selected) {
183
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
184
draw_rect_clipped(rect, accent, false);
185
}
186
}
187
188
/// AUDIO ///
189
190
void AnimationTrackEditAudio::_preview_changed(ObjectID p_which) {
191
Object *object = ObjectDB::get_instance(id);
192
193
if (!object) {
194
return;
195
}
196
197
Ref<AudioStream> stream = object->call("get_stream");
198
199
if (stream.is_valid() && stream->get_instance_id() == p_which) {
200
queue_redraw();
201
}
202
}
203
204
int AnimationTrackEditAudio::get_key_height() const {
205
if (!ObjectDB::get_instance(id)) {
206
return AnimationTrackEdit::get_key_height();
207
}
208
209
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
210
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
211
return int(font->get_height(font_size) * 1.5);
212
}
213
214
Rect2 AnimationTrackEditAudio::get_key_rect(int p_index, float p_pixels_sec) {
215
Object *object = ObjectDB::get_instance(id);
216
217
if (!object) {
218
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
219
}
220
221
Ref<AudioStream> stream = object->call("get_stream");
222
223
if (stream.is_null()) {
224
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
225
}
226
227
bool play = get_animation()->track_get_key_value(get_track(), p_index);
228
if (play) {
229
float len = stream->get_length();
230
231
if (len == 0) {
232
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
233
len = preview->get_length();
234
}
235
236
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
237
len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
238
}
239
240
return Rect2(0, 0, len * p_pixels_sec, get_size().height);
241
} else {
242
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
243
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
244
int fh = font->get_height(font_size) * 0.8;
245
return Rect2(0, 0, fh, get_size().height);
246
}
247
}
248
249
bool AnimationTrackEditAudio::is_key_selectable_by_distance() const {
250
return false;
251
}
252
253
void AnimationTrackEditAudio::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
254
Object *object = ObjectDB::get_instance(id);
255
256
if (!object) {
257
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
258
return;
259
}
260
261
Ref<AudioStream> stream = object->call("get_stream");
262
263
if (stream.is_null()) {
264
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
265
return;
266
}
267
268
bool play = get_animation()->track_get_key_value(get_track(), p_index);
269
if (play) {
270
float len = stream->get_length();
271
272
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
273
274
float preview_len = preview->get_length();
275
276
if (len == 0) {
277
len = preview_len;
278
}
279
280
int pixel_len = len * p_pixels_sec;
281
282
int pixel_begin = p_x;
283
int pixel_end = p_x + pixel_len;
284
285
if (pixel_end < p_clip_left) {
286
return;
287
}
288
289
if (pixel_begin > p_clip_right) {
290
return;
291
}
292
293
int from_x = MAX(pixel_begin, p_clip_left);
294
int to_x = MIN(pixel_end, p_clip_right);
295
296
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
297
float limit = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
298
int limit_x = pixel_begin + limit * p_pixels_sec;
299
to_x = MIN(limit_x, to_x);
300
}
301
302
if (to_x <= from_x) {
303
return;
304
}
305
306
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
307
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
308
float fh = int(font->get_height(font_size) * 1.5);
309
Rect2 rect = Rect2(from_x, (get_size().height - fh) / 2, to_x - from_x, fh);
310
draw_rect(rect, Color(0.25, 0.25, 0.25));
311
312
Vector<Vector2> points;
313
points.resize((to_x - from_x) * 2);
314
preview_len = preview->get_length();
315
316
for (int i = from_x; i < to_x; i++) {
317
float ofs = (i - pixel_begin) * preview_len / pixel_len;
318
float ofs_n = ((i + 1) - pixel_begin) * preview_len / pixel_len;
319
float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
320
float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
321
322
int idx = i - from_x;
323
points.write[idx * 2 + 0] = Vector2(i, rect.position.y + min * rect.size.y);
324
points.write[idx * 2 + 1] = Vector2(i, rect.position.y + max * rect.size.y);
325
}
326
327
Vector<Color> colors = { Color(0.75, 0.75, 0.75) };
328
329
RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
330
331
if (p_selected) {
332
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
333
draw_rect(rect, accent, false);
334
}
335
} else {
336
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
337
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
338
int fh = font->get_height(font_size) * 0.8;
339
Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
340
341
Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
342
draw_rect_clipped(rect, color);
343
344
if (p_selected) {
345
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
346
draw_rect_clipped(rect, accent, false);
347
}
348
}
349
}
350
351
void AnimationTrackEditAudio::set_node(Object *p_object) {
352
id = p_object->get_instance_id();
353
}
354
355
AnimationTrackEditAudio::AnimationTrackEditAudio() {
356
AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AnimationTrackEditAudio::_preview_changed));
357
}
358
359
/// SPRITE FRAME / FRAME_COORDS ///
360
361
int AnimationTrackEditSpriteFrame::get_key_height() const {
362
if (!ObjectDB::get_instance(id)) {
363
return AnimationTrackEdit::get_key_height();
364
}
365
366
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
367
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
368
return int(font->get_height(font_size) * 2);
369
}
370
371
Rect2 AnimationTrackEditSpriteFrame::get_key_rect(int p_index, float p_pixels_sec) {
372
Object *object = ObjectDB::get_instance(id);
373
374
if (!object) {
375
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
376
}
377
378
Size2 size;
379
380
if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
381
Ref<Texture2D> texture = object->call("get_texture");
382
if (texture.is_null()) {
383
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
384
}
385
386
size = texture->get_size();
387
388
if (bool(object->call("is_region_enabled"))) {
389
size = Rect2(object->call("get_region_rect")).size;
390
}
391
392
int hframes = object->call("get_hframes");
393
int vframes = object->call("get_vframes");
394
395
if (hframes > 1) {
396
size.x /= hframes;
397
}
398
if (vframes > 1) {
399
size.y /= vframes;
400
}
401
} else if (Object::cast_to<AnimatedSprite2D>(object) || Object::cast_to<AnimatedSprite3D>(object)) {
402
Ref<SpriteFrames> sf = object->call("get_sprite_frames");
403
if (sf.is_null()) {
404
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
405
}
406
407
List<StringName> animations;
408
sf->get_animation_list(&animations);
409
410
int frame = get_animation()->track_get_key_value(get_track(), p_index);
411
String animation_name;
412
if (animations.size() == 1) {
413
animation_name = animations.front()->get();
414
} else {
415
// Go through other track to find if animation is set
416
String animation_path = String(get_animation()->track_get_path(get_track()));
417
animation_path = animation_path.replace(":frame", ":animation");
418
int animation_track = get_animation()->find_track(animation_path, get_animation()->track_get_type(get_track()));
419
float track_time = get_animation()->track_get_key_time(get_track(), p_index);
420
int animation_index = get_animation()->track_find_key(animation_track, track_time);
421
animation_name = get_animation()->track_get_key_value(animation_track, animation_index);
422
}
423
424
Ref<Texture2D> texture = sf->get_frame_texture(animation_name, frame);
425
if (texture.is_null()) {
426
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
427
}
428
429
size = texture->get_size();
430
}
431
432
size = size.floor();
433
434
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
435
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
436
int height = int(font->get_height(font_size) * 2);
437
int width = height * size.width / size.height;
438
439
return Rect2(0, 0, width, get_size().height);
440
}
441
442
bool AnimationTrackEditSpriteFrame::is_key_selectable_by_distance() const {
443
return false;
444
}
445
446
void AnimationTrackEditSpriteFrame::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
447
Object *object = ObjectDB::get_instance(id);
448
449
if (!object) {
450
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
451
return;
452
}
453
454
Ref<Texture2D> texture;
455
Rect2 region;
456
457
if (Object::cast_to<Sprite2D>(object) || Object::cast_to<Sprite3D>(object)) {
458
texture = object->call("get_texture");
459
if (texture.is_null()) {
460
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
461
return;
462
}
463
464
int hframes = object->call("get_hframes");
465
int vframes = object->call("get_vframes");
466
467
Vector2 coords;
468
if (is_coords) {
469
coords = get_animation()->track_get_key_value(get_track(), p_index);
470
} else {
471
int frame = get_animation()->track_get_key_value(get_track(), p_index);
472
coords.x = frame % hframes;
473
coords.y = frame / hframes;
474
}
475
476
region.size = texture->get_size();
477
478
if (bool(object->call("is_region_enabled"))) {
479
region = Rect2(object->call("get_region_rect"));
480
}
481
482
if (hframes > 1) {
483
region.size.x /= hframes;
484
}
485
if (vframes > 1) {
486
region.size.y /= vframes;
487
}
488
489
region.position.x += region.size.x * coords.x;
490
region.position.y += region.size.y * coords.y;
491
492
} else if (Object::cast_to<AnimatedSprite2D>(object) || Object::cast_to<AnimatedSprite3D>(object)) {
493
Ref<SpriteFrames> sf = object->call("get_sprite_frames");
494
if (sf.is_null()) {
495
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
496
return;
497
}
498
499
List<StringName> animations;
500
sf->get_animation_list(&animations);
501
502
int frame = get_animation()->track_get_key_value(get_track(), p_index);
503
String animation_name;
504
if (animations.size() == 1) {
505
animation_name = animations.front()->get();
506
} else {
507
// Go through other track to find if animation is set
508
String animation_path = String(get_animation()->track_get_path(get_track()));
509
animation_path = animation_path.replace(":frame", ":animation");
510
int animation_track = get_animation()->find_track(animation_path, get_animation()->track_get_type(get_track()));
511
float track_time = get_animation()->track_get_key_time(get_track(), p_index);
512
int animation_index = get_animation()->track_find_key(animation_track, track_time);
513
animation_name = get_animation()->track_get_key_value(animation_track, animation_index);
514
}
515
516
texture = sf->get_frame_texture(animation_name, frame);
517
if (texture.is_null()) {
518
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
519
return;
520
}
521
522
region.size = texture->get_size();
523
}
524
525
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
526
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
527
int height = int(font->get_height(font_size) * 2);
528
529
int width = height * region.size.width / region.size.height;
530
531
Rect2 rect(p_x, int(get_size().height - height) / 2, width, height);
532
533
if (rect.position.x + rect.size.x < p_clip_left) {
534
return;
535
}
536
537
if (rect.position.x > p_clip_right) {
538
return;
539
}
540
541
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
542
Color bg = accent;
543
bg.a = 0.15;
544
545
draw_rect_clipped(rect, bg);
546
547
draw_texture_region_clipped(texture, rect, region);
548
549
if (p_selected) {
550
draw_rect_clipped(rect, accent, false);
551
}
552
}
553
554
void AnimationTrackEditSpriteFrame::set_node(Object *p_object) {
555
id = p_object->get_instance_id();
556
}
557
558
void AnimationTrackEditSpriteFrame::set_as_coords() {
559
is_coords = true;
560
}
561
562
/// SUB ANIMATION ///
563
564
int AnimationTrackEditSubAnim::get_key_height() const {
565
if (!ObjectDB::get_instance(id)) {
566
return AnimationTrackEdit::get_key_height();
567
}
568
569
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
570
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
571
return int(font->get_height(font_size) * 1.5);
572
}
573
574
Rect2 AnimationTrackEditSubAnim::get_key_rect(int p_index, float p_pixels_sec) {
575
Object *object = ObjectDB::get_instance(id);
576
577
if (!object) {
578
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
579
}
580
581
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
582
583
if (!ap) {
584
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
585
}
586
587
String anim = get_animation()->track_get_key_value(get_track(), p_index);
588
589
if (anim != "[stop]" && ap->has_animation(anim)) {
590
float len = ap->get_animation(anim)->get_length();
591
592
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
593
len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
594
}
595
596
return Rect2(0, 0, len * p_pixels_sec, get_size().height);
597
} else {
598
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
599
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
600
int fh = font->get_height(font_size) * 0.8;
601
return Rect2(0, 0, fh, get_size().height);
602
}
603
}
604
605
bool AnimationTrackEditSubAnim::is_key_selectable_by_distance() const {
606
return false;
607
}
608
609
void AnimationTrackEditSubAnim::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
610
Object *object = ObjectDB::get_instance(id);
611
612
if (!object) {
613
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
614
return;
615
}
616
617
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
618
619
if (!ap) {
620
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
621
return;
622
}
623
624
String anim = get_animation()->track_get_key_value(get_track(), p_index);
625
626
if (anim != "[stop]" && ap->has_animation(anim)) {
627
float len = ap->get_animation(anim)->get_length();
628
629
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
630
len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
631
}
632
633
int pixel_len = len * p_pixels_sec;
634
635
int pixel_begin = p_x;
636
int pixel_end = p_x + pixel_len;
637
638
if (pixel_end < p_clip_left) {
639
return;
640
}
641
642
if (pixel_begin > p_clip_right) {
643
return;
644
}
645
646
int from_x = MAX(pixel_begin, p_clip_left);
647
int to_x = MIN(pixel_end, p_clip_right);
648
649
if (to_x <= from_x) {
650
return;
651
}
652
653
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
654
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
655
int fh = font->get_height(font_size) * 1.5;
656
657
Rect2 rect(from_x, int(get_size().height - fh) / 2, to_x - from_x, fh);
658
659
Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
660
Color bg = color;
661
bg.r = 1 - color.r;
662
bg.g = 1 - color.g;
663
bg.b = 1 - color.b;
664
draw_rect(rect, bg);
665
666
Vector<Vector2> points;
667
Vector<Color> colors = { color };
668
{
669
Ref<Animation> ap_anim = ap->get_animation(anim);
670
671
for (int i = 0; i < ap_anim->get_track_count(); i++) {
672
float h = (rect.size.height - 2) / ap_anim->get_track_count();
673
674
int y = 2 + h * i + h / 2;
675
676
for (int j = 0; j < ap_anim->track_get_key_count(i); j++) {
677
float ofs = ap_anim->track_get_key_time(i, j);
678
int x = p_x + ofs * p_pixels_sec + 2;
679
680
if (x < from_x || x >= (to_x - 4)) {
681
continue;
682
}
683
684
points.push_back(Point2(x, y));
685
points.push_back(Point2(x + 1, y));
686
}
687
}
688
}
689
690
if (points.size() > 2) {
691
RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
692
}
693
694
int limit = to_x - from_x - 4;
695
if (limit > 0) {
696
draw_string(font, Point2(from_x + 2, int(get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size)), anim, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color);
697
}
698
699
if (p_selected) {
700
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
701
draw_rect(rect, accent, false);
702
}
703
} else {
704
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
705
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
706
int fh = font->get_height(font_size) * 0.8;
707
Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
708
709
Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
710
draw_rect_clipped(rect, color);
711
712
if (p_selected) {
713
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
714
draw_rect_clipped(rect, accent, false);
715
}
716
}
717
}
718
719
void AnimationTrackEditSubAnim::set_node(Object *p_object) {
720
id = p_object->get_instance_id();
721
}
722
723
//// VOLUME DB ////
724
725
int AnimationTrackEditVolumeDB::get_key_height() const {
726
Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
727
return volume_texture->get_height() * 1.2;
728
}
729
730
void AnimationTrackEditVolumeDB::draw_bg(int p_clip_left, int p_clip_right) {
731
Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
732
int tex_h = volume_texture->get_height();
733
734
int y_from = (get_size().height - tex_h) / 2;
735
int y_size = tex_h;
736
737
Color color(1, 1, 1, 0.3);
738
draw_texture_rect(volume_texture, Rect2(p_clip_left, y_from, p_clip_right - p_clip_left, y_from + y_size), false, color);
739
}
740
741
void AnimationTrackEditVolumeDB::draw_fg(int p_clip_left, int p_clip_right) {
742
Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
743
int tex_h = volume_texture->get_height();
744
int y_from = (get_size().height - tex_h) / 2;
745
int db0 = y_from + (24 / 80.0) * tex_h;
746
747
draw_line(Vector2(p_clip_left, db0), Vector2(p_clip_right, db0), Color(1, 1, 1, 0.3));
748
}
749
750
void AnimationTrackEditVolumeDB::draw_key_link(int p_index, float p_pixels_sec, int p_x, int p_next_x, int p_clip_left, int p_clip_right) {
751
if (p_x > p_clip_right || p_next_x < p_clip_left) {
752
return;
753
}
754
755
float db = get_animation()->track_get_key_value(get_track(), p_index);
756
float db_n = get_animation()->track_get_key_value(get_track(), p_index + 1);
757
758
db = CLAMP(db, -60, 24);
759
db_n = CLAMP(db_n, -60, 24);
760
761
float h = 1.0 - ((db + 60) / 84.0);
762
float h_n = 1.0 - ((db_n + 60) / 84.0);
763
764
int from_x = p_x;
765
int to_x = p_next_x;
766
767
if (from_x < p_clip_left) {
768
h = Math::lerp(h, h_n, float(p_clip_left - from_x) / float(to_x - from_x));
769
from_x = p_clip_left;
770
}
771
772
if (to_x > p_clip_right) {
773
h_n = Math::lerp(h, h_n, float(p_clip_right - from_x) / float(to_x - from_x));
774
to_x = p_clip_right;
775
}
776
777
Ref<Texture2D> volume_texture = get_editor_theme_icon(SNAME("ColorTrackVu"));
778
int tex_h = volume_texture->get_height();
779
780
int y_from = (get_size().height - tex_h) / 2;
781
782
Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
783
color.a *= 0.7;
784
785
draw_line(Point2(from_x, y_from + h * tex_h), Point2(to_x, y_from + h_n * tex_h), color, 2);
786
}
787
788
////////////////////////
789
790
/// AUDIO ///
791
792
void AnimationTrackEditTypeAudio::_preview_changed(ObjectID p_which) {
793
for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
794
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
795
if (stream.is_valid() && stream->get_instance_id() == p_which) {
796
queue_redraw();
797
return;
798
}
799
}
800
}
801
802
int AnimationTrackEditTypeAudio::get_key_height() const {
803
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
804
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
805
return int(font->get_height(font_size) * 1.5);
806
}
807
808
Rect2 AnimationTrackEditTypeAudio::get_key_rect(int p_index, float p_pixels_sec) {
809
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
810
811
if (stream.is_null()) {
812
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
813
}
814
815
float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), p_index);
816
float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), p_index);
817
818
float len = stream->get_length();
819
820
if (len == 0) {
821
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
822
len = preview->get_length();
823
}
824
825
len -= end_ofs;
826
len -= start_ofs;
827
if (len <= 0.0001) {
828
len = 0.0001;
829
}
830
831
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
832
len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
833
}
834
835
return Rect2(0, 0, len * p_pixels_sec, get_size().height);
836
}
837
838
bool AnimationTrackEditTypeAudio::is_key_selectable_by_distance() const {
839
return false;
840
}
841
842
void AnimationTrackEditTypeAudio::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
843
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), p_index);
844
if (stream.is_null()) {
845
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right); // Draw diamond.
846
return;
847
}
848
849
float len = stream->get_length();
850
if (len == 0) {
851
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right); // Draw diamond.
852
return;
853
}
854
855
float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), p_index);
856
float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), p_index);
857
858
int px_offset = 0;
859
if (len_resizing && p_index == len_resizing_index) {
860
float ofs_local = len_resizing_rel / get_timeline()->get_zoom_scale();
861
if (len_resizing_start) {
862
start_ofs += ofs_local;
863
px_offset = ofs_local * p_pixels_sec;
864
} else {
865
end_ofs -= ofs_local;
866
}
867
}
868
869
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
870
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
871
float fh = int(font->get_height(font_size) * 1.5);
872
873
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
874
875
int pixel_total_len = len * p_pixels_sec;
876
877
len -= end_ofs;
878
len -= start_ofs;
879
880
if (len <= 0.0001) {
881
len = 0.0001;
882
}
883
884
int pixel_len = len * p_pixels_sec;
885
886
int pixel_begin = px_offset + p_x;
887
int pixel_end = px_offset + p_x + pixel_len;
888
889
if (pixel_end < p_clip_left) {
890
return;
891
}
892
893
if (pixel_begin > p_clip_right) {
894
return;
895
}
896
897
int from_x = MAX(pixel_begin, p_clip_left);
898
int to_x = MIN(pixel_end, p_clip_right);
899
900
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
901
float limit = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
902
int limit_x = pixel_begin + limit * p_pixels_sec;
903
to_x = MIN(limit_x, to_x);
904
}
905
906
if (to_x <= from_x) {
907
to_x = from_x + 1;
908
}
909
910
int h = get_size().height;
911
Rect2 rect = Rect2(from_x, (h - fh) / 2, to_x - from_x, fh);
912
draw_rect(rect, Color(0.25, 0.25, 0.25));
913
914
Vector<Vector2> points;
915
points.resize((to_x - from_x) * 2);
916
float preview_len = preview->get_length();
917
918
for (int i = from_x; i < to_x; i++) {
919
float ofs = (i - pixel_begin) * preview_len / pixel_total_len;
920
float ofs_n = ((i + 1) - pixel_begin) * preview_len / pixel_total_len;
921
ofs += start_ofs;
922
ofs_n += start_ofs;
923
924
float max = preview->get_max(ofs, ofs_n) * 0.5 + 0.5;
925
float min = preview->get_min(ofs, ofs_n) * 0.5 + 0.5;
926
927
int idx = i - from_x;
928
points.write[idx * 2 + 0] = Vector2(i, rect.position.y + min * rect.size.y);
929
points.write[idx * 2 + 1] = Vector2(i, rect.position.y + max * rect.size.y);
930
}
931
932
Vector<Color> colors = { Color(0.75, 0.75, 0.75) };
933
934
RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
935
936
Color cut_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
937
cut_color.a = 0.7;
938
if (start_ofs > 0 && pixel_begin > p_clip_left) {
939
draw_rect(Rect2(pixel_begin, rect.position.y, 1, rect.size.y), cut_color);
940
}
941
if (end_ofs > 0 && pixel_end < p_clip_right) {
942
draw_rect(Rect2(pixel_end, rect.position.y, 1, rect.size.y), cut_color);
943
}
944
945
if (p_selected) {
946
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
947
draw_rect(rect, accent, false);
948
}
949
}
950
951
AnimationTrackEditTypeAudio::AnimationTrackEditTypeAudio() {
952
AudioStreamPreviewGenerator::get_singleton()->connect("preview_updated", callable_mp(this, &AnimationTrackEditTypeAudio::_preview_changed));
953
}
954
955
bool AnimationTrackEditTypeAudio::can_drop_data(const Point2 &p_point, const Variant &p_data) const {
956
if (p_point.x > get_timeline()->get_name_limit() && p_point.x < get_size().width - get_timeline()->get_buttons_width()) {
957
Dictionary drag_data = p_data;
958
if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
959
Ref<AudioStream> res = drag_data["resource"];
960
if (res.is_valid()) {
961
return true;
962
}
963
}
964
965
if (drag_data.has("type") && String(drag_data["type"]) == "files") {
966
Vector<String> files = drag_data["files"];
967
968
if (files.size() == 1) {
969
Ref<AudioStream> res = ResourceLoader::load(files[0]);
970
if (res.is_valid()) {
971
return true;
972
}
973
}
974
}
975
}
976
977
return AnimationTrackEdit::can_drop_data(p_point, p_data);
978
}
979
980
void AnimationTrackEditTypeAudio::drop_data(const Point2 &p_point, const Variant &p_data) {
981
if (p_point.x > get_timeline()->get_name_limit() && p_point.x < get_size().width - get_timeline()->get_buttons_width()) {
982
Ref<AudioStream> stream;
983
Dictionary drag_data = p_data;
984
if (drag_data.has("type") && String(drag_data["type"]) == "resource") {
985
stream = drag_data["resource"];
986
} else if (drag_data.has("type") && String(drag_data["type"]) == "files") {
987
Vector<String> files = drag_data["files"];
988
989
if (files.size() == 1) {
990
stream = ResourceLoader::load(files[0]);
991
}
992
}
993
994
if (stream.is_valid()) {
995
int x = p_point.x - get_timeline()->get_name_limit();
996
float ofs = x / get_timeline()->get_zoom_scale();
997
ofs += get_timeline()->get_value();
998
999
ofs = get_editor()->snap_time(ofs);
1000
1001
while (get_animation()->track_find_key(get_track(), ofs, Animation::FIND_MODE_APPROX) != -1) { //make sure insertion point is valid
1002
ofs += 0.0001;
1003
}
1004
1005
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1006
undo_redo->create_action(TTR("Add Audio Track Clip"));
1007
undo_redo->add_do_method(get_animation().ptr(), "audio_track_insert_key", get_track(), ofs, stream);
1008
undo_redo->add_undo_method(get_animation().ptr(), "track_remove_key_at_time", get_track(), ofs);
1009
undo_redo->commit_action();
1010
1011
queue_redraw();
1012
return;
1013
}
1014
}
1015
1016
AnimationTrackEdit::drop_data(p_point, p_data);
1017
}
1018
1019
void AnimationTrackEditTypeAudio::gui_input(const Ref<InputEvent> &p_event) {
1020
ERR_FAIL_COND(p_event.is_null());
1021
1022
Ref<InputEventMouseMotion> mm = p_event;
1023
if (!len_resizing && mm.is_valid()) {
1024
bool use_hsize_cursor = false;
1025
for (int i = 0; i < get_animation()->track_get_key_count(get_track()); i++) {
1026
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), i);
1027
1028
if (stream.is_null()) {
1029
continue;
1030
}
1031
1032
float len = stream->get_length();
1033
if (len == 0) {
1034
continue;
1035
}
1036
1037
float start_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), i);
1038
float end_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), i);
1039
len -= end_ofs;
1040
len -= start_ofs;
1041
1042
if (get_animation()->track_get_key_count(get_track()) > i + 1) {
1043
len = MIN(len, get_animation()->track_get_key_time(get_track(), i + 1) - get_animation()->track_get_key_time(get_track(), i));
1044
}
1045
1046
float ofs = get_animation()->track_get_key_time(get_track(), i);
1047
1048
ofs -= get_timeline()->get_value();
1049
ofs *= get_timeline()->get_zoom_scale();
1050
ofs += get_timeline()->get_name_limit();
1051
1052
int end = ofs + len * get_timeline()->get_zoom_scale();
1053
1054
if (end >= get_timeline()->get_name_limit() && end <= get_size().width - get_timeline()->get_buttons_width() && Math::abs(mm->get_position().x - end) < 5 * EDSCALE) {
1055
len_resizing_start = false;
1056
use_hsize_cursor = true;
1057
len_resizing_index = i;
1058
}
1059
1060
if (ofs >= get_timeline()->get_name_limit() && ofs <= get_size().width - get_timeline()->get_buttons_width() && Math::abs(mm->get_position().x - ofs) < 5 * EDSCALE) {
1061
len_resizing_start = true;
1062
use_hsize_cursor = true;
1063
len_resizing_index = i;
1064
}
1065
}
1066
over_drag_position = use_hsize_cursor;
1067
}
1068
1069
if (len_resizing && mm.is_valid()) {
1070
// Rezising index is some.
1071
len_resizing_rel += mm->get_relative().x;
1072
float ofs_local = len_resizing_rel / get_timeline()->get_zoom_scale();
1073
float prev_ofs_start = get_animation()->audio_track_get_key_start_offset(get_track(), len_resizing_index);
1074
float prev_ofs_end = get_animation()->audio_track_get_key_end_offset(get_track(), len_resizing_index);
1075
Ref<AudioStream> stream = get_animation()->audio_track_get_key_stream(get_track(), len_resizing_index);
1076
float len = stream->get_length();
1077
if (len == 0) {
1078
Ref<AudioStreamPreview> preview = AudioStreamPreviewGenerator::get_singleton()->generate_preview(stream);
1079
float preview_len = preview->get_length();
1080
len = preview_len;
1081
}
1082
1083
if (len_resizing_start) {
1084
len_resizing_rel = CLAMP(ofs_local, -prev_ofs_start, len - prev_ofs_end - prev_ofs_start) * get_timeline()->get_zoom_scale();
1085
} else {
1086
len_resizing_rel = CLAMP(ofs_local, -(len - prev_ofs_end - prev_ofs_start), prev_ofs_end) * get_timeline()->get_zoom_scale();
1087
}
1088
1089
queue_redraw();
1090
accept_event();
1091
return;
1092
}
1093
1094
Ref<InputEventMouseButton> mb = p_event;
1095
if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT && over_drag_position) {
1096
len_resizing = true;
1097
// In case if resizing index is not set yet reset the flag.
1098
if (len_resizing_index < 0) {
1099
len_resizing = false;
1100
return;
1101
}
1102
len_resizing_from_px = mb->get_position().x;
1103
len_resizing_rel = 0;
1104
queue_redraw();
1105
accept_event();
1106
return;
1107
}
1108
1109
EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
1110
if (len_resizing && mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == MouseButton::LEFT) {
1111
if (len_resizing_rel == 0 || len_resizing_index < 0) {
1112
len_resizing = false;
1113
return;
1114
}
1115
1116
if (len_resizing_start) {
1117
float ofs_local = len_resizing_rel / get_timeline()->get_zoom_scale();
1118
float prev_ofs = get_animation()->audio_track_get_key_start_offset(get_track(), len_resizing_index);
1119
float prev_time = get_animation()->track_get_key_time(get_track(), len_resizing_index);
1120
float new_ofs = prev_ofs + ofs_local;
1121
float new_time = prev_time + ofs_local;
1122
if (prev_time != new_time) {
1123
undo_redo->create_action(TTR("Change Audio Track Clip Start Offset"));
1124
1125
undo_redo->add_do_method(get_animation().ptr(), "track_set_key_time", get_track(), len_resizing_index, new_time);
1126
undo_redo->add_undo_method(get_animation().ptr(), "track_set_key_time", get_track(), len_resizing_index, prev_time);
1127
1128
undo_redo->add_do_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, new_ofs);
1129
undo_redo->add_undo_method(get_animation().ptr(), "audio_track_set_key_start_offset", get_track(), len_resizing_index, prev_ofs);
1130
1131
undo_redo->commit_action();
1132
}
1133
} else {
1134
float ofs_local = -len_resizing_rel / get_timeline()->get_zoom_scale();
1135
float prev_ofs = get_animation()->audio_track_get_key_end_offset(get_track(), len_resizing_index);
1136
float new_ofs = prev_ofs + ofs_local;
1137
if (prev_ofs != new_ofs) {
1138
undo_redo->create_action(TTR("Change Audio Track Clip End Offset"));
1139
undo_redo->add_do_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, new_ofs);
1140
undo_redo->add_undo_method(get_animation().ptr(), "audio_track_set_key_end_offset", get_track(), len_resizing_index, prev_ofs);
1141
undo_redo->commit_action();
1142
}
1143
}
1144
1145
len_resizing_index = -1;
1146
len_resizing = false;
1147
queue_redraw();
1148
accept_event();
1149
return;
1150
}
1151
1152
AnimationTrackEdit::gui_input(p_event);
1153
}
1154
1155
Control::CursorShape AnimationTrackEditTypeAudio::get_cursor_shape(const Point2 &p_pos) const {
1156
if (over_drag_position || len_resizing) {
1157
return Control::CURSOR_HSIZE;
1158
} else {
1159
return get_default_cursor_shape();
1160
}
1161
}
1162
1163
////////////////////
1164
/// SUB ANIMATION ///
1165
1166
int AnimationTrackEditTypeAnimation::get_key_height() const {
1167
if (!ObjectDB::get_instance(id)) {
1168
return AnimationTrackEdit::get_key_height();
1169
}
1170
1171
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1172
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1173
return int(font->get_height(font_size) * 1.5);
1174
}
1175
1176
Rect2 AnimationTrackEditTypeAnimation::get_key_rect(int p_index, float p_pixels_sec) {
1177
Object *object = ObjectDB::get_instance(id);
1178
1179
if (!object) {
1180
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
1181
}
1182
1183
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
1184
1185
if (!ap) {
1186
return AnimationTrackEdit::get_key_rect(p_index, p_pixels_sec);
1187
}
1188
1189
String anim = get_animation()->animation_track_get_key_animation(get_track(), p_index);
1190
1191
if (anim != "[stop]" && ap->has_animation(anim)) {
1192
float len = ap->get_animation(anim)->get_length();
1193
1194
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
1195
len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
1196
}
1197
1198
return Rect2(0, 0, len * p_pixels_sec, get_size().height);
1199
} else {
1200
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1201
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1202
int fh = font->get_height(font_size) * 0.8;
1203
return Rect2(0, 0, fh, get_size().height);
1204
}
1205
}
1206
1207
bool AnimationTrackEditTypeAnimation::is_key_selectable_by_distance() const {
1208
return false;
1209
}
1210
1211
void AnimationTrackEditTypeAnimation::draw_key(int p_index, float p_pixels_sec, int p_x, bool p_selected, int p_clip_left, int p_clip_right) {
1212
Object *object = ObjectDB::get_instance(id);
1213
1214
if (!object) {
1215
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
1216
return;
1217
}
1218
1219
AnimationPlayer *ap = Object::cast_to<AnimationPlayer>(object);
1220
1221
if (!ap) {
1222
AnimationTrackEdit::draw_key(p_index, p_pixels_sec, p_x, p_selected, p_clip_left, p_clip_right);
1223
return;
1224
}
1225
1226
String anim = get_animation()->animation_track_get_key_animation(get_track(), p_index);
1227
1228
if (anim != "[stop]" && ap->has_animation(anim)) {
1229
float len = ap->get_animation(anim)->get_length();
1230
1231
if (get_animation()->track_get_key_count(get_track()) > p_index + 1) {
1232
len = MIN(len, get_animation()->track_get_key_time(get_track(), p_index + 1) - get_animation()->track_get_key_time(get_track(), p_index));
1233
}
1234
1235
int pixel_len = len * p_pixels_sec;
1236
1237
int pixel_begin = p_x;
1238
int pixel_end = p_x + pixel_len;
1239
1240
if (pixel_end < p_clip_left) {
1241
return;
1242
}
1243
1244
if (pixel_begin > p_clip_right) {
1245
return;
1246
}
1247
1248
int from_x = MAX(pixel_begin, p_clip_left);
1249
int to_x = MIN(pixel_end, p_clip_right);
1250
1251
if (to_x <= from_x) {
1252
return;
1253
}
1254
1255
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1256
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1257
int fh = font->get_height(font_size) * 1.5;
1258
1259
Rect2 rect(from_x, int(get_size().height - fh) / 2, to_x - from_x, fh);
1260
1261
Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
1262
Color bg = color;
1263
bg.r = 1 - color.r;
1264
bg.g = 1 - color.g;
1265
bg.b = 1 - color.b;
1266
draw_rect(rect, bg);
1267
1268
Vector<Vector2> points;
1269
Vector<Color> colors = { color };
1270
{
1271
Ref<Animation> ap_anim = ap->get_animation(anim);
1272
1273
for (int i = 0; i < ap_anim->get_track_count(); i++) {
1274
float h = (rect.size.height - 2) / ap_anim->get_track_count();
1275
1276
int y = 2 + h * i + h / 2;
1277
1278
for (int j = 0; j < ap_anim->track_get_key_count(i); j++) {
1279
float ofs = ap_anim->track_get_key_time(i, j);
1280
int x = p_x + ofs * p_pixels_sec + 2;
1281
1282
if (x < from_x || x >= (to_x - 4)) {
1283
continue;
1284
}
1285
1286
points.push_back(Point2(x, y));
1287
points.push_back(Point2(x + 1, y));
1288
}
1289
}
1290
}
1291
1292
if (points.size() > 2) {
1293
RS::get_singleton()->canvas_item_add_multiline(get_canvas_item(), points, colors);
1294
}
1295
1296
int limit = to_x - from_x - 4;
1297
if (limit > 0) {
1298
draw_string(font, Point2(from_x + 2, int(get_size().height - font->get_height(font_size)) / 2 + font->get_ascent(font_size)), anim, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, color);
1299
}
1300
1301
if (p_selected) {
1302
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
1303
draw_rect(rect, accent, false);
1304
}
1305
} else {
1306
Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
1307
int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
1308
int fh = font->get_height(font_size) * 0.8;
1309
Rect2 rect(Vector2(p_x, int(get_size().height - fh) / 2), Size2(fh, fh));
1310
1311
Color color = get_theme_color(SceneStringName(font_color), SNAME("Label"));
1312
draw_rect_clipped(rect, color);
1313
1314
if (p_selected) {
1315
Color accent = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
1316
draw_rect_clipped(rect, accent, false);
1317
}
1318
}
1319
}
1320
1321
void AnimationTrackEditTypeAnimation::set_node(Object *p_object) {
1322
id = p_object->get_instance_id();
1323
}
1324
1325
/////////
1326
AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_value_track_edit(Object *p_object, Variant::Type p_type, const String &p_property, PropertyHint p_hint, const String &p_hint_string, int p_usage) {
1327
if (p_property == "playing" && (p_object->is_class("AudioStreamPlayer") || p_object->is_class("AudioStreamPlayer2D") || p_object->is_class("AudioStreamPlayer3D"))) {
1328
AnimationTrackEditAudio *audio = memnew(AnimationTrackEditAudio);
1329
audio->set_node(p_object);
1330
return audio;
1331
}
1332
1333
if (p_property == "frame" && (p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D") || p_object->is_class("AnimatedSprite2D") || p_object->is_class("AnimatedSprite3D"))) {
1334
AnimationTrackEditSpriteFrame *sprite = memnew(AnimationTrackEditSpriteFrame);
1335
sprite->set_node(p_object);
1336
return sprite;
1337
}
1338
1339
if (p_property == "frame_coords" && (p_object->is_class("Sprite2D") || p_object->is_class("Sprite3D"))) {
1340
AnimationTrackEditSpriteFrame *sprite = memnew(AnimationTrackEditSpriteFrame);
1341
sprite->set_as_coords();
1342
sprite->set_node(p_object);
1343
return sprite;
1344
}
1345
1346
if (p_property == "current_animation" && (p_object->is_class("AnimationPlayer"))) {
1347
AnimationTrackEditSubAnim *player = memnew(AnimationTrackEditSubAnim);
1348
player->set_node(p_object);
1349
return player;
1350
}
1351
1352
if (p_property == "volume_db") {
1353
AnimationTrackEditVolumeDB *vu = memnew(AnimationTrackEditVolumeDB);
1354
return vu;
1355
}
1356
1357
if (p_type == Variant::BOOL) {
1358
return memnew(AnimationTrackEditBool);
1359
}
1360
if (p_type == Variant::COLOR) {
1361
return memnew(AnimationTrackEditColor);
1362
}
1363
1364
return nullptr;
1365
}
1366
1367
AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_audio_track_edit() {
1368
return memnew(AnimationTrackEditTypeAudio);
1369
}
1370
1371
AnimationTrackEdit *AnimationTrackEditDefaultPlugin::create_animation_track_edit(Object *p_object) {
1372
AnimationTrackEditTypeAnimation *an = memnew(AnimationTrackEditTypeAnimation);
1373
an->set_node(p_object);
1374
return an;
1375
}
1376
1377