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