Rename GameStates to States and adjust keybindings

Replace the old `GameStates` struct with a new `states` module, rename
the
field in `App` to `states`, and update construction to pass CLI args.
Remove the now‑unused `game_states.rs` file and adjust imports
accordingly.
Update keybinding definitions: change the wildcard symbol to "[All]" and
simplify the wildcard detection logic by dropping the modifiers check.
This commit is contained in:
2026-03-16 10:27:44 +01:00
parent 2dbe378470
commit fd0f6defea
8 changed files with 71 additions and 88 deletions
+2 -22
View File
@@ -21,17 +21,7 @@ pub enum Event {
pub struct App { pub struct App {
pub exit: bool, pub exit: bool,
pub view: View, pub view: View,
pub game_states: GameStates, pub states: GameStates,
pub username: String,
pub game_mode: GameMode,
pub map_width: u8,
pub map_height: u8,
pub perk_deck: PerkDecks,
pub starting_wood: u8,
pub starting_iron: u8,
pub supply_limit: u8,
pub xp_modifier: f32,
pub skill_points_limit: u16,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
@@ -51,18 +41,8 @@ impl App {
pub fn new(args: Cli) -> Self { pub fn new(args: Cli) -> Self {
Self { Self {
exit: false, exit: false,
game_states: GameStates::new(),
view: args.view, view: args.view,
username: args.username, states: GameStates::new(args),
game_mode: args.game_mode,
map_width: args.map_width,
map_height: args.map_height,
perk_deck: args.perk_deck,
starting_wood: args.starting_wood,
starting_iron: args.starting_iron,
supply_limit: args.supply_limit,
xp_modifier: args.xp_modifier,
skill_points_limit: args.skill_points_limit,
} }
} }
+5 -6
View File
@@ -112,7 +112,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
kind: KeyEventKind::Press, kind: KeyEventKind::Press,
modifiers: KeyModifiers::NONE, modifiers: KeyModifiers::NONE,
group: Group::Input, group: Group::Input,
symbol: "*", symbol: "[All]",
description: "All keyboard characters", description: "All keyboard characters",
}, },
]; ];
@@ -129,11 +129,10 @@ pub fn event_to_action(event: &KeyEvent) -> Option<Action> {
return Some(b.action); return Some(b.action);
} }
if KEYBINDINGS.iter().any(|b| { if KEYBINDINGS
matches!(b.action, Action::WildCard(_)) .iter()
&& b.kind == event.kind .any(|b| matches!(b.action, Action::WildCard(_)) && b.kind == event.kind)
&& b.modifiers == event.modifiers {
}) {
if let KeyCode::Char(c) = event.code { if let KeyCode::Char(c) = event.code {
return Some(Action::WildCard(c)); return Some(Action::WildCard(c));
} }
+5 -13
View File
@@ -10,23 +10,15 @@ pub fn main_menu_keybindings(app: &mut App, event: &KeyEvent) {
Action::Quit => app.exit = true, Action::Quit => app.exit = true,
Action::Quit2 => app.exit = true, Action::Quit2 => app.exit = true,
Action::Up => { Action::Up => {
app.game_states.main_menu_state.selected_view = app app.states.main_menu.selected_view =
.game_states app.states.main_menu.selected_view.saturating_sub(1).max(1);
.main_menu_state
.selected_view
.saturating_sub(1)
.max(1);
} }
Action::Down => { Action::Down => {
app.game_states.main_menu_state.selected_view = app app.states.main_menu.selected_view =
.game_states app.states.main_menu.selected_view.saturating_add(1).min(4);
.main_menu_state
.selected_view
.saturating_add(1)
.min(4);
} }
Action::Space => { Action::Space => {
let selected_view: usize = app.game_states.main_menu_state.selected_view; let selected_view: usize = app.states.main_menu.selected_view;
if selected_view == 1 { if selected_view == 1 {
app.view = View::Skirmish; app.view = View::Skirmish;
+5 -14
View File
@@ -12,24 +12,15 @@ pub fn settings_keybindings(app: &mut App, key_event: &KeyEvent) {
Action::Quit => app.exit = true, Action::Quit => app.exit = true,
Action::Quit2 => app.exit = true, Action::Quit2 => app.exit = true,
Action::Esc => app.view = View::MainMenu, Action::Esc => app.view = View::MainMenu,
Action::Space => { Action::Space => app.states.settings.show_popup = !app.states.settings.show_popup,
app.game_states.settings_state.show_popup =
!app.game_states.settings_state.show_popup
}
Action::Backspace => { Action::Backspace => {
if app.game_states.settings_state.show_popup { if app.states.settings.show_popup {
app.game_states app.states.settings.selected_setting_new_value.pop();
.settings_state
.selected_setting_new_value
.pop();
} }
} }
Action::WildCard(c) => { Action::WildCard(c) => {
if app.game_states.settings_state.show_popup { if app.states.settings.show_popup {
app.game_states app.states.settings.selected_setting_new_value.push(c);
.settings_state
.selected_setting_new_value
.push(c);
} }
} }
_ => (), _ => (),
+3 -3
View File
@@ -1,14 +1,14 @@
pub mod app; pub mod app;
pub mod game_states;
pub mod keybind; pub mod keybind;
pub mod keybindings; pub mod keybindings;
pub mod states;
pub mod view; pub mod view;
pub mod views; pub mod views;
pub mod widgets; pub mod widgets;
pub use app::{App, Event, GameMode, PerkDecks, handle_input_events}; pub use app::{App, Event, GameMode, PerkDecks, handle_input_events};
pub use game_states::{ pub use keybind::handle_keybindings;
pub use states::{
GameStates, MainMenuState, PerkDecksState, SettingsState, SkillsConfigState, SkirmishState, GameStates, MainMenuState, PerkDecksState, SettingsState, SkillsConfigState, SkirmishState,
}; };
pub use keybind::handle_keybindings;
pub use view::View; pub use view::View;
+38 -13
View File
@@ -1,10 +1,15 @@
#[derive(Debug, Clone, PartialEq, Eq, Hash)] use crate::{
app::{GameMode, PerkDecks},
cli::Cli,
};
#[derive(Debug, Clone, PartialEq)]
pub struct GameStates { pub struct GameStates {
pub main_menu_state: MainMenuState, pub main_menu: MainMenuState,
pub skirmish_state: SkirmishState, pub skirmish: SkirmishState,
pub perk_decks_state: PerkDecksState, pub perk_decks: PerkDecksState,
pub skills_config_state: SkillsConfigState, pub skills_config: SkillsConfigState,
pub settings_state: SettingsState, pub settings: SettingsState,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@@ -34,43 +39,63 @@ pub struct SkillsConfigState {
pub selected_skill: usize, pub selected_skill: usize,
} }
#[derive(Debug, Clone, PartialEq, Eq, Hash)] #[derive(Debug, Clone, PartialEq)]
pub struct SettingsState { pub struct SettingsState {
pub id: usize, pub id: usize,
pub name: &'static str, pub name: &'static str,
pub username: String,
pub game_mode: GameMode,
pub map_width: u8,
pub map_height: u8,
pub perk_deck: PerkDecks,
pub starting_wood: u8,
pub starting_iron: u8,
pub supply_limit: u8,
pub xp_modifier: f32,
pub skill_points_limit: u16,
pub selected_setting: usize, pub selected_setting: usize,
pub show_popup: bool, pub show_popup: bool,
pub selected_setting_new_value: String, pub selected_setting_new_value: String,
} }
impl GameStates { impl GameStates {
pub fn new() -> Self { pub fn new(args: Cli) -> Self {
Self { Self {
main_menu_state: MainMenuState { main_menu: MainMenuState {
id: 0, id: 0,
name: "Main Menu", name: "Main Menu",
selected_view: 1, selected_view: 1,
}, },
skirmish_state: SkirmishState { skirmish: SkirmishState {
id: 1, id: 1,
name: "Skirmish", name: "Skirmish",
}, },
perk_decks_state: PerkDecksState { perk_decks: PerkDecksState {
id: 2, id: 2,
name: "Perk Decks", name: "Perk Decks",
selected_perk_deck: 0, selected_perk_deck: 0,
}, },
skills_config_state: SkillsConfigState { skills_config: SkillsConfigState {
id: 3, id: 3,
name: "Skills Config", name: "Skills Config",
selected_skill: 0, selected_skill: 0,
}, },
settings_state: SettingsState { settings: SettingsState {
id: 4, id: 4,
name: "Settings", name: "Settings",
selected_setting: 0, selected_setting: 0,
show_popup: false, show_popup: false,
selected_setting_new_value: String::new(), selected_setting_new_value: String::new(),
username: args.username,
game_mode: args.game_mode,
map_width: args.map_width,
map_height: args.map_height,
perk_deck: args.perk_deck,
starting_wood: args.starting_wood,
starting_iron: args.starting_iron,
supply_limit: args.supply_limit,
xp_modifier: args.xp_modifier,
skill_points_limit: args.skill_points_limit,
}, },
} }
} }
+1 -1
View File
@@ -65,7 +65,7 @@ pub fn main_menu_view(app: &App, area: Rect, buf: &mut Buffer) {
.map(|(i, view)| { .map(|(i, view)| {
let view_string: String = format_view_string(format!("{:?}", view)); let view_string: String = format_view_string(format!("{:?}", view));
let styled: Line<'_> = if app.game_states.main_menu_state.selected_view == i { let styled: Line<'_> = if app.states.main_menu.selected_view == i {
Line::from(format!("> {view_string}")).yellow() Line::from(format!("> {view_string}")).yellow()
} else { } else {
Line::from(view_string).white() Line::from(view_string).white()
+12 -16
View File
@@ -45,23 +45,19 @@ pub fn settings_view(app: &App, area: Rect, buf: &mut Buffer) {
height: main_area.height / 3, height: main_area.height / 3,
}; };
let popup: Paragraph<'_> = Paragraph::new( let popup: Paragraph<'_> =
app.game_states Paragraph::new(app.states.settings.selected_setting_new_value.clone())
.settings_state .wrap(Wrap { trim: true })
.selected_setting_new_value .style(Style::default().yellow())
.clone(), .block(
) Block::new()
.wrap(Wrap { trim: true }) .title("Insert value")
.style(Style::default().yellow()) .title_style(Style::default().green())
.block( .borders(Borders::ALL)
Block::new() .border_style(Style::default().blue()),
.title("Insert value") );
.title_style(Style::default().green())
.borders(Borders::ALL)
.border_style(Style::default().blue()),
);
if app.game_states.settings_state.show_popup { if app.states.settings.show_popup {
popup.render(popup_area, buf); popup.render(popup_area, buf);
} }
} }