FTXUI  5.0.0
C++ functional terminal UI.
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
5
6// NOLINTBEGIN(*-magic-numbers)
7namespace ftxui::animation {
8
9namespace easing {
10
11namespace {
12constexpr float kPi = 3.14159265358979323846f;
13constexpr 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
29float Linear(float p) {
30 return p;
31}
32
33/// @brief Modeled after the parabola y = x^2
34float QuadraticIn(float p) {
35 return p * p;
36}
37
38// @brief Modeled after the parabola y = -x^2 + 2x
39float 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]
46float 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
51float CubicIn(float p) {
52 return p * p * p;
53}
54
55// @brief Modeled after the cubic y = (x - 1)^3 + 1
56float 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]
64float 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
73float QuarticIn(float p) {
74 return p * p * p * p;
75}
76
77// @brief Modeled after the quartic y = 1 - (x - 1)^4
78float 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]
86float 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
95float QuinticIn(float p) {
96 return p * p * p * p * p;
97}
98
99// @brief Modeled after the quintic y = (x - 1)^5 + 1
100float 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]
108float 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
117float 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)
122float SineOut(float p) {
123 return std::sin(p * kPi2);
124}
125
126// @brief Modeled after half sine wave
127float 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
132float 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
137float 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]
144float 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))
152float 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
157float 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]
164float 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))
177float 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
184float 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]
191float 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)
202float 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))
207float 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]
215float 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
224float BounceIn(float p) {
225 return 1.f - BounceOut(1.f - p);
226}
227
228float 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
244float 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
254 float to,
255 Duration duration,
256 easing::Function easing_function,
258 : value_(from),
259 from_(*from),
260 to_(to),
261 duration_(duration),
262 easing_function_(std::move(easing_function)),
263 current_(-delay) {
265}
266
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
282}
283
284} // namespace ftxui::animation
285
286// NOLINTEND(*-magic-numbers)
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))
std::chrono::duration< float > Duration
Definition animation.hpp:20
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26