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 to bevy 0.9 dev #127

Merged
merged 4 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -21,7 +21,7 @@ open_url = ["webbrowser"]
default_fonts = ["egui/default_fonts"]

[dependencies]
bevy = { version = "0.8", default-features = false, features = ["bevy_render", "bevy_core_pipeline", "bevy_asset"] }
bevy = { git = "https://github.com/bevyengine/bevy", branch = "main", default-features = false, features = ["bevy_render", "bevy_core_pipeline", "bevy_asset"] }
mvlabat marked this conversation as resolved.
Show resolved Hide resolved
egui = { version = "0.19.0", default-features = false, features = ["bytemuck"] }
webbrowser = { version = "0.7", optional = true }

Expand All @@ -32,7 +32,7 @@ thread_local = { version = "1.1.0", optional = true }
[dev-dependencies]
once_cell = "1.9.0"
version-sync = "0.9.2"
bevy = { version = "0.8", default-features = false, features = [
bevy = { git = "https://github.com/bevyengine/bevy", branch = "main", default-features = false, features = [
mvlabat marked this conversation as resolved.
Show resolved Hide resolved
"x11",
"png",
"bevy_pbr",
Expand Down
12 changes: 6 additions & 6 deletions examples/render_to_image_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ struct PreviewPassCube;
#[derive(Component)]
struct MainPassCube;

#[derive(Deref)]
#[derive(Deref, Resource)]
struct CubePreviewImage(Handle<Image>);

fn setup(
Expand Down Expand Up @@ -83,7 +83,7 @@ fn setup(

// The cube that will be rendered to the texture.
commands
.spawn_bundle(PbrBundle {
.spawn(PbrBundle {
mesh: cube_handle,
material: preview_material_handle,
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 1.0)),
Expand All @@ -94,13 +94,13 @@ fn setup(

// Light
// NOTE: Currently lights are shared between passes - see https://github.com/bevyengine/bevy/issues/3462
commands.spawn_bundle(PointLightBundle {
commands.spawn(PointLightBundle {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 10.0)),
..default()
});

commands
.spawn_bundle(Camera3dBundle {
.spawn(Camera3dBundle {
camera_3d: Camera3d {
clear_color: ClearColorConfig::Custom(Color::rgba(1.0, 1.0, 1.0, 0.0)),
..default()
Expand All @@ -124,7 +124,7 @@ fn setup(

// Main pass cube.
commands
.spawn_bundle(PbrBundle {
.spawn(PbrBundle {
mesh: cube_handle,
material: main_material_handle,
transform: Transform {
Expand All @@ -137,7 +137,7 @@ fn setup(
.insert(MainPassCube);

// The main pass camera.
commands.spawn_bundle(Camera3dBundle {
commands.spawn(Camera3dBundle {
transform: Transform::from_translation(Vec3::new(0.0, 0.0, 15.0))
.looking_at(Vec3::default(), Vec3::Y),
..default()
Expand Down
11 changes: 6 additions & 5 deletions examples/side_panel.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use bevy::{prelude::*, render::camera::Projection};
use bevy_egui::{egui, EguiContext, EguiPlugin};

#[derive(Default)]
#[derive(Default, Resource)]
struct OccupiedScreenSpace {
left: f32,
top: f32,
Expand All @@ -11,6 +11,7 @@ struct OccupiedScreenSpace {

const CAMERA_TARGET: Vec3 = Vec3::ZERO;

#[derive(Resource)]
struct OriginalCameraTransform(Transform);

fn main() {
Expand Down Expand Up @@ -67,18 +68,18 @@ fn setup_system(
mut meshes: ResMut<Assets<Mesh>>,
mut materials: ResMut<Assets<StandardMaterial>>,
) {
commands.spawn_bundle(PbrBundle {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Plane { size: 5.0 })),
material: materials.add(Color::rgb(0.3, 0.5, 0.3).into()),
..Default::default()
});
commands.spawn_bundle(PbrBundle {
commands.spawn(PbrBundle {
mesh: meshes.add(Mesh::from(shape::Cube { size: 1.0 })),
material: materials.add(Color::rgb(0.8, 0.7, 0.6).into()),
transform: Transform::from_xyz(0.0, 0.5, 0.0),
..Default::default()
});
commands.spawn_bundle(PointLightBundle {
commands.spawn(PointLightBundle {
point_light: PointLight {
intensity: 1500.0,
shadows_enabled: true,
Expand All @@ -93,7 +94,7 @@ fn setup_system(
Transform::from_translation(camera_pos).looking_at(CAMERA_TARGET, Vec3::Y);
commands.insert_resource(OriginalCameraTransform(camera_transform));

commands.spawn_bundle(Camera3dBundle {
commands.spawn(Camera3dBundle {
transform: camera_transform,
..Default::default()
});
Expand Down
5 changes: 3 additions & 2 deletions examples/two_windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use once_cell::sync::Lazy;

static SECOND_WINDOW_ID: Lazy<WindowId> = Lazy::new(WindowId::new);

#[derive(Resource)]
struct Images {
bevy_icon: Handle<Image>,
}
Expand Down Expand Up @@ -51,7 +52,7 @@ fn create_new_window(mut create_window_events: EventWriter<CreateWindow>, mut co
},
});
// second window camera
commands.spawn_bundle(Camera3dBundle {
commands.spawn(Camera3dBundle {
camera: Camera {
target: RenderTarget::Window(*SECOND_WINDOW_ID),
..Default::default()
Expand All @@ -72,7 +73,7 @@ struct UiState {
input: String,
}

#[derive(Default)]
#[derive(Default, Resource)]
struct SharedUiState {
shared_input: String,
}
Expand Down
2 changes: 1 addition & 1 deletion examples/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
.add_system(ui_example)
.run();
}
#[derive(Default)]
#[derive(Default, Resource)]
struct UiState {
label: String,
value: f32,
Expand Down
2 changes: 2 additions & 0 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::render_systems::{
use bevy::{
core::cast_slice,
ecs::world::{FromWorld, World},
prelude::Resource,
render::{
render_graph::{Node, NodeRunError, RenderGraphContext},
render_resource::{
Expand All @@ -26,6 +27,7 @@ use bevy::{
window::WindowId,
};

#[derive(Resource)]
pub struct EguiPipeline {
pipeline: RenderPipeline,

Expand Down
96 changes: 76 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ use arboard::Clipboard;
use bevy::{
app::{App, CoreStage, Plugin, StartupStage},
asset::{AssetEvent, Assets, Handle},
ecs::{
event::EventReader,
schedule::{ParallelSystemDescriptorCoercion, SystemLabel},
system::ResMut,
},
ecs::{event::EventReader, schedule::SystemLabel, system::ResMut},
input::InputSystem,
log,
prelude::{IntoSystemDescriptor, Resource},
render::{render_graph::RenderGraph, texture::Image, RenderApp, RenderStage},
utils::HashMap,
window::WindowId,
Expand All @@ -77,14 +74,15 @@ use egui_node::EguiNode;
use render_systems::EguiTransforms;
#[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))]
use std::cell::{RefCell, RefMut};
use std::ops::Deref;
#[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))]
use thread_local::ThreadLocal;

/// Adds all Egui resources and render graph nodes.
pub struct EguiPlugin;

/// A resource for storing global UI settings.
#[derive(Clone, Debug, PartialEq)]
#[derive(Clone, Debug, PartialEq, Resource)]
pub struct EguiSettings {
/// Global scale factor for egui widgets (`1.0` by default).
///
Expand Down Expand Up @@ -116,10 +114,58 @@ impl Default for EguiSettings {
}
}

/// Wrap the `WindowId` to `EguiRenderOutput` hashmap
#[derive(Resource)]
pub struct EguiRenderOutputContainer(pub HashMap<WindowId, EguiRenderOutput>);

impl Deref for EguiRenderOutputContainer {
type Target = HashMap<WindowId, EguiRenderOutput>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
Comment on lines +121 to +127
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can actually just use Bevy's Deref and DerefMut derives to achieve this.


/// Wrap the `WindowId` to `EguiInput` hashmap
#[derive(Resource)]
pub struct EguiRenderInputContainer(pub HashMap<WindowId, EguiInput>);

impl Deref for EguiRenderInputContainer {
type Target = HashMap<WindowId, EguiInput>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// Wrap the `WindowId` to `EguiOutputContainer` hashmap
#[derive(Resource)]
pub struct EguiOutputContainer(pub HashMap<WindowId, EguiOutput>);

impl Deref for EguiOutputContainer {
type Target = HashMap<WindowId, EguiOutput>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// Wrap the `WindowId` to `WindowSize` hashmap
#[derive(Resource)]
pub struct EguiWindowSizeContainer(pub HashMap<WindowId, WindowSize>);

impl Deref for EguiWindowSizeContainer {
type Target = HashMap<WindowId, WindowSize>;

fn deref(&self) -> &Self::Target {
&self.0
}
}

/// Is used for storing the input passed to Egui. The actual resource is `HashMap<WindowId, EguiInput>`.
///
/// It gets reset during the [`EguiSystem::ProcessInput`] system.
#[derive(Clone, Debug, Default)]
#[derive(Clone, Debug, Default, Resource)]
pub struct EguiInput {
/// Egui's raw input.
pub raw_input: egui::RawInput,
Expand All @@ -129,7 +175,7 @@ pub struct EguiInput {
///
/// The resource is available only if `manage_clipboard` feature is enabled.
#[cfg(feature = "manage_clipboard")]
#[derive(Default)]
#[derive(Default, Resource)]
pub struct EguiClipboard {
#[cfg(not(target_arch = "wasm32"))]
clipboard: ThreadLocal<Option<RefCell<Clipboard>>>,
Expand Down Expand Up @@ -198,7 +244,7 @@ impl EguiClipboard {
}

/// Is used for storing Egui shapes. The actual resource is `HashMap<WindowId, EguiShapes>`.
#[derive(Clone, Default, Debug)]
#[derive(Clone, Default, Debug, Resource)]
pub struct EguiRenderOutput {
/// Pairs of rectangles and paint commands.
///
Expand All @@ -210,14 +256,14 @@ pub struct EguiRenderOutput {
}

/// Is used for storing Egui output. The actual resource is `HashMap<WindowId, EguiOutput>`.
#[derive(Clone, Default)]
#[derive(Clone, Default, Resource)]
pub struct EguiOutput {
/// The field gets updated during the [`EguiSystem::ProcessOutput`] system in the [`CoreStage::PostUpdate`].
pub platform_output: egui::PlatformOutput,
}

/// A resource for storing `bevy_egui` context.
#[derive(Clone)]
#[derive(Clone, Resource)]
pub struct EguiContext {
ctx: HashMap<WindowId, egui::Context>,
user_textures: HashMap<Handle<Image>, u64>,
Expand Down Expand Up @@ -433,10 +479,19 @@ impl Plugin for EguiPlugin {
fn build(&self, app: &mut App) {
let world = &mut app.world;
world.insert_resource(EguiSettings::default());
world.insert_resource(HashMap::<WindowId, EguiInput>::default());
world.insert_resource(HashMap::<WindowId, EguiOutput>::default());
world.insert_resource(HashMap::<WindowId, WindowSize>::default());
world.insert_resource(HashMap::<WindowId, EguiRenderOutput>::default());
world.insert_resource(EguiRenderInputContainer(
HashMap::<WindowId, EguiInput>::default(),
));
Comment on lines +482 to +484
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just make all these new resources derive Default. Then you can just do EguiRenderInputContainer::default() (or even world.init_resource::<EguiRenderInputContainer>()).

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And will also make it possible to initialize resources if some users decide not to add the egui plugin, by adding the resources and systems manually (with an intention to replace some of them).

world.insert_resource(EguiOutputContainer(
HashMap::<WindowId, EguiOutput>::default(),
));
world.insert_resource(EguiWindowSizeContainer(
HashMap::<WindowId, WindowSize>::default(),
));
world.insert_resource(EguiRenderOutputContainer(HashMap::<
WindowId,
EguiRenderOutput,
>::default()));
world.insert_resource(EguiManagedTextures::default());
#[cfg(feature = "manage_clipboard")]
world.insert_resource(EguiClipboard::default());
Expand Down Expand Up @@ -490,21 +545,22 @@ impl Plugin for EguiPlugin {
}
}

#[derive(Default)]
#[derive(Default, Resource)]
pub(crate) struct EguiManagedTextures(HashMap<(WindowId, u64), EguiManagedTexture>);

#[derive(Resource)]
pub(crate) struct EguiManagedTexture {
handle: Handle<Image>,
/// Stored in full so we can do partial updates (which bevy doesn't support).
color_image: egui::ColorImage,
}

fn update_egui_textures(
mut egui_render_output: ResMut<HashMap<WindowId, EguiRenderOutput>>,
mut egui_render_output: ResMut<EguiRenderOutputContainer>,
mut egui_managed_textures: ResMut<EguiManagedTextures>,
mut image_assets: ResMut<Assets<Image>>,
) {
for (&window_id, egui_render_output) in egui_render_output.iter_mut() {
for (&window_id, egui_render_output) in egui_render_output.0.iter_mut() {
let set_textures = std::mem::take(&mut egui_render_output.textures_delta.set);

for (texture_id, image_delta) in set_textures {
Expand Down Expand Up @@ -553,12 +609,12 @@ fn update_egui_textures(

fn free_egui_textures(
mut egui_context: ResMut<EguiContext>,
mut egui_render_output: ResMut<HashMap<WindowId, EguiRenderOutput>>,
mut egui_render_output: ResMut<EguiRenderOutputContainer>,
mut egui_managed_textures: ResMut<EguiManagedTextures>,
mut image_assets: ResMut<Assets<Image>>,
mut image_events: EventReader<AssetEvent<Image>>,
) {
for (&window_id, egui_render_output) in egui_render_output.iter_mut() {
for (&window_id, egui_render_output) in egui_render_output.0.iter_mut() {
let free_textures = std::mem::take(&mut egui_render_output.textures_delta.free);
for texture_id in free_textures {
if let egui::TextureId::Managed(texture_id) = texture_id {
Expand Down
Loading