Skip to content

Commit

Permalink
Implement level-of-detail on text labels
Browse files Browse the repository at this point in the history
  • Loading branch information
grtlr committed Dec 13, 2024
1 parent ebce83d commit 94a8e64
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions crates/viewer/re_view_graph/src/ui/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ impl DrawableLabel {
}

pub struct TextLabel {
color: Option<Color32>,
frame: Frame,
galley: Arc<Galley>,
}
Expand All @@ -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
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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,
})
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 94a8e64

Please sign in to comment.