diff --git a/Cargo.toml b/Cargo.toml index 10508eb..6bf4ff9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,13 +10,13 @@ license = "MIT" readme = "README.md" [dependencies] -bevy = { version = "0.16", default-features = false, features = [ +bevy = { version = "0.17", default-features = false, features = [ "std", "bevy_log", ] } clap = { version = "4.5", features = ["derive"] } bevy_console_derive = { path = "./bevy_console_derive", version = "0.5.0" } -bevy_egui = { version = "0.35", default-features = false, features = [ +bevy_egui = { version = "0.37", default-features = false, features = [ "render", "default_fonts", ] } @@ -26,7 +26,7 @@ strip-ansi-escapes = "0.2" trie-rs = "0.2" [dev-dependencies] -bevy = { version = "0.16", features = ["std", "bevy_log"] } +bevy = { version = "0.17", features = ["std", "bevy_log"] } color-print = { version = "0.3" } diff --git a/examples/capture_bevy_logs.rs b/examples/capture_bevy_logs.rs index 2f78392..815f7d2 100644 --- a/examples/capture_bevy_logs.rs +++ b/examples/capture_bevy_logs.rs @@ -9,9 +9,11 @@ fn main() { level: log::Level::INFO, filter: "error,capture_bevy_logs=info".to_owned(), custom_layer: make_layer, + fmt_layer: |_| None, }), ConsolePlugin, )) + .add_systems(Startup, setup_camera_system) .add_systems(Startup, || { log::info!("Hi!"); log::warn!("This is a warning!"); @@ -21,3 +23,7 @@ fn main() { }) .run(); } + +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} diff --git a/examples/change_console_key.rs b/examples/change_console_key.rs index 2218790..8710982 100644 --- a/examples/change_console_key.rs +++ b/examples/change_console_key.rs @@ -5,6 +5,7 @@ use bevy_console::{ConsoleConfiguration, ConsolePlugin}; fn main() { App::new() .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .insert_resource(ConsoleConfiguration { keys: vec![ // Console key on a swedish keyboard @@ -21,3 +22,7 @@ fn main() { }) .run(); } + +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} diff --git a/examples/completions.rs b/examples/completions.rs index bdf5c3a..88662f2 100644 --- a/examples/completions.rs +++ b/examples/completions.rs @@ -6,6 +6,7 @@ fn main() { App::new() // set background to red .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .insert_resource(ConsoleConfiguration { arg_completions: vec![ vec!["custom".into(), "foo".into()], @@ -18,6 +19,10 @@ fn main() { .run(); } +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + #[derive(Clone, Copy, ValueEnum)] pub enum Variant { Foo, diff --git a/examples/log_command.rs b/examples/log_command.rs index 8638942..9029f7a 100644 --- a/examples/log_command.rs +++ b/examples/log_command.rs @@ -6,10 +6,15 @@ fn main() { App::new() // set background to red .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .add_console_command::(log_command) .run(); } +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + /// Prints given arguments to the console #[derive(Parser, ConsoleCommand)] #[command(name = "log")] diff --git a/examples/raw_commands.rs b/examples/raw_commands.rs index 00a047d..bb84a41 100644 --- a/examples/raw_commands.rs +++ b/examples/raw_commands.rs @@ -4,11 +4,16 @@ use bevy_console::{ConsoleCommandEntered, ConsolePlugin, ConsoleSet}; fn main() { App::new() .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) .add_systems(Update, raw_commands.in_set(ConsoleSet::Commands)) .run(); } -fn raw_commands(mut console_commands: EventReader) { +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + +fn raw_commands(mut console_commands: MessageReader) { for ConsoleCommandEntered { command_name, args } in console_commands.read() { println!(r#"Entered command "{command_name}" with args {:#?}"#, args); } diff --git a/examples/write_to_console.rs b/examples/write_to_console.rs index 5b37630..1bf8d31 100644 --- a/examples/write_to_console.rs +++ b/examples/write_to_console.rs @@ -4,6 +4,7 @@ use bevy_console::{ConsolePlugin, ConsoleSet, PrintConsoleLine}; fn main() { App::new() .add_plugins((DefaultPlugins, ConsolePlugin)) + .add_systems(Startup, setup_camera_system) // NOTE: this wouldn't work for this particular case, // systems in the [`ConsoleSet::Commands`] do not run if there are no console commands entered // .add_systems(Update, write_to_console.in_set(ConsoleSet::Commands)) @@ -12,6 +13,10 @@ fn main() { .run(); } -fn write_to_console(mut console_line: EventWriter) { +fn setup_camera_system(mut commands: Commands) { + commands.spawn(Camera2d); +} + +fn write_to_console(mut console_line: MessageWriter) { console_line.write(PrintConsoleLine::new("Hello".into())); } diff --git a/src/commands/exit.rs b/src/commands/exit.rs index 413b99c..7b87767 100644 --- a/src/commands/exit.rs +++ b/src/commands/exit.rs @@ -12,7 +12,7 @@ pub(crate) struct ExitCommand; pub(crate) fn exit_command( mut exit: ConsoleCommand, - mut exit_writer: EventWriter, + mut exit_writer: MessageWriter, ) { if let Some(Ok(_)) = exit.take() { exit_writer.write(AppExit::Success); diff --git a/src/console.rs b/src/console.rs index f767809..78681de 100644 --- a/src/console.rs +++ b/src/console.rs @@ -1,3 +1,4 @@ +use bevy::ecs::query::FilteredAccessSet; use bevy::ecs::resource::Resource; use bevy::ecs::{ component::Tick, @@ -27,9 +28,9 @@ use crate::{ ConsoleSet, }; -type ConsoleCommandEnteredReaderSystemParam = EventReader<'static, 'static, ConsoleCommandEntered>; +type ConsoleCommandEnteredReaderSystemParam = MessageReader<'static, 'static, ConsoleCommandEntered>; -type PrintConsoleLineWriterSystemParam = EventWriter<'static, PrintConsoleLine>; +type PrintConsoleLineWriterSystemParam = MessageWriter<'static, PrintConsoleLine>; /// A super-trait for command like structures pub trait Command: NamedCommand + CommandFactory + FromArgMatches + Sized + Resource {} @@ -69,10 +70,11 @@ pub trait NamedCommand { /// ``` pub struct ConsoleCommand<'w, T> { command: Option>, - console_line: EventWriter<'w, PrintConsoleLine>, + console_line: MessageWriter<'w, PrintConsoleLine>, } impl ConsoleCommand<'_, T> { + /// /// Returns Some(T) if the command was executed and arguments were valid. /// /// This method should only be called once. @@ -128,9 +130,9 @@ unsafe impl SystemParam for ConsoleCommand<'_, T> { type State = ConsoleCommandState; type Item<'w, 's> = ConsoleCommand<'w, T>; - fn init_state(world: &mut World, system_meta: &mut SystemMeta) -> Self::State { - let event_reader = ConsoleCommandEnteredReaderSystemParam::init_state(world, system_meta); - let console_line = PrintConsoleLineWriterSystemParam::init_state(world, system_meta); + fn init_state(world: &mut World) -> Self::State { + let event_reader = ConsoleCommandEnteredReaderSystemParam::init_state(world); + let console_line = PrintConsoleLineWriterSystemParam::init_state(world); ConsoleCommandState { event_reader, console_line, @@ -138,6 +140,14 @@ unsafe impl SystemParam for ConsoleCommand<'_, T> { } } + fn init_access( + state: &Self::State, + system_meta: &mut SystemMeta, + component_access_set: &mut FilteredAccessSet, + world: &mut World, + ) { + } + #[inline] unsafe fn get_param<'w, 's>( state: &'s mut Self::State, @@ -189,7 +199,7 @@ unsafe impl SystemParam for ConsoleCommand<'_, T> { } } /// Parsed raw console command into `command` and `args`. -#[derive(Clone, Debug, Event)] +#[derive(Clone, Debug, Event, Message)] pub struct ConsoleCommandEntered { /// the command definition pub command_name: String, @@ -198,7 +208,7 @@ pub struct ConsoleCommandEntered { } /// Events to print to the console. -#[derive(Clone, Debug, Eq, Event, PartialEq)] +#[derive(Clone, Debug, Eq, Event, PartialEq, Message)] pub struct PrintConsoleLine { /// Console line pub line: String, @@ -240,7 +250,7 @@ pub struct ConsoleConfiguration { pub moveable: bool, /// show the title bar or not pub show_title_bar: bool, - /// Background color of console window + /// Background color of console window pub background_color: Color32, /// Foreground (text) color pub foreground_color: Color32, @@ -488,9 +498,9 @@ pub(crate) fn console_ui( mut egui_context: EguiContexts, config: Res, mut cache: ResMut, - mut keyboard_input_events: EventReader, + mut keyboard_input_events: MessageReader, mut state: ResMut, - command_entered: EventWriter, + command_entered: MessageWriter, mut console_open: ResMut, ) { let keyboard_input_events = keyboard_input_events.read().collect::>(); @@ -674,7 +684,7 @@ fn handle_enter( config: Res<'_, ConsoleConfiguration>, cache: &ResMut<'_, ConsoleCache>, state: &mut ResMut<'_, ConsoleState>, - mut command_entered: EventWriter<'_, ConsoleCommandEntered>, + mut command_entered: MessageWriter<'_, ConsoleCommandEntered>, ui: &mut egui::Ui, text_edit_response: &egui::Response, ) { @@ -730,7 +740,7 @@ fn handle_enter( pub(crate) fn receive_console_line( mut console_state: ResMut, - mut events: EventReader, + mut events: MessageReader, ) { for event in events.read() { let event: &PrintConsoleLine = event; diff --git a/src/lib.rs b/src/lib.rs index 5610f99..3418c80 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -48,12 +48,13 @@ pub enum ConsoleSet { } /// Run condition which does not run any command systems if no command was entered -fn have_commands(commands: EventReader) -> bool { +fn have_commands(commands: MessageReader) -> bool { !commands.is_empty() } /// builds the predictive search engine for completions fn init(config: Res, mut cache: ResMut) { + println!("lib.rs:init"); let mut trie_builder = TrieBuilder::new(); for cmd in config.commands.keys() { trie_builder.push(cmd); @@ -72,8 +73,8 @@ impl Plugin for ConsolePlugin { .init_resource::() .init_resource::() .init_resource::() - .add_event::() - .add_event::() + .add_message::() + .add_message::() .add_console_command::(clear_command) .add_console_command::(exit_command) .add_console_command::(help_command) diff --git a/src/log.rs b/src/log.rs index 567fece..28611c4 100644 --- a/src/log.rs +++ b/src/log.rs @@ -6,7 +6,7 @@ use std::{ use bevy::{ app::{App, Update}, log::tracing_subscriber::{self, EnvFilter, Layer, Registry}, - prelude::{EventWriter, IntoScheduleConfigs, ResMut, Resource}, + prelude::{MessageWriter, IntoScheduleConfigs, ResMut, Resource}, }; use crate::{ConsoleSet, PrintConsoleLine}; @@ -45,7 +45,7 @@ impl Write for BevyLogBufferWriter { /// Flushes the log buffer and sends its content to the console pub fn send_log_buffer_to_console( buffer: ResMut, - mut console_lines: EventWriter, + mut console_lines: MessageWriter, ) { let mut buffer = buffer.0.lock().unwrap(); // read and clean buffer