Overview
SHA1 Hash: | b6026abfcf4b7f1c07603edd0e8a56c78d2c0daf |
---|---|
Date: | 2012-07-15 22:24:12 |
User: | kinaba |
Comment: | Simplified GUI code. |
Timelines: | family | ancestors | descendants | both | trunk |
Diffs: | redesign |
Downloads: | Tarball | ZIP archive |
Other Links: | files | file ages | manifest |
Tags And Properties
- branch=trunk inherited from [16f0b5784f]
- sym-trunk inherited from [16f0b5784f]
Changes
Modified src/gui.d from [9f0067d733f090c1] to [a1f9b7cabb9073dd].
9 9 { 10 10 this.solver = new Solver(g); 11 11 setup_size(g.map.W, g.map.H); 12 12 setup_resources(g); 13 13 draw(g); 14 14 } 15 15 16 - private void delegate(char c) fn; 17 - void set_fn(F)(F f) { this.fn = f; } 18 - 19 - void run(bool automate = false) 16 + void run(void delegate(char c) command, bool automate = true) 20 17 { 21 18 if(automate) { 22 19 Timer t = new Timer; 23 20 t.interval = 50; 24 - t.tick ~= (Timer sender, EventArgs ea){ 25 - fn(solver.single_step()); 26 - }; 21 + t.tick ~= (Timer s, EventArgs e){command(solver.single_step());}; 27 22 t.start(); 28 23 this.closing ~= (Form f,CancelEventArgs c){t.stop();}; 29 24 } else { 30 - setup_keyhandling(); 25 + setup_keyhandling(command); 31 26 } 32 27 Application.run(this); 33 28 } 34 29 35 30 override void on_game_changed(char c, in Game g, bool finished) 36 31 { 37 32 draw(g); 38 33 } 39 34 40 35 private: 41 - int cell; 42 - 43 36 void setup_size(int W, int H) 44 37 { 45 38 this.formBorderStyle = FormBorderStyle.FIXED_DIALOG; 46 39 this.maximizeBox = false; 47 40 this.minimizeBox = false; 48 41 this.cell = min(1024/W, 640/H); 49 42 this.clientSize = Size(W*cell, H*cell); 50 43 } 51 44 45 + int cell; 52 46 Font font; 53 47 Color[char] colors; 54 48 string[char] render; 55 49 Graphics graphicContext; 56 50 57 51 void setup_resources(in Game g) 58 52 { ................................................................................ 81 75 this.render['\\'] = "λ"; 82 76 this.render['R'] = "☃"; 83 77 this.render['d'] = "☠"; 84 78 this.render['L'] = "☒"; 85 79 this.render['O'] = "☐"; 86 80 this.render['W'] = "ꔣ"; 87 81 this.render['!'] = "✄"; 88 - foreach(c,tp; g.map.tr_target) { 89 - char d = g.map[tp]; 90 - this.render[c] = [cast(dchar)('☢'+d-'1')].to!string(); 91 - } 92 - foreach(char c; '1'..':') this.render[c] = [cast(dchar)('☢'+c-'1')].to!string(); 82 + foreach(c,tp; g.map.tr_target) this.render[c] = [cast(dchar)('☢'+g.map[tp]-'1')].to!string(); 83 + foreach(char c; '1'..':') this.render[c] = [cast(dchar)('☢'+c-'1')].to!string(); 93 84 this.paint ~= (Control c, PaintEventArgs ev) { 94 85 graphicContext.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clientSize.height)); 95 86 }; 96 87 } 97 88 98 89 void draw(in Game g) 99 90 { ................................................................................ 126 117 " Tide: ", g.water_until_rise, 127 118 " Wadler: ", g.hige_until_rise, 128 119 " Razor: ", g.map.razor); 129 120 invalidate(); 130 121 } 131 122 132 123 private: 133 - void setup_keyhandling() 124 + void setup_keyhandling(void delegate(char c) command) 134 125 { 135 126 noMessageFilter(); 136 - this.keyDown ~= &my_keydown; 137 - } 138 - 139 - void do_manual_command(char c) 140 - { 141 - solver.force(c); 142 - fn(c); 143 - } 144 - 145 - void my_keydown(Control c, KeyEventArgs ev) 146 - { 147 - switch(ev.keyCode) 148 - { 149 - case Keys.DOWN: do_manual_command('D'); break; 150 - case Keys.UP: do_manual_command('U'); break; 151 - case Keys.LEFT: do_manual_command('L'); break; 152 - case Keys.RIGHT: do_manual_command('R'); break; 153 - case Keys.W: do_manual_command('W'); break; 154 - case Keys.S: do_manual_command('S'); break; 155 - case Keys.A: do_manual_command('A'); break; 156 - case Keys.G: fn(solver.single_step()); break; 157 - default: break; 158 - } 127 + this.keyDown ~= (Control c, KeyEventArgs ev) { 128 + void do_manual_command(char c) 129 + { 130 + solver.force(c); 131 + command(c); 132 + } 133 + switch(ev.keyCode) 134 + { 135 + case Keys.DOWN: do_manual_command('D'); break; 136 + case Keys.UP: do_manual_command('U'); break; 137 + case Keys.LEFT: do_manual_command('L'); break; 138 + case Keys.RIGHT: do_manual_command('R'); break; 139 + case Keys.W: do_manual_command('W'); break; 140 + case Keys.S: do_manual_command('S'); break; 141 + case Keys.A: do_manual_command('A'); break; 142 + case Keys.G: command(solver.single_step()); break; 143 + default: break; 144 + } 145 + }; 159 146 } 160 147 161 148 Solver solver; 162 149 }
Modified src/gui_main.d from [05082d0f328d756c] to [4375fba2fc66f9ab].
1 +import util; 1 2 import gui; 2 3 import output; 3 4 import driver; 4 5 import solver; 5 6 import std.stdio; 6 7 pragma(lib, "dfl.lib"); 7 8 8 -void main(string[] args) 9 +void main(string[] argv) 9 10 { 10 - Driver d = new Driver(stdin); 11 + bool automate = (argv.length>=2 && std.string.indexOf(argv[1],"auto")>=0); 12 + 13 + auto d = new Driver(stdin); 11 14 d.addObserver!(GuardedOutput)(); 12 - auto g = d.addObserver!(GUI!MainSolver)(); 13 - g.set_fn(&d.command); 14 - g.run(); 15 + d.addObserver!(GUI!MainSolver)().run(&d.command, automate); 15 16 }