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); e02668367d 2012-07-15 kinaba: setup_size(g.map.W, g.map.H); b8acb5f918 2012-07-14 kinaba: setup_resources(g); 9d4aca73fa 2012-07-14 kinaba: draw(g); 9d4aca73fa 2012-07-14 kinaba: } 9d4aca73fa 2012-07-14 kinaba: b6026abfcf 2012-07-15 kinaba: void run(void delegate(char c) command, bool automate = true) 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; b6026abfcf 2012-07-15 kinaba: t.tick ~= (Timer s, EventArgs e){command(solver.single_step());}; 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 { b6026abfcf 2012-07-15 kinaba: setup_keyhandling(command); 8a59754e60 2012-07-14 kinaba: } 9d4aca73fa 2012-07-14 kinaba: Application.run(this); bee0596f0f 2012-07-14 kinaba: } 6293256fec 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: void setup_size(int W, int H) 6293256fec 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: b6026abfcf 2012-07-15 kinaba: int cell; 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: b8acb5f918 2012-07-14 kinaba: void setup_resources(in Game g) 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); 0e9ec945bb 2012-07-15 kinaba: this.colors['*'] = 0e9ec945bb 2012-07-15 kinaba: this.colors['@'] = Color(255,127,127); 6293256fec 2012-07-14 kinaba: this.colors['R'] = Color(128,128,0); 68b686ff6d 2012-07-15 kinaba: this.colors['r'] = Color(100,128,255); 71cbce70b5 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); 9d983af88c 2012-07-15 kinaba: this.colors['w'] = Color(204,229,255); 9d983af88c 2012-07-15 kinaba: this.colors['W'] = 9d983af88c 2012-07-15 kinaba: this.colors['!'] = Color(159,159,159); b8acb5f918 2012-07-14 kinaba: foreach(char c; 'A'..'J') this.colors[c] = Color(142,142,255); b8acb5f918 2012-07-14 kinaba: foreach(char c; '1'..':') this.colors[c] = Color(255,142,255); 6293256fec 2012-07-14 kinaba: this.render['#'] = "■"; 6293256fec 2012-07-14 kinaba: this.render['*'] = "✹"; 0e9ec945bb 2012-07-15 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'] = "☃"; 68b686ff6d 2012-07-15 kinaba: this.render['r'] = "☃"; 71cbce70b5 2012-07-14 kinaba: this.render['d'] = "☠"; 6293256fec 2012-07-14 kinaba: this.render['L'] = "☒"; deca17f61a 2012-07-14 kinaba: this.render['O'] = "☐"; 9d983af88c 2012-07-15 kinaba: this.render['W'] = "ꔣ"; 9d983af88c 2012-07-15 kinaba: this.render['!'] = "✄"; d40deaae5a 2012-07-15 kinaba: foreach(char c; g.tr.source_list) d40deaae5a 2012-07-15 kinaba: this.render[c] = [cast(dchar)('☢'+g.tr.target_of(c)-'1')].to!string(); d40deaae5a 2012-07-15 kinaba: foreach(char c; g.tr.target_list) d40deaae5a 2012-07-15 kinaba: this.render[c] = [cast(dchar)('☢'+c-'1')].to!string(); 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(); 9d983af88c 2012-07-15 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. e02668367d 2012-07-15 kinaba: for(int y=1; y<=g.map.H; ++y) e02668367d 2012-07-15 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); e02668367d 2012-07-15 kinaba: char c = g.map[y,x]; 6293256fec 2012-07-14 kinaba: if( c != ' ' ) { 68b686ff6d 2012-07-15 kinaba: if( c == 'R' ) 68b686ff6d 2012-07-15 kinaba: c = (g.dead ? 'd' : g.cleared ? 'r' : 'R'); 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. 9d983af88c 2012-07-15 kinaba: this.text = .text( 9d983af88c 2012-07-15 kinaba: "Score: ", g.score, 9d983af88c 2012-07-15 kinaba: " Air: ", g.hp, 9d983af88c 2012-07-15 kinaba: " Tide: ", g.water_until_rise, 9d983af88c 2012-07-15 kinaba: " Wadler: ", g.hige_until_rise, e02668367d 2012-07-15 kinaba: " Razor: ", g.map.razor); 9d4aca73fa 2012-07-14 kinaba: invalidate(); 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: private: b6026abfcf 2012-07-15 kinaba: void setup_keyhandling(void delegate(char c) command) 6293256fec 2012-07-14 kinaba: { 9d4aca73fa 2012-07-14 kinaba: noMessageFilter(); b6026abfcf 2012-07-15 kinaba: this.keyDown ~= (Control c, KeyEventArgs ev) { b6026abfcf 2012-07-15 kinaba: void do_manual_command(char c) b6026abfcf 2012-07-15 kinaba: { b6026abfcf 2012-07-15 kinaba: solver.force(c); b6026abfcf 2012-07-15 kinaba: command(c); b6026abfcf 2012-07-15 kinaba: } b6026abfcf 2012-07-15 kinaba: switch(ev.keyCode) b6026abfcf 2012-07-15 kinaba: { b6026abfcf 2012-07-15 kinaba: case Keys.DOWN: do_manual_command('D'); break; b6026abfcf 2012-07-15 kinaba: case Keys.UP: do_manual_command('U'); break; b6026abfcf 2012-07-15 kinaba: case Keys.LEFT: do_manual_command('L'); break; b6026abfcf 2012-07-15 kinaba: case Keys.RIGHT: do_manual_command('R'); break; b6026abfcf 2012-07-15 kinaba: case Keys.W: do_manual_command('W'); break; b6026abfcf 2012-07-15 kinaba: case Keys.S: do_manual_command('S'); break; b6026abfcf 2012-07-15 kinaba: case Keys.A: do_manual_command('A'); break; b6026abfcf 2012-07-15 kinaba: case Keys.G: command(solver.single_step()); break; b6026abfcf 2012-07-15 kinaba: default: break; b6026abfcf 2012-07-15 kinaba: } b6026abfcf 2012-07-15 kinaba: }; 6293256fec 2012-07-14 kinaba: } 6293256fec 2012-07-14 kinaba: 9d4aca73fa 2012-07-14 kinaba: Solver solver; 6293256fec 2012-07-14 kinaba: }