| 1 | use super::{ |
| 2 | BitDepth, ClipSpec, ClsEntry, Comp, ExpressionModifier, InputBinding, NEXT_COMP_REVISION, |
| 3 | Node, NodeId, ParamValue, REVISION_STRIDE, ShakeModifier, is_modifier_type, |
| 4 | }; |
| 5 | use crate::lua_value::{LuaKey, LuaTable, LuaValue, ParseError, parse_lua}; |
| 6 | use crate::spline::{BezierSpline, Gradient, PolyPath, read_vec2}; |
| 7 | use std::collections::{HashMap, HashSet}; |
| 8 | use std::path::Path; |
| 9 | use std::sync::atomic::Ordering; |
| 10 | |
| 11 | impl Comp { |
| 12 | pub fn from_fusion_file(path: impl AsRef<Path>) -> Result<Self, Box<dyn std::error::Error>> { |
| 13 | let path = path.as_ref(); |
| 14 | let bytes = std::fs::read(path)?; |
| 15 | let text = String::from_utf8_lossy(&bytes); |
| 16 | let mut comp = Self::from_fusion_str(&text)?; |
| 17 | comp.path = Some(path.to_path_buf()); |
| 18 | if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) { |
| 19 | comp.name = stem.to_owned(); |
| 20 | } |
| 21 | Ok(comp) |
| 22 | } |
| 23 | |
| 24 | pub fn from_fusion_str(text: &str) -> Result<Self, ParseError> { |
| 25 | let LuaValue::Table(root) = parse_lua(text)? else { |
| 26 | return Err(ParseError { |
| 27 | offset: 0, |
| 28 | line: 1, |
| 29 | message: "comp root is not a table".into(), |
| 30 | }); |
| 31 | }; |
| 32 | |
| 33 | let current_time = root.get_f64("CurrentTime").filter(|t| t.is_finite()).unwrap_or(0.0); |
| 34 | let global_range = root |
| 35 | .get_table("GlobalRange") |
| 36 | .and_then(read_vec2) |
| 37 | .and_then(normalize_frame_range) |
| 38 | .unwrap_or([0, 1000]); |
| 39 | let render_range = root |
| 40 | .get_table("RenderRange") |
| 41 | .and_then(read_vec2) |
| 42 | .and_then(normalize_frame_range) |
| 43 | .unwrap_or(global_range); |
| 44 | let audio_filename = root.get_str("AudioFilename").map(str::to_owned); |
| 45 | let audio_offset = root.get_f64("AudioOffset").filter(|t| t.is_finite()).unwrap_or(0.0); |
| 46 | |
| 47 | // Extract FrameFormat from Prefs.Comp.FrameFormat (default 1920x1080 @ 30fps, Int8) |
| 48 | let mut width = 1920u32; |
| 49 | let mut height = 1080u32; |
| 50 | let mut fps = 30.0_f64; |
| 51 | let mut depth_full = BitDepth::Int8; |
| 52 | let mut explicit_size = false; |
| 53 | if let Some(ff) = root |
| 54 | .get_table("Prefs") |
| 55 | .and_then(|p| p.get_table("Comp")) |
| 56 | .and_then(|c| c.get_table("FrameFormat")) |
| 57 | { |
| 58 | if let Some(w) = ff.get_f64("Width").and_then(normalize_frame_dim) { |
| 59 | width = w; |
| 60 | explicit_size = true; |
| 61 | } |
| 62 | if let Some(h) = ff.get_f64("Height").and_then(normalize_frame_dim) { |
| 63 | height = h; |
| 64 | explicit_size = true; |
| 65 | } |
| 66 | if let Some(r) = ff.get_f64("Rate").filter(|r| r.is_finite() && *r > 0.0) { |
| 67 | fps = r; |
| 68 | } |
| 69 | if let Some(d) = ff |
| 70 | .get_f64("DepthFull") |
| 71 | .filter(|v| v.is_finite()) |
| 72 | .and_then(|v| BitDepth::from_fusion_depth(v.round() as i32)) |
| 73 | { |
| 74 | depth_full = d; |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | let tools_len = root.get_table("Tools").map_or(0, |t| t.entries.len()); |
| 79 | let approx_nodes = tools_len / 2; |
| 80 | let mut nodes = Vec::with_capacity(approx_nodes); |
| 81 | let mut node_by_name = HashMap::with_capacity(approx_nodes); |
| 82 | let mut splines = HashMap::with_capacity(tools_len - approx_nodes); |
| 83 | let mut poly_paths = HashMap::new(); |
| 84 | let mut gradient_modifiers = HashMap::new(); |
| 85 | let mut text_modifiers = HashMap::new(); |
| 86 | let mut text_splines = HashMap::new(); |
| 87 | let mut polyline_splines = HashMap::new(); |
| 88 | let mut text_cls = HashMap::new(); |
| 89 | let mut shakes = HashMap::new(); |
| 90 | let mut expression_modifiers = HashMap::new(); |
| 91 | let mut tracker_aliases: Vec<(String, String)> = Vec::new(); |
| 92 | let mut instanced_from: Vec<(NodeId, String)> = Vec::new(); |
| 93 | let mut macro_owner: HashMap<NodeId, NodeId> = HashMap::new(); |
| 94 | |
| 95 | if let Some(tools) = root.get_table("Tools") { |
| 96 | parse_tools_table( |
| 97 | tools, |
| 98 | false, |
| 99 | &mut nodes, |
| 100 | &mut node_by_name, |
| 101 | &mut splines, |
| 102 | &mut poly_paths, |
| 103 | &mut gradient_modifiers, |
| 104 | &mut text_modifiers, |
| 105 | &mut text_splines, |
| 106 | &mut polyline_splines, |
| 107 | &mut text_cls, |
| 108 | &mut shakes, |
| 109 | &mut expression_modifiers, |
| 110 | &mut tracker_aliases, |
| 111 | &mut instanced_from, |
| 112 | &mut macro_owner, |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | // Resolve TrackerModifier aliases (e.g. `Track1` -> `Track1TrackerPath`) into `poly_paths` |
| 117 | for (tracker_name, src_path) in tracker_aliases { |
| 118 | if let Some(pp) = poly_paths.get(&src_path).cloned() { |
| 119 | poly_paths.insert(tracker_name, pp); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | let cross_macro_wires = clear_cross_macro_wires(&mut nodes, &macro_owner); |
| 124 | |
| 125 | // For MacroOperator/GroupOperator Flow display: wire external inputs from inner InstanceInput targets |
| 126 | // so Flow shows `Transform1 -> Invert -> Merge2` cleanly while `engine` follows `macro_output`. |
| 127 | if nodes.iter().any(|n| matches!(n.kind.as_str(), "MacroOperator" | "GroupOperator")) { |
| 128 | let mut name_to_idx: HashMap<&str, usize> = HashMap::with_capacity(nodes.len()); |
| 129 | for (idx, n) in nodes.iter().enumerate() { |
| 130 | name_to_idx.insert(n.name.as_str(), idx); |
| 131 | } |
| 132 | let mut group_wires: Vec<(usize, Vec<(String, InputBinding)>)> = Vec::new(); |
| 133 | for (idx, n) in nodes.iter().enumerate() { |
| 134 | if !matches!(n.kind.as_str(), "MacroOperator" | "GroupOperator") { |
| 135 | continue; |
| 136 | } |
| 137 | let mut flow_wires = Vec::new(); |
| 138 | for (in_name, b) in &n.inputs { |
| 139 | if let (Some(inner_op), Some(inner_port)) = (&b.source_op, &b.source_output) { |
| 140 | if let Some(&inner_idx) = name_to_idx.get(inner_op.as_str()) { |
| 141 | if let Some(inner_in) = nodes[inner_idx].inputs.get(inner_port) { |
| 142 | if let Some(ext_src) = &inner_in.source_op { |
| 143 | // Only connect if ext_src is a visible (non-internal) flow node |
| 144 | if node_by_name.get(ext_src).is_some_and(|&id| id != n.id) { |
| 145 | flow_wires.push(( |
| 146 | in_name.clone(), |
| 147 | InputBinding { |
| 148 | value: None, |
| 149 | source_op: Some(ext_src.clone()), |
| 150 | source_output: inner_in.source_output.clone(), |
| 151 | expression: None, |
| 152 | }, |
| 153 | )); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | if !flow_wires.is_empty() { |
| 161 | group_wires.push((idx, flow_wires)); |
| 162 | } |
| 163 | } |
| 164 | for (idx, wires) in group_wires { |
| 165 | nodes[idx].inputs.extend(wires); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | // Resolve Fusion tool-level `SourceOp` instanced node inheritance |
| 170 | // (e.g. `Instance_ColorCorrector1_6 = ColorCorrector { SourceOp = "ColorCorrector1_6" }`), |
| 171 | // walking chains up to depth 8 (cycle-safe) while NEVER copying image/mask/scene wires. |
| 172 | if !instanced_from.is_empty() { |
| 173 | let parent_of: HashMap<NodeId, NodeId> = instanced_from |
| 174 | .iter() |
| 175 | .filter_map(|(nid, master_name)| { |
| 176 | node_by_name |
| 177 | .get(master_name) |
| 178 | .copied() |
| 179 | .filter(|&mid| mid != *nid) |
| 180 | .map(|mid| (*nid, mid)) |
| 181 | }) |
| 182 | .collect(); |
| 183 | let mut orig_inputs: HashMap<NodeId, HashMap<String, InputBinding>> = |
| 184 | HashMap::with_capacity(parent_of.len()); |
| 185 | for &pid in parent_of.values() { |
| 186 | orig_inputs |
| 187 | .entry(pid) |
| 188 | .or_insert_with(|| nodes[pid.0].inputs.clone()); |
| 189 | } |
| 190 | |
| 191 | let mut visited = HashSet::new(); |
| 192 | let mut blocked: HashSet<String> = HashSet::new(); |
| 193 | for &(nid, _) in &instanced_from { |
| 194 | visited.clear(); |
| 195 | visited.insert(nid); |
| 196 | blocked.clear(); |
| 197 | let mut cur = nid; |
| 198 | for _depth in 0..8 { |
| 199 | let Some(&pid) = parent_of.get(&cur) else { |
| 200 | break; |
| 201 | }; |
| 202 | if !visited.insert(pid) { |
| 203 | break; |
| 204 | } |
| 205 | let inst_inputs = &mut nodes[nid.0].inputs; |
| 206 | if let Some(parent_map) = orig_inputs.get(&pid) { |
| 207 | for (inp_name, parent_binding) in parent_map { |
| 208 | if is_non_inheritable_instance_port(inp_name, parent_binding, &node_by_name) { |
| 209 | continue; |
| 210 | } |
| 211 | if blocked.contains(inp_name) { |
| 212 | continue; |
| 213 | } |
| 214 | if parent_binding.value.is_none() |
| 215 | && parent_binding.source_op.is_none() |
| 216 | && parent_binding.expression.is_none() |
| 217 | { |
| 218 | // An empty `Param = Input {}` on an ancestor marks a de-instanced |
| 219 | // override to default and stops further inheritance up the chain. |
| 220 | blocked.insert(inp_name.clone()); |
| 221 | continue; |
| 222 | } |
| 223 | if !inst_inputs.contains_key(inp_name) { |
| 224 | inst_inputs.insert(inp_name.clone(), parent_binding.clone()); |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | cur = pid; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | // Apply cyclic [0..6] endpoint continuity on LUTBezier splines attached to HueCurves nodes. |
| 234 | const HUE_CURVE_PORTS: &[&str] = &[ |
| 235 | "Hue", |
| 236 | "Sat", |
| 237 | "Lum", |
| 238 | "Red", |
| 239 | "Green", |
| 240 | "Blue", |
| 241 | "RedSuppress", |
| 242 | "GreenSuppress", |
| 243 | "BlueSuppress", |
| 244 | ]; |
| 245 | if !splines.is_empty() && nodes.iter().any(|n| n.kind == "HueCurves") { |
| 246 | let mut hue_spline_names: HashSet<String> = HashSet::new(); |
| 247 | for n in &nodes { |
| 248 | if n.kind == "HueCurves" { |
| 249 | for &port in HUE_CURVE_PORTS { |
| 250 | if let Some(sp_name) = n.connected_node(port) { |
| 251 | hue_spline_names.insert(sp_name.to_owned()); |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | for sp_name in hue_spline_names { |
| 257 | if let Some(sp) = splines.get_mut(&sp_name) { |
| 258 | sp.apply_hue_curves_cyclic_ends(); |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Infer comp dimensions from first Background/TextPlus if Prefs omitted Width/Height |
| 264 | if !explicit_size { |
| 265 | for n in &nodes { |
| 266 | if matches!(n.kind.as_str(), "Background" | "TextPlus" | "FastNoise") { |
| 267 | if let (Some(w), Some(h)) = (n.static_number("Width"), n.static_number("Height")) { |
| 268 | if w.is_finite() && h.is_finite() && w >= 64.0 && h >= 64.0 { |
| 269 | width = normalize_frame_dim(w).unwrap_or(width); |
| 270 | height = normalize_frame_dim(h).unwrap_or(height); |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | Ok(Self { |
| 279 | path: None, |
| 280 | name: "Untitled".to_owned(), |
| 281 | width, |
| 282 | height, |
| 283 | fps, |
| 284 | depth_full, |
| 285 | current_time, |
| 286 | global_range, |
| 287 | render_range, |
| 288 | audio_filename, |
| 289 | audio_offset, |
| 290 | nodes, |
| 291 | node_by_name, |
| 292 | splines, |
| 293 | poly_paths, |
| 294 | gradient_modifiers, |
| 295 | text_modifiers, |
| 296 | text_splines, |
| 297 | polyline_splines, |
| 298 | text_cls, |
| 299 | shakes, |
| 300 | expression_modifiers, |
| 301 | cross_macro_wires, |
| 302 | raw_root: Some(root), |
| 303 | revision: NEXT_COMP_REVISION.fetch_add(REVISION_STRIDE, Ordering::Relaxed), |
| 304 | }) |
| 305 | } |
| 306 | |
| 307 | } |
| 308 | |
| 309 | pub(super) fn normalize_frame_dim(v: f64) -> Option<u32> { |
| 310 | v.is_finite() |
| 311 | .then(|| v.round().clamp(1.0, 65536.0) as u32) |
| 312 | } |
| 313 | |
| 314 | fn normalize_frame_range([a, b]: [f64; 2]) -> Option<[i64; 2]> { |
| 315 | (a.is_finite() && b.is_finite()).then(|| { |
| 316 | let lo = i32::MIN as f64; |
| 317 | let hi = i32::MAX as f64; |
| 318 | [a.round().clamp(lo, hi) as i64, b.round().clamp(lo, hi) as i64] |
| 319 | }) |
| 320 | } |
| 321 | |
| 322 | /// Fusion resolves a macro-internal `SourceOp` only within the macro's own scope (itself, its |
| 323 | /// enclosing macros, its nested macros) or at top level; a name that only exists inside an |
| 324 | /// unrelated macro stays unconnected. Returns the `(node, port)` wires whose `source_op` was |
| 325 | /// cleared here so saving keeps the original text. |
| 326 | fn clear_cross_macro_wires(nodes: &mut [Node], owner: &HashMap<NodeId, NodeId>) -> HashSet<(String, String)> { |
| 327 | if owner.is_empty() { |
| 328 | return HashSet::new(); |
| 329 | } |
| 330 | enum NameMatches { |
| 331 | One(NodeId), |
| 332 | Many(Vec<NodeId>), |
| 333 | } |
| 334 | let mut by_name: HashMap<&str, NameMatches> = HashMap::with_capacity(nodes.len()); |
| 335 | for n in nodes.iter() { |
| 336 | match by_name.get_mut(n.name.as_str()) { |
| 337 | None => { |
| 338 | by_name.insert(n.name.as_str(), NameMatches::One(n.id)); |
| 339 | } |
| 340 | Some(NameMatches::One(first)) => { |
| 341 | let first = *first; |
| 342 | by_name.insert(n.name.as_str(), NameMatches::Many(vec![first, n.id])); |
| 343 | } |
| 344 | Some(NameMatches::Many(v)) => v.push(n.id), |
| 345 | } |
| 346 | } |
| 347 | let encloses = |outer: NodeId, mut inner: NodeId| { |
| 348 | loop { |
| 349 | if inner == outer { |
| 350 | return true; |
| 351 | } |
| 352 | match owner.get(&inner) { |
| 353 | Some(&up) => inner = up, |
| 354 | None => return false, |
| 355 | } |
| 356 | } |
| 357 | }; |
| 358 | let in_scope = |n_scope: NodeId, t: NodeId| -> bool { |
| 359 | if !nodes[t.0].internal { |
| 360 | return true; |
| 361 | } |
| 362 | owner |
| 363 | .get(&t) |
| 364 | .is_some_and(|&t_scope| encloses(t_scope, n_scope) || encloses(n_scope, t_scope)) |
| 365 | }; |
| 366 | let mut cleared = Vec::new(); |
| 367 | for n in nodes.iter().filter(|n| n.internal) { |
| 368 | let Some(&n_scope) = owner.get(&n.id) else { continue }; |
| 369 | for (port, b) in &n.inputs { |
| 370 | let Some(candidates) = b.source_op.as_deref().and_then(|s| by_name.get(s)) else { |
| 371 | continue; |
| 372 | }; |
| 373 | let any_in_scope = match candidates { |
| 374 | NameMatches::One(t) => in_scope(n_scope, *t), |
| 375 | NameMatches::Many(vec) => vec.iter().any(|&t| in_scope(n_scope, t)), |
| 376 | }; |
| 377 | if !any_in_scope { |
| 378 | cleared.push((n.id, port.clone())); |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | cleared |
| 383 | .into_iter() |
| 384 | .map(|(id, port)| { |
| 385 | nodes[id.0].inputs.get_mut(&port).expect("collected from this map").source_op = None; |
| 386 | (nodes[id.0].name.clone(), port) |
| 387 | }) |
| 388 | .collect() |
| 389 | } |
| 390 | |
| 391 | fn is_non_inheritable_instance_port( |
| 392 | name: &str, |
| 393 | binding: &InputBinding, |
| 394 | node_by_name: &HashMap<String, NodeId>, |
| 395 | ) -> bool { |
| 396 | if matches!( |
| 397 | name, |
| 398 | "Input" |
| 399 | | "Background" |
| 400 | | "Foreground" |
| 401 | | "EffectMask" |
| 402 | | "MaterialInput" |
| 403 | | "GarbageMatte" |
| 404 | | "SolidMatte" |
| 405 | | "Mask" |
| 406 | | "TrackingMask" |
| 407 | | "RegionMask" |
| 408 | | "MatchReference" |
| 409 | | "MatchMask" |
| 410 | | "Image" |
| 411 | | "Image1" |
| 412 | | "Image2" |
| 413 | | "BlurImage" |
| 414 | | "DisplaceImage" |
| 415 | ) || name.starts_with("SceneInput") |
| 416 | || name.starts_with("MainInput") |
| 417 | { |
| 418 | return true; |
| 419 | } |
| 420 | // Never inherit flow-graph image/scene/material connections targeting another flow node; |
| 421 | // modifiers (`BezierSpline`, `PolyPath`, `Shake`, `Expression`, etc.) are not in `node_by_name`. |
| 422 | binding.value.is_none() |
| 423 | && binding.expression.is_none() |
| 424 | && binding |
| 425 | .source_op |
| 426 | .as_deref() |
| 427 | .is_some_and(|op| node_by_name.contains_key(op)) |
| 428 | } |
| 429 | |
| 430 | pub(super) fn parse_input_binding(t: &LuaTable) -> InputBinding { |
| 431 | let mut source_op = None; |
| 432 | let mut source_output = None; |
| 433 | let mut expression = None; |
| 434 | let mut value = None; |
| 435 | for (k, v) in &t.entries { |
| 436 | let LuaKey::Str(s) = k else { continue }; |
| 437 | match s.as_str() { |
| 438 | "SourceOp" => { |
| 439 | if let Some(sv) = v.as_str() { |
| 440 | source_op = Some(sv.to_owned()); |
| 441 | } |
| 442 | } |
| 443 | "Source" => { |
| 444 | if let Some(sv) = v.as_str() { |
| 445 | source_output = Some(sv.to_owned()); |
| 446 | } |
| 447 | } |
| 448 | "Expression" => { |
| 449 | if let Some(sv) = v.as_str() { |
| 450 | expression = Some(sv.to_owned()); |
| 451 | } |
| 452 | } |
| 453 | "Value" => { |
| 454 | value = Some(parse_param_value(v)); |
| 455 | } |
| 456 | _ => {} |
| 457 | } |
| 458 | } |
| 459 | InputBinding { |
| 460 | value, |
| 461 | source_op, |
| 462 | source_output, |
| 463 | expression, |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | pub(super) fn parse_param_value(v: &LuaValue) -> ParamValue { |
| 468 | match v { |
| 469 | LuaValue::Number(n) => ParamValue::Number(*n), |
| 470 | LuaValue::Bool(b) => ParamValue::Number(if *b { 1.0 } else { 0.0 }), |
| 471 | LuaValue::String(s) => ParamValue::Text(s.clone()), |
| 472 | LuaValue::Table(t) => match t.tag.as_deref() { |
| 473 | Some("FuID") => ParamValue::FuId( |
| 474 | t.get_index(1) |
| 475 | .and_then(LuaValue::as_str) |
| 476 | .unwrap_or("") |
| 477 | .to_owned(), |
| 478 | ), |
| 479 | Some("Number") => ParamValue::Number(t.get_f64("Value").unwrap_or(0.0)), |
| 480 | Some("Gradient") => ParamValue::Gradient(Gradient::from_lua(t)), |
| 481 | _ => { |
| 482 | if let Some(p) = read_vec2(t) { |
| 483 | ParamValue::Point(p) |
| 484 | } else { |
| 485 | ParamValue::Raw(v.clone()) |
| 486 | } |
| 487 | } |
| 488 | }, |
| 489 | LuaValue::Nil => ParamValue::Raw(LuaValue::Nil), |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | fn try_parse_gradient_keyframes(kfs: &LuaTable) -> Option<Vec<(f64, Gradient)>> { |
| 494 | let mut grads = Vec::with_capacity(kfs.entries.len()); |
| 495 | for (k, v) in &kfs.entries { |
| 496 | let time = match k { |
| 497 | LuaKey::Index(i) => *i as f64, |
| 498 | LuaKey::Number(n) => *n, |
| 499 | _ => continue, |
| 500 | }; |
| 501 | let kt = v.as_table()?; |
| 502 | let val = kt |
| 503 | .get_table("Value") |
| 504 | .or_else(|| kt.get_index(1).and_then(LuaValue::as_table))?; |
| 505 | if val.tag.as_deref() == Some("Gradient") { |
| 506 | grads.push((time, Gradient::from_lua(val))); |
| 507 | } else { |
| 508 | return None; |
| 509 | } |
| 510 | } |
| 511 | if grads.is_empty() { |
| 512 | return None; |
| 513 | } |
| 514 | grads.sort_by(|a, b| a.0.total_cmp(&b.0)); |
| 515 | Some(grads) |
| 516 | } |
| 517 | |
| 518 | fn try_parse_text_keyframes(kfs: &LuaTable) -> Option<Vec<(f64, String)>> { |
| 519 | let mut texts = Vec::with_capacity(kfs.entries.len()); |
| 520 | for (k, v) in &kfs.entries { |
| 521 | let time = match k { |
| 522 | LuaKey::Index(i) => *i as f64, |
| 523 | LuaKey::Number(n) => *n, |
| 524 | _ => continue, |
| 525 | }; |
| 526 | let kt = v.as_table()?; |
| 527 | let val = kt.get_table("Value")?; |
| 528 | if matches!(val.tag.as_deref(), Some("Text" | "StyledText")) { |
| 529 | let s = val.get_str("Value").unwrap_or("").to_owned(); |
| 530 | texts.push((time, s)); |
| 531 | } else { |
| 532 | return None; |
| 533 | } |
| 534 | } |
| 535 | if texts.is_empty() { |
| 536 | return None; |
| 537 | } |
| 538 | texts.sort_by(|a, b| a.0.total_cmp(&b.0)); |
| 539 | Some(texts) |
| 540 | } |
| 541 | |
| 542 | fn try_parse_polyline_keyframes(kfs: &LuaTable) -> Option<Vec<(f64, Vec<crate::spline::PolyPoint>)>> { |
| 543 | let mut shapes = Vec::with_capacity(kfs.entries.len()); |
| 544 | for (k, v) in &kfs.entries { |
| 545 | let time = match k { |
| 546 | LuaKey::Index(i) => *i as f64, |
| 547 | LuaKey::Number(n) => *n, |
| 548 | _ => continue, |
| 549 | }; |
| 550 | let kt = v.as_table()?; |
| 551 | let val = kt.get_table("Value")?; |
| 552 | if val.tag.as_deref() == Some("Polyline") { |
| 553 | let (_, pts) = PolyPath::parse_polyline_table(val); |
| 554 | shapes.push((time, pts)); |
| 555 | } else { |
| 556 | return None; |
| 557 | } |
| 558 | } |
| 559 | if shapes.is_empty() { |
| 560 | return None; |
| 561 | } |
| 562 | shapes.sort_by(|a, b| a.0.total_cmp(&b.0)); |
| 563 | Some(shapes) |
| 564 | } |
| 565 | |
| 566 | #[allow(clippy::too_many_arguments)] |
| 567 | fn parse_tools_table( |
| 568 | tools: &LuaTable, |
| 569 | is_internal: bool, |
| 570 | nodes: &mut Vec<Node>, |
| 571 | node_by_name: &mut HashMap<String, NodeId>, |
| 572 | splines: &mut HashMap<String, BezierSpline>, |
| 573 | poly_paths: &mut HashMap<String, PolyPath>, |
| 574 | gradient_modifiers: &mut HashMap<String, Vec<(f64, Gradient)>>, |
| 575 | text_modifiers: &mut HashMap<String, String>, |
| 576 | text_splines: &mut HashMap<String, Vec<(f64, String)>>, |
| 577 | polyline_splines: &mut HashMap<String, (BezierSpline, Vec<(f64, Vec<crate::spline::PolyPoint>)>)>, |
| 578 | text_cls: &mut HashMap<String, Vec<ClsEntry>>, |
| 579 | shakes: &mut HashMap<String, ShakeModifier>, |
| 580 | expression_modifiers: &mut HashMap<String, ExpressionModifier>, |
| 581 | tracker_aliases: &mut Vec<(String, String)>, |
| 582 | instanced_from: &mut Vec<(NodeId, String)>, |
| 583 | macro_owner: &mut HashMap<NodeId, NodeId>, |
| 584 | ) { |
| 585 | for (key, val) in &tools.entries { |
| 586 | let LuaKey::Str(name) = key else { continue }; |
| 587 | let Some(tbl) = val.as_table() else { continue }; |
| 588 | let kind_ref = tbl.tag.as_deref().unwrap_or("Unknown"); |
| 589 | |
| 590 | if kind_ref == "BezierSpline" || kind_ref == "LUTBezier" { |
| 591 | if let Some(kfs) = tbl.get_table("KeyFrames") { |
| 592 | let first_val_tag = kfs.entries.iter().find_map(|(k, v)| { |
| 593 | if !matches!(k, LuaKey::Index(_) | LuaKey::Number(_)) { |
| 594 | return None; |
| 595 | } |
| 596 | let kt = v.as_table()?; |
| 597 | kt.get_table("Value") |
| 598 | .or_else(|| kt.get_index(1).and_then(LuaValue::as_table)) |
| 599 | .and_then(|t| t.tag.as_deref()) |
| 600 | }); |
| 601 | match first_val_tag { |
| 602 | Some("Gradient") if try_parse_gradient_keyframes(kfs).is_some_and(|g| { gradient_modifiers.insert(name.clone(), g); true }) => { |
| 603 | continue; |
| 604 | } |
| 605 | Some("Text" | "StyledText") if try_parse_text_keyframes(kfs).is_some_and(|t| { text_splines.insert(name.clone(), t); true }) => { |
| 606 | continue; |
| 607 | } |
| 608 | Some("Polyline") if try_parse_polyline_keyframes(kfs).is_some_and(|p| { |
| 609 | polyline_splines.insert(name.clone(), (BezierSpline::from_lua(name.clone(), tbl), p)); |
| 610 | true |
| 611 | }) => { |
| 612 | continue; |
| 613 | } |
| 614 | _ => {} |
| 615 | } |
| 616 | } |
| 617 | splines.insert(name.clone(), BezierSpline::from_lua(name.clone(), tbl)); |
| 618 | continue; |
| 619 | } |
| 620 | if kind_ref == "PolyPath" || kind_ref == "Path" { |
| 621 | poly_paths.insert(name.clone(), PolyPath::from_lua(name.clone(), tbl)); |
| 622 | continue; |
| 623 | } |
| 624 | if kind_ref == "TrackerModifier" { |
| 625 | if let Some(in_tbl) = tbl.get_table("Inputs") { |
| 626 | if let Some(src_op) = in_tbl |
| 627 | .get_table("TrackedCenter0") |
| 628 | .and_then(|c| c.get_str("SourceOp")) |
| 629 | { |
| 630 | tracker_aliases.push((name.clone(), src_op.to_owned())); |
| 631 | } else if let Some(p) = in_tbl |
| 632 | .get_table("PatternCenter0") |
| 633 | .and_then(|c| c.get_table("Value")) |
| 634 | .and_then(read_vec2) |
| 635 | { |
| 636 | poly_paths.insert( |
| 637 | name.clone(), |
| 638 | PolyPath { |
| 639 | name: name.clone(), |
| 640 | displacement_op: None, |
| 641 | displacement_value: 0.0, |
| 642 | closed: false, |
| 643 | points: vec![crate::spline::PolyPoint { |
| 644 | x: p[0] - 0.5, |
| 645 | y: p[1] - 0.5, |
| 646 | lx: 0.0, |
| 647 | ly: 0.0, |
| 648 | rx: 0.0, |
| 649 | ry: 0.0, |
| 650 | linear: true, |
| 651 | }], |
| 652 | }, |
| 653 | ); |
| 654 | } |
| 655 | } |
| 656 | continue; |
| 657 | } |
| 658 | if kind_ref == "StyledTextCLS" || kind_ref == "CharacterLevelStyling" { |
| 659 | if let Some(in_tbl) = tbl.get_table("Inputs") { |
| 660 | if let Some(txt_in) = in_tbl.get_table("Text").or_else(|| in_tbl.get_table("StyledText")) { |
| 661 | if let Some(txt) = txt_in |
| 662 | .get_table("Value") |
| 663 | .and_then(|v| v.get_str("Value")) |
| 664 | .or_else(|| txt_in.get_str("Value")) |
| 665 | { |
| 666 | text_modifiers.insert(name.clone(), txt.to_owned()); |
| 667 | } |
| 668 | } |
| 669 | if let Some(arr) = in_tbl |
| 670 | .get_table("RightClickHereToAnimateCharacterLevelStyling") |
| 671 | .or_else(|| in_tbl.get_table("CharacterLevelStyling")) |
| 672 | .and_then(|c| c.get_table("Value")) |
| 673 | .and_then(|v| v.get_table("Array")) |
| 674 | { |
| 675 | let mut entries = Vec::new(); |
| 676 | for (_, ev) in &arr.entries { |
| 677 | let Some(et) = ev.as_table() else { continue }; |
| 678 | if let Some(param) = et.get_f64("Parameter") { |
| 679 | entries.push(ClsEntry { |
| 680 | prop_id: param.round().max(0.0) as u32, |
| 681 | start: et.get_f64("First").unwrap_or(0.0).round().max(0.0) as usize, |
| 682 | end: et.get_f64("Last").unwrap_or(0.0).round().max(0.0) as usize, |
| 683 | value: et.get_f64("Value").unwrap_or(0.0) as f32, |
| 684 | text: et.get_str("String").unwrap_or("").to_owned(), |
| 685 | }); |
| 686 | } else if let (Some(pid), Some(s), Some(e), Some(val)) = ( |
| 687 | et.get_index(1).and_then(LuaValue::as_f64), |
| 688 | et.get_index(2).and_then(LuaValue::as_f64), |
| 689 | et.get_index(3).and_then(LuaValue::as_f64), |
| 690 | et.get_f64("Value"), |
| 691 | ) { |
| 692 | entries.push(ClsEntry { |
| 693 | prop_id: pid.round().max(0.0) as u32, |
| 694 | start: s.round().max(0.0) as usize, |
| 695 | end: e.round().max(0.0) as usize, |
| 696 | value: val as f32, |
| 697 | text: et.get_str("String").unwrap_or("").to_owned(), |
| 698 | }); |
| 699 | } |
| 700 | } |
| 701 | if !entries.is_empty() { |
| 702 | text_cls.insert(name.clone(), entries); |
| 703 | } |
| 704 | } |
| 705 | } |
| 706 | continue; |
| 707 | } |
| 708 | if kind_ref == "Shake" { |
| 709 | let mut inputs = HashMap::new(); |
| 710 | if let Some(in_tbl) = tbl.get_table("Inputs") { |
| 711 | inputs.reserve(in_tbl.entries.len()); |
| 712 | for (ik, iv) in &in_tbl.entries { |
| 713 | let LuaKey::Str(in_name) = ik else { continue }; |
| 714 | let Some(it) = iv.as_table() else { continue }; |
| 715 | inputs.insert(in_name.clone(), parse_input_binding(it)); |
| 716 | } |
| 717 | } |
| 718 | shakes.insert(name.clone(), ShakeModifier { inputs }); |
| 719 | continue; |
| 720 | } |
| 721 | if kind_ref == "Expression" { |
| 722 | let mut inputs = HashMap::new(); |
| 723 | if let Some(in_tbl) = tbl.get_table("Inputs") { |
| 724 | inputs.reserve(in_tbl.entries.len()); |
| 725 | for (ik, iv) in &in_tbl.entries { |
| 726 | let LuaKey::Str(in_name) = ik else { continue }; |
| 727 | let Some(it) = iv.as_table() else { continue }; |
| 728 | inputs.insert(in_name.clone(), parse_input_binding(it)); |
| 729 | } |
| 730 | } |
| 731 | if let Some(uc) = tbl.get_table("UserControls") { |
| 732 | for (uk, uv) in &uc.entries { |
| 733 | let LuaKey::Str(uname) = uk else { continue }; |
| 734 | let Some(ut) = uv.as_table() else { continue }; |
| 735 | let missing = inputs.get(uname).is_none_or(|b| { |
| 736 | b.value.is_none() && b.source_op.is_none() && b.expression.is_none() |
| 737 | }); |
| 738 | if missing { |
| 739 | if let Some(val) = ut.get("INP_Default").map(parse_param_value) { |
| 740 | inputs.insert( |
| 741 | uname.clone(), |
| 742 | InputBinding { |
| 743 | value: Some(val), |
| 744 | source_op: None, |
| 745 | source_output: None, |
| 746 | expression: None, |
| 747 | }, |
| 748 | ); |
| 749 | } |
| 750 | } |
| 751 | } |
| 752 | } |
| 753 | expression_modifiers.insert(name.clone(), ExpressionModifier { inputs }); |
| 754 | continue; |
| 755 | } |
| 756 | if is_modifier_type(kind_ref) { |
| 757 | if tbl.get_table("KeyFrames").is_some() { |
| 758 | splines.insert(name.clone(), BezierSpline::from_lua(name.clone(), tbl)); |
| 759 | } |
| 760 | continue; |
| 761 | } |
| 762 | |
| 763 | let mut view_info_tbl: Option<&LuaTable> = None; |
| 764 | let mut bypassed = false; |
| 765 | let mut inputs_tbl: Option<&LuaTable> = None; |
| 766 | let mut user_controls_tbl: Option<&LuaTable> = None; |
| 767 | let mut clips_tbl: Option<&LuaTable> = None; |
| 768 | let mut inner_tools_tbl: Option<&LuaTable> = None; |
| 769 | let mut outputs_tbl: Option<&LuaTable> = None; |
| 770 | let mut source_op_str: Option<&str> = None; |
| 771 | for (tk, tv) in &tbl.entries { |
| 772 | let LuaKey::Str(s) = tk else { continue }; |
| 773 | match s.as_str() { |
| 774 | "ViewInfo" => view_info_tbl = tv.as_table(), |
| 775 | "PassThrough" => if let Some(b) = tv.as_bool() { bypassed = b; }, |
| 776 | "Inputs" => inputs_tbl = tv.as_table(), |
| 777 | "UserControls" => user_controls_tbl = tv.as_table(), |
| 778 | "Clips" => clips_tbl = tv.as_table(), |
| 779 | "Tools" => inner_tools_tbl = tv.as_table(), |
| 780 | "Outputs" => outputs_tbl = tv.as_table(), |
| 781 | "SourceOp" => source_op_str = tv.as_str(), |
| 782 | _ => {} |
| 783 | } |
| 784 | } |
| 785 | |
| 786 | let pos = view_info_tbl |
| 787 | .and_then(|vi| vi.get_table("Pos")) |
| 788 | .and_then(read_vec2) |
| 789 | .map(|[x, y]| [x as f32, y as f32]) |
| 790 | .unwrap_or([0.0, 0.0]); |
| 791 | |
| 792 | let mut inputs = HashMap::new(); |
| 793 | if let Some(in_tbl) = inputs_tbl { |
| 794 | inputs.reserve(in_tbl.entries.len()); |
| 795 | for (ik, iv) in &in_tbl.entries { |
| 796 | let LuaKey::Str(in_name) = ik else { continue }; |
| 797 | let Some(it) = iv.as_table() else { continue }; |
| 798 | let binding = parse_input_binding(it); |
| 799 | inputs.insert(in_name.clone(), binding); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | // Populate defaults declared in Fusion `UserControls` (needed for ShakeTransform, Custom, etc.) |
| 804 | if let Some(uc) = user_controls_tbl { |
| 805 | for (uk, uv) in &uc.entries { |
| 806 | let LuaKey::Str(uname) = uk else { continue }; |
| 807 | let Some(ut) = uv.as_table() else { continue }; |
| 808 | let missing = inputs.get(uname).is_none_or(|b| { |
| 809 | b.value.is_none() && b.source_op.is_none() && b.expression.is_none() |
| 810 | }); |
| 811 | if missing { |
| 812 | let is_point = ut.get_str("LINKID_DataType") == Some("Point") |
| 813 | || ut.get_str("INPID_InputControl") == Some("OffsetControl"); |
| 814 | let def_val = if is_point { |
| 815 | let dx = ut.get_f64("INP_DefaultX").unwrap_or(0.5); |
| 816 | let dy = ut.get_f64("INP_DefaultY").unwrap_or(0.5); |
| 817 | Some(ParamValue::Point([dx, dy])) |
| 818 | } else { |
| 819 | ut.get("INP_Default").map(parse_param_value) |
| 820 | }; |
| 821 | if let Some(val) = def_val { |
| 822 | inputs.insert( |
| 823 | uname.clone(), |
| 824 | InputBinding { |
| 825 | value: Some(val), |
| 826 | source_op: None, |
| 827 | source_output: None, |
| 828 | expression: None, |
| 829 | }, |
| 830 | ); |
| 831 | } |
| 832 | } |
| 833 | } |
| 834 | } |
| 835 | |
| 836 | let mut clips = Vec::new(); |
| 837 | if let Some(clips_tbl) = clips_tbl { |
| 838 | for (_, cv) in &clips_tbl.entries { |
| 839 | if let Some(ct) = cv.as_table() { |
| 840 | if let Some(clip) = parse_clip_spec(ct) { |
| 841 | clips.push(clip); |
| 842 | } |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | if kind_ref == "Saver" && clips.is_empty() { |
| 848 | if let Some(c) = inputs_tbl |
| 849 | .and_then(|i| i.get_table("Clip")) |
| 850 | .and_then(|c| c.get_table("Value")) |
| 851 | { |
| 852 | if let Some(filename) = c.get_str("Filename") { |
| 853 | clips.push(ClipSpec { |
| 854 | filename: filename.to_owned(), |
| 855 | alternate_filename: None, |
| 856 | start_frame: 0, |
| 857 | length: None, |
| 858 | trim_in: 0, |
| 859 | trim_out: 0, |
| 860 | global_start: 0, |
| 861 | global_end: 0, |
| 862 | extend_first: 0, |
| 863 | extend_last: 0, |
| 864 | loop_clip: false, |
| 865 | reverse: false, |
| 866 | }); |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | // ColorCurves and HueCurves embed their LUTBezier splines inside a nested `Tools` table! |
| 872 | if kind_ref == "ColorCurves" || kind_ref == "HueCurves" { |
| 873 | if let Some(inner_tools) = inner_tools_tbl { |
| 874 | parse_tools_table( |
| 875 | inner_tools, |
| 876 | true, |
| 877 | nodes, |
| 878 | node_by_name, |
| 879 | splines, |
| 880 | poly_paths, |
| 881 | gradient_modifiers, |
| 882 | text_modifiers, |
| 883 | text_splines, |
| 884 | polyline_splines, |
| 885 | text_cls, |
| 886 | shakes, |
| 887 | expression_modifiers, |
| 888 | tracker_aliases, |
| 889 | instanced_from, |
| 890 | macro_owner, |
| 891 | ); |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | let mut macro_output = None; |
| 896 | let mut macro_inner_start = None; |
| 897 | if matches!(kind_ref, "MacroOperator" | "GroupOperator") { |
| 898 | let mut out_name: Option<&str> = None; |
| 899 | if let Some(outs) = outputs_tbl { |
| 900 | for (_, ov) in &outs.entries { |
| 901 | if let Some(ot) = ov.as_table() { |
| 902 | if let Some(src_op) = ot.get_str("SourceOp") { |
| 903 | out_name = Some(src_op); |
| 904 | break; |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | } |
| 909 | let inner_start = nodes.len(); |
| 910 | macro_inner_start = Some(inner_start); |
| 911 | if let Some(inner_tools) = inner_tools_tbl { |
| 912 | parse_tools_table( |
| 913 | inner_tools, |
| 914 | true, |
| 915 | nodes, |
| 916 | node_by_name, |
| 917 | splines, |
| 918 | poly_paths, |
| 919 | gradient_modifiers, |
| 920 | text_modifiers, |
| 921 | text_splines, |
| 922 | polyline_splines, |
| 923 | text_cls, |
| 924 | shakes, |
| 925 | expression_modifiers, |
| 926 | tracker_aliases, |
| 927 | instanced_from, |
| 928 | macro_owner, |
| 929 | ); |
| 930 | } |
| 931 | if let Some(oname) = out_name { |
| 932 | macro_output = nodes[inner_start..] |
| 933 | .iter() |
| 934 | .find(|n| n.name == oname) |
| 935 | .map(|n| n.id) |
| 936 | .or_else(|| node_by_name.get(oname).copied()); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | let id = NodeId(nodes.len()); |
| 941 | if let Some(inner_start) = macro_inner_start { |
| 942 | for nid_raw in inner_start..id.0 { |
| 943 | macro_owner.entry(NodeId(nid_raw)).or_insert(id); |
| 944 | } |
| 945 | } |
| 946 | // Don't overwrite an already-registered top-level node name if an internal macro reuses a local name. |
| 947 | if !is_internal || !node_by_name.contains_key(name) { |
| 948 | node_by_name.insert(name.clone(), id); |
| 949 | } |
| 950 | if let Some(master) = source_op_str { |
| 951 | instanced_from.push((id, master.to_owned())); |
| 952 | } |
| 953 | nodes.push(Node { |
| 954 | id, |
| 955 | name: name.clone(), |
| 956 | kind: kind_ref.to_owned(), |
| 957 | pos, |
| 958 | bypassed, |
| 959 | internal: is_internal, |
| 960 | macro_output, |
| 961 | inputs, |
| 962 | clips, |
| 963 | raw: tbl.clone(), |
| 964 | }); |
| 965 | } |
| 966 | } |
| 967 | |
| 968 | fn parse_clip_spec(ct: &LuaTable) -> Option<ClipSpec> { |
| 969 | let mut filename: Option<&str> = None; |
| 970 | let mut alternate_filename: Option<&str> = None; |
| 971 | let mut start_frame: i64 = -1; |
| 972 | let mut length: Option<i64> = None; |
| 973 | let mut trim_in: i64 = 0; |
| 974 | let mut trim_out: i64 = 0; |
| 975 | let mut global_start: i64 = 0; |
| 976 | let mut global_end: i64 = 0; |
| 977 | let mut extend_first: i64 = 0; |
| 978 | let mut extend_last: i64 = 0; |
| 979 | let mut loop_clip = false; |
| 980 | let mut reverse = false; |
| 981 | |
| 982 | for (k, v) in &ct.entries { |
| 983 | let LuaKey::Str(s) = k else { continue }; |
| 984 | match s.as_str() { |
| 985 | "Filename" => filename = v.as_str(), |
| 986 | "AlternateFilename" => alternate_filename = v.as_str(), |
| 987 | "StartFrame" => if let Some(n) = v.as_f64() { start_frame = n as i64; }, |
| 988 | "Length" => if let Some(n) = v.as_f64() { length = Some(n as i64); }, |
| 989 | "TrimIn" => if let Some(n) = v.as_f64() { trim_in = n as i64; }, |
| 990 | "TrimOut" => if let Some(n) = v.as_f64() { trim_out = n as i64; }, |
| 991 | "GlobalStart" => if let Some(n) = v.as_f64() { global_start = n as i64; }, |
| 992 | "GlobalEnd" => if let Some(n) = v.as_f64() { global_end = n as i64; }, |
| 993 | "ExtendFirst" => if let Some(n) = v.as_f64() { extend_first = n as i64; }, |
| 994 | "ExtendLast" => if let Some(n) = v.as_f64() { extend_last = n as i64; }, |
| 995 | "Loop" => if let Some(n) = v.as_f64() { loop_clip = n == 0.0; }, |
| 996 | "Reverse" => if let Some(b) = v.as_bool() { reverse = b; }, |
| 997 | _ => {} |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | let filename = filename?; |
| 1002 | Some(ClipSpec { |
| 1003 | filename: filename.to_owned(), |
| 1004 | alternate_filename: alternate_filename.map(str::to_owned), |
| 1005 | start_frame, |
| 1006 | length, |
| 1007 | trim_in, |
| 1008 | trim_out, |
| 1009 | global_start, |
| 1010 | global_end, |
| 1011 | extend_first, |
| 1012 | extend_last, |
| 1013 | loop_clip, |
| 1014 | reverse, |
| 1015 | }) |
| 1016 | } |
| 1017 | |
| 1018 | #[cfg(test)] |
| 1019 | mod tests { |
| 1020 | use super::*; |
| 1021 | |
| 1022 | #[test] |
| 1023 | fn styled_text_cls_right_click_rules_parse_inside_macro_operator() { |
| 1024 | let src = r#" |
| 1025 | Composition { |
| 1026 | Tools = ordered() { |
| 1027 | MyMacro = MacroOperator { |
| 1028 | Outputs = ordered() { |
| 1029 | Output1 = InstanceOutput { SourceOp = "Txt1", Source = "Output", }, |
| 1030 | }, |
| 1031 | Tools = ordered() { |
| 1032 | Txt1 = TextPlus { |
| 1033 | Inputs = { |
| 1034 | StyledText = Input { SourceOp = "CLS1", Source = "Value", }, |
| 1035 | }, |
| 1036 | }, |
| 1037 | CLS1 = StyledTextCLS { |
| 1038 | Inputs = { |
| 1039 | Text = Input { Value = StyledText { Value = "Hello" }, }, |
| 1040 | RightClickHereToAnimateCharacterLevelStyling = Input { |
| 1041 | Value = StyledTextCLS_Data { |
| 1042 | Array = { |
| 1043 | { Parameter = 100, First = 0, Last = 1, String = "Arial", }, |
| 1044 | { Parameter = 102, First = 2, Last = 4, Value = 0.25, }, |
| 1045 | }, |
| 1046 | }, |
| 1047 | }, |
| 1048 | }, |
| 1049 | }, |
| 1050 | }, |
| 1051 | }, |
| 1052 | }, |
| 1053 | } |
| 1054 | "#; |
| 1055 | let comp = Comp::from_fusion_str(src).expect("parse macro StyledTextCLS"); |
| 1056 | let txt = comp.nodes.iter().find(|n| n.name == "Txt1").expect("Txt1"); |
| 1057 | assert_eq!(comp.text_modifiers.get("CLS1").map(String::as_str), Some("Hello")); |
| 1058 | let entries = comp.text_cls_entries(txt, "StyledText"); |
| 1059 | assert_eq!(entries.len(), 2); |
| 1060 | assert_eq!(entries[0].prop_id, 100); |
| 1061 | assert_eq!(entries[0].text, "Arial"); |
| 1062 | assert_eq!(entries[1].prop_id, 102); |
| 1063 | assert!((entries[1].value - 0.25).abs() < 1e-6); |
| 1064 | } |
| 1065 | |
| 1066 | #[test] |
| 1067 | fn clear_cross_macro_wires_handles_duplicate_internal_names_across_sibling_macros() { |
| 1068 | let src = r#" |
| 1069 | Composition { |
| 1070 | Tools = ordered() { |
| 1071 | MacroA = MacroOperator { |
| 1072 | Outputs = ordered() { |
| 1073 | Output1 = InstanceOutput { SourceOp = "OutA", Source = "Output", }, |
| 1074 | }, |
| 1075 | Tools = ordered() { |
| 1076 | Background1 = Background { }, |
| 1077 | OnlyInA = Background { }, |
| 1078 | OutA = Merge { |
| 1079 | Inputs = { |
| 1080 | Background = Input { SourceOp = "Background1", Source = "Output", }, |
| 1081 | Foreground = Input { SourceOp = "OnlyInB", Source = "Output", }, |
| 1082 | }, |
| 1083 | }, |
| 1084 | }, |
| 1085 | }, |
| 1086 | MacroB = MacroOperator { |
| 1087 | Outputs = ordered() { |
| 1088 | Output1 = InstanceOutput { SourceOp = "OutB", Source = "Output", }, |
| 1089 | }, |
| 1090 | Tools = ordered() { |
| 1091 | Background1 = Background { }, |
| 1092 | OnlyInB = Background { }, |
| 1093 | OutB = Merge { |
| 1094 | Inputs = { |
| 1095 | Background = Input { SourceOp = "Background1", Source = "Output", }, |
| 1096 | Foreground = Input { SourceOp = "OnlyInA", Source = "Output", }, |
| 1097 | }, |
| 1098 | }, |
| 1099 | }, |
| 1100 | }, |
| 1101 | }, |
| 1102 | } |
| 1103 | "#; |
| 1104 | let comp = Comp::from_fusion_str(src).expect("parse sibling macros with duplicate inner names"); |
| 1105 | let out_a = comp.nodes.iter().find(|n| n.name == "OutA").expect("OutA"); |
| 1106 | let out_b = comp.nodes.iter().find(|n| n.name == "OutB").expect("OutB"); |
| 1107 | assert_eq!(out_a.connected_node("Background"), Some("Background1")); |
| 1108 | assert_eq!(out_b.connected_node("Background"), Some("Background1")); |
| 1109 | assert_eq!(out_a.connected_node("Foreground"), None, "OutA -> OnlyInB must be cleared"); |
| 1110 | assert_eq!(out_b.connected_node("Foreground"), None, "OutB -> OnlyInA must be cleared"); |
| 1111 | } |
| 1112 | |
| 1113 | #[test] |
| 1114 | fn macro_instance_input_default_does_not_overwrite_omitted_inner_input() { |
| 1115 | let src = r#" |
| 1116 | Composition { |
| 1117 | Tools = ordered() { |
| 1118 | Colorize1 = MacroOperator { |
| 1119 | Inputs = ordered() { |
| 1120 | Input11 = InstanceInput { |
| 1121 | SourceOp = "Color", |
| 1122 | Source = "NumberIn1", |
| 1123 | Default = 0, |
| 1124 | }, |
| 1125 | Input12 = InstanceInput { |
| 1126 | SourceOp = "Color", |
| 1127 | Source = "NumberIn2", |
| 1128 | Default = 0.3, |
| 1129 | }, |
| 1130 | Input13 = InstanceInput { |
| 1131 | SourceOp = "Color", |
| 1132 | Source = "NumberIn3", |
| 1133 | Default = 0.5, |
| 1134 | }, |
| 1135 | }, |
| 1136 | Outputs = ordered() { |
| 1137 | MainOutput1 = InstanceOutput { SourceOp = "Color", Source = "Output", }, |
| 1138 | }, |
| 1139 | Tools = ordered() { |
| 1140 | Color = Custom { |
| 1141 | Inputs = { |
| 1142 | NumberIn1 = Input { Value = 0.02, }, |
| 1143 | NumberIn3 = Input { Value = 0.607, }, |
| 1144 | }, |
| 1145 | }, |
| 1146 | }, |
| 1147 | }, |
| 1148 | }, |
| 1149 | } |
| 1150 | "#; |
| 1151 | let comp = Comp::from_fusion_str(src).expect("parse macro InstanceInput Default"); |
| 1152 | let color = comp.nodes.iter().find(|n| n.name == "Color").expect("Color"); |
| 1153 | let expr = crate::expr::ExprEngine::new(); |
| 1154 | assert!((comp.eval_number(color, "NumberIn1", 0.0, 0.0, &expr) - 0.02).abs() < 1e-9); |
| 1155 | assert_eq!(color.static_number("NumberIn2"), None); |
| 1156 | assert!((comp.eval_number(color, "NumberIn2", 0.0, 0.0, &expr) - 0.0).abs() < 1e-9); |
| 1157 | assert!((comp.eval_number(color, "NumberIn3", 0.0, 0.0, &expr) - 0.607).abs() < 1e-9); |
| 1158 | } |
| 1159 | } |