From 7fb3c66d0b94bf7bad03787a3828518cf5fef7f2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sun, 6 Dec 2020 16:09:36 +0100 Subject: [PATCH] advance_after_rect --- egui/src/layout.rs | 49 ++++++++++++++++++++++++++++------------------ egui/src/ui.rs | 21 ++++++++------------ 2 files changed, 38 insertions(+), 32 deletions(-) diff --git a/egui/src/layout.rs b/egui/src/layout.rs index fab74bf4a..b16797cb7 100644 --- a/egui/src/layout.rs +++ b/egui/src/layout.rs @@ -303,25 +303,6 @@ impl Layout { rect } - /// Advance the cursor by this many points. - pub fn advance_cursor(self, region: &mut Region, amount: f32) { - match self.main_dir { - Direction::LeftToRight => region.cursor.x += amount, - Direction::RightToLeft => region.cursor.x -= amount, - Direction::TopDown => region.cursor.y += amount, - Direction::BottomUp => region.cursor.y -= amount, - } - } - - /// Advance the cursor by this spacing - pub fn advance_cursor2(self, region: &mut Region, amount: Vec2) { - if self.main_dir.is_horizontal() { - self.advance_cursor(region, amount.x) - } else { - self.advance_cursor(region, amount.y) - } - } - /// Reserve this much space and move the cursor. /// Returns where to put the widget. /// @@ -375,6 +356,36 @@ impl Layout { Rect::from_min_size(child_pos, child_size) } + + /// Advance the cursor by this many points. + pub fn advance_cursor(self, region: &mut Region, amount: f32) { + match self.main_dir { + Direction::LeftToRight => region.cursor.x += amount, + Direction::RightToLeft => region.cursor.x -= amount, + Direction::TopDown => region.cursor.y += amount, + Direction::BottomUp => region.cursor.y -= amount, + } + } + + /// Advance the cursor by this spacing + pub fn advance_cursor2(self, region: &mut Region, amount: Vec2) { + if self.main_dir.is_horizontal() { + self.advance_cursor(region, amount.x) + } else { + self.advance_cursor(region, amount.y) + } + } + + /// Advance cursor after a widget was added to a specific rectangle. + pub fn advance_after_rect(self, region: &mut Region, rect: Rect, item_spacing: Vec2) { + match self.main_dir { + Direction::LeftToRight => region.cursor.x = rect.right() + item_spacing.x, + Direction::RightToLeft => region.cursor.x = rect.left() - item_spacing.x, + Direction::TopDown => region.cursor.y = rect.bottom() + item_spacing.y, + Direction::BottomUp => region.cursor.y = rect.top() - item_spacing.y, + } + region.expand_to_include_rect(rect); + } } // ---------------------------------------------------------------------------- diff --git a/egui/src/ui.rs b/egui/src/ui.rs index cdcd38919..4991e8541 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -439,11 +439,10 @@ impl Ui { /// Returns where to put the widget. fn allocate_space_impl(&mut self, desired_size: Vec2) -> Rect { let child_rect = self.layout.next_space(&self.region, desired_size); - self.layout - .advance_cursor2(&mut self.region, child_rect.size()); + let item_spacing = self.style().spacing.item_spacing; - self.layout.advance_cursor2(&mut self.region, item_spacing); - self.region.expand_to_include_rect(child_rect); + self.layout + .advance_after_rect(&mut self.region, child_rect, item_spacing); self.next_auto_id = self.next_auto_id.wrapping_add(1); child_rect @@ -463,11 +462,9 @@ impl Ui { let ret = add_contents(&mut child_ui); let child_rect = child_ui.region.max_rect; - self.layout - .advance_cursor2(&mut self.region, child_rect.size()); let item_spacing = self.style().spacing.item_spacing; - self.layout.advance_cursor2(&mut self.region, item_spacing); - self.region.expand_to_include_rect(child_rect); + self.layout + .advance_after_rect(&mut self.region, child_rect, item_spacing); let response = self.interact_hover(child_rect); (ret, response) @@ -477,7 +474,7 @@ impl Ui { /// If the contents overflow, more space will be allocated. /// When finished, the amount of space actually used (`min_rect`) will be allocated. /// So you can request a lot of space and then use less. - fn allocate_ui_min( + pub fn allocate_ui_min( &mut self, initial_size: Vec2, add_contents: impl FnOnce(&mut Self) -> R, @@ -487,11 +484,9 @@ impl Ui { let ret = add_contents(&mut child_ui); let child_rect = child_ui.region.min_rect; - self.layout - .advance_cursor2(&mut self.region, child_rect.size()); let item_spacing = self.style().spacing.item_spacing; - self.layout.advance_cursor2(&mut self.region, item_spacing); - self.region.expand_to_include_rect(child_rect); + self.layout + .advance_after_rect(&mut self.region, child_rect, item_spacing); let response = self.interact_hover(child_rect); (ret, response)