diff options
| author | tslil clingman <> | 2020-01-19 09:51:40 -0800 |
|---|---|---|
| committer | tslil clingman <> | 2020-01-19 09:51:40 -0800 |
| commit | 3b9c74fa818026b7489a1ad0f8e122f9388d739a (patch) | |
| tree | 7034c3bfc62fc417911d8688f7db0df9f7de7bd8 /src | |
| parent | a134b701949a735c4f340c5e2dce189261830f6b (diff) | |
Renaming for sanity
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 111 |
1 files changed, 62 insertions, 49 deletions
diff --git a/src/main.rs b/src/main.rs index 9ccefde..fd946fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,14 +6,14 @@ use pancurses::*; mod game; use crate::game::*; -pub struct GameStateGUI { +pub struct GameLog { action_window: Window, pieces_window: Window, } -impl GameStateGUI { - pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> GameStateGUI { - GameStateGUI { +impl GameLog { + pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> GameLog { + GameLog { pieces_window: newwin(2, width, ypos, xpos), action_window: newwin(height, width, ypos + 3, xpos), } @@ -36,7 +36,7 @@ impl GameStateGUI { } } -struct BoardGUI { +pub struct BoardGUI { board_window: Window, cell_windows: Vec<Window>, size: u8, @@ -50,7 +50,7 @@ struct BoardGUI { } #[derive(PartialEq)] -enum BGAction { +pub enum BGAction { Quit, None, } @@ -137,7 +137,7 @@ impl BoardGUI { } } - fn update<L: Fn(String)>(&self, game: &Game<L>) { + pub fn update<L: Fn(String)>(&self, game: &Game<L>) { // Is this really the `best' way? assert!(game.get_size() == self.size); let mut pos = Position { x: 0, y: 0 }; @@ -152,7 +152,7 @@ impl BoardGUI { } } - fn handle_key(&mut self, key: Input) -> BGAction { + pub fn handle_key(&mut self, key: Input) -> BGAction { match key { KeyDown => { if 0 < self.cur_cell.y { @@ -191,7 +191,7 @@ impl BoardGUI { } } - fn new( + pub fn new( ypos: i32, xpos: i32, size: u8, @@ -244,39 +244,61 @@ impl BoardGUI { } } -struct MoveEntry { - win: Window, +pub struct ActionEntry { + window: Window, } -impl MoveEntry { - fn init(&self) { - self.win.mvaddstr(0, 0, "Enter move: "); - // Make shaded box for entry - self.win.refresh(); +impl ActionEntry { + pub fn new(ypos: i32, xpos: i32, height: i32, width: i32) -> ActionEntry { + let ae = ActionEntry { + window: newwin(height, width, ypos, xpos), + }; + ae.window.addstr("Enter action: "); + ae.window.attrset(Attribute::Reverse); + ae.window.refresh(); + ae } - // fn read_action(&self) -> &str { - // "a1" - // } + fn read_action(&self) -> String { + let mut result = String::new(); + loop { + if let Some(inp) = self.window.getch() { + match inp { + Character(c) => result.push(c), + _ => break, + }; + } + } + result + } } -struct MessageLog { - win: Window, - has_colours: bool, - red_colour: u8, +pub struct MessageLog { + window: Window, } impl MessageLog { - fn init(&self) {} - - fn log_error(&self, str: String) { - if self.has_colours { - self.win.attrset(ColorPair(self.red_colour)); + pub fn new( + ypos: i32, + xpos: i32, + height: i32, + width: i32, + has_colours: bool, + red_colour: u8, + ) -> MessageLog { + let ml = MessageLog { + window: newwin(height, width, ypos, xpos), + }; + if has_colours { + ml.window.attrset(ColorPair(red_colour)); } - // self.win.clear(); - self.win.addstr(str); - self.win.attrset(Attribute::Normal); - self.win.refresh(); + ml + } + + pub fn log_error(&self, str: String) { + self.window.clear(); + self.window.addstr(str); + self.window.refresh(); } } @@ -301,21 +323,12 @@ fn main() { screen.refresh(); - let mut bg = BoardGUI::new(0, 0, 5, 4, 8, 3, has_colours, white_colour, black_colour); - let gs = GameStateGUI::new(0, 5 * 8 + 3 + 3, 100, 20); - - let mew = newwin(1, 30, 25, 0); - let mlw = newwin(3, 70, 26, 0); - let me = MoveEntry { win: mew }; - let ml = MessageLog { - win: mlw, - has_colours: has_colours, - red_colour: red_colour, - }; - me.init(); - ml.init(); + let mut board_gui = BoardGUI::new(0, 0, 5, 4, 8, 3, has_colours, white_colour, black_colour); + let game_log = GameLog::new(0, 5 * 8 + 3 + 3, 100, 20); + let message_log = MessageLog::new(26, 0, 3, 70, has_colours, red_colour); + let action_entry = ActionEntry::new(25, 0, 1, 30); - let log = |s| MessageLog::log_error(&ml, s); + let log = |s| message_log.log_error(s); let mut game = Game::new(5, "tslil", "taktician", log); let pos = Position { x: 0, y: 0 }; @@ -323,8 +336,8 @@ fn main() { game.perform_action(action); - bg.update(&game); - gs.update(&game); + board_gui.update(&game); + game_log.update(&game); // game_state.perform_action(&action); // game_state.is_legal_action(&Action::Move(Player::White, pos, Direction::Up, vec![0, 1])); @@ -332,7 +345,7 @@ fn main() { loop { if let Some(inp) = screen.getch() { - if bg.handle_key(inp) == BGAction::Quit { + if board_gui.handle_key(inp) == BGAction::Quit { break; } } |
