Skip to main content

Crate bevy_mod_imgui

Crate bevy_mod_imgui 

Source
Expand description

A Dear ImGui integration for the Bevy game engine.

§Minimal Example

use bevy::prelude::*;
use bevy_mod_imgui::prelude::*;

#[derive(Resource)]
struct ImguiState {
    demo_window_open: bool,
}

fn main() {
    let mut app = App::new();
    app.insert_resource(ClearColor(Color::srgba(0.2, 0.2, 0.2, 1.0)))
        .insert_resource(ImguiState {
            demo_window_open: true,
        })
        .add_plugins(DefaultPlugins)
        .add_plugins(bevy_mod_imgui::ImguiPlugin::default())
        .add_systems(Startup, |mut commands: Commands| {
            commands.spawn(Camera3d::default());
        })
        .add_systems(Update, imgui_example_ui);
    app.run();
}

fn imgui_example_ui(mut context: NonSendMut<ImguiContext>, mut state: ResMut<ImguiState>) {
    let ui = context.ui();
    if state.demo_window_open {
        ui.show_demo_window(&mut state.demo_window_open);
    }
}

§Configuring the imgui::Io flags

During Startup, the with_io_mut function can be used to get mutable access to the underlying imgui::Io struct, so that flags can be configured:

app.add_plugins(bevy_mod_imgui::ImguiPlugin::default())
   .add_systems(Startup, |mut imgui: NonSendMut<ImguiContext>| {
        imgui.with_io_mut(|io| {
            io.config_docking_always_tab_bar = true;
        });
    });

§Minimal Example with Docking

With the docking feature enabled, windows can be docked by creating a dock space, and then creating them as usual:

use bevy::prelude::*;
use bevy_mod_imgui::prelude::*;

fn main() {
    let mut app = App::new();
    app.insert_resource(ClearColor(Color::srgba(0.2, 0.2, 0.2, 1.0)))
        .add_plugins(DefaultPlugins)
        .add_plugins(bevy_mod_imgui::ImguiPlugin {
            ..Default::default()
        })
        .add_systems(Startup, |mut commands: Commands| {
            commands.spawn(Camera3d::default());
        })
        .add_systems(Update, imgui_example_ui);
    app.run();
}

fn imgui_example_ui(mut context: NonSendMut<ImguiContext>) {
    let ui = context.ui();
    ui.dockspace_over_main_viewport();
    let window = ui.window("Drag me");
    window
        .size([300.0, 100.0], imgui::Condition::FirstUseEver)
        .position([0.0, 0.0], imgui::Condition::FirstUseEver)
        .build(|| {
            ui.text("Drag the window title-bar to dock it!");
        });
}

Modules§

prelude

Structs§

ImguiContext
The ImGui context resource.
ImguiNodeLabel
The label used by the render node responsible for rendering ImGui
ImguiPlugin
Configuration settings for this plugin