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     /** Initializes with a duration and a percent
 36      * @param {Number} duration duration in seconds
 37      * @param {Number} percent
 38      * @return {Boolean}
 39      */
 40     initWithDuration:function (duration, percent) {
 41         if (this._super(duration)) {
 42             this._to = percent;
 43             return true;
 44         }
 45         return false;
 46     },
 47 
 48     /**
 49      * @deprecated It is not needed anymore.
 50      * @param zone
 51      */
 52     copyWithZone:function (zone) {
 53         var newZone = null;
 54         var copy = null;
 55         if (zone && zone._copyObject) {
 56             //in case of being called at sub class
 57             copy = zone._copyObject;
 58         } else {
 59             copy = new cc.ProgressTo();
 60             zone = newZone = new cc.Zone(copy);
 61         }
 62 
 63         this._super(zone);
 64 
 65         copy.initWithDuration(this._duration, this._to);
 66 
 67         return copy;
 68     },
 69 
 70     /**
 71      * @param {cc.Node} target
 72      */
 73     startWithTarget:function (target) {
 74         this._super(target);
 75         this._from = target.getPercentage();
 76 
 77         // XXX: Is this correct ?
 78         // Adding it to support CCRepeat
 79         if (this._from == 100) {
 80             this._from = 0;
 81         }
 82     },
 83 
 84     /**
 85      * @param {Number} time time in seconds
 86      */
 87     update:function (time) {
 88         if (this._target  instanceof cc.ProgressTimer) {
 89             this._target.setPercentage(this._from + (this._to - this._from) * time);
 90         }
 91     }
 92 });
 93 
 94 /** Creates and initializes with a duration and a percent
 95  * @param {Number} duration duration in seconds
 96  * @param {Number} percent
 97  * @return {cc.ProgressTo}
 98  * @example
 99  * // example
100  * var to = cc.ProgressTo.create(2, 100);
101  *
102  */
103 cc.ProgressTo.create = function (duration, percent) {
104     var progressTo = new cc.ProgressTo();
105     progressTo.initWithDuration(duration, percent);
106 
107     return progressTo;
108 };
109 
110 /**
111  * Progress from a percentage to another percentage
112  * @class
113  * @extends cc.ActionInterval
114  */
115 cc.ProgressFromTo = cc.ActionInterval.extend(/** @lends cc.ProgressFromTo# */{
116     _to:0,
117     _from:0,
118     /** Initializes the action with a duration, a "from" percentage and a "to" percentage
119      * @param {Number} duration duration in seconds
120      * @param {Number} fromPercentage
121      * @param {Number} toPercentage
122      * @return {Boolean}
123      */
124     initWithDuration:function (duration, fromPercentage, toPercentage) {
125         if (this._super(duration)) {
126             this._to = toPercentage;
127             this._from = fromPercentage;
128             return true;
129         }
130         return false;
131     },
132 
133     /**
134      * @deprecated It is not needed anymore.
135      * @param zone
136      */
137     copyWithZone:function (zone) {
138         var newZone = null;
139         var copy = null;
140         if (zone && zone._copyObject) {
141             //in case of being called at sub class
142             copy = zone._copyObject;
143         } else {
144             copy = new cc.ProgressFromTo();
145             zone = newZone = new cc.Zone(copy);
146         }
147 
148         this._super(zone);
149         copy.initWithDuration(this._duration, this._from, this._to);
150         return copy;
151     },
152 
153     /**
154      * @return {cc.ActionInterval}
155      */
156     reverse:function () {
157         return cc.ProgressFromTo.create(this._duration, this._to, this._from);
158     },
159 
160     /**
161      * @param {cc.Node} target
162      */
163     startWithTarget:function (target) {
164         this._super(target);
165     },
166 
167     /**
168      * @param {Number} time time in seconds
169      */
170     update:function (time) {
171         if (this._target  instanceof cc.ProgressTimer) {
172             this._target.setPercentage(this._from + (this._to - this._from) * time);
173         }
174     }
175 });
176 
177 /** Creates and initializes the action with a duration, a "from" percentage and a "to" percentage
178  * @param {Number} duration duration in seconds
179  * @param {Number} fromPercentage
180  * @param {Number} toPercentage
181  * @return {cc.ProgressFromTo}
182  * @example
183  * // example
184  *  var fromTO = cc.ProgressFromTo.create(2, 100.0, 0.0);
185  */
186 cc.ProgressFromTo.create = function (duration, fromPercentage, toPercentage) {
187     var progressFromTo = new cc.ProgressFromTo();
188     progressFromTo.initWithDuration(duration, fromPercentage, toPercentage);
189     return progressFromTo;
190 };
191