source: js/viewer-f0/js/main.js @ 172

Last change on this file since 172 was 172, checked in by mmichalski, 10 years ago

Get creature from diffrent sources

File size: 7.0 KB
Line 
1var Viewer =
2{
3
4    Mode: {
5        LINK: 0,
6        EMBEDDED: 1,
7        TEXTAREA: 2,
8        CODE : 3
9    },
10    context: undefined,
11    urlToXML: "f0def.xml",//"http://www.framsticks.com/files/apps/config/f0def.xml",
12    xml: undefined,
13    model: new Model(),
14    part: new Part(),
15    joint: new Joint(),
16    neuro: new Neuro(),
17    neuroConn: new NeuroConn(),
18    parts: [],
19    joints: [],
20    neurons: [],
21    neuroConns: [],
22    neuroClasses: {},
23    modelOne: undefined,
24    _graphicsEngine: new GraphicsEngine(),
25    _neuronDrawer: new NeuronDrawer(),
26    downloadXML: function () {
27        var local = this;
28        $.ajax({
29            url: local.urlToXML,
30            dataType: "xml",
31            async: false,
32            success: function (xml) {
33                var xmlDoc = $.parseXML(xml);
34                $xml = $(xmlDoc);
35                local.xml = $(xml);
36            },
37            error: function () {
38                alert("Can't download file f0def.xml");
39            }
40        })
41    },
42    parseGeneXml: function () {
43        this._parseClass($(this.xml.find("CLASS")));
44        this._parseNeuroClass($(this.xml.find("NEUROCLASS")))
45    },
46    _parseClass: function(nodes){
47        var local = this;
48
49        nodes.each(function (entry) {
50            var node = $(nodes[entry]);
51
52            if (node.attr("NAME") == "Model") {
53                local.model.setModel(node);
54            }
55            else if (node.attr("NAME") == "Part")
56                local.part.setModel(node);
57            else if (node.attr("NAME") == "Joint")
58                local.joint.setModel(node);
59            else if (node.attr("NAME") == "Neuro")
60                local.neuro.setModel(node);
61            else if (node.attr("NAME") == "NeuroConn")
62                local.neuroConn.setModel(node);
63            else {
64                console.log("Could not recognize NAME:", node.attr("NAME"));
65            }
66
67        });
68    },
69    _parseNeuroClass: function(data){
70
71        for(var i = 0; i < data.length; i++)
72        {
73            var neuroClass = new NeuroClass();
74            this.neuroClasses[data[i].attributes[0].value] = neuroClass;
75            neuroClass.setModel(data);
76        }
77
78    },
79    analyseLine: function (line) {
80        //ignore comment
81        if (line[0] == '#' || line == "")
82            return;
83
84        var object;
85        var type = line[0];
86        if (type == "p") {
87            object = $.extend(true, {}, this.part);
88            this.parts.push(object);
89        }
90        else if (type == "j") {
91            object = $.extend(true, {}, this.joint);
92            this.joints.push(object);
93        }
94        else if (type == "n") {
95            object = $.extend(true, {}, this.neuro);
96            this.neurons.push(object);
97        }
98        else if (type == "c") {
99            object = $.extend(true, {}, this.neuroConn);
100            this.neuroConns.push(object);
101        }
102        else if (type == "m") {
103            object = $.extend(true, {}, this.model);
104            this.modelOne = object;
105        }
106        else
107            throw new Error("Undefined element: \"" + type + "\"");
108
109        //remove char and ":"
110        line = line.substring(2);
111        var lines = line.match(/([^,"]+|"[^"]+")+/g);
112
113        if (lines != null)
114            lines.forEach(function (value) {
115                value = value.trim();
116                if (value == "")
117                    object.setValue();
118                else if (value.indexOf("=") == -1) {
119                    object.setValue(value);
120                }
121                else {
122                    var name = value.substring(0, value.indexOf("="));
123                    var val = value.substring(value.indexOf("=") + 1);
124                    object.setValue(name, val);
125                }
126            })
127    },
128    getCreature: function (mode, content) {
129
130        var lines = undefined;
131        if(mode == this.Mode.LINK)
132            lines = this._getCreatureFromLink(content);
133        else if(mode == this.Mode.EMBEDDED)
134            lines = this._getCreatureFromEmbeddedCode(content);
135        else if (mode == this.Mode.TEXTAREA)
136            lines = this._getCreatureFromTextArea(content);
137        else if(mode == this.Mode.CODE)
138            lines = this._getCreatureFromCode(content);
139        else
140        {
141            throw "Wrong mode in function getCreature";
142        }
143        return lines;
144    },
145    _getCreatureFromLink: function(link){
146        var lines = undefined;
147        $.ajax({
148            url: link,
149            async: false,
150            dataType: "text",
151            success: function (data) {
152                lines = data.split("\n");
153
154            },
155            error: function () {
156                alert("Can't download creature");
157            }
158        });
159        return lines;
160    },
161    _getCreatureFromTextArea: function(area){
162        var lines = undefined;
163        lines = area.val();
164        lines = lines.split("\n");
165        return lines;
166    },
167    _getCreatureFromCode: function(code){
168        var lines = code;
169        lines = lines.split("\n");
170        return lines;
171    },
172    _getCreatureFromEmbeddedCode: function(link){
173        var lines = undefined;
174
175        $.ajax({
176            url: link,
177            async: false,
178            dataType: "html",
179            success: function (data) {
180                var geno = $($(data).find("div")[0]).comments().html();
181                geno = geno.replace("FRED_GEN", "");
182                lines = geno.split("\n");
183            },
184            error: function () {
185                alert("Can't download creature");
186            }
187        });
188
189        return lines;
190    },
191    parseCreature: function (mode, content) {
192        var lines = this.getCreature(mode, content);
193        lines.splice(0, 1);
194        var local = this;
195        lines.forEach(function (value) {
196            local.analyseLine(value);
197        });
198    },
199    renderCreature: function () {
200
201        if($("#axisBox").is(":checked"))
202            this._graphicsEngine.showPartAxis();
203
204        for (var i = 0; i < this.parts.length; i++)
205            this._graphicsEngine.addPart(this.parts[i])
206
207        for (var i = 0; i < this.joints.length; i++) {
208            this._graphicsEngine.addJoint(this.joints[i]);
209        }
210
211    },
212    mainLoop: function () {
213        this._graphicsEngine.initializeScene();
214        this.renderCreature();
215        this._graphicsEngine.debugTest();
216        this._graphicsEngine.renderScene();
217
218        this._neuronDrawer.initializeScene();
219        new SmartLayout(this.neurons, this.neuroConns);
220        this._neuronDrawer.drawNetwork(this.neurons, this.neuroConns, einfos, this.neuroClasses);
221        this._neuronDrawer.renderScene();
222
223    }
224}
225
226function openWindow() {
227    var debugCreatureName = "example4";
228    Viewer.downloadXML();
229    Viewer.parseGeneXml();
230    //Viewer.parseCreature(Viewer.Mode.LINK, "http://localhost:63343/FramestickFavi/creatures/" + debugCreatureName + ".txt");
231    //Viewer.parseCreature(Viewer.Mode.TEXTAREA, $("#geno"));
232    //Viewer.parseCreature(Viewer.Mode.CODE, "//0\np:\np: x=1");
233    Viewer.parseCreature(Viewer.Mode.EMBEDDED, "http://ec.framsticks.com/www/index.php?PAGE=view_genotype&ID=55")
234    Viewer.mainLoop();
235
236
237
238}
Note: See TracBrowser for help on using the repository browser.