Differences From Artifact [9f0067d733f090c1]:
- File
src/gui.d
- 2012-07-15 12:20:17 - part of checkin [0e9ec945bb] on branch trunk - horock gui. (user: kinaba) [annotate]
To Artifact [a1f9b7cabb9073dd]:
- File
src/gui.d
- 2012-07-15 13:24:12 - part of checkin [b6026abfcf] on branch trunk - Simplified GUI code. (user: kinaba) [annotate]
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 }