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  *
 29  * @class
 30  * @extends cc.Class
 31  */
 32 cc.ActionTweenDelegate = cc.Class.extend(/** @lends cc.ActionTweenDelegate */{
 33 
 34     /**
 35      * Update Tween Action.
 36      * @param value
 37      * @param key
 38      */
 39     updateTweenAction:function(value, key){}
 40 });
 41 
 42 /**
 43  * cc.ActionTween
 44  * cc.ActionTween is an action that lets you update any property of an object.
 45  *
 46  * @class
 47  * @extends cc.ActionInterval
 48  * @example
 49  * //For example, if you want to modify the "width" property of a target from 200 to 300 in 2 seconds, then:
 50  *  var modifyWidth = cc.actionTween(2,"width",200,300)
 51  *  target.runAction(modifyWidth);
 52  *
 53  * //Another example: cc.ScaleTo action could be rewriten using cc.PropertyAction:
 54  * // scaleA and scaleB are equivalents
 55  * var scaleA = cc.scaleTo(2,3);
 56  * var scaleB = cc.actionTween(2,"scale",1,3);
 57  * @param {Number} duration
 58  * @param {String} key
 59  * @param {Number} from
 60  * @param {Number} to
 61  */
 62 cc.ActionTween = cc.ActionInterval.extend(/** @lends cc.ActionTween */{
 63     key:"",
 64     from:0,
 65     to:0,
 66     delta:0,
 67 
 68 	/**
 69      * Constructor function, override it to extend the construction behavior, remember to call "this._super()" in the extended "ctor" function. <br />
 70 	 * Creates an initializes the action with the property name (key), and the from and to parameters.
 71 	 * @param {Number} duration
 72 	 * @param {String} key
 73 	 * @param {Number} from
 74 	 * @param {Number} to
 75 	 */
 76     ctor:function(duration, key, from, to){
 77         cc.ActionInterval.prototype.ctor.call(this);
 78         this.key = "";
 79 
 80 		to !== undefined && this.initWithDuration(duration, key, from, to);
 81     },
 82 
 83     /**
 84      * initializes the action with the property name (key), and the from and to parameters.
 85      * @param {Number} duration
 86      * @param {String} key
 87      * @param {Number} from
 88      * @param {Number} to
 89      * @return {Boolean}
 90      */
 91     initWithDuration:function (duration, key, from, to) {
 92         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
 93             this.key = key;
 94             this.to = to;
 95             this.from = from;
 96             return true;
 97         }
 98         return false;
 99     },
100 
101     /**
102      * Start this tween with target.
103      * @param {cc.ActionTweenDelegate} target
104      */
105     startWithTarget:function (target) {
106         if(!target || !target.updateTweenAction)
107             throw new Error("cc.ActionTween.startWithTarget(): target must be non-null, and target must implement updateTweenAction function");
108         cc.ActionInterval.prototype.startWithTarget.call(this, target);
109         this.delta = this.to - this.from;
110     },
111 
112     /**
113      * Called once per frame. Time is the number of seconds of a frame interval.
114      *
115      * @param {Number}  dt
116      */
117     update:function (dt) {
118         this.target.updateTweenAction(this.to - this.delta * (1 - dt), this.key);
119     },
120 
121     /**
122      * returns a reversed action.
123      * @return {cc.ActionTween}
124      */
125     reverse:function () {
126         return new cc.ActionTween(this.duration, this.key, this.to, this.from);
127     },
128 
129     /**
130      * to copy object with deep copy.
131      * returns a clone of action.
132      *
133      * @return {cc.ActionTween}
134      */
135     clone:function(){
136         var action = new cc.ActionTween();
137         action.initWithDuration(this._duration, this.key, this.from, this.to);
138         return action;
139     }
140 });
141 
142 /**
143  * Creates an initializes the action with the property name (key), and the from and to parameters.
144  * @function
145  * @param {Number} duration
146  * @param {String} key
147  * @param {Number} from
148  * @param {Number} to
149  * @return {cc.ActionTween}
150  */
151 cc.actionTween = function (duration, key, from, to) {
152     return new cc.ActionTween(duration, key, from, to);
153 };
154 
155 /**
156  * Please use cc.actionTween instead.
157  * Creates an initializes the action with the property name (key), and the from and to parameters.
158  * @static
159  * @deprecated since v3.0 <br /> Please use cc.actionTween instead.
160  * @param {Number} duration
161  * @param {String} key
162  * @param {Number} from
163  * @param {Number} to
164  * @return {cc.ActionTween}
165  */
166 cc.ActionTween.create = cc.actionTween;