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