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