diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d652a4f..44c70f0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Center window titles. * Tweak size and alignment of some emojis to match other text. * Rename `PaintCmd` to `Shape`. +* Replace tuple `(Rect, Shape)` with tuple-struct `ClippedShape`. * Rename feature `"serde"` to `"persistence"`. * Break out the modules `math` and `paint` into separate crates `emath` and `epaint`. diff --git a/egui/src/context.rs b/egui/src/context.rs index 2e0d0103..95c51c55 100644 --- a/egui/src/context.rs +++ b/egui/src/context.rs @@ -596,7 +596,7 @@ impl Context { /// You can transform the returned shapes into triangles with a call to /// `Context::tessellate`. #[must_use] - pub fn end_frame(&self) -> (Output, Vec<(Rect, Shape)>) { + pub fn end_frame(&self) -> (Output, Vec) { if self.input.wants_repaint() { self.request_repaint(); } @@ -613,13 +613,13 @@ impl Context { (output, shapes) } - fn drain_paint_lists(&self) -> Vec<(Rect, Shape)> { + fn drain_paint_lists(&self) -> Vec { let memory = self.memory(); self.graphics().drain(memory.areas.order()).collect() } /// Tessellate the given shapes into triangle meshes. - pub fn tessellate(&self, shapes: Vec<(Rect, Shape)>) -> PaintJobs { + pub fn tessellate(&self, shapes: Vec) -> PaintJobs { let mut tessellation_options = self.memory().options.tessellation_options; tessellation_options.aa_size = 1.0 / self.pixels_per_point(); let paint_stats = PaintStats::from_shapes(&shapes); // TODO: internal allocations diff --git a/egui/src/layers.rs b/egui/src/layers.rs index b95e4cb8..1059536b 100644 --- a/egui/src/layers.rs +++ b/egui/src/layers.rs @@ -1,6 +1,6 @@ +use crate::{math::Rect, Id, *}; use epaint::ahash::AHashMap; - -use crate::{math::Rect, paint::Shape, Id, *}; +use epaint::{ClippedShape, Shape}; /// Different layer categories #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] @@ -76,7 +76,7 @@ pub struct ShapeIdx(usize); /// A list of [`Shape`]s paired with a clip rectangle. #[derive(Clone, Default)] -pub struct PaintList(Vec<(Rect, Shape)>); +pub struct PaintList(Vec); impl PaintList { pub fn is_empty(&self) -> bool { @@ -86,13 +86,13 @@ impl PaintList { /// Returns the index of the new [`Shape`] that can be used with `PaintList::set`. pub fn add(&mut self, clip_rect: Rect, shape: Shape) -> ShapeIdx { let idx = ShapeIdx(self.0.len()); - self.0.push((clip_rect, shape)); + self.0.push(ClippedShape(clip_rect, shape)); idx } pub fn extend(&mut self, clip_rect: Rect, mut shapes: Vec) { self.0 - .extend(shapes.drain(..).map(|shape| (clip_rect, shape))) + .extend(shapes.drain(..).map(|shape| ClippedShape(clip_rect, shape))) } /// Modify an existing [`Shape`]. @@ -104,12 +104,12 @@ impl PaintList { /// and then later setting it using `paint_list.set(idx, cr, frame);`. pub fn set(&mut self, idx: ShapeIdx, clip_rect: Rect, shape: Shape) { assert!(idx.0 < self.0.len()); - self.0[idx.0] = (clip_rect, shape); + self.0[idx.0] = ClippedShape(clip_rect, shape); } /// Translate each [`Shape`] and clip rectangle by this much, in-place pub fn translate(&mut self, delta: Vec2) { - for (clip_rect, shape) in &mut self.0 { + for ClippedShape(clip_rect, shape) in &mut self.0 { *clip_rect = clip_rect.translate(delta); shape.translate(delta); } @@ -126,10 +126,7 @@ impl GraphicLayers { .or_default() } - pub fn drain( - &mut self, - area_order: &[LayerId], - ) -> impl ExactSizeIterator { + pub fn drain(&mut self, area_order: &[LayerId]) -> impl ExactSizeIterator { let mut all_shapes: Vec<_> = Default::default(); for &order in &Order::ALL { diff --git a/epaint/src/lib.rs b/epaint/src/lib.rs index 058ed089..835a6a80 100644 --- a/epaint/src/lib.rs +++ b/epaint/src/lib.rs @@ -61,7 +61,7 @@ pub use { shape::Shape, stats::PaintStats, stroke::Stroke, - tessellator::{PaintJob, PaintJobs, TessellationOptions}, + tessellator::TessellationOptions, text::{Galley, TextStyle}, texture_atlas::{Texture, TextureAtlas}, triangles::{Triangles, Vertex}, @@ -101,3 +101,21 @@ pub(crate) struct PaintRect { pub fill: Color32, pub stroke: Stroke, } + +/// A [`Shape`] within a clip rectangle. +/// +/// Everything is using logical points. +#[derive(Clone, Debug)] +pub struct ClippedShape( + /// Clip / scissor rectangle. + /// Only show the part of the [`shape`] that falls within this. + pub emath::Rect, + /// The shape + pub Shape, +); + +/// A clip triangle and some textured triangles, all in points (logical pixels). +pub type PaintJob = (emath::Rect, Triangles); + +/// Grouped by clip rectangles, in points (logical pixels). +pub type PaintJobs = Vec; diff --git a/epaint/src/stats.rs b/epaint/src/stats.rs index aa94b083..8a4d15ba 100644 --- a/epaint/src/stats.rs +++ b/epaint/src/stats.rs @@ -1,4 +1,4 @@ -use {crate::*, emath::*}; +use crate::*; #[derive(Clone, Copy, PartialEq)] enum ElementSize { @@ -146,13 +146,13 @@ pub struct PaintStats { } impl PaintStats { - pub fn from_shapes(shapes: &[(Rect, Shape)]) -> Self { + pub fn from_shapes(shapes: &[ClippedShape]) -> Self { let mut stats = Self::default(); stats.shape_path.element_size = ElementSize::Heterogenous; // nicer display later stats.shape_vec.element_size = ElementSize::Heterogenous; // nicer display later stats.shapes = AllocInfo::from_slice(shapes); - for (_, shape) in shapes { + for ClippedShape(_, shape) in shapes { stats.add(shape); } stats diff --git a/epaint/src/tessellator.rs b/epaint/src/tessellator.rs index 4e7d152b..3ec7aded 100644 --- a/epaint/src/tessellator.rs +++ b/epaint/src/tessellator.rs @@ -9,12 +9,6 @@ use crate::{text::Fonts, *}; use emath::*; use std::f32::consts::TAU; -/// A clip triangle and some textured triangles. -pub type PaintJob = (Rect, Triangles); - -/// Grouped by clip rectangles, in pixel coordinates -pub type PaintJobs = Vec; - // ---------------------------------------------------------------------------- #[derive(Clone, Debug, Default)] @@ -670,14 +664,14 @@ impl Tessellator { /// ## Returns /// A list of clip rectangles with matching [`Triangles`]. pub fn tessellate_shapes( - shapes: Vec<(Rect, Shape)>, + shapes: Vec, options: TessellationOptions, fonts: &Fonts, ) -> Vec<(Rect, Triangles)> { let mut tessellator = Tessellator::from_options(options); let mut jobs = PaintJobs::default(); - for (clip_rect, shape) in shapes { + for ClippedShape(clip_rect, shape) in shapes { let start_new_job = match jobs.last() { None => true, Some(job) => job.0 != clip_rect || job.1.texture_id != shape.texture_id(), diff --git a/epaint/src/triangles.rs b/epaint/src/triangles.rs index 47ae2164..8db0ec1b 100644 --- a/epaint/src/triangles.rs +++ b/epaint/src/triangles.rs @@ -29,7 +29,7 @@ pub struct Triangles { /// The vertex data indexed by `indices`. pub vertices: Vec, - /// The texture to use when drawing these triangles + /// The texture to use when drawing these triangles. pub texture_id: TextureId, }