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 fffb626 commit 9a9a667
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 32 deletions.
12 changes: 5 additions & 7 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 Down Expand Up @@ -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<Handle<Image>, u64>,
last_texture_id: u64,
Expand Down Expand Up @@ -580,6 +580,8 @@ impl Plugin for EguiPlugin {
world.init_resource::<EguiMousePosition>();
world.insert_resource(TouchId::default());
app.add_plugins(ExtractResourcePlugin::<EguiSettings>::default());
app.add_plugins(ExtractResourcePlugin::<EguiUserTextures>::default());
app.add_plugins(ExtractResourcePlugin::<ExtractedEguiManagedTextures>::default());
app.add_plugins(ExtractComponentPlugin::<EguiContext>::default());
app.add_plugins(ExtractComponentPlugin::<WindowSize>::default());
app.add_plugins(ExtractComponentPlugin::<EguiRenderOutput>::default());
Expand Down Expand Up @@ -650,11 +652,7 @@ impl Plugin for EguiPlugin {
.init_resource::<EguiTransforms>()
.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,
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 @@ -21,6 +23,17 @@ use bevy::{
utils::HashMap,
};

/// 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 @@ -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<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 All @@ -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<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 @@ -176,7 +174,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 9a9a667

Please sign in to comment.