diff options
Diffstat (limited to 'src/gui.rs')
| -rw-r--r-- | src/gui.rs | 116 |
1 files changed, 96 insertions, 20 deletions
@@ -8,27 +8,96 @@ use crate::game::*; pub struct GameLog { action_window: Window, pieces_window: Window, + has_colours: bool, + white_colour: u8, + black_colour: u8, } impl GameLog { - pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> GameLog { + pub fn new( + ypos: i32, + xpos: i32, + height: i32, + width: i32, + has_colours: bool, + white_colour: u8, + black_colour: u8, + ) -> GameLog { GameLog { - pieces_window: newwin(2, width, ypos, xpos), - action_window: newwin(height, width, ypos + 3, xpos), + pieces_window: newwin(4, width, ypos, xpos), + action_window: newwin(height, width, ypos + 5, xpos), + has_colours: has_colours, + white_colour: white_colour, + black_colour: black_colour, } } pub fn update(&self, game: &Game) { - self.pieces_window.clear(); + // No need to clear, always stays the same size + self.pieces_window.mv(0, 0); + + if self.has_colours { + self.pieces_window.attrset(ColorPair(self.white_colour)); + } + self.pieces_window.addstr(format!( + "W: {:2}", + game.query_pieces(Player::White, Stone::Flat) + )); + + self.pieces_window.attrset(Attribute::Normal); + self.pieces_window.addstr(" "); + + if self.has_colours { + self.pieces_window.attrset(ColorPair(self.black_colour)); + } self.pieces_window.addstr(format!( - "W: {:2} B: {:2}\nWC: {} BC: {}", - game.query_pieces(Player::White, Stone::Flat), + "B: {:2}", game.query_pieces(Player::Black, Stone::Flat), - game.query_pieces(Player::White, Stone::Capstone), + )); + + self.pieces_window.attrset(Attribute::Normal); + self.pieces_window.mv(1, 0); + + if self.has_colours { + self.pieces_window.attrset(ColorPair(self.white_colour)); + } + self.pieces_window.addstr(format!( + "WC: {}", + game.query_pieces(Player::White, Stone::Capstone) + )); + + self.pieces_window.attrset(Attribute::Normal); + self.pieces_window.addstr(" "); + + if self.has_colours { + self.pieces_window.attrset(ColorPair(self.black_colour)); + } + self.pieces_window.addstr(format!( + "BC: {}", game.query_pieces(Player::Black, Stone::Capstone), )); + self.pieces_window.attrset(Attribute::Normal); + + self.pieces_window.mv(3, 0); + self.pieces_window.addstr("Player: "); + match game.query_current_player() { + Player::Black => { + if self.has_colours { + self.pieces_window.attrset(ColorPair(self.black_colour)); + } + self.pieces_window.addstr("Black"); + } + Player::White => { + if self.has_colours { + self.pieces_window.attrset(ColorPair(self.white_colour)); + } + self.pieces_window.addstr("White"); + } + } + self.pieces_window.refresh(); + // Perhaps it's best to avoid clearing things? Overwrite the specific characters instead? self.action_window.clear(); self.action_window.addstr(game.query_action_lines()); self.action_window.refresh(); @@ -124,7 +193,7 @@ impl BoardGUI { fn draw_stack(&self, pos: &Position, stack: &Stack) { if let Some(win) = self.cell_windows.get(self.pos_to_idx(pos)) { - win.mv(0, 0); + win.clear(); let l = stack.len() as i32; if l > 0 { for i in 0..l { @@ -136,19 +205,24 @@ impl BoardGUI { } } - pub fn update(&self, game: &Game) { + pub fn update(&self, game: &Game, squares: Vec<Position>) { // Is this really the `best' way? assert!(game.get_size() == self.size); - let mut pos = Position { x: 0, y: 0 }; - for y in 0..self.size { - pos.y = y; - for x in 0..self.size { - pos.x = x; - if let Some(stack) = game.query_square(&pos) { - self.draw_stack(&pos, stack); - } + // for x in 0..self.size { + // for y in 0..self.size { + // let pos = Position { x: x, y: y }; + // if let Some(stack) = game.query_square(&pos) { + // self.draw_stack(&pos, stack); + // } + // } + // } + for p in squares { + if let Some(stack) = game.query_square(&p) { + self.draw_stack(&p, stack); } } + // Prevent redraw of the grid itself? + self.board_window.touch(); } pub fn read_action(&mut self) -> BGAction { @@ -201,12 +275,14 @@ impl BoardGUI { white_colour: u8, black_colour: u8, ) -> BoardGUI { - let mut cell_windows: Vec<Window> = Vec::new(); - let hoff = hoff as i32; let s: i32 = size as i32; let h: i32 = cell_height as i32; let w: i32 = cell_width as i32; + + let board_window = newwin(h * s + 3, w * s + 3 + hoff, ypos, xpos); + + let mut cell_windows: Vec<Window> = Vec::new(); for i in 0..s * s { let y: i32 = i / s; let x: i32 = i % s; @@ -214,7 +290,7 @@ impl BoardGUI { } let bg = BoardGUI { - board_window: newwin(h * s + 3, w * s + 3 + hoff, ypos, xpos), + board_window: board_window, cell_windows: cell_windows, size: size, cell_height: cell_height, |
