use std::fmt;
use std::hash::Hash;
use std::ops::RangeInclusive;
use nadir_core::hash::fnv1a64;
use nadir_core::{Color, Key, Rect, Vec2};
use crate::edit::{Caret, apply};
use crate::id::{Id, split};
use crate::input::Edit;
use crate::ui::{DebugUi, Focus, Interaction, Layer, Popup};
const SCOPE: u64 = 0x7363_6f70_655f_6964;
const DEFAULT_PLOT_HEIGHT: f32 = 30.0;
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Response {
pub hovered: bool,
pub active: bool,
pub clicked: bool,
pub changed: bool,
pub submitted: bool,
pub lost_focus: bool,
}
impl From<Interaction> for Response {
fn from(interaction: Interaction) -> Self {
Self {
hovered: interaction.hovered,
active: interaction.held,
clicked: interaction.clicked,
..Self::default()
}
}
}
#[non_exhaustive]
#[derive(Clone, Debug, PartialEq)]
pub struct Plot {
pub range: RangeInclusive<f32>,
pub height: f32,
pub selected: Option<usize>,
}
impl Plot {
pub fn new(range: RangeInclusive<f32>) -> Self {
Self {
range,
height: DEFAULT_PLOT_HEIGHT,
selected: None,
}
}
}
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct PlotResponse {
pub response: Response,
pub hovered: Option<usize>,
pub clicked: Option<usize>,
}
#[derive(Clone, Copy, Debug)]
struct Row {
x: f32,
top: f32,
height: f32,
}
pub struct Ui<'a> {
core: &'a mut DebugUi,
layer: Layer,
scope: Id,
clip: Rect,
left: f32,
width: f32,
indent: f32,
y: f32,
row: Option<Row>,
}
impl<'a> Ui<'a> {
pub(crate) fn new(
core: &'a mut DebugUi,
layer: Layer,
scope: Id,
clip: Rect,
left: f32,
width: f32,
y: f32,
) -> Self {
Self {
core,
layer,
scope,
clip,
left,
width,
indent: 0.0,
y,
row: None,
}
}
pub(crate) fn y(&self) -> f32 {
self.y
}
pub(crate) fn frame_number(&self) -> u64 {
self.core.frames
}
pub(crate) fn style(&self) -> crate::Style {
self.core.style
}
pub(crate) fn row_step(&self) -> f32 {
self.core.row_height() + self.core.style.spacing
}
pub(crate) fn room(&self) -> f32 {
self.clip.max.y - self.core.style.padding - self.y
}
pub(crate) fn skip_rows(&mut self, rows: usize) {
if self.row.is_none() {
self.y += self.row_step() * rows as f32;
}
}
pub(crate) fn wheel_over(&self, height: f32) -> f32 {
let rect = Rect::from_min_size(
Vec2::new(self.left + self.indent, self.y),
Vec2::new(self.width - self.indent, height),
);
self.core.wheel_over(self.layer, self.clip, rect)
}
pub fn with_id(&mut self, key: impl Hash, add: impl FnOnce(&mut Ui<'_>)) {
let scope = self.scope;
self.scope = scope.with((SCOPE, key));
add(self);
self.scope = scope;
}
pub fn horizontal(&mut self, add: impl FnOnce(&mut Ui<'_>)) {
if self.row.is_some() {
add(self);
return;
}
self.row = Some(Row {
x: self.left + self.indent,
top: self.y,
height: 0.0,
});
add(self);
if let Some(row) = self.row.take()
&& row.height > 0.0
{
self.y += row.height + self.core.style.spacing;
}
}
pub fn key_pressed(&self, key: Key) -> bool {
self.core
.input
.edits
.iter()
.any(|edit| matches!(*edit, Edit::Key { key: pressed, .. } if pressed == key))
}
pub fn focus_next(&mut self) {
self.core.focus_next = true;
}
pub fn label(&mut self, text: impl fmt::Display) {
let color = self.core.style.text;
self.label_colored(color, text);
}
pub fn label_colored(&mut self, color: Color, text: impl fmt::Display) {
let height = self.core.row_height();
if self.row.is_none() {
let probe = Rect::from_min_size(
Vec2::new(self.left + self.indent, self.y),
Vec2::new(self.width - self.indent, height),
);
if !probe.intersects(self.clip) {
self.allocate(None, height);
return;
}
}
let shown = self.core.format(text);
let width = self.core.width(self.core.text(shown));
let rect = self.allocate(Some(width), height);
let at = rect.min + Vec2::new(0.0, self.core.style.padding);
self.core.text_at(self.layer, self.clip, at, shown, color);
}
pub fn button(&mut self, text: impl fmt::Display) -> Response {
let (id, shown) = self.labeled(text);
let padding = self.core.style.padding;
let width = self.core.width(self.core.text(shown)) + 4.0 * padding;
let height = self.core.row_height();
let rect = self.allocate(Some(width), height);
let interaction = self.interact(id, rect);
let color = self.state_color(interaction);
self.fill(rect, color);
let at = rect.min + Vec2::new(2.0 * padding, padding);
self.write(at, shown, self.core.style.text, rect);
interaction.into()
}
pub fn checkbox(&mut self, text: impl fmt::Display, value: &mut bool) -> Response {
let (id, shown) = self.labeled(text);
let style = self.core.style;
let height = self.core.row_height();
let width = height + style.spacing + self.core.width(self.core.text(shown));
let rect = self.allocate(Some(width), height);
let interaction = self.interact(id, rect);
let mut response = Response::from(interaction);
if interaction.clicked {
*value = !*value;
response.changed = true;
}
let side = height - 2.0 * style.padding;
let mark = Rect::from_min_size(rect.min + Vec2::splat(style.padding), Vec2::splat(side));
let color = self.state_color(interaction);
self.fill(mark, color);
if *value {
let inner = Rect::new(mark.min + Vec2::splat(2.0), mark.max - Vec2::splat(2.0));
self.fill(inner, style.accent);
}
let at = Vec2::new(
rect.min.x + height + style.spacing,
rect.min.y + style.padding,
);
self.write(at, shown, style.text, rect);
response
}
pub fn slider_f32(
&mut self,
text: impl fmt::Display,
value: &mut f32,
range: RangeInclusive<f32>,
) -> Response {
let (id, shown) = self.labeled(text);
let (control, label) = self.control_row(shown);
let interaction = self.interact(id, control);
let mut response = Response::from(interaction);
let (low, high) = ordered(*range.start(), *range.end());
let usable = low.is_finite() && high.is_finite();
if interaction.held && usable {
let t = fraction(self.core.pointer.x, control);
let next = low * (1.0 - t) + high * t;
if next != *value {
*value = next;
response.changed = true;
}
}
let t = if usable && high > low {
unit((*value - low) / (high - low))
} else {
0.0
};
let shown_value = self.core.format(format_args!("{:.3}", *value));
self.draw_slider(interaction, control, t, shown_value);
self.write_label(label, shown);
response
}
pub fn slider_i32(
&mut self,
text: impl fmt::Display,
value: &mut i32,
range: RangeInclusive<i32>,
) -> Response {
let (id, shown) = self.labeled(text);
let (control, label) = self.control_row(shown);
let interaction = self.interact(id, control);
let mut response = Response::from(interaction);
let (low, high) = {
let (a, b) = (i64::from(*range.start()), i64::from(*range.end()));
(a.min(b), a.max(b))
};
let span = (high - low) as f64;
if interaction.held {
let t = f64::from(fraction(self.core.pointer.x, control));
let next = (low + (span * t).round() as i64).clamp(low, high);
let next = next as i32;
if next != *value {
*value = next;
response.changed = true;
}
}
let t = if span > 0.0 {
((i64::from(*value) - low) as f64 / span).clamp(0.0, 1.0) as f32
} else {
0.0
};
let shown_value = self.core.format(*value);
self.draw_slider(interaction, control, t, shown_value);
self.write_label(label, shown);
response
}
pub fn drag_f32(
&mut self,
text: impl fmt::Display,
value: &mut f32,
per_unit: f32,
) -> Response {
let (id, shown) = self.labeled(text);
let (control, label) = self.control_row(shown);
let interaction = self.interact(id, control);
let mut response = Response::from(interaction);
let pointer = self.core.pointer;
if let Some(active) = self.core.active_mut(id) {
let moved = pointer.x - active.last.x;
active.last = pointer;
let next = *value + moved * per_unit;
if next.is_finite() && next != *value {
*value = next;
response.changed = true;
}
}
let color = self.state_color(interaction);
self.fill(control, color);
let shown_value = self.core.format(format_args!("{:.3}", *value));
self.write_centered(control, shown_value);
self.write_label(label, shown);
response
}
pub fn text_input(&mut self, text: impl fmt::Display, value: &mut String) -> Response {
self.text_field(text, value, false).0
}
pub(crate) fn text_line(
&mut self,
text: impl fmt::Display,
value: &mut String,
) -> (Response, bool) {
self.text_field(text, value, true)
}
fn text_field(
&mut self,
text: impl fmt::Display,
value: &mut String,
whole: bool,
) -> (Response, bool) {
let (id, shown) = self.labeled(text);
let (control, label) = if whole {
let height = self.core.row_height();
let rect = self.allocate(None, height);
(rect, None)
} else {
let (control, label) = self.control_row(shown);
(control, Some(label))
};
let interaction = self.interact(id, control);
let mut response = Response::from(interaction);
let style = self.core.style;
let inner = Rect::new(
control.min + Vec2::new(style.padding, 0.0),
control.max - Vec2::new(style.padding, 0.0),
);
let core = &mut *self.core;
if core.lost == Some(id) {
core.lost = None;
response.lost_focus = true;
}
let mut focused = core.focus.is_some_and(|focus| focus.id == id);
let mut fresh = false;
if interaction.pressed {
if !focused {
take_focus(core, id, value);
focused = true;
}
if let (Some(focus), Some(at)) = (core.focus.as_mut(), core.press) {
let x = at.x - inner.min.x + focus.scroll;
focus.caret = Caret::at(nearest_caret(&*core.metrics, value, x));
}
} else if focused && core.press.is_some() {
core.focus = None;
focused = false;
response.lost_focus = true;
}
if core.focus_next {
core.focus_next = false;
if !focused {
take_focus(core, id, value);
focused = true;
fresh = true;
}
}
if focused && let Some(focus) = core.focus.as_mut() {
focus.seen = true;
if value.len() != focus.len || fnv1a64(value.as_bytes()) != focus.hash {
focus.caret = Caret::at(value.len());
focus.scroll = 0.0;
}
focus.caret.fit(value);
let mut escaped = false;
for &edit in core.input.edits.iter().filter(|_| !fresh) {
let edited = apply(value, &mut focus.caret, edit);
response.changed |= edited.changed;
response.submitted |= edited.submitted;
if edited.escaped {
escaped = true;
break;
}
}
focus.len = value.len();
focus.hash = fnv1a64(value.as_bytes());
let caret_x = core.metrics.caret_x(value, focus.caret.caret);
let end_x = core.metrics.caret_x(value, value.len());
let room = (inner.width() - 1.0).max(0.0);
if caret_x - focus.scroll > room {
focus.scroll = caret_x - room;
}
if caret_x < focus.scroll {
focus.scroll = caret_x;
}
focus.scroll = focus.scroll.min(end_x - room).max(0.0);
if escaped {
core.focus = None;
focused = false;
response.lost_focus = true;
}
}
let color = if focused {
style.active
} else {
self.state_color(interaction)
};
self.fill(control, color);
let field = self.clip.intersection(inner);
let focus = self.core.focus.filter(|_| focused);
let scroll = focus.map_or(0.0, |focus| focus.scroll);
let start = inner.min.x - scroll;
if let (Some(focus), Some(field)) = (focus, field) {
let (from, to) = focus.caret.selection();
if from != to {
let x0 = start + self.core.metrics.caret_x(value, from);
let x1 = start + self.core.metrics.caret_x(value, to);
let selection = Rect::new(
Vec2::new(x0, control.min.y + style.padding),
Vec2::new(x1, control.max.y - style.padding),
);
self.core
.rect(self.layer, field, selection, style.selection);
}
}
if let Some(field) = field {
let range = self.core.format(&*value);
let at = Vec2::new(start, control.min.y + style.padding);
self.core.text_at(self.layer, field, at, range, style.text);
}
if let (Some(focus), Some(field)) = (focus, field) {
let x = start + self.core.metrics.caret_x(value, focus.caret.caret);
let caret = Rect::from_min_size(
Vec2::new(x, control.min.y + style.padding),
Vec2::new(1.0, self.core.metrics.line_height()),
);
self.core.rect(self.layer, field, caret, style.text);
}
if let Some(label) = label {
self.write_label(label, shown);
}
(response, focused)
}
pub fn combo(
&mut self,
text: impl fmt::Display,
selected: &mut usize,
items: &[&str],
) -> Response {
let (id, shown) = self.labeled(text);
let (control, label) = self.control_row(shown);
let interaction = self.interact(id, control);
let mut response = Response::from(interaction);
let style = self.core.style;
let row = self.core.row_height();
let open = self.core.popup.is_some_and(|popup| popup.id == id);
if interaction.clicked {
self.core.popup = (!open).then_some(Popup {
id,
seen: true,
anchor: control,
rect: control,
});
}
let color = self.state_color(interaction);
self.fill(control, color);
if let Some(item) = items.get(*selected) {
let range = self.core.format(item);
let at = control.min + Vec2::splat(style.padding);
self.write(at, range, style.text, control);
}
self.write_label(label, shown);
if self.core.popup.is_some_and(|popup| popup.id == id) {
let screen = self.core.space.size;
let height = row * items.len() as f32;
let mut top = control.max.y;
if top + height > screen.y && control.min.y - height >= 0.0 {
top = control.min.y - height;
}
let rect = Rect::from_min_size(
Vec2::new(control.min.x, top),
Vec2::new(control.width(), height),
);
if let Some(popup) = self.core.popup.as_mut() {
popup.seen = true;
popup.anchor = control;
popup.rect = rect;
}
let (layer, clip) = (self.layer, self.clip);
self.layer = Layer::Popup;
self.clip = rect;
self.fill(rect, style.panel);
for (index, item) in items.iter().enumerate() {
let at = Rect::from_min_size(
Vec2::new(rect.min.x, rect.min.y + row * index as f32),
Vec2::new(rect.width(), row),
);
let picked = self.interact(id.with(index), at);
if picked.hovered || picked.held {
self.fill(at, style.hot);
}
let range = self.core.format(item);
self.write(at.min + Vec2::splat(style.padding), range, style.text, at);
if picked.clicked {
*selected = index;
response.changed = true;
self.core.popup = None;
}
}
self.layer = layer;
self.clip = clip;
}
response
}
pub fn tree_node(
&mut self,
text: impl fmt::Display,
add: impl FnOnce(&mut Ui<'_>),
) -> Response {
let (id, shown) = self.labeled(text);
let style = self.core.style;
let height = self.core.row_height();
let rect = self.allocate(None, height);
let interaction = self.interact(id, rect);
let mut response = Response::from(interaction);
let mut open = self.core.open.contains(&id);
if interaction.clicked {
open = !open;
if open {
self.core.open.insert(id);
} else {
self.core.open.remove(&id);
}
response.changed = true;
}
if interaction.hovered || interaction.held {
self.fill(rect, style.hot);
}
let mark = self.core.format(if open { "-" } else { "+" });
let at = rect.min + Vec2::splat(style.padding);
self.write(at, mark, style.text, rect);
let at = Vec2::new(rect.min.x + height, rect.min.y + style.padding);
self.write(at, shown, style.text, rect);
if open {
let (scope, indent) = (self.scope, self.indent);
self.scope = id;
self.indent += style.indent;
add(self);
self.scope = scope;
self.indent = indent;
}
response
}
pub fn separator(&mut self) {
let style = self.core.style;
let rect = self.allocate(None, style.spacing + 1.0);
let line = Rect::from_min_size(
Vec2::new(rect.min.x, rect.min.y + (style.spacing * 0.5).floor()),
Vec2::new(rect.width(), 1.0),
);
self.fill(line, style.dim);
}
pub fn plot(
&mut self,
text: impl fmt::Display,
values: impl IntoIterator<Item = f32>,
plot: Plot,
) -> PlotResponse {
let (id, shown) = self.labeled(text);
let style = self.core.style;
let text_width = self.core.width(self.core.text(shown));
let height = if plot.height.is_finite() && plot.height > 0.0 {
plot.height.max(1.0)
} else {
DEFAULT_PLOT_HEIGHT
};
let (graph, label) = self.control_rects(text_width, height);
let interaction = self.interact(id, graph);
self.core.values.clear();
self.core.values.extend(values);
let count = self.core.values.len();
let x_of = |index: usize| {
if count > 1 {
graph.min.x + graph.width() * index as f32 / (count - 1) as f32
} else {
graph.center().x
}
};
let index_at = |x: f32| {
if count == 0 {
return None;
}
let share = fraction(x, graph);
Some(((share * (count - 1) as f32).round() as usize).min(count - 1))
};
let (low, high) = ordered(*plot.range.start(), *plot.range.end());
let usable = low.is_finite() && high.is_finite() && high > low;
let y_of = |value: f32| {
let share = if usable {
unit((value - low) / (high - low))
} else {
0.5
};
graph.max.y - 0.5 - share * (graph.height() - 1.0)
};
self.fill(graph, style.widget);
let clip = self.clip.intersection(graph);
if let Some(clip) = clip {
if let Some(selected) = plot.selected.filter(|&index| index < count) {
let x = x_of(selected).min(graph.max.x - 1.0);
let line =
Rect::from_min_size(Vec2::new(x, graph.min.y), Vec2::new(1.0, graph.height()));
self.core.rect(self.layer, clip, line, style.hot);
}
let mut previous: Option<Vec2> = None;
for index in 0..count {
let value = self.core.values[index];
let point = value
.is_finite()
.then(|| Vec2::new(x_of(index), y_of(value)));
match (previous, point) {
(Some(from), Some(to)) => {
self.core.line(self.layer, clip, from, to, style.accent);
}
(None, Some(at))
if !self
.core
.values
.get(index + 1)
.is_some_and(|next| next.is_finite()) =>
{
let dot = Rect::from_min_size(at - Vec2::splat(0.5), Vec2::ONE);
self.core.rect(self.layer, clip, dot, style.accent);
}
_ => {}
}
previous = point;
}
}
self.write_label(label, shown);
let pointer = self.core.pointer;
let release = self.core.release.map(|(at, _)| at);
PlotResponse {
response: interaction.into(),
hovered: if interaction.hovered {
index_at(pointer.x)
} else {
None
},
clicked: if interaction.clicked {
release.and_then(|at| index_at(at.x))
} else {
None
},
}
}
pub fn color_edit(&mut self, text: impl fmt::Display, color: &mut Color) -> Response {
let (id, shown) = self.labeled(text);
let style = self.core.style;
let height = self.core.row_height();
let width = height + style.spacing + self.core.width(self.core.text(shown));
let rect = self.allocate(Some(width), height);
let swatch = Rect::from_min_size(
rect.min + Vec2::splat(style.padding),
Vec2::splat(height - 2.0 * style.padding),
);
self.fill(swatch, *color);
let at = Vec2::new(
rect.min.x + height + style.spacing,
rect.min.y + style.padding,
);
self.write(at, shown, style.text, rect);
let mut response = Response::default();
let scope = self.scope;
self.scope = id;
let mut channels = [color.r, color.g, color.b, color.a].map(i32::from);
for (name, channel) in ["R", "G", "B", "A"].into_iter().zip(&mut channels) {
let moved = self.slider_i32(name, channel, 0..=255);
response.changed |= moved.changed;
response.active |= moved.active;
response.hovered |= moved.hovered;
}
self.scope = scope;
if response.changed {
let [r, g, b, a] = channels.map(|channel| channel.clamp(0, 255) as u8);
*color = Color::rgba(r, g, b, a);
}
response
}
fn labeled(&mut self, text: impl fmt::Display) -> (Id, (u32, u32)) {
let (start, end) = self.core.format(text);
let (shown, key) = split(self.core.text((start, end)));
let id = self.scope.with(key);
(id, (start, start + shown.len() as u32))
}
fn allocate(&mut self, width: Option<f32>, height: f32) -> Rect {
let spacing = self.core.style.spacing;
if let Some(row) = self.row.as_mut() {
let width = width.unwrap_or(self.core.style.control_width);
let rect = Rect::from_min_size(Vec2::new(row.x, row.top), Vec2::new(width, height));
row.x += width + spacing;
row.height = row.height.max(height);
return rect;
}
let x = self.left + self.indent;
let width = width.unwrap_or(self.width - self.indent);
let rect = Rect::from_min_size(Vec2::new(x, self.y), Vec2::new(width, height));
self.y += height + spacing;
rect
}
fn control_row(&mut self, shown: (u32, u32)) -> (Rect, Vec2) {
let width = self.core.width(self.core.text(shown));
let height = self.core.row_height();
self.control_rects(width, height)
}
fn control_rects(&mut self, text_width: f32, height: f32) -> (Rect, Vec2) {
let style = self.core.style;
let (rect, control) = if self.row.is_some() {
let control = style.control_width;
(
self.allocate(Some(control + style.spacing + text_width), height),
control,
)
} else {
let rect = self.allocate(None, height);
(rect, (rect.width() * style.control_share).floor().max(0.0))
};
let control = Rect::from_min_size(rect.min, Vec2::new(control, height));
let label = Vec2::new(control.max.x + style.spacing, rect.min.y + style.padding);
(control, label)
}
fn interact(&mut self, id: Id, rect: Rect) -> Interaction {
self.core.interact(self.layer, self.clip, id, rect)
}
fn state_color(&self, interaction: Interaction) -> Color {
let style = &self.core.style;
if interaction.held {
style.active
} else if interaction.hovered {
style.hot
} else {
style.widget
}
}
fn fill(&mut self, rect: Rect, color: Color) {
self.core.rect(self.layer, self.clip, rect, color);
}
fn write(&mut self, at: Vec2, range: (u32, u32), color: Color, within: Rect) {
if let Some(clip) = self.clip.intersection(within) {
self.core.text_at(self.layer, clip, at, range, color);
}
}
fn write_centered(&mut self, control: Rect, range: (u32, u32)) {
let width = self.core.width(self.core.text(range));
let at = Vec2::new(
(control.center().x - width * 0.5).floor(),
control.min.y + self.core.style.padding,
);
let color = self.core.style.text;
self.write(at, range, color, control);
}
fn write_label(&mut self, at: Vec2, range: (u32, u32)) {
let color = self.core.style.text;
self.core.text_at(self.layer, self.clip, at, range, color);
}
fn draw_slider(&mut self, interaction: Interaction, control: Rect, t: f32, value: (u32, u32)) {
let style = self.core.style;
let color = self.state_color(interaction);
self.fill(control, color);
let handle_width = (control.height() * 0.5).floor().max(1.0);
let x = control.min.x + t * (control.width() - handle_width).max(0.0);
let handle = Rect::from_min_size(
Vec2::new(x, control.min.y),
Vec2::new(handle_width, control.height()),
);
self.fill(handle, style.accent);
self.write_centered(control, value);
}
}
fn ordered(a: f32, b: f32) -> (f32, f32) {
if b < a { (b, a) } else { (a, b) }
}
fn fraction(x: f32, rect: Rect) -> f32 {
unit((x - rect.min.x) / rect.width())
}
fn unit(share: f32) -> f32 {
if share.is_nan() {
0.0
} else {
share.clamp(0.0, 1.0)
}
}
fn take_focus(core: &mut DebugUi, id: Id, value: &str) {
if let Some(old) = core.focus.filter(|old| old.id != id) {
core.lost = Some(old.id);
}
if core.lost == Some(id) {
core.lost = None;
}
core.focus = Some(Focus {
id,
seen: true,
caret: Caret::at(value.len()),
scroll: 0.0,
len: value.len(),
hash: fnv1a64(value.as_bytes()),
});
}
fn nearest_caret(metrics: &dyn crate::TextMetrics, text: &str, x: f32) -> usize {
let mut best = (f32::INFINITY, 0);
for byte in text
.char_indices()
.map(|(byte, _)| byte)
.chain([text.len()])
{
let distance = (metrics.caret_x(text, byte) - x).abs();
if distance < best.0 {
best = (distance, byte);
}
}
best.1
}