1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3  Copyright (c) 2008-2010 Ricardo Quesada
  4  Copyright (c) 2011      Zynga Inc.
  5 
  6  http://www.cocos2d-x.org
  7 
  8  Permission is hereby granted, free of charge, to any person obtaining a copy
  9  of this software and associated documentation files (the "Software"), to deal
 10  in the Software without restriction, including without limitation the rights
 11  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 12  copies of the Software, and to permit persons to whom the Software is
 13  furnished to do so, subject to the following conditions:
 14 
 15  The above copyright notice and this permission notice shall be included in
 16  all copies or substantial portions of the Software.
 17 
 18  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 19  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 20  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 21  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 22  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 23  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 24  THE SOFTWARE.
 25  ****************************************************************************/
 26 
 27 /**
 28  * @class
 29  * @extends cc.Class
 30  */
 31 cc.ActionTweenDelegate = cc.Class.extend(/** @lends cc.ActionTweenDelegate */{
 32      updateTweenAction:function(value, key){
 33 
 34      }
 35 });
 36 
 37 /**
 38  * cc.ActionTween
 39  * cc.ActionTween is an action that lets you update any property of an object.
 40  *
 41  * @class
 42  * @extends cc.ActionInterval
 43  * @example
 44  * //For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:
 45  *  var modifyWidth = cc.ActionTween.create(2,"width",200,300)
 46  *  target.runAction(modifyWidth);
 47  *
 48  * //Another example: cc.ScaleTo action could be rewriten using cc.PropertyAction:
 49  * // scaleA and scaleB are equivalents
 50  * var scaleA = cc.ScaleTo.create(2,3);
 51  * var scaleB = cc.ActionTween.create(2,"scale",1,3);
 52  */
 53 cc.ActionTween = cc.ActionInterval.extend(/** @lends cc.ActionTween */{
 54     key:"",
 55     from:0,
 56     to:0,
 57     delta:0,
 58 
 59 	/**
 60 	 * Creates an initializes the action with the property name (key), and the from and to parameters.
 61 	 * @constructor
 62 	 * @param {Number} duration
 63 	 * @param {String} key
 64 	 * @param {Number} from
 65 	 * @param {Number} to
 66 	 */
 67     ctor:function(duration, key, from, to){
 68         cc.ActionInterval.prototype.ctor.call(this);
 69         this.key = "";
 70 
 71 		to !== undefined && this.initWithDuration(duration, key, from, to);
 72     },
 73 
 74     /**
 75      * initializes the action with the property name (key), and the from and to parameters.
 76      * @param {Number} duration
 77      * @param {String} key
 78      * @param {Number} from
 79      * @param {Number} to
 80      * @return {Boolean}
 81      */
 82     initWithDuration:function (duration, key, from, to) {
 83         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
 84             this.key = key;
 85             this.to = to;
 86             this.from = from;
 87             return true;
 88         }
 89         return false;
 90     },
 91     /**
 92      * @param {cc.Node} target
 93      */
 94     startWithTarget:function (target) {
 95         if(!target || !target.updateTweenAction)
 96             throw "cc.ActionTween.startWithTarget(): target must be non-null, and target must implement updateTweenAction function";
 97         cc.ActionInterval.prototype.startWithTarget.call(this, target);
 98         this.delta = this.to - this.from;
 99     },
100     /**
101      * @param {Number} dt
102      */
103     update:function (dt) {
104         this.target.updateTweenAction(this.to - this.delta * (1 - dt), this.key);
105     },
106     /**
107      * @return {cc.ActionTween}
108      */
109     reverse:function () {
110         return cc.ActionTween.create(this.duration, this.key, this.to, this.from);
111     },
112 
113     clone:function(){
114         var action = new cc.ActionTween();
115         action.initWithDuration(this._duration, this.key, this.from, this.to);
116         return action;
117     }
118 });
119 
120 /**
121  * Creates an initializes the action with the property name (key), and the from and to parameters.
122  * @param {Number} duration
123  * @param {String} key
124  * @param {Number} from
125  * @param {Number} to
126  * @return {cc.ActionTween}
127  */
128 cc.ActionTween.create = function (duration, key, from, to) {
129     var ret = new cc.ActionTween();
130     if (ret.initWithDuration(duration, key, from, to))
131         return ret;
132     return null;
133 };
134