8#include "algorithm.hpp"
30 void update(value_type new_value, utc_nanoseconds current_time)
noexcept
32 if (not initialized) {
34 _old_value = new_value;
35 _new_value = new_value;
36 _start_time = current_time;
38 }
else if (new_value != _new_value) {
39 _old_value = _new_value;
40 _new_value = new_value;
41 _start_time = current_time;
43 _current_time = current_time;
50 tt_axiom(initialized);
51 return progress() < 1.0f;
57 tt_axiom(initialized);
58 return std::lerp(_old_value, _new_value, progress());
62 value_type _old_value;
63 value_type _new_value;
64 utc_nanoseconds _start_time;
65 utc_nanoseconds _current_time;
67 bool initialized =
false;
69 float progress() const noexcept
71 using namespace std::literals::chrono_literals;
73 ttlet dt = _current_time - _start_time;
74 ttlet p =
static_cast<float>(dt / 1ms) /
static_cast<float>(_animation_duration / 1ms);
75 return std::clamp(p, 0.0f, 1.0f);
A type that gets animated between two values.
Definition animator.hpp:17
animator(std::chrono::nanoseconds animation_duration) noexcept
Constructor.
Definition animator.hpp:24
void update(value_type new_value, utc_nanoseconds current_time) noexcept
Update the value and time.
Definition animator.hpp:30
bool is_animating() const noexcept
Check if the animation is currently running.
Definition animator.hpp:48
value_type current_value() const noexcept
The interpolated value between start and end value.
Definition animator.hpp:56