generated from GarandPLG/rust-flake-template
Extract keybinding handling to separate module
This commit is contained in:
+2
-19
@@ -1,8 +1,5 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
app::{
|
app::{GameStates, handle_keybindings},
|
||||||
GameStates,
|
|
||||||
keybindings::{Action, event_to_action, main_menu_keybindings},
|
|
||||||
},
|
|
||||||
cli::Cli,
|
cli::Cli,
|
||||||
};
|
};
|
||||||
use clap::ValueEnum;
|
use clap::ValueEnum;
|
||||||
@@ -103,21 +100,7 @@ impl App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn handle_key_event(&mut self, key_event: KeyEvent) -> Result<()> {
|
fn handle_key_event(&mut self, key_event: KeyEvent) -> Result<()> {
|
||||||
match self.view {
|
handle_keybindings(self, key_event)
|
||||||
View::MainMenu => main_menu_keybindings(self, &key_event),
|
|
||||||
_ => {
|
|
||||||
if let Some(action) = event_to_action(&key_event) {
|
|
||||||
match action {
|
|
||||||
Action::Quit => self.exit = true,
|
|
||||||
Action::Quit2 => self.exit = true,
|
|
||||||
Action::Esc => self.view = View::MainMenu,
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,24 @@
|
|||||||
|
use crate::app::{
|
||||||
|
App, View,
|
||||||
|
keybindings::{Action, event_to_action, main_menu_keybindings},
|
||||||
|
};
|
||||||
|
use ratatui::crossterm::event::KeyEvent;
|
||||||
|
use std::io::Result;
|
||||||
|
|
||||||
|
pub fn handle_keybindings(app: &mut App, key_event: KeyEvent) -> Result<()> {
|
||||||
|
match app.view {
|
||||||
|
View::MainMenu => main_menu_keybindings(app, &key_event),
|
||||||
|
_ => {
|
||||||
|
if let Some(action) = event_to_action(&key_event) {
|
||||||
|
match action {
|
||||||
|
Action::Quit => app.exit = true,
|
||||||
|
Action::Quit2 => app.exit = true,
|
||||||
|
Action::Esc => app.view = View::MainMenu,
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod game_states;
|
pub mod game_states;
|
||||||
|
pub mod keybind;
|
||||||
pub mod keybindings;
|
pub mod keybindings;
|
||||||
pub mod widget;
|
pub mod widget;
|
||||||
pub mod widgets;
|
pub mod widgets;
|
||||||
@@ -8,3 +9,4 @@ pub use app::{App, Event, GameMode, PerkDecks, View, handle_input_events};
|
|||||||
pub use game_states::{
|
pub use game_states::{
|
||||||
GameStates, MainMenuState, PerkDecksState, SettingsState, SkillsConfigState, SkirmishState,
|
GameStates, MainMenuState, PerkDecksState, SettingsState, SkillsConfigState, SkirmishState,
|
||||||
};
|
};
|
||||||
|
pub use keybind::handle_keybindings;
|
||||||
|
|||||||
Reference in New Issue
Block a user