git @ Cat's Eye Technologies Circute / 6923123
Restore ability to depict on a canvas. Chris Pressey 2 years ago
1 changed file(s) with 61 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
66 "yoob/element-factory.js",
77 "yoob/playfield.js",
88 "yoob/playfield-html-view.js",
9 "yoob/playfield-canvas-view.js",
910 "yoob/controller.js",
1011 "yoob/source-manager.js",
1112 "yoob/preset-manager.js",
2122 var subPanel = yoob.makeDiv(container);
2223 var selectConfiguration = yoob.makeSelect(subPanel, 'example configuration:', []);
2324
25 yoob.makeLineBreak(subPanel);
26 var changeDepiction;
27 var selectDepiction = yoob.makeSelect(subPanel, 'depict using:', [
28 ['text', 'text'],
29 ['canvas', 'canvas']
30 ], function(value) {
31 changeDepiction(value);
32 });
33
2434 var displayContainer = yoob.makeDiv(container);
2535 displayContainer.id = 'display_container';
2636
27 var generationDisplay = yoob.makePre(displayContainer);
28 generationDisplay.id = 'generation_display';
37 var displayText = yoob.makePre(displayContainer);
38
39 var displayCanvas = yoob.makeCanvas(displayContainer);
2940
3041 var editor = yoob.makeTextArea(displayContainer, 40, 25);
3142
32 var generationView = new yoob.PlayfieldHTMLView().init({
33 element: generationDisplay
43 var htmlView = new yoob.PlayfieldHTMLView().init({
44 element: displayText
3445 });
35
36 // NOTE this is kind of chintzy, but, oh well
37 generationView.draw = function() {
46 htmlView.draw = function() {
3847 this.element.innerHTML = this.pf.dump(dumpMapper);
3948 };
4049
50 // CanvasView
51 var colourMap = {
52 'Space': '#ffffff',
53 'Spark': '#ff0000',
54 'Tail': '#a0a0a0',
55 'Wire': '#ffff00',
56 'NAND': '#0000ff'
57 };
58 var canvasView = new yoob.PlayfieldCanvasView().init({
59 canvas: displayCanvas
60 });
61 canvasView.drawCell = function(ctx, value, playfieldX, playfieldY,
62 canvasX, canvasY, cellWidth, cellHeight) {
63 ctx.fillStyle = colourMap[value] || '#ffffff';
64 ctx.fillRect(canvasX, canvasY, cellWidth, cellHeight);
65 };
66
67 // Playfield
68 var pf;
69
70 // "View Manager"
71 var currentView = 'text';
72 var views = {
73 'text': htmlView,
74 'canvas': canvasView
75 };
76 var draw = function(pf) {
77 views[currentView].setPlayfield(pf);
78 views[currentView].draw();
79 };
80 changeDepiction = function(value) {
81 if (value === 'text') {
82 displayText.style.display = 'block';
83 displayCanvas.style.display = 'none';
84 } else {
85 displayText.style.display = 'none';
86 displayCanvas.style.display = 'block';
87 }
88 currentView = value;
89 draw(pf);
90 }
91
4192 /* ----- launch, phase 2: connect the controller ----- */
42 var pf;
4393 var controller = (new yoob.Controller()).init({
4494 panelContainer: controlPanel,
4595 step: function() {
4696 var newPf = (new yoob.Playfield()).init({ defaultValue: 'Space' });
4797 evolve_playfield(pf, newPf);
4898 pf = newPf;
49 generationView.setPlayfield(pf);
50 generationView.draw();
99 draw(pf);
51100 },
52101 reset: function(text) {
53102 pf = (new yoob.Playfield()).init({ defaultValue: 'Space' });
54103 pf.load(0, 0, text, loadMapper);
55 generationView.setPlayfield(pf);
56 generationView.draw();
104 draw(pf);
57105 }
58106 });
59107 controller.clickStop();
61109 var sourceManager = (new yoob.SourceManager()).init({
62110 panelContainer: controlPanel,
63111 editor: editor,
64 hideDuringEdit: [generationDisplay],
112 hideDuringEdit: [displayText, displayCanvas],
65113 disableDuringEdit: [controller.panel],
66114 storageKey: 'circute.alp',
67115 onDone: function() {