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 
10 namespace ftxui::animation {
11 // Components who haven't completed their animation can call this function to
12 // request a new frame to be drawn later.
13 //
14 // When there is no new events and no animations to complete, no new frame is
15 // drawn.
17 
18 using Clock = std::chrono::steady_clock;
19 using TimePoint = std::chrono::time_point<Clock>;
20 using Duration = std::chrono::duration<float>;
21 
22 // Parameter of Component::OnAnimation(param).
23 class Params {
24  public:
25  explicit Params(Duration duration) : duration_(duration) {}
26 
27  /// The duration this animation step represents.
28  Duration duration() const { return duration_; }
29 
30  private:
31  Duration duration_;
32 };
33 
34 namespace easing {
35 using Function = std::function<float(float)>;
36 // Linear interpolation (no easing)
37 float Linear(float p);
38 
39 // Quadratic easing; p^2
40 float QuadraticIn(float p);
41 float QuadraticOut(float p);
42 float QuadraticInOut(float p);
43 
44 // Cubic easing; p^3
45 float CubicIn(float p);
46 float CubicOut(float p);
47 float CubicInOut(float p);
48 
49 // Quartic easing; p^4
50 float QuarticIn(float p);
51 float QuarticOut(float p);
52 float QuarticInOut(float p);
53 
54 // Quintic easing; p^5
55 float QuinticIn(float p);
56 float QuinticOut(float p);
57 float QuinticInOut(float p);
58 
59 // Sine wave easing; sin(p * PI/2)
60 float SineIn(float p);
61 float SineOut(float p);
62 float SineInOut(float p);
63 
64 // Circular easing; sqrt(1 - p^2)
65 float CircularIn(float p);
66 float CircularOut(float p);
67 float CircularInOut(float p);
68 
69 // Exponential easing, base 2
70 float ExponentialIn(float p);
71 float ExponentialOut(float p);
72 float ExponentialInOut(float p);
73 
74 // Exponentially-damped sine wave easing
75 float ElasticIn(float p);
76 float ElasticOut(float p);
77 float ElasticInOut(float p);
78 
79 // Overshooting cubic easing;
80 float BackIn(float p);
81 float BackOut(float p);
82 float BackInOut(float p);
83 
84 // Exponentially-decaying bounce easing
85 float BounceIn(float p);
86 float BounceOut(float p);
87 float BounceInOut(float p);
88 } // namespace easing
89 
90 class Animator {
91  public:
92  explicit Animator(float* from,
93  float to = 0.f,
94  Duration duration = std::chrono::milliseconds(250),
95  easing::Function easing_function = easing::Linear,
96  Duration delay = std::chrono::milliseconds(0));
97 
98  void OnAnimation(Params&);
99 
100  float to() const { return to_; }
101 
102  private:
103  float* value_;
104  float from_;
105  float to_;
106  Duration duration_;
107  easing::Function easing_function_;
108  Duration current_;
109 };
110 
111 } // namespace ftxui::animation
112 
113 #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:253
void OnAnimation(Params &)
Definition: animation.cpp:267
Params(Duration duration)
Definition: animation.hpp:25
Duration duration() const
The duration this animation step represents.
Definition: animation.hpp:28
float ElasticIn(float p)
Definition: animation.cpp:177
float CircularInOut(float p)
Definition: animation.cpp:144
float SineInOut(float p)
Definition: animation.cpp:127
float BounceInOut(float p)
Definition: animation.cpp:244
float CubicIn(float p)
Definition: animation.cpp:51
float CubicInOut(float p)
Definition: animation.cpp:64
float Linear(float p)
Modeled after the line y = x.
Definition: animation.cpp:29
float BounceIn(float p)
Definition: animation.cpp:224
float CircularOut(float p)
Definition: animation.cpp:137
float BackOut(float p)
Definition: animation.cpp:207
float BackInOut(float p)
Definition: animation.cpp:215
float ExponentialInOut(float p)
Definition: animation.cpp:164
float ElasticInOut(float p)
Definition: animation.cpp:191
float QuarticInOut(float p)
Definition: animation.cpp:86
float QuadraticInOut(float p)
Definition: animation.cpp:46
float QuarticOut(float p)
Definition: animation.cpp:78
float SineOut(float p)
Definition: animation.cpp:122
float CircularIn(float p)
Definition: animation.cpp:132
float ExponentialOut(float p)
Definition: animation.cpp:157
float QuadraticOut(float p)
Definition: animation.cpp:39
float QuinticOut(float p)
Definition: animation.cpp:100
float QuadraticIn(float p)
Modeled after the parabola y = x^2.
Definition: animation.cpp:34
float ExponentialIn(float p)
Definition: animation.cpp:152
float QuinticIn(float p)
Definition: animation.cpp:95
float BounceOut(float p)
Definition: animation.cpp:228
float CubicOut(float p)
Definition: animation.cpp:56
float ElasticOut(float p)
Definition: animation.cpp:184
std::function< float(float)> Function
Definition: animation.hpp:35
float BackIn(float p)
Definition: animation.cpp:202
float SineIn(float p)
Definition: animation.cpp:117
float QuarticIn(float p)
Definition: animation.cpp:73
float QuinticInOut(float p)
Definition: animation.cpp:108
std::chrono::steady_clock Clock
Definition: animation.hpp:18
std::chrono::duration< float > Duration
Definition: animation.hpp:20
std::chrono::time_point< Clock > TimePoint
Definition: animation.hpp:19