Skip to content

Commit b61e559

Browse files
feat: home menu + credit + small fixes (#47)
* feat: home menu + credit + small fixes Signed-off-by: Mauran <thomas.mauran@etu.umontpellier.fr> --------- Signed-off-by: Mauran <thomas.mauran@etu.umontpellier.fr>
1 parent 747a556 commit b61e559

13 files changed

Lines changed: 409 additions & 214 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "chess-tui"
3-
version = "1.0.1"
3+
version = "1.1.0"
44
authors = ["Thomas Mauran"]
55
license = "MIT"
66
edition = "2021"

examples/demo.gif

-16.4 KB
Loading

examples/demo.tape

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ Set WindowBarSize 40
1818
Type "cargo run" Sleep 500ms Enter
1919

2020
Sleep 1s
21+
Down @0.3s
22+
Down @0.3s
23+
Down @0.3s
24+
Space @0.3s
25+
Sleep 3s
2126

2227
Down @0.3s
2328
Down @0.3s

examples/helper.gif

11.4 KB
Loading

examples/helper.tape

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Set WindowBarSize 40
1717

1818
Type "cargo run" Sleep 500ms Enter
1919

20+
Sleep 2s
21+
Space @0.3s
2022
Sleep 1s
2123

2224
Type h

src/app.rs

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@ pub struct App {
1111
pub running: bool,
1212
/// board
1313
pub board: Board,
14-
1514
/// show help popup
16-
pub show_popup: bool,
15+
pub show_help_popup: bool,
16+
/// show credit popup
17+
pub show_credit_popup: bool,
18+
/// show home menu
19+
pub show_home_menu: bool,
20+
/// menu current cursor
21+
pub menu_cursor: u8,
1722
}
1823

1924
impl Default for App {
2025
fn default() -> Self {
2126
Self {
2227
running: true,
2328
board: Board::default(),
24-
show_popup: false,
29+
show_help_popup: false,
30+
show_credit_popup: false,
31+
show_home_menu: true,
32+
menu_cursor: 0,
2533
}
2634
}
2735
}
@@ -32,8 +40,11 @@ impl App {
3240
Self::default()
3341
}
3442

35-
pub fn show_popup(&mut self) {
36-
self.show_popup = !self.show_popup;
43+
pub fn toggle_help_popup(&mut self) {
44+
self.show_help_popup = !self.show_help_popup;
45+
}
46+
pub fn toggle_credit_popup(&mut self) {
47+
self.show_credit_popup = !self.show_credit_popup;
3748
}
3849

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

58+
pub fn menu_cursor_up(&mut self) {
59+
if self.menu_cursor > 0 {
60+
self.menu_cursor -= 1
61+
} else {
62+
self.menu_cursor = 2
63+
}
64+
}
65+
66+
pub fn menu_cursor_down(&mut self) {
67+
if self.menu_cursor < 2 {
68+
self.menu_cursor += 1
69+
} else {
70+
self.menu_cursor = 0
71+
}
72+
}
73+
4774
pub fn restart(&mut self) {
4875
if self.board.is_draw || self.board.is_checkmate {
4976
self.board = Board::default()
5077
}
5178
}
79+
80+
pub fn menu_select(&mut self) {
81+
match self.menu_cursor {
82+
0 => self.show_home_menu = false,
83+
1 => self.show_help_popup = true,
84+
2 => self.show_credit_popup = true,
85+
_ => {}
86+
}
87+
}
5288
}

src/board.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ impl Board {
260260
let piece_color = get_piece_color(self.board, self.cursor_coordinates);
261261
let piece_type = get_piece_type(self.board, self.cursor_coordinates);
262262

263-
let authorized_positions=
263+
let authorized_positions =
264264
self.get_authorized_positions(piece_type, piece_color, self.cursor_coordinates);
265265

266266
if authorized_positions.is_empty() {
@@ -570,9 +570,10 @@ impl Board {
570570
self.selected_coordinates,
571571
);
572572

573+
// Draw grey if the color is in the authorized positions
573574
for coords in positions.clone() {
574575
if i == coords[0] && j == coords[1] {
575-
cell_color = Color::LightRed
576+
cell_color = Color::Rgb(100, 100, 100)
576577
}
577578
}
578579
}

src/constants.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,12 @@ use ratatui::style::Color;
33
pub const UNDEFINED_POSITION: i8 = -1;
44
pub const WHITE: Color = Color::Rgb(160, 160, 160);
55
pub const BLACK: Color = Color::Rgb(128, 95, 69);
6+
7+
pub const TITLE: &str = r#"
8+
██████╗██╗ ██╗███████╗███████╗███████╗ ████████╗██╗ ██╗██╗
9+
██╔════╝██║ ██║██╔════╝██╔════╝██╔════╝ ╚══██╔══╝██║ ██║██║
10+
██║ ███████║█████╗ ███████╗███████╗█████╗██║ ██║ ██║██║
11+
██║ ██╔══██║██╔══╝ ╚════██║╚════██║╚════╝██║ ██║ ██║██║
12+
╚██████╗██║ ██║███████╗███████║███████║ ██║ ╚██████╔╝██║
13+
╚═════╝╚═╝ ╚═╝╚══════╝╚══════╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝
14+
"#;

src/handler.rs

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,36 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
1717
// Counter handlers
1818
KeyCode::Right => app.board.cursor_right(),
1919
KeyCode::Left => app.board.cursor_left(),
20-
KeyCode::Up => app.board.cursor_up(),
21-
KeyCode::Down => app.board.cursor_down(),
22-
KeyCode::Char(' ') => app.board.select_cell(),
23-
KeyCode::Char('h') => app.show_popup(),
20+
KeyCode::Up => {
21+
if app.show_home_menu {
22+
app.menu_cursor_up()
23+
} else {
24+
app.board.cursor_up()
25+
}
26+
}
27+
KeyCode::Down => {
28+
if app.show_home_menu {
29+
app.menu_cursor_down()
30+
} else {
31+
app.board.cursor_down()
32+
}
33+
}
34+
KeyCode::Char(' ') | KeyCode::Enter => {
35+
if !app.show_home_menu {
36+
app.board.select_cell()
37+
} else {
38+
app.menu_select()
39+
}
40+
}
41+
KeyCode::Char('h') => {
42+
if !app.show_home_menu {
43+
app.show_help_popup = true
44+
}
45+
}
46+
KeyCode::Char('x') => {
47+
app.show_credit_popup = false;
48+
app.show_help_popup = false;
49+
}
2450
KeyCode::Char('r') => app.restart(),
2551
KeyCode::Esc => app.board.unselect_cell(),
2652
// Other handlers you could add here.

0 commit comments

Comments
 (0)