NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
animation.hpp
浏览该文件的文档.
1#ifndef NEFORCE_TUI_COMPONENT_ANIMATION_HPP__
2#define NEFORCE_TUI_COMPONENT_ANIMATION_HPP__
3
10
13NEFORCE_BEGIN_NAMESPACE__
14NEFORCE_BEGIN_TUI__
15
20
24namespace easing {
26 using function = function<float(float)>;
27
28 inline function linear() {
29 return [](const float t) { return t; };
30 }
31
32 inline function quadratic_in() {
33 return [](const float t) { return t * t; };
34 }
35
36 inline function quadratic_out() {
37 return [](const float t) { return t * (2.0F - t); };
38 }
39
40 inline function quadratic_in_out() {
41 return [](const float t) { return (t < 0.5F) ? 2.0F * t * t : -1.0F + (4.0F - 2.0F * t) * t; };
42 }
43
44 inline function cubic_in() {
45 return [](const float t) { return t * t * t; };
46 }
47
48 inline function cubic_out() {
49 return [](const float t) {
50 const float t1 = t - 1.0F;
51 return t1 * t1 * t1 + 1.0F;
52 };
53 }
54
55 inline function cubic_in_out() {
56 return [](const float t) {
57 return (t < 0.5F) ? 4.0F * t * t * t : (t - 1.0F) * (2.0F * t - 2.0F) * (2.0F * t - 2.0F) + 1.0F;
58 };
59 }
60
61 inline function sine_in() {
62 return [](const float t) { return 1.0F - cosine(t * 3.14159265F / 2.0F); };
63 }
64
65 inline function sine_out() {
66 return [](const float t) { return sine(t * 3.14159265F / 2.0F); };
67 }
68
69 inline function sine_in_out() {
70 return [](const float t) { return -(cosine(3.14159265F * t) - 1.0F) / 2.0F; };
71 }
72
73 inline function elastic_out() {
74 return [](const float t) -> float {
75 if (t == 0.0F || t == 1.0F) {
76 return t;
77 }
78 return static_cast<float>(power(2.0F, static_cast<uint32_t>(-10.0F * t)) *
79 sine((t - 0.075F) * 6.2831853F / 0.3F)) +
80 1.0F;
81 };
82 }
83
84 inline function bounce_out() {
85 return [](float t) {
86 if (t < 1.0F / 2.75F) {
87 return 7.5625F * t * t;
88 }
89 if (t < 2.0F / 2.75F) {
90 t -= 1.5F / 2.75F;
91 return 7.5625F * t * t + 0.75F;
92 }
93 if (t < 2.5F / 2.75F) {
94 t -= 2.25F / 2.75F;
95 return 7.5625F * t * t + 0.9375F;
96 }
97 t -= 2.625F / 2.75F;
98 return 7.5625F * t * t + 0.984375F;
99 };
100 }
101} // namespace easing
102
103
110class NEFORCE_API animator {
111public:
120 animator(float* value, float to, milliseconds dur, easing::function easing_fn = easing::quadratic_out(),
121 milliseconds delay = 0_ms);
122
129
134 NEFORCE_NODISCARD float to() const noexcept { return to_; }
135
140 NEFORCE_NODISCARD bool is_done() const noexcept { return done_; }
141
147 void reset(float to, milliseconds dur);
148
149private:
150 float* value_;
151 float from_;
152 float to_;
153 milliseconds duration_ms_;
154 milliseconds delay_ms_;
155 milliseconds elapsed_ms_ = 0_ms;
156 bool done_ = false;
157 bool started_ = false;
158 easing::function easing_fn_;
159};
160 // TUI
162
163NEFORCE_END_TUI__
164NEFORCE_END_NAMESPACE__
165#endif // NEFORCE_TUI_COMPONENT_ANIMATION_HPP__
函数包装器主模板声明
animator(float *value, float to, milliseconds dur, easing::function easing_fn=easing::quadratic_out(), milliseconds delay=0_ms)
构造函数
bool is_done() const noexcept
检查动画是否已完成
bool on_animation(milliseconds delta)
每帧调用以推进动画
float to() const noexcept
获取目标值
void reset(float to, milliseconds dur)
重置动画
持续时间类型
通用函数包装器
unsigned int uint32_t
32位无符号整数类型
duration< int64_t, milli > milliseconds
毫秒持续时间
constexpr decimal_t sine(decimal_t x) noexcept
计算正弦值
constexpr decimal_t cosine(decimal_t x) noexcept
计算余弦值
constexpr T power(const T &x, uint32_t n) noexcept
幂运算
缓动函数命名空间
function< float(float)> function
缓动函数类型:输入归一化时间 [0,1],输出进度 [0,1]