Skip to content

Commit

Permalink
Animation.h:
Browse files Browse the repository at this point in the history
* Slight improvement of TransitionAnimation by introducing a variable transition_start_time_s and making the other variables relative to that.
  • Loading branch information
razterizer committed Nov 18, 2024
1 parent 6e380de commit 99556a0
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ namespace easings

struct TransitionAnimation
{
float ease_in_start_time_s = 0.f;
float ease_in_end_time_s = 0.f;
float ease_out_start_time_s = 0.f;
float ease_out_end_time_s = 0.f;
float transition_start_time_s = 0.f;
// Relative to transition_start_time_s.
float enter_rel_start_time_s = 0.f;
float enter_rel_end_time_s = 0.f;
float exit_rel_start_time_s = 0.f;
float exit_rel_end_time_s = 0.f;

float animate(float time_s, float value_start, float value_stationary, float value_end,
std::function<float(float)>& ease_enter_func, std::function<float(float)>& ease_exit_func) const
{
float t_enter = math::value_to_param(time_s, ease_in_start_time_s, ease_in_end_time_s);
auto rel_time_s = time_s - transition_start_time_s;

float t_enter = math::value_to_param(rel_time_s, enter_rel_start_time_s, enter_rel_end_time_s);
if (math::in_range<float>(t_enter, {}, 0.f, Range::FreeOpen))
return value_start;
if (math::in_range<float>(t_enter, 0.f, 1.f, Range::ClosedOpen))
return math::lerp(ease_enter_func(t_enter), value_start, value_stationary);
float t_exit = math::value_to_param(time_s, ease_out_start_time_s, ease_out_end_time_s);
float t_exit = math::value_to_param(rel_time_s, exit_rel_start_time_s, exit_rel_end_time_s);
if (math::in_range<float>(t_exit, 0.f, 1.f, Range::ClosedOpen))
return math::lerp(ease_exit_func(t_exit), value_stationary, value_end);
if (math::in_range<float>(t_exit, 1.f, {}, Range::ClosedFree))
Expand All @@ -49,7 +53,6 @@ struct TransitionAnimation

bool done(float time_s) const
{
float t_ease_out = math::value_to_param(time_s, ease_out_start_time_s, ease_out_end_time_s);
return math::in_range<float>(t_ease_out, 1.f, {}, Range::ClosedFree);
return time_s - transition_start_time_s >= exit_rel_end_time_s;
}
};

0 comments on commit 99556a0

Please sign in to comment.