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