From cf241f1fc4b11007cdd2c0a908f25fbfc5ee9cd1 Mon Sep 17 00:00:00 2001 From: Ryan Butler Date: Sat, 7 Oct 2023 17:20:31 +0200 Subject: [PATCH] Simplify extraction by using derive (#211) --- src/egui_node.rs | 5 ++--- src/lib.rs | 17 +++++++++++------ src/render_systems.rs | 22 ++-------------------- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/src/egui_node.rs b/src/egui_node.rs index bb7a8c644..ebe0b67c9 100644 --- a/src/egui_node.rs +++ b/src/egui_node.rs @@ -1,9 +1,8 @@ use crate::{ render_systems::{ EguiPipelines, EguiTextureBindGroups, EguiTextureId, EguiTransform, EguiTransforms, - ExtractedEguiSettings, }, - EguiRenderOutput, WindowSize, + EguiRenderOutput, EguiSettings, WindowSize, }; use bevy::{ core::cast_slice, @@ -199,7 +198,7 @@ impl Node for EguiNode { let window_size = *window_size; let paint_jobs = std::mem::take(&mut render_output.paint_jobs); - let egui_settings = &world.get_resource::().unwrap(); + let egui_settings = &world.get_resource::().unwrap(); let render_device = world.get_resource::().unwrap(); diff --git a/src/lib.rs b/src/lib.rs index 8f131a9ca..f5d5d8fd5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -83,6 +83,8 @@ use bevy::{ Shader, SystemSet, With, Without, }, render::{ + extract_component::{ExtractComponent, ExtractComponentPlugin}, + extract_resource::{ExtractResource, ExtractResourcePlugin}, render_resource::{AddressMode, SamplerDescriptor, SpecializedRenderPipelines}, texture::{Image, ImageSampler}, ExtractSchedule, Render, RenderApp, RenderSet, @@ -100,7 +102,7 @@ use thread_local::ThreadLocal; pub struct EguiPlugin; /// A resource for storing global UI settings. -#[derive(Clone, Debug, Resource)] +#[derive(Clone, Debug, Resource, ExtractResource)] pub struct EguiSettings { /// Global scale factor for Egui widgets (`1.0` by default). /// @@ -255,7 +257,7 @@ impl EguiClipboard { } /// Is used for storing Egui shapes and textures delta. -#[derive(Component, Clone, Default, Debug, Resource)] +#[derive(Component, Clone, Default, Debug, ExtractComponent)] pub struct EguiRenderOutput { /// Pairs of rectangles and paint commands. /// @@ -274,7 +276,7 @@ pub struct EguiOutput { } /// A component for storing `bevy_egui` context. -#[derive(Clone, Component, Default)] +#[derive(Clone, Component, Default, ExtractComponent)] pub struct EguiContext(egui::Context); impl EguiContext { @@ -510,7 +512,7 @@ impl EguiUserTextures { } /// Stores physical size and scale factor, is used as a helper to calculate logical size. -#[derive(Component, Debug, Default, Clone, Copy, PartialEq)] +#[derive(Component, Debug, Default, Clone, Copy, PartialEq, ExtractComponent)] pub struct WindowSize { physical_width: f32, physical_height: f32, @@ -577,6 +579,10 @@ impl Plugin for EguiPlugin { world.init_resource::(); world.init_resource::(); world.insert_resource(TouchId::default()); + app.add_plugins(ExtractResourcePlugin::::default()); + app.add_plugins(ExtractComponentPlugin::::default()); + app.add_plugins(ExtractComponentPlugin::::default()); + app.add_plugins(ExtractComponentPlugin::::default()); app.add_systems( PreStartup, @@ -645,9 +651,8 @@ impl Plugin for EguiPlugin { .add_systems( ExtractSchedule, ( - render_systems::extract_egui_render_data_system, - render_systems::extract_egui_textures_system, render_systems::setup_new_windows_render_system, + render_systems::extract_egui_textures_system, ) .into_configs(), ) diff --git a/src/render_systems.rs b/src/render_systems.rs index 51e2a7911..eb2136b8f 100644 --- a/src/render_systems.rs +++ b/src/render_systems.rs @@ -1,6 +1,6 @@ use crate::{ egui_node::{EguiNode, EguiPipeline, EguiPipelineKey}, - EguiContextQueryReadOnly, EguiManagedTextures, EguiSettings, EguiUserTextures, WindowSize, + EguiManagedTextures, EguiSettings, EguiUserTextures, WindowSize, }; use bevy::{ asset::HandleId, @@ -21,10 +21,6 @@ use bevy::{ utils::HashMap, }; -/// Extracted Egui settings. -#[derive(Resource, Deref, DerefMut, Default)] -pub struct ExtractedEguiSettings(pub EguiSettings); - /// Corresponds to Egui's [`egui::TextureId`]. #[derive(Debug, PartialEq, Eq, Hash)] pub enum EguiTextureId { @@ -78,20 +74,6 @@ pub fn setup_new_windows_render_system( } } -/// Extracts Egui context, render output, settings and application window sizes. -pub fn extract_egui_render_data_system( - mut commands: Commands, - egui_settings: Extract>, - contexts: Extract>, -) { - commands.insert_resource(ExtractedEguiSettings(egui_settings.clone())); - for context in contexts.iter() { - commands - .get_or_spawn(context.window_entity) - .insert((*context.window_size, context.render_output.clone())); - } -} - /// Extracts Egui textures. pub fn extract_egui_textures_system( mut commands: Commands, @@ -147,7 +129,7 @@ impl EguiTransform { pub fn prepare_egui_transforms_system( mut egui_transforms: ResMut, window_sizes: Query<(Entity, &WindowSize)>, - egui_settings: Res, + egui_settings: Res, render_device: Res, render_queue: Res,