Skip to content

Commit

Permalink
Implement better support (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheRawMeatball authored Feb 18, 2023
1 parent cdce403 commit 130f40d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
15 changes: 1 addition & 14 deletions examples/two_windows.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use bevy::{
prelude::*,
render::{camera::RenderTarget, render_graph::RenderGraph, RenderApp},
render::camera::RenderTarget,
window::{CreateWindow, PresentMode, WindowId},
};
use bevy_egui::{EguiContext, EguiPlugin};
Expand All @@ -23,22 +23,9 @@ fn main() {
.add_system(ui_first_window_system)
.add_system(ui_second_window_system);

let render_app = app.sub_app_mut(RenderApp);
let mut graph = render_app.world.get_resource_mut::<RenderGraph>().unwrap();

bevy_egui::setup_pipeline(
&mut graph,
bevy_egui::RenderGraphConfig {
window_id: *SECOND_WINDOW_ID,
egui_pass: SECONDARY_EGUI_PASS,
},
);

app.run();
}

const SECONDARY_EGUI_PASS: &str = "secondary_egui_pass";

fn create_new_window_system(
mut create_window_events: EventWriter<CreateWindow>,
mut commands: Commands,
Expand Down
23 changes: 14 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ use bevy::{
window::WindowId,
};
use egui_node::EguiNode;
use std::borrow::Cow;
#[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))]
use std::cell::{RefCell, RefMut};
#[cfg(all(feature = "manage_clipboard", not(target_arch = "wasm32")))]
Expand Down Expand Up @@ -302,8 +303,6 @@ impl EguiContext {
}

/// Egui context for a specific window.
/// If you want to display UI on a non-primary window, make sure to set up the render graph by
/// calling [`setup_pipeline`].
#[must_use]
#[track_caller]
pub fn ctx_for_window_mut(&mut self, window: WindowId) -> &egui::Context {
Expand All @@ -312,8 +311,7 @@ impl EguiContext {
.unwrap_or_else(|| panic!("`EguiContext::ctx_for_window_mut` was called for an uninitialized context (window {window}), consider moving your UI system to the `CoreStage::Update` stage or run it after the `EguiSystem::BeginFrame` system (`StartupStage::Startup` or `EguiStartupSystem::InitContexts` for startup systems respectively)"))
}

/// Fallible variant of [`EguiContext::ctx_for_window_mut`]. Make sure to set up the render
/// graph by calling [`setup_pipeline`].
/// Fallible variant of [`EguiContext::ctx_for_window_mut`].
#[must_use]
pub fn try_ctx_for_window_mut(&mut self, window: WindowId) -> Option<&egui::Context> {
self.ctx.get(&window)
Expand Down Expand Up @@ -515,6 +513,10 @@ impl Plugin for EguiPlugin {
RenderStage::Extract,
render_systems::extract_egui_textures_system,
)
.add_system_to_stage(
RenderStage::Extract,
render_systems::setup_new_windows_system,
)
.add_system_to_stage(
RenderStage::Prepare,
render_systems::prepare_egui_transforms_system,
Expand Down Expand Up @@ -624,14 +626,14 @@ pub struct RenderGraphConfig {
/// Target window.
pub window_id: WindowId,
/// Render pass name.
pub egui_pass: &'static str,
pub egui_pass: Cow<'static, str>,
}

impl Default for RenderGraphConfig {
fn default() -> Self {
RenderGraphConfig {
window_id: WindowId::primary(),
egui_pass: node::EGUI_PASS,
egui_pass: Cow::Borrowed(node::EGUI_PASS),
}
}
}
Expand All @@ -641,16 +643,19 @@ impl Default for RenderGraphConfig {
/// The pipeline for the primary window will already be set up by the [`EguiPlugin`],
/// so you'll only need to manually call this if you want to use multiple windows.
pub fn setup_pipeline(render_graph: &mut RenderGraph, config: RenderGraphConfig) {
render_graph.add_node(config.egui_pass, EguiNode::new(config.window_id));
render_graph.add_node(
config.egui_pass.to_string(),
EguiNode::new(config.window_id),
);

render_graph
.add_node_edge(
bevy::render::main_graph::node::CAMERA_DRIVER,
config.egui_pass,
config.egui_pass.to_string(),
)
.unwrap();

let _ = render_graph.add_node_edge("ui_pass_driver", config.egui_pass);
let _ = render_graph.add_node_edge("ui_pass_driver", config.egui_pass.to_string());
}

#[cfg(test)]
Expand Down
23 changes: 20 additions & 3 deletions src/render_systems.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::{
egui_node::{EguiPipeline, EguiPipelineKey},
EguiContext, EguiManagedTextures, EguiRenderOutput, EguiRenderOutputContainer, EguiSettings,
EguiWindowSizeContainer, WindowSize,
setup_pipeline, EguiContext, EguiManagedTextures, EguiRenderOutput, EguiRenderOutputContainer,
EguiSettings, EguiWindowSizeContainer, RenderGraphConfig, WindowSize,
};
use bevy::{
asset::HandleId,
prelude::*,
render::{
render_asset::RenderAssets,
render_graph::RenderGraph,
render_resource::{
BindGroup, BindGroupDescriptor, BindGroupEntry, BindingResource, BufferId,
CachedRenderPipelineId, DynamicUniformBuffer, PipelineCache, ShaderType,
Expand All @@ -19,7 +20,7 @@ use bevy::{
Extract,
},
utils::HashMap,
window::WindowId,
window::{CreateWindow, WindowId},
};

/// Extracted Egui render output.
Expand Down Expand Up @@ -72,6 +73,22 @@ impl ExtractedEguiTextures {
}
}

/// Calls [`setup_pipeline`] for newly created windows to ensure egui works on them.
pub fn setup_new_windows_system(
mut new_windows: Extract<EventReader<CreateWindow>>,
mut graph: ResMut<RenderGraph>,
) {
for window in new_windows.iter() {
setup_pipeline(
&mut graph,
RenderGraphConfig {
window_id: window.id,
egui_pass: std::borrow::Cow::Owned(format!("egui{}", window.id)),
},
)
}
}

/// Extracts Egui context, render output, settings and application window sizes.
pub fn extract_egui_render_data_system(
mut commands: Commands,
Expand Down

0 comments on commit 130f40d

Please sign in to comment.