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  * @namespace Base singleton object for ccs.sceneReader
 27  */
 28 ccs.sceneReader = /** @lends ccs.sceneReader# */{
 29     _baseBath:"",
 30     _listener:null,
 31     _selector:null,
 32     _node: null,
 33 
 34     /**
 35      * create node with json file that exported by CocoStudio scene editor
 36      * @param pszFileName
 37      * @returns {cc.Node}
 38      */
 39     createNodeWithSceneFile: function (pszFileName) {
 40         this._baseBath = cc.path.dirname(pszFileName);
 41         var jsonDict = cc.loader.getRes(pszFileName);
 42 
 43         if (!jsonDict) throw "Please load the resource first : " + pszFileName;
 44 
 45         this._node = this.createObject(jsonDict, null);
 46         ccs.triggerManager.parse(jsonDict["Triggers"]||[]);
 47         return this._node;
 48     },
 49 
 50     /**
 51      *  create object from data
 52      * @param {Object} inputFiles
 53      * @param {cc.Node} parenet
 54      * @returns {cc.Node}
 55      */
 56     createObject: function (inputFiles, parenet) {
 57         var className = inputFiles["classname"];
 58         if (className == "CCNode") {
 59             var gb = null;
 60             if (!parenet) {
 61                 gb = cc.Node.create();
 62             }
 63             else {
 64                 gb = cc.Node.create();
 65                 parenet.addChild(gb);
 66             }
 67 
 68             this.setPropertyFromJsonDict(gb, inputFiles);
 69 
 70             var components = inputFiles["components"];
 71             for (var i = 0; i < components.length; i++) {
 72                 var subDict = components[i];
 73                 if (!subDict) {
 74                     break;
 75                 }
 76                 var className = subDict["classname"];
 77                 var comName = subDict["name"];
 78 
 79                 var fileData = subDict["fileData"];
 80                 var path = "", plistFile = "";
 81                 var resType = 0;
 82                 if (fileData != null) {
 83                     if(fileData["resourceType"] !== undefined){
 84                         resType = fileData["resourceType"]
 85                     }else{
 86                         resType =-1;
 87                     }
 88 
 89                     path = cc.path.join(this._baseBath, fileData["path"]);
 90                     plistFile = fileData["plistFile"];
 91                 }
 92 
 93                 var pathExtname = cc.path.extname(path);
 94 
 95                 if (className == "CCSprite") {
 96                     var sprite = null;
 97 
 98                     if (resType == 0) {
 99                         if (pathExtname != ".png") continue;
100                         sprite = cc.Sprite.create(path);
101                     }
102                     else if (resType == 1) {
103                         if (pathExtname != ".plist") continue;
104 
105                         plistFile = cc.path.join(this._baseBath, plistFile);
106                         var pngFile = cc.path.changeExtname(plistFile, ".png");
107                         cc.spriteFrameCache.addSpriteFrames(plistFile, pngFile);
108                         sprite = cc.Sprite.create("#" + fileData["path"]);
109                     }
110                     else {
111                         continue;
112                     }
113 
114                     var render = ccs.ComRender.create(sprite, "CCSprite");
115                     if (comName != null) {
116                         render.setName(comName);
117                     }
118 
119                     gb.addComponent(render);
120                     this._callSelector(sprite, subDict);
121                 }
122                 else if (className == "CCTMXTiledMap") {
123                     var tmx = null;
124                     if (resType == 0) {
125                         if (pathExtname != ".tmx") continue;
126                         tmx = cc.TMXTiledMap.create(path);
127                     }
128                     else {
129                         continue;
130                     }
131 
132                     var render = ccs.ComRender.create(tmx, "CCTMXTiledMap");
133                     if (comName != null) {
134                         render.setName(comName);
135                     }
136                     gb.addComponent(render);
137                     this._callSelector(tmx, subDict);
138                 }
139                 else if (className == "CCParticleSystemQuad") {
140                     if (pathExtname != ".plist") continue;
141 
142                     var particle = null;
143                     if (resType == 0) {
144                         particle = cc.ParticleSystem.create(path);
145                     }
146                     else {
147                         cc.log("unknown resourcetype on CCParticleSystemQuad!");
148                         continue;
149                     }
150 
151                     particle.setPosition(0, 0);
152                     var render = ccs.ComRender.create(particle, "CCParticleSystemQuad");
153                     if (comName != null) {
154                         render.setName(comName);
155                     }
156                     gb.addComponent(render);
157                     this._callSelector(particle, subDict);
158                 }
159                 else if (className == "CCArmature") {
160                     if (resType != 0) {
161                         continue;
162                     }
163                     var jsonDict = cc.loader.getRes(path);
164                     if (!jsonDict) cc.log("Please load the resource [%s] first!", path);
165                     var armature_data = jsonDict["armature_data"];
166                     var subData = armature_data[0];
167                     var name = subData["name"];
168 
169                     ccs.armatureDataManager.addArmatureFileInfo(path);
170 
171                     var armature = ccs.Armature.create(name);
172                     var render = ccs.ComRender.create(armature, "CCArmature");
173                     if (comName != null) {
174                         render.setName(comName);
175                     }
176                     gb.addComponent(render);
177 
178                     var actionName = subDict["selectedactionname"];
179                     if (actionName && armature.getAnimation()) {
180                         armature.getAnimation().play(actionName);
181                     }
182                     jsonDict = null;
183                     subData = null;
184                     this._callSelector(armature, subDict);
185                 }
186                 else if (className == "CCComAudio") {
187                     var audio = null;
188                     if (resType == 0) {
189                         audio = ccs.ComAudio.create();
190                     }
191                     else {
192                         continue;
193                     }
194                     audio.preloadEffect(path);
195                     if (comName) {
196                         audio.setName(comName);
197                     }
198                     gb.addComponent(audio);
199                     this._callSelector(audio, subDict);
200                 }
201                 else if (className == "CCComAttribute") {
202                     var attribute = null;
203                     if (resType == 0) {
204                         attribute = ccs.ComAttribute.create();
205                         if (path != "") attribute.parse(path);
206                     }
207                     else {
208                         cc.log("unknown resourcetype on CCComAttribute!");
209                         continue;
210                     }
211                     if (comName) {
212                         attribute.setName(comName);
213                     }
214                     gb.addComponent(attribute);
215                     this._callSelector(attribute, subDict);
216                 }
217                 else if (className == "CCBackgroundAudio") {
218                     if(!pathExtname) continue;
219                     if(resType!=0) continue;
220 
221                     var audio  = ccs.ComAudio.create();
222                     audio.preloadBackgroundMusic(path);
223                     audio.setFile(path);
224                     var bLoop = Boolean(subDict["loop"] || 0);
225                     audio.setLoop(bLoop);
226                     if (comName) {
227                         audio.setName(comName);
228                     }
229                     gb.addComponent(audio);
230                     audio.playBackgroundMusic(path, bLoop);
231                     this._callSelector(audio, subDict);
232                 }
233                 else if (className == "GUIComponent") {
234                     var widget = ccs.uiReader.widgetFromJsonFile(path);
235                     var render = ccs.ComRender.create(widget, "GUIComponent");
236                     if (comName != null) {
237                         render.setName(comName);
238                     }
239                     gb.addComponent(render);
240                     this._callSelector(audio, subDict);
241                 }
242                 subDict = null;
243             }
244             var gameobjects = inputFiles["gameobjects"];
245             for (var i = 0; i < gameobjects.length; i++) {
246                 var subDict = gameobjects[i];
247                 if (!subDict) {
248                     break;
249                 }
250                 this.createObject(subDict, gb);
251                 subDict = null;
252             }
253 
254             return gb;
255         }
256 
257         return null;
258     },
259 
260 
261     nodeByTag: function (parent, tag) {
262         if (parent == null) {
263             return null;
264         }
265         var retNode = null;
266         var children = parent.getChildren();
267 
268         for (var i = 0; i < children.length; i++) {
269             var child = children[i];
270             if (child && child.getTag() == tag) {
271                 retNode = child;
272                 break;
273             }
274             else {
275                 retNode = this.nodeByTag(child, tag);
276                 if (retNode) {
277                     break;
278                 }
279             }
280         }
281         return retNode;
282     },
283 
284     getNodeByTag: function (tag) {
285         if (this._node == null) {
286             return null;
287         }
288         if (this._node.getTag() == tag) {
289             return this._node;
290         }
291         return this.nodeByTag(this._node, tag);
292     },
293 
294     /**
295      * set property
296      * @param {cc.Node} node
297      * @param {Object} dict
298      */
299     setPropertyFromJsonDict: function (node, dict) {
300         var x = (typeof dict["x"] === 'undefined')?0:dict["x"];
301         var y = (typeof dict["y"] === 'undefined')?0:dict["y"];
302         node.setPosition(x, y);
303 
304         var bVisible = Boolean((typeof dict["visible"] === 'undefined')?1:dict["visible"]);
305         node.setVisible(bVisible);
306 
307         var nTag = (typeof dict["objecttag"] === 'undefined')?-1:dict["objecttag"];
308         node.setTag(nTag);
309 
310         var nZorder = (typeof dict["zorder"] === 'undefined')?0:dict["zorder"];
311         node.setLocalZOrder(nZorder);
312 
313         var fScaleX = (typeof dict["scalex"] === 'undefined')?1:dict["scalex"];
314         var fScaleY = (typeof dict["scaley"] === 'undefined')?1:dict["scaley"];
315         node.setScaleX(fScaleX);
316         node.setScaleY(fScaleY);
317 
318         var fRotationZ = (typeof dict["rotation"] === 'undefined')?0:dict["rotation"];
319         node.setRotation(fRotationZ);
320     },
321     setTarget : function(selector,listener){
322         this._listener = listener;
323         this._selector = selector;
324     },
325     _callSelector:function(obj,subDict){
326         if(this._selector){
327             this._selector.call(this._listener,obj,subDict);
328         }
329     },
330 
331 	version: function () {
332 		return "1.2.0.0";
333 	},
334 
335     /**
336      * Clear data
337      */
338     clear: function () {
339 	    ccs.triggerManager.removeAll();
340 	    cc.audioEngine.end();
341     }
342 };