1 /****************************************************************************
  2  Copyright (c) 2010-2012 cocos2d-x.org
  3 
  4  http://www.cocos2d-x.org
  5 
  6  Permission is hereby granted, free of charge, to any person obtaining a copy
  7  of this software and associated documentation files (the "Software"), to deal
  8  in the Software without restriction, including without limitation the rights
  9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 10  copies of the Software, and to permit persons to whom the Software is
 11  furnished to do so, subject to the following conditions:
 12 
 13  The above copyright notice and this permission notice shall be included in
 14  all copies or substantial portions of the Software.
 15 
 16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 22  THE SOFTWARE.
 23  ****************************************************************************/
 24 
 25 
 26 //animation type
 27 /**
 28  * the animation just have one frame
 29  * @constant
 30  * @type {number}
 31  */
 32 ccs.ANIMATION_TYPE_SINGLE_FRAME = -4;
 33 /**
 34  * the animation isn't loop
 35  * @constant
 36  * @type {number}
 37  */
 38 ccs.ANIMATION_TYPE_NO_LOOP = -3;
 39 /**
 40  * the animation to loop from front
 41  * @constant
 42  * @type {number}
 43  */
 44 ccs.ANIMATION_TYPE_TO_LOOP_FRONT = -2;
 45 /**
 46  * the animation to loop from back
 47  * @constant
 48  * @type {number}
 49  */
 50 ccs.ANIMATION_TYPE_TO_LOOP_BACK = -1;
 51 /**
 52  * the animation loop from front
 53  * @constant
 54  * @type {number}
 55  */
 56 ccs.ANIMATION_TYPE_LOOP_FRONT = 0;
 57 /**
 58  * the animation loop from back
 59  * @constant
 60  * @type {number}
 61  */
 62 ccs.ANIMATION_TYPE_LOOP_BACK = 1;
 63 /**
 64  * the animation max
 65  * @constant
 66  * @type {number}
 67  */
 68 ccs.ANIMATION_TYPE_MAX = 2;
 69 
 70 /**
 71  * Base class for ccs.ProcessBase objects.
 72  * @class
 73  * @extends ccs.Class
 74  *
 75  * @property {Number}   currentFrameIndex   - <@readonly> The current frame's index
 76  * @property {Boolean}  paused              - <@readonly> Indicate whether the process is paused
 77  * @property {Boolean}  completed           - <@readonly> Indicate whether the process is done
 78  * @property {Number}   currentPercent      - <@readonly> The current percentage of the process
 79  * @property {Number}   rawDuration         - <@readonly> The duration
 80  * @property {Number}   loop                - <@readonly> The number of loop
 81  * @property {Number}   tweenEasing         - <@readonly> The tween easing
 82  * @property {Number}   animationInterval   - The animation internal
 83  * @property {Number}   processScale        - The process scale
 84  * @property {Boolean}  playing             - <@readonly> Indicate whether the process is playing
 85  */
 86 ccs.ProcessBase = ccs.Class.extend(/** @lends ccs.ProcessBase# */{
 87     processScale:1,
 88     _isComplete:true,
 89     _isPause:true,
 90     _isPlaying:false,
 91     _currentPercent:0.0,
 92     _rawDuration:0,
 93     _loopType:0,
 94     _tweenEasing:0,
 95     animationInternal:null,
 96     _currentFrame:0,
 97     _durationTween:0,
 98     _nextFrameIndex:0,
 99     _curFrameIndex:null,
100     _isLoopBack:false,
101     ctor:function () {
102         this.processScale = 1;
103         this._isComplete = true;
104         this._isPause = true;
105         this._isPlaying = false;
106         this._currentFrame = 0;
107         this._currentPercent = 0.0;
108         this._durationTween = 0;
109         this._rawDuration = 0;
110         this._loopType = ccs.ANIMATION_TYPE_LOOP_BACK;
111         this._tweenEasing = ccs.TweenType.linear;
112         this.animationInternal = 1/60;
113         this._curFrameIndex = 0;
114         this._durationTween = 0;
115         this._isLoopBack = false;
116     },
117 
118     /**
119      * Pause the Process
120      */
121     pause:function () {
122         this._isPause = true;
123         this._isPlaying = false;
124     },
125 
126     /**
127      * Resume the Process
128      */
129     resume:function () {
130         this._isPause = false;
131         this._isPlaying = true;
132     },
133 
134     /**
135      * Stop the Process
136      */
137     stop:function () {
138         this._isComplete = true;
139         this._isPlaying = false;
140     },
141 
142     /**
143      * Play the Process
144      * @param {Number} durationTo
145      * @param {ccs.TweenType} tweenEasing
146      */
147     play:function (durationTo, tweenEasing) {
148         this._isComplete = false;
149         this._isPause = false;
150         this._isPlaying = true;
151         this._currentFrame = 0;
152         this._nextFrameIndex = durationTo;
153         this._tweenEasing = tweenEasing;
154     },
155 
156     update:function (dt) {
157         if (this._isComplete || this._isPause) {
158             return false;
159         }
160         if (this._rawDuration <= 0) {
161             return false;
162         }
163         var locNextFrameIndex = this._nextFrameIndex;
164         var locCurrentFrame = this._currentFrame;
165         if (locNextFrameIndex <= 0) {
166             this._currentPercent = 1;
167             locCurrentFrame = 0;
168         }else{
169             /*
170              *  update currentFrame, every update add the frame passed.
171              *  dt/this.animationInternal determine it is not a frame animation. If frame speed changed, it will not make our
172              *  animation speed slower or quicker.
173              */
174             locCurrentFrame += this.processScale * (dt / this.animationInternal);
175 
176             this._currentPercent = locCurrentFrame / locNextFrameIndex;
177 
178             /*
179              *	if currentFrame is bigger or equal than this._nextFrameIndex, then reduce it util currentFrame is
180              *  smaller than this._nextFrameIndex
181              */
182             locCurrentFrame = ccs.fmodf(locCurrentFrame, locNextFrameIndex);
183         }
184         this._currentFrame = locCurrentFrame
185         this.updateHandler();
186         return true;
187     },
188 
189     /**
190      * update will call this handler, you can handle your logic here
191      */
192     updateHandler:function () {
193         //override
194     },
195 
196     /**
197      * goto frame
198      * @param {Number} frameIndex
199      */
200     gotoFrame:function (frameIndex) {
201         var locLoopType = this._loopType;
202         if (locLoopType == ccs.ANIMATION_TYPE_NO_LOOP) {
203             locLoopType = ccs.ANIMATION_TYPE_MAX;
204         }
205         else if (locLoopType == ccs.ANIMATION_TYPE_TO_LOOP_FRONT) {
206             locLoopType = ccs.ANIMATION_TYPE_LOOP_FRONT;
207         }
208         this._loopType = locLoopType;
209         this._curFrameIndex = frameIndex;
210         this._nextFrameIndex = this._durationTween;
211     },
212 
213     /**
214      * get currentFrameIndex
215      * @return {Number}
216      */
217     getCurrentFrameIndex:function () {
218         this._curFrameIndex = (this._rawDuration-1) * this._currentPercent;
219         return this._curFrameIndex;
220     },
221 
222     /**
223      * whether the animation is pause
224      * @returns {boolean}
225      */
226     isPause:function () {
227         return this._isPause;
228     },
229 
230     /**
231      * whether the animation is complete
232      * @returns {boolean}
233      */
234     isComplete:function () {
235         return this._isComplete;
236     },
237 
238     /**
239      * current percent getter
240      * @returns {number}
241      */
242     getCurrentPercent:function () {
243         return this._currentPercent;
244     },
245 
246     /**
247      * rawDuration getter
248      * @returns {number}
249      */
250     getRawDuration:function () {
251         return this._rawDuration;
252     },
253 
254     /**
255      *  loop type getter
256      * @returns {number}
257      */
258     getLoop:function () {
259         return this._loopType;
260     },
261 
262     /**
263      * tween easing getter
264      * @returns {number}
265      */
266     getTweenEasing:function () {
267         return this._tweenEasing;
268     },
269 
270     /**
271      * animationInternal getter
272      * @returns {number}
273      */
274     getAnimationInternal:function () {
275         return this.animationInternal;
276     },
277 
278     /**
279      * animationInternal setter
280      * @param animationInternal
281      */
282     setAnimationInternal:function(animationInternal){
283         this.animationInternal = animationInternal;
284     },
285 
286     /**
287      * process scale getter
288      * @returns {number}
289      */
290     getProcessScale:function () {
291         return this.processScale;
292     },
293 
294     /**
295      * process scale setter
296      * @param processScale
297      */
298     setProcessScale:function (processScale) {
299         this.processScale = processScale;
300     },
301 
302     /**
303      * whether the animation is playing
304      * @returns {boolean}
305      */
306     isPlaying:function () {
307         return this._isPlaying;
308     }
309 });
310 
311 window._p = ccs.ProcessBase.prototype;
312 
313 // Extended properties
314 /** @expose */
315 _p.currentFrameIndex;
316 cc.defineGetterSetter(_p, "currentFrameIndex", _p.getCurrentFrameIndex);
317 /** @expose */
318 _p.paused;
319 cc.defineGetterSetter(_p, "paused", _p.isPause);
320 /** @expose */
321 _p.completed;
322 cc.defineGetterSetter(_p, "completed", _p.isComplete);
323 /** @expose */
324 _p.currentPercent;
325 cc.defineGetterSetter(_p, "currentPercent", _p.getCurrentPercent);
326 /** @expose */
327 _p.rawDuration;
328 cc.defineGetterSetter(_p, "rawDuration", _p.getRawDuration);
329 /** @expose */
330 _p.loop;
331 cc.defineGetterSetter(_p, "loop", _p.getLoop);
332 /** @expose */
333 _p.tweenEasing;
334 cc.defineGetterSetter(_p, "tweenEasing", _p.getTweenEasing);
335 /** @expose */
336 _p.playing;
337 cc.defineGetterSetter(_p, "playing", _p.isPlaying);
338 
339 delete window._p;
340