From df2e366bf9ed831fec49c63c19a3f1efa4e24550 Mon Sep 17 00:00:00 2001 From: clover caruso Date: Mon, 30 Nov 2026 14:14:31 -0800 Subject: [PATCH] Split spline paint/label rendering into paint.rs (#16) --- crates/spline/src/lib.rs | 563 +--------------------------------- crates/spline/src/paint.rs | 599 +++++++++++++++++++++++++++++++++++++ 2 files changed, 600 insertions(+), 562 deletions(-) create mode 100644 crates/spline/src/paint.rs diff --git a/crates/spline/src/lib.rs b/crates/spline/src/lib.rs index ff42c0028668a9d4b8a730d699d4cb2d824d3cad..2b6b905756c6d380953d5fde99c860326f4526d2 100644 --- a/crates/spline/src/lib.rs +++ b/crates/spline/src/lib.rs @@ -8,19 +8,17 @@ //! - `Cmd+F` / Numpad `.` / `F` fit-to-view, `L` Linear/Smooth, `I` Insert key, `X/Del/BS` Delete //! - Crisp high-contrast curves (zero XOR!), generous hit targets, live GPU Viewer feedback. +mod paint; pub mod presets; pub use presets::EasingPreset; -use draw::{Glyphs, Primitive}; -use model::spline::{cubic, segment_control_points}; use model::{Comp, CompHistory, HandleMode, Keyframe, NodeId}; use presets::{ apply_easing_preset, apply_handle_mode, compute_curve_bounds, display_to_value, enforce_aligned_handle, value_to_display, }; use std::collections::{HashMap, HashSet}; -use std::sync::Arc; use winit::event::{ElementState, MouseButton}; use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; @@ -1271,540 +1269,6 @@ impl SplineView { self.v_range = [v0, v1]; } } - - fn hover_readout_string(&self, comp: &Comp) -> Option { - let hov = self.hovered.as_ref()?; - let sp = comp.splines.get(&hov.spline)?; - let kf = sp.keyframes.get(hov.key_idx)?; - let (t, v) = match hov.part { - HandlePart::Key => (kf.time, kf.value), - HandlePart::LeftHandle => { - let h = kf.lh?; - (h[0], h[1]) - } - HandlePart::RightHandle => { - let h = kf.rh?; - (h[0], h[1]) - } - }; - if (t - t.round()).abs() < 1e-3 { - Some(format!("(f{}, {:.3})", t.round() as i64, v)) - } else { - Some(format!("(f{t:.1}, {v:.3})")) - } - } - - pub fn prepare_labels(&mut self, comp: &Comp) { - let font_sm = text::Font { - families: Arc::from([text::Family::Mono]), - size: 10.0, - weight: 500, - italic: false, - }; - let font_ch = text::Font { - families: Arc::from([text::Family::Sans]), - size: 11.0, - weight: 500, - italic: false, - }; - let make_layout = |s: &str, font: &text::Font| { - let styles = [text::Style::new(font.clone())]; - text::Paragraph { - text: s, - styles: &styles, - spans: &[], - replacements: &[], - width: None, - indent: 0.0, - line_height: 1.2, - align: text::Align::Left, - tab: 32.0, - } - .layout() - }; - for (label, _) in &self.channels { - self.text_cache.entry(label.clone()).or_insert_with(|| make_layout(label, &font_ch)); - } - let header = self - .current_node - .map(|id| format!("Splines: {}", comp.node(id).name)) - .unwrap_or_else(|| "Splines (Comp)".to_owned()); - self.text_cache.entry(header.clone()).or_insert_with(|| make_layout(&header, &font_ch)); - self.text_cache - .entry("Norm".to_owned()) - .or_insert_with(|| make_layout("Norm", &font_sm)); - for popup_hdr in ["Set Handle Type", "Set Interpolation"] { - self.text_cache - .entry(popup_hdr.to_owned()) - .or_insert_with(|| make_layout(popup_hdr, &font_sm)); - } - for &(_, item_lbl) in HANDLE_MENU_ITEMS { - self.text_cache - .entry(item_lbl.to_owned()) - .or_insert_with(|| make_layout(item_lbl, &font_ch)); - } - for &(_, item_lbl) in EASING_MENU_ITEMS { - self.text_cache - .entry(item_lbl.to_owned()) - .or_insert_with(|| make_layout(item_lbl, &font_ch)); - } - for hud in [ - "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", - "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", - "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", - "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", - ] { - self.text_cache.entry(hud.to_owned()).or_insert_with(|| make_layout(hud, &font_sm)); - } - - if let Some(readout) = self.hover_readout_string(comp) { - if readout != self.last_hover_readout || !self.text_cache.contains_key("__hover_readout") { - self.text_cache - .insert("__hover_readout".to_owned(), make_layout(&readout, &font_sm)); - self.last_hover_readout = readout; - } - } - - let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); - let t_start = (self.t_range[0] / t_step).floor() as i64; - let t_end = (self.t_range[1] / t_step).ceil() as i64; - for ti in t_start..=t_end { - let key = format!("t:{}", (ti as f64 * t_step).round() as i64); - self.text_cache.entry(key.clone()).or_insert_with(|| make_layout(&key[2..], &font_sm)); - } - let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); - let v_start = (self.v_range[0] / v_step).floor() as i64; - let v_end = (self.v_range[1] / v_step).ceil() as i64; - for vi in v_start..=v_end { - let val = vi as f64 * v_step; - let s = format!("{val:.2}"); - let key = format!("v:{s}"); - self.text_cache.entry(key).or_insert_with(|| make_layout(&s, &font_sm)); - } - } - - pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec> { - let mut out = Vec::with_capacity(512); - let [w, h] = self.size; - let plot = self.plot_rect(); - - out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); - out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); - out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); - out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); - - let header = self - .current_node - .map(|id| format!("Splines: {}", comp.node(id).name)) - .unwrap_or_else(|| "Splines (Comp)".to_owned()); - if let Some(hl) = self.text_cache.get(&header) { - out.push(Primitive::Text { - text: hl as &dyn Glyphs, - origin: [10.0, 5.0], - clip: Some([8.0, 0.0, SIDEBAR_W - 70.0, RULER_H]), - ink: self.palette.text, - }); - } - - // Clickable "Normalize" pill in the sidebar header - let pill = self.normalize_pill_rect(); - let pill_fill = if self.normalized { - [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] - } else { - [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.9] - }; - let pill_stroke = if self.normalized { self.palette.accent } else { self.palette.rule }; - out.push(Primitive::RoundedRect { - rect: pill, - radius: 4.0, - stroke: None, - color: pill_fill, - }); - out.push(Primitive::RoundedRect { - rect: pill, - radius: 4.0, - stroke: Some(1.0), - color: pill_stroke, - }); - if let Some(nl) = self.text_cache.get("Norm") { - out.push(Primitive::Text { - text: nl as &dyn Glyphs, - origin: [pill[0] + 7.0, pill[1] + 3.0], - clip: Some(pill), - ink: if self.normalized { self.palette.text } else { self.palette.text_dim }, - }); - } - - for (i, (label, sname)) in self.channels.iter().enumerate() { - let y = RULER_H + 6.0 + i as f32 * 22.0; - if y > h - 24.0 { - break; - } - let hidden = self.hidden_splines.contains(sname); - let col = spline_color(comp, sname, i); - out.push(Primitive::RoundedRect { - rect: [10.0, y + 4.0, 20.0, y + 14.0], - radius: 3.0, - stroke: if hidden { Some(1.2) } else { None }, - color: if hidden { self.palette.text_dim } else { col }, - }); - if let Some(tl) = self.text_cache.get(label) { - out.push(Primitive::Text { - text: tl as &dyn Glyphs, - origin: [26.0, y + 2.0], - clip: Some([26.0, y, SIDEBAR_W - 8.0, y + 20.0]), - ink: if hidden { self.palette.text_dim } else { self.palette.text }, - }); - } - } - - let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); - let t_start = (self.t_range[0] / t_step).floor() as i64; - let t_end = (self.t_range[1] / t_step).ceil() as i64; - for ti in t_start..=t_end { - let t = ti as f64 * t_step; - let x = self.data_to_local(t, 0.0)[0]; - if x < plot[0] || x > plot[2] { - continue; - } - out.push(Primitive::Segment { from: [x, plot[1]], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); - let key = format!("t:{}", t.round() as i64); - if let Some(tl) = self.text_cache.get(&key) { - out.push(Primitive::Text { - text: tl as &dyn Glyphs, - origin: [x + 3.0, 6.0], - clip: Some([plot[0], 0.0, plot[2], RULER_H]), - ink: self.palette.text_dim, - }); - } - } - - let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); - let v_start = (self.v_range[0] / v_step).floor() as i64; - let v_end = (self.v_range[1] / v_step).ceil() as i64; - for vi in v_start..=v_end { - let v = vi as f64 * v_step; - let y = self.data_to_local(0.0, v)[1]; - if y < plot[1] || y > plot[3] { - continue; - } - let is_zero = v.abs() < 1e-6; - out.push(Primitive::Segment { - from: [plot[0], y], - to: [plot[2], y], - width: if is_zero { 1.3 } else { 1.0 }, - color: if is_zero { self.palette.zero_line } else { self.palette.grid }, - }); - let key = format!("v:{v:.2}"); - if let Some(vl) = self.text_cache.get(&key) { - out.push(Primitive::Text { - text: vl as &dyn Glyphs, - origin: [plot[0] + 4.0, y - 12.0], - clip: Some(plot), - ink: self.palette.text_dim, - }); - } - } - - out.push(Primitive::PushClip { rect: plot, radius: 0.0 }); - - if let Some(m) = &self.modal { - let piv = self.data_to_local(m.pivot[0], m.pivot[1]); - match m.axis { - AxisLock::X => out.push(Primitive::Segment { - from: [plot[0], piv[1]], - to: [plot[2], piv[1]], - width: 1.5, - color: [0.95, 0.28, 0.34, 0.85], - }), - AxisLock::Y => out.push(Primitive::Segment { - from: [piv[0], plot[1]], - to: [piv[0], plot[3]], - width: 1.5, - color: [0.28, 0.90, 0.42, 0.85], - }), - AxisLock::None => {} - } - } - - let px = self.data_to_local(current_frame, 0.0)[0]; - if px >= plot[0] && px <= plot[2] { - out.push(Primitive::Segment { - from: [px, plot[1]], - to: [px, plot[3]], - width: 1.6, - color: [0.96, 0.36, 0.34, 0.90], - }); - } - - for (ci, (_, sname)) in self.channels.iter().enumerate() { - if self.hidden_splines.contains(sname) { - continue; - } - let Some(sp) = comp.splines.get(sname) else { continue }; - let bounds = self.spline_bounds(comp, sname); - let col = spline_color(comp, sname, ci); - let any_sel = self.selected.iter().any(|p| p.spline == *sname); - let curve_w = if any_sel { 2.4 } else { 1.7 }; - let curve_col = if any_sel { - [(col[0] + 0.15).min(1.0), (col[1] + 0.15).min(1.0), (col[2] + 0.15).min(1.0), 1.0] - } else { - col - }; - - if let Some(first) = sp.keyframes.first() { - let p = self.spline_to_local(first.time, first.value, bounds); - if p[0] > plot[0] { - out.push(Primitive::Segment { - from: [plot[0], p[1]], - to: p, - width: 1.1, - color: [curve_col[0], curve_col[1], curve_col[2], 0.35], - }); - } - } - if let Some(last) = sp.keyframes.last() { - let p = self.spline_to_local(last.time, last.value, bounds); - if p[0] < plot[2] { - out.push(Primitive::Segment { - from: p, - to: [plot[2], p[1]], - width: 1.1, - color: [curve_col[0], curve_col[1], curve_col[2], 0.35], - }); - } - } - - let dense = sp.keyframes.len() > 250; - for seg in 0..sp.keyframes.len().saturating_sub(1) { - let k0 = &sp.keyframes[seg]; - let k1 = &sp.keyframes[seg + 1]; - if k1.time < self.t_range[0] || k0.time > self.t_range[1] { - continue; - } - if k0.flags.step_out { - // Vertical step at k0.time, then horizontal to k1.time (matches model::spline::eval_segment). - let a = self.spline_to_local(k0.time, k0.value, bounds); - let m = self.spline_to_local(k0.time, k1.value, bounds); - let b = self.spline_to_local(k1.time, k1.value, bounds); - out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); - out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); - } else if k1.flags.step_in { - let a = self.spline_to_local(k0.time, k0.value, bounds); - let m = self.spline_to_local(k1.time, k0.value, bounds); - let b = self.spline_to_local(k1.time, k1.value, bounds); - out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); - out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); - } else if dense || (k1.flags.linear && k0.rh.is_none() && k1.lh.is_none()) { - let a = self.spline_to_local(k0.time, k0.value, bounds); - let b = self.spline_to_local(k1.time, k1.value, bounds); - out.push(Primitive::Segment { from: a, to: b, width: curve_w, color: curve_col }); - } else { - let [p0, p1, p2, p3] = segment_control_points(k0, k1); - const SUBS: usize = 20; - let mut prev = self.spline_to_local(p0[0], p0[1], bounds); - for s in 1..=SUBS { - let u = s as f64 / SUBS as f64; - let t = cubic(p0[0], p1[0], p2[0], p3[0], u); - let v = cubic(p0[1], p1[1], p2[1], p3[1], u); - let cur = self.spline_to_local(t, v, bounds); - out.push(Primitive::Segment { from: prev, to: cur, width: curve_w, color: curve_col }); - prev = cur; - } - } - } - - if !dense || (self.t_range[1] - self.t_range[0]) < 180.0 { - for (ki, kf) in sp.keyframes.iter().enumerate() { - if kf.time < self.t_range[0] - 5.0 || kf.time > self.t_range[1] + 5.0 { - continue; - } - let kp = self.spline_to_local(kf.time, kf.value, bounds); - let kref = PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::Key }; - let ksel = self.selected.contains(&kref); - let khov = self.hovered.as_ref() == Some(&kref); - let show_handles = self.is_key_or_handle_selected(sname, ki); - - if show_handles { - for (part, hopt) in [(HandlePart::LeftHandle, kf.lh), (HandlePart::RightHandle, kf.rh)] { - if let Some(h) = hopt { - let hp = self.spline_to_local(h[0], h[1], bounds); - let href = PointRef { spline: sname.clone(), key_idx: ki, part }; - let hsel = self.selected.contains(&href); - let hhov = self.hovered.as_ref() == Some(&href); - out.push(Primitive::Segment { - from: kp, - to: hp, - width: 1.4, - color: if hsel { [1.0, 0.85, 0.35, 1.0] } else { [0.75, 0.78, 0.86, 0.75] }, - }); - let hr = if hsel || hhov { 5.2 } else { 4.2 }; - out.push(Primitive::RoundedRect { - rect: [hp[0] - hr, hp[1] - hr, hp[0] + hr, hp[1] + hr], - radius: hr, - stroke: None, - color: if hsel { [1.0, 0.86, 0.28, 1.0] } else if hhov { [1.0, 1.0, 1.0, 1.0] } else { [0.85, 0.50, 0.95, 1.0] }, - }); - } - } - } - - let kr = if ksel || khov { 5.4 } else { 4.2 }; - out.push(Primitive::RoundedRect { - rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], - radius: 1.5, - stroke: None, - color: if ksel { [1.0, 0.84, 0.24, 1.0] } else if khov { [1.0, 1.0, 1.0, 1.0] } else { curve_col }, - }); - out.push(Primitive::RoundedRect { - rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], - radius: 1.5, - stroke: Some(1.2), - color: [0.02, 0.02, 0.03, 1.0], - }); - } - } - } - - // Hover readout pill showing real `(f, )` for the hovered keyframe/handle - if let (Some(hov), Some(rl)) = (self.hovered.as_ref(), self.text_cache.get("__hover_readout")) { - if let Some(sp) = comp.splines.get(&hov.spline) { - if let Some(kf) = sp.keyframes.get(hov.key_idx) { - let bounds = self.spline_bounds(comp, &hov.spline); - let pt = match hov.part { - HandlePart::Key => Some([kf.time, kf.value]), - HandlePart::LeftHandle => kf.lh, - HandlePart::RightHandle => kf.rh, - }; - if let Some([t, v]) = pt { - let p = self.spline_to_local(t, v, bounds); - let pill_w = (self.last_hover_readout.len() as f32 * 6.4 + 12.0).max(76.0); - let pill_h = 18.0; - let rx = (p[0] + 10.0).clamp(plot[0] + 4.0, (plot[2] - pill_w - 4.0).max(plot[0] + 4.0)); - let ry = (p[1] - pill_h - 8.0).clamp(plot[1] + 4.0, (plot[3] - pill_h - 4.0).max(plot[1] + 4.0)); - let r = [rx, ry, rx + pill_w, ry + pill_h]; - out.push(Primitive::RoundedRect { - rect: r, - radius: 4.0, - stroke: None, - color: [0.08, 0.09, 0.11, 0.94], - }); - out.push(Primitive::RoundedRect { - rect: r, - radius: 4.0, - stroke: Some(1.0), - color: self.palette.accent, - }); - out.push(Primitive::Text { - text: rl as &dyn Glyphs, - origin: [rx + 6.0, ry + 3.0], - clip: Some(r), - ink: self.palette.text, - }); - } - } - } - } - - if let Some(bs) = &self.box_select { - let x0 = bs.start_local[0].min(bs.cur_local[0]); - let x1 = bs.start_local[0].max(bs.cur_local[0]); - let y0 = bs.start_local[1].min(bs.cur_local[1]); - let y1 = bs.start_local[1].max(bs.cur_local[1]); - out.push(Primitive::RoundedRect { - rect: [x0, y0, x1, y1], - radius: 2.0, - stroke: None, - color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], - }); - out.push(Primitive::RoundedRect { - rect: [x0, y0, x1, y1], - radius: 2.0, - stroke: Some(1.2), - color: self.palette.accent, - }); - } - - out.push(Primitive::PopClip); - - let hud_key = match self.modal.as_ref().map(|m| m.mode) { - Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", - Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", - Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", - None => "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", - }; - out.push(Primitive::Rect { rect: [SIDEBAR_W, h - 24.0, w, h], color: self.palette.ruler }); - if let Some(hl) = self.text_cache.get(hud_key) { - out.push(Primitive::Text { - text: hl as &dyn Glyphs, - origin: [SIDEBAR_W + 10.0, h - 18.0], - clip: Some([SIDEBAR_W + 6.0, h - 24.0, w - 6.0, h]), - ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, - }); - } - - // Floating Blender-style popup menu (`V` Handle Type / `T` Interpolation & Easing) - if let Some(popup) = &self.popup { - let pr = self.popup_rect(popup); - let hov_idx = self.popup_item_at(popup, self.pointer_local); - out.push(Primitive::RoundedRect { - rect: pr, - radius: 6.0, - stroke: None, - color: [0.09, 0.10, 0.12, 0.96], - }); - out.push(Primitive::RoundedRect { - rect: pr, - radius: 6.0, - stroke: Some(1.0), - color: self.palette.rule, - }); - let hdr_text = match popup.kind { - PopupKind::HandleType => "Set Handle Type", - PopupKind::Easing => "Set Interpolation", - }; - if let Some(hl) = self.text_cache.get(hdr_text) { - out.push(Primitive::Text { - text: hl as &dyn Glyphs, - origin: [pr[0] + 10.0, pr[1] + 5.0], - clip: Some([pr[0], pr[1], pr[2], pr[1] + POPUP_HEADER_H]), - ink: self.palette.text_dim, - }); - } - out.push(Primitive::Segment { - from: [pr[0] + 6.0, pr[1] + POPUP_HEADER_H - 1.0], - to: [pr[2] - 6.0, pr[1] + POPUP_HEADER_H - 1.0], - width: 1.0, - color: self.palette.rule, - }); - let labels: Vec<&'static str> = match popup.kind { - PopupKind::HandleType => HANDLE_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), - PopupKind::Easing => EASING_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), - }; - for (idx, lbl) in labels.into_iter().enumerate() { - let ry0 = pr[1] + POPUP_HEADER_H + idx as f32 * POPUP_ROW_H; - let ry1 = ry0 + POPUP_ROW_H; - if hov_idx == Some(idx) { - out.push(Primitive::RoundedRect { - rect: [pr[0] + 4.0, ry0 + 1.0, pr[2] - 4.0, ry1 - 1.0], - radius: 4.0, - stroke: None, - color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.25], - }); - } - if let Some(il) = self.text_cache.get(lbl) { - out.push(Primitive::Text { - text: il as &dyn Glyphs, - origin: [pr[0] + 10.0, ry0 + 4.0], - clip: Some([pr[0] + 4.0, ry0, pr[2] - 4.0, ry1]), - ink: self.palette.text, - }); - } - } - } - - out - } } fn point_in_rect(p: [f32; 2], r: [f32; 4]) -> bool { @@ -1820,31 +1284,6 @@ fn ensure_handles(kf: &mut Keyframe) { } } -fn spline_color(comp: &Comp, name: &str, idx: usize) -> [f32; 4] { - if let Some(sp) = comp.splines.get(name) { - let [r, g, b] = sp.color; - if r as u16 + g as u16 + b as u16 > 120 { - return [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0]; - } - } - const PALETTE: &[[f32; 4]] = &[ - [0.98, 0.44, 0.62, 1.0], - [0.36, 0.84, 0.96, 1.0], - [0.50, 0.92, 0.46, 1.0], - [0.98, 0.78, 0.28, 1.0], - [0.74, 0.56, 0.98, 1.0], - ]; - PALETTE[idx % PALETTE.len()] -} - -fn nice_step(raw: f64) -> f64 { - let raw = raw.max(1e-6); - let pow = 10.0_f64.powf(raw.log10().floor()); - let frac = raw / pow; - let nice = if frac < 1.5 { 1.0 } else if frac < 3.5 { 2.0 } else if frac < 7.5 { 5.0 } else { 10.0 }; - nice * pow -} - fn hypot2(a: [f32; 2], b: [f32; 2]) -> f32 { ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2)).sqrt() } diff --git a/crates/spline/src/paint.rs b/crates/spline/src/paint.rs new file mode 100644 index 0000000000000000000000000000000000000000..e4260a910b2f0da8fe76ecd78e2ec9a581575dbe --- /dev/null +++ b/crates/spline/src/paint.rs @@ -0,0 +1,599 @@ +//! Label layout and primitive painting for [`SplineView`]. + +use super::{ + AxisLock, EASING_MENU_ITEMS, HANDLE_MENU_ITEMS, HandlePart, POPUP_HEADER_H, POPUP_ROW_H, + PointRef, PopupKind, RULER_H, SIDEBAR_W, SplineView, TransformMode, +}; +use draw::{Glyphs, Primitive}; +use model::Comp; +use model::spline::{cubic, segment_control_points}; +use std::sync::Arc; + +impl SplineView { + fn hover_readout_string(&self, comp: &Comp) -> Option { + let hov = self.hovered.as_ref()?; + let sp = comp.splines.get(&hov.spline)?; + let kf = sp.keyframes.get(hov.key_idx)?; + let (t, v) = match hov.part { + HandlePart::Key => (kf.time, kf.value), + HandlePart::LeftHandle => { + let h = kf.lh?; + (h[0], h[1]) + } + HandlePart::RightHandle => { + let h = kf.rh?; + (h[0], h[1]) + } + }; + if (t - t.round()).abs() < 1e-3 { + Some(format!("(f{}, {:.3})", t.round() as i64, v)) + } else { + Some(format!("(f{t:.1}, {v:.3})")) + } + } + + pub fn prepare_labels(&mut self, comp: &Comp) { + let font_sm = text::Font { + families: Arc::from([text::Family::Mono]), + size: 10.0, + weight: 500, + italic: false, + }; + let font_ch = text::Font { + families: Arc::from([text::Family::Sans]), + size: 11.0, + weight: 500, + italic: false, + }; + let make_layout = |s: &str, font: &text::Font| { + let styles = [text::Style::new(font.clone())]; + text::Paragraph { + text: s, + styles: &styles, + spans: &[], + replacements: &[], + width: None, + indent: 0.0, + line_height: 1.2, + align: text::Align::Left, + tab: 32.0, + } + .layout() + }; + for (label, _) in &self.channels { + self.text_cache.entry(label.clone()).or_insert_with(|| make_layout(label, &font_ch)); + } + let header = self + .current_node + .map(|id| format!("Splines: {}", comp.node(id).name)) + .unwrap_or_else(|| "Splines (Comp)".to_owned()); + self.text_cache.entry(header.clone()).or_insert_with(|| make_layout(&header, &font_ch)); + self.text_cache + .entry("Norm".to_owned()) + .or_insert_with(|| make_layout("Norm", &font_sm)); + for popup_hdr in ["Set Handle Type", "Set Interpolation"] { + self.text_cache + .entry(popup_hdr.to_owned()) + .or_insert_with(|| make_layout(popup_hdr, &font_sm)); + } + for &(_, item_lbl) in HANDLE_MENU_ITEMS { + self.text_cache + .entry(item_lbl.to_owned()) + .or_insert_with(|| make_layout(item_lbl, &font_ch)); + } + for &(_, item_lbl) in EASING_MENU_ITEMS { + self.text_cache + .entry(item_lbl.to_owned()) + .or_insert_with(|| make_layout(item_lbl, &font_ch)); + } + for hud in [ + "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", + "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", + "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", + "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", + ] { + self.text_cache.entry(hud.to_owned()).or_insert_with(|| make_layout(hud, &font_sm)); + } + + if let Some(readout) = self.hover_readout_string(comp) { + if readout != self.last_hover_readout || !self.text_cache.contains_key("__hover_readout") { + self.text_cache + .insert("__hover_readout".to_owned(), make_layout(&readout, &font_sm)); + self.last_hover_readout = readout; + } + } + + let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); + let t_start = (self.t_range[0] / t_step).floor() as i64; + let t_end = (self.t_range[1] / t_step).ceil() as i64; + for ti in t_start..=t_end { + let key = format!("t:{}", (ti as f64 * t_step).round() as i64); + self.text_cache.entry(key.clone()).or_insert_with(|| make_layout(&key[2..], &font_sm)); + } + let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); + let v_start = (self.v_range[0] / v_step).floor() as i64; + let v_end = (self.v_range[1] / v_step).ceil() as i64; + for vi in v_start..=v_end { + let val = vi as f64 * v_step; + let s = format!("{val:.2}"); + let key = format!("v:{s}"); + self.text_cache.entry(key).or_insert_with(|| make_layout(&s, &font_sm)); + } + } + + pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec> { + let mut out = Vec::with_capacity(512); + let [w, h] = self.size; + let plot = self.plot_rect(); + + self.paint_chrome_and_sidebar(comp, w, h, &mut out); + self.paint_grid_and_rulers(plot, &mut out); + + out.push(Primitive::PushClip { rect: plot, radius: 0.0 }); + + self.paint_modal_guides(plot, &mut out); + + let px = self.data_to_local(current_frame, 0.0)[0]; + if px >= plot[0] && px <= plot[2] { + out.push(Primitive::Segment { + from: [px, plot[1]], + to: [px, plot[3]], + width: 1.6, + color: [0.96, 0.36, 0.34, 0.90], + }); + } + + self.paint_curves_and_handles(comp, plot, &mut out); + self.paint_hover_readout(comp, plot, &mut out); + self.paint_box_select(&mut out); + + out.push(Primitive::PopClip); + + self.paint_hud(w, h, &mut out); + self.paint_popup(&mut out); + + out + } + + fn paint_chrome_and_sidebar<'a>(&'a self, comp: &'a Comp, w: f32, h: f32, out: &mut Vec>) { + out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); + out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); + out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); + out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); + + let header = self + .current_node + .map(|id| format!("Splines: {}", comp.node(id).name)) + .unwrap_or_else(|| "Splines (Comp)".to_owned()); + if let Some(hl) = self.text_cache.get(&header) { + out.push(Primitive::Text { + text: hl as &dyn Glyphs, + origin: [10.0, 5.0], + clip: Some([8.0, 0.0, SIDEBAR_W - 70.0, RULER_H]), + ink: self.palette.text, + }); + } + + // Clickable "Normalize" pill in the sidebar header + let pill = self.normalize_pill_rect(); + let pill_fill = if self.normalized { + [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] + } else { + [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.9] + }; + let pill_stroke = if self.normalized { self.palette.accent } else { self.palette.rule }; + out.push(Primitive::RoundedRect { + rect: pill, + radius: 4.0, + stroke: None, + color: pill_fill, + }); + out.push(Primitive::RoundedRect { + rect: pill, + radius: 4.0, + stroke: Some(1.0), + color: pill_stroke, + }); + if let Some(nl) = self.text_cache.get("Norm") { + out.push(Primitive::Text { + text: nl as &dyn Glyphs, + origin: [pill[0] + 7.0, pill[1] + 3.0], + clip: Some(pill), + ink: if self.normalized { self.palette.text } else { self.palette.text_dim }, + }); + } + + for (i, (label, sname)) in self.channels.iter().enumerate() { + let y = RULER_H + 6.0 + i as f32 * 22.0; + if y > h - 24.0 { + break; + } + let hidden = self.hidden_splines.contains(sname); + let col = spline_color(comp, sname, i); + out.push(Primitive::RoundedRect { + rect: [10.0, y + 4.0, 20.0, y + 14.0], + radius: 3.0, + stroke: if hidden { Some(1.2) } else { None }, + color: if hidden { self.palette.text_dim } else { col }, + }); + if let Some(tl) = self.text_cache.get(label) { + out.push(Primitive::Text { + text: tl as &dyn Glyphs, + origin: [26.0, y + 2.0], + clip: Some([26.0, y, SIDEBAR_W - 8.0, y + 20.0]), + ink: if hidden { self.palette.text_dim } else { self.palette.text }, + }); + } + } + } + + fn paint_grid_and_rulers<'a>(&'a self, plot: [f32; 4], out: &mut Vec>) { + let t_step = nice_step((self.t_range[1] - self.t_range[0]) / 8.0); + let t_start = (self.t_range[0] / t_step).floor() as i64; + let t_end = (self.t_range[1] / t_step).ceil() as i64; + for ti in t_start..=t_end { + let t = ti as f64 * t_step; + let x = self.data_to_local(t, 0.0)[0]; + if x < plot[0] || x > plot[2] { + continue; + } + out.push(Primitive::Segment { from: [x, plot[1]], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); + let key = format!("t:{}", t.round() as i64); + if let Some(tl) = self.text_cache.get(&key) { + out.push(Primitive::Text { + text: tl as &dyn Glyphs, + origin: [x + 3.0, 6.0], + clip: Some([plot[0], 0.0, plot[2], RULER_H]), + ink: self.palette.text_dim, + }); + } + } + + let v_step = nice_step((self.v_range[1] - self.v_range[0]) / 6.0); + let v_start = (self.v_range[0] / v_step).floor() as i64; + let v_end = (self.v_range[1] / v_step).ceil() as i64; + for vi in v_start..=v_end { + let v = vi as f64 * v_step; + let y = self.data_to_local(0.0, v)[1]; + if y < plot[1] || y > plot[3] { + continue; + } + let is_zero = v.abs() < 1e-6; + out.push(Primitive::Segment { + from: [plot[0], y], + to: [plot[2], y], + width: if is_zero { 1.3 } else { 1.0 }, + color: if is_zero { self.palette.zero_line } else { self.palette.grid }, + }); + let key = format!("v:{v:.2}"); + if let Some(vl) = self.text_cache.get(&key) { + out.push(Primitive::Text { + text: vl as &dyn Glyphs, + origin: [plot[0] + 4.0, y - 12.0], + clip: Some(plot), + ink: self.palette.text_dim, + }); + } + } + } + + fn paint_modal_guides<'a>(&'a self, plot: [f32; 4], out: &mut Vec>) { + if let Some(m) = &self.modal { + let piv = self.data_to_local(m.pivot[0], m.pivot[1]); + match m.axis { + AxisLock::X => out.push(Primitive::Segment { + from: [plot[0], piv[1]], + to: [plot[2], piv[1]], + width: 1.5, + color: [0.95, 0.28, 0.34, 0.85], + }), + AxisLock::Y => out.push(Primitive::Segment { + from: [piv[0], plot[1]], + to: [piv[0], plot[3]], + width: 1.5, + color: [0.28, 0.90, 0.42, 0.85], + }), + AxisLock::None => {} + } + } + } + + fn paint_curves_and_handles<'a>(&'a self, comp: &'a Comp, plot: [f32; 4], out: &mut Vec>) { + for (ci, (_, sname)) in self.channels.iter().enumerate() { + if self.hidden_splines.contains(sname) { + continue; + } + let Some(sp) = comp.splines.get(sname) else { continue }; + let bounds = self.spline_bounds(comp, sname); + let col = spline_color(comp, sname, ci); + let any_sel = self.selected.iter().any(|p| p.spline == *sname); + let curve_w = if any_sel { 2.4 } else { 1.7 }; + let curve_col = if any_sel { + [(col[0] + 0.15).min(1.0), (col[1] + 0.15).min(1.0), (col[2] + 0.15).min(1.0), 1.0] + } else { + col + }; + + if let Some(first) = sp.keyframes.first() { + let p = self.spline_to_local(first.time, first.value, bounds); + if p[0] > plot[0] { + out.push(Primitive::Segment { + from: [plot[0], p[1]], + to: p, + width: 1.1, + color: [curve_col[0], curve_col[1], curve_col[2], 0.35], + }); + } + } + if let Some(last) = sp.keyframes.last() { + let p = self.spline_to_local(last.time, last.value, bounds); + if p[0] < plot[2] { + out.push(Primitive::Segment { + from: p, + to: [plot[2], p[1]], + width: 1.1, + color: [curve_col[0], curve_col[1], curve_col[2], 0.35], + }); + } + } + + let dense = sp.keyframes.len() > 250; + for seg in 0..sp.keyframes.len().saturating_sub(1) { + let k0 = &sp.keyframes[seg]; + let k1 = &sp.keyframes[seg + 1]; + if k1.time < self.t_range[0] || k0.time > self.t_range[1] { + continue; + } + if k0.flags.step_out { + // Vertical step at k0.time, then horizontal to k1.time (matches model::spline::eval_segment). + let a = self.spline_to_local(k0.time, k0.value, bounds); + let m = self.spline_to_local(k0.time, k1.value, bounds); + let b = self.spline_to_local(k1.time, k1.value, bounds); + out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); + out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); + } else if k1.flags.step_in { + let a = self.spline_to_local(k0.time, k0.value, bounds); + let m = self.spline_to_local(k1.time, k0.value, bounds); + let b = self.spline_to_local(k1.time, k1.value, bounds); + out.push(Primitive::Segment { from: a, to: m, width: curve_w, color: curve_col }); + out.push(Primitive::Segment { from: m, to: b, width: curve_w, color: curve_col }); + } else if dense || (k1.flags.linear && k0.rh.is_none() && k1.lh.is_none()) { + let a = self.spline_to_local(k0.time, k0.value, bounds); + let b = self.spline_to_local(k1.time, k1.value, bounds); + out.push(Primitive::Segment { from: a, to: b, width: curve_w, color: curve_col }); + } else { + let [p0, p1, p2, p3] = segment_control_points(k0, k1); + const SUBS: usize = 20; + let mut prev = self.spline_to_local(p0[0], p0[1], bounds); + for s in 1..=SUBS { + let u = s as f64 / SUBS as f64; + let t = cubic(p0[0], p1[0], p2[0], p3[0], u); + let v = cubic(p0[1], p1[1], p2[1], p3[1], u); + let cur = self.spline_to_local(t, v, bounds); + out.push(Primitive::Segment { from: prev, to: cur, width: curve_w, color: curve_col }); + prev = cur; + } + } + } + + if !dense || (self.t_range[1] - self.t_range[0]) < 180.0 { + for (ki, kf) in sp.keyframes.iter().enumerate() { + if kf.time < self.t_range[0] - 5.0 || kf.time > self.t_range[1] + 5.0 { + continue; + } + let kp = self.spline_to_local(kf.time, kf.value, bounds); + let kref = PointRef { spline: sname.clone(), key_idx: ki, part: HandlePart::Key }; + let ksel = self.selected.contains(&kref); + let khov = self.hovered.as_ref() == Some(&kref); + let show_handles = self.is_key_or_handle_selected(sname, ki); + + if show_handles { + for (part, hopt) in [(HandlePart::LeftHandle, kf.lh), (HandlePart::RightHandle, kf.rh)] { + if let Some(h) = hopt { + let hp = self.spline_to_local(h[0], h[1], bounds); + let href = PointRef { spline: sname.clone(), key_idx: ki, part }; + let hsel = self.selected.contains(&href); + let hhov = self.hovered.as_ref() == Some(&href); + out.push(Primitive::Segment { + from: kp, + to: hp, + width: 1.4, + color: if hsel { [1.0, 0.85, 0.35, 1.0] } else { [0.75, 0.78, 0.86, 0.75] }, + }); + let hr = if hsel || hhov { 5.2 } else { 4.2 }; + out.push(Primitive::RoundedRect { + rect: [hp[0] - hr, hp[1] - hr, hp[0] + hr, hp[1] + hr], + radius: hr, + stroke: None, + color: if hsel { [1.0, 0.86, 0.28, 1.0] } else if hhov { [1.0, 1.0, 1.0, 1.0] } else { [0.85, 0.50, 0.95, 1.0] }, + }); + } + } + } + + let kr = if ksel || khov { 5.4 } else { 4.2 }; + out.push(Primitive::RoundedRect { + rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], + radius: 1.5, + stroke: None, + color: if ksel { [1.0, 0.84, 0.24, 1.0] } else if khov { [1.0, 1.0, 1.0, 1.0] } else { curve_col }, + }); + out.push(Primitive::RoundedRect { + rect: [kp[0] - kr, kp[1] - kr, kp[0] + kr, kp[1] + kr], + radius: 1.5, + stroke: Some(1.2), + color: [0.02, 0.02, 0.03, 1.0], + }); + } + } + } + } + + fn paint_hover_readout<'a>(&'a self, comp: &'a Comp, plot: [f32; 4], out: &mut Vec>) { + // Hover readout pill showing real `(f, )` for the hovered keyframe/handle + if let (Some(hov), Some(rl)) = (self.hovered.as_ref(), self.text_cache.get("__hover_readout")) { + if let Some(sp) = comp.splines.get(&hov.spline) { + if let Some(kf) = sp.keyframes.get(hov.key_idx) { + let bounds = self.spline_bounds(comp, &hov.spline); + let pt = match hov.part { + HandlePart::Key => Some([kf.time, kf.value]), + HandlePart::LeftHandle => kf.lh, + HandlePart::RightHandle => kf.rh, + }; + if let Some([t, v]) = pt { + let p = self.spline_to_local(t, v, bounds); + let pill_w = (self.last_hover_readout.len() as f32 * 6.4 + 12.0).max(76.0); + let pill_h = 18.0; + let rx = (p[0] + 10.0).clamp(plot[0] + 4.0, (plot[2] - pill_w - 4.0).max(plot[0] + 4.0)); + let ry = (p[1] - pill_h - 8.0).clamp(plot[1] + 4.0, (plot[3] - pill_h - 4.0).max(plot[1] + 4.0)); + let r = [rx, ry, rx + pill_w, ry + pill_h]; + out.push(Primitive::RoundedRect { + rect: r, + radius: 4.0, + stroke: None, + color: [0.08, 0.09, 0.11, 0.94], + }); + out.push(Primitive::RoundedRect { + rect: r, + radius: 4.0, + stroke: Some(1.0), + color: self.palette.accent, + }); + out.push(Primitive::Text { + text: rl as &dyn Glyphs, + origin: [rx + 6.0, ry + 3.0], + clip: Some(r), + ink: self.palette.text, + }); + } + } + } + } + } + + fn paint_box_select<'a>(&'a self, out: &mut Vec>) { + if let Some(bs) = &self.box_select { + let x0 = bs.start_local[0].min(bs.cur_local[0]); + let x1 = bs.start_local[0].max(bs.cur_local[0]); + let y0 = bs.start_local[1].min(bs.cur_local[1]); + let y1 = bs.start_local[1].max(bs.cur_local[1]); + out.push(Primitive::RoundedRect { + rect: [x0, y0, x1, y1], + radius: 2.0, + stroke: None, + color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], + }); + out.push(Primitive::RoundedRect { + rect: [x0, y0, x1, y1], + radius: 2.0, + stroke: Some(1.2), + color: self.palette.accent, + }); + } + } + + fn paint_hud<'a>(&'a self, w: f32, h: f32, out: &mut Vec>) { + let hud_key = match self.modal.as_ref().map(|m| m.mode) { + Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X: Frame / Y: Value]", + Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", + Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X: Time / Y: Value]", + None => "Drag or G/R/S X/Y: Axis V: Handle T: Easing N: Norm I: Insert X/Del: Delete L: Linear F: Fit", + }; + out.push(Primitive::Rect { rect: [SIDEBAR_W, h - 24.0, w, h], color: self.palette.ruler }); + if let Some(hl) = self.text_cache.get(hud_key) { + out.push(Primitive::Text { + text: hl as &dyn Glyphs, + origin: [SIDEBAR_W + 10.0, h - 18.0], + clip: Some([SIDEBAR_W + 6.0, h - 24.0, w - 6.0, h]), + ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, + }); + } + } + + fn paint_popup<'a>(&'a self, out: &mut Vec>) { + // Floating Blender-style popup menu (`V` Handle Type / `T` Interpolation & Easing) + if let Some(popup) = &self.popup { + let pr = self.popup_rect(popup); + let hov_idx = self.popup_item_at(popup, self.pointer_local); + out.push(Primitive::RoundedRect { + rect: pr, + radius: 6.0, + stroke: None, + color: [0.09, 0.10, 0.12, 0.96], + }); + out.push(Primitive::RoundedRect { + rect: pr, + radius: 6.0, + stroke: Some(1.0), + color: self.palette.rule, + }); + let hdr_text = match popup.kind { + PopupKind::HandleType => "Set Handle Type", + PopupKind::Easing => "Set Interpolation", + }; + if let Some(hl) = self.text_cache.get(hdr_text) { + out.push(Primitive::Text { + text: hl as &dyn Glyphs, + origin: [pr[0] + 10.0, pr[1] + 5.0], + clip: Some([pr[0], pr[1], pr[2], pr[1] + POPUP_HEADER_H]), + ink: self.palette.text_dim, + }); + } + out.push(Primitive::Segment { + from: [pr[0] + 6.0, pr[1] + POPUP_HEADER_H - 1.0], + to: [pr[2] - 6.0, pr[1] + POPUP_HEADER_H - 1.0], + width: 1.0, + color: self.palette.rule, + }); + let labels: Vec<&'static str> = match popup.kind { + PopupKind::HandleType => HANDLE_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), + PopupKind::Easing => EASING_MENU_ITEMS.iter().map(|&(_, l)| l).collect(), + }; + for (idx, lbl) in labels.into_iter().enumerate() { + let ry0 = pr[1] + POPUP_HEADER_H + idx as f32 * POPUP_ROW_H; + let ry1 = ry0 + POPUP_ROW_H; + if hov_idx == Some(idx) { + out.push(Primitive::RoundedRect { + rect: [pr[0] + 4.0, ry0 + 1.0, pr[2] - 4.0, ry1 - 1.0], + radius: 4.0, + stroke: None, + color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.25], + }); + } + if let Some(il) = self.text_cache.get(lbl) { + out.push(Primitive::Text { + text: il as &dyn Glyphs, + origin: [pr[0] + 10.0, ry0 + 4.0], + clip: Some([pr[0] + 4.0, ry0, pr[2] - 4.0, ry1]), + ink: self.palette.text, + }); + } + } + } + } +} + +fn spline_color(comp: &Comp, name: &str, idx: usize) -> [f32; 4] { + if let Some(sp) = comp.splines.get(name) { + let [r, g, b] = sp.color; + if r as u16 + g as u16 + b as u16 > 120 { + return [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0]; + } + } + const PALETTE: &[[f32; 4]] = &[ + [0.98, 0.44, 0.62, 1.0], + [0.36, 0.84, 0.96, 1.0], + [0.50, 0.92, 0.46, 1.0], + [0.98, 0.78, 0.28, 1.0], + [0.74, 0.56, 0.98, 1.0], + ]; + PALETTE[idx % PALETTE.len()] +} + +fn nice_step(raw: f64) -> f64 { + let raw = raw.max(1e-6); + let pow = 10.0_f64.powf(raw.log10().floor()); + let frac = raw / pow; + let nice = if frac < 1.5 { 1.0 } else if frac < 3.5 { 2.0 } else if frac < 7.5 { 5.0 } else { 10.0 }; + nice * pow +} -- 2.54.1