Skip to content

Instantly share code, notes, and snippets.

@ItsDoot
Last active December 9, 2024 08:35
Show Gist options
  • Save ItsDoot/5d31ca07db3fa46d6cb3fac3f3aeafe3 to your computer and use it in GitHub Desktop.
Save ItsDoot/5d31ca07db3fa46d6cb3fac3f3aeafe3 to your computer and use it in GitHub Desktop.
Example of my PoC bevy egui commands-based integration
use bevy::{
app::{App, Update},
prelude::{Commands, ResMut, Resource, World},
DefaultPlugins,
};
use bevy_egui::EguiPlugin;
use bevy_egui_commands::prelude::*;
use egui::{Button, Label, Layout, Response};
#[derive(Resource, Default)]
pub struct Counter(i32);
pub fn render(mut commands: Commands, text: ResMut<Counter>) {
commands.root(TopBottomPanel::top("top"), |mut ui: UiCommands| {
ui.add(Label::new("Top panel"), ());
});
commands.root(SidePanel::left("left"), |mut ui: UiCommands| {
ui.container(Group, (), |mut ui: UiCommands| {
ui.add(Label::new("Left panel"), ());
});
});
commands.root(SidePanel::right("right"), |mut ui: UiCommands| {
ui.container(Layout::bottom_up(egui::Align::Min), (), |mut ui: UiCommands| {
ui.add(Label::new("Right panel"), ());
});
});
commands.root(CentralPanel::default(), |mut ui: UiCommands| {
ui.add(
Button::new("Hello world!"),
|world: &mut World, resp: Response| {
let mut counter = world.get_resource_mut::<Counter>().unwrap();
if resp.clicked() {
counter.0 += 1;
}
},
);
ui.add(Label::new(format!("Clicked: {}", text.0)), ());
});
}
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.init_resource::<Counter>()
.add_systems(Update, render)
.run();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment