FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
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
10
12/// @brief RequestAnimationFrame is a function that requests a new frame to be
13/// drawn in the next animation cycle.
14///
15/// @note This function is typically called by components that need to
16/// update their state or appearance over time, such as animations or
17/// transitions. This is useful when the change doesn't depend depend on the
18/// events seen by the terminal, but rather on the passage of time.
19///
20/// Components who haven't completed their animation can call this function to
21/// request a new frame to be drawn later.
22///
23/// When there is no new events and no animations to complete, no new frame is
24/// drawn.
25///
26/// @ingroup component
28
29using Clock = std::chrono::steady_clock;
30using TimePoint = std::chrono::time_point<Clock>;
31using Duration = std::chrono::duration<float>;
32
33// Parameter of Component::OnAnimation(param).
34class FTXUI_EXPORT(COMPONENT) Params {
35 public:
36 explicit Params(Duration duration) : duration_(duration) {}
37
38 /// The duration this animation step represents.
39 Duration duration() const { return duration_; }
40
41 private:
42 Duration duration_;
43};
44
45namespace easing {
46using Function = std::function<float(float)>;
47// Linear interpolation (no easing)
48FTXUI_EXPORT(COMPONENT) float Linear(float p);
49
50// Quadratic easing; p^2
51FTXUI_EXPORT(COMPONENT) float QuadraticIn(float p);
52FTXUI_EXPORT(COMPONENT) float QuadraticOut(float p);
53FTXUI_EXPORT(COMPONENT) float QuadraticInOut(float p);
54
55// Cubic easing; p^3
56FTXUI_EXPORT(COMPONENT) float CubicIn(float p);
57FTXUI_EXPORT(COMPONENT) float CubicOut(float p);
58FTXUI_EXPORT(COMPONENT) float CubicInOut(float p);
59
60// Quartic easing; p^4
61FTXUI_EXPORT(COMPONENT) float QuarticIn(float p);
62FTXUI_EXPORT(COMPONENT) float QuarticOut(float p);
63FTXUI_EXPORT(COMPONENT) float QuarticInOut(float p);
64
65// Quintic easing; p^5
66FTXUI_EXPORT(COMPONENT) float QuinticIn(float p);
67FTXUI_EXPORT(COMPONENT) float QuinticOut(float p);
68FTXUI_EXPORT(COMPONENT) float QuinticInOut(float p);
69
70// Sine wave easing; sin(p * PI/2)
71FTXUI_EXPORT(COMPONENT) float SineIn(float p);
72FTXUI_EXPORT(COMPONENT) float SineOut(float p);
73FTXUI_EXPORT(COMPONENT) float SineInOut(float p);
74
75// Circular easing; sqrt(1 - p^2)
76FTXUI_EXPORT(COMPONENT) float CircularIn(float p);
77FTXUI_EXPORT(COMPONENT) float CircularOut(float p);
78FTXUI_EXPORT(COMPONENT) float CircularInOut(float p);
79
80// Exponential easing, base 2
81FTXUI_EXPORT(COMPONENT) float ExponentialIn(float p);
82FTXUI_EXPORT(COMPONENT) float ExponentialOut(float p);
83FTXUI_EXPORT(COMPONENT) float ExponentialInOut(float p);
84
85// Exponentially-damped sine wave easing
86FTXUI_EXPORT(COMPONENT) float ElasticIn(float p);
87FTXUI_EXPORT(COMPONENT) float ElasticOut(float p);
88FTXUI_EXPORT(COMPONENT) float ElasticInOut(float p);
89
90// Overshooting cubic easing;
91FTXUI_EXPORT(COMPONENT) float BackIn(float p);
92FTXUI_EXPORT(COMPONENT) float BackOut(float p);
93FTXUI_EXPORT(COMPONENT) float BackInOut(float p);
94
95// Exponentially-decaying bounce easing
96FTXUI_EXPORT(COMPONENT) float BounceIn(float p);
97FTXUI_EXPORT(COMPONENT) float BounceOut(float p);
98FTXUI_EXPORT(COMPONENT) float BounceInOut(float p);
99} // namespace easing
100
101class FTXUI_EXPORT(COMPONENT) Animator {
102 public:
103 explicit Animator(float* from,
104 float to = 0.f,
105 Duration duration = std::chrono::milliseconds(250),
106 easing::Function easing_function = easing::Linear,
107 Duration delay = std::chrono::milliseconds(0));
108
109 void OnAnimation(Params&);
110
111 float to() const { return to_; }
112
113 private:
114 float* value_;
115 float from_;
116 float to_;
117 Duration duration_;
118 easing::Function easing_function_;
119 Duration current_;
120};
121
122} // namespace ftxui::animation
123
124#endif /* end of include guard: FTXUI_ANIMATION_HPP */
#define FTXUI_EXPORT(component)
Definition export.hpp:24
The FTXUI ftxui::animation::easing:: namespace.
std::function< float(float)> Function
Definition animation.hpp:46
std::chrono::steady_clock Clock
Definition animation.hpp:29
std::chrono::duration< float > Duration
Definition animation.hpp:31
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:30
void RequestAnimationFrame()
Definition app.cpp:73
std::string value_