Skip to content

Commit

Permalink
Allow read access to shapes added to painter this frame (#3866)
Browse files Browse the repository at this point in the history
Co-authored-by: Emil Ernerfeldt <[email protected]>
  • Loading branch information
brunizzl and emilk authored Jan 24, 2024
1 parent 3a8e234 commit bcf032a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
6 changes: 6 additions & 0 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,12 @@ impl Context {
self.write(move |ctx| writer(&mut ctx.viewport().graphics))
}

/// Read-only access to [`GraphicLayers`], where painted [`crate::Shape`]s are written to.
#[inline]
pub(crate) fn graphics<R>(&self, reader: impl FnOnce(&GraphicLayers) -> R) -> R {
self.write(move |ctx| reader(&ctx.viewport().graphics))
}

/// Read-only access to [`PlatformOutput`].
///
/// This is what egui outputs each frame.
Expand Down
11 changes: 10 additions & 1 deletion crates/egui/src/layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl LayerId {

/// A unique identifier of a specific [`Shape`] in a [`PaintList`].
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct ShapeIdx(usize);
pub struct ShapeIdx(pub usize);

/// A list of [`Shape`]s paired with a clip rectangle.
#[derive(Clone, Default)]
Expand Down Expand Up @@ -158,6 +158,11 @@ impl PaintList {
shape.translate(delta);
}
}

/// Read-only access to all held shapes.
pub fn all_entries(&self) -> impl ExactSizeIterator<Item = &ClippedShape> {
self.0.iter()
}
}

#[derive(Clone, Default)]
Expand All @@ -170,6 +175,10 @@ impl GraphicLayers {
.or_default()
}

pub fn get(&self, layer_id: LayerId) -> Option<&PaintList> {
self.0[layer_id.order as usize].get(&layer_id.id)
}

pub fn drain(&mut self, area_order: &[LayerId]) -> Vec<ClippedShape> {
crate::profile_function!();

Expand Down
13 changes: 12 additions & 1 deletion crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use epaint::{
text::{Fonts, Galley},
CircleShape, RectShape, Rounding, Shape, Stroke,
CircleShape, ClippedShape, RectShape, Rounding, Shape, Stroke,
};

/// Helper to paint shapes and text to a specific region on a specific layer.
Expand Down Expand Up @@ -193,6 +193,17 @@ impl Painter {
self.transform_shape(&mut shape);
self.paint_list(|l| l.set(idx, self.clip_rect, shape));
}

/// Access all shapes added this frame.
pub fn for_each_shape(&self, mut reader: impl FnMut(&ClippedShape)) {
self.ctx.graphics(|g| {
if let Some(list) = g.get(self.layer_id) {
for c in list.all_entries() {
reader(c);
}
}
});
}
}

/// ## Debug painting
Expand Down

0 comments on commit bcf032a

Please sign in to comment.