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

Make it easy to panels inside of Ui:s (#629)

* Allow using the layout cursor to restrict available area

* Avoid id clashes when putting panels inside a Ui

* Panels: Propagate height/width range to inner Ui

* Allow easy placement of panels inside of Ui:s

* demo: simplify Windows with Panels demo
This commit is contained in:
Emil Ernerfeldt
2021-08-20 00:10:06 +02:00
committed by GitHub
parent ee50cca696
commit 3e2746a288
6 changed files with 164 additions and 83 deletions

View File

@@ -63,6 +63,36 @@ impl Align {
Self::Max => 1.0,
}
}
/// ``` rust
/// assert_eq!(emath::Align::Min.align_size_within_range(2.0, 10.0..=20.0), 10.0..=12.0);
/// assert_eq!(emath::Align::Center.align_size_within_range(2.0, 10.0..=20.0), 14.0..=16.0);
/// assert_eq!(emath::Align::Max.align_size_within_range(2.0, 10.0..=20.0), 18.0..=20.0);
/// assert_eq!(emath::Align::Min.align_size_within_range(f32::INFINITY, 10.0..=20.0), 10.0..=f32::INFINITY);
/// assert_eq!(emath::Align::Center.align_size_within_range(f32::INFINITY, 10.0..=20.0), f32::NEG_INFINITY..=f32::INFINITY);
/// assert_eq!(emath::Align::Max.align_size_within_range(f32::INFINITY, 10.0..=20.0), f32::NEG_INFINITY..=20.0);
/// ```
#[inline]
pub fn align_size_within_range(
self,
size: f32,
range: RangeInclusive<f32>,
) -> RangeInclusive<f32> {
let min = *range.start();
let max = *range.end();
match self {
Self::Min => min..=min + size,
Self::Center => {
if size == f32::INFINITY {
f32::NEG_INFINITY..=f32::INFINITY
} else {
let left = (min + max) / 2.0 - size / 2.0;
left..=left + size
}
}
Self::Max => max - size..=max,
}
}
}
impl Default for Align {
@@ -126,18 +156,9 @@ impl Align2 {
/// e.g. center a size within a given frame
pub fn align_size_within_rect(self, size: Vec2, frame: Rect) -> Rect {
let x = match self.x() {
Align::Min => frame.left(),
Align::Center => frame.center().x - size.x / 2.0,
Align::Max => frame.right() - size.x,
};
let y = match self.y() {
Align::Min => frame.top(),
Align::Center => frame.center().y - size.y / 2.0,
Align::Max => frame.bottom() - size.y,
};
Rect::from_min_size(Pos2::new(x, y), size)
let x_range = self.x().align_size_within_range(size.x, frame.x_range());
let y_range = self.y().align_size_within_range(size.y, frame.y_range());
Rect::from_x_y_ranges(x_range, y_range)
}
pub fn pos_in_rect(self, frame: &Rect) -> Pos2 {