generated from GarandPLG/rust-flake-template
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:
+2
-22
@@ -21,17 +21,7 @@ pub enum Event {
|
||||
pub struct App {
|
||||
pub exit: bool,
|
||||
pub view: View,
|
||||
pub game_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,
|
||||
pub states: GameStates,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
|
||||
@@ -51,18 +41,8 @@ impl App {
|
||||
pub fn new(args: Cli) -> Self {
|
||||
Self {
|
||||
exit: false,
|
||||
game_states: GameStates::new(),
|
||||
view: args.view,
|
||||
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,
|
||||
states: GameStates::new(args),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +112,7 @@ pub static KEYBINDINGS: &[KeyBinding] = &[
|
||||
kind: KeyEventKind::Press,
|
||||
modifiers: KeyModifiers::NONE,
|
||||
group: Group::Input,
|
||||
symbol: "*",
|
||||
symbol: "[All]",
|
||||
description: "All keyboard characters",
|
||||
},
|
||||
];
|
||||
@@ -129,11 +129,10 @@ pub fn event_to_action(event: &KeyEvent) -> Option<Action> {
|
||||
return Some(b.action);
|
||||
}
|
||||
|
||||
if KEYBINDINGS.iter().any(|b| {
|
||||
matches!(b.action, Action::WildCard(_))
|
||||
&& b.kind == event.kind
|
||||
&& b.modifiers == event.modifiers
|
||||
}) {
|
||||
if KEYBINDINGS
|
||||
.iter()
|
||||
.any(|b| matches!(b.action, Action::WildCard(_)) && b.kind == event.kind)
|
||||
{
|
||||
if let KeyCode::Char(c) = event.code {
|
||||
return Some(Action::WildCard(c));
|
||||
}
|
||||
|
||||
@@ -10,23 +10,15 @@ pub fn main_menu_keybindings(app: &mut App, event: &KeyEvent) {
|
||||
Action::Quit => app.exit = true,
|
||||
Action::Quit2 => app.exit = true,
|
||||
Action::Up => {
|
||||
app.game_states.main_menu_state.selected_view = app
|
||||
.game_states
|
||||
.main_menu_state
|
||||
.selected_view
|
||||
.saturating_sub(1)
|
||||
.max(1);
|
||||
app.states.main_menu.selected_view =
|
||||
app.states.main_menu.selected_view.saturating_sub(1).max(1);
|
||||
}
|
||||
Action::Down => {
|
||||
app.game_states.main_menu_state.selected_view = app
|
||||
.game_states
|
||||
.main_menu_state
|
||||
.selected_view
|
||||
.saturating_add(1)
|
||||
.min(4);
|
||||
app.states.main_menu.selected_view =
|
||||
app.states.main_menu.selected_view.saturating_add(1).min(4);
|
||||
}
|
||||
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 {
|
||||
app.view = View::Skirmish;
|
||||
|
||||
@@ -12,24 +12,15 @@ pub fn settings_keybindings(app: &mut App, key_event: &KeyEvent) {
|
||||
Action::Quit => app.exit = true,
|
||||
Action::Quit2 => app.exit = true,
|
||||
Action::Esc => app.view = View::MainMenu,
|
||||
Action::Space => {
|
||||
app.game_states.settings_state.show_popup =
|
||||
!app.game_states.settings_state.show_popup
|
||||
}
|
||||
Action::Space => app.states.settings.show_popup = !app.states.settings.show_popup,
|
||||
Action::Backspace => {
|
||||
if app.game_states.settings_state.show_popup {
|
||||
app.game_states
|
||||
.settings_state
|
||||
.selected_setting_new_value
|
||||
.pop();
|
||||
if app.states.settings.show_popup {
|
||||
app.states.settings.selected_setting_new_value.pop();
|
||||
}
|
||||
}
|
||||
Action::WildCard(c) => {
|
||||
if app.game_states.settings_state.show_popup {
|
||||
app.game_states
|
||||
.settings_state
|
||||
.selected_setting_new_value
|
||||
.push(c);
|
||||
if app.states.settings.show_popup {
|
||||
app.states.settings.selected_setting_new_value.push(c);
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
|
||||
+3
-3
@@ -1,14 +1,14 @@
|
||||
pub mod app;
|
||||
pub mod game_states;
|
||||
pub mod keybind;
|
||||
pub mod keybindings;
|
||||
pub mod states;
|
||||
pub mod view;
|
||||
pub mod views;
|
||||
pub mod widgets;
|
||||
|
||||
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,
|
||||
};
|
||||
pub use keybind::handle_keybindings;
|
||||
pub use view::View;
|
||||
|
||||
@@ -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 main_menu_state: MainMenuState,
|
||||
pub skirmish_state: SkirmishState,
|
||||
pub perk_decks_state: PerkDecksState,
|
||||
pub skills_config_state: SkillsConfigState,
|
||||
pub settings_state: SettingsState,
|
||||
pub main_menu: MainMenuState,
|
||||
pub skirmish: SkirmishState,
|
||||
pub perk_decks: PerkDecksState,
|
||||
pub skills_config: SkillsConfigState,
|
||||
pub settings: SettingsState,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
@@ -34,43 +39,63 @@ pub struct SkillsConfigState {
|
||||
pub selected_skill: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct SettingsState {
|
||||
pub id: usize,
|
||||
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 show_popup: bool,
|
||||
pub selected_setting_new_value: String,
|
||||
}
|
||||
|
||||
impl GameStates {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(args: Cli) -> Self {
|
||||
Self {
|
||||
main_menu_state: MainMenuState {
|
||||
main_menu: MainMenuState {
|
||||
id: 0,
|
||||
name: "Main Menu",
|
||||
selected_view: 1,
|
||||
},
|
||||
skirmish_state: SkirmishState {
|
||||
skirmish: SkirmishState {
|
||||
id: 1,
|
||||
name: "Skirmish",
|
||||
},
|
||||
perk_decks_state: PerkDecksState {
|
||||
perk_decks: PerkDecksState {
|
||||
id: 2,
|
||||
name: "Perk Decks",
|
||||
selected_perk_deck: 0,
|
||||
},
|
||||
skills_config_state: SkillsConfigState {
|
||||
skills_config: SkillsConfigState {
|
||||
id: 3,
|
||||
name: "Skills Config",
|
||||
selected_skill: 0,
|
||||
},
|
||||
settings_state: SettingsState {
|
||||
settings: SettingsState {
|
||||
id: 4,
|
||||
name: "Settings",
|
||||
selected_setting: 0,
|
||||
show_popup: false,
|
||||
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,
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,7 @@ pub fn main_menu_view(app: &App, area: Rect, buf: &mut Buffer) {
|
||||
.map(|(i, 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()
|
||||
} else {
|
||||
Line::from(view_string).white()
|
||||
|
||||
+12
-16
@@ -45,23 +45,19 @@ pub fn settings_view(app: &App, area: Rect, buf: &mut Buffer) {
|
||||
height: main_area.height / 3,
|
||||
};
|
||||
|
||||
let popup: Paragraph<'_> = Paragraph::new(
|
||||
app.game_states
|
||||
.settings_state
|
||||
.selected_setting_new_value
|
||||
.clone(),
|
||||
)
|
||||
.wrap(Wrap { trim: true })
|
||||
.style(Style::default().yellow())
|
||||
.block(
|
||||
Block::new()
|
||||
.title("Insert value")
|
||||
.title_style(Style::default().green())
|
||||
.borders(Borders::ALL)
|
||||
.border_style(Style::default().blue()),
|
||||
);
|
||||
let popup: Paragraph<'_> =
|
||||
Paragraph::new(app.states.settings.selected_setting_new_value.clone())
|
||||
.wrap(Wrap { trim: true })
|
||||
.style(Style::default().yellow())
|
||||
.block(
|
||||
Block::new()
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user