| 1 | use super::*; |
| 2 | |
| 3 | fn make_layout(s: &str, font: &text::Font) -> text::Layout { |
| 4 | let styles = [text::Style::new(font.clone())]; |
| 5 | text::Paragraph { |
| 6 | text: s, |
| 7 | styles: &styles, |
| 8 | spans: &[], |
| 9 | replacements: &[], |
| 10 | width: None, |
| 11 | indent: 0.0, |
| 12 | line_height: 1.2, |
| 13 | align: text::Align::Left, |
| 14 | tab: 32.0, |
| 15 | } |
| 16 | .layout() |
| 17 | } |
| 18 | |
| 19 | impl FlowView { |
| 20 | pub fn prepare_labels(&mut self, comp: &Comp) { |
| 21 | self.sanitize_stale(comp); |
| 22 | let mut label_font: Option<text::Font> = None; |
| 23 | let mut badge_font: Option<text::Font> = None; |
| 24 | let mut tiny_font: Option<text::Font> = None; |
| 25 | let mut port_font: Option<text::Font> = None; |
| 26 | |
| 27 | if self.label_cache.len() > comp.nodes.len() + LABEL_CACHE_SLACK { |
| 28 | self.label_cache.clear(); |
| 29 | } |
| 30 | if self.badge_cache.len() > 512 { |
| 31 | self.badge_cache.clear(); |
| 32 | } |
| 33 | for n in &comp.nodes { |
| 34 | if !self.label_cache.contains_key(n.name.as_str()) { |
| 35 | let font = label_font.get_or_insert_with(|| text::Font { |
| 36 | families: Arc::from([text::Family::Sans]), |
| 37 | size: 9.5, |
| 38 | weight: 500, |
| 39 | italic: false, |
| 40 | }); |
| 41 | self.label_cache.insert(n.name.clone(), make_layout(&n.name, font)); |
| 42 | } |
| 43 | } |
| 44 | for key in ["preserved", "cycle!"] { |
| 45 | if !self.badge_cache.contains_key(key) { |
| 46 | let font = tiny_font.get_or_insert_with(|| text::Font { |
| 47 | families: Arc::from([text::Family::Mono]), |
| 48 | size: 7.5, |
| 49 | weight: 600, |
| 50 | italic: false, |
| 51 | }); |
| 52 | self.badge_cache.insert(key.to_owned(), make_layout(key, font)); |
| 53 | } |
| 54 | } |
| 55 | let wanted = [ |
| 56 | self.hovered_handle.as_ref().map(|h| h.port.as_deref().unwrap_or("Output")), |
| 57 | self.wire_drag.as_ref().and_then(|wd| wd.target.as_ref()).map(|t| t.port.as_str()), |
| 58 | ]; |
| 59 | for name in wanted.into_iter().flatten() { |
| 60 | if !self.badge_cache.contains_key(name) { |
| 61 | let font = port_font.get_or_insert_with(|| text::Font { |
| 62 | families: Arc::from([text::Family::Sans]), |
| 63 | size: 9.0, |
| 64 | weight: 500, |
| 65 | italic: false, |
| 66 | }); |
| 67 | self.badge_cache.insert(name.to_owned(), make_layout(name, font)); |
| 68 | } |
| 69 | } |
| 70 | for key in [ |
| 71 | HUD_ROOT, |
| 72 | HUD_GROUP, |
| 73 | "GRAB [LMB/Enter confirm, RMB/Esc cancel, X/Y axis, Shift: Splice]", |
| 74 | "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", |
| 75 | "SCALE [LMB/Enter confirm, RMB/Esc cancel, X/Y axis]", |
| 76 | ] { |
| 77 | if !self.badge_cache.contains_key(key) { |
| 78 | let font = badge_font.get_or_insert_with(|| text::Font { |
| 79 | families: Arc::from([text::Family::Mono]), |
| 80 | size: 10.0, |
| 81 | weight: 500, |
| 82 | italic: false, |
| 83 | }); |
| 84 | self.badge_cache.insert(key.to_owned(), make_layout(key, font)); |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | pub fn paint<'a>(&'a self, comp: &'a Comp) -> Vec<Primitive<'a>> { |
| 90 | let mut out = Vec::with_capacity(comp.nodes.len().min(64) * 10 + 128); |
| 91 | let w = if self.size[0].is_finite() { self.size[0].max(1.0) } else { 1.0 }; |
| 92 | let h = if self.size[1].is_finite() { self.size[1].max(1.0) } else { 1.0 }; |
| 93 | let zoom = if self.zoom.is_finite() && self.zoom > 0.0 { self.zoom.clamp(0.12, 3.5) } else { 1.0 }; |
| 94 | |
| 95 | // 1. Subtle architectural grid aligned to Fusion's 110×33 flow cells |
| 96 | let mult = if zoom < 0.35 { 4.0 } else if zoom < 0.70 { 2.0 } else { 1.0 }; |
| 97 | let step_x = 110.0 * mult; |
| 98 | let step_y = 33.0 * mult * 2.0; |
| 99 | let tl = self.local_to_world([0.0, 0.0]); |
| 100 | let br = self.local_to_world([w, h]); |
| 101 | if tl[0].is_finite() && tl[1].is_finite() && br[0].is_finite() && br[1].is_finite() && zoom == self.zoom { |
| 102 | let gx0 = (tl[0] / step_x).floor() as i32; |
| 103 | let gx1 = (br[0] / step_x).ceil() as i32; |
| 104 | let gy0 = (tl[1] / step_y).floor() as i32; |
| 105 | let gy1 = (br[1] / step_y).ceil() as i32; |
| 106 | if gx1.saturating_sub(gx0) <= 512 { |
| 107 | for gx in gx0..=gx1 { |
| 108 | let x = self.world_to_local([gx as f32 * step_x, 0.0])[0]; |
| 109 | let col = if gx % 4 == 0 { self.palette.grid_major } else { self.palette.grid_minor }; |
| 110 | out.push(Primitive::Segment { from: [x, 0.0], to: [x, h], width: 1.0, color: col }); |
| 111 | } |
| 112 | } |
| 113 | if gy1.saturating_sub(gy0) <= 512 { |
| 114 | for gy in gy0..=gy1 { |
| 115 | let y = self.world_to_local([0.0, gy as f32 * step_y])[1]; |
| 116 | let col = if gy % 4 == 0 { self.palette.grid_major } else { self.palette.grid_minor }; |
| 117 | out.push(Primitive::Segment { from: [0.0, y], to: [w, y], width: 1.0, color: col }); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // 2. Modal Blender axis-lock guide lines |
| 123 | if let Some(m) = &self.modal { |
| 124 | let piv = self.world_to_local(m.pivot_world); |
| 125 | match m.axis { |
| 126 | AxisLock::X => out.push(Primitive::Segment { |
| 127 | from: [0.0, piv[1]], |
| 128 | to: [w, piv[1]], |
| 129 | width: 1.5, |
| 130 | color: [0.95, 0.28, 0.34, 0.85], |
| 131 | }), |
| 132 | AxisLock::Y => out.push(Primitive::Segment { |
| 133 | from: [piv[0], 0.0], |
| 134 | to: [piv[0], h], |
| 135 | width: 1.5, |
| 136 | color: [0.28, 0.90, 0.42, 0.85], |
| 137 | }), |
| 138 | AxisLock::None => {} |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // 3. Output squares (idle gray; wires overpaint connected ones white), straight Fusion wires |
| 143 | // and role-coloured input triangles. |
| 144 | let handles = self.zoom >= ports::HANDLE_MIN_ZOOM; |
| 145 | let m = ports::Metrics::at(self.zoom); |
| 146 | let large = ports::Metrics::large(self.zoom); |
| 147 | let on_screen = |r: [f32; 4]| r[2] >= -24.0 && r[0] <= w + 24.0 && r[3] >= -24.0 && r[1] <= h + 24.0; |
| 148 | let out_hot = |id: NodeId| { |
| 149 | self.wire_drag.as_ref().is_some_and(|wd| wd.src == id) |
| 150 | || self.hovered_handle.as_ref().is_some_and(|hh| hh.node == id && hh.port.is_none()) |
| 151 | }; |
| 152 | let out_fill = |id: NodeId, connected: bool| { |
| 153 | if out_hot(id) { |
| 154 | [0.98, 0.84, 0.30, 1.0] |
| 155 | } else if connected { |
| 156 | [0.96, 0.97, 1.0, 1.0] |
| 157 | } else { |
| 158 | [0.42, 0.44, 0.50, 1.0] |
| 159 | } |
| 160 | }; |
| 161 | if handles { |
| 162 | for n in comp.nodes.iter().filter(|n| self.scope.shows(n)) { |
| 163 | let r = self.node_rect_local(n.pos); |
| 164 | if on_screen(r) { |
| 165 | push_square(&mut out, m.output_center(r), m.out_half, out_fill(n.id, false)); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | for dst in comp.nodes.iter().filter(|n| self.scope.shows(n)) { |
| 171 | let dr = self.node_rect_local(dst.pos); |
| 172 | let dst_vis = on_screen(dr); |
| 173 | if !dst_vis { |
| 174 | let any_wire_vis = dst.inputs.values().any(|b| { |
| 175 | let Some(src_name) = b.source_op.as_deref() else { return false }; |
| 176 | let Some(src_id) = self.scope.resolve(comp, src_name) else { return false }; |
| 177 | let sr = self.node_rect_local(comp.node(src_id).pos); |
| 178 | on_screen([sr[0].min(dr[0]), sr[1].min(dr[1]), sr[2].max(dr[2]), sr[3].max(dr[3])]) |
| 179 | }); |
| 180 | if !any_wire_vis { |
| 181 | continue; |
| 182 | } |
| 183 | } |
| 184 | self.for_each_input(comp, dst, |slot| { |
| 185 | if let Some(src) = slot.src { |
| 186 | let sr = self.node_rect_local(comp.node(src).pos); |
| 187 | let bbox = [sr[0].min(dr[0]), sr[1].min(dr[1]), sr[2].max(dr[2]), sr[3].max(dr[3])]; |
| 188 | if on_screen(bbox) { |
| 189 | let is_hot = self.hovered_wire.as_ref().is_some_and(|hw| hw.src == src && hw.dst == dst.id && hw.port == slot.port); |
| 190 | let is_sel = self.selected.contains(&src) || self.selected.contains(&dst.id); |
| 191 | let (color, width) = if is_hot { |
| 192 | ([0.98, 0.82, 0.24, 1.0], 2.4) |
| 193 | } else if is_sel { |
| 194 | ([0.92, 0.94, 0.98, 0.96], 1.6) |
| 195 | } else { |
| 196 | ([0.64, 0.66, 0.72, 0.76], 1.1) |
| 197 | }; |
| 198 | for seg in self.wire_route(sr, dr, &slot).points().windows(2) { |
| 199 | out.push(Primitive::Segment { from: seg[0], to: seg[1], width, color }); |
| 200 | } |
| 201 | if handles && on_screen(sr) { |
| 202 | push_square(&mut out, m.output_center(sr), m.out_half, out_fill(src, true)); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | if handles && dst_vis { |
| 207 | let target = self.wire_drag.as_ref().and_then(|wd| wd.target.as_ref()).filter(|t| t.node == dst.id && t.port == slot.port); |
| 208 | let hot = target.is_some() || self.hovered_handle.as_ref().is_some_and(|hh| hh.node == dst.id && hh.port.as_deref() == Some(slot.port)); |
| 209 | let tri = input_arrow_svg(slot.edge, large); |
| 210 | let fill = if slot.src.is_some() || hot { slot.color } else { [slot.color[0], slot.color[1], slot.color[2], 0.78] }; |
| 211 | out.push(Primitive::Path { data: tri, origin: slot.tip, style: PathStyle::Fill, color: fill }); |
| 212 | let (outline, outline_w) = match target { |
| 213 | Some(t) if t.cycle => ([0.96, 0.30, 0.34, 1.0], 1.6), |
| 214 | _ if hot => ([1.0, 1.0, 1.0, 1.0], 1.4), |
| 215 | _ => ([0.04, 0.04, 0.06, 0.92], 0.9), |
| 216 | }; |
| 217 | out.push(Primitive::Path { data: tri, origin: slot.tip, style: PathStyle::Stroke(outline_w), color: outline }); |
| 218 | } |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | // Rubber-band wire from the output square, tinted by the hovered input's role |
| 223 | if let Some(wd) = &self.wire_drag { |
| 224 | if wd.src.0 < comp.nodes.len() { |
| 225 | let a = m.output_tip(self.node_rect_local(comp.node(wd.src).pos)); |
| 226 | let slot = wd.target.as_ref().filter(|t| t.node.0 < comp.nodes.len()).and_then(|t| { |
| 227 | self.input_slot(comp, comp.node(t.node), &t.port) |
| 228 | }); |
| 229 | let is_cycle = wd.target.as_ref().is_some_and(|t| t.cycle); |
| 230 | let (end, end_normal) = match &slot { |
| 231 | Some(s) => (s.base(m), s.edge.normal()), |
| 232 | None => (wd.cursor_local, [-1.0, 0.0]), |
| 233 | }; |
| 234 | let wire_col = match &slot { |
| 235 | _ if is_cycle => [0.96, 0.30, 0.34, 1.0], |
| 236 | Some(s) => s.color, |
| 237 | None => [0.98, 0.84, 0.30, 1.0], |
| 238 | }; |
| 239 | const STEPS: usize = 16; |
| 240 | let dist = ((end[0] - a[0]).powi(2) + (end[1] - a[1]).powi(2)).sqrt(); |
| 241 | let handle = (dist * 0.42).clamp(24.0, 140.0); |
| 242 | let c1 = [a[0] + handle, a[1]]; |
| 243 | let c2 = [end[0] + end_normal[0] * handle * 0.65, end[1] + end_normal[1] * handle * 0.65]; |
| 244 | let width = if is_cycle { 2.4 } else { 2.0 }; |
| 245 | let mut prev = a; |
| 246 | for i in 1..=STEPS { |
| 247 | let t = i as f32 / STEPS as f32; |
| 248 | let u = 1.0 - t; |
| 249 | let u2 = u * u; |
| 250 | let t2 = t * t; |
| 251 | let cur = [ |
| 252 | u2 * u * a[0] + 3.0 * u2 * t * c1[0] + 3.0 * u * t2 * c2[0] + t2 * t * end[0], |
| 253 | u2 * u * a[1] + 3.0 * u2 * t * c1[1] + 3.0 * u * t2 * c2[1] + t2 * t * end[1], |
| 254 | ]; |
| 255 | out.push(Primitive::Segment { from: prev, to: cur, width, color: wire_col }); |
| 256 | prev = cur; |
| 257 | } |
| 258 | if is_cycle { |
| 259 | if let Some(badge) = self.badge_cache.get("cycle!") { |
| 260 | let bx = end[0] + 8.0; |
| 261 | let by = end[1] - 14.0; |
| 262 | out.push(Primitive::RoundedRect { |
| 263 | rect: [bx, by, bx + 36.0, by + 13.0], |
| 264 | radius: 3.0, |
| 265 | stroke: None, |
| 266 | color: [0.30, 0.06, 0.08, 0.94], |
| 267 | }); |
| 268 | out.push(Primitive::RoundedRect { |
| 269 | rect: [bx, by, bx + 36.0, by + 13.0], |
| 270 | radius: 3.0, |
| 271 | stroke: Some(1.0), |
| 272 | color: [0.96, 0.30, 0.34, 0.95], |
| 273 | }); |
| 274 | out.push(Primitive::Text { |
| 275 | text: badge as &dyn Glyphs, |
| 276 | origin: [bx + 5.0, by + 2.0], |
| 277 | clip: Some([bx, by, bx + 36.0, by + 13.0]), |
| 278 | ink: [1.0, 0.65, 0.65, 1.0], |
| 279 | }); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // 4. Sleek Thin Fusion-Style Node Bars (color line along BOTTOM!) |
| 286 | for n in comp.nodes.iter().filter(|n| self.scope.shows(n)) { |
| 287 | let r = self.node_rect_local(n.pos); |
| 288 | if !on_screen(r) { |
| 289 | continue; |
| 290 | } |
| 291 | let sel = self.selected.contains(&n.id); |
| 292 | let primary = self.primary == Some(n.id); |
| 293 | let hovered = self.hovered_node == Some(n.id); |
| 294 | let v1 = self.viewed_nodes[0] == Some(n.id); |
| 295 | let v2 = self.viewed_nodes[1] == Some(n.id); |
| 296 | let supported = is_supported_tool(&n.kind); |
| 297 | let accent = if supported { |
| 298 | node_accent(&n.kind) |
| 299 | } else { |
| 300 | [0.92, 0.64, 0.24, 1.0] |
| 301 | }; |
| 302 | |
| 303 | let bg = if n.bypassed { |
| 304 | [self.palette.node_fill[0] * 0.6, self.palette.node_fill[1] * 0.6, self.palette.node_fill[2] * 0.6, 0.65] |
| 305 | } else if sel { |
| 306 | self.palette.node_selected |
| 307 | } else if hovered { |
| 308 | self.palette.node_hover |
| 309 | } else if !supported { |
| 310 | [ |
| 311 | (self.palette.node_fill[0] + 0.035).min(1.0), |
| 312 | (self.palette.node_fill[1] + 0.020).min(1.0), |
| 313 | self.palette.node_fill[2], |
| 314 | 1.0, |
| 315 | ] |
| 316 | } else { |
| 317 | self.palette.node_fill |
| 318 | }; |
| 319 | |
| 320 | let radius = (3.5 * self.zoom).clamp(1.5, 5.0); |
| 321 | out.push(Primitive::RoundedRect { rect: r, radius, stroke: None, color: bg }); |
| 322 | |
| 323 | // Bottom category color line (exact Fusion look — thin sleek strip on bottom edge!) |
| 324 | let bar_h = (2.6 * self.zoom).clamp(1.4, 3.4); |
| 325 | out.push(Primitive::Rect { |
| 326 | rect: [r[0] + 1.5, (r[3] - bar_h).max(r[1]), r[2] - 1.5, r[3]], |
| 327 | color: if n.bypassed { [0.28, 0.28, 0.32, 0.6] } else { accent }, |
| 328 | }); |
| 329 | |
| 330 | // Crisp border (amber accent for preserved/passthrough unknown nodes) |
| 331 | let border_col = if primary { |
| 332 | [0.98, 0.78, 0.30, 1.0] |
| 333 | } else if sel { |
| 334 | self.palette.accent |
| 335 | } else if !supported { |
| 336 | [0.92, 0.64, 0.24, 0.85] |
| 337 | } else { |
| 338 | self.palette.node_border |
| 339 | }; |
| 340 | out.push(Primitive::RoundedRect { |
| 341 | rect: r, |
| 342 | radius, |
| 343 | stroke: Some(if sel || primary { 1.4 } else if !supported { 1.15 } else { 0.9 }), |
| 344 | color: border_col, |
| 345 | }); |
| 346 | // Subtle top dashed ticks for preserved/passthrough unknown nodes |
| 347 | if !supported && self.zoom >= 0.45 { |
| 348 | let mut tx = r[0] + 5.0; |
| 349 | while tx + 4.0 < r[2] - 4.0 { |
| 350 | out.push(Primitive::Segment { |
| 351 | from: [tx, r[1] + 0.8], |
| 352 | to: [(tx + 4.0).min(r[2] - 4.0), r[1] + 0.8], |
| 353 | width: 1.2, |
| 354 | color: [0.96, 0.72, 0.30, 0.90], |
| 355 | }); |
| 356 | tx += 8.0; |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | // Node label centered cleanly inside thin bar |
| 361 | if self.zoom >= 0.42 { |
| 362 | if let Some(layout) = self.label_cache.get(&n.name) { |
| 363 | let ink = if n.bypassed { self.palette.text_dim } else { self.palette.text }; |
| 364 | let pad_x = (6.0 * self.zoom).clamp(3.0, 8.0); |
| 365 | let text_y = (r[1] + r[3] - bar_h) * 0.5 - 6.0; |
| 366 | out.push(Primitive::Text { |
| 367 | text: layout as &dyn Glyphs, |
| 368 | origin: [r[0] + pad_x, text_y], |
| 369 | clip: Some([r[0] + pad_x, r[1] + 1.0, r[2] - 4.0, r[3] - bar_h]), |
| 370 | ink, |
| 371 | }); |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | // Preserved/passthrough pill badge underneath bottom-right for unknown/OFX nodes |
| 376 | if !supported && self.zoom >= 0.50 { |
| 377 | if let Some(badge) = self.badge_cache.get("preserved") { |
| 378 | let bw = 46.0; |
| 379 | let bh = 10.5; |
| 380 | let bx = (r[2] - bw).max(r[0] + 28.0); |
| 381 | let by = r[3] + 1.5; |
| 382 | out.push(Primitive::RoundedRect { |
| 383 | rect: [bx, by, bx + bw, by + bh], |
| 384 | radius: 3.0, |
| 385 | stroke: None, |
| 386 | color: [0.14, 0.10, 0.05, 0.92], |
| 387 | }); |
| 388 | out.push(Primitive::RoundedRect { |
| 389 | rect: [bx, by, bx + bw, by + bh], |
| 390 | radius: 3.0, |
| 391 | stroke: Some(0.8), |
| 392 | color: [0.92, 0.64, 0.24, 0.72], |
| 393 | }); |
| 394 | out.push(Primitive::Text { |
| 395 | text: badge as &dyn Glyphs, |
| 396 | origin: [bx + 4.0, by + 1.0], |
| 397 | clip: Some([bx + 2.0, by, bx + bw - 2.0, by + bh]), |
| 398 | ink: [0.98, 0.78, 0.40, 1.0], |
| 399 | }); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | // Fusion's viewer pill tucked under the bottom-left: left dot = Viewer 1, right dot = Viewer 2. |
| 404 | if v1 || v2 { |
| 405 | let (px, py) = (r[0] + 4.0, r[3] + 1.0); |
| 406 | out.push(Primitive::RoundedRect { |
| 407 | rect: [px, py, px + 22.0, py + 9.0], |
| 408 | radius: 3.0, |
| 409 | stroke: None, |
| 410 | color: [0.02, 0.02, 0.03, 0.92], |
| 411 | }); |
| 412 | for (dot, lit, color) in [(px + 3.5, v1, [0.24, 0.90, 0.82, 1.0]), (px + 13.0, v2, [0.98, 0.68, 0.28, 1.0])] { |
| 413 | let rect = [dot, py + 2.0, dot + 5.0, py + 7.0]; |
| 414 | if lit { |
| 415 | out.push(Primitive::RoundedRect { rect, radius: 2.5, stroke: None, color }); |
| 416 | } else { |
| 417 | out.push(Primitive::RoundedRect { rect, radius: 2.5, stroke: Some(1.0), color: [0.46, 0.48, 0.55, 0.9] }); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | if n.bypassed { |
| 423 | let ym = (r[1] + r[3]) * 0.5; |
| 424 | out.push(Primitive::Segment { |
| 425 | from: [r[0] + 3.0, ym], |
| 426 | to: [r[2] - 3.0, ym], |
| 427 | width: 1.4, |
| 428 | color: [0.95, 0.35, 0.35, 0.85], |
| 429 | }); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | // Port-name label for the hovered handle or wire-drop target |
| 434 | let label = match (&self.wire_drag, &self.hovered_handle) { |
| 435 | (Some(wd), _) => wd.target.as_ref().filter(|t| t.node.0 < comp.nodes.len()).and_then(|t| { |
| 436 | self.input_slot(comp, comp.node(t.node), &t.port).map(|s| (t.port.as_str(), s.base(m), s.edge.normal())) |
| 437 | }), |
| 438 | (None, Some(hh)) if hh.node.0 < comp.nodes.len() && handles => match &hh.port { |
| 439 | None => Some(("Output", m.output_tip(self.node_rect_local(comp.node(hh.node).pos)), [1.0, 0.0])), |
| 440 | Some(port) => self |
| 441 | .input_slot(comp, comp.node(hh.node), port) |
| 442 | .map(|s| (port.as_str(), s.base(m), s.edge.normal())), |
| 443 | }, |
| 444 | _ => None, |
| 445 | }; |
| 446 | if let Some((name, at, normal)) = label { |
| 447 | if let Some(layout) = self.badge_cache.get(name) { |
| 448 | let (pw, ph, gap) = (layout.width() + 10.0, 14.0, 3.0); |
| 449 | let x0 = if normal[0] < 0.0 { at[0] - gap - pw } else if normal[0] > 0.0 { at[0] + gap } else { at[0] - pw * 0.5 }; |
| 450 | let y0 = if normal[1] < 0.0 { at[1] - gap - ph } else if normal[1] > 0.0 { at[1] + gap } else { at[1] - ph * 0.5 }; |
| 451 | let rect = [x0, y0, x0 + pw, y0 + ph]; |
| 452 | out.push(Primitive::RoundedRect { rect, radius: 3.0, stroke: None, color: [0.02, 0.02, 0.03, 0.94] }); |
| 453 | out.push(Primitive::RoundedRect { rect, radius: 3.0, stroke: Some(0.8), color: [0.46, 0.48, 0.55, 0.9] }); |
| 454 | out.push(Primitive::Text { |
| 455 | text: layout as &dyn Glyphs, |
| 456 | origin: [x0 + 5.0, y0 + (ph - layout.height()) * 0.5], |
| 457 | clip: Some(rect), |
| 458 | ink: [0.94, 0.95, 0.98, 1.0], |
| 459 | }); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | // 5. Box select marquee |
| 464 | if let Some(bs) = &self.box_select { |
| 465 | let x0 = bs.start_local[0].min(bs.cur_local[0]); |
| 466 | let x1 = bs.start_local[0].max(bs.cur_local[0]); |
| 467 | let y0 = bs.start_local[1].min(bs.cur_local[1]); |
| 468 | let y1 = bs.start_local[1].max(bs.cur_local[1]); |
| 469 | out.push(Primitive::RoundedRect { |
| 470 | rect: [x0, y0, x1, y1], |
| 471 | radius: 2.0, |
| 472 | stroke: None, |
| 473 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], |
| 474 | }); |
| 475 | out.push(Primitive::RoundedRect { |
| 476 | rect: [x0, y0, x1, y1], |
| 477 | radius: 2.0, |
| 478 | stroke: Some(1.2), |
| 479 | color: self.palette.accent, |
| 480 | }); |
| 481 | } |
| 482 | |
| 483 | // 6. Status HUD strip |
| 484 | let hud_key = match self.modal.as_ref().map(|m| m.mode) { |
| 485 | Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X/Y axis, Shift: Splice]", |
| 486 | Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", |
| 487 | Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X/Y axis]", |
| 488 | None if !self.scope.is_root() => HUD_GROUP, |
| 489 | None => HUD_ROOT, |
| 490 | }; |
| 491 | if let Some(l) = self.badge_cache.get(hud_key) { |
| 492 | let pill_h = 20.0; |
| 493 | out.push(Primitive::RoundedRect { |
| 494 | rect: [8.0, h - pill_h - 6.0, w - 8.0, h - 6.0], |
| 495 | radius: 4.0, |
| 496 | stroke: None, |
| 497 | color: [self.palette.bg[0] * 0.75, self.palette.bg[1] * 0.75, self.palette.bg[2] * 0.75, 0.86], |
| 498 | }); |
| 499 | out.push(Primitive::Text { |
| 500 | text: l as &dyn Glyphs, |
| 501 | origin: [14.0, h - pill_h - 2.5], |
| 502 | clip: Some([12.0, h - pill_h - 6.0, w - 12.0, h - 6.0]), |
| 503 | ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, |
| 504 | }); |
| 505 | } |
| 506 | |
| 507 | out |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /// Static SVG triangles pointing INTO the node edge (`origin` sits flush on the node border). |
| 512 | fn input_arrow_svg(edge: Edge, large: bool) -> &'static str { |
| 513 | match (edge, large) { |
| 514 | (Edge::Left, true) => "M 0 0 L -6.5 -3.6 L -6.5 3.6 Z", |
| 515 | (Edge::Left, false) => "M 0 0 L -4.5 -2.6 L -4.5 2.6 Z", |
| 516 | (Edge::Right, true) => "M 0 0 L 6.5 -3.6 L 6.5 3.6 Z", |
| 517 | (Edge::Right, false) => "M 0 0 L 4.5 -2.6 L 4.5 2.6 Z", |
| 518 | (Edge::Top, true) => "M 0 0 L -3.6 -6.5 L 3.6 -6.5 Z", |
| 519 | (Edge::Top, false) => "M 0 0 L -2.6 -4.5 L 2.6 -4.5 Z", |
| 520 | (Edge::Bottom, true) => "M 0 0 L -3.6 6.5 L 3.6 6.5 Z", |
| 521 | (Edge::Bottom, false) => "M 0 0 L -2.6 4.5 L 2.6 4.5 Z", |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | fn push_square(out: &mut Vec<Primitive<'_>>, c: [f32; 2], half: f32, fill: [f32; 4]) { |
| 526 | let rect = [c[0] - half, c[1] - half, c[0] + half, c[1] + half]; |
| 527 | out.push(Primitive::RoundedRect { rect, radius: 0.6, stroke: None, color: fill }); |
| 528 | out.push(Primitive::RoundedRect { rect, radius: 0.6, stroke: Some(0.9), color: [0.04, 0.04, 0.06, 0.92] }); |
| 529 | } |
| 530 | |
| 531 | fn node_accent(kind: &str) -> [f32; 4] { |
| 532 | match kind { |
| 533 | "Loader" | "Saver" => [0.32, 0.72, 0.92, 1.0], |
| 534 | "Merge" | "Switch" | "Fuse.Switch" => [0.58, 0.60, 0.66, 1.0], |
| 535 | "Background" | "FastNoise" => [0.36, 0.80, 0.42, 1.0], |
| 536 | "TextPlus" | "Text3D" => [0.34, 0.86, 0.54, 1.0], |
| 537 | "ColorCorrector" | "BrightnessContrast" | "ColorCurves" | "HueCurves" | "LumaKeyer" | "UltraKeyer" => { |
| 538 | [0.92, 0.58, 0.22, 1.0] |
| 539 | } |
| 540 | "Blur" | "DirectionalBlur" | "Glow" | "SoftGlow" | "Displace" | "ErodeDilate" => { |
| 541 | [0.92, 0.52, 0.24, 1.0] |
| 542 | } |
| 543 | "Transform" | "Scale" | "Crop" | "TimeStretcher" | "TimeSpeed" => [0.84, 0.66, 0.28, 1.0], |
| 544 | "EllipseMask" | "RectangleMask" | "PolylineMask" | "BitmapMask" => [0.32, 0.62, 0.98, 1.0], |
| 545 | k if k.starts_with("Fuse.") => [0.88, 0.44, 0.76, 1.0], |
| 546 | _ => [0.54, 0.54, 0.60, 1.0], |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | #[cfg(test)] |
| 551 | mod tests { |
| 552 | use super::*; |
| 553 | use std::time::Instant; |
| 554 | |
| 555 | #[test] |
| 556 | fn prepare_labels_steady_state_is_idempotent() { |
| 557 | let comp = Comp::from_fusion_str( |
| 558 | r#"Composition { |
| 559 | Tools = ordered() { |
| 560 | BG1 = Background { ViewInfo = OperatorInfo { Pos = { 0, 0 } } }, |
| 561 | Blur1 = Blur { |
| 562 | Inputs = { Input = Input { SourceOp = "BG1", Source = "Output" } }, |
| 563 | ViewInfo = OperatorInfo { Pos = { 200, 0 } }, |
| 564 | }, |
| 565 | } |
| 566 | }"#, |
| 567 | ) |
| 568 | .unwrap(); |
| 569 | let mut view = FlowView::new(&ui::Theme::dark()); |
| 570 | view.prepare_labels(&comp); |
| 571 | let label_ptrs: Vec<(*const text::Layout, usize)> = comp |
| 572 | .nodes |
| 573 | .iter() |
| 574 | .map(|n| (view.label_cache.get(&n.name).unwrap() as *const text::Layout, view.label_cache.len())) |
| 575 | .collect(); |
| 576 | let hud_ptr = view.badge_cache.get(HUD_ROOT).unwrap() as *const text::Layout; |
| 577 | let badge_count = view.badge_cache.len(); |
| 578 | |
| 579 | for _ in 0..10 { |
| 580 | view.prepare_labels(&comp); |
| 581 | } |
| 582 | for (i, n) in comp.nodes.iter().enumerate() { |
| 583 | let ptr = view.label_cache.get(&n.name).unwrap() as *const text::Layout; |
| 584 | assert_eq!(ptr, label_ptrs[i].0); |
| 585 | assert_eq!(view.label_cache.len(), label_ptrs[i].1); |
| 586 | } |
| 587 | assert_eq!(view.badge_cache.get(HUD_ROOT).unwrap() as *const text::Layout, hud_ptr); |
| 588 | assert_eq!(view.badge_cache.len(), badge_count); |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn paint_culls_offscreen_nodes_and_wires() { |
| 593 | let comp = Comp::from_fusion_str( |
| 594 | r#"Composition { |
| 595 | Tools = ordered() { |
| 596 | Left = Background { ViewInfo = OperatorInfo { Pos = { -2000, 0 } } }, |
| 597 | Right = Blur { |
| 598 | Inputs = { Input = Input { SourceOp = "Left", Source = "Output" } }, |
| 599 | ViewInfo = OperatorInfo { Pos = { 2000, 0 } }, |
| 600 | }, |
| 601 | FarA = Background { ViewInfo = OperatorInfo { Pos = { -3000, -3000 } } }, |
| 602 | FarB = Blur { |
| 603 | Inputs = { Input = Input { SourceOp = "FarA", Source = "Output" } }, |
| 604 | ViewInfo = OperatorInfo { Pos = { -2800, -3000 } }, |
| 605 | }, |
| 606 | } |
| 607 | }"#, |
| 608 | ) |
| 609 | .unwrap(); |
| 610 | let mut view = FlowView::new(&ui::Theme::dark()); |
| 611 | view.resize([800.0, 600.0]); |
| 612 | view.pan = [0.0, 0.0]; |
| 613 | view.zoom = 1.0; |
| 614 | view.prepare_labels(&comp); |
| 615 | |
| 616 | let prims = view.paint(&comp); |
| 617 | // No node bars (no text primitives for node names) should be emitted because all 4 nodes are off-screen. |
| 618 | let node_texts = prims |
| 619 | .iter() |
| 620 | .filter(|p| matches!(p, Primitive::Text { .. })) |
| 621 | .count(); |
| 622 | // Only the 1 HUD text should exist (no node labels). |
| 623 | assert_eq!(node_texts, 1); |
| 624 | let prims_len = prims.len(); |
| 625 | drop(prims); |
| 626 | |
| 627 | // Now pan far away from Left->Right too so zero wires cross the viewport. |
| 628 | view.pan = [0.0, 2000.0]; |
| 629 | let prims_empty = view.paint(&comp); |
| 630 | assert!(prims_empty.len() < prims_len, "wire crossing viewport should be drawn only when crossing"); |
| 631 | } |
| 632 | |
| 633 | #[test] |
| 634 | fn badge_cache_is_capped_across_many_port_names() { |
| 635 | let comp = Comp::from_fusion_str( |
| 636 | r#"Composition { |
| 637 | Tools = ordered() { |
| 638 | BG1 = Background { ViewInfo = OperatorInfo { Pos = { 0, 0 } } }, |
| 639 | } |
| 640 | }"#, |
| 641 | ) |
| 642 | .unwrap(); |
| 643 | let mut view = FlowView::new(&ui::Theme::dark()); |
| 644 | for i in 0..600 { |
| 645 | view.hovered_handle = Some(HandleId { |
| 646 | node: NodeId(0), |
| 647 | port: Some(format!("CustomPort{i}")), |
| 648 | }); |
| 649 | view.prepare_labels(&comp); |
| 650 | } |
| 651 | assert!(view.badge_cache.len() <= 513, "badge_cache grew unboundedly: {}", view.badge_cache.len()); |
| 652 | } |
| 653 | |
| 654 | #[test] |
| 655 | fn bench_flow_prepare_and_paint() { |
| 656 | let path = "/Volumes/Project/trees/fusion/30_ground_is_crazy001.comp"; |
| 657 | let comp = if std::path::Path::new(path).exists() { |
| 658 | Comp::from_fusion_str(&std::fs::read_to_string(path).unwrap()).unwrap() |
| 659 | } else { |
| 660 | let mut src = String::from("Composition { Tools = ordered() {\n"); |
| 661 | for i in 0..200 { |
| 662 | let x = (i % 20) * 140; |
| 663 | let y = (i / 20) * 60; |
| 664 | if i == 0 { |
| 665 | src.push_str(&format!("N{i} = Background {{ ViewInfo = OperatorInfo {{ Pos = {{ {x}, {y} }} }} }},\n")); |
| 666 | } else { |
| 667 | src.push_str(&format!("N{i} = Blur {{ Inputs = {{ Input = Input {{ SourceOp = \"N{}\", Source = \"Output\" }} }}, ViewInfo = OperatorInfo {{ Pos = {{ {x}, {y} }} }} }},\n", i - 1)); |
| 668 | } |
| 669 | } |
| 670 | src.push_str("} }"); |
| 671 | Comp::from_fusion_str(&src).unwrap() |
| 672 | }; |
| 673 | let mut view = FlowView::new(&ui::Theme::dark()); |
| 674 | view.resize([1280.0, 720.0]); |
| 675 | view.fit_comp(&comp); |
| 676 | // Warmup |
| 677 | view.prepare_labels(&comp); |
| 678 | std::hint::black_box(view.paint(&comp).len()); |
| 679 | |
| 680 | // Zoomed-in steady state (only part of the graph visible) |
| 681 | view.zoom = 1.0; |
| 682 | if let Some(n) = comp.nodes.first() { |
| 683 | view.pan = n.pos; |
| 684 | } |
| 685 | let frames = 500; |
| 686 | let t0 = Instant::now(); |
| 687 | let mut total_prims = 0usize; |
| 688 | for _ in 0..frames { |
| 689 | view.prepare_labels(&comp); |
| 690 | let prims = view.paint(&comp); |
| 691 | total_prims += prims.len(); |
| 692 | std::hint::black_box(prims.len()); |
| 693 | } |
| 694 | let dt = t0.elapsed(); |
| 695 | eprintln!( |
| 696 | "[BENCH flow] nodes={} frames={} total_ms={:.2} us_per_frame={:.2} avg_prims={}", |
| 697 | comp.nodes.len(), |
| 698 | frames, |
| 699 | dt.as_secs_f64() * 1000.0, |
| 700 | dt.as_secs_f64() * 1e6 / frames as f64, |
| 701 | total_prims / frames |
| 702 | ); |
| 703 | } |
| 704 | } |