Skip to content

Commit 01c2cec

Browse files
authored
update bevy_egui to 0.35, bump bevy to 0.16.1 (#115)
1 parent af1a54a commit 01c2cec

File tree

4 files changed

+57
-42
lines changed

4 files changed

+57
-42
lines changed

Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ readme = "README.md"
1515
bevy_egui = ["dep:bevy_egui"]
1616

1717
[dependencies]
18-
bevy = { version = "0.16", default-features = false, features = [
18+
bevy = { version = "0.16.1", default-features = false, features = [
1919
"bevy_core_pipeline",
2020
"bevy_render",
2121
"bevy_window",
2222
] }
23-
bevy_egui = { version = "0.34", optional = true, default-features = false }
23+
bevy_egui = { version = "0.35", optional = true, default-features = false }
2424

2525
[dev-dependencies]
26-
bevy = { version = "0.16" }
26+
bevy = { version = "0.16.1" }
2727
float-cmp = "0.10.0"
28-
bevy_egui = { version = "0.34", default-features = false, features = [
28+
bevy_egui = { version = "0.35", default-features = false, features = [
2929
"render",
3030
"default_fonts",
3131
] }

examples/egui.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,16 @@
22
//! egui windows
33
44
use bevy::prelude::*;
5-
use bevy_egui::{egui, EguiContexts, EguiPlugin};
5+
use bevy_egui::{egui, EguiContexts, EguiPlugin, EguiPrimaryContextPass};
66
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
77

88
fn main() {
99
let mut app = App::new();
1010
app.add_plugins(DefaultPlugins)
11-
.add_plugins(EguiPlugin {
12-
enable_multipass_for_primary_context: false,
13-
})
11+
.add_plugins(EguiPlugin::default())
1412
.add_plugins(PanOrbitCameraPlugin)
1513
.add_systems(Startup, setup)
16-
.add_systems(Update, ui_example_system);
14+
.add_systems(EguiPrimaryContextPass, ui_example_system);
1715

1816
app.run();
1917
}
@@ -49,20 +47,21 @@ fn setup(
4947
));
5048
}
5149

52-
fn ui_example_system(mut contexts: EguiContexts) {
50+
fn ui_example_system(mut contexts: EguiContexts) -> Result {
5351
egui::SidePanel::left("left_panel")
5452
.resizable(true)
55-
.show(contexts.ctx_mut(), |ui| {
53+
.show(contexts.ctx_mut()?, |ui| {
5654
ui.label("Left resizeable panel");
5755
});
5856

59-
egui::Window::new("Movable Window").show(contexts.ctx_mut(), |ui| {
57+
egui::Window::new("Movable Window").show(contexts.ctx_mut()?, |ui| {
6058
ui.label("Hello world");
6159
});
6260

6361
egui::Window::new("Immovable Window")
6462
.movable(false)
65-
.show(contexts.ctx_mut(), |ui| {
63+
.show(contexts.ctx_mut()?, |ui| {
6664
ui.label("Hello world");
6765
});
66+
Ok(())
6867
}

examples/egui_multiple_windows.rs

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,26 @@
22
//! This is a combination of the egui and multiple_windows examples, and doesn't show anything new,
33
//! it's primarily here for easy e2e testing.
44
5-
use bevy::prelude::*;
65
use bevy::render::camera::RenderTarget;
76
use bevy::window::WindowRef;
8-
use bevy_egui::{egui, EguiContexts, EguiPlugin};
7+
use bevy::{ecs::schedule::ScheduleLabel, prelude::*};
8+
use bevy_egui::{
9+
egui, EguiContext, EguiMultipassSchedule, EguiPlugin, EguiPrimaryContextPass,
10+
PrimaryEguiContext,
11+
};
912
use bevy_panorbit_camera::{PanOrbitCamera, PanOrbitCameraPlugin};
1013

14+
#[derive(ScheduleLabel, Clone, Debug, PartialEq, Eq, Hash)]
15+
pub struct SecondWindowContextPass;
16+
1117
fn main() {
1218
let mut app = App::new();
1319
app.add_plugins(DefaultPlugins)
14-
.add_plugins(EguiPlugin {
15-
enable_multipass_for_primary_context: false,
16-
})
20+
.add_plugins(EguiPlugin::default())
1721
.add_plugins(PanOrbitCameraPlugin)
1822
.add_systems(Startup, setup)
19-
.add_systems(Update, ui_example_system);
23+
.add_systems(EguiPrimaryContextPass, ui_example_system_first_window)
24+
.add_systems(SecondWindowContextPass, ui_example_system_second_window);
2025

2126
app.run();
2227
}
@@ -49,6 +54,7 @@ fn setup(
4954
commands.spawn((
5055
Transform::from_translation(Vec3::new(0.0, 1.5, 5.0)),
5156
PanOrbitCamera::default(),
57+
PrimaryEguiContext,
5258
));
5359

5460
// Spawn a second window
@@ -67,13 +73,24 @@ fn setup(
6773
},
6874
Transform::from_translation(Vec3::new(5.0, 1.5, 7.0)),
6975
PanOrbitCamera::default(),
76+
EguiMultipassSchedule::new(SecondWindowContextPass),
7077
));
7178
}
7279

73-
fn ui_example_system(mut contexts: EguiContexts, windows: Query<Entity, With<Window>>) {
74-
for window in windows.iter() {
75-
egui::Window::new("Hello").show(contexts.ctx_for_entity_mut(window), |ui| {
76-
ui.label("world");
77-
});
78-
}
80+
fn ui_example_system_first_window(
81+
mut egui_ctx: Single<&mut EguiContext, With<PrimaryEguiContext>>,
82+
) -> Result {
83+
egui::Window::new("Hello").show(egui_ctx.get_mut(), |ui| {
84+
ui.label("world");
85+
});
86+
Ok(())
87+
}
88+
89+
fn ui_example_system_second_window(
90+
mut egui_ctx: Single<&mut EguiContext, Without<PrimaryEguiContext>>,
91+
) -> Result {
92+
egui::Window::new("Hello").show(egui_ctx.get_mut(), |ui| {
93+
ui.label("world2");
94+
});
95+
Ok(())
7996
}

src/egui.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bevy::prelude::*;
2+
use bevy_egui::EguiContext;
23

34
/// A resource that tracks whether egui wants focus on the current and previous frames,
45
/// in order to determine whether PanOrbitCamera should react to input events.
@@ -29,29 +30,27 @@ pub struct EguiWantsFocus {
2930
pub struct EguiFocusIncludesHover(pub bool);
3031

3132
pub fn check_egui_wants_focus(
32-
mut contexts: bevy_egui::EguiContexts,
33+
mut contexts: Query<&mut EguiContext>,
3334
mut wants_focus: ResMut<EguiWantsFocus>,
3435
include_hover: Res<EguiFocusIncludesHover>,
35-
windows: Query<Entity, With<Window>>,
36-
) {
37-
// The window that the user is interacting with and the window that contains the egui context
38-
// that the user is interacting with are always going to be the same. Therefore, we can assume
39-
// that if any of the egui contexts want focus, then it must be the one that the user is
40-
// interacting with.
41-
let new_wants_focus = windows.iter().any(|window| {
42-
if let Some(ctx) = contexts.try_ctx_for_entity_mut(window) {
43-
let mut value = ctx.wants_pointer_input() || ctx.wants_keyboard_input();
44-
if include_hover.0 {
45-
value |= ctx.is_pointer_over_area()
46-
}
47-
value
48-
} else {
49-
false
36+
) -> Result {
37+
// Check all egui contexts to see if any of them want focus. If any context wants focus,
38+
// we assume that's the one the user is interacting with and prevent camera input.
39+
let mut new_wants_focus = false;
40+
for mut context in contexts.iter_mut() {
41+
let context = context.get_mut();
42+
let mut context_wants_focus =
43+
context.wants_pointer_input() || context.wants_keyboard_input();
44+
if include_hover.0 {
45+
context_wants_focus |= context.is_pointer_over_area();
5046
}
51-
});
47+
new_wants_focus |= context_wants_focus;
48+
}
49+
5250
let new_res = EguiWantsFocus {
5351
prev: wants_focus.curr,
5452
curr: new_wants_focus,
5553
};
5654
wants_focus.set_if_neq(new_res);
55+
Ok(())
5756
}

0 commit comments

Comments
 (0)