1 /**************************************************************************** 2 Copyright (c) 2011-2012 cocos2d-x.org 3 Copyright (c) 2013-2014 Chukong Technologies Inc. 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 //animation type 28 /** 29 * the animation just have one frame 30 * @constant 31 * @type {number} 32 */ 33 ccs.ANIMATION_TYPE_SINGLE_FRAME = -4; 34 /** 35 * the animation isn't loop 36 * @constant 37 * @type {number} 38 */ 39 ccs.ANIMATION_TYPE_NO_LOOP = -3; 40 /** 41 * the animation to loop from front 42 * @constant 43 * @type {number} 44 */ 45 ccs.ANIMATION_TYPE_TO_LOOP_FRONT = -2; 46 /** 47 * the animation to loop from back 48 * @constant 49 * @type {number} 50 */ 51 ccs.ANIMATION_TYPE_TO_LOOP_BACK = -1; 52 /** 53 * the animation loop from front 54 * @constant 55 * @type {number} 56 */ 57 ccs.ANIMATION_TYPE_LOOP_FRONT = 0; 58 /** 59 * the animation loop from back 60 * @constant 61 * @type {number} 62 */ 63 ccs.ANIMATION_TYPE_LOOP_BACK = 1; 64 /** 65 * the animation max 66 * @constant 67 * @type {number} 68 */ 69 ccs.ANIMATION_TYPE_MAX = 2; 70 71 /** 72 * Base class for ccs.ProcessBase objects. 73 * @class 74 * @extends ccs.Class 75 * 76 * @property {Number} currentFrameIndex - <@readonly> The current frame's index 77 * @property {Boolean} paused - <@readonly> Indicate whether the process is paused 78 * @property {Boolean} completed - <@readonly> Indicate whether the process is done 79 * @property {Number} currentPercent - <@readonly> The current percentage of the process 80 * @property {Number} rawDuration - <@readonly> The duration 81 * @property {Number} loop - <@readonly> The number of loop 82 * @property {Number} tweenEasing - <@readonly> The tween easing 83 * @property {Number} animationInterval - The animation internal 84 * @property {Number} processScale - The process scale 85 * @property {Boolean} playing - <@readonly> Indicate whether the process is playing 86 */ 87 ccs.ProcessBase = ccs.Class.extend(/** @lends ccs.ProcessBase# */{ 88 processScale: 1, 89 _isComplete: true, 90 _isPause: true, 91 _isPlaying: false, 92 _currentPercent: 0.0, 93 _rawDuration: 0, 94 _loopType: 0, 95 _tweenEasing: 0, 96 animationInternal: null, 97 _currentFrame: 0, 98 _durationTween: 0, 99 _nextFrameIndex: 0, 100 _curFrameIndex: null, 101 _isLoopBack: false, 102 ctor: function () { 103 this.processScale = 1; 104 this._isComplete = true; 105 this._isPause = true; 106 this._isPlaying = false; 107 this._currentFrame = 0; 108 this._currentPercent = 0.0; 109 this._durationTween = 0; 110 this._rawDuration = 0; 111 this._loopType = ccs.ANIMATION_TYPE_LOOP_BACK; 112 this._tweenEasing = ccs.TweenType.linear; 113 this.animationInternal = 1 / 60; 114 this._curFrameIndex = 0; 115 this._durationTween = 0; 116 this._isLoopBack = false; 117 }, 118 119 /** 120 * Pause the Process 121 */ 122 pause: function () { 123 this._isPause = true; 124 this._isPlaying = false; 125 }, 126 127 /** 128 * Resume the Process 129 */ 130 resume: function () { 131 this._isPause = false; 132 this._isPlaying = true; 133 }, 134 135 /** 136 * Stop the Process 137 */ 138 stop: function () { 139 this._isComplete = true; 140 this._isPlaying = false; 141 }, 142 143 /** 144 * Play the Process 145 * @param {Number} durationTo 146 * @param {ccs.TweenType} tweenEasing 147 */ 148 play: function (durationTo, tweenEasing) { 149 this._isComplete = false; 150 this._isPause = false; 151 this._isPlaying = true; 152 this._currentFrame = 0; 153 this._nextFrameIndex = durationTo; 154 this._tweenEasing = tweenEasing; 155 }, 156 157 update: function (dt) { 158 if (this._isComplete || this._isPause) { 159 return false; 160 } 161 if (this._rawDuration <= 0) { 162 return false; 163 } 164 var locNextFrameIndex = this._nextFrameIndex; 165 var locCurrentFrame = this._currentFrame; 166 if (locNextFrameIndex <= 0) { 167 this._currentPercent = 1; 168 locCurrentFrame = 0; 169 } else { 170 /* 171 * update currentFrame, every update add the frame passed. 172 * dt/this.animationInternal determine it is not a frame animation. If frame speed changed, it will not make our 173 * animation speed slower or quicker. 174 */ 175 locCurrentFrame += this.processScale * (dt / this.animationInternal); 176 177 this._currentPercent = locCurrentFrame / locNextFrameIndex; 178 179 /* 180 * if currentFrame is bigger or equal than this._nextFrameIndex, then reduce it util currentFrame is 181 * smaller than this._nextFrameIndex 182 */ 183 locCurrentFrame = ccs.fmodf(locCurrentFrame, locNextFrameIndex); 184 } 185 this._currentFrame = locCurrentFrame 186 this.updateHandler(); 187 return true; 188 }, 189 190 /** 191 * update will call this handler, you can handle your logic here 192 */ 193 updateHandler: function () { 194 //override 195 }, 196 197 /** 198 * goto frame 199 * @param {Number} frameIndex 200 */ 201 gotoFrame: function (frameIndex) { 202 var locLoopType = this._loopType; 203 if (locLoopType == ccs.ANIMATION_TYPE_NO_LOOP) { 204 locLoopType = ccs.ANIMATION_TYPE_MAX; 205 } 206 else if (locLoopType == ccs.ANIMATION_TYPE_TO_LOOP_FRONT) { 207 locLoopType = ccs.ANIMATION_TYPE_LOOP_FRONT; 208 } 209 this._loopType = locLoopType; 210 this._curFrameIndex = frameIndex; 211 this._nextFrameIndex = this._durationTween; 212 }, 213 214 /** 215 * get currentFrameIndex 216 * @return {Number} 217 */ 218 getCurrentFrameIndex: function () { 219 this._curFrameIndex = (this._rawDuration - 1) * this._currentPercent; 220 return this._curFrameIndex; 221 }, 222 223 /** 224 * whether the animation is pause 225 * @returns {boolean} 226 */ 227 isPause: function () { 228 return this._isPause; 229 }, 230 231 /** 232 * whether the animation is complete 233 * @returns {boolean} 234 */ 235 isComplete: function () { 236 return this._isComplete; 237 }, 238 239 /** 240 * current percent getter 241 * @returns {number} 242 */ 243 getCurrentPercent: function () { 244 return this._currentPercent; 245 }, 246 247 /** 248 * rawDuration getter 249 * @returns {number} 250 */ 251 getRawDuration: function () { 252 return this._rawDuration; 253 }, 254 255 /** 256 * loop type getter 257 * @returns {number} 258 */ 259 getLoop: function () { 260 return this._loopType; 261 }, 262 263 /** 264 * tween easing getter 265 * @returns {number} 266 */ 267 getTweenEasing: function () { 268 return this._tweenEasing; 269 }, 270 271 /** 272 * animationInternal getter 273 * @returns {number} 274 */ 275 getAnimationInternal: function () { 276 return this.animationInternal; 277 }, 278 279 /** 280 * animationInternal setter 281 * @param animationInternal 282 */ 283 setAnimationInternal: function (animationInternal) { 284 this.animationInternal = animationInternal; 285 }, 286 287 /** 288 * process scale getter 289 * @returns {number} 290 */ 291 getProcessScale: function () { 292 return this.processScale; 293 }, 294 295 /** 296 * process scale setter 297 * @param processScale 298 */ 299 setProcessScale: function (processScale) { 300 this.processScale = processScale; 301 }, 302 303 /** 304 * whether the animation is playing 305 * @returns {boolean} 306 */ 307 isPlaying: function () { 308 return this._isPlaying; 309 } 310 }); 311 312 var _p = ccs.ProcessBase.prototype; 313 314 // Extended properties 315 /** @expose */ 316 _p.currentFrameIndex; 317 cc.defineGetterSetter(_p, "currentFrameIndex", _p.getCurrentFrameIndex); 318 /** @expose */ 319 _p.paused; 320 cc.defineGetterSetter(_p, "paused", _p.isPause); 321 /** @expose */ 322 _p.completed; 323 cc.defineGetterSetter(_p, "completed", _p.isComplete); 324 /** @expose */ 325 _p.currentPercent; 326 cc.defineGetterSetter(_p, "currentPercent", _p.getCurrentPercent); 327 /** @expose */ 328 _p.rawDuration; 329 cc.defineGetterSetter(_p, "rawDuration", _p.getRawDuration); 330 /** @expose */ 331 _p.loop; 332 cc.defineGetterSetter(_p, "loop", _p.getLoop); 333 /** @expose */ 334 _p.tweenEasing; 335 cc.defineGetterSetter(_p, "tweenEasing", _p.getTweenEasing); 336 /** @expose */ 337 _p.playing; 338 cc.defineGetterSetter(_p, "playing", _p.isPlaying); 339 340 _p = null; 341