From 31cc31a67ba2e45dd4bee5bf90a9d394201b3599 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Mon, 22 Jan 2024 16:47:50 +0100 Subject: [PATCH] Add `Align2::anchor_size` (#3863) --- crates/egui/src/painter.rs | 4 ++-- crates/egui_plot/src/items/mod.rs | 4 +--- crates/emath/src/align.rs | 17 +++++++++++++++++ crates/epaint/src/shape.rs | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/crates/egui/src/painter.rs b/crates/egui/src/painter.rs index 8d6e3e6a8d6..962b0d3b03a 100644 --- a/crates/egui/src/painter.rs +++ b/crates/egui/src/painter.rs @@ -229,7 +229,7 @@ impl Painter { text: impl ToString, ) -> Rect { let galley = self.layout_no_wrap(text.to_string(), FontId::monospace(12.0), color); - let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size())); + let rect = anchor.anchor_size(pos, galley.size()); let frame_rect = rect.expand(2.0); self.add(Shape::rect_filled( frame_rect, @@ -378,7 +378,7 @@ impl Painter { text_color: Color32, ) -> Rect { let galley = self.layout_no_wrap(text.to_string(), font_id, text_color); - let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size())); + let rect = anchor.anchor_size(pos, galley.size()); self.galley(rect.min, galley, text_color); rect } diff --git a/crates/egui_plot/src/items/mod.rs b/crates/egui_plot/src/items/mod.rs index 4c2eb77a92e..24bcb9d6ef9 100644 --- a/crates/egui_plot/src/items/mod.rs +++ b/crates/egui_plot/src/items/mod.rs @@ -728,9 +728,7 @@ impl PlotItem for Text { .into_galley(ui, Some(false), f32::INFINITY, TextStyle::Small); let pos = transform.position_from_point(&self.position); - let rect = self - .anchor - .anchor_rect(Rect::from_min_size(pos, galley.size())); + let rect = self.anchor.anchor_size(pos, galley.size()); shapes.push(epaint::TextShape::new(rect.min, galley, color).into()); diff --git a/crates/emath/src/align.rs b/crates/emath/src/align.rs index 97ae44ff444..8cc515836cb 100644 --- a/crates/emath/src/align.rs +++ b/crates/emath/src/align.rs @@ -186,6 +186,23 @@ impl Align2 { Rect::from_min_size(pos2(x, y), rect.size()) } + /// Use this anchor to position something around `pos`, + /// e.g. [`Self::RIGHT_TOP`] means the right-top of the rect + /// will end up at `pos`. + pub fn anchor_size(self, pos: Pos2, size: Vec2) -> Rect { + let x = match self.x() { + Align::Min => pos.x, + Align::Center => pos.x - 0.5 * size.x, + Align::Max => pos.x - size.x, + }; + let y = match self.y() { + Align::Min => pos.y, + Align::Center => pos.y - 0.5 * size.y, + Align::Max => pos.y - size.y, + }; + Rect::from_min_size(pos2(x, y), size) + } + /// e.g. center a size within a given frame pub fn align_size_within_rect(self, size: Vec2, frame: Rect) -> Rect { let x_range = self.x().align_size_within_range(size.x, frame.x_range()); diff --git a/crates/epaint/src/shape.rs b/crates/epaint/src/shape.rs index 4ecba1d1d1e..61b9d75e92b 100644 --- a/crates/epaint/src/shape.rs +++ b/crates/epaint/src/shape.rs @@ -264,7 +264,7 @@ impl Shape { color: Color32, ) -> Self { let galley = fonts.layout_no_wrap(text.to_string(), font_id, color); - let rect = anchor.anchor_rect(Rect::from_min_size(pos, galley.size())); + let rect = anchor.anchor_size(pos, galley.size()); Self::galley(rect.min, galley, color) }