From 1e1bfa4dc75fd2f51c45e4e14e15cd507c984757 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Sat, 19 Dec 2020 11:14:21 +0100 Subject: [PATCH] Add ui.allocate_painter helper --- CHANGELOG.md | 1 + egui/src/demos/demo_window.rs | 11 +++++------ egui/src/ui.rs | 7 +++++++ 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 665ad763..de2303aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). * Turn off `Window` title bars with `window.title_bar(false)`. * `ImageButton` - `ui.add(ImageButton::new(...))`. * `ui.vertical_centered` and `ui.vertical_centered_justified`. +* `ui.allocate_painter` helper. * Mouse-over explanation to duplicate ID warning. * You can now easily constrain Egui to a portion of the screen using `RawInput::screen_rect`. * You can now control the minimum and maixumum number of decimals to show in a `Slider` or `DragValue`. diff --git a/egui/src/demos/demo_window.rs b/egui/src/demos/demo_window.rs index ee191231..32e4e9d3 100644 --- a/egui/src/demos/demo_window.rs +++ b/egui/src/demos/demo_window.rs @@ -243,7 +243,7 @@ impl Default for Painting { fn default() -> Self { Self { lines: Default::default(), - stroke: Stroke::new(1.0, LIGHT_GRAY), + stroke: Stroke::new(1.0, LIGHT_BLUE), } } } @@ -265,11 +265,10 @@ impl Painting { } fn content(&mut self, ui: &mut Ui) { - let rect = ui.allocate_space(ui.available_size_before_wrap_finite()); - let response = ui.interact(rect, ui.id(), Sense::drag()); - let rect = response.rect; - let clip_rect = ui.clip_rect().intersect(rect); // Make sure we don't paint out of bounds - let painter = Painter::new(ui.ctx().clone(), ui.layer_id(), clip_rect); + let painter = ui.allocate_painter(ui.available_size_before_wrap_finite()); + let rect = painter.clip_rect(); + let id = ui.make_position_id(); + let response = ui.interact(rect, id, Sense::drag()); if self.lines.is_empty() { self.lines.push(vec![]); diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 2fd7ab3c..d89ba56d 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -525,6 +525,13 @@ impl Ui { let response = self.interact_hover(final_child_rect); (ret, response) } + + /// Convenience function to get a region to paint on + pub fn allocate_painter(&mut self, desired_size: Vec2) -> Painter { + let rect = self.allocate_space(desired_size); + let clip_rect = self.clip_rect().intersect(rect); // Make sure we don't paint out of bounds + Painter::new(self.ctx().clone(), self.layer_id(), clip_rect) + } } /// # Adding widgets