Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions src/game_logic/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,26 @@ impl UI {
}

// CURSOR MOVEMENT

pub fn cursor_custom_column(&mut self, to_move_col: u8) {
if !self.is_cell_selected() {
self.cursor_coordinates.col = to_move_col;
}
}

pub fn cursor_custom_row(&mut self, to_move_row: u8) {
if !self.is_cell_selected() {
self.cursor_coordinates.row = to_move_row;
}
}

/// Move the cursor up
pub fn cursor_up(&mut self, authorized_positions: Vec<Coord>) {
if self.is_cell_selected() {
self.move_selected_piece_cursor(false, -1, authorized_positions);
} else if self.cursor_coordinates.row > 0 {
} else if self.cursor_coordinates.row == 0 {
self.cursor_coordinates.row = 7;
} else {
self.cursor_coordinates.row -= 1;
}
}
Expand All @@ -124,16 +139,17 @@ impl UI {
pub fn cursor_down(&mut self, authorized_positions: Vec<Coord>) {
if self.is_cell_selected() {
self.move_selected_piece_cursor(false, 1, authorized_positions);
} else if self.cursor_coordinates.row < 7 {
self.cursor_coordinates.row += 1;
}
self.cursor_coordinates.row = (self.cursor_coordinates.row + 1) % 8;
}

/// Move the cursor to the left
pub fn cursor_left(&mut self, authorized_positions: Vec<Coord>) {
if self.is_cell_selected() {
self.move_selected_piece_cursor(false, -1, authorized_positions);
} else if self.cursor_coordinates.col > 0 {
} else if self.cursor_coordinates.col == 0 {
self.cursor_coordinates.col = 7;
} else {
self.cursor_coordinates.col -= 1;
}
}
Expand All @@ -151,9 +167,8 @@ impl UI {
pub fn cursor_right(&mut self, authorized_positions: Vec<Coord>) {
if self.is_cell_selected() {
self.move_selected_piece_cursor(false, 1, authorized_positions);
} else if self.cursor_coordinates.col < 7 {
self.cursor_coordinates.col += 1;
}
self.cursor_coordinates.col = (self.cursor_coordinates.col + 1) % 8;
}

/// Move the cursor to the right when we are doing a promotion
Expand Down
32 changes: 26 additions & 6 deletions src/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,34 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.quit();
}
// Exit application on `Ctrl-C`
KeyCode::Char('c' | 'C') => {
KeyCode::Char('C') => {
if key_event.modifiers == KeyModifiers::CONTROL {
app.quit();
}
}
// Counter handlers
// Counter handlers
KeyCode::Right | KeyCode::Char('l') => {
KeyCode::Char(c) if ('a'..='h').contains(&c) => {
let to_move_col: u8 = c as u8 - 97;

if !(app.game.game_state == GameState::Checkmate)
&& !(app.game.game_state == GameState::Draw)
{
app.game.ui.cursor_custom_column(to_move_col);
}
}

KeyCode::Char(r) if ('1'..='8').contains(&r) => {
let to_move_row: u8 = r as u8 - 48 - 1;

if !(app.game.game_state == GameState::Checkmate)
&& !(app.game.game_state == GameState::Draw)
{
app.game.ui.cursor_custom_row(to_move_row);
}
}

KeyCode::Right | KeyCode::Char('r') => {
if (app.current_page == Pages::Multiplayer
&& (app.hosting.is_none() || app.selected_color.is_none()))
|| (app.current_page == Pages::Bot && app.selected_color.is_none())
Expand All @@ -85,7 +105,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
}
}

KeyCode::Left | KeyCode::Char('h') => {
KeyCode::Left | KeyCode::Char('e') => {
if (app.current_page == Pages::Multiplayer
&& (app.hosting.is_none() || app.selected_color.is_none()))
|| (app.current_page == Pages::Bot && app.selected_color.is_none())
Expand All @@ -104,7 +124,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.game.ui.cursor_left(authorized_positions);
}
}
KeyCode::Up | KeyCode::Char('k') => {
KeyCode::Up | KeyCode::Char('w') => {
if app.current_page == Pages::Home {
app.menu_cursor_up(Pages::variant_count() as u8);
} else if !(app.game.game_state == GameState::Checkmate)
Expand All @@ -118,7 +138,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.game.ui.cursor_up(authorized_positions);
}
}
KeyCode::Down | KeyCode::Char('j') => {
KeyCode::Down | KeyCode::Char('s') => {
if app.current_page == Pages::Home {
app.menu_cursor_down(Pages::variant_count() as u8);
} else if !(app.game.game_state == GameState::Checkmate)
Expand Down Expand Up @@ -168,7 +188,7 @@ pub fn handle_key_events(key_event: KeyEvent, app: &mut App) -> AppResult<()> {
app.toggle_help_popup();
}
}
KeyCode::Char('r') => {
KeyCode::Char('h') => {
// We can't restart the game if it's a multiplayer one
if app.game.opponent.is_none() {
app.restart();
Expand Down
Loading