summaryrefslogtreecommitdiff
path: root/src/gui.rs
diff options
context:
space:
mode:
authortslil clingman <>2020-02-10 17:36:50 -0800
committertslil clingman <>2020-02-10 17:36:50 -0800
commit15f64980573085e9ab6a9d47fc2a6e8092d6d109 (patch)
treee0b6e1119d642592326deaad2f2634d027a98c2d /src/gui.rs
parent942c13ee26b180316f46921531c737fa8eb4197e (diff)
Feature completion for 0.2.0
Diffstat (limited to 'src/gui.rs')
-rw-r--r--src/gui.rs92
1 files changed, 51 insertions, 41 deletions
diff --git a/src/gui.rs b/src/gui.rs
index 74f55eb..3a828b6 100644
--- a/src/gui.rs
+++ b/src/gui.rs
@@ -14,6 +14,7 @@ use crate::parser::*;
pub enum GUIResult {
Undo,
Quit,
+ Save,
SwitchElement,
Raw(String),
}
@@ -63,7 +64,7 @@ impl GameLog {
}
self.pieces_window.addstr(format!(
"W: {:2}",
- game.query_pieces(Player::White, Stone::Flat)
+ game.get_pieces(Player::White, Stone::Flat)
));
self.pieces_window.attrset(Attribute::Normal);
@@ -74,10 +75,14 @@ impl GameLog {
}
self.pieces_window.addstr(format!(
"B: {:2}",
- game.query_pieces(Player::Black, Stone::Flat),
+ game.get_pieces(Player::Black, Stone::Flat),
));
self.pieces_window.attrset(Attribute::Normal);
+
+ self.pieces_window
+ .addstr(format!(" Variant: {}", game.get_start_type().to_string()));
+
self.pieces_window.mv(1, 0);
if self.has_colours {
@@ -85,7 +90,7 @@ impl GameLog {
}
self.pieces_window.addstr(format!(
"WC: {}",
- game.query_pieces(Player::White, Stone::Capstone)
+ game.get_pieces(Player::White, Stone::Capstone)
));
self.pieces_window.attrset(Attribute::Normal);
@@ -96,13 +101,16 @@ impl GameLog {
}
self.pieces_window.addstr(format!(
"BC: {}",
- game.query_pieces(Player::Black, Stone::Capstone),
+ game.get_pieces(Player::Black, Stone::Capstone),
));
self.pieces_window.attrset(Attribute::Normal);
+ self.pieces_window
+ .addstr(format!(" Ply: {}", game.get_ply() + 1));
+
self.pieces_window.mv(3, 0);
- self.pieces_window.addstr("Turn: ");
- match game.query_current_player() {
+ self.pieces_window.addstr("Active player: ");
+ match game.get_current_player() {
Player::Black => {
if self.has_colours {
self.pieces_window.attrset(ColorPair(self.black_colour));
@@ -114,9 +122,9 @@ impl GameLog {
}
}
}
- self.pieces_window.addstr(game.query_current_player_name());
+ self.pieces_window.addstr(game.get_current_player_name());
- let new_lines = game.query_action_lines();
+ let new_lines = game.get_action_lines();
if new_lines.len() > self.num_lines {
self.offset = new_lines.len() - self.num_lines;
} else {
@@ -161,6 +169,7 @@ impl GameLog {
Character('\t') => return GUIResult::SwitchElement,
Character('U') => return GUIResult::Undo,
Character('Q') => return GUIResult::Quit,
+ Character('S') => return GUIResult::Save,
_ => (),
}
}
@@ -285,6 +294,7 @@ impl BoardGUI {
self.board_window.untouch();
}
+ /*
pub fn read_action(&mut self) -> GUIResult {
loop {
if let Some(key) = self.board_window.getch() {
@@ -322,7 +332,7 @@ impl BoardGUI {
}
}
}
- }
+ }*/
pub fn new(
ypos: i32,
@@ -509,12 +519,12 @@ enum InputSource {
}
const ACTION_HEADER: &str = "Enter action in PTN";
-const LOG_HEADER: &str = "U to undo a turn | <UP>/<DOWN> to scroll | Q to quit";
+const LOG_HEADER: &str = "U to undo a turn | <UP>/<DOWN> to scroll | S to save PTN | Q to quit";
pub struct GUI {
screen: Window,
game_log: GameLog,
- message_log: MessageLog,
+ pub message_log: MessageLog,
action_entry: ActionEntry,
board_gui: BoardGUI,
input_source: InputSource,
@@ -533,6 +543,25 @@ impl GUI {
self.screen.refresh();
}
+ fn save(&self, game: &Game) {
+ let file = File::create(format!("Game_{}.ptn", game.get_date_string()));
+
+ match file {
+ Err(e) => {
+ self.message_log
+ .log_error(format!("Unable to open PTN file for writing: {}", e));
+ }
+ Ok(file) => {
+ if let Err(e) = write!(&file, "{}", game.to_string()) {
+ self.message_log
+ .log_error(format!("Unable to write PTN to file: {}", e));
+ } else {
+ self.message_log.log_message(String::from("PTN saved."));
+ }
+ }
+ }
+ }
+
pub fn new(game: &Game, warning: String) -> GUI {
let screen = initscr();
cbreak();
@@ -623,7 +652,7 @@ impl GUI {
) -> Result<GUIResult, String> {
match self.input_source {
InputSource::Player => {
- let player = game.query_current_player();
+ let player = game.get_current_player();
let inp = match player {
Player::Black => {
@@ -662,7 +691,7 @@ impl GUI {
p1_white: bool,
input: Result<GUIResult, String>,
) -> bool {
- let player = game.query_current_player();
+ let player = game.get_current_player();
let engine_turn = call_proc
&& ((p1_white && player == Player::Black) || ((!p1_white) && player == Player::White));
@@ -681,6 +710,11 @@ impl GUI {
self.screen.getch();
return false;
}
+ GUIResult::Save => {
+ self.save(game);
+ self.screen.getch();
+ return false;
+ }
GUIResult::SwitchElement => match self.input_source {
InputSource::Player => {
self.input_source = InputSource::Log;
@@ -703,7 +737,7 @@ impl GUI {
}
},
GUIResult::Raw(raw) => {
- let stone_owner = game.query_stone_owner();
+ let stone_owner = game.get_stone_owner();
let act = parse_action(stone_owner, &raw);
match act {
Err(err) => {
@@ -714,7 +748,7 @@ impl GUI {
}
}
Ok(act) => {
- let pn = game.query_current_player_name();
+ let pn = game.get_current_player_name();
self.message_log.log_message(format!(
"{} performs {}{}",
pn,
@@ -736,7 +770,7 @@ impl GUI {
return false;
} else {
self.message_log.log_error(format!(
- "Cannot perform \"{}\": {}",
+ "Cannot perform \"{}\": p{}",
act_string, e
));
}
@@ -754,31 +788,7 @@ impl GUI {
if let Some(inp) = self.screen.getch() {
match inp {
Character('S') => {
- let file = File::create(format!(
- "Game_{}.ptn",
- game.get_date_string()
- ));
-
- match file {
- Err(e) => {
- self.message_log.log_error(format!(
- "Unable to open PTN file for writing: {}",
- e
- ));
- }
- Ok(file) => {
- if let Err(e) = write!(
- &file,
- "{}",
- game.to_string()
- ) {
- self.message_log.log_error(format!(
- "Unable to write PTN to file: {}",
- e
- ));
- }
- }
- }
+ self.save(game);
return false;
}
Character('Q') => {