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