diff --git a/Cargo.lock b/Cargo.lock index fb55335..2b7418f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1521,7 +1521,6 @@ name = "war_in_tunnels" version = "0.1.0" dependencies = [ "clap", - "crossterm", "ratatui", ] diff --git a/Cargo.toml b/Cargo.toml index 08935ce..5230481 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,5 +5,4 @@ edition = "2024" [dependencies] clap = { version = "4.5.53", features = ["derive"] } -crossterm = "0.29.0" ratatui = "0.30.0" diff --git a/src/app.rs b/src/app/app.rs similarity index 70% rename from src/app.rs rename to src/app/app.rs index 3d1401b..9c99e1a 100644 --- a/src/app.rs +++ b/src/app/app.rs @@ -1,5 +1,7 @@ -use crossterm::event::{self, KeyCode, KeyEvent, KeyEventKind}; -use ratatui::{DefaultTerminal, Frame, buffer::Buffer, layout::Rect, widgets::Widget}; +use ratatui::{ + DefaultTerminal, Frame, + crossterm::event::{self, KeyCode, KeyEvent, KeyEventKind}, +}; use std::{ io::Result, sync::mpsc::{self, Receiver}, @@ -7,16 +9,17 @@ use std::{ pub struct App { pub exit: bool, - // pub username: String, - // pub game_mode: String, - // pub map_width: u8, - // pub map_height: u8, - // pub perk_deck: String, - // pub starting_wood: u8, - // pub starting_iron: u8, - // pub supply_limit: u8, - // pub xp_modifier: f32, - // pub skill_points_limit: u16, + pub default_window: String, + pub username: String, + pub game_mode: String, + pub map_width: u8, + pub map_height: u8, + pub perk_deck: String, + pub starting_wood: u8, + pub starting_iron: u8, + pub supply_limit: u8, + pub xp_modifier: f32, + pub skill_points_limit: u16, } pub enum Event { @@ -53,14 +56,6 @@ impl App { } } -impl Widget for &App { - fn render(self, area: Rect, buf: &mut Buffer) - where - Self: Sized, - { - } -} - pub fn handle_input_events(tx: mpsc::Sender) { loop { match event::read() { diff --git a/src/app/mod.rs b/src/app/mod.rs new file mode 100644 index 0000000..39c8669 --- /dev/null +++ b/src/app/mod.rs @@ -0,0 +1,5 @@ +pub mod app; +pub mod widget; +pub mod widgets; + +pub use app::{App, Event, handle_input_events}; diff --git a/src/app/widget.rs b/src/app/widget.rs new file mode 100644 index 0000000..beb7e06 --- /dev/null +++ b/src/app/widget.rs @@ -0,0 +1,14 @@ +use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget}; + +use crate::app::{App, widgets::main_menu_widget}; + +impl Widget for &App { + fn render(self, area: Rect, buf: &mut Buffer) + where + Self: Sized, + { + if self.default_window == "main_menu" { + main_menu_widget(area, buf); + } + } +} diff --git a/src/app/widgets/main_menu.rs b/src/app/widgets/main_menu.rs new file mode 100644 index 0000000..71dd8c9 --- /dev/null +++ b/src/app/widgets/main_menu.rs @@ -0,0 +1,47 @@ +use ratatui::{ + buffer::Buffer, + layout::{Alignment, Constraint, Layout, Rect}, + style::{Color, Stylize}, + text::Line, + widgets::{Block, Borders, Paragraph, Widget}, +}; + +pub fn main_menu_widget(area: Rect, buf: &mut Buffer) { + let vertical_layout: Layout = + Layout::vertical([Constraint::Percentage(90), Constraint::Percentage(10)]); + + let [main_menu_area, keybindings_area] = vertical_layout.areas(area); + + let title_text: String = vec![ + r" __ __ _ _____ _ ", + r"/ / /\ \ \__ _ _ __ (_)_ __ /__ \_ _ _ __ _ __ ___| |___", + r"\ \/ \/ / _` | '__| | | '_ \ / /\/ | | | '_ \| '_ \ / _ \ / __|", + r" \ /\ / (_| | | | | | | | / / | |_| | | | | | | | __/ \__ \", + r" \/ \/ \__,_|_| |_|_| |_| \/ \__,_|_| |_|_| |_|\___|_|___/", + ] + .join("\n"); + + let title: Paragraph<'_> = Paragraph::new(title_text) + .alignment(Alignment::Center) + .style(Color::Yellow) + .block( + Block::default() + .borders(Borders::LEFT | Borders::RIGHT | Borders::TOP) + .style(Color::Gray), + ); + + title.render(main_menu_area, buf); + + let keybindings_text: Vec> = + vec![Line::from_iter(["q".bold().red(), "\t - Quit".into()])]; + + let keybindings: Paragraph<'_> = Paragraph::new(keybindings_text) + .alignment(Alignment::Left) + .block( + Block::default() + .borders(Borders::ALL) + .title("[ Keybinding ]"), + ); + + keybindings.render(keybindings_area, buf); +} diff --git a/src/app/widgets/mod.rs b/src/app/widgets/mod.rs new file mode 100644 index 0000000..13ba747 --- /dev/null +++ b/src/app/widgets/mod.rs @@ -0,0 +1,3 @@ +pub mod main_menu; + +pub use main_menu::main_menu_widget; diff --git a/src/cli.rs b/src/cli.rs index 267cef6..c9d18f5 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -3,37 +3,91 @@ use clap::Parser; #[derive(Parser, Debug)] #[command(version, about = "War in Tunnels", long_about = "War in Tunnels")] pub struct Cli { - #[arg(long, help = "Username", value_name = "String")] + #[arg( + long, + help = "Default window", + value_name = "main_menu | skirmish | skills_config | perk_decks | settings", + default_value = "main_menu" + )] + pub default_window: String, + + #[arg( + long, + help = "Username", + value_name = "String", + default_value = "Player" + )] pub username: String, - #[arg(long, help = "Game mode", value_name = "LastManStanding or FrontLines")] + #[arg( + long, + help = "Game mode", + value_name = "LastManStanding or FrontLines", + default_value = "LastManStanding" + )] pub game_mode: String, - #[arg(long, help = "Map width", value_name = "Positive integer [20; 100]")] + #[arg( + long, + help = "Map width", + value_name = "Positive integer [20; 100]", + default_value = "50" + )] pub map_width: u8, - #[arg(long, help = "Map height", value_name = "Positive integer [11; 50]")] + #[arg( + long, + help = "Map height", + value_name = "Positive integer [11; 50]", + default_value = "25" + )] pub map_height: u8, - #[arg(long, help = "Perk Deck", value_name = "String (check Perk Deck tab)")] + #[arg( + long, + help = "Perk Deck", + value_name = "String (check Perk Deck tab)", + default_value = "Silesian" + )] pub perk_deck: String, - #[arg(long, help = "Starting wood", value_name = "Positive integer")] + #[arg( + long, + help = "Starting wood", + value_name = "Positive integer", + default_value = "50" + )] pub starting_wood: u8, - #[arg(long, help = "Starting iron", value_name = "Positive integer")] + #[arg( + long, + help = "Starting iron", + value_name = "Positive integer", + default_value = "25" + )] pub starting_iron: u8, - #[arg(long, help = "Supply limit", value_name = "Positive integer")] + #[arg( + long, + help = "Supply limit", + value_name = "Positive integer", + default_value = "99" + )] pub supply_limit: u8, - #[arg(long, help = "XP modifier", value_name = "Float [0.5; 2.0]")] + #[arg( + long, + help = "XP modifier", + value_name = "Float [0.5; 2.0]", + default_value = "1.0" + )] pub xp_modifier: f32, #[arg( long, help = "Skill points limit", - value_name = "Positive integer [120; 690]" + value_name = "Positive integer [120; 690]", + default_value = "120" )] pub skill_points_limit: u16, } diff --git a/src/main.rs b/src/main.rs index 81f2188..4c5c210 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,20 @@ use war_in_tunnels::{ fn main() -> Result<()> { let args: Cli = get_args(); let mut terminal: Terminal> = ratatui::init(); - let mut app: App = App { exit: false }; + let mut app: App = App { + exit: false, + default_window: args.default_window, + 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, + }; let (event_tx, event_rx) = mpsc::channel::(); @@ -23,5 +36,6 @@ fn main() -> Result<()> { let app_result: Result<()> = app.run(&mut terminal, event_rx); ratatui::restore(); + app_result }