From 94a8e64a4efd77d6e98b57fe4691eccf981f8223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jochen=20G=C3=B6rtler?= Date: Fri, 13 Dec 2024 11:09:11 +0100 Subject: [PATCH] Implement level-of-detail on text labels --- crates/viewer/re_view_graph/src/ui/draw.rs | 28 ++++++++++++++++------ 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/viewer/re_view_graph/src/ui/draw.rs b/crates/viewer/re_view_graph/src/ui/draw.rs index df47b3307b99f..989f63d8f3c11 100644 --- a/crates/viewer/re_view_graph/src/ui/draw.rs +++ b/crates/viewer/re_view_graph/src/ui/draw.rs @@ -35,6 +35,7 @@ impl DrawableLabel { } pub struct TextLabel { + color: Option, frame: Frame, galley: Arc, } @@ -52,7 +53,7 @@ pub enum LevelOfDetail { impl LevelOfDetail { pub fn from_scaling(zoom: f32) -> Self { - if zoom < 0.25 { + if zoom < 0.20 { Self::Low } else { Self::Full @@ -64,7 +65,7 @@ impl DrawableLabel { pub fn size(&self) -> Vec2 { match self { Self::Circle(CircleLabel { radius, .. }) => Vec2::splat(radius * 2.0), - Self::Text(TextLabel { galley, frame }) => { + Self::Text(TextLabel { galley, frame, .. }) => { frame.inner_margin.sum() + galley.size() + Vec2::splat(frame.stroke.width * 2.0) } } @@ -98,7 +99,11 @@ impl DrawableLabel { .fill(ui.style().visuals.widgets.noninteractive.bg_fill) .stroke(Stroke::new(1.0, ui.style().visuals.text_color())); - Self::Text(TextLabel { frame, galley }) + Self::Text(TextLabel { + frame, + galley, + color, + }) } } @@ -131,7 +136,7 @@ fn draw_circle_label( } fn draw_text_label(ui: &mut Ui, label: &TextLabel, highlight: InteractionHighlight) -> Response { - let TextLabel { galley, frame } = label; + let TextLabel { galley, frame, .. } = label; let visuals = &ui.style().visuals; let bg = match highlight.hover { @@ -154,7 +159,11 @@ fn draw_text_label(ui: &mut Ui, label: &TextLabel, highlight: InteractionHighlig } fn draw_rect_label(ui: &mut Ui, label: &TextLabel, highlight: InteractionHighlight) -> Response { - let TextLabel { galley, frame } = label; + let TextLabel { + galley, + frame, + color, + } = label; let visuals = &ui.style().visuals; let bg = match highlight.hover { @@ -167,12 +176,17 @@ fn draw_rect_label(ui: &mut Ui, label: &TextLabel, highlight: InteractionHighlig _ => Stroke::new(1.0, visuals.text_color()), }; + // We use `gamma` to correct for the fact that text is not completely solid. + let fill_color = color + .unwrap_or_else(|| visuals.text_color()) + .gamma_multiply(0.5); + frame .stroke(stroke) .fill(bg) .show(ui, |ui| { - let (resp, painter) = ui.allocate_painter(galley.rect.size(), Sense::click()); - painter.rect_filled(galley.rect, 0.0, Color32::RED); + let (resp, painter) = ui.allocate_painter(galley.rect.size(), Sense::hover()); + painter.rect_filled(resp.rect, 0.0, fill_color); resp }) .inner