1
0
mirror of https://github.com/emilk/egui.git synced 2026-08-29 04:40:03 -04:00

Add anchors to windows and areas (#310)

This is so that you can put a window in e.g. the top right corner
or the center of the screen.
This commit is contained in:
Emil Ernerfeldt
2021-04-18 10:01:41 +02:00
committed by GitHub
parent 5667e7eb51
commit 580d27e0d3
6 changed files with 220 additions and 56 deletions

View File

@@ -52,6 +52,15 @@ impl Align {
Self::Max => 1.0,
}
}
/// Convert `Min => -1.0`, `Center => 0.0` or `Max => 1.0`.
pub fn to_sign(&self) -> f32 {
match self {
Self::Min => -1.0,
Self::Center => 0.0,
Self::Max => 1.0,
}
}
}
impl Default for Align {
@@ -88,6 +97,11 @@ impl Align2 {
self.0[1]
}
/// -1, 0, or +1 for each axis
pub fn to_sign(&self) -> Vec2 {
vec2(self.x().to_sign(), self.y().to_sign())
}
/// Used e.g. to anchor a piece of text to a part of the rectangle.
/// Give a position within the rect, specified by the aligns
pub fn anchor_rect(self, rect: Rect) -> Rect {
@@ -119,6 +133,21 @@ impl Align2 {
Rect::from_min_size(Pos2::new(x, y), size)
}
pub fn pos_in_rect(self, frame: &Rect) -> Pos2 {
let x = match self.x() {
Align::Min => frame.left(),
Align::Center => frame.center().x,
Align::Max => frame.right(),
};
let y = match self.y() {
Align::Min => frame.top(),
Align::Center => frame.center().y,
Align::Max => frame.bottom(),
};
pos2(x, y)
}
}
pub fn center_size_in_rect(size: Vec2, frame: Rect) -> Rect {