1 /****************************************************************************
  2  Copyright (c) 2008-2010 Ricardo Quesada
  3  Copyright (c) 2011-2012 cocos2d-x.org
  4  Copyright (c) 2013-2014 Chukong Technologies 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 
 33     /**
 34      * @function
 35      * @param value
 36      * @param key
 37      */
 38     updateTweenAction:function(value, key){
 39 
 40     }
 41 });
 42 
 43 /**
 44  * cc.ActionTween
 45  * cc.ActionTween is an action that lets you update any property of an object.
 46  *
 47  * @class
 48  * @extends cc.ActionInterval
 49  * @example
 50  * //For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:
 51  *  var modifyWidth = cc.actionTween(2,"width",200,300)
 52  *  target.runAction(modifyWidth);
 53  *
 54  * //Another example: cc.ScaleTo action could be rewriten using cc.PropertyAction:
 55  * // scaleA and scaleB are equivalents
 56  * var scaleA = cc.scaleTo(2,3);
 57  * var scaleB = cc.actionTween(2,"scale",1,3);
 58  */
 59 cc.ActionTween = cc.ActionInterval.extend(/** @lends cc.ActionTween */{
 60     key:"",
 61     from:0,
 62     to:0,
 63     delta:0,
 64 
 65 	/**
 66 	 * Creates an initializes the action with the property name (key), and the from and to parameters.
 67 	 * Constructor of cc.ActionTween
 68 	 * @param {Number} duration
 69 	 * @param {String} key
 70 	 * @param {Number} from
 71 	 * @param {Number} to
 72 	 */
 73     ctor:function(duration, key, from, to){
 74         cc.ActionInterval.prototype.ctor.call(this);
 75         this.key = "";
 76 
 77 		to !== undefined && this.initWithDuration(duration, key, from, to);
 78     },
 79 
 80     /**
 81      * initializes the action with the property name (key), and the from and to parameters.
 82      * @param {Number} duration
 83      * @param {String} key
 84      * @param {Number} from
 85      * @param {Number} to
 86      * @return {Boolean}
 87      */
 88     initWithDuration:function (duration, key, from, to) {
 89         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
 90             this.key = key;
 91             this.to = to;
 92             this.from = from;
 93             return true;
 94         }
 95         return false;
 96     },
 97     /**
 98      * @param {cc.Node} target
 99      */
100     startWithTarget:function (target) {
101         if(!target || !target.updateTweenAction)
102             throw "cc.ActionTween.startWithTarget(): target must be non-null, and target must implement updateTweenAction function";
103         cc.ActionInterval.prototype.startWithTarget.call(this, target);
104         this.delta = this.to - this.from;
105     },
106     /**
107      * @param {Number} dt
108      */
109     update:function (dt) {
110         this.target.updateTweenAction(this.to - this.delta * (1 - dt), this.key);
111     },
112     /**
113      * @return {cc.ActionTween}
114      */
115     reverse:function () {
116         return cc.actionTween(this.duration, this.key, this.to, this.from);
117     },
118 
119     clone:function(){
120         var action = new cc.ActionTween();
121         action.initWithDuration(this._duration, this.key, this.from, this.to);
122         return action;
123     }
124 });
125 
126 /**
127  * Creates an initializes the action with the property name (key), and the from and to parameters.
128  * @function
129  * @param {Number} duration
130  * @param {String} key
131  * @param {Number} from
132  * @param {Number} to
133  * @return {cc.ActionTween}
134  */
135 cc.actionTween = function (duration, key, from, to) {
136     return new cc.ActionTween(duration, key, from, to);
137 };
138 /**
139  * Please use cc.actionTween instead
140  * Creates an initializes the action with the property name (key), and the from and to parameters.
141  * @static
142  * @deprecated
143  * @param {Number} duration
144  * @param {String} key
145  * @param {Number} from
146  * @param {Number} to
147  * @return {cc.ActionTween}
148  */
149 cc.ActionTween.create = cc.actionTween;