7#include "../macros.hpp"
8#include "../utility/utility.hpp"
9#include "../time/time.hpp"
14hi_export_module(hikogui.algorithm.animator);
16hi_export
namespace hi::inline
v1 {
18enum class animator_state {
19 uninitialized, idle, running,
end
33 _animation_duration(animation_duration), _old_value(), _new_value(), _start_time(), _current_time(std::chrono::utc_clock::now()), _state(animator_state::uninitialized)
42 [[nodiscard]] animator_state
update(value_type new_value, utc_nanoseconds current_time)
noexcept
44 if (_state == animator_state::uninitialized) {
45 _state = animator_state::idle;
46 _old_value = new_value;
47 _new_value = new_value;
49 }
else if (new_value != _new_value) {
50 _state = animator_state::running;
51 _old_value = _new_value;
52 _new_value = new_value;
53 _start_time = current_time;
55 _current_time = current_time;
56 return is_animating();
65 case animator_state::uninitialized:
66 return animator_state::uninitialized;
67 case animator_state::idle:
68 return animator_state::idle;
69 case animator_state::running:
70 if (progress() < 1.0f) {
71 return animator_state::running;
73 return _state = animator_state::end;
75 case animator_state::end:
76 return _state = animator_state::idle;
86 return std::lerp(_old_value, _new_value, progress());
92 value_type _old_value;
93 value_type _new_value;
94 utc_nanoseconds _start_time;
95 utc_nanoseconds _current_time;
96 mutable animator_state _state;
98 float progress() const noexcept
100 using namespace std::chrono_literals;
102 auto const dt = _current_time - _start_time;
103 auto const p =
static_cast<float>(dt / 1ms) /
static_cast<float>(_animation_duration / 1ms);
104 return std::clamp(p, 0.0f, 1.0f);
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
A type that gets animated between two values.
Definition animator.hpp:25
animator(std::chrono::nanoseconds animation_duration) noexcept
Constructor.
Definition animator.hpp:32
value_type current_value() const noexcept
The interpolated value between start and end value.
Definition animator.hpp:84
animator_state is_animating() const noexcept
Check if the animation is currently running.
Definition animator.hpp:62
animator_state update(value_type new_value, utc_nanoseconds current_time) noexcept
Update the value and time.
Definition animator.hpp:42