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

Simplify extraction by using derive #211

Merged
merged 1 commit 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
5 changes: 2 additions & 3 deletions src/egui_node.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
render_systems::{
EguiPipelines, EguiTextureBindGroups, EguiTextureId, EguiTransform, EguiTransforms,
ExtractedEguiSettings,
},
EguiRenderOutput, WindowSize,
EguiRenderOutput, EguiSettings, WindowSize,
};
use bevy::{
core::cast_slice,
Expand Down Expand Up @@ -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::<ExtractedEguiSettings>().unwrap();
let egui_settings = &world.get_resource::<EguiSettings>().unwrap();

let render_device = world.get_resource::<RenderDevice>().unwrap();

Expand Down
17 changes: 11 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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).
///
Expand Down Expand Up @@ -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.
///
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -577,6 +579,10 @@ impl Plugin for EguiPlugin {
world.init_resource::<EguiUserTextures>();
world.init_resource::<EguiMousePosition>();
world.insert_resource(TouchId::default());
app.add_plugins(ExtractResourcePlugin::<EguiSettings>::default());
app.add_plugins(ExtractComponentPlugin::<EguiContext>::default());
app.add_plugins(ExtractComponentPlugin::<WindowSize>::default());
app.add_plugins(ExtractComponentPlugin::<EguiRenderOutput>::default());

app.add_systems(
PreStartup,
Expand Down Expand Up @@ -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(),
)
Expand Down
22 changes: 2 additions & 20 deletions src/render_systems.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
egui_node::{EguiNode, EguiPipeline, EguiPipelineKey},
EguiContextQueryReadOnly, EguiManagedTextures, EguiSettings, EguiUserTextures, WindowSize,
EguiManagedTextures, EguiSettings, EguiUserTextures, WindowSize,
};
use bevy::{
asset::HandleId,
Expand All @@ -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 {
Expand Down Expand Up @@ -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<Res<EguiSettings>>,
contexts: Extract<Query<EguiContextQueryReadOnly>>,
) {
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,
Expand Down Expand Up @@ -147,7 +129,7 @@ impl EguiTransform {
pub fn prepare_egui_transforms_system(
mut egui_transforms: ResMut<EguiTransforms>,
window_sizes: Query<(Entity, &WindowSize)>,
egui_settings: Res<ExtractedEguiSettings>,
egui_settings: Res<EguiSettings>,

render_device: Res<RenderDevice>,
render_queue: Res<RenderQueue>,
Expand Down