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  */
 33 cc.ProgressTo = cc.ActionInterval.extend(/** @lends cc.ProgressTo# */{
 34     _to:0,
 35     _from:0,
 36 
 37 	/**
 38 	 * Creates a ProgressTo action with a duration and a percent
 39 	 * Constructor of cc.ProgressTo
 40 	 * @param {Number} duration duration in seconds
 41 	 * @param {Number} percent
 42 	 * @example
 43 	 * var to = new cc.ProgressTo(2, 100);
 44 	 */
 45     ctor: function(duration, percent){
 46         cc.ActionInterval.prototype.ctor.call(this);
 47         this._to = 0;
 48         this._from = 0;
 49 
 50 		percent !== undefined && this.initWithDuration(duration, percent);
 51     },
 52 
 53     /** Initializes with a duration and a percent
 54      * @param {Number} duration duration in seconds
 55      * @param {Number} percent
 56      * @return {Boolean}
 57      */
 58     initWithDuration:function (duration, percent) {
 59         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
 60             this._to = percent;
 61             return true;
 62         }
 63         return false;
 64     },
 65 
 66     clone:function(){
 67         var action = new cc.ProgressTo();
 68         action.initWithDuration(this._duration, this._to);
 69         return action;
 70     },
 71 
 72     reverse: function(){
 73         cc.log("cc.ProgressTo.reverse(): reverse hasn't been supported.");
 74         return null;
 75     },
 76 
 77     /**
 78      * @param {cc.Node} target
 79      */
 80     startWithTarget:function (target) {
 81         cc.ActionInterval.prototype.startWithTarget.call(this, target);
 82         this._from = target.percentage;
 83 
 84         // XXX: Is this correct ?
 85         // Adding it to support CCRepeat
 86         if (this._from == 100)
 87             this._from = 0;
 88     },
 89 
 90     /**
 91      * @param {Number} time time in seconds
 92      */
 93     update:function (time) {
 94         if (this.target  instanceof cc.ProgressTimer)
 95             this.target.percentage = this._from + (this._to - this._from) * time;
 96     }
 97 });
 98 
 99 /**
100  * Creates and initializes with a duration and a percent
101  * @function
102  * @param {Number} duration duration in seconds
103  * @param {Number} percent
104  * @return {cc.ProgressTo}
105  * @example
106  * // example
107  * var to = cc.progressTo(2, 100);
108  */
109 cc.progressTo = function (duration, percent) {
110     return new cc.ProgressTo(duration, percent);
111 };
112 /**
113  * Please use cc.progressTo instead
114  * Creates and initializes with a duration and a percent
115  * @static
116  * @deprecated
117  * @param {Number} duration duration in seconds
118  * @param {Number} percent
119  * @return {cc.ProgressTo}
120  */
121 cc.ProgressTo.create = cc.progressTo;
122 
123 /**
124  * Progress from a percentage to another percentage
125  * @class
126  * @extends cc.ActionInterval
127  */
128 cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{
129     _to:0,
130     _from:0,
131 
132 	/**
133 	 * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
134 	 * Constructor of cc.ProgressFromTo
135 	 * @param {Number} duration duration in seconds
136 	 * @param {Number} fromPercentage
137 	 * @param {Number} toPercentage
138 	 * @example
139 	 *  var fromTo = new cc.ProgressFromTo(2, 100.0, 0.0);
140 	 */
141     ctor:function(duration, fromPercentage, toPercentage){
142         cc.ActionInterval.prototype.ctor.call(this);
143         this._to = 0;
144         this._from = 0;
145 
146 		toPercentage !== undefined && this.initWithDuration(duration, fromPercentage, toPercentage);
147     },
148 
149     /** Initializes the action with a duration, a "from" percentage and a "to" percentage
150      * @param {Number} duration duration in seconds
151      * @param {Number} fromPercentage
152      * @param {Number} toPercentage
153      * @return {Boolean}
154      */
155     initWithDuration:function (duration, fromPercentage, toPercentage) {
156         if (cc.ActionInterval.prototype.initWithDuration.call(this, duration)) {
157             this._to = toPercentage;
158             this._from = fromPercentage;
159             return true;
160         }
161         return false;
162     },
163 
164     clone:function(){
165         var action = new cc.ProgressFromTo();
166         action.initWithDuration(this._duration, this._from, this._to);
167         return action;
168     },
169 
170     /**
171      * @return {cc.ActionInterval}
172      */
173     reverse:function () {
174         return cc.ProgressFromTo.create(this._duration, this._to, this._from);
175     },
176 
177     /**
178      * @param {cc.Node} target
179      */
180     startWithTarget:function (target) {
181         cc.ActionInterval.prototype.startWithTarget.call(this, target);
182     },
183 
184     /**
185      * @param {Number} time time in seconds
186      */
187     update:function (time) {
188         if (this.target  instanceof cc.ProgressTimer)
189             this.target.percentage = this._from + (this._to - this._from) * time;
190     }
191 });
192 
193 /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
194  * @function
195  * @param {Number} duration duration in seconds
196  * @param {Number} fromPercentage
197  * @param {Number} toPercentage
198  * @return {cc.ProgressFromTo}
199  * @example
200  * // example
201  *  var fromTO = cc.progressFromTo(2, 100.0, 0.0);
202  */
203 cc.progressFromTo = function (duration, fromPercentage, toPercentage) {
204     return new cc.ProgressFromTo(duration, fromPercentage, toPercentage);
205 };
206 /**
207  * Please use cc.progressFromTo instead
208  * Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
209  * @static
210  * @deprecated
211  * @param {Number} duration duration in seconds
212  * @param {Number} fromPercentage
213  * @param {Number} toPercentage
214  * @return {cc.ProgressFromTo}
215  */
216 cc.ProgressFromTo.create = cc.progressFromTo;
217