Work out some of the view issues by reorganizing a bit.
Cat's Eye Technologies
9 years ago
5 | 5 | #input { |
6 | 6 | display: none; |
7 | 7 | } |
8 | #output { | |
8 | #display_container { | |
9 | border: 1px solid red; | |
10 | padding: 4px; | |
11 | } | |
12 | #display_text { | |
9 | 13 | border: 1px solid blue; |
10 | 14 | } |
11 | #canvas { | |
15 | #display_canvas { | |
12 | 16 | display: none; |
13 | 17 | border: 1px solid green; |
14 | 18 | } |
44 | 48 | </select> |
45 | 49 | </span> |
46 | 50 | |
47 | <textarea id="input" rows="10"> | |
51 | <textarea id="input" rows="20" cols="80"> | |
48 | 52 | ========= |
49 | 53 | = = |
50 | 54 | ==N== = |
54 | 58 | ========= |
55 | 59 | </textarea> |
56 | 60 | |
57 | Depict with: | |
58 | <select id="select_depiction"> | |
59 | <option>text</option> | |
60 | <option>canvas</option> | |
61 | </select> | |
62 | ||
63 | <pre id="output"></pre> | |
64 | <canvas id="canvas" width="400" height="250"> | |
65 | There would be an HTML5 canvas here, but your browser isn't displaying it. | |
66 | </canvas> | |
61 | <div id="display_container"> | |
62 | Depict with: | |
63 | <select id="select_depiction"> | |
64 | <option>text</option> | |
65 | <option>canvas</option> | |
66 | </select> | |
67 | ||
68 | <pre id="display_text"></pre> | |
69 | <canvas id="display_canvas" width="400" height="250"> | |
70 | There would be an HTML5 canvas here, but your browser isn't displaying it. | |
71 | </canvas> | |
72 | </div> | |
67 | 73 | |
68 | 74 | <div id="and" style="display: none;" |
69 | 75 | > = |
241 | 247 | <script src="yoob/playfield-canvas-view.js"></script> |
242 | 248 | <script src="../script/circute.js"></script> |
243 | 249 | <script> |
244 | var output = document.getElementById('output'); | |
245 | var canvas = document.getElementById('canvas'); | |
246 | var currentView = 'text'; | |
247 | ||
248 | var c = new yoob.Controller(); | |
249 | 250 | var pf; |
250 | 251 | |
251 | var htmlView = new yoob.PlayfieldHTMLView().init(pf, output); | |
252 | // Text view | |
253 | var displayText = document.getElementById('display_text'); | |
254 | var htmlView = new yoob.PlayfieldHTMLView().init(pf, displayText); | |
252 | 255 | htmlView.render = function(state) { |
253 | 256 | return dumpMapper(state); |
254 | 257 | }; |
255 | 258 | |
259 | // Canvas view | |
260 | var displayCanvas = document.getElementById('display_canvas'); | |
256 | 261 | var colourMap = { |
257 | 262 | 'Space': '#ffffff', |
258 | 263 | 'Spark': '#ff0000', |
260 | 265 | 'Wire': '#ffff00', |
261 | 266 | 'NAND': '#0000ff' |
262 | 267 | }; |
263 | ||
264 | var canvasView = new yoob.PlayfieldCanvasView().init(pf, canvas); | |
268 | var canvasView = new yoob.PlayfieldCanvasView().init(pf, displayCanvas); | |
265 | 269 | canvasView.drawCell = function(ctx, value, playfieldX, playfieldY, |
266 | 270 | canvasX, canvasY, cellWidth, cellHeight) { |
267 | 271 | ctx.fillStyle = colourMap[value] || '#ffffff'; |
268 | 272 | ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight); |
269 | 273 | }; |
270 | c.views = { | |
274 | ||
275 | // "View Manager" | |
276 | var currentView = 'text'; | |
277 | var views = { | |
271 | 278 | 'text': htmlView, |
272 | 279 | 'canvas': canvasView |
280 | }; | |
281 | var draw = function() { | |
282 | views[currentView].pf = pf; | |
283 | views[currentView].draw(); | |
273 | 284 | }; |
274 | 285 | |
275 | 286 | var selectDepiction = document.getElementById('select_depiction'); |
276 | 287 | selectDepiction.onchange = function() { |
277 | 288 | var value = selectDepiction.options[selectDepiction.selectedIndex].value; |
278 | 289 | if (value === 'text') { |
279 | output.style.display = 'block'; | |
280 | canvas.style.display = 'none'; | |
281 | currentView = value; | |
282 | c.draw(); | |
290 | displayText.style.display = 'block'; | |
291 | displayCanvas.style.display = 'none'; | |
283 | 292 | } else { |
284 | output.style.display = 'none'; | |
285 | canvas.style.display = 'block'; | |
286 | currentView = value; | |
287 | c.draw(); | |
293 | displayText.style.display = 'none'; | |
294 | displayCanvas.style.display = 'block'; | |
288 | 295 | } |
289 | }; | |
290 | ||
291 | // this isn't overriding anything | |
292 | c.draw = function() { | |
293 | this.views[currentView].pf = pf; | |
294 | this.views[currentView].draw(); | |
295 | }; | |
296 | currentView = value; | |
297 | draw(); | |
298 | }; | |
299 | ||
300 | // Controller. We don't subclass, we just monkeypatch. | |
301 | var c = new yoob.Controller(); | |
296 | 302 | |
297 | 303 | c.load = function(text) { |
298 | 304 | pf = new yoob.Playfield(); |
299 | 305 | pf.setDefault('Space'); |
300 | 306 | pf.load(0, 0, text, loadMapper); |
301 | this.draw(); | |
307 | draw(); | |
302 | 308 | }; |
303 | 309 | |
304 | 310 | c.step = function() { |
306 | 312 | newPf.setDefault('Space'); |
307 | 313 | evolve_playfield(pf, newPf); |
308 | 314 | pf = newPf; |
309 | this.draw(); | |
315 | draw(); | |
310 | 316 | }; |
311 | 317 | |
312 | 318 | c.connect({ |
318 | 324 | 'speed': 'speed', |
319 | 325 | 'select': 'select_source', |
320 | 326 | 'source': 'input', |
321 | 'display': 'output' | |
327 | 'display': 'display_container' | |
322 | 328 | }); |
323 | 329 | |
324 | 330 | c.click_load(); |