| 1 | //! Interactive on-screen Viewer gizmos for selected Fusion nodes |
| 2 | //! (`Transform`, `Merge`, `EllipseMask`, `RectangleMask`, `PolylineMask`). |
| 3 | |
| 4 | use draw::Primitive; |
| 5 | use model::{Comp, ExprEngine, Node, NodeId, ParamValue}; |
| 6 | use std::f32::consts::TAU; |
| 7 | |
| 8 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 9 | pub enum GizmoHandle { |
| 10 | Center, |
| 11 | AxisX, |
| 12 | AxisY, |
| 13 | Rotate, |
| 14 | Scale, |
| 15 | Width, |
| 16 | Height, |
| 17 | } |
| 18 | |
| 19 | #[derive(Clone, Debug)] |
| 20 | pub struct GizmoEdit { |
| 21 | pub node_id: NodeId, |
| 22 | pub input: &'static str, |
| 23 | pub value: ParamValue, |
| 24 | pub label: &'static str, |
| 25 | pub coalesce_key: String, |
| 26 | } |
| 27 | |
| 28 | #[derive(Clone, Debug)] |
| 29 | pub struct GizmoDrag { |
| 30 | pub node_id: NodeId, |
| 31 | pub handle: GizmoHandle, |
| 32 | pub start_local: [f32; 2], |
| 33 | pub start_center: [f64; 2], |
| 34 | pub start_size: f64, |
| 35 | pub start_angle: f64, |
| 36 | pub start_width: f64, |
| 37 | pub start_height: f64, |
| 38 | pub start_angle_ptr: f64, |
| 39 | pub start_dist_px: f32, |
| 40 | } |
| 41 | |
| 42 | pub struct GizmoState { |
| 43 | pub enabled: bool, |
| 44 | pub hovered: Option<GizmoHandle>, |
| 45 | pub drag: Option<GizmoDrag>, |
| 46 | expr: ExprEngine, |
| 47 | } |
| 48 | |
| 49 | impl Default for GizmoState { |
| 50 | fn default() -> Self { |
| 51 | Self::new() |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | impl GizmoState { |
| 56 | pub fn new() -> Self { |
| 57 | Self { |
| 58 | enabled: true, |
| 59 | hovered: None, |
| 60 | drag: None, |
| 61 | expr: ExprEngine::new(), |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | pub fn is_dragging(&self) -> bool { |
| 66 | self.drag.is_some() |
| 67 | } |
| 68 | |
| 69 | pub fn supports_node(comp: &Comp, node_id: NodeId) -> bool { |
| 70 | if node_id.0 >= comp.nodes.len() { |
| 71 | return false; |
| 72 | } |
| 73 | matches!( |
| 74 | comp.node(node_id).kind.as_str(), |
| 75 | "Transform" | "Merge" | "EllipseMask" | "RectangleMask" | "PolylineMask" |
| 76 | ) |
| 77 | } |
| 78 | |
| 79 | /// Maps Fusion normalized coordinates (`[0..1]`, Y-up) to viewer-local screen pixels. |
| 80 | pub fn fu_to_local(fu: [f64; 2], img_rect: [f32; 4]) -> [f32; 2] { |
| 81 | let w = (img_rect[2] - img_rect[0]).max(1.0); |
| 82 | let h = (img_rect[3] - img_rect[1]).max(1.0); |
| 83 | [ |
| 84 | img_rect[0] + fu[0] as f32 * w, |
| 85 | img_rect[3] - fu[1] as f32 * h, |
| 86 | ] |
| 87 | } |
| 88 | |
| 89 | /// Maps viewer-local screen pixels to Fusion normalized coordinates (`[0..1]`, Y-up). |
| 90 | pub fn local_to_fu(pt: [f32; 2], img_rect: [f32; 4]) -> [f64; 2] { |
| 91 | let w = (img_rect[2] - img_rect[0]).max(1.0); |
| 92 | let h = (img_rect[3] - img_rect[1]).max(1.0); |
| 93 | [ |
| 94 | f64::from((pt[0] - img_rect[0]) / w), |
| 95 | f64::from((img_rect[3] - pt[1]) / h), |
| 96 | ] |
| 97 | } |
| 98 | |
| 99 | fn eval_node_params(&self, comp: &Comp, node: &Node, time: f64) -> NodeGizmoParams { |
| 100 | let fin = |v: f64, d: f64| if v.is_finite() { v } else { d }; |
| 101 | let t = if time.is_finite() { time } else { 0.0 }; |
| 102 | let c = comp.eval_point(node, "Center", t, [0.5, 0.5], &self.expr); |
| 103 | let center = [fin(c[0], 0.5), fin(c[1], 0.5)]; |
| 104 | let size = fin(comp.eval_number(node, "Size", t, 1.0, &self.expr), 1.0); |
| 105 | let angle = fin(comp.eval_number(node, "Angle", t, 0.0, &self.expr), 0.0); |
| 106 | let width = fin(comp.eval_number(node, "Width", t, 0.5, &self.expr), 0.5); |
| 107 | let height = fin(comp.eval_number(node, "Height", t, 0.5, &self.expr), 0.5); |
| 108 | NodeGizmoParams { center, size, angle, width, height } |
| 109 | } |
| 110 | |
| 111 | fn handle_positions(&self, comp: &Comp, node: &Node, time: f64, img_rect: [f32; 4]) -> Vec<(GizmoHandle, [f32; 2])> { |
| 112 | let p = self.eval_node_params(comp, node, time); |
| 113 | let c = Self::fu_to_local(p.center, img_rect); |
| 114 | let rad = (p.angle as f32).to_radians(); |
| 115 | // Fusion CCW angle in Y-up => screen space (Y-down): +X dir is (cos, -sin), +Y dir is (-sin, -cos) |
| 116 | let ux = [rad.cos(), -rad.sin()]; |
| 117 | let uy = [-rad.sin(), -rad.cos()]; |
| 118 | let img_w = (img_rect[2] - img_rect[0]).max(1.0); |
| 119 | let img_h = (img_rect[3] - img_rect[1]).max(1.0); |
| 120 | |
| 121 | match node.kind.as_str() { |
| 122 | "Transform" | "Merge" => { |
| 123 | let arm = 58.0_f32; |
| 124 | let ring_r = 46.0_f32; |
| 125 | let diag = (0.7071_f32) * (ring_r + 12.0); |
| 126 | vec![ |
| 127 | (GizmoHandle::AxisX, [c[0] + ux[0] * arm, c[1] + ux[1] * arm]), |
| 128 | (GizmoHandle::AxisY, [c[0] + uy[0] * arm, c[1] + uy[1] * arm]), |
| 129 | (GizmoHandle::Rotate, [c[0] - uy[0] * ring_r, c[1] - uy[1] * ring_r]), |
| 130 | ( |
| 131 | GizmoHandle::Scale, |
| 132 | [ |
| 133 | c[0] + (ux[0] + uy[0]) * diag, |
| 134 | c[1] + (ux[1] + uy[1]) * diag, |
| 135 | ], |
| 136 | ), |
| 137 | (GizmoHandle::Center, c), |
| 138 | ] |
| 139 | } |
| 140 | "EllipseMask" | "RectangleMask" => { |
| 141 | let hw = (p.width.abs() as f32 * 0.5 * img_w).max(10.0); |
| 142 | let hh = if node.kind == "EllipseMask" { |
| 143 | // Fusion EllipseMask: both Width & Height are in frame-width units |
| 144 | (p.height.abs() as f32 * 0.5 * img_w).max(10.0) |
| 145 | } else { |
| 146 | // Fusion RectangleMask: Width in frame-width units, Height in frame-height units |
| 147 | (p.height.abs() as f32 * 0.5 * img_h).max(10.0) |
| 148 | }; |
| 149 | vec![ |
| 150 | (GizmoHandle::Width, [c[0] + ux[0] * hw, c[1] + ux[1] * hw]), |
| 151 | (GizmoHandle::Height, [c[0] + uy[0] * hh, c[1] + uy[1] * hh]), |
| 152 | (GizmoHandle::Rotate, [c[0] + ux[0] * (hw + 22.0), c[1] + ux[1] * (hw + 22.0)]), |
| 153 | (GizmoHandle::Center, c), |
| 154 | ] |
| 155 | } |
| 156 | "PolylineMask" => vec![(GizmoHandle::Center, c)], |
| 157 | _ => Vec::new(), |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | pub fn hit_test( |
| 162 | &self, |
| 163 | comp: &Comp, |
| 164 | node_id: NodeId, |
| 165 | time: f64, |
| 166 | img_rect: [f32; 4], |
| 167 | pointer_local: [f32; 2], |
| 168 | ) -> Option<GizmoHandle> { |
| 169 | if !self.enabled || !Self::supports_node(comp, node_id) { |
| 170 | return None; |
| 171 | } |
| 172 | let node = comp.node(node_id); |
| 173 | let handles = self.handle_positions(comp, node, time, img_rect); |
| 174 | let mut best: Option<(f32, GizmoHandle)> = None; |
| 175 | for (h, pos) in handles { |
| 176 | let d = hypot2(pointer_local, pos); |
| 177 | let tol = if h == GizmoHandle::Center { 13.0 } else { 11.0 }; |
| 178 | if d <= tol && best.is_none_or(|(bd, _)| d < bd) { |
| 179 | best = Some((d, h)); |
| 180 | } |
| 181 | } |
| 182 | best.map(|(_, h)| h) |
| 183 | } |
| 184 | |
| 185 | pub fn update_hover( |
| 186 | &mut self, |
| 187 | comp: &Comp, |
| 188 | node_id: Option<NodeId>, |
| 189 | time: f64, |
| 190 | img_rect: [f32; 4], |
| 191 | pointer_local: [f32; 2], |
| 192 | ) { |
| 193 | self.hovered = node_id.and_then(|id| self.hit_test(comp, id, time, img_rect, pointer_local)); |
| 194 | } |
| 195 | |
| 196 | pub fn begin_drag( |
| 197 | &mut self, |
| 198 | comp: &Comp, |
| 199 | node_id: NodeId, |
| 200 | time: f64, |
| 201 | img_rect: [f32; 4], |
| 202 | pointer_local: [f32; 2], |
| 203 | ) -> bool { |
| 204 | let Some(handle) = self.hit_test(comp, node_id, time, img_rect, pointer_local) else { |
| 205 | return false; |
| 206 | }; |
| 207 | let node = comp.node(node_id); |
| 208 | let p = self.eval_node_params(comp, node, time); |
| 209 | let c = Self::fu_to_local(p.center, img_rect); |
| 210 | let dx = f64::from(pointer_local[0] - c[0]); |
| 211 | let dy = f64::from(c[1] - pointer_local[1]); // Y-up |
| 212 | let start_angle_ptr = dy.atan2(dx).to_degrees(); |
| 213 | let start_dist_px = hypot2(pointer_local, c).max(8.0); |
| 214 | self.drag = Some(GizmoDrag { |
| 215 | node_id, |
| 216 | handle, |
| 217 | start_local: pointer_local, |
| 218 | start_center: p.center, |
| 219 | start_size: p.size, |
| 220 | start_angle: p.angle, |
| 221 | start_width: p.width, |
| 222 | start_height: p.height, |
| 223 | start_angle_ptr, |
| 224 | start_dist_px, |
| 225 | }); |
| 226 | true |
| 227 | } |
| 228 | |
| 229 | pub fn update_drag( |
| 230 | &mut self, |
| 231 | comp: &Comp, |
| 232 | img_rect: [f32; 4], |
| 233 | pointer_local: [f32; 2], |
| 234 | fine: bool, |
| 235 | ) -> Option<GizmoEdit> { |
| 236 | if !pointer_local[0].is_finite() || !pointer_local[1].is_finite() { |
| 237 | return None; |
| 238 | } |
| 239 | let d = self.drag.as_ref()?; |
| 240 | if !Self::supports_node(comp, d.node_id) { |
| 241 | return None; |
| 242 | } |
| 243 | let kind = comp.node(d.node_id).kind.as_str(); |
| 244 | let img_w = f64::from((img_rect[2] - img_rect[0]).max(1.0)); |
| 245 | let img_h = f64::from((img_rect[3] - img_rect[1]).max(1.0)); |
| 246 | let sens = if fine { 0.2 } else { 1.0 }; |
| 247 | let c = Self::fu_to_local(d.start_center, img_rect); |
| 248 | let dx_px = f64::from(pointer_local[0] - d.start_local[0]) * sens; |
| 249 | let dy_px = f64::from(d.start_local[1] - pointer_local[1]) * sens; // Y-up |
| 250 | |
| 251 | match d.handle { |
| 252 | GizmoHandle::Center => { |
| 253 | let nx = d.start_center[0] + dx_px / img_w; |
| 254 | let ny = d.start_center[1] + dy_px / img_h; |
| 255 | Some(GizmoEdit { |
| 256 | node_id: d.node_id, |
| 257 | input: "Center", |
| 258 | value: ParamValue::Point([nx, ny]), |
| 259 | label: "Move Center", |
| 260 | coalesce_key: format!("gizmo:{}:Center", d.node_id.0), |
| 261 | }) |
| 262 | } |
| 263 | GizmoHandle::AxisX | GizmoHandle::AxisY => { |
| 264 | // Constrain the pointer motion to the node's rotated axis (screen space, Y-down). |
| 265 | let rad = d.start_angle.to_radians(); |
| 266 | let (axis, label) = if d.handle == GizmoHandle::AxisX { |
| 267 | ([rad.cos(), -rad.sin()], "Move Center X") |
| 268 | } else { |
| 269 | ([-rad.sin(), -rad.cos()], "Move Center Y") |
| 270 | }; |
| 271 | let t = dx_px * axis[0] + (-dy_px) * axis[1]; |
| 272 | Some(GizmoEdit { |
| 273 | node_id: d.node_id, |
| 274 | input: "Center", |
| 275 | value: ParamValue::Point([ |
| 276 | d.start_center[0] + t * axis[0] / img_w, |
| 277 | d.start_center[1] - t * axis[1] / img_h, |
| 278 | ]), |
| 279 | label: label, |
| 280 | coalesce_key: format!("gizmo:{}:Center", d.node_id.0), |
| 281 | }) |
| 282 | } |
| 283 | GizmoHandle::Rotate => { |
| 284 | let rx = f64::from(pointer_local[0] - c[0]); |
| 285 | let ry = f64::from(c[1] - pointer_local[1]); |
| 286 | let cur_ang = ry.atan2(rx).to_degrees(); |
| 287 | let mut delta = cur_ang - d.start_angle_ptr; |
| 288 | if delta > 180.0 { delta -= 360.0; } |
| 289 | if delta < -180.0 { delta += 360.0; } |
| 290 | let new_ang = d.start_angle + delta * sens; |
| 291 | Some(GizmoEdit { |
| 292 | node_id: d.node_id, |
| 293 | input: "Angle", |
| 294 | value: ParamValue::Number(new_ang), |
| 295 | label: "Rotate Gizmo", |
| 296 | coalesce_key: format!("gizmo:{}:Angle", d.node_id.0), |
| 297 | }) |
| 298 | } |
| 299 | GizmoHandle::Scale => { |
| 300 | let cur_dist = hypot2(pointer_local, c).max(4.0); |
| 301 | let ratio = f64::from(cur_dist / d.start_dist_px); |
| 302 | let new_size = (d.start_size * (1.0 + (ratio - 1.0) * sens)).clamp(0.01, 20.0); |
| 303 | Some(GizmoEdit { |
| 304 | node_id: d.node_id, |
| 305 | input: "Size", |
| 306 | value: ParamValue::Number(new_size), |
| 307 | label: "Scale Gizmo", |
| 308 | coalesce_key: format!("gizmo:{}:Size", d.node_id.0), |
| 309 | }) |
| 310 | } |
| 311 | GizmoHandle::Width => { |
| 312 | let rad = d.start_angle.to_radians(); |
| 313 | let ux = [rad.cos(), -rad.sin()]; |
| 314 | let rx = f64::from(pointer_local[0] - c[0]); |
| 315 | let ry = f64::from(pointer_local[1] - c[1]); |
| 316 | let proj = (rx * ux[0] + ry * ux[1]).abs(); |
| 317 | let raw_w = ((proj * 2.0) / img_w).clamp(0.005, 4.0); |
| 318 | let new_w = d.start_width + (raw_w - d.start_width) * sens; |
| 319 | Some(GizmoEdit { |
| 320 | node_id: d.node_id, |
| 321 | input: "Width", |
| 322 | value: ParamValue::Number(new_w), |
| 323 | label: "Resize Mask Width", |
| 324 | coalesce_key: format!("gizmo:{}:Width", d.node_id.0), |
| 325 | }) |
| 326 | } |
| 327 | GizmoHandle::Height => { |
| 328 | let rad = d.start_angle.to_radians(); |
| 329 | let uy = [-rad.sin(), -rad.cos()]; |
| 330 | let rx = f64::from(pointer_local[0] - c[0]); |
| 331 | let ry = f64::from(pointer_local[1] - c[1]); |
| 332 | let proj = (rx * uy[0] + ry * uy[1]).abs(); |
| 333 | let denom = if kind == "EllipseMask" { img_w } else { img_h }; |
| 334 | let raw_h = ((proj * 2.0) / denom).clamp(0.005, 4.0); |
| 335 | let new_h = d.start_height + (raw_h - d.start_height) * sens; |
| 336 | Some(GizmoEdit { |
| 337 | node_id: d.node_id, |
| 338 | input: "Height", |
| 339 | value: ParamValue::Number(new_h), |
| 340 | label: "Resize Mask Height", |
| 341 | coalesce_key: format!("gizmo:{}:Height", d.node_id.0), |
| 342 | }) |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | pub fn end_drag(&mut self) { |
| 348 | self.drag = None; |
| 349 | } |
| 350 | |
| 351 | pub fn paint<'a>( |
| 352 | &self, |
| 353 | comp: &Comp, |
| 354 | node_id: NodeId, |
| 355 | time: f64, |
| 356 | img_rect: [f32; 4], |
| 357 | vis_rect: [f32; 4], |
| 358 | ) -> Vec<Primitive<'a>> { |
| 359 | let mut out = Vec::new(); |
| 360 | if !self.enabled || !Self::supports_node(comp, node_id) { |
| 361 | return out; |
| 362 | } |
| 363 | let node = comp.node(node_id); |
| 364 | let p = self.eval_node_params(comp, node, time); |
| 365 | let c = Self::fu_to_local(p.center, img_rect); |
| 366 | let rad = (p.angle as f32).to_radians(); |
| 367 | let ux = [rad.cos(), -rad.sin()]; |
| 368 | let uy = [-rad.sin(), -rad.cos()]; |
| 369 | let img_w = (img_rect[2] - img_rect[0]).max(1.0); |
| 370 | let img_h = (img_rect[3] - img_rect[1]).max(1.0); |
| 371 | let active_h = self.drag.as_ref().map(|d| d.handle).or(self.hovered); |
| 372 | |
| 373 | out.push(Primitive::PushClip { rect: vis_rect, radius: 0.0 }); |
| 374 | |
| 375 | match node.kind.as_str() { |
| 376 | "Transform" | "Merge" => { |
| 377 | let arm = 58.0_f32; |
| 378 | let ring_r = 46.0_f32; |
| 379 | let diag = 0.7071_f32 * (ring_r + 12.0); |
| 380 | |
| 381 | // Subtle rotation ring (28 segments) |
| 382 | let ring_col = if active_h == Some(GizmoHandle::Rotate) { |
| 383 | [1.0, 0.85, 0.30, 0.95] |
| 384 | } else { |
| 385 | [0.72, 0.78, 0.96, 0.45] |
| 386 | }; |
| 387 | const SEGS: usize = 28; |
| 388 | for i in 0..SEGS { |
| 389 | let a0 = (i as f32 / SEGS as f32) * TAU; |
| 390 | let a1 = ((i + 1) as f32 / SEGS as f32) * TAU; |
| 391 | out.push(Primitive::Segment { |
| 392 | from: [c[0] + a0.cos() * ring_r, c[1] + a0.sin() * ring_r], |
| 393 | to: [c[0] + a1.cos() * ring_r, c[1] + a1.sin() * ring_r], |
| 394 | width: 1.2, |
| 395 | color: ring_col, |
| 396 | }); |
| 397 | } |
| 398 | |
| 399 | // X axis arrow (coral red) |
| 400 | let px = [c[0] + ux[0] * arm, c[1] + ux[1] * arm]; |
| 401 | let col_x = if active_h == Some(GizmoHandle::AxisX) { |
| 402 | [1.0, 0.50, 0.48, 1.0] |
| 403 | } else { |
| 404 | [0.95, 0.34, 0.34, 0.92] |
| 405 | }; |
| 406 | out.push(Primitive::Segment { from: c, to: px, width: 1.8, color: col_x }); |
| 407 | push_handle_dot(&mut out, px, 4.5, col_x, active_h == Some(GizmoHandle::AxisX)); |
| 408 | |
| 409 | // Y axis arrow (emerald green) |
| 410 | let py = [c[0] + uy[0] * arm, c[1] + uy[1] * arm]; |
| 411 | let col_y = if active_h == Some(GizmoHandle::AxisY) { |
| 412 | [0.48, 1.0, 0.64, 1.0] |
| 413 | } else { |
| 414 | [0.30, 0.88, 0.48, 0.92] |
| 415 | }; |
| 416 | out.push(Primitive::Segment { from: c, to: py, width: 1.8, color: col_y }); |
| 417 | push_handle_dot(&mut out, py, 4.5, col_y, active_h == Some(GizmoHandle::AxisY)); |
| 418 | |
| 419 | // Rotation knob on bottom of ring |
| 420 | let pr = [c[0] - uy[0] * ring_r, c[1] - uy[1] * ring_r]; |
| 421 | push_handle_dot( |
| 422 | &mut out, |
| 423 | pr, |
| 424 | 4.5, |
| 425 | [0.58, 0.74, 1.0, 0.95], |
| 426 | active_h == Some(GizmoHandle::Rotate), |
| 427 | ); |
| 428 | |
| 429 | // Scale square handle (NE) |
| 430 | let ps = [c[0] + (ux[0] + uy[0]) * diag, c[1] + (ux[1] + uy[1]) * diag]; |
| 431 | out.push(Primitive::Segment { |
| 432 | from: c, |
| 433 | to: ps, |
| 434 | width: 1.0, |
| 435 | color: [0.96, 0.80, 0.32, 0.45], |
| 436 | }); |
| 437 | push_handle_square( |
| 438 | &mut out, |
| 439 | ps, |
| 440 | 4.5, |
| 441 | [0.98, 0.80, 0.30, 0.95], |
| 442 | active_h == Some(GizmoHandle::Scale), |
| 443 | ); |
| 444 | |
| 445 | // Center crosshair |
| 446 | push_center_crosshair(&mut out, c, active_h == Some(GizmoHandle::Center)); |
| 447 | } |
| 448 | "EllipseMask" => { |
| 449 | let hw = (p.width.abs() as f32 * 0.5 * img_w).max(4.0); |
| 450 | let hh = (p.height.abs() as f32 * 0.5 * img_w).max(4.0); |
| 451 | let outline_col = [0.30, 0.92, 0.62, 0.88]; |
| 452 | const SEGS: usize = 40; |
| 453 | for i in 0..SEGS { |
| 454 | let a0 = (i as f32 / SEGS as f32) * TAU; |
| 455 | let a1 = ((i + 1) as f32 / SEGS as f32) * TAU; |
| 456 | let p0 = [ |
| 457 | c[0] + ux[0] * (a0.cos() * hw) + uy[0] * (a0.sin() * hh), |
| 458 | c[1] + ux[1] * (a0.cos() * hw) + uy[1] * (a0.sin() * hh), |
| 459 | ]; |
| 460 | let p1 = [ |
| 461 | c[0] + ux[0] * (a1.cos() * hw) + uy[0] * (a1.sin() * hh), |
| 462 | c[1] + ux[1] * (a1.cos() * hw) + uy[1] * (a1.sin() * hh), |
| 463 | ]; |
| 464 | out.push(Primitive::Segment { from: p0, to: p1, width: 1.5, color: outline_col }); |
| 465 | } |
| 466 | let pw = [c[0] + ux[0] * hw, c[1] + ux[1] * hw]; |
| 467 | let ph = [c[0] + uy[0] * hh, c[1] + uy[1] * hh]; |
| 468 | let pr = [c[0] + ux[0] * (hw + 22.0), c[1] + ux[1] * (hw + 22.0)]; |
| 469 | out.push(Primitive::Segment { from: pw, to: pr, width: 1.1, color: [0.60, 0.80, 1.0, 0.6] }); |
| 470 | push_handle_square(&mut out, pw, 4.2, outline_col, active_h == Some(GizmoHandle::Width)); |
| 471 | push_handle_square(&mut out, ph, 4.2, outline_col, active_h == Some(GizmoHandle::Height)); |
| 472 | push_handle_dot(&mut out, pr, 4.2, [0.58, 0.74, 1.0, 0.95], active_h == Some(GizmoHandle::Rotate)); |
| 473 | push_center_crosshair(&mut out, c, active_h == Some(GizmoHandle::Center)); |
| 474 | } |
| 475 | "RectangleMask" => { |
| 476 | let hw = (p.width.abs() as f32 * 0.5 * img_w).max(4.0); |
| 477 | let hh = (p.height.abs() as f32 * 0.5 * img_h).max(4.0); |
| 478 | let outline_col = [0.30, 0.92, 0.62, 0.88]; |
| 479 | let corners = [ |
| 480 | [c[0] - ux[0] * hw - uy[0] * hh, c[1] - ux[1] * hw - uy[1] * hh], |
| 481 | [c[0] + ux[0] * hw - uy[0] * hh, c[1] + ux[1] * hw - uy[1] * hh], |
| 482 | [c[0] + ux[0] * hw + uy[0] * hh, c[1] + ux[1] * hw + uy[1] * hh], |
| 483 | [c[0] - ux[0] * hw + uy[0] * hh, c[1] - ux[1] * hw + uy[1] * hh], |
| 484 | ]; |
| 485 | for i in 0..4 { |
| 486 | out.push(Primitive::Segment { |
| 487 | from: corners[i], |
| 488 | to: corners[(i + 1) % 4], |
| 489 | width: 1.5, |
| 490 | color: outline_col, |
| 491 | }); |
| 492 | } |
| 493 | let pw = [c[0] + ux[0] * hw, c[1] + ux[1] * hw]; |
| 494 | let ph = [c[0] + uy[0] * hh, c[1] + uy[1] * hh]; |
| 495 | let pr = [c[0] + ux[0] * (hw + 22.0), c[1] + ux[1] * (hw + 22.0)]; |
| 496 | out.push(Primitive::Segment { from: pw, to: pr, width: 1.1, color: [0.60, 0.80, 1.0, 0.6] }); |
| 497 | push_handle_square(&mut out, pw, 4.2, outline_col, active_h == Some(GizmoHandle::Width)); |
| 498 | push_handle_square(&mut out, ph, 4.2, outline_col, active_h == Some(GizmoHandle::Height)); |
| 499 | push_handle_dot(&mut out, pr, 4.2, [0.58, 0.74, 1.0, 0.95], active_h == Some(GizmoHandle::Rotate)); |
| 500 | push_center_crosshair(&mut out, c, active_h == Some(GizmoHandle::Center)); |
| 501 | } |
| 502 | "PolylineMask" => { |
| 503 | let pts = extract_polyline_points(comp, node, time); |
| 504 | let poly_col = [0.34, 0.88, 0.98, 0.90]; |
| 505 | let handle_col = [0.88, 0.56, 0.98, 0.75]; |
| 506 | if pts.len() >= 2 { |
| 507 | let place = |dx: f64, dy: f64| rotate_about(Self::fu_to_local([p.center[0] + dx, p.center[1] + dy], img_rect), c, rad); |
| 508 | for i in 0..pts.len() { |
| 509 | let a = &pts[i]; |
| 510 | let b = &pts[(i + 1) % pts.len()]; |
| 511 | if ![a.x, a.y, a.rx, a.ry, a.lx, a.ly, b.x, b.y, b.lx, b.ly] |
| 512 | .iter() |
| 513 | .all(|v| v.is_finite()) |
| 514 | { |
| 515 | continue; |
| 516 | } |
| 517 | let p0 = place(a.x, a.y); |
| 518 | let p1 = place(a.x + a.rx, a.y + a.ry); |
| 519 | let p2 = place(b.x + b.lx, b.y + b.ly); |
| 520 | let p3 = place(b.x, b.y); |
| 521 | let curved = a.rx.abs() + a.ry.abs() + b.lx.abs() + b.ly.abs() > 1e-5; |
| 522 | if curved { |
| 523 | let mut prev = p0; |
| 524 | for s in 1..=10 { |
| 525 | let t = s as f32 / 10.0; |
| 526 | let u = 1.0 - t; |
| 527 | let cur = [ |
| 528 | u * u * u * p0[0] + 3.0 * u * u * t * p1[0] + 3.0 * u * t * t * p2[0] + t * t * t * p3[0], |
| 529 | u * u * u * p0[1] + 3.0 * u * u * t * p1[1] + 3.0 * u * t * t * p2[1] + t * t * t * p3[1], |
| 530 | ]; |
| 531 | out.push(Primitive::Segment { from: prev, to: cur, width: 1.5, color: poly_col }); |
| 532 | prev = cur; |
| 533 | } |
| 534 | } else { |
| 535 | out.push(Primitive::Segment { from: p0, to: p3, width: 1.5, color: poly_col }); |
| 536 | } |
| 537 | if a.rx.abs() + a.ry.abs() > 1e-4 { |
| 538 | out.push(Primitive::Segment { from: p0, to: p1, width: 1.0, color: handle_col }); |
| 539 | push_handle_dot(&mut out, p1, 2.8, handle_col, false); |
| 540 | } |
| 541 | if a.lx.abs() + a.ly.abs() > 1e-4 { |
| 542 | let pl = place(a.x + a.lx, a.y + a.ly); |
| 543 | out.push(Primitive::Segment { from: p0, to: pl, width: 1.0, color: handle_col }); |
| 544 | push_handle_dot(&mut out, pl, 2.8, handle_col, false); |
| 545 | } |
| 546 | push_handle_square(&mut out, p0, 3.4, poly_col, false); |
| 547 | } |
| 548 | } |
| 549 | push_center_crosshair(&mut out, c, active_h == Some(GizmoHandle::Center)); |
| 550 | } |
| 551 | _ => {} |
| 552 | } |
| 553 | |
| 554 | out.push(Primitive::PopClip); |
| 555 | out |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | struct NodeGizmoParams { |
| 560 | center: [f64; 2], |
| 561 | size: f64, |
| 562 | angle: f64, |
| 563 | width: f64, |
| 564 | height: f64, |
| 565 | } |
| 566 | |
| 567 | /// Rotates screen point `p` about `c` counter-clockwise as Fusion sees it (screen Y points down). |
| 568 | fn rotate_about(p: [f32; 2], c: [f32; 2], rad: f32) -> [f32; 2] { |
| 569 | let (rx, ry) = (p[0] - c[0], p[1] - c[1]); |
| 570 | [c[0] + rx * rad.cos() + ry * rad.sin(), c[1] - rx * rad.sin() + ry * rad.cos()] |
| 571 | } |
| 572 | |
| 573 | fn extract_polyline_points(comp: &Comp, node: &Node, time: f64) -> Vec<model::PolyPoint> { |
| 574 | if let Some(src) = node.connected_node("Polyline") { |
| 575 | if let Some(pts) = comp.eval_polyline_spline(src, time) { |
| 576 | return pts; |
| 577 | } |
| 578 | if let Some(pp) = comp.poly_paths.get(src) { |
| 579 | return pp.points.clone(); |
| 580 | } |
| 581 | } |
| 582 | if let Some(pl_tbl) = node |
| 583 | .raw |
| 584 | .get_table("Inputs") |
| 585 | .and_then(|i| i.get_table("Polyline").or_else(|| i.get_table("PolyLine"))) |
| 586 | .and_then(|p| p.get_table("Value")) |
| 587 | { |
| 588 | return model::PolyPath::parse_polyline_table(pl_tbl).1; |
| 589 | } |
| 590 | Vec::new() |
| 591 | } |
| 592 | |
| 593 | fn push_center_crosshair(out: &mut Vec<Primitive<'_>>, c: [f32; 2], hot: bool) { |
| 594 | let col = if hot { [1.0, 0.90, 0.35, 1.0] } else { [0.96, 0.96, 0.98, 0.92] }; |
| 595 | let r = if hot { 8.0 } else { 6.5 }; |
| 596 | out.push(Primitive::Segment { from: [c[0] - r, c[1]], to: [c[0] + r, c[1]], width: 1.6, color: col }); |
| 597 | out.push(Primitive::Segment { from: [c[0], c[1] - r], to: [c[0], c[1] + r], width: 1.6, color: col }); |
| 598 | out.push(Primitive::RoundedRect { |
| 599 | rect: [c[0] - 4.5, c[1] - 4.5, c[0] + 4.5, c[1] + 4.5], |
| 600 | radius: 4.5, |
| 601 | stroke: Some(1.4), |
| 602 | color: col, |
| 603 | }); |
| 604 | } |
| 605 | |
| 606 | fn push_handle_dot(out: &mut Vec<Primitive<'_>>, p: [f32; 2], r: f32, col: [f32; 4], hot: bool) { |
| 607 | let rad = if hot { r + 1.5 } else { r }; |
| 608 | out.push(Primitive::RoundedRect { |
| 609 | rect: [p[0] - rad, p[1] - rad, p[0] + rad, p[1] + rad], |
| 610 | radius: rad, |
| 611 | stroke: None, |
| 612 | color: if hot { [1.0, 1.0, 1.0, 1.0] } else { col }, |
| 613 | }); |
| 614 | out.push(Primitive::RoundedRect { |
| 615 | rect: [p[0] - rad, p[1] - rad, p[0] + rad, p[1] + rad], |
| 616 | radius: rad, |
| 617 | stroke: Some(1.1), |
| 618 | color: [0.05, 0.05, 0.08, 0.9], |
| 619 | }); |
| 620 | } |
| 621 | |
| 622 | fn push_handle_square(out: &mut Vec<Primitive<'_>>, p: [f32; 2], r: f32, col: [f32; 4], hot: bool) { |
| 623 | let rad = if hot { r + 1.4 } else { r }; |
| 624 | out.push(Primitive::RoundedRect { |
| 625 | rect: [p[0] - rad, p[1] - rad, p[0] + rad, p[1] + rad], |
| 626 | radius: 1.5, |
| 627 | stroke: None, |
| 628 | color: if hot { [1.0, 1.0, 1.0, 1.0] } else { col }, |
| 629 | }); |
| 630 | out.push(Primitive::RoundedRect { |
| 631 | rect: [p[0] - rad, p[1] - rad, p[0] + rad, p[1] + rad], |
| 632 | radius: 1.5, |
| 633 | stroke: Some(1.1), |
| 634 | color: [0.05, 0.05, 0.08, 0.9], |
| 635 | }); |
| 636 | } |
| 637 | |
| 638 | fn hypot2(a: [f32; 2], b: [f32; 2]) -> f32 { |
| 639 | ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2)).sqrt() |
| 640 | } |
| 641 | |
| 642 | #[cfg(test)] |
| 643 | mod tests { |
| 644 | use super::*; |
| 645 | |
| 646 | fn sample_gizmo_comp() -> Comp { |
| 647 | let src = r#"Composition { |
| 648 | CurrentTime = 0, |
| 649 | RenderRange = { 0, 100 }, |
| 650 | GlobalRange = { 0, 100 }, |
| 651 | Tools = ordered() { |
| 652 | XF1 = Transform { |
| 653 | Inputs = { |
| 654 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 655 | Size = Input { Value = 1.0, }, |
| 656 | Angle = Input { Value = 0.0, }, |
| 657 | }, |
| 658 | }, |
| 659 | Ell1 = EllipseMask { |
| 660 | Inputs = { |
| 661 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 662 | Width = Input { Value = 0.4, }, |
| 663 | Height = Input { Value = 0.2, }, |
| 664 | Angle = Input { Value = 0.0, }, |
| 665 | }, |
| 666 | }, |
| 667 | Rect1 = RectangleMask { |
| 668 | Inputs = { |
| 669 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 670 | Width = Input { Value = 0.5, }, |
| 671 | Height = Input { Value = 0.5, }, |
| 672 | }, |
| 673 | }, |
| 674 | }, |
| 675 | }"#; |
| 676 | Comp::from_fusion_str(src).expect("parse gizmo test comp") |
| 677 | } |
| 678 | |
| 679 | #[test] |
| 680 | fn fu_to_local_roundtrip_is_exact() { |
| 681 | let rect = [100.0, 50.0, 900.0, 500.0]; // 800x450 |
| 682 | let fu = [0.25, 0.75]; |
| 683 | let local = GizmoState::fu_to_local(fu, rect); |
| 684 | assert!((local[0] - 300.0).abs() < 1e-4); |
| 685 | assert!((local[1] - 162.5).abs() < 1e-4); |
| 686 | let back = GizmoState::local_to_fu(local, rect); |
| 687 | assert!((back[0] - fu[0]).abs() < 1e-5); |
| 688 | assert!((back[1] - fu[1]).abs() < 1e-5); |
| 689 | } |
| 690 | |
| 691 | #[test] |
| 692 | fn transform_gizmo_hit_and_drag_updates_center_and_size() { |
| 693 | let comp = sample_gizmo_comp(); |
| 694 | let xf = comp.find_node("XF1").unwrap(); |
| 695 | let rect = [0.0, 0.0, 800.0, 450.0]; |
| 696 | let mut gz = GizmoState::new(); |
| 697 | |
| 698 | // Hit center at (400, 225) |
| 699 | assert_eq!(gz.hit_test(&comp, xf, 0.0, rect, [400.0, 225.0]), Some(GizmoHandle::Center)); |
| 700 | assert!(gz.begin_drag(&comp, xf, 0.0, rect, [400.0, 225.0])); |
| 701 | let edit = gz.update_drag(&comp, rect, [480.0, 180.0], false).unwrap(); |
| 702 | assert_eq!(edit.input, "Center"); |
| 703 | if let ParamValue::Point([x, y]) = edit.value { |
| 704 | assert!((x - 0.6).abs() < 1e-4); |
| 705 | assert!((y - 0.6).abs() < 1e-4); |
| 706 | } else { |
| 707 | panic!("expected Point"); |
| 708 | } |
| 709 | gz.end_drag(); |
| 710 | |
| 711 | // Hit X-axis handle at (458, 225) |
| 712 | assert_eq!(gz.hit_test(&comp, xf, 0.0, rect, [458.0, 225.0]), Some(GizmoHandle::AxisX)); |
| 713 | assert!(gz.begin_drag(&comp, xf, 0.0, rect, [458.0, 225.0])); |
| 714 | let edit_x = gz.update_drag(&comp, rect, [538.0, 100.0], false).unwrap(); |
| 715 | if let ParamValue::Point([x, y]) = edit_x.value { |
| 716 | assert!((x - 0.6).abs() < 1e-4); |
| 717 | assert!((y - 0.5).abs() < 1e-4, "AxisX must keep Y locked"); |
| 718 | } else { |
| 719 | panic!("expected Point"); |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | #[test] |
| 724 | fn ellipse_and_rectangle_mask_gizmos_respect_fusion_units() { |
| 725 | let comp = sample_gizmo_comp(); |
| 726 | let ell = comp.find_node("Ell1").unwrap(); |
| 727 | let rect_node = comp.find_node("Rect1").unwrap(); |
| 728 | let img = [0.0, 0.0, 800.0, 400.0]; // 2:1 aspect |
| 729 | let mut gz = GizmoState::new(); |
| 730 | |
| 731 | // EllipseMask Height=0.2 in frame-width units -> half_h = 0.1 * 800 = 80px above center (400, 200) -> (400, 120) |
| 732 | assert_eq!(gz.hit_test(&comp, ell, 0.0, img, [400.0, 120.0]), Some(GizmoHandle::Height)); |
| 733 | assert!(gz.begin_drag(&comp, ell, 0.0, img, [400.0, 120.0])); |
| 734 | let edit = gz.update_drag(&comp, img, [400.0, 80.0], false).unwrap(); |
| 735 | assert_eq!(edit.input, "Height"); |
| 736 | if let ParamValue::Number(h) = edit.value { |
| 737 | assert!((h - 0.3).abs() < 1e-4, "expected 0.3, got {h}"); |
| 738 | } else { |
| 739 | panic!("expected Number"); |
| 740 | } |
| 741 | gz.end_drag(); |
| 742 | |
| 743 | // RectangleMask Height=0.5 in frame-height units -> half_h = 0.25 * 400 = 100px above center (400, 200) -> (400, 100) |
| 744 | assert_eq!(gz.hit_test(&comp, rect_node, 0.0, img, [400.0, 100.0]), Some(GizmoHandle::Height)); |
| 745 | } |
| 746 | |
| 747 | const ROTATED_COMP: &str = r#"Composition { |
| 748 | Tools = ordered() { |
| 749 | XF1 = Transform { |
| 750 | Inputs = { |
| 751 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 752 | Size = Input { Value = 1.0, }, |
| 753 | Angle = Input { Value = 90.0, }, |
| 754 | }, |
| 755 | }, |
| 756 | Ell1 = EllipseMask { |
| 757 | Inputs = { |
| 758 | Center = Input { Value = { 0.5, 0.5 }, }, |
| 759 | Width = Input { Value = 0.4, }, |
| 760 | Height = Input { Value = 0.2, }, |
| 761 | }, |
| 762 | }, |
| 763 | }, |
| 764 | }"#; |
| 765 | |
| 766 | #[test] |
| 767 | fn rotated_axis_handles_move_only_along_their_rotated_axis() { |
| 768 | let comp = Comp::from_fusion_str(ROTATED_COMP).expect("rotated comp"); |
| 769 | let xf = comp.find_node("XF1").unwrap(); |
| 770 | let rect = [0.0, 0.0, 800.0, 450.0]; |
| 771 | let mut gz = GizmoState::new(); |
| 772 | |
| 773 | // At 90deg the X axis points up the screen: 58px above the center (400, 225). |
| 774 | assert_eq!(gz.hit_test(&comp, xf, 0.0, rect, [400.0, 167.0]), Some(GizmoHandle::AxisX)); |
| 775 | assert!(gz.begin_drag(&comp, xf, 0.0, rect, [400.0, 167.0])); |
| 776 | let edit = gz.update_drag(&comp, rect, [430.0, 127.0], false).unwrap(); |
| 777 | let ParamValue::Point([x, y]) = edit.value else { panic!("expected Point") }; |
| 778 | assert!((x - 0.5).abs() < 1e-4, "perpendicular motion must be ignored, x = {x}"); |
| 779 | assert!((y - (0.5 + 40.0 / 450.0)).abs() < 1e-4, "y = {y}"); |
| 780 | gz.end_drag(); |
| 781 | |
| 782 | // The Y axis points left at 90deg: 58px left of center. |
| 783 | assert_eq!(gz.hit_test(&comp, xf, 0.0, rect, [342.0, 225.0]), Some(GizmoHandle::AxisY)); |
| 784 | assert!(gz.begin_drag(&comp, xf, 0.0, rect, [342.0, 225.0])); |
| 785 | let edit = gz.update_drag(&comp, rect, [322.0, 235.0], false).unwrap(); |
| 786 | let ParamValue::Point([x, y]) = edit.value else { panic!("expected Point") }; |
| 787 | assert!((x - (0.5 - 20.0 / 800.0)).abs() < 1e-4, "x = {x}"); |
| 788 | assert!((y - 0.5).abs() < 1e-4, "y = {y}"); |
| 789 | } |
| 790 | |
| 791 | #[test] |
| 792 | fn fine_mode_scales_mask_width_toward_the_start_value() { |
| 793 | let comp = Comp::from_fusion_str(ROTATED_COMP).expect("comp"); |
| 794 | let ell = comp.find_node("Ell1").unwrap(); |
| 795 | let rect = [0.0, 0.0, 800.0, 400.0]; |
| 796 | let mut gz = GizmoState::new(); |
| 797 | |
| 798 | assert!(gz.begin_drag(&comp, ell, 0.0, rect, [560.0, 200.0])); |
| 799 | let full = gz.update_drag(&comp, rect, [600.0, 200.0], false).unwrap(); |
| 800 | let fine = gz.update_drag(&comp, rect, [600.0, 200.0], true).unwrap(); |
| 801 | let (ParamValue::Number(full), ParamValue::Number(fine)) = (full.value, fine.value) else { panic!("expected Number") }; |
| 802 | assert!((full - 0.5).abs() < 1e-4, "full = {full}"); |
| 803 | assert!((fine - 0.42).abs() < 1e-4, "fine = {fine}"); |
| 804 | } |
| 805 | |
| 806 | #[test] |
| 807 | fn polyline_rotation_is_counter_clockwise_on_screen() { |
| 808 | let r = rotate_about([110.0, 50.0], [100.0, 50.0], 90.0_f32.to_radians()); |
| 809 | assert!((r[0] - 100.0).abs() < 1e-4 && (r[1] - 40.0).abs() < 1e-4, "{r:?}"); |
| 810 | } |
| 811 | } |