6293256fec 2012-07-14 kinaba: import dfl.all; 6293256fec 2012-07-14 kinaba: import util; 6293256fec 2012-07-14 kinaba: import game; bee0596f0f 2012-07-14 kinaba: import driver; 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: class GUI(Solver) : Form, GameObserver 6293256fec 2012-07-14 kinaba: { a0c3529225 2012-07-14 kinaba: this(in Game g) 9d4aca73fa 2012-07-14 kinaba: { 9d4aca73fa 2012-07-14 kinaba: this.solver = new Solver(g); 9d4aca73fa 2012-07-14 kinaba: setup_size(g.map.W, g.map.H); 9d4aca73fa 2012-07-14 kinaba: setup_resources(); 9d4aca73fa 2012-07-14 kinaba: draw(g); 9d4aca73fa 2012-07-14 kinaba: } 9d4aca73fa 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: private void delegate(char c) fn; 9d4aca73fa 2012-07-14 kinaba: void set_fn(F)(F f) { this.fn = f; } 9d4aca73fa 2012-07-14 kinaba: 8a59754e60 2012-07-14 kinaba: void run(bool automate = false) 9d4aca73fa 2012-07-14 kinaba: { 8a59754e60 2012-07-14 kinaba: if(automate) { 8a59754e60 2012-07-14 kinaba: Timer t = new Timer; 0c10424b3c 2012-07-14 kinaba: t.interval = 50; 8a59754e60 2012-07-14 kinaba: t.tick ~= (Timer sender, EventArgs ea){ 8a59754e60 2012-07-14 kinaba: fn(solver.single_step()); 8a59754e60 2012-07-14 kinaba: }; 8a59754e60 2012-07-14 kinaba: t.start(); 976b6210b7 2012-07-14 kinaba: this.closing ~= (Form f,CancelEventArgs c){t.stop();}; db3405fee7 2012-07-14 kinaba: } else { db3405fee7 2012-07-14 kinaba: setup_keyhandling(); 8a59754e60 2012-07-14 kinaba: } 9d4aca73fa 2012-07-14 kinaba: Application.run(this); bee0596f0f 2012-07-14 kinaba: } bee0596f0f 2012-07-14 kinaba: aea6cfc7e3 2012-07-14 kinaba: override void on_game_changed(char c, in Game g, bool finished) 9d4aca73fa 2012-07-14 kinaba: { 9d4aca73fa 2012-07-14 kinaba: draw(g); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: private: 9d4aca73fa 2012-07-14 kinaba: int cell; 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: void setup_size(int W, int H) 9d4aca73fa 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: this.formBorderStyle = FormBorderStyle.FIXED_DIALOG; 6293256fec 2012-07-14 kinaba: this.maximizeBox = false; 6293256fec 2012-07-14 kinaba: this.minimizeBox = false; 9d4aca73fa 2012-07-14 kinaba: this.cell = min(1024/W, 640/H); 9d4aca73fa 2012-07-14 kinaba: this.clientSize = Size(W*cell, H*cell); 9d4aca73fa 2012-07-14 kinaba: } bee0596f0f 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: Font font; 9d4aca73fa 2012-07-14 kinaba: Color[char] colors; 9d4aca73fa 2012-07-14 kinaba: string[char] render; 9d4aca73fa 2012-07-14 kinaba: Graphics graphicContext; 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: void setup_resources() 9d4aca73fa 2012-07-14 kinaba: { 9d4aca73fa 2012-07-14 kinaba: this.graphicContext = new MemoryGraphics(this.clientSize.width, this.clientSize.height); 9d4aca73fa 2012-07-14 kinaba: this.setStyle(ControlStyles.OPAQUE, true); 6293256fec 2012-07-14 kinaba: this.font = new Font("MS Gothic", cell-2, GraphicsUnit.PIXEL); 6293256fec 2012-07-14 kinaba: this.backColor = Color(255,255,255); 6293256fec 2012-07-14 kinaba: this.colors['#'] = 6293256fec 2012-07-14 kinaba: this.colors['.'] = Color(255,191,127); 6293256fec 2012-07-14 kinaba: this.colors['*'] = Color(255,127,127); 6293256fec 2012-07-14 kinaba: this.colors['R'] = Color(128,128,0); 9d4aca73fa 2012-07-14 kinaba: this.colors['D'] = Color(255,0,0); 6293256fec 2012-07-14 kinaba: this.colors['\\'] = 6293256fec 2012-07-14 kinaba: this.colors['L'] = 6293256fec 2012-07-14 kinaba: this.colors['O'] = Color(127,255,127); 9d4aca73fa 2012-07-14 kinaba: this.colors['W'] = Color(204,229,255); 6293256fec 2012-07-14 kinaba: this.render['#'] = "■"; 6293256fec 2012-07-14 kinaba: this.render['*'] = "✹"; 6293256fec 2012-07-14 kinaba: this.render['.'] = "♒"; 6293256fec 2012-07-14 kinaba: this.render['\\'] = "λ"; 6293256fec 2012-07-14 kinaba: this.render['R'] = "☃"; 6293256fec 2012-07-14 kinaba: this.render['D'] = "☠"; 6293256fec 2012-07-14 kinaba: this.render['L'] = "☒"; 6293256fec 2012-07-14 kinaba: this.render['O'] = "☐"; 9d4aca73fa 2012-07-14 kinaba: this.paint ~= (Control c, PaintEventArgs ev) { 9d4aca73fa 2012-07-14 kinaba: graphicContext.copyTo(ev.graphics, Rect(0,0,this.clientSize.width,this.clientSize.height)); 9d4aca73fa 2012-07-14 kinaba: }; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: aea6cfc7e3 2012-07-14 kinaba: void draw(in Game g) 6293256fec 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: int scrW = this.clientSize.width; bee0596f0f 2012-07-14 kinaba: int scrH = this.clientSize.height; 9d4aca73fa 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Fill bg. 9d4aca73fa 2012-07-14 kinaba: graphicContext.fillRectangle(this.backColor, Rect(0,0,scrW,scrH)); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Fill water. 6293256fec 2012-07-14 kinaba: int w = g.water_level(); 9d4aca73fa 2012-07-14 kinaba: graphicContext.fillRectangle(this.colors['W'], Rect(0, scrH-cell*w-1, scrW, cell*w+1)); 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: // Paint map. 6293256fec 2012-07-14 kinaba: for(int y=1; y<=g.map.H; ++y) 6293256fec 2012-07-14 kinaba: for(int x=1; x<=g.map.W; ++x) { 6293256fec 2012-07-14 kinaba: Rect r = Rect(cell*(x-1), scrH-cell*y, cell, cell); 6293256fec 2012-07-14 kinaba: char c = g.map[y,x]; 6293256fec 2012-07-14 kinaba: if( c != ' ' ) { 6293256fec 2012-07-14 kinaba: if( c == 'R' && g.dead ) 6293256fec 2012-07-14 kinaba: c = 'D'; 9d4aca73fa 2012-07-14 kinaba: graphicContext.drawText(this.render[c], font, this.colors[c], r); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } bee0596f0f 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: // Update textual info. 9d4aca73fa 2012-07-14 kinaba: this.text = .text("Score: ", g.score, " Air: ", g.hp, " Tide: ", g.water_until_rise); 9d4aca73fa 2012-07-14 kinaba: invalidate(); 9d4aca73fa 2012-07-14 kinaba: } 9d4aca73fa 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: private: 9d4aca73fa 2012-07-14 kinaba: void setup_keyhandling() 9d4aca73fa 2012-07-14 kinaba: { 9d4aca73fa 2012-07-14 kinaba: noMessageFilter(); 9d4aca73fa 2012-07-14 kinaba: this.keyDown ~= &my_keydown; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 6293256fec 2012-07-14 kinaba: void my_keydown(Control c, KeyEventArgs ev) 6293256fec 2012-07-14 kinaba: { 6293256fec 2012-07-14 kinaba: switch(ev.keyCode) 6293256fec 2012-07-14 kinaba: { bee0596f0f 2012-07-14 kinaba: case Keys.DOWN: fn('D'); break; bee0596f0f 2012-07-14 kinaba: case Keys.UP: fn('U'); break; bee0596f0f 2012-07-14 kinaba: case Keys.LEFT: fn('L'); break; bee0596f0f 2012-07-14 kinaba: case Keys.RIGHT: fn('R'); break; bee0596f0f 2012-07-14 kinaba: case Keys.W: fn('W'); break; bee0596f0f 2012-07-14 kinaba: case Keys.A: fn('A'); break; 9d4aca73fa 2012-07-14 kinaba: case Keys.G: fn(solver.single_step()); break; 6293256fec 2012-07-14 kinaba: default: break; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: Solver solver; 6293256fec 2012-07-14 kinaba: }