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  Copyright (C) 2010      Lam Pham
  6 
  7  http://www.cocos2d-x.org
  8 
  9  Permission is hereby granted, free of charge, to any person obtaining a copy
 10  of this software and associated documentation files (the "Software"), to deal
 11  in the Software without restriction, including without limitation the rights
 12  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 13  copies of the Software, and to permit persons to whom the Software is
 14  furnished to do so, subject to the following conditions:
 15 
 16  The above copyright notice and this permission notice shall be included in
 17  all copies or substantial portions of the Software.
 18 
 19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 24  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 25  THE SOFTWARE.
 26  ****************************************************************************/
 27 
 28 /**
 29  * Progress to percentage
 30  * @class
 31  * @extends cc.ActionInterval
 32  * @param {Number} duration duration in seconds
 33  * @param {Number} percent
 34  * @example
 35  * var to = new cc.ProgressTo(2, 100);
 36  */
 37 cc.ProgressTo = cc.ActionInterval.extend(/** @lends cc.ProgressTo# */{
 38     _to:0,
 39     _from:0,
 40 
 41 	/**
 42 	 * Creates a ProgressTo action with a duration and a percent
 43 	 * Constructor of cc.ProgressTo
 44      * @param {Number} duration duration in seconds
 45      * @param {Number} percent
 46 	 */
 47     ctor: function(duration, percent){
 48         cc.ActionInterval.prototype.ctor.call(this);
 49         this._to = 0;
 50         this._from = 0;
 51 
 52 		percent !== undefined && this.initWithDuration(duration, percent);
 53     },
 54 
 55     /** Initializes with a duration and a percent
 56      * @param {Number} duration duration in seconds
 57      * @param {Number} percent
 58      * @return {Boolean}
 59      */
 60     initWithDuration:function (duration, percent) {
 61         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
 62             this._to = percent;
 63             return true;
 64         }
 65         return false;
 66     },
 67     /**
 68      * return a new cc.ProgressTo, all the configuration is the same as the original
 69      * @returns {cc.ProgressTo}
 70      */
 71     clone:function(){
 72         var action = new cc.ProgressTo();
 73         action.initWithDuration(this._duration, this._to);
 74         return action;
 75     },
 76     /**
 77      * reverse hasn't been supported
 78      * @returns {null}
 79      */
 80     reverse: function(){
 81         cc.log("cc.ProgressTo.reverse(): reverse hasn't been supported.");
 82         return null;
 83     },
 84 
 85     /**
 86      * start with a target
 87      * @param {cc.Node} target
 88      */
 89     startWithTarget:function (target) {
 90         cc.ActionInterval.prototype.startWithTarget.call(this, target);
 91         this._from = target.percentage;
 92     },
 93 
 94     /**
 95      * custom update
 96      * @param {Number} time time in seconds
 97      */
 98     update:function (time) {
 99         if (this.target  instanceof cc.ProgressTimer)
100             this.target.percentage = this._from + (this._to - this._from) * time;
101     }
102 });
103 
104 /**
105  * Creates and initializes with a duration and a percent
106  * @function
107  * @param {Number} duration duration in seconds
108  * @param {Number} percent
109  * @return {cc.ProgressTo}
110  * @example
111  * // example
112  * var to = cc.progressTo(2, 100);
113  */
114 cc.progressTo = function (duration, percent) {
115     return new cc.ProgressTo(duration, percent);
116 };
117 /**
118  * Please use cc.progressTo instead
119  * Creates and initializes with a duration and a percent
120  * @static
121  * @deprecated since v3.0,please use cc.progressTo instead.
122  * @param {Number} duration duration in seconds
123  * @param {Number} percent
124  * @return {cc.ProgressTo}
125  */
126 cc.ProgressTo.create = cc.progressTo;
127 
128 /**
129  * Progress from a percentage to another percentage
130  * @class
131  * @extends cc.ActionInterval
132  * @param {Number} duration duration in seconds
133  * @param {Number} fromPercentage
134  * @param {Number} toPercentage
135  * @example
136  *  var fromTo = new cc.ProgressFromTo(2, 100.0, 0.0);
137  */
138 cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{
139     _to:0,
140     _from:0,
141 
142 	/**
143 	 * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
144 	 * Constructor of cc.ProgressFromTo
145      * @param {Number} duration duration in seconds
146      * @param {Number} fromPercentage
147      * @param {Number} toPercentage
148 	 */
149     ctor:function(duration, fromPercentage, toPercentage){
150         cc.ActionInterval.prototype.ctor.call(this);
151         this._to = 0;
152         this._from = 0;
153 
154 		toPercentage !== undefined && this.initWithDuration(duration, fromPercentage, toPercentage);
155     },
156 
157     /** Initializes the action with a duration, a "from" percentage and a "to" percentage
158      * @param {Number} duration duration in seconds
159      * @param {Number} fromPercentage
160      * @param {Number} toPercentage
161      * @return {Boolean}
162      */
163     initWithDuration:function (duration, fromPercentage, toPercentage) {
164         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
165             this._to = toPercentage;
166             this._from = fromPercentage;
167             return true;
168         }
169         return false;
170     },
171     /**
172      * return a new cc.ProgressTo, all the configuration is the same as the original
173      * @returns {cc.ProgressFromTo}
174      */
175     clone:function(){
176         var action = new cc.ProgressFromTo();
177         action.initWithDuration(this._duration, this._from, this._to);
178         return action;
179     },
180 
181     /**
182      * @return {cc.ActionInterval}
183      */
184     reverse:function () {
185         return cc.progressFromTo(this._duration, this._to, this._from);
186     },
187 
188     /**
189      * start with a target
190      * @param {cc.Node} target
191      */
192     startWithTarget:function (target) {
193         cc.ActionInterval.prototype.startWithTarget.call(this, target);
194     },
195 
196     /**
197      * @param {Number} time time in seconds
198      */
199     update:function (time) {
200         if (this.target  instanceof cc.ProgressTimer)
201             this.target.percentage = this._from + (this._to - this._from) * time;
202     }
203 });
204 
205 /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
206  * @function
207  * @param {Number} duration duration in seconds
208  * @param {Number} fromPercentage
209  * @param {Number} toPercentage
210  * @return {cc.ProgressFromTo}
211  * @example
212  * // example
213  *  var fromTo = cc.progressFromTo(2, 100.0, 0.0);
214  */
215 cc.progressFromTo = function (duration, fromPercentage, toPercentage) {
216     return new cc.ProgressFromTo(duration, fromPercentage, toPercentage);
217 };
218 /**
219  * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
220  * @static
221  * @deprecated since v3.0,please use cc.ProgressFromTo(duration, fromPercentage, toPercentage) instead.
222  * @param {Number} duration duration in seconds
223  * @param {Number} fromPercentage
224  * @param {Number} toPercentage
225  * @return {cc.ProgressFromTo}
226  */
227 cc.ProgressFromTo.create = cc.progressFromTo;
228