Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
] }
Expand All @@ -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" }


Expand Down
6 changes: 6 additions & 0 deletions examples/capture_bevy_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand All @@ -21,3 +23,7 @@ fn main() {
})
.run();
}

fn setup_camera_system(mut commands: Commands) {
commands.spawn(Camera2d);
}
5 changes: 5 additions & 0 deletions examples/change_console_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -21,3 +22,7 @@ fn main() {
})
.run();
}

fn setup_camera_system(mut commands: Commands) {
commands.spawn(Camera2d);
}
5 changes: 5 additions & 0 deletions examples/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand All @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions examples/log_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<LogCommand, _>(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")]
Expand Down
7 changes: 6 additions & 1 deletion examples/raw_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsoleCommandEntered>) {
fn setup_camera_system(mut commands: Commands) {
commands.spawn(Camera2d);
}

fn raw_commands(mut console_commands: MessageReader<ConsoleCommandEntered>) {
for ConsoleCommandEntered { command_name, args } in console_commands.read() {
println!(r#"Entered command "{command_name}" with args {:#?}"#, args);
}
Expand Down
7 changes: 6 additions & 1 deletion examples/write_to_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -12,6 +13,10 @@ fn main() {
.run();
}

fn write_to_console(mut console_line: EventWriter<PrintConsoleLine>) {
fn setup_camera_system(mut commands: Commands) {
commands.spawn(Camera2d);
}

fn write_to_console(mut console_line: MessageWriter<PrintConsoleLine>) {
console_line.write(PrintConsoleLine::new("Hello".into()));
}
2 changes: 1 addition & 1 deletion src/commands/exit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub(crate) struct ExitCommand;

pub(crate) fn exit_command(
mut exit: ConsoleCommand<ExitCommand>,
mut exit_writer: EventWriter<AppExit>,
mut exit_writer: MessageWriter<AppExit>,
) {
if let Some(Ok(_)) = exit.take() {
exit_writer.write(AppExit::Success);
Expand Down
36 changes: 23 additions & 13 deletions src/console.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bevy::ecs::query::FilteredAccessSet;
use bevy::ecs::resource::Resource;
use bevy::ecs::{
component::Tick,
Expand Down Expand Up @@ -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 {}
Expand Down Expand Up @@ -69,10 +70,11 @@ pub trait NamedCommand {
/// ```
pub struct ConsoleCommand<'w, T> {
command: Option<Result<T, clap::Error>>,
console_line: EventWriter<'w, PrintConsoleLine>,
console_line: MessageWriter<'w, PrintConsoleLine>,
}

impl<T> ConsoleCommand<'_, T> {
///
/// Returns Some(T) if the command was executed and arguments were valid.
///
/// This method should only be called once.
Expand Down Expand Up @@ -128,16 +130,24 @@ unsafe impl<T: Command> SystemParam for ConsoleCommand<'_, T> {
type State = ConsoleCommandState<T>;
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,
marker: PhantomData,
}
}

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,
Expand Down Expand Up @@ -189,7 +199,7 @@ unsafe impl<T: Command> 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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -488,9 +498,9 @@ pub(crate) fn console_ui(
mut egui_context: EguiContexts,
config: Res<ConsoleConfiguration>,
mut cache: ResMut<ConsoleCache>,
mut keyboard_input_events: EventReader<KeyboardInput>,
mut keyboard_input_events: MessageReader<KeyboardInput>,
mut state: ResMut<ConsoleState>,
command_entered: EventWriter<ConsoleCommandEntered>,
command_entered: MessageWriter<ConsoleCommandEntered>,
mut console_open: ResMut<ConsoleOpen>,
) {
let keyboard_input_events = keyboard_input_events.read().collect::<Vec<_>>();
Expand Down Expand Up @@ -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,
) {
Expand Down Expand Up @@ -730,7 +740,7 @@ fn handle_enter(

pub(crate) fn receive_console_line(
mut console_state: ResMut<ConsoleState>,
mut events: EventReader<PrintConsoleLine>,
mut events: MessageReader<PrintConsoleLine>,
) {
for event in events.read() {
let event: &PrintConsoleLine = event;
Expand Down
7 changes: 4 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ConsoleCommandEntered>) -> bool {
fn have_commands(commands: MessageReader<ConsoleCommandEntered>) -> bool {
!commands.is_empty()
}

/// builds the predictive search engine for completions
fn init(config: Res<ConsoleConfiguration>, mut cache: ResMut<ConsoleCache>) {
println!("lib.rs:init");
let mut trie_builder = TrieBuilder::new();
for cmd in config.commands.keys() {
trie_builder.push(cmd);
Expand All @@ -72,8 +73,8 @@ impl Plugin for ConsolePlugin {
.init_resource::<ConsoleState>()
.init_resource::<ConsoleOpen>()
.init_resource::<ConsoleCache>()
.add_event::<ConsoleCommandEntered>()
.add_event::<PrintConsoleLine>()
.add_message::<ConsoleCommandEntered>()
.add_message::<PrintConsoleLine>()
.add_console_command::<ClearCommand, _>(clear_command)
.add_console_command::<ExitCommand, _>(exit_command)
.add_console_command::<HelpCommand, _>(help_command)
Expand Down
4 changes: 2 additions & 2 deletions src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<BevyLogBuffer>,
mut console_lines: EventWriter<PrintConsoleLine>,
mut console_lines: MessageWriter<PrintConsoleLine>,
) {
let mut buffer = buffer.0.lock().unwrap();
// read and clean buffer
Expand Down
Loading