diff --git a/egui/src/layers.rs b/egui/src/layers.rs index 36d57b1b..fe19b771 100644 --- a/egui/src/layers.rs +++ b/egui/src/layers.rs @@ -1,6 +1,6 @@ use ahash::AHashMap; -use crate::{math::Rect, paint::PaintCmd, Id}; +use crate::{math::Rect, paint::PaintCmd, Id, *}; /// Different layer categories #[derive(Clone, Copy, Debug, Hash, Eq, PartialEq, Ord, PartialOrd)] @@ -105,6 +105,14 @@ impl PaintList { assert!(idx.0 < self.0.len()); self.0[idx.0] = (clip_rect, cmd); } + + /// Translate each paint-command and clip rectangle by this much, in-place + pub fn translate(&mut self, delta: Vec2) { + for (clip_rect, cmd) in &mut self.0 { + *clip_rect = clip_rect.translate(delta); + cmd.translate(delta); + } + } } #[derive(Clone, Default)] diff --git a/egui/src/paint/command.rs b/egui/src/paint/command.rs index 859b60e9..7696ad5e 100644 --- a/egui/src/paint/command.rs +++ b/egui/src/paint/command.rs @@ -3,6 +3,7 @@ use { crate::{ align::{anchor_rect, Align}, math::{Pos2, Rect}, + *, }, }; @@ -152,6 +153,35 @@ impl PaintCmd { super::TextureId::Egui } } + + /// Translate location by this much, in-place + pub fn translate(&mut self, delta: Vec2) { + match self { + PaintCmd::Noop => {} + PaintCmd::Circle { center, .. } => { + *center += delta; + } + PaintCmd::LineSegment { points, .. } => { + for p in points { + *p += delta; + } + } + PaintCmd::Path { points, .. } => { + for p in points { + *p += delta; + } + } + PaintCmd::Rect { rect, .. } => { + *rect = rect.translate(delta); + } + PaintCmd::Text { pos, .. } => { + *pos += delta; + } + PaintCmd::Triangles(triangles) => { + triangles.translate(delta); + } + } + } } #[derive(Clone, Copy, Debug, Default, PartialEq)] diff --git a/egui/src/paint/tessellator.rs b/egui/src/paint/tessellator.rs index 5af7d9d0..bb0ab574 100644 --- a/egui/src/paint/tessellator.rs +++ b/egui/src/paint/tessellator.rs @@ -243,6 +243,13 @@ impl Triangles { } output } + + /// Translate location by this much, in-place + pub fn translate(&mut self, delta: Vec2) { + for v in &mut self.vertices { + v.pos += delta; + } + } } // ---------------------------------------------------------------------------- @@ -697,7 +704,7 @@ fn tessellate_paint_command( if triangles.is_valid() { out.append(triangles); } else { - debug_assert!(false, "Ivalid Triangles in PaintCmd::Traingles"); + debug_assert!(false, "Invalid Triangles in PaintCmd::Triangles"); } } PaintCmd::LineSegment { points, stroke } => {