FTXUI
6.1.9
C++ functional terminal UI.
Toggle main menu visibility
Loading...
Searching...
No Matches
animation.cpp
Go to the documentation of this file.
1
#include <cmath>
// for sin, pow, sqrt, cos
2
#include <utility>
// for move
3
4
#include "
ftxui/component/animation.hpp
"
5
6
// NOLINTBEGIN(*-magic-numbers)
7
namespace
ftxui::animation
{
8
9
namespace
easing
{
10
11
namespace
{
12
constexpr
float
kPi = 3.14159265358979323846f;
13
constexpr
float
kPi2 = kPi / 2.f;
14
}
// namespace
15
16
// Easing function have been taken out of:
17
// https://github.com/warrenm/AHEasing/blob/master/AHEasing/easing.c
18
//
19
// Corresponding license:
20
// Copyright (c) 2011, Auerhaus Development, LLC
21
//
22
// This program is free software. It comes without any warranty, to
23
// the extent permitted by applicable law. You can redistribute it
24
// and/or modify it under the terms of the Do What The Fuck You Want
25
// To Public License, Version 2, as published by Sam Hocevar. See
26
// http://sam.zoy.org/wtfpl/COPYING for more details.
27
28
/// @brief Modeled after the line y = x
29
float
Linear
(
float
p) {
30
return
p;
31
}
32
33
/// @brief Modeled after the parabola y = x^2
34
float
QuadraticIn
(
float
p) {
35
return
p * p;
36
}
37
38
// @brief Modeled after the parabola y = -x^2 + 2x
39
float
QuadraticOut
(
float
p) {
40
return
-(p * (p - 2.f));
41
}
42
43
// @brief Modeled after the piecewise quadratic
44
// y = (1/2)((2x)^2) ; [0, 0.5)
45
// y = -(1/2)((2x-1)*(2x-3) - 1) ; [0.5, 1]
46
float
QuadraticInOut
(
float
p) {
47
return
p < 0.5f ? 2.f * p * p : (-2.f * p * p) + (4.f * p) - 1.f;
48
}
49
50
// @brief Modeled after the cubic y = x^3
51
float
CubicIn
(
float
p) {
52
return
p * p * p;
53
}
54
55
// @brief Modeled after the cubic y = (x - 1)^3 + 1
56
float
CubicOut
(
float
p) {
57
const
float
f = (p - 1.f);
58
return
f * f * f + 1.f;
59
}
60
61
// @brief Modeled after the piecewise cubic
62
// y = (1/2)((2x)^3) ; [0, 0.5)
63
// y = (1/2)((2x-2)^3 + 2) ; [0.5, 1]
64
float
CubicInOut
(
float
p) {
65
if
(p < 0.5f) {
66
return
4.f * p * p * p;
67
}
68
const
float
f = ((2.f * p) - 2.f);
69
return
0.5f * f * f * f + 1.f;
70
}
71
72
// @brief Modeled after the quartic x^4
73
float
QuarticIn
(
float
p) {
74
return
p * p * p * p;
75
}
76
77
// @brief Modeled after the quartic y = 1 - (x - 1)^4
78
float
QuarticOut
(
float
p) {
79
const
float
f = (p - 1.f);
80
return
f * f * f * (1.f - p) + 1.f;
81
}
82
83
// @brief Modeled after the piecewise quartic
84
// y = (1/2)((2x)^4) ; [0, 0.5)
85
// y = -(1/2)((2x-2)^4 - 2) ; [0.5, 1]
86
float
QuarticInOut
(
float
p) {
87
if
(p < 0.5f) {
88
return
8.f * p * p * p * p;
89
}
90
const
float
f = (p - 1.f);
91
return
-8.f * f * f * f * f + 1.f;
92
}
93
94
// @brief Modeled after the quintic y = x^5
95
float
QuinticIn
(
float
p) {
96
return
p * p * p * p * p;
97
}
98
99
// @brief Modeled after the quintic y = (x - 1)^5 + 1
100
float
QuinticOut
(
float
p) {
101
const
float
f = (p - 1.f);
102
return
f * f * f * f * f + 1.f;
103
}
104
105
// @brief Modeled after the piecewise quintic
106
// y = (1/2)((2x)^5) ; [0, 0.5)
107
// y = (1/2)((2x-2)^5 + 2) ; [0.5, 1]
108
float
QuinticInOut
(
float
p) {
109
if
(p < 0.5f) {
110
return
16.f * p * p * p * p * p;
111
}
112
const
float
f = ((2.f * p) - 2.f);
113
return
0.5f * f * f * f * f * f + 1.f;
114
}
115
116
// @brief Modeled after quarter-cycle of sine wave
117
float
SineIn
(
float
p) {
118
return
std::sin((p - 1.f) * kPi2) + 1.f;
119
}
120
121
// @brief Modeled after quarter-cycle of sine wave (different phase)
122
float
SineOut
(
float
p) {
123
return
std::sin(p * kPi2);
124
}
125
126
// @brief Modeled after half sine wave
127
float
SineInOut
(
float
p) {
128
return
0.5f * (1.f - std::cos(p * kPi));
129
}
130
131
// @brief Modeled after shifted quadrant IV of unit circle
132
float
CircularIn
(
float
p) {
133
return
1.f - std::sqrt(1.f - (p * p));
134
}
135
136
// @brief Modeled after shifted quadrant II of unit circle
137
float
CircularOut
(
float
p) {
138
return
std::sqrt((2.f - p) * p);
139
}
140
141
// @brief Modeled after the piecewise circular function
142
// y = (1/2)(1 - sqrt(1 - 4x^2)) ; [0, 0.5)
143
// y = (1/2)(sqrt(-(2x - 3)*(2x - 1)) + 1) ; [0.5, 1]
144
float
CircularInOut
(
float
p) {
145
if
(p < 0.5f) {
146
return
0.5f * (1.f - std::sqrt(1.f - 4.f * (p * p)));
147
}
148
return
0.5f * (std::sqrt(-((2.f * p) - 3.f) * ((2.f * p) - 1.f)) + 1.f);
149
}
150
151
// @brief Modeled after the exponential function y = 2^(10(x - 1))
152
float
ExponentialIn
(
float
p) {
153
return
(p == 0.f) ? p : std::pow(2.f, 10.f * (p - 1.f));
154
}
155
156
// @brief Modeled after the exponential function y = -2^(-10x) + 1
157
float
ExponentialOut
(
float
p) {
158
return
(p == 1.f) ? p : 1.f - std::pow(2.f, -10.f * p);
159
}
160
161
// @brief Modeled after the piecewise exponential
162
// y = (1/2)2^(10(2x - 1)) ; [0,0.5)
163
// y = -(1/2)*2^(-10(2x - 1))) + 1 ; [0.5,1]
164
float
ExponentialInOut
(
float
p) {
165
if
(p == 0.f || p == 1.f) {
166
return
p;
167
}
168
169
if
(p < 0.5f) {
170
return
0.5f * std::pow(2.f, (20.f * p) - 10.f);
171
}
172
return
-0.5f * std::pow(2.f, (-20.f * p) + 10.f) + 1.f;
173
}
174
175
// @brief Modeled after the damped sine wave y = sin(13pi/2*x)*pow(2, 10 * (x -
176
// 1))
177
float
ElasticIn
(
float
p) {
178
return
std::sin(13.f * kPi2 * p) * std::pow(2.f, 10.f * (p - 1.f));
179
}
180
181
// @brief Modeled after the damped sine wave y = sin(-13pi/2*(x + 1))*pow(2,
182
// -10x) +
183
// 1
184
float
ElasticOut
(
float
p) {
185
return
std::sin(-13.f * kPi2 * (p + 1.f)) * std::pow(2.f, -10.f * p) + 1.f;
186
}
187
188
// @brief Modeled after the piecewise exponentially-damped sine wave:
189
// y = (1/2)*sin(13pi/2*(2*x))*pow(2, 10 * ((2*x) - 1)) ; [0,0.5)
190
// y = (1/2)*(sin(-13pi/2*((2x-1)+1))*pow(2,-10(2*x-1)) + 2) ; [0.5, 1]
191
float
ElasticInOut
(
float
p) {
192
if
(p < 0.5f) {
193
return
0.5f * std::sin(13.f * kPi2 * (2.f * p)) *
194
std::pow(2.f, 10.f * ((2.f * p) - 1.f));
195
}
196
return
0.5f * (std::sin(-13.f * kPi2 * ((2.f * p - 1.f) + 1.f)) *
197
std::pow(2.f, -10.f * (2.f * p - 1.f)) +
198
2.f);
199
}
200
201
// @brief Modeled after the overshooting cubic y = x^3-x*sin(x*pi)
202
float
BackIn
(
float
p) {
203
return
p * p * p - p * std::sin(p * kPi);
204
}
205
206
// @brief Modeled after overshooting cubic y = 1-((1-x)^3-(1-x)*sin((1-x)*pi))
207
float
BackOut
(
float
p) {
208
const
float
f = (1.f - p);
209
return
1.f - (f * f * f - f * std::sin(f * kPi));
210
}
211
212
// @brief Modeled after the piecewise overshooting cubic function:
213
// y = (1/2)*((2x)^3-(2x)*sin(2*x*pi)) ; [0, 0.5)
214
// y = (1/2)*(1-((1-x)^3-(1-x)*sin((1-x)*pi))+1) ; [0.5, 1]
215
float
BackInOut
(
float
p) {
216
if
(p < 0.5f) {
217
const
float
f = 2.f * p;
218
return
0.5f * (f * f * f - f * std::sin(f * kPi));
219
}
220
const
float
f = (1.f - (2.f * p - 1.f));
221
return
0.5f * (1.f - (f * f * f - f * std::sin(f * kPi))) + 0.5f;
222
}
223
224
float
BounceIn
(
float
p) {
225
return
1.f -
BounceOut
(1.f - p);
226
}
227
228
float
BounceOut
(
float
p) {
229
if
(p < 4.f / 11.f) {
230
return
(121.f * p * p) / 16.f;
231
}
232
233
if
(p < 8.f / 11.f) {
234
return
(363.f / 40.f * p * p) - (99.f / 10.f * p) + 17.f / 5.f;
235
}
236
237
if
(p < 9.f / 10.f) {
238
return
(4356.f / 361.f * p * p) - (35442.f / 1805.f * p) + 16061.f / 1805.f;
239
}
240
241
return
(54.f / 5.f * p * p) - (513 / 25.f * p) + 268 / 25.f;
242
}
243
244
float
BounceInOut
(
float
p) {
245
if
(p < 0.5f) {
246
return
0.5f *
BounceIn
(p * 2.f);
247
}
248
return
0.5f *
BounceOut
(p * 2.f - 1.f) + 0.5f;
249
}
250
251
}
// namespace easing
252
253
Animator::Animator
(
float
* from,
254
float
to
,
255
Duration
duration,
256
easing::Function
easing_function,
257
Duration
delay)
258
: value_(from),
259
from_(*from),
260
to_(
to
),
261
duration_(duration),
262
easing_function_(std::move(easing_function)),
263
current_(-delay) {
264
RequestAnimationFrame
();
265
}
266
267
void
Animator::OnAnimation
(
Params
& params) {
268
current_ += params.
duration
();
269
270
if
(current_ >= duration_) {
271
*value_ = to_;
272
return
;
273
}
274
275
if
(current_ <=
Duration
()) {
276
*value_ = from_;
277
}
else
{
278
*value_ = from_ + (to_ - from_) * easing_function_(current_ / duration_);
279
}
280
281
RequestAnimationFrame
();
282
}
283
284
}
// namespace ftxui::animation
285
286
// NOLINTEND(*-magic-numbers)
animation.hpp
ftxui::animation::Animator::Animator
Animator(float *from, float to=0.f, Duration duration=std::chrono::milliseconds(250), easing::Function easing_function=easing::Linear, Duration delay=std::chrono::milliseconds(0))
Definition
animation.cpp:253
ftxui::animation::Animator::to
float to() const
Definition
animation.hpp:100
ftxui::animation::Animator::OnAnimation
void OnAnimation(Params &)
Definition
animation.cpp:267
ftxui::animation::Params
Definition
animation.hpp:23
ftxui::animation::Params::duration
Duration duration() const
The duration this animation step represents.
Definition
animation.hpp:28
ftxui::animation::easing
Definition
animation.hpp:34
ftxui::animation::easing::ElasticIn
float ElasticIn(float p)
Definition
animation.cpp:177
ftxui::animation::easing::CircularInOut
float CircularInOut(float p)
Definition
animation.cpp:144
ftxui::animation::easing::SineInOut
float SineInOut(float p)
Definition
animation.cpp:127
ftxui::animation::easing::BounceInOut
float BounceInOut(float p)
Definition
animation.cpp:244
ftxui::animation::easing::CubicIn
float CubicIn(float p)
Definition
animation.cpp:51
ftxui::animation::easing::CubicInOut
float CubicInOut(float p)
Definition
animation.cpp:64
ftxui::animation::easing::Linear
float Linear(float p)
Modeled after the line y = x.
Definition
animation.cpp:29
ftxui::animation::easing::BounceIn
float BounceIn(float p)
Definition
animation.cpp:224
ftxui::animation::easing::CircularOut
float CircularOut(float p)
Definition
animation.cpp:137
ftxui::animation::easing::BackOut
float BackOut(float p)
Definition
animation.cpp:207
ftxui::animation::easing::BackInOut
float BackInOut(float p)
Definition
animation.cpp:215
ftxui::animation::easing::ExponentialInOut
float ExponentialInOut(float p)
Definition
animation.cpp:164
ftxui::animation::easing::ElasticInOut
float ElasticInOut(float p)
Definition
animation.cpp:191
ftxui::animation::easing::QuarticInOut
float QuarticInOut(float p)
Definition
animation.cpp:86
ftxui::animation::easing::QuadraticInOut
float QuadraticInOut(float p)
Definition
animation.cpp:46
ftxui::animation::easing::QuarticOut
float QuarticOut(float p)
Definition
animation.cpp:78
ftxui::animation::easing::SineOut
float SineOut(float p)
Definition
animation.cpp:122
ftxui::animation::easing::CircularIn
float CircularIn(float p)
Definition
animation.cpp:132
ftxui::animation::easing::ExponentialOut
float ExponentialOut(float p)
Definition
animation.cpp:157
ftxui::animation::easing::QuadraticOut
float QuadraticOut(float p)
Definition
animation.cpp:39
ftxui::animation::easing::QuinticOut
float QuinticOut(float p)
Definition
animation.cpp:100
ftxui::animation::easing::QuadraticIn
float QuadraticIn(float p)
Modeled after the parabola y = x^2.
Definition
animation.cpp:34
ftxui::animation::easing::ExponentialIn
float ExponentialIn(float p)
Definition
animation.cpp:152
ftxui::animation::easing::QuinticIn
float QuinticIn(float p)
Definition
animation.cpp:95
ftxui::animation::easing::BounceOut
float BounceOut(float p)
Definition
animation.cpp:228
ftxui::animation::easing::CubicOut
float CubicOut(float p)
Definition
animation.cpp:56
ftxui::animation::easing::ElasticOut
float ElasticOut(float p)
Definition
animation.cpp:184
ftxui::animation::easing::Function
std::function< float(float)> Function
Definition
animation.hpp:35
ftxui::animation::easing::BackIn
float BackIn(float p)
Definition
animation.cpp:202
ftxui::animation::easing::SineIn
float SineIn(float p)
Definition
animation.cpp:117
ftxui::animation::easing::QuarticIn
float QuarticIn(float p)
Definition
animation.cpp:73
ftxui::animation::easing::QuinticInOut
float QuinticInOut(float p)
Definition
animation.cpp:108
ftxui::animation
Definition
animation.hpp:10
ftxui::animation::Duration
std::chrono::duration< float > Duration
Definition
animation.hpp:20
ftxui::animation::RequestAnimationFrame
void RequestAnimationFrame()
Definition
screen_interactive.cpp:63