]> git.mjollnir.org Git - moodle.git/blob
3e1208ad3dc232036d137bbc8cb6155b15ee9762
[moodle.git] /
1 package flare.animate.interpolate
2 {
3         import flare.util.Arrays;
4         
5         /**
6          * Interpolator for numeric <code>Array</code> values. Each value
7          * contained in the array should be a numeric (<code>Number</code> or
8          * <code>int</code>) value.
9          */
10         public class ArrayInterpolator extends Interpolator
11         {
12                 private var _start:Array;
13                 private var _end:Array;
14                 private var _cur:Array;
15                 
16                 /**
17                  * Creates a new ArrayInterpolator.
18                  * @param target the object whose property is being interpolated
19                  * @param property the property to interpolate
20                  * @param start the starting array of values to interpolate from
21                  * @param end the target array to interpolate to. This should be an
22                  *  array of numerical values.
23                  */
24                 public function ArrayInterpolator(target:Object, property:String,
25                                                   start:Object, end:Object)
26                 {
27                         super(target, property, start, end);
28                 }
29                 
30                 /**
31                  * Initializes this interpolator.
32                  * @param start the starting value of the interpolation
33                  * @param end the target value of the interpolation
34                  */
35                 protected override function init(start:Object, end:Object) : void
36                 {
37                         _start = start as Array;
38                         _end = end as Array;
39                         
40                         if (!_end) throw new Error("Target array is null!");
41                         if (!_start) _start = Arrays.copy(_end);
42                         if (_start.length != _end.length)
43                                 throw new Error("Array dimensions don't match");
44                         
45                         if (_cur == null || _cur == _start || _cur == _end) {
46                                 _cur = Arrays.copy(_start);
47                         } else {
48                                 _cur = Arrays.copy(_start, _cur);
49                         }
50                 }
51                 
52                 /**
53                  * Calculate and set an interpolated property value.
54                  * @param f the interpolation fraction (typically between 0 and 1)
55                  */
56                 public override function interpolate(f:Number) : void
57                 {
58                         for (var i:uint=0; i<_cur.length; ++i) {
59                                 _cur[i] = _start[i] + f*(_end[i] - _start[i]);
60                         }
61                         _prop.setValue(_target, _cur);
62                 }
63                 
64         } // end of class ArrayInterpolator
65 }