FTXUI  5.0.0
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
9
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
18using Clock = std::chrono::steady_clock;
19using TimePoint = std::chrono::time_point<Clock>;
20using Duration = std::chrono::duration<float>;
21
22// Parameter of Component::OnAnimation(param).
23class 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
34namespace easing {
35using Function = std::function<float(float)>;
36// Linear interpolation (no easing)
37float Linear(float p);
38
39// Quadratic easing; p^2
40float QuadraticIn(float p);
41float QuadraticOut(float p);
42float QuadraticInOut(float p);
43
44// Cubic easing; p^3
45float CubicIn(float p);
46float CubicOut(float p);
47float CubicInOut(float p);
48
49// Quartic easing; p^4
50float QuarticIn(float p);
51float QuarticOut(float p);
52float QuarticInOut(float p);
53
54// Quintic easing; p^5
55float QuinticIn(float p);
56float QuinticOut(float p);
57float QuinticInOut(float p);
58
59// Sine wave easing; sin(p * PI/2)
60float SineIn(float p);
61float SineOut(float p);
62float SineInOut(float p);
63
64// Circular easing; sqrt(1 - p^2)
65float CircularIn(float p);
66float CircularOut(float p);
67float CircularInOut(float p);
68
69// Exponential easing, base 2
70float ExponentialIn(float p);
71float ExponentialOut(float p);
72float ExponentialInOut(float p);
73
74// Exponentially-damped sine wave easing
75float ElasticIn(float p);
76float ElasticOut(float p);
77float ElasticInOut(float p);
78
79// Overshooting cubic easing;
80float BackIn(float p);
81float BackOut(float p);
82float BackInOut(float p);
83
84// Exponentially-decaying bounce easing
85float BounceIn(float p);
86float BounceOut(float p);
87float BounceInOut(float p);
88} // namespace easing
89
90class 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 */
Params(Duration duration)
Definition animation.hpp:25
Duration duration() const
The duration this animation step represents.
Definition animation.hpp:28
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
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26