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

Use ExtractResource for Egui*Textures #210

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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 @@ -579,6 +579,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_plugins(ExtractResourcePlugin::<EguiSettings>::default());
app.add_plugins(ExtractComponentPlugin::<EguiContext>::default());
app.add_plugins(ExtractComponentPlugin::<WindowSize>::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
52 changes: 27 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,21 @@ use bevy::{
utils::HashMap,
};

/// Extracted Egui settings.
#[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 @@ -31,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 All @@ -74,23 +93,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 +178,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