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