Skip to content

Commit

Permalink
Added header Animation.h
Browse files Browse the repository at this point in the history
  • Loading branch information
razterizer committed Nov 18, 2024
1 parent d921255 commit fade4c2
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions Animation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
//
// Animations.h
// Termin8or
//
// Created by Rasmus Anthin on 2024-11-18.
//

#pragma once
#include <Core/Math.h>
#include <functional>


namespace easings
{
std::function<float(float)> ease_in_sine = [](float t)
{
return 1.f - std::cos((t * math::c_pi) * 0.5f);
};

std::function<float(float)> ease_out_sine = [](float t)
{
return std::sin((t * math::c_pi) * 0.5f);
};
}


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 animate(float time_s, float value_start, float value_stationary, float value_end,
std::function<float(float)>& ease_in_func, std::function<float(float)>& ease_out_func) const
{
float t_ease_in = math::value_to_param(time_s, ease_in_start_time_s, ease_in_end_time_s);
if (math::in_range<float>(t_ease_in, {}, 0.f, Range::FreeOpen))
return value_start;
if (math::in_range<float>(t_ease_in, 0.f, 1.f, Range::ClosedOpen))
return math::lerp(ease_in_func(1.f - t_ease_in), value_stationary, value_start);
float t_ease_out = math::value_to_param(time_s, ease_out_start_time_s, ease_out_end_time_s);
if (math::in_range<float>(t_ease_out, 0.f, 1.f, Range::ClosedOpen))
return math::lerp(ease_out_func(1.f - t_ease_out), value_end, value_stationary);
if (math::in_range<float>(t_ease_out, 1.f, {}, Range::ClosedFree))
return value_end;
return value_stationary;
}

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);
}
};

0 comments on commit fade4c2

Please sign in to comment.