FTXUI  5.0.0
C++ functional terminal UI.
animation.hpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#ifndef FTXUI_ANIMATION_HPP
5#define FTXUI_ANIMATION_HPP
6
7#include <chrono> // for milliseconds, duration, steady_clock, time_point
8#include <functional> // for function
9
11
12namespace ftxui {
13
14namespace animation {
15// Components who haven't completed their animation can call this function to
16// request a new frame to be drawn later.
17//
18// When there is no new events and no animations to complete, no new frame is
19// drawn.
21
22using Clock = std::chrono::steady_clock;
23using TimePoint = std::chrono::time_point<Clock>;
24using Duration = std::chrono::duration<float>;
25
26// Parameter of Component::OnAnimation(param).
27class Params {
28 public:
30
31 /// The duration this animation step represents.
32 Duration duration() const { return duration_; }
33
34 private:
35 Duration duration_;
36};
37
38namespace easing {
39using Function = std::function<float(float)>;
40// Linear interpolation (no easing)
41float Linear(float p);
42
43// Quadratic easing; p^2
44float QuadraticIn(float p);
45float QuadraticOut(float p);
46float QuadraticInOut(float p);
47
48// Cubic easing; p^3
49float CubicIn(float p);
50float CubicOut(float p);
51float CubicInOut(float p);
52
53// Quartic easing; p^4
54float QuarticIn(float p);
55float QuarticOut(float p);
56float QuarticInOut(float p);
57
58// Quintic easing; p^5
59float QuinticIn(float p);
60float QuinticOut(float p);
61float QuinticInOut(float p);
62
63// Sine wave easing; sin(p * PI/2)
64float SineIn(float p);
65float SineOut(float p);
66float SineInOut(float p);
67
68// Circular easing; sqrt(1 - p^2)
69float CircularIn(float p);
70float CircularOut(float p);
71float CircularInOut(float p);
72
73// Exponential easing, base 2
74float ExponentialIn(float p);
75float ExponentialOut(float p);
76float ExponentialInOut(float p);
77
78// Exponentially-damped sine wave easing
79float ElasticIn(float p);
80float ElasticOut(float p);
81float ElasticInOut(float p);
82
83// Overshooting cubic easing;
84float BackIn(float p);
85float BackOut(float p);
86float BackInOut(float p);
87
88// Exponentially-decaying bounce easing
89float BounceIn(float p);
90float BounceOut(float p);
91float BounceInOut(float p);
92} // namespace easing
93
94class Animator {
95 public:
96 Animator(float* from,
97 float to = 0.f,
98 Duration duration = std::chrono::milliseconds(250),
99 easing::Function easing_function = easing::Linear,
100 Duration delay = std::chrono::milliseconds(0));
101
102 void OnAnimation(Params&);
103
104 float to() const { return to_; }
105
106 private:
107 float* value_;
108 float from_;
109 float to_;
110 Duration duration_;
111 easing::Function easing_function_;
112 Duration current_;
113};
114
115} // namespace animation
116} // namespace ftxui
117
118#endif /* end of include guard: FTXUI_ANIMATION_HPP */
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:254
void OnAnimation(Params &)
Definition: animation.cpp:268
Params(Duration duration)
Definition: animation.hpp:29
Duration duration() const
The duration this animation step represents.
Definition: animation.hpp:32
float ElasticIn(float p)
Definition: animation.cpp:178
float CircularInOut(float p)
Definition: animation.cpp:145
float SineInOut(float p)
Definition: animation.cpp:128
float BounceInOut(float p)
Definition: animation.cpp:245
float CubicIn(float p)
Definition: animation.cpp:52
float CubicInOut(float p)
Definition: animation.cpp:65
float Linear(float p)
Modeled after the line y = x.
Definition: animation.cpp:30
float BounceIn(float p)
Definition: animation.cpp:225
float CircularOut(float p)
Definition: animation.cpp:138
float BackOut(float p)
Definition: animation.cpp:208
float BackInOut(float p)
Definition: animation.cpp:216
float ExponentialInOut(float p)
Definition: animation.cpp:165
float ElasticInOut(float p)
Definition: animation.cpp:192
float QuarticInOut(float p)
Definition: animation.cpp:87
float QuadraticInOut(float p)
Definition: animation.cpp:47
float QuarticOut(float p)
Definition: animation.cpp:79
float SineOut(float p)
Definition: animation.cpp:123
float CircularIn(float p)
Definition: animation.cpp:133
float ExponentialOut(float p)
Definition: animation.cpp:158
float QuadraticOut(float p)
Definition: animation.cpp:40
float QuinticOut(float p)
Definition: animation.cpp:101
float QuadraticIn(float p)
Modeled after the parabola y = x^2.
Definition: animation.cpp:35
float ExponentialIn(float p)
Definition: animation.cpp:153
float QuinticIn(float p)
Definition: animation.cpp:96
float BounceOut(float p)
Definition: animation.cpp:229
float CubicOut(float p)
Definition: animation.cpp:57
float ElasticOut(float p)
Definition: animation.cpp:185
std::function< float(float)> Function
Definition: animation.hpp:39
float BackIn(float p)
Definition: animation.cpp:203
float SineIn(float p)
Definition: animation.cpp:118
float QuarticIn(float p)
Definition: animation.cpp:74
float QuinticInOut(float p)
Definition: animation.cpp:109
std::chrono::steady_clock Clock
Definition: animation.hpp:22
std::chrono::duration< float > Duration
Definition: animation.hpp:24
std::chrono::time_point< Clock > TimePoint
Definition: animation.hpp:23