Skip to content

Commit

Permalink
Add Align2::anchor_size (#3863)
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk authored Jan 22, 2024
1 parent 9fb83d3 commit 31cc31a
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions crates/egui/src/painter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions crates/egui_plot/src/items/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
17 changes: 17 additions & 0 deletions crates/emath/src/align.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
2 changes: 1 addition & 1 deletion crates/epaint/src/shape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 31cc31a

Please sign in to comment.