Index: src/gui.d ================================================================== --- src/gui.d +++ src/gui.d @@ -11,25 +11,20 @@ setup_size(g.map.W, g.map.H); setup_resources(g); draw(g); } - private void delegate(char c) fn; - void set_fn(F)(F f) { this.fn = f; } - - void run(bool automate = false) + void run(void delegate(char c) command, bool automate = true) { if(automate) { Timer t = new Timer; t.interval = 50; - t.tick ~= (Timer sender, EventArgs ea){ - fn(solver.single_step()); - }; + t.tick ~= (Timer s, EventArgs e){command(solver.single_step());}; t.start(); this.closing ~= (Form f,CancelEventArgs c){t.stop();}; } else { - setup_keyhandling(); + setup_keyhandling(command); } Application.run(this); } override void on_game_changed(char c, in Game g, bool finished) @@ -36,21 +31,20 @@ { draw(g); } private: - int cell; - void setup_size(int W, int H) { this.formBorderStyle = FormBorderStyle.FIXED_DIALOG; this.maximizeBox = false; this.minimizeBox = false; this.cell = min(1024/W, 640/H); this.clientSize = Size(W*cell, H*cell); } + int cell; Font font; Color[char] colors; string[char] render; Graphics graphicContext; @@ -83,15 +77,12 @@ this.render['d'] = "☠"; this.render['L'] = "☒"; this.render['O'] = "☐"; this.render['W'] = "ꔣ"; this.render['!'] = "✄"; - foreach(c,tp; g.map.tr_target) { - char d = g.map[tp]; - this.render[c] = [cast(dchar)('☢'+d-'1')].to!string(); - } - foreach(char c; '1'..':') this.render[c] = [cast(dchar)('☢'+c-'1')].to!string(); + foreach(c,tp; g.map.tr_target) this.render[c] = [cast(dchar)('☢'+g.map[tp]-'1')].to!string(); + foreach(char c; '1'..':') this.render[c] = [cast(dchar)('☢'+c-'1')].to!string(); this.paint ~= (Control c, PaintEventArgs ev) { graphicContext.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clientSize.height)); }; } @@ -128,35 +119,31 @@ " Razor: ", g.map.razor); invalidate(); } private: - void setup_keyhandling() + void setup_keyhandling(void delegate(char c) command) { noMessageFilter(); - this.keyDown ~= &my_keydown; - } - - void do_manual_command(char c) - { - solver.force(c); - fn(c); - } - - void my_keydown(Control c, KeyEventArgs ev) - { - switch(ev.keyCode) - { - case Keys.DOWN: do_manual_command('D'); break; - case Keys.UP: do_manual_command('U'); break; - case Keys.LEFT: do_manual_command('L'); break; - case Keys.RIGHT: do_manual_command('R'); break; - case Keys.W: do_manual_command('W'); break; - case Keys.S: do_manual_command('S'); break; - case Keys.A: do_manual_command('A'); break; - case Keys.G: fn(solver.single_step()); break; - default: break; - } + this.keyDown ~= (Control c, KeyEventArgs ev) { + void do_manual_command(char c) + { + solver.force(c); + command(c); + } + switch(ev.keyCode) + { + case Keys.DOWN: do_manual_command('D'); break; + case Keys.UP: do_manual_command('U'); break; + case Keys.LEFT: do_manual_command('L'); break; + case Keys.RIGHT: do_manual_command('R'); break; + case Keys.W: do_manual_command('W'); break; + case Keys.S: do_manual_command('S'); break; + case Keys.A: do_manual_command('A'); break; + case Keys.G: command(solver.single_step()); break; + default: break; + } + }; } Solver solver; } Index: src/gui_main.d ================================================================== --- src/gui_main.d +++ src/gui_main.d @@ -1,15 +1,16 @@ +import util; import gui; import output; import driver; import solver; import std.stdio; pragma(lib, "dfl.lib"); -void main(string[] args) +void main(string[] argv) { - Driver d = new Driver(stdin); + bool automate = (argv.length>=2 && std.string.indexOf(argv[1],"auto")>=0); + + auto d = new Driver(stdin); d.addObserver!(GuardedOutput)(); - auto g = d.addObserver!(GUI!MainSolver)(); - g.set_fn(&d.command); - g.run(); + d.addObserver!(GUI!MainSolver)().run(&d.command, automate); }