Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update for bevy 0.10 #148

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ default_fonts = ["egui/default_fonts"]
serde = ["egui/serde"]

[dependencies]
bevy = { version = "0.9.0", default-features = false, features = ["bevy_render", "bevy_core_pipeline", "bevy_asset"] }
bevy = { version = "0.10", default-features = false, features = ["bevy_render", "bevy_core_pipeline", "bevy_asset"] }
egui = { version = "0.21.0", default-features = false, features = ["bytemuck"] }
webbrowser = { version = "0.8.2", optional = true }

Expand All @@ -33,7 +33,7 @@ thread_local = { version = "1.1.0", optional = true }
[dev-dependencies]
once_cell = "1.16.0"
version-sync = "0.9.4"
bevy = { version = "0.9.0", default-features = false, features = [
bevy = { version = "0.10", default-features = false, features = [
"x11",
"png",
"bevy_pbr",
Expand Down
14 changes: 8 additions & 6 deletions examples/render_to_image_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use bevy::{
view::RenderLayers,
},
};
use bevy_egui::{egui, EguiContext, EguiPlugin};
use bevy_egui::{egui, EguiContext, EguiPlugin, EguiUserTextures};
use egui::Widget;

fn main() {
Expand All @@ -34,7 +34,7 @@ struct MainPassCube;
struct CubePreviewImage(Handle<Image>);

fn setup(
mut egui_ctx: ResMut<EguiContext>,
mut egui_ctx: ResMut<EguiUserTextures>,
mut commands: Commands,
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
Expand All @@ -58,6 +58,7 @@ fn setup(
usage: TextureUsages::TEXTURE_BINDING
| TextureUsages::COPY_DST
| TextureUsages::RENDER_ATTACHMENT,
view_formats: &[],
},
..default()
};
Expand Down Expand Up @@ -107,7 +108,7 @@ fn setup(
},
camera: Camera {
// render before the "main pass" camera
priority: -1,
order: -1,
target: RenderTarget::Image(image_handle),
..default()
},
Expand Down Expand Up @@ -145,17 +146,18 @@ fn setup(
}

fn render_to_image_example_system(
mut egui_ctx: ResMut<EguiContext>,
egui_user_textures: Res<EguiUserTextures>,
cube_preview_image: Res<CubePreviewImage>,
preview_cube_query: Query<&Handle<StandardMaterial>, With<PreviewPassCube>>,
main_cube_query: Query<&Handle<StandardMaterial>, With<MainPassCube>>,
mut materials: ResMut<Assets<StandardMaterial>>,
egui_ctx: Query<&EguiContext, With<Window>>,
) {
let cube_preview_texture_id = egui_ctx.image_id(&cube_preview_image).unwrap();
let cube_preview_texture_id = egui_user_textures.image_id(&cube_preview_image).unwrap();
let preview_material_handle = preview_cube_query.single();
let preview_material = materials.get_mut(preview_material_handle).unwrap();

let ctx = egui_ctx.ctx_mut();
let ctx = egui_ctx.iter().next().unwrap();
let mut apply = false;
egui::Window::new("Cube material preview").show(ctx, |ui| {
ui.image(cube_preview_texture_id, [300.0, 300.0]);
Expand Down
21 changes: 13 additions & 8 deletions examples/side_panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,38 @@ fn main() {
}

fn ui_example_system(
mut egui_context: ResMut<EguiContext>,
egui_ctx: Query<&EguiContext>,
mut occupied_screen_space: ResMut<OccupiedScreenSpace>,
) {
let ctx = egui_ctx.iter().next().unwrap();

occupied_screen_space.left = egui::SidePanel::left("left_panel")
.resizable(true)
.show(egui_context.ctx_mut(), |ui| {
.show(ctx, |ui| {
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.width();
occupied_screen_space.right = egui::SidePanel::right("right_panel")
.resizable(true)
.show(egui_context.ctx_mut(), |ui| {
.show(ctx, |ui| {
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.width();
occupied_screen_space.top = egui::TopBottomPanel::top("top_panel")
.resizable(true)
.show(egui_context.ctx_mut(), |ui| {
.show(ctx, |ui| {
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
.rect
.height();
occupied_screen_space.bottom = egui::TopBottomPanel::bottom("bottom_panel")
.resizable(true)
.show(egui_context.ctx_mut(), |ui| {
.show(ctx, |ui| {
ui.allocate_rect(ui.available_rect_before_wrap(), egui::Sense::hover());
})
.response
Expand All @@ -69,7 +71,10 @@ fn setup_system(
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
mesh: meshes.add(Mesh::from(shape::Plane {
size: 5.0,
subdivisions: 0,
})),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
});
Expand Down Expand Up @@ -103,7 +108,7 @@ fn setup_system(
fn update_camera_transform_system(
occupied_screen_space: Res<OccupiedScreenSpace>,
original_camera_transform: Res<OriginalCameraTransform>,
windows: Res<Windows>,
windows: Query<&Window>,
mut camera_query: Query<(&Projection, &mut Transform)>,
) {
let (camera_projection, mut transform) = match camera_query.get_single_mut() {
Expand All @@ -115,7 +120,7 @@ fn update_camera_transform_system(
let frustum_height = 2.0 * distance_to_target * (camera_projection.fov * 0.5).tan();
let frustum_width = frustum_height * camera_projection.aspect_ratio;

let window = windows.get_primary().unwrap();
let window = windows.iter().next().unwrap();

let left_taken = occupied_screen_space.left / window.width();
let right_taken = occupied_screen_space.right / window.width();
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fn main() {
.run();
}

fn ui_example_system(mut egui_context: ResMut<EguiContext>) {
egui::Window::new("Hello").show(egui_context.ctx_mut(), |ui| {
fn ui_example_system(egui_ctx: Query<&EguiContext>) {
egui::Window::new("Hello").show(egui_ctx.iter().next().unwrap(), |ui| {
ui.label("world");
});
}
49 changes: 23 additions & 26 deletions examples/two_windows.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
use bevy::{
prelude::*,
render::camera::RenderTarget,
window::{CreateWindow, PresentMode, WindowId},
window::{PresentMode, WindowRef, WindowResolution},
};
use bevy_egui::{EguiContext, EguiPlugin};
use once_cell::sync::Lazy;

static SECOND_WINDOW_ID: Lazy<WindowId> = Lazy::new(WindowId::new);
use bevy_egui::{EguiContext, EguiPlugin, EguiUserTextures};

#[derive(Resource)]
struct Images {
Expand All @@ -26,25 +23,21 @@ fn main() {
app.run();
}

fn create_new_window_system(
mut create_window_events: EventWriter<CreateWindow>,
mut commands: Commands,
) {
// sends out a "CreateWindow" event, which will be received by the windowing backend
create_window_events.send(CreateWindow {
id: *SECOND_WINDOW_ID,
descriptor: WindowDescriptor {
width: 800.,
height: 600.,
fn create_new_window_system(mut commands: Commands) {
// Spawn a second window
let second_window_id = commands
.spawn(Window {
title: "Second window".to_owned(),
resolution: WindowResolution::new(800.0, 600.0),
present_mode: PresentMode::AutoVsync,
title: "Second window".to_string(),
..Default::default()
},
});
})
.id();

// second window camera
commands.spawn(Camera3dBundle {
camera: Camera {
target: RenderTarget::Window(*SECOND_WINDOW_ID),
target: RenderTarget::Window(WindowRef::Entity(second_window_id)),
..Default::default()
},
transform: Transform::from_xyz(6.0, 0.0, 0.0).looking_at(Vec3::ZERO, Vec3::Y),
Expand All @@ -69,15 +62,16 @@ struct SharedUiState {
}

fn ui_first_window_system(
mut egui_context: ResMut<EguiContext>,
mut egui_user_textures: ResMut<EguiUserTextures>,
mut ui_state: Local<UiState>,
mut shared_ui_state: ResMut<SharedUiState>,
images: Res<Images>,
egui_ctx: Query<&EguiContext>,
) {
let bevy_texture_id = egui_context.add_image(images.bevy_icon.clone_weak());
let bevy_texture_id = egui_user_textures.add_image(images.bevy_icon.clone_weak());
egui::Window::new("First Window")
.vscroll(true)
.show(egui_context.ctx_mut(), |ui| {
.show(egui_ctx.iter().next().unwrap(), |ui| {
ui.horizontal(|ui| {
ui.label("Write something: ");
ui.text_edit_singleline(&mut ui_state.input);
Expand All @@ -92,15 +86,18 @@ fn ui_first_window_system(
}

fn ui_second_window_system(
mut egui_context: ResMut<EguiContext>,
mut egui_user_textures: ResMut<EguiUserTextures>,
mut ui_state: Local<UiState>,
mut shared_ui_state: ResMut<SharedUiState>,
images: Res<Images>,
egui_ctx: Query<&EguiContext>,
) {
let bevy_texture_id = egui_context.add_image(images.bevy_icon.clone_weak());
let ctx = match egui_context.try_ctx_for_window_mut(*SECOND_WINDOW_ID) {
let bevy_texture_id = egui_user_textures.add_image(images.bevy_icon.clone_weak());
let ctx = match egui_ctx.iter().nth(1) {
Some(ctx) => ctx,
None => return,
None => {
return;
}
};
egui::Window::new("Second Window")
.vscroll(true)
Expand Down
39 changes: 22 additions & 17 deletions examples/ui.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use bevy::prelude::*;
use bevy_egui::{egui, EguiContext, EguiPlugin, EguiSettings};
use bevy_egui::{egui, EguiContext, EguiPlugin, EguiSettings, EguiUserTextures};

struct Images {
bevy_icon: Handle<Image>,
Expand All @@ -23,7 +23,7 @@ impl FromWorld for Images {
fn main() {
App::new()
.insert_resource(ClearColor(Color::rgb(0.0, 0.0, 0.0)))
.insert_resource(Msaa { samples: 4 })
.insert_resource(Msaa::Sample4)
.init_resource::<UiState>()
.add_plugins(DefaultPlugins)
.add_plugin(EguiPlugin)
Expand All @@ -43,8 +43,8 @@ struct UiState {
is_window_open: bool,
}

fn configure_visuals_system(mut egui_ctx: ResMut<EguiContext>) {
egui_ctx.ctx_mut().set_visuals(egui::Visuals {
fn configure_visuals_system(egui_ctx: Query<&EguiContext>) {
egui_ctx.iter().next().unwrap().set_visuals(egui::Visuals {
window_rounding: 0.0.into(),
..Default::default()
});
Expand All @@ -58,12 +58,12 @@ fn update_ui_scale_factor_system(
keyboard_input: Res<Input<KeyCode>>,
mut toggle_scale_factor: Local<Option<bool>>,
mut egui_settings: ResMut<EguiSettings>,
windows: Res<Windows>,
windows: Query<&Window>,
) {
if keyboard_input.just_pressed(KeyCode::Slash) || toggle_scale_factor.is_none() {
*toggle_scale_factor = Some(!toggle_scale_factor.unwrap_or(true));

if let Some(window) = windows.get_primary() {
if let Some(window) = windows.iter().next() {
let scale_factor = if toggle_scale_factor.unwrap() {
1.0
} else {
Expand All @@ -75,7 +75,7 @@ fn update_ui_scale_factor_system(
}

fn ui_example_system(
mut egui_ctx: ResMut<EguiContext>,
mut egui_user_textures: ResMut<EguiUserTextures>,
mut ui_state: ResMut<UiState>,
// You are not required to store Egui texture ids in systems. We store this one here just to
// demonstrate that rendering by using a texture id of a removed image is handled without
Expand All @@ -85,11 +85,13 @@ fn ui_example_system(
// If you need to access the ids from multiple systems, you can also initialize the `Images`
// resource while building the app and use `Res<Images>` instead.
images: Local<Images>,
egui_ctx: Query<&EguiContext>,
) {
let ctx = egui_ctx.iter().next().unwrap();
let egui_texture_handle = ui_state
.egui_texture_handle
.get_or_insert_with(|| {
egui_ctx.ctx_mut().load_texture(
ctx.load_texture(
"example-image",
egui::ColorImage::example(),
Default::default(),
Expand All @@ -103,12 +105,14 @@ fn ui_example_system(

if !*is_initialized {
*is_initialized = true;
*rendered_texture_id = egui_ctx.add_image(images.bevy_icon.clone_weak());
*rendered_texture_id = egui_user_textures.add_image(images.bevy_icon.clone_weak());
}

let ctx = egui_ctx.iter().next().unwrap();

egui::SidePanel::left("side_panel")
.default_width(200.0)
.show(egui_ctx.ctx_mut(), |ui| {
.show(ctx, |ui| {
ui.heading("Side Panel");

ui.horizontal(|ui| {
Expand Down Expand Up @@ -149,7 +153,7 @@ fn ui_example_system(
});
});

egui::TopBottomPanel::top("top_panel").show(egui_ctx.ctx_mut(), |ui| {
egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
// The top panel is often a good place for a menu bar:
egui::menu::bar(ui, |ui| {
egui::menu::menu_button(ui, "File", |ui| {
Expand All @@ -160,7 +164,7 @@ fn ui_example_system(
});
});

egui::CentralPanel::default().show(egui_ctx.ctx_mut(), |ui| {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Egui Template");
ui.hyperlink("https://github.com/emilk/egui_template");
ui.add(egui::github_link_file_line!(
Expand All @@ -185,7 +189,7 @@ fn ui_example_system(
egui::Window::new("Window")
.vscroll(true)
.open(&mut ui_state.is_window_open)
.show(egui_ctx.ctx_mut(), |ui| {
.show(ctx, |ui| {
ui.label("Windows can be moved by dragging them.");
ui.label("They are automatically sized based on contents.");
ui.label("You can turn on resizing and scrolling if you like.");
Expand All @@ -198,14 +202,15 @@ fn ui_example_system(
if load || invert {
// If an image is already added to the context, it'll return an existing texture id.
if ui_state.inverted {
*rendered_texture_id = egui_ctx.add_image(images.bevy_icon_inverted.clone_weak());
*rendered_texture_id =
egui_user_textures.add_image(images.bevy_icon_inverted.clone_weak());
} else {
*rendered_texture_id = egui_ctx.add_image(images.bevy_icon.clone_weak());
*rendered_texture_id = egui_user_textures.add_image(images.bevy_icon.clone_weak());
};
}
if remove {
egui_ctx.remove_image(&images.bevy_icon);
egui_ctx.remove_image(&images.bevy_icon_inverted);
egui_user_textures.remove_image(&images.bevy_icon);
egui_user_textures.remove_image(&images.bevy_icon_inverted);
}
}

Expand Down
Loading