#![doc = include_str ! ("../README.md")]
#![deny(missing_docs)]
use bevy::prelude::*;
pub use bevy_console_derive::ConsoleCommand;
use bevy_egui::{EguiPlugin, EguiPreUpdateSet, EguiPrimaryContextPass};
use console::{ConsoleCache, block_keyboard_input, block_mouse_input};
use trie_rs::TrieBuilder;
#[cfg(feature = "default-commands")]
use crate::commands::{
clear::{ClearCommand, clear_command},
exit::{ExitCommand, exit_command},
help::{HelpCommand, help_command},
};
pub use crate::console::{
AddConsoleCommand, Command, ConsoleCommand, ConsoleCommandEntered, ConsoleConfiguration,
ConsoleOpen, NamedCommand, PrintConsoleLine,
};
pub use crate::log::*;
use crate::console::{ConsoleState, console_ui, receive_console_line};
pub use clap;
mod color;
#[cfg(feature = "default-commands")]
mod commands;
mod console;
mod log;
mod macros;
pub struct ConsolePlugin;
#[derive(SystemSet, Debug, Hash, PartialEq, Eq, Clone)]
pub enum ConsoleSet {
Startup,
ConsoleUI,
Commands,
PostCommands,
}
fn have_commands(commands: MessageReader<ConsoleCommandEntered>) -> bool {
!commands.is_empty()
}
fn init(config: Res<ConsoleConfiguration>, mut cache: ResMut<ConsoleCache>) {
debug!("lib.rs:init");
let mut trie_builder = TrieBuilder::new();
for cmd in config.commands.keys() {
trie_builder.push(cmd);
}
for completions in &config.arg_completions {
trie_builder.push(completions.join(" "));
}
cache.commands_trie = Some(trie_builder.build());
}
impl Plugin for ConsolePlugin {
fn build(&self, app: &mut App) {
app.init_resource::<ConsoleConfiguration>()
.init_resource::<ConsoleState>()
.init_resource::<ConsoleOpen>()
.init_resource::<ConsoleCache>()
.add_message::<ConsoleCommandEntered>()
.add_message::<PrintConsoleLine>();
#[cfg(feature = "default-commands")]
app.add_console_command::<ClearCommand, _>(clear_command)
.add_console_command::<ExitCommand, _>(exit_command)
.add_console_command::<HelpCommand, _>(help_command);
app.add_systems(Startup, init.after(ConsoleSet::Startup))
.add_systems(
PreUpdate,
(block_mouse_input, block_keyboard_input)
.after(EguiPreUpdateSet::ProcessInput)
.before(EguiPreUpdateSet::BeginPass),
)
.add_systems(
EguiPrimaryContextPass,
(
console_ui.in_set(ConsoleSet::ConsoleUI),
receive_console_line.in_set(ConsoleSet::PostCommands),
),
)
.configure_sets(
EguiPrimaryContextPass,
(
ConsoleSet::Commands
.after(ConsoleSet::ConsoleUI)
.run_if(have_commands),
ConsoleSet::PostCommands.after(ConsoleSet::Commands),
),
);
if !app.is_plugin_added::<EguiPlugin>() {
app.add_plugins(EguiPlugin::default());
}
}
}