jakobhellermann / bevy-inspector-egui

Inspector plugin for the bevy game engine
Apache License 2.0
1.18k stars 173 forks source link

Dynamically open or hide world inspector window #119

Closed Shatur closed 1 year ago

Shatur commented 1 year ago

Previously it could be controlled with WorldInspectorParams. Now I have to copy world_inspector_ui system in order to add this feature. I would suggest to add a resource to control this behavior. Or we could make world_inspector_ui public. Or we could wait for stageless in Bevy 0.10 and provide the ability to specify condition on plugin creation.

jakobhellermann commented 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.

mrchantey commented 1 year ago

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

jakobhellermann commented 1 year ago

You can now do this: https://github.com/jakobhellermann/bevy-inspector-egui/blob/acb000ae3c1341b5e48389fedd8e97bf329bd106/crates/bevy-inspector-egui/examples/quick/world_inspector.rs#L8-L10