Add ui.allocate_painter helper

This commit is contained in:
Emil Ernerfeldt 2020-12-19 11:14:21 +01:00
parent 58f36eb6ef
commit 1e1bfa4dc7
3 changed files with 13 additions and 6 deletions

View file

@ -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)`. * Turn off `Window` title bars with `window.title_bar(false)`.
* `ImageButton` - `ui.add(ImageButton::new(...))`. * `ImageButton` - `ui.add(ImageButton::new(...))`.
* `ui.vertical_centered` and `ui.vertical_centered_justified`. * `ui.vertical_centered` and `ui.vertical_centered_justified`.
* `ui.allocate_painter` helper.
* Mouse-over explanation to duplicate ID warning. * 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 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`. * You can now control the minimum and maixumum number of decimals to show in a `Slider` or `DragValue`.

View file

@ -243,7 +243,7 @@ impl Default for Painting {
fn default() -> Self { fn default() -> Self {
Self { Self {
lines: Default::default(), 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) { fn content(&mut self, ui: &mut Ui) {
let rect = ui.allocate_space(ui.available_size_before_wrap_finite()); let painter = ui.allocate_painter(ui.available_size_before_wrap_finite());
let response = ui.interact(rect, ui.id(), Sense::drag()); let rect = painter.clip_rect();
let rect = response.rect; let id = ui.make_position_id();
let clip_rect = ui.clip_rect().intersect(rect); // Make sure we don't paint out of bounds let response = ui.interact(rect, id, Sense::drag());
let painter = Painter::new(ui.ctx().clone(), ui.layer_id(), clip_rect);
if self.lines.is_empty() { if self.lines.is_empty() {
self.lines.push(vec![]); self.lines.push(vec![]);

View file

@ -525,6 +525,13 @@ impl Ui {
let response = self.interact_hover(final_child_rect); let response = self.interact_hover(final_child_rect);
(ret, response) (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 /// # Adding widgets