-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathAnimation.hx
More file actions
366 lines (312 loc) · 13.4 KB
/
Copy pathAnimation.hx
File metadata and controls
366 lines (312 loc) · 13.4 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package haxe.ui.styles.animation;
import haxe.ui.constants.AnimationDirection;
import haxe.ui.constants.AnimationFillMode;
import haxe.ui.styles.EasingFunction;
import haxe.ui.styles.elements.AnimationKeyFrames;
import haxe.ui.util.StyleUtil;
@:structInit
class AnimationOptions {
static private inline var DEFAULT_DURATION:Float = 0;
static private inline var DEFAULT_DELAY:Float = 0;
static private inline var DEFAULT_ITERATION_COUNT:Int = 1;
static private var DEFAULT_EASING_FUNCTION:EasingFunction = EasingFunction.EASE;
static private inline var DEFAULT_DIRECTION:AnimationDirection = AnimationDirection.NORMAL;
static private inline var DEFAULT_FILL_MODE:AnimationFillMode = AnimationFillMode.FORWARDS;
@:optional public var duration:Null<Float>;
@:optional public var delay:Null<Float>;
@:optional public var iterationCount:Null<Int>;
@:optional public var easingFunction:EasingFunction;
@:optional public var direction:AnimationDirection;
@:optional public var fillMode:AnimationFillMode;
public function compareTo(op:AnimationOptions):Bool {
@SuppressWarnings("checkstyle:Indentation")
return op != null &&
op.duration == duration &&
op.delay == delay &&
op.iterationCount == iterationCount &&
op.easingFunction == easingFunction &&
op.direction == direction &&
op.fillMode == fillMode;
}
public function compareToAnimation(anim:Animation):Bool {
@SuppressWarnings("checkstyle:Indentation")
return ((duration == null && anim.duration == DEFAULT_DURATION) || (duration != null && anim.duration == duration)) &&
((delay == null && anim.delay == DEFAULT_DELAY) || (delay != null && anim.delay == delay)) &&
((iterationCount == null && anim.iterationCount == DEFAULT_ITERATION_COUNT) || (iterationCount != null && anim.iterationCount == iterationCount)) &&
((easingFunction == null && anim.easingFunction == DEFAULT_EASING_FUNCTION) || (easingFunction != null && anim.easingFunction == easingFunction)) &&
((direction == null && anim.direction == DEFAULT_DIRECTION) || (direction != null && anim.direction == direction)) &&
((fillMode == null && anim.fillMode == DEFAULT_FILL_MODE) || (fillMode != null && anim.fillMode == fillMode));
}
}
@:access(haxe.ui.styles.animation.AnimationOptions)
class Animation {
//***********************************************************************************************************
// Helpers
//***********************************************************************************************************
public static function createWithKeyFrames(animationKeyFrames:AnimationKeyFrames, target:Dynamic, options:AnimationOptions = null):Animation {
var animation:Animation = new Animation(target, options);
animation.name = animationKeyFrames.id;
if (animation._keyframes == null) {
animation._keyframes = [];
}
for (keyFrame in animationKeyFrames.keyFrames) {
var kf = new KeyFrame();
switch (keyFrame.time) {
case Value.VDimension(v):
switch (v) {
case Dimension.PERCENT(p):
kf.time = p / 100;
kf.easingFunction = animation.easingFunction;
kf.directives = keyFrame.directives;
animation._keyframes.push(kf);
case _:
}
case _:
}
}
return animation;
}
//***********************************************************************************************************
// Public API
//***********************************************************************************************************
/**
Returns the current key frame running in the animation.
**/
public var currentKeyFrame(get, never):KeyFrame;
/**
Specifies a delay for the start of an animation in seconds. If using negative values, the animation will start as if it
had already been playing for N seconds.
**/
public var delay(default, null):Float = AnimationOptions.DEFAULT_DELAY;
/**
Specifies whether an animation should be played forwards, backwards or in alternate cycles.
@see `haxe.ui.constants.AnimationDirection`
**/
public var direction(default, null):AnimationDirection = AnimationOptions.DEFAULT_DIRECTION;
/**
Defines how long time an animation should take to complete.
**/
public var duration(default, null):Float = AnimationOptions.DEFAULT_DURATION;
/**
Specifies the speed curve of the animation.
@see `haxe.ui.styles.EasingFunction`
**/
public var easingFunction(default, null):EasingFunction = AnimationOptions.DEFAULT_EASING_FUNCTION;
/**
Specifies a style for the target when the animation is not playing (befores it starts, after it ends, or both).
@see `haxe.ui.constants.AnimationFillMode`
**/
public var fillMode(default, null):AnimationFillMode = AnimationOptions.DEFAULT_FILL_MODE;
/**
Specifies the number of times an animation should run before it stops. For an infinite loop set to -1.
**/
public var iterationCount(default, null):Int = 1;
/**
Specifies the total keyframes count in the animation.
**/
public var keyframeCount(get, never):Int;
/**
The name of the animation.
**/
public var name:String;
/**
Returns if the animation is running.
**/
public var running(default, null):Bool;
/**
Specifies the target to apply the animation.
**/
public var target(default, null):Dynamic;
public function new(target:Dynamic, options:AnimationOptions = null) {
this.target = target;
if (options != null) {
if (options.duration != null) this.duration = options.duration;
if (options.easingFunction != null) this.easingFunction = options.easingFunction;
if (options.delay != null) this.delay = options.delay;
if (options.iterationCount != null) this.iterationCount = options.iterationCount;
if (options.direction != null) this.direction = options.direction;
if (options.fillMode != null) this.fillMode = options.fillMode;
}
}
/**
Starts to run the animation.
**/
public function run(onFinish:Void->Void = null) {
if (keyframeCount == 0 || running) {
return;
}
if (!_initialized) {
_initialize();
}
_currentKeyFrameIndex = -1;
_currentIterationCount = 0;
running = true;
_saveState();
_runNextKeyframe(onFinish);
}
/**
Stops the animation if it is running.
**/
public function stop() {
if (running == false) {
return;
}
running = false;
var currentKF:KeyFrame = currentKeyFrame;
if (currentKF != null) {
currentKF.stop();
_currentKeyFrameIndex = -1;
}
_keyframes = null;
_restoreState();
}
//***********************************************************************************************************
// Private API
//***********************************************************************************************************
private var _currentKeyFrameIndex:Int = -1;
private var _currentIterationCount:Int = -1;
private var _initialState:Map<String, Dynamic>;
private var _initialized:Bool = false;
private var _keyframes:Array<KeyFrame>;
private function get_keyframeCount():Int {
return _keyframes == null ? 0 : _keyframes.length;
}
private function get_currentKeyFrame():KeyFrame {
return _currentKeyFrameIndex >= 0 ? _keyframes[_currentKeyFrameIndex] : null;
}
private function _initialize() {
switch (direction) {
case AnimationDirection.NORMAL:
//Nothing
case AnimationDirection.REVERSE:
_reverseCurrentKeyframes();
case AnimationDirection.ALTERNATE:
_addAlternateKeyframes();
case AnimationDirection.ALTERNATE_REVERSE:
_reverseCurrentKeyframes();
_addAlternateKeyframes();
}
var currentTime:Float = 0;
for (keyframe in _keyframes) {
switch (direction) {
case AnimationDirection.NORMAL, AnimationDirection.ALTERNATE:
//Nothing
case AnimationDirection.REVERSE, AnimationDirection.ALTERNATE_REVERSE:
keyframe.time = 1 - keyframe.time;
}
keyframe.time = duration * keyframe.time - currentTime;
currentTime += keyframe.time;
}
if (delay > 0) {
var keyframe:KeyFrame = new KeyFrame();
keyframe.time = delay;
keyframe.easingFunction = easingFunction;
_keyframes.unshift(keyframe);
} else if (delay < 0) {
//Remove all frames until delay is reached. For the last keyframe, we apply a negative delay to play the animation in the exact time.
currentTime = 0;
var lastKeyframe:KeyFrame = null;
while (_keyframes.length > 0) {
var keyframe:KeyFrame = _keyframes[0];
currentTime -= keyframe.time;
if (currentTime >= delay) {
lastKeyframe = keyframe;
_keyframes.splice(0, 1);
} else {
keyframe.delay = -(currentTime - delay + keyframe.time);
if (lastKeyframe != null) {
lastKeyframe.time = 0;
_keyframes.unshift(lastKeyframe);
}
break;
}
}
}
_initialized = true;
}
private function _runNextKeyframe(onFinish:Void->Void = null) {
if (running == false) {
return;
}
if (++_currentKeyFrameIndex >= _keyframes.length) {
_currentKeyFrameIndex = -1;
_restoreState();
if (iterationCount == -1 || ++_currentIterationCount < iterationCount) {
_saveState();
_runNextKeyframe(onFinish);
} else {
running = false;
if (onFinish != null) {
onFinish();
}
}
return;
} else {
currentKeyFrame.run(target, _runNextKeyframe.bind(onFinish));
}
}
private function _addAlternateKeyframes() {
var i:Int = _keyframes.length;
while (--i >= 0) {
var keyframe:KeyFrame = _keyframes[i];
var newKeyframe:KeyFrame = new KeyFrame();
newKeyframe.time = 1 - keyframe.time;
newKeyframe.easingFunction = _getReverseEasingFunction(keyframe.easingFunction);
newKeyframe.directives = keyframe.directives;
_keyframes.push(newKeyframe);
}
}
private function _reverseCurrentKeyframes() {
_keyframes.reverse();
var func = _getReverseEasingFunction(easingFunction);
for (keyframe in _keyframes) {
keyframe.easingFunction = func;
}
}
private function _getReverseEasingFunction(easingFunction:EasingFunction):EasingFunction {
return switch (easingFunction) {
case EasingFunction.QUAD_OUT: EasingFunction.QUAD_IN;
case EasingFunction.QUAD_IN: EasingFunction.QUAD_OUT;
case EasingFunction.CUBIC_OUT: EasingFunction.CUBIC_IN;
case EasingFunction.CUBIC_IN: EasingFunction.CUBIC_OUT;
case EasingFunction.EASE_OUT: EasingFunction.EASE_IN;
case EasingFunction.EASE_IN: EasingFunction.EASE_OUT;
case EasingFunction.QUART_OUT: EasingFunction.QUART_IN;
case EasingFunction.QUART_IN: EasingFunction.QUART_OUT;
case _: easingFunction;
}
}
private function _saveState() {
if (!_shouldRestoreState()) {
return;
}
if (_initialState == null) {
_initialState = new Map<String, Dynamic>();
}
for (keyframe in _keyframes) {
for (directive in keyframe.directives) {
var property:String = StyleUtil.styleProperty2ComponentProperty(directive.directive);
if (!_initialState.exists(property)) {
_initialState.set(property, Reflect.getProperty(target, property));
}
}
}
}
private function _restoreState() {
if (!_shouldRestoreState()) {
return;
}
if (_initialState != null) {
for (property in _initialState.keys()) {
Reflect.setProperty(target, property, _initialState.get(property));
}
_initialState = null;
}
}
private function _shouldRestoreState():Bool {
@SuppressWarnings("checkstyle:Indentation")
return fillMode == AnimationFillMode.NONE ||
(fillMode == AnimationFillMode.FORWARDS && direction != AnimationDirection.NORMAL && direction != AnimationDirection.ALTERNATE) ||
(fillMode == AnimationFillMode.BACKWARDS && direction != AnimationDirection.REVERSE && direction != AnimationDirection.ALTERNATE_REVERSE);
}
}