Merge pull request #95023 from TokageItLab/snap-anim-end

Snap current position to the edge on animation finished
This commit is contained in:
Rémi Verschelde 2024-08-06 12:31:02 +02:00
commit cd76e36132
No known key found for this signature in database
GPG key ID: C3336907360768E1
2 changed files with 5 additions and 1 deletions

View file

@ -237,6 +237,7 @@ AnimationNode::NodeTimeInfo AnimationNodeAnimation::_process(const AnimationMixe
} }
// Finished. // Finished.
if (Animation::is_less_approx(prev_playback_time, anim_size) && Animation::is_greater_or_equal_approx(cur_playback_time, anim_size)) { if (Animation::is_less_approx(prev_playback_time, anim_size) && Animation::is_greater_or_equal_approx(cur_playback_time, anim_size)) {
cur_playback_time = anim_size;
process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_finished), animation); process_state->tree->call_deferred(SNAME("emit_signal"), SceneStringName(animation_finished), animation);
} }
} }

View file

@ -204,19 +204,20 @@ void AnimationPlayer::_process_playback_data(PlaybackData &cd, double p_delta, f
} }
double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process. double prev_pos = cd.pos; // The animation may be changed during process, so it is safer that the state is changed before process.
cd.pos = next_pos;
// End detection. // End detection.
if (p_is_current) { if (p_is_current) {
if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) { if (cd.from->animation->get_loop_mode() == Animation::LOOP_NONE) {
if (!backwards && Animation::is_less_or_equal_approx(prev_pos, len) && Math::is_equal_approx(next_pos, len)) { if (!backwards && Animation::is_less_or_equal_approx(prev_pos, len) && Math::is_equal_approx(next_pos, len)) {
// Playback finished. // Playback finished.
next_pos = len; // Snap to the edge.
end_reached = true; end_reached = true;
end_notify = Animation::is_less_approx(prev_pos, len); // Notify only if not already at the end. end_notify = Animation::is_less_approx(prev_pos, len); // Notify only if not already at the end.
p_blend = 1.0; p_blend = 1.0;
} }
if (backwards && Animation::is_greater_or_equal_approx(prev_pos, 0) && Math::is_equal_approx(next_pos, 0)) { if (backwards && Animation::is_greater_or_equal_approx(prev_pos, 0) && Math::is_equal_approx(next_pos, 0)) {
// Playback finished. // Playback finished.
next_pos = 0; // Snap to the edge.
end_reached = true; end_reached = true;
end_notify = Animation::is_greater_approx(prev_pos, 0); // Notify only if not already at the beginning. end_notify = Animation::is_greater_approx(prev_pos, 0); // Notify only if not already at the beginning.
p_blend = 1.0; p_blend = 1.0;
@ -224,6 +225,8 @@ void AnimationPlayer::_process_playback_data(PlaybackData &cd, double p_delta, f
} }
} }
cd.pos = next_pos;
PlaybackInfo pi; PlaybackInfo pi;
if (p_started) { if (p_started) {
pi.time = prev_pos; pi.time = prev_pos;