diff options
Diffstat (limited to 'src/gui.rs')
| -rw-r--r-- | src/gui.rs | 68 |
1 files changed, 59 insertions, 9 deletions
@@ -8,6 +8,7 @@ use crate::game::*; #[derive(PartialEq)] pub enum GUIResult { Undo, + Quit, SwitchElement, Raw(String), } @@ -18,6 +19,9 @@ pub struct GameLog { has_colours: bool, white_colour: u8, black_colour: u8, + num_lines: usize, + offset: usize, + action_lines: Vec<String>, } impl GameLog { @@ -33,17 +37,20 @@ impl GameLog { let gl = GameLog { pieces_window: newwin(4, width, ypos, xpos), action_window: newwin(height - 5, width, ypos + 5, xpos), - has_colours: has_colours, - white_colour: white_colour, - black_colour: black_colour, + has_colours, + white_colour, + black_colour, + num_lines: (height - 7) as usize, + offset: 0, + action_lines: Vec::new(), }; - gl.action_window.setscrreg(0, height); - gl.action_window.scrollok(true); + gl.action_window.keypad(true); + gl.action_window.nodelay(false); gl } - pub fn update(&self, game: &Game) { + pub fn update(&mut self, game: &Game) { self.pieces_window.clear(); if self.has_colours { @@ -104,12 +111,55 @@ impl GameLog { } self.pieces_window.addstr(game.query_current_player_name()); + let new_lines = game.query_action_lines(); + if new_lines.len() > self.num_lines { + self.offset = new_lines.len() - self.num_lines; + } else { + self.offset = 0; + } + self.action_lines = new_lines.clone(); + self.list_actions(); + self.pieces_window.refresh(); + } - // Perhaps it's best to avoid clearing things? Overwrite the specific characters instead? + fn list_actions(&self) { self.action_window.clear(); - self.action_window.addstr(game.query_action_lines()); + self.action_window.draw_box(0, 0); + let mut y = 1; + for i in 0..self.num_lines { + if let Some(line) = self.action_lines.get(i + self.offset) { + self.action_window.mv(y, 2); + self.action_window.addstr(line); + y += 1; + } + } self.action_window.refresh(); + self.action_window.mv(1, 2); + } + + pub fn read_action(&mut self) -> GUIResult { + loop { + self.list_actions(); + if let Some(inp) = self.action_window.getch() { + match inp { + KeyUp => { + if self.offset > 0 { + self.offset -= 1; + } + } + KeyDown => { + if self.offset + self.num_lines < self.action_lines.len() { + self.offset += 1; + } + } + Character('\t') => return GUIResult::SwitchElement, + Character('U') => return GUIResult::Undo, + Character('Q') => return GUIResult::Quit, + _ => (), + } + } + } } } @@ -389,7 +439,7 @@ impl ActionEntry { self.window.addch('_'); self.window.mv(y, x - 1); } else { - if result.len() < self.max_len { + if (result.len() < self.max_len) && (ascii > 32) && (ascii < 127) { result.push(c); self.window.addch(c); } |
