From 2057993f54d83c7d32986ce8493ceef65e8c976b Mon Sep 17 00:00:00 2001 From: Konkitoman Date: Wed, 2 Aug 2023 20:49:41 +0300 Subject: [PATCH] Some more work now stuff is rendered corectly --- crates/eframe/src/native/run.rs | 2 -- crates/egui/src/context.rs | 40 +++++++++++++++++++++++++++++---- crates/egui/src/memory.rs | 12 ++++++++++ examples/viewports/src/main.rs | 14 ++++++++++++ 4 files changed, 62 insertions(+), 6 deletions(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 3535223c7..63f0ac296 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -980,7 +980,6 @@ mod glow_integration { let window = &mut *window.write(); if let Some(winit_state) = &mut window.egui_winit { if let Some(win) = window.window.clone() { - println!("Render beagin!"); let win = win.read(); let input = winit_state.take_egui_input(&win); let output = @@ -1026,7 +1025,6 @@ mod glow_integration { "failed to get current context to swap buffers", )); } - println!("Should render sync"); return; } } diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index b6db46305..c8e045009 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -189,7 +189,7 @@ struct ContextImpl { frame_stack: Vec<(u64, u64)>, // The output of a frame: - graphics: GraphicLayers, + graphics: HashMap, output: PlatformOutput, paint_stats: PaintStats, @@ -241,6 +241,17 @@ impl ContextImpl { viewport_id: u64, parent_viewport_id: u64, ) { + // This is used to pause the last frame + if !self.frame_stack.is_empty() { + let viewport_id = self.get_viewport_id(); + println!("Pause: {viewport_id}"); + self.layer_rects_prev_viewports.insert( + viewport_id, + std::mem::take(&mut self.layer_rects_this_frame), + ); + self.memory.pause_frame(viewport_id); + } + self.frame_stack.push((viewport_id, parent_viewport_id)); self.repaint.start_frame(self.get_viewport_id()); @@ -581,7 +592,7 @@ impl Context { /// Read-write access to [`GraphicLayers`], where painted [`crate::Shape`]s are written to. #[inline] pub(crate) fn graphics_mut(&self, writer: impl FnOnce(&mut GraphicLayers) -> R) -> R { - self.write(move |ctx| writer(&mut ctx.graphics)) + self.write(move |ctx| writer(ctx.graphics.entry(ctx.get_viewport_id()).or_default())) } /// Read-only access to [`PlatformOutput`]. @@ -1459,7 +1470,22 @@ impl Context { }) }); - self.write(|ctx| ctx.frame_stack.pop()); + // This is used to resume the last frame! + let is_last = self.write(|ctx| { + ctx.frame_stack.pop(); + ctx.frame_stack.is_empty() + }); + if !is_last { + let viewport_id = self.get_viewport_id(); + println!("Resume: {viewport_id}"); + self.write(|ctx| { + ctx.layer_rects_prev_frame = ctx + .layer_rects_prev_viewports + .remove(&viewport_id) + .unwrap_or_default(); + ctx.memory.resume_frame(viewport_id) + }); + } FullOutput { platform_output, repaint_after, @@ -1471,7 +1497,13 @@ impl Context { } fn drain_paint_lists(&self) -> Vec { - self.write(|ctx| ctx.graphics.drain(ctx.memory.areas.order()).collect()) + self.write(|ctx| { + ctx.graphics + .entry(ctx.get_viewport_id()) + .or_default() + .drain(ctx.memory.areas.order()) + .collect() + }) } /// Tessellate the given shapes into triangle meshes. diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index 5ca45e01b..328c261b5 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -385,6 +385,13 @@ impl Memory { } } + pub(crate) fn pause_frame(&mut self, viewport_id: u64) { + self.interactions + .insert(viewport_id, std::mem::take(&mut self.interaction)); + self.viewports_areas + .insert(viewport_id, std::mem::take(&mut self.areas)); + } + pub(crate) fn end_frame( &mut self, input: &InputState, @@ -403,6 +410,11 @@ impl Memory { self.viewports_areas.retain(|id, _| viewports.contains(id)); } + pub(crate) fn resume_frame(&mut self, viewport_id: u64) { + self.interaction = self.interactions.remove(&viewport_id).unwrap(); + self.areas = self.viewports_areas.remove(&viewport_id).unwrap(); + } + /// Top-most layer at the given position. pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option { self.areas.layer_id_at(pos, resize_interact_radius_side) diff --git a/examples/viewports/src/main.rs b/examples/viewports/src/main.rs index f8c17b7e1..54f63a5bf 100644 --- a/examples/viewports/src/main.rs +++ b/examples/viewports/src/main.rs @@ -8,6 +8,9 @@ fn main() { let mut to_repair = false; + let mut show = false; + let mut value = 0.0; + let _ = eframe::run_simple_native( "Viewports Examples", NativeOptions { @@ -35,10 +38,21 @@ fn main() { ui.label("Parent Viewport ID: "); ui.label(format!("{parent_viewport_id}")) }); + ui.checkbox(&mut show, "Show"); + ui.add( + egui::widgets::DragValue::new(&mut value) + .clamp_range(-10..=10) + .speed(0.1), + ); }) }, ); + if show { + ui.label("Is shown!"); + ui.label(format!("Value: {value}")); + } + egui::CollapsingHeader::new("Show Test1").show(ui, |ui| { egui::Window::new("Test1").show(ctx, move |ui, id, parent_id| { ui.label(format!("Frame: {}", ui.ctx().frame_nr()));