use crate::draw_list::DrawList;
use crate::tree::Ui;
use crate::widget::NATURAL_FONT_SIZE;
pub const NAV_DEBUG_PATH: [f32; 4] = [1.0, 0.0, 1.0, 1.0];
pub const NAV_DEBUG_CHOSEN: [f32; 4] = [0.0, 1.0, 0.0, 1.0];
pub const NAV_DEBUG_BEAM: [f32; 4] = [1.0, 1.0, 0.0, 1.0];
pub const NAV_DEBUG_OUTSIDE: [f32; 4] = [0.5, 0.5, 0.5, 1.0];
const LABEL_INSET: f32 = 2.0;
impl Ui {
pub fn set_nav_debug(&mut self, on: bool) {
self.focus.debug = on;
}
#[must_use]
pub fn nav_debug(&self) -> bool {
self.focus.debug
}
pub(crate) fn emit_nav_debug(&self, list: &mut DrawList) {
if !self.focus.debug {
return;
}
let rect = |key| self.store.by_key(key).map(|node| node.rect);
let inset = glam::Vec2::splat(LABEL_INSET);
if let Some(focused) = self.focus.focused
&& let Some(mut at) = self.nodes.iter().position(|node| node.key == focused)
{
let mut path = Vec::new();
loop {
let node = &self.nodes[at];
let (min, max) = self.store.get(node.slot).rect;
list.rect_outline(min, max, 1.0, NAV_DEBUG_PATH);
let (start, end) = node.selector;
path.push(&self.selectors[start..end]);
match node.parent {
Some(parent) => at = parent,
None => break,
}
}
let root = self.store.get(self.nodes[at].slot).rect.0;
path.retain(|selector| !selector.is_empty());
path.reverse();
list.text(
root + inset,
path.join(" > "),
NAV_DEBUG_PATH,
NATURAL_FONT_SIZE,
);
}
for scored in &self.focus.scores {
let Some((min, max)) = rect(scored.key) else {
continue;
};
let colour = if scored.chosen {
NAV_DEBUG_CHOSEN
} else if scored.score.in_beam {
NAV_DEBUG_BEAM
} else {
NAV_DEBUG_OUTSIDE
};
list.rect_outline(min, max, 1.0, colour);
let beam = if scored.score.in_beam { "b" } else { "" };
list.text(
min + inset,
format!("{beam}{:.0}", scored.score.distance),
colour,
NATURAL_FONT_SIZE,
);
}
}
}