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 * RelativeData 27 * @constructor 28 */ 29 ccs.RelativeData = function(){ 30 this.plistFiles=[]; 31 this.armatures=[]; 32 this.animations=[]; 33 this.textures=[]; 34 }; 35 36 /** 37 * format and manage armature configuration and armature animation 38 * @class 39 * @extends ccs.Class 40 */ 41 ccs.ArmatureDataManager = ccs.Class.extend(/** @lends ccs.ArmatureDataManager# */{ 42 _animationDatas:null, 43 _armarureDatas:null, 44 _textureDatas:null, 45 _autoLoadSpriteFile:null, 46 _relativeDatas:null, 47 ctor:function () { 48 this._animationDatas = {}; 49 this._armarureDatas = {}; 50 this._textureDatas = {}; 51 this._autoLoadSpriteFile = false; 52 this._relativeDatas = {}; 53 }, 54 init:function () { 55 56 }, 57 58 /** 59 * remove armature cache data by configFilePath 60 * @param {String} configFilePath 61 */ 62 removeArmatureFileInfo:function(configFilePath){ 63 var data = this.getRelativeData(configFilePath); 64 for (var i = 0; i < data.armatures.length; i++) { 65 var obj = data.armatures[i]; 66 this.removeArmatureData(obj); 67 } 68 for (var i = 0; i < data.animations.length; i++) { 69 var obj = data.animations[i]; 70 this.removeAnimationData(obj); 71 } 72 for (var i = 0; i < data.textures.length; i++) { 73 var obj = data.textures[i]; 74 this.removeTextureData(obj); 75 } 76 for (var i = 0; i < data.plistFiles.length; i++) { 77 var obj = data.plistFiles[i]; 78 cc.SpriteFrameCache.getInstance().removeSpriteFramesFromFile(obj); 79 } 80 delete this._relativeDatas[configFilePath]; 81 ccs.DataReaderHelper.removeConfigFile(configFilePath); 82 }, 83 84 /** 85 * Add armature data 86 * @param {string} id The id of the armature data 87 * @param {ccs.ArmatureData} armatureData 88 */ 89 addArmatureData:function (id, armatureData, configFilePath) { 90 if (this._armarureDatas) { 91 var data = this.getRelativeData(configFilePath); 92 data.armatures.push(id); 93 this._armarureDatas[id] = armatureData; 94 } 95 }, 96 97 /** 98 * remove armature data 99 * @param {string} id 100 */ 101 removeArmatureData:function(id){ 102 if (this._armarureDatas.hasOwnProperty(id)) 103 delete this._armarureDatas[id]; 104 }, 105 106 /** 107 * get armatureData by id 108 * @param {String} id 109 * @return {ccs.ArmatureData} 110 */ 111 getArmatureData:function (id) { 112 var armatureData = null; 113 if (this._armarureDatas) { 114 armatureData = this._armarureDatas[id]; 115 } 116 return armatureData; 117 }, 118 119 /** 120 * get armatureDatas 121 * @return {Object} 122 */ 123 getArmatureDatas:function () { 124 return this._armarureDatas; 125 }, 126 127 /** 128 * add animation data 129 * @param {String} id 130 * @param {ccs.AnimationData} animationData 131 */ 132 addAnimationData:function (id, animationData, configFilePath) { 133 if (this._animationDatas) { 134 var data = this.getRelativeData(configFilePath); 135 data.animations.push(id); 136 this._animationDatas[id] = animationData; 137 } 138 }, 139 140 /** 141 * remove animation data 142 * @param {string} id 143 */ 144 removeAnimationData:function(id){ 145 if (this._animationDatas.hasOwnProperty(id)) 146 delete this._animationDatas[id]; 147 }, 148 149 /** 150 * get animationData by id 151 * @param {String} id 152 * @return {ccs.AnimationData} 153 */ 154 getAnimationData:function (id) { 155 var animationData = null; 156 if (this._animationDatas[id]) { 157 animationData = this._animationDatas[id]; 158 } 159 return animationData; 160 }, 161 162 /** 163 * get animationDatas 164 * @return {Object} 165 */ 166 getAnimationDatas:function () { 167 return this._animationDatas; 168 }, 169 170 /** 171 * add texture data 172 * @param {String} id 173 * @param {ccs.TextureData} textureData 174 */ 175 addTextureData:function (id, textureData, configFilePath) { 176 if (this._textureDatas) { 177 var data = this.getRelativeData(configFilePath); 178 data.textures.push(id); 179 this._textureDatas[id] = textureData; 180 } 181 }, 182 183 /** 184 * remove texture data 185 * @param {string} id 186 */ 187 removeTextureData:function(id){ 188 if (this._textureDatas.hasOwnProperty(id)) 189 delete this._textureDatas[id]; 190 }, 191 192 /** 193 * get textureData by id 194 * @param {String} id 195 * @return {ccs.TextureData} 196 */ 197 getTextureData:function (id) { 198 var textureData = null; 199 if (this._textureDatas) { 200 textureData = this._textureDatas[id]; 201 } 202 return textureData; 203 }, 204 205 /** 206 * get textureDatas 207 * @return {Object} 208 */ 209 getTextureDatas:function () { 210 return this._textureDatas; 211 }, 212 213 /** 214 * Add ArmatureFileInfo, it is managed by CCArmatureDataManager. 215 * @param {String} imagePath 216 * @param {String} plistPath 217 * @param {String} configFilePath 218 * @example 219 * //example1 220 * ccs.ArmatureDataManager.getInstance().addArmatureFileInfo("res/test.json"); 221 * //example2 222 * ccs.ArmatureDataManager.getInstance().addArmatureFileInfo("res/test.png","res/test.plist","res/test.json"); 223 */ 224 addArmatureFileInfo:function (/*imagePath,plistPath,configFilePath*/) { 225 var imagePath,plistPath,configFilePath; 226 var isLoadSpriteFrame = false; 227 if (arguments.length == 1) { 228 configFilePath = arguments[0]; 229 isLoadSpriteFrame = true; 230 this.addRelativeData(configFilePath); 231 } else if (arguments.length == 3){ 232 imagePath = arguments[0]; 233 plistPath = arguments[1]; 234 configFilePath = arguments[2]; 235 this.addRelativeData(configFilePath); 236 this.addSpriteFrameFromFile(plistPath, imagePath, configFilePath); 237 } 238 ccs.DataReaderHelper.addDataFromFile(configFilePath,isLoadSpriteFrame); 239 }, 240 241 /** 242 * Add ArmatureFileInfo, it is managed by CCArmatureDataManager. 243 * @param {String} imagePath 244 * @param {String} plistPath 245 * @param {String} configFilePath 246 * @param {Object} target 247 * @param {Function} configFilePath 248 */ 249 addArmatureFileInfoAsync:function (/*imagePath, plistPath, configFilePath,target,selector*/) { 250 var imagePath, plistPath, configFilePath,target,selector; 251 var isLoadSpriteFrame = false; 252 if (arguments.length == 3) { 253 configFilePath = arguments[0]; 254 selector = arguments[1]; 255 target = arguments[2]; 256 isLoadSpriteFrame = true; 257 this.addRelativeData(configFilePath); 258 } else if (arguments.length == 5){ 259 imagePath = arguments[0]; 260 plistPath = arguments[1]; 261 configFilePath = arguments[2]; 262 selector = arguments[3]; 263 target = arguments[4]; 264 this.addRelativeData(configFilePath); 265 this.addSpriteFrameFromFile(plistPath, imagePath, configFilePath); 266 } 267 ccs.DataReaderHelper.addDataFromFileAsync(configFilePath,target,selector,isLoadSpriteFrame); 268 269 }, 270 271 /** 272 * Add sprite frame to CCSpriteFrameCache, it will save display name and it's relative image name 273 * @param {String} plistPath 274 * @param {String} imagePath 275 */ 276 addSpriteFrameFromFile:function (plistPath, imagePath, configFilePath) { 277 var data = this.getRelativeData(configFilePath); 278 data.plistFiles.push(plistPath); 279 ccs.SpriteFrameCacheHelper.getInstance().addSpriteFrameFromFile(plistPath, imagePath); 280 }, 281 282 isAutoLoadSpriteFile:function(){ 283 return this._autoLoadSpriteFile; 284 }, 285 286 /** 287 * add RelativeData 288 * @param {String} configFilePath 289 */ 290 addRelativeData: function (configFilePath) { 291 if (!this._relativeDatas.hasOwnProperty(configFilePath)) 292 this._relativeDatas[configFilePath] = new ccs.RelativeData(); 293 }, 294 295 /** 296 * get RelativeData 297 * @param {String} configFilePath 298 * @returns {ccs.RelativeData} 299 */ 300 getRelativeData: function (configFilePath) { 301 return this._relativeDatas[configFilePath]; 302 }, 303 304 removeAll:function () { 305 this._animationDatas = null; 306 this._armarureDatas = null; 307 this._textureDatas = null; 308 ccs.DataReaderHelper.purge(); 309 } 310 }); 311 312 ccs.ArmatureDataManager._instance = null; 313 /** 314 * returns a shared instance of the ArmatureDataManager 315 * @function 316 * @return {ccs.ArmatureDataManager} 317 */ 318 ccs.ArmatureDataManager.getInstance = function () { 319 if (!this._instance) { 320 this._instance = new ccs.ArmatureDataManager(); 321 this._instance.init(); 322 } 323 return this._instance; 324 }; 325 ccs.ArmatureDataManager.purge = function () { 326 ccs.SpriteFrameCacheHelper.purge(); 327 ccs.DataReaderHelper.purge(); 328 this._instance = null; 329 };