Skip to content

Commit

Permalink
feat: home menu + credit + small fixes (thomas-mauran#47)
Browse files Browse the repository at this point in the history
* feat: home menu + credit + small fixes

Signed-off-by: Mauran <thomas.mauran@etu.umontpellier.fr>

---------

Signed-off-by: Mauran <thomas.mauran@etu.umontpellier.fr>
  • Loading branch information
thomas-mauran authored Dec 9, 2023
1 parent 747a556 commit b61e559
Show file tree
Hide file tree
Showing 13 changed files with 409 additions and 214 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "chess-tui"
version = "1.0.1"
version = "1.1.0"
authors = ["Thomas Mauran"]
license = "MIT"
edition = "2021"
Expand Down
Binary file modified examples/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions examples/demo.tape
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ Set WindowBarSize 40
Type "cargo run" Sleep 500ms Enter

Sleep 1s
Down @0.3s
Down @0.3s
Down @0.3s
Space @0.3s
Sleep 3s

Down @0.3s
Down @0.3s
Expand Down
Binary file modified examples/helper.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions examples/helper.tape
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Set WindowBarSize 40

Type "cargo run" Sleep 500ms Enter

Sleep 2s
Space @0.3s
Sleep 1s

Type h
Expand Down
46 changes: 41 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,25 @@ pub struct App {
pub running: bool,
/// board
pub board: Board,

/// show help popup
pub show_popup: bool,
pub show_help_popup: bool,
/// show credit popup
pub show_credit_popup: bool,
/// show home menu
pub show_home_menu: bool,
/// menu current cursor
pub menu_cursor: u8,
}

impl Default for App {
fn default() -> Self {
Self {
running: true,
board: Board::default(),
show_popup: false,
show_help_popup: false,
show_credit_popup: false,
show_home_menu: true,
menu_cursor: 0,
}
}
}
Expand All @@ -32,8 +40,11 @@ impl App {
Self::default()
}

pub fn show_popup(&mut self) {
self.show_popup = !self.show_popup;
pub fn toggle_help_popup(&mut self) {
self.show_help_popup = !self.show_help_popup;
}
pub fn toggle_credit_popup(&mut self) {
self.show_credit_popup = !self.show_credit_popup;
}

/// Handles the tick event of the terminal.
Expand All @@ -44,9 +55,34 @@ impl App {
self.running = false;
}

pub fn menu_cursor_up(&mut self) {
if self.menu_cursor > 0 {
self.menu_cursor -= 1
} else {
self.menu_cursor = 2
}
}

pub fn menu_cursor_down(&mut self) {
if self.menu_cursor < 2 {
self.menu_cursor += 1
} else {
self.menu_cursor = 0
}
}

pub fn restart(&mut self) {
if self.board.is_draw || self.board.is_checkmate {
self.board = Board::default()
}
}

pub fn menu_select(&mut self) {
match self.menu_cursor {
0 => self.show_home_menu = false,
1 => self.show_help_popup = true,
2 => self.show_credit_popup = true,
_ => {}
}
}
}
5 changes: 3 additions & 2 deletions src/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ impl Board {
let piece_color = get_piece_color(self.board, self.cursor_coordinates);
let piece_type = get_piece_type(self.board, self.cursor_coordinates);

let authorized_positions=
let authorized_positions =
self.get_authorized_positions(piece_type, piece_color, self.cursor_coordinates);

if authorized_positions.is_empty() {
Expand Down Expand Up @@ -570,9 +570,10 @@ impl Board {
self.selected_coordinates,
);

// Draw grey if the color is in the authorized positions
for coords in positions.clone() {
if i == coords[0] && j == coords[1] {
cell_color = Color::LightRed
cell_color = Color::Rgb(100, 100, 100)
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ use ratatui::style::Color;
pub const UNDEFINED_POSITION: i8 = -1;
pub const WHITE: Color = Color::Rgb(160, 160, 160);
pub const BLACK: Color = Color::Rgb(128, 95, 69);

pub const TITLE: &str = r#"
██████╗██╗ ██╗███████╗███████╗███████╗ ████████╗██╗ ██╗██╗
██╔════╝██║ ██║██╔════╝██╔════╝██╔════╝ ╚══██╔══╝██║ ██║██║
██║ ███████║█████╗ ███████╗███████╗█████╗██║ ██║ ██║██║
██║ ██╔══██║██╔══╝ ╚════██║╚════██║╚════╝██║ ██║ ██║██║
╚██████╗██║ ██║███████╗███████║███████║ ██║ ╚██████╔╝██║
╚═════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
"#;
34 changes: 30 additions & 4 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,36 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
// Counter handlers
KeyCode::Right => app.board.cursor_right(),
KeyCode::Left => app.board.cursor_left(),
KeyCode::Up => app.board.cursor_up(),
KeyCode::Down => app.board.cursor_down(),
KeyCode::Char(' ') => app.board.select_cell(),
KeyCode::Char('h') => app.show_popup(),
KeyCode::Up => {
if app.show_home_menu {
app.menu_cursor_up()
} else {
app.board.cursor_up()
}
}
KeyCode::Down => {
if app.show_home_menu {
app.menu_cursor_down()
} else {
app.board.cursor_down()
}
}
KeyCode::Char(' ') | KeyCode::Enter => {
if !app.show_home_menu {
app.board.select_cell()
} else {
app.menu_select()
}
}
KeyCode::Char('h') => {
if !app.show_home_menu {
app.show_help_popup = true
}
}
KeyCode::Char('x') => {
app.show_credit_popup = false;
app.show_help_popup = false;
}
KeyCode::Char('r') => app.restart(),
KeyCode::Esc => app.board.unselect_cell(),
// Other handlers you could add here.
Expand Down
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ pub mod handler;
// Chess pieces structures
pub mod pieces;

// Board structure
pub mod board;

// Constats for the app
pub mod constants;

// Utils methods for the board
pub mod utils;

// popups render methods
pub mod popups;
Loading

0 comments on commit b61e559

Please sign in to comment.