Differences From Artifact [8bca70d55515c665]:
- File
gui.d
- 2012-07-14 08:55:55 - part of checkin [69105bf94a] on branch trunk - Stand alone solver. (user: kinaba) [annotate]
- File
src/gui.d
- 2012-07-14 09:16:47 - part of checkin [6293256fec] on branch trunk - Preparing for submission... (user: kinaba) [annotate]
To Artifact [e76b6b97ba56256b]:
- File
src/gui.d
- 2012-07-14 11:24:30 - part of checkin [bee0596f0f] on branch trunk - Refactoring. (user: kinaba) [annotate]
1 import dfl.all; 1 import dfl.all;
2 import util; 2 import util;
3 import game; 3 import game;
4 import output; 4 import output;
> 5 import driver;
5 //import solver; 6 //import solver;
> 7 pragma(lib, "dfl.lib");
6 8
7 class GUI : Form | 9 class GUI : Form, GameObserver
8 { 10 {
> 11 bool on_game_changed(char c, const(Game) g, bool finished) {
> 12 draw(gr, g);
> 13 invalidate();
> 14 return false;
> 15 }
> 16
9 private { 17 private {
10 Game g; <
11 int cell; 18 int cell;
12 int turn = 0; 19 int turn = 0;
13 20
14 Font font; 21 Font font;
15 Color[char] colors; 22 Color[char] colors;
16 string[char] render; 23 string[char] render;
> 24 void delegate(char c) fn;
17 } 25 }
18 26
19 this(Game g) | 27 this(const(Game) g)
20 { 28 {
21 noMessageFilter(); 29 noMessageFilter();
22 this.setStyle(ControlStyles.OPAQUE, true); 30 this.setStyle(ControlStyles.OPAQUE, true);
23 this.g = g; | 31 this.fn = fn;
24 32
25 this.paint ~= &my_paint; 33 this.paint ~= &my_paint;
26 this.keyDown ~= &my_keydown; 34 this.keyDown ~= &my_keydown;
27 35
28 this.formBorderStyle = FormBorderStyle.FIXED_DIALOG; 36 this.formBorderStyle = FormBorderStyle.FIXED_DIALOG;
29 this.maximizeBox = false; 37 this.maximizeBox = false;
30 this.minimizeBox = false; 38 this.minimizeBox = false;
31 this.cell = min(1024/g.map.W, 640/g.map.H); 39 this.cell = min(1024/g.map.W, 640/g.map.H);
32 this.clientSize = Size(g.map.W*cell, g.map.H*cell); 40 this.clientSize = Size(g.map.W*cell, g.map.H*cell);
33 set_text(); <
> 41
> 42 const scrH = this.clientSize.height;
> 43 const scrW = this.clientSize.width;
> 44 this.gr = new MemoryGraphics(scrW, scrH);
34 45
35 // Resources 46 // Resources
36 this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL); 47 this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL);
37 this.backColor = Color(255,255,255); 48 this.backColor = Color(255,255,255);
38 this.colors['#'] = 49 this.colors['#'] =
39 this.colors['.'] = Color(255,191,127); 50 this.colors['.'] = Color(255,191,127);
40 this.colors['*'] = Color(255,127,127); 51 this.colors['*'] = Color(255,127,127);
................................................................................................................................................................................
49 this.render['*'] = "✹"; 60 this.render['*'] = "✹";
50 this.render['.'] = "♒"; 61 this.render['.'] = "♒";
51 this.render['\\'] = "λ"; 62 this.render['\\'] = "λ";
52 this.render['R'] = "☃"; 63 this.render['R'] = "☃";
53 this.render['D'] = "☠"; 64 this.render['D'] = "☠";
54 this.render['L'] = "☒"; 65 this.render['L'] = "☒";
55 this.render['O'] = "☐"; 66 this.render['O'] = "☐";
> 67 draw(gr, g);
> 68 }
> 69
> 70 void set_fn(F)(F f) { this.fn = f; }
> 71
> 72 void run() {
> 73 Application.run(this);
56 } 74 }
57 75
58 private: 76 private:
> 77 Graphics gr;
> 78
59 void my_paint(Control, PaintEventArgs ev) 79 void my_paint(Control, PaintEventArgs ev)
60 { 80 {
61 const scrH = this.clientSize.height; <
62 const scrW = this.clientSize.width; <
63 Graphics gr = new MemoryGraphics(scrW, scrH, ev.graphics); <
64 scope(exit) { <
65 gr.copyTo(ev.graphics, Rect(0,0,scrW,scrH)); | 81 gr.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clien
66 gr.dispose(); <
67 } | 82 }
68 83
> 84 void draw(Graphics gr, const(Game) g)
> 85 {
> 86 int scrW = this.clientSize.width;
> 87 int scrH = this.clientSize.height;
69 // Fill bg. 88 // Fill bg.
70 gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH)); 89 gr.fillRectangle(this.backColor, Rect(0,0,scrW,scrH));
71 90
72 // Fill water. 91 // Fill water.
73 int w = g.water_level(); 92 int w = g.water_level();
74 gr.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW, 93 gr.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW,
75 94
................................................................................................................................................................................
80 char c = g.map[y,x]; 99 char c = g.map[y,x];
81 if( c != ' ' ) { 100 if( c != ' ' ) {
82 if( c == 'R' && g.dead ) 101 if( c == 'R' && g.dead )
83 c = 'D'; 102 c = 'D';
84 gr.drawText(this.render[c], font, this.colors[c] 103 gr.drawText(this.render[c], font, this.colors[c]
85 } 104 }
86 } 105 }
> 106
> 107 set_text(g);
87 } 108 }
88 109
89 void my_keydown(Control c, KeyEventArgs ev) 110 void my_keydown(Control c, KeyEventArgs ev)
90 { 111 {
91 switch(ev.keyCode) 112 switch(ev.keyCode)
92 { 113 {
93 case Keys.DOWN: g.command('D'); break; | 114 case Keys.DOWN: fn('D'); break;
94 case Keys.UP: g.command('U'); break; | 115 case Keys.UP: fn('U'); break;
95 case Keys.LEFT: g.command('L'); break; | 116 case Keys.LEFT: fn('L'); break;
96 case Keys.RIGHT: g.command('R'); break; | 117 case Keys.RIGHT: fn('R'); break;
97 case Keys.W: g.command('W'); break; | 118 case Keys.W: fn('W'); break;
98 case Keys.A: g.command('A'); break; | 119 case Keys.A: fn('A'); break;
99 // case Keys.G: solver.act(g); break; <
100 default: break; 120 default: break;
101 } 121 }
102 if(g.cleared) <
103 Application.exit(); <
104 invalidate(); <
105 set_text(); <
106 } 122 }
107 123
108 void set_text() { | 124 void set_text(const(Game) g) {
109 this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", 125 this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ",
110 } 126 }
111 } 127 }
112 128
113 void main(string[] args) 129 void main(string[] args)
114 { 130 {
115 auto g = Game.load(File(args[1])); | 131 auto d = new Driver(File(args[1]));
116 g.set_output(new GuardedOutput(g)); | 132 d.addObserver!(GuardedOutput)();
117 auto myForm = new GUI(g); | 133 GUI g = d.addObserver!(GUI)();
118 Application.run(myForm); | 134 g.set_fn(&d.command);
> 135 g.run();
119 } 136 }