From 4c24f67cd5c482b3712374b6d60fc6e4e7e4049a Mon Sep 17 00:00:00 2001 From: Schrottkatze Date: Wed, 18 Jun 2025 17:13:52 +0200 Subject: [PATCH] make debug console top level --- src/debug.rs | 81 +++++++++++++++++++ src/{game => }/debug/console.rs | 33 ++++---- src/{game => }/debug/console/cli.rs | 6 +- src/{game => }/debug/console/ui.rs | 4 +- src/{game => }/debug/console/ui/components.rs | 2 +- src/debugging.rs | 14 ---- src/game.rs | 13 +-- src/game/debug.rs | 19 ----- src/main.rs | 7 +- 9 files changed, 111 insertions(+), 68 deletions(-) create mode 100644 src/debug.rs rename src/{game => }/debug/console.rs (75%) rename src/{game => }/debug/console/cli.rs (78%) rename src/{game => }/debug/console/ui.rs (97%) rename src/{game => }/debug/console/ui/components.rs (98%) delete mode 100644 src/debugging.rs delete mode 100644 src/game/debug.rs diff --git a/src/debug.rs b/src/debug.rs new file mode 100644 index 0000000..c165af7 --- /dev/null +++ b/src/debug.rs @@ -0,0 +1,81 @@ +//! Seperate out debugging uis/plugins in it's own module for cleanliness. +use bevy::prelude::*; +use bevy_inspector_egui::bevy_egui::EguiPlugin; +use clap::Subcommand; +use console::ConsoleLog; +use util::{console_err, console_log}; + +use crate::AppState; + +mod console; + +mod util { + use bevy::prelude::*; + + use super::DebugEvent; + + pub fn console_log(dbg_writer: &mut EventWriter, s: impl Into) { + dbg_writer.write(DebugEvent::PrintToConsole { + error: false, + text: s.into(), + }); + } + + pub fn console_err(dbg_writer: &mut EventWriter, s: impl Into) { + dbg_writer.write(DebugEvent::PrintToConsole { + error: false, + text: s.into(), + }); + } +} + +/// Debug system set. +#[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] +pub struct DebugSet; + +pub fn plugin(app: &mut App) { + app.add_plugins(EguiPlugin { + enable_multipass_for_primary_context: true, + }) + .add_systems(Update, start_game.in_set(DebugSet)) + .add_event::() + .add_plugins(console::plugin); +} + +#[derive(Event, Debug, Subcommand, PartialEq, Eq)] +enum DebugEvent { + /// Close the debug console. + #[command(name = "close", aliases = ["close-console"])] + CloseDebugConsole, + + /// Output a string to the console. + #[command(name = "echo", aliases = ["print", "print-to-console"])] + PrintToConsole { + /// Print as error + #[arg(short, long)] + error: bool, + /// The text to be printed in the console. + text: String, + }, + + /// Start the game + StartGame, +} + +fn start_game( + mut dbg_reader: EventReader, + mut log: ResMut, + cur_state: Res>, + mut next_state: ResMut>, +) { + for ev in dbg_reader.read() { + if *ev == DebugEvent::StartGame { + if *cur_state == AppState::Ingame { + log.err("Can't start game since it's already started."); + } else { + log.write("Starting game..."); + next_state.set(AppState::Ingame); + } + } + } +} diff --git a/src/game/debug/console.rs b/src/debug/console.rs similarity index 75% rename from src/game/debug/console.rs rename to src/debug/console.rs index 449bd1b..a1ceef4 100644 --- a/src/game/debug/console.rs +++ b/src/debug/console.rs @@ -1,12 +1,14 @@ +use std::panic::Location; + use bevy::prelude::*; use ui::{ DebugConsole, autofocus, autoscroll, handle_open_console, process_prompt, update_content, update_scroll_position, }; -use crate::{AppState, cleanup::despawn, game::GameplaySet}; +use crate::{AppState, cleanup::despawn}; -use super::DebugEvent; +use super::{DebugEvent, DebugSet}; mod cli; mod ui; @@ -15,14 +17,12 @@ pub(super) fn plugin(app: &mut App) { app.init_state::() .add_systems( OnEnter(ConsoleState::Open), - (open_console, (autofocus, autoscroll).after(open_console)), + (open_console, (autofocus, autoscroll).after(open_console)).in_set(DebugSet), ) .add_systems( Update, ( - handle_open_console - .in_set(GameplaySet) - .run_if(in_state(ConsoleState::Closed).and(in_state(AppState::Ingame))), + handle_open_console.run_if(in_state(ConsoleState::Closed)), update_content.run_if(resource_changed::), ( update_scroll_position, @@ -30,15 +30,19 @@ pub(super) fn plugin(app: &mut App) { execute_console_events, ) .run_if(in_state(ConsoleState::Open)), - ), + ) + .in_set(DebugSet), + ) + .add_systems( + OnExit(ConsoleState::Open), + despawn::.in_set(DebugSet), ) - .add_systems(OnExit(ConsoleState::Open), despawn::) .insert_resource::(ConsoleLog::new()); } /// the usize is a read index #[derive(Resource, Clone)] -struct ConsoleLog(Vec, usize); +pub struct ConsoleLog(Vec, usize); impl ConsoleLog { fn new() -> Self { @@ -56,15 +60,15 @@ impl ConsoleLog { res } - pub fn input(&mut self, content: &str) { + fn input(&mut self, content: &str) { self.0.push(ConsoleEvent::Input(content.to_string())) } - pub fn output(&mut self, content: &str) { + pub fn write(&mut self, content: &str) { self.0.push(ConsoleEvent::Output(content.to_string())) } - pub fn error(&mut self, content: &str) { + pub fn err(&mut self, content: &str) { self.0.push(ConsoleEvent::Error(content.to_string())) } } @@ -98,8 +102,9 @@ fn execute_console_events( for ev in ev_reader.read() { match ev { DebugEvent::CloseDebugConsole => next_state.set(ConsoleState::Closed), - DebugEvent::PrintToConsole { text } => log.output(text), - _ => todo!(), + DebugEvent::PrintToConsole { text, error: false } => log.write(text), + DebugEvent::PrintToConsole { text, error: true } => log.err(text), + _ => {} }; } } diff --git a/src/game/debug/console/cli.rs b/src/debug/console/cli.rs similarity index 78% rename from src/game/debug/console/cli.rs rename to src/debug/console/cli.rs index ecdf825..bd43e1c 100644 --- a/src/game/debug/console/cli.rs +++ b/src/debug/console/cli.rs @@ -1,20 +1,20 @@ use clap::{Parser, Subcommand}; -use crate::game::debug::DebugEvent; +use crate::debug::DebugEvent; pub(super) fn respond(line: &str) -> Result { let args = shlex::split(line).ok_or("Invalid Quoting")?; let cli = Cli::try_parse_from(args).map_err(|e| e.to_string()); // help detection - let command_contains_help = (line.contains("help") || line.contains("-h")); + let command_contains_help = line.contains("help") || line.contains("-h"); let some_line_starts_with_usage = cli .as_ref() .is_err_and(|item| item.lines().any(|line| line.starts_with("Usage: "))); if command_contains_help && some_line_starts_with_usage { let Err(text) = cli else { unreachable!() }; - return Ok(DebugEvent::PrintToConsole { text }); + return Ok(DebugEvent::PrintToConsole { text, error: false }); } Ok(cli?.cmd) diff --git a/src/game/debug/console/ui.rs b/src/debug/console/ui.rs similarity index 97% rename from src/game/debug/console/ui.rs rename to src/debug/console/ui.rs index de265e3..6dbdb0f 100644 --- a/src/game/debug/console/ui.rs +++ b/src/debug/console/ui.rs @@ -16,7 +16,7 @@ use bevy_ui_text_input::TextSubmitEvent; pub(super) use components::console; use components::{Content, Prompt}; -use crate::game::debug::DebugEvent; +use crate::debug::DebugEvent; use super::{ConsoleEvent, ConsoleLog, ConsoleState, OPEN_CONSOLE_DEFAULT, cli::respond}; @@ -75,7 +75,7 @@ pub fn process_prompt( Ok(debug_ev) => { debug_event_writer.write(debug_ev); } - Err(e) => log.error(&e), + Err(e) => log.err(&e), } } } diff --git a/src/game/debug/console/ui/components.rs b/src/debug/console/ui/components.rs similarity index 98% rename from src/game/debug/console/ui/components.rs rename to src/debug/console/ui/components.rs index 7ec8ce2..4a7773e 100644 --- a/src/game/debug/console/ui/components.rs +++ b/src/debug/console/ui/components.rs @@ -1,7 +1,7 @@ use bevy::{ecs::spawn::SpawnWith, prelude::*}; use bevy_ui_text_input::{TextInputMode, TextInputNode, TextInputPrompt}; -use crate::game::debug::console::{ConsoleEvent, ConsoleLog}; +use crate::debug::console::{ConsoleEvent, ConsoleLog}; use super::DebugConsole; diff --git a/src/debugging.rs b/src/debugging.rs deleted file mode 100644 index 02560fa..0000000 --- a/src/debugging.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Seperate out debugging uis/plugins in it's own module for cleanliness. -use bevy::prelude::*; -use bevy_inspector_egui::bevy_egui::EguiPlugin; - -pub fn plugin(app: &mut App) { - app.add_plugins(EguiPlugin { - enable_multipass_for_primary_context: true, - }) - .add_plugins(( - // WorldInspectorPlugin::new(), - // StateInspectorPlugin::::new(), - // StateInspectorPlugin::::new(), - )); -} diff --git a/src/game.rs b/src/game.rs index 731d28e..6b69f1b 100644 --- a/src/game.rs +++ b/src/game.rs @@ -6,18 +6,7 @@ use crate::{ }; mod camera; -mod debug; mod scene; -mod tram { - use bevy::prelude::*; - - fn spawn_tram( - mut c: Commands, - mut meshes: ResMut>, - mut materials: ResMut>, - ) { - } -} /// Gameplay system set. All functions in this control the gameplay (duh). #[derive(SystemSet, Debug, Clone, PartialEq, Eq, Hash)] @@ -29,6 +18,6 @@ pub fn plugin(app: &mut App) { OnExit(AppState::Ingame), despawn::.in_set(GameplaySet), ) - .add_plugins((debug::plugin, camera::plugin)); + .add_plugins(camera::plugin); app.configure_sets(Update, GameplaySet.run_if(in_state(AppState::Ingame))); } diff --git a/src/game/debug.rs b/src/game/debug.rs deleted file mode 100644 index 1ed78b9..0000000 --- a/src/game/debug.rs +++ /dev/null @@ -1,19 +0,0 @@ -use bevy::prelude::*; -use clap::Subcommand; - -mod console; - -#[derive(Event, Debug, Subcommand)] -enum DebugEvent { - /// Close the debug console. - #[command(name = "close", aliases = ["close-console"])] - CloseDebugConsole, - - /// Output a string to the console. - #[command(name = "echo", aliases = ["print", "print-to-console"])] - PrintToConsole { text: String }, -} - -pub(super) fn plugin(app: &mut App) { - app.add_plugins(console::plugin).add_event::(); -} diff --git a/src/main.rs b/src/main.rs index 6868b28..be9794e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,15 @@ use bevy::prelude::*; use bevy_skein::SkeinPlugin; use bevy_ui_text_input::TextInputPlugin; +use clap::ValueEnum; mod camera; mod cleanup; -mod debugging; +mod debug; mod game; mod menus; -#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash, Reflect)] +#[derive(States, Default, Debug, Clone, PartialEq, Eq, Hash, Reflect, ValueEnum)] #[allow(unused)] enum AppState { #[default] @@ -23,7 +24,7 @@ fn main() { .register_type::() .add_systems(Startup, camera::setup) .add_plugins((DefaultPlugins, TextInputPlugin)) - .add_plugins((game::plugin, menus::plugin, debugging::plugin)) + .add_plugins((game::plugin, menus::plugin, debug::plugin)) .add_plugins(SkeinPlugin::default()) .init_state::() .register_type::()