generated from GarandPLG/rust-flake-template
Refactor gaamestates
This commit is contained in:
@@ -2,7 +2,6 @@ use crate::{
|
|||||||
app::{GameStates, handle_keybindings, view::View},
|
app::{GameStates, handle_keybindings, view::View},
|
||||||
cli::Cli,
|
cli::Cli,
|
||||||
};
|
};
|
||||||
use clap::ValueEnum;
|
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
DefaultTerminal, Frame,
|
DefaultTerminal, Frame,
|
||||||
crossterm::event::{self, KeyEvent},
|
crossterm::event::{self, KeyEvent},
|
||||||
@@ -24,19 +23,6 @@ pub struct App {
|
|||||||
pub states: GameStates,
|
pub states: GameStates,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
|
|
||||||
pub enum GameMode {
|
|
||||||
LastManStanding,
|
|
||||||
FrontLines,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
|
|
||||||
pub enum PerkDecks {
|
|
||||||
Silesian,
|
|
||||||
BogeyMan,
|
|
||||||
Anteater,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
pub fn new(args: Cli) -> Self {
|
pub fn new(args: Cli) -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ pub fn settings_keybindings(app: &mut App, key_event: &KeyEvent) {
|
|||||||
Action::Space => app.states.settings.show_popup = !app.states.settings.show_popup,
|
Action::Space => app.states.settings.show_popup = !app.states.settings.show_popup,
|
||||||
Action::Enter => {
|
Action::Enter => {
|
||||||
// FIXME: No feedback
|
// FIXME: No feedback
|
||||||
|
// FIXME: Extract match block into a function
|
||||||
if app.states.settings.show_popup {
|
if app.states.settings.show_popup {
|
||||||
let option =
|
let option =
|
||||||
&app.states.settings.options[app.states.settings.selected_setting].value;
|
&app.states.settings.options[app.states.settings.selected_setting].value;
|
||||||
|
|||||||
+3
-4
@@ -1,14 +1,13 @@
|
|||||||
pub mod app;
|
pub mod app;
|
||||||
pub mod keybind;
|
pub mod keybind;
|
||||||
pub mod keybindings;
|
pub mod keybindings;
|
||||||
|
pub mod state;
|
||||||
pub mod states;
|
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, handle_input_events};
|
||||||
pub use keybind::handle_keybindings;
|
pub use keybind::handle_keybindings;
|
||||||
pub use states::{
|
pub use state::GameStates;
|
||||||
GameStates, MainMenuState, PerkDecksState, SettingsState, SkillsConfigState, SkirmishState,
|
|
||||||
};
|
|
||||||
pub use view::View;
|
pub use view::View;
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
use std::fmt::Display;
|
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
app::{GameMode, PerkDecks},
|
app::states::{
|
||||||
|
MainMenuState, PerkDecksState, SettingsOption, SettingsState, SettingsValue,
|
||||||
|
SkillsConfigState, SkirmishState,
|
||||||
|
},
|
||||||
cli::Cli,
|
cli::Cli,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -14,92 +15,6 @@ pub struct GameStates {
|
|||||||
pub settings: SettingsState,
|
pub settings: SettingsState,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub struct MainMenuState {
|
|
||||||
pub id: usize,
|
|
||||||
pub name: &'static str,
|
|
||||||
pub selected_view: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub struct SkirmishState {
|
|
||||||
pub id: usize,
|
|
||||||
pub name: &'static str,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub struct PerkDecksState {
|
|
||||||
pub id: usize,
|
|
||||||
pub name: &'static str,
|
|
||||||
pub selected_perk_deck: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
||||||
pub struct SkillsConfigState {
|
|
||||||
pub id: usize,
|
|
||||||
pub name: &'static str,
|
|
||||||
pub selected_skill: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct SettingsState {
|
|
||||||
pub id: usize,
|
|
||||||
pub name: &'static str,
|
|
||||||
pub selected_setting: usize,
|
|
||||||
pub show_popup: bool,
|
|
||||||
pub selected_setting_new_value: String,
|
|
||||||
pub error_message: String,
|
|
||||||
pub options: Vec<SettingsOption>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub enum SettingsValue {
|
|
||||||
U8(u8),
|
|
||||||
F32(f32),
|
|
||||||
U16(u16),
|
|
||||||
Text(String),
|
|
||||||
GameMode(GameMode),
|
|
||||||
PerkDeck(PerkDecks),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
|
||||||
pub struct SettingsOption {
|
|
||||||
pub name: &'static str,
|
|
||||||
pub value: SettingsValue,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for SettingsValue {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
SettingsValue::U8(v) => write!(f, "{}", v),
|
|
||||||
SettingsValue::F32(v) => write!(f, "{}", v),
|
|
||||||
SettingsValue::U16(v) => write!(f, "{}", v),
|
|
||||||
SettingsValue::Text(v) => write!(f, "{}", v),
|
|
||||||
SettingsValue::GameMode(v) => write!(f, "{}", v),
|
|
||||||
SettingsValue::PerkDeck(v) => write!(f, "{}", v),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for GameMode {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
GameMode::FrontLines => write!(f, "Front Lines"),
|
|
||||||
GameMode::LastManStanding => write!(f, "Last Man Standing"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Display for PerkDecks {
|
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
||||||
match self {
|
|
||||||
PerkDecks::Silesian => write!(f, "Silesian"),
|
|
||||||
PerkDecks::BogeyMan => write!(f, "Bogey Man"),
|
|
||||||
PerkDecks::Anteater => write!(f, "Anteater"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl GameStates {
|
impl GameStates {
|
||||||
pub fn new(args: Cli) -> Self {
|
pub fn new(args: Cli) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct MainMenuState {
|
||||||
|
pub id: usize,
|
||||||
|
pub name: &'static str,
|
||||||
|
pub selected_view: usize,
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
pub mod main_menu;
|
||||||
|
pub mod perk_decks;
|
||||||
|
pub mod settings;
|
||||||
|
pub mod skills_config;
|
||||||
|
pub mod skirmish;
|
||||||
|
|
||||||
|
pub use main_menu::MainMenuState;
|
||||||
|
pub use perk_decks::{PerkDecks, PerkDecksState};
|
||||||
|
pub use settings::{GameMode, SettingsOption, SettingsState, SettingsValue};
|
||||||
|
pub use skills_config::SkillsConfigState;
|
||||||
|
pub use skirmish::SkirmishState;
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
use clap::ValueEnum;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct PerkDecksState {
|
||||||
|
pub id: usize,
|
||||||
|
pub name: &'static str,
|
||||||
|
pub selected_perk_deck: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
|
||||||
|
pub enum PerkDecks {
|
||||||
|
Silesian,
|
||||||
|
BogeyMan,
|
||||||
|
Anteater,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for PerkDecks {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
PerkDecks::Silesian => write!(f, "Silesian"),
|
||||||
|
PerkDecks::BogeyMan => write!(f, "Bogey Man"),
|
||||||
|
PerkDecks::Anteater => write!(f, "Anteater"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
use crate::app::states::PerkDecks;
|
||||||
|
use clap::ValueEnum;
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct SettingsState {
|
||||||
|
pub id: usize,
|
||||||
|
pub name: &'static str,
|
||||||
|
pub selected_setting: usize,
|
||||||
|
pub show_popup: bool,
|
||||||
|
pub selected_setting_new_value: String,
|
||||||
|
pub error_message: String,
|
||||||
|
pub options: Vec<SettingsOption>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub struct SettingsOption {
|
||||||
|
pub name: &'static str,
|
||||||
|
pub value: SettingsValue,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
|
pub enum SettingsValue {
|
||||||
|
U8(u8),
|
||||||
|
F32(f32),
|
||||||
|
U16(u16),
|
||||||
|
Text(String),
|
||||||
|
GameMode(GameMode),
|
||||||
|
PerkDeck(PerkDecks),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for SettingsValue {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
SettingsValue::U8(v) => write!(f, "{}", v),
|
||||||
|
SettingsValue::F32(v) => write!(f, "{}", v),
|
||||||
|
SettingsValue::U16(v) => write!(f, "{}", v),
|
||||||
|
SettingsValue::Text(v) => write!(f, "{}", v),
|
||||||
|
SettingsValue::GameMode(v) => write!(f, "{}", v),
|
||||||
|
SettingsValue::PerkDeck(v) => write!(f, "{}", v),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, ValueEnum)]
|
||||||
|
pub enum GameMode {
|
||||||
|
LastManStanding,
|
||||||
|
FrontLines,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for GameMode {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
GameMode::FrontLines => write!(f, "Front Lines"),
|
||||||
|
GameMode::LastManStanding => write!(f, "Last Man Standing"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct SkillsConfigState {
|
||||||
|
pub id: usize,
|
||||||
|
pub name: &'static str,
|
||||||
|
pub selected_skill: usize,
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
pub struct SkirmishState {
|
||||||
|
pub id: usize,
|
||||||
|
pub name: &'static str,
|
||||||
|
}
|
||||||
+4
-1
@@ -1,4 +1,7 @@
|
|||||||
use crate::app::{GameMode, PerkDecks, view::View};
|
use crate::app::{
|
||||||
|
states::{GameMode, PerkDecks},
|
||||||
|
view::View,
|
||||||
|
};
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
|
|||||||
Reference in New Issue
Block a user