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         // XXX: Is this correct ?
 94         // Adding it to support CCRepeat
 95         if (this._from == 100)
 96             this._from = 0;
 97     },
 98 
 99     /**
100      * custom update
101      * @param {Number} time time in seconds
102      */
103     update:function (time) {
104         if (this.target  instanceof cc.ProgressTimer)
105             this.target.percentage = this._from + (this._to - this._from) * time;
106     }
107 });
108 
109 /**
110  * Creates and initializes with a duration and a percent
111  * @function
112  * @param {Number} duration duration in seconds
113  * @param {Number} percent
114  * @return {cc.ProgressTo}
115  * @example
116  * // example
117  * var to = cc.progressTo(2, 100);
118  */
119 cc.progressTo = function (duration, percent) {
120     return new cc.ProgressTo(duration, percent);
121 };
122 /**
123  * Please use cc.progressTo instead
124  * Creates and initializes with a duration and a percent
125  * @static
126  * @deprecated since v3.0,please use cc.progressTo instead.
127  * @param {Number} duration duration in seconds
128  * @param {Number} percent
129  * @return {cc.ProgressTo}
130  * @example
131  * //example
132  * var progress = cc.ProgressTo.create(duration,percent);
133  */
134 cc.ProgressTo.create = cc.progressTo;
135 
136 /**
137  * Progress from a percentage to another percentage
138  * @class
139  * @extends cc.ActionInterval
140  * @param {Number} duration duration in seconds
141  * @param {Number} fromPercentage
142  * @param {Number} toPercentage
143  * @example
144  *  var fromTo = new cc.ProgressFromTo(2, 100.0, 0.0);
145  */
146 cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{
147     _to:0,
148     _from:0,
149 
150 	/**
151 	 * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
152 	 * Constructor of cc.ProgressFromTo
153      * @param {Number} duration duration in seconds
154      * @param {Number} fromPercentage
155      * @param {Number} toPercentage
156 	 */
157     ctor:function(duration, fromPercentage, toPercentage){
158         cc.ActionInterval.prototype.ctor.call(this);
159         this._to = 0;
160         this._from = 0;
161 
162 		toPercentage !== undefined && this.initWithDuration(duration, fromPercentage, toPercentage);
163     },
164 
165     /** Initializes the action with a duration, a "from" percentage and a "to" percentage
166      * @param {Number} duration duration in seconds
167      * @param {Number} fromPercentage
168      * @param {Number} toPercentage
169      * @return {Boolean}
170      */
171     initWithDuration:function (duration, fromPercentage, toPercentage) {
172         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
173             this._to = toPercentage;
174             this._from = fromPercentage;
175             return true;
176         }
177         return false;
178     },
179     /**
180      * return a new cc.ProgressTo, all the configuration is the same as the original
181      * @returns {cc.ProgressFromTo}
182      */
183     clone:function(){
184         var action = new cc.ProgressFromTo();
185         action.initWithDuration(this._duration, this._from, this._to);
186         return action;
187     },
188 
189     /**
190      * @return {cc.ActionInterval}
191      */
192     reverse:function () {
193         return cc.progressFromTo(this._duration, this._to, this._from);
194     },
195 
196     /**
197      * start with a target
198      * @param {cc.Node} target
199      */
200     startWithTarget:function (target) {
201         cc.ActionInterval.prototype.startWithTarget.call(this, target);
202     },
203 
204     /**
205      * @param {Number} time time in seconds
206      */
207     update:function (time) {
208         if (this.target  instanceof cc.ProgressTimer)
209             this.target.percentage = this._from + (this._to - this._from) * time;
210     }
211 });
212 
213 /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
214  * @function
215  * @param {Number} duration duration in seconds
216  * @param {Number} fromPercentage
217  * @param {Number} toPercentage
218  * @return {cc.ProgressFromTo}
219  * @example
220  * // example
221  *  var fromTo = cc.progressFromTo(2, 100.0, 0.0);
222  */
223 cc.progressFromTo = function (duration, fromPercentage, toPercentage) {
224     return new cc.ProgressFromTo(duration, fromPercentage, toPercentage);
225 };
226 /**
227  * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
228  * @static
229  * @deprecated since v3.0,please use cc.ProgressFromTo(duration, fromPercentage, toPercentage) instead.
230  * @param {Number} duration duration in seconds
231  * @param {Number} fromPercentage
232  * @param {Number} toPercentage
233  * @return {cc.ProgressFromTo}
234  * @example
235  * //example
236  * var progress = cc.ProgressFromTo.create(duration, fromPercentage, toPercentage);
237  */
238 cc.ProgressFromTo.create = cc.progressFromTo;
239