Skip to content

Commit

Permalink
Use ExtractResource trait for EguiManagedTextures and EguiUserTextures
Browse files Browse the repository at this point in the history
  • Loading branch information
TheButlah committed Sep 27, 2023
1 parent a9a47f1 commit 16776c3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 28 deletions.
8 changes: 5 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")))]
Expand All @@ -83,6 +83,7 @@ use bevy::{
Shader, SystemSet, With, Without,
},
render::{
extract_resource::{ExtractResource, ExtractResourcePlugin},
render_resource::{AddressMode, SamplerDescriptor, SpecializedRenderPipelines},
texture::{Image, ImageSampler},
ExtractSchedule, Render, RenderApp, RenderSet,
Expand Down Expand Up @@ -469,7 +470,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<Handle<Image>, u64>,
last_texture_id: u64,
Expand Down Expand Up @@ -577,6 +578,8 @@ impl Plugin for EguiPlugin {
world.init_resource::<EguiUserTextures>();
world.init_resource::<EguiMousePosition>();
world.insert_resource(TouchId::default());
app.add_plugins(ExtractResourcePlugin::<EguiUserTextures>::default());
app.add_plugins(ExtractResourcePlugin::<ExtractedEguiManagedTextures>::default());

app.add_systems(
PreStartup,
Expand Down Expand Up @@ -646,7 +649,6 @@ impl Plugin for EguiPlugin {
ExtractSchedule,
(
render_systems::extract_egui_render_data_system,
render_systems::extract_egui_textures_system,
render_systems::setup_new_windows_render_system,
)
.into_configs(),
Expand Down
48 changes: 23 additions & 25 deletions src/render_systems.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand All @@ -25,6 +27,17 @@ use bevy::{
#[derive(Resource, Deref, DerefMut, Default)]
pub struct ExtractedEguiSettings(pub EguiSettings);

/// The extracted version of [`EguiManagedTextures`].
#[derive(Debug, Resource)]
pub struct ExtractedEguiManagedTextures(pub HashMap<(Entity, u64), Handle<Image>>);
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 {
Expand All @@ -35,24 +48,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<Image>>,
pub egui_textures: Res<'w, ExtractedEguiManagedTextures>,
/// Maps Bevy managed texture handles to Egui user texture ids.
pub user_textures: HashMap<Handle<Image>, 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<Item = (EguiTextureId, HandleId)> + '_ {
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())),
)
Expand Down Expand Up @@ -92,23 +107,6 @@ pub fn extract_egui_render_data_system(
}
}

/// Extracts Egui textures.
pub fn extract_egui_textures_system(
mut commands: Commands,
egui_user_textures: Extract<Res<EguiUserTextures>>,
egui_managed_textures: Extract<Res<EguiManagedTextures>>,
) {
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 {
Expand Down Expand Up @@ -194,7 +192,7 @@ pub struct EguiTextureBindGroups(pub HashMap<EguiTextureId, BindGroup>);
/// Queues bind groups.
pub fn queue_bind_groups_system(
mut commands: Commands,
egui_textures: Res<ExtractedEguiTextures>,
egui_textures: ExtractedEguiTextures,
render_device: Res<RenderDevice>,
gpu_images: Res<RenderAssets<Image>>,
egui_pipeline: Res<EguiPipeline>,
Expand Down

0 comments on commit 16776c3

Please sign in to comment.