Skip to content

Commit

Permalink
egui 0.29 support
Browse files Browse the repository at this point in the history
  • Loading branch information
PPakalns committed Oct 3, 2024
1 parent 3aed12f commit 354ced5
Show file tree
Hide file tree
Showing 11 changed files with 105 additions and 36 deletions.
37 changes: 20 additions & 17 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ required-features = ["render"]
name = "simple"
required-features = ["render"]
[[example]]
name = "simple_multipass"
required-features = ["render"]
[[example]]
name = "two_windows"
required-features = ["render"]
[[example]]
Expand All @@ -46,10 +49,10 @@ required-features = ["render"]

[dependencies]
bevy = { version = "0.14.0", default-features = false, features = [
"bevy_asset",
"bevy_winit",
"bevy_asset",
"bevy_winit",
] }
egui = { version = "0.28", default-features = false, features = ["bytemuck"] }
egui = { version = "0.29", default-features = false, features = ["bytemuck"] }
bytemuck = "1"
webbrowser = { version = "1.0.1", optional = true }
wgpu-types = "0.20"
Expand All @@ -61,25 +64,25 @@ thread_local = { version = "1.1.0", optional = true }
[dev-dependencies]
version-sync = "0.9.4"
bevy = { version = "0.14.0", default-features = false, features = [
"x11",
"png",
"bevy_pbr",
"bevy_core_pipeline",
"tonemapping_luts",
"webgl2",
"x11",
"png",
"bevy_pbr",
"bevy_core_pipeline",
"tonemapping_luts",
"webgl2",
] }
egui = { version = "0.28", default-features = false, features = ["bytemuck"] }
egui = { version = "0.29", default-features = false, features = ["bytemuck"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
winit = "0.30"
web-sys = { version = "0.3.63", features = [
"Clipboard",
"ClipboardEvent",
"DataTransfer",
'Document',
'EventTarget',
"Window",
"Navigator",
"Clipboard",
"ClipboardEvent",
"DataTransfer",
'Document',
'EventTarget',
"Window",
"Navigator",
] }
js-sys = "0.3.63"
wasm-bindgen = "0.2.84"
Expand Down
2 changes: 1 addition & 1 deletion examples/paint_callback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use wgpu_types::{Extent3d, TextureUsages};

fn main() {
App::new()
.add_plugins((DefaultPlugins, EguiPlugin, CustomPipelinePlugin))
.add_plugins((DefaultPlugins, EguiPlugin::default(), CustomPipelinePlugin))
.add_systems(Startup, setup_worldspace)
.add_systems(
Update,
Expand Down
2 changes: 1 addition & 1 deletion examples/render_egui_to_texture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use wgpu_types::{Extent3d, TextureUsages};
fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins);
app.add_plugins(EguiPlugin);
app.add_plugins(EguiPlugin::default());
app.add_systems(Startup, setup_worldspace);
app.add_systems(Update, (update_screenspace, update_worldspace));
app.run();
Expand Down
2 changes: 1 addition & 1 deletion examples/render_to_image_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use bevy_egui::{egui::Widget, EguiContexts, EguiPlugin, EguiUserTextures};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.add_plugins(EguiPlugin::default())
.add_systems(Startup, setup)
.add_systems(Update, rotator_system)
.add_systems(Update, render_to_image_example_system)
Expand Down
2 changes: 1 addition & 1 deletion examples/side_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
App::new()
.insert_resource(WinitSettings::desktop_app())
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.add_plugins(EguiPlugin::default())
.init_resource::<OccupiedScreenSpace>()
.add_systems(Startup, setup_system)
.add_systems(Update, ui_example_system)
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bevy_egui::{EguiContexts, EguiPlugin};
fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.add_plugins(EguiPlugin::default())
// Systems that create Egui widgets should be run during the `CoreSet::Update` set,
// or after the `EguiSet::BeginFrame` system (which belongs to the `CoreSet::PreUpdate` set).
.add_systems(Update, ui_example_system)
Expand Down
36 changes: 36 additions & 0 deletions examples/simple_multipass.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::num::NonZero;

use bevy::prelude::*;
use bevy_egui::{EguiContext, EguiFullOutput, EguiInput, EguiPlugin};

fn main() {
App::new()
.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin { manual_run: true })
// Systems that create Egui widgets should be run during the `CoreSet::Update` set,
// or after the `EguiSet::BeginFrame` system (which belongs to the `CoreSet::PreUpdate` set).
.add_systems(Update, ui_example_system)
.run();
}

fn ui_example_system(mut contexts: Query<(&mut EguiContext, &mut EguiInput, &mut EguiFullOutput)>) {
for (mut ctx, mut egui_input, mut egui_full_output) in contexts.iter_mut() {
let ui = |ctx: &egui::Context| {
egui::Window::new("Hello").show(ctx, |ui| {
let passes = ui
.ctx()
.viewport(|viewport| viewport.output.num_completed_passes)
+ 1;
ui.label(format!("Passes: {}", passes));
ui.ctx().request_discard("Trying to reach max limit");
});
};

let ctx = ctx.get_mut();
ctx.memory_mut(|memory| {
memory.options.max_passes = NonZero::new(5).unwrap();
});

**egui_full_output = Some(ctx.run(egui_input.take(), ui));
}
}
2 changes: 1 addition & 1 deletion examples/two_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct Images {
fn main() {
let mut app = App::new();
app.add_plugins(DefaultPlugins)
.add_plugins(EguiPlugin)
.add_plugins(EguiPlugin::default())
.init_resource::<SharedUiState>()
.add_systems(Startup, load_assets_system)
.add_systems(Startup, create_new_window_system)
Expand Down
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ fn main() {
}),
..default()
}))
.add_plugins(EguiPlugin)
.add_plugins(EguiPlugin::default())
.add_systems(Startup, configure_visuals_system)
.add_systems(Startup, configure_ui_state_system)
.add_systems(Update, update_ui_scale_factor_system)
Expand Down
35 changes: 27 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,13 @@ use bevy::{
use std::cell::{RefCell, RefMut};

/// Adds all Egui resources and render graph nodes.
pub struct EguiPlugin;
#[derive(Default)]
pub struct EguiPlugin {
/// Controls if egui must be run manually
///
/// using `egui::context::Context` object `run` or `begin_pass` and `end_pass` function calls.
pub manual_run: bool,
}

/// A resource for storing global UI settings.
#[derive(Clone, Debug, Resource, Reflect)]
Expand Down Expand Up @@ -183,6 +189,10 @@ impl Default for EguiSettings {
#[derive(Component, Clone, Debug, Default, Deref, DerefMut)]
pub struct EguiInput(pub egui::RawInput);

/// Is used to store Egui context output.
#[derive(Component, Clone, Default, Deref, DerefMut)]
pub struct EguiFullOutput(pub Option<egui::FullOutput>);

/// A resource for accessing clipboard.
///
/// The resource is available only if `manage_clipboard` feature is enabled.
Expand Down Expand Up @@ -714,12 +724,17 @@ impl Plugin for EguiPlugin {
.after(InputSystem)
.after(EguiSet::InitContexts),
);
app.add_systems(
PreUpdate,
begin_frame_system
.in_set(EguiSet::BeginFrame)
.after(EguiSet::ProcessInput),
);

if !self.manual_run {
app.add_systems(
PreUpdate,
begin_pass_system
.in_set(EguiSet::BeginFrame)
.after(EguiSet::ProcessInput),
);
app.add_systems(PostUpdate, end_pass_system.before(EguiSet::ProcessOutput));
}

app.add_systems(
PostUpdate,
process_output_system.in_set(EguiSet::ProcessOutput),
Expand Down Expand Up @@ -789,6 +804,8 @@ pub struct EguiContextQuery {
pub ctx: &'static mut EguiContext,
/// Encapsulates [`egui::RawInput`].
pub egui_input: &'static mut EguiInput,
/// Encapsulates [`egui::FullOutput`].
pub egui_full_output: &'static mut EguiFullOutput,
/// Egui shapes and textures delta.
pub render_output: &'static mut EguiRenderOutput,
/// Encapsulates [`egui::PlatformOutput`].
Expand Down Expand Up @@ -826,6 +843,7 @@ pub fn setup_new_windows_system(
EguiContext::default(),
EguiRenderOutput::default(),
EguiInput::default(),
EguiFullOutput::default(),
EguiOutput::default(),
RenderTargetSize::default(),
));
Expand All @@ -848,6 +866,7 @@ pub fn setup_render_to_texture_handles_system(
EguiContext::default(),
EguiRenderOutput::default(),
EguiInput::default(),
EguiFullOutput::default(),
EguiOutput::default(),
RenderTargetSize::default(),
));
Expand Down Expand Up @@ -978,7 +997,7 @@ mod tests {
.build()
.disable::<WinitPlugin>(),
)
.add_plugins(EguiPlugin)
.add_plugins(EguiPlugin::default())
.update();
}
}
19 changes: 15 additions & 4 deletions src/systems.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[cfg(feature = "render")]
use crate::EguiRenderToTextureHandle;
use crate::{
EguiContext, EguiContextQuery, EguiContextQueryItem, EguiInput, EguiSettings, RenderTargetSize,
EguiContext, EguiContextQuery, EguiContextQueryItem, EguiFullOutput, EguiInput, EguiSettings,
RenderTargetSize,
};

#[cfg(feature = "render")]
Expand Down Expand Up @@ -491,9 +492,16 @@ pub fn update_contexts_system(
}

/// Marks frame start for Egui.
pub fn begin_frame_system(mut contexts: Query<(&mut EguiContext, &mut EguiInput)>) {
pub fn begin_pass_system(mut contexts: Query<(&mut EguiContext, &mut EguiInput)>) {
for (mut ctx, mut egui_input) in contexts.iter_mut() {
ctx.get_mut().begin_frame(egui_input.take());
ctx.get_mut().begin_pass(egui_input.take());
}
}

/// Marks frame end for Egui.
pub fn end_pass_system(mut contexts: Query<(&mut EguiContext, &mut EguiFullOutput)>) {
for (mut ctx, mut full_output) in contexts.iter_mut() {
**full_output = Some(ctx.get_mut().end_pass());
}
}

Expand All @@ -513,7 +521,10 @@ pub fn process_output_system(

for mut context in contexts.iter_mut() {
let ctx = context.ctx.get_mut();
let full_output = ctx.end_frame();
let Some(full_output) = context.egui_full_output.0.take() else {
log::error!("bevy_egui frame output has not been prepared!");
continue;
};
let egui::FullOutput {
platform_output,
shapes,
Expand Down

0 comments on commit 354ced5

Please sign in to comment.