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  * Base class for ccs.SceneReader
 27  * @class
 28  * @extends ccs.Class
 29  */
 30 ccs.SceneReader = ccs.Class.extend(/** @lends ccs.SceneReader# */{
 31     _baseBath:"",
 32     ctor: function () {
 33         this._instance = null;
 34         this._baseBath = "";
 35     },
 36     /**
 37      * create node with json file that exported by cocostudio scene editor
 38      * @param pszFileName
 39      * @returns {cc.Node}
 40      */
 41     createNodeWithSceneFile: function (pszFileName) {
 42         var data = 0;
 43         var node = null;
 44         do {
 45             if (!pszFileName)
 46                 break;
 47 
 48             var pos = pszFileName.lastIndexOf("/");
 49             if(pos>-1){
 50                 this._baseBath =pszFileName.substr(0,pos+1);
 51             }
 52             data = cc.FileUtils.getInstance().getTextFileData(pszFileName);
 53 
 54             if (!data)
 55                 break;
 56 
 57             var jsonDict = JSON.parse(data);
 58             node = this.createObject(jsonDict, null);
 59         } while (0);
 60         this._baseBath = "";
 61         return node;
 62     },
 63 
 64     /**
 65      *  create object from data
 66      * @param {Object} inputFiles
 67      * @param {cc.Node} parenet
 68      * @returns {cc.Node}
 69      */
 70     createObject: function (inputFiles, parenet) {
 71         var className = inputFiles["classname"];
 72         if (className == "CCNode") {
 73             var gb = null;
 74             if (!parenet) {
 75                 gb = cc.Node.create();
 76             }
 77             else {
 78                 gb = cc.Node.create();
 79                 parenet.addChild(gb);
 80             }
 81 
 82             this.setPropertyFromJsonDict(gb, inputFiles);
 83 
 84             var components = inputFiles["components"];
 85             for (var i = 0; i < components.length; i++) {
 86                 var subDict = components[i];
 87                 if (!subDict) {
 88                     break;
 89                 }
 90                 var className = subDict["classname"];
 91                 var comName = subDict["name"];
 92 
 93                 var fileData = subDict["fileData"];
 94                 var path = "",fullPath = "",plistFile = "",fullPlistFile = "";
 95                 var resType = 0;
 96                 path +=this._baseBath;
 97                 if (fileData != null) {
 98                     if(fileData.hasOwnProperty("resourceType")){
 99                         resType = fileData["resourceType"]
100                     }else{
101                         resType =-1;
102                     }
103 
104                     path += fileData["path"];
105                     plistFile += fileData["plistFile"];
106 
107                     fullPath = cc.FileUtils.getInstance().fullPathForFilename(path);
108                     fullPlistFile = cc.FileUtils.getInstance().fullPathForFilename(plistFile);
109                 }
110 
111                 if (className == "CCSprite") {
112                     var sprite = null;
113 
114                     if (resType == 0) {
115                         var startPos = path.lastIndexOf(".png");
116                         if (startPos <= -1) {
117                             continue;
118                         }
119                         sprite = cc.Sprite.create(path);
120                     }
121                     else if (resType == 1) {
122                         var startPos = plistFile.lastIndexOf(".plist");
123                         if (startPos <= -1) {
124                             continue;
125                         }
126                         var startPos = plistFile.lastIndexOf(".", plistFile.length);
127                         var pngFile = plistFile.substr(0, startPos);
128                         pngFile = pngFile + ".png";
129 
130                         plistFile = this._baseBath + plistFile;
131                         pngFile = this._baseBath + pngFile;
132                         cc.SpriteFrameCache.getInstance().addSpriteFrames(plistFile, pngFile);
133                         sprite = cc.Sprite.createWithSpriteFrameName(fileData["path"]);
134                     }
135                     else {
136                         continue;
137                     }
138 
139                     var render = ccs.ComRender.create(sprite, "CCSprite");
140                     if (comName != null) {
141                         render.setName(comName);
142                     }
143 
144                     gb.addComponent(render);
145                 }
146                 else if (className == "CCTMXTiledMap") {
147                     var tmx = null;
148                     if (resType == 0) {
149                         var startPos = path.lastIndexOf(".tmx");
150                         if (startPos <= -1) {
151                             continue;
152                         }
153                         tmx = cc.TMXTiledMap.create(path);
154                     }
155                     else {
156                         continue;
157                     }
158 
159                     var render = ccs.ComRender.create(tmx, "CCTMXTiledMap");
160                     if (comName != null) {
161                         render.setName(comName);
162                     }
163                     gb.addComponent(render);
164                 }
165                 else if (className == "CCParticleSystemQuad") {
166                     var startPos = path.lastIndexOf(".plist");
167                     if (startPos <= -1) {
168                         continue;
169                     }
170 
171                     var particle = null;
172                     if (resType == 0) {
173                         particle = cc.ParticleSystem.create(path);
174                     }
175                     else {
176                         cc.log("unknown resourcetype on CCParticleSystemQuad!");
177                     }
178 
179                     particle.setPosition(0, 0);
180                     var render = ccs.ComRender.create(particle, "CCParticleSystemQuad");
181                     if (comName != null) {
182                         render.setName(comName);
183                     }
184                     gb.addComponent(render);
185                 }
186                 else if (className == "CCArmature") {
187                     if (resType != 0) {
188                         continue;
189                     }
190                     var reDir = path;
191                     var file_path = "";
192                     var pos = reDir.lastIndexOf('/');
193                     if (pos != -1) {
194                         file_path = reDir.substr(0, pos + 1);
195                     }
196                     var des = cc.FileUtils.getInstance().getTextFileData(path);
197                     if (!des) {
198                         cc.log("read json file[%s] error!\n", path);
199                         continue;
200                     }
201                     var jsonDict = JSON.parse(des);
202                     var armature_data = jsonDict["armature_data"];
203                     var childrenCount = armature_data.length;
204                     var subData = armature_data[0];
205                     var name = subData["name"];
206 
207                     var config_file_path = jsonDict["config_file_path"];
208                     childrenCount = config_file_path.length;
209                     for (var i = 0; i < childrenCount; ++i) {
210                         var plist = config_file_path[i];
211                         var plistpath = "";
212                         plistpath += file_path;
213                         plistpath += plist;
214                         var locFullPlistPath = cc.FileUtils.getInstance().fullPathForFilename(plistpath);
215                         var root = cc.FileUtils.getInstance().createDictionaryWithContentsOfFile(locFullPlistPath);
216                         var metadata = root["metadata"];
217                         var textureFileName = metadata["textureFileName"];
218 
219                         var textupath = "";
220                         textupath += file_path;
221                         textupath += textureFileName;
222                         ccs.ArmatureDataManager.getInstance().addArmatureFileInfo(textupath, plistpath, path);
223                     }
224 
225                     var armature = ccs.Armature.create(name);
226                     var render = ccs.ComRender.create(armature, "CCArmature");
227                     if (comName != null) {
228                         render.setName(comName);
229                     }
230                     gb.addComponent(render);
231 
232                     var actionName = subDict["selectedactionname"];
233                     if (actionName && armature.getAnimation()) {
234                         armature.getAnimation().play(actionName);
235                     }
236                     jsonDict = null;
237                     subData = null;
238                     des = null;
239                 }
240                 else if (className == "CCComAudio") {
241                     var audio = null;
242                     if (resType == 0) {
243                         audio = ccs.ComAudio.create();
244                     }
245                     else {
246                         continue;
247                     }
248                     audio.preloadEffect(path);
249                     gb.addComponent(audio);
250                 }
251                 else if (className == "CCComAttribute") {
252                     var attribute = null;
253                     if (resType == 0) {
254                         attribute = ccs.ComAttribute.create();
255                         if(this._baseBath!=path){
256                             var data = cc.FileUtils.getInstance().getTextFileData(path);
257                             if (data) {
258                                 attribute.setDict(JSON.parse(data));
259                             }
260                         }
261                     }
262                     else {
263                         cc.log("unknown resourcetype on CCComAttribute!");
264                         continue;
265                     }
266                     gb.addComponent(attribute);
267                 }
268                 else if (className == "CCBackgroundAudio") {
269                     var audio = null;
270                     if (resType == 0) {
271                         audio = ccs.ComAudio.create();
272                     }
273                     else {
274                         continue;
275                     }
276                     audio.preloadBackgroundMusic(path);
277                     audio.setFile(path);
278                     var bLoop = Boolean(subDict["loop"] || 0);
279                     audio.setLoop(bLoop);
280                     gb.addComponent(audio);
281                     audio.playBackgroundMusic(path, bLoop);
282                 }
283                 else if (className == "GUIComponent") {
284                     var pLayer = ccs.UILayer.create();
285                     pLayer.scheduleUpdate();
286                     var widget = ccs.GUIReader.getInstance().widgetFromJsonFile(path);
287                     pLayer.addWidget(widget);
288                     var render = ccs.ComRender.create(pLayer, "GUIComponent");
289                     if (comName != null) {
290                         render.setName(comName);
291                     }
292                     gb.addComponent(render);
293                 }
294                 subDict = null;
295             }
296             var gameobjects = inputFiles["gameobjects"];
297             for (var i = 0; i < gameobjects.length; i++) {
298                 var subDict = gameobjects[i];
299                 if (!subDict) {
300                     break;
301                 }
302                 this.createObject(subDict, gb);
303                 subDict = null;
304             }
305 
306             return gb;
307         }
308 
309         return null;
310     },
311 
312     /**
313      * set property
314      * @param {cc.Node} node
315      * @param {Object} dict
316      */
317     setPropertyFromJsonDict: function (node, dict) {
318         var x = dict["x"] || 0;
319         var y = dict["y"] || 0;
320         node.setPosition(cc.p(x, y));
321 
322         var bVisible = Boolean(dict["visible"] || 1);
323         node.setVisible(bVisible);
324 
325         var nTag = dict["objecttag"] || -1;
326         node.setTag(nTag);
327 
328         var nZorder = dict["zorder"] || 0;
329         node.setZOrder(nZorder);
330 
331         var fScaleX = dict["scalex"] || 1;
332         var fScaleY = dict["scaley"] || 1;
333         node.setScaleX(fScaleX);
334         node.setScaleY(fScaleY);
335 
336         var fRotationZ = dict["rotation"] || 0;
337         node.setRotation(fRotationZ);
338     },
339 
340     /**
341      * purge instance
342      */
343     purge: function () {
344         this._instance = null;
345     }
346 });
347 ccs.SceneReader._instance = null;
348 /**
349  * get a singleton SceneReader
350  * @function
351  * @return {ccs.SceneReader}
352  */
353 ccs.SceneReader.getInstance = function () {
354     if (!this._instance) {
355         this._instance = new ccs.SceneReader();
356     }
357     return this._instance;
358 };
359 ccs.SceneReader.sceneReaderVersion = function () {
360     return "1.0.0.0";
361 };
362