Closed Shatur closed 1 year ago
Or we could make world_inspector_ui public.
bevy_inspector::ui_for_world
is public which is reusable for more customized solutions.
If you toggle your window you already have some system for that, and doing the UI for the window in that system seems like it wouldn't be that much more complicated.
That said, I'm open to making this easier, and curious if we can makeWorldInspectorPlugin.run_if
possible with new bevy apis.
For those looking for a copy-paste solution in the meantime, here's one that will toggle on a G
keypress:
use bevy::prelude::*;
use bevy_inspector_egui::{
bevy_egui::{self, EguiPlugin},
bevy_inspector, egui, DefaultInspectorConfigPlugin,
};
pub struct WorldInspectorPlugin;
const DEFAULT_SIZE: (f32, f32) = (320., 160.);
#[derive(Default, Resource)]
pub struct WorldInspectorParams {
pub enabled: bool,
}
impl Plugin for WorldInspectorPlugin {
fn build(&self, app: &mut App) {
if !app.is_plugin_added::<DefaultInspectorConfigPlugin>() {
app.add_plugin(DefaultInspectorConfigPlugin);
}
if !app.is_plugin_added::<EguiPlugin>() {
app.add_plugin(EguiPlugin);
}
app.insert_resource(WorldInspectorParams::default());
app.add_system(world_inspector_ui);
app.add_system(toggle_inspector_on_keypress);
}
}
fn world_inspector_ui(world: &mut World) {
let params = world.resource::<WorldInspectorParams>();
if !params.enabled {
return;
}
let mut egui_context = world
.query_filtered::<&mut EguiContext, With<PrimaryWindow>>()
.single(world)
.clone();
egui::Window::new("World Inspector")
.default_size(DEFAULT_SIZE)
.show(egui_context.get_mut(), |ui| {
egui::ScrollArea::vertical().show(ui, |ui| {
bevy_inspector::ui_for_world(world, ui);
ui.allocate_space(ui.available_size());
});
});
}
fn toggle_inspector_on_keypress(
mut params: ResMut<WorldInspectorParams>,
keys: Res<Input<KeyCode>>,
) {
if keys.any_just_pressed([KeyCode::G]) {
params.enabled = !params.enabled;
}
}
Why G
? cos this is a Great library, thanks team!
Edit: updated for bevy 0.10
Previously it could be controlled with
WorldInspectorParams
. Now I have to copyworld_inspector_ui
system in order to add this feature. I would suggest to add a resource to control this behavior. Or we could makeworld_inspector_ui
public. Or we could wait for stageless in Bevy 0.10 and provide the ability to specify condition on plugin creation.