diff --git a/src/lib.rs b/src/lib.rs index f5d5d8fd5..e1e04364b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,7 @@ pub use egui; use crate::{ egui_node::{EguiPipeline, EGUI_SHADER_HANDLE}, - render_systems::EguiTransforms, + render_systems::{EguiTransforms, ExtractedEguiManagedTextures}, systems::*, }; #[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))] @@ -471,7 +471,7 @@ impl<'w, 's> EguiContexts<'w, 's> { pub struct EguiMousePosition(pub Option<(Entity, egui::Vec2)>); /// A resource for storing `bevy_egui` user textures. -#[derive(Clone, Resource, Default)] +#[derive(Clone, Resource, Default, ExtractResource)] pub struct EguiUserTextures { textures: HashMap, u64>, last_texture_id: u64, @@ -580,6 +580,8 @@ impl Plugin for EguiPlugin { world.init_resource::(); world.insert_resource(TouchId::default()); app.add_plugins(ExtractResourcePlugin::::default()); + app.add_plugins(ExtractResourcePlugin::::default()); + app.add_plugins(ExtractResourcePlugin::::default()); app.add_plugins(ExtractComponentPlugin::::default()); app.add_plugins(ExtractComponentPlugin::::default()); app.add_plugins(ExtractComponentPlugin::::default()); @@ -650,11 +652,7 @@ impl Plugin for EguiPlugin { .init_resource::() .add_systems( ExtractSchedule, - ( - render_systems::setup_new_windows_render_system, - render_systems::extract_egui_textures_system, - ) - .into_configs(), + render_systems::setup_new_windows_render_system, ) .add_systems( Render, diff --git a/src/render_systems.rs b/src/render_systems.rs index eb2136b8f..fc3b6e6e0 100644 --- a/src/render_systems.rs +++ b/src/render_systems.rs @@ -4,8 +4,10 @@ use crate::{ }; use bevy::{ asset::HandleId, + ecs::system::SystemParam, prelude::*, render::{ + extract_resource::ExtractResource, render_asset::RenderAssets, render_graph::RenderGraph, render_resource::{ @@ -21,6 +23,17 @@ use bevy::{ utils::HashMap, }; +/// The extracted version of [`EguiManagedTextures`]. +#[derive(Debug, Resource)] +pub struct ExtractedEguiManagedTextures(pub HashMap<(Entity, u64), Handle>); +impl ExtractResource for ExtractedEguiManagedTextures { + type Source = EguiManagedTextures; + + fn extract_resource(source: &Self::Source) -> Self { + Self(source.iter().map(|(k, v)| (*k, v.handle.clone())).collect()) + } +} + /// Corresponds to Egui's [`egui::TextureId`]. #[derive(Debug, PartialEq, Eq, Hash)] pub enum EguiTextureId { @@ -31,24 +44,26 @@ pub enum EguiTextureId { } /// Extracted Egui textures. -#[derive(Resource, Default)] -pub struct ExtractedEguiTextures { +#[derive(SystemParam)] +pub struct ExtractedEguiTextures<'w> { /// Maps Egui managed texture ids to Bevy image handles. - pub egui_textures: HashMap<(Entity, u64), Handle>, + pub egui_textures: Res<'w, ExtractedEguiManagedTextures>, /// Maps Bevy managed texture handles to Egui user texture ids. - pub user_textures: HashMap, u64>, + pub user_textures: Res<'w, EguiUserTextures>, } -impl ExtractedEguiTextures { +impl ExtractedEguiTextures<'_> { /// Returns an iterator over all textures (both Egui and Bevy managed). pub fn handles(&self) -> impl Iterator + '_ { self.egui_textures + .0 .iter() - .map(|(&(window, texture_id), handle)| { - (EguiTextureId::Managed(window, texture_id), handle.id()) + .map(|(&(window, texture_id), managed_tex)| { + (EguiTextureId::Managed(window, texture_id), managed_tex.id()) }) .chain( self.user_textures + .textures .iter() .map(|(handle, id)| (EguiTextureId::User(*id), handle.id())), ) @@ -74,23 +89,6 @@ pub fn setup_new_windows_render_system( } } -/// Extracts Egui textures. -pub fn extract_egui_textures_system( - mut commands: Commands, - egui_user_textures: Extract>, - egui_managed_textures: Extract>, -) { - commands.insert_resource(ExtractedEguiTextures { - egui_textures: egui_managed_textures - .iter() - .map(|(&(window_id, texture_id), managed_texture)| { - ((window_id, texture_id), managed_texture.handle.clone()) - }) - .collect(), - user_textures: egui_user_textures.textures.clone(), - }); -} - /// Describes the transform buffer. #[derive(Resource, Default)] pub struct EguiTransforms { @@ -176,7 +174,7 @@ pub struct EguiTextureBindGroups(pub HashMap); /// Queues bind groups. pub fn queue_bind_groups_system( mut commands: Commands, - egui_textures: Res, + egui_textures: ExtractedEguiTextures, render_device: Res, gpu_images: Res>, egui_pipeline: Res,