-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d921255
commit fade4c2
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |