From a644ac276b87941dd79d837c659d38bc2da57fa2 Mon Sep 17 00:00:00 2001 From: Mikolaj Wielgus Date: Fri, 19 Jul 2024 14:44:03 +0200 Subject: [PATCH] egui: add widget to control showing bboxes --- src/bin/topola-egui/painter.rs | 35 ++++++++++++++++++++++----------- src/bin/topola-egui/top.rs | 3 +++ src/bin/topola-egui/viewport.rs | 2 +- src/geometry/primitive.rs | 14 ++++++------- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/bin/topola-egui/painter.rs b/src/bin/topola-egui/painter.rs index 10d6da7..fa400b6 100644 --- a/src/bin/topola-egui/painter.rs +++ b/src/bin/topola-egui/painter.rs @@ -7,11 +7,20 @@ use topola::{ pub struct Painter<'a> { ui: &'a mut egui::Ui, transform: egui::emath::RectTransform, + paint_bboxes: bool, } impl<'a> Painter<'a> { - pub fn new(ui: &'a mut egui::Ui, transform: egui::emath::RectTransform) -> Self { - Self { ui, transform } + pub fn new( + ui: &'a mut egui::Ui, + transform: egui::emath::RectTransform, + paint_bboxes: bool, + ) -> Self { + Self { + ui, + transform, + paint_bboxes, + } } pub fn paint_primitive(&mut self, shape: &PrimitiveShape, color: egui::epaint::Color32) { @@ -49,16 +58,18 @@ impl<'a> Painter<'a> { self.ui.painter().add(epaint_shape); - let envelope = AccessPrimitiveShape::envelope(shape, 0.0); - let rect = egui::epaint::Rect { - min: [envelope.lower()[0] as f32, -envelope.upper()[1] as f32].into(), - max: [envelope.upper()[0] as f32, -envelope.lower()[1] as f32].into(), - }; - self.ui.painter().add(egui::Shape::rect_stroke( - self.transform.transform_rect(rect), - egui::Rounding::ZERO, - egui::Stroke::new(1.0, egui::Color32::GRAY), - )); + if self.paint_bboxes { + let bbox = AccessPrimitiveShape::bbox(shape, 0.0); + let rect = egui::epaint::Rect { + min: [bbox.lower()[0] as f32, -bbox.upper()[1] as f32].into(), + max: [bbox.upper()[0] as f32, -bbox.lower()[1] as f32].into(), + }; + self.ui.painter().add(egui::Shape::rect_stroke( + self.transform.transform_rect(rect), + egui::Rounding::ZERO, + egui::Stroke::new(1.0, egui::Color32::GRAY), + )); + } } pub fn paint_dot(&mut self, circle: Circle, color: egui::epaint::Color32) { diff --git a/src/bin/topola-egui/top.rs b/src/bin/topola-egui/top.rs index 3ecf924..9d68312 100644 --- a/src/bin/topola-egui/top.rs +++ b/src/bin/topola-egui/top.rs @@ -22,6 +22,7 @@ pub struct Top { pub is_placing_via: bool, pub show_ratsnest: bool, pub show_navmesh: bool, + pub show_bboxes: bool, } impl Top { @@ -30,6 +31,7 @@ impl Top { is_placing_via: false, show_ratsnest: false, show_navmesh: false, + show_bboxes: false, } } @@ -109,6 +111,7 @@ impl Top { ui.toggle_value(&mut self.show_ratsnest, "Show Ratsnest"); ui.toggle_value(&mut self.show_navmesh, "Show Navmesh"); + ui.toggle_value(&mut self.show_bboxes, "Show Bboxes"); ui.separator(); diff --git a/src/bin/topola-egui/viewport.rs b/src/bin/topola-egui/viewport.rs index 9d699cc..296b52f 100644 --- a/src/bin/topola-egui/viewport.rs +++ b/src/bin/topola-egui/viewport.rs @@ -70,7 +70,7 @@ impl Viewport { .translate(ctx.input(|i| -i.raw_scroll_delta / new_scale)); let transform = egui::emath::RectTransform::from_to(self.from_rect, viewport_rect); - let mut painter = Painter::new(ui, transform); + let mut painter = Painter::new(ui, transform, top.show_bboxes); if let Some(ref mut invoker) = maybe_invoker { if ctx.input(|i| i.pointer.any_click()) { diff --git a/src/geometry/primitive.rs b/src/geometry/primitive.rs index a79d966..2c3f324 100644 --- a/src/geometry/primitive.rs +++ b/src/geometry/primitive.rs @@ -14,11 +14,11 @@ pub trait AccessPrimitiveShape: AccessShape { fn priority(&self) -> usize; fn inflate(&self, margin: f64) -> PrimitiveShape; fn intersects(&self, other: &PrimitiveShape) -> bool; - fn envelope(&self, margin: f64) -> AABB<[f64; 2]>; + fn bbox(&self, margin: f64) -> AABB<[f64; 2]>; fn width(&self) -> f64; fn envelope_3d(&self, margin: f64, layer: usize) -> AABB<[f64; 3]> { - let envelope = self.envelope(margin); + let envelope = self.bbox(margin); AABB::from_corners( [envelope.lower()[0], envelope.lower()[1], layer as f64], [envelope.upper()[0], envelope.upper()[1], layer as f64], @@ -26,7 +26,7 @@ pub trait AccessPrimitiveShape: AccessShape { } fn full_height_envelope_3d(&self, margin: f64, layer_count: usize) -> AABB<[f64; 3]> { - let envelope = self.envelope(margin); + let envelope = self.bbox(margin); AABB::from_corners( [envelope.lower()[0], envelope.lower()[1], 0.0], [ @@ -113,7 +113,7 @@ impl AccessPrimitiveShape for DotShape { } } - fn envelope(&self, margin: f64) -> AABB<[f64; 2]> { + fn bbox(&self, margin: f64) -> AABB<[f64; 2]> { AABB::from_corners( [ self.circle.pos.x() - self.circle.r - margin, @@ -215,7 +215,7 @@ impl AccessPrimitiveShape for SegShape { } } - fn envelope(&self, margin: f64) -> AABB<[f64; 2]> { + fn bbox(&self, margin: f64) -> AABB<[f64; 2]> { let points: Vec<[f64; 2]> = self .polygon() .exterior() @@ -371,7 +371,7 @@ impl AccessPrimitiveShape for BendShape { } } - fn envelope(&self, _margin: f64) -> AABB<[f64; 2]> { + fn bbox(&self, _margin: f64) -> AABB<[f64; 2]> { let halfwidth = self.inner_circle.r + self.width; AABB::from_corners( [ @@ -393,6 +393,6 @@ impl AccessPrimitiveShape for BendShape { impl RTreeObject for PrimitiveShape { type Envelope = AABB<[f64; 2]>; fn envelope(&self) -> Self::Envelope { - AccessPrimitiveShape::envelope(self, 0.0) + AccessPrimitiveShape::bbox(self, 0.0) } }