From fade4c28477b9810133eb695b5d8fb3257d0cbd9 Mon Sep 17 00:00:00 2001 From: Rasmus Anthin Date: Mon, 18 Nov 2024 08:13:58 +0100 Subject: [PATCH] Added header Animation.h --- Animation.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Animation.h diff --git a/Animation.h b/Animation.h new file mode 100644 index 0000000..8e49269 --- /dev/null +++ b/Animation.h @@ -0,0 +1,55 @@ +// +// Animations.h +// Termin8or +// +// Created by Rasmus Anthin on 2024-11-18. +// + +#pragma once +#include +#include + + +namespace easings +{ + std::function ease_in_sine = [](float t) + { + return 1.f - std::cos((t * math::c_pi) * 0.5f); + }; + + std::function 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& ease_in_func, std::function& 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(t_ease_in, {}, 0.f, Range::FreeOpen)) + return value_start; + if (math::in_range(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(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(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(t_ease_out, 1.f, {}, Range::ClosedFree); + } +};