1 /***************************************
  2  * Copyright 2011, 2012 GlobWeb contributors.
  3  *
  4  * This file is part of GlobWeb.
  5  *
  6  * GlobWeb is free software: you can redistribute it and/or modify
  7  * it under the terms of the GNU Lesser General Public License as published by
  8  * the Free Software Foundation, version 3 of the License, or
  9  * (at your option) any later version.
 10  *
 11  * GlobWeb is distributed in the hope that it will be useful,
 12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14  * GNU Lesser General Public License for more details.
 15  *
 16  * You should have received a copy of the GNU General Public License
 17  * along with GlobWeb. If not, see <http://www.gnu.org/licenses/>.
 18  ***************************************/
 19 
 20 define(['./Utils','./Animation'], function(Utils,Animation) {
 21 
 22 /**************************************************************************************************************/
 23 
 24 /** @constructor
 25   Generic animation to interpolate arbitrary values
 26   The animation will interpolate between startValue and endValue, using the
 27   interpolateFunction(t, startValue, endValue) (t [0,1])
 28   The interpolated value is then given to the setFunction(value)
 29   */
 30 var InterpolatedAnimation = function(duration, startValue, endValue, interpolationFunction, setFunction)
 31 {
 32     // Call ancestor constructor
 33     Animation.prototype.constructor.call(this);
 34 
 35     this.values = [[0.0, startValue], [1.0, endValue]];
 36     this.duration = duration;
 37     this.interpolationFunction = interpolationFunction;
 38     this.setFunction = setFunction;
 39 }
 40 
 41 /**************************************************************************************************************/
 42 
 43 Utils.inherits(Animation, InterpolatedAnimation);
 44 
 45 /**************************************************************************************************************/
 46 
 47 /*
 48 	Adds a new value to the animation
 49 	't' is the value [0, 1] at which the animation value must reach 'value'
 50 */
 51 InterpolatedAnimation.prototype.addValue = function(t, value)
 52 {
 53     var count = this.values.length;
 54     var upper = 0;
 55     while (upper < count && this.values[upper][0] < t) upper++;
 56     // Insert new value at position 'upper'
 57     this.values.splice(upper, 0, [t, value]);
 58 }
 59 
 60 /**************************************************************************************************************/
 61 
 62 InterpolatedAnimation.prototype.start = function()
 63 {
 64     Animation.prototype.start.call(this);
 65     this.setFunction(this.startValue);
 66 }
 67 
 68 /**************************************************************************************************************/
 69 
 70 InterpolatedAnimation.prototype.stop = function()
 71 {
 72     Animation.prototype.stop.call(this);
 73     this.setFunction(this.endValue);
 74 }
 75 
 76 /**************************************************************************************************************/
 77 
 78 /*
 79 	Animation update method
 80 */
 81 InterpolatedAnimation.prototype.update = function(now)
 82 {
 83     var t = Numeric.map01(now, this.startTime, this.startTime + this.duration);
 84     if (t >= 1)
 85     {
 86         this.stop();
 87         return;
 88     }
 89 
 90     // Find upper and lower bounds
 91     var count = this.values.length;
 92     var upper = 0;
 93     while (upper < count && this.values[upper][0] < t) upper++;
 94     upper = Math.min(upper, count-1);
 95     var lower = Math.max(0, upper-1);
 96 
 97     // Remap t between lower and upper bounds
 98     t = Numeric.map01(t, this.values[lower][0], this.values[upper][0]);
 99 	// Interpolate value
100     var value = this.interpolationFunction(t, this.values[lower][1], this.values[upper][1]);
101 	// Use interpolated value
102     this.setFunction(value);
103 }
104 
105 /**************************************************************************************************************/
106 
107 return InterpolatedAnimation;
108 
109 });
110