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