1 #pragma once
2 #include <wayfire/config/option.hpp>
3 
4 namespace wf
5 {
6 namespace animation
7 {
8 namespace smoothing
9 {
10 /**
11  * A smooth function is a function which takes a double in [0, 1] and returns
12  * another double in R. Both ranges represent percentage of a progress of
13  * an animation.
14  */
15 using smooth_function = std::function<double (double)>;
16 
17 /** linear smoothing function, i.e x -> x */
18 extern smooth_function linear;
19 /** "circle" smoothing function, i.e x -> sqrt(2x - x*x) */
20 extern smooth_function circle;
21 /** "sigmoid" smoothing function, i.e x -> 1.0 / (1 + exp(-12 * x + 6)) */
22 extern smooth_function sigmoid;
23 }
24 
25 /**
26  * A transition from start to end.
27  */
28 struct transition_t
29 {
30     double start, end;
31 };
32 
33 /**
34  * duration_t is a class which can be used to track progress over a specific
35  * time interval.
36  */
37 class duration_t
38 {
39   public:
40     /**
41      * Construct a new duration.
42      * Initially, the duration is not running and its progress is 1.
43      *
44      * @param length The length of the duration in milliseconds.
45      * @param smooth The smoothing function for transitions.
46      */
47     duration_t(std::shared_ptr<wf::config::option_t<int>> length = nullptr,
48         smoothing::smooth_function smooth = smoothing::circle);
49 
50     /* Copy-constructor */
51     duration_t(const duration_t& other);
52     /* Copy-assignment */
53     duration_t& operator =(const duration_t& other);
54 
55     /* Move-constructor */
56     duration_t(duration_t&& other) = default;
57     /* Move-assignment */
58     duration_t& operator =(duration_t&& other) = default;
59 
60     /**
61      * Start the duration.
62      * This means that the progress will get reset to 0.
63      */
64     void start();
65 
66     /**
67      * Get the progress of the duration in percentage.
68      * The progress will be smoothed using the smoothing function.
69      *
70      * @return The current progress after smoothing. It is guaranteed that when
71      *   the duration starts, progress will be close to 0, and when it is
72      *   finished, it will be close to 1.
73      */
74     double progress() const;
75 
76     /**
77      * Check if the duration is still running.
78      * Note that even when the duration first finishes, this function will
79      * still return that the function is running one time.
80      *
81      * @return Whether the duration still has not elapsed.
82      */
83     bool running();
84 
85     class impl;
86     /** Implementation details. */
87     std::shared_ptr<impl> priv;
88 };
89 
90 /**
91  * A timed transition is a transition between two states which happens
92  * over a period of time.
93  *
94  * During the transition, the current state is smoothly interpolated between
95  * start and end.
96  */
97 struct timed_transition_t : public transition_t
98 {
99     /**
100      * Construct a new timed transition using the given duration to measure
101      * progress.
102      *
103      * @duration The duration to use for time measurement
104      * @start The start state.
105      * @end The end state.
106      */
107     timed_transition_t(const duration_t& duration,
108         double start = 0, double end = 0);
109 
110     /**
111      * Set the transition start to the current state and the end to the given
112      * @new_end.
113      */
114     void restart_with_end(double new_end);
115 
116     /**
117      * Set the transition start to the current state, and don't change the end.
118      */
119     void restart_same_end();
120 
121     /**
122      * Set the transition start and end state.
123      * @param start The start of the transition.
124      * @param end The end of the transition.
125      */
126     void set(double start, double end);
127 
128     /**
129      * Swap start and end values.
130      */
131     void flip();
132 
133     /**
134      * Implicitly convert the transition to its current state.
135      */
136     operator double() const;
137 
138   private:
139     std::shared_ptr<const duration_t::impl> duration;
140 };
141 
142 class simple_animation_t : public duration_t, public timed_transition_t
143 {
144   public:
145     simple_animation_t(
146         std::shared_ptr<wf::config::option_t<int>> length = nullptr,
147         smoothing::smooth_function smooth = smoothing::circle);
148 
149     /**
150      * Set the start and the end of the animation and start the duration.
151      */
152     void animate(double start, double end);
153 
154     /**
155      * Animate from the current progress to the given end, and start the
156      * duration.
157      */
158     void animate(double end);
159 
160     /**
161      * Animate from the current progress to the current end, and start the
162      * duration.
163      */
164     void animate();
165 };
166 }
167 }
168