-
Notifications
You must be signed in to change notification settings - Fork 1
/
easer.cc
200 lines (185 loc) · 4.44 KB
/
easer.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <gtk/gtk.h>
#include "easer.h"
#include "v4l2_wayland.h"
#include "dingle_dots.h"
#include "vwdrawable.h"
Easer::Easer() {}
EasingFuncPtr Easer::easer_type_to_easing_func(Easer_Type type)
{
EasingFuncPtr func;
switch (type) {
case EASER_LINEAR:
func = &LinearInterpolation;
break;
case EASER_QUAD_EASE_IN:
func = &QuadraticEaseIn;
break;
case EASER_QUAD_EASE_OUT:
func = &QuadraticEaseOut;
break;
case EASER_QUAD_EASE_IN_OUT:
func = &QuadraticEaseInOut;
break;
case EASER_CUBIC_EASE_IN:
func = & CubicEaseIn;
break;
case EASER_CUBIC_EASE_OUT:
func = &CubicEaseOut;
break;
case EASER_CUBIC_EASE_IN_OUT:
func = &CubicEaseInOut;
break;
case EASER_QUARTIC_EASE_IN:
func = &QuarticEaseIn;
break;
case EASER_QUARTIC_EASE_OUT:
func = &QuarticEaseOut;
break;
case EASER_QUARTIC_EASE_IN_OUT:
func = &QuarticEaseInOut;
break;
case EASER_QUINTIC_EASE_IN:
func = &QuinticEaseIn;
break;
case EASER_QUINTIC_EASE_OUT:
func = &QuinticEaseOut;
break;
case EASER_QUINTIC_EASE_IN_OUT:
func = &QuinticEaseInOut;
break;
case EASER_SINE_EASE_IN:
func = &SineEaseIn;
break;
case EASER_SINE_EASE_OUT:
func = &SineEaseOut;
break;
case EASER_SINE_EASE_IN_OUT:
func = &SineEaseInOut;
break;
case EASER_CIRCULAR_EASE_IN:
func = &CircularEaseIn;
break;
case EASER_CIRCULAR_EASE_OUT:
func = &CircularEaseOut;
break;
case EASER_CIRCULAR_EASE_IN_OUT:
func = &CircularEaseInOut;
break;
case EASER_EXPONENTIAL_EASE_IN:
func = &ExponentialEaseIn;
break;
case EASER_EXPONENTIAL_EASE_OUT:
func = &ExponentialEaseOut;
break;
case EASER_EXPONENTIAL_EASE_IN_OUT:
func = &ExponentialEaseInOut;
break;
case EASER_BACK_EASE_IN:
func = &BackEaseIn;
break;
case EASER_BACK_EASE_OUT:
func = &BackEaseOut;
break;
case EASER_BACK_EASE_IN_OUT:
func = &BackEaseInOut;
break;
case EASER_ELASTIC_EASE_IN:
func = &ElasticEaseIn;
break;
case EASER_ELASTIC_EASE_OUT:
func = &ElasticEaseOut;
break;
case EASER_ELASTIC_EASE_IN_OUT:
func = &ElasticEaseInOut;
break;
case EASER_BOUNCE_EASE_IN:
func = &BounceEaseIn;
break;
case EASER_BOUNCE_EASE_OUT:
func = &BounceEaseOut;
break;
case EASER_BOUNCE_EASE_IN_OUT:
func = &BounceEaseInOut;
break;
default:
func = 0;
break;
}
return func;
}
void Easer::initialize(DingleDots *dd, Easable *target, Easer_Type type, boost::function<void(double)> functor,
double value_start, double value_finish,
double duration_secs)
{
this->active = FALSE;
this->target = target;
this->value = nullptr;
this->dd = dd;
this->setter = functor;
this->duration_secs = duration_secs;
this->value_start = value_start;
this->value_finish = value_finish;
this->easing_func = easer_type_to_easing_func(type);
}
void Easer::start() {
target->add_easer(this);
this->active = TRUE;
dd->queue_draw();
clock_gettime(CLOCK_MONOTONIC, &this->start_ts);
}
void Easer::finalize() {
this->setter(this->value_finish);
for (std::vector<Easer *>::iterator it = this->finsh_easers.begin();
it != this->finsh_easers.end(); ++it) {
Easer *e = *it;
e->start();
}
for (std::vector<boost::function <void()>>::iterator it = this->finish_actions.begin();
it != this->finish_actions.end(); ++it) {
auto action = *it;
action();
}
this->active = FALSE;
}
void Easer::update_value() {
double ratio_complete = 0.0;
double delta;
double easer_value;
ratio_complete = min(1.0, this->get_ratio_complete());
easer_value = (this->easing_func)(ratio_complete);
delta = this->value_finish - this->value_start;
this->setter(this->value_start + easer_value * delta);
dd->queue_draw();
}
bool Easer::done()
{
double ratio_complete = get_ratio_complete();
if (ratio_complete < 1.0) {
return FALSE;
} else {
return TRUE;
}
}
double Easer::get_ratio_complete() {
struct timespec now_ts;
struct timespec diff_ts;
double ratio_complete;
double time_passed_secs;
clock_gettime(CLOCK_MONOTONIC, &now_ts);
timespec_diff(&this->start_ts, &now_ts, &diff_ts);
time_passed_secs = timespec_to_seconds(&diff_ts);
ratio_complete = (time_passed_secs / this->duration_secs);
return ratio_complete;
}
double Easer::time_left_secs()
{
return (1.0 - this->get_ratio_complete()) * this->duration_secs;
}
void Easer::add_finish_easer(Easer *e)
{
this->finsh_easers.push_back(e);
}
void Easer::add_finish_action(boost::function<void()> action)
{
this->finish_actions.push_back(action);
}