| 1 | //! Inputs the catalog cannot describe: a node's own `UserControls`, tools Fusion's introspection |
| 2 | //! skipped (curated schemas), and live inputs no row covers. |
| 3 | |
| 4 | use super::catalog::{Control, InputDef, control_for, finite_or, humanize, sanitize_allowed, sanitize_scale}; |
| 5 | use super::layout::{ToolLayout, build_layout}; |
| 6 | use super::schemas::{ParamKind, infer_float_range, schema_for_kind}; |
| 7 | use model::{Comp, LuaKey, LuaTable, NodeId, ParamValue}; |
| 8 | use std::collections::HashSet; |
| 9 | |
| 10 | /// Rows for each `UserControls` entry of a tool/macro/group table, grouped into pages by |
| 11 | /// `ICS_ControlPage`. |
| 12 | pub fn user_defs(raw: &LuaTable) -> Vec<InputDef> { |
| 13 | let Some(controls) = raw.get_table("UserControls") else { return Vec::new() }; |
| 14 | controls |
| 15 | .entries |
| 16 | .iter() |
| 17 | .filter_map(|(key, value)| { |
| 18 | let LuaKey::Str(id) = key else { return None }; |
| 19 | user_def(id, value.as_table()?) |
| 20 | }) |
| 21 | .collect() |
| 22 | } |
| 23 | |
| 24 | fn user_def(id: &str, t: &LuaTable) -> Option<InputDef> { |
| 25 | let data_type = t.get_str("LINKID_DataType").unwrap_or("Number"); |
| 26 | let entries: Vec<String> = t |
| 27 | .positional_values() |
| 28 | .filter_map(|v| v.as_table()) |
| 29 | .filter_map(|e| e.get_str("CCS_AddString").or(e.get_str("CCID_AddID")).or(e.get_str("MBTNC_AddButton"))) |
| 30 | .map(str::to_owned) |
| 31 | .collect(); |
| 32 | let control_name = t.get_str("INPID_InputControl").unwrap_or(match data_type { |
| 33 | "Point" => "OffsetControl", |
| 34 | "Text" | "FuID" => "TextEditControl", |
| 35 | _ => "SliderControl", |
| 36 | }); |
| 37 | let control = control_for(control_name, data_type, !entries.is_empty())?; |
| 38 | let scale = sanitize_scale(t.get_f64("INP_MinScale"), t.get_f64("INP_MaxScale")); |
| 39 | let allowed = sanitize_allowed(t.get_f64("INP_MinAllowed"), t.get_f64("INP_MaxAllowed")); |
| 40 | let name = t.get_str("LINKS_Name").map_or_else(|| humanize(id), str::to_owned); |
| 41 | Some(InputDef { |
| 42 | fuid: matches!(data_type, "FuID" | "Text"), |
| 43 | default: if data_type == "Point" { |
| 44 | [finite_or(t.get_f64("INP_DefaultX"), 0.5), finite_or(t.get_f64("INP_DefaultY"), 0.5)] |
| 45 | } else { |
| 46 | [finite_or(t.get_f64("INP_Default"), 0.0), 0.0] |
| 47 | }, |
| 48 | default_text: t.get_str("INP_Default").unwrap_or_default().to_owned(), |
| 49 | scale, |
| 50 | allowed, |
| 51 | integer: t.get_bool("INP_Integer").unwrap_or(false), |
| 52 | page: Some(t.get_str("ICS_ControlPage").unwrap_or("User").to_owned()), |
| 53 | entries, |
| 54 | group: t.get_f64("IC_ControlGroup").filter(|g| g.is_finite()).map(|g| { |
| 55 | let cid = t.get_f64("IC_ControlID").filter(|v| v.is_finite() && *v >= 0.0).unwrap_or(0.0); |
| 56 | (g as i64, cid.min(f64::from(u32::MAX)) as u32) |
| 57 | }), |
| 58 | ..InputDef::new(id, &name, control) |
| 59 | }) |
| 60 | } |
| 61 | |
| 62 | /// Rows for live inputs of `node` that `covered` rejects, typed by their stored value. |
| 63 | pub fn extra_defs(comp: &Comp, node: NodeId, covered: impl Fn(&str) -> bool) -> Vec<InputDef> { |
| 64 | let mut keys: Vec<&String> = comp |
| 65 | .node(node) |
| 66 | .inputs |
| 67 | .iter() |
| 68 | .filter(|(key, b)| { |
| 69 | let wired = b.source_op.as_deref().is_some_and(|op| comp.node_by_name.contains_key(op)); |
| 70 | !covered(key) |
| 71 | && !(wired && b.value.is_none()) |
| 72 | && (b.value.is_some() || b.expression.is_some() || b.source_op.is_some()) |
| 73 | }) |
| 74 | .map(|(key, _)| key) |
| 75 | .collect(); |
| 76 | keys.sort(); |
| 77 | keys.into_iter() |
| 78 | .map(|key| { |
| 79 | let value = comp.node(node).inputs[key].value.as_ref(); |
| 80 | let (control, fuid) = match value { |
| 81 | Some(ParamValue::Point(_)) => (Control::Offset, false), |
| 82 | Some(ParamValue::Text(_)) => (Control::Text, false), |
| 83 | Some(ParamValue::FuId(_)) => (Control::Text, true), |
| 84 | _ => (Control::Slider, false), |
| 85 | }; |
| 86 | let number = value.and_then(ParamValue::as_f64).unwrap_or(0.0); |
| 87 | InputDef { |
| 88 | fuid, |
| 89 | default: value.and_then(ParamValue::as_point).unwrap_or([number, 0.0]), |
| 90 | default_text: value.and_then(ParamValue::as_str).unwrap_or_default().to_owned(), |
| 91 | scale: if control == Control::Slider { infer_float_range(key, number).into() } else { [0.0, 1.0] }, |
| 92 | page: Some("Other".to_owned()), |
| 93 | ..InputDef::new(key, key, control) |
| 94 | } |
| 95 | }) |
| 96 | .collect() |
| 97 | } |
| 98 | |
| 99 | /// A layout from the curated schema, for tools the Fusion catalog skipped. |
| 100 | pub fn curated_layout(kind: &str) -> Option<ToolLayout> { |
| 101 | let schema = schema_for_kind(kind); |
| 102 | if schema.is_empty() { |
| 103 | return None; |
| 104 | } |
| 105 | let mut defs = Vec::new(); |
| 106 | let mut covered = HashSet::new(); |
| 107 | for spec in schema { |
| 108 | let base = |control| InputDef { page: Some("Controls".to_owned()), ..InputDef::new(spec.name, spec.label, control) }; |
| 109 | match spec.kind { |
| 110 | ParamKind::Float { min, max, default, .. } => { |
| 111 | defs.push(InputDef { default: [default, 0.0], scale: [min, max], ..base(Control::Slider) }); |
| 112 | } |
| 113 | ParamKind::Vec2 { default, .. } => defs.push(InputDef { default, ..base(Control::Offset) }), |
| 114 | ParamKind::Bool { default } => { |
| 115 | defs.push(InputDef { default: [f64::from(u8::from(default)), 0.0], integer: true, ..base(Control::Checkbox) }); |
| 116 | } |
| 117 | ParamKind::Enum { options, default } => defs.push(InputDef { |
| 118 | fuid: true, |
| 119 | default_text: default.to_owned(), |
| 120 | entries: options.iter().map(|o| (*o).to_owned()).collect(), |
| 121 | ..base(Control::Combo) |
| 122 | }), |
| 123 | ParamKind::IntEnum { options, default } => { |
| 124 | let sequential = options.iter().enumerate().all(|(i, (code, _))| *code == i as i32); |
| 125 | defs.push(if sequential { |
| 126 | InputDef { |
| 127 | default: [f64::from(default), 0.0], |
| 128 | entries: options.iter().map(|(_, label)| (*label).to_owned()).collect(), |
| 129 | ..base(Control::Combo) |
| 130 | } |
| 131 | } else { |
| 132 | let codes = options.iter().map(|(code, _)| f64::from(*code)); |
| 133 | let hi = codes.clone().fold(f64::MIN, f64::max); |
| 134 | InputDef { default: [f64::from(default), 0.0], scale: [codes.fold(f64::MAX, f64::min), hi], integer: true, ..base(Control::Slider) } |
| 135 | }); |
| 136 | } |
| 137 | ParamKind::Color { r, g, b, a, default } => { |
| 138 | for (n, id) in [Some(r), Some(g), Some(b), a].into_iter().enumerate() { |
| 139 | let Some(id) = id else { continue }; |
| 140 | covered.insert(id.to_owned()); |
| 141 | let name = if n == 0 { format!("{} Red", spec.label) } else { id.to_owned() }; |
| 142 | defs.push(InputDef { |
| 143 | default: [default[n], 0.0], |
| 144 | group: Some((0, n as u32)), |
| 145 | ..InputDef { page: Some("Controls".to_owned()), ..InputDef::new(id, &name, Control::Color) } |
| 146 | }); |
| 147 | } |
| 148 | continue; |
| 149 | } |
| 150 | ParamKind::Text { default } => { |
| 151 | defs.push(InputDef { fuid: false, default_text: default.to_owned(), ..base(Control::Text) }); |
| 152 | } |
| 153 | } |
| 154 | covered.insert(spec.name.to_owned()); |
| 155 | } |
| 156 | Some(build_layout(kind, defs, covered)) |
| 157 | } |