Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/animation/animation_player.cpp
9902 views
1
/**************************************************************************/
2
/* animation_player.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "animation_player.h"
32
#include "animation_player.compat.inc"
33
34
#include "core/config/engine.h"
35
36
bool AnimationPlayer::_set(const StringName &p_name, const Variant &p_value) {
37
String name = p_name;
38
if (name.begins_with("playback/play")) { // For backward compatibility.
39
set_current_animation(p_value);
40
} else if (name.begins_with("next/")) {
41
String which = name.get_slicec('/', 1);
42
animation_set_next(which, p_value);
43
} else if (p_name == SceneStringName(blend_times)) {
44
Array array = p_value;
45
int len = array.size();
46
ERR_FAIL_COND_V(len % 3, false);
47
48
for (int i = 0; i < len / 3; i++) {
49
StringName from = array[i * 3 + 0];
50
StringName to = array[i * 3 + 1];
51
float time = array[i * 3 + 2];
52
set_blend_time(from, to, time);
53
}
54
#ifndef DISABLE_DEPRECATED
55
} else if (p_name == "method_call_mode") {
56
set_callback_mode_method(static_cast<AnimationCallbackModeMethod>((int)p_value));
57
} else if (p_name == "playback_process_mode") {
58
set_callback_mode_process(static_cast<AnimationCallbackModeProcess>((int)p_value));
59
} else if (p_name == "playback_active") {
60
set_active(p_value);
61
#endif // DISABLE_DEPRECATED
62
} else {
63
return false;
64
}
65
return true;
66
}
67
68
bool AnimationPlayer::_get(const StringName &p_name, Variant &r_ret) const {
69
String name = p_name;
70
71
if (name == "playback/play") { // For backward compatibility.
72
73
r_ret = get_current_animation();
74
75
} else if (name.begins_with("next/")) {
76
String which = name.get_slicec('/', 1);
77
r_ret = animation_get_next(which);
78
79
} else if (p_name == SceneStringName(blend_times)) {
80
Vector<BlendKey> keys;
81
for (const KeyValue<BlendKey, double> &E : blend_times) {
82
keys.ordered_insert(E.key);
83
}
84
85
Array array;
86
for (int i = 0; i < keys.size(); i++) {
87
array.push_back(keys[i].from);
88
array.push_back(keys[i].to);
89
array.push_back(blend_times.get(keys[i]));
90
}
91
92
r_ret = array;
93
#ifndef DISABLE_DEPRECATED
94
} else if (name == "method_call_mode") {
95
r_ret = get_callback_mode_method();
96
} else if (name == "playback_process_mode") {
97
r_ret = get_callback_mode_process();
98
} else if (name == "playback_active") {
99
r_ret = is_active();
100
#endif // DISABLE_DEPRECATED
101
} else {
102
return false;
103
}
104
105
return true;
106
}
107
108
void AnimationPlayer::_validate_property(PropertyInfo &p_property) const {
109
if (Engine::get_singleton()->is_editor_hint() && p_property.name == "current_animation") {
110
List<String> names;
111
112
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
113
names.push_back(E.key);
114
}
115
names.push_front("[stop]");
116
String hint;
117
for (List<String>::Element *E = names.front(); E; E = E->next()) {
118
if (E != names.front()) {
119
hint += ",";
120
}
121
hint += E->get();
122
}
123
124
p_property.hint_string = hint;
125
} else if (!auto_capture && p_property.name.begins_with("playback_auto_capture_")) {
126
p_property.usage = PROPERTY_USAGE_NONE;
127
}
128
}
129
130
void AnimationPlayer::_get_property_list(List<PropertyInfo> *p_list) const {
131
List<PropertyInfo> anim_names;
132
133
for (const KeyValue<StringName, AnimationData> &E : animation_set) {
134
AHashMap<StringName, StringName>::ConstIterator F = animation_next_set.find(E.key);
135
if (F && F->value != StringName()) {
136
anim_names.push_back(PropertyInfo(Variant::STRING, "next/" + String(E.key), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
137
}
138
}
139
140
for (const PropertyInfo &E : anim_names) {
141
p_list->push_back(E);
142
}
143
144
p_list->push_back(PropertyInfo(Variant::ARRAY, "blend_times", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL));
145
}
146
147
void AnimationPlayer::_notification(int p_what) {
148
switch (p_what) {
149
case NOTIFICATION_READY: {
150
if (!Engine::get_singleton()->is_editor_hint() && animation_set.has(autoplay)) {
151
set_active(active);
152
play(autoplay);
153
_check_immediately_after_start();
154
}
155
} break;
156
}
157
}
158
159
void AnimationPlayer::_process_playback_data(PlaybackData &cd, double p_delta, float p_blend, bool p_seeked, bool p_internal_seeked, bool p_started, bool p_is_current) {
160
double speed = speed_scale * cd.speed_scale;
161
bool backwards = std::signbit(speed); // Negative zero means playing backwards too.
162
double delta = p_started ? 0 : p_delta * speed;
163
double next_pos = cd.pos + delta;
164
165
double start = cd.get_start_time();
166
double end = cd.get_end_time();
167
168
Animation::LoopedFlag looped_flag = Animation::LOOPED_FLAG_NONE;
169
170
switch (cd.from->animation->get_loop_mode()) {
171
case Animation::LOOP_NONE: {
172
if (Animation::is_less_approx(next_pos, start)) {
173
next_pos = start;
174
} else if (Animation::is_greater_approx(next_pos, end)) {
175
next_pos = end;
176
}
177
delta = next_pos - cd.pos; // Fix delta (after determination of backwards because negative zero is lost here).
178
} break;
179
180
case Animation::LOOP_LINEAR: {
181
if (Animation::is_less_approx(next_pos, start) && Animation::is_greater_or_equal_approx(cd.pos, start)) {
182
looped_flag = Animation::LOOPED_FLAG_START;
183
}
184
if (Animation::is_greater_approx(next_pos, end) && Animation::is_less_or_equal_approx(cd.pos, end)) {
185
looped_flag = Animation::LOOPED_FLAG_END;
186
}
187
next_pos = Math::fposmod(next_pos - start, end - start) + start;
188
} break;
189
190
case Animation::LOOP_PINGPONG: {
191
if (Animation::is_less_approx(next_pos, start) && Animation::is_greater_or_equal_approx(cd.pos, start)) {
192
cd.speed_scale *= -1.0;
193
looped_flag = Animation::LOOPED_FLAG_START;
194
}
195
if (Animation::is_greater_approx(next_pos, end) && Animation::is_less_or_equal_approx(cd.pos, end)) {
196
cd.speed_scale *= -1.0;
197
looped_flag = Animation::LOOPED_FLAG_END;
198
}
199
next_pos = Math::pingpong(next_pos - start, end - start) + start;
200
} break;
201
202
default:
203
break;
204
}
205
206
double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.
207
208
// End detection.
209
if (p_is_current) {
210
if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {
211
if (!backwards && Animation::is_less_or_equal_approx(prev_pos, end) && Math::is_equal_approx(next_pos, end)) {
212
// Playback finished.
213
next_pos = end; // Snap to the edge.
214
end_reached = true;
215
end_notify = Animation::is_less_approx(prev_pos, end); // Notify only if not already at the end.
216
p_blend = 1.0;
217
}
218
if (backwards && Animation::is_greater_or_equal_approx(prev_pos, start) && Math::is_equal_approx(next_pos, start)) {
219
// Playback finished.
220
next_pos = start; // Snap to the edge.
221
end_reached = true;
222
end_notify = Animation::is_greater_approx(prev_pos, start); // Notify only if not already at the beginning.
223
p_blend = 1.0;
224
}
225
}
226
}
227
228
cd.pos = next_pos;
229
230
PlaybackInfo pi;
231
if (p_started) {
232
pi.time = prev_pos;
233
pi.delta = 0;
234
pi.start = start;
235
pi.end = end;
236
pi.seeked = true;
237
} else {
238
pi.time = next_pos;
239
pi.delta = delta;
240
pi.start = start;
241
pi.end = end;
242
pi.seeked = p_seeked;
243
}
244
if (Math::is_zero_approx(pi.delta) && backwards) {
245
pi.delta = -0.0; // Sign is needed to handle converted Continuous track from Discrete track correctly.
246
}
247
// Immediately after playback, discrete keys should be retrieved with EXACT mode since behind keys must be ignored at that time.
248
pi.is_external_seeking = !p_internal_seeked && !p_started;
249
pi.looped_flag = looped_flag;
250
pi.weight = p_blend;
251
make_animation_instance(cd.from->name, pi);
252
}
253
254
float AnimationPlayer::get_current_blend_amount() {
255
Playback &c = playback;
256
float blend = 1.0;
257
for (const Blend &b : c.blend) {
258
blend = blend - b.blend_left;
259
}
260
return MAX(0, blend);
261
}
262
263
void AnimationPlayer::_blend_playback_data(double p_delta, bool p_started) {
264
Playback &c = playback;
265
266
bool seeked = c.seeked; // The animation may be changed during process, so it is safer that the state is changed before process.
267
bool internal_seeked = c.internal_seeked;
268
269
if (!Math::is_zero_approx(p_delta)) {
270
c.seeked = false;
271
c.internal_seeked = false;
272
}
273
274
// Second, process current animation to check if the animation end reached.
275
_process_playback_data(c.current, p_delta, get_current_blend_amount(), seeked, internal_seeked, p_started, true);
276
277
// Finally, if not end the animation, do blending.
278
if (end_reached) {
279
playback.blend.clear();
280
return;
281
}
282
List<List<Blend>::Element *> to_erase;
283
for (List<Blend>::Element *E = c.blend.front(); E; E = E->next()) {
284
Blend &b = E->get();
285
b.blend_left = MAX(0, b.blend_left - Math::abs(speed_scale * p_delta) / b.blend_time);
286
if (Animation::is_less_or_equal_approx(b.blend_left, 0)) {
287
to_erase.push_back(E);
288
b.blend_left = CMP_EPSILON; // May want to play last frame.
289
}
290
// Note: There may be issues if an animation event triggers an animation change while this blend is active,
291
// so it is best to use "deferred" calls instead of "immediate" for animation events that can trigger new animations.
292
_process_playback_data(b.data, p_delta, b.blend_left, false, false, false);
293
}
294
for (List<Blend>::Element *&E : to_erase) {
295
c.blend.erase(E);
296
}
297
}
298
299
bool AnimationPlayer::_blend_pre_process(double p_delta, int p_track_count, const AHashMap<NodePath, int> &p_track_map) {
300
if (!playback.current.from) {
301
_set_process(false);
302
return false;
303
}
304
305
tmp_from = playback.current.from->animation->get_instance_id();
306
end_reached = false;
307
end_notify = false;
308
309
bool started = playback.started; // The animation may be changed during process, so it is safer that the state is changed before process.
310
if (playback.started) {
311
playback.started = false;
312
}
313
314
AnimationData *prev_from = playback.current.from;
315
_blend_playback_data(p_delta, started);
316
317
if (prev_from != playback.current.from) {
318
return false; // Animation has been changed in the process (may be caused by method track), abort process.
319
}
320
321
return true;
322
}
323
324
void AnimationPlayer::_blend_capture(double p_delta) {
325
blend_capture(p_delta * Math::abs(speed_scale));
326
}
327
328
void AnimationPlayer::_blend_post_process() {
329
if (end_reached) {
330
// If the method track changes current animation, the animation is not finished.
331
if (tmp_from == playback.current.from->animation->get_instance_id()) {
332
if (playback_queue.size()) {
333
String old = playback.assigned;
334
play(playback_queue.front()->get());
335
String new_name = playback.assigned;
336
playback_queue.pop_front();
337
if (end_notify) {
338
emit_signal(SceneStringName(animation_changed), old, new_name);
339
}
340
} else {
341
_clear_caches();
342
playing = false;
343
_set_process(false);
344
if (end_notify) {
345
emit_signal(SceneStringName(animation_finished), playback.assigned);
346
emit_signal(SNAME("current_animation_changed"), "");
347
if (movie_quit_on_finish && OS::get_singleton()->has_feature("movie")) {
348
print_line(vformat("Movie Maker mode is enabled. Quitting on animation finish as requested by: %s", get_path()));
349
get_tree()->quit();
350
}
351
}
352
}
353
}
354
end_reached = false;
355
end_notify = false;
356
}
357
tmp_from = ObjectID();
358
}
359
360
void AnimationPlayer::queue(const StringName &p_name) {
361
if (!is_playing()) {
362
play(p_name);
363
} else {
364
playback_queue.push_back(p_name);
365
}
366
}
367
368
Vector<String> AnimationPlayer::get_queue() {
369
Vector<String> ret;
370
for (const StringName &E : playback_queue) {
371
ret.push_back(E);
372
}
373
374
return ret;
375
}
376
377
void AnimationPlayer::clear_queue() {
378
playback_queue.clear();
379
}
380
381
void AnimationPlayer::play_backwards(const StringName &p_name, double p_custom_blend) {
382
play(p_name, p_custom_blend, -1, true);
383
}
384
385
void AnimationPlayer::play_section_with_markers_backwards(const StringName &p_name, const StringName &p_start_marker, const StringName &p_end_marker, double p_custom_blend) {
386
play_section_with_markers(p_name, p_start_marker, p_end_marker, p_custom_blend, -1, true);
387
}
388
389
void AnimationPlayer::play_section_backwards(const StringName &p_name, double p_start_time, double p_end_time, double p_custom_blend) {
390
play_section(p_name, p_start_time, p_end_time, p_custom_blend, -1, true);
391
}
392
393
void AnimationPlayer::play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
394
if (auto_capture) {
395
play_with_capture(p_name, auto_capture_duration, p_custom_blend, p_custom_scale, p_from_end, auto_capture_transition_type, auto_capture_ease_type);
396
} else {
397
_play(p_name, p_custom_blend, p_custom_scale, p_from_end);
398
}
399
}
400
401
void AnimationPlayer::_play(const StringName &p_name, double p_custom_blend, float p_custom_scale, bool p_from_end) {
402
play_section_with_markers(p_name, StringName(), StringName(), p_custom_blend, p_custom_scale, p_from_end);
403
}
404
405
void AnimationPlayer::play_section_with_markers(const StringName &p_name, const StringName &p_start_marker, const StringName &p_end_marker, double p_custom_blend, float p_custom_scale, bool p_from_end) {
406
StringName name = p_name;
407
408
if (name == StringName()) {
409
name = playback.assigned;
410
}
411
412
ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
413
414
Ref<Animation> animation = animation_set[name].animation;
415
416
ERR_FAIL_COND_MSG(p_start_marker == p_end_marker && p_start_marker, vformat("Start marker and end marker cannot be the same marker: %s.", p_start_marker));
417
ERR_FAIL_COND_MSG(p_start_marker && !animation->has_marker(p_start_marker), vformat("Marker %s not found in animation: %s.", p_start_marker, name));
418
ERR_FAIL_COND_MSG(p_end_marker && !animation->has_marker(p_end_marker), vformat("Marker %s not found in animation: %s.", p_end_marker, name));
419
420
double start_time = p_start_marker ? animation->get_marker_time(p_start_marker) : -1;
421
double end_time = p_end_marker ? animation->get_marker_time(p_end_marker) : -1;
422
423
ERR_FAIL_COND_MSG(p_start_marker && p_end_marker && Animation::is_greater_approx(start_time, end_time), vformat("End marker %s is placed earlier than start marker %s in animation: %s.", p_end_marker, p_start_marker, name));
424
425
if (p_start_marker && Animation::is_less_approx(start_time, 0)) {
426
WARN_PRINT_ED(vformat("Negative time start marker: %s is invalid in the section, so the start of the animation: %s is used instead.", p_start_marker, playback.current.from->animation->get_name()));
427
}
428
if (p_end_marker && Animation::is_less_approx(end_time, 0)) {
429
WARN_PRINT_ED(vformat("Negative time end marker: %s is invalid in the section, so the end of the animation: %s is used instead.", p_end_marker, playback.current.from->animation->get_name()));
430
}
431
432
play_section(name, start_time, end_time, p_custom_blend, p_custom_scale, p_from_end);
433
}
434
435
void AnimationPlayer::play_section(const StringName &p_name, double p_start_time, double p_end_time, double p_custom_blend, float p_custom_scale, bool p_from_end) {
436
StringName name = p_name;
437
438
if (name == StringName()) {
439
name = playback.assigned;
440
}
441
442
ERR_FAIL_COND_MSG(!animation_set.has(name), vformat("Animation not found: %s.", name));
443
ERR_FAIL_COND_MSG(p_start_time >= 0 && p_end_time >= 0 && Math::is_equal_approx(p_start_time, p_end_time), "Start time and end time must not equal to each other.");
444
ERR_FAIL_COND_MSG(p_start_time >= 0 && p_end_time >= 0 && Animation::is_greater_approx(p_start_time, p_end_time), vformat("Start time %f is greater than end time %f.", p_start_time, p_end_time));
445
446
Playback &c = playback;
447
448
if (c.current.from) {
449
double blend_time = 0.0;
450
// Find if it can blend.
451
BlendKey bk;
452
bk.from = c.current.from->name;
453
bk.to = name;
454
455
if (Animation::is_greater_or_equal_approx(p_custom_blend, 0)) {
456
blend_time = p_custom_blend;
457
} else if (blend_times.has(bk)) {
458
blend_time = blend_times[bk];
459
} else {
460
bk.from = "*";
461
if (blend_times.has(bk)) {
462
blend_time = blend_times[bk];
463
} else {
464
bk.from = c.current.from->name;
465
bk.to = "*";
466
467
if (blend_times.has(bk)) {
468
blend_time = blend_times[bk];
469
}
470
}
471
}
472
473
if (Animation::is_less_approx(p_custom_blend, 0) && Math::is_zero_approx(blend_time) && default_blend_time) {
474
blend_time = default_blend_time;
475
}
476
if (Animation::is_greater_approx(blend_time, 0)) {
477
Blend b;
478
b.data = c.current;
479
b.blend_left = get_current_blend_amount();
480
b.blend_time = blend_time;
481
c.blend.push_back(b);
482
} else {
483
c.blend.clear();
484
}
485
}
486
487
if (get_current_animation() != p_name) {
488
_clear_playing_caches();
489
}
490
491
c.current.from = &animation_set[name];
492
c.current.speed_scale = p_custom_scale;
493
c.current.start_time = p_start_time;
494
c.current.end_time = p_end_time;
495
496
double start = playback.current.get_start_time();
497
double end = playback.current.get_end_time();
498
499
if (!end_reached) {
500
playback_queue.clear();
501
}
502
503
if (c.assigned != name) { // Reset.
504
c.current.pos = p_from_end ? end : start;
505
c.assigned = name;
506
emit_signal(SNAME("current_animation_changed"), c.assigned);
507
} else {
508
if (p_from_end && Math::is_equal_approx(c.current.pos, start)) {
509
// Animation reset but played backwards, set position to the end.
510
seek_internal(end, true, true, true);
511
} else if (!p_from_end && Math::is_equal_approx(c.current.pos, end)) {
512
// Animation resumed but already ended, set position to the beginning.
513
seek_internal(start, true, true, true);
514
} else if (playing) {
515
return;
516
}
517
}
518
519
c.seeked = false;
520
c.started = true;
521
522
_set_process(true); // Always process when starting an animation.
523
playing = true;
524
525
emit_signal(SceneStringName(animation_started), c.assigned);
526
527
if (is_inside_tree() && Engine::get_singleton()->is_editor_hint()) {
528
return; // No next in this case.
529
}
530
531
StringName next = animation_get_next(p_name);
532
if (next != StringName() && animation_set.has(next)) {
533
queue(next);
534
}
535
}
536
537
void AnimationPlayer::_capture(const StringName &p_name, bool p_from_end, double p_duration, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {
538
StringName name = p_name;
539
if (name == StringName()) {
540
name = playback.assigned;
541
}
542
543
Ref<Animation> anim = get_animation(name);
544
if (anim.is_null() || !anim->is_capture_included()) {
545
return;
546
}
547
if (std::signbit(p_duration)) {
548
double max_dur = 0;
549
double current_pos = playback.current.pos;
550
if (playback.assigned != name) {
551
current_pos = p_from_end ? anim->get_length() : 0;
552
}
553
for (int i = 0; i < anim->get_track_count(); i++) {
554
if (anim->track_get_type(i) != Animation::TYPE_VALUE) {
555
continue;
556
}
557
if (anim->value_track_get_update_mode(i) != Animation::UPDATE_CAPTURE) {
558
continue;
559
}
560
if (anim->track_get_key_count(i) == 0) {
561
continue;
562
}
563
max_dur = MAX(max_dur, p_from_end ? current_pos - anim->track_get_key_time(i, anim->track_get_key_count(i) - 1) : anim->track_get_key_time(i, 0) - current_pos);
564
}
565
p_duration = max_dur;
566
}
567
if (Math::is_zero_approx(p_duration)) {
568
return;
569
}
570
capture(name, p_duration, p_trans_type, p_ease_type);
571
}
572
573
void AnimationPlayer::play_with_capture(const StringName &p_name, double p_duration, double p_custom_blend, float p_custom_scale, bool p_from_end, Tween::TransitionType p_trans_type, Tween::EaseType p_ease_type) {
574
_capture(p_name, p_from_end, p_duration, p_trans_type, p_ease_type);
575
_play(p_name, p_custom_blend, p_custom_scale, p_from_end);
576
}
577
578
bool AnimationPlayer::is_playing() const {
579
return playing;
580
}
581
582
void AnimationPlayer::set_current_animation(const String &p_animation) {
583
if (p_animation == "[stop]" || p_animation.is_empty()) {
584
stop();
585
} else if (!is_playing()) {
586
play(p_animation);
587
} else if (playback.assigned != p_animation) {
588
float speed = playback.current.speed_scale;
589
play(p_animation, -1.0, speed, std::signbit(speed));
590
} else {
591
// Same animation, do not replay from start.
592
}
593
}
594
595
String AnimationPlayer::get_current_animation() const {
596
return (is_playing() ? playback.assigned : "");
597
}
598
599
void AnimationPlayer::set_assigned_animation(const String &p_animation) {
600
if (is_playing()) {
601
float speed = playback.current.speed_scale;
602
play(p_animation, -1.0, speed, std::signbit(speed));
603
} else {
604
ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
605
playback.current.pos = 0;
606
playback.current.from = &animation_set[p_animation];
607
playback.current.start_time = -1;
608
playback.current.end_time = -1;
609
playback.assigned = p_animation;
610
emit_signal(SNAME("current_animation_changed"), playback.assigned);
611
}
612
}
613
614
String AnimationPlayer::get_assigned_animation() const {
615
return playback.assigned;
616
}
617
618
void AnimationPlayer::pause() {
619
_stop_internal(false, false);
620
}
621
622
void AnimationPlayer::stop(bool p_keep_state) {
623
_stop_internal(true, p_keep_state);
624
}
625
626
void AnimationPlayer::set_speed_scale(float p_speed) {
627
speed_scale = p_speed;
628
}
629
630
float AnimationPlayer::get_speed_scale() const {
631
return speed_scale;
632
}
633
634
float AnimationPlayer::get_playing_speed() const {
635
if (!playing) {
636
return 0;
637
}
638
return speed_scale * playback.current.speed_scale;
639
}
640
641
void AnimationPlayer::seek_internal(double p_time, bool p_update, bool p_update_only, bool p_is_internal_seek) {
642
if (!active) {
643
return;
644
}
645
646
bool is_backward = Animation::is_less_approx(p_time, playback.current.pos);
647
648
_check_immediately_after_start();
649
650
playback.current.pos = p_time;
651
if (!playback.current.from) {
652
if (playback.assigned) {
653
ERR_FAIL_COND_MSG(!animation_set.has(playback.assigned), vformat("Animation not found: %s.", playback.assigned));
654
playback.current.from = &animation_set[playback.assigned];
655
}
656
if (!playback.current.from) {
657
return; // There is no animation.
658
}
659
}
660
661
double start = playback.current.get_start_time();
662
double end = playback.current.get_end_time();
663
664
// Clamp the seek position.
665
p_time = CLAMP(p_time, start, end);
666
667
playback.seeked = true;
668
playback.internal_seeked = p_is_internal_seek;
669
670
if (p_update) {
671
_process_animation(is_backward ? -0.0 : 0.0, p_update_only);
672
playback.seeked = false; // If animation was proceeded here, no more seek in internal process.
673
}
674
}
675
676
void AnimationPlayer::seek(double p_time, bool p_update, bool p_update_only) {
677
seek_internal(p_time, p_update, p_update_only);
678
}
679
680
void AnimationPlayer::advance(double p_time) {
681
_check_immediately_after_start();
682
AnimationMixer::advance(p_time);
683
}
684
685
void AnimationPlayer::_check_immediately_after_start() {
686
if (playback.started) {
687
_process_animation(0); // Force process current key for Discrete/Method/Audio/AnimationPlayback. Then, started flag is cleared.
688
}
689
}
690
691
bool AnimationPlayer::is_valid() const {
692
return (playback.current.from);
693
}
694
695
double AnimationPlayer::get_current_animation_position() const {
696
ERR_FAIL_NULL_V_MSG(playback.current.from, 0, "AnimationPlayer has no current animation.");
697
return playback.current.pos;
698
}
699
700
double AnimationPlayer::get_current_animation_length() const {
701
ERR_FAIL_NULL_V_MSG(playback.current.from, 0, "AnimationPlayer has no current animation.");
702
return playback.current.from->animation->get_length();
703
}
704
705
void AnimationPlayer::set_section_with_markers(const StringName &p_start_marker, const StringName &p_end_marker) {
706
ERR_FAIL_NULL_MSG(playback.current.from, "AnimationPlayer has no current animation.");
707
ERR_FAIL_COND_MSG(p_start_marker == p_end_marker && p_start_marker, vformat("Start marker and end marker cannot be the same marker: %s.", p_start_marker));
708
ERR_FAIL_COND_MSG(p_start_marker && !playback.current.from->animation->has_marker(p_start_marker), vformat("Marker %s not found in animation: %s.", p_start_marker, playback.current.from->animation->get_name()));
709
ERR_FAIL_COND_MSG(p_end_marker && !playback.current.from->animation->has_marker(p_end_marker), vformat("Marker %s not found in animation: %s.", p_end_marker, playback.current.from->animation->get_name()));
710
double start_time = p_start_marker ? playback.current.from->animation->get_marker_time(p_start_marker) : -1;
711
double end_time = p_end_marker ? playback.current.from->animation->get_marker_time(p_end_marker) : -1;
712
if (p_start_marker && Animation::is_less_approx(start_time, 0)) {
713
WARN_PRINT_ONCE_ED(vformat("Marker %s time must be positive in animation: %s.", p_start_marker, playback.current.from->animation->get_name()));
714
}
715
if (p_end_marker && Animation::is_less_approx(end_time, 0)) {
716
WARN_PRINT_ONCE_ED(vformat("Marker %s time must be positive in animation: %s.", p_end_marker, playback.current.from->animation->get_name()));
717
}
718
set_section(start_time, end_time);
719
}
720
721
void AnimationPlayer::set_section(double p_start_time, double p_end_time) {
722
ERR_FAIL_NULL_MSG(playback.current.from, "AnimationPlayer has no current animation.");
723
ERR_FAIL_COND_MSG(Animation::is_greater_or_equal_approx(p_start_time, 0) && Animation::is_greater_or_equal_approx(p_end_time, 0) && Animation::is_greater_or_equal_approx(p_start_time, p_end_time), vformat("Start time %f is greater than end time %f.", p_start_time, p_end_time));
724
playback.current.start_time = p_start_time;
725
playback.current.end_time = p_end_time;
726
playback.current.pos = CLAMP(playback.current.pos, playback.current.get_start_time(), playback.current.get_end_time());
727
}
728
729
void AnimationPlayer::reset_section() {
730
playback.current.start_time = -1;
731
playback.current.end_time = -1;
732
}
733
734
double AnimationPlayer::get_section_start_time() const {
735
ERR_FAIL_NULL_V_MSG(playback.current.from, playback.current.start_time, "AnimationPlayer has no current animation.");
736
return playback.current.get_start_time();
737
}
738
739
double AnimationPlayer::get_section_end_time() const {
740
ERR_FAIL_NULL_V_MSG(playback.current.from, playback.current.end_time, "AnimationPlayer has no current animation.");
741
return playback.current.get_end_time();
742
}
743
744
bool AnimationPlayer::has_section() const {
745
return Animation::is_greater_or_equal_approx(playback.current.start_time, 0) || Animation::is_greater_or_equal_approx(playback.current.end_time, 0);
746
}
747
748
void AnimationPlayer::set_autoplay(const String &p_name) {
749
if (is_inside_tree() && !Engine::get_singleton()->is_editor_hint()) {
750
WARN_PRINT("Setting autoplay after the node has been added to the scene has no effect.");
751
}
752
753
autoplay = p_name;
754
}
755
756
String AnimationPlayer::get_autoplay() const {
757
return autoplay;
758
}
759
760
void AnimationPlayer::set_movie_quit_on_finish_enabled(bool p_enabled) {
761
movie_quit_on_finish = p_enabled;
762
}
763
764
bool AnimationPlayer::is_movie_quit_on_finish_enabled() const {
765
return movie_quit_on_finish;
766
}
767
768
void AnimationPlayer::_stop_internal(bool p_reset, bool p_keep_state) {
769
_clear_caches();
770
Playback &c = playback;
771
// c.blend.clear();
772
double start = c.current.from ? playback.current.get_start_time() : 0;
773
if (p_reset) {
774
c.blend.clear();
775
if (p_keep_state) {
776
c.current.pos = start;
777
} else {
778
is_stopping = true;
779
seek_internal(start, true, true, true);
780
is_stopping = false;
781
}
782
c.current.from = nullptr;
783
c.current.speed_scale = 1;
784
emit_signal(SNAME("current_animation_changed"), "");
785
}
786
_set_process(false);
787
playback_queue.clear();
788
playing = false;
789
}
790
791
void AnimationPlayer::animation_set_next(const StringName &p_animation, const StringName &p_next) {
792
ERR_FAIL_COND_MSG(!animation_set.has(p_animation), vformat("Animation not found: %s.", p_animation));
793
animation_next_set[p_animation] = p_next;
794
}
795
796
StringName AnimationPlayer::animation_get_next(const StringName &p_animation) const {
797
if (!animation_next_set.has(p_animation)) {
798
return StringName();
799
}
800
return animation_next_set[p_animation];
801
}
802
803
void AnimationPlayer::set_default_blend_time(double p_default) {
804
default_blend_time = p_default;
805
}
806
807
double AnimationPlayer::get_default_blend_time() const {
808
return default_blend_time;
809
}
810
811
void AnimationPlayer::set_blend_time(const StringName &p_animation1, const StringName &p_animation2, double p_time) {
812
ERR_FAIL_COND_MSG(!animation_set.has(p_animation1), vformat("Animation not found: %s.", p_animation1));
813
ERR_FAIL_COND_MSG(!animation_set.has(p_animation2), vformat("Animation not found: %s.", p_animation2));
814
ERR_FAIL_COND_MSG(p_time < 0, "Blend time cannot be smaller than 0.");
815
816
BlendKey bk;
817
bk.from = p_animation1;
818
bk.to = p_animation2;
819
if (Math::is_zero_approx(p_time)) {
820
blend_times.erase(bk);
821
} else {
822
blend_times[bk] = p_time;
823
}
824
}
825
826
double AnimationPlayer::get_blend_time(const StringName &p_animation1, const StringName &p_animation2) const {
827
BlendKey bk;
828
bk.from = p_animation1;
829
bk.to = p_animation2;
830
831
if (blend_times.has(bk)) {
832
return blend_times[bk];
833
} else {
834
return 0;
835
}
836
}
837
838
void AnimationPlayer::set_auto_capture(bool p_auto_capture) {
839
auto_capture = p_auto_capture;
840
notify_property_list_changed();
841
}
842
843
bool AnimationPlayer::is_auto_capture() const {
844
return auto_capture;
845
}
846
847
void AnimationPlayer::set_auto_capture_duration(double p_auto_capture_duration) {
848
auto_capture_duration = p_auto_capture_duration;
849
}
850
851
double AnimationPlayer::get_auto_capture_duration() const {
852
return auto_capture_duration;
853
}
854
855
void AnimationPlayer::set_auto_capture_transition_type(Tween::TransitionType p_auto_capture_transition_type) {
856
auto_capture_transition_type = p_auto_capture_transition_type;
857
}
858
859
Tween::TransitionType AnimationPlayer::get_auto_capture_transition_type() const {
860
return auto_capture_transition_type;
861
}
862
863
void AnimationPlayer::set_auto_capture_ease_type(Tween::EaseType p_auto_capture_ease_type) {
864
auto_capture_ease_type = p_auto_capture_ease_type;
865
}
866
867
Tween::EaseType AnimationPlayer::get_auto_capture_ease_type() const {
868
return auto_capture_ease_type;
869
}
870
871
#ifdef TOOLS_ENABLED
872
void AnimationPlayer::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
873
const String pf = p_function;
874
if (p_idx == 0 && (pf == "play" || pf == "play_backwards" || pf == "has_animation" || pf == "queue")) {
875
List<StringName> al;
876
get_animation_list(&al);
877
for (const StringName &name : al) {
878
r_options->push_back(String(name).quote());
879
}
880
}
881
AnimationMixer::get_argument_options(p_function, p_idx, r_options);
882
}
883
#endif
884
885
void AnimationPlayer::_animation_removed(const StringName &p_name, const StringName &p_library) {
886
AnimationMixer::_animation_removed(p_name, p_library);
887
888
StringName name = p_library == StringName() ? p_name : StringName(String(p_library) + "/" + String(p_name));
889
890
if (!animation_set.has(name)) {
891
return; // No need to update because not the one from the library being used.
892
}
893
894
_animation_set_cache_update();
895
896
// Erase blends if needed
897
List<BlendKey> to_erase;
898
for (const KeyValue<BlendKey, double> &E : blend_times) {
899
BlendKey bk = E.key;
900
if (bk.from == name || bk.to == name) {
901
to_erase.push_back(bk);
902
}
903
}
904
905
while (to_erase.size()) {
906
blend_times.erase(to_erase.front()->get());
907
to_erase.pop_front();
908
}
909
}
910
911
void AnimationPlayer::_rename_animation(const StringName &p_from_name, const StringName &p_to_name) {
912
AnimationMixer::_rename_animation(p_from_name, p_to_name);
913
914
// Rename autoplay or blends if needed.
915
List<BlendKey> to_erase;
916
HashMap<BlendKey, double, BlendKey> to_insert;
917
for (const KeyValue<BlendKey, double> &E : blend_times) {
918
BlendKey bk = E.key;
919
BlendKey new_bk = bk;
920
bool erase = false;
921
if (bk.from == p_from_name) {
922
new_bk.from = p_to_name;
923
erase = true;
924
}
925
if (bk.to == p_from_name) {
926
new_bk.to = p_to_name;
927
erase = true;
928
}
929
930
if (erase) {
931
to_erase.push_back(bk);
932
to_insert[new_bk] = E.value;
933
}
934
}
935
936
while (to_erase.size()) {
937
blend_times.erase(to_erase.front()->get());
938
to_erase.pop_front();
939
}
940
941
while (to_insert.size()) {
942
blend_times[to_insert.begin()->key] = to_insert.begin()->value;
943
to_insert.remove(to_insert.begin());
944
}
945
946
if (autoplay == p_from_name) {
947
autoplay = p_to_name;
948
}
949
}
950
951
void AnimationPlayer::_bind_methods() {
952
ClassDB::bind_method(D_METHOD("animation_set_next", "animation_from", "animation_to"), &AnimationPlayer::animation_set_next);
953
ClassDB::bind_method(D_METHOD("animation_get_next", "animation_from"), &AnimationPlayer::animation_get_next);
954
955
ClassDB::bind_method(D_METHOD("set_blend_time", "animation_from", "animation_to", "sec"), &AnimationPlayer::set_blend_time);
956
ClassDB::bind_method(D_METHOD("get_blend_time", "animation_from", "animation_to"), &AnimationPlayer::get_blend_time);
957
958
ClassDB::bind_method(D_METHOD("set_default_blend_time", "sec"), &AnimationPlayer::set_default_blend_time);
959
ClassDB::bind_method(D_METHOD("get_default_blend_time"), &AnimationPlayer::get_default_blend_time);
960
961
ClassDB::bind_method(D_METHOD("set_auto_capture", "auto_capture"), &AnimationPlayer::set_auto_capture);
962
ClassDB::bind_method(D_METHOD("is_auto_capture"), &AnimationPlayer::is_auto_capture);
963
ClassDB::bind_method(D_METHOD("set_auto_capture_duration", "auto_capture_duration"), &AnimationPlayer::set_auto_capture_duration);
964
ClassDB::bind_method(D_METHOD("get_auto_capture_duration"), &AnimationPlayer::get_auto_capture_duration);
965
ClassDB::bind_method(D_METHOD("set_auto_capture_transition_type", "auto_capture_transition_type"), &AnimationPlayer::set_auto_capture_transition_type);
966
ClassDB::bind_method(D_METHOD("get_auto_capture_transition_type"), &AnimationPlayer::get_auto_capture_transition_type);
967
ClassDB::bind_method(D_METHOD("set_auto_capture_ease_type", "auto_capture_ease_type"), &AnimationPlayer::set_auto_capture_ease_type);
968
ClassDB::bind_method(D_METHOD("get_auto_capture_ease_type"), &AnimationPlayer::get_auto_capture_ease_type);
969
970
ClassDB::bind_method(D_METHOD("play", "name", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
971
ClassDB::bind_method(D_METHOD("play_section_with_markers", "name", "start_marker", "end_marker", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play_section_with_markers, DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
972
ClassDB::bind_method(D_METHOD("play_section", "name", "start_time", "end_time", "custom_blend", "custom_speed", "from_end"), &AnimationPlayer::play_section, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false));
973
ClassDB::bind_method(D_METHOD("play_backwards", "name", "custom_blend"), &AnimationPlayer::play_backwards, DEFVAL(StringName()), DEFVAL(-1));
974
ClassDB::bind_method(D_METHOD("play_section_with_markers_backwards", "name", "start_marker", "end_marker", "custom_blend"), &AnimationPlayer::play_section_with_markers_backwards, DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(StringName()), DEFVAL(-1));
975
ClassDB::bind_method(D_METHOD("play_section_backwards", "name", "start_time", "end_time", "custom_blend"), &AnimationPlayer::play_section_backwards, DEFVAL(StringName()), DEFVAL(-1), DEFVAL(-1), DEFVAL(-1));
976
ClassDB::bind_method(D_METHOD("play_with_capture", "name", "duration", "custom_blend", "custom_speed", "from_end", "trans_type", "ease_type"), &AnimationPlayer::play_with_capture, DEFVAL(StringName()), DEFVAL(-1.0), DEFVAL(-1), DEFVAL(1.0), DEFVAL(false), DEFVAL(Tween::TRANS_LINEAR), DEFVAL(Tween::EASE_IN));
977
ClassDB::bind_method(D_METHOD("pause"), &AnimationPlayer::pause);
978
ClassDB::bind_method(D_METHOD("stop", "keep_state"), &AnimationPlayer::stop, DEFVAL(false));
979
ClassDB::bind_method(D_METHOD("is_playing"), &AnimationPlayer::is_playing);
980
981
ClassDB::bind_method(D_METHOD("set_current_animation", "animation"), &AnimationPlayer::set_current_animation);
982
ClassDB::bind_method(D_METHOD("get_current_animation"), &AnimationPlayer::get_current_animation);
983
ClassDB::bind_method(D_METHOD("set_assigned_animation", "animation"), &AnimationPlayer::set_assigned_animation);
984
ClassDB::bind_method(D_METHOD("get_assigned_animation"), &AnimationPlayer::get_assigned_animation);
985
ClassDB::bind_method(D_METHOD("queue", "name"), &AnimationPlayer::queue);
986
ClassDB::bind_method(D_METHOD("get_queue"), &AnimationPlayer::get_queue);
987
ClassDB::bind_method(D_METHOD("clear_queue"), &AnimationPlayer::clear_queue);
988
989
ClassDB::bind_method(D_METHOD("set_speed_scale", "speed"), &AnimationPlayer::set_speed_scale);
990
ClassDB::bind_method(D_METHOD("get_speed_scale"), &AnimationPlayer::get_speed_scale);
991
ClassDB::bind_method(D_METHOD("get_playing_speed"), &AnimationPlayer::get_playing_speed);
992
993
ClassDB::bind_method(D_METHOD("set_autoplay", "name"), &AnimationPlayer::set_autoplay);
994
ClassDB::bind_method(D_METHOD("get_autoplay"), &AnimationPlayer::get_autoplay);
995
996
ClassDB::bind_method(D_METHOD("find_animation", "animation"), &AnimationPlayer::find_animation);
997
ClassDB::bind_method(D_METHOD("find_animation_library", "animation"), &AnimationPlayer::find_animation_library);
998
999
ClassDB::bind_method(D_METHOD("set_movie_quit_on_finish_enabled", "enabled"), &AnimationPlayer::set_movie_quit_on_finish_enabled);
1000
ClassDB::bind_method(D_METHOD("is_movie_quit_on_finish_enabled"), &AnimationPlayer::is_movie_quit_on_finish_enabled);
1001
1002
ClassDB::bind_method(D_METHOD("get_current_animation_position"), &AnimationPlayer::get_current_animation_position);
1003
ClassDB::bind_method(D_METHOD("get_current_animation_length"), &AnimationPlayer::get_current_animation_length);
1004
1005
ClassDB::bind_method(D_METHOD("set_section_with_markers", "start_marker", "end_marker"), &AnimationPlayer::set_section_with_markers, DEFVAL(StringName()), DEFVAL(StringName()));
1006
ClassDB::bind_method(D_METHOD("set_section", "start_time", "end_time"), &AnimationPlayer::set_section, DEFVAL(-1), DEFVAL(-1));
1007
ClassDB::bind_method(D_METHOD("reset_section"), &AnimationPlayer::reset_section);
1008
1009
ClassDB::bind_method(D_METHOD("get_section_start_time"), &AnimationPlayer::get_section_start_time);
1010
ClassDB::bind_method(D_METHOD("get_section_end_time"), &AnimationPlayer::get_section_end_time);
1011
ClassDB::bind_method(D_METHOD("has_section"), &AnimationPlayer::has_section);
1012
1013
ClassDB::bind_method(D_METHOD("seek", "seconds", "update", "update_only"), &AnimationPlayer::seek, DEFVAL(false), DEFVAL(false));
1014
1015
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "current_animation", PROPERTY_HINT_ENUM, "", PROPERTY_USAGE_EDITOR), "set_current_animation", "get_current_animation");
1016
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "assigned_animation", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_assigned_animation", "get_assigned_animation");
1017
ADD_PROPERTY(PropertyInfo(Variant::STRING_NAME, "autoplay", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_autoplay", "get_autoplay");
1018
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_length", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_length");
1019
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "current_animation_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "", "get_current_animation_position");
1020
1021
ADD_GROUP("Playback Options", "playback_");
1022
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "playback_auto_capture"), "set_auto_capture", "is_auto_capture");
1023
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_auto_capture_duration", PROPERTY_HINT_NONE, "suffix:s"), "set_auto_capture_duration", "get_auto_capture_duration");
1024
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_auto_capture_transition_type", PROPERTY_HINT_ENUM, "Linear,Sine,Quint,Quart,Quad,Expo,Elastic,Cubic,Circ,Bounce,Back,Spring"), "set_auto_capture_transition_type", "get_auto_capture_transition_type");
1025
ADD_PROPERTY(PropertyInfo(Variant::INT, "playback_auto_capture_ease_type", PROPERTY_HINT_ENUM, "In,Out,InOut,OutIn"), "set_auto_capture_ease_type", "get_auto_capture_ease_type");
1026
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "playback_default_blend_time", PROPERTY_HINT_RANGE, "0,4096,0.01,suffix:s"), "set_default_blend_time", "get_default_blend_time");
1027
1028
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "speed_scale", PROPERTY_HINT_RANGE, "-4,4,0.001,or_less,or_greater"), "set_speed_scale", "get_speed_scale");
1029
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "movie_quit_on_finish"), "set_movie_quit_on_finish_enabled", "is_movie_quit_on_finish_enabled");
1030
1031
ADD_SIGNAL(MethodInfo(SNAME("current_animation_changed"), PropertyInfo(Variant::STRING, "name")));
1032
ADD_SIGNAL(MethodInfo(SNAME("animation_changed"), PropertyInfo(Variant::STRING_NAME, "old_name"), PropertyInfo(Variant::STRING_NAME, "new_name")));
1033
}
1034
1035
AnimationPlayer::AnimationPlayer() {
1036
}
1037
1038
AnimationPlayer::~AnimationPlayer() {
1039
}
1040
1041