| author | |
| committer | |
| log | 3619b5dd25149814756d1cfb3e63fc0918a31e7c |
| tree | 4293a243f053d6f1e4b0a7820c6f66348f4ce33a |
| parent | ebca4bd9b78139ca1e7cfd89ee7bf1b99629d8b2 |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
Assisted-by: claude-sonnet-5.512 files changed, 2863 insertions(+), 2819 deletions(-)
crates/app/src/keyframes.rs+2-379| ... | ... | @@ -1,6 +1,8 @@ |
| 1 | 1 | //! Docked Keyframes dope-sheet pane: animated node/channel tracks, audio waveform track, |
| 2 | 2 | //! green RAM cache bar, and interactive drag-to-retime keyframes with `CompHistory` undo. |
| 3 | 3 | |
| 4 | mod paint; | |
| 5 | ||
| 4 | 6 | use draw::{Glyphs, Primitive}; |
| 5 | 7 | use model::{Comp, CompHistory, Keyframe, NodeId}; |
| 6 | 8 | use std::collections::{HashMap, HashSet}; |
| ... | ... | @@ -507,377 +509,6 @@ impl KeyframesView { |
| 507 | 509 | _ => {} |
| 508 | 510 | } |
| 509 | 511 | } |
| 510 | ||
| 511 | pub fn prepare_labels(&mut self) { | |
| 512 | if self.text_cache.len() > TEXT_CACHE_CAP { | |
| 513 | self.text_cache.clear(); | |
| 514 | } | |
| 515 | let font_sm = text::Font { | |
| 516 | families: Arc::from([text::Family::Mono]), | |
| 517 | size: 10.0, | |
| 518 | weight: 500, | |
| 519 | italic: false, | |
| 520 | }; | |
| 521 | let font_row = text::Font { | |
| 522 | families: Arc::from([text::Family::Sans]), | |
| 523 | size: 11.0, | |
| 524 | weight: 500, | |
| 525 | italic: false, | |
| 526 | }; | |
| 527 | let font_hdr = text::Font { | |
| 528 | families: Arc::from([text::Family::Sans]), | |
| 529 | size: 11.0, | |
| 530 | weight: 600, | |
| 531 | italic: false, | |
| 532 | }; | |
| 533 | let make = |s: &str, font: &text::Font| { | |
| 534 | let styles = [text::Style::new(font.clone())]; | |
| 535 | text::Paragraph { | |
| 536 | text: s, | |
| 537 | styles: &styles, | |
| 538 | spans: &[], | |
| 539 | replacements: &[], | |
| 540 | width: None, | |
| 541 | indent: 0.0, | |
| 542 | line_height: 1.2, | |
| 543 | align: text::Align::Left, | |
| 544 | tab: 32.0, | |
| 545 | } | |
| 546 | .layout() | |
| 547 | }; | |
| 548 | ||
| 549 | for (k, txt, f) in [ | |
| 550 | ("hdr:title", "Keyframes", &font_hdr), | |
| 551 | ("hdr:all", "All", &font_sm), | |
| 552 | ("hdr:sel", "Sel", &font_sm), | |
| 553 | ("hdr:audio", "Audio", &font_row), | |
| 554 | ("footer", "Drag keys/spans to retime · Ruler: Scrub · Wheel: Zoom · Del: Remove · F: Fit", &font_sm), | |
| 555 | ] { | |
| 556 | self.text_cache.entry(k.to_owned()).or_insert_with(|| make(txt, f)); | |
| 557 | } | |
| 558 | ||
| 559 | for row in &self.rows { | |
| 560 | match row { | |
| 561 | TrackRow::NodeHeader { node_id, label, .. } => { | |
| 562 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 563 | let text = format!("{glyph} {label}"); | |
| 564 | self.text_cache.entry(format!("nh:{text}")).or_insert_with(|| make(&text, &font_hdr)); | |
| 565 | } | |
| 566 | TrackRow::Channel { label, .. } => { | |
| 567 | self.text_cache.entry(format!("ch:{label}")).or_insert_with(|| make(label, &font_row)); | |
| 568 | } | |
| 569 | } | |
| 570 | } | |
| 571 | ||
| 572 | let plot = self.plot_rect(); | |
| 573 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 574 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 575 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 576 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 577 | for ti in t_start..=t_end { | |
| 578 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); | |
| 579 | self.text_cache.entry(key.clone()).or_insert_with(|| make(&key[2..], &font_sm)); | |
| 580 | } | |
| 581 | } | |
| 582 | ||
| 583 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 584 | let mut out = Vec::with_capacity(512); | |
| 585 | let [w, h] = self.size; | |
| 586 | let plot = self.plot_rect(); | |
| 587 | let top = self.tracks_top(); | |
| 588 | ||
| 589 | // Sidebar + Ruler chrome | |
| 590 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); | |
| 591 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); | |
| 592 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); | |
| 593 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); | |
| 594 | ||
| 595 | if let Some(tl) = self.text_cache.get("hdr:title") { | |
| 596 | out.push(Primitive::Text { | |
| 597 | text: tl as &dyn Glyphs, | |
| 598 | origin: [10.0, 6.0], | |
| 599 | clip: Some([8.0, 0.0, SIDEBAR_W - 52.0, RULER_H]), | |
| 600 | ink: self.palette.text, | |
| 601 | }); | |
| 602 | } | |
| 603 | // "All / Sel" filter pill | |
| 604 | let pill_rect = [SIDEBAR_W - 46.0, 4.0, SIDEBAR_W - 8.0, RULER_H - 4.0]; | |
| 605 | out.push(Primitive::RoundedRect { | |
| 606 | rect: pill_rect, | |
| 607 | radius: 4.0, | |
| 608 | stroke: None, | |
| 609 | color: if self.selected_only { | |
| 610 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] | |
| 611 | } else { | |
| 612 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.7] | |
| 613 | }, | |
| 614 | }); | |
| 615 | out.push(Primitive::RoundedRect { | |
| 616 | rect: pill_rect, | |
| 617 | radius: 4.0, | |
| 618 | stroke: Some(1.0), | |
| 619 | color: if self.selected_only { self.palette.accent } else { self.palette.rule }, | |
| 620 | }); | |
| 621 | let mode_key = if self.selected_only { "hdr:sel" } else { "hdr:all" }; | |
| 622 | if let Some(ml) = self.text_cache.get(mode_key) { | |
| 623 | out.push(Primitive::Text { | |
| 624 | text: ml as &dyn Glyphs, | |
| 625 | origin: [pill_rect[0] + 9.0, 7.0], | |
| 626 | clip: Some(pill_rect), | |
| 627 | ink: if self.selected_only { self.palette.text } else { self.palette.text_dim }, | |
| 628 | }); | |
| 629 | } | |
| 630 | ||
| 631 | // Ruler frame ticks | |
| 632 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 633 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 634 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 635 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 636 | for ti in t_start..=t_end { | |
| 637 | let t = ti as f64 * t_step; | |
| 638 | let x = self.time_to_x(t); | |
| 639 | if x < plot[0] || x > plot[2] { | |
| 640 | continue; | |
| 641 | } | |
| 642 | out.push(Primitive::Segment { from: [x, RULER_H], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); | |
| 643 | let key = format!("t:{}", t.round() as i64); | |
| 644 | if let Some(tl) = self.text_cache.get(&key) { | |
| 645 | out.push(Primitive::Text { | |
| 646 | text: tl as &dyn Glyphs, | |
| 647 | origin: [x + 3.0, 5.0], | |
| 648 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), | |
| 649 | ink: self.palette.text_dim, | |
| 650 | }); | |
| 651 | } | |
| 652 | } | |
| 653 | ||
| 654 | // Green RAM cache bar along bottom edge of ruler | |
| 655 | for &(c0, c1) in &self.cached_ranges { | |
| 656 | let x0 = self.time_to_x(c0 as f64).max(plot[0]); | |
| 657 | let x1 = self.time_to_x((c1 + 1) as f64).min(plot[2]); | |
| 658 | if x1 > x0 { | |
| 659 | out.push(Primitive::Rect { | |
| 660 | rect: [x0, RULER_H - 3.5, x1.max(x0 + 2.0), RULER_H - 0.5], | |
| 661 | color: [0.22, 0.86, 0.48, 0.92], | |
| 662 | }); | |
| 663 | } | |
| 664 | } | |
| 665 | ||
| 666 | // Dedicated Audio Waveform row when audio is present | |
| 667 | if !self.audio_peaks.is_empty() { | |
| 668 | let ay0 = RULER_H; | |
| 669 | let ay1 = RULER_H + AUDIO_ROW_H; | |
| 670 | out.push(Primitive::Rect { rect: [0.0, ay0, w, ay1], color: self.palette.row_alt }); | |
| 671 | out.push(Primitive::Segment { from: [0.0, ay1], to: [w, ay1], width: 1.0, color: self.palette.rule }); | |
| 672 | if let Some(al) = self.text_cache.get("hdr:audio") { | |
| 673 | out.push(Primitive::Text { | |
| 674 | text: al as &dyn Glyphs, | |
| 675 | origin: [12.0, ay0 + 5.0], | |
| 676 | clip: Some([8.0, ay0, SIDEBAR_W - 8.0, ay1]), | |
| 677 | ink: [0.54, 0.80, 1.0, 0.95], | |
| 678 | }); | |
| 679 | } | |
| 680 | out.push(Primitive::PushClip { rect: [plot[0], ay0, plot[2], ay1], radius: 0.0 }); | |
| 681 | let mid_y = 0.5 * (ay0 + ay1); | |
| 682 | let max_half = AUDIO_ROW_H * 0.42; | |
| 683 | let a0 = self.audio_range[0]; | |
| 684 | let n = self.audio_peaks.len() as i64; | |
| 685 | let f_vis0 = (self.t_range[0].floor() as i64).max(a0); | |
| 686 | let f_vis1 = (self.t_range[1].ceil() as i64).min(a0 + n - 1); | |
| 687 | let bar_step = ((plot[2] - plot[0]) / ((self.t_range[1] - self.t_range[0]).max(1.0) as f32)).max(1.5); | |
| 688 | let stride = ((1.8 / bar_step).ceil() as i64).max(1); | |
| 689 | let mut f = f_vis0; | |
| 690 | while f <= f_vis1 { | |
| 691 | let mut peak = 0.0_f32; | |
| 692 | for off in 0..stride { | |
| 693 | let idx = (f + off - a0) as usize; | |
| 694 | if let Some(&v) = self.audio_peaks.get(idx) { | |
| 695 | if v > peak { peak = v; } | |
| 696 | } | |
| 697 | } | |
| 698 | if peak > 0.01 { | |
| 699 | let x = self.time_to_x(f as f64); | |
| 700 | let hh = (peak * max_half).max(1.0); | |
| 701 | let bw = (bar_step * stride as f32 - 0.6).max(1.2); | |
| 702 | out.push(Primitive::Rect { | |
| 703 | rect: [x, mid_y - hh, x + bw, mid_y + hh], | |
| 704 | color: [0.46, 0.74, 0.98, 0.55], | |
| 705 | }); | |
| 706 | } | |
| 707 | f += stride; | |
| 708 | } | |
| 709 | out.push(Primitive::PopClip); | |
| 710 | } | |
| 711 | ||
| 712 | // Render range subtle shading | |
| 713 | let rx0 = self.time_to_x(comp.render_range[0] as f64); | |
| 714 | let rx1 = self.time_to_x(comp.render_range[1] as f64); | |
| 715 | out.push(Primitive::PushClip { rect: [plot[0], top, plot[2], plot[3]], radius: 0.0 }); | |
| 716 | if rx0 > plot[0] { | |
| 717 | out.push(Primitive::Rect { rect: [plot[0], top, rx0.min(plot[2]), plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 718 | } | |
| 719 | if rx1 < plot[2] { | |
| 720 | out.push(Primitive::Rect { rect: [rx1.max(plot[0]), top, plot[2], plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 721 | } | |
| 722 | out.push(Primitive::PopClip); | |
| 723 | ||
| 724 | // Track rows (clipped to track area) | |
| 725 | out.push(Primitive::PushClip { rect: [0.0, top, w, plot[3]], radius: 0.0 }); | |
| 726 | for (ri, row) in self.rows.iter().enumerate() { | |
| 727 | let y0 = self.row_y(ri); | |
| 728 | let y1 = y0 + ROW_H; | |
| 729 | if y1 < top || y0 > plot[3] { | |
| 730 | continue; | |
| 731 | } | |
| 732 | if ri % 2 == 1 { | |
| 733 | out.push(Primitive::Rect { rect: [0.0, y0, w, y1], color: self.palette.row_alt }); | |
| 734 | } | |
| 735 | out.push(Primitive::Segment { from: [0.0, y1], to: [w, y1], width: 1.0, color: self.palette.grid }); | |
| 736 | ||
| 737 | match row { | |
| 738 | TrackRow::NodeHeader { node_id, label, splines } => { | |
| 739 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 740 | let key = format!("nh:{glyph} {label}"); | |
| 741 | if let Some(tl) = self.text_cache.get(&key) { | |
| 742 | out.push(Primitive::Text { | |
| 743 | text: tl as &dyn Glyphs, | |
| 744 | origin: [10.0, y0 + 4.0], | |
| 745 | clip: Some([6.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 746 | ink: self.palette.text, | |
| 747 | }); | |
| 748 | } | |
| 749 | // Summary span + tick dots on node header row | |
| 750 | let mut t_min = f64::INFINITY; | |
| 751 | let mut t_max = f64::NEG_INFINITY; | |
| 752 | for sname in splines { | |
| 753 | if let Some(sp) = comp.splines.get(sname) { | |
| 754 | for kf in &sp.keyframes { | |
| 755 | t_min = t_min.min(kf.time); | |
| 756 | t_max = t_max.max(kf.time); | |
| 757 | } | |
| 758 | } | |
| 759 | } | |
| 760 | if t_min.is_finite() { | |
| 761 | let sx0 = self.time_to_x(t_min).max(plot[0]); | |
| 762 | let sx1 = self.time_to_x(t_max).min(plot[2]); | |
| 763 | if sx1 >= sx0 { | |
| 764 | out.push(Primitive::RoundedRect { | |
| 765 | rect: [sx0, y0 + 6.0, sx1.max(sx0 + 4.0), y1 - 6.0], | |
| 766 | radius: 3.0, | |
| 767 | stroke: None, | |
| 768 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.22], | |
| 769 | }); | |
| 770 | } | |
| 771 | } | |
| 772 | } | |
| 773 | TrackRow::Channel { label, spline, color, .. } => { | |
| 774 | out.push(Primitive::RoundedRect { | |
| 775 | rect: [20.0, y0 + 7.0, 28.0, y0 + 15.0], | |
| 776 | radius: 2.0, | |
| 777 | stroke: None, | |
| 778 | color: *color, | |
| 779 | }); | |
| 780 | if let Some(tl) = self.text_cache.get(&format!("ch:{label}")) { | |
| 781 | out.push(Primitive::Text { | |
| 782 | text: tl as &dyn Glyphs, | |
| 783 | origin: [34.0, y0 + 4.0], | |
| 784 | clip: Some([32.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 785 | ink: self.palette.text_dim, | |
| 786 | }); | |
| 787 | } | |
| 788 | ||
| 789 | if let Some(sp) = comp.splines.get(spline) { | |
| 790 | if sp.keyframes.len() >= 2 { | |
| 791 | let sx0 = self.time_to_x(sp.keyframes.first().unwrap().time).max(plot[0]); | |
| 792 | let sx1 = self.time_to_x(sp.keyframes.last().unwrap().time).min(plot[2]); | |
| 793 | if sx1 > sx0 { | |
| 794 | out.push(Primitive::RoundedRect { | |
| 795 | rect: [sx0, y0 + 8.0, sx1, y1 - 8.0], | |
| 796 | radius: 2.0, | |
| 797 | stroke: None, | |
| 798 | color: [color[0], color[1], color[2], 0.28], | |
| 799 | }); | |
| 800 | } | |
| 801 | } | |
| 802 | let cy = y0 + ROW_H * 0.5; | |
| 803 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 804 | let kx = self.time_to_x(kf.time); | |
| 805 | if kx < plot[0] - 8.0 || kx > plot[2] + 8.0 { | |
| 806 | continue; | |
| 807 | } | |
| 808 | let kref = DopeKeyRef { spline: spline.clone(), key_idx: ki }; | |
| 809 | let sel = self.selected_keys.contains(&kref); | |
| 810 | let hov = self.hovered_key.as_ref() == Some(&kref); | |
| 811 | let r = if sel || hov { 5.0 } else { 4.0 }; | |
| 812 | out.push(Primitive::RoundedRect { | |
| 813 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 814 | radius: 1.5, | |
| 815 | stroke: None, | |
| 816 | color: if sel { | |
| 817 | [1.0, 0.84, 0.24, 1.0] | |
| 818 | } else if hov { | |
| 819 | [1.0, 1.0, 1.0, 1.0] | |
| 820 | } else { | |
| 821 | *color | |
| 822 | }, | |
| 823 | }); | |
| 824 | out.push(Primitive::RoundedRect { | |
| 825 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 826 | radius: 1.5, | |
| 827 | stroke: Some(1.0), | |
| 828 | color: [0.04, 0.04, 0.06, 0.9], | |
| 829 | }); | |
| 830 | } | |
| 831 | } | |
| 832 | } | |
| 833 | } | |
| 834 | } | |
| 835 | ||
| 836 | // Box select overlay | |
| 837 | if let Some((a, b)) = self.box_select { | |
| 838 | let x0 = a[0].min(b[0]); | |
| 839 | let x1 = a[0].max(b[0]); | |
| 840 | let y0 = a[1].min(b[1]); | |
| 841 | let y1 = a[1].max(b[1]); | |
| 842 | out.push(Primitive::RoundedRect { | |
| 843 | rect: [x0, y0, x1, y1], | |
| 844 | radius: 2.0, | |
| 845 | stroke: None, | |
| 846 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 847 | }); | |
| 848 | out.push(Primitive::RoundedRect { | |
| 849 | rect: [x0, y0, x1, y1], | |
| 850 | radius: 2.0, | |
| 851 | stroke: Some(1.2), | |
| 852 | color: self.palette.accent, | |
| 853 | }); | |
| 854 | } | |
| 855 | out.push(Primitive::PopClip); | |
| 856 | ||
| 857 | // Playhead vertical line | |
| 858 | let px = self.time_to_x(current_frame); | |
| 859 | if px >= plot[0] && px <= plot[2] { | |
| 860 | out.push(Primitive::Segment { | |
| 861 | from: [px, 0.0], | |
| 862 | to: [px, plot[3]], | |
| 863 | width: 1.6, | |
| 864 | color: [0.96, 0.36, 0.34, 0.92], | |
| 865 | }); | |
| 866 | } | |
| 867 | ||
| 868 | // Footer bar | |
| 869 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - FOOTER_H, w, h], color: self.palette.ruler }); | |
| 870 | if let Some(fl) = self.text_cache.get("footer") { | |
| 871 | out.push(Primitive::Text { | |
| 872 | text: fl as &dyn Glyphs, | |
| 873 | origin: [SIDEBAR_W + 10.0, h - FOOTER_H + 5.0], | |
| 874 | clip: Some([SIDEBAR_W + 6.0, h - FOOTER_H, w - 6.0, h]), | |
| 875 | ink: self.palette.text_dim, | |
| 876 | }); | |
| 877 | } | |
| 878 | ||
| 879 | out | |
| 880 | } | |
| 881 | 512 | } |
| 882 | 513 | |
| 883 | 514 | pub fn build_tracks( |
| ... | ... | @@ -986,14 +617,6 @@ pub fn retime_selected_keys( |
| 986 | 617 | .collect() |
| 987 | 618 | } |
| 988 | 619 | |
| 989 | fn nice_step(raw: f64) -> f64 { | |
| 990 | let raw = raw.max(1.0); | |
| 991 | let pow = 10.0_f64.powf(raw.log10().floor()); | |
| 992 | let frac = raw / pow; | |
| 993 | let nice = if frac < 1.5 { 1.0 } else if frac < 3.5 { 2.0 } else if frac < 7.5 { 5.0 } else { 10.0 }; | |
| 994 | (nice * pow).max(1.0) | |
| 995 | } | |
| 996 | ||
| 997 | 620 | #[cfg(test)] |
| 998 | 621 | mod tests { |
| 999 | 622 | use super::*; |
crates/app/src/keyframes/paint.rs created+384| ... | ... | @@ -0,0 +1,384 @@ |
| 1 | //! Label layout and primitive painting for [`KeyframesView`]. | |
| 2 | ||
| 3 | use super::*; | |
| 4 | ||
| 5 | impl KeyframesView { | |
| 6 | pub fn prepare_labels(&mut self) { | |
| 7 | if self.text_cache.len() > TEXT_CACHE_CAP { | |
| 8 | self.text_cache.clear(); | |
| 9 | } | |
| 10 | let font_sm = text::Font { | |
| 11 | families: Arc::from([text::Family::Mono]), | |
| 12 | size: 10.0, | |
| 13 | weight: 500, | |
| 14 | italic: false, | |
| 15 | }; | |
| 16 | let font_row = text::Font { | |
| 17 | families: Arc::from([text::Family::Sans]), | |
| 18 | size: 11.0, | |
| 19 | weight: 500, | |
| 20 | italic: false, | |
| 21 | }; | |
| 22 | let font_hdr = text::Font { | |
| 23 | families: Arc::from([text::Family::Sans]), | |
| 24 | size: 11.0, | |
| 25 | weight: 600, | |
| 26 | italic: false, | |
| 27 | }; | |
| 28 | let make = |s: &str, font: &text::Font| { | |
| 29 | let styles = [text::Style::new(font.clone())]; | |
| 30 | text::Paragraph { | |
| 31 | text: s, | |
| 32 | styles: &styles, | |
| 33 | spans: &[], | |
| 34 | replacements: &[], | |
| 35 | width: None, | |
| 36 | indent: 0.0, | |
| 37 | line_height: 1.2, | |
| 38 | align: text::Align::Left, | |
| 39 | tab: 32.0, | |
| 40 | } | |
| 41 | .layout() | |
| 42 | }; | |
| 43 | ||
| 44 | for (k, txt, f) in [ | |
| 45 | ("hdr:title", "Keyframes", &font_hdr), | |
| 46 | ("hdr:all", "All", &font_sm), | |
| 47 | ("hdr:sel", "Sel", &font_sm), | |
| 48 | ("hdr:audio", "Audio", &font_row), | |
| 49 | ("footer", "Drag keys/spans to retime · Ruler: Scrub · Wheel: Zoom · Del: Remove · F: Fit", &font_sm), | |
| 50 | ] { | |
| 51 | self.text_cache.entry(k.to_owned()).or_insert_with(|| make(txt, f)); | |
| 52 | } | |
| 53 | ||
| 54 | for row in &self.rows { | |
| 55 | match row { | |
| 56 | TrackRow::NodeHeader { node_id, label, .. } => { | |
| 57 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 58 | let text = format!("{glyph} {label}"); | |
| 59 | self.text_cache.entry(format!("nh:{text}")).or_insert_with(|| make(&text, &font_hdr)); | |
| 60 | } | |
| 61 | TrackRow::Channel { label, .. } => { | |
| 62 | self.text_cache.entry(format!("ch:{label}")).or_insert_with(|| make(label, &font_row)); | |
| 63 | } | |
| 64 | } | |
| 65 | } | |
| 66 | ||
| 67 | let plot = self.plot_rect(); | |
| 68 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 69 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 70 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 71 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 72 | for ti in t_start..=t_end { | |
| 73 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); | |
| 74 | self.text_cache.entry(key.clone()).or_insert_with(|| make(&key[2..], &font_sm)); | |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 79 | let mut out = Vec::with_capacity(512); | |
| 80 | let [w, h] = self.size; | |
| 81 | let plot = self.plot_rect(); | |
| 82 | let top = self.tracks_top(); | |
| 83 | ||
| 84 | // Sidebar + Ruler chrome | |
| 85 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); | |
| 86 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); | |
| 87 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); | |
| 88 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); | |
| 89 | ||
| 90 | if let Some(tl) = self.text_cache.get("hdr:title") { | |
| 91 | out.push(Primitive::Text { | |
| 92 | text: tl as &dyn Glyphs, | |
| 93 | origin: [10.0, 6.0], | |
| 94 | clip: Some([8.0, 0.0, SIDEBAR_W - 52.0, RULER_H]), | |
| 95 | ink: self.palette.text, | |
| 96 | }); | |
| 97 | } | |
| 98 | // "All / Sel" filter pill | |
| 99 | let pill_rect = [SIDEBAR_W - 46.0, 4.0, SIDEBAR_W - 8.0, RULER_H - 4.0]; | |
| 100 | out.push(Primitive::RoundedRect { | |
| 101 | rect: pill_rect, | |
| 102 | radius: 4.0, | |
| 103 | stroke: None, | |
| 104 | color: if self.selected_only { | |
| 105 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] | |
| 106 | } else { | |
| 107 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.7] | |
| 108 | }, | |
| 109 | }); | |
| 110 | out.push(Primitive::RoundedRect { | |
| 111 | rect: pill_rect, | |
| 112 | radius: 4.0, | |
| 113 | stroke: Some(1.0), | |
| 114 | color: if self.selected_only { self.palette.accent } else { self.palette.rule }, | |
| 115 | }); | |
| 116 | let mode_key = if self.selected_only { "hdr:sel" } else { "hdr:all" }; | |
| 117 | if let Some(ml) = self.text_cache.get(mode_key) { | |
| 118 | out.push(Primitive::Text { | |
| 119 | text: ml as &dyn Glyphs, | |
| 120 | origin: [pill_rect[0] + 9.0, 7.0], | |
| 121 | clip: Some(pill_rect), | |
| 122 | ink: if self.selected_only { self.palette.text } else { self.palette.text_dim }, | |
| 123 | }); | |
| 124 | } | |
| 125 | ||
| 126 | // Ruler frame ticks | |
| 127 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 128 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 129 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 130 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 131 | for ti in t_start..=t_end { | |
| 132 | let t = ti as f64 * t_step; | |
| 133 | let x = self.time_to_x(t); | |
| 134 | if x < plot[0] || x > plot[2] { | |
| 135 | continue; | |
| 136 | } | |
| 137 | out.push(Primitive::Segment { from: [x, RULER_H], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); | |
| 138 | let key = format!("t:{}", t.round() as i64); | |
| 139 | if let Some(tl) = self.text_cache.get(&key) { | |
| 140 | out.push(Primitive::Text { | |
| 141 | text: tl as &dyn Glyphs, | |
| 142 | origin: [x + 3.0, 5.0], | |
| 143 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), | |
| 144 | ink: self.palette.text_dim, | |
| 145 | }); | |
| 146 | } | |
| 147 | } | |
| 148 | ||
| 149 | // Green RAM cache bar along bottom edge of ruler | |
| 150 | for &(c0, c1) in &self.cached_ranges { | |
| 151 | let x0 = self.time_to_x(c0 as f64).max(plot[0]); | |
| 152 | let x1 = self.time_to_x((c1 + 1) as f64).min(plot[2]); | |
| 153 | if x1 > x0 { | |
| 154 | out.push(Primitive::Rect { | |
| 155 | rect: [x0, RULER_H - 3.5, x1.max(x0 + 2.0), RULER_H - 0.5], | |
| 156 | color: [0.22, 0.86, 0.48, 0.92], | |
| 157 | }); | |
| 158 | } | |
| 159 | } | |
| 160 | ||
| 161 | // Dedicated Audio Waveform row when audio is present | |
| 162 | if !self.audio_peaks.is_empty() { | |
| 163 | let ay0 = RULER_H; | |
| 164 | let ay1 = RULER_H + AUDIO_ROW_H; | |
| 165 | out.push(Primitive::Rect { rect: [0.0, ay0, w, ay1], color: self.palette.row_alt }); | |
| 166 | out.push(Primitive::Segment { from: [0.0, ay1], to: [w, ay1], width: 1.0, color: self.palette.rule }); | |
| 167 | if let Some(al) = self.text_cache.get("hdr:audio") { | |
| 168 | out.push(Primitive::Text { | |
| 169 | text: al as &dyn Glyphs, | |
| 170 | origin: [12.0, ay0 + 5.0], | |
| 171 | clip: Some([8.0, ay0, SIDEBAR_W - 8.0, ay1]), | |
| 172 | ink: [0.54, 0.80, 1.0, 0.95], | |
| 173 | }); | |
| 174 | } | |
| 175 | out.push(Primitive::PushClip { rect: [plot[0], ay0, plot[2], ay1], radius: 0.0 }); | |
| 176 | let mid_y = 0.5 * (ay0 + ay1); | |
| 177 | let max_half = AUDIO_ROW_H * 0.42; | |
| 178 | let a0 = self.audio_range[0]; | |
| 179 | let n = self.audio_peaks.len() as i64; | |
| 180 | let f_vis0 = (self.t_range[0].floor() as i64).max(a0); | |
| 181 | let f_vis1 = (self.t_range[1].ceil() as i64).min(a0 + n - 1); | |
| 182 | let bar_step = ((plot[2] - plot[0]) / ((self.t_range[1] - self.t_range[0]).max(1.0) as f32)).max(1.5); | |
| 183 | let stride = ((1.8 / bar_step).ceil() as i64).max(1); | |
| 184 | let mut f = f_vis0; | |
| 185 | while f <= f_vis1 { | |
| 186 | let mut peak = 0.0_f32; | |
| 187 | for off in 0..stride { | |
| 188 | let idx = (f + off - a0) as usize; | |
| 189 | if let Some(&v) = self.audio_peaks.get(idx) { | |
| 190 | if v > peak { peak = v; } | |
| 191 | } | |
| 192 | } | |
| 193 | if peak > 0.01 { | |
| 194 | let x = self.time_to_x(f as f64); | |
| 195 | let hh = (peak * max_half).max(1.0); | |
| 196 | let bw = (bar_step * stride as f32 - 0.6).max(1.2); | |
| 197 | out.push(Primitive::Rect { | |
| 198 | rect: [x, mid_y - hh, x + bw, mid_y + hh], | |
| 199 | color: [0.46, 0.74, 0.98, 0.55], | |
| 200 | }); | |
| 201 | } | |
| 202 | f += stride; | |
| 203 | } | |
| 204 | out.push(Primitive::PopClip); | |
| 205 | } | |
| 206 | ||
| 207 | // Render range subtle shading | |
| 208 | let rx0 = self.time_to_x(comp.render_range[0] as f64); | |
| 209 | let rx1 = self.time_to_x(comp.render_range[1] as f64); | |
| 210 | out.push(Primitive::PushClip { rect: [plot[0], top, plot[2], plot[3]], radius: 0.0 }); | |
| 211 | if rx0 > plot[0] { | |
| 212 | out.push(Primitive::Rect { rect: [plot[0], top, rx0.min(plot[2]), plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 213 | } | |
| 214 | if rx1 < plot[2] { | |
| 215 | out.push(Primitive::Rect { rect: [rx1.max(plot[0]), top, plot[2], plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 216 | } | |
| 217 | out.push(Primitive::PopClip); | |
| 218 | ||
| 219 | // Track rows (clipped to track area) | |
| 220 | out.push(Primitive::PushClip { rect: [0.0, top, w, plot[3]], radius: 0.0 }); | |
| 221 | for (ri, row) in self.rows.iter().enumerate() { | |
| 222 | let y0 = self.row_y(ri); | |
| 223 | let y1 = y0 + ROW_H; | |
| 224 | if y1 < top || y0 > plot[3] { | |
| 225 | continue; | |
| 226 | } | |
| 227 | if ri % 2 == 1 { | |
| 228 | out.push(Primitive::Rect { rect: [0.0, y0, w, y1], color: self.palette.row_alt }); | |
| 229 | } | |
| 230 | out.push(Primitive::Segment { from: [0.0, y1], to: [w, y1], width: 1.0, color: self.palette.grid }); | |
| 231 | ||
| 232 | match row { | |
| 233 | TrackRow::NodeHeader { node_id, label, splines } => { | |
| 234 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 235 | let key = format!("nh:{glyph} {label}"); | |
| 236 | if let Some(tl) = self.text_cache.get(&key) { | |
| 237 | out.push(Primitive::Text { | |
| 238 | text: tl as &dyn Glyphs, | |
| 239 | origin: [10.0, y0 + 4.0], | |
| 240 | clip: Some([6.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 241 | ink: self.palette.text, | |
| 242 | }); | |
| 243 | } | |
| 244 | // Summary span + tick dots on node header row | |
| 245 | let mut t_min = f64::INFINITY; | |
| 246 | let mut t_max = f64::NEG_INFINITY; | |
| 247 | for sname in splines { | |
| 248 | if let Some(sp) = comp.splines.get(sname) { | |
| 249 | for kf in &sp.keyframes { | |
| 250 | t_min = t_min.min(kf.time); | |
| 251 | t_max = t_max.max(kf.time); | |
| 252 | } | |
| 253 | } | |
| 254 | } | |
| 255 | if t_min.is_finite() { | |
| 256 | let sx0 = self.time_to_x(t_min).max(plot[0]); | |
| 257 | let sx1 = self.time_to_x(t_max).min(plot[2]); | |
| 258 | if sx1 >= sx0 { | |
| 259 | out.push(Primitive::RoundedRect { | |
| 260 | rect: [sx0, y0 + 6.0, sx1.max(sx0 + 4.0), y1 - 6.0], | |
| 261 | radius: 3.0, | |
| 262 | stroke: None, | |
| 263 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.22], | |
| 264 | }); | |
| 265 | } | |
| 266 | } | |
| 267 | } | |
| 268 | TrackRow::Channel { label, spline, color, .. } => { | |
| 269 | out.push(Primitive::RoundedRect { | |
| 270 | rect: [20.0, y0 + 7.0, 28.0, y0 + 15.0], | |
| 271 | radius: 2.0, | |
| 272 | stroke: None, | |
| 273 | color: *color, | |
| 274 | }); | |
| 275 | if let Some(tl) = self.text_cache.get(&format!("ch:{label}")) { | |
| 276 | out.push(Primitive::Text { | |
| 277 | text: tl as &dyn Glyphs, | |
| 278 | origin: [34.0, y0 + 4.0], | |
| 279 | clip: Some([32.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 280 | ink: self.palette.text_dim, | |
| 281 | }); | |
| 282 | } | |
| 283 | ||
| 284 | if let Some(sp) = comp.splines.get(spline) { | |
| 285 | if sp.keyframes.len() >= 2 { | |
| 286 | let sx0 = self.time_to_x(sp.keyframes.first().unwrap().time).max(plot[0]); | |
| 287 | let sx1 = self.time_to_x(sp.keyframes.last().unwrap().time).min(plot[2]); | |
| 288 | if sx1 > sx0 { | |
| 289 | out.push(Primitive::RoundedRect { | |
| 290 | rect: [sx0, y0 + 8.0, sx1, y1 - 8.0], | |
| 291 | radius: 2.0, | |
| 292 | stroke: None, | |
| 293 | color: [color[0], color[1], color[2], 0.28], | |
| 294 | }); | |
| 295 | } | |
| 296 | } | |
| 297 | let cy = y0 + ROW_H * 0.5; | |
| 298 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 299 | let kx = self.time_to_x(kf.time); | |
| 300 | if kx < plot[0] - 8.0 || kx > plot[2] + 8.0 { | |
| 301 | continue; | |
| 302 | } | |
| 303 | let kref = DopeKeyRef { spline: spline.clone(), key_idx: ki }; | |
| 304 | let sel = self.selected_keys.contains(&kref); | |
| 305 | let hov = self.hovered_key.as_ref() == Some(&kref); | |
| 306 | let r = if sel || hov { 5.0 } else { 4.0 }; | |
| 307 | out.push(Primitive::RoundedRect { | |
| 308 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 309 | radius: 1.5, | |
| 310 | stroke: None, | |
| 311 | color: if sel { | |
| 312 | [1.0, 0.84, 0.24, 1.0] | |
| 313 | } else if hov { | |
| 314 | [1.0, 1.0, 1.0, 1.0] | |
| 315 | } else { | |
| 316 | *color | |
| 317 | }, | |
| 318 | }); | |
| 319 | out.push(Primitive::RoundedRect { | |
| 320 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 321 | radius: 1.5, | |
| 322 | stroke: Some(1.0), | |
| 323 | color: [0.04, 0.04, 0.06, 0.9], | |
| 324 | }); | |
| 325 | } | |
| 326 | } | |
| 327 | } | |
| 328 | } | |
| 329 | } | |
| 330 | ||
| 331 | // Box select overlay | |
| 332 | if let Some((a, b)) = self.box_select { | |
| 333 | let x0 = a[0].min(b[0]); | |
| 334 | let x1 = a[0].max(b[0]); | |
| 335 | let y0 = a[1].min(b[1]); | |
| 336 | let y1 = a[1].max(b[1]); | |
| 337 | out.push(Primitive::RoundedRect { | |
| 338 | rect: [x0, y0, x1, y1], | |
| 339 | radius: 2.0, | |
| 340 | stroke: None, | |
| 341 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 342 | }); | |
| 343 | out.push(Primitive::RoundedRect { | |
| 344 | rect: [x0, y0, x1, y1], | |
| 345 | radius: 2.0, | |
| 346 | stroke: Some(1.2), | |
| 347 | color: self.palette.accent, | |
| 348 | }); | |
| 349 | } | |
| 350 | out.push(Primitive::PopClip); | |
| 351 | ||
| 352 | // Playhead vertical line | |
| 353 | let px = self.time_to_x(current_frame); | |
| 354 | if px >= plot[0] && px <= plot[2] { | |
| 355 | out.push(Primitive::Segment { | |
| 356 | from: [px, 0.0], | |
| 357 | to: [px, plot[3]], | |
| 358 | width: 1.6, | |
| 359 | color: [0.96, 0.36, 0.34, 0.92], | |
| 360 | }); | |
| 361 | } | |
| 362 | ||
| 363 | // Footer bar | |
| 364 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - FOOTER_H, w, h], color: self.palette.ruler }); | |
| 365 | if let Some(fl) = self.text_cache.get("footer") { | |
| 366 | out.push(Primitive::Text { | |
| 367 | text: fl as &dyn Glyphs, | |
| 368 | origin: [SIDEBAR_W + 10.0, h - FOOTER_H + 5.0], | |
| 369 | clip: Some([SIDEBAR_W + 6.0, h - FOOTER_H, w - 6.0, h]), | |
| 370 | ink: self.palette.text_dim, | |
| 371 | }); | |
| 372 | } | |
| 373 | ||
| 374 | out | |
| 375 | } | |
| 376 | } | |
| 377 | ||
| 378 | fn nice_step(raw: f64) -> f64 { | |
| 379 | let raw = raw.max(1.0); | |
| 380 | let pow = 10.0_f64.powf(raw.log10().floor()); | |
| 381 | let frac = raw / pow; | |
| 382 | let nice = if frac < 1.5 { 1.0 } else if frac < 3.5 { 2.0 } else if frac < 7.5 { 5.0 } else { 10.0 }; | |
| 383 | (nice * pow).max(1.0) | |
| 384 | } |
crates/engine/src/media.rs+13-785| ... | ... | @@ -1,6 +1,18 @@ |
| 1 | 1 | //! Media loading (PNG/JPG/EXR/TIFF stills & numbered image sequences) and |
| 2 | 2 | //! CPU rasterizers (`TextPlus` via `text_engine`, `PolylineMask` via `zeno`). |
| 3 | 3 | |
| 4 | mod exr; | |
| 5 | mod raster; | |
| 6 | mod video; | |
| 7 | ||
| 8 | pub use exr::f32_to_f16_bits; | |
| 9 | pub use raster::{TextPlusParams, rasterize_polyline_mask, rasterize_text_plus}; | |
| 10 | #[cfg(target_os = "macos")] | |
| 11 | pub use video::AvFoundationDecoder; | |
| 12 | pub use video::{FfmpegDecoder, VideoDecoder, VideoInfo, open_video_decoder}; | |
| 13 | use exr::{apply_post_multiply_rgba8, decode_exr_rgba, is_exr_path, normalize_channel_opt}; | |
| 14 | use video::is_video_path; | |
| 15 | ||
| 4 | 16 | use crate::text_engine; |
| 5 | 17 | use model::{ClipSpec, ClsEntry, PolyPoint}; |
| 6 | 18 | use std::collections::HashMap; |
| ... | ... | @@ -36,552 +48,6 @@ impl DecodedImage { |
| 36 | 48 | } |
| 37 | 49 | } |
| 38 | 50 | |
| 39 | /// Converts IEEE-754 `f32` to IEEE-754 binary16 (`f16`) bits for direct `Rgba16Float` GPU upload. | |
| 40 | #[inline] | |
| 41 | pub fn f32_to_f16_bits(val: f32) -> u16 { | |
| 42 | let bits = val.to_bits(); | |
| 43 | let sign = ((bits >> 16) & 0x8000) as u16; | |
| 44 | let exp = ((bits >> 23) & 0xFF) as i32; | |
| 45 | let frac = bits & 0x007F_FFFF; | |
| 46 | ||
| 47 | if exp == 0xFF { | |
| 48 | return if frac != 0 { 0 } else { sign | 0x7C00 }; | |
| 49 | } | |
| 50 | let new_exp = exp - 127 + 15; | |
| 51 | if new_exp >= 31 { | |
| 52 | return sign | 0x7BFF; | |
| 53 | } | |
| 54 | if new_exp <= 0 { | |
| 55 | if new_exp < -10 { | |
| 56 | return sign; | |
| 57 | } | |
| 58 | let m = (frac | 0x0080_0000) >> (1 - new_exp + 13); | |
| 59 | return sign | (m as u16); | |
| 60 | } | |
| 61 | let rounded = frac + 0x1000; | |
| 62 | if rounded >= 0x0080_0000 { | |
| 63 | let e = new_exp + 1; | |
| 64 | return if e >= 31 { sign | 0x7BFF } else { sign | ((e as u16) << 10) }; | |
| 65 | } | |
| 66 | sign | ((new_exp as u16) << 10) | ((rounded >> 13) as u16) | |
| 67 | } | |
| 68 | ||
| 69 | #[derive(Clone, Copy, Debug)] | |
| 70 | pub struct VideoInfo { | |
| 71 | pub width: u32, | |
| 72 | pub height: u32, | |
| 73 | pub fps: f64, | |
| 74 | pub nb_frames: i64, | |
| 75 | } | |
| 76 | ||
| 77 | pub trait VideoDecoder: Send { | |
| 78 | fn info(&self) -> VideoInfo; | |
| 79 | fn decode_frame(&mut self, frame_idx: i64) -> Option<Vec<u8>>; | |
| 80 | fn backend_name(&self) -> &'static str; | |
| 81 | } | |
| 82 | ||
| 83 | #[cfg(target_os = "macos")] | |
| 84 | mod av_ffi { | |
| 85 | use std::ffi::c_char; | |
| 86 | ||
| 87 | #[repr(C)] | |
| 88 | pub struct AvVideoDecoder { | |
| 89 | _private: [u8; 0], | |
| 90 | } | |
| 91 | ||
| 92 | unsafe extern "C" { | |
| 93 | pub fn av_video_open( | |
| 94 | path: *const c_char, | |
| 95 | out_w: *mut u32, | |
| 96 | out_h: *mut u32, | |
| 97 | out_fps: *mut f64, | |
| 98 | out_nb_frames: *mut i64, | |
| 99 | ) -> *mut AvVideoDecoder; | |
| 100 | ||
| 101 | pub fn av_video_decode_frame( | |
| 102 | dec: *mut AvVideoDecoder, | |
| 103 | frame_idx: i64, | |
| 104 | out_rgba: *mut u8, | |
| 105 | out_len: usize, | |
| 106 | ) -> i32; | |
| 107 | ||
| 108 | pub fn av_video_close(dec: *mut AvVideoDecoder); | |
| 109 | } | |
| 110 | } | |
| 111 | ||
| 112 | #[cfg(target_os = "macos")] | |
| 113 | pub struct AvFoundationDecoder { | |
| 114 | ptr: *mut av_ffi::AvVideoDecoder, | |
| 115 | info: VideoInfo, | |
| 116 | } | |
| 117 | ||
| 118 | #[cfg(target_os = "macos")] | |
| 119 | unsafe impl Send for AvFoundationDecoder {} | |
| 120 | ||
| 121 | #[cfg(target_os = "macos")] | |
| 122 | impl Drop for AvFoundationDecoder { | |
| 123 | fn drop(&mut self) { | |
| 124 | if !self.ptr.is_null() { | |
| 125 | unsafe { av_ffi::av_video_close(self.ptr) }; | |
| 126 | self.ptr = std::ptr::null_mut(); | |
| 127 | } | |
| 128 | } | |
| 129 | } | |
| 130 | ||
| 131 | #[cfg(target_os = "macos")] | |
| 132 | impl AvFoundationDecoder { | |
| 133 | pub fn open(path: &Path) -> Option<Self> { | |
| 134 | use std::ffi::CString; | |
| 135 | let path_str = path.to_str()?; | |
| 136 | let c_path = CString::new(path_str).ok()?; | |
| 137 | let mut width = 0u32; | |
| 138 | let mut height = 0u32; | |
| 139 | let mut fps = 0.0f64; | |
| 140 | let mut nb_frames = 0i64; | |
| 141 | let ptr = unsafe { | |
| 142 | av_ffi::av_video_open( | |
| 143 | c_path.as_ptr(), | |
| 144 | &mut width, | |
| 145 | &mut height, | |
| 146 | &mut fps, | |
| 147 | &mut nb_frames, | |
| 148 | ) | |
| 149 | }; | |
| 150 | if ptr.is_null() || width == 0 || height == 0 { | |
| 151 | if !ptr.is_null() { | |
| 152 | unsafe { av_ffi::av_video_close(ptr) }; | |
| 153 | } | |
| 154 | return None; | |
| 155 | } | |
| 156 | Some(Self { | |
| 157 | ptr, | |
| 158 | info: VideoInfo { | |
| 159 | width, | |
| 160 | height, | |
| 161 | fps: if fps > 0.0 { fps } else { 30.0 }, | |
| 162 | nb_frames, | |
| 163 | }, | |
| 164 | }) | |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | #[cfg(target_os = "macos")] | |
| 169 | impl VideoDecoder for AvFoundationDecoder { | |
| 170 | fn info(&self) -> VideoInfo { | |
| 171 | self.info | |
| 172 | } | |
| 173 | ||
| 174 | fn decode_frame(&mut self, frame_idx: i64) -> Option<Vec<u8>> { | |
| 175 | let len = (self.info.width as usize) | |
| 176 | .checked_mul(self.info.height as usize)? | |
| 177 | .checked_mul(4)?; | |
| 178 | let mut buf = vec![0u8; len]; | |
| 179 | let ok = unsafe { | |
| 180 | av_ffi::av_video_decode_frame(self.ptr, frame_idx.max(0), buf.as_mut_ptr(), len) | |
| 181 | }; | |
| 182 | if ok != 0 { | |
| 183 | Some(buf) | |
| 184 | } else { | |
| 185 | None | |
| 186 | } | |
| 187 | } | |
| 188 | ||
| 189 | fn backend_name(&self) -> &'static str { | |
| 190 | "AVFoundation/VideoToolbox" | |
| 191 | } | |
| 192 | } | |
| 193 | ||
| 194 | pub struct FfmpegDecoder { | |
| 195 | path: PathBuf, | |
| 196 | info: VideoInfo, | |
| 197 | } | |
| 198 | ||
| 199 | impl FfmpegDecoder { | |
| 200 | pub fn open(path: &Path) -> Option<Self> { | |
| 201 | let info = probe_video(path)?; | |
| 202 | Some(Self { | |
| 203 | path: path.to_path_buf(), | |
| 204 | info, | |
| 205 | }) | |
| 206 | } | |
| 207 | } | |
| 208 | ||
| 209 | impl VideoDecoder for FfmpegDecoder { | |
| 210 | fn info(&self) -> VideoInfo { | |
| 211 | self.info | |
| 212 | } | |
| 213 | ||
| 214 | fn decode_frame(&mut self, frame_idx: i64) -> Option<Vec<u8>> { | |
| 215 | decode_video_frame(&self.path, self.info, frame_idx) | |
| 216 | } | |
| 217 | ||
| 218 | fn backend_name(&self) -> &'static str { | |
| 219 | "ffmpeg" | |
| 220 | } | |
| 221 | } | |
| 222 | ||
| 223 | /// Opens a hardware-accelerated video decoder (`AVFoundation`/`VideoToolbox` on macOS), | |
| 224 | /// falling back to `ffmpeg` for unsupported containers or codecs. | |
| 225 | pub fn open_video_decoder(path: &Path) -> Option<Box<dyn VideoDecoder>> { | |
| 226 | #[cfg(target_os = "macos")] | |
| 227 | { | |
| 228 | if let Some(av) = AvFoundationDecoder::open(path) { | |
| 229 | return Some(Box::new(av)); | |
| 230 | } | |
| 231 | } | |
| 232 | FfmpegDecoder::open(path).map(|ff| Box::new(ff) as Box<dyn VideoDecoder>) | |
| 233 | } | |
| 234 | ||
| 235 | fn is_video_path(path: &Path) -> bool { | |
| 236 | let Some(ext) = path.extension().and_then(|e| e.to_str()) else { | |
| 237 | return false; | |
| 238 | }; | |
| 239 | matches!( | |
| 240 | ext.to_ascii_lowercase().as_str(), | |
| 241 | "mov" | "mp4" | "m4v" | "mkv" | "avi" | "webm" | "mxf" | |
| 242 | ) | |
| 243 | } | |
| 244 | ||
| 245 | fn probe_video(path: &Path) -> Option<VideoInfo> { | |
| 246 | let out = Command::new("ffprobe") | |
| 247 | .args([ | |
| 248 | "-v", | |
| 249 | "error", | |
| 250 | "-select_streams", | |
| 251 | "v:0", | |
| 252 | "-show_entries", | |
| 253 | "stream=width,height,r_frame_rate,nb_frames", | |
| 254 | "-of", | |
| 255 | "default=noprint_wrappers=1", | |
| 256 | ]) | |
| 257 | .arg(path) | |
| 258 | .output() | |
| 259 | .ok()?; | |
| 260 | if !out.status.success() { | |
| 261 | return None; | |
| 262 | } | |
| 263 | let text = String::from_utf8_lossy(&out.stdout); | |
| 264 | let mut width = 0u32; | |
| 265 | let mut height = 0u32; | |
| 266 | let mut fps = 30.0_f64; | |
| 267 | let mut nb_frames = 0i64; | |
| 268 | for line in text.lines() { | |
| 269 | if let Some((k, v)) = line.split_once('=') { | |
| 270 | match k.trim() { | |
| 271 | "width" => width = v.trim().parse().unwrap_or(0), | |
| 272 | "height" => height = v.trim().parse().unwrap_or(0), | |
| 273 | "nb_frames" => nb_frames = v.trim().parse().unwrap_or(0), | |
| 274 | "r_frame_rate" => { | |
| 275 | if let Some((n, d)) = v.trim().split_once('/') { | |
| 276 | let num: f64 = n.parse().unwrap_or(30.0); | |
| 277 | let den: f64 = d.parse().unwrap_or(1.0); | |
| 278 | if den > 0.0 && num > 0.0 { | |
| 279 | fps = num / den; | |
| 280 | } | |
| 281 | } else if let Ok(r) = v.trim().parse::<f64>() { | |
| 282 | if r > 0.0 { | |
| 283 | fps = r; | |
| 284 | } | |
| 285 | } | |
| 286 | } | |
| 287 | _ => {} | |
| 288 | } | |
| 289 | } | |
| 290 | } | |
| 291 | if width == 0 || height == 0 { | |
| 292 | return None; | |
| 293 | } | |
| 294 | Some(VideoInfo { width, height, fps, nb_frames }) | |
| 295 | } | |
| 296 | ||
| 297 | fn decode_video_frame(path: &Path, info: VideoInfo, frame_idx: i64) -> Option<Vec<u8>> { | |
| 298 | let idx = frame_idx.max(0); | |
| 299 | let ts = if idx == 0 { | |
| 300 | 0.0 | |
| 301 | } else { | |
| 302 | ((idx as f64) - 0.005) / info.fps.max(1.0) | |
| 303 | }; | |
| 304 | let out = Command::new("ffmpeg") | |
| 305 | .args([ | |
| 306 | "-v", | |
| 307 | "error", | |
| 308 | "-ss", | |
| 309 | &format!("{ts:.6}"), | |
| 310 | "-i", | |
| 311 | ]) | |
| 312 | .arg(path) | |
| 313 | .args([ | |
| 314 | "-frames:v", | |
| 315 | "1", | |
| 316 | "-f", | |
| 317 | "rawvideo", | |
| 318 | "-pix_fmt", | |
| 319 | "rgba", | |
| 320 | "-", | |
| 321 | ]) | |
| 322 | .output() | |
| 323 | .ok()?; | |
| 324 | if !out.status.success() { | |
| 325 | return None; | |
| 326 | } | |
| 327 | let expected = (info.width as usize) * (info.height as usize) * 4; | |
| 328 | if out.stdout.len() < expected { | |
| 329 | return None; | |
| 330 | } | |
| 331 | let mut buf = out.stdout; | |
| 332 | buf.truncate(expected); | |
| 333 | Some(buf) | |
| 334 | } | |
| 335 | ||
| 336 | fn is_exr_path(path: &Path) -> bool { | |
| 337 | path.extension() | |
| 338 | .and_then(|e| e.to_str()) | |
| 339 | .is_some_and(|e| e.eq_ignore_ascii_case("exr")) | |
| 340 | } | |
| 341 | ||
| 342 | fn full_exr_channel_name(layer_name: Option<&str>, ch_name: &str) -> String { | |
| 343 | match layer_name { | |
| 344 | Some(ln) if !ln.is_empty() && !ch_name.starts_with(&format!("{ln}.")) => { | |
| 345 | format!("{ln}.{ch_name}") | |
| 346 | } | |
| 347 | _ => ch_name.to_owned(), | |
| 348 | } | |
| 349 | } | |
| 350 | ||
| 351 | fn normalize_channel_opt(opt: Option<&str>) -> Option<String> { | |
| 352 | opt.map(str::trim) | |
| 353 | .filter(|s| !s.is_empty()) | |
| 354 | .map(str::to_owned) | |
| 355 | } | |
| 356 | ||
| 357 | fn apply_post_multiply_rgba8(raw: &mut [u8]) { | |
| 358 | for px in raw.chunks_exact_mut(4) { | |
| 359 | let a = u16::from(px[3]); | |
| 360 | if a < 255 { | |
| 361 | px[0] = ((u16::from(px[0]) * a + 127) / 255) as u8; | |
| 362 | px[1] = ((u16::from(px[1]) * a + 127) / 255) as u8; | |
| 363 | px[2] = ((u16::from(px[2]) * a + 127) / 255) as u8; | |
| 364 | } | |
| 365 | } | |
| 366 | } | |
| 367 | ||
| 368 | fn find_exr_channel_loc( | |
| 369 | headers: &[exr::meta::header::Header], | |
| 370 | w: usize, | |
| 371 | h: usize, | |
| 372 | preferred: Option<&str>, | |
| 373 | beauty_upper: &str, | |
| 374 | beauty_lower: &str, | |
| 375 | ) -> Option<(usize, usize)> { | |
| 376 | let valid_layer = |header: &exr::meta::header::Header| { | |
| 377 | !header.deep && header.layer_size.width() == w && header.layer_size.height() == h | |
| 378 | }; | |
| 379 | ||
| 380 | if let Some(pref) = preferred.map(str::trim).filter(|s| !s.is_empty()) { | |
| 381 | for (li, header) in headers.iter().enumerate() { | |
| 382 | if !valid_layer(header) { | |
| 383 | continue; | |
| 384 | } | |
| 385 | let lname = header.own_attributes.layer_name.as_ref().map(|t| t.to_string()); | |
| 386 | for (ci, ch) in header.channels.list.iter().enumerate() { | |
| 387 | if ch.sampling != exr::math::Vec2(1, 1) { | |
| 388 | continue; | |
| 389 | } | |
| 390 | let full = full_exr_channel_name(lname.as_deref(), &ch.name.to_string()); | |
| 391 | if full == pref { | |
| 392 | return Some((li, ci)); | |
| 393 | } | |
| 394 | } | |
| 395 | } | |
| 396 | let suffix = format!(".{pref}"); | |
| 397 | for (li, header) in headers.iter().enumerate() { | |
| 398 | if !valid_layer(header) { | |
| 399 | continue; | |
| 400 | } | |
| 401 | let lname = header.own_attributes.layer_name.as_ref().map(|t| t.to_string()); | |
| 402 | for (ci, ch) in header.channels.list.iter().enumerate() { | |
| 403 | if ch.sampling != exr::math::Vec2(1, 1) { | |
| 404 | continue; | |
| 405 | } | |
| 406 | let full = full_exr_channel_name(lname.as_deref(), &ch.name.to_string()); | |
| 407 | if full.ends_with(&suffix) { | |
| 408 | return Some((li, ci)); | |
| 409 | } | |
| 410 | } | |
| 411 | } | |
| 412 | return None; | |
| 413 | } | |
| 414 | ||
| 415 | // Beauty pass fallback: check ViewLayer.Combined.<C>, Combined.<C>, *.Combined.<C>, | |
| 416 | // then exact bare <C>/<c>. Never match non-beauty layers such as CryptoObject00.R! | |
| 417 | let view_combined = format!("ViewLayer.Combined.{beauty_upper}"); | |
| 418 | let combined = format!("Combined.{beauty_upper}"); | |
| 419 | let any_combined = format!(".Combined.{beauty_upper}"); | |
| 420 | let matcher_passes: [&dyn Fn(&str) -> bool; 4] = [ | |
| 421 | &|full: &str| full == view_combined, | |
| 422 | &|full: &str| full == combined || full.ends_with(&any_combined), | |
| 423 | &|full: &str| full == beauty_upper, | |
| 424 | &|full: &str| full == beauty_lower, | |
| 425 | ]; | |
| 426 | for pred in matcher_passes { | |
| 427 | for (li, header) in headers.iter().enumerate() { | |
| 428 | if !valid_layer(header) { | |
| 429 | continue; | |
| 430 | } | |
| 431 | let lname = header.own_attributes.layer_name.as_ref().map(|t| t.to_string()); | |
| 432 | for (ci, ch) in header.channels.list.iter().enumerate() { | |
| 433 | if ch.sampling != exr::math::Vec2(1, 1) { | |
| 434 | continue; | |
| 435 | } | |
| 436 | let full = full_exr_channel_name(lname.as_deref(), &ch.name.to_string()); | |
| 437 | if pred(&full) { | |
| 438 | return Some((li, ci)); | |
| 439 | } | |
| 440 | } | |
| 441 | } | |
| 442 | } | |
| 443 | None | |
| 444 | } | |
| 445 | ||
| 446 | /// Decodes a single-layer or multi-layer (Blender `ViewLayer.Combined.R/G/B/A`) OpenEXR | |
| 447 | /// selectively for only the matched RGBA channels, producing clamped RGBA8 and linear HDR | |
| 448 | /// RGBA16F/RGBA32F buffers without decoding unrelated AOV/Cryptomatte layers. | |
| 449 | fn decode_exr_rgba( | |
| 450 | path: &Path, | |
| 451 | red_name: Option<&str>, | |
| 452 | green_name: Option<&str>, | |
| 453 | blue_name: Option<&str>, | |
| 454 | alpha_name: Option<&str>, | |
| 455 | post_multiply: bool, | |
| 456 | ) -> Option<DecodedImage> { | |
| 457 | use exr::block::reader::ChunksReader; | |
| 458 | use exr::meta::attribute::SampleType; | |
| 459 | use exr::prelude::*; | |
| 460 | ||
| 461 | let file = std::fs::File::open(path).ok()?; | |
| 462 | let chunks_reader = exr::block::read(std::io::BufReader::new(file), false).ok()?; | |
| 463 | let first_header = chunks_reader.headers().first()?; | |
| 464 | let w = first_header.layer_size.width(); | |
| 465 | let h = first_header.layer_size.height(); | |
| 466 | if w == 0 || h == 0 { | |
| 467 | return None; | |
| 468 | } | |
| 469 | ||
| 470 | let r_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, red_name, "R", "r")?; | |
| 471 | let g_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, green_name, "G", "g"); | |
| 472 | let b_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, blue_name, "B", "b"); | |
| 473 | let a_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, alpha_name, "A", "a"); | |
| 474 | ||
| 475 | let target_locs = [Some(r_loc), g_loc, b_loc, a_loc]; | |
| 476 | let mut needed_layers: Vec<usize> = target_locs.iter().flatten().map(|(l, _)| *l).collect(); | |
| 477 | needed_layers.sort_unstable(); | |
| 478 | needed_layers.dedup(); | |
| 479 | ||
| 480 | let mut raw_f32 = vec![0.0_f32; w * h * 4]; | |
| 481 | let mut line_f32 = Vec::<f32>::new(); | |
| 482 | let mut line_f16 = Vec::<f16>::new(); | |
| 483 | let mut line_u32 = Vec::<u32>::new(); | |
| 484 | ||
| 485 | chunks_reader | |
| 486 | .filter_chunks(false, |_, tile, block| { | |
| 487 | tile.is_largest_resolution_level() && needed_layers.contains(&block.layer) | |
| 488 | }) | |
| 489 | .ok()? | |
| 490 | .decompress_parallel(false, |meta, block| { | |
| 491 | let header = &meta.headers[block.index.layer]; | |
| 492 | for line in block.lines(&header.channels) { | |
| 493 | let loc = (line.location.layer, line.location.channel); | |
| 494 | let mut comp_mask = [false; 4]; | |
| 495 | for (c, t) in target_locs.iter().enumerate() { | |
| 496 | if *t == Some(loc) { | |
| 497 | comp_mask[c] = true; | |
| 498 | } | |
| 499 | } | |
| 500 | if !comp_mask.iter().any(|&m| m) { | |
| 501 | continue; | |
| 502 | } | |
| 503 | let y = line.location.position.y(); | |
| 504 | let x0 = line.location.position.x(); | |
| 505 | let len = line.location.sample_count; | |
| 506 | if y >= h || x0 + len > w { | |
| 507 | continue; | |
| 508 | } | |
| 509 | ||
| 510 | line_f32.clear(); | |
| 511 | match header.channels.list[line.location.channel].sample_type { | |
| 512 | SampleType::F32 => { | |
| 513 | line_f32.resize(len, 0.0); | |
| 514 | line.read_samples_into_slice(&mut line_f32)?; | |
| 515 | } | |
| 516 | SampleType::F16 => { | |
| 517 | line_f16.resize(len, f16::ZERO); | |
| 518 | line.read_samples_into_slice(&mut line_f16)?; | |
| 519 | line_f32.extend(line_f16.iter().map(|v| v.to_f32())); | |
| 520 | } | |
| 521 | SampleType::U32 => { | |
| 522 | line_u32.resize(len, 0); | |
| 523 | line.read_samples_into_slice(&mut line_u32)?; | |
| 524 | line_f32.extend(line_u32.iter().map(|&v| v as f32)); | |
| 525 | } | |
| 526 | } | |
| 527 | ||
| 528 | let row_base = (y * w + x0) * 4; | |
| 529 | for (dx, &sample) in line_f32.iter().enumerate() { | |
| 530 | let val = if sample.is_nan() { 0.0 } else { sample }; | |
| 531 | let px_off = row_base + dx * 4; | |
| 532 | for (c, &enabled) in comp_mask.iter().enumerate() { | |
| 533 | if enabled { | |
| 534 | raw_f32[px_off + c] = val; | |
| 535 | } | |
| 536 | } | |
| 537 | } | |
| 538 | } | |
| 539 | Ok(()) | |
| 540 | }) | |
| 541 | .ok()?; | |
| 542 | ||
| 543 | let mut raw = vec![0u8; w * h * 4]; | |
| 544 | let mut raw_f16 = vec![0u16; w * h * 4]; | |
| 545 | for i in 0..(w * h) { | |
| 546 | let off = i * 4; | |
| 547 | let mut r_raw = raw_f32[off]; | |
| 548 | let mut g_raw = if g_loc.is_some() { raw_f32[off + 1] } else { r_raw }; | |
| 549 | let mut b_raw = if b_loc.is_some() { raw_f32[off + 2] } else { r_raw }; | |
| 550 | let a_raw = if a_loc.is_some() { raw_f32[off + 3] } else { 1.0 }; | |
| 551 | ||
| 552 | if post_multiply && a_raw < 1.0 { | |
| 553 | let a_mul = a_raw.clamp(0.0, 1.0); | |
| 554 | r_raw *= a_mul; | |
| 555 | g_raw *= a_mul; | |
| 556 | b_raw *= a_mul; | |
| 557 | } | |
| 558 | raw_f32[off] = r_raw; | |
| 559 | raw_f32[off + 1] = g_raw; | |
| 560 | raw_f32[off + 2] = b_raw; | |
| 561 | raw_f32[off + 3] = a_raw; | |
| 562 | ||
| 563 | raw_f16[off] = f32_to_f16_bits(r_raw); | |
| 564 | raw_f16[off + 1] = f32_to_f16_bits(g_raw); | |
| 565 | raw_f16[off + 2] = f32_to_f16_bits(b_raw); | |
| 566 | raw_f16[off + 3] = f32_to_f16_bits(a_raw); | |
| 567 | ||
| 568 | let a = a_raw.clamp(0.0, 1.0); | |
| 569 | let r = r_raw.clamp(0.0, 1.0); | |
| 570 | let g = g_raw.clamp(0.0, 1.0); | |
| 571 | let b = b_raw.clamp(0.0, 1.0); | |
| 572 | raw[off] = (r * 255.0 + 0.5) as u8; | |
| 573 | raw[off + 1] = (g * 255.0 + 0.5) as u8; | |
| 574 | raw[off + 2] = (b * 255.0 + 0.5) as u8; | |
| 575 | raw[off + 3] = (a * 255.0 + 0.5) as u8; | |
| 576 | } | |
| 577 | Some(DecodedImage { | |
| 578 | size: [w as u32, h as u32], | |
| 579 | rgba: Arc::new(raw), | |
| 580 | rgba_f16: Some(Arc::new(raw_f16)), | |
| 581 | rgba_f32: Some(Arc::new(raw_f32)), | |
| 582 | }) | |
| 583 | } | |
| 584 | ||
| 585 | 51 | const MAX_VIDEO_DECODERS: usize = 12; |
| 586 | 52 | |
| 587 | 53 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| ... | ... | @@ -865,244 +331,6 @@ pub fn resolve_clip_path(clip: &ClipSpec, frame: i64) -> Option<PathBuf> { |
| 865 | 331 | let candidate = base.with_file_name(new_name); |
| 866 | 332 | candidate.exists().then_some(candidate) |
| 867 | 333 | } |
| 868 | ||
| 869 | /// Parameters for rasterizing a Fusion `TextPlus` node with [`crate::text_engine`]. | |
| 870 | pub struct TextPlusParams<'a> { | |
| 871 | pub text: &'a str, | |
| 872 | pub font_family: &'a str, | |
| 873 | pub font_style: &'a str, | |
| 874 | pub size_norm: f32, // Fusion Size (fraction of canvas width, ~0.08 default) | |
| 875 | pub tracking: f32, // Fusion CharacterSpacing (1.0 = normal) | |
| 876 | pub line_spacing: f32, // Fusion LineSpacing (1.0 = normal) | |
| 877 | pub center: [f32; 2], // Fusion normalized Y-up (0.5, 0.5) | |
| 878 | pub h_lcr: f32, // Fusion HorizontalLeftCenterRight: -1=Left, 0=Center, +1=Right | |
| 879 | pub v_tcb: f32, // Fusion VerticalTopCenterBottom: -1=Top, 0=Center, +1=Bottom | |
| 880 | pub fill_rgba: [f32; 4], | |
| 881 | pub write_on_start: f32, | |
| 882 | pub write_on_end: f32, | |
| 883 | pub shear_x: f32, // Fusion CharacterShearX | |
| 884 | pub cls: &'a [ClsEntry], // Optional StyledTextCLS per-character overrides | |
| 885 | pub outline_rgba: [f32; 4], // Fusion Element 2 color, already scaled by Opacity2 | |
| 886 | pub outline_width_px: f32, // Fusion Thickness2 as a full stroke width; 0 disables the outline | |
| 887 | } | |
| 888 | ||
| 889 | /// Rasterizes a `TextPlus` layer (outline under fill) into premultiplied RGBA8 with automatic | |
| 890 | /// Domain-of-Definition (DoD) expansion so off-screen glyphs (e.g. text shifted into frame by a | |
| 891 | /// downstream `Transform`) are never clipped; `dod_pad` adds margin for decoration drawn from | |
| 892 | /// the result. Returns `(tex_size, dod_origin_px, rgba)`. | |
| 893 | pub fn rasterize_text_plus(dims: [u32; 2], params: &TextPlusParams<'_>, dod_pad: i32) -> ([u32; 2], [i32; 2], Vec<u8>) { | |
| 894 | let [w, h] = dims; | |
| 895 | let empty = || (dims, [0, 0], vec![0u8; (w as usize) * (h as usize) * 4]); | |
| 896 | if params.text.is_empty() || params.size_norm <= 1e-4 { | |
| 897 | return empty(); | |
| 898 | } | |
| 899 | ||
| 900 | // Compute WriteOn character range on the FULL string so glyph layout never shifts as letters type on! | |
| 901 | let total_chars = params.text.chars().count(); | |
| 902 | let c_start = ((params.write_on_start.clamp(0.0, 1.0)) * total_chars as f32).floor() as usize; | |
| 903 | let c_end = ((params.write_on_end.clamp(0.0, 1.0)) * total_chars as f32).ceil() as usize; | |
| 904 | if c_start >= c_end || c_start >= total_chars { | |
| 905 | return empty(); | |
| 906 | } | |
| 907 | let Some(font) = text_engine::Font::find(params.font_family, params.font_style) else { | |
| 908 | return empty(); | |
| 909 | }; | |
| 910 | let face = font.face(); | |
| 911 | ||
| 912 | // Build per-character CLS X/Y offsets (Fusion prop 1350 = X, 1351 = Y in normalized width units). | |
| 913 | let mut cls_dx = vec![0.0_f32; total_chars]; | |
| 914 | let mut cls_dy = vec![0.0_f32; total_chars]; | |
| 915 | for e in params.cls { | |
| 916 | let s = e.start.min(total_chars); | |
| 917 | let end = (e.end + 1).min(total_chars); | |
| 918 | for i in s..end { | |
| 919 | if e.prop_id == 1350 { | |
| 920 | cls_dx[i] = e.value; | |
| 921 | } else if e.prop_id == 1351 { | |
| 922 | cls_dy[i] = e.value; | |
| 923 | } | |
| 924 | } | |
| 925 | } | |
| 926 | ||
| 927 | // Fusion Text+ `Size` defines the line box (ascent + descent) as `0.8 * Size * W` | |
| 928 | // (calibrated across Open Sans, Liberation Sans, DejaVu Sans, and AT Name Sans Text | |
| 929 | // on 16:9, square, and vertical frames). | |
| 930 | let line_box_px = params.size_norm * w as f32 * 0.8; | |
| 931 | let (ascent_em, descent_em) = text_engine::vertical_metrics(&face); | |
| 932 | let font_px = (line_box_px / (ascent_em + descent_em).max(0.5)).clamp(6.0, 1024.0); | |
| 933 | let descent = descent_em * font_px; | |
| 934 | ||
| 935 | // Fusion `CharacterSpacing = 1.0` adds nothing; each +1.0 adds `Size * W` between characters, | |
| 936 | // independent of the font (probed on Fusion 21.1 with Liberation Sans, 8 gaps, 1.3 and 2.0). | |
| 937 | let extra_track = (params.tracking - 1.0) * params.size_norm * w as f32; | |
| 938 | ||
| 939 | let lines: Vec<&str> = params.text.split('\n').collect(); | |
| 940 | let line_height = (line_box_px * params.line_spacing.max(0.05)).max(1.0); | |
| 941 | ||
| 942 | // Fusion Center is Y-up; convert to top-down pixel coordinates. | |
| 943 | let cx = params.center[0] * w as f32; | |
| 944 | let cy = (1.0 - params.center[1]) * h as f32; | |
| 945 | ||
| 946 | // VerticalTopCenterBottom: -1=Top (baseline_0 = cy + font_px - descent), 0=Center, +1=Bottom (last baseline = cy). | |
| 947 | let v_frac = params.v_tcb.clamp(-1.0, 1.0) * 0.5 + 0.5; | |
| 948 | let top_anchor = (font_px - descent).max(0.0); | |
| 949 | let block_h = (lines.len().saturating_sub(1) as f32) * line_height + top_anchor; | |
| 950 | let baseline_0 = cy + top_anchor - v_frac * block_h; | |
| 951 | let h_frac = params.h_lcr.clamp(-1.0, 1.0) * 0.5 + 0.5; | |
| 952 | ||
| 953 | let outlined = params.outline_width_px > 1e-3 && params.outline_rgba[3] > 0.0; | |
| 954 | ||
| 955 | // Pass 1: vector-rasterize every visible glyph and collect exact bounds to expand the DoD. | |
| 956 | let mut placed = Vec::new(); | |
| 957 | let (mut min_x, mut min_y, mut max_x, mut max_y) = (0i32, 0i32, w as i32, h as i32); | |
| 958 | let mut grow = |c: &text_engine::Coverage, pad: i32| { | |
| 959 | min_x = min_x.min(c.left - pad); | |
| 960 | min_y = min_y.min(c.top - pad); | |
| 961 | max_x = max_x.max(c.left + c.width as i32 + pad); | |
| 962 | max_y = max_y.max(c.top + c.height as i32 + pad); | |
| 963 | }; | |
| 964 | ||
| 965 | let mut char_cursor = 0usize; | |
| 966 | for (li, line_str) in lines.iter().enumerate() { | |
| 967 | let shaped = text_engine::shape_line(&face, line_str, font_px, extra_track); | |
| 968 | let start_x = cx - shaped.width * h_frac; | |
| 969 | let baseline_y = baseline_0 + (li as f32) * line_height; | |
| 970 | for g in &shaped.glyphs { | |
| 971 | let idx = char_cursor + g.char_index; | |
| 972 | if idx < c_start || idx >= c_end { | |
| 973 | continue; | |
| 974 | } | |
| 975 | let origin = [ | |
| 976 | start_x + g.x + cls_dx[idx] * w as f32, | |
| 977 | baseline_y - g.y - cls_dy[idx] * w as f32, | |
| 978 | ]; | |
| 979 | let path = text_engine::glyph_path(&font, &face, g.id, font_px, origin, -params.shear_x); | |
| 980 | let fill = text_engine::fill(&path); | |
| 981 | let line = if outlined { text_engine::stroke(&path, params.outline_width_px) } else { None }; | |
| 982 | if let Some(c) = &fill { | |
| 983 | grow(c, dod_pad); | |
| 984 | } | |
| 985 | if let Some(c) = &line { | |
| 986 | grow(c, dod_pad); | |
| 987 | } | |
| 988 | placed.push((fill, line)); | |
| 989 | } | |
| 990 | char_cursor += line_str.chars().count() + 1; // + '\n' separator | |
| 991 | } | |
| 992 | ||
| 993 | // Clamp DoD expansion safely to at most +/- 1 canvas width/height so GPU textures stay bounded. | |
| 994 | let origin_x = min_x.max(-(w as i32)); | |
| 995 | let origin_y = min_y.max(-(h as i32)); | |
| 996 | let end_x = max_x.min((w as i32) * 2); | |
| 997 | let end_y = max_y.min((h as i32) * 2); | |
| 998 | let tw = (end_x - origin_x).max(1) as usize; | |
| 999 | let th = (end_y - origin_y).max(1) as usize; | |
| 1000 | ||
| 1001 | let mut fill_plane = vec![0u8; tw * th]; | |
| 1002 | let mut line_plane = vec![0.0_f32; if outlined { tw * th } else { 0 }]; | |
| 1003 | for (fill, line) in &placed { | |
| 1004 | if let Some(c) = fill { | |
| 1005 | blit_max(c, [origin_x, origin_y], [tw, th], |i, a| fill_plane[i] = fill_plane[i].max(a)); | |
| 1006 | } | |
| 1007 | if let Some(c) = line { | |
| 1008 | blit_max(c, [origin_x, origin_y], [tw, th], |i, a| line_plane[i] = line_plane[i].max(a as f32 / 255.0)); | |
| 1009 | } | |
| 1010 | } | |
| 1011 | ||
| 1012 | let (fc, oc) = (params.fill_rgba, params.outline_rgba); | |
| 1013 | let mut out = vec![0u8; tw * th * 4]; | |
| 1014 | for (i, px) in out.chunks_exact_mut(4).enumerate() { | |
| 1015 | let fa = fill_plane[i] as f32 / 255.0 * fc[3]; | |
| 1016 | let oa = if outlined { line_plane[i] * oc[3] } else { 0.0 }; | |
| 1017 | if fa <= 0.0 && oa <= 0.0 { | |
| 1018 | continue; | |
| 1019 | } | |
| 1020 | let under = 1.0 - fa; | |
| 1021 | for k in 0..3 { | |
| 1022 | let v = fc[k] * fa + oc[k] * oa * under; | |
| 1023 | px[k] = (v * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | |
| 1024 | } | |
| 1025 | px[3] = ((fa + oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | |
| 1026 | } | |
| 1027 | ||
| 1028 | ([tw as u32, th as u32], [origin_x, origin_y], out) | |
| 1029 | } | |
| 1030 | ||
| 1031 | /// Calls `f(plane_index, coverage)` for every non-zero coverage pixel that lands inside the plane. | |
| 1032 | fn blit_max(c: &text_engine::Coverage, origin: [i32; 2], plane: [usize; 2], mut f: impl FnMut(usize, u8)) { | |
| 1033 | for y in 0..c.height as i32 { | |
| 1034 | let py = c.top + y - origin[1]; | |
| 1035 | if py < 0 || py >= plane[1] as i32 { | |
| 1036 | continue; | |
| 1037 | } | |
| 1038 | for x in 0..c.width as i32 { | |
| 1039 | let px = c.left + x - origin[0]; | |
| 1040 | let a = c.data[(y as u32 * c.width + x as u32) as usize]; | |
| 1041 | if a > 0 && px >= 0 && px < plane[0] as i32 { | |
| 1042 | f(py as usize * plane[0] + px as usize, a); | |
| 1043 | } | |
| 1044 | } | |
| 1045 | } | |
| 1046 | } | |
| 1047 | ||
| 1048 | /// Rasterizes a closed Fusion `PolylineMask` into RGBA8 `[w, h]` via `zeno`. | |
| 1049 | pub fn rasterize_polyline_mask( | |
| 1050 | dims: [u32; 2], | |
| 1051 | points: &[PolyPoint], | |
| 1052 | center: [f32; 2], | |
| 1053 | invert: bool, | |
| 1054 | level: f32, | |
| 1055 | ) -> Vec<u8> { | |
| 1056 | let [w, h] = dims; | |
| 1057 | let mut out = vec![0u8; (w as usize) * (h as usize) * 4]; | |
| 1058 | if points.len() < 2 { | |
| 1059 | return out; | |
| 1060 | } | |
| 1061 | let to_px = |x: f64, y: f64| -> [f32; 2] { | |
| 1062 | let fx = (center[0] + x as f32) * w as f32; | |
| 1063 | let fy = (1.0 - (center[1] + y as f32)) * h as f32; | |
| 1064 | [fx, fy] | |
| 1065 | }; | |
| 1066 | let mut cmds = Vec::new(); | |
| 1067 | let p0 = to_px(points[0].x, points[0].y); | |
| 1068 | cmds.push(zeno::Command::MoveTo(zeno::Vector::new(p0[0], p0[1]))); | |
| 1069 | for i in 0..points.len() { | |
| 1070 | let a = &points[i]; | |
| 1071 | let b = &points[(i + 1) % points.len()]; | |
| 1072 | let c1 = to_px(a.x + a.rx, a.y + a.ry); | |
| 1073 | let c2 = to_px(b.x + b.lx, b.y + b.ly); | |
| 1074 | let p = to_px(b.x, b.y); | |
| 1075 | if a.linear && b.linear { | |
| 1076 | cmds.push(zeno::Command::LineTo(zeno::Vector::new(p[0], p[1]))); | |
| 1077 | } else { | |
| 1078 | cmds.push(zeno::Command::CurveTo( | |
| 1079 | zeno::Vector::new(c1[0], c1[1]), | |
| 1080 | zeno::Vector::new(c2[0], c2[1]), | |
| 1081 | zeno::Vector::new(p[0], p[1]), | |
| 1082 | )); | |
| 1083 | } | |
| 1084 | } | |
| 1085 | cmds.push(zeno::Command::Close); | |
| 1086 | ||
| 1087 | let mut mask = vec![0u8; (w as usize) * (h as usize)]; | |
| 1088 | let mut scratch = zeno::Scratch::new(); | |
| 1089 | let placement = zeno::Mask::with_scratch(&cmds, &mut scratch) | |
| 1090 | .size(w, h) | |
| 1091 | .origin(zeno::Origin::TopLeft) | |
| 1092 | .render_into(&mut mask, None); | |
| 1093 | let _ = placement; | |
| 1094 | for (i, &cov) in mask.iter().enumerate() { | |
| 1095 | let mut a = cov as f32 / 255.0; | |
| 1096 | if invert { a = 1.0 - a; } | |
| 1097 | let b = (a * level * 255.0).clamp(0.0, 255.0) as u8; | |
| 1098 | out[i * 4] = b; | |
| 1099 | out[i * 4 + 1] = b; | |
| 1100 | out[i * 4 + 2] = b; | |
| 1101 | out[i * 4 + 3] = b; | |
| 1102 | } | |
| 1103 | out | |
| 1104 | } | |
| 1105 | ||
| 1106 | 334 | #[cfg(test)] |
| 1107 | 335 | mod tests { |
| 1108 | 336 | use super::*; |
| ... | ... | @@ -1344,7 +572,7 @@ mod tests { |
| 1344 | 572 | |
| 1345 | 573 | #[test] |
| 1346 | 574 | fn exr_channel_cache_isolation_and_non_beauty_guard() { |
| 1347 | use exr::prelude::*; | |
| 575 | use ::exr::prelude::*; | |
| 1348 | 576 | let dir = std::env::temp_dir().join(format!("fission_exr_chan_test_{}", std::process::id())); |
| 1349 | 577 | let _ = std::fs::remove_dir_all(&dir); |
| 1350 | 578 | std::fs::create_dir_all(&dir).unwrap(); |
crates/engine/src/media/exr.rs created+280| ... | ... | @@ -0,0 +1,280 @@ |
| 1 | use super::*; | |
| 2 | ||
| 3 | /// Converts IEEE-754 `f32` to IEEE-754 binary16 (`f16`) bits for direct `Rgba16Float` GPU upload. | |
| 4 | #[inline] | |
| 5 | pub fn f32_to_f16_bits(val: f32) -> u16 { | |
| 6 | let bits = val.to_bits(); | |
| 7 | let sign = ((bits >> 16) & 0x8000) as u16; | |
| 8 | let exp = ((bits >> 23) & 0xFF) as i32; | |
| 9 | let frac = bits & 0x007F_FFFF; | |
| 10 | ||
| 11 | if exp == 0xFF { | |
| 12 | return if frac != 0 { 0 } else { sign | 0x7C00 }; | |
| 13 | } | |
| 14 | let new_exp = exp - 127 + 15; | |
| 15 | if new_exp >= 31 { | |
| 16 | return sign | 0x7BFF; | |
| 17 | } | |
| 18 | if new_exp <= 0 { | |
| 19 | if new_exp < -10 { | |
| 20 | return sign; | |
| 21 | } | |
| 22 | let m = (frac | 0x0080_0000) >> (1 - new_exp + 13); | |
| 23 | return sign | (m as u16); | |
| 24 | } | |
| 25 | let rounded = frac + 0x1000; | |
| 26 | if rounded >= 0x0080_0000 { | |
| 27 | let e = new_exp + 1; | |
| 28 | return if e >= 31 { sign | 0x7BFF } else { sign | ((e as u16) << 10) }; | |
| 29 | } | |
| 30 | sign | ((new_exp as u16) << 10) | ((rounded >> 13) as u16) | |
| 31 | } | |
| 32 | ||
| 33 | pub(super) fn is_exr_path(path: &Path) -> bool { | |
| 34 | path.extension() | |
| 35 | .and_then(|e| e.to_str()) | |
| 36 | .is_some_and(|e| e.eq_ignore_ascii_case("exr")) | |
| 37 | } | |
| 38 | ||
| 39 | pub(super) fn full_exr_channel_name(layer_name: Option<&str>, ch_name: &str) -> String { | |
| 40 | match layer_name { | |
| 41 | Some(ln) if !ln.is_empty() && !ch_name.starts_with(&format!("{ln}.")) => { | |
| 42 | format!("{ln}.{ch_name}") | |
| 43 | } | |
| 44 | _ => ch_name.to_owned(), | |
| 45 | } | |
| 46 | } | |
| 47 | ||
| 48 | pub(super) fn normalize_channel_opt(opt: Option<&str>) -> Option<String> { | |
| 49 | opt.map(str::trim) | |
| 50 | .filter(|s| !s.is_empty()) | |
| 51 | .map(str::to_owned) | |
| 52 | } | |
| 53 | ||
| 54 | pub(super) fn apply_post_multiply_rgba8(raw: &mut [u8]) { | |
| 55 | for px in raw.chunks_exact_mut(4) { | |
| 56 | let a = u16::from(px[3]); | |
| 57 | if a < 255 { | |
| 58 | px[0] = ((u16::from(px[0]) * a + 127) / 255) as u8; | |
| 59 | px[1] = ((u16::from(px[1]) * a + 127) / 255) as u8; | |
| 60 | px[2] = ((u16::from(px[2]) * a + 127) / 255) as u8; | |
| 61 | } | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | pub(super) fn find_exr_channel_loc( | |
| 66 | headers: &[::exr::meta::header::Header], | |
| 67 | w: usize, | |
| 68 | h: usize, | |
| 69 | preferred: Option<&str>, | |
| 70 | beauty_upper: &str, | |
| 71 | beauty_lower: &str, | |
| 72 | ) -> Option<(usize, usize)> { | |
| 73 | let valid_layer = |header: &::exr::meta::header::Header| { | |
| 74 | !header.deep && header.layer_size.width() == w && header.layer_size.height() == h | |
| 75 | }; | |
| 76 | ||
| 77 | if let Some(pref) = preferred.map(str::trim).filter(|s| !s.is_empty()) { | |
| 78 | for (li, header) in headers.iter().enumerate() { | |
| 79 | if !valid_layer(header) { | |
| 80 | continue; | |
| 81 | } | |
| 82 | let lname = header.own_attributes.layer_name.as_ref().map(|t| t.to_string()); | |
| 83 | for (ci, ch) in header.channels.list.iter().enumerate() { | |
| 84 | if ch.sampling != ::exr::math::Vec2(1, 1) { | |
| 85 | continue; | |
| 86 | } | |
| 87 | let full = full_exr_channel_name(lname.as_deref(), &ch.name.to_string()); | |
| 88 | if full == pref { | |
| 89 | return Some((li, ci)); | |
| 90 | } | |
| 91 | } | |
| 92 | } | |
| 93 | let suffix = format!(".{pref}"); | |
| 94 | for (li, header) in headers.iter().enumerate() { | |
| 95 | if !valid_layer(header) { | |
| 96 | continue; | |
| 97 | } | |
| 98 | let lname = header.own_attributes.layer_name.as_ref().map(|t| t.to_string()); | |
| 99 | for (ci, ch) in header.channels.list.iter().enumerate() { | |
| 100 | if ch.sampling != ::exr::math::Vec2(1, 1) { | |
| 101 | continue; | |
| 102 | } | |
| 103 | let full = full_exr_channel_name(lname.as_deref(), &ch.name.to_string()); | |
| 104 | if full.ends_with(&suffix) { | |
| 105 | return Some((li, ci)); | |
| 106 | } | |
| 107 | } | |
| 108 | } | |
| 109 | return None; | |
| 110 | } | |
| 111 | ||
| 112 | // Beauty pass fallback: check ViewLayer.Combined.<C>, Combined.<C>, *.Combined.<C>, | |
| 113 | // then exact bare <C>/<c>. Never match non-beauty layers such as CryptoObject00.R! | |
| 114 | let view_combined = format!("ViewLayer.Combined.{beauty_upper}"); | |
| 115 | let combined = format!("Combined.{beauty_upper}"); | |
| 116 | let any_combined = format!(".Combined.{beauty_upper}"); | |
| 117 | let matcher_passes: [&dyn Fn(&str) -> bool; 4] = [ | |
| 118 | &|full: &str| full == view_combined, | |
| 119 | &|full: &str| full == combined || full.ends_with(&any_combined), | |
| 120 | &|full: &str| full == beauty_upper, | |
| 121 | &|full: &str| full == beauty_lower, | |
| 122 | ]; | |
| 123 | for pred in matcher_passes { | |
| 124 | for (li, header) in headers.iter().enumerate() { | |
| 125 | if !valid_layer(header) { | |
| 126 | continue; | |
| 127 | } | |
| 128 | let lname = header.own_attributes.layer_name.as_ref().map(|t| t.to_string()); | |
| 129 | for (ci, ch) in header.channels.list.iter().enumerate() { | |
| 130 | if ch.sampling != ::exr::math::Vec2(1, 1) { | |
| 131 | continue; | |
| 132 | } | |
| 133 | let full = full_exr_channel_name(lname.as_deref(), &ch.name.to_string()); | |
| 134 | if pred(&full) { | |
| 135 | return Some((li, ci)); | |
| 136 | } | |
| 137 | } | |
| 138 | } | |
| 139 | } | |
| 140 | None | |
| 141 | } | |
| 142 | ||
| 143 | /// Decodes a single-layer or multi-layer (Blender `ViewLayer.Combined.R/G/B/A`) OpenEXR | |
| 144 | /// selectively for only the matched RGBA channels, producing clamped RGBA8 and linear HDR | |
| 145 | /// RGBA16F/RGBA32F buffers without decoding unrelated AOV/Cryptomatte layers. | |
| 146 | pub(super) fn decode_exr_rgba( | |
| 147 | path: &Path, | |
| 148 | red_name: Option<&str>, | |
| 149 | green_name: Option<&str>, | |
| 150 | blue_name: Option<&str>, | |
| 151 | alpha_name: Option<&str>, | |
| 152 | post_multiply: bool, | |
| 153 | ) -> Option<DecodedImage> { | |
| 154 | use ::exr::block::reader::ChunksReader; | |
| 155 | use ::exr::meta::attribute::SampleType; | |
| 156 | use ::exr::prelude::*; | |
| 157 | ||
| 158 | let file = std::fs::File::open(path).ok()?; | |
| 159 | let chunks_reader = ::exr::block::read(std::io::BufReader::new(file), false).ok()?; | |
| 160 | let first_header = chunks_reader.headers().first()?; | |
| 161 | let w = first_header.layer_size.width(); | |
| 162 | let h = first_header.layer_size.height(); | |
| 163 | if w == 0 || h == 0 { | |
| 164 | return None; | |
| 165 | } | |
| 166 | ||
| 167 | let r_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, red_name, "R", "r")?; | |
| 168 | let g_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, green_name, "G", "g"); | |
| 169 | let b_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, blue_name, "B", "b"); | |
| 170 | let a_loc = find_exr_channel_loc(chunks_reader.headers(), w, h, alpha_name, "A", "a"); | |
| 171 | ||
| 172 | let target_locs = [Some(r_loc), g_loc, b_loc, a_loc]; | |
| 173 | let mut needed_layers: Vec<usize> = target_locs.iter().flatten().map(|(l, _)| *l).collect(); | |
| 174 | needed_layers.sort_unstable(); | |
| 175 | needed_layers.dedup(); | |
| 176 | ||
| 177 | let mut raw_f32 = vec![0.0_f32; w * h * 4]; | |
| 178 | let mut line_f32 = Vec::<f32>::new(); | |
| 179 | let mut line_f16 = Vec::<f16>::new(); | |
| 180 | let mut line_u32 = Vec::<u32>::new(); | |
| 181 | ||
| 182 | chunks_reader | |
| 183 | .filter_chunks(false, |_, tile, block| { | |
| 184 | tile.is_largest_resolution_level() && needed_layers.contains(&block.layer) | |
| 185 | }) | |
| 186 | .ok()? | |
| 187 | .decompress_parallel(false, |meta, block| { | |
| 188 | let header = &meta.headers[block.index.layer]; | |
| 189 | for line in block.lines(&header.channels) { | |
| 190 | let loc = (line.location.layer, line.location.channel); | |
| 191 | let mut comp_mask = [false; 4]; | |
| 192 | for (c, t) in target_locs.iter().enumerate() { | |
| 193 | if *t == Some(loc) { | |
| 194 | comp_mask[c] = true; | |
| 195 | } | |
| 196 | } | |
| 197 | if !comp_mask.iter().any(|&m| m) { | |
| 198 | continue; | |
| 199 | } | |
| 200 | let y = line.location.position.y(); | |
| 201 | let x0 = line.location.position.x(); | |
| 202 | let len = line.location.sample_count; | |
| 203 | if y >= h || x0 + len > w { | |
| 204 | continue; | |
| 205 | } | |
| 206 | ||
| 207 | line_f32.clear(); | |
| 208 | match header.channels.list[line.location.channel].sample_type { | |
| 209 | SampleType::F32 => { | |
| 210 | line_f32.resize(len, 0.0); | |
| 211 | line.read_samples_into_slice(&mut line_f32)?; | |
| 212 | } | |
| 213 | SampleType::F16 => { | |
| 214 | line_f16.resize(len, f16::ZERO); | |
| 215 | line.read_samples_into_slice(&mut line_f16)?; | |
| 216 | line_f32.extend(line_f16.iter().map(|v| v.to_f32())); | |
| 217 | } | |
| 218 | SampleType::U32 => { | |
| 219 | line_u32.resize(len, 0); | |
| 220 | line.read_samples_into_slice(&mut line_u32)?; | |
| 221 | line_f32.extend(line_u32.iter().map(|&v| v as f32)); | |
| 222 | } | |
| 223 | } | |
| 224 | ||
| 225 | let row_base = (y * w + x0) * 4; | |
| 226 | for (dx, &sample) in line_f32.iter().enumerate() { | |
| 227 | let val = if sample.is_nan() { 0.0 } else { sample }; | |
| 228 | let px_off = row_base + dx * 4; | |
| 229 | for (c, &enabled) in comp_mask.iter().enumerate() { | |
| 230 | if enabled { | |
| 231 | raw_f32[px_off + c] = val; | |
| 232 | } | |
| 233 | } | |
| 234 | } | |
| 235 | } | |
| 236 | Ok(()) | |
| 237 | }) | |
| 238 | .ok()?; | |
| 239 | ||
| 240 | let mut raw = vec![0u8; w * h * 4]; | |
| 241 | let mut raw_f16 = vec![0u16; w * h * 4]; | |
| 242 | for i in 0..(w * h) { | |
| 243 | let off = i * 4; | |
| 244 | let mut r_raw = raw_f32[off]; | |
| 245 | let mut g_raw = if g_loc.is_some() { raw_f32[off + 1] } else { r_raw }; | |
| 246 | let mut b_raw = if b_loc.is_some() { raw_f32[off + 2] } else { r_raw }; | |
| 247 | let a_raw = if a_loc.is_some() { raw_f32[off + 3] } else { 1.0 }; | |
| 248 | ||
| 249 | if post_multiply && a_raw < 1.0 { | |
| 250 | let a_mul = a_raw.clamp(0.0, 1.0); | |
| 251 | r_raw *= a_mul; | |
| 252 | g_raw *= a_mul; | |
| 253 | b_raw *= a_mul; | |
| 254 | } | |
| 255 | raw_f32[off] = r_raw; | |
| 256 | raw_f32[off + 1] = g_raw; | |
| 257 | raw_f32[off + 2] = b_raw; | |
| 258 | raw_f32[off + 3] = a_raw; | |
| 259 | ||
| 260 | raw_f16[off] = f32_to_f16_bits(r_raw); | |
| 261 | raw_f16[off + 1] = f32_to_f16_bits(g_raw); | |
| 262 | raw_f16[off + 2] = f32_to_f16_bits(b_raw); | |
| 263 | raw_f16[off + 3] = f32_to_f16_bits(a_raw); | |
| 264 | ||
| 265 | let a = a_raw.clamp(0.0, 1.0); | |
| 266 | let r = r_raw.clamp(0.0, 1.0); | |
| 267 | let g = g_raw.clamp(0.0, 1.0); | |
| 268 | let b = b_raw.clamp(0.0, 1.0); | |
| 269 | raw[off] = (r * 255.0 + 0.5) as u8; | |
| 270 | raw[off + 1] = (g * 255.0 + 0.5) as u8; | |
| 271 | raw[off + 2] = (b * 255.0 + 0.5) as u8; | |
| 272 | raw[off + 3] = (a * 255.0 + 0.5) as u8; | |
| 273 | } | |
| 274 | Some(DecodedImage { | |
| 275 | size: [w as u32, h as u32], | |
| 276 | rgba: Arc::new(raw), | |
| 277 | rgba_f16: Some(Arc::new(raw_f16)), | |
| 278 | rgba_f32: Some(Arc::new(raw_f32)), | |
| 279 | }) | |
| 280 | } |
crates/engine/src/media/raster.rs created+238| ... | ... | @@ -0,0 +1,238 @@ |
| 1 | use super::*; | |
| 2 | ||
| 3 | /// Parameters for rasterizing a Fusion `TextPlus` node with [`crate::text_engine`]. | |
| 4 | pub struct TextPlusParams<'a> { | |
| 5 | pub text: &'a str, | |
| 6 | pub font_family: &'a str, | |
| 7 | pub font_style: &'a str, | |
| 8 | pub size_norm: f32, // Fusion Size (fraction of canvas width, ~0.08 default) | |
| 9 | pub tracking: f32, // Fusion CharacterSpacing (1.0 = normal) | |
| 10 | pub line_spacing: f32, // Fusion LineSpacing (1.0 = normal) | |
| 11 | pub center: [f32; 2], // Fusion normalized Y-up (0.5, 0.5) | |
| 12 | pub h_lcr: f32, // Fusion HorizontalLeftCenterRight: -1=Left, 0=Center, +1=Right | |
| 13 | pub v_tcb: f32, // Fusion VerticalTopCenterBottom: -1=Top, 0=Center, +1=Bottom | |
| 14 | pub fill_rgba: [f32; 4], | |
| 15 | pub write_on_start: f32, | |
| 16 | pub write_on_end: f32, | |
| 17 | pub shear_x: f32, // Fusion CharacterShearX | |
| 18 | pub cls: &'a [ClsEntry], // Optional StyledTextCLS per-character overrides | |
| 19 | pub outline_rgba: [f32; 4], // Fusion Element 2 color, already scaled by Opacity2 | |
| 20 | pub outline_width_px: f32, // Fusion Thickness2 as a full stroke width; 0 disables the outline | |
| 21 | } | |
| 22 | ||
| 23 | /// Rasterizes a `TextPlus` layer (outline under fill) into premultiplied RGBA8 with automatic | |
| 24 | /// Domain-of-Definition (DoD) expansion so off-screen glyphs (e.g. text shifted into frame by a | |
| 25 | /// downstream `Transform`) are never clipped; `dod_pad` adds margin for decoration drawn from | |
| 26 | /// the result. Returns `(tex_size, dod_origin_px, rgba)`. | |
| 27 | pub fn rasterize_text_plus(dims: [u32; 2], params: &TextPlusParams<'_>, dod_pad: i32) -> ([u32; 2], [i32; 2], Vec<u8>) { | |
| 28 | let [w, h] = dims; | |
| 29 | let empty = || (dims, [0, 0], vec![0u8; (w as usize) * (h as usize) * 4]); | |
| 30 | if params.text.is_empty() || params.size_norm <= 1e-4 { | |
| 31 | return empty(); | |
| 32 | } | |
| 33 | ||
| 34 | // Compute WriteOn character range on the FULL string so glyph layout never shifts as letters type on! | |
| 35 | let total_chars = params.text.chars().count(); | |
| 36 | let c_start = ((params.write_on_start.clamp(0.0, 1.0)) * total_chars as f32).floor() as usize; | |
| 37 | let c_end = ((params.write_on_end.clamp(0.0, 1.0)) * total_chars as f32).ceil() as usize; | |
| 38 | if c_start >= c_end || c_start >= total_chars { | |
| 39 | return empty(); | |
| 40 | } | |
| 41 | let Some(font) = text_engine::Font::find(params.font_family, params.font_style) else { | |
| 42 | return empty(); | |
| 43 | }; | |
| 44 | let face = font.face(); | |
| 45 | ||
| 46 | // Build per-character CLS X/Y offsets (Fusion prop 1350 = X, 1351 = Y in normalized width units). | |
| 47 | let mut cls_dx = vec![0.0_f32; total_chars]; | |
| 48 | let mut cls_dy = vec![0.0_f32; total_chars]; | |
| 49 | for e in params.cls { | |
| 50 | let s = e.start.min(total_chars); | |
| 51 | let end = (e.end + 1).min(total_chars); | |
| 52 | for i in s..end { | |
| 53 | if e.prop_id == 1350 { | |
| 54 | cls_dx[i] = e.value; | |
| 55 | } else if e.prop_id == 1351 { | |
| 56 | cls_dy[i] = e.value; | |
| 57 | } | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | // Fusion Text+ `Size` defines the line box (ascent + descent) as `0.8 * Size * W` | |
| 62 | // (calibrated across Open Sans, Liberation Sans, DejaVu Sans, and AT Name Sans Text | |
| 63 | // on 16:9, square, and vertical frames). | |
| 64 | let line_box_px = params.size_norm * w as f32 * 0.8; | |
| 65 | let (ascent_em, descent_em) = text_engine::vertical_metrics(&face); | |
| 66 | let font_px = (line_box_px / (ascent_em + descent_em).max(0.5)).clamp(6.0, 1024.0); | |
| 67 | let descent = descent_em * font_px; | |
| 68 | ||
| 69 | // Fusion `CharacterSpacing = 1.0` adds nothing; each +1.0 adds `Size * W` between characters, | |
| 70 | // independent of the font (probed on Fusion 21.1 with Liberation Sans, 8 gaps, 1.3 and 2.0). | |
| 71 | let extra_track = (params.tracking - 1.0) * params.size_norm * w as f32; | |
| 72 | ||
| 73 | let lines: Vec<&str> = params.text.split('\n').collect(); | |
| 74 | let line_height = (line_box_px * params.line_spacing.max(0.05)).max(1.0); | |
| 75 | ||
| 76 | // Fusion Center is Y-up; convert to top-down pixel coordinates. | |
| 77 | let cx = params.center[0] * w as f32; | |
| 78 | let cy = (1.0 - params.center[1]) * h as f32; | |
| 79 | ||
| 80 | // VerticalTopCenterBottom: -1=Top (baseline_0 = cy + font_px - descent), 0=Center, +1=Bottom (last baseline = cy). | |
| 81 | let v_frac = params.v_tcb.clamp(-1.0, 1.0) * 0.5 + 0.5; | |
| 82 | let top_anchor = (font_px - descent).max(0.0); | |
| 83 | let block_h = (lines.len().saturating_sub(1) as f32) * line_height + top_anchor; | |
| 84 | let baseline_0 = cy + top_anchor - v_frac * block_h; | |
| 85 | let h_frac = params.h_lcr.clamp(-1.0, 1.0) * 0.5 + 0.5; | |
| 86 | ||
| 87 | let outlined = params.outline_width_px > 1e-3 && params.outline_rgba[3] > 0.0; | |
| 88 | ||
| 89 | // Pass 1: vector-rasterize every visible glyph and collect exact bounds to expand the DoD. | |
| 90 | let mut placed = Vec::new(); | |
| 91 | let (mut min_x, mut min_y, mut max_x, mut max_y) = (0i32, 0i32, w as i32, h as i32); | |
| 92 | let mut grow = |c: &text_engine::Coverage, pad: i32| { | |
| 93 | min_x = min_x.min(c.left - pad); | |
| 94 | min_y = min_y.min(c.top - pad); | |
| 95 | max_x = max_x.max(c.left + c.width as i32 + pad); | |
| 96 | max_y = max_y.max(c.top + c.height as i32 + pad); | |
| 97 | }; | |
| 98 | ||
| 99 | let mut char_cursor = 0usize; | |
| 100 | for (li, line_str) in lines.iter().enumerate() { | |
| 101 | let shaped = text_engine::shape_line(&face, line_str, font_px, extra_track); | |
| 102 | let start_x = cx - shaped.width * h_frac; | |
| 103 | let baseline_y = baseline_0 + (li as f32) * line_height; | |
| 104 | for g in &shaped.glyphs { | |
| 105 | let idx = char_cursor + g.char_index; | |
| 106 | if idx < c_start || idx >= c_end { | |
| 107 | continue; | |
| 108 | } | |
| 109 | let origin = [ | |
| 110 | start_x + g.x + cls_dx[idx] * w as f32, | |
| 111 | baseline_y - g.y - cls_dy[idx] * w as f32, | |
| 112 | ]; | |
| 113 | let path = text_engine::glyph_path(&font, &face, g.id, font_px, origin, -params.shear_x); | |
| 114 | let fill = text_engine::fill(&path); | |
| 115 | let line = if outlined { text_engine::stroke(&path, params.outline_width_px) } else { None }; | |
| 116 | if let Some(c) = &fill { | |
| 117 | grow(c, dod_pad); | |
| 118 | } | |
| 119 | if let Some(c) = &line { | |
| 120 | grow(c, dod_pad); | |
| 121 | } | |
| 122 | placed.push((fill, line)); | |
| 123 | } | |
| 124 | char_cursor += line_str.chars().count() + 1; // + '\n' separator | |
| 125 | } | |
| 126 | ||
| 127 | // Clamp DoD expansion safely to at most +/- 1 canvas width/height so GPU textures stay bounded. | |
| 128 | let origin_x = min_x.max(-(w as i32)); | |
| 129 | let origin_y = min_y.max(-(h as i32)); | |
| 130 | let end_x = max_x.min((w as i32) * 2); | |
| 131 | let end_y = max_y.min((h as i32) * 2); | |
| 132 | let tw = (end_x - origin_x).max(1) as usize; | |
| 133 | let th = (end_y - origin_y).max(1) as usize; | |
| 134 | ||
| 135 | let mut fill_plane = vec![0u8; tw * th]; | |
| 136 | let mut line_plane = vec![0.0_f32; if outlined { tw * th } else { 0 }]; | |
| 137 | for (fill, line) in &placed { | |
| 138 | if let Some(c) = fill { | |
| 139 | blit_max(c, [origin_x, origin_y], [tw, th], |i, a| fill_plane[i] = fill_plane[i].max(a)); | |
| 140 | } | |
| 141 | if let Some(c) = line { | |
| 142 | blit_max(c, [origin_x, origin_y], [tw, th], |i, a| line_plane[i] = line_plane[i].max(a as f32 / 255.0)); | |
| 143 | } | |
| 144 | } | |
| 145 | ||
| 146 | let (fc, oc) = (params.fill_rgba, params.outline_rgba); | |
| 147 | let mut out = vec![0u8; tw * th * 4]; | |
| 148 | for (i, px) in out.chunks_exact_mut(4).enumerate() { | |
| 149 | let fa = fill_plane[i] as f32 / 255.0 * fc[3]; | |
| 150 | let oa = if outlined { line_plane[i] * oc[3] } else { 0.0 }; | |
| 151 | if fa <= 0.0 && oa <= 0.0 { | |
| 152 | continue; | |
| 153 | } | |
| 154 | let under = 1.0 - fa; | |
| 155 | for k in 0..3 { | |
| 156 | let v = fc[k] * fa + oc[k] * oa * under; | |
| 157 | px[k] = (v * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | |
| 158 | } | |
| 159 | px[3] = ((fa + oa * under) * 255.0 + 0.5).clamp(0.0, 255.0) as u8; | |
| 160 | } | |
| 161 | ||
| 162 | ([tw as u32, th as u32], [origin_x, origin_y], out) | |
| 163 | } | |
| 164 | ||
| 165 | /// Calls `f(plane_index, coverage)` for every non-zero coverage pixel that lands inside the plane. | |
| 166 | fn blit_max(c: &text_engine::Coverage, origin: [i32; 2], plane: [usize; 2], mut f: impl FnMut(usize, u8)) { | |
| 167 | for y in 0..c.height as i32 { | |
| 168 | let py = c.top + y - origin[1]; | |
| 169 | if py < 0 || py >= plane[1] as i32 { | |
| 170 | continue; | |
| 171 | } | |
| 172 | for x in 0..c.width as i32 { | |
| 173 | let px = c.left + x - origin[0]; | |
| 174 | let a = c.data[(y as u32 * c.width + x as u32) as usize]; | |
| 175 | if a > 0 && px >= 0 && px < plane[0] as i32 { | |
| 176 | f(py as usize * plane[0] + px as usize, a); | |
| 177 | } | |
| 178 | } | |
| 179 | } | |
| 180 | } | |
| 181 | ||
| 182 | /// Rasterizes a closed Fusion `PolylineMask` into RGBA8 `[w, h]` via `zeno`. | |
| 183 | pub fn rasterize_polyline_mask( | |
| 184 | dims: [u32; 2], | |
| 185 | points: &[PolyPoint], | |
| 186 | center: [f32; 2], | |
| 187 | invert: bool, | |
| 188 | level: f32, | |
| 189 | ) -> Vec<u8> { | |
| 190 | let [w, h] = dims; | |
| 191 | let mut out = vec![0u8; (w as usize) * (h as usize) * 4]; | |
| 192 | if points.len() < 2 { | |
| 193 | return out; | |
| 194 | } | |
| 195 | let to_px = |x: f64, y: f64| -> [f32; 2] { | |
| 196 | let fx = (center[0] + x as f32) * w as f32; | |
| 197 | let fy = (1.0 - (center[1] + y as f32)) * h as f32; | |
| 198 | [fx, fy] | |
| 199 | }; | |
| 200 | let mut cmds = Vec::new(); | |
| 201 | let p0 = to_px(points[0].x, points[0].y); | |
| 202 | cmds.push(zeno::Command::MoveTo(zeno::Vector::new(p0[0], p0[1]))); | |
| 203 | for i in 0..points.len() { | |
| 204 | let a = &points[i]; | |
| 205 | let b = &points[(i + 1) % points.len()]; | |
| 206 | let c1 = to_px(a.x + a.rx, a.y + a.ry); | |
| 207 | let c2 = to_px(b.x + b.lx, b.y + b.ly); | |
| 208 | let p = to_px(b.x, b.y); | |
| 209 | if a.linear && b.linear { | |
| 210 | cmds.push(zeno::Command::LineTo(zeno::Vector::new(p[0], p[1]))); | |
| 211 | } else { | |
| 212 | cmds.push(zeno::Command::CurveTo( | |
| 213 | zeno::Vector::new(c1[0], c1[1]), | |
| 214 | zeno::Vector::new(c2[0], c2[1]), | |
| 215 | zeno::Vector::new(p[0], p[1]), | |
| 216 | )); | |
| 217 | } | |
| 218 | } | |
| 219 | cmds.push(zeno::Command::Close); | |
| 220 | ||
| 221 | let mut mask = vec![0u8; (w as usize) * (h as usize)]; | |
| 222 | let mut scratch = zeno::Scratch::new(); | |
| 223 | let placement = zeno::Mask::with_scratch(&cmds, &mut scratch) | |
| 224 | .size(w, h) | |
| 225 | .origin(zeno::Origin::TopLeft) | |
| 226 | .render_into(&mut mask, None); | |
| 227 | let _ = placement; | |
| 228 | for (i, &cov) in mask.iter().enumerate() { | |
| 229 | let mut a = cov as f32 / 255.0; | |
| 230 | if invert { a = 1.0 - a; } | |
| 231 | let b = (a * level * 255.0).clamp(0.0, 255.0) as u8; | |
| 232 | out[i * 4] = b; | |
| 233 | out[i * 4 + 1] = b; | |
| 234 | out[i * 4 + 2] = b; | |
| 235 | out[i * 4 + 3] = b; | |
| 236 | } | |
| 237 | out | |
| 238 | } |
crates/engine/src/media/video.rs created+268| ... | ... | @@ -0,0 +1,268 @@ |
| 1 | use super::*; | |
| 2 | ||
| 3 | #[derive(Clone, Copy, Debug)] | |
| 4 | pub struct VideoInfo { | |
| 5 | pub width: u32, | |
| 6 | pub height: u32, | |
| 7 | pub fps: f64, | |
| 8 | pub nb_frames: i64, | |
| 9 | } | |
| 10 | ||
| 11 | pub trait VideoDecoder: Send { | |
| 12 | fn info(&self) -> VideoInfo; | |
| 13 | fn decode_frame(&mut self, frame_idx: i64) -> Option<Vec<u8>>; | |
| 14 | fn backend_name(&self) -> &'static str; | |
| 15 | } | |
| 16 | ||
| 17 | #[cfg(target_os = "macos")] | |
| 18 | mod av_ffi { | |
| 19 | use std::ffi::c_char; | |
| 20 | ||
| 21 | #[repr(C)] | |
| 22 | pub struct AvVideoDecoder { | |
| 23 | _private: [u8; 0], | |
| 24 | } | |
| 25 | ||
| 26 | unsafe extern "C" { | |
| 27 | pub fn av_video_open( | |
| 28 | path: *const c_char, | |
| 29 | out_w: *mut u32, | |
| 30 | out_h: *mut u32, | |
| 31 | out_fps: *mut f64, | |
| 32 | out_nb_frames: *mut i64, | |
| 33 | ) -> *mut AvVideoDecoder; | |
| 34 | ||
| 35 | pub fn av_video_decode_frame( | |
| 36 | dec: *mut AvVideoDecoder, | |
| 37 | frame_idx: i64, | |
| 38 | out_rgba: *mut u8, | |
| 39 | out_len: usize, | |
| 40 | ) -> i32; | |
| 41 | ||
| 42 | pub fn av_video_close(dec: *mut AvVideoDecoder); | |
| 43 | } | |
| 44 | } | |
| 45 | ||
| 46 | #[cfg(target_os = "macos")] | |
| 47 | pub struct AvFoundationDecoder { | |
| 48 | ptr: *mut av_ffi::AvVideoDecoder, | |
| 49 | info: VideoInfo, | |
| 50 | } | |
| 51 | ||
| 52 | #[cfg(target_os = "macos")] | |
| 53 | unsafe impl Send for AvFoundationDecoder {} | |
| 54 | ||
| 55 | #[cfg(target_os = "macos")] | |
| 56 | impl Drop for AvFoundationDecoder { | |
| 57 | fn drop(&mut self) { | |
| 58 | if !self.ptr.is_null() { | |
| 59 | unsafe { av_ffi::av_video_close(self.ptr) }; | |
| 60 | self.ptr = std::ptr::null_mut(); | |
| 61 | } | |
| 62 | } | |
| 63 | } | |
| 64 | ||
| 65 | #[cfg(target_os = "macos")] | |
| 66 | impl AvFoundationDecoder { | |
| 67 | pub fn open(path: &Path) -> Option<Self> { | |
| 68 | use std::ffi::CString; | |
| 69 | let path_str = path.to_str()?; | |
| 70 | let c_path = CString::new(path_str).ok()?; | |
| 71 | let mut width = 0u32; | |
| 72 | let mut height = 0u32; | |
| 73 | let mut fps = 0.0f64; | |
| 74 | let mut nb_frames = 0i64; | |
| 75 | let ptr = unsafe { | |
| 76 | av_ffi::av_video_open( | |
| 77 | c_path.as_ptr(), | |
| 78 | &mut width, | |
| 79 | &mut height, | |
| 80 | &mut fps, | |
| 81 | &mut nb_frames, | |
| 82 | ) | |
| 83 | }; | |
| 84 | if ptr.is_null() || width == 0 || height == 0 { | |
| 85 | if !ptr.is_null() { | |
| 86 | unsafe { av_ffi::av_video_close(ptr) }; | |
| 87 | } | |
| 88 | return None; | |
| 89 | } | |
| 90 | Some(Self { | |
| 91 | ptr, | |
| 92 | info: VideoInfo { | |
| 93 | width, | |
| 94 | height, | |
| 95 | fps: if fps > 0.0 { fps } else { 30.0 }, | |
| 96 | nb_frames, | |
| 97 | }, | |
| 98 | }) | |
| 99 | } | |
| 100 | } | |
| 101 | ||
| 102 | #[cfg(target_os = "macos")] | |
| 103 | impl VideoDecoder for AvFoundationDecoder { | |
| 104 | fn info(&self) -> VideoInfo { | |
| 105 | self.info | |
| 106 | } | |
| 107 | ||
| 108 | fn decode_frame(&mut self, frame_idx: i64) -> Option<Vec<u8>> { | |
| 109 | let len = (self.info.width as usize) | |
| 110 | .checked_mul(self.info.height as usize)? | |
| 111 | .checked_mul(4)?; | |
| 112 | let mut buf = vec![0u8; len]; | |
| 113 | let ok = unsafe { | |
| 114 | av_ffi::av_video_decode_frame(self.ptr, frame_idx.max(0), buf.as_mut_ptr(), len) | |
| 115 | }; | |
| 116 | if ok != 0 { | |
| 117 | Some(buf) | |
| 118 | } else { | |
| 119 | None | |
| 120 | } | |
| 121 | } | |
| 122 | ||
| 123 | fn backend_name(&self) -> &'static str { | |
| 124 | "AVFoundation/VideoToolbox" | |
| 125 | } | |
| 126 | } | |
| 127 | ||
| 128 | pub struct FfmpegDecoder { | |
| 129 | path: PathBuf, | |
| 130 | info: VideoInfo, | |
| 131 | } | |
| 132 | ||
| 133 | impl FfmpegDecoder { | |
| 134 | pub fn open(path: &Path) -> Option<Self> { | |
| 135 | let info = probe_video(path)?; | |
| 136 | Some(Self { | |
| 137 | path: path.to_path_buf(), | |
| 138 | info, | |
| 139 | }) | |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 143 | impl VideoDecoder for FfmpegDecoder { | |
| 144 | fn info(&self) -> VideoInfo { | |
| 145 | self.info | |
| 146 | } | |
| 147 | ||
| 148 | fn decode_frame(&mut self, frame_idx: i64) -> Option<Vec<u8>> { | |
| 149 | decode_video_frame(&self.path, self.info, frame_idx) | |
| 150 | } | |
| 151 | ||
| 152 | fn backend_name(&self) -> &'static str { | |
| 153 | "ffmpeg" | |
| 154 | } | |
| 155 | } | |
| 156 | ||
| 157 | /// Opens a hardware-accelerated video decoder (`AVFoundation`/`VideoToolbox` on macOS), | |
| 158 | /// falling back to `ffmpeg` for unsupported containers or codecs. | |
| 159 | pub fn open_video_decoder(path: &Path) -> Option<Box<dyn VideoDecoder>> { | |
| 160 | #[cfg(target_os = "macos")] | |
| 161 | { | |
| 162 | if let Some(av) = AvFoundationDecoder::open(path) { | |
| 163 | return Some(Box::new(av)); | |
| 164 | } | |
| 165 | } | |
| 166 | FfmpegDecoder::open(path).map(|ff| Box::new(ff) as Box<dyn VideoDecoder>) | |
| 167 | } | |
| 168 | ||
| 169 | pub(super) fn is_video_path(path: &Path) -> bool { | |
| 170 | let Some(ext) = path.extension().and_then(|e| e.to_str()) else { | |
| 171 | return false; | |
| 172 | }; | |
| 173 | matches!( | |
| 174 | ext.to_ascii_lowercase().as_str(), | |
| 175 | "mov" | "mp4" | "m4v" | "mkv" | "avi" | "webm" | "mxf" | |
| 176 | ) | |
| 177 | } | |
| 178 | ||
| 179 | pub(super) fn probe_video(path: &Path) -> Option<VideoInfo> { | |
| 180 | let out = Command::new("ffprobe") | |
| 181 | .args([ | |
| 182 | "-v", | |
| 183 | "error", | |
| 184 | "-select_streams", | |
| 185 | "v:0", | |
| 186 | "-show_entries", | |
| 187 | "stream=width,height,r_frame_rate,nb_frames", | |
| 188 | "-of", | |
| 189 | "default=noprint_wrappers=1", | |
| 190 | ]) | |
| 191 | .arg(path) | |
| 192 | .output() | |
| 193 | .ok()?; | |
| 194 | if !out.status.success() { | |
| 195 | return None; | |
| 196 | } | |
| 197 | let text = String::from_utf8_lossy(&out.stdout); | |
| 198 | let mut width = 0u32; | |
| 199 | let mut height = 0u32; | |
| 200 | let mut fps = 30.0_f64; | |
| 201 | let mut nb_frames = 0i64; | |
| 202 | for line in text.lines() { | |
| 203 | if let Some((k, v)) = line.split_once('=') { | |
| 204 | match k.trim() { | |
| 205 | "width" => width = v.trim().parse().unwrap_or(0), | |
| 206 | "height" => height = v.trim().parse().unwrap_or(0), | |
| 207 | "nb_frames" => nb_frames = v.trim().parse().unwrap_or(0), | |
| 208 | "r_frame_rate" => { | |
| 209 | if let Some((n, d)) = v.trim().split_once('/') { | |
| 210 | let num: f64 = n.parse().unwrap_or(30.0); | |
| 211 | let den: f64 = d.parse().unwrap_or(1.0); | |
| 212 | if den > 0.0 && num > 0.0 { | |
| 213 | fps = num / den; | |
| 214 | } | |
| 215 | } else if let Ok(r) = v.trim().parse::<f64>() { | |
| 216 | if r > 0.0 { | |
| 217 | fps = r; | |
| 218 | } | |
| 219 | } | |
| 220 | } | |
| 221 | _ => {} | |
| 222 | } | |
| 223 | } | |
| 224 | } | |
| 225 | if width == 0 || height == 0 { | |
| 226 | return None; | |
| 227 | } | |
| 228 | Some(VideoInfo { width, height, fps, nb_frames }) | |
| 229 | } | |
| 230 | ||
| 231 | pub(super) fn decode_video_frame(path: &Path, info: VideoInfo, frame_idx: i64) -> Option<Vec<u8>> { | |
| 232 | let idx = frame_idx.max(0); | |
| 233 | let ts = if idx == 0 { | |
| 234 | 0.0 | |
| 235 | } else { | |
| 236 | ((idx as f64) - 0.005) / info.fps.max(1.0) | |
| 237 | }; | |
| 238 | let out = Command::new("ffmpeg") | |
| 239 | .args([ | |
| 240 | "-v", | |
| 241 | "error", | |
| 242 | "-ss", | |
| 243 | &format!("{ts:.6}"), | |
| 244 | "-i", | |
| 245 | ]) | |
| 246 | .arg(path) | |
| 247 | .args([ | |
| 248 | "-frames:v", | |
| 249 | "1", | |
| 250 | "-f", | |
| 251 | "rawvideo", | |
| 252 | "-pix_fmt", | |
| 253 | "rgba", | |
| 254 | "-", | |
| 255 | ]) | |
| 256 | .output() | |
| 257 | .ok()?; | |
| 258 | if !out.status.success() { | |
| 259 | return None; | |
| 260 | } | |
| 261 | let expected = (info.width as usize) * (info.height as usize) * 4; | |
| 262 | if out.stdout.len() < expected { | |
| 263 | return None; | |
| 264 | } | |
| 265 | let mut buf = out.stdout; | |
| 266 | buf.truncate(expected); | |
| 267 | Some(buf) | |
| 268 | } |
crates/flow/src/hit.rs created+228| ... | ... | @@ -0,0 +1,228 @@ |
| 1 | use super::*; | |
| 2 | ||
| 3 | impl FlowView { | |
| 4 | pub(crate) fn world_to_local(&self, w: [f32; 2]) -> [f32; 2] { | |
| 5 | [ | |
| 6 | (w[0] - self.pan[0]) * self.zoom + self.size[0] * 0.5, | |
| 7 | (w[1] - self.pan[1]) * self.zoom + self.size[1] * 0.5, | |
| 8 | ] | |
| 9 | } | |
| 10 | ||
| 11 | pub(crate) fn local_to_world(&self, l: [f32; 2]) -> [f32; 2] { | |
| 12 | [ | |
| 13 | (l[0] - self.size[0] * 0.5) / self.zoom + self.pan[0], | |
| 14 | (l[1] - self.size[1] * 0.5) / self.zoom + self.pan[1], | |
| 15 | ] | |
| 16 | } | |
| 17 | ||
| 18 | pub(crate) fn node_rect_local(&self, pos: [f32; 2]) -> [f32; 4] { | |
| 19 | let c = self.world_to_local(pos); | |
| 20 | let hw = NODE_W * 0.5 * self.zoom; | |
| 21 | let hh = NODE_H * 0.5 * self.zoom; | |
| 22 | [c[0] - hw, c[1] - hh, c[0] + hw, c[1] + hh] | |
| 23 | } | |
| 24 | ||
| 25 | /// Computes auto-wrapped port geometry for a wire `src -> dst` so ports snap to the | |
| 26 | /// closest facing sides of each node (Fusion's signature clean multi-directional routing!). | |
| 27 | pub(crate) fn wire_endpoints(&self, comp: &Comp, src: NodeId, dst: NodeId, role: PortRole) -> ([f32; 2], Edge, [f32; 2], Edge) { | |
| 28 | let sp = comp.node(src).pos; | |
| 29 | let dp = comp.node(dst).pos; | |
| 30 | let sr = self.node_rect_local(sp); | |
| 31 | let dr = self.node_rect_local(dp); | |
| 32 | ||
| 33 | let dx = dp[0] - sp[0]; | |
| 34 | let dy = dp[1] - sp[1]; | |
| 35 | // Weight Y slightly more because nodes are wider than tall (~5:1 aspect)! | |
| 36 | let out_edge = closest_edge(dx, dy); | |
| 37 | let in_edge = closest_edge_for_input(-dx, -dy, role); | |
| 38 | ||
| 39 | let a = out_edge.point_on_rect(sr, 0.5); | |
| 40 | let b = in_edge.point_on_rect(dr, 0.5); | |
| 41 | (a, out_edge, b, in_edge) | |
| 42 | } | |
| 43 | ||
| 44 | /// Computes a node's resting output port edge (average direction of its consumers, defaulting to Right). | |
| 45 | pub(crate) fn node_output_edge(&self, comp: &Comp, id: NodeId) -> Edge { | |
| 46 | let name = &comp.node(id).name; | |
| 47 | let sp = comp.node(id).pos; | |
| 48 | let mut sx = 0.0_f32; | |
| 49 | let mut sy = 0.0_f32; | |
| 50 | let mut count = 0; | |
| 51 | for other in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 52 | for binding in other.inputs.values() { | |
| 53 | if binding.source_op.as_deref() == Some(name.as_str()) { | |
| 54 | let dx = other.pos[0] - sp[0]; | |
| 55 | let dy = other.pos[1] - sp[1]; | |
| 56 | let len = (dx * dx + dy * dy).sqrt().max(1.0); | |
| 57 | sx += dx / len; | |
| 58 | sy += dy / len; | |
| 59 | count += 1; | |
| 60 | } | |
| 61 | } | |
| 62 | } | |
| 63 | if count == 0 { | |
| 64 | Edge::Right | |
| 65 | } else { | |
| 66 | closest_edge(sx, sy) | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | pub(crate) fn hit_node(&self, comp: &Comp, local: [f32; 2]) -> Option<NodeId> { | |
| 71 | for n in comp.nodes.iter().rev() { | |
| 72 | if !self.scope.shows(n) { continue; } | |
| 73 | let r = self.node_rect_local(n.pos); | |
| 74 | if local[0] >= r[0] && local[0] <= r[2] && local[1] >= r[1] && local[1] <= r[3] { | |
| 75 | return Some(n.id); | |
| 76 | } | |
| 77 | } | |
| 78 | None | |
| 79 | } | |
| 80 | ||
| 81 | pub(crate) fn hit_output_port(&self, comp: &Comp, local: [f32; 2]) -> Option<NodeId> { | |
| 82 | for n in comp.nodes.iter().rev() { | |
| 83 | if !self.scope.shows(n) { continue; } | |
| 84 | let r = self.node_rect_local(n.pos); | |
| 85 | let edge = self.node_output_edge(comp, n.id); | |
| 86 | let nrm = edge.normal(); | |
| 87 | let p0 = edge.point_on_rect(r, 0.5); | |
| 88 | let p = [p0[0] + nrm[0] * 5.0, p0[1] + nrm[1] * 5.0]; | |
| 89 | let dx = local[0] - p[0]; | |
| 90 | let dy = local[1] - p[1]; | |
| 91 | if dx * dx + dy * dy <= 10.0 * 10.0 { | |
| 92 | return Some(n.id); | |
| 93 | } | |
| 94 | } | |
| 95 | None | |
| 96 | } | |
| 97 | ||
| 98 | pub(crate) fn hit_input_port(&self, comp: &Comp, local: [f32; 2]) -> Option<(NodeId, &'static str)> { | |
| 99 | for n in comp.nodes.iter().rev() { | |
| 100 | if !self.scope.shows(n) { continue; } | |
| 101 | let r = self.node_rect_local(n.pos); | |
| 102 | if local[0] >= r[0] - 10.0 && local[0] <= r[2] + 10.0 && local[1] >= r[1] - 10.0 && local[1] <= r[3] + 10.0 { | |
| 103 | // Pick Background/Foreground/Mask cleanly based on approach edge for Merge/Displace! | |
| 104 | if n.kind == "Merge" || n.kind == "Displace" || n.kind == "Dissolve" { | |
| 105 | if local[1] < r[1] + 2.0 { | |
| 106 | return Some((n.id, "Foreground")); | |
| 107 | } | |
| 108 | if local[1] > r[3] - 2.0 { | |
| 109 | return Some((n.id, "EffectMask")); | |
| 110 | } | |
| 111 | return Some((n.id, "Background")); | |
| 112 | } | |
| 113 | if n.kind == "Merge3D" || n.kind == "Renderer3D" { | |
| 114 | return Some((n.id, "SceneInput1")); | |
| 115 | } | |
| 116 | if local[1] > r[3] - 1.5 && n.kind != "PipeRouter" { | |
| 117 | return Some((n.id, "EffectMask")); | |
| 118 | } | |
| 119 | return Some((n.id, default_main_input_port(&n.kind))); | |
| 120 | } | |
| 121 | } | |
| 122 | None | |
| 123 | } | |
| 124 | ||
| 125 | /// Hit-tests already-connected input port triangles so the user can drag an input off to disconnect/rewire. | |
| 126 | pub(crate) fn hit_connected_input_port(&self, comp: &Comp, local: [f32; 2]) -> Option<(NodeId, String, NodeId)> { | |
| 127 | let hit_r2 = 10.5_f32 * 10.5_f32; | |
| 128 | for dst in comp.nodes.iter().rev() { | |
| 129 | if !self.scope.shows(dst) { continue; } | |
| 130 | for (port, binding) in &dst.inputs { | |
| 131 | let Some(src_name) = &binding.source_op else { continue }; | |
| 132 | let Some(src_id) = self.scope.resolve(comp, src_name) else { continue }; | |
| 133 | let role = PortRole::from_input_name(port); | |
| 134 | let (_a, _ae, b, be) = self.wire_endpoints(comp, src_id, dst.id, role); | |
| 135 | let bn = be.normal(); | |
| 136 | let center = [b[0] + bn[0] * 4.0, b[1] + bn[1] * 4.0]; | |
| 137 | let dx = local[0] - center[0]; | |
| 138 | let dy = local[1] - center[1]; | |
| 139 | if dx * dx + dy * dy <= hit_r2 { | |
| 140 | return Some((dst.id, port.clone(), src_id)); | |
| 141 | } | |
| 142 | } | |
| 143 | } | |
| 144 | None | |
| 145 | } | |
| 146 | ||
| 147 | pub(crate) fn hit_wire(&self, comp: &Comp, local: [f32; 2], ignore_node: Option<NodeId>) -> Option<WireRef> { | |
| 148 | let mut best: Option<(f32, WireRef)> = None; | |
| 149 | for dst in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 150 | if Some(dst.id) == ignore_node { | |
| 151 | continue; | |
| 152 | } | |
| 153 | for (port, binding) in &dst.inputs { | |
| 154 | let Some(src_name) = &binding.source_op else { continue }; | |
| 155 | let Some(src_id) = self.scope.resolve(comp, src_name) else { continue }; | |
| 156 | if Some(src_id) == ignore_node { | |
| 157 | continue; | |
| 158 | } | |
| 159 | let role = PortRole::from_input_name(port); | |
| 160 | let (a, ae, b, be) = self.wire_endpoints(comp, src_id, dst.id, role); | |
| 161 | let d = dist_to_straight_wire(local, a, ae, b, be, self.zoom); | |
| 162 | if d < 10.0 && best.as_ref().is_none_or(|(bd, _)| d < *bd) { | |
| 163 | best = Some(( | |
| 164 | d, | |
| 165 | WireRef { | |
| 166 | src: src_id, | |
| 167 | dst: dst.id, | |
| 168 | port: port.clone(), | |
| 169 | }, | |
| 170 | )); | |
| 171 | } | |
| 172 | } | |
| 173 | } | |
| 174 | best.map(|(_, w)| w) | |
| 175 | } | |
| 176 | } | |
| 177 | ||
| 178 | /// Picks the closest node edge facing vector `(dx, dy)` accounting for thin rectangular bar aspect. | |
| 179 | fn closest_edge(dx: f32, dy: f32) -> Edge { | |
| 180 | // Nodes are 84x21 (4:1 aspect), so require a balanced X bias so horizontal rows snap Left/Right | |
| 181 | // while vertical stacks naturally snap Top/Bottom! | |
| 182 | if dx.abs() * 0.58 >= dy.abs() { | |
| 183 | if dx >= 0.0 { Edge::Right } else { Edge::Left } | |
| 184 | } else if dy >= 0.0 { | |
| 185 | Edge::Bottom | |
| 186 | } else { | |
| 187 | Edge::Top | |
| 188 | } | |
| 189 | } | |
| 190 | ||
| 191 | /// Auto-wraps input port to the closest side facing the producer, gently nudging Mask/FG when needed. | |
| 192 | fn closest_edge_for_input(dx_to_src: f32, dy_to_src: f32, role: PortRole) -> Edge { | |
| 193 | let primary = closest_edge(dx_to_src, dy_to_src); | |
| 194 | match role { | |
| 195 | PortRole::Mask => { | |
| 196 | // Masks prefer Top/Bottom unless directly horizontal | |
| 197 | if dy_to_src.abs() >= dx_to_src.abs() * 0.35 { | |
| 198 | if dy_to_src >= 0.0 { Edge::Bottom } else { Edge::Top } | |
| 199 | } else { | |
| 200 | primary | |
| 201 | } | |
| 202 | } | |
| 203 | _ => primary, | |
| 204 | } | |
| 205 | } | |
| 206 | ||
| 207 | fn dist_to_straight_wire(pt: [f32; 2], a: [f32; 2], ae: Edge, b: [f32; 2], be: Edge, zoom: f32) -> f32 { | |
| 208 | let stub = (8.0 * zoom).clamp(4.0, 11.0); | |
| 209 | let an = ae.normal(); | |
| 210 | let bn = be.normal(); | |
| 211 | let a_out = [a[0] + an[0] * stub, a[1] + an[1] * stub]; | |
| 212 | let b_out = [b[0] + bn[0] * stub, b[1] + bn[1] * stub]; | |
| 213 | dist_to_seg(pt, a, a_out) | |
| 214 | .min(dist_to_seg(pt, a_out, b_out)) | |
| 215 | .min(dist_to_seg(pt, b_out, b)) | |
| 216 | } | |
| 217 | ||
| 218 | pub(crate) fn dist_to_seg(p: [f32; 2], a: [f32; 2], b: [f32; 2]) -> f32 { | |
| 219 | let ab = [b[0] - a[0], b[1] - a[1]]; | |
| 220 | let len2 = ab[0] * ab[0] + ab[1] * ab[1]; | |
| 221 | let t = if len2 < 1e-6 { | |
| 222 | 0.0 | |
| 223 | } else { | |
| 224 | (((p[0] - a[0]) * ab[0] + (p[1] - a[1]) * ab[1]) / len2).clamp(0.0, 1.0) | |
| 225 | }; | |
| 226 | let q = [a[0] + ab[0] * t, a[1] + ab[1] * t]; | |
| 227 | ((p[0] - q[0]).powi(2) + (p[1] - q[1]).powi(2)).sqrt() | |
| 228 | } |
crates/flow/src/lib.rs+3-725| ... | ... | @@ -6,6 +6,8 @@ |
| 6 | 6 | //! - `1`/`2` multi-viewer routing, `Cmd+F` / Numpad `.` fit-to-view, `P` bypass, drag-onto-wire insert. |
| 7 | 7 | //! - `Cmd+C` / `Cmd+V` node clipboard requests, double-click into Macro/Group subgraphs, `Esc` back out. |
| 8 | 8 | |
| 9 | mod hit; | |
| 10 | mod paint; | |
| 9 | 11 | mod scope; |
| 10 | 12 | |
| 11 | 13 | use draw::{Glyphs, PathStyle, Primitive}; |
| ... | ... | @@ -362,179 +364,6 @@ impl FlowView { |
| 362 | 364 | } |
| 363 | 365 | } |
| 364 | 366 | |
| 365 | fn world_to_local(&self, w: [f32; 2]) -> [f32; 2] { | |
| 366 | [ | |
| 367 | (w[0] - self.pan[0]) * self.zoom + self.size[0] * 0.5, | |
| 368 | (w[1] - self.pan[1]) * self.zoom + self.size[1] * 0.5, | |
| 369 | ] | |
| 370 | } | |
| 371 | ||
| 372 | fn local_to_world(&self, l: [f32; 2]) -> [f32; 2] { | |
| 373 | [ | |
| 374 | (l[0] - self.size[0] * 0.5) / self.zoom + self.pan[0], | |
| 375 | (l[1] - self.size[1] * 0.5) / self.zoom + self.pan[1], | |
| 376 | ] | |
| 377 | } | |
| 378 | ||
| 379 | fn node_rect_local(&self, pos: [f32; 2]) -> [f32; 4] { | |
| 380 | let c = self.world_to_local(pos); | |
| 381 | let hw = NODE_W * 0.5 * self.zoom; | |
| 382 | let hh = NODE_H * 0.5 * self.zoom; | |
| 383 | [c[0] - hw, c[1] - hh, c[0] + hw, c[1] + hh] | |
| 384 | } | |
| 385 | ||
| 386 | /// Computes auto-wrapped port geometry for a wire `src -> dst` so ports snap to the | |
| 387 | /// closest facing sides of each node (Fusion's signature clean multi-directional routing!). | |
| 388 | fn wire_endpoints(&self, comp: &Comp, src: NodeId, dst: NodeId, role: PortRole) -> ([f32; 2], Edge, [f32; 2], Edge) { | |
| 389 | let sp = comp.node(src).pos; | |
| 390 | let dp = comp.node(dst).pos; | |
| 391 | let sr = self.node_rect_local(sp); | |
| 392 | let dr = self.node_rect_local(dp); | |
| 393 | ||
| 394 | let dx = dp[0] - sp[0]; | |
| 395 | let dy = dp[1] - sp[1]; | |
| 396 | // Weight Y slightly more because nodes are wider than tall (~5:1 aspect)! | |
| 397 | let out_edge = closest_edge(dx, dy); | |
| 398 | let in_edge = closest_edge_for_input(-dx, -dy, role); | |
| 399 | ||
| 400 | let a = out_edge.point_on_rect(sr, 0.5); | |
| 401 | let b = in_edge.point_on_rect(dr, 0.5); | |
| 402 | (a, out_edge, b, in_edge) | |
| 403 | } | |
| 404 | ||
| 405 | /// Computes a node's resting output port edge (average direction of its consumers, defaulting to Right). | |
| 406 | fn node_output_edge(&self, comp: &Comp, id: NodeId) -> Edge { | |
| 407 | let name = &comp.node(id).name; | |
| 408 | let sp = comp.node(id).pos; | |
| 409 | let mut sx = 0.0_f32; | |
| 410 | let mut sy = 0.0_f32; | |
| 411 | let mut count = 0; | |
| 412 | for other in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 413 | for binding in other.inputs.values() { | |
| 414 | if binding.source_op.as_deref() == Some(name.as_str()) { | |
| 415 | let dx = other.pos[0] - sp[0]; | |
| 416 | let dy = other.pos[1] - sp[1]; | |
| 417 | let len = (dx * dx + dy * dy).sqrt().max(1.0); | |
| 418 | sx += dx / len; | |
| 419 | sy += dy / len; | |
| 420 | count += 1; | |
| 421 | } | |
| 422 | } | |
| 423 | } | |
| 424 | if count == 0 { | |
| 425 | Edge::Right | |
| 426 | } else { | |
| 427 | closest_edge(sx, sy) | |
| 428 | } | |
| 429 | } | |
| 430 | ||
| 431 | fn hit_node(&self, comp: &Comp, local: [f32; 2]) -> Option<NodeId> { | |
| 432 | for n in comp.nodes.iter().rev() { | |
| 433 | if !self.scope.shows(n) { continue; } | |
| 434 | let r = self.node_rect_local(n.pos); | |
| 435 | if local[0] >= r[0] && local[0] <= r[2] && local[1] >= r[1] && local[1] <= r[3] { | |
| 436 | return Some(n.id); | |
| 437 | } | |
| 438 | } | |
| 439 | None | |
| 440 | } | |
| 441 | ||
| 442 | fn hit_output_port(&self, comp: &Comp, local: [f32; 2]) -> Option<NodeId> { | |
| 443 | for n in comp.nodes.iter().rev() { | |
| 444 | if !self.scope.shows(n) { continue; } | |
| 445 | let r = self.node_rect_local(n.pos); | |
| 446 | let edge = self.node_output_edge(comp, n.id); | |
| 447 | let nrm = edge.normal(); | |
| 448 | let p0 = edge.point_on_rect(r, 0.5); | |
| 449 | let p = [p0[0] + nrm[0] * 5.0, p0[1] + nrm[1] * 5.0]; | |
| 450 | let dx = local[0] - p[0]; | |
| 451 | let dy = local[1] - p[1]; | |
| 452 | if dx * dx + dy * dy <= 10.0 * 10.0 { | |
| 453 | return Some(n.id); | |
| 454 | } | |
| 455 | } | |
| 456 | None | |
| 457 | } | |
| 458 | ||
| 459 | fn hit_input_port(&self, comp: &Comp, local: [f32; 2]) -> Option<(NodeId, &'static str)> { | |
| 460 | for n in comp.nodes.iter().rev() { | |
| 461 | if !self.scope.shows(n) { continue; } | |
| 462 | let r = self.node_rect_local(n.pos); | |
| 463 | if local[0] >= r[0] - 10.0 && local[0] <= r[2] + 10.0 && local[1] >= r[1] - 10.0 && local[1] <= r[3] + 10.0 { | |
| 464 | // Pick Background/Foreground/Mask cleanly based on approach edge for Merge/Displace! | |
| 465 | if n.kind == "Merge" || n.kind == "Displace" || n.kind == "Dissolve" { | |
| 466 | if local[1] < r[1] + 2.0 { | |
| 467 | return Some((n.id, "Foreground")); | |
| 468 | } | |
| 469 | if local[1] > r[3] - 2.0 { | |
| 470 | return Some((n.id, "EffectMask")); | |
| 471 | } | |
| 472 | return Some((n.id, "Background")); | |
| 473 | } | |
| 474 | if n.kind == "Merge3D" || n.kind == "Renderer3D" { | |
| 475 | return Some((n.id, "SceneInput1")); | |
| 476 | } | |
| 477 | if local[1] > r[3] - 1.5 && n.kind != "PipeRouter" { | |
| 478 | return Some((n.id, "EffectMask")); | |
| 479 | } | |
| 480 | return Some((n.id, default_main_input_port(&n.kind))); | |
| 481 | } | |
| 482 | } | |
| 483 | None | |
| 484 | } | |
| 485 | ||
| 486 | /// Hit-tests already-connected input port triangles so the user can drag an input off to disconnect/rewire. | |
| 487 | fn hit_connected_input_port(&self, comp: &Comp, local: [f32; 2]) -> Option<(NodeId, String, NodeId)> { | |
| 488 | let hit_r2 = 10.5_f32 * 10.5_f32; | |
| 489 | for dst in comp.nodes.iter().rev() { | |
| 490 | if !self.scope.shows(dst) { continue; } | |
| 491 | for (port, binding) in &dst.inputs { | |
| 492 | let Some(src_name) = &binding.source_op else { continue }; | |
| 493 | let Some(src_id) = self.scope.resolve(comp, src_name) else { continue }; | |
| 494 | let role = PortRole::from_input_name(port); | |
| 495 | let (_a, _ae, b, be) = self.wire_endpoints(comp, src_id, dst.id, role); | |
| 496 | let bn = be.normal(); | |
| 497 | let center = [b[0] + bn[0] * 4.0, b[1] + bn[1] * 4.0]; | |
| 498 | let dx = local[0] - center[0]; | |
| 499 | let dy = local[1] - center[1]; | |
| 500 | if dx * dx + dy * dy <= hit_r2 { | |
| 501 | return Some((dst.id, port.clone(), src_id)); | |
| 502 | } | |
| 503 | } | |
| 504 | } | |
| 505 | None | |
| 506 | } | |
| 507 | ||
| 508 | fn hit_wire(&self, comp: &Comp, local: [f32; 2], ignore_node: Option<NodeId>) -> Option<WireRef> { | |
| 509 | let mut best: Option<(f32, WireRef)> = None; | |
| 510 | for dst in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 511 | if Some(dst.id) == ignore_node { | |
| 512 | continue; | |
| 513 | } | |
| 514 | for (port, binding) in &dst.inputs { | |
| 515 | let Some(src_name) = &binding.source_op else { continue }; | |
| 516 | let Some(src_id) = self.scope.resolve(comp, src_name) else { continue }; | |
| 517 | if Some(src_id) == ignore_node { | |
| 518 | continue; | |
| 519 | } | |
| 520 | let role = PortRole::from_input_name(port); | |
| 521 | let (a, ae, b, be) = self.wire_endpoints(comp, src_id, dst.id, role); | |
| 522 | let d = dist_to_straight_wire(local, a, ae, b, be, self.zoom); | |
| 523 | if d < 10.0 && best.as_ref().is_none_or(|(bd, _)| d < *bd) { | |
| 524 | best = Some(( | |
| 525 | d, | |
| 526 | WireRef { | |
| 527 | src: src_id, | |
| 528 | dst: dst.id, | |
| 529 | port: port.clone(), | |
| 530 | }, | |
| 531 | )); | |
| 532 | } | |
| 533 | } | |
| 534 | } | |
| 535 | best.map(|(_, w)| w) | |
| 536 | } | |
| 537 | ||
| 538 | 367 | fn begin_modal(&mut self, comp: &Comp, history: &mut CompHistory, mode: TransformMode) { |
| 539 | 368 | if self.selected.is_empty() { |
| 540 | 369 | return; |
| ... | ... | @@ -1082,564 +911,12 @@ impl FlowView { |
| 1082 | 911 | self.pan[0] += before[0] - after[0]; |
| 1083 | 912 | self.pan[1] += before[1] - after[1]; |
| 1084 | 913 | } |
| 1085 | ||
| 1086 | pub fn prepare_labels(&mut self, comp: &Comp) { | |
| 1087 | let label_font = text::Font { | |
| 1088 | families: Arc::from([text::Family::Sans]), | |
| 1089 | size: 9.5, | |
| 1090 | weight: 500, | |
| 1091 | italic: false, | |
| 1092 | }; | |
| 1093 | let badge_font = text::Font { | |
| 1094 | families: Arc::from([text::Family::Mono]), | |
| 1095 | size: 10.0, | |
| 1096 | weight: 500, | |
| 1097 | italic: false, | |
| 1098 | }; | |
| 1099 | let tiny_font = text::Font { | |
| 1100 | families: Arc::from([text::Family::Mono]), | |
| 1101 | size: 7.5, | |
| 1102 | weight: 600, | |
| 1103 | italic: false, | |
| 1104 | }; | |
| 1105 | let make_layout = |s: &str, font: &text::Font| { | |
| 1106 | let styles = [text::Style::new(font.clone())]; | |
| 1107 | text::Paragraph { | |
| 1108 | text: s, | |
| 1109 | styles: &styles, | |
| 1110 | spans: &[], | |
| 1111 | replacements: &[], | |
| 1112 | width: None, | |
| 1113 | indent: 0.0, | |
| 1114 | line_height: 1.2, | |
| 1115 | align: text::Align::Left, | |
| 1116 | tab: 32.0, | |
| 1117 | } | |
| 1118 | .layout() | |
| 1119 | }; | |
| 1120 | if self.label_cache.len() > comp.nodes.len() + LABEL_CACHE_SLACK { | |
| 1121 | self.label_cache.clear(); | |
| 1122 | } | |
| 1123 | for n in &comp.nodes { | |
| 1124 | self.label_cache.entry(n.name.clone()).or_insert_with(|| make_layout(&n.name, &label_font)); | |
| 1125 | } | |
| 1126 | self.badge_cache | |
| 1127 | .entry("preserved".to_owned()) | |
| 1128 | .or_insert_with(|| make_layout("preserved", &tiny_font)); | |
| 1129 | self.badge_cache | |
| 1130 | .entry("cycle!".to_owned()) | |
| 1131 | .or_insert_with(|| make_layout("cycle!", &tiny_font)); | |
| 1132 | for key in [ | |
| 1133 | HUD_ROOT, | |
| 1134 | HUD_GROUP, | |
| 1135 | "GRAB [LMB/Enter confirm, RMB/Esc cancel, X/Y axis, Shift: Splice]", | |
| 1136 | "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 1137 | "SCALE [LMB/Enter confirm, RMB/Esc cancel, X/Y axis]", | |
| 1138 | ] { | |
| 1139 | self.badge_cache.entry(key.to_owned()).or_insert_with(|| make_layout(key, &badge_font)); | |
| 1140 | } | |
| 1141 | } | |
| 1142 | ||
| 1143 | pub fn paint<'a>(&'a self, comp: &'a Comp) -> Vec<Primitive<'a>> { | |
| 1144 | let mut out = Vec::with_capacity(comp.nodes.len() * 10 + 128); | |
| 1145 | let [w, h] = self.size; | |
| 1146 | ||
| 1147 | // 1. Subtle architectural grid aligned to Fusion's 110×33 flow cells | |
| 1148 | let mult = if self.zoom < 0.35 { 4.0 } else if self.zoom < 0.70 { 2.0 } else { 1.0 }; | |
| 1149 | let step_x = 110.0 * mult; | |
| 1150 | let step_y = 33.0 * mult * 2.0; | |
| 1151 | let tl = self.local_to_world([0.0, 0.0]); | |
| 1152 | let br = self.local_to_world([w, h]); | |
| 1153 | let gx0 = (tl[0] / step_x).floor() as i32; | |
| 1154 | let gx1 = (br[0] / step_x).ceil() as i32; | |
| 1155 | let gy0 = (tl[1] / step_y).floor() as i32; | |
| 1156 | let gy1 = (br[1] / step_y).ceil() as i32; | |
| 1157 | for gx in gx0..=gx1 { | |
| 1158 | let x = self.world_to_local([gx as f32 * step_x, 0.0])[0]; | |
| 1159 | let col = if gx % 4 == 0 { self.palette.grid_major } else { self.palette.grid_minor }; | |
| 1160 | out.push(Primitive::Segment { from: [x, 0.0], to: [x, h], width: 1.0, color: col }); | |
| 1161 | } | |
| 1162 | for gy in gy0..=gy1 { | |
| 1163 | let y = self.world_to_local([0.0, gy as f32 * step_y])[1]; | |
| 1164 | let col = if gy % 4 == 0 { self.palette.grid_major } else { self.palette.grid_minor }; | |
| 1165 | out.push(Primitive::Segment { from: [0.0, y], to: [w, y], width: 1.0, color: col }); | |
| 1166 | } | |
| 1167 | ||
| 1168 | // 2. Modal Blender axis-lock guide lines | |
| 1169 | if let Some(m) = &self.modal { | |
| 1170 | let piv = self.world_to_local(m.pivot_world); | |
| 1171 | match m.axis { | |
| 1172 | AxisLock::X => out.push(Primitive::Segment { | |
| 1173 | from: [0.0, piv[1]], | |
| 1174 | to: [w, piv[1]], | |
| 1175 | width: 1.5, | |
| 1176 | color: [0.95, 0.28, 0.34, 0.85], | |
| 1177 | }), | |
| 1178 | AxisLock::Y => out.push(Primitive::Segment { | |
| 1179 | from: [piv[0], 0.0], | |
| 1180 | to: [piv[0], h], | |
| 1181 | width: 1.5, | |
| 1182 | color: [0.28, 0.90, 0.42, 0.85], | |
| 1183 | }), | |
| 1184 | AxisLock::None => {} | |
| 1185 | } | |
| 1186 | } | |
| 1187 | ||
| 1188 | // 3. Straight Fusion wires + crisp white output squares (□) + inward-pointing colored input arrows (▶)! | |
| 1189 | let large_ports = self.zoom >= 0.72; | |
| 1190 | let tri_len = if large_ports { 6.5 } else { 4.5 }; | |
| 1191 | let out_half = if large_ports { 2.8 } else { 2.0 }; | |
| 1192 | let out_gap = if large_ports { 2.2 } else { 1.5 }; | |
| 1193 | let port_outline = [0.04, 0.04, 0.06, 0.92]; | |
| 1194 | ||
| 1195 | for dst in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 1196 | // Track used edges on `dst` so multiple inputs on the same edge spread cleanly instead of overlapping | |
| 1197 | let mut used_edges: HashMap<u8, usize> = HashMap::new(); | |
| 1198 | let mut sorted_ports: Vec<(&String, &model::InputBinding)> = dst.inputs.iter().collect(); | |
| 1199 | sorted_ports.sort_by_key(|(p, _)| match PortRole::from_input_name(p) { | |
| 1200 | PortRole::Background => 0u8, | |
| 1201 | PortRole::Foreground => 1, | |
| 1202 | PortRole::Mask => 2, | |
| 1203 | PortRole::Scene3D => 3, | |
| 1204 | PortRole::Aux => 4, | |
| 1205 | }); | |
| 1206 | for (port, binding) in sorted_ports { | |
| 1207 | let Some(src_name) = &binding.source_op else { continue }; | |
| 1208 | let Some(src_id) = self.scope.resolve(comp, src_name) else { continue }; | |
| 1209 | let role = PortRole::from_input_name(port); | |
| 1210 | let (a, ae, mut b, mut be) = self.wire_endpoints(comp, src_id, dst.id, role); | |
| 1211 | let ekey = |e: Edge| match e { Edge::Left => 0u8, Edge::Right => 1, Edge::Top => 2, Edge::Bottom => 3 }; | |
| 1212 | let count = used_edges.entry(ekey(be)).or_insert(0); | |
| 1213 | if *count > 0 { | |
| 1214 | // Spread horizontal top/bottom ports (e.g. FG + Mask) like Fusion's ColorCorrector / Merge | |
| 1215 | let dr = self.node_rect_local(dst.pos); | |
| 1216 | if matches!(be, Edge::Top | Edge::Bottom) { | |
| 1217 | let t = if *count == 1 { 0.25 } else { 0.75 }; | |
| 1218 | b = be.point_on_rect(dr, t); | |
| 1219 | } else if role == PortRole::Mask { | |
| 1220 | be = Edge::Bottom; | |
| 1221 | b = be.point_on_rect(dr, 0.5); | |
| 1222 | } else { | |
| 1223 | let t = if *count == 1 { 0.30 } else { 0.70 }; | |
| 1224 | b = be.point_on_rect(dr, t); | |
| 1225 | } | |
| 1226 | } | |
| 1227 | *count += 1; | |
| 1228 | ||
| 1229 | let is_hot = self.hovered_wire.as_ref().is_some_and(|hw| hw.src == src_id && hw.dst == dst.id && hw.port == *port); | |
| 1230 | let is_sel = self.selected.contains(&src_id) || self.selected.contains(&dst.id); | |
| 1231 | ||
| 1232 | let wire_col = if is_hot { | |
| 1233 | [0.98, 0.82, 0.24, 1.0] | |
| 1234 | } else if is_sel { | |
| 1235 | [0.92, 0.94, 0.98, 0.96] | |
| 1236 | } else { | |
| 1237 | [0.64, 0.66, 0.72, 0.76] | |
| 1238 | }; | |
| 1239 | let width = if is_hot { 2.4 } else if is_sel { 1.6 } else { 1.1 }; | |
| 1240 | let an = ae.normal(); | |
| 1241 | let bn = be.normal(); | |
| 1242 | let op = [a[0] + an[0] * (out_gap + out_half), a[1] + an[1] * (out_gap + out_half)]; | |
| 1243 | let ip_base = [b[0] + bn[0] * tri_len, b[1] + bn[1] * tri_len]; | |
| 1244 | ||
| 1245 | // Straight wire directly from output square center to input triangle base (exact Fusion look!) | |
| 1246 | out.push(Primitive::Segment { from: op, to: ip_base, width, color: wire_col }); | |
| 1247 | ||
| 1248 | if self.zoom >= 0.36 { | |
| 1249 | // Output port: crisp Fusion white square `□` with thin dark outline | |
| 1250 | out.push(Primitive::RoundedRect { | |
| 1251 | rect: [op[0] - out_half, op[1] - out_half, op[0] + out_half, op[1] + out_half], | |
| 1252 | radius: 0.6, | |
| 1253 | stroke: None, | |
| 1254 | color: [0.94, 0.95, 0.98, 0.98], | |
| 1255 | }); | |
| 1256 | out.push(Primitive::RoundedRect { | |
| 1257 | rect: [op[0] - out_half, op[1] - out_half, op[0] + out_half, op[1] + out_half], | |
| 1258 | radius: 0.6, | |
| 1259 | stroke: Some(0.9), | |
| 1260 | color: port_outline, | |
| 1261 | }); | |
| 1262 | ||
| 1263 | // Input port: crisp Fusion colored triangle pointing INTO `dst`! | |
| 1264 | let pcol = port_color(role); | |
| 1265 | let tri = input_arrow_svg(be, large_ports); | |
| 1266 | out.push(Primitive::Path { | |
| 1267 | data: tri, | |
| 1268 | origin: b, | |
| 1269 | style: PathStyle::Fill, | |
| 1270 | color: pcol, | |
| 1271 | }); | |
| 1272 | out.push(Primitive::Path { | |
| 1273 | data: tri, | |
| 1274 | origin: b, | |
| 1275 | style: PathStyle::Stroke(0.9), | |
| 1276 | color: port_outline, | |
| 1277 | }); | |
| 1278 | } | |
| 1279 | } | |
| 1280 | } | |
| 1281 | ||
| 1282 | // Active wire drag preview (smooth cubic bezier + cycle rejection feedback) | |
| 1283 | if let Some(wd) = &self.wire_drag { | |
| 1284 | if wd.src.0 < comp.nodes.len() { | |
| 1285 | let sr = self.node_rect_local(comp.node(wd.src).pos); | |
| 1286 | let edge = self.node_output_edge(comp, wd.src); | |
| 1287 | let a = edge.point_on_rect(sr, 0.5); | |
| 1288 | let (end, is_cycle, target_role) = match wd.target { | |
| 1289 | Some((dst, port, cycle)) if dst.0 < comp.nodes.len() => { | |
| 1290 | let role = PortRole::from_input_name(port); | |
| 1291 | let (_, _, b, _) = self.wire_endpoints(comp, wd.src, dst, role); | |
| 1292 | (b, cycle, Some(role)) | |
| 1293 | } | |
| 1294 | _ => (wd.cursor_local, false, None), | |
| 1295 | }; | |
| 1296 | let wire_col = if is_cycle { | |
| 1297 | [0.96, 0.30, 0.34, 1.0] | |
| 1298 | } else if let Some(role) = target_role { | |
| 1299 | port_color(role) | |
| 1300 | } else { | |
| 1301 | [0.98, 0.84, 0.30, 1.0] | |
| 1302 | }; | |
| 1303 | let pts = sample_preview_bezier(a, edge.normal(), end, 16); | |
| 1304 | for seg in pts.windows(2) { | |
| 1305 | out.push(Primitive::Segment { | |
| 1306 | from: seg[0], | |
| 1307 | to: seg[1], | |
| 1308 | width: if is_cycle { 2.4 } else { 2.0 }, | |
| 1309 | color: wire_col, | |
| 1310 | }); | |
| 1311 | } | |
| 1312 | if is_cycle { | |
| 1313 | if let Some(badge) = self.badge_cache.get("cycle!") { | |
| 1314 | let bx = end[0] + 8.0; | |
| 1315 | let by = end[1] - 14.0; | |
| 1316 | out.push(Primitive::RoundedRect { | |
| 1317 | rect: [bx, by, bx + 36.0, by + 13.0], | |
| 1318 | radius: 3.0, | |
| 1319 | stroke: None, | |
| 1320 | color: [0.30, 0.06, 0.08, 0.94], | |
| 1321 | }); | |
| 1322 | out.push(Primitive::RoundedRect { | |
| 1323 | rect: [bx, by, bx + 36.0, by + 13.0], | |
| 1324 | radius: 3.0, | |
| 1325 | stroke: Some(1.0), | |
| 1326 | color: [0.96, 0.30, 0.34, 0.95], | |
| 1327 | }); | |
| 1328 | out.push(Primitive::Text { | |
| 1329 | text: badge as &dyn Glyphs, | |
| 1330 | origin: [bx + 5.0, by + 2.0], | |
| 1331 | clip: Some([bx, by, bx + 36.0, by + 13.0]), | |
| 1332 | ink: [1.0, 0.65, 0.65, 1.0], | |
| 1333 | }); | |
| 1334 | } | |
| 1335 | } | |
| 1336 | } | |
| 1337 | } | |
| 1338 | ||
| 1339 | // 4. Sleek Thin Fusion-Style Node Bars (color line along BOTTOM!) | |
| 1340 | for n in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 1341 | let r = self.node_rect_local(n.pos); | |
| 1342 | if r[2] < -20.0 || r[0] > w + 20.0 || r[3] < -20.0 || r[1] > h + 20.0 { | |
| 1343 | continue; | |
| 1344 | } | |
| 1345 | let sel = self.selected.contains(&n.id); | |
| 1346 | let primary = self.primary == Some(n.id); | |
| 1347 | let hovered = self.hovered_node == Some(n.id); | |
| 1348 | let v1 = self.viewed_nodes[0] == Some(n.id); | |
| 1349 | let v2 = self.viewed_nodes[1] == Some(n.id); | |
| 1350 | let supported = is_supported_tool(&n.kind); | |
| 1351 | let accent = if supported { | |
| 1352 | node_accent(&n.kind) | |
| 1353 | } else { | |
| 1354 | [0.92, 0.64, 0.24, 1.0] | |
| 1355 | }; | |
| 1356 | ||
| 1357 | let bg = if n.bypassed { | |
| 1358 | [self.palette.node_fill[0] * 0.6, self.palette.node_fill[1] * 0.6, self.palette.node_fill[2] * 0.6, 0.65] | |
| 1359 | } else if sel { | |
| 1360 | self.palette.node_selected | |
| 1361 | } else if hovered { | |
| 1362 | self.palette.node_hover | |
| 1363 | } else if !supported { | |
| 1364 | [ | |
| 1365 | (self.palette.node_fill[0] + 0.035).min(1.0), | |
| 1366 | (self.palette.node_fill[1] + 0.020).min(1.0), | |
| 1367 | self.palette.node_fill[2], | |
| 1368 | 1.0, | |
| 1369 | ] | |
| 1370 | } else { | |
| 1371 | self.palette.node_fill | |
| 1372 | }; | |
| 1373 | ||
| 1374 | let radius = (3.5 * self.zoom).clamp(1.5, 5.0); | |
| 1375 | out.push(Primitive::RoundedRect { rect: r, radius, stroke: None, color: bg }); | |
| 1376 | ||
| 1377 | // Bottom category color line (exact Fusion look — thin sleek strip on bottom edge!) | |
| 1378 | let bar_h = (2.6 * self.zoom).clamp(1.4, 3.4); | |
| 1379 | out.push(Primitive::Rect { | |
| 1380 | rect: [r[0] + 1.5, (r[3] - bar_h).max(r[1]), r[2] - 1.5, r[3]], | |
| 1381 | color: if n.bypassed { [0.28, 0.28, 0.32, 0.6] } else { accent }, | |
| 1382 | }); | |
| 1383 | ||
| 1384 | // Crisp border (amber accent for preserved/passthrough unknown nodes) | |
| 1385 | let border_col = if primary { | |
| 1386 | [0.98, 0.78, 0.30, 1.0] | |
| 1387 | } else if sel { | |
| 1388 | self.palette.accent | |
| 1389 | } else if !supported { | |
| 1390 | [0.92, 0.64, 0.24, 0.85] | |
| 1391 | } else { | |
| 1392 | self.palette.node_border | |
| 1393 | }; | |
| 1394 | out.push(Primitive::RoundedRect { | |
| 1395 | rect: r, | |
| 1396 | radius, | |
| 1397 | stroke: Some(if sel || primary { 1.4 } else if !supported { 1.15 } else { 0.9 }), | |
| 1398 | color: border_col, | |
| 1399 | }); | |
| 1400 | // Subtle top dashed ticks for preserved/passthrough unknown nodes | |
| 1401 | if !supported && self.zoom >= 0.45 { | |
| 1402 | let mut tx = r[0] + 5.0; | |
| 1403 | while tx + 4.0 < r[2] - 4.0 { | |
| 1404 | out.push(Primitive::Segment { | |
| 1405 | from: [tx, r[1] + 0.8], | |
| 1406 | to: [(tx + 4.0).min(r[2] - 4.0), r[1] + 0.8], | |
| 1407 | width: 1.2, | |
| 1408 | color: [0.96, 0.72, 0.30, 0.90], | |
| 1409 | }); | |
| 1410 | tx += 8.0; | |
| 1411 | } | |
| 1412 | } | |
| 1413 | ||
| 1414 | // Node label centered cleanly inside thin bar | |
| 1415 | if self.zoom >= 0.42 { | |
| 1416 | if let Some(layout) = self.label_cache.get(&n.name) { | |
| 1417 | let ink = if n.bypassed { self.palette.text_dim } else { self.palette.text }; | |
| 1418 | let pad_x = (6.0 * self.zoom).clamp(3.0, 8.0); | |
| 1419 | let text_y = (r[1] + r[3] - bar_h) * 0.5 - 6.0; | |
| 1420 | out.push(Primitive::Text { | |
| 1421 | text: layout as &dyn Glyphs, | |
| 1422 | origin: [r[0] + pad_x, text_y], | |
| 1423 | clip: Some([r[0] + pad_x, r[1] + 1.0, r[2] - 4.0, r[3] - bar_h]), | |
| 1424 | ink, | |
| 1425 | }); | |
| 1426 | } | |
| 1427 | } | |
| 1428 | ||
| 1429 | // Preserved/passthrough pill badge underneath bottom-right for unknown/OFX nodes | |
| 1430 | if !supported && self.zoom >= 0.50 { | |
| 1431 | if let Some(badge) = self.badge_cache.get("preserved") { | |
| 1432 | let bw = 46.0; | |
| 1433 | let bh = 10.5; | |
| 1434 | let bx = (r[2] - bw).max(r[0] + 28.0); | |
| 1435 | let by = r[3] + 1.5; | |
| 1436 | out.push(Primitive::RoundedRect { | |
| 1437 | rect: [bx, by, bx + bw, by + bh], | |
| 1438 | radius: 3.0, | |
| 1439 | stroke: None, | |
| 1440 | color: [0.14, 0.10, 0.05, 0.92], | |
| 1441 | }); | |
| 1442 | out.push(Primitive::RoundedRect { | |
| 1443 | rect: [bx, by, bx + bw, by + bh], | |
| 1444 | radius: 3.0, | |
| 1445 | stroke: Some(0.8), | |
| 1446 | color: [0.92, 0.64, 0.24, 0.72], | |
| 1447 | }); | |
| 1448 | out.push(Primitive::Text { | |
| 1449 | text: badge as &dyn Glyphs, | |
| 1450 | origin: [bx + 4.0, by + 1.0], | |
| 1451 | clip: Some([bx + 2.0, by, bx + bw - 2.0, by + bh]), | |
| 1452 | ink: [0.98, 0.78, 0.40, 1.0], | |
| 1453 | }); | |
| 1454 | } | |
| 1455 | } | |
| 1456 | ||
| 1457 | // Fusion-style tiny viewer indicator pill underneath bottom-left (`● ○` for Viewer 1 / Viewer 2!) | |
| 1458 | if v1 || v2 { | |
| 1459 | let pw = 22.0; | |
| 1460 | let ph = 9.0; | |
| 1461 | let px = r[0] + 4.0; | |
| 1462 | let py = r[3] + 1.5; | |
| 1463 | out.push(Primitive::RoundedRect { | |
| 1464 | rect: [px, py, px + pw, py + ph], | |
| 1465 | radius: 3.0, | |
| 1466 | stroke: None, | |
| 1467 | color: [0.02, 0.02, 0.03, 0.92], | |
| 1468 | }); | |
| 1469 | let d1 = if v1 { [0.24, 0.90, 0.82, 1.0] } else { [0.26, 0.28, 0.34, 0.6] }; | |
| 1470 | let d2 = if v2 { [0.98, 0.68, 0.28, 1.0] } else { [0.26, 0.28, 0.34, 0.6] }; | |
| 1471 | out.push(Primitive::RoundedRect { | |
| 1472 | rect: [px + 3.5, py + 2.0, px + 8.5, py + 7.0], | |
| 1473 | radius: 2.5, | |
| 1474 | stroke: None, | |
| 1475 | color: d1, | |
| 1476 | }); | |
| 1477 | out.push(Primitive::RoundedRect { | |
| 1478 | rect: [px + 13.0, py + 2.0, px + 18.0, py + 7.0], | |
| 1479 | radius: 2.5, | |
| 1480 | stroke: None, | |
| 1481 | color: d2, | |
| 1482 | }); | |
| 1483 | } | |
| 1484 | ||
| 1485 | if n.bypassed { | |
| 1486 | let ym = (r[1] + r[3]) * 0.5; | |
| 1487 | out.push(Primitive::Segment { | |
| 1488 | from: [r[0] + 3.0, ym], | |
| 1489 | to: [r[2] - 3.0, ym], | |
| 1490 | width: 1.4, | |
| 1491 | color: [0.95, 0.35, 0.35, 0.85], | |
| 1492 | }); | |
| 1493 | } | |
| 1494 | } | |
| 1495 | ||
| 1496 | // 5. Box select marquee | |
| 1497 | if let Some(bs) = &self.box_select { | |
| 1498 | let x0 = bs.start_local[0].min(bs.cur_local[0]); | |
| 1499 | let x1 = bs.start_local[0].max(bs.cur_local[0]); | |
| 1500 | let y0 = bs.start_local[1].min(bs.cur_local[1]); | |
| 1501 | let y1 = bs.start_local[1].max(bs.cur_local[1]); | |
| 1502 | out.push(Primitive::RoundedRect { | |
| 1503 | rect: [x0, y0, x1, y1], | |
| 1504 | radius: 2.0, | |
| 1505 | stroke: None, | |
| 1506 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 1507 | }); | |
| 1508 | out.push(Primitive::RoundedRect { | |
| 1509 | rect: [x0, y0, x1, y1], | |
| 1510 | radius: 2.0, | |
| 1511 | stroke: Some(1.2), | |
| 1512 | color: self.palette.accent, | |
| 1513 | }); | |
| 1514 | } | |
| 1515 | ||
| 1516 | // 6. Status HUD strip | |
| 1517 | let hud_key = match self.modal.as_ref().map(|m| m.mode) { | |
| 1518 | Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X/Y axis, Shift: Splice]", | |
| 1519 | Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 1520 | Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X/Y axis]", | |
| 1521 | None if !self.scope.is_root() => HUD_GROUP, | |
| 1522 | None => HUD_ROOT, | |
| 1523 | }; | |
| 1524 | if let Some(l) = self.badge_cache.get(hud_key) { | |
| 1525 | let pill_h = 20.0; | |
| 1526 | out.push(Primitive::RoundedRect { | |
| 1527 | rect: [8.0, h - pill_h - 6.0, w - 8.0, h - 6.0], | |
| 1528 | radius: 4.0, | |
| 1529 | stroke: None, | |
| 1530 | color: [self.palette.bg[0] * 0.75, self.palette.bg[1] * 0.75, self.palette.bg[2] * 0.75, 0.86], | |
| 1531 | }); | |
| 1532 | out.push(Primitive::Text { | |
| 1533 | text: l as &dyn Glyphs, | |
| 1534 | origin: [14.0, h - pill_h - 2.5], | |
| 1535 | clip: Some([12.0, h - pill_h - 6.0, w - 12.0, h - 6.0]), | |
| 1536 | ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, | |
| 1537 | }); | |
| 1538 | } | |
| 1539 | ||
| 1540 | out | |
| 1541 | } | |
| 1542 | 914 | } |
| 1543 | 915 | |
| 1544 | 916 | fn read_only_notice(actions: &mut Vec<FlowAction>) { |
| 1545 | 917 | actions.push(FlowAction::StatusNotice("Group contents are read-only; press Esc to leave the group".to_owned())); |
| 1546 | 918 | } |
| 1547 | 919 | |
| 1548 | /// Static SVG triangles pointing INTO the node edge (`origin` sits flush on the node border). | |
| 1549 | fn input_arrow_svg(edge: Edge, large: bool) -> &'static str { | |
| 1550 | match (edge, large) { | |
| 1551 | (Edge::Left, true) => "M 0 0 L -6.5 -3.6 L -6.5 3.6 Z", | |
| 1552 | (Edge::Left, false) => "M 0 0 L -4.5 -2.6 L -4.5 2.6 Z", | |
| 1553 | (Edge::Right, true) => "M 0 0 L 6.5 -3.6 L 6.5 3.6 Z", | |
| 1554 | (Edge::Right, false) => "M 0 0 L 4.5 -2.6 L 4.5 2.6 Z", | |
| 1555 | (Edge::Top, true) => "M 0 0 L -3.6 -6.5 L 3.6 -6.5 Z", | |
| 1556 | (Edge::Top, false) => "M 0 0 L -2.6 -4.5 L 2.6 -4.5 Z", | |
| 1557 | (Edge::Bottom, true) => "M 0 0 L -3.6 6.5 L 3.6 6.5 Z", | |
| 1558 | (Edge::Bottom, false) => "M 0 0 L -2.6 4.5 L 2.6 4.5 Z", | |
| 1559 | } | |
| 1560 | } | |
| 1561 | ||
| 1562 | /// Picks the closest node edge facing vector `(dx, dy)` accounting for thin rectangular bar aspect. | |
| 1563 | fn closest_edge(dx: f32, dy: f32) -> Edge { | |
| 1564 | // Nodes are 84x21 (4:1 aspect), so require a balanced X bias so horizontal rows snap Left/Right | |
| 1565 | // while vertical stacks naturally snap Top/Bottom! | |
| 1566 | if dx.abs() * 0.58 >= dy.abs() { | |
| 1567 | if dx >= 0.0 { Edge::Right } else { Edge::Left } | |
| 1568 | } else if dy >= 0.0 { | |
| 1569 | Edge::Bottom | |
| 1570 | } else { | |
| 1571 | Edge::Top | |
| 1572 | } | |
| 1573 | } | |
| 1574 | ||
| 1575 | /// Auto-wraps input port to the closest side facing the producer, gently nudging Mask/FG when needed. | |
| 1576 | fn closest_edge_for_input(dx_to_src: f32, dy_to_src: f32, role: PortRole) -> Edge { | |
| 1577 | let primary = closest_edge(dx_to_src, dy_to_src); | |
| 1578 | match role { | |
| 1579 | PortRole::Mask => { | |
| 1580 | // Masks prefer Top/Bottom unless directly horizontal | |
| 1581 | if dy_to_src.abs() >= dx_to_src.abs() * 0.35 { | |
| 1582 | if dy_to_src >= 0.0 { Edge::Bottom } else { Edge::Top } | |
| 1583 | } else { | |
| 1584 | primary | |
| 1585 | } | |
| 1586 | } | |
| 1587 | _ => primary, | |
| 1588 | } | |
| 1589 | } | |
| 1590 | ||
| 1591 | fn port_color(role: PortRole) -> [f32; 4] { | |
| 1592 | match role { | |
| 1593 | PortRole::Background => [0.94, 0.76, 0.26, 1.0], // Fusion gold | |
| 1594 | PortRole::Foreground => [0.34, 0.86, 0.46, 1.0], // Fusion green | |
| 1595 | PortRole::Mask => [0.32, 0.64, 0.98, 1.0], // Fusion blue | |
| 1596 | PortRole::Scene3D => [0.30, 0.86, 0.90, 1.0], // Cyan | |
| 1597 | PortRole::Aux => [0.74, 0.58, 0.90, 1.0], // Violet | |
| 1598 | } | |
| 1599 | } | |
| 1600 | ||
| 1601 | fn node_accent(kind: &str) -> [f32; 4] { | |
| 1602 | match kind { | |
| 1603 | "Loader" | "Saver" => [0.32, 0.72, 0.92, 1.0], | |
| 1604 | "Merge" | "Switch" | "Fuse.Switch" => [0.58, 0.60, 0.66, 1.0], | |
| 1605 | "Background" | "FastNoise" => [0.36, 0.80, 0.42, 1.0], | |
| 1606 | "TextPlus" | "Text3D" => [0.34, 0.86, 0.54, 1.0], | |
| 1607 | "ColorCorrector" | "BrightnessContrast" | "ColorCurves" | "HueCurves" | "LumaKeyer" | "UltraKeyer" => { | |
| 1608 | [0.92, 0.58, 0.22, 1.0] | |
| 1609 | } | |
| 1610 | "Blur" | "DirectionalBlur" | "Glow" | "SoftGlow" | "Displace" | "ErodeDilate" => { | |
| 1611 | [0.92, 0.52, 0.24, 1.0] | |
| 1612 | } | |
| 1613 | "Transform" | "Scale" | "Crop" | "TimeStretcher" | "TimeSpeed" => [0.84, 0.66, 0.28, 1.0], | |
| 1614 | "EllipseMask" | "RectangleMask" | "PolylineMask" | "BitmapMask" => [0.32, 0.62, 0.98, 1.0], | |
| 1615 | k if k.starts_with("Fuse.") => [0.88, 0.44, 0.76, 1.0], | |
| 1616 | _ => [0.54, 0.54, 0.60, 1.0], | |
| 1617 | } | |
| 1618 | } | |
| 1619 | ||
| 1620 | fn dist_to_straight_wire(pt: [f32; 2], a: [f32; 2], ae: Edge, b: [f32; 2], be: Edge, zoom: f32) -> f32 { | |
| 1621 | let stub = (8.0 * zoom).clamp(4.0, 11.0); | |
| 1622 | let an = ae.normal(); | |
| 1623 | let bn = be.normal(); | |
| 1624 | let a_out = [a[0] + an[0] * stub, a[1] + an[1] * stub]; | |
| 1625 | let b_out = [b[0] + bn[0] * stub, b[1] + bn[1] * stub]; | |
| 1626 | dist_to_seg(pt, a, a_out) | |
| 1627 | .min(dist_to_seg(pt, a_out, b_out)) | |
| 1628 | .min(dist_to_seg(pt, b_out, b)) | |
| 1629 | } | |
| 1630 | ||
| 1631 | fn dist_to_seg(p: [f32; 2], a: [f32; 2], b: [f32; 2]) -> f32 { | |
| 1632 | let ab = [b[0] - a[0], b[1] - a[1]]; | |
| 1633 | let len2 = ab[0] * ab[0] + ab[1] * ab[1]; | |
| 1634 | let t = if len2 < 1e-6 { | |
| 1635 | 0.0 | |
| 1636 | } else { | |
| 1637 | (((p[0] - a[0]) * ab[0] + (p[1] - a[1]) * ab[1]) / len2).clamp(0.0, 1.0) | |
| 1638 | }; | |
| 1639 | let q = [a[0] + ab[0] * t, a[1] + ab[1] * t]; | |
| 1640 | ((p[0] - q[0]).powi(2) + (p[1] - q[1]).powi(2)).sqrt() | |
| 1641 | } | |
| 1642 | ||
| 1643 | 920 | /// Samples a smooth cubic Bezier preview wire leaving `a` along `out_normal` and arriving at `b`. |
| 1644 | 921 | pub fn sample_preview_bezier(a: [f32; 2], out_normal: [f32; 2], b: [f32; 2], steps: usize) -> Vec<[f32; 2]> { |
| 1645 | 922 | let steps = steps.max(2); |
| ... | ... | @@ -1663,6 +940,7 @@ pub fn sample_preview_bezier(a: [f32; 2], out_normal: [f32; 2], b: [f32; 2], ste |
| 1663 | 940 | #[cfg(test)] |
| 1664 | 941 | mod tests { |
| 1665 | 942 | use super::*; |
| 943 | use crate::hit::dist_to_seg; | |
| 1666 | 944 | |
| 1667 | 945 | fn test_comp() -> Comp { |
| 1668 | 946 | let src = r#"Composition { |
crates/flow/src/paint.rs created+503| ... | ... | @@ -0,0 +1,503 @@ |
| 1 | use super::*; | |
| 2 | ||
| 3 | impl FlowView { | |
| 4 | pub fn prepare_labels(&mut self, comp: &Comp) { | |
| 5 | let label_font = text::Font { | |
| 6 | families: Arc::from([text::Family::Sans]), | |
| 7 | size: 9.5, | |
| 8 | weight: 500, | |
| 9 | italic: false, | |
| 10 | }; | |
| 11 | let badge_font = text::Font { | |
| 12 | families: Arc::from([text::Family::Mono]), | |
| 13 | size: 10.0, | |
| 14 | weight: 500, | |
| 15 | italic: false, | |
| 16 | }; | |
| 17 | let tiny_font = text::Font { | |
| 18 | families: Arc::from([text::Family::Mono]), | |
| 19 | size: 7.5, | |
| 20 | weight: 600, | |
| 21 | italic: false, | |
| 22 | }; | |
| 23 | let make_layout = |s: &str, font: &text::Font| { | |
| 24 | let styles = [text::Style::new(font.clone())]; | |
| 25 | text::Paragraph { | |
| 26 | text: s, | |
| 27 | styles: &styles, | |
| 28 | spans: &[], | |
| 29 | replacements: &[], | |
| 30 | width: None, | |
| 31 | indent: 0.0, | |
| 32 | line_height: 1.2, | |
| 33 | align: text::Align::Left, | |
| 34 | tab: 32.0, | |
| 35 | } | |
| 36 | .layout() | |
| 37 | }; | |
| 38 | if self.label_cache.len() > comp.nodes.len() + LABEL_CACHE_SLACK { | |
| 39 | self.label_cache.clear(); | |
| 40 | } | |
| 41 | for n in &comp.nodes { | |
| 42 | self.label_cache.entry(n.name.clone()).or_insert_with(|| make_layout(&n.name, &label_font)); | |
| 43 | } | |
| 44 | self.badge_cache | |
| 45 | .entry("preserved".to_owned()) | |
| 46 | .or_insert_with(|| make_layout("preserved", &tiny_font)); | |
| 47 | self.badge_cache | |
| 48 | .entry("cycle!".to_owned()) | |
| 49 | .or_insert_with(|| make_layout("cycle!", &tiny_font)); | |
| 50 | for key in [ | |
| 51 | HUD_ROOT, | |
| 52 | HUD_GROUP, | |
| 53 | "GRAB [LMB/Enter confirm, RMB/Esc cancel, X/Y axis, Shift: Splice]", | |
| 54 | "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 55 | "SCALE [LMB/Enter confirm, RMB/Esc cancel, X/Y axis]", | |
| 56 | ] { | |
| 57 | self.badge_cache.entry(key.to_owned()).or_insert_with(|| make_layout(key, &badge_font)); | |
| 58 | } | |
| 59 | } | |
| 60 | ||
| 61 | pub fn paint<'a>(&'a self, comp: &'a Comp) -> Vec<Primitive<'a>> { | |
| 62 | let mut out = Vec::with_capacity(comp.nodes.len() * 10 + 128); | |
| 63 | let [w, h] = self.size; | |
| 64 | ||
| 65 | // 1. Subtle architectural grid aligned to Fusion's 110×33 flow cells | |
| 66 | let mult = if self.zoom < 0.35 { 4.0 } else if self.zoom < 0.70 { 2.0 } else { 1.0 }; | |
| 67 | let step_x = 110.0 * mult; | |
| 68 | let step_y = 33.0 * mult * 2.0; | |
| 69 | let tl = self.local_to_world([0.0, 0.0]); | |
| 70 | let br = self.local_to_world([w, h]); | |
| 71 | let gx0 = (tl[0] / step_x).floor() as i32; | |
| 72 | let gx1 = (br[0] / step_x).ceil() as i32; | |
| 73 | let gy0 = (tl[1] / step_y).floor() as i32; | |
| 74 | let gy1 = (br[1] / step_y).ceil() as i32; | |
| 75 | for gx in gx0..=gx1 { | |
| 76 | let x = self.world_to_local([gx as f32 * step_x, 0.0])[0]; | |
| 77 | let col = if gx % 4 == 0 { self.palette.grid_major } else { self.palette.grid_minor }; | |
| 78 | out.push(Primitive::Segment { from: [x, 0.0], to: [x, h], width: 1.0, color: col }); | |
| 79 | } | |
| 80 | for gy in gy0..=gy1 { | |
| 81 | let y = self.world_to_local([0.0, gy as f32 * step_y])[1]; | |
| 82 | let col = if gy % 4 == 0 { self.palette.grid_major } else { self.palette.grid_minor }; | |
| 83 | out.push(Primitive::Segment { from: [0.0, y], to: [w, y], width: 1.0, color: col }); | |
| 84 | } | |
| 85 | ||
| 86 | // 2. Modal Blender axis-lock guide lines | |
| 87 | if let Some(m) = &self.modal { | |
| 88 | let piv = self.world_to_local(m.pivot_world); | |
| 89 | match m.axis { | |
| 90 | AxisLock::X => out.push(Primitive::Segment { | |
| 91 | from: [0.0, piv[1]], | |
| 92 | to: [w, piv[1]], | |
| 93 | width: 1.5, | |
| 94 | color: [0.95, 0.28, 0.34, 0.85], | |
| 95 | }), | |
| 96 | AxisLock::Y => out.push(Primitive::Segment { | |
| 97 | from: [piv[0], 0.0], | |
| 98 | to: [piv[0], h], | |
| 99 | width: 1.5, | |
| 100 | color: [0.28, 0.90, 0.42, 0.85], | |
| 101 | }), | |
| 102 | AxisLock::None => {} | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | // 3. Straight Fusion wires + crisp white output squares (□) + inward-pointing colored input arrows (▶)! | |
| 107 | let large_ports = self.zoom >= 0.72; | |
| 108 | let tri_len = if large_ports { 6.5 } else { 4.5 }; | |
| 109 | let out_half = if large_ports { 2.8 } else { 2.0 }; | |
| 110 | let out_gap = if large_ports { 2.2 } else { 1.5 }; | |
| 111 | let port_outline = [0.04, 0.04, 0.06, 0.92]; | |
| 112 | ||
| 113 | for dst in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 114 | // Track used edges on `dst` so multiple inputs on the same edge spread cleanly instead of overlapping | |
| 115 | let mut used_edges: HashMap<u8, usize> = HashMap::new(); | |
| 116 | let mut sorted_ports: Vec<(&String, &model::InputBinding)> = dst.inputs.iter().collect(); | |
| 117 | sorted_ports.sort_by_key(|(p, _)| match PortRole::from_input_name(p) { | |
| 118 | PortRole::Background => 0u8, | |
| 119 | PortRole::Foreground => 1, | |
| 120 | PortRole::Mask => 2, | |
| 121 | PortRole::Scene3D => 3, | |
| 122 | PortRole::Aux => 4, | |
| 123 | }); | |
| 124 | for (port, binding) in sorted_ports { | |
| 125 | let Some(src_name) = &binding.source_op else { continue }; | |
| 126 | let Some(src_id) = self.scope.resolve(comp, src_name) else { continue }; | |
| 127 | let role = PortRole::from_input_name(port); | |
| 128 | let (a, ae, mut b, mut be) = self.wire_endpoints(comp, src_id, dst.id, role); | |
| 129 | let ekey = |e: Edge| match e { Edge::Left => 0u8, Edge::Right => 1, Edge::Top => 2, Edge::Bottom => 3 }; | |
| 130 | let count = used_edges.entry(ekey(be)).or_insert(0); | |
| 131 | if *count > 0 { | |
| 132 | // Spread horizontal top/bottom ports (e.g. FG + Mask) like Fusion's ColorCorrector / Merge | |
| 133 | let dr = self.node_rect_local(dst.pos); | |
| 134 | if matches!(be, Edge::Top | Edge::Bottom) { | |
| 135 | let t = if *count == 1 { 0.25 } else { 0.75 }; | |
| 136 | b = be.point_on_rect(dr, t); | |
| 137 | } else if role == PortRole::Mask { | |
| 138 | be = Edge::Bottom; | |
| 139 | b = be.point_on_rect(dr, 0.5); | |
| 140 | } else { | |
| 141 | let t = if *count == 1 { 0.30 } else { 0.70 }; | |
| 142 | b = be.point_on_rect(dr, t); | |
| 143 | } | |
| 144 | } | |
| 145 | *count += 1; | |
| 146 | ||
| 147 | let is_hot = self.hovered_wire.as_ref().is_some_and(|hw| hw.src == src_id && hw.dst == dst.id && hw.port == *port); | |
| 148 | let is_sel = self.selected.contains(&src_id) || self.selected.contains(&dst.id); | |
| 149 | ||
| 150 | let wire_col = if is_hot { | |
| 151 | [0.98, 0.82, 0.24, 1.0] | |
| 152 | } else if is_sel { | |
| 153 | [0.92, 0.94, 0.98, 0.96] | |
| 154 | } else { | |
| 155 | [0.64, 0.66, 0.72, 0.76] | |
| 156 | }; | |
| 157 | let width = if is_hot { 2.4 } else if is_sel { 1.6 } else { 1.1 }; | |
| 158 | let an = ae.normal(); | |
| 159 | let bn = be.normal(); | |
| 160 | let op = [a[0] + an[0] * (out_gap + out_half), a[1] + an[1] * (out_gap + out_half)]; | |
| 161 | let ip_base = [b[0] + bn[0] * tri_len, b[1] + bn[1] * tri_len]; | |
| 162 | ||
| 163 | // Straight wire directly from output square center to input triangle base (exact Fusion look!) | |
| 164 | out.push(Primitive::Segment { from: op, to: ip_base, width, color: wire_col }); | |
| 165 | ||
| 166 | if self.zoom >= 0.36 { | |
| 167 | // Output port: crisp Fusion white square `□` with thin dark outline | |
| 168 | out.push(Primitive::RoundedRect { | |
| 169 | rect: [op[0] - out_half, op[1] - out_half, op[0] + out_half, op[1] + out_half], | |
| 170 | radius: 0.6, | |
| 171 | stroke: None, | |
| 172 | color: [0.94, 0.95, 0.98, 0.98], | |
| 173 | }); | |
| 174 | out.push(Primitive::RoundedRect { | |
| 175 | rect: [op[0] - out_half, op[1] - out_half, op[0] + out_half, op[1] + out_half], | |
| 176 | radius: 0.6, | |
| 177 | stroke: Some(0.9), | |
| 178 | color: port_outline, | |
| 179 | }); | |
| 180 | ||
| 181 | // Input port: crisp Fusion colored triangle pointing INTO `dst`! | |
| 182 | let pcol = port_color(role); | |
| 183 | let tri = input_arrow_svg(be, large_ports); | |
| 184 | out.push(Primitive::Path { | |
| 185 | data: tri, | |
| 186 | origin: b, | |
| 187 | style: PathStyle::Fill, | |
| 188 | color: pcol, | |
| 189 | }); | |
| 190 | out.push(Primitive::Path { | |
| 191 | data: tri, | |
| 192 | origin: b, | |
| 193 | style: PathStyle::Stroke(0.9), | |
| 194 | color: port_outline, | |
| 195 | }); | |
| 196 | } | |
| 197 | } | |
| 198 | } | |
| 199 | ||
| 200 | // Active wire drag preview (smooth cubic bezier + cycle rejection feedback) | |
| 201 | if let Some(wd) = &self.wire_drag { | |
| 202 | if wd.src.0 < comp.nodes.len() { | |
| 203 | let sr = self.node_rect_local(comp.node(wd.src).pos); | |
| 204 | let edge = self.node_output_edge(comp, wd.src); | |
| 205 | let a = edge.point_on_rect(sr, 0.5); | |
| 206 | let (end, is_cycle, target_role) = match wd.target { | |
| 207 | Some((dst, port, cycle)) if dst.0 < comp.nodes.len() => { | |
| 208 | let role = PortRole::from_input_name(port); | |
| 209 | let (_, _, b, _) = self.wire_endpoints(comp, wd.src, dst, role); | |
| 210 | (b, cycle, Some(role)) | |
| 211 | } | |
| 212 | _ => (wd.cursor_local, false, None), | |
| 213 | }; | |
| 214 | let wire_col = if is_cycle { | |
| 215 | [0.96, 0.30, 0.34, 1.0] | |
| 216 | } else if let Some(role) = target_role { | |
| 217 | port_color(role) | |
| 218 | } else { | |
| 219 | [0.98, 0.84, 0.30, 1.0] | |
| 220 | }; | |
| 221 | let pts = sample_preview_bezier(a, edge.normal(), end, 16); | |
| 222 | for seg in pts.windows(2) { | |
| 223 | out.push(Primitive::Segment { | |
| 224 | from: seg[0], | |
| 225 | to: seg[1], | |
| 226 | width: if is_cycle { 2.4 } else { 2.0 }, | |
| 227 | color: wire_col, | |
| 228 | }); | |
| 229 | } | |
| 230 | if is_cycle { | |
| 231 | if let Some(badge) = self.badge_cache.get("cycle!") { | |
| 232 | let bx = end[0] + 8.0; | |
| 233 | let by = end[1] - 14.0; | |
| 234 | out.push(Primitive::RoundedRect { | |
| 235 | rect: [bx, by, bx + 36.0, by + 13.0], | |
| 236 | radius: 3.0, | |
| 237 | stroke: None, | |
| 238 | color: [0.30, 0.06, 0.08, 0.94], | |
| 239 | }); | |
| 240 | out.push(Primitive::RoundedRect { | |
| 241 | rect: [bx, by, bx + 36.0, by + 13.0], | |
| 242 | radius: 3.0, | |
| 243 | stroke: Some(1.0), | |
| 244 | color: [0.96, 0.30, 0.34, 0.95], | |
| 245 | }); | |
| 246 | out.push(Primitive::Text { | |
| 247 | text: badge as &dyn Glyphs, | |
| 248 | origin: [bx + 5.0, by + 2.0], | |
| 249 | clip: Some([bx, by, bx + 36.0, by + 13.0]), | |
| 250 | ink: [1.0, 0.65, 0.65, 1.0], | |
| 251 | }); | |
| 252 | } | |
| 253 | } | |
| 254 | } | |
| 255 | } | |
| 256 | ||
| 257 | // 4. Sleek Thin Fusion-Style Node Bars (color line along BOTTOM!) | |
| 258 | for n in comp.nodes.iter().filter(|n| self.scope.shows(n)) { | |
| 259 | let r = self.node_rect_local(n.pos); | |
| 260 | if r[2] < -20.0 || r[0] > w + 20.0 || r[3] < -20.0 || r[1] > h + 20.0 { | |
| 261 | continue; | |
| 262 | } | |
| 263 | let sel = self.selected.contains(&n.id); | |
| 264 | let primary = self.primary == Some(n.id); | |
| 265 | let hovered = self.hovered_node == Some(n.id); | |
| 266 | let v1 = self.viewed_nodes[0] == Some(n.id); | |
| 267 | let v2 = self.viewed_nodes[1] == Some(n.id); | |
| 268 | let supported = is_supported_tool(&n.kind); | |
| 269 | let accent = if supported { | |
| 270 | node_accent(&n.kind) | |
| 271 | } else { | |
| 272 | [0.92, 0.64, 0.24, 1.0] | |
| 273 | }; | |
| 274 | ||
| 275 | let bg = if n.bypassed { | |
| 276 | [self.palette.node_fill[0] * 0.6, self.palette.node_fill[1] * 0.6, self.palette.node_fill[2] * 0.6, 0.65] | |
| 277 | } else if sel { | |
| 278 | self.palette.node_selected | |
| 279 | } else if hovered { | |
| 280 | self.palette.node_hover | |
| 281 | } else if !supported { | |
| 282 | [ | |
| 283 | (self.palette.node_fill[0] + 0.035).min(1.0), | |
| 284 | (self.palette.node_fill[1] + 0.020).min(1.0), | |
| 285 | self.palette.node_fill[2], | |
| 286 | 1.0, | |
| 287 | ] | |
| 288 | } else { | |
| 289 | self.palette.node_fill | |
| 290 | }; | |
| 291 | ||
| 292 | let radius = (3.5 * self.zoom).clamp(1.5, 5.0); | |
| 293 | out.push(Primitive::RoundedRect { rect: r, radius, stroke: None, color: bg }); | |
| 294 | ||
| 295 | // Bottom category color line (exact Fusion look — thin sleek strip on bottom edge!) | |
| 296 | let bar_h = (2.6 * self.zoom).clamp(1.4, 3.4); | |
| 297 | out.push(Primitive::Rect { | |
| 298 | rect: [r[0] + 1.5, (r[3] - bar_h).max(r[1]), r[2] - 1.5, r[3]], | |
| 299 | color: if n.bypassed { [0.28, 0.28, 0.32, 0.6] } else { accent }, | |
| 300 | }); | |
| 301 | ||
| 302 | // Crisp border (amber accent for preserved/passthrough unknown nodes) | |
| 303 | let border_col = if primary { | |
| 304 | [0.98, 0.78, 0.30, 1.0] | |
| 305 | } else if sel { | |
| 306 | self.palette.accent | |
| 307 | } else if !supported { | |
| 308 | [0.92, 0.64, 0.24, 0.85] | |
| 309 | } else { | |
| 310 | self.palette.node_border | |
| 311 | }; | |
| 312 | out.push(Primitive::RoundedRect { | |
| 313 | rect: r, | |
| 314 | radius, | |
| 315 | stroke: Some(if sel || primary { 1.4 } else if !supported { 1.15 } else { 0.9 }), | |
| 316 | color: border_col, | |
| 317 | }); | |
| 318 | // Subtle top dashed ticks for preserved/passthrough unknown nodes | |
| 319 | if !supported && self.zoom >= 0.45 { | |
| 320 | let mut tx = r[0] + 5.0; | |
| 321 | while tx + 4.0 < r[2] - 4.0 { | |
| 322 | out.push(Primitive::Segment { | |
| 323 | from: [tx, r[1] + 0.8], | |
| 324 | to: [(tx + 4.0).min(r[2] - 4.0), r[1] + 0.8], | |
| 325 | width: 1.2, | |
| 326 | color: [0.96, 0.72, 0.30, 0.90], | |
| 327 | }); | |
| 328 | tx += 8.0; | |
| 329 | } | |
| 330 | } | |
| 331 | ||
| 332 | // Node label centered cleanly inside thin bar | |
| 333 | if self.zoom >= 0.42 { | |
| 334 | if let Some(layout) = self.label_cache.get(&n.name) { | |
| 335 | let ink = if n.bypassed { self.palette.text_dim } else { self.palette.text }; | |
| 336 | let pad_x = (6.0 * self.zoom).clamp(3.0, 8.0); | |
| 337 | let text_y = (r[1] + r[3] - bar_h) * 0.5 - 6.0; | |
| 338 | out.push(Primitive::Text { | |
| 339 | text: layout as &dyn Glyphs, | |
| 340 | origin: [r[0] + pad_x, text_y], | |
| 341 | clip: Some([r[0] + pad_x, r[1] + 1.0, r[2] - 4.0, r[3] - bar_h]), | |
| 342 | ink, | |
| 343 | }); | |
| 344 | } | |
| 345 | } | |
| 346 | ||
| 347 | // Preserved/passthrough pill badge underneath bottom-right for unknown/OFX nodes | |
| 348 | if !supported && self.zoom >= 0.50 { | |
| 349 | if let Some(badge) = self.badge_cache.get("preserved") { | |
| 350 | let bw = 46.0; | |
| 351 | let bh = 10.5; | |
| 352 | let bx = (r[2] - bw).max(r[0] + 28.0); | |
| 353 | let by = r[3] + 1.5; | |
| 354 | out.push(Primitive::RoundedRect { | |
| 355 | rect: [bx, by, bx + bw, by + bh], | |
| 356 | radius: 3.0, | |
| 357 | stroke: None, | |
| 358 | color: [0.14, 0.10, 0.05, 0.92], | |
| 359 | }); | |
| 360 | out.push(Primitive::RoundedRect { | |
| 361 | rect: [bx, by, bx + bw, by + bh], | |
| 362 | radius: 3.0, | |
| 363 | stroke: Some(0.8), | |
| 364 | color: [0.92, 0.64, 0.24, 0.72], | |
| 365 | }); | |
| 366 | out.push(Primitive::Text { | |
| 367 | text: badge as &dyn Glyphs, | |
| 368 | origin: [bx + 4.0, by + 1.0], | |
| 369 | clip: Some([bx + 2.0, by, bx + bw - 2.0, by + bh]), | |
| 370 | ink: [0.98, 0.78, 0.40, 1.0], | |
| 371 | }); | |
| 372 | } | |
| 373 | } | |
| 374 | ||
| 375 | // Fusion-style tiny viewer indicator pill underneath bottom-left (`● ○` for Viewer 1 / Viewer 2!) | |
| 376 | if v1 || v2 { | |
| 377 | let pw = 22.0; | |
| 378 | let ph = 9.0; | |
| 379 | let px = r[0] + 4.0; | |
| 380 | let py = r[3] + 1.5; | |
| 381 | out.push(Primitive::RoundedRect { | |
| 382 | rect: [px, py, px + pw, py + ph], | |
| 383 | radius: 3.0, | |
| 384 | stroke: None, | |
| 385 | color: [0.02, 0.02, 0.03, 0.92], | |
| 386 | }); | |
| 387 | let d1 = if v1 { [0.24, 0.90, 0.82, 1.0] } else { [0.26, 0.28, 0.34, 0.6] }; | |
| 388 | let d2 = if v2 { [0.98, 0.68, 0.28, 1.0] } else { [0.26, 0.28, 0.34, 0.6] }; | |
| 389 | out.push(Primitive::RoundedRect { | |
| 390 | rect: [px + 3.5, py + 2.0, px + 8.5, py + 7.0], | |
| 391 | radius: 2.5, | |
| 392 | stroke: None, | |
| 393 | color: d1, | |
| 394 | }); | |
| 395 | out.push(Primitive::RoundedRect { | |
| 396 | rect: [px + 13.0, py + 2.0, px + 18.0, py + 7.0], | |
| 397 | radius: 2.5, | |
| 398 | stroke: None, | |
| 399 | color: d2, | |
| 400 | }); | |
| 401 | } | |
| 402 | ||
| 403 | if n.bypassed { | |
| 404 | let ym = (r[1] + r[3]) * 0.5; | |
| 405 | out.push(Primitive::Segment { | |
| 406 | from: [r[0] + 3.0, ym], | |
| 407 | to: [r[2] - 3.0, ym], | |
| 408 | width: 1.4, | |
| 409 | color: [0.95, 0.35, 0.35, 0.85], | |
| 410 | }); | |
| 411 | } | |
| 412 | } | |
| 413 | ||
| 414 | // 5. Box select marquee | |
| 415 | if let Some(bs) = &self.box_select { | |
| 416 | let x0 = bs.start_local[0].min(bs.cur_local[0]); | |
| 417 | let x1 = bs.start_local[0].max(bs.cur_local[0]); | |
| 418 | let y0 = bs.start_local[1].min(bs.cur_local[1]); | |
| 419 | let y1 = bs.start_local[1].max(bs.cur_local[1]); | |
| 420 | out.push(Primitive::RoundedRect { | |
| 421 | rect: [x0, y0, x1, y1], | |
| 422 | radius: 2.0, | |
| 423 | stroke: None, | |
| 424 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 425 | }); | |
| 426 | out.push(Primitive::RoundedRect { | |
| 427 | rect: [x0, y0, x1, y1], | |
| 428 | radius: 2.0, | |
| 429 | stroke: Some(1.2), | |
| 430 | color: self.palette.accent, | |
| 431 | }); | |
| 432 | } | |
| 433 | ||
| 434 | // 6. Status HUD strip | |
| 435 | let hud_key = match self.modal.as_ref().map(|m| m.mode) { | |
| 436 | Some(TransformMode::Grab) => "GRAB [LMB/Enter confirm, RMB/Esc cancel, X/Y axis, Shift: Splice]", | |
| 437 | Some(TransformMode::Rotate) => "ROTATE [LMB/Enter confirm, RMB/Esc cancel]", | |
| 438 | Some(TransformMode::Scale) => "SCALE [LMB/Enter confirm, RMB/Esc cancel, X/Y axis]", | |
| 439 | None if !self.scope.is_root() => HUD_GROUP, | |
| 440 | None => HUD_ROOT, | |
| 441 | }; | |
| 442 | if let Some(l) = self.badge_cache.get(hud_key) { | |
| 443 | let pill_h = 20.0; | |
| 444 | out.push(Primitive::RoundedRect { | |
| 445 | rect: [8.0, h - pill_h - 6.0, w - 8.0, h - 6.0], | |
| 446 | radius: 4.0, | |
| 447 | stroke: None, | |
| 448 | color: [self.palette.bg[0] * 0.75, self.palette.bg[1] * 0.75, self.palette.bg[2] * 0.75, 0.86], | |
| 449 | }); | |
| 450 | out.push(Primitive::Text { | |
| 451 | text: l as &dyn Glyphs, | |
| 452 | origin: [14.0, h - pill_h - 2.5], | |
| 453 | clip: Some([12.0, h - pill_h - 6.0, w - 12.0, h - 6.0]), | |
| 454 | ink: if self.modal.is_some() { [0.98, 0.82, 0.30, 1.0] } else { self.palette.text_dim }, | |
| 455 | }); | |
| 456 | } | |
| 457 | ||
| 458 | out | |
| 459 | } | |
| 460 | } | |
| 461 | ||
| 462 | /// Static SVG triangles pointing INTO the node edge (`origin` sits flush on the node border). | |
| 463 | fn input_arrow_svg(edge: Edge, large: bool) -> &'static str { | |
| 464 | match (edge, large) { | |
| 465 | (Edge::Left, true) => "M 0 0 L -6.5 -3.6 L -6.5 3.6 Z", | |
| 466 | (Edge::Left, false) => "M 0 0 L -4.5 -2.6 L -4.5 2.6 Z", | |
| 467 | (Edge::Right, true) => "M 0 0 L 6.5 -3.6 L 6.5 3.6 Z", | |
| 468 | (Edge::Right, false) => "M 0 0 L 4.5 -2.6 L 4.5 2.6 Z", | |
| 469 | (Edge::Top, true) => "M 0 0 L -3.6 -6.5 L 3.6 -6.5 Z", | |
| 470 | (Edge::Top, false) => "M 0 0 L -2.6 -4.5 L 2.6 -4.5 Z", | |
| 471 | (Edge::Bottom, true) => "M 0 0 L -3.6 6.5 L 3.6 6.5 Z", | |
| 472 | (Edge::Bottom, false) => "M 0 0 L -2.6 4.5 L 2.6 4.5 Z", | |
| 473 | } | |
| 474 | } | |
| 475 | ||
| 476 | fn port_color(role: PortRole) -> [f32; 4] { | |
| 477 | match role { | |
| 478 | PortRole::Background => [0.94, 0.76, 0.26, 1.0], // Fusion gold | |
| 479 | PortRole::Foreground => [0.34, 0.86, 0.46, 1.0], // Fusion green | |
| 480 | PortRole::Mask => [0.32, 0.64, 0.98, 1.0], // Fusion blue | |
| 481 | PortRole::Scene3D => [0.30, 0.86, 0.90, 1.0], // Cyan | |
| 482 | PortRole::Aux => [0.74, 0.58, 0.90, 1.0], // Violet | |
| 483 | } | |
| 484 | } | |
| 485 | ||
| 486 | fn node_accent(kind: &str) -> [f32; 4] { | |
| 487 | match kind { | |
| 488 | "Loader" | "Saver" => [0.32, 0.72, 0.92, 1.0], | |
| 489 | "Merge" | "Switch" | "Fuse.Switch" => [0.58, 0.60, 0.66, 1.0], | |
| 490 | "Background" | "FastNoise" => [0.36, 0.80, 0.42, 1.0], | |
| 491 | "TextPlus" | "Text3D" => [0.34, 0.86, 0.54, 1.0], | |
| 492 | "ColorCorrector" | "BrightnessContrast" | "ColorCurves" | "HueCurves" | "LumaKeyer" | "UltraKeyer" => { | |
| 493 | [0.92, 0.58, 0.22, 1.0] | |
| 494 | } | |
| 495 | "Blur" | "DirectionalBlur" | "Glow" | "SoftGlow" | "Displace" | "ErodeDilate" => { | |
| 496 | [0.92, 0.52, 0.24, 1.0] | |
| 497 | } | |
| 498 | "Transform" | "Scale" | "Crop" | "TimeStretcher" | "TimeSpeed" => [0.84, 0.66, 0.28, 1.0], | |
| 499 | "EllipseMask" | "RectangleMask" | "PolylineMask" | "BitmapMask" => [0.32, 0.62, 0.98, 1.0], | |
| 500 | k if k.starts_with("Fuse.") => [0.88, 0.44, 0.76, 1.0], | |
| 501 | _ => [0.54, 0.54, 0.60, 1.0], | |
| 502 | } | |
| 503 | } |
crates/viewer/src/clock.rs created+440| ... | ... | @@ -0,0 +1,440 @@ |
| 1 | use model::Comp; | |
| 2 | use std::time::{Duration, Instant}; | |
| 3 | ||
| 4 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 5 | pub enum SyncMode { | |
| 6 | /// Wall/audio clock master: skips frames to hold real-time FPS. | |
| 7 | RealtimeDrop, | |
| 8 | /// Steps exactly +1 integer frame, throttled so it never runs faster than `comp.fps`. | |
| 9 | EveryFrame, | |
| 10 | } | |
| 11 | ||
| 12 | /// Outcome of advancing [`PlaybackClock::tick_synced`]. | |
| 13 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 14 | pub struct TickResult { | |
| 15 | pub playing: bool, | |
| 16 | pub frame_changed: bool, | |
| 17 | pub looped: bool, | |
| 18 | } | |
| 19 | ||
| 20 | /// Shared timeline & playback clock shared across all open viewers (Viewer 1, Viewer 2, Spline). | |
| 21 | pub struct PlaybackClock { | |
| 22 | pub current_frame: f64, | |
| 23 | pub playing: bool, | |
| 24 | pub sync_mode: SyncMode, | |
| 25 | /// Optional user-defined preview/export `[in, out]` frame markers (inclusive). | |
| 26 | pub in_out_range: [Option<i64>; 2], | |
| 27 | anchor_time: Instant, | |
| 28 | anchor_frame: f64, | |
| 29 | last_every_step: Instant, | |
| 30 | } | |
| 31 | ||
| 32 | impl Default for PlaybackClock { | |
| 33 | fn default() -> Self { | |
| 34 | Self::new() | |
| 35 | } | |
| 36 | } | |
| 37 | ||
| 38 | impl PlaybackClock { | |
| 39 | pub fn new() -> Self { | |
| 40 | let now = Instant::now(); | |
| 41 | Self { | |
| 42 | current_frame: 0.0, | |
| 43 | playing: false, | |
| 44 | sync_mode: SyncMode::RealtimeDrop, | |
| 45 | in_out_range: [None, None], | |
| 46 | anchor_time: now, | |
| 47 | anchor_frame: 0.0, | |
| 48 | last_every_step: now, | |
| 49 | } | |
| 50 | } | |
| 51 | ||
| 52 | pub fn init_from_comp(&mut self, comp: &Comp) { | |
| 53 | let cur = comp.current_time.round(); | |
| 54 | let r0 = comp.render_range[0] as f64; | |
| 55 | let r1 = comp.render_range[1] as f64; | |
| 56 | self.current_frame = if cur >= r0 && cur <= r1 { cur } else { r0 }; | |
| 57 | self.in_out_range = [None, None]; | |
| 58 | self.anchor_frame = self.current_frame; | |
| 59 | self.anchor_time = Instant::now(); | |
| 60 | self.last_every_step = self.anchor_time; | |
| 61 | } | |
| 62 | ||
| 63 | /// Returns the active inclusive preview/export range `[in.unwrap_or(r0), out.unwrap_or(r1)]`, | |
| 64 | /// clamped to `comp.render_range` and sorted ascending. | |
| 65 | pub fn effective_preview_range(&self, comp: &Comp) -> [i64; 2] { | |
| 66 | let r0 = comp.render_range[0]; | |
| 67 | let r1 = comp.render_range[1].max(r0); | |
| 68 | let a = self.in_out_range[0].unwrap_or(r0).clamp(r0, r1); | |
| 69 | let b = self.in_out_range[1].unwrap_or(r1).clamp(r0, r1); | |
| 70 | if a <= b { [a, b] } else { [b, a] } | |
| 71 | } | |
| 72 | ||
| 73 | pub fn set_in_point(&mut self, comp: &Comp) { | |
| 74 | let r0 = comp.render_range[0]; | |
| 75 | let r1 = comp.render_range[1].max(r0); | |
| 76 | let f = (self.current_frame.round() as i64).clamp(r0, r1); | |
| 77 | self.in_out_range[0] = Some(f); | |
| 78 | if self.playing { | |
| 79 | let [p0, p1] = self.effective_preview_range(comp); | |
| 80 | if self.current_frame < p0 as f64 || self.current_frame > p1 as f64 { | |
| 81 | self.current_frame = p0 as f64; | |
| 82 | } | |
| 83 | let now = Instant::now(); | |
| 84 | self.anchor_time = now; | |
| 85 | self.anchor_frame = self.current_frame; | |
| 86 | self.last_every_step = now; | |
| 87 | } | |
| 88 | } | |
| 89 | ||
| 90 | pub fn set_out_point(&mut self, comp: &Comp) { | |
| 91 | let r0 = comp.render_range[0]; | |
| 92 | let r1 = comp.render_range[1].max(r0); | |
| 93 | let f = (self.current_frame.round() as i64).clamp(r0, r1); | |
| 94 | self.in_out_range[1] = Some(f); | |
| 95 | if self.playing { | |
| 96 | let [p0, p1] = self.effective_preview_range(comp); | |
| 97 | if self.current_frame < p0 as f64 || self.current_frame > p1 as f64 { | |
| 98 | self.current_frame = p0 as f64; | |
| 99 | } | |
| 100 | let now = Instant::now(); | |
| 101 | self.anchor_time = now; | |
| 102 | self.anchor_frame = self.current_frame; | |
| 103 | self.last_every_step = now; | |
| 104 | } | |
| 105 | } | |
| 106 | ||
| 107 | pub fn clear_in_out(&mut self) { | |
| 108 | self.in_out_range = [None, None]; | |
| 109 | if self.playing { | |
| 110 | let now = Instant::now(); | |
| 111 | self.anchor_time = now; | |
| 112 | self.anchor_frame = self.current_frame; | |
| 113 | self.last_every_step = now; | |
| 114 | } | |
| 115 | } | |
| 116 | ||
| 117 | /// Exact audio track timestamp (in seconds) for `frame` under `comp.fps` and `comp.audio_offset`. | |
| 118 | pub fn audio_time_for_frame(comp: &Comp, frame: f64) -> f64 { | |
| 119 | (frame - comp.audio_offset) / comp.fps.max(1.0) | |
| 120 | } | |
| 121 | ||
| 122 | /// Exact audio track timestamp (in seconds) for the current playhead frame. | |
| 123 | pub fn audio_target_secs(&self, comp: &Comp) -> f64 { | |
| 124 | Self::audio_time_for_frame(comp, self.current_frame) | |
| 125 | } | |
| 126 | ||
| 127 | /// Expected live audio playback timestamp (in seconds) at wall-clock `now`. | |
| 128 | /// | |
| 129 | /// In `RealtimeDrop`, tracks continuous wall-clock frame progress across the preview range. | |
| 130 | /// In `EveryFrame`, bounds sub-frame progress to `[0, 1/fps]` so slow frame renders do not | |
| 131 | /// let the audio stream drift ahead of the displayed frame. | |
| 132 | pub fn expected_audio_secs(&self, comp: &Comp, now: Instant) -> f64 { | |
| 133 | let fps = comp.fps.max(1.0); | |
| 134 | if !self.playing { | |
| 135 | return Self::audio_time_for_frame(comp, self.current_frame); | |
| 136 | } | |
| 137 | match self.sync_mode { | |
| 138 | SyncMode::RealtimeDrop => { | |
| 139 | let [p0, p1] = self.effective_preview_range(comp); | |
| 140 | let r0 = p0 as f64; | |
| 141 | let r1 = p1.max(p0) as f64; | |
| 142 | let span = (r1 - r0 + 1.0).max(1.0); | |
| 143 | if span <= 1.0 { | |
| 144 | return Self::audio_time_for_frame(comp, r0); | |
| 145 | } | |
| 146 | let base = if self.anchor_frame < r0 || self.anchor_frame > r1 { | |
| 147 | r0 | |
| 148 | } else { | |
| 149 | self.anchor_frame | |
| 150 | }; | |
| 151 | let elapsed = now.saturating_duration_since(self.anchor_time).as_secs_f64(); | |
| 152 | let continuous = r0 + (base - r0 + elapsed * fps).rem_euclid(span); | |
| 153 | Self::audio_time_for_frame(comp, continuous) | |
| 154 | } | |
| 155 | SyncMode::EveryFrame => { | |
| 156 | let frame_dur = 1.0 / fps; | |
| 157 | let sub = now | |
| 158 | .saturating_duration_since(self.last_every_step) | |
| 159 | .as_secs_f64() | |
| 160 | .clamp(0.0, frame_dur); | |
| 161 | Self::audio_time_for_frame(comp, self.current_frame) + sub | |
| 162 | } | |
| 163 | } | |
| 164 | } | |
| 165 | ||
| 166 | pub fn set_frame(&mut self, comp: &Comp, frame: f64, now: Instant) -> bool { | |
| 167 | let clamped = frame | |
| 168 | .round() | |
| 169 | .clamp(comp.global_range[0] as f64, comp.global_range[1] as f64); | |
| 170 | let changed = (clamped - self.current_frame).abs() > 1e-6; | |
| 171 | self.current_frame = clamped; | |
| 172 | if self.playing { | |
| 173 | self.anchor_time = now; | |
| 174 | self.anchor_frame = self.current_frame; | |
| 175 | self.last_every_step = now; | |
| 176 | } | |
| 177 | changed | |
| 178 | } | |
| 179 | ||
| 180 | pub fn toggle_play(&mut self, now: Instant) { | |
| 181 | self.playing = !self.playing; | |
| 182 | self.anchor_time = now; | |
| 183 | self.anchor_frame = self.current_frame; | |
| 184 | self.last_every_step = now; | |
| 185 | } | |
| 186 | ||
| 187 | /// Toggles playback and, when starting, snaps playhead into `effective_preview_range(comp)` | |
| 188 | /// if it was outside so video and audio start locked on the same frame. | |
| 189 | pub fn toggle_play_with_comp(&mut self, comp: &Comp, now: Instant) { | |
| 190 | self.playing = !self.playing; | |
| 191 | if self.playing { | |
| 192 | let [p0, p1] = self.effective_preview_range(comp); | |
| 193 | let r0 = p0 as f64; | |
| 194 | let r1 = p1.max(p0) as f64; | |
| 195 | if self.current_frame < r0 || self.current_frame > r1 { | |
| 196 | self.current_frame = r0; | |
| 197 | } | |
| 198 | } | |
| 199 | self.anchor_time = now; | |
| 200 | self.anchor_frame = self.current_frame; | |
| 201 | self.last_every_step = now; | |
| 202 | } | |
| 203 | ||
| 204 | /// Advances the playback clock; returns `(still_playing, frame_changed)`. | |
| 205 | /// In `EveryFrame` mode, throttles to `<= comp.fps` so it never runs faster than realtime! | |
| 206 | pub fn tick(&mut self, comp: &Comp, now: Instant) -> (bool, bool) { | |
| 207 | let res = self.tick_synced(comp, now); | |
| 208 | (res.playing, res.frame_changed) | |
| 209 | } | |
| 210 | ||
| 211 | /// Advances the playback clock and reports frame change plus exact loop wraparound. | |
| 212 | pub fn tick_synced(&mut self, comp: &Comp, now: Instant) -> TickResult { | |
| 213 | if !self.playing { | |
| 214 | return TickResult { | |
| 215 | playing: false, | |
| 216 | frame_changed: false, | |
| 217 | looped: false, | |
| 218 | }; | |
| 219 | } | |
| 220 | let [p0, p1] = self.effective_preview_range(comp); | |
| 221 | let r0 = p0 as f64; | |
| 222 | let r1 = p1.max(p0) as f64; | |
| 223 | let span = (r1 - r0 + 1.0).max(1.0); | |
| 224 | let fps = comp.fps.max(1.0); | |
| 225 | let frame_dur = Duration::from_secs_f64(1.0 / fps); | |
| 226 | ||
| 227 | let mut changed = false; | |
| 228 | let mut looped = false; | |
| 229 | match self.sync_mode { | |
| 230 | SyncMode::RealtimeDrop => { | |
| 231 | if span <= 1.0 { | |
| 232 | if (self.current_frame - r0).abs() >= 1.0 { | |
| 233 | self.current_frame = r0; | |
| 234 | changed = true; | |
| 235 | } | |
| 236 | self.anchor_frame = r0; | |
| 237 | self.anchor_time = now; | |
| 238 | } else { | |
| 239 | if self.anchor_frame < r0 || self.anchor_frame > r1 { | |
| 240 | self.anchor_frame = r0; | |
| 241 | self.anchor_time = now; | |
| 242 | if (self.current_frame - r0).abs() >= 1.0 { | |
| 243 | self.current_frame = r0; | |
| 244 | changed = true; | |
| 245 | looped = true; | |
| 246 | } | |
| 247 | } | |
| 248 | let elapsed = now.saturating_duration_since(self.anchor_time).as_secs_f64(); | |
| 249 | let adv = (elapsed * fps).floor(); | |
| 250 | let raw = (self.anchor_frame - r0) + adv; | |
| 251 | let wrapped = raw >= span; | |
| 252 | let next = r0 + raw.rem_euclid(span); | |
| 253 | if wrapped { | |
| 254 | // Keep the sub-frame overshoot so looping never drifts behind the wall clock. | |
| 255 | let overshoot = Duration::from_secs_f64((elapsed * fps - adv) / fps); | |
| 256 | self.anchor_frame = next; | |
| 257 | self.anchor_time = now.checked_sub(overshoot).unwrap_or(now); | |
| 258 | looped = true; | |
| 259 | } | |
| 260 | if (next - self.current_frame).abs() >= 1.0 { | |
| 261 | self.current_frame = next; | |
| 262 | changed = true; | |
| 263 | } | |
| 264 | } | |
| 265 | } | |
| 266 | SyncMode::EveryFrame => { | |
| 267 | let elapsed = now.saturating_duration_since(self.last_every_step); | |
| 268 | if elapsed >= frame_dur { | |
| 269 | let out_or_end = self.current_frame + 1.0 > r1 || self.current_frame < r0; | |
| 270 | let next = if out_or_end { | |
| 271 | if span > 1.0 { | |
| 272 | looped = true; | |
| 273 | } | |
| 274 | r0 | |
| 275 | } else { | |
| 276 | self.current_frame + 1.0 | |
| 277 | }; | |
| 278 | if (next - self.current_frame).abs() >= 1.0 { | |
| 279 | changed = true; | |
| 280 | } | |
| 281 | self.current_frame = next; | |
| 282 | // Preserve exact cadence under minor timer jitter while avoiding catch-up bursts after stalls. | |
| 283 | if elapsed < frame_dur.mul_f64(2.0) { | |
| 284 | self.last_every_step += frame_dur; | |
| 285 | } else { | |
| 286 | self.last_every_step = now; | |
| 287 | } | |
| 288 | } | |
| 289 | } | |
| 290 | } | |
| 291 | TickResult { | |
| 292 | playing: true, | |
| 293 | frame_changed: changed, | |
| 294 | looped, | |
| 295 | } | |
| 296 | } | |
| 297 | ||
| 298 | pub fn next_wake(&self, comp: &Comp) -> Duration { | |
| 299 | let fps = comp.fps.max(1.0); | |
| 300 | Duration::from_secs_f64((0.5 / fps).min(0.016)) | |
| 301 | } | |
| 302 | } | |
| 303 | ||
| 304 | #[cfg(test)] | |
| 305 | mod tests { | |
| 306 | use super::*; | |
| 307 | use crate::tests::test_comp; | |
| 308 | ||
| 309 | #[test] | |
| 310 | fn effective_preview_range_sorts_and_clamps() { | |
| 311 | let comp = test_comp(); | |
| 312 | let mut clock = PlaybackClock::new(); | |
| 313 | clock.init_from_comp(&comp); | |
| 314 | assert_eq!(clock.effective_preview_range(&comp), [10, 50]); | |
| 315 | ||
| 316 | clock.in_out_range = [Some(20), None]; | |
| 317 | assert_eq!(clock.effective_preview_range(&comp), [20, 50]); | |
| 318 | ||
| 319 | clock.in_out_range = [None, Some(30)]; | |
| 320 | assert_eq!(clock.effective_preview_range(&comp), [10, 30]); | |
| 321 | ||
| 322 | // Reversed and out-of-bounds points get clamped and sorted cleanly | |
| 323 | clock.in_out_range = [Some(99), Some(2)]; | |
| 324 | assert_eq!(clock.effective_preview_range(&comp), [10, 50]); | |
| 325 | ||
| 326 | clock.in_out_range = [Some(35), Some(18)]; | |
| 327 | assert_eq!(clock.effective_preview_range(&comp), [18, 35]); | |
| 328 | } | |
| 329 | ||
| 330 | #[test] | |
| 331 | fn playback_clock_in_out_wraparound_realtime_and_every_frame() { | |
| 332 | let comp = test_comp(); // fps = 30, render_range = [10, 50] | |
| 333 | let t0 = Instant::now(); | |
| 334 | ||
| 335 | // RealtimeDrop: starting outside [20, 24] snaps cleanly to 20, then wraps 24 -> 20 | |
| 336 | let mut rt = PlaybackClock::new(); | |
| 337 | rt.init_from_comp(&comp); | |
| 338 | rt.in_out_range = [Some(20), Some(24)]; | |
| 339 | rt.set_frame(&comp, 12.0, t0); // outside In/Out range | |
| 340 | rt.toggle_play_with_comp(&comp, t0); | |
| 341 | assert_eq!(rt.current_frame, 20.0); | |
| 342 | ||
| 343 | let step = Duration::from_secs_f64(1.0 / 30.0); | |
| 344 | // Advance 4 frames -> frame 24 (last frame in [20..=24]) | |
| 345 | let r4 = rt.tick_synced(&comp, t0 + step * 4 + Duration::from_millis(2)); | |
| 346 | assert!(r4.playing); | |
| 347 | assert!(r4.frame_changed); | |
| 348 | assert!(!r4.looped); | |
| 349 | assert_eq!(rt.current_frame, 24.0); | |
| 350 | ||
| 351 | // Advance 5th frame -> wraps cleanly back to 20 and reports looped = true | |
| 352 | let r5 = rt.tick_synced(&comp, t0 + step * 5 + Duration::from_millis(2)); | |
| 353 | assert!(r5.playing); | |
| 354 | assert!(r5.frame_changed); | |
| 355 | assert!(r5.looped); | |
| 356 | assert_eq!(rt.current_frame, 20.0); | |
| 357 | ||
| 358 | // Next frame after wrap advances from 20 -> 21 without double-looping | |
| 359 | let r6 = rt.tick_synced(&comp, t0 + step * 6 + Duration::from_millis(4)); | |
| 360 | assert!(r6.frame_changed); | |
| 361 | assert!(!r6.looped); | |
| 362 | assert_eq!(rt.current_frame, 21.0); | |
| 363 | ||
| 364 | // EveryFrame: steps +1 per >=1/fps, preserves cadence across minor jitter, wraps 22 -> 20 | |
| 365 | let mut ef = PlaybackClock::new(); | |
| 366 | ef.init_from_comp(&comp); | |
| 367 | ef.sync_mode = SyncMode::EveryFrame; | |
| 368 | ef.in_out_range = [Some(20), Some(22)]; | |
| 369 | ef.set_frame(&comp, 20.0, t0); | |
| 370 | ef.toggle_play_with_comp(&comp, t0); | |
| 371 | ||
| 372 | // Minor 4ms jitter on step 1 | |
| 373 | let e1 = ef.tick_synced(&comp, t0 + step + Duration::from_millis(4)); | |
| 374 | assert_eq!((e1.frame_changed, e1.looped, ef.current_frame), (true, false, 21.0)); | |
| 375 | // Step 2 fires at t0 + 2*step (doesn't lose the 4ms jitter!) | |
| 376 | let e2 = ef.tick_synced(&comp, t0 + step * 2 + Duration::from_millis(1)); | |
| 377 | assert_eq!((e2.frame_changed, e2.looped, ef.current_frame), (true, false, 22.0)); | |
| 378 | // Step 3 wraps 22 -> 20 with looped = true | |
| 379 | let e3 = ef.tick_synced(&comp, t0 + step * 3 + Duration::from_millis(1)); | |
| 380 | assert_eq!((e3.frame_changed, e3.looped, ef.current_frame), (true, true, 20.0)); | |
| 381 | } | |
| 382 | ||
| 383 | #[test] | |
| 384 | fn audio_sync_target_time_computation() { | |
| 385 | let mut comp = test_comp(); | |
| 386 | comp.fps = 24.0; | |
| 387 | comp.audio_offset = 12.0; | |
| 388 | ||
| 389 | let t0 = Instant::now(); | |
| 390 | let mut clock = PlaybackClock::new(); | |
| 391 | clock.init_from_comp(&comp); | |
| 392 | clock.in_out_range = [Some(12), Some(36)]; | |
| 393 | clock.set_frame(&comp, 0.0, t0); | |
| 394 | assert!((clock.audio_target_secs(&comp) - (-0.5)).abs() < 1e-9); | |
| 395 | ||
| 396 | clock.set_frame(&comp, 12.0, t0); | |
| 397 | assert!((clock.audio_target_secs(&comp) - 0.0).abs() < 1e-9); | |
| 398 | ||
| 399 | clock.set_frame(&comp, 36.0, t0); | |
| 400 | assert!((clock.audio_target_secs(&comp) - 1.0).abs() < 1e-9); | |
| 401 | ||
| 402 | // RealtimeDrop expected_audio_secs tracks continuous sub-frame progress & wraparound | |
| 403 | clock.set_frame(&comp, 24.0, t0); | |
| 404 | clock.toggle_play_with_comp(&comp, t0); | |
| 405 | let half_sec = Duration::from_millis(500); | |
| 406 | let exp_rt = clock.expected_audio_secs(&comp, t0 + half_sec); | |
| 407 | assert!((exp_rt - 1.0).abs() < 1e-6, "expected 1.0s at frame 36, got {exp_rt}"); | |
| 408 | ||
| 409 | // EveryFrame expected_audio_secs caps sub-frame progress at 1/fps when a frame stalls | |
| 410 | clock.sync_mode = SyncMode::EveryFrame; | |
| 411 | clock.set_frame(&comp, 24.0, t0); // expected base = (24 - 12) / 24 = 0.5s | |
| 412 | let exp_stalled = clock.expected_audio_secs(&comp, t0 + Duration::from_millis(400)); | |
| 413 | let max_expected = 0.5 + (1.0 / 24.0); | |
| 414 | assert!( | |
| 415 | (exp_stalled - max_expected).abs() < 1e-6, | |
| 416 | "EveryFrame stalled expected_audio_secs should cap at {max_expected}, got {exp_stalled}" | |
| 417 | ); | |
| 418 | } | |
| 419 | ||
| 420 | #[test] | |
| 421 | fn loop_wraparound_keeps_the_sub_frame_overshoot() { | |
| 422 | let comp = test_comp(); // 30 fps | |
| 423 | let t0 = Instant::now(); | |
| 424 | let step = Duration::from_secs_f64(1.0 / 30.0); | |
| 425 | let mut clock = PlaybackClock::new(); | |
| 426 | clock.init_from_comp(&comp); | |
| 427 | clock.in_out_range = [Some(20), Some(24)]; | |
| 428 | clock.set_frame(&comp, 20.0, t0); | |
| 429 | clock.toggle_play_with_comp(&comp, t0); | |
| 430 | ||
| 431 | // Wrap with half a frame of overshoot... | |
| 432 | let wrap = clock.tick_synced(&comp, t0 + step.mul_f64(5.5)); | |
| 433 | assert!(wrap.looped); | |
| 434 | assert_eq!(clock.current_frame, 20.0); | |
| 435 | // ...so 0.6 frames later a whole frame has elapsed on the original timeline. | |
| 436 | let next = clock.tick_synced(&comp, t0 + step.mul_f64(6.1)); | |
| 437 | assert!(!next.looped); | |
| 438 | assert_eq!(clock.current_frame, 21.0); | |
| 439 | } | |
| 440 | } |
crates/viewer/src/lib.rs+5-930| ... | ... | @@ -6,8 +6,11 @@ |
| 6 | 6 | //! - Proxy scaling (1:1, 1:2, 1:4) with identical framing & proportions |
| 7 | 7 | //! - Surgical cache-busting controls + live GPU telemetry + `Cmd+F` / Numpad `.` fit-to-view. |
| 8 | 8 | |
| 9 | mod clock; | |
| 9 | 10 | pub mod gizmos; |
| 11 | mod paint; | |
| 10 | 12 | |
| 13 | pub use clock::{PlaybackClock, SyncMode, TickResult}; | |
| 11 | 14 | pub use gizmos::{GizmoEdit, GizmoHandle, GizmoState}; |
| 12 | 15 | |
| 13 | 16 | use draw::{Glyphs, Primitive, RasterImage}; |
| ... | ... | @@ -15,310 +18,9 @@ use engine::{CacheMode, DisplaySettings, Engine, EvalStats, RamFrameCache, RamFr |
| 15 | 18 | use model::{Comp, NodeId}; |
| 16 | 19 | use std::collections::HashSet; |
| 17 | 20 | use std::sync::Arc; |
| 18 | use std::time::{Duration, Instant}; | |
| 19 | 21 | use winit::event::{ElementState, MouseButton}; |
| 20 | 22 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; |
| 21 | 23 | |
| 22 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 23 | pub enum SyncMode { | |
| 24 | /// Wall/audio clock master: skips frames to hold real-time FPS. | |
| 25 | RealtimeDrop, | |
| 26 | /// Steps exactly +1 integer frame, throttled so it never runs faster than `comp.fps`. | |
| 27 | EveryFrame, | |
| 28 | } | |
| 29 | ||
| 30 | /// Outcome of advancing [`PlaybackClock::tick_synced`]. | |
| 31 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 32 | pub struct TickResult { | |
| 33 | pub playing: bool, | |
| 34 | pub frame_changed: bool, | |
| 35 | pub looped: bool, | |
| 36 | } | |
| 37 | ||
| 38 | /// Shared timeline & playback clock shared across all open viewers (Viewer 1, Viewer 2, Spline). | |
| 39 | pub struct PlaybackClock { | |
| 40 | pub current_frame: f64, | |
| 41 | pub playing: bool, | |
| 42 | pub sync_mode: SyncMode, | |
| 43 | /// Optional user-defined preview/export `[in, out]` frame markers (inclusive). | |
| 44 | pub in_out_range: [Option<i64>; 2], | |
| 45 | anchor_time: Instant, | |
| 46 | anchor_frame: f64, | |
| 47 | last_every_step: Instant, | |
| 48 | } | |
| 49 | ||
| 50 | impl Default for PlaybackClock { | |
| 51 | fn default() -> Self { | |
| 52 | Self::new() | |
| 53 | } | |
| 54 | } | |
| 55 | ||
| 56 | impl PlaybackClock { | |
| 57 | pub fn new() -> Self { | |
| 58 | let now = Instant::now(); | |
| 59 | Self { | |
| 60 | current_frame: 0.0, | |
| 61 | playing: false, | |
| 62 | sync_mode: SyncMode::RealtimeDrop, | |
| 63 | in_out_range: [None, None], | |
| 64 | anchor_time: now, | |
| 65 | anchor_frame: 0.0, | |
| 66 | last_every_step: now, | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | pub fn init_from_comp(&mut self, comp: &Comp) { | |
| 71 | let cur = comp.current_time.round(); | |
| 72 | let r0 = comp.render_range[0] as f64; | |
| 73 | let r1 = comp.render_range[1] as f64; | |
| 74 | self.current_frame = if cur >= r0 && cur <= r1 { cur } else { r0 }; | |
| 75 | self.in_out_range = [None, None]; | |
| 76 | self.anchor_frame = self.current_frame; | |
| 77 | self.anchor_time = Instant::now(); | |
| 78 | self.last_every_step = self.anchor_time; | |
| 79 | } | |
| 80 | ||
| 81 | /// Returns the active inclusive preview/export range `[in.unwrap_or(r0), out.unwrap_or(r1)]`, | |
| 82 | /// clamped to `comp.render_range` and sorted ascending. | |
| 83 | pub fn effective_preview_range(&self, comp: &Comp) -> [i64; 2] { | |
| 84 | let r0 = comp.render_range[0]; | |
| 85 | let r1 = comp.render_range[1].max(r0); | |
| 86 | let a = self.in_out_range[0].unwrap_or(r0).clamp(r0, r1); | |
| 87 | let b = self.in_out_range[1].unwrap_or(r1).clamp(r0, r1); | |
| 88 | if a <= b { [a, b] } else { [b, a] } | |
| 89 | } | |
| 90 | ||
| 91 | pub fn set_in_point(&mut self, comp: &Comp) { | |
| 92 | let r0 = comp.render_range[0]; | |
| 93 | let r1 = comp.render_range[1].max(r0); | |
| 94 | let f = (self.current_frame.round() as i64).clamp(r0, r1); | |
| 95 | self.in_out_range[0] = Some(f); | |
| 96 | if self.playing { | |
| 97 | let [p0, p1] = self.effective_preview_range(comp); | |
| 98 | if self.current_frame < p0 as f64 || self.current_frame > p1 as f64 { | |
| 99 | self.current_frame = p0 as f64; | |
| 100 | } | |
| 101 | let now = Instant::now(); | |
| 102 | self.anchor_time = now; | |
| 103 | self.anchor_frame = self.current_frame; | |
| 104 | self.last_every_step = now; | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | pub fn set_out_point(&mut self, comp: &Comp) { | |
| 109 | let r0 = comp.render_range[0]; | |
| 110 | let r1 = comp.render_range[1].max(r0); | |
| 111 | let f = (self.current_frame.round() as i64).clamp(r0, r1); | |
| 112 | self.in_out_range[1] = Some(f); | |
| 113 | if self.playing { | |
| 114 | let [p0, p1] = self.effective_preview_range(comp); | |
| 115 | if self.current_frame < p0 as f64 || self.current_frame > p1 as f64 { | |
| 116 | self.current_frame = p0 as f64; | |
| 117 | } | |
| 118 | let now = Instant::now(); | |
| 119 | self.anchor_time = now; | |
| 120 | self.anchor_frame = self.current_frame; | |
| 121 | self.last_every_step = now; | |
| 122 | } | |
| 123 | } | |
| 124 | ||
| 125 | pub fn clear_in_out(&mut self) { | |
| 126 | self.in_out_range = [None, None]; | |
| 127 | if self.playing { | |
| 128 | let now = Instant::now(); | |
| 129 | self.anchor_time = now; | |
| 130 | self.anchor_frame = self.current_frame; | |
| 131 | self.last_every_step = now; | |
| 132 | } | |
| 133 | } | |
| 134 | ||
| 135 | /// Exact audio track timestamp (in seconds) for `frame` under `comp.fps` and `comp.audio_offset`. | |
| 136 | pub fn audio_time_for_frame(comp: &Comp, frame: f64) -> f64 { | |
| 137 | (frame - comp.audio_offset) / comp.fps.max(1.0) | |
| 138 | } | |
| 139 | ||
| 140 | /// Exact audio track timestamp (in seconds) for the current playhead frame. | |
| 141 | pub fn audio_target_secs(&self, comp: &Comp) -> f64 { | |
| 142 | Self::audio_time_for_frame(comp, self.current_frame) | |
| 143 | } | |
| 144 | ||
| 145 | /// Expected live audio playback timestamp (in seconds) at wall-clock `now`. | |
| 146 | /// | |
| 147 | /// In `RealtimeDrop`, tracks continuous wall-clock frame progress across the preview range. | |
| 148 | /// In `EveryFrame`, bounds sub-frame progress to `[0, 1/fps]` so slow frame renders do not | |
| 149 | /// let the audio stream drift ahead of the displayed frame. | |
| 150 | pub fn expected_audio_secs(&self, comp: &Comp, now: Instant) -> f64 { | |
| 151 | let fps = comp.fps.max(1.0); | |
| 152 | if !self.playing { | |
| 153 | return Self::audio_time_for_frame(comp, self.current_frame); | |
| 154 | } | |
| 155 | match self.sync_mode { | |
| 156 | SyncMode::RealtimeDrop => { | |
| 157 | let [p0, p1] = self.effective_preview_range(comp); | |
| 158 | let r0 = p0 as f64; | |
| 159 | let r1 = p1.max(p0) as f64; | |
| 160 | let span = (r1 - r0 + 1.0).max(1.0); | |
| 161 | if span <= 1.0 { | |
| 162 | return Self::audio_time_for_frame(comp, r0); | |
| 163 | } | |
| 164 | let base = if self.anchor_frame < r0 || self.anchor_frame > r1 { | |
| 165 | r0 | |
| 166 | } else { | |
| 167 | self.anchor_frame | |
| 168 | }; | |
| 169 | let elapsed = now.saturating_duration_since(self.anchor_time).as_secs_f64(); | |
| 170 | let continuous = r0 + (base - r0 + elapsed * fps).rem_euclid(span); | |
| 171 | Self::audio_time_for_frame(comp, continuous) | |
| 172 | } | |
| 173 | SyncMode::EveryFrame => { | |
| 174 | let frame_dur = 1.0 / fps; | |
| 175 | let sub = now | |
| 176 | .saturating_duration_since(self.last_every_step) | |
| 177 | .as_secs_f64() | |
| 178 | .clamp(0.0, frame_dur); | |
| 179 | Self::audio_time_for_frame(comp, self.current_frame) + sub | |
| 180 | } | |
| 181 | } | |
| 182 | } | |
| 183 | ||
| 184 | pub fn set_frame(&mut self, comp: &Comp, frame: f64, now: Instant) -> bool { | |
| 185 | let clamped = frame | |
| 186 | .round() | |
| 187 | .clamp(comp.global_range[0] as f64, comp.global_range[1] as f64); | |
| 188 | let changed = (clamped - self.current_frame).abs() > 1e-6; | |
| 189 | self.current_frame = clamped; | |
| 190 | if self.playing { | |
| 191 | self.anchor_time = now; | |
| 192 | self.anchor_frame = self.current_frame; | |
| 193 | self.last_every_step = now; | |
| 194 | } | |
| 195 | changed | |
| 196 | } | |
| 197 | ||
| 198 | pub fn toggle_play(&mut self, now: Instant) { | |
| 199 | self.playing = !self.playing; | |
| 200 | self.anchor_time = now; | |
| 201 | self.anchor_frame = self.current_frame; | |
| 202 | self.last_every_step = now; | |
| 203 | } | |
| 204 | ||
| 205 | /// Toggles playback and, when starting, snaps playhead into `effective_preview_range(comp)` | |
| 206 | /// if it was outside so video and audio start locked on the same frame. | |
| 207 | pub fn toggle_play_with_comp(&mut self, comp: &Comp, now: Instant) { | |
| 208 | self.playing = !self.playing; | |
| 209 | if self.playing { | |
| 210 | let [p0, p1] = self.effective_preview_range(comp); | |
| 211 | let r0 = p0 as f64; | |
| 212 | let r1 = p1.max(p0) as f64; | |
| 213 | if self.current_frame < r0 || self.current_frame > r1 { | |
| 214 | self.current_frame = r0; | |
| 215 | } | |
| 216 | } | |
| 217 | self.anchor_time = now; | |
| 218 | self.anchor_frame = self.current_frame; | |
| 219 | self.last_every_step = now; | |
| 220 | } | |
| 221 | ||
| 222 | /// Advances the playback clock; returns `(still_playing, frame_changed)`. | |
| 223 | /// In `EveryFrame` mode, throttles to `<= comp.fps` so it never runs faster than realtime! | |
| 224 | pub fn tick(&mut self, comp: &Comp, now: Instant) -> (bool, bool) { | |
| 225 | let res = self.tick_synced(comp, now); | |
| 226 | (res.playing, res.frame_changed) | |
| 227 | } | |
| 228 | ||
| 229 | /// Advances the playback clock and reports frame change plus exact loop wraparound. | |
| 230 | pub fn tick_synced(&mut self, comp: &Comp, now: Instant) -> TickResult { | |
| 231 | if !self.playing { | |
| 232 | return TickResult { | |
| 233 | playing: false, | |
| 234 | frame_changed: false, | |
| 235 | looped: false, | |
| 236 | }; | |
| 237 | } | |
| 238 | let [p0, p1] = self.effective_preview_range(comp); | |
| 239 | let r0 = p0 as f64; | |
| 240 | let r1 = p1.max(p0) as f64; | |
| 241 | let span = (r1 - r0 + 1.0).max(1.0); | |
| 242 | let fps = comp.fps.max(1.0); | |
| 243 | let frame_dur = Duration::from_secs_f64(1.0 / fps); | |
| 244 | ||
| 245 | let mut changed = false; | |
| 246 | let mut looped = false; | |
| 247 | match self.sync_mode { | |
| 248 | SyncMode::RealtimeDrop => { | |
| 249 | if span <= 1.0 { | |
| 250 | if (self.current_frame - r0).abs() >= 1.0 { | |
| 251 | self.current_frame = r0; | |
| 252 | changed = true; | |
| 253 | } | |
| 254 | self.anchor_frame = r0; | |
| 255 | self.anchor_time = now; | |
| 256 | } else { | |
| 257 | if self.anchor_frame < r0 || self.anchor_frame > r1 { | |
| 258 | self.anchor_frame = r0; | |
| 259 | self.anchor_time = now; | |
| 260 | if (self.current_frame - r0).abs() >= 1.0 { | |
| 261 | self.current_frame = r0; | |
| 262 | changed = true; | |
| 263 | looped = true; | |
| 264 | } | |
| 265 | } | |
| 266 | let elapsed = now.saturating_duration_since(self.anchor_time).as_secs_f64(); | |
| 267 | let adv = (elapsed * fps).floor(); | |
| 268 | let raw = (self.anchor_frame - r0) + adv; | |
| 269 | let wrapped = raw >= span; | |
| 270 | let next = r0 + raw.rem_euclid(span); | |
| 271 | if wrapped { | |
| 272 | // Keep the sub-frame overshoot so looping never drifts behind the wall clock. | |
| 273 | let overshoot = Duration::from_secs_f64((elapsed * fps - adv) / fps); | |
| 274 | self.anchor_frame = next; | |
| 275 | self.anchor_time = now.checked_sub(overshoot).unwrap_or(now); | |
| 276 | looped = true; | |
| 277 | } | |
| 278 | if (next - self.current_frame).abs() >= 1.0 { | |
| 279 | self.current_frame = next; | |
| 280 | changed = true; | |
| 281 | } | |
| 282 | } | |
| 283 | } | |
| 284 | SyncMode::EveryFrame => { | |
| 285 | let elapsed = now.saturating_duration_since(self.last_every_step); | |
| 286 | if elapsed >= frame_dur { | |
| 287 | let out_or_end = self.current_frame + 1.0 > r1 || self.current_frame < r0; | |
| 288 | let next = if out_or_end { | |
| 289 | if span > 1.0 { | |
| 290 | looped = true; | |
| 291 | } | |
| 292 | r0 | |
| 293 | } else { | |
| 294 | self.current_frame + 1.0 | |
| 295 | }; | |
| 296 | if (next - self.current_frame).abs() >= 1.0 { | |
| 297 | changed = true; | |
| 298 | } | |
| 299 | self.current_frame = next; | |
| 300 | // Preserve exact cadence under minor timer jitter while avoiding catch-up bursts after stalls. | |
| 301 | if elapsed < frame_dur.mul_f64(2.0) { | |
| 302 | self.last_every_step += frame_dur; | |
| 303 | } else { | |
| 304 | self.last_every_step = now; | |
| 305 | } | |
| 306 | } | |
| 307 | } | |
| 308 | } | |
| 309 | TickResult { | |
| 310 | playing: true, | |
| 311 | frame_changed: changed, | |
| 312 | looped, | |
| 313 | } | |
| 314 | } | |
| 315 | ||
| 316 | pub fn next_wake(&self, comp: &Comp) -> Duration { | |
| 317 | let fps = comp.fps.max(1.0); | |
| 318 | Duration::from_secs_f64((0.5 / fps).min(0.016)) | |
| 319 | } | |
| 320 | } | |
| 321 | ||
| 322 | 24 | pub enum ViewerAction { |
| 323 | 25 | TogglePlay, |
| 324 | 26 | SetFrame(f64), |
| ... | ... | @@ -648,230 +350,6 @@ impl ViewerState { |
| 648 | 350 | } |
| 649 | 351 | } |
| 650 | 352 | |
| 651 | pub fn prepare_hud(&mut self, comp: &Comp, engine: &Engine, clock: &PlaybackClock) { | |
| 652 | self.in_out_range = clock.in_out_range; | |
| 653 | let node_name = self | |
| 654 | .viewed_node | |
| 655 | .and_then(|id| comp.nodes.get(id.0)) | |
| 656 | .map_or("(empty)", |n| n.name.as_str()); | |
| 657 | let cache_lbl = match engine.cache.mode { | |
| 658 | CacheMode::Enabled => "CACHE", | |
| 659 | CacheMode::Bypass => "BYPASS", | |
| 660 | }; | |
| 661 | let sync_lbl = match clock.sync_mode { | |
| 662 | SyncMode::RealtimeDrop => "RT", | |
| 663 | SyncMode::EveryFrame => "EVERY", | |
| 664 | }; | |
| 665 | let hiq_lbl = if engine.hiq { "HiQ" } else { "FAST" }; | |
| 666 | let (_, _, _, eff_scale) = self.image_layout(comp); | |
| 667 | let zoom_pct = (eff_scale * 100.0).round() as i32; | |
| 668 | let s = &self.last_stats; | |
| 669 | let io_suffix = if clock.in_out_range[0].is_some() || clock.in_out_range[1].is_some() { | |
| 670 | let [a, b] = clock.effective_preview_range(comp); | |
| 671 | format!(" · [{a}..{b}]") | |
| 672 | } else { | |
| 673 | String::new() | |
| 674 | }; | |
| 675 | let line = format!( | |
| 676 | "[V{}] {} · f{}{} · {}% · {:.1}ms · ev:{} h:{} m:{} · {:.0}MB · 1:{} · {} · {} · {}", | |
| 677 | self.slot + 1, | |
| 678 | node_name, | |
| 679 | clock.current_frame.round() as i64, | |
| 680 | io_suffix, | |
| 681 | zoom_pct, | |
| 682 | s.eval_ms, | |
| 683 | s.nodes_evaluated, | |
| 684 | s.cache_hits, | |
| 685 | s.cache_misses, | |
| 686 | s.cached_vram_mb, | |
| 687 | engine.proxy_scale, | |
| 688 | hiq_lbl, | |
| 689 | cache_lbl, | |
| 690 | sync_lbl, | |
| 691 | ); | |
| 692 | if self.hud_layout.as_ref().is_none_or(|(prev, _)| prev != &line) { | |
| 693 | let font = text::Font { | |
| 694 | families: Arc::from([text::Family::Mono]), | |
| 695 | size: 10.5, | |
| 696 | weight: 500, | |
| 697 | italic: false, | |
| 698 | }; | |
| 699 | let styles = [text::Style::new(font)]; | |
| 700 | let layout = text::Paragraph { | |
| 701 | text: &line, | |
| 702 | styles: &styles, | |
| 703 | spans: &[], | |
| 704 | replacements: &[], | |
| 705 | width: None, | |
| 706 | indent: 0.0, | |
| 707 | line_height: 1.2, | |
| 708 | align: text::Align::Left, | |
| 709 | tab: 32.0, | |
| 710 | } | |
| 711 | .layout(); | |
| 712 | self.hud_layout = Some((line, layout)); | |
| 713 | } | |
| 714 | ||
| 715 | if self.menu_item_layouts.len() != CONTEXT_MENU_ITEMS.len() { | |
| 716 | let font = text::Font { | |
| 717 | families: Arc::from([text::Family::Sans]), | |
| 718 | size: 12.0, | |
| 719 | weight: 500, | |
| 720 | italic: false, | |
| 721 | }; | |
| 722 | let styles = [text::Style::new(font)]; | |
| 723 | self.menu_item_layouts = CONTEXT_MENU_ITEMS | |
| 724 | .iter() | |
| 725 | .map(|(_, label)| { | |
| 726 | text::Paragraph { | |
| 727 | text: label, | |
| 728 | styles: &styles, | |
| 729 | spans: &[], | |
| 730 | replacements: &[], | |
| 731 | width: None, | |
| 732 | indent: 0.0, | |
| 733 | line_height: 1.2, | |
| 734 | align: text::Align::Left, | |
| 735 | tab: 32.0, | |
| 736 | } | |
| 737 | .layout() | |
| 738 | }) | |
| 739 | .collect(); | |
| 740 | } | |
| 741 | } | |
| 742 | ||
| 743 | pub fn build_ui( | |
| 744 | &mut self, | |
| 745 | ui: &mut ui::Ui, | |
| 746 | pane_id: ui::Id, | |
| 747 | canvas_id: ui::Id, | |
| 748 | engine: &Engine, | |
| 749 | clock: &PlaybackClock, | |
| 750 | ) -> Vec<ViewerAction> { | |
| 751 | self.in_out_range = clock.in_out_range; | |
| 752 | let mut actions = Vec::new(); | |
| 753 | ui.open_as( | |
| 754 | pane_id, | |
| 755 | ui::Spec { | |
| 756 | size: [ui::fill(), ui::fill()], | |
| 757 | axis: ui::Axis::Y, | |
| 758 | fill: Some(self.background()), | |
| 759 | ..ui::Spec::default() | |
| 760 | }, | |
| 761 | ); | |
| 762 | ||
| 763 | ui.open( | |
| 764 | ("top_bar", self.slot), | |
| 765 | ui::Spec { | |
| 766 | size: [ui::fill(), ui::px(30.0)], | |
| 767 | axis: ui::Axis::X, | |
| 768 | pad: [6.0, 4.0], | |
| 769 | gap: 4.0, | |
| 770 | fill: Some(self.palette.bar), | |
| 771 | ..ui::Spec::default() | |
| 772 | }, | |
| 773 | ); | |
| 774 | ||
| 775 | if self.slot == 0 { | |
| 776 | let play_lbl = if clock.playing { "Pause" } else { "Play" }; | |
| 777 | if tool_btn(ui, ("play", self.slot), play_lbl, clock.playing).clicked { | |
| 778 | actions.push(ViewerAction::TogglePlay); | |
| 779 | } | |
| 780 | if tool_btn(ui, ("prev", self.slot), "<", false).clicked { | |
| 781 | actions.push(ViewerAction::SetFrame(clock.current_frame - 1.0)); | |
| 782 | } | |
| 783 | if tool_btn(ui, ("next", self.slot), ">", false).clicked { | |
| 784 | actions.push(ViewerAction::SetFrame(clock.current_frame + 1.0)); | |
| 785 | } | |
| 786 | let sync_lbl = match clock.sync_mode { | |
| 787 | SyncMode::RealtimeDrop => "RT", | |
| 788 | SyncMode::EveryFrame => "Every", | |
| 789 | }; | |
| 790 | if tool_btn(ui, ("sync", self.slot), sync_lbl, clock.sync_mode == SyncMode::EveryFrame).clicked { | |
| 791 | actions.push(ViewerAction::ToggleSyncMode); | |
| 792 | } | |
| 793 | for scale in [1u32, 2, 4] { | |
| 794 | let lbl = format!("1:{scale}"); | |
| 795 | if tool_btn(ui, ("proxy", self.slot, scale), &lbl, engine.proxy_scale == scale).clicked { | |
| 796 | actions.push(ViewerAction::SetProxy(scale)); | |
| 797 | } | |
| 798 | } | |
| 799 | if tool_btn(ui, ("hiq", self.slot), "HiQ", engine.hiq).clicked { | |
| 800 | actions.push(ViewerAction::ToggleHiQ); | |
| 801 | } | |
| 802 | } | |
| 803 | ||
| 804 | let ch_lbl = match self.display.channel { | |
| 805 | 1 => "R", | |
| 806 | 2 => "G", | |
| 807 | 3 => "B", | |
| 808 | 4 => "A", | |
| 809 | _ => "RGBA", | |
| 810 | }; | |
| 811 | if tool_btn(ui, ("chan", self.slot), ch_lbl, self.display.channel != 0).clicked { | |
| 812 | self.display.channel = (self.display.channel + 1) % 5; | |
| 813 | self.dirty = true; | |
| 814 | actions.push(ViewerAction::NeedRender); | |
| 815 | } | |
| 816 | let filt_lbl = if self.display.smooth_resize { "Smooth" } else { "Pixel" }; | |
| 817 | if tool_btn(ui, ("smooth", self.slot), filt_lbl, self.display.smooth_resize).clicked { | |
| 818 | self.display.smooth_resize = !self.display.smooth_resize; | |
| 819 | self.dirty = true; | |
| 820 | actions.push(ViewerAction::NeedRender); | |
| 821 | } | |
| 822 | if tool_btn(ui, ("fit", self.slot), "Fit", self.fit_mode).clicked { | |
| 823 | self.fit(); | |
| 824 | } | |
| 825 | if tool_btn(ui, ("gizmo", self.slot), "Gizmo", self.gizmos.enabled).clicked { | |
| 826 | self.gizmos.enabled = !self.gizmos.enabled; | |
| 827 | } | |
| 828 | ||
| 829 | if self.slot == 0 { | |
| 830 | let bypass_lbl = match engine.cache.mode { | |
| 831 | CacheMode::Enabled => "Cache", | |
| 832 | CacheMode::Bypass => "Bypass", | |
| 833 | }; | |
| 834 | if tool_btn(ui, ("cache_mode", self.slot), bypass_lbl, engine.cache.mode == CacheMode::Bypass).clicked { | |
| 835 | actions.push(ViewerAction::ToggleCacheBypass); | |
| 836 | } | |
| 837 | if tool_btn(ui, ("bust_frame", self.slot), "Bust F", false).clicked { | |
| 838 | let f = clock.current_frame.round() as i64; | |
| 839 | self.cached_frames.remove(&f); | |
| 840 | self.dirty = true; | |
| 841 | actions.push(ViewerAction::BustFrame(f)); | |
| 842 | } | |
| 843 | if tool_btn(ui, ("bust_sub", self.slot), "Node", false).clicked { | |
| 844 | self.cached_frames.clear(); | |
| 845 | self.dirty = true; | |
| 846 | actions.push(ViewerAction::BustSubtree); | |
| 847 | } | |
| 848 | if tool_btn(ui, ("bust_gpu", self.slot), "GPU", false).clicked { | |
| 849 | self.cached_frames.clear(); | |
| 850 | self.dirty = true; | |
| 851 | actions.push(ViewerAction::BustGpu); | |
| 852 | } | |
| 853 | if tool_btn(ui, ("bust_all", self.slot), "All", false).clicked { | |
| 854 | self.cached_frames.clear(); | |
| 855 | self.dirty = true; | |
| 856 | actions.push(ViewerAction::BustAll); | |
| 857 | } | |
| 858 | } | |
| 859 | ui.close(); | |
| 860 | ||
| 861 | ui.open_as( | |
| 862 | canvas_id, | |
| 863 | ui::Spec { | |
| 864 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 865 | size: [ui::fill(), ui::fill()], | |
| 866 | ..ui::Spec::default() | |
| 867 | }, | |
| 868 | ); | |
| 869 | ui.close(); | |
| 870 | ||
| 871 | ui.close(); | |
| 872 | actions | |
| 873 | } | |
| 874 | ||
| 875 | 353 | fn scrubber_rect(&self) -> [f32; 4] { |
| 876 | 354 | [ |
| 877 | 355 | 10.0, |
| ... | ... | @@ -1108,285 +586,14 @@ impl ViewerState { |
| 1108 | 586 | } |
| 1109 | 587 | actions |
| 1110 | 588 | } |
| 1111 | ||
| 1112 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 1113 | let mut out = Vec::with_capacity(64); | |
| 1114 | let [w, _h] = self.size; | |
| 1115 | ||
| 1116 | if self.viewed_node.is_some() { | |
| 1117 | if let Some(img) = &self.output_image { | |
| 1118 | let (full_rect, vis, _, _) = self.image_layout(comp); | |
| 1119 | if vis[2] > vis[0] + 0.5 && vis[3] > vis[1] + 0.5 { | |
| 1120 | out.push(Primitive::Image { image: img, rect: vis, radius: 0.0 }); | |
| 1121 | out.push(Primitive::RoundedRect { | |
| 1122 | rect: [ | |
| 1123 | full_rect[0].max(vis[0]), | |
| 1124 | full_rect[1].max(vis[1]), | |
| 1125 | full_rect[2].min(vis[2]), | |
| 1126 | full_rect[3].min(vis[3]), | |
| 1127 | ], | |
| 1128 | radius: 1.0, | |
| 1129 | stroke: Some(1.0), | |
| 1130 | color: self.palette.border, | |
| 1131 | }); | |
| 1132 | if let Some(sel) = self.selected_node { | |
| 1133 | out.extend(self.gizmos.paint(comp, sel, current_frame, full_rect, vis)); | |
| 1134 | } | |
| 1135 | } | |
| 1136 | } | |
| 1137 | } | |
| 1138 | ||
| 1139 | if let Some((_, layout)) = &self.hud_layout { | |
| 1140 | out.push(Primitive::RoundedRect { | |
| 1141 | rect: [8.0, 5.0, (w - 8.0).max(20.0), 24.0], | |
| 1142 | radius: 4.0, | |
| 1143 | stroke: None, | |
| 1144 | color: [self.palette.bg[0] * 0.72, self.palette.bg[1] * 0.72, self.palette.bg[2] * 0.72, 0.82], | |
| 1145 | }); | |
| 1146 | out.push(Primitive::Text { | |
| 1147 | text: layout as &dyn Glyphs, | |
| 1148 | origin: [14.0, 8.0], | |
| 1149 | clip: Some([12.0, 5.0, w - 12.0, 24.0]), | |
| 1150 | ink: self.palette.text, | |
| 1151 | }); | |
| 1152 | } | |
| 1153 | ||
| 1154 | let sr = self.scrubber_rect(); | |
| 1155 | out.push(Primitive::RoundedRect { rect: sr, radius: 4.0, stroke: None, color: self.palette.scrub_bg }); | |
| 1156 | let r0 = comp.render_range[0] as f64; | |
| 1157 | let r1 = comp.render_range[1].max(comp.render_range[0] + 1) as f64; | |
| 1158 | let span = (r1 - r0).max(1.0); | |
| 1159 | let bar_w = (sr[2] - sr[0]).max(1.0); | |
| 1160 | let tick_w = (bar_w / span as f32).max(2.0); | |
| 1161 | ||
| 1162 | // Audio waveform overlay across the timeline scrubber | |
| 1163 | if !self.audio_peaks.is_empty() && bar_w > 16.0 { | |
| 1164 | let max_bars = (bar_w / 3.0).floor().clamp(8.0, 480.0) as usize; | |
| 1165 | let n = self.audio_peaks.len(); | |
| 1166 | let mid_y = 0.5 * (sr[1] + sr[3] - 3.0); | |
| 1167 | let max_half = ((sr[3] - sr[1] - 5.0) * 0.5).max(1.5); | |
| 1168 | let step_w = bar_w / max_bars as f32; | |
| 1169 | for b in 0..max_bars { | |
| 1170 | let i0 = (b * n) / max_bars; | |
| 1171 | let i1 = (((b + 1) * n) / max_bars).max(i0 + 1).min(n); | |
| 1172 | let mut peak = 0.0_f32; | |
| 1173 | for &v in &self.audio_peaks[i0..i1] { | |
| 1174 | if v > peak { peak = v; } | |
| 1175 | } | |
| 1176 | if peak > 0.015 { | |
| 1177 | let h = (peak * max_half).max(1.0); | |
| 1178 | let x = sr[0] + b as f32 * step_w; | |
| 1179 | out.push(Primitive::Rect { | |
| 1180 | rect: [x, mid_y - h, (x + (step_w - 0.8).max(1.0)).min(sr[2]), mid_y + h], | |
| 1181 | color: [0.46, 0.72, 0.96, 0.36], | |
| 1182 | }); | |
| 1183 | } | |
| 1184 | } | |
| 1185 | } | |
| 1186 | ||
| 1187 | // Green RAM cache ranges along the bottom of the scrubber | |
| 1188 | for &(c0, c1) in &self.ram_cached_ranges { | |
| 1189 | let f0 = (c0 as f64).max(r0); | |
| 1190 | let f1 = (c1 as f64).min(r1); | |
| 1191 | if f1 < f0 { | |
| 1192 | continue; | |
| 1193 | } | |
| 1194 | let x0 = sr[0] + ((f0 - r0) / span) as f32 * bar_w; | |
| 1195 | let x1 = (sr[0] + ((f1 - r0) / span) as f32 * bar_w + tick_w).min(sr[2]); | |
| 1196 | out.push(Primitive::Rect { | |
| 1197 | rect: [x0, sr[3] - 4.0, x1.max(x0 + 2.0), sr[3] - 1.0], | |
| 1198 | color: [0.22, 0.86, 0.48, 0.92], | |
| 1199 | }); | |
| 1200 | } | |
| 1201 | // Individual GPU-cached frames from this session | |
| 1202 | for &cf in &self.cached_frames { | |
| 1203 | if (cf as f64) >= r0 && (cf as f64) <= r1 { | |
| 1204 | let frac = ((cf as f64 - r0) / span) as f32; | |
| 1205 | let x = sr[0] + frac * bar_w; | |
| 1206 | out.push(Primitive::Rect { | |
| 1207 | rect: [x, sr[3] - 4.0, (x + tick_w).min(sr[2]), sr[3] - 1.0], | |
| 1208 | color: [0.24, 0.84, 0.50, 0.85], | |
| 1209 | }); | |
| 1210 | } | |
| 1211 | } | |
| 1212 | ||
| 1213 | // Crisp In/Out preview range highlight & bracket markers | |
| 1214 | if self.in_out_range[0].is_some() || self.in_out_range[1].is_some() { | |
| 1215 | let rr0 = comp.render_range[0]; | |
| 1216 | let rr1 = comp.render_range[1].max(rr0); | |
| 1217 | let raw_a = self.in_out_range[0].unwrap_or(rr0).clamp(rr0, rr1); | |
| 1218 | let raw_b = self.in_out_range[1].unwrap_or(rr1).clamp(rr0, rr1); | |
| 1219 | let (in_f, out_f) = if raw_a <= raw_b { (raw_a, raw_b) } else { (raw_b, raw_a) }; | |
| 1220 | let ix0 = sr[0] + ((in_f as f64 - r0) / span).clamp(0.0, 1.0) as f32 * bar_w; | |
| 1221 | let ix1 = sr[0] + ((out_f as f64 - r0) / span).clamp(0.0, 1.0) as f32 * bar_w; | |
| 1222 | let span_x1 = ix1.max(ix0 + 2.0).min(sr[2]); | |
| 1223 | let acc = self.palette.accent; | |
| 1224 | out.push(Primitive::RoundedRect { | |
| 1225 | rect: [ix0, sr[1], span_x1, sr[3]], | |
| 1226 | radius: 3.0, | |
| 1227 | stroke: None, | |
| 1228 | color: [acc[0], acc[1], acc[2], 0.22], | |
| 1229 | }); | |
| 1230 | out.push(Primitive::Rect { | |
| 1231 | rect: [ix0, sr[1], span_x1, sr[1] + 2.0], | |
| 1232 | color: [acc[0], acc[1], acc[2], 0.85], | |
| 1233 | }); | |
| 1234 | let bracket_col = [ | |
| 1235 | (acc[0] + 0.16).min(1.0), | |
| 1236 | (acc[1] + 0.16).min(1.0), | |
| 1237 | (acc[2] + 0.16).min(1.0), | |
| 1238 | 0.98, | |
| 1239 | ]; | |
| 1240 | // In bracket `[` | |
| 1241 | out.push(Primitive::Rect { | |
| 1242 | rect: [ix0, sr[1] - 1.0, (ix0 + 2.0).min(sr[2]), sr[3] + 1.0], | |
| 1243 | color: bracket_col, | |
| 1244 | }); | |
| 1245 | out.push(Primitive::Rect { | |
| 1246 | rect: [ix0, sr[1] - 1.0, (ix0 + 6.0).min(sr[2]), sr[1] + 1.0], | |
| 1247 | color: bracket_col, | |
| 1248 | }); | |
| 1249 | out.push(Primitive::Rect { | |
| 1250 | rect: [ix0, sr[3] - 1.0, (ix0 + 6.0).min(sr[2]), sr[3] + 1.0], | |
| 1251 | color: bracket_col, | |
| 1252 | }); | |
| 1253 | // Out bracket `]` | |
| 1254 | let ox = span_x1; | |
| 1255 | out.push(Primitive::Rect { | |
| 1256 | rect: [(ox - 2.0).max(sr[0]), sr[1] - 1.0, ox, sr[3] + 1.0], | |
| 1257 | color: bracket_col, | |
| 1258 | }); | |
| 1259 | out.push(Primitive::Rect { | |
| 1260 | rect: [(ox - 6.0).max(sr[0]), sr[1] - 1.0, ox, sr[1] + 1.0], | |
| 1261 | color: bracket_col, | |
| 1262 | }); | |
| 1263 | out.push(Primitive::Rect { | |
| 1264 | rect: [(ox - 6.0).max(sr[0]), sr[3] - 1.0, ox, sr[3] + 1.0], | |
| 1265 | color: bracket_col, | |
| 1266 | }); | |
| 1267 | } | |
| 1268 | ||
| 1269 | let frac = ((current_frame - r0) / span).clamp(0.0, 1.0) as f32; | |
| 1270 | let px = sr[0] + frac * bar_w; | |
| 1271 | out.push(Primitive::RoundedRect { | |
| 1272 | rect: [sr[0], sr[1], px.max(sr[0] + 2.0), sr[3]], | |
| 1273 | radius: 4.0, | |
| 1274 | stroke: None, | |
| 1275 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.25], | |
| 1276 | }); | |
| 1277 | out.push(Primitive::RoundedRect { | |
| 1278 | rect: [px - 3.0, sr[1] - 2.0, px + 3.0, sr[3] + 2.0], | |
| 1279 | radius: 2.0, | |
| 1280 | stroke: None, | |
| 1281 | color: [0.98, 0.40, 0.36, 1.0], | |
| 1282 | }); | |
| 1283 | ||
| 1284 | // Knife-styled right-click floating context menu | |
| 1285 | if let Some(anchor) = self.context_menu { | |
| 1286 | let mr = self.context_menu_rect(anchor); | |
| 1287 | // Soft drop shadow | |
| 1288 | out.push(Primitive::RoundedRect { | |
| 1289 | rect: [mr[0] + 2.0, mr[1] + 4.0, mr[2] + 2.0, mr[3] + 4.0], | |
| 1290 | radius: 8.0, | |
| 1291 | stroke: None, | |
| 1292 | color: [0.0, 0.0, 0.0, 0.38], | |
| 1293 | }); | |
| 1294 | let bg = self.palette.bar; | |
| 1295 | let panel_fill = [ | |
| 1296 | (bg[0] + 0.03).min(1.0), | |
| 1297 | (bg[1] + 0.03).min(1.0), | |
| 1298 | (bg[2] + 0.035).min(1.0), | |
| 1299 | 0.97, | |
| 1300 | ]; | |
| 1301 | out.push(Primitive::RoundedRect { | |
| 1302 | rect: mr, | |
| 1303 | radius: 7.0, | |
| 1304 | stroke: None, | |
| 1305 | color: panel_fill, | |
| 1306 | }); | |
| 1307 | out.push(Primitive::RoundedRect { | |
| 1308 | rect: mr, | |
| 1309 | radius: 7.0, | |
| 1310 | stroke: Some(1.0), | |
| 1311 | color: self.palette.border, | |
| 1312 | }); | |
| 1313 | ||
| 1314 | // Hairline separator after export actions | |
| 1315 | let sep_y = mr[1] | |
| 1316 | + CONTEXT_MENU_PAD_Y | |
| 1317 | + (CONTEXT_MENU_SEP_AFTER + 1) as f32 * CONTEXT_MENU_ITEM_H | |
| 1318 | + CONTEXT_MENU_SEP_H * 0.5; | |
| 1319 | out.push(Primitive::Rect { | |
| 1320 | rect: [mr[0] + 8.0, sep_y - 0.5, mr[2] - 8.0, sep_y + 0.5], | |
| 1321 | color: self.palette.border, | |
| 1322 | }); | |
| 1323 | ||
| 1324 | let acc = self.palette.accent; | |
| 1325 | for (idx, layout) in self.menu_item_layouts.iter().enumerate() { | |
| 1326 | let item_r = self.context_menu_item_rect(mr, idx); | |
| 1327 | if self.context_menu_hover == Some(idx) { | |
| 1328 | out.push(Primitive::RoundedRect { | |
| 1329 | rect: item_r, | |
| 1330 | radius: 4.0, | |
| 1331 | stroke: None, | |
| 1332 | color: [acc[0], acc[1], acc[2], 0.24], | |
| 1333 | }); | |
| 1334 | } | |
| 1335 | out.push(Primitive::Text { | |
| 1336 | text: layout as &dyn Glyphs, | |
| 1337 | origin: [item_r[0] + 10.0, item_r[1] + 5.0], | |
| 1338 | clip: Some([item_r[0] + 4.0, item_r[1], item_r[2] - 4.0, item_r[3]]), | |
| 1339 | ink: self.palette.text, | |
| 1340 | }); | |
| 1341 | } | |
| 1342 | } | |
| 1343 | ||
| 1344 | out | |
| 1345 | } | |
| 1346 | } | |
| 1347 | ||
| 1348 | /// Sleek Knife-style compact toolbar control (soft tinted surface + hairline border, | |
| 1349 | /// accent tint when active — replaces the chunky generic `ui::button`). | |
| 1350 | fn tool_btn(ui: &mut ui::Ui, part: impl std::hash::Hash, label: &str, active: bool) -> ui::Signal { | |
| 1351 | let t = &ui.theme; | |
| 1352 | let fill = if active { | |
| 1353 | [t.accent[0], t.accent[1], t.accent[2], 0.24] | |
| 1354 | } else { | |
| 1355 | [t.base[0], t.base[1], t.base[2], 0.65] | |
| 1356 | }; | |
| 1357 | let border = if active { | |
| 1358 | [t.accent[0], t.accent[1], t.accent[2], 0.65] | |
| 1359 | } else { | |
| 1360 | t.rule | |
| 1361 | }; | |
| 1362 | let ink = if active { t.text } else { t.text_dim }; | |
| 1363 | ui.leaf( | |
| 1364 | part, | |
| 1365 | ui::Spec { | |
| 1366 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 1367 | flags: ui::Flags::CLICKABLE, | |
| 1368 | size: [ui::fit(), ui::px(22.0)], | |
| 1369 | text: Some(label), | |
| 1370 | font_size: Some(11.5), | |
| 1371 | medium: true, | |
| 1372 | color: Some(ink), | |
| 1373 | fill: Some(fill), | |
| 1374 | border: Some(border), | |
| 1375 | hover_fill: Some(t.ghost_soft), | |
| 1376 | hover_border: Some(t.accent), | |
| 1377 | radius: 5.0, | |
| 1378 | pad: [7.0, 0.0], | |
| 1379 | center: true, | |
| 1380 | ..ui::Spec::default() | |
| 1381 | }, | |
| 1382 | ) | |
| 1383 | 589 | } |
| 1384 | 590 | |
| 1385 | 591 | #[cfg(test)] |
| 1386 | 592 | mod tests { |
| 1387 | 593 | use super::*; |
| 594 | use std::time::Instant; | |
| 1388 | 595 | |
| 1389 | fn test_comp() -> Comp { | |
| 596 | pub(crate) fn test_comp() -> Comp { | |
| 1390 | 597 | let src = r#"Composition { |
| 1391 | 598 | CurrentTime = 15, |
| 1392 | 599 | RenderRange = { 10, 50 }, |
| ... | ... | @@ -1404,27 +611,6 @@ mod tests { |
| 1404 | 611 | Comp::from_fusion_str(src).expect("parse test comp") |
| 1405 | 612 | } |
| 1406 | 613 | |
| 1407 | #[test] | |
| 1408 | fn effective_preview_range_sorts_and_clamps() { | |
| 1409 | let comp = test_comp(); | |
| 1410 | let mut clock = PlaybackClock::new(); | |
| 1411 | clock.init_from_comp(&comp); | |
| 1412 | assert_eq!(clock.effective_preview_range(&comp), [10, 50]); | |
| 1413 | ||
| 1414 | clock.in_out_range = [Some(20), None]; | |
| 1415 | assert_eq!(clock.effective_preview_range(&comp), [20, 50]); | |
| 1416 | ||
| 1417 | clock.in_out_range = [None, Some(30)]; | |
| 1418 | assert_eq!(clock.effective_preview_range(&comp), [10, 30]); | |
| 1419 | ||
| 1420 | // Reversed and out-of-bounds points get clamped and sorted cleanly | |
| 1421 | clock.in_out_range = [Some(99), Some(2)]; | |
| 1422 | assert_eq!(clock.effective_preview_range(&comp), [10, 50]); | |
| 1423 | ||
| 1424 | clock.in_out_range = [Some(35), Some(18)]; | |
| 1425 | assert_eq!(clock.effective_preview_range(&comp), [18, 35]); | |
| 1426 | } | |
| 1427 | ||
| 1428 | 614 | #[test] |
| 1429 | 615 | fn hotkeys_and_right_click_menu_emit_actions() { |
| 1430 | 616 | let comp = test_comp(); |
| ... | ... | @@ -1542,117 +728,6 @@ mod tests { |
| 1542 | 728 | assert!(!viewer.is_context_menu_open()); |
| 1543 | 729 | } |
| 1544 | 730 | |
| 1545 | #[test] | |
| 1546 | fn playback_clock_in_out_wraparound_realtime_and_every_frame() { | |
| 1547 | let comp = test_comp(); // fps = 30, render_range = [10, 50] | |
| 1548 | let t0 = Instant::now(); | |
| 1549 | ||
| 1550 | // RealtimeDrop: starting outside [20, 24] snaps cleanly to 20, then wraps 24 -> 20 | |
| 1551 | let mut rt = PlaybackClock::new(); | |
| 1552 | rt.init_from_comp(&comp); | |
| 1553 | rt.in_out_range = [Some(20), Some(24)]; | |
| 1554 | rt.set_frame(&comp, 12.0, t0); // outside In/Out range | |
| 1555 | rt.toggle_play_with_comp(&comp, t0); | |
| 1556 | assert_eq!(rt.current_frame, 20.0); | |
| 1557 | ||
| 1558 | let step = Duration::from_secs_f64(1.0 / 30.0); | |
| 1559 | // Advance 4 frames -> frame 24 (last frame in [20..=24]) | |
| 1560 | let r4 = rt.tick_synced(&comp, t0 + step * 4 + Duration::from_millis(2)); | |
| 1561 | assert!(r4.playing); | |
| 1562 | assert!(r4.frame_changed); | |
| 1563 | assert!(!r4.looped); | |
| 1564 | assert_eq!(rt.current_frame, 24.0); | |
| 1565 | ||
| 1566 | // Advance 5th frame -> wraps cleanly back to 20 and reports looped = true | |
| 1567 | let r5 = rt.tick_synced(&comp, t0 + step * 5 + Duration::from_millis(2)); | |
| 1568 | assert!(r5.playing); | |
| 1569 | assert!(r5.frame_changed); | |
| 1570 | assert!(r5.looped); | |
| 1571 | assert_eq!(rt.current_frame, 20.0); | |
| 1572 | ||
| 1573 | // Next frame after wrap advances from 20 -> 21 without double-looping | |
| 1574 | let r6 = rt.tick_synced(&comp, t0 + step * 6 + Duration::from_millis(4)); | |
| 1575 | assert!(r6.frame_changed); | |
| 1576 | assert!(!r6.looped); | |
| 1577 | assert_eq!(rt.current_frame, 21.0); | |
| 1578 | ||
| 1579 | // EveryFrame: steps +1 per >=1/fps, preserves cadence across minor jitter, wraps 22 -> 20 | |
| 1580 | let mut ef = PlaybackClock::new(); | |
| 1581 | ef.init_from_comp(&comp); | |
| 1582 | ef.sync_mode = SyncMode::EveryFrame; | |
| 1583 | ef.in_out_range = [Some(20), Some(22)]; | |
| 1584 | ef.set_frame(&comp, 20.0, t0); | |
| 1585 | ef.toggle_play_with_comp(&comp, t0); | |
| 1586 | ||
| 1587 | // Minor 4ms jitter on step 1 | |
| 1588 | let e1 = ef.tick_synced(&comp, t0 + step + Duration::from_millis(4)); | |
| 1589 | assert_eq!((e1.frame_changed, e1.looped, ef.current_frame), (true, false, 21.0)); | |
| 1590 | // Step 2 fires at t0 + 2*step (doesn't lose the 4ms jitter!) | |
| 1591 | let e2 = ef.tick_synced(&comp, t0 + step * 2 + Duration::from_millis(1)); | |
| 1592 | assert_eq!((e2.frame_changed, e2.looped, ef.current_frame), (true, false, 22.0)); | |
| 1593 | // Step 3 wraps 22 -> 20 with looped = true | |
| 1594 | let e3 = ef.tick_synced(&comp, t0 + step * 3 + Duration::from_millis(1)); | |
| 1595 | assert_eq!((e3.frame_changed, e3.looped, ef.current_frame), (true, true, 20.0)); | |
| 1596 | } | |
| 1597 | ||
| 1598 | #[test] | |
| 1599 | fn audio_sync_target_time_computation() { | |
| 1600 | let mut comp = test_comp(); | |
| 1601 | comp.fps = 24.0; | |
| 1602 | comp.audio_offset = 12.0; | |
| 1603 | ||
| 1604 | let t0 = Instant::now(); | |
| 1605 | let mut clock = PlaybackClock::new(); | |
| 1606 | clock.init_from_comp(&comp); | |
| 1607 | clock.in_out_range = [Some(12), Some(36)]; | |
| 1608 | clock.set_frame(&comp, 0.0, t0); | |
| 1609 | assert!((clock.audio_target_secs(&comp) - (-0.5)).abs() < 1e-9); | |
| 1610 | ||
| 1611 | clock.set_frame(&comp, 12.0, t0); | |
| 1612 | assert!((clock.audio_target_secs(&comp) - 0.0).abs() < 1e-9); | |
| 1613 | ||
| 1614 | clock.set_frame(&comp, 36.0, t0); | |
| 1615 | assert!((clock.audio_target_secs(&comp) - 1.0).abs() < 1e-9); | |
| 1616 | ||
| 1617 | // RealtimeDrop expected_audio_secs tracks continuous sub-frame progress & wraparound | |
| 1618 | clock.set_frame(&comp, 24.0, t0); | |
| 1619 | clock.toggle_play_with_comp(&comp, t0); | |
| 1620 | let half_sec = Duration::from_millis(500); | |
| 1621 | let exp_rt = clock.expected_audio_secs(&comp, t0 + half_sec); | |
| 1622 | assert!((exp_rt - 1.0).abs() < 1e-6, "expected 1.0s at frame 36, got {exp_rt}"); | |
| 1623 | ||
| 1624 | // EveryFrame expected_audio_secs caps sub-frame progress at 1/fps when a frame stalls | |
| 1625 | clock.sync_mode = SyncMode::EveryFrame; | |
| 1626 | clock.set_frame(&comp, 24.0, t0); // expected base = (24 - 12) / 24 = 0.5s | |
| 1627 | let exp_stalled = clock.expected_audio_secs(&comp, t0 + Duration::from_millis(400)); | |
| 1628 | let max_expected = 0.5 + (1.0 / 24.0); | |
| 1629 | assert!( | |
| 1630 | (exp_stalled - max_expected).abs() < 1e-6, | |
| 1631 | "EveryFrame stalled expected_audio_secs should cap at {max_expected}, got {exp_stalled}" | |
| 1632 | ); | |
| 1633 | } | |
| 1634 | ||
| 1635 | #[test] | |
| 1636 | fn loop_wraparound_keeps_the_sub_frame_overshoot() { | |
| 1637 | let comp = test_comp(); // 30 fps | |
| 1638 | let t0 = Instant::now(); | |
| 1639 | let step = Duration::from_secs_f64(1.0 / 30.0); | |
| 1640 | let mut clock = PlaybackClock::new(); | |
| 1641 | clock.init_from_comp(&comp); | |
| 1642 | clock.in_out_range = [Some(20), Some(24)]; | |
| 1643 | clock.set_frame(&comp, 20.0, t0); | |
| 1644 | clock.toggle_play_with_comp(&comp, t0); | |
| 1645 | ||
| 1646 | // Wrap with half a frame of overshoot... | |
| 1647 | let wrap = clock.tick_synced(&comp, t0 + step.mul_f64(5.5)); | |
| 1648 | assert!(wrap.looped); | |
| 1649 | assert_eq!(clock.current_frame, 20.0); | |
| 1650 | // ...so 0.6 frames later a whole frame has elapsed on the original timeline. | |
| 1651 | let next = clock.tick_synced(&comp, t0 + step.mul_f64(6.1)); | |
| 1652 | assert!(!next.looped); | |
| 1653 | assert_eq!(clock.current_frame, 21.0); | |
| 1654 | } | |
| 1655 | ||
| 1656 | 731 | #[test] |
| 1657 | 732 | fn gizmo_needs_a_viewed_node_and_reports_drag_end() { |
| 1658 | 733 | let comp = Comp::from_fusion_str( |
crates/viewer/src/paint.rs created+499| ... | ... | @@ -0,0 +1,499 @@ |
| 1 | use super::*; | |
| 2 | ||
| 3 | impl ViewerState { | |
| 4 | pub fn prepare_hud(&mut self, comp: &Comp, engine: &Engine, clock: &PlaybackClock) { | |
| 5 | self.in_out_range = clock.in_out_range; | |
| 6 | let node_name = self | |
| 7 | .viewed_node | |
| 8 | .and_then(|id| comp.nodes.get(id.0)) | |
| 9 | .map_or("(empty)", |n| n.name.as_str()); | |
| 10 | let cache_lbl = match engine.cache.mode { | |
| 11 | CacheMode::Enabled => "CACHE", | |
| 12 | CacheMode::Bypass => "BYPASS", | |
| 13 | }; | |
| 14 | let sync_lbl = match clock.sync_mode { | |
| 15 | SyncMode::RealtimeDrop => "RT", | |
| 16 | SyncMode::EveryFrame => "EVERY", | |
| 17 | }; | |
| 18 | let hiq_lbl = if engine.hiq { "HiQ" } else { "FAST" }; | |
| 19 | let (_, _, _, eff_scale) = self.image_layout(comp); | |
| 20 | let zoom_pct = (eff_scale * 100.0).round() as i32; | |
| 21 | let s = &self.last_stats; | |
| 22 | let io_suffix = if clock.in_out_range[0].is_some() || clock.in_out_range[1].is_some() { | |
| 23 | let [a, b] = clock.effective_preview_range(comp); | |
| 24 | format!(" · [{a}..{b}]") | |
| 25 | } else { | |
| 26 | String::new() | |
| 27 | }; | |
| 28 | let line = format!( | |
| 29 | "[V{}] {} · f{}{} · {}% · {:.1}ms · ev:{} h:{} m:{} · {:.0}MB · 1:{} · {} · {} · {}", | |
| 30 | self.slot + 1, | |
| 31 | node_name, | |
| 32 | clock.current_frame.round() as i64, | |
| 33 | io_suffix, | |
| 34 | zoom_pct, | |
| 35 | s.eval_ms, | |
| 36 | s.nodes_evaluated, | |
| 37 | s.cache_hits, | |
| 38 | s.cache_misses, | |
| 39 | s.cached_vram_mb, | |
| 40 | engine.proxy_scale, | |
| 41 | hiq_lbl, | |
| 42 | cache_lbl, | |
| 43 | sync_lbl, | |
| 44 | ); | |
| 45 | if self.hud_layout.as_ref().is_none_or(|(prev, _)| prev != &line) { | |
| 46 | let font = text::Font { | |
| 47 | families: Arc::from([text::Family::Mono]), | |
| 48 | size: 10.5, | |
| 49 | weight: 500, | |
| 50 | italic: false, | |
| 51 | }; | |
| 52 | let styles = [text::Style::new(font)]; | |
| 53 | let layout = text::Paragraph { | |
| 54 | text: &line, | |
| 55 | styles: &styles, | |
| 56 | spans: &[], | |
| 57 | replacements: &[], | |
| 58 | width: None, | |
| 59 | indent: 0.0, | |
| 60 | line_height: 1.2, | |
| 61 | align: text::Align::Left, | |
| 62 | tab: 32.0, | |
| 63 | } | |
| 64 | .layout(); | |
| 65 | self.hud_layout = Some((line, layout)); | |
| 66 | } | |
| 67 | ||
| 68 | if self.menu_item_layouts.len() != CONTEXT_MENU_ITEMS.len() { | |
| 69 | let font = text::Font { | |
| 70 | families: Arc::from([text::Family::Sans]), | |
| 71 | size: 12.0, | |
| 72 | weight: 500, | |
| 73 | italic: false, | |
| 74 | }; | |
| 75 | let styles = [text::Style::new(font)]; | |
| 76 | self.menu_item_layouts = CONTEXT_MENU_ITEMS | |
| 77 | .iter() | |
| 78 | .map(|(_, label)| { | |
| 79 | text::Paragraph { | |
| 80 | text: label, | |
| 81 | styles: &styles, | |
| 82 | spans: &[], | |
| 83 | replacements: &[], | |
| 84 | width: None, | |
| 85 | indent: 0.0, | |
| 86 | line_height: 1.2, | |
| 87 | align: text::Align::Left, | |
| 88 | tab: 32.0, | |
| 89 | } | |
| 90 | .layout() | |
| 91 | }) | |
| 92 | .collect(); | |
| 93 | } | |
| 94 | } | |
| 95 | ||
| 96 | pub fn build_ui( | |
| 97 | &mut self, | |
| 98 | ui: &mut ui::Ui, | |
| 99 | pane_id: ui::Id, | |
| 100 | canvas_id: ui::Id, | |
| 101 | engine: &Engine, | |
| 102 | clock: &PlaybackClock, | |
| 103 | ) -> Vec<ViewerAction> { | |
| 104 | self.in_out_range = clock.in_out_range; | |
| 105 | let mut actions = Vec::new(); | |
| 106 | ui.open_as( | |
| 107 | pane_id, | |
| 108 | ui::Spec { | |
| 109 | size: [ui::fill(), ui::fill()], | |
| 110 | axis: ui::Axis::Y, | |
| 111 | fill: Some(self.background()), | |
| 112 | ..ui::Spec::default() | |
| 113 | }, | |
| 114 | ); | |
| 115 | ||
| 116 | ui.open( | |
| 117 | ("top_bar", self.slot), | |
| 118 | ui::Spec { | |
| 119 | size: [ui::fill(), ui::px(30.0)], | |
| 120 | axis: ui::Axis::X, | |
| 121 | pad: [6.0, 4.0], | |
| 122 | gap: 4.0, | |
| 123 | fill: Some(self.palette.bar), | |
| 124 | ..ui::Spec::default() | |
| 125 | }, | |
| 126 | ); | |
| 127 | ||
| 128 | if self.slot == 0 { | |
| 129 | let play_lbl = if clock.playing { "Pause" } else { "Play" }; | |
| 130 | if tool_btn(ui, ("play", self.slot), play_lbl, clock.playing).clicked { | |
| 131 | actions.push(ViewerAction::TogglePlay); | |
| 132 | } | |
| 133 | if tool_btn(ui, ("prev", self.slot), "<", false).clicked { | |
| 134 | actions.push(ViewerAction::SetFrame(clock.current_frame - 1.0)); | |
| 135 | } | |
| 136 | if tool_btn(ui, ("next", self.slot), ">", false).clicked { | |
| 137 | actions.push(ViewerAction::SetFrame(clock.current_frame + 1.0)); | |
| 138 | } | |
| 139 | let sync_lbl = match clock.sync_mode { | |
| 140 | SyncMode::RealtimeDrop => "RT", | |
| 141 | SyncMode::EveryFrame => "Every", | |
| 142 | }; | |
| 143 | if tool_btn(ui, ("sync", self.slot), sync_lbl, clock.sync_mode == SyncMode::EveryFrame).clicked { | |
| 144 | actions.push(ViewerAction::ToggleSyncMode); | |
| 145 | } | |
| 146 | for scale in [1u32, 2, 4] { | |
| 147 | let lbl = format!("1:{scale}"); | |
| 148 | if tool_btn(ui, ("proxy", self.slot, scale), &lbl, engine.proxy_scale == scale).clicked { | |
| 149 | actions.push(ViewerAction::SetProxy(scale)); | |
| 150 | } | |
| 151 | } | |
| 152 | if tool_btn(ui, ("hiq", self.slot), "HiQ", engine.hiq).clicked { | |
| 153 | actions.push(ViewerAction::ToggleHiQ); | |
| 154 | } | |
| 155 | } | |
| 156 | ||
| 157 | let ch_lbl = match self.display.channel { | |
| 158 | 1 => "R", | |
| 159 | 2 => "G", | |
| 160 | 3 => "B", | |
| 161 | 4 => "A", | |
| 162 | _ => "RGBA", | |
| 163 | }; | |
| 164 | if tool_btn(ui, ("chan", self.slot), ch_lbl, self.display.channel != 0).clicked { | |
| 165 | self.display.channel = (self.display.channel + 1) % 5; | |
| 166 | self.dirty = true; | |
| 167 | actions.push(ViewerAction::NeedRender); | |
| 168 | } | |
| 169 | let filt_lbl = if self.display.smooth_resize { "Smooth" } else { "Pixel" }; | |
| 170 | if tool_btn(ui, ("smooth", self.slot), filt_lbl, self.display.smooth_resize).clicked { | |
| 171 | self.display.smooth_resize = !self.display.smooth_resize; | |
| 172 | self.dirty = true; | |
| 173 | actions.push(ViewerAction::NeedRender); | |
| 174 | } | |
| 175 | if tool_btn(ui, ("fit", self.slot), "Fit", self.fit_mode).clicked { | |
| 176 | self.fit(); | |
| 177 | } | |
| 178 | if tool_btn(ui, ("gizmo", self.slot), "Gizmo", self.gizmos.enabled).clicked { | |
| 179 | self.gizmos.enabled = !self.gizmos.enabled; | |
| 180 | } | |
| 181 | ||
| 182 | if self.slot == 0 { | |
| 183 | let bypass_lbl = match engine.cache.mode { | |
| 184 | CacheMode::Enabled => "Cache", | |
| 185 | CacheMode::Bypass => "Bypass", | |
| 186 | }; | |
| 187 | if tool_btn(ui, ("cache_mode", self.slot), bypass_lbl, engine.cache.mode == CacheMode::Bypass).clicked { | |
| 188 | actions.push(ViewerAction::ToggleCacheBypass); | |
| 189 | } | |
| 190 | if tool_btn(ui, ("bust_frame", self.slot), "Bust F", false).clicked { | |
| 191 | let f = clock.current_frame.round() as i64; | |
| 192 | self.cached_frames.remove(&f); | |
| 193 | self.dirty = true; | |
| 194 | actions.push(ViewerAction::BustFrame(f)); | |
| 195 | } | |
| 196 | if tool_btn(ui, ("bust_sub", self.slot), "Node", false).clicked { | |
| 197 | self.cached_frames.clear(); | |
| 198 | self.dirty = true; | |
| 199 | actions.push(ViewerAction::BustSubtree); | |
| 200 | } | |
| 201 | if tool_btn(ui, ("bust_gpu", self.slot), "GPU", false).clicked { | |
| 202 | self.cached_frames.clear(); | |
| 203 | self.dirty = true; | |
| 204 | actions.push(ViewerAction::BustGpu); | |
| 205 | } | |
| 206 | if tool_btn(ui, ("bust_all", self.slot), "All", false).clicked { | |
| 207 | self.cached_frames.clear(); | |
| 208 | self.dirty = true; | |
| 209 | actions.push(ViewerAction::BustAll); | |
| 210 | } | |
| 211 | } | |
| 212 | ui.close(); | |
| 213 | ||
| 214 | ui.open_as( | |
| 215 | canvas_id, | |
| 216 | ui::Spec { | |
| 217 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 218 | size: [ui::fill(), ui::fill()], | |
| 219 | ..ui::Spec::default() | |
| 220 | }, | |
| 221 | ); | |
| 222 | ui.close(); | |
| 223 | ||
| 224 | ui.close(); | |
| 225 | actions | |
| 226 | } | |
| 227 | ||
| 228 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 229 | let mut out = Vec::with_capacity(64); | |
| 230 | let [w, _h] = self.size; | |
| 231 | ||
| 232 | if self.viewed_node.is_some() { | |
| 233 | if let Some(img) = &self.output_image { | |
| 234 | let (full_rect, vis, _, _) = self.image_layout(comp); | |
| 235 | if vis[2] > vis[0] + 0.5 && vis[3] > vis[1] + 0.5 { | |
| 236 | out.push(Primitive::Image { image: img, rect: vis, radius: 0.0 }); | |
| 237 | out.push(Primitive::RoundedRect { | |
| 238 | rect: [ | |
| 239 | full_rect[0].max(vis[0]), | |
| 240 | full_rect[1].max(vis[1]), | |
| 241 | full_rect[2].min(vis[2]), | |
| 242 | full_rect[3].min(vis[3]), | |
| 243 | ], | |
| 244 | radius: 1.0, | |
| 245 | stroke: Some(1.0), | |
| 246 | color: self.palette.border, | |
| 247 | }); | |
| 248 | if let Some(sel) = self.selected_node { | |
| 249 | out.extend(self.gizmos.paint(comp, sel, current_frame, full_rect, vis)); | |
| 250 | } | |
| 251 | } | |
| 252 | } | |
| 253 | } | |
| 254 | ||
| 255 | if let Some((_, layout)) = &self.hud_layout { | |
| 256 | out.push(Primitive::RoundedRect { | |
| 257 | rect: [8.0, 5.0, (w - 8.0).max(20.0), 24.0], | |
| 258 | radius: 4.0, | |
| 259 | stroke: None, | |
| 260 | color: [self.palette.bg[0] * 0.72, self.palette.bg[1] * 0.72, self.palette.bg[2] * 0.72, 0.82], | |
| 261 | }); | |
| 262 | out.push(Primitive::Text { | |
| 263 | text: layout as &dyn Glyphs, | |
| 264 | origin: [14.0, 8.0], | |
| 265 | clip: Some([12.0, 5.0, w - 12.0, 24.0]), | |
| 266 | ink: self.palette.text, | |
| 267 | }); | |
| 268 | } | |
| 269 | ||
| 270 | let sr = self.scrubber_rect(); | |
| 271 | out.push(Primitive::RoundedRect { rect: sr, radius: 4.0, stroke: None, color: self.palette.scrub_bg }); | |
| 272 | let r0 = comp.render_range[0] as f64; | |
| 273 | let r1 = comp.render_range[1].max(comp.render_range[0] + 1) as f64; | |
| 274 | let span = (r1 - r0).max(1.0); | |
| 275 | let bar_w = (sr[2] - sr[0]).max(1.0); | |
| 276 | let tick_w = (bar_w / span as f32).max(2.0); | |
| 277 | ||
| 278 | // Audio waveform overlay across the timeline scrubber | |
| 279 | if !self.audio_peaks.is_empty() && bar_w > 16.0 { | |
| 280 | let max_bars = (bar_w / 3.0).floor().clamp(8.0, 480.0) as usize; | |
| 281 | let n = self.audio_peaks.len(); | |
| 282 | let mid_y = 0.5 * (sr[1] + sr[3] - 3.0); | |
| 283 | let max_half = ((sr[3] - sr[1] - 5.0) * 0.5).max(1.5); | |
| 284 | let step_w = bar_w / max_bars as f32; | |
| 285 | for b in 0..max_bars { | |
| 286 | let i0 = (b * n) / max_bars; | |
| 287 | let i1 = (((b + 1) * n) / max_bars).max(i0 + 1).min(n); | |
| 288 | let mut peak = 0.0_f32; | |
| 289 | for &v in &self.audio_peaks[i0..i1] { | |
| 290 | if v > peak { peak = v; } | |
| 291 | } | |
| 292 | if peak > 0.015 { | |
| 293 | let h = (peak * max_half).max(1.0); | |
| 294 | let x = sr[0] + b as f32 * step_w; | |
| 295 | out.push(Primitive::Rect { | |
| 296 | rect: [x, mid_y - h, (x + (step_w - 0.8).max(1.0)).min(sr[2]), mid_y + h], | |
| 297 | color: [0.46, 0.72, 0.96, 0.36], | |
| 298 | }); | |
| 299 | } | |
| 300 | } | |
| 301 | } | |
| 302 | ||
| 303 | // Green RAM cache ranges along the bottom of the scrubber | |
| 304 | for &(c0, c1) in &self.ram_cached_ranges { | |
| 305 | let f0 = (c0 as f64).max(r0); | |
| 306 | let f1 = (c1 as f64).min(r1); | |
| 307 | if f1 < f0 { | |
| 308 | continue; | |
| 309 | } | |
| 310 | let x0 = sr[0] + ((f0 - r0) / span) as f32 * bar_w; | |
| 311 | let x1 = (sr[0] + ((f1 - r0) / span) as f32 * bar_w + tick_w).min(sr[2]); | |
| 312 | out.push(Primitive::Rect { | |
| 313 | rect: [x0, sr[3] - 4.0, x1.max(x0 + 2.0), sr[3] - 1.0], | |
| 314 | color: [0.22, 0.86, 0.48, 0.92], | |
| 315 | }); | |
| 316 | } | |
| 317 | // Individual GPU-cached frames from this session | |
| 318 | for &cf in &self.cached_frames { | |
| 319 | if (cf as f64) >= r0 && (cf as f64) <= r1 { | |
| 320 | let frac = ((cf as f64 - r0) / span) as f32; | |
| 321 | let x = sr[0] + frac * bar_w; | |
| 322 | out.push(Primitive::Rect { | |
| 323 | rect: [x, sr[3] - 4.0, (x + tick_w).min(sr[2]), sr[3] - 1.0], | |
| 324 | color: [0.24, 0.84, 0.50, 0.85], | |
| 325 | }); | |
| 326 | } | |
| 327 | } | |
| 328 | ||
| 329 | // Crisp In/Out preview range highlight & bracket markers | |
| 330 | if self.in_out_range[0].is_some() || self.in_out_range[1].is_some() { | |
| 331 | let rr0 = comp.render_range[0]; | |
| 332 | let rr1 = comp.render_range[1].max(rr0); | |
| 333 | let raw_a = self.in_out_range[0].unwrap_or(rr0).clamp(rr0, rr1); | |
| 334 | let raw_b = self.in_out_range[1].unwrap_or(rr1).clamp(rr0, rr1); | |
| 335 | let (in_f, out_f) = if raw_a <= raw_b { (raw_a, raw_b) } else { (raw_b, raw_a) }; | |
| 336 | let ix0 = sr[0] + ((in_f as f64 - r0) / span).clamp(0.0, 1.0) as f32 * bar_w; | |
| 337 | let ix1 = sr[0] + ((out_f as f64 - r0) / span).clamp(0.0, 1.0) as f32 * bar_w; | |
| 338 | let span_x1 = ix1.max(ix0 + 2.0).min(sr[2]); | |
| 339 | let acc = self.palette.accent; | |
| 340 | out.push(Primitive::RoundedRect { | |
| 341 | rect: [ix0, sr[1], span_x1, sr[3]], | |
| 342 | radius: 3.0, | |
| 343 | stroke: None, | |
| 344 | color: [acc[0], acc[1], acc[2], 0.22], | |
| 345 | }); | |
| 346 | out.push(Primitive::Rect { | |
| 347 | rect: [ix0, sr[1], span_x1, sr[1] + 2.0], | |
| 348 | color: [acc[0], acc[1], acc[2], 0.85], | |
| 349 | }); | |
| 350 | let bracket_col = [ | |
| 351 | (acc[0] + 0.16).min(1.0), | |
| 352 | (acc[1] + 0.16).min(1.0), | |
| 353 | (acc[2] + 0.16).min(1.0), | |
| 354 | 0.98, | |
| 355 | ]; | |
| 356 | // In bracket `[` | |
| 357 | out.push(Primitive::Rect { | |
| 358 | rect: [ix0, sr[1] - 1.0, (ix0 + 2.0).min(sr[2]), sr[3] + 1.0], | |
| 359 | color: bracket_col, | |
| 360 | }); | |
| 361 | out.push(Primitive::Rect { | |
| 362 | rect: [ix0, sr[1] - 1.0, (ix0 + 6.0).min(sr[2]), sr[1] + 1.0], | |
| 363 | color: bracket_col, | |
| 364 | }); | |
| 365 | out.push(Primitive::Rect { | |
| 366 | rect: [ix0, sr[3] - 1.0, (ix0 + 6.0).min(sr[2]), sr[3] + 1.0], | |
| 367 | color: bracket_col, | |
| 368 | }); | |
| 369 | // Out bracket `]` | |
| 370 | let ox = span_x1; | |
| 371 | out.push(Primitive::Rect { | |
| 372 | rect: [(ox - 2.0).max(sr[0]), sr[1] - 1.0, ox, sr[3] + 1.0], | |
| 373 | color: bracket_col, | |
| 374 | }); | |
| 375 | out.push(Primitive::Rect { | |
| 376 | rect: [(ox - 6.0).max(sr[0]), sr[1] - 1.0, ox, sr[1] + 1.0], | |
| 377 | color: bracket_col, | |
| 378 | }); | |
| 379 | out.push(Primitive::Rect { | |
| 380 | rect: [(ox - 6.0).max(sr[0]), sr[3] - 1.0, ox, sr[3] + 1.0], | |
| 381 | color: bracket_col, | |
| 382 | }); | |
| 383 | } | |
| 384 | ||
| 385 | let frac = ((current_frame - r0) / span).clamp(0.0, 1.0) as f32; | |
| 386 | let px = sr[0] + frac * bar_w; | |
| 387 | out.push(Primitive::RoundedRect { | |
| 388 | rect: [sr[0], sr[1], px.max(sr[0] + 2.0), sr[3]], | |
| 389 | radius: 4.0, | |
| 390 | stroke: None, | |
| 391 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.25], | |
| 392 | }); | |
| 393 | out.push(Primitive::RoundedRect { | |
| 394 | rect: [px - 3.0, sr[1] - 2.0, px + 3.0, sr[3] + 2.0], | |
| 395 | radius: 2.0, | |
| 396 | stroke: None, | |
| 397 | color: [0.98, 0.40, 0.36, 1.0], | |
| 398 | }); | |
| 399 | ||
| 400 | // Knife-styled right-click floating context menu | |
| 401 | if let Some(anchor) = self.context_menu { | |
| 402 | let mr = self.context_menu_rect(anchor); | |
| 403 | // Soft drop shadow | |
| 404 | out.push(Primitive::RoundedRect { | |
| 405 | rect: [mr[0] + 2.0, mr[1] + 4.0, mr[2] + 2.0, mr[3] + 4.0], | |
| 406 | radius: 8.0, | |
| 407 | stroke: None, | |
| 408 | color: [0.0, 0.0, 0.0, 0.38], | |
| 409 | }); | |
| 410 | let bg = self.palette.bar; | |
| 411 | let panel_fill = [ | |
| 412 | (bg[0] + 0.03).min(1.0), | |
| 413 | (bg[1] + 0.03).min(1.0), | |
| 414 | (bg[2] + 0.035).min(1.0), | |
| 415 | 0.97, | |
| 416 | ]; | |
| 417 | out.push(Primitive::RoundedRect { | |
| 418 | rect: mr, | |
| 419 | radius: 7.0, | |
| 420 | stroke: None, | |
| 421 | color: panel_fill, | |
| 422 | }); | |
| 423 | out.push(Primitive::RoundedRect { | |
| 424 | rect: mr, | |
| 425 | radius: 7.0, | |
| 426 | stroke: Some(1.0), | |
| 427 | color: self.palette.border, | |
| 428 | }); | |
| 429 | ||
| 430 | // Hairline separator after export actions | |
| 431 | let sep_y = mr[1] | |
| 432 | + CONTEXT_MENU_PAD_Y | |
| 433 | + (CONTEXT_MENU_SEP_AFTER + 1) as f32 * CONTEXT_MENU_ITEM_H | |
| 434 | + CONTEXT_MENU_SEP_H * 0.5; | |
| 435 | out.push(Primitive::Rect { | |
| 436 | rect: [mr[0] + 8.0, sep_y - 0.5, mr[2] - 8.0, sep_y + 0.5], | |
| 437 | color: self.palette.border, | |
| 438 | }); | |
| 439 | ||
| 440 | let acc = self.palette.accent; | |
| 441 | for (idx, layout) in self.menu_item_layouts.iter().enumerate() { | |
| 442 | let item_r = self.context_menu_item_rect(mr, idx); | |
| 443 | if self.context_menu_hover == Some(idx) { | |
| 444 | out.push(Primitive::RoundedRect { | |
| 445 | rect: item_r, | |
| 446 | radius: 4.0, | |
| 447 | stroke: None, | |
| 448 | color: [acc[0], acc[1], acc[2], 0.24], | |
| 449 | }); | |
| 450 | } | |
| 451 | out.push(Primitive::Text { | |
| 452 | text: layout as &dyn Glyphs, | |
| 453 | origin: [item_r[0] + 10.0, item_r[1] + 5.0], | |
| 454 | clip: Some([item_r[0] + 4.0, item_r[1], item_r[2] - 4.0, item_r[3]]), | |
| 455 | ink: self.palette.text, | |
| 456 | }); | |
| 457 | } | |
| 458 | } | |
| 459 | ||
| 460 | out | |
| 461 | } | |
| 462 | } | |
| 463 | ||
| 464 | /// Sleek Knife-style compact toolbar control (soft tinted surface + hairline border, | |
| 465 | /// accent tint when active — replaces the chunky generic `ui::button`). | |
| 466 | fn tool_btn(ui: &mut ui::Ui, part: impl std::hash::Hash, label: &str, active: bool) -> ui::Signal { | |
| 467 | let t = &ui.theme; | |
| 468 | let fill = if active { | |
| 469 | [t.accent[0], t.accent[1], t.accent[2], 0.24] | |
| 470 | } else { | |
| 471 | [t.base[0], t.base[1], t.base[2], 0.65] | |
| 472 | }; | |
| 473 | let border = if active { | |
| 474 | [t.accent[0], t.accent[1], t.accent[2], 0.65] | |
| 475 | } else { | |
| 476 | t.rule | |
| 477 | }; | |
| 478 | let ink = if active { t.text } else { t.text_dim }; | |
| 479 | ui.leaf( | |
| 480 | part, | |
| 481 | ui::Spec { | |
| 482 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 483 | flags: ui::Flags::CLICKABLE, | |
| 484 | size: [ui::fit(), ui::px(22.0)], | |
| 485 | text: Some(label), | |
| 486 | font_size: Some(11.5), | |
| 487 | medium: true, | |
| 488 | color: Some(ink), | |
| 489 | fill: Some(fill), | |
| 490 | border: Some(border), | |
| 491 | hover_fill: Some(t.ghost_soft), | |
| 492 | hover_border: Some(t.accent), | |
| 493 | radius: 5.0, | |
| 494 | pad: [7.0, 0.0], | |
| 495 | center: true, | |
| 496 | ..ui::Spec::default() | |
| 497 | }, | |
| 498 | ) | |
| 499 | } |