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