-
Notifications
You must be signed in to change notification settings - Fork 4
/
Tween.cpp
183 lines (151 loc) · 4.34 KB
/
Tween.cpp
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
/*--------------------------------------------------------------------
This file is part of the TweenDuino tweening/animation library.
Created by Weston Wedding and heavily influenced by API presented
by the Greensock animation library, GSAP.
This work is licensed under a Creative Commons
Attribution_ShareAlike 3.0 Unported License.
http://creativecommons.org/licenses/by_sa/3.0/
--------------------------------------------------------------------*/
#if defined(ARDUINO) && ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Tween.h>
TweenDuino::Tween::Tween(float& t, unsigned long duration, float finalVal)
: target(t), duration(duration), finalVal(finalVal) {
initialized = false;
active = false;
onFirstUpdate = true;
time = 0;
ratio = 0;
startVal = t;
totalChange = finalVal - startVal;
completed = false;
ease = nullptr;
startTime = 0;
lastUpdateTime = 0;
}
TweenDuino::Tween::~Tween() {
if (ease) delete ease;
}
TweenDuino::Tween *TweenDuino::Tween::to(float& target, unsigned long duration, float to) {
Tween *tween = new Tween(target, duration, to);
tween->setEase(LINEAR, INOUT);
return tween;
}
TweenDuino::Tween *TweenDuino::Tween::to(float& target, unsigned long duration, float to, Ease ease, EaseType type) {
Tween *tween = new Tween(target, duration, to);
tween->setEase(ease, type);
return tween;
}
bool TweenDuino::Tween::isActive() {
return initialized && lastUpdateTime >= startTime && !completed;
}
bool TweenDuino::Tween::isComplete() {
return completed;
}
unsigned long TweenDuino::Tween::getDuration() {
return duration;
}
unsigned long TweenDuino::Tween::getStartTime() {
return startTime;
}
void TweenDuino::Tween::setEase(Ease e, EaseType type) {
if (ease != nullptr){
delete ease;
ease = nullptr;
}
switch(e) {
case LINEAR:
ease = new ht::ease::LinearEase();
break;
case SINE:
ease = new ht::ease::SineEase();
break;
case QUAD:
ease = new ht::ease::QuadraticEase();
break;
case QUART:
ease = new ht::ease::QuarticEase();
break;
case QUINT:
ease = new ht::ease::QuinticEase();
break;
case CUBIC:
ease = new ht::ease::CubicEase();
break;
}
easeType = type;
}
void TweenDuino::Tween::begin(unsigned long timeMs) {
if (ease == nullptr) {
setEase(Ease::LINEAR, EaseType::INOUT);
}
startTime = timeMs;
time = timeMs;
// These are somewhat arbitrary values to just get the ease values working.
// The actual tween result will be derived from actual time passage and what value we're tweening to/from.
ease->duration(1);
ease->scale(1);
initialized = true;
}
/**
* Update the tween. If the tween has not been initialized with begin(), the first update
* also becomes the startTime of the tween.
*/
void TweenDuino::Tween::update(unsigned long updTime) {
if (!initialized) {
begin(updTime);
}
// We set startVal here instead of begin() because we want to be able to cooperate with
// other code that might have adjusted the "target" value before we started to touch it.
if (onFirstUpdate) {
startVal = target;
totalChange = finalVal - startVal;
}
unsigned long prevTime = time;
// Set some times before potentially updating state further (if there's any time left);
if (updTime >= duration + startTime) {
totalTime = duration;
time = duration;
ratio = 1.0;
completed = true;
} else if (updTime < startTime) {
totalTime = 0;
time = 0;
ratio = 0.0;
} else {
totalTime = updTime;
time = updTime;
ratio = getRatio((float)(time - startTime) / (float)duration);
}
onFirstUpdate = false;
lastUpdateTime = updTime;
// Save ourselves some cycles if haven't moved ahead in time,
// or if we're done rendering.
if (time == prevTime) {
return;
}
target = totalChange * ratio + startVal;
}
void TweenDuino::Tween::restartFrom(unsigned long newStart) {
completed = false;
onFirstUpdate = true;
time = 0;
lastUpdateTime = 0;
startTime = newStart;
}
double TweenDuino::Tween::getRatio(float t) {
if (ease == nullptr) {
return 0.0d;
}
if (easeType == IN) {
return ease->easeIn(t);
} else if (easeType == OUT) {
return ease->easeOut(t);
} else if (easeType == INOUT) {
return ease->easeInOut(t);
}
return 0.0d;
}