From a51bcf3262350c4316a6bf7eaeee3134d5cd7e31 Mon Sep 17 00:00:00 2001 From: tslil clingman <> Date: Tue, 28 Jan 2020 12:08:35 -0800 Subject: restructured main loop to handle GUI element focus --- src/gui.rs | 60 ++++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 18 deletions(-) (limited to 'src/gui.rs') diff --git a/src/gui.rs b/src/gui.rs index ed5acb5..4899bb7 100644 --- a/src/gui.rs +++ b/src/gui.rs @@ -5,6 +5,22 @@ pub use pancurses::*; use crate::game::*; +#[derive(PartialEq)] +pub enum GUIResult { + Undo, + SwitchElement, + Raw(String), +} + +impl GUIResult { + pub fn is_raw(&self) -> bool { + match self { + GUIResult::Raw(_) => true, + _ => false, + } + } +} + pub struct GameLog { action_window: Window, pieces_window: Window, @@ -23,13 +39,17 @@ impl GameLog { white_colour: u8, black_colour: u8, ) -> GameLog { - GameLog { + let gl = GameLog { pieces_window: newwin(4, width, ypos, xpos), - action_window: newwin(height, width, ypos + 5, xpos), + action_window: newwin(height - 5, width, ypos + 5, xpos), has_colours: has_colours, white_colour: white_colour, black_colour: black_colour, - } + }; + + gl.action_window.setscrreg(0, height); + gl.action_window.scrollok(true); + gl } pub fn update(&self, game: &Game) { @@ -78,7 +98,7 @@ impl GameLog { self.pieces_window.attrset(Attribute::Normal); self.pieces_window.mv(3, 0); - self.pieces_window.addstr("Player: "); + self.pieces_window.addstr("Turn: "); match game.query_current_player() { Player::Black => { if self.has_colours { @@ -115,11 +135,6 @@ pub struct BoardGUI { black_colour: u8, } -pub enum BGAction { - Quit, - Selected(Action), -} - impl BoardGUI { fn draw_grid(&self) { let s: i32 = self.size as i32; @@ -224,7 +239,7 @@ impl BoardGUI { self.board_window.untouch(); } - pub fn read_action(&mut self) -> BGAction { + pub fn read_action(&mut self) -> GUIResult { loop { if let Some(key) = self.board_window.getch() { match key { @@ -252,7 +267,7 @@ impl BoardGUI { self.mv_to_cell(&self.cur_cell, 0); } } - Character('q') => return BGAction::Quit, + Character('q') => (), x => { self.board_window.addstr(format!("{:?}", x)); @@ -285,7 +300,12 @@ impl BoardGUI { for i in 0..s * s { let y: i32 = i / s; let x: i32 = i % s; - cell_windows.push(newwin(h - 1, w - 1, (s - y - 1) * h + 1, hoff + x * w + 1)); + cell_windows.push(newwin( + h - 1, + w - 1, + (s - y - 1) * h + 1 + ypos, + hoff + x * w + 1 + xpos, + )); } let bg = BoardGUI { @@ -357,7 +377,7 @@ impl ActionEntry { ae } - pub fn read_action(&self) -> String { + pub fn read_action(&self) -> GUIResult { self.window.mv(0, self.xstart); self.window.refresh(); @@ -365,8 +385,9 @@ impl ActionEntry { loop { if let Some(inp) = self.window.getch() { match inp { - Character('\n') => return result, - KeyEnter => return result, // TODO: is this ever used? + Character('\t') => return GUIResult::SwitchElement, + Character('\n') => break, + Character(c) => { // Annoyingly there are two backspace possibilities let ascii = c as usize; @@ -392,11 +413,15 @@ impl ActionEntry { self.window.mv(y, x - 1); } } + + KeyEnter => break, // TODO: is this ever used? _ => (), }; } self.window.refresh(); } + self.clear_entry_area(); + return GUIResult::Raw(result); } } @@ -415,12 +440,11 @@ impl MessageLog { has_colours: bool, red_colour: u8, ) -> MessageLog { - let ml = MessageLog { + MessageLog { window: newwin(height, width, ypos, xpos), has_colours: has_colours, red_colour: red_colour, - }; - ml + } } pub fn log_message(&self, str: String) { -- cgit v1.3.1