var Viewer = { Mode: { LINK: 0, EMBEDDED: 1, TEXTAREA: 2, CODE : 3 }, context: undefined, urlToXML: "f0def.xml",//"http://www.framsticks.com/files/apps/config/f0def.xml", xml: undefined, model: new Model(), part: new Part(), joint: new Joint(), neuro: new Neuro(), neuroConn: new NeuroConn(), parts: [], joints: [], neurons: [], neuroConns: [], neuroClasses: {}, modelOne: undefined, _graphicsEngine: new GraphicsEngine(), _neuronDrawer: new NeuronDrawer(), downloadXML: function () { var local = this; $.ajax({ url: local.urlToXML, dataType: "xml", async: false, success: function (xml) { var xmlDoc = $.parseXML(xml); $xml = $(xmlDoc); local.xml = $(xml); }, error: function () { alert("Can't download file f0def.xml"); } }) }, parseGeneXml: function () { this._parseClass($(this.xml.find("CLASS"))); this._parseNeuroClass($(this.xml.find("NEUROCLASS"))) }, _parseClass: function(nodes){ var local = this; nodes.each(function (entry) { var node = $(nodes[entry]); if (node.attr("NAME") == "Model") { local.model.setModel(node); } else if (node.attr("NAME") == "Part") local.part.setModel(node); else if (node.attr("NAME") == "Joint") local.joint.setModel(node); else if (node.attr("NAME") == "Neuro") local.neuro.setModel(node); else if (node.attr("NAME") == "NeuroConn") local.neuroConn.setModel(node); else { console.log("Could not recognize NAME:", node.attr("NAME")); } }); }, _parseNeuroClass: function(data){ for(var i = 0; i < data.length; i++) { var neuroClass = new NeuroClass(); this.neuroClasses[data[i].attributes[0].value] = neuroClass; neuroClass.setModel(data); } }, analyseLine: function (line) { //ignore comment if (line[0] == '#' || line == "") return; var object; var type = line[0]; if (type == "p") { object = $.extend(true, {}, this.part); this.parts.push(object); } else if (type == "j") { object = $.extend(true, {}, this.joint); this.joints.push(object); } else if (type == "n") { object = $.extend(true, {}, this.neuro); this.neurons.push(object); } else if (type == "c") { object = $.extend(true, {}, this.neuroConn); this.neuroConns.push(object); } else if (type == "m") { object = $.extend(true, {}, this.model); this.modelOne = object; } else throw new Error("Undefined element: \"" + type + "\""); //remove char and ":" line = line.substring(2); var lines = line.match(/([^,"]+|"[^"]+")+/g); if (lines != null) lines.forEach(function (value) { value = value.trim(); if (value == "") object.setValue(); else if (value.indexOf("=") == -1) { object.setValue(value); } else { var name = value.substring(0, value.indexOf("=")); var val = value.substring(value.indexOf("=") + 1); object.setValue(name, val); } }) }, getCreature: function (mode, content) { var lines = undefined; if(mode == this.Mode.LINK) lines = this._getCreatureFromLink(content); else if(mode == this.Mode.EMBEDDED) lines = this._getCreatureFromEmbeddedCode(content); else if (mode == this.Mode.TEXTAREA) lines = this._getCreatureFromTextArea(content); else if(mode == this.Mode.CODE) lines = this._getCreatureFromCode(content); else { throw "Wrong mode in function getCreature"; } return lines; }, _getCreatureFromLink: function(link){ var lines = undefined; $.ajax({ url: link, async: false, dataType: "text", success: function (data) { lines = data.split("\n"); }, error: function () { alert("Can't download creature"); } }); return lines; }, _getCreatureFromTextArea: function(area){ var lines = undefined; lines = area.val(); lines = lines.split("\n"); return lines; }, _getCreatureFromCode: function(code){ var lines = code; lines = lines.split("\n"); return lines; }, _getCreatureFromEmbeddedCode: function(link){ var lines = undefined; $.ajax({ url: link, async: false, dataType: "html", success: function (data) { var geno = $($(data).find("div")[0]).comments().html(); geno = geno.replace("FRED_GEN", ""); lines = geno.split("\n"); }, error: function () { alert("Can't download creature"); } }); return lines; }, parseCreature: function (mode, content) { var lines = this.getCreature(mode, content); lines.splice(0, 1); var local = this; lines.forEach(function (value) { local.analyseLine(value); }); }, renderCreature: function () { if($("#axisBox").is(":checked")) this._graphicsEngine.showPartAxis(); for (var i = 0; i < this.parts.length; i++) this._graphicsEngine.addPart(this.parts[i]) for (var i = 0; i < this.joints.length; i++) { this._graphicsEngine.addJoint(this.joints[i]); } }, mainLoop: function () { this._graphicsEngine.initializeScene(); this.renderCreature(); this._graphicsEngine.debugTest(); this._graphicsEngine.renderScene(); this._neuronDrawer.initializeScene(); new SmartLayout(this.neurons, this.neuroConns); this._neuronDrawer.drawNetwork(this.neurons, this.neuroConns, einfos, this.neuroClasses); this._neuronDrawer.renderScene(); } } function openWindow() { var debugCreatureName = "example4"; Viewer.downloadXML(); Viewer.parseGeneXml(); //Viewer.parseCreature(Viewer.Mode.LINK, "http://localhost:63343/FramestickFavi/creatures/" + debugCreatureName + ".txt"); //Viewer.parseCreature(Viewer.Mode.TEXTAREA, $("#geno")); //Viewer.parseCreature(Viewer.Mode.CODE, "//0\np:\np: x=1"); Viewer.parseCreature(Viewer.Mode.EMBEDDED, "http://ec.framsticks.com/www/index.php?PAGE=view_genotype&ID=55") Viewer.mainLoop(); }