1 /**************************************************************************** 2 Copyright (c) 2010-2012 cocos2d-x.org 3 Copyright (c) 2008-2010 Ricardo Quesada 4 Copyright (c) 2011 Zynga Inc. 5 6 http://www.cocos2d-x.org 7 8 Permission is hereby granted, free of charge, to any person obtaining a copy 9 of this software and associated documentation files (the "Software"), to deal 10 in the Software without restriction, including without limitation the rights 11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 copies of the Software, and to permit persons to whom the Software is 13 furnished to do so, subject to the following conditions: 14 15 The above copyright notice and this permission notice shall be included in 16 all copies or substantial portions of the Software. 17 18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 THE SOFTWARE. 25 ****************************************************************************/ 26 27 /** 28 * <p> 29 * Singleton that manages the Animations.<br/> 30 * It saves in a cache the animations. You should use this class if you want to save your animations in a cache.<br/> 31 * </p> 32 * @class 33 * @extends cc.Class 34 * 35 * @example 36 * cc.AnimationCache.getInstance().addAnimation(animation,"animation1"); 37 */ 38 cc.AnimationCache = cc.Class.extend(/** @lends cc.AnimationCache# */{ 39 /** 40 * Adds a cc.Animation with a name. 41 * @param {cc.Animation} animation 42 * @param {String} name 43 */ 44 addAnimation:function (animation, name) { 45 this._animations[name] = animation; 46 }, 47 48 /** 49 * Deletes a cc.Animation from the cache. 50 * @param {String} name 51 */ 52 removeAnimation:function (name) { 53 if (!name) { 54 return; 55 } 56 if (this._animations.hasOwnProperty(name)) { 57 delete this._animations[name]; 58 } 59 }, 60 61 /** 62 * <p> 63 * Returns a cc.Animation that was previously added.<br/> 64 * If the name is not found it will return nil.<br/> 65 * You should retain the returned copy if you are going to use it.</br> 66 * </p> 67 * @param {String} name 68 * @return {cc.Animation} 69 */ 70 getAnimation:function (name) { 71 if (this._animations.hasOwnProperty(name)) 72 return this._animations[name]; 73 return null; 74 }, 75 76 /** 77 * <p> 78 * Adds an animation from an NSDictionary<br/> 79 * Make sure that the frames were previously loaded in the cc.SpriteFrameCache. 80 * </p> 81 * @param {object} dictionary 82 * @param {String} plist 83 */ 84 _addAnimationsWithDictionary:function (dictionary,plist) { 85 var animations = dictionary["animations"]; 86 if (!animations) { 87 cc.log("cocos2d: cc.AnimationCache: No animations were found in provided dictionary."); 88 return; 89 } 90 91 var version = 1; 92 var properties = dictionary["properties"]; 93 if (properties) { 94 version = (properties["format"] != null) ? parseInt(properties["format"]) : version; 95 var spritesheets = properties["spritesheets"]; 96 var spriteFrameCache = cc.SpriteFrameCache.getInstance(); 97 var fileUtils = cc.FileUtils.getInstance(), path; 98 for (var i = 0; i < spritesheets.length; i++) { 99 path = fileUtils.fullPathFromRelativeFile(spritesheets[i], plist); 100 spriteFrameCache.addSpriteFrames(path); 101 } 102 } 103 104 switch (version) { 105 case 1: 106 this._parseVersion1(animations); 107 break; 108 case 2: 109 this._parseVersion2(animations); 110 break; 111 default : 112 cc.log("cc.AnimationCache. Invalid animation format"); 113 break; 114 } 115 }, 116 117 /** 118 * <p> 119 * Adds an animation from a plist file.<br/> 120 * Make sure that the frames were previously loaded in the cc.SpriteFrameCache. 121 * </p> 122 * @param {String} plist 123 */ 124 addAnimations:function (plist) { 125 if(!plist) 126 throw "cc.AnimationCache.addAnimations(): Invalid texture file name"; 127 var fileUtils = cc.FileUtils.getInstance(); 128 var path = fileUtils.fullPathForFilename(plist); 129 var dict = fileUtils.dictionaryWithContentsOfFileThreadSafe(path); 130 131 if(!dict){ 132 cc.log("cc.AnimationCache.addAnimations(): File could not be found"); 133 return; 134 } 135 136 this._addAnimationsWithDictionary(dict,plist); 137 }, 138 139 _parseVersion1:function (animations) { 140 var frameCache = cc.SpriteFrameCache.getInstance(); 141 142 for (var key in animations) { 143 var animationDict = animations[key]; 144 var frameNames = animationDict["frames"]; 145 var delay = parseFloat(animationDict["delay"]) || 0; 146 var animation = null; 147 if (!frameNames) { 148 cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' found in dictionary without any frames - cannot add to animation cache."); 149 continue; 150 } 151 152 var frames = []; 153 for (var i = 0; i < frameNames.length; i++) { 154 var spriteFrame = frameCache.getSpriteFrame(frameNames[i]); 155 if (!spriteFrame) { 156 cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' refers to frame '" + frameNames[i] 157 + "' which is not currently in the cc.SpriteFrameCache. This frame will not be added to the animation."); 158 continue; 159 } 160 var animFrame = new cc.AnimationFrame(); 161 animFrame.initWithSpriteFrame(spriteFrame, 1, null); 162 frames.push(animFrame); 163 } 164 165 if (frames.length === 0) { 166 cc.log("cocos2d: cc.AnimationCache: None of the frames for animation '" + key 167 + "' were found in the cc.SpriteFrameCache. Animation is not being added to the Animation Cache."); 168 continue; 169 } else if (frames.length != frameNames.length) { 170 cc.log("cocos2d: cc.AnimationCache: An animation in your dictionary refers to a frame which is not in the cc.SpriteFrameCache." + 171 " Some or all of the frames for the animation '" + key + "' may be missing."); 172 } 173 animation = cc.Animation.createWithAnimationFrames(frames, delay, 1); 174 cc.AnimationCache.getInstance().addAnimation(animation, key); 175 } 176 }, 177 178 _parseVersion2:function (animations) { 179 var frameCache = cc.SpriteFrameCache.getInstance(); 180 181 for (var key in animations) { 182 var animationDict = animations[key]; 183 184 var isLoop = animationDict["loop"]; 185 var loopsTemp = parseInt(animationDict["loops"]); 186 var loops = isLoop ? cc.REPEAT_FOREVER : ((isNaN(loopsTemp)) ? 1 : loopsTemp); 187 var restoreOriginalFrame = (animationDict["restoreOriginalFrame"] && animationDict["restoreOriginalFrame"] == true) ? true : false; 188 var frameArray = animationDict["frames"]; 189 190 if (!frameArray) { 191 cc.log("cocos2d: CCAnimationCache: Animation '" + key + "' found in dictionary without any frames - cannot add to animation cache."); 192 continue; 193 } 194 195 //Array of AnimationFrames 196 var arr = []; 197 for (var i = 0; i < frameArray.length; i++) { 198 var entry = frameArray[i]; 199 var spriteFrameName = entry["spriteframe"]; 200 var spriteFrame = frameCache.getSpriteFrame(spriteFrameName); 201 if (!spriteFrame) { 202 cc.log("cocos2d: cc.AnimationCache: Animation '" + key + "' refers to frame '" + spriteFrameName 203 + "' which is not currently in the cc.SpriteFrameCache. This frame will not be added to the animation."); 204 continue; 205 } 206 207 var delayUnits = parseFloat(entry["delayUnits"]) || 0; 208 var userInfo = entry["notification"]; 209 var animFrame = new cc.AnimationFrame(); 210 animFrame.initWithSpriteFrame(spriteFrame, delayUnits, userInfo); 211 arr.push(animFrame); 212 } 213 214 var delayPerUnit = parseFloat(animationDict["delayPerUnit"]) || 0; 215 var animation = new cc.Animation(); 216 animation.initWithAnimationFrames(arr, delayPerUnit, loops); 217 animation.setRestoreOriginalFrame(restoreOriginalFrame); 218 cc.AnimationCache.getInstance().addAnimation(animation, key); 219 } 220 }, 221 222 /** 223 * initialize cc.AnimationCache 224 * @return {Boolean} 225 */ 226 init:function () { 227 this._animations = {}; 228 return true; 229 }, 230 231 _animations:null 232 }); 233 234 /** 235 * Purges the cache. It releases all the cc.Animation objects and the shared instance. 236 */ 237 cc.AnimationCache.purgeSharedAnimationCache = function () { 238 if (cc.s_sharedAnimationCache) { 239 cc.s_sharedAnimationCache._animations = null; 240 cc.s_sharedAnimationCache = null; 241 } 242 }; 243 244 /** 245 * Retruns ths shared instance of the Animation cache 246 * @return {cc.AnimationCache} 247 */ 248 cc.AnimationCache.getInstance = function () { 249 if (cc.s_sharedAnimationCache === null) { 250 cc.s_sharedAnimationCache = new cc.AnimationCache(); 251 cc.s_sharedAnimationCache.init(); 252 } 253 return cc.s_sharedAnimationCache; 254 }; 255 256 cc.s_sharedAnimationCache = null; 257