OGLplus  (0.59.0) a C++ wrapper for rendering APIs

animated_value.hpp
Go to the documentation of this file.
1 
9 #ifndef EAGINE_ANIMATED_VALUE_HPP
10 #define EAGINE_ANIMATED_VALUE_HPP
11 
12 #include "math/functions.hpp"
13 #include "valid_if/positive.hpp"
14 #include <type_traits>
15 
16 namespace eagine {
17 //------------------------------------------------------------------------------
19 template <typename T, typename F, typename S = float>
21 public:
23  animated_value() = default;
24 
26  animated_value(const T& initial)
27  : _curr{initial}
28  , _next{initial} {}
29 
31  auto set(T value, valid_if_positive<F> duration, S slope) -> auto& {
32  _curr = _next;
33  _next = value;
34  _duration = extract(duration);
35  _phase = F(0);
36  _slope = slope;
37  return *this;
38  }
39 
41  auto set(T value, valid_if_positive<F> duration) -> auto& {
42  return set(value, duration, 2);
43  }
44 
46  auto get() const -> T {
47  return math::blend(
48  _next,
49  _curr,
50  math::sigmoid01(math::clamp(_phase / _duration, 0, 1), _slope));
51  }
52 
54  auto update(F deltaT) -> auto& {
55  _phase += deltaT;
56  return *this;
57  }
58 
60  auto is_done() const noexcept -> bool {
61  return _phase >= _duration;
62  }
63 
64 private:
65  T _curr{0};
66  T _next{0};
67  F _duration{1};
68  F _phase{_duration};
69  S _slope{2};
70 };
71 //------------------------------------------------------------------------------
72 } // namespace eagine
73 
74 #endif
static constexpr auto clamp(T x, Min min, Max max) noexcept
Clamps x to be between min and max.
Definition: functions.hpp:96
Common code is placed in this namespace.
Definition: eagine.hpp:21
Class representing a value animated between two points, with easing.
Definition: animated_value.hpp:20
static constexpr auto extract(api_result_value< Result, api_result_validity::never > &) noexcept -> Result &
Overload of extract for api_result_value.
Definition: c_api_wrap.hpp:270
auto update(F deltaT) -> auto &
Updates the transition factor by the specified amount.
Definition: animated_value.hpp:54
Primary template for conditionally valid values.
Definition: decl.hpp:49
auto is_done() const noexcept -> bool
Indicates if the current transition factor is higher than current duration.
Definition: animated_value.hpp:60
animated_value()=default
Default constructor.
static constexpr auto sigmoid01(T x, C c) noexcept
Calculates the sigmoid of x. The value c controls steepness.
Definition: functions.hpp:134
static constexpr auto blend(T v1, T v2, A alpha) noexcept
Blends v1 and v2, using alpha as the blending factor.
Definition: functions.hpp:110
auto set(T value, valid_if_positive< F > duration) -> auto &
Sets the next point to animate into and the duration of the transition.
Definition: animated_value.hpp:41
animated_value(const T &initial)
Initializing to the specified value.
Definition: animated_value.hpp:26
auto set(T value, valid_if_positive< F > duration, S slope) -> auto &
Sets the next point to animate into, duration and sigmoid slope.
Definition: animated_value.hpp:31
auto get() const -> T
Gets the current value between the current and the next point.
Definition: animated_value.hpp:46

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).