diff --git a/crates/app/src/inspector.rs b/crates/app/src/inspector.rs deleted file mode 100644 index b7762169df7751d0013e13e9c70704b5c79c157a..0000000000000000000000000000000000000000 --- a/crates/app/src/inspector.rs +++ /dev/null @@ -1,1802 +0,0 @@ -//! Inspector dock pane — Fusion 21.1 parameter editor with scrub sliders, Vec2, RGBA color groups, -//! booleans, FuID enum cyclers, text fields, expression badges, and keyframe diamonds (`◆`). - -use engine::EvalStats; -use model::{Comp, CompHistory, ExprEngine, NodeId, ParamValue, is_supported_tool}; -use std::collections::{HashMap, HashSet}; -use std::time::{Duration, Instant}; - -#[derive(Clone, Copy, Debug, PartialEq)] -pub enum ParamKind { - Float { min: f64, max: f64, default: f64, step: f64 }, - Vec2 { min: f64, max: f64, default: [f64; 2] }, - Bool { default: bool }, - Enum { options: &'static [&'static str], default: &'static str }, - Color { - r: &'static str, - g: &'static str, - b: &'static str, - a: Option<&'static str>, - default: [f64; 4], - }, - Text { default: &'static str }, -} - -#[derive(Clone, Copy, Debug)] -pub struct ParamSpec { - pub name: &'static str, - pub label: &'static str, - pub kind: ParamKind, -} - -#[derive(Clone, Debug)] -pub enum InspectorAction { - /// A parameter or node property changed; bust subtree & re-render viewers. - ParamEdited(NodeId), - /// A keyframe was added or deleted; refresh spline editor + bust subtree. - KeyframeEdited(NodeId), - /// Node was renamed. - NodeRenamed(NodeId), - /// Status notice to show in window title. - StatusNotice(String), -} - -#[derive(Clone, Debug)] -struct DragState { - node_id: NodeId, - key: String, - start_x: f32, - start_val: f64, - min: f64, - max: f64, - width: f32, -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum KeyframeState { - Static, - AnimatedOtherFrame, - KeyframeAtCurrent, -} - -pub struct InspectorState { - pub bg: [f32; 4], - pub panel_bg: [f32; 4], - pub rule: [f32; 4], - pub text: [f32; 4], - pub text_dim: [f32; 4], - pub accent: [f32; 4], - expr: ExprEngine, - /// Node name edit buffer synced with the currently selected node. - name_node: Option, - name_buf: String, - /// Text parameter edit buffers keyed by `(node_id, param_name)`. - text_bufs: HashMap<(usize, String), String>, - /// Active scrub slider drag. - drag: Option, - /// Last click timestamp per slider key for double-click reset. - last_click: HashMap, -} - -impl InspectorState { - pub fn new(theme: &ui::Theme) -> Self { - let b = theme.base; - let lift = |c: [f32; 4], d: f32| { - [ - (c[0] + d).clamp(0.0, 1.0), - (c[1] + d).clamp(0.0, 1.0), - (c[2] + d).clamp(0.0, 1.0), - 1.0, - ] - }; - Self { - bg: b, - panel_bg: lift(b, 0.020), - rule: theme.rule, - text: theme.text, - text_dim: theme.text_dim, - accent: theme.accent, - expr: ExprEngine::new(), - name_node: None, - name_buf: String::new(), - text_bufs: HashMap::new(), - drag: None, - last_click: HashMap::new(), - } - } - - pub fn background(&self) -> [f32; 4] { - self.bg - } - - pub fn reset(&mut self) { - self.name_node = None; - self.name_buf.clear(); - self.text_bufs.clear(); - self.drag = None; - } - - pub fn build_ui( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - selected: Option, - current_frame: f64, - stats: &EvalStats, - ) -> Vec { - let mut actions = Vec::new(); - let bg = self.bg; - let panel_bg = self.panel_bg; - let rule = self.rule; - let text_col = self.text; - let text_dim = self.text_dim; - let accent = self.accent; - - ui.open_as( - pane_id, - ui::Spec { - size: [ui::fill(), ui::fill()], - axis: ui::Axis::Y, - fill: Some(bg), - ..ui::Spec::default() - }, - ); - - let Some(node_id) = selected.filter(|id| id.0 < comp.nodes.len()) else { - // Empty state - ui.open( - "insp-empty", - ui::Spec { - size: [ui::fill(), ui::fill()], - axis: ui::Axis::Y, - pad: [14.0, 18.0], - gap: 8.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "empty-title", - ui::Spec { - size: [ui::fill(), ui::px(20.0)], - text: Some("No Node Selected"), - font_size: Some(12.5), - medium: true, - color: Some(text_col), - ..ui::Spec::default() - }, - ); - ui.leaf( - "empty-hint", - ui::Spec { - size: [ui::fill(), ui::fit()], - text: Some("Select a node in the Flow graph to inspect and keyframe its parameters, or press Shift+Space to add a tool."), - font_size: Some(11.0), - overflow: ui::Overflow::Wrap, - color: Some(text_dim), - ..ui::Spec::default() - }, - ); - self.render_stats_footer(ui, stats, panel_bg, rule, text_dim); - ui.close(); - ui.close(); - return actions; - }; - - // Sync editable name buffer when selection changes or node was renamed externally (e.g. undo) - let current_name = comp.node(node_id).name.clone(); - let name_field_id = pane_id.child(("node-name", node_id.0)); - if self.name_node != Some(node_id) - || (ui.focused() != Some(name_field_id) && self.name_buf != current_name) - { - self.name_node = Some(node_id); - self.name_buf = current_name.clone(); - } - - // 1. Header: Node name + Class badge + Bypass toggle - ui.open( - "insp-header", - ui::Spec { - size: [ui::fill(), ui::children()], - axis: ui::Axis::Y, - pad: [10.0, 8.0], - gap: 6.0, - fill: Some(panel_bg), - border: Some(rule), - ..ui::Spec::default() - }, - ); - - // Row 1: Editable name + Bypass button - ui.open( - "insp-name-row", - ui::Spec { - size: [ui::fill(), ui::px(26.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - let name_sig = ui::text_field( - ui, - name_field_id, - &mut self.name_buf, - "Node name", - ui::Spec { - size: [ui::fill(), ui::px(24.0)], - font_size: Some(12.0), - medium: true, - fill: Some(bg), - border: Some(rule), - hover_border: Some(accent), - radius: 4.0, - pad: [6.0, 2.0], - ..ui::Spec::default() - }, - ); - let enter_pressed = name_sig.events.iter().any(|ev| { - matches!( - ev, - ui::Event::Key { key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter), .. } - ) - }); - if enter_pressed && self.name_buf.trim() != current_name { - let desired = self.name_buf.trim().to_owned(); - history.push_snapshot(comp, "Rename Node"); - match comp.rename_node(node_id, &desired) { - Ok(applied) => { - self.name_buf = applied; - actions.push(InspectorAction::NodeRenamed(node_id)); - } - Err(e) => { - self.name_buf = current_name.clone(); - actions.push(InspectorAction::StatusNotice(e)); - } - } - ui.set_focus(None); - } - - let bypassed = comp.node(node_id).bypassed; - let pass_lbl = if bypassed { "Bypassed" } else { "Active" }; - let pass_fill = if bypassed { - [0.85, 0.28, 0.28, 0.28] - } else { - [0.24, 0.78, 0.48, 0.18] - }; - let pass_border = if bypassed { - [0.92, 0.36, 0.36, 0.75] - } else { - [0.28, 0.82, 0.52, 0.55] - }; - let pass_sig = ui.leaf( - ("insp-pass", node_id.0), - ui::Spec { - cursor: Some(winit::window::CursorIcon::Pointer), - flags: ui::Flags::CLICKABLE, - size: [ui::fit(), ui::px(24.0)], - text: Some(pass_lbl), - font_size: Some(10.5), - medium: true, - color: Some(text_col), - fill: Some(pass_fill), - border: Some(pass_border), - radius: 4.0, - pad: [8.0, 0.0], - center: true, - ..ui::Spec::default() - }, - ); - if pass_sig.clicked { - history.push_snapshot(comp, "Toggle Passthrough"); - comp.toggle_passthrough(node_id); - actions.push(InspectorAction::ParamEdited(node_id)); - } - ui.close(); // insp-name-row - - // Row 2: Tool class badge + supported/preserved indicator + frame badge - let node_kind = comp.node(node_id).kind.clone(); - let supported = is_supported_tool(&node_kind); - let frame_str = format!("f{:.0}", current_frame); - ui.open( - "insp-meta-row", - ui::Spec { - size: [ui::fill(), ui::px(18.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "insp-kind-badge", - ui::Spec { - size: [ui::fit(), ui::px(18.0)], - text: Some(&node_kind), - font_size: Some(10.0), - medium: true, - color: Some(accent), - fill: Some([accent[0], accent[1], accent[2], 0.14]), - border: Some([accent[0], accent[1], accent[2], 0.40]), - radius: 3.0, - pad: [6.0, 0.0], - center: true, - ..ui::Spec::default() - }, - ); - if !supported { - ui.leaf( - "insp-preserved-badge", - ui::Spec { - size: [ui::fit(), ui::px(18.0)], - text: Some("preserved"), - font_size: Some(9.5), - medium: true, - color: Some([0.98, 0.78, 0.40, 1.0]), - fill: Some([0.24, 0.16, 0.06, 0.85]), - border: Some([0.92, 0.64, 0.24, 0.60]), - radius: 3.0, - pad: [5.0, 0.0], - center: true, - ..ui::Spec::default() - }, - ); - } - ui.leaf( - "insp-spacer", - ui::Spec { - size: [ui::fill(), ui::px(18.0)], - ..ui::Spec::default() - }, - ); - ui.leaf( - "insp-frame", - ui::Spec { - size: [ui::fit(), ui::px(18.0)], - text: Some(&frame_str), - font_size: Some(10.0), - color: Some(text_dim), - center: true, - ..ui::Spec::default() - }, - ); - ui.close(); // insp-meta-row - ui.close(); // insp-header - - // 2. Scrollable Parameter List - ui.open( - "insp-scroll", - ui::Spec { - flags: ui::Flags::SCROLL | ui::Flags::CLIP, - size: [ui::fill(), ui::fill()], - axis: ui::Axis::Y, - pad: [10.0, 8.0], - gap: 6.0, - ..ui::Spec::default() - }, - ); - - let schema = schema_for_kind(&node_kind); - let mut covered: HashSet<&str> = HashSet::new(); - - for spec in schema { - covered.insert(spec.name); - if let ParamKind::Color { r, g, b, a, .. } = spec.kind { - covered.insert(r); - covered.insert(g); - covered.insert(b); - if let Some(alpha_k) = a { - covered.insert(alpha_k); - } - } - self.render_param_spec(ui, pane_id, comp, history, node_id, spec, current_frame, &mut actions); - } - - // Fallback: list all remaining present non-image inputs on the node! - let mut extra_keys: Vec = comp - .node(node_id) - .inputs - .iter() - .filter_map(|(k, b)| { - if covered.contains(k.as_str()) { - return None; - } - // Skip image/node connections unless they also have a value or spline - let is_node_conn = b - .source_op - .as_deref() - .is_some_and(|op| comp.node_by_name.contains_key(op)); - if is_node_conn && b.value.is_none() { - return None; - } - if b.value.is_none() && b.expression.is_none() && b.source_op.is_none() { - return None; - } - Some(k.clone()) - }) - .collect(); - extra_keys.sort(); - - if !extra_keys.is_empty() { - ui.leaf( - "insp-extra-hdr", - ui::Spec { - size: [ui::fill(), ui::px(18.0)], - text: Some("OTHER INPUTS"), - font_size: Some(9.5), - medium: true, - color: Some(text_dim), - ..ui::Spec::default() - }, - ); - for key in extra_keys { - self.render_fallback_input(ui, pane_id, comp, history, node_id, &key, current_frame, &mut actions); - } - } - - // Loader clip info if Loader node - if node_kind == "Loader" { - let clip_summary = comp - .node(node_id) - .clips - .first() - .map(|c| c.filename.clone()) - .unwrap_or_else(|| "(no clip)".to_owned()); - ui.open( - "insp-loader-clip", - ui::Spec { - size: [ui::fill(), ui::children()], - axis: ui::Axis::Y, - gap: 3.0, - pad: [6.0, 5.0], - fill: Some(panel_bg), - border: Some(rule), - radius: 4.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "clip-lbl", - ui::Spec { - size: [ui::fill(), ui::px(14.0)], - text: Some("Clip Filename"), - font_size: Some(10.0), - color: Some(text_dim), - ..ui::Spec::default() - }, - ); - ui.leaf( - "clip-path", - ui::Spec { - size: [ui::fill(), ui::fit()], - text: Some(&clip_summary), - font_size: Some(10.5), - overflow: ui::Overflow::Wrap, - color: Some(text_col), - ..ui::Spec::default() - }, - ); - ui.close(); - } - - ui.close(); // insp-scroll - - // 3. Footer: EvalStats summary - self.render_stats_footer(ui, stats, panel_bg, rule, text_dim); - - ui.close(); // pane_id - actions - } - - fn render_stats_footer( - &self, - ui: &mut ui::Ui, - stats: &EvalStats, - panel_bg: [f32; 4], - rule: [f32; 4], - text_dim: [f32; 4], - ) { - let line = format!( - "Eval {:.1}ms · {} nodes · cache {}/{} · {:.0}MB", - stats.eval_ms, - stats.nodes_evaluated, - stats.cache_hits, - stats.cache_hits + stats.cache_misses, - stats.cached_vram_mb, - ); - ui.open( - "insp-footer", - ui::Spec { - size: [ui::fill(), ui::px(24.0)], - axis: ui::Axis::X, - pad: [10.0, 4.0], - fill: Some(panel_bg), - border: Some(rule), - ..ui::Spec::default() - }, - ); - ui.leaf( - "insp-stats-txt", - ui::Spec { - size: [ui::fill(), ui::fill()], - text: Some(&line), - font_size: Some(10.0), - color: Some(text_dim), - ..ui::Spec::default() - }, - ); - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn render_param_spec( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - spec: &ParamSpec, - current_frame: f64, - actions: &mut Vec, - ) { - match spec.kind { - ParamKind::Float { min, max, default, step: _ } => { - self.render_float_row( - ui, - pane_id, - comp, - history, - node_id, - spec.name, - spec.label, - min, - max, - default, - current_frame, - actions, - ); - } - ParamKind::Vec2 { min, max, default } => { - self.render_vec2_row( - ui, - pane_id, - comp, - history, - node_id, - spec.name, - spec.label, - min, - max, - default, - current_frame, - actions, - ); - } - ParamKind::Bool { default } => { - self.render_bool_row(ui, comp, history, node_id, spec.name, spec.label, default, actions); - } - ParamKind::Enum { options, default } => { - self.render_enum_row(ui, comp, history, node_id, spec.name, spec.label, options, default, actions); - } - ParamKind::Color { r, g, b, a, default } => { - self.render_color_group( - ui, - pane_id, - comp, - history, - node_id, - spec.label, - r, - g, - b, - a, - default, - current_frame, - actions, - ); - } - ParamKind::Text { default } => { - self.render_text_row(ui, pane_id, comp, history, node_id, spec.name, spec.label, default, actions); - } - } - } - - #[allow(clippy::too_many_arguments)] - fn render_fallback_input( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - key: &str, - current_frame: f64, - actions: &mut Vec, - ) { - let val = comp.node(node_id).inputs.get(key).and_then(|b| b.value.clone()); - match val { - Some(ParamValue::Point(pt)) => { - self.render_vec2_row(ui, pane_id, comp, history, node_id, key, key, -1.0, 2.0, pt, current_frame, actions); - } - Some(ParamValue::Text(_)) => { - self.render_text_row(ui, pane_id, comp, history, node_id, key, key, "", actions); - } - Some(ParamValue::FuId(cur)) => { - let text_dim = self.text_dim; - let text_col = self.text; - let panel_bg = self.panel_bg; - let rule = self.rule; - ui.open( - ("fb-fuid", node_id.0, key), - ui::Spec { - size: [ui::fill(), ui::px(22.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "lbl", - ui::Spec { - size: [ui::px(92.0), ui::fill()], - text: Some(key), - font_size: Some(10.5), - overflow: ui::Overflow::Ellipsis, - color: Some(text_dim), - ..ui::Spec::default() - }, - ); - ui.leaf( - "val", - ui::Spec { - size: [ui::fill(), ui::fill()], - text: Some(&cur), - font_size: Some(10.5), - color: Some(text_col), - fill: Some(panel_bg), - border: Some(rule), - radius: 4.0, - pad: [6.0, 0.0], - ..ui::Spec::default() - }, - ); - ui.close(); - } - Some(ParamValue::Number(v)) => { - let (min, max) = infer_float_range(key, v); - self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, v, current_frame, actions); - } - None => { - // Animated spline or expression-only scalar input - let cur = comp.eval_number(comp.node(node_id), key, current_frame, 0.0, &self.expr); - let (min, max) = infer_float_range(key, cur); - self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, 0.0, current_frame, actions); - } - _ => {} - } - } - - #[allow(clippy::too_many_arguments)] - fn render_float_row( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - input_name: &str, - label: &str, - min: f64, - max: f64, - default: f64, - current_frame: f64, - actions: &mut Vec, - ) { - let cur_val = comp.eval_number(comp.node(node_id), input_name, current_frame, default, &self.expr); - let has_expr = comp - .node(node_id) - .inputs - .get(input_name) - .and_then(|b| b.expression.as_ref()) - .is_some(); - let kf_state = keyframe_state(comp, node_id, input_name, current_frame); - - ui.open( - ("row-f", node_id.0, input_name), - ui::Spec { - size: [ui::fill(), ui::px(22.0)], - axis: ui::Axis::X, - gap: 4.0, - ..ui::Spec::default() - }, - ); - - // Label - ui.leaf( - "lbl", - ui::Spec { - size: [ui::px(86.0), ui::fill()], - text: Some(label), - font_size: Some(10.5), - overflow: ui::Overflow::Ellipsis, - color: Some(self.text_dim), - ..ui::Spec::default() - }, - ); - - // Expression `=` badge if present - if has_expr { - ui.leaf( - "expr-badge", - ui::Spec { - size: [ui::px(16.0), ui::px(18.0)], - text: Some("="), - font_size: Some(10.5), - bold: true, - color: Some([0.36, 0.88, 0.92, 1.0]), - fill: Some([0.10, 0.24, 0.28, 0.85]), - border: Some([0.32, 0.78, 0.86, 0.65]), - radius: 3.0, - center: true, - ..ui::Spec::default() - }, - ); - } - - // Scrub slider - let slider_id = pane_id.child(("sld", node_id.0, input_name)); - let drag_key = format!("{}:{input_name}", node_id.0); - if let Some(new_val) = self.scrub_slider( - ui, - slider_id, - &drag_key, - node_id, - cur_val, - min, - max, - default, - history, - comp, - input_name, - ) { - if kf_state != KeyframeState::Static { - let _ = comp.set_keyframe(node_id, input_name, current_frame, new_val); - actions.push(InspectorAction::KeyframeEdited(node_id)); - } else { - comp.set_input_value(node_id, input_name, ParamValue::Number(new_val)); - actions.push(InspectorAction::ParamEdited(node_id)); - } - } - - // Reset button (small dot/arrow when non-default) - let is_default = (cur_val - default).abs() < 1e-4; - if !is_default { - let rst = ui.leaf( - ("rst", node_id.0, input_name), - ui::Spec { - cursor: Some(winit::window::CursorIcon::Pointer), - flags: ui::Flags::CLICKABLE, - size: [ui::px(18.0), ui::px(20.0)], - text: Some("↺"), - font_size: Some(10.5), - color: Some(self.text_dim), - hover_fill: Some(self.panel_bg), - radius: 3.0, - center: true, - ..ui::Spec::default() - }, - ); - if rst.clicked { - history.push_snapshot(comp, format!("Reset {label}")); - if kf_state != KeyframeState::Static { - let _ = comp.set_keyframe(node_id, input_name, current_frame, default); - actions.push(InspectorAction::KeyframeEdited(node_id)); - } else { - comp.set_input_value(node_id, input_name, ParamValue::Number(default)); - actions.push(InspectorAction::ParamEdited(node_id)); - } - } - } - - // Keyframe diamond button (`◆`) - if self.keyframe_button(ui, ("kf", node_id.0, input_name), kf_state) { - self.toggle_keyframe(comp, history, node_id, input_name, current_frame, cur_val, kf_state, actions); - } - - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn render_vec2_row( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - input_name: &str, - label: &str, - min: f64, - max: f64, - default: [f64; 2], - current_frame: f64, - actions: &mut Vec, - ) { - let cur = comp.eval_point(comp.node(node_id), input_name, current_frame, default, &self.expr); - let has_expr = comp - .node(node_id) - .inputs - .get(input_name) - .and_then(|b| b.expression.as_ref()) - .is_some(); - - ui.open( - ("row-v2", node_id.0, input_name), - ui::Spec { - size: [ui::fill(), ui::px(22.0)], - axis: ui::Axis::X, - gap: 4.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "lbl", - ui::Spec { - size: [ui::px(86.0), ui::fill()], - text: Some(label), - font_size: Some(10.5), - overflow: ui::Overflow::Ellipsis, - color: Some(self.text_dim), - ..ui::Spec::default() - }, - ); - if has_expr { - ui.leaf( - "expr-badge", - ui::Spec { - size: [ui::px(16.0), ui::px(18.0)], - text: Some("="), - font_size: Some(10.5), - bold: true, - color: Some([0.36, 0.88, 0.92, 1.0]), - fill: Some([0.10, 0.24, 0.28, 0.85]), - border: Some([0.32, 0.78, 0.86, 0.65]), - radius: 3.0, - center: true, - ..ui::Spec::default() - }, - ); - } - - let x_id = pane_id.child(("v2x", node_id.0, input_name)); - let x_key = format!("{}:{input_name}:x", node_id.0); - if let Some(nx) = self.scrub_slider( - ui, - x_id, - &x_key, - node_id, - cur[0], - min, - max, - default[0], - history, - comp, - input_name, - ) { - comp.set_input_value(node_id, input_name, ParamValue::Point([nx, cur[1]])); - actions.push(InspectorAction::ParamEdited(node_id)); - } - - let y_id = pane_id.child(("v2y", node_id.0, input_name)); - let y_key = format!("{}:{input_name}:y", node_id.0); - if let Some(ny) = self.scrub_slider( - ui, - y_id, - &y_key, - node_id, - cur[1], - min, - max, - default[1], - history, - comp, - input_name, - ) { - comp.set_input_value(node_id, input_name, ParamValue::Point([cur[0], ny])); - actions.push(InspectorAction::ParamEdited(node_id)); - } - - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn render_bool_row( - &mut self, - ui: &mut ui::Ui, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - input_name: &str, - label: &str, - default: bool, - actions: &mut Vec, - ) { - let checked = comp - .node(node_id) - .static_number(input_name) - .map(|v| v >= 0.5) - .unwrap_or(default); - - ui.open( - ("row-b", node_id.0, input_name), - ui::Spec { - size: [ui::fill(), ui::px(22.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "lbl", - ui::Spec { - size: [ui::px(86.0), ui::fill()], - text: Some(label), - font_size: Some(10.5), - overflow: ui::Overflow::Ellipsis, - color: Some(self.text_dim), - ..ui::Spec::default() - }, - ); - let fill = if checked { - [self.accent[0], self.accent[1], self.accent[2], 0.28] - } else { - self.panel_bg - }; - let border = if checked { self.accent } else { self.rule }; - let sig = ui.leaf( - ("chk", node_id.0, input_name), - ui::Spec { - cursor: Some(winit::window::CursorIcon::Pointer), - flags: ui::Flags::CLICKABLE, - size: [ui::px(54.0), ui::px(20.0)], - text: Some(if checked { "✓ On" } else { "Off" }), - font_size: Some(10.5), - medium: checked, - color: Some(if checked { self.text } else { self.text_dim }), - fill: Some(fill), - border: Some(border), - radius: 4.0, - center: true, - ..ui::Spec::default() - }, - ); - if sig.clicked { - history.push_snapshot(comp, format!("Toggle {label}")); - let next = if checked { 0.0 } else { 1.0 }; - comp.set_input_value(node_id, input_name, ParamValue::Number(next)); - actions.push(InspectorAction::ParamEdited(node_id)); - } - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn render_enum_row( - &mut self, - ui: &mut ui::Ui, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - input_name: &str, - label: &str, - options: &'static [&'static str], - default: &'static str, - actions: &mut Vec, - ) { - let cur = comp - .node(node_id) - .static_str(input_name) - .unwrap_or(default) - .to_owned(); - - ui.open( - ("row-e", node_id.0, input_name), - ui::Spec { - size: [ui::fill(), ui::px(22.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "lbl", - ui::Spec { - size: [ui::px(86.0), ui::fill()], - text: Some(label), - font_size: Some(10.5), - overflow: ui::Overflow::Ellipsis, - color: Some(self.text_dim), - ..ui::Spec::default() - }, - ); - let btn_lbl = format!("{cur} ▾"); - let sig = ui.leaf( - ("enum", node_id.0, input_name), - ui::Spec { - cursor: Some(winit::window::CursorIcon::Pointer), - flags: ui::Flags::CLICKABLE, - size: [ui::fill(), ui::px(20.0)], - text: Some(&btn_lbl), - font_size: Some(10.5), - color: Some(self.text), - fill: Some(self.panel_bg), - border: Some(self.rule), - hover_border: Some(self.accent), - radius: 4.0, - pad: [6.0, 0.0], - ..ui::Spec::default() - }, - ); - if sig.clicked && !options.is_empty() { - let idx = options.iter().position(|&o| o == cur).unwrap_or(0); - let next_idx = if ui.modifiers().shift_key() { - (idx + options.len() - 1) % options.len() - } else { - (idx + 1) % options.len() - }; - history.push_snapshot(comp, format!("Set {label}")); - comp.set_input_value(node_id, input_name, ParamValue::FuId(options[next_idx].to_owned())); - actions.push(InspectorAction::ParamEdited(node_id)); - } - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn render_color_group( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - label: &str, - r_key: &'static str, - g_key: &'static str, - b_key: &'static str, - a_key: Option<&'static str>, - default: [f64; 4], - current_frame: f64, - actions: &mut Vec, - ) { - let rv = comp.eval_number(comp.node(node_id), r_key, current_frame, default[0], &self.expr); - let gv = comp.eval_number(comp.node(node_id), g_key, current_frame, default[1], &self.expr); - let bv = comp.eval_number(comp.node(node_id), b_key, current_frame, default[2], &self.expr); - let _av = a_key - .map(|ak| comp.eval_number(comp.node(node_id), ak, current_frame, default[3], &self.expr)) - .unwrap_or(default[3]); - let swatch = [ - rv.clamp(0.0, 1.0) as f32, - gv.clamp(0.0, 1.0) as f32, - bv.clamp(0.0, 1.0) as f32, - 1.0, - ]; - - ui.open( - ("grp-col", node_id.0, label), - ui::Spec { - size: [ui::fill(), ui::children()], - axis: ui::Axis::Y, - gap: 4.0, - pad: [6.0, 5.0], - fill: Some(self.panel_bg), - border: Some(self.rule), - radius: 4.0, - ..ui::Spec::default() - }, - ); - // Swatch header - ui.open( - "col-hdr", - ui::Spec { - size: [ui::fill(), ui::px(18.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "col-lbl", - ui::Spec { - size: [ui::px(80.0), ui::fill()], - text: Some(label), - font_size: Some(10.5), - medium: true, - color: Some(self.text), - ..ui::Spec::default() - }, - ); - ui.leaf( - "col-swatch", - ui::Spec { - size: [ui::fill(), ui::px(16.0)], - fill: Some(swatch), - border: Some(self.rule), - radius: 3.0, - ..ui::Spec::default() - }, - ); - ui.close(); - - self.render_float_row(ui, pane_id, comp, history, node_id, r_key, "Red", 0.0, 1.0, default[0], current_frame, actions); - self.render_float_row(ui, pane_id, comp, history, node_id, g_key, "Green", 0.0, 1.0, default[1], current_frame, actions); - self.render_float_row(ui, pane_id, comp, history, node_id, b_key, "Blue", 0.0, 1.0, default[2], current_frame, actions); - if let Some(ak) = a_key { - self.render_float_row(ui, pane_id, comp, history, node_id, ak, "Alpha", 0.0, 1.0, default[3], current_frame, actions); - } - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn render_text_row( - &mut self, - ui: &mut ui::Ui, - pane_id: ui::Id, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - input_name: &str, - label: &str, - default: &str, - actions: &mut Vec, - ) { - let field_id = pane_id.child(("txt", node_id.0, input_name)); - let key = (node_id.0, input_name.to_owned()); - let model_val = comp - .node(node_id) - .static_str(input_name) - .unwrap_or(default) - .to_owned(); - let buf = self.text_bufs.entry(key).or_insert_with(|| model_val.clone()); - if ui.focused() != Some(field_id) && *buf != model_val { - *buf = model_val.clone(); - } - let prev = buf.clone(); - - ui.open( - ("row-t", node_id.0, input_name), - ui::Spec { - size: [ui::fill(), ui::px(24.0)], - axis: ui::Axis::X, - gap: 6.0, - ..ui::Spec::default() - }, - ); - ui.leaf( - "lbl", - ui::Spec { - size: [ui::px(86.0), ui::fill()], - text: Some(label), - font_size: Some(10.5), - overflow: ui::Overflow::Ellipsis, - color: Some(self.text_dim), - ..ui::Spec::default() - }, - ); - ui::text_field( - ui, - field_id, - buf, - label, - ui::Spec { - size: [ui::fill(), ui::px(22.0)], - font_size: Some(11.0), - fill: Some(self.panel_bg), - border: Some(self.rule), - hover_border: Some(self.accent), - radius: 4.0, - pad: [6.0, 2.0], - ..ui::Spec::default() - }, - ); - if *buf != prev { - history.push_coalesced( - comp, - format!("Edit {label}"), - format!("text:{}:{input_name}", node_id.0), - ); - comp.set_input_value(node_id, input_name, ParamValue::Text(buf.clone())); - actions.push(InspectorAction::ParamEdited(node_id)); - } - ui.close(); - } - - #[allow(clippy::too_many_arguments)] - fn scrub_slider( - &mut self, - ui: &mut ui::Ui, - slider_id: ui::Id, - drag_key: &str, - node_id: NodeId, - value: f64, - min: f64, - max: f64, - default: f64, - history: &mut CompHistory, - comp: &Comp, - input_name: &str, - ) -> Option { - let sig = ui.signal(slider_id); - let rect = ui.laid_out(slider_id); - let width = rect.map(|r| (r[2] - r[0]).max(40.0)).unwrap_or(110.0); - let mut updated: Option = None; - - if let Some(press) = sig.press { - let now = Instant::now(); - let is_double = self - .last_click - .get(drag_key) - .is_some_and(|prev| now.duration_since(*prev) < Duration::from_millis(320)); - self.last_click.insert(drag_key.to_owned(), now); - if is_double { - history.push_snapshot(comp, format!("Reset {input_name}")); - self.drag = None; - updated = Some(default); - } else { - self.drag = Some(DragState { - node_id, - key: drag_key.to_owned(), - start_x: press[0], - start_val: value, - min, - max, - width, - }); - } - } else if sig.dragging { - if let (Some(d), Some(ptr)) = (&self.drag, ui.pointer()) { - if d.key == drag_key && d.node_id == node_id { - let fine = if ui.modifiers().shift_key() { 0.15 } else { 1.0 }; - let next = compute_scrub_value(d.start_val, ptr[0] - d.start_x, d.width, d.min, d.max, fine); - if (next - value).abs() > 1e-6 { - history.push_coalesced( - comp, - format!("Adjust {input_name}"), - format!("scrub:{drag_key}"), - ); - updated = Some(next); - } - } - } - } else if self.drag.as_ref().is_some_and(|d| d.key == drag_key) { - self.drag = None; - history.break_coalescing(); - } - - let display_val = updated.unwrap_or(value); - let frac = normalize_slider_fraction(display_val, min, max); - let val_text = format_param_number(display_val); - let fill_w = (width * frac).clamp(0.0, width); - let active_drag = self.drag.as_ref().is_some_and(|d| d.key == drag_key); - - ui.open_as( - slider_id, - ui::Spec { - cursor: Some(winit::window::CursorIcon::EwResize), - flags: ui::Flags::CLICKABLE | ui::Flags::CLIP, - size: [ui::fill(), ui::px(20.0)], - fill: Some(self.panel_bg), - border: Some(if active_drag || sig.hovered { self.accent } else { self.rule }), - radius: 4.0, - ..ui::Spec::default() - }, - ); - if fill_w > 0.5 { - ui.leaf( - "fill", - ui::Spec { - flags: ui::Flags::FLOAT | ui::Flags::INERT, - position: [0.0, 0.0], - size: [ui::px(fill_w), ui::px(20.0)], - fill: Some([self.accent[0], self.accent[1], self.accent[2], if active_drag { 0.34 } else { 0.22 }]), - radius: 3.0, - ..ui::Spec::default() - }, - ); - } - ui.leaf( - "txt", - ui::Spec { - flags: ui::Flags::INERT, - size: [ui::fill(), ui::fill()], - text: Some(&val_text), - font_size: Some(10.5), - color: Some(self.text), - center: true, - ..ui::Spec::default() - }, - ); - ui.close(); - - updated - } - - fn keyframe_button(&self, ui: &mut ui::Ui, part: impl std::hash::Hash, state: KeyframeState) -> bool { - let (ink, fill, border) = match state { - KeyframeState::KeyframeAtCurrent => ( - [1.0, 0.80, 0.22, 1.0], - Some([0.98, 0.72, 0.18, 0.24]), - Some([0.98, 0.76, 0.20, 0.85]), - ), - KeyframeState::AnimatedOtherFrame => ( - [0.92, 0.64, 0.24, 0.90], - Some([0.92, 0.64, 0.24, 0.12]), - Some([0.92, 0.64, 0.24, 0.45]), - ), - KeyframeState::Static => (self.text_dim, None, None), - }; - let sig = ui.leaf( - part, - ui::Spec { - cursor: Some(winit::window::CursorIcon::Pointer), - flags: ui::Flags::CLICKABLE, - size: [ui::px(20.0), ui::px(20.0)], - text: Some("◆"), - font_size: Some(10.5), - color: Some(ink), - fill, - border, - hover_fill: Some([0.98, 0.76, 0.22, 0.18]), - radius: 4.0, - center: true, - ..ui::Spec::default() - }, - ); - sig.clicked - } - - #[allow(clippy::too_many_arguments)] - fn toggle_keyframe( - &self, - comp: &mut Comp, - history: &mut CompHistory, - node_id: NodeId, - input_name: &str, - current_frame: f64, - cur_val: f64, - state: KeyframeState, - actions: &mut Vec, - ) { - match state { - KeyframeState::KeyframeAtCurrent => { - if let Some(sp_name) = comp - .node(node_id) - .inputs - .get(input_name) - .and_then(|b| b.source_op.clone()) - { - history.push_snapshot(comp, format!("Delete Keyframe {input_name}")); - if comp.delete_keyframe(&sp_name, current_frame) { - actions.push(InspectorAction::KeyframeEdited(node_id)); - actions.push(InspectorAction::StatusNotice(format!( - "Deleted keyframe on {input_name} @ f{current_frame:.0}" - ))); - } - } - } - KeyframeState::Static | KeyframeState::AnimatedOtherFrame => { - history.push_snapshot(comp, format!("Set Keyframe {input_name}")); - match comp.set_keyframe(node_id, input_name, current_frame, cur_val) { - Ok(_) => { - actions.push(InspectorAction::KeyframeEdited(node_id)); - actions.push(InspectorAction::StatusNotice(format!( - "Keyframed {input_name} = {:.3} @ f{current_frame:.0}", - cur_val - ))); - } - Err(e) => actions.push(InspectorAction::StatusNotice(e)), - } - } - } - } -} - -/// Computes whether an input is static, animated on another frame, or has a keyframe at `frame`. -pub fn keyframe_state(comp: &Comp, node_id: NodeId, input_name: &str, frame: f64) -> KeyframeState { - let Some(sp_name) = comp - .node(node_id) - .inputs - .get(input_name) - .and_then(|b| b.source_op.as_deref()) - else { - return KeyframeState::Static; - }; - let Some(spline) = comp.splines.get(sp_name) else { - return KeyframeState::Static; - }; - if spline.keyframes.iter().any(|kf| (kf.time - frame).abs() < 1e-4) { - KeyframeState::KeyframeAtCurrent - } else if !spline.keyframes.is_empty() { - KeyframeState::AnimatedOtherFrame - } else { - KeyframeState::Static - } -} - -pub fn normalize_slider_fraction(value: f64, min: f64, max: f64) -> f32 { - let span = (max - min).abs().max(1e-6); - ((value - min) / span).clamp(0.0, 1.0) as f32 -} - -pub fn compute_scrub_value(start_val: f64, dx: f32, width: f32, min: f64, max: f64, fine_scale: f64) -> f64 { - let span = (max - min).max(1e-4); - let delta = (f64::from(dx) / f64::from(width.max(40.0))) * span * fine_scale; - (start_val + delta).clamp(min, max) -} - -pub fn format_param_number(v: f64) -> String { - if v.abs() >= 100.0 { - format!("{v:.1}") - } else if v.abs() >= 10.0 { - format!("{v:.2}") - } else { - format!("{v:.3}") - } -} - -pub fn infer_float_range(name: &str, val: f64) -> (f64, f64) { - let lower = name.to_ascii_lowercase(); - if lower.contains("angle") || lower.contains("rotate") { - (-360.0, 360.0) - } else if lower.contains("width") || lower.contains("height") { - if val > 2.0 { - (1.0, (val * 2.0).max(4096.0)) - } else { - (0.0, 2.0) - } - } else if lower.contains("blend") || lower.contains("alpha") || lower.contains("opacity") { - (0.0, 1.0) - } else if lower.contains("size") || lower.contains("blur") || lower.contains("glow") { - (0.0, (val.abs() * 2.0).max(50.0)) - } else if val < 0.0 || val > 1.0 { - let hi = (val.abs() * 2.0).max(10.0); - (-hi, hi) - } else { - (0.0, 1.0) - } -} - -const APPLY_MODES: &[&str] = &[ - "Normal", - "Screen", - "Multiply", - "Overlay", - "Soft Light", - "Hard Light", - "Color Dodge", - "Color Burn", - "Darken", - "Lighten", - "Difference", - "Exclusion", - "Hue", - "Saturation", - "Color", - "Luminosity", -]; - -const BLUR_FILTERS: &[&str] = &["Fast Gaussian", "Gaussian", "Box", "Bartlett", "Multi-box"]; -const CLIPPING_MODES: &[&str] = &["Frame", "Domain", "None"]; -const EDGES_MODES: &[&str] = &["Canvas", "Wrap", "Duplicate", "Mirror"]; -const SHAPE3D_TYPES: &[&str] = &[ - "SurfacePlaneInputs", - "SurfaceSphereInputs", - "SurfaceCubeInputs", - "SurfaceCylinderInputs", - "SurfaceConeInputs", - "SurfaceTorusInputs", -]; - -const fn flt(name: &'static str, label: &'static str, min: f64, max: f64, default: f64) -> ParamSpec { - ParamSpec { - name, - label, - kind: ParamKind::Float { min, max, default, step: 0.01 }, - } -} - -const fn v2(name: &'static str, label: &'static str, min: f64, max: f64, default: [f64; 2]) -> ParamSpec { - ParamSpec { - name, - label, - kind: ParamKind::Vec2 { min, max, default }, - } -} - -const fn boolean(name: &'static str, label: &'static str, default: bool) -> ParamSpec { - ParamSpec { - name, - label, - kind: ParamKind::Bool { default }, - } -} - -const fn fuid( - name: &'static str, - label: &'static str, - options: &'static [&'static str], - default: &'static str, -) -> ParamSpec { - ParamSpec { - name, - label, - kind: ParamKind::Enum { options, default }, - } -} - -/// Curated Fusion 21.1 parameter schemas for major compositor tools. -pub fn schema_for_kind(kind: &str) -> &'static [ParamSpec] { - match kind { - "Merge" => const { &[ - fuid("ApplyMode", "Apply Mode", APPLY_MODES, "Normal"), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - flt("SubMerge", "Subtractive", 0.0, 1.0, 0.0), - v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), - flt("Size", "Size", 0.0, 5.0, 1.0), - flt("Angle", "Angle", -360.0, 360.0, 0.0), - boolean("FlipHoriz", "Flip Horiz", false), - boolean("FlipVert", "Flip Vert", false), - fuid("Edges", "Edges", EDGES_MODES, "Canvas"), - boolean("PerformDepthMerge", "Depth Merge", false), - ] }, - "Transform" => const { &[ - v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), - v2("Pivot", "Pivot", -1.0, 2.0, [0.5, 0.5]), - flt("Size", "Size", 0.0, 5.0, 1.0), - flt("Aspect", "Aspect", 0.1, 4.0, 1.0), - flt("Angle", "Angle", -360.0, 360.0, 0.0), - boolean("FlipHoriz", "Flip Horiz", false), - boolean("FlipVert", "Flip Vert", false), - fuid("Edges", "Edges", EDGES_MODES, "Canvas"), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "Blur" => const { &[ - fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), - boolean("LockXY", "Lock X/Y", true), - flt("XBlurSize", "Blur Size", 0.0, 100.0, 1.0), - flt("YBlurSize", "Y Blur Size", 0.0, 100.0, 1.0), - fuid("ClippingMode", "Clipping", CLIPPING_MODES, "Frame"), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "Glow" => const { &[ - flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), - flt("Glow", "Glow", 0.0, 2.0, 0.8), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), - ] }, - "SoftGlow" => const { &[ - fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), - flt("Threshold", "Threshold", 0.0, 1.0, 0.0), - flt("Gain", "Gain", 0.0, 5.0, 2.0), - flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "BrightnessContrast" => const { &[ - flt("Gain", "Gain", 0.0, 5.0, 1.0), - flt("Lift", "Lift", -1.0, 1.0, 0.0), - flt("Gamma", "Gamma", 0.05, 5.0, 1.0), - flt("Contrast", "Contrast", -1.0, 1.0, 0.0), - flt("Brightness", "Brightness", -1.0, 1.0, 0.0), - flt("Saturation", "Saturation", 0.0, 4.0, 1.0), - boolean("PreDividePostMultiply", "Pre-Div/Post", false), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "ColorCorrector" => const { &[ - flt("WheelSaturation1", "Saturation", 0.0, 4.0, 1.0), - flt("WheelHue1", "Hue", -1.0, 1.0, 0.0), - flt("MasterRGBGain", "Gain", 0.0, 5.0, 1.0), - flt("MasterRGBGamma", "Gamma", 0.05, 5.0, 1.0), - flt("MasterRGBLift", "Lift", -1.0, 1.0, 0.0), - flt("MasterRGBContrast", "Contrast", 0.0, 3.0, 1.0), - flt("MasterRGBBrightness", "Brightness", -1.0, 1.0, 0.0), - boolean("PreDividePostMultiply", "Pre-Div/Post", false), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "Background" => const { &[ - ParamSpec { - name: "TopLeftColor", - label: "Color", - kind: ParamKind::Color { - r: "TopLeftRed", - g: "TopLeftGreen", - b: "TopLeftBlue", - a: Some("TopLeftAlpha"), - default: [0.0, 0.0, 0.0, 1.0], - }, - }, - flt("Width", "Width", 1.0, 7680.0, 1920.0), - flt("Height", "Height", 1.0, 4320.0, 1080.0), - ] }, - "TextPlus" => const { &[ - ParamSpec { - name: "StyledText", - label: "Styled Text", - kind: ParamKind::Text { default: "" }, - }, - ParamSpec { - name: "Font", - label: "Font", - kind: ParamKind::Text { default: "Open Sans" }, - }, - flt("Size", "Size", 0.005, 1.0, 0.08), - flt("Tracking", "Tracking", 0.0, 4.0, 1.0), - v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), - ParamSpec { - name: "TextColor", - label: "Text Color", - kind: ParamKind::Color { - r: "Red1", - g: "Green1", - b: "Blue1", - a: Some("Alpha1"), - default: [1.0, 1.0, 1.0, 1.0], - }, - }, - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "EllipseMask" => const { &[ - flt("Level", "Level", 0.0, 1.0, 1.0), - flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), - flt("BorderWidth", "Border", -0.2, 0.2, 0.0), - boolean("Solid", "Solid", true), - boolean("Invert", "Invert", false), - v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), - flt("Width", "Width", 0.0, 2.0, 0.5), - flt("Height", "Height", 0.0, 2.0, 0.5), - flt("Angle", "Angle", -360.0, 360.0, 0.0), - ] }, - "RectangleMask" => const { &[ - flt("Level", "Level", 0.0, 1.0, 1.0), - flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), - flt("CornerRadius", "Corner", 0.0, 1.0, 0.0), - boolean("Solid", "Solid", true), - boolean("Invert", "Invert", false), - v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), - flt("Width", "Width", 0.0, 2.0, 0.5), - flt("Height", "Height", 0.0, 2.0, 0.5), - flt("Angle", "Angle", -360.0, 360.0, 0.0), - ] }, - "Displace" => const { &[ - flt("XRefraction", "X Refract", -1.0, 1.0, 0.0), - flt("YRefraction", "Y Refract", -1.0, 1.0, 0.0), - flt("RefractionStrength", "Strength", -2.0, 2.0, 0.0), - flt("LightAngle", "Light Angle", -360.0, 360.0, 0.0), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "Vortex" => const { &[ - v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), - flt("Size", "Size", 0.0, 2.0, 0.5), - flt("Angle", "Angle", -720.0, 720.0, 90.0), - flt("Power", "Power", 0.1, 5.0, 1.0), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "FastNoise" => const { &[ - flt("Detail", "Detail", 0.0, 10.0, 2.0), - flt("Contrast", "Contrast", 0.0, 5.0, 1.0), - flt("Brightness", "Brightness", -1.0, 1.0, 0.0), - flt("XScale", "Scale", 0.01, 50.0, 2.0), - flt("Seethe", "Seethe", 0.0, 100.0, 0.0), - flt("SeetheRate", "Seethe Rate", 0.0, 2.0, 0.0), - v2("Center", "Center", -2.0, 3.0, [0.5, 0.5]), - ] }, - "UltraKeyer" => const { &[ - ParamSpec { - name: "KeyColor", - label: "Key Color", - kind: ParamKind::Color { - r: "BackgroundRed", - g: "BackgroundGreen", - b: "BackgroundBlue", - a: None, - default: [0.0, 1.0, 0.0, 1.0], - }, - }, - flt("MatteThresholdLow", "Matte Low", 0.0, 1.0, 0.0), - flt("MatteThresholdHigh", "Matte High", 0.0, 1.0, 1.0), - flt("SpillSuppression", "Spill", 0.0, 1.0, 0.0), - boolean("PostMultiplyImage", "Post-Multiply", true), - flt("Blend", "Blend", 0.0, 1.0, 1.0), - ] }, - "Loader" => const { &[ - flt("GlobalIn", "Global In", 0.0, 10000.0, 0.0), - flt("GlobalOut", "Global Out", 0.0, 10000.0, 100.0), - boolean("Loop", "Loop", false), - boolean("Reverse", "Reverse", false), - ] }, - "Camera3D" => const { &[ - flt("FLength", "Focal Len", 5.0, 200.0, 24.0), - flt("PlaneOfFocus", "Focus Dist", 0.1, 100.0, 5.0), - flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), - flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), - flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 5.0), - flt("Transform3DOp.Rotate.X", "Rot X", -180.0, 180.0, 0.0), - flt("Transform3DOp.Rotate.Y", "Rot Y", -180.0, 180.0, 0.0), - flt("Transform3DOp.Rotate.Z", "Rot Z", -180.0, 180.0, 0.0), - ] }, - "Shape3D" => const { &[ - fuid("Shape", "Shape", SHAPE3D_TYPES, "SurfacePlaneInputs"), - flt("SurfacePlaneInputs.Width", "Width", 0.01, 20.0, 1.0), - flt("SurfacePlaneInputs.Height", "Height", 0.01, 20.0, 1.0), - flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), - flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), - flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 0.0), - flt("Transform3DOp.Scale.X", "Scale", 0.01, 20.0, 1.0), - ] }, - _ => &[], - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn curated_schemas_cover_all_major_tools() { - let major = [ - "Merge", - "Transform", - "Blur", - "Glow", - "SoftGlow", - "BrightnessContrast", - "ColorCorrector", - "Background", - "TextPlus", - "EllipseMask", - "RectangleMask", - "Displace", - "Vortex", - "FastNoise", - "UltraKeyer", - "Loader", - "Camera3D", - "Shape3D", - ]; - for tool in major { - let s = schema_for_kind(tool); - assert!(!s.is_empty(), "expected non-empty curated schema for {tool}"); - } - assert!(schema_for_kind("UnknownCustomTool").is_empty()); - } - - #[test] - fn slider_math_and_clamping() { - assert!((normalize_slider_fraction(0.5, 0.0, 1.0) - 0.5).abs() < 1e-6); - assert!((normalize_slider_fraction(-5.0, 0.0, 1.0) - 0.0).abs() < 1e-6); - assert!((normalize_slider_fraction(10.0, 0.0, 1.0) - 1.0).abs() < 1e-6); - - let v = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 1.0); - assert!((v - 1.0).abs() < 1e-6); - let v_fine = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 0.1); - assert!((v_fine - 0.55).abs() < 1e-6); - } - - #[test] - fn keyframe_state_transitions() { - let src = r#"Composition { - Tools = ordered() { - BC1 = BrightnessContrast { - Inputs = { Gain = Input { Value = 1.0 } }, - }, - }, - }"#; - let mut comp = Comp::from_fusion_str(src).unwrap(); - let bc = comp.find_node("BC1").unwrap(); - assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); - - let sp = comp.set_keyframe(bc, "Gain", 10.0, 1.5).unwrap(); - assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::KeyframeAtCurrent); - assert_eq!(keyframe_state(&comp, bc, "Gain", 20.0), KeyframeState::AnimatedOtherFrame); - - assert!(comp.delete_keyframe(&sp, 10.0)); - assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); - } -} diff --git a/crates/app/src/inspector/mod.rs b/crates/app/src/inspector/mod.rs new file mode 100644 index 0000000000000000000000000000000000000000..4421e3a45abd2fad5a8242de99b7ad53e4928dcc --- /dev/null +++ b/crates/app/src/inspector/mod.rs @@ -0,0 +1,512 @@ +//! Inspector dock pane — Fusion 21.1 parameter editor with scrub sliders, Vec2, RGBA color groups, +//! booleans, FuID enum cyclers, text fields, expression badges, and keyframe diamonds (`◆`). + +mod schemas; +mod widgets; + +#[allow(unused_imports)] +pub use schemas::{ParamKind, ParamSpec, infer_float_range, schema_for_kind}; +#[allow(unused_imports)] +pub use widgets::{ + KeyframeState, compute_scrub_value, format_param_number, keyframe_state, + normalize_slider_fraction, +}; + +use engine::EvalStats; +use model::{Comp, CompHistory, ExprEngine, NodeId, is_supported_tool}; +use std::collections::{HashMap, HashSet}; +use std::time::Instant; +use widgets::DragState; + +#[derive(Clone, Debug)] +pub enum InspectorAction { + /// A parameter or node property changed; bust subtree & re-render viewers. + ParamEdited(NodeId), + /// A keyframe was added or deleted; refresh spline editor + bust subtree. + KeyframeEdited(NodeId), + /// Node was renamed. + NodeRenamed(NodeId), + /// Status notice to show in window title. + StatusNotice(String), +} + +#[derive(Clone, Copy, Debug)] +pub struct InspectorPalette { + pub bg: [f32; 4], + pub panel_bg: [f32; 4], + pub rule: [f32; 4], + pub text: [f32; 4], + pub text_dim: [f32; 4], + pub accent: [f32; 4], +} + +impl InspectorPalette { + pub fn from_theme(theme: &ui::Theme) -> Self { + let b = theme.base; + let lift = |c: [f32; 4], d: f32| { + [ + (c[0] + d).clamp(0.0, 1.0), + (c[1] + d).clamp(0.0, 1.0), + (c[2] + d).clamp(0.0, 1.0), + 1.0, + ] + }; + Self { + bg: b, + panel_bg: lift(b, 0.020), + rule: theme.rule, + text: theme.text, + text_dim: theme.text_dim, + accent: theme.accent, + } + } +} + +pub struct InspectorState { + pub palette: InspectorPalette, + expr: ExprEngine, + /// Node name edit buffer synced with the currently selected node. + name_node: Option, + name_buf: String, + /// Text parameter edit buffers keyed by `(node_id, param_name)`. + text_bufs: HashMap<(usize, String), String>, + /// Active scrub slider drag. + drag: Option, + /// Last click timestamp per slider key for double-click reset. + last_click: HashMap, +} + +impl InspectorState { + pub fn new(theme: &ui::Theme) -> Self { + Self { + palette: InspectorPalette::from_theme(theme), + expr: ExprEngine::new(), + name_node: None, + name_buf: String::new(), + text_bufs: HashMap::new(), + drag: None, + last_click: HashMap::new(), + } + } + + pub fn background(&self) -> [f32; 4] { + self.palette.bg + } + + pub fn reset(&mut self) { + self.name_node = None; + self.name_buf.clear(); + self.text_bufs.clear(); + self.drag = None; + } + + pub fn build_ui( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + selected: Option, + current_frame: f64, + stats: &EvalStats, + ) -> Vec { + let mut actions = Vec::new(); + let p = self.palette; + + ui.open_as( + pane_id, + ui::Spec { + size: [ui::fill(), ui::fill()], + axis: ui::Axis::Y, + fill: Some(p.bg), + ..ui::Spec::default() + }, + ); + + let Some(node_id) = selected.filter(|id| id.0 < comp.nodes.len()) else { + // Empty state + ui.open( + "insp-empty", + ui::Spec { + size: [ui::fill(), ui::fill()], + axis: ui::Axis::Y, + pad: [14.0, 18.0], + gap: 8.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "empty-title", + ui::Spec { + size: [ui::fill(), ui::px(20.0)], + text: Some("No Node Selected"), + font_size: Some(12.5), + medium: true, + color: Some(p.text), + ..ui::Spec::default() + }, + ); + ui.leaf( + "empty-hint", + ui::Spec { + size: [ui::fill(), ui::fit()], + text: Some("Select a node in the Flow graph to inspect and keyframe its parameters, or press Shift+Space to add a tool."), + font_size: Some(11.0), + overflow: ui::Overflow::Wrap, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + self.render_stats_footer(ui, stats); + ui.close(); + ui.close(); + return actions; + }; + + // Sync editable name buffer when selection changes or node was renamed externally (e.g. undo) + let current_name = comp.node(node_id).name.clone(); + let name_field_id = pane_id.child(("node-name", node_id.0)); + if self.name_node != Some(node_id) + || (ui.focused() != Some(name_field_id) && self.name_buf != current_name) + { + self.name_node = Some(node_id); + self.name_buf = current_name.clone(); + } + + // 1. Header: Node name + Class badge + Bypass toggle + ui.open( + "insp-header", + ui::Spec { + size: [ui::fill(), ui::children()], + axis: ui::Axis::Y, + pad: [10.0, 8.0], + gap: 6.0, + fill: Some(p.panel_bg), + border: Some(p.rule), + ..ui::Spec::default() + }, + ); + + // Row 1: Editable name + Bypass button + ui.open( + "insp-name-row", + ui::Spec { + size: [ui::fill(), ui::px(26.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + let name_sig = ui::text_field( + ui, + name_field_id, + &mut self.name_buf, + "Node name", + ui::Spec { + size: [ui::fill(), ui::px(24.0)], + font_size: Some(12.0), + medium: true, + fill: Some(p.bg), + border: Some(p.rule), + hover_border: Some(p.accent), + radius: 4.0, + pad: [6.0, 2.0], + ..ui::Spec::default() + }, + ); + let enter_pressed = name_sig.events.iter().any(|ev| { + matches!( + ev, + ui::Event::Key { key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter), .. } + ) + }); + if enter_pressed && self.name_buf.trim() != current_name { + let desired = self.name_buf.trim().to_owned(); + history.push_snapshot(comp, "Rename Node"); + match comp.rename_node(node_id, &desired) { + Ok(applied) => { + self.name_buf = applied; + actions.push(InspectorAction::NodeRenamed(node_id)); + } + Err(e) => { + self.name_buf = current_name.clone(); + actions.push(InspectorAction::StatusNotice(e)); + } + } + ui.set_focus(None); + } + + let bypassed = comp.node(node_id).bypassed; + let pass_lbl = if bypassed { "Bypassed" } else { "Active" }; + let pass_fill = if bypassed { + [0.85, 0.28, 0.28, 0.28] + } else { + [0.24, 0.78, 0.48, 0.18] + }; + let pass_border = if bypassed { + [0.92, 0.36, 0.36, 0.75] + } else { + [0.28, 0.82, 0.52, 0.55] + }; + let pass_sig = ui.leaf( + ("insp-pass", node_id.0), + ui::Spec { + cursor: Some(winit::window::CursorIcon::Pointer), + flags: ui::Flags::CLICKABLE, + size: [ui::fit(), ui::px(24.0)], + text: Some(pass_lbl), + font_size: Some(10.5), + medium: true, + color: Some(p.text), + fill: Some(pass_fill), + border: Some(pass_border), + radius: 4.0, + pad: [8.0, 0.0], + center: true, + ..ui::Spec::default() + }, + ); + if pass_sig.clicked { + history.push_snapshot(comp, "Toggle Passthrough"); + comp.toggle_passthrough(node_id); + actions.push(InspectorAction::ParamEdited(node_id)); + } + ui.close(); // insp-name-row + + // Row 2: Tool class badge + supported/preserved indicator + frame badge + let node_kind = comp.node(node_id).kind.clone(); + let supported = is_supported_tool(&node_kind); + let frame_str = format!("f{:.0}", current_frame); + ui.open( + "insp-meta-row", + ui::Spec { + size: [ui::fill(), ui::px(18.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "insp-kind-badge", + ui::Spec { + size: [ui::fit(), ui::px(18.0)], + text: Some(&node_kind), + font_size: Some(10.0), + medium: true, + color: Some(p.accent), + fill: Some([p.accent[0], p.accent[1], p.accent[2], 0.14]), + border: Some([p.accent[0], p.accent[1], p.accent[2], 0.40]), + radius: 3.0, + pad: [6.0, 0.0], + center: true, + ..ui::Spec::default() + }, + ); + if !supported { + ui.leaf( + "insp-preserved-badge", + ui::Spec { + size: [ui::fit(), ui::px(18.0)], + text: Some("preserved"), + font_size: Some(9.5), + medium: true, + color: Some([0.98, 0.78, 0.40, 1.0]), + fill: Some([0.24, 0.16, 0.06, 0.85]), + border: Some([0.92, 0.64, 0.24, 0.60]), + radius: 3.0, + pad: [5.0, 0.0], + center: true, + ..ui::Spec::default() + }, + ); + } + ui.leaf( + "insp-spacer", + ui::Spec { + size: [ui::fill(), ui::px(18.0)], + ..ui::Spec::default() + }, + ); + ui.leaf( + "insp-frame", + ui::Spec { + size: [ui::fit(), ui::px(18.0)], + text: Some(&frame_str), + font_size: Some(10.0), + color: Some(p.text_dim), + center: true, + ..ui::Spec::default() + }, + ); + ui.close(); // insp-meta-row + ui.close(); // insp-header + + // 2. Scrollable Parameter List + ui.open( + "insp-scroll", + ui::Spec { + flags: ui::Flags::SCROLL | ui::Flags::CLIP, + size: [ui::fill(), ui::fill()], + axis: ui::Axis::Y, + pad: [10.0, 8.0], + gap: 6.0, + ..ui::Spec::default() + }, + ); + + let schema = schema_for_kind(&node_kind); + let mut covered: HashSet<&str> = HashSet::new(); + + for spec in schema { + covered.insert(spec.name); + if let ParamKind::Color { r, g, b, a, .. } = spec.kind { + covered.insert(r); + covered.insert(g); + covered.insert(b); + if let Some(alpha_k) = a { + covered.insert(alpha_k); + } + } + self.render_param_spec(ui, pane_id, comp, history, node_id, spec, current_frame, &mut actions); + } + + // Fallback: list all remaining present non-image inputs on the node! + let extra_keys = schemas::extra_input_keys(comp, node_id, &covered); + if !extra_keys.is_empty() { + ui.leaf( + "insp-extra-hdr", + ui::Spec { + size: [ui::fill(), ui::px(18.0)], + text: Some("OTHER INPUTS"), + font_size: Some(9.5), + medium: true, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + for key in extra_keys { + self.render_fallback_input(ui, pane_id, comp, history, node_id, &key, current_frame, &mut actions); + } + } + + // Loader clip info if Loader node + if node_kind == "Loader" { + let clip_summary = comp + .node(node_id) + .clips + .first() + .map(|c| c.filename.clone()) + .unwrap_or_else(|| "(no clip)".to_owned()); + ui.open( + "insp-loader-clip", + ui::Spec { + size: [ui::fill(), ui::children()], + axis: ui::Axis::Y, + gap: 3.0, + pad: [6.0, 5.0], + fill: Some(p.panel_bg), + border: Some(p.rule), + radius: 4.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "clip-lbl", + ui::Spec { + size: [ui::fill(), ui::px(14.0)], + text: Some("Clip Filename"), + font_size: Some(10.0), + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + ui.leaf( + "clip-path", + ui::Spec { + size: [ui::fill(), ui::fit()], + text: Some(&clip_summary), + font_size: Some(10.5), + overflow: ui::Overflow::Wrap, + color: Some(p.text), + ..ui::Spec::default() + }, + ); + ui.close(); + } + + ui.close(); // insp-scroll + + // 3. Footer: EvalStats summary + self.render_stats_footer(ui, stats); + + ui.close(); // pane_id + actions + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn curated_schemas_cover_all_major_tools() { + let major = [ + "Merge", + "Transform", + "Blur", + "Glow", + "SoftGlow", + "BrightnessContrast", + "ColorCorrector", + "Background", + "TextPlus", + "EllipseMask", + "RectangleMask", + "Displace", + "Vortex", + "FastNoise", + "UltraKeyer", + "Loader", + "Camera3D", + "Shape3D", + ]; + for tool in major { + let s = schema_for_kind(tool); + assert!(!s.is_empty(), "expected non-empty curated schema for {tool}"); + } + assert!(schema_for_kind("UnknownCustomTool").is_empty()); + } + + #[test] + fn slider_math_and_clamping() { + assert!((normalize_slider_fraction(0.5, 0.0, 1.0) - 0.5).abs() < 1e-6); + assert!((normalize_slider_fraction(-5.0, 0.0, 1.0) - 0.0).abs() < 1e-6); + assert!((normalize_slider_fraction(10.0, 0.0, 1.0) - 1.0).abs() < 1e-6); + + let v = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 1.0); + assert!((v - 1.0).abs() < 1e-6); + let v_fine = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 0.1); + assert!((v_fine - 0.55).abs() < 1e-6); + } + + #[test] + fn keyframe_state_transitions() { + let src = r#"Composition { + Tools = ordered() { + BC1 = BrightnessContrast { + Inputs = { Gain = Input { Value = 1.0 } }, + }, + }, + }"#; + let mut comp = Comp::from_fusion_str(src).unwrap(); + let bc = comp.find_node("BC1").unwrap(); + assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); + + let sp = comp.set_keyframe(bc, "Gain", 10.0, 1.5).unwrap(); + assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::KeyframeAtCurrent); + assert_eq!(keyframe_state(&comp, bc, "Gain", 20.0), KeyframeState::AnimatedOtherFrame); + + assert!(comp.delete_keyframe(&sp, 10.0)); + assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); + } +} diff --git a/crates/app/src/inspector/schemas.rs b/crates/app/src/inspector/schemas.rs new file mode 100644 index 0000000000000000000000000000000000000000..6ec2f01f2c98ad6ff47507224e172ce7eb0fab28 --- /dev/null +++ b/crates/app/src/inspector/schemas.rs @@ -0,0 +1,347 @@ +//! Curated Fusion 21.1 parameter schemas and fallback input discovery for the Inspector. + +use model::{Comp, NodeId}; +use std::collections::HashSet; + +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum ParamKind { + Float { min: f64, max: f64, default: f64, step: f64 }, + Vec2 { min: f64, max: f64, default: [f64; 2] }, + Bool { default: bool }, + Enum { options: &'static [&'static str], default: &'static str }, + Color { + r: &'static str, + g: &'static str, + b: &'static str, + a: Option<&'static str>, + default: [f64; 4], + }, + Text { default: &'static str }, +} + +#[derive(Clone, Copy, Debug)] +pub struct ParamSpec { + pub name: &'static str, + pub label: &'static str, + pub kind: ParamKind, +} + +/// Collects and sorts remaining non-image inputs on `node_id` not covered by its curated schema. +pub(super) fn extra_input_keys(comp: &Comp, node_id: NodeId, covered: &HashSet<&str>) -> Vec { + let mut extra_keys: Vec = comp + .node(node_id) + .inputs + .iter() + .filter_map(|(k, b)| { + if covered.contains(k.as_str()) { + return None; + } + // Skip image/node connections unless they also have a value or spline + let is_node_conn = b + .source_op + .as_deref() + .is_some_and(|op| comp.node_by_name.contains_key(op)); + if is_node_conn && b.value.is_none() { + return None; + } + if b.value.is_none() && b.expression.is_none() && b.source_op.is_none() { + return None; + } + Some(k.clone()) + }) + .collect(); + extra_keys.sort(); + extra_keys +} + +pub fn infer_float_range(name: &str, val: f64) -> (f64, f64) { + let lower = name.to_ascii_lowercase(); + if lower.contains("angle") || lower.contains("rotate") { + (-360.0, 360.0) + } else if lower.contains("width") || lower.contains("height") { + if val > 2.0 { + (1.0, (val * 2.0).max(4096.0)) + } else { + (0.0, 2.0) + } + } else if lower.contains("blend") || lower.contains("alpha") || lower.contains("opacity") { + (0.0, 1.0) + } else if lower.contains("size") || lower.contains("blur") || lower.contains("glow") { + (0.0, (val.abs() * 2.0).max(50.0)) + } else if val < 0.0 || val > 1.0 { + let hi = (val.abs() * 2.0).max(10.0); + (-hi, hi) + } else { + (0.0, 1.0) + } +} + +const APPLY_MODES: &[&str] = &[ + "Normal", + "Screen", + "Multiply", + "Overlay", + "Soft Light", + "Hard Light", + "Color Dodge", + "Color Burn", + "Darken", + "Lighten", + "Difference", + "Exclusion", + "Hue", + "Saturation", + "Color", + "Luminosity", +]; + +const BLUR_FILTERS: &[&str] = &["Fast Gaussian", "Gaussian", "Box", "Bartlett", "Multi-box"]; +const CLIPPING_MODES: &[&str] = &["Frame", "Domain", "None"]; +const EDGES_MODES: &[&str] = &["Canvas", "Wrap", "Duplicate", "Mirror"]; +const SHAPE3D_TYPES: &[&str] = &[ + "SurfacePlaneInputs", + "SurfaceSphereInputs", + "SurfaceCubeInputs", + "SurfaceCylinderInputs", + "SurfaceConeInputs", + "SurfaceTorusInputs", +]; + +const fn flt(name: &'static str, label: &'static str, min: f64, max: f64, default: f64) -> ParamSpec { + ParamSpec { + name, + label, + kind: ParamKind::Float { min, max, default, step: 0.01 }, + } +} + +const fn v2(name: &'static str, label: &'static str, min: f64, max: f64, default: [f64; 2]) -> ParamSpec { + ParamSpec { + name, + label, + kind: ParamKind::Vec2 { min, max, default }, + } +} + +const fn boolean(name: &'static str, label: &'static str, default: bool) -> ParamSpec { + ParamSpec { + name, + label, + kind: ParamKind::Bool { default }, + } +} + +const fn fuid( + name: &'static str, + label: &'static str, + options: &'static [&'static str], + default: &'static str, +) -> ParamSpec { + ParamSpec { + name, + label, + kind: ParamKind::Enum { options, default }, + } +} + +/// Curated Fusion 21.1 parameter schemas for major compositor tools. +pub fn schema_for_kind(kind: &str) -> &'static [ParamSpec] { + match kind { + "Merge" => const { &[ + fuid("ApplyMode", "Apply Mode", APPLY_MODES, "Normal"), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + flt("SubMerge", "Subtractive", 0.0, 1.0, 0.0), + v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), + flt("Size", "Size", 0.0, 5.0, 1.0), + flt("Angle", "Angle", -360.0, 360.0, 0.0), + boolean("FlipHoriz", "Flip Horiz", false), + boolean("FlipVert", "Flip Vert", false), + fuid("Edges", "Edges", EDGES_MODES, "Canvas"), + boolean("PerformDepthMerge", "Depth Merge", false), + ] }, + "Transform" => const { &[ + v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), + v2("Pivot", "Pivot", -1.0, 2.0, [0.5, 0.5]), + flt("Size", "Size", 0.0, 5.0, 1.0), + flt("Aspect", "Aspect", 0.1, 4.0, 1.0), + flt("Angle", "Angle", -360.0, 360.0, 0.0), + boolean("FlipHoriz", "Flip Horiz", false), + boolean("FlipVert", "Flip Vert", false), + fuid("Edges", "Edges", EDGES_MODES, "Canvas"), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "Blur" => const { &[ + fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), + boolean("LockXY", "Lock X/Y", true), + flt("XBlurSize", "Blur Size", 0.0, 100.0, 1.0), + flt("YBlurSize", "Y Blur Size", 0.0, 100.0, 1.0), + fuid("ClippingMode", "Clipping", CLIPPING_MODES, "Frame"), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "Glow" => const { &[ + flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), + flt("Glow", "Glow", 0.0, 2.0, 0.8), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), + ] }, + "SoftGlow" => const { &[ + fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), + flt("Threshold", "Threshold", 0.0, 1.0, 0.0), + flt("Gain", "Gain", 0.0, 5.0, 2.0), + flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "BrightnessContrast" => const { &[ + flt("Gain", "Gain", 0.0, 5.0, 1.0), + flt("Lift", "Lift", -1.0, 1.0, 0.0), + flt("Gamma", "Gamma", 0.05, 5.0, 1.0), + flt("Contrast", "Contrast", -1.0, 1.0, 0.0), + flt("Brightness", "Brightness", -1.0, 1.0, 0.0), + flt("Saturation", "Saturation", 0.0, 4.0, 1.0), + boolean("PreDividePostMultiply", "Pre-Div/Post", false), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "ColorCorrector" => const { &[ + flt("WheelSaturation1", "Saturation", 0.0, 4.0, 1.0), + flt("WheelHue1", "Hue", -1.0, 1.0, 0.0), + flt("MasterRGBGain", "Gain", 0.0, 5.0, 1.0), + flt("MasterRGBGamma", "Gamma", 0.05, 5.0, 1.0), + flt("MasterRGBLift", "Lift", -1.0, 1.0, 0.0), + flt("MasterRGBContrast", "Contrast", 0.0, 3.0, 1.0), + flt("MasterRGBBrightness", "Brightness", -1.0, 1.0, 0.0), + boolean("PreDividePostMultiply", "Pre-Div/Post", false), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "Background" => const { &[ + ParamSpec { + name: "TopLeftColor", + label: "Color", + kind: ParamKind::Color { + r: "TopLeftRed", + g: "TopLeftGreen", + b: "TopLeftBlue", + a: Some("TopLeftAlpha"), + default: [0.0, 0.0, 0.0, 1.0], + }, + }, + flt("Width", "Width", 1.0, 7680.0, 1920.0), + flt("Height", "Height", 1.0, 4320.0, 1080.0), + ] }, + "TextPlus" => const { &[ + ParamSpec { + name: "StyledText", + label: "Styled Text", + kind: ParamKind::Text { default: "" }, + }, + ParamSpec { + name: "Font", + label: "Font", + kind: ParamKind::Text { default: "Open Sans" }, + }, + flt("Size", "Size", 0.005, 1.0, 0.08), + flt("Tracking", "Tracking", 0.0, 4.0, 1.0), + v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), + ParamSpec { + name: "TextColor", + label: "Text Color", + kind: ParamKind::Color { + r: "Red1", + g: "Green1", + b: "Blue1", + a: Some("Alpha1"), + default: [1.0, 1.0, 1.0, 1.0], + }, + }, + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "EllipseMask" => const { &[ + flt("Level", "Level", 0.0, 1.0, 1.0), + flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), + flt("BorderWidth", "Border", -0.2, 0.2, 0.0), + boolean("Solid", "Solid", true), + boolean("Invert", "Invert", false), + v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), + flt("Width", "Width", 0.0, 2.0, 0.5), + flt("Height", "Height", 0.0, 2.0, 0.5), + flt("Angle", "Angle", -360.0, 360.0, 0.0), + ] }, + "RectangleMask" => const { &[ + flt("Level", "Level", 0.0, 1.0, 1.0), + flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), + flt("CornerRadius", "Corner", 0.0, 1.0, 0.0), + boolean("Solid", "Solid", true), + boolean("Invert", "Invert", false), + v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), + flt("Width", "Width", 0.0, 2.0, 0.5), + flt("Height", "Height", 0.0, 2.0, 0.5), + flt("Angle", "Angle", -360.0, 360.0, 0.0), + ] }, + "Displace" => const { &[ + flt("XRefraction", "X Refract", -1.0, 1.0, 0.0), + flt("YRefraction", "Y Refract", -1.0, 1.0, 0.0), + flt("RefractionStrength", "Strength", -2.0, 2.0, 0.0), + flt("LightAngle", "Light Angle", -360.0, 360.0, 0.0), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "Vortex" => const { &[ + v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), + flt("Size", "Size", 0.0, 2.0, 0.5), + flt("Angle", "Angle", -720.0, 720.0, 90.0), + flt("Power", "Power", 0.1, 5.0, 1.0), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "FastNoise" => const { &[ + flt("Detail", "Detail", 0.0, 10.0, 2.0), + flt("Contrast", "Contrast", 0.0, 5.0, 1.0), + flt("Brightness", "Brightness", -1.0, 1.0, 0.0), + flt("XScale", "Scale", 0.01, 50.0, 2.0), + flt("Seethe", "Seethe", 0.0, 100.0, 0.0), + flt("SeetheRate", "Seethe Rate", 0.0, 2.0, 0.0), + v2("Center", "Center", -2.0, 3.0, [0.5, 0.5]), + ] }, + "UltraKeyer" => const { &[ + ParamSpec { + name: "KeyColor", + label: "Key Color", + kind: ParamKind::Color { + r: "BackgroundRed", + g: "BackgroundGreen", + b: "BackgroundBlue", + a: None, + default: [0.0, 1.0, 0.0, 1.0], + }, + }, + flt("MatteThresholdLow", "Matte Low", 0.0, 1.0, 0.0), + flt("MatteThresholdHigh", "Matte High", 0.0, 1.0, 1.0), + flt("SpillSuppression", "Spill", 0.0, 1.0, 0.0), + boolean("PostMultiplyImage", "Post-Multiply", true), + flt("Blend", "Blend", 0.0, 1.0, 1.0), + ] }, + "Loader" => const { &[ + flt("GlobalIn", "Global In", 0.0, 10000.0, 0.0), + flt("GlobalOut", "Global Out", 0.0, 10000.0, 100.0), + boolean("Loop", "Loop", false), + boolean("Reverse", "Reverse", false), + ] }, + "Camera3D" => const { &[ + flt("FLength", "Focal Len", 5.0, 200.0, 24.0), + flt("PlaneOfFocus", "Focus Dist", 0.1, 100.0, 5.0), + flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), + flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), + flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 5.0), + flt("Transform3DOp.Rotate.X", "Rot X", -180.0, 180.0, 0.0), + flt("Transform3DOp.Rotate.Y", "Rot Y", -180.0, 180.0, 0.0), + flt("Transform3DOp.Rotate.Z", "Rot Z", -180.0, 180.0, 0.0), + ] }, + "Shape3D" => const { &[ + fuid("Shape", "Shape", SHAPE3D_TYPES, "SurfacePlaneInputs"), + flt("SurfacePlaneInputs.Width", "Width", 0.01, 20.0, 1.0), + flt("SurfacePlaneInputs.Height", "Height", 0.01, 20.0, 1.0), + flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), + flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), + flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 0.0), + flt("Transform3DOp.Scale.X", "Scale", 0.01, 20.0, 1.0), + ] }, + _ => &[], + } +} diff --git a/crates/app/src/inspector/widgets.rs b/crates/app/src/inspector/widgets.rs new file mode 100644 index 0000000000000000000000000000000000000000..8fa4c95af719367bc04f5c6f2a47da3dadd58132 --- /dev/null +++ b/crates/app/src/inspector/widgets.rs @@ -0,0 +1,969 @@ +//! Reusable Inspector UI control builders: scrub sliders, Vec2, RGBA color groups, +//! booleans, FuID enum cyclers, text fields, expression badges, and keyframe diamonds (`◆`). + +use super::schemas::{ParamKind, ParamSpec, infer_float_range}; +use super::{InspectorAction, InspectorState}; +use engine::EvalStats; +use model::{Comp, CompHistory, NodeId, ParamValue}; +use std::time::{Duration, Instant}; + +#[derive(Clone, Debug)] +pub(super) struct DragState { + pub node_id: NodeId, + pub key: String, + pub start_x: f32, + pub start_val: f64, + pub min: f64, + pub max: f64, + pub width: f32, +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum KeyframeState { + Static, + AnimatedOtherFrame, + KeyframeAtCurrent, +} + +impl InspectorState { + pub(super) fn render_stats_footer(&self, ui: &mut ui::Ui, stats: &EvalStats) { + let p = self.palette; + let line = format!( + "Eval {:.1}ms · {} nodes · cache {}/{} · {:.0}MB", + stats.eval_ms, + stats.nodes_evaluated, + stats.cache_hits, + stats.cache_hits + stats.cache_misses, + stats.cached_vram_mb, + ); + ui.open( + "insp-footer", + ui::Spec { + size: [ui::fill(), ui::px(24.0)], + axis: ui::Axis::X, + pad: [10.0, 4.0], + fill: Some(p.panel_bg), + border: Some(p.rule), + ..ui::Spec::default() + }, + ); + ui.leaf( + "insp-stats-txt", + ui::Spec { + size: [ui::fill(), ui::fill()], + text: Some(&line), + font_size: Some(10.0), + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + pub(super) fn render_param_spec( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + spec: &ParamSpec, + current_frame: f64, + actions: &mut Vec, + ) { + match spec.kind { + ParamKind::Float { min, max, default, step: _ } => { + self.render_float_row( + ui, + pane_id, + comp, + history, + node_id, + spec.name, + spec.label, + min, + max, + default, + current_frame, + actions, + ); + } + ParamKind::Vec2 { min, max, default } => { + self.render_vec2_row( + ui, + pane_id, + comp, + history, + node_id, + spec.name, + spec.label, + min, + max, + default, + current_frame, + actions, + ); + } + ParamKind::Bool { default } => { + self.render_bool_row(ui, comp, history, node_id, spec.name, spec.label, default, actions); + } + ParamKind::Enum { options, default } => { + self.render_enum_row(ui, comp, history, node_id, spec.name, spec.label, options, default, actions); + } + ParamKind::Color { r, g, b, a, default } => { + self.render_color_group( + ui, + pane_id, + comp, + history, + node_id, + spec.label, + r, + g, + b, + a, + default, + current_frame, + actions, + ); + } + ParamKind::Text { default } => { + self.render_text_row(ui, pane_id, comp, history, node_id, spec.name, spec.label, default, actions); + } + } + } + + #[allow(clippy::too_many_arguments)] + pub(super) fn render_fallback_input( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + key: &str, + current_frame: f64, + actions: &mut Vec, + ) { + let val = comp.node(node_id).inputs.get(key).and_then(|b| b.value.clone()); + match val { + Some(ParamValue::Point(pt)) => { + self.render_vec2_row(ui, pane_id, comp, history, node_id, key, key, -1.0, 2.0, pt, current_frame, actions); + } + Some(ParamValue::Text(_)) => { + self.render_text_row(ui, pane_id, comp, history, node_id, key, key, "", actions); + } + Some(ParamValue::FuId(cur)) => { + let p = self.palette; + ui.open( + ("fb-fuid", node_id.0, key), + ui::Spec { + size: [ui::fill(), ui::px(22.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "lbl", + ui::Spec { + size: [ui::px(92.0), ui::fill()], + text: Some(key), + font_size: Some(10.5), + overflow: ui::Overflow::Ellipsis, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + ui.leaf( + "val", + ui::Spec { + size: [ui::fill(), ui::fill()], + text: Some(&cur), + font_size: Some(10.5), + color: Some(p.text), + fill: Some(p.panel_bg), + border: Some(p.rule), + radius: 4.0, + pad: [6.0, 0.0], + ..ui::Spec::default() + }, + ); + ui.close(); + } + Some(ParamValue::Number(v)) => { + let (min, max) = infer_float_range(key, v); + self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, v, current_frame, actions); + } + None => { + // Animated spline or expression-only scalar input + let cur = comp.eval_number(comp.node(node_id), key, current_frame, 0.0, &self.expr); + let (min, max) = infer_float_range(key, cur); + self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, 0.0, current_frame, actions); + } + _ => {} + } + } + + #[allow(clippy::too_many_arguments)] + fn render_float_row( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + input_name: &str, + label: &str, + min: f64, + max: f64, + default: f64, + current_frame: f64, + actions: &mut Vec, + ) { + let cur_val = comp.eval_number(comp.node(node_id), input_name, current_frame, default, &self.expr); + let has_expr = comp + .node(node_id) + .inputs + .get(input_name) + .and_then(|b| b.expression.as_ref()) + .is_some(); + let kf_state = keyframe_state(comp, node_id, input_name, current_frame); + let p = self.palette; + + ui.open( + ("row-f", node_id.0, input_name), + ui::Spec { + size: [ui::fill(), ui::px(22.0)], + axis: ui::Axis::X, + gap: 4.0, + ..ui::Spec::default() + }, + ); + + // Label + ui.leaf( + "lbl", + ui::Spec { + size: [ui::px(86.0), ui::fill()], + text: Some(label), + font_size: Some(10.5), + overflow: ui::Overflow::Ellipsis, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + + // Expression `=` badge if present + if has_expr { + render_expr_badge(ui); + } + + // Scrub slider + let slider_id = pane_id.child(("sld", node_id.0, input_name)); + let drag_key = format!("{}:{input_name}", node_id.0); + if let Some(new_val) = self.scrub_slider( + ui, + slider_id, + &drag_key, + node_id, + cur_val, + min, + max, + default, + history, + comp, + input_name, + ) { + if kf_state != KeyframeState::Static { + let _ = comp.set_keyframe(node_id, input_name, current_frame, new_val); + actions.push(InspectorAction::KeyframeEdited(node_id)); + } else { + comp.set_input_value(node_id, input_name, ParamValue::Number(new_val)); + actions.push(InspectorAction::ParamEdited(node_id)); + } + } + + // Reset button (small dot/arrow when non-default) + let is_default = (cur_val - default).abs() < 1e-4; + if !is_default { + let rst = ui.leaf( + ("rst", node_id.0, input_name), + ui::Spec { + cursor: Some(winit::window::CursorIcon::Pointer), + flags: ui::Flags::CLICKABLE, + size: [ui::px(18.0), ui::px(20.0)], + text: Some("↺"), + font_size: Some(10.5), + color: Some(p.text_dim), + hover_fill: Some(p.panel_bg), + radius: 3.0, + center: true, + ..ui::Spec::default() + }, + ); + if rst.clicked { + history.push_snapshot(comp, format!("Reset {label}")); + if kf_state != KeyframeState::Static { + let _ = comp.set_keyframe(node_id, input_name, current_frame, default); + actions.push(InspectorAction::KeyframeEdited(node_id)); + } else { + comp.set_input_value(node_id, input_name, ParamValue::Number(default)); + actions.push(InspectorAction::ParamEdited(node_id)); + } + } + } + + // Keyframe diamond button (`◆`) + if self.keyframe_button(ui, ("kf", node_id.0, input_name), kf_state) { + self.toggle_keyframe(comp, history, node_id, input_name, current_frame, cur_val, kf_state, actions); + } + + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + fn render_vec2_row( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + input_name: &str, + label: &str, + min: f64, + max: f64, + default: [f64; 2], + current_frame: f64, + actions: &mut Vec, + ) { + let cur = comp.eval_point(comp.node(node_id), input_name, current_frame, default, &self.expr); + let has_expr = comp + .node(node_id) + .inputs + .get(input_name) + .and_then(|b| b.expression.as_ref()) + .is_some(); + + ui.open( + ("row-v2", node_id.0, input_name), + ui::Spec { + size: [ui::fill(), ui::px(22.0)], + axis: ui::Axis::X, + gap: 4.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "lbl", + ui::Spec { + size: [ui::px(86.0), ui::fill()], + text: Some(label), + font_size: Some(10.5), + overflow: ui::Overflow::Ellipsis, + color: Some(self.palette.text_dim), + ..ui::Spec::default() + }, + ); + if has_expr { + render_expr_badge(ui); + } + + let x_id = pane_id.child(("v2x", node_id.0, input_name)); + let x_key = format!("{}:{input_name}:x", node_id.0); + if let Some(nx) = self.scrub_slider( + ui, + x_id, + &x_key, + node_id, + cur[0], + min, + max, + default[0], + history, + comp, + input_name, + ) { + comp.set_input_value(node_id, input_name, ParamValue::Point([nx, cur[1]])); + actions.push(InspectorAction::ParamEdited(node_id)); + } + + let y_id = pane_id.child(("v2y", node_id.0, input_name)); + let y_key = format!("{}:{input_name}:y", node_id.0); + if let Some(ny) = self.scrub_slider( + ui, + y_id, + &y_key, + node_id, + cur[1], + min, + max, + default[1], + history, + comp, + input_name, + ) { + comp.set_input_value(node_id, input_name, ParamValue::Point([cur[0], ny])); + actions.push(InspectorAction::ParamEdited(node_id)); + } + + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + fn render_bool_row( + &mut self, + ui: &mut ui::Ui, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + input_name: &str, + label: &str, + default: bool, + actions: &mut Vec, + ) { + let checked = comp + .node(node_id) + .static_number(input_name) + .map(|v| v >= 0.5) + .unwrap_or(default); + let p = self.palette; + + ui.open( + ("row-b", node_id.0, input_name), + ui::Spec { + size: [ui::fill(), ui::px(22.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "lbl", + ui::Spec { + size: [ui::px(86.0), ui::fill()], + text: Some(label), + font_size: Some(10.5), + overflow: ui::Overflow::Ellipsis, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + let fill = if checked { + [p.accent[0], p.accent[1], p.accent[2], 0.28] + } else { + p.panel_bg + }; + let border = if checked { p.accent } else { p.rule }; + let sig = ui.leaf( + ("chk", node_id.0, input_name), + ui::Spec { + cursor: Some(winit::window::CursorIcon::Pointer), + flags: ui::Flags::CLICKABLE, + size: [ui::px(54.0), ui::px(20.0)], + text: Some(if checked { "✓ On" } else { "Off" }), + font_size: Some(10.5), + medium: checked, + color: Some(if checked { p.text } else { p.text_dim }), + fill: Some(fill), + border: Some(border), + radius: 4.0, + center: true, + ..ui::Spec::default() + }, + ); + if sig.clicked { + history.push_snapshot(comp, format!("Toggle {label}")); + let next = if checked { 0.0 } else { 1.0 }; + comp.set_input_value(node_id, input_name, ParamValue::Number(next)); + actions.push(InspectorAction::ParamEdited(node_id)); + } + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + fn render_enum_row( + &mut self, + ui: &mut ui::Ui, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + input_name: &str, + label: &str, + options: &'static [&'static str], + default: &'static str, + actions: &mut Vec, + ) { + let cur = comp + .node(node_id) + .static_str(input_name) + .unwrap_or(default) + .to_owned(); + let p = self.palette; + + ui.open( + ("row-e", node_id.0, input_name), + ui::Spec { + size: [ui::fill(), ui::px(22.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "lbl", + ui::Spec { + size: [ui::px(86.0), ui::fill()], + text: Some(label), + font_size: Some(10.5), + overflow: ui::Overflow::Ellipsis, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + let btn_lbl = format!("{cur} ▾"); + let sig = ui.leaf( + ("enum", node_id.0, input_name), + ui::Spec { + cursor: Some(winit::window::CursorIcon::Pointer), + flags: ui::Flags::CLICKABLE, + size: [ui::fill(), ui::px(20.0)], + text: Some(&btn_lbl), + font_size: Some(10.5), + color: Some(p.text), + fill: Some(p.panel_bg), + border: Some(p.rule), + hover_border: Some(p.accent), + radius: 4.0, + pad: [6.0, 0.0], + ..ui::Spec::default() + }, + ); + if sig.clicked && !options.is_empty() { + let idx = options.iter().position(|&o| o == cur).unwrap_or(0); + let next_idx = if ui.modifiers().shift_key() { + (idx + options.len() - 1) % options.len() + } else { + (idx + 1) % options.len() + }; + history.push_snapshot(comp, format!("Set {label}")); + comp.set_input_value(node_id, input_name, ParamValue::FuId(options[next_idx].to_owned())); + actions.push(InspectorAction::ParamEdited(node_id)); + } + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + fn render_color_group( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + label: &str, + r_key: &'static str, + g_key: &'static str, + b_key: &'static str, + a_key: Option<&'static str>, + default: [f64; 4], + current_frame: f64, + actions: &mut Vec, + ) { + let rv = comp.eval_number(comp.node(node_id), r_key, current_frame, default[0], &self.expr); + let gv = comp.eval_number(comp.node(node_id), g_key, current_frame, default[1], &self.expr); + let bv = comp.eval_number(comp.node(node_id), b_key, current_frame, default[2], &self.expr); + let _av = a_key + .map(|ak| comp.eval_number(comp.node(node_id), ak, current_frame, default[3], &self.expr)) + .unwrap_or(default[3]); + let swatch = [ + rv.clamp(0.0, 1.0) as f32, + gv.clamp(0.0, 1.0) as f32, + bv.clamp(0.0, 1.0) as f32, + 1.0, + ]; + let p = self.palette; + + ui.open( + ("grp-col", node_id.0, label), + ui::Spec { + size: [ui::fill(), ui::children()], + axis: ui::Axis::Y, + gap: 4.0, + pad: [6.0, 5.0], + fill: Some(p.panel_bg), + border: Some(p.rule), + radius: 4.0, + ..ui::Spec::default() + }, + ); + // Swatch header + ui.open( + "col-hdr", + ui::Spec { + size: [ui::fill(), ui::px(18.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "col-lbl", + ui::Spec { + size: [ui::px(80.0), ui::fill()], + text: Some(label), + font_size: Some(10.5), + medium: true, + color: Some(p.text), + ..ui::Spec::default() + }, + ); + ui.leaf( + "col-swatch", + ui::Spec { + size: [ui::fill(), ui::px(16.0)], + fill: Some(swatch), + border: Some(p.rule), + radius: 3.0, + ..ui::Spec::default() + }, + ); + ui.close(); + + self.render_float_row(ui, pane_id, comp, history, node_id, r_key, "Red", 0.0, 1.0, default[0], current_frame, actions); + self.render_float_row(ui, pane_id, comp, history, node_id, g_key, "Green", 0.0, 1.0, default[1], current_frame, actions); + self.render_float_row(ui, pane_id, comp, history, node_id, b_key, "Blue", 0.0, 1.0, default[2], current_frame, actions); + if let Some(ak) = a_key { + self.render_float_row(ui, pane_id, comp, history, node_id, ak, "Alpha", 0.0, 1.0, default[3], current_frame, actions); + } + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + fn render_text_row( + &mut self, + ui: &mut ui::Ui, + pane_id: ui::Id, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + input_name: &str, + label: &str, + default: &str, + actions: &mut Vec, + ) { + let field_id = pane_id.child(("txt", node_id.0, input_name)); + let key = (node_id.0, input_name.to_owned()); + let model_val = comp + .node(node_id) + .static_str(input_name) + .unwrap_or(default) + .to_owned(); + let p = self.palette; + let buf = self.text_bufs.entry(key).or_insert_with(|| model_val.clone()); + if ui.focused() != Some(field_id) && *buf != model_val { + *buf = model_val.clone(); + } + let prev = buf.clone(); + + ui.open( + ("row-t", node_id.0, input_name), + ui::Spec { + size: [ui::fill(), ui::px(24.0)], + axis: ui::Axis::X, + gap: 6.0, + ..ui::Spec::default() + }, + ); + ui.leaf( + "lbl", + ui::Spec { + size: [ui::px(86.0), ui::fill()], + text: Some(label), + font_size: Some(10.5), + overflow: ui::Overflow::Ellipsis, + color: Some(p.text_dim), + ..ui::Spec::default() + }, + ); + ui::text_field( + ui, + field_id, + buf, + label, + ui::Spec { + size: [ui::fill(), ui::px(22.0)], + font_size: Some(11.0), + fill: Some(p.panel_bg), + border: Some(p.rule), + hover_border: Some(p.accent), + radius: 4.0, + pad: [6.0, 2.0], + ..ui::Spec::default() + }, + ); + if *buf != prev { + history.push_coalesced( + comp, + format!("Edit {label}"), + format!("text:{}:{input_name}", node_id.0), + ); + comp.set_input_value(node_id, input_name, ParamValue::Text(buf.clone())); + actions.push(InspectorAction::ParamEdited(node_id)); + } + ui.close(); + } + + #[allow(clippy::too_many_arguments)] + fn scrub_slider( + &mut self, + ui: &mut ui::Ui, + slider_id: ui::Id, + drag_key: &str, + node_id: NodeId, + value: f64, + min: f64, + max: f64, + default: f64, + history: &mut CompHistory, + comp: &Comp, + input_name: &str, + ) -> Option { + let sig = ui.signal(slider_id); + let rect = ui.laid_out(slider_id); + let width = rect.map(|r| (r[2] - r[0]).max(40.0)).unwrap_or(110.0); + let mut updated: Option = None; + + if let Some(press) = sig.press { + let now = Instant::now(); + let is_double = self + .last_click + .get(drag_key) + .is_some_and(|prev| now.duration_since(*prev) < Duration::from_millis(320)); + self.last_click.insert(drag_key.to_owned(), now); + if is_double { + history.push_snapshot(comp, format!("Reset {input_name}")); + self.drag = None; + updated = Some(default); + } else { + self.drag = Some(DragState { + node_id, + key: drag_key.to_owned(), + start_x: press[0], + start_val: value, + min, + max, + width, + }); + } + } else if sig.dragging { + if let (Some(d), Some(ptr)) = (&self.drag, ui.pointer()) { + if d.key == drag_key && d.node_id == node_id { + let fine = if ui.modifiers().shift_key() { 0.15 } else { 1.0 }; + let next = compute_scrub_value(d.start_val, ptr[0] - d.start_x, d.width, d.min, d.max, fine); + if (next - value).abs() > 1e-6 { + history.push_coalesced( + comp, + format!("Adjust {input_name}"), + format!("scrub:{drag_key}"), + ); + updated = Some(next); + } + } + } + } else if self.drag.as_ref().is_some_and(|d| d.key == drag_key) { + self.drag = None; + history.break_coalescing(); + } + + let display_val = updated.unwrap_or(value); + let frac = normalize_slider_fraction(display_val, min, max); + let val_text = format_param_number(display_val); + let fill_w = (width * frac).clamp(0.0, width); + let active_drag = self.drag.as_ref().is_some_and(|d| d.key == drag_key); + let p = self.palette; + + ui.open_as( + slider_id, + ui::Spec { + cursor: Some(winit::window::CursorIcon::EwResize), + flags: ui::Flags::CLICKABLE | ui::Flags::CLIP, + size: [ui::fill(), ui::px(20.0)], + fill: Some(p.panel_bg), + border: Some(if active_drag || sig.hovered { p.accent } else { p.rule }), + radius: 4.0, + ..ui::Spec::default() + }, + ); + if fill_w > 0.5 { + ui.leaf( + "fill", + ui::Spec { + flags: ui::Flags::FLOAT | ui::Flags::INERT, + position: [0.0, 0.0], + size: [ui::px(fill_w), ui::px(20.0)], + fill: Some([p.accent[0], p.accent[1], p.accent[2], if active_drag { 0.34 } else { 0.22 }]), + radius: 3.0, + ..ui::Spec::default() + }, + ); + } + ui.leaf( + "txt", + ui::Spec { + flags: ui::Flags::INERT, + size: [ui::fill(), ui::fill()], + text: Some(&val_text), + font_size: Some(10.5), + color: Some(p.text), + center: true, + ..ui::Spec::default() + }, + ); + ui.close(); + + updated + } + + fn keyframe_button(&self, ui: &mut ui::Ui, part: impl std::hash::Hash, state: KeyframeState) -> bool { + let (ink, fill, border) = match state { + KeyframeState::KeyframeAtCurrent => ( + [1.0, 0.80, 0.22, 1.0], + Some([0.98, 0.72, 0.18, 0.24]), + Some([0.98, 0.76, 0.20, 0.85]), + ), + KeyframeState::AnimatedOtherFrame => ( + [0.92, 0.64, 0.24, 0.90], + Some([0.92, 0.64, 0.24, 0.12]), + Some([0.92, 0.64, 0.24, 0.45]), + ), + KeyframeState::Static => (self.palette.text_dim, None, None), + }; + let sig = ui.leaf( + part, + ui::Spec { + cursor: Some(winit::window::CursorIcon::Pointer), + flags: ui::Flags::CLICKABLE, + size: [ui::px(20.0), ui::px(20.0)], + text: Some("◆"), + font_size: Some(10.5), + color: Some(ink), + fill, + border, + hover_fill: Some([0.98, 0.76, 0.22, 0.18]), + radius: 4.0, + center: true, + ..ui::Spec::default() + }, + ); + sig.clicked + } + + #[allow(clippy::too_many_arguments)] + fn toggle_keyframe( + &self, + comp: &mut Comp, + history: &mut CompHistory, + node_id: NodeId, + input_name: &str, + current_frame: f64, + cur_val: f64, + state: KeyframeState, + actions: &mut Vec, + ) { + match state { + KeyframeState::KeyframeAtCurrent => { + if let Some(sp_name) = comp + .node(node_id) + .inputs + .get(input_name) + .and_then(|b| b.source_op.clone()) + { + history.push_snapshot(comp, format!("Delete Keyframe {input_name}")); + if comp.delete_keyframe(&sp_name, current_frame) { + actions.push(InspectorAction::KeyframeEdited(node_id)); + actions.push(InspectorAction::StatusNotice(format!( + "Deleted keyframe on {input_name} @ f{current_frame:.0}" + ))); + } + } + } + KeyframeState::Static | KeyframeState::AnimatedOtherFrame => { + history.push_snapshot(comp, format!("Set Keyframe {input_name}")); + match comp.set_keyframe(node_id, input_name, current_frame, cur_val) { + Ok(_) => { + actions.push(InspectorAction::KeyframeEdited(node_id)); + actions.push(InspectorAction::StatusNotice(format!( + "Keyframed {input_name} = {:.3} @ f{current_frame:.0}", + cur_val + ))); + } + Err(e) => actions.push(InspectorAction::StatusNotice(e)), + } + } + } + } +} + +fn render_expr_badge(ui: &mut ui::Ui) { + ui.leaf( + "expr-badge", + ui::Spec { + size: [ui::px(16.0), ui::px(18.0)], + text: Some("="), + font_size: Some(10.5), + bold: true, + color: Some([0.36, 0.88, 0.92, 1.0]), + fill: Some([0.10, 0.24, 0.28, 0.85]), + border: Some([0.32, 0.78, 0.86, 0.65]), + radius: 3.0, + center: true, + ..ui::Spec::default() + }, + ); +} + +/// Computes whether an input is static, animated on another frame, or has a keyframe at `frame`. +pub fn keyframe_state(comp: &Comp, node_id: NodeId, input_name: &str, frame: f64) -> KeyframeState { + let Some(sp_name) = comp + .node(node_id) + .inputs + .get(input_name) + .and_then(|b| b.source_op.as_deref()) + else { + return KeyframeState::Static; + }; + let Some(spline) = comp.splines.get(sp_name) else { + return KeyframeState::Static; + }; + if spline.keyframes.iter().any(|kf| (kf.time - frame).abs() < 1e-4) { + KeyframeState::KeyframeAtCurrent + } else if !spline.keyframes.is_empty() { + KeyframeState::AnimatedOtherFrame + } else { + KeyframeState::Static + } +} + +pub fn normalize_slider_fraction(value: f64, min: f64, max: f64) -> f32 { + let span = (max - min).abs().max(1e-6); + ((value - min) / span).clamp(0.0, 1.0) as f32 +} + +pub fn compute_scrub_value(start_val: f64, dx: f32, width: f32, min: f64, max: f64, fine_scale: f64) -> f64 { + let span = (max - min).max(1e-4); + let delta = (f64::from(dx) / f64::from(width.max(40.0))) * span * fine_scale; + (start_val + delta).clamp(min, max) +} + +pub fn format_param_number(v: f64) -> String { + if v.abs() >= 100.0 { + format!("{v:.1}") + } else if v.abs() >= 10.0 { + format!("{v:.2}") + } else { + format!("{v:.3}") + } +}