[134] | 1 | var geneWindow = |
---|
| 2 | { |
---|
| 3 | context: undefined, |
---|
| 4 | urlToXML: "f0def.xml", |
---|
| 5 | debugCreatureName: "test", |
---|
| 6 | xml: undefined, |
---|
| 7 | model: new Model(), |
---|
| 8 | part: new Part(), |
---|
| 9 | joint: new Joint(), |
---|
| 10 | neuro: new Neuro(), |
---|
| 11 | neuroConn: new NeuroConn(), |
---|
| 12 | parts: [], |
---|
| 13 | joints: [], |
---|
| 14 | neurons: [], |
---|
| 15 | neuroConns: [], |
---|
[143] | 16 | neuroClasses: [], |
---|
[134] | 17 | modelOne: undefined, |
---|
| 18 | _graphicsEngine: new GraphicsEngine(), |
---|
[143] | 19 | _neuronDrawer: new NeuronDrawer(), |
---|
[134] | 20 | downloadXML: function () { |
---|
| 21 | var local = this; |
---|
| 22 | $.ajax({ |
---|
[149] | 23 | url: "http://www.framsticks.com/files/apps/config/f0def.xml", |
---|
[134] | 24 | dataType: "xml", |
---|
| 25 | async: false, |
---|
| 26 | success: function (xml) { |
---|
| 27 | var xmlDoc = $.parseXML(xml); |
---|
| 28 | $xml = $(xmlDoc); |
---|
| 29 | local.xml = $(xml); |
---|
| 30 | }, |
---|
| 31 | error: function () { |
---|
| 32 | alert("Can't download file f0def.xml"); |
---|
| 33 | } |
---|
| 34 | }) |
---|
| 35 | }, |
---|
| 36 | parseGeneXml: function () { |
---|
[143] | 37 | this._parseClass($(this.xml.find("CLASS"))); |
---|
| 38 | this._parseNeuroClass($(this.xml.find("NEUROCLASS"))) |
---|
| 39 | }, |
---|
| 40 | _parseClass: function(nodes){ |
---|
[134] | 41 | var local = this; |
---|
| 42 | |
---|
| 43 | nodes.each(function (entry) { |
---|
| 44 | var node = $(nodes[entry]); |
---|
| 45 | |
---|
| 46 | if (node.attr("NAME") == "Model") { |
---|
| 47 | local.model.setModel(node); |
---|
| 48 | } |
---|
| 49 | else if (node.attr("NAME") == "Part") |
---|
| 50 | local.part.setModel(node); |
---|
| 51 | else if (node.attr("NAME") == "Joint") |
---|
| 52 | local.joint.setModel(node); |
---|
| 53 | else if (node.attr("NAME") == "Neuro") |
---|
| 54 | local.neuro.setModel(node); |
---|
| 55 | else if (node.attr("NAME") == "NeuroConn") |
---|
| 56 | local.neuroConn.setModel(node); |
---|
| 57 | else { |
---|
[149] | 58 | console.log("Could not recognize NAME:", node.attr("NAME")); |
---|
[134] | 59 | } |
---|
| 60 | |
---|
| 61 | }); |
---|
| 62 | }, |
---|
[143] | 63 | _parseNeuroClass: function(data){ |
---|
| 64 | |
---|
| 65 | for(var i = 0; i < data.length; i++) |
---|
| 66 | { |
---|
| 67 | var neuroClass = new NeuroClass(); |
---|
| 68 | neuroClass.setModel(data); |
---|
| 69 | this.neuroClasses.push(neuroClass); |
---|
| 70 | } |
---|
| 71 | }, |
---|
[134] | 72 | analyseLine: function (line) { |
---|
| 73 | //ignore comment |
---|
| 74 | if (line[0] == '#' || line == "") |
---|
| 75 | return; |
---|
| 76 | |
---|
| 77 | var object; |
---|
| 78 | var type = line[0]; |
---|
| 79 | if (type == "p") { |
---|
| 80 | object = $.extend(true, {}, this.part); |
---|
| 81 | this.parts.push(object); |
---|
| 82 | } |
---|
| 83 | else if (type == "j") { |
---|
| 84 | object = $.extend(true, {}, this.joint); |
---|
| 85 | this.joints.push(object); |
---|
| 86 | } |
---|
| 87 | else if (type == "n") { |
---|
| 88 | object = $.extend(true, {}, this.neuro); |
---|
| 89 | this.neurons.push(object); |
---|
| 90 | } |
---|
| 91 | else if (type == "c") { |
---|
| 92 | object = $.extend(true, {}, this.neuroConn); |
---|
| 93 | this.neuroConns.push(object); |
---|
| 94 | } |
---|
| 95 | else if (type == "m") { |
---|
| 96 | object = $.extend(true, {}, this.model); |
---|
| 97 | this.modelOne = object; |
---|
| 98 | } |
---|
| 99 | else |
---|
| 100 | throw new Error("Undefined element: \"" + type + "\""); |
---|
| 101 | |
---|
| 102 | //remove char and ":" |
---|
| 103 | line = line.substring(2); |
---|
| 104 | var lines = line.match(/([^,"]+|"[^"]+")+/g); |
---|
| 105 | |
---|
| 106 | if (lines != null) |
---|
| 107 | lines.forEach(function (value) { |
---|
| 108 | value = value.trim(); |
---|
| 109 | if (value == "") |
---|
| 110 | object.setValue(); |
---|
| 111 | else if (value.indexOf("=") == -1) { |
---|
| 112 | object.setValue(value); |
---|
| 113 | } |
---|
| 114 | else { |
---|
| 115 | var name = value.substring(0, value.indexOf("=")); |
---|
| 116 | var val = value.substring(value.indexOf("=") + 1); |
---|
| 117 | object.setValue(name, val); |
---|
| 118 | } |
---|
| 119 | }) |
---|
| 120 | }, |
---|
| 121 | downloadCreature: function () { |
---|
| 122 | var lines; |
---|
[143] | 123 | $.ajax({ |
---|
| 124 | url: "http://localhost:63343/FramestickFavi/creatures/" + this.debugCreatureName + ".txt", |
---|
[134] | 125 | async: false, |
---|
| 126 | dataType: "text", |
---|
| 127 | success: function (data) { |
---|
| 128 | lines = data.split("\n"); |
---|
| 129 | |
---|
| 130 | }, |
---|
| 131 | error: function () { |
---|
| 132 | alert("Can't download creature"); |
---|
| 133 | } |
---|
| 134 | |
---|
[143] | 135 | }); |
---|
[134] | 136 | |
---|
[143] | 137 | //lines = $("#geno").val(); |
---|
| 138 | //lines = lines.split("\n"); |
---|
[134] | 139 | |
---|
| 140 | return lines; |
---|
| 141 | }, |
---|
| 142 | parseCreature: function () { |
---|
| 143 | var lines = this.downloadCreature(); |
---|
| 144 | lines.splice(0, 1); |
---|
| 145 | var local = this; |
---|
| 146 | lines.forEach(function (value) { |
---|
| 147 | local.analyseLine(value); |
---|
| 148 | }); |
---|
| 149 | }, |
---|
| 150 | renderCreature: function () { |
---|
| 151 | |
---|
| 152 | if($("#axisBox").is(":checked")) |
---|
| 153 | this._graphicsEngine.showPartAxis(); |
---|
| 154 | |
---|
| 155 | for (var i = 0; i < this.parts.length; i++) |
---|
| 156 | this._graphicsEngine.addPart(this.parts[i]) |
---|
| 157 | |
---|
| 158 | for (var i = 0; i < this.joints.length; i++) { |
---|
| 159 | this._graphicsEngine.addJoint(this.joints[i]); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | }, |
---|
| 163 | mainLoop: function () { |
---|
| 164 | this._graphicsEngine.initializeScene(); |
---|
| 165 | this.renderCreature(); |
---|
| 166 | //this._graphicsEngine.debugTest(); |
---|
| 167 | this._graphicsEngine.renderScene(); |
---|
[143] | 168 | |
---|
| 169 | this._neuronDrawer.initializeScene(); |
---|
| 170 | new SmartLayout(this.neurons, this.neuroConns); |
---|
| 171 | this._neuronDrawer.drawNetwork(this.neurons, this.neuroConns, einfos); |
---|
| 172 | this._neuronDrawer.renderScene(); |
---|
| 173 | |
---|
[134] | 174 | } |
---|
| 175 | |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | function openWindow() { |
---|
| 179 | geneWindow.downloadXML(); |
---|
| 180 | geneWindow.parseGeneXml(); |
---|
| 181 | geneWindow.parseCreature(); |
---|
| 182 | geneWindow.mainLoop(); |
---|
| 183 | |
---|
| 184 | } |
---|