use crate::color::Color;
use crate::event::{Cursor, Key, Mods};
use crate::geom::{Insets, Point, Rect, Size, pt, rect};
use crate::id::{ById, Id, fold, hash};
use crate::layout::{Align, BAR, Content, Flow, Length, NONE, Spec};
use crate::pen::Inked;
use crate::told::Role;
use crate::ui::{Measure, Response, Ui};
use std::ops::RangeInclusive;
pub(crate) const INNER: u64 = 0xb0a2_d1a2_b0a2_d1a2;
const BARS: [u64; 2] = [0xb0a2_dba2_0000_0001, 0xb0a2_dba2_0000_0002];
const STEP: f32 = 40.0;
const NEARER: f32 = 1.25;
const MARGIN: f32 = 24.0;
const LEGIBLE: f32 = 4.0;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Board {
pub x: f32,
pub y: f32,
pub zoom: f32,
}
impl Default for Board {
fn default() -> Self {
Board { x: 0.0, y: 0.0, zoom: 1.0 }
}
}
impl Board {
pub fn spot(&self, point: Point, at: Rect) -> Point {
pt(at.x + (point.x - self.x) * self.zoom, at.y + (point.y - self.y) * self.zoom)
}
pub fn point(&self, place: Point, at: Rect) -> Point {
pt(self.x + (place.x - at.x) / self.zoom, self.y + (place.y - at.y) / self.zoom)
}
pub fn sight(&self, size: Size) -> Rect {
rect(self.x, self.y, size.w / self.zoom, size.h / self.zoom)
}
pub fn zoom_at(&mut self, by: f32, at: Point, zoom: RangeInclusive<f32>) {
let was = self.zoom;
self.zoom = (self.zoom * by).clamp(*zoom.start(), *zoom.end());
(self.x, self.y) = (self.x + at.x / was - at.x / self.zoom, self.y + at.y / was - at.y / self.zoom);
}
pub fn fit(&mut self, all: Rect, size: Size, zoom: RangeInclusive<f32>) {
if all.w <= 0.0 && all.h <= 0.0 {
return;
}
let near = ((size.w - 2.0 * MARGIN) / all.w.max(1e-6)).min((size.h - 2.0 * MARGIN) / all.h.max(1e-6));
self.zoom = near.clamp(*zoom.start(), *zoom.end());
(self.x, self.y) = (all.x + all.w / 2.0 - size.w / 2.0 / self.zoom, all.y + all.h / 2.0 - size.h / 2.0 / self.zoom);
}
fn mend(&mut self, zoom: &RangeInclusive<f32>) {
let zoom_ok = self.zoom.is_finite() && self.zoom > 0.0;
self.zoom = if zoom_ok { self.zoom } else { 1.0 }.clamp(*zoom.start(), *zoom.end());
(self.x, self.y) = (if self.x.is_finite() { self.x } else { 0.0 }, if self.y.is_finite() { self.y } else { 0.0 });
}
}
#[derive(Default)]
pub(crate) struct Boards {
pub settle: Option<fn(&mut Ui)>,
pub run: Vec<OnRun>,
kept: ById<Kept>,
fit: bool,
inside: Vec<bool>,
pub graphs: crate::nodes::Graphs,
}
pub(crate) struct OnRun {
id: Id,
pub inner: u32,
zoom: f32,
pub scale: f32,
pub sight: Rect,
bounds: Option<Rect>,
bars: [Option<[f32; 2]>; 2],
}
#[derive(Clone, Copy, Default)]
struct Kept {
bounds: Option<Rect>,
dragged: Option<(usize, Point, [f32; 2])>,
held: Option<(usize, f32, [f32; 2])>,
focus_inside: bool,
}
fn bars(bounds: Option<Rect>, sight: Rect) -> [Option<[f32; 2]>; 2] {
let all = bounds.map_or(sight, |bounds| around(bounds, sight));
let along = |from: f32, long: f32, start: f32, whole: f32| (whole > long + 0.5).then(|| [(from - start) / whole, long / whole]);
[along(sight.x, sight.w, all.x, all.w), along(sight.y, sight.h, all.y, all.h)]
}
fn around(a: Rect, b: Rect) -> Rect {
let (x, y) = (a.x.min(b.x), a.y.min(b.y));
rect(x, y, a.right().max(b.right()) - x, a.bottom().max(b.bottom()) - y)
}
pub(crate) fn part_id(of: Id, key: u64) -> Id {
hash((of, hash(key)))
}
impl Ui {
#[cfg_attr(debug_assertions, track_caller)]
pub fn board(&mut self, board: &mut Board, name: &str, zoom: RangeInclusive<f32>) -> Response {
self.open_board(board, name, zoom, true, true)
}
pub fn work_board(&mut self, board: &mut Board, name: &str, zoom: RangeInclusive<f32>) -> Response {
self.open_board(board, name, zoom, false, true)
}
pub fn in_sight(&mut self, at: Rect) -> bool {
let Some(board) = self.board_here() else { return true };
let on = &mut self.boards.run[board];
on.bounds = Some(on.bounds.map_or(at, |bounds| around(bounds, at)));
let sight = on.sight;
at.x <= sight.right() && at.right() >= sight.x && at.y <= sight.bottom() && at.bottom() >= sight.y
}
pub fn fit_board(&mut self) {
(self.boards.fit, self.again) = (true, true);
}
fn board_here(&self) -> Option<usize> {
let layer = self.open.last()?.layer;
self.boards.run.iter().rposition(|board| self.open.iter().any(|open| open.at == board.inner && open.layer == layer))
}
pub(crate) fn zoom_here(&self) -> f32 {
self.board_here().map_or(1.0, |board| self.boards.run[board].scale)
}
#[cfg_attr(debug_assertions, track_caller)]
pub(crate) fn open_board(&mut self, board: &mut Board, name: &str, zoom: RangeInclusive<f32>, plain: bool, join: bool) -> Response {
self.boards.settle = Some(settle);
let zoom = zoom.start().max(1e-6)..=zoom.end().max(zoom.start().max(1e-6));
board.mend(&zoom);
let (id, outer) = (self.next_id(), self.zoom_here());
let ground = self.interact(id);
let inner_id = part_id(id, INNER);
let room = self.rooms.get(&inner_id).map_or([self.size().w, self.size().h], |room| [room[0], room[1]]);
let size = Size { w: room[0] / outer, h: room[1] / outer };
let mut kept = self.boards.kept.get(&id).copied().unwrap_or_default();
self.steer(board, &ground, &mut kept, size, outer, plain, &zoom);
let shown = board.zoom * outer;
let sight = board.sight(size);
let bars = bars(kept.bounds, sight);
let pad = Insets { right: if bars[1].is_some() { BAR } else { 0.0 }, bottom: if bars[0].is_some() { BAR } else { 0.0 }, ..Insets::ZERO };
self.el().flow(Flow::Layers).grow().pad(pad).clip().sense().focusable().open();
self.tell(&ground, Role::Group, None, name);
for (axis, along) in bars.into_iter().enumerate() {
self.bar(board, &mut kept, id, axis, along, size, sight);
}
self.join_next = join;
let inner = self.tree.specs.len() as u32;
self.el().key(INNER).flow(Flow::Layers).grow().shifted(board.x * shown, board.y * shown).open();
self.measures.push(Measure { at: inner, within: inner, id: inner_id, read: [room[0], room[1], 0.0] });
self.layout_key = fold(self.layout_key, board.zoom.to_bits() as u64);
self.boards.run.push(OnRun { id, inner, zoom: board.zoom, scale: shown, sight, bounds: None, bars });
self.boards.kept.insert(id, kept);
ground
}
#[allow(clippy::too_many_arguments)]
fn steer(&mut self, board: &mut Board, ground: &Response, kept: &mut Kept, size: Size, outer: f32, plain: bool, zoom: &RangeInclusive<f32>) {
if let Some(asked) = self.zoom_taken(ground) {
let shown = board.zoom * outer;
(board.x, board.y) = (board.x - asked.moved.x / shown, board.y - asked.moved.y / shown);
board.zoom_at(asked.by, pt((asked.at.x - ground.rect.x) / outer, (asked.at.y - ground.rect.y) / outer), zoom.clone());
}
let wheel = self.input.wheel;
if ground.over && wheel != [0.0; 2] && !self.scrolls_under(ground.rect) {
(board.x, board.y, self.input.wheel) = (board.x + wheel[0] / outer / board.zoom, board.y + wheel[1] / outer / board.zoom, [0.0; 2]);
}
self.drag_board(board, ground, kept, outer, plain);
let keyed = ground.focused || kept.focus_inside || self.focus == 0 && ground.over;
let middle = pt(size.w / 2.0, size.h / 2.0);
let both = Mods { shift: true, ..Mods::CTRL };
let nearer = [Key::Char('='), Key::Char('+')].iter().any(|key| keyed && (self.key(*key, Mods::CTRL) || self.key(*key, both)));
let further = keyed && self.key(Key::Char('-'), Mods::CTRL);
if nearer || further {
board.zoom_at(if nearer { NEARER } else { 1.0 / NEARER }, middle, zoom.clone());
}
if keyed && self.key(Key::Char('0'), Mods::CTRL) {
board.zoom_at(1.0 / board.zoom, middle, zoom.clone());
}
if std::mem::take(&mut self.boards.fit) | (keyed && self.key(Key::Char('1'), Mods::SHIFT)) {
board.fit(kept.bounds.unwrap_or_default(), size, zoom.clone());
}
let arrows = [Key::Left, Key::Right, Key::Up, Key::Down].map(|key| ground.focused && self.key(key, Mods::NONE));
let step = STEP / board.zoom;
board.x += step * (arrows[1] as i32 - arrows[0] as i32) as f32;
board.y += step * (arrows[3] as i32 - arrows[2] as i32) as f32;
}
fn scrolls_under(&self, r: Rect) -> bool {
let Some(p) = self.input.pointer else { return false };
let inside = |s: &Rect| s.x >= r.x && s.y >= r.y && s.right() <= r.right() && s.bottom() <= r.bottom() && (s.w < r.w || s.h < r.h);
self.survey.scrollers.iter().any(|scroller| scroller.seen.contains(p) && inside(&scroller.rect))
}
fn drag_board(&mut self, board: &mut Board, ground: &Response, kept: &mut Kept, outer: f32, plain: bool) {
let p = self.input.pointer.unwrap_or_default();
let spaced = ground.over && (self.focus == 0 || ground.focused) && self.key_held(Key::Space);
let button = match () {
_ if !ground.over => None,
_ if self.input.pressed[2] => Some(2),
_ if self.input.pressed[0] && (spaced || plain && ground.hovered) => Some(0),
_ => None,
};
if let Some(button) = button {
kept.dragged = Some((button, p, [board.x, board.y]));
self.input.pressed[0] &= !spaced;
}
if spaced {
self.set_cursor(Cursor::Move);
}
let Some((button, from, was)) = kept.dragged else { return };
if !self.input.down[button] {
kept.dragged = None;
return;
}
let shown = board.zoom * outer;
(board.x, board.y) = (was[0] - (p.x - from.x) / shown, was[1] - (p.y - from.y) / shown);
self.set_cursor(Cursor::Move);
}
#[allow(clippy::too_many_arguments)]
fn bar(&mut self, board: &mut Board, kept: &mut Kept, of: Id, axis: usize, along: Option<[f32; 2]>, size: Size, sight: Rect) {
let Some([from, long]) = along else {
kept.held = kept.held.filter(|held| held.0 != axis);
return;
};
let t = self.theme;
let id = part_id(of, BARS[axis]);
let strip = self.interact(id);
let run = [size.w, size.h][axis];
let (part_long, start) = ((long * run).max(24.0).min(run), [sight.x, sight.y][axis]);
let part_from = from * (run - part_long) / (1.0 - long).max(1e-6);
let whole = [start - from * [sight.w, sight.h][axis] / long, [sight.w, sight.h][axis] / long];
let p = self.input.pointer.map_or(0.0, |p| [p.x - strip.rect.x, p.y - strip.rect.y][axis]);
if strip.pressed && self.input.pressed[0] {
let caught = if p >= part_from && p < part_from + part_long { p - part_from } else { part_long / 2.0 };
kept.held = Some((axis, caught, whole));
}
if let Some((_, caught, [first, length])) = kept.held.filter(|held| held.0 == axis && strip.pressed) {
let to = first + (p - caught) / (run - part_long).max(1.0) * (length - [sight.w, sight.h][axis]);
let to = to.clamp(first, first + length - [sight.w, sight.h][axis]);
*[&mut board.x, &mut board.y][axis] = to;
}
let color = if strip.hovered || strip.pressed { t.text_faint } else { t.line_strong };
let (place, at) = ([(Align::Start, Align::End), (Align::End, Align::Start)][axis], [[0.0, BAR], [BAR, 0.0]][axis]);
let strip_box = self.el().key(BARS[axis]).flow(Flow::Layers).place(place).at(at[0], at[1]).sense();
let strip_box = if axis == 0 { strip_box.w(Length::Grow(1.0)).h(Length::Px(BAR)) } else { strip_box.w(Length::Px(BAR)).h(Length::Grow(1.0)) };
strip_box.open();
let (x, y, w, h) = if axis == 0 { (part_from + 2.0, 2.0, part_long - 4.0, 6.0) } else { (2.0, part_from + 2.0, 6.0, part_long - 4.0) };
self.el().at(x, y).size(w.max(0.0), h.max(0.0)).radius(3.0).fill(color).show();
self.close();
}
}
fn settle(ui: &mut Ui) {
let mut run = std::mem::take(&mut ui.boards.run);
for board in &run {
let end = ui.tree.specs[board.inner as usize].end;
zoom_boxes(ui, board.inner, end, board.zoom);
let focus = ui.focus;
let inside = focus != 0 && ui.tree.specs[board.inner as usize + 1..end as usize].iter().any(|spec| spec.id == focus);
let kept = ui.boards.kept.entry(board.id).or_default();
kept.focus_inside = inside;
if kept.bounds != board.bounds {
kept.bounds = board.bounds;
ui.again |= bars(board.bounds, board.sight) != board.bars;
}
}
run.clear();
ui.boards.run = run;
}
fn zoom_boxes(ui: &mut Ui, inner: u32, end: u32, by: f32) {
if by == 1.0 {
return;
}
let Ui { tree, shapes, boards, .. } = ui;
let inside = &mut boards.inside;
inside.clear();
inside.resize((end - inner) as usize, false);
inside[0] = true;
for at in inner + 1..end {
let spec = &mut tree.specs[at as usize];
let up = spec.frame.parent;
let on = up != NONE && up >= inner && inside.get((up - inner) as usize) == Some(&true);
inside[(at - inner) as usize] = on;
if on {
zoom_box(spec, shapes, by);
}
}
}
fn zoom_box(spec: &mut Spec, shapes: &mut [Inked], by: f32) {
let frame = &mut spec.frame;
for axis in 0..2 {
if let Length::Px(size) = &mut frame.size[axis] {
*size *= by;
}
(frame.min[axis], frame.max[axis], frame.nudge[axis]) = (frame.min[axis] * by, frame.max[axis] * by, frame.nudge[axis] * by);
}
let pad = frame.pad;
frame.pad = Insets { left: pad.left * by, top: pad.top * by, right: pad.right * by, bottom: pad.bottom * by };
(frame.gap, frame.shadow, spec.radius, spec.border_width) = (frame.gap * by, frame.shadow * by, spec.radius * by, spec.border_width * by);
match &mut spec.content {
Content::Text { font, color, .. } => {
font.size *= by;
*color = if font.size < LEGIBLE { Color::NONE } else { *color };
}
Content::Shape { shape } => {
if let Some(shape) = shapes.get_mut(*shape as usize) {
shape.scale_by(by);
}
}
_ => {}
}
}