forked from scratchfoundation/scratch-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransition.as
More file actions
106 lines (94 loc) · 3.58 KB
/
Transition.as
File metadata and controls
106 lines (94 loc) · 3.58 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
/*
* Scratch Project Editor and Player
* Copyright (C) 2014 Massachusetts Institute of Technology
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package util {
import flash.utils.getTimer;
public class Transition {
private static var activeTransitions:Array = [];
private var interpolate:Function;
private var setValue:Function;
private var startValue:*;
private var endValue:*;
private var delta:*;
private var whenDone:Function;
private var startMSecs:uint;
private var duration:uint;
public function Transition(interpolate:Function, setValue:Function, startValue:*, endValue:*, secs:Number, whenDone:Function) {
// Create a transition animation between two values (either scalars or Arrays).
this.interpolate = interpolate;
this.setValue = setValue;
this.startValue = startValue;
this.endValue = endValue;
this.whenDone = whenDone;
if (startValue is Array) {
delta = [];
for (var i:int = 0; i < startValue.length; i++) {
this.delta.push(endValue[i] - startValue[i]);
}
} else {
delta = endValue - startValue;
}
startMSecs = getTimer();
duration = 1000 * secs;
}
public static function linear(setValue:Function, startValue:*, endValue:*, secs:Number, whenDone:Function = null):void {
activeTransitions.push(new Transition(linearFunc, setValue, startValue, endValue, secs, whenDone));
}
public static function quadratic(setValue:Function, startValue:*, endValue:*, secs:Number, whenDone:Function = null):void {
activeTransitions.push(new Transition(quadraticFunc, setValue, startValue, endValue, secs, whenDone));
}
public static function cubic(setValue:Function, startValue:*, endValue:*, secs:Number, whenDone:Function = null):void {
activeTransitions.push(new Transition(cubicFunc, setValue, startValue, endValue, secs, whenDone));
}
public static function step(evt:*):void {
if (activeTransitions.length == 0) return;
var now:uint = getTimer();
var newActive:Array = [];
for each (var t:Transition in activeTransitions) {
if (t.apply(now)) newActive.push(t);
}
activeTransitions = newActive;
}
private function apply(now:uint):Boolean {
var msecs:int = now - startMSecs;
if (msecs < 50) { // ensure that start value is processed for at least one frame
setValue(startValue);
return true;
}
var t:Number = (now - startMSecs) / duration;
if (t > 1.0) {
setValue(endValue);
if (whenDone != null) whenDone();
return false;
}
if (startValue is Array) {
var a:Array = [];
for (var i:int = 0; i < startValue.length; i++) {
a.push(startValue[i] + (delta[i] * (1.0 - interpolate(1.0 - t))));
}
setValue(a);
} else {
setValue(startValue + (delta * (1.0 - interpolate(1.0 - t))));
}
return true;
}
// Transition functions:
private static function linearFunc(t:Number):Number { return t }
private static function quadraticFunc(t:Number):Number { return t * t }
private static function cubicFunc(t:Number):Number { return t * t * t }
}}