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 CC_ANIMATION_TYPE_SINGLE_FRAME = -4;
 33 /**
 34  * the animation isn't loop
 35  * @constant
 36  * @type {number}
 37  */
 38 CC_ANIMATION_TYPE_NO_LOOP = -3;
 39 /**
 40  * the animation to loop from front
 41  * @constant
 42  * @type {number}
 43  */
 44 CC_ANIMATION_TYPE_TO_LOOP_FRONT = -2;
 45 /**
 46  * the animation to loop from back
 47  * @constant
 48  * @type {number}
 49  */
 50 CC_ANIMATION_TYPE_TO_LOOP_BACK = -1;
 51 /**
 52  * the animation loop from front
 53  * @constant
 54  * @type {number}
 55  */
 56 CC_ANIMATION_TYPE_LOOP_FRONT = 0;
 57 /**
 58  * the animation loop from back
 59  * @constant
 60  * @type {number}
 61  */
 62 CC_ANIMATION_TYPE_LOOP_BACK = 1;
 63 /**
 64  * the animation max
 65  * @constant
 66  * @type {number}
 67  */
 68 CC_ANIMATION_TYPE_MAX = 2;
 69 
 70 /**
 71  * Base class for ccs.ProcessBase objects.
 72  * @class
 73  * @extends ccs.Class
 74  */
 75 ccs.ProcessBase = ccs.Class.extend(/** @lends ccs.ProcessBase# */{
 76     _processScale:1,
 77     _isComplete:true,
 78     _isPause:true,
 79     _isPlaying:false,
 80     _currentPercent:0.0,
 81     _rawDuration:0,
 82     _loopType:0,
 83     _tweenEasing:0,
 84     _animationInternal:null,
 85     _currentFrame:0,
 86     _durationTween:0,
 87     _nextFrameIndex:0,
 88     _curFrameIndex:null,
 89     _isLoopBack:false,
 90     ctor:function () {
 91         this._processScale = 1;
 92         this._isComplete = true;
 93         this._isPause = true;
 94         this._isPlaying = false;
 95         this._currentFrame = 0;
 96         this._currentPercent = 0.0;
 97         this._durationTween = 0;
 98         this._rawDuration = 0;
 99         this._loopType = CC_ANIMATION_TYPE_LOOP_BACK;
100         this._tweenEasing = ccs.TweenType.linear;
101         this._animationInternal = cc.Director.getInstance().getAnimationInterval();
102         this._curFrameIndex = 0;
103         this._durationTween = 0;
104         this._isLoopBack = false;
105     },
106 
107     /**
108      * Pause the Process
109      */
110     pause:function () {
111         this._isPause = true;
112         this._isPlaying = false;
113     },
114 
115     /**
116      * Resume the Process
117      */
118     resume:function () {
119         this._isPause = false;
120         this._isPlaying = true;
121     },
122 
123     /**
124      * Stop the Process
125      */
126     stop:function () {
127         this._isComplete = true;
128         this._isPlaying = false;
129         this._currentFrame = 0;
130         this._currentPercent = 0;
131     },
132 
133     /**
134      * play animation by animation name.
135      * @param {Number} durationTo
136      *         he frames between two animation changing-over.It's meaning is changing to this animation need how many frames
137      *         -1 : use the value from CCMovementData get from flash design panel
138      * @param {Number} durationTween he
139      *         frame count you want to play in the game.if  _durationTween is 80, then the animation will played 80 frames in a loop
140      *         -1 : use the value from CCMovementData get from flash design panel
141      * @param {Number} loop
142      *          Whether the animation is loop.
143      *         loop < 0 : use the value from CCMovementData get from flash design panel
144      *         loop = 0 : this animation is not loop
145      *         loop > 0 : this animation is loop
146      * @param {Number} tweenEasing
147      *          CCTween easing is used for calculate easing effect
148      *         TWEEN_EASING_MAX : use the value from CCMovementData get from flash design panel
149      *         -1 : fade out
150      *         0  : line
151      *         1  : fade in
152      *         2  : fade in and out
153      */
154     play:function (durationTo, durationTween, loop, tweenEasing) {
155         this._isComplete = false;
156         this._isPause = false;
157         this._isPlaying = true;
158         this._currentFrame = 0;
159 
160         /*
161          *  Set this._nextFrameIndex to durationTo, it is used for change tween between two animation.
162          *  When changing end, this._nextFrameIndex will be setted to _durationTween
163          */
164         this._nextFrameIndex = durationTo;
165         this._tweenEasing = tweenEasing;
166     },
167 
168     update:function (dt) {
169         if (this._isComplete || this._isPause) {
170             return false;
171         }
172         if (this._rawDuration <= 0) {
173             return false;
174         }
175         var locNextFrameIndex = this._nextFrameIndex;
176         var locCurrentFrame = this._currentFrame;
177         if (locNextFrameIndex <= 0) {
178             this._currentPercent = 1;
179             locCurrentFrame = 0;
180         }else{
181             /*
182              *  update currentFrame, every update add the frame passed.
183              *  dt/this._animationInternal determine it is not a frame animation. If frame speed changed, it will not make our
184              *  animation speed slower or quicker.
185              */
186             locCurrentFrame += this._processScale * (dt / this._animationInternal);
187 
188             this._currentPercent = locCurrentFrame / locNextFrameIndex;
189 
190             /*
191              *	if currentFrame is bigger or equal than this._nextFrameIndex, then reduce it util currentFrame is
192              *  smaller than this._nextFrameIndex
193              */
194             locCurrentFrame = ccs.fmodf(locCurrentFrame, locNextFrameIndex);
195         }
196         this._currentFrame = locCurrentFrame
197         this.updateHandler();
198         return true;
199     },
200 
201     /**
202      * update will call this handler, you can handle your logic here
203      */
204     updateHandler:function () {
205         //override
206     },
207 
208     /**
209      * goto frame
210      * @param {Number} frameIndex
211      */
212     gotoFrame:function (frameIndex) {
213         var locLoopType = this._loopType;
214         if (locLoopType == CC_ANIMATION_TYPE_NO_LOOP) {
215             locLoopType = CC_ANIMATION_TYPE_MAX;
216         }
217         else if (locLoopType == CC_ANIMATION_TYPE_TO_LOOP_FRONT) {
218             locLoopType = CC_ANIMATION_TYPE_LOOP_FRONT;
219         }
220         this._loopType = locLoopType;
221         this._curFrameIndex = frameIndex;
222         this._nextFrameIndex = this._durationTween;
223     },
224 
225     /**
226      * get currentFrameIndex
227      * @return {Number}
228      */
229     getCurrentFrameIndex:function () {
230         this._curFrameIndex = this._rawDuration * this._currentPercent;
231         return this._curFrameIndex;
232     },
233 
234     /**
235      * whether the animation is pause
236      * @returns {boolean}
237      */
238     isPause:function () {
239         return this._isPause;
240     },
241 
242     /**
243      * whether the animation is complete
244      * @returns {boolean}
245      */
246     isComplete:function () {
247         return this._isComplete;
248     },
249 
250     /**
251      * current percent getter
252      * @returns {number}
253      */
254     getCurrentPercent:function () {
255         return this._currentPercent;
256     },
257 
258     /**
259      * rawDuration getter
260      * @returns {number}
261      */
262     getRawDuration:function () {
263         return this._rawDuration;
264     },
265 
266     /**
267      *  loop type getter
268      * @returns {number}
269      */
270     getLoop:function () {
271         return this._loopType;
272     },
273 
274     /**
275      * tween easing getter
276      * @returns {number}
277      */
278     getTweenEasing:function () {
279         return this._tweenEasing;
280     },
281 
282     /**
283      * animationInternal getter
284      * @returns {number}
285      */
286     getAnimationInternal:function () {
287         return this._animationInternal;
288     },
289 
290     /**
291      * animationInternal setter
292      * @param animationInternal
293      */
294     setAnimationInternal:function(animationInternal){
295         this._animationInternal = animationInternal;
296     },
297 
298     /**
299      * process scale getter
300      * @returns {number}
301      */
302     getProcessScale:function () {
303         return this._processScale;
304     },
305 
306     /**
307      * process scale setter
308      * @param processScale
309      */
310     setProcessScale:function (processScale) {
311         this._processScale = processScale;
312     },
313 
314     /**
315      * whether the animation is playing
316      * @returns {boolean}
317      */
318     isPlaying:function () {
319         return this._isPlaying;
320     }
321 });
322