| author | |
| committer | |
| log | 518f2c643c33dd2bc47ae03dc746b478d8e40892 |
| tree | 83399337203907d52f3c543baa4ceff8374b13b8 |
| parent | 7de1736f7facb72ef19b0be821d574d3f7903bea |
| signature | Signed by SSH key SHA256:52mNGHRsVFBDED9IAX5pe+LRWUefqTbxEReunq21QvU |
13 files changed, 5340 insertions(+), 5308 deletions(-)
Cargo.lock+27-26| ... | ... | @@ -190,6 +190,32 @@ dependencies = [ |
| 190 | 190 | "libc", |
| 191 | 191 | ] |
| 192 | 192 | |
| 193 | [[package]] | |
| 194 | name = "app" | |
| 195 | version = "0.1.0" | |
| 196 | dependencies = [ | |
| 197 | "accesskit", | |
| 198 | "accesskit_winit", | |
| 199 | "dock", | |
| 200 | "draw", | |
| 201 | "engine", | |
| 202 | "flow", | |
| 203 | "model", | |
| 204 | "objc2 0.6.5", | |
| 205 | "objc2-app-kit 0.3.2", | |
| 206 | "objc2-foundation 0.3.2", | |
| 207 | "png", | |
| 208 | "pollster", | |
| 209 | "spline", | |
| 210 | "state", | |
| 211 | "text", | |
| 212 | "theme", | |
| 213 | "ui", | |
| 214 | "viewer", | |
| 215 | "wgpu", | |
| 216 | "winit", | |
| 217 | ] | |
| 218 | ||
| 193 | 219 | [[package]] |
| 194 | 220 | name = "arboard" |
| 195 | 221 | version = "3.7.0" |
| ... | ... | @@ -3242,32 +3268,6 @@ dependencies = [ |
| 3242 | 3268 | "float-cmp", |
| 3243 | 3269 | ] |
| 3244 | 3270 | |
| 3245 | [[package]] | |
| 3246 | name = "studio" | |
| 3247 | version = "0.1.0" | |
| 3248 | dependencies = [ | |
| 3249 | "accesskit", | |
| 3250 | "accesskit_winit", | |
| 3251 | "dock", | |
| 3252 | "draw", | |
| 3253 | "engine", | |
| 3254 | "flow", | |
| 3255 | "model", | |
| 3256 | "objc2 0.6.5", | |
| 3257 | "objc2-app-kit 0.3.2", | |
| 3258 | "objc2-foundation 0.3.2", | |
| 3259 | "png", | |
| 3260 | "pollster", | |
| 3261 | "spline", | |
| 3262 | "state", | |
| 3263 | "text", | |
| 3264 | "theme", | |
| 3265 | "ui", | |
| 3266 | "viewer", | |
| 3267 | "wgpu", | |
| 3268 | "winit", | |
| 3269 | ] | |
| 3270 | ||
| 3271 | 3271 | [[package]] |
| 3272 | 3272 | name = "svgtypes" |
| 3273 | 3273 | version = "0.16.2" |
| ... | ... | @@ -3503,6 +3503,7 @@ version = "0.1.0" |
| 3503 | 3503 | dependencies = [ |
| 3504 | 3504 | "diff", |
| 3505 | 3505 | "draw", |
| 3506 | "flate2", | |
| 3506 | 3507 | "highlight", |
| 3507 | 3508 | "memmap2", |
| 3508 | 3509 | "text", |
Cargo.toml+1-1| ... | ... | @@ -5,7 +5,7 @@ members = [ |
| 5 | 5 | "crates/flow", |
| 6 | 6 | "crates/spline", |
| 7 | 7 | "crates/viewer", |
| 8 | "crates/studio", | |
| 8 | "crates/app", | |
| 9 | 9 | ] |
| 10 | 10 | resolver = "3" |
| 11 | 11 |
crates/app/Cargo.toml created+33| ... | ... | @@ -0,0 +1,33 @@ |
| 1 | [package] | |
| 2 | name = "app" | |
| 3 | edition.workspace = true | |
| 4 | version.workspace = true | |
| 5 | publish.workspace = true | |
| 6 | ||
| 7 | [[bin]] | |
| 8 | name = "fission" | |
| 9 | path = "src/main.rs" | |
| 10 | ||
| 11 | [dependencies] | |
| 12 | accesskit.workspace = true | |
| 13 | accesskit_winit.workspace = true | |
| 14 | dock.workspace = true | |
| 15 | draw.workspace = true | |
| 16 | engine.workspace = true | |
| 17 | flow.workspace = true | |
| 18 | model.workspace = true | |
| 19 | png.workspace = true | |
| 20 | pollster.workspace = true | |
| 21 | spline.workspace = true | |
| 22 | state.workspace = true | |
| 23 | text.workspace = true | |
| 24 | theme.workspace = true | |
| 25 | ui.workspace = true | |
| 26 | viewer.workspace = true | |
| 27 | wgpu.workspace = true | |
| 28 | winit.workspace = true | |
| 29 | ||
| 30 | [target.'cfg(target_os = "macos")'.dependencies] | |
| 31 | objc2.workspace = true | |
| 32 | objc2-app-kit = { workspace = true, features = ["std", "NSWorkspace", "NSAccessibility", "NSApplication", "NSResponder", "NSMenu", "NSMenuItem"] } | |
| 33 | objc2-foundation = { workspace = true, features = ["std", "NSString"] } |
crates/app/src/add_tool.rs created+557| ... | ... | @@ -0,0 +1,557 @@ |
| 1 | //! `Shift+Space` Add Tool fuzzy-search launcher modal overlay. | |
| 2 | ||
| 3 | use winit::keyboard::{Key, NamedKey}; | |
| 4 | ||
| 5 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 6 | pub enum ToolCategory { | |
| 7 | Generator, | |
| 8 | Composite, | |
| 9 | Color, | |
| 10 | Keyer, | |
| 11 | BlurGlow, | |
| 12 | TransformWarp, | |
| 13 | Mask, | |
| 14 | Particle, | |
| 15 | Scene3D, | |
| 16 | Utility, | |
| 17 | } | |
| 18 | ||
| 19 | impl ToolCategory { | |
| 20 | pub fn label(self) -> &'static str { | |
| 21 | match self { | |
| 22 | Self::Generator => "Generator", | |
| 23 | Self::Composite => "Composite", | |
| 24 | Self::Color => "Color", | |
| 25 | Self::Keyer => "Keyer", | |
| 26 | Self::BlurGlow => "Blur/Glow", | |
| 27 | Self::TransformWarp => "Transform/Warp", | |
| 28 | Self::Mask => "Mask", | |
| 29 | Self::Particle => "Particle", | |
| 30 | Self::Scene3D => "3D", | |
| 31 | Self::Utility => "Utility", | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | pub fn color(self) -> [f32; 4] { | |
| 36 | match self { | |
| 37 | Self::Generator => [0.36, 0.82, 0.48, 1.0], | |
| 38 | Self::Composite => [0.68, 0.72, 0.82, 1.0], | |
| 39 | Self::Color => [0.94, 0.62, 0.24, 1.0], | |
| 40 | Self::Keyer => [0.30, 0.84, 0.64, 1.0], | |
| 41 | Self::BlurGlow => [0.94, 0.50, 0.28, 1.0], | |
| 42 | Self::TransformWarp => [0.88, 0.72, 0.28, 1.0], | |
| 43 | Self::Mask => [0.34, 0.66, 0.98, 1.0], | |
| 44 | Self::Particle => [0.88, 0.46, 0.78, 1.0], | |
| 45 | Self::Scene3D => [0.30, 0.86, 0.92, 1.0], | |
| 46 | Self::Utility => [0.56, 0.62, 0.72, 1.0], | |
| 47 | } | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 52 | pub struct ToolEntry { | |
| 53 | pub kind: &'static str, | |
| 54 | pub display: &'static str, | |
| 55 | pub aliases: &'static str, | |
| 56 | pub category: ToolCategory, | |
| 57 | pub is_generator: bool, | |
| 58 | } | |
| 59 | ||
| 60 | pub const TOOL_CATALOG: &[ToolEntry] = &[ | |
| 61 | // Generator | |
| 62 | ToolEntry { kind: "Background", display: "Background", aliases: "bg solid color", category: ToolCategory::Generator, is_generator: true }, | |
| 63 | ToolEntry { kind: "FastNoise", display: "FastNoise", aliases: "fn noise fractal perlin", category: ToolCategory::Generator, is_generator: true }, | |
| 64 | ToolEntry { kind: "Plasma", display: "Plasma", aliases: "gradient organic", category: ToolCategory::Generator, is_generator: true }, | |
| 65 | ToolEntry { kind: "TextPlus", display: "Text+", aliases: "textplus title font", category: ToolCategory::Generator, is_generator: true }, | |
| 66 | ToolEntry { kind: "Loader", display: "Loader", aliases: "ld read media footage image", category: ToolCategory::Generator, is_generator: true }, | |
| 67 | // Composite | |
| 68 | ToolEntry { kind: "Merge", display: "Merge", aliases: "mrg over blend comp", category: ToolCategory::Composite, is_generator: false }, | |
| 69 | ToolEntry { kind: "Dissolve", display: "Dissolve", aliases: "dx mix crossfade", category: ToolCategory::Composite, is_generator: false }, | |
| 70 | ToolEntry { kind: "ChannelBoolean", display: "Channel Booleans", aliases: "cb channel matte", category: ToolCategory::Composite, is_generator: false }, | |
| 71 | ToolEntry { kind: "Switch", display: "Switch", aliases: "sw select", category: ToolCategory::Composite, is_generator: false }, | |
| 72 | ToolEntry { kind: "Shadow", display: "Shadow", aliases: "drop shadow", category: ToolCategory::Composite, is_generator: false }, | |
| 73 | // Color | |
| 74 | ToolEntry { kind: "BrightnessContrast", display: "Brightness / Contrast", aliases: "bc gain lift gamma", category: ToolCategory::Color, is_generator: false }, | |
| 75 | ToolEntry { kind: "ColorCorrector", display: "Color Corrector", aliases: "cc grade wheel saturation", category: ToolCategory::Color, is_generator: false }, | |
| 76 | ToolEntry { kind: "ColorGain", display: "Color Gain", aliases: "cg rgb gain", category: ToolCategory::Color, is_generator: false }, | |
| 77 | ToolEntry { kind: "ColorCurves", display: "Color Curves", aliases: "curves lut", category: ToolCategory::Color, is_generator: false }, | |
| 78 | ToolEntry { kind: "HueCurves", display: "Hue Curves", aliases: "hue sat", category: ToolCategory::Color, is_generator: false }, | |
| 79 | ToolEntry { kind: "GamutConvert", display: "Gamut", aliases: "colorspace srgb linear", category: ToolCategory::Color, is_generator: false }, | |
| 80 | ToolEntry { kind: "InvertColor", display: "Invert Color", aliases: "negate invert", category: ToolCategory::Color, is_generator: false }, | |
| 81 | ToolEntry { kind: "FilmGrain", display: "Film Grain", aliases: "grain noise", category: ToolCategory::Color, is_generator: false }, | |
| 82 | ToolEntry { kind: "Fuse.Colorize", display: "Colorize", aliases: "tint duotone", category: ToolCategory::Color, is_generator: false }, | |
| 83 | ToolEntry { kind: "Fuse.Outline", display: "Outline", aliases: "stroke edge", category: ToolCategory::Color, is_generator: false }, | |
| 84 | // Keyer | |
| 85 | ToolEntry { kind: "UltraKeyer", display: "Ultra Keyer", aliases: "uk chroma green blue screen", category: ToolCategory::Keyer, is_generator: false }, | |
| 86 | ToolEntry { kind: "LumaKeyer", display: "Luma Keyer", aliases: "lk luminance key", category: ToolCategory::Keyer, is_generator: false }, | |
| 87 | ToolEntry { kind: "Cryptomatte", display: "Cryptomatte", aliases: "crypto id matte", category: ToolCategory::Keyer, is_generator: false }, | |
| 88 | // Blur / Glow | |
| 89 | ToolEntry { kind: "Blur", display: "Blur", aliases: "bl gaussian defocus", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 90 | ToolEntry { kind: "DirectionalBlur", display: "Directional Blur", aliases: "db radial zoom motion blur", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 91 | ToolEntry { kind: "VariBlur", display: "VariBlur", aliases: "vb depth blur", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 92 | ToolEntry { kind: "Glow", display: "Glow", aliases: "gl bloom", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 93 | ToolEntry { kind: "SoftGlow", display: "Soft Glow", aliases: "sg bloom halo", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 94 | ToolEntry { kind: "Fuse.XGlow", display: "XGlow", aliases: "exponential glow", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 95 | ToolEntry { kind: "Sharpen", display: "Sharpen", aliases: "unsharp crisp", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 96 | ToolEntry { kind: "Filter", display: "Filter", aliases: "sobel emboss laplacian", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 97 | ToolEntry { kind: "ErodeDilate", display: "Erode / Dilate", aliases: "ed shrink grow matte", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 98 | // Transform / Warp | |
| 99 | ToolEntry { kind: "Transform", display: "Transform", aliases: "xf move scale rotate", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 100 | ToolEntry { kind: "CameraShake", display: "Camera Shake", aliases: "shake jitter", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 101 | ToolEntry { kind: "CornerPositioner", display: "Corner Positioner", aliases: "cp pin perspective", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 102 | ToolEntry { kind: "DVE", display: "DVE", aliases: "3d card tilt", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 103 | ToolEntry { kind: "Scale", display: "Scale", aliases: "resize", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 104 | ToolEntry { kind: "BetterResize", display: "Resize", aliases: "rs scale format", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 105 | ToolEntry { kind: "Crop", display: "Crop", aliases: "trim canvas", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 106 | ToolEntry { kind: "Displace", display: "Displace", aliases: "dsp refract warp bump", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 107 | ToolEntry { kind: "Vortex", display: "Vortex", aliases: "swirl twist", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 108 | ToolEntry { kind: "Fuse.XfChroma", display: "XfChroma", aliases: "chromatic aberration rgb split", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 109 | // Mask | |
| 110 | ToolEntry { kind: "EllipseMask", display: "Ellipse Mask", aliases: "el circle oval", category: ToolCategory::Mask, is_generator: true }, | |
| 111 | ToolEntry { kind: "RectangleMask", display: "Rectangle Mask", aliases: "rect box square", category: ToolCategory::Mask, is_generator: true }, | |
| 112 | ToolEntry { kind: "PolylineMask", display: "Polygon Mask", aliases: "poly roto spline", category: ToolCategory::Mask, is_generator: true }, | |
| 113 | ToolEntry { kind: "BitmapMask", display: "Bitmap Mask", aliases: "bmp alpha luma", category: ToolCategory::Mask, is_generator: false }, | |
| 114 | // Particle | |
| 115 | ToolEntry { kind: "pEmitter", display: "pEmitter", aliases: "pe particle emit", category: ToolCategory::Particle, is_generator: true }, | |
| 116 | ToolEntry { kind: "pRender", display: "pRender", aliases: "pr particle render", category: ToolCategory::Particle, is_generator: false }, | |
| 117 | ToolEntry { kind: "pTurbulence", display: "pTurbulence", aliases: "pt particle wind", category: ToolCategory::Particle, is_generator: false }, | |
| 118 | ToolEntry { kind: "pDirectionalForce", display: "pDirectionalForce", aliases: "pf gravity force", category: ToolCategory::Particle, is_generator: false }, | |
| 119 | ToolEntry { kind: "pPointForce", display: "pPointForce", aliases: "particle attract", category: ToolCategory::Particle, is_generator: false }, | |
| 120 | ToolEntry { kind: "pDrag", display: "pDrag", aliases: "particle damping", category: ToolCategory::Particle, is_generator: false }, | |
| 121 | ToolEntry { kind: "pFriction", display: "pFriction", aliases: "particle friction", category: ToolCategory::Particle, is_generator: false }, | |
| 122 | ToolEntry { kind: "pKill", display: "pKill", aliases: "particle kill", category: ToolCategory::Particle, is_generator: false }, | |
| 123 | ToolEntry { kind: "pMerge", display: "pMerge", aliases: "particle merge", category: ToolCategory::Particle, is_generator: false }, | |
| 124 | // 3D | |
| 125 | ToolEntry { kind: "Renderer3D", display: "Renderer 3D", aliases: "3rn render 3d", category: ToolCategory::Scene3D, is_generator: false }, | |
| 126 | ToolEntry { kind: "Merge3D", display: "Merge 3D", aliases: "3mg scene 3d", category: ToolCategory::Scene3D, is_generator: false }, | |
| 127 | ToolEntry { kind: "Camera3D", display: "Camera 3D", aliases: "3cm cam", category: ToolCategory::Scene3D, is_generator: true }, | |
| 128 | ToolEntry { kind: "Shape3D", display: "Shape 3D", aliases: "3sh sphere plane cube", category: ToolCategory::Scene3D, is_generator: true }, | |
| 129 | ToolEntry { kind: "ImagePlane3D", display: "Image Plane 3D", aliases: "3im card plane", category: ToolCategory::Scene3D, is_generator: false }, | |
| 130 | ToolEntry { kind: "Transform3D", display: "Transform 3D", aliases: "3xf move 3d", category: ToolCategory::Scene3D, is_generator: false }, | |
| 131 | ToolEntry { kind: "LightAmbient", display: "Ambient Light", aliases: "3la light", category: ToolCategory::Scene3D, is_generator: true }, | |
| 132 | ToolEntry { kind: "LightDirectional", display: "Directional Light", aliases: "3dl sun light", category: ToolCategory::Scene3D, is_generator: true }, | |
| 133 | // Utility | |
| 134 | ToolEntry { kind: "Saver", display: "Saver", aliases: "sv write export png", category: ToolCategory::Utility, is_generator: false }, | |
| 135 | ToolEntry { kind: "PipeRouter", display: "Pipe Router", aliases: "dot router wire", category: ToolCategory::Utility, is_generator: false }, | |
| 136 | ToolEntry { kind: "TimeStretcher", display: "Time Stretcher", aliases: "ts retime freeze", category: ToolCategory::Utility, is_generator: false }, | |
| 137 | ToolEntry { kind: "TimeSpeed", display: "Time Speed", aliases: "tsp speed delay", category: ToolCategory::Utility, is_generator: false }, | |
| 138 | ToolEntry { kind: "ChangeDepth", display: "Change Depth", aliases: "cd bit depth float", category: ToolCategory::Utility, is_generator: false }, | |
| 139 | ToolEntry { kind: "SetDomain", display: "Set Domain", aliases: "dod roi", category: ToolCategory::Utility, is_generator: false }, | |
| 140 | ToolEntry { kind: "Tracker", display: "Tracker", aliases: "tr matchmove", category: ToolCategory::Utility, is_generator: false }, | |
| 141 | ToolEntry { kind: "Note", display: "Sticky Note", aliases: "comment note", category: ToolCategory::Utility, is_generator: true }, | |
| 142 | ToolEntry { kind: "Underlay", display: "Underlay", aliases: "backdrop group", category: ToolCategory::Utility, is_generator: true }, | |
| 143 | ]; | |
| 144 | ||
| 145 | /// Case-insensitive fuzzy/subsequence scoring for tool search. | |
| 146 | /// Higher score is better; returns `None` if `query` does not match `candidate`. | |
| 147 | pub fn fuzzy_score(query: &str, entry: &ToolEntry) -> Option<i32> { | |
| 148 | let q = query.trim().to_ascii_lowercase(); | |
| 149 | if q.is_empty() { | |
| 150 | return Some(0); | |
| 151 | } | |
| 152 | let kind_l = entry.kind.to_ascii_lowercase(); | |
| 153 | let disp_l = entry.display.to_ascii_lowercase(); | |
| 154 | let alias_l = entry.aliases.to_ascii_lowercase(); | |
| 155 | let cat_l = entry.category.label().to_ascii_lowercase(); | |
| 156 | ||
| 157 | let mut best: Option<i32> = None; | |
| 158 | for (field, bonus) in [(&kind_l, 40), (&disp_l, 35), (&alias_l, 15), (&cat_l, 0)] { | |
| 159 | if let Some(s) = score_field(&q, field) { | |
| 160 | let total = s + bonus; | |
| 161 | best = Some(best.map_or(total, |prev| prev.max(total))); | |
| 162 | } | |
| 163 | } | |
| 164 | best | |
| 165 | } | |
| 166 | ||
| 167 | fn score_field(query: &str, target: &str) -> Option<i32> { | |
| 168 | if target == query { | |
| 169 | return Some(1000); | |
| 170 | } | |
| 171 | if target.starts_with(query) { | |
| 172 | return Some(850 - (target.len() as i32 - query.len() as i32)); | |
| 173 | } | |
| 174 | if let Some(idx) = target.find(query) { | |
| 175 | let boundary_bonus = if idx == 0 || matches!(target.as_bytes()[idx - 1], b' ' | b'.' | b'/' | b'_') { | |
| 176 | 120 | |
| 177 | } else { | |
| 178 | 0 | |
| 179 | }; | |
| 180 | return Some(620 + boundary_bonus - idx as i32 * 4); | |
| 181 | } | |
| 182 | ||
| 183 | // Subsequence match with consecutive-run & word-boundary bonuses | |
| 184 | let q_bytes = query.as_bytes(); | |
| 185 | let t_bytes = target.as_bytes(); | |
| 186 | let mut qi = 0usize; | |
| 187 | let mut score = 200i32; | |
| 188 | let mut prev_match: Option<usize> = None; | |
| 189 | ||
| 190 | for (ti, &tb) in t_bytes.iter().enumerate() { | |
| 191 | if qi < q_bytes.len() && tb == q_bytes[qi] { | |
| 192 | if prev_match.is_some_and(|p| p + 1 == ti) { | |
| 193 | score += 28; | |
| 194 | } else if let Some(p) = prev_match { | |
| 195 | score -= ((ti - p) as i32).min(20); | |
| 196 | } | |
| 197 | if ti == 0 || matches!(t_bytes[ti - 1], b' ' | b'.' | b'/' | b'_') { | |
| 198 | score += 22; | |
| 199 | } | |
| 200 | prev_match = Some(ti); | |
| 201 | qi += 1; | |
| 202 | } | |
| 203 | } | |
| 204 | ||
| 205 | if qi == q_bytes.len() { | |
| 206 | Some(score - (t_bytes.len() as i32)) | |
| 207 | } else { | |
| 208 | None | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | /// Returns matching tool entries ranked from best to worst. | |
| 213 | pub fn search_tools(query: &str) -> Vec<&'static ToolEntry> { | |
| 214 | let mut scored: Vec<(i32, usize, &'static ToolEntry)> = TOOL_CATALOG | |
| 215 | .iter() | |
| 216 | .enumerate() | |
| 217 | .filter_map(|(idx, entry)| fuzzy_score(query, entry).map(|s| (s, idx, entry))) | |
| 218 | .collect(); | |
| 219 | scored.sort_by(|a, b| b.0.cmp(&a.0).then_with(|| a.1.cmp(&b.1))); | |
| 220 | scored.into_iter().map(|(_, _, e)| e).collect() | |
| 221 | } | |
| 222 | ||
| 223 | pub struct AddToolLauncher { | |
| 224 | pub open: bool, | |
| 225 | pub query: String, | |
| 226 | pub selected_idx: usize, | |
| 227 | needs_focus: bool, | |
| 228 | } | |
| 229 | ||
| 230 | impl Default for AddToolLauncher { | |
| 231 | fn default() -> Self { | |
| 232 | Self::new() | |
| 233 | } | |
| 234 | } | |
| 235 | ||
| 236 | impl AddToolLauncher { | |
| 237 | pub fn new() -> Self { | |
| 238 | Self { | |
| 239 | open: false, | |
| 240 | query: String::new(), | |
| 241 | selected_idx: 0, | |
| 242 | needs_focus: false, | |
| 243 | } | |
| 244 | } | |
| 245 | ||
| 246 | pub fn open(&mut self) { | |
| 247 | self.open = true; | |
| 248 | self.query.clear(); | |
| 249 | self.selected_idx = 0; | |
| 250 | self.needs_focus = true; | |
| 251 | } | |
| 252 | ||
| 253 | pub fn close(&mut self) { | |
| 254 | self.open = false; | |
| 255 | self.needs_focus = false; | |
| 256 | } | |
| 257 | ||
| 258 | /// Builds the floating modal overlay when open. Returns `Some(ToolEntry)` if a tool was chosen. | |
| 259 | pub fn build_ui(&mut self, ui: &mut ui::Ui, window_size: [f32; 2]) -> Option<ToolEntry> { | |
| 260 | if !self.open { | |
| 261 | return None; | |
| 262 | } | |
| 263 | ||
| 264 | let theme = ui.theme.clone(); | |
| 265 | let results = search_tools(&self.query); | |
| 266 | if self.selected_idx >= results.len() { | |
| 267 | self.selected_idx = 0; | |
| 268 | } | |
| 269 | ||
| 270 | let modal_id = ui::Id::ROOT.child("add-tool-modal"); | |
| 271 | let input_id = modal_id.child("query"); | |
| 272 | ||
| 273 | // Handle Up / Down / Enter / Escape from the search field before rendering rows | |
| 274 | let mut picked: Option<ToolEntry> = None; | |
| 275 | let mut should_close = false; | |
| 276 | ||
| 277 | let scrim_id = ui::Id::ROOT.child("add-tool-scrim"); | |
| 278 | let scrim_sig = ui.signal(scrim_id); | |
| 279 | if scrim_sig.clicked { | |
| 280 | should_close = true; | |
| 281 | } | |
| 282 | ||
| 283 | // Full-window barrier scrim so clicks behind modal don't leak | |
| 284 | ui.open_as( | |
| 285 | scrim_id, | |
| 286 | ui::Spec { | |
| 287 | flags: ui::Flags::FLOAT | ui::Flags::BARRIER | ui::Flags::CLICKABLE, | |
| 288 | position: [0.0, 0.0], | |
| 289 | size: [ui::px(window_size[0]), ui::px(window_size[1])], | |
| 290 | fill: Some([0.0, 0.0, 0.0, 0.42]), | |
| 291 | ..ui::Spec::default() | |
| 292 | }, | |
| 293 | ); | |
| 294 | ui.close(); | |
| 295 | ||
| 296 | let modal_w = 460.0_f32.min((window_size[0] - 40.0).max(260.0)); | |
| 297 | let modal_h = 380.0_f32.min((window_size[1] - 80.0).max(220.0)); | |
| 298 | let modal_x = ((window_size[0] - modal_w) * 0.5).max(16.0); | |
| 299 | let modal_y = (window_size[1] * 0.14).max(24.0); | |
| 300 | ||
| 301 | ui.open_as( | |
| 302 | modal_id, | |
| 303 | ui::Spec { | |
| 304 | flags: ui::Flags::FLOAT | ui::Flags::BARRIER | ui::Flags::CLICKABLE | ui::Flags::CLIP, | |
| 305 | position: [modal_x, modal_y], | |
| 306 | size: [ui::px(modal_w), ui::px(modal_h)], | |
| 307 | axis: ui::Axis::Y, | |
| 308 | fill: Some(theme.popup), | |
| 309 | border: Some(theme.accent), | |
| 310 | shadow: Some([0.0, 0.0, 0.0, 0.45]), | |
| 311 | radius: 10.0, | |
| 312 | pad: [10.0, 10.0], | |
| 313 | gap: 8.0, | |
| 314 | ..ui::Spec::default() | |
| 315 | }, | |
| 316 | ); | |
| 317 | ||
| 318 | // Header | |
| 319 | ui.open( | |
| 320 | "hdr", | |
| 321 | ui::Spec { | |
| 322 | size: [ui::fill(), ui::px(20.0)], | |
| 323 | axis: ui::Axis::X, | |
| 324 | gap: 6.0, | |
| 325 | ..ui::Spec::default() | |
| 326 | }, | |
| 327 | ); | |
| 328 | ui.leaf( | |
| 329 | "title", | |
| 330 | ui::Spec { | |
| 331 | size: [ui::fill(), ui::fill()], | |
| 332 | text: Some("Add Tool"), | |
| 333 | font_size: Some(12.5), | |
| 334 | medium: true, | |
| 335 | color: Some(theme.text), | |
| 336 | ..ui::Spec::default() | |
| 337 | }, | |
| 338 | ); | |
| 339 | ui.leaf( | |
| 340 | "hint", | |
| 341 | ui::Spec { | |
| 342 | size: [ui::fit(), ui::fill()], | |
| 343 | text: Some("↑↓ select · Enter add · Esc close"), | |
| 344 | font_size: Some(10.0), | |
| 345 | color: Some(theme.text_dim), | |
| 346 | ..ui::Spec::default() | |
| 347 | }, | |
| 348 | ); | |
| 349 | ui.close(); | |
| 350 | ||
| 351 | // Search input | |
| 352 | let prev_query = self.query.clone(); | |
| 353 | let input_sig = ui::text_field( | |
| 354 | ui, | |
| 355 | input_id, | |
| 356 | &mut self.query, | |
| 357 | "Type tool name (e.g. Merge, Blur, BC, xf, Text+)…", | |
| 358 | ui::Spec { | |
| 359 | size: [ui::fill(), ui::px(30.0)], | |
| 360 | font_size: Some(13.0), | |
| 361 | fill: Some(theme.base), | |
| 362 | border: Some(theme.accent), | |
| 363 | radius: 6.0, | |
| 364 | pad: [10.0, 4.0], | |
| 365 | ..ui::Spec::default() | |
| 366 | }, | |
| 367 | ); | |
| 368 | if self.needs_focus { | |
| 369 | ui.set_focus(Some(input_id)); | |
| 370 | self.needs_focus = false; | |
| 371 | } | |
| 372 | if self.query != prev_query { | |
| 373 | self.selected_idx = 0; | |
| 374 | } | |
| 375 | ||
| 376 | for ev in &input_sig.events { | |
| 377 | if let ui::Event::Key { key, .. } = ev { | |
| 378 | match key { | |
| 379 | Key::Named(NamedKey::Escape) => should_close = true, | |
| 380 | Key::Named(NamedKey::ArrowDown) => { | |
| 381 | if !results.is_empty() { | |
| 382 | self.selected_idx = (self.selected_idx + 1) % results.len(); | |
| 383 | } | |
| 384 | } | |
| 385 | Key::Named(NamedKey::ArrowUp) => { | |
| 386 | if !results.is_empty() { | |
| 387 | self.selected_idx = (self.selected_idx + results.len() - 1) % results.len(); | |
| 388 | } | |
| 389 | } | |
| 390 | Key::Named(NamedKey::Enter) => { | |
| 391 | if let Some(&entry) = results.get(self.selected_idx) { | |
| 392 | picked = Some(*entry); | |
| 393 | should_close = true; | |
| 394 | } | |
| 395 | } | |
| 396 | _ => {} | |
| 397 | } | |
| 398 | } | |
| 399 | } | |
| 400 | ||
| 401 | // Scrollable results list | |
| 402 | ui.open( | |
| 403 | "results", | |
| 404 | ui::Spec { | |
| 405 | flags: ui::Flags::SCROLL | ui::Flags::CLIP, | |
| 406 | size: [ui::fill(), ui::fill()], | |
| 407 | axis: ui::Axis::Y, | |
| 408 | gap: 2.0, | |
| 409 | ..ui::Spec::default() | |
| 410 | }, | |
| 411 | ); | |
| 412 | if results.is_empty() { | |
| 413 | ui.leaf( | |
| 414 | "no-match", | |
| 415 | ui::Spec { | |
| 416 | size: [ui::fill(), ui::px(28.0)], | |
| 417 | text: Some("No matching tools"), | |
| 418 | font_size: Some(11.5), | |
| 419 | color: Some(theme.text_dim), | |
| 420 | center: true, | |
| 421 | ..ui::Spec::default() | |
| 422 | }, | |
| 423 | ); | |
| 424 | } else { | |
| 425 | for (idx, entry) in results.iter().enumerate() { | |
| 426 | let is_sel = idx == self.selected_idx; | |
| 427 | let cat_col = entry.category.color(); | |
| 428 | let row_id = ui.open( | |
| 429 | ("row", entry.kind), | |
| 430 | ui::Spec { | |
| 431 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 432 | flags: ui::Flags::CLICKABLE, | |
| 433 | size: [ui::fill(), ui::px(26.0)], | |
| 434 | axis: ui::Axis::X, | |
| 435 | pad: [8.0, 2.0], | |
| 436 | gap: 8.0, | |
| 437 | fill: if is_sel { | |
| 438 | Some([theme.accent[0], theme.accent[1], theme.accent[2], 0.22]) | |
| 439 | } else { | |
| 440 | None | |
| 441 | }, | |
| 442 | hover_fill: Some([theme.accent[0], theme.accent[1], theme.accent[2], 0.14]), | |
| 443 | radius: 5.0, | |
| 444 | ..ui::Spec::default() | |
| 445 | }, | |
| 446 | ); | |
| 447 | let row_sig = ui.signal(row_id); | |
| 448 | if row_sig.clicked { | |
| 449 | picked = Some(**entry); | |
| 450 | should_close = true; | |
| 451 | } | |
| 452 | ||
| 453 | // Category color pip | |
| 454 | ui.leaf( | |
| 455 | "pip", | |
| 456 | ui::Spec { | |
| 457 | size: [ui::px(6.0), ui::px(14.0)], | |
| 458 | fill: Some(cat_col), | |
| 459 | radius: 2.0, | |
| 460 | ..ui::Spec::default() | |
| 461 | }, | |
| 462 | ); | |
| 463 | ui.leaf( | |
| 464 | "name", | |
| 465 | ui::Spec { | |
| 466 | size: [ui::fill(), ui::fill()], | |
| 467 | text: Some(entry.display), | |
| 468 | font_size: Some(12.0), | |
| 469 | medium: is_sel, | |
| 470 | color: Some(theme.text), | |
| 471 | ..ui::Spec::default() | |
| 472 | }, | |
| 473 | ); | |
| 474 | ui.leaf( | |
| 475 | "kind", | |
| 476 | ui::Spec { | |
| 477 | size: [ui::fit(), ui::fill()], | |
| 478 | text: Some(entry.kind), | |
| 479 | font_size: Some(10.0), | |
| 480 | color: Some(theme.text_dim), | |
| 481 | ..ui::Spec::default() | |
| 482 | }, | |
| 483 | ); | |
| 484 | ui.leaf( | |
| 485 | "cat", | |
| 486 | ui::Spec { | |
| 487 | size: [ui::fit(), ui::px(18.0)], | |
| 488 | text: Some(entry.category.label()), | |
| 489 | font_size: Some(9.5), | |
| 490 | color: Some(cat_col), | |
| 491 | fill: Some([cat_col[0], cat_col[1], cat_col[2], 0.14]), | |
| 492 | radius: 3.0, | |
| 493 | pad: [5.0, 0.0], | |
| 494 | center: true, | |
| 495 | ..ui::Spec::default() | |
| 496 | }, | |
| 497 | ); | |
| 498 | ui.close(); | |
| 499 | } | |
| 500 | } | |
| 501 | ui.close(); // results | |
| 502 | ui.close(); // modal_id | |
| 503 | ||
| 504 | if should_close { | |
| 505 | self.close(); | |
| 506 | ui.set_focus(None); | |
| 507 | } | |
| 508 | picked | |
| 509 | } | |
| 510 | } | |
| 511 | ||
| 512 | #[cfg(test)] | |
| 513 | mod tests { | |
| 514 | use super::*; | |
| 515 | ||
| 516 | #[test] | |
| 517 | fn empty_query_returns_all_tools() { | |
| 518 | let all = search_tools(""); | |
| 519 | assert_eq!(all.len(), TOOL_CATALOG.len()); | |
| 520 | assert!(all.len() >= 50); | |
| 521 | } | |
| 522 | ||
| 523 | #[test] | |
| 524 | fn exact_and_prefix_rank_above_subsequence() { | |
| 525 | let res = search_tools("blur"); | |
| 526 | assert!(!res.is_empty()); | |
| 527 | assert_eq!(res[0].kind, "Blur"); | |
| 528 | ||
| 529 | let res_mrg = search_tools("merge"); | |
| 530 | assert_eq!(res_mrg[0].kind, "Merge"); | |
| 531 | } | |
| 532 | ||
| 533 | #[test] | |
| 534 | fn alias_and_subsequence_matching_works() { | |
| 535 | // Alias "bc" -> BrightnessContrast | |
| 536 | let res_bc = search_tools("bc"); | |
| 537 | assert!(res_bc.iter().take(3).any(|e| e.kind == "BrightnessContrast")); | |
| 538 | ||
| 539 | // Alias "xf" -> Transform | |
| 540 | let res_xf = search_tools("xf"); | |
| 541 | assert!(res_xf.iter().take(3).any(|e| e.kind == "Transform")); | |
| 542 | ||
| 543 | // Subsequence "tplus" -> TextPlus | |
| 544 | let res_tp = search_tools("tplus"); | |
| 545 | assert!(res_tp.iter().any(|e| e.kind == "TextPlus")); | |
| 546 | ||
| 547 | // Case-insensitive category match | |
| 548 | let res_3d = search_tools("camera"); | |
| 549 | assert!(res_3d.iter().any(|e| e.kind == "Camera3D")); | |
| 550 | } | |
| 551 | ||
| 552 | #[test] | |
| 553 | fn non_matching_query_returns_empty() { | |
| 554 | let res = search_tools("zzzznotatool999"); | |
| 555 | assert!(res.is_empty()); | |
| 556 | } | |
| 557 | } |
crates/app/src/inspector.rs created+1802| ... | ... | @@ -0,0 +1,1802 @@ |
| 1 | //! Inspector dock pane — Fusion 21.1 parameter editor with scrub sliders, Vec2, RGBA color groups, | |
| 2 | //! booleans, FuID enum cyclers, text fields, expression badges, and keyframe diamonds (`â—†`). | |
| 3 | ||
| 4 | use engine::EvalStats; | |
| 5 | use model::{Comp, CompHistory, ExprEngine, NodeId, ParamValue, is_supported_tool}; | |
| 6 | use std::collections::{HashMap, HashSet}; | |
| 7 | use std::time::{Duration, Instant}; | |
| 8 | ||
| 9 | #[derive(Clone, Copy, Debug, PartialEq)] | |
| 10 | pub enum ParamKind { | |
| 11 | Float { min: f64, max: f64, default: f64, step: f64 }, | |
| 12 | Vec2 { min: f64, max: f64, default: [f64; 2] }, | |
| 13 | Bool { default: bool }, | |
| 14 | Enum { options: &'static [&'static str], default: &'static str }, | |
| 15 | Color { | |
| 16 | r: &'static str, | |
| 17 | g: &'static str, | |
| 18 | b: &'static str, | |
| 19 | a: Option<&'static str>, | |
| 20 | default: [f64; 4], | |
| 21 | }, | |
| 22 | Text { default: &'static str }, | |
| 23 | } | |
| 24 | ||
| 25 | #[derive(Clone, Copy, Debug)] | |
| 26 | pub struct ParamSpec { | |
| 27 | pub name: &'static str, | |
| 28 | pub label: &'static str, | |
| 29 | pub kind: ParamKind, | |
| 30 | } | |
| 31 | ||
| 32 | #[derive(Clone, Debug)] | |
| 33 | pub enum InspectorAction { | |
| 34 | /// A parameter or node property changed; bust subtree & re-render viewers. | |
| 35 | ParamEdited(NodeId), | |
| 36 | /// A keyframe was added or deleted; refresh spline editor + bust subtree. | |
| 37 | KeyframeEdited(NodeId), | |
| 38 | /// Node was renamed. | |
| 39 | NodeRenamed(NodeId), | |
| 40 | /// Status notice to show in window title. | |
| 41 | StatusNotice(String), | |
| 42 | } | |
| 43 | ||
| 44 | #[derive(Clone, Debug)] | |
| 45 | struct DragState { | |
| 46 | node_id: NodeId, | |
| 47 | key: String, | |
| 48 | start_x: f32, | |
| 49 | start_val: f64, | |
| 50 | min: f64, | |
| 51 | max: f64, | |
| 52 | width: f32, | |
| 53 | } | |
| 54 | ||
| 55 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 56 | pub enum KeyframeState { | |
| 57 | Static, | |
| 58 | AnimatedOtherFrame, | |
| 59 | KeyframeAtCurrent, | |
| 60 | } | |
| 61 | ||
| 62 | pub struct InspectorState { | |
| 63 | pub bg: [f32; 4], | |
| 64 | pub panel_bg: [f32; 4], | |
| 65 | pub rule: [f32; 4], | |
| 66 | pub text: [f32; 4], | |
| 67 | pub text_dim: [f32; 4], | |
| 68 | pub accent: [f32; 4], | |
| 69 | expr: ExprEngine, | |
| 70 | /// Node name edit buffer synced with the currently selected node. | |
| 71 | name_node: Option<NodeId>, | |
| 72 | name_buf: String, | |
| 73 | /// Text parameter edit buffers keyed by `(node_id, param_name)`. | |
| 74 | text_bufs: HashMap<(usize, String), String>, | |
| 75 | /// Active scrub slider drag. | |
| 76 | drag: Option<DragState>, | |
| 77 | /// Last click timestamp per slider key for double-click reset. | |
| 78 | last_click: HashMap<String, Instant>, | |
| 79 | } | |
| 80 | ||
| 81 | impl InspectorState { | |
| 82 | pub fn new(theme: &ui::Theme) -> Self { | |
| 83 | let b = theme.base; | |
| 84 | let lift = |c: [f32; 4], d: f32| { | |
| 85 | [ | |
| 86 | (c[0] + d).clamp(0.0, 1.0), | |
| 87 | (c[1] + d).clamp(0.0, 1.0), | |
| 88 | (c[2] + d).clamp(0.0, 1.0), | |
| 89 | 1.0, | |
| 90 | ] | |
| 91 | }; | |
| 92 | Self { | |
| 93 | bg: b, | |
| 94 | panel_bg: lift(b, 0.020), | |
| 95 | rule: theme.rule, | |
| 96 | text: theme.text, | |
| 97 | text_dim: theme.text_dim, | |
| 98 | accent: theme.accent, | |
| 99 | expr: ExprEngine::new(), | |
| 100 | name_node: None, | |
| 101 | name_buf: String::new(), | |
| 102 | text_bufs: HashMap::new(), | |
| 103 | drag: None, | |
| 104 | last_click: HashMap::new(), | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | pub fn background(&self) -> [f32; 4] { | |
| 109 | self.bg | |
| 110 | } | |
| 111 | ||
| 112 | pub fn reset(&mut self) { | |
| 113 | self.name_node = None; | |
| 114 | self.name_buf.clear(); | |
| 115 | self.text_bufs.clear(); | |
| 116 | self.drag = None; | |
| 117 | } | |
| 118 | ||
| 119 | pub fn build_ui( | |
| 120 | &mut self, | |
| 121 | ui: &mut ui::Ui, | |
| 122 | pane_id: ui::Id, | |
| 123 | comp: &mut Comp, | |
| 124 | history: &mut CompHistory, | |
| 125 | selected: Option<NodeId>, | |
| 126 | current_frame: f64, | |
| 127 | stats: &EvalStats, | |
| 128 | ) -> Vec<InspectorAction> { | |
| 129 | let mut actions = Vec::new(); | |
| 130 | let bg = self.bg; | |
| 131 | let panel_bg = self.panel_bg; | |
| 132 | let rule = self.rule; | |
| 133 | let text_col = self.text; | |
| 134 | let text_dim = self.text_dim; | |
| 135 | let accent = self.accent; | |
| 136 | ||
| 137 | ui.open_as( | |
| 138 | pane_id, | |
| 139 | ui::Spec { | |
| 140 | size: [ui::fill(), ui::fill()], | |
| 141 | axis: ui::Axis::Y, | |
| 142 | fill: Some(bg), | |
| 143 | ..ui::Spec::default() | |
| 144 | }, | |
| 145 | ); | |
| 146 | ||
| 147 | let Some(node_id) = selected.filter(|id| id.0 < comp.nodes.len()) else { | |
| 148 | // Empty state | |
| 149 | ui.open( | |
| 150 | "insp-empty", | |
| 151 | ui::Spec { | |
| 152 | size: [ui::fill(), ui::fill()], | |
| 153 | axis: ui::Axis::Y, | |
| 154 | pad: [14.0, 18.0], | |
| 155 | gap: 8.0, | |
| 156 | ..ui::Spec::default() | |
| 157 | }, | |
| 158 | ); | |
| 159 | ui.leaf( | |
| 160 | "empty-title", | |
| 161 | ui::Spec { | |
| 162 | size: [ui::fill(), ui::px(20.0)], | |
| 163 | text: Some("No Node Selected"), | |
| 164 | font_size: Some(12.5), | |
| 165 | medium: true, | |
| 166 | color: Some(text_col), | |
| 167 | ..ui::Spec::default() | |
| 168 | }, | |
| 169 | ); | |
| 170 | ui.leaf( | |
| 171 | "empty-hint", | |
| 172 | ui::Spec { | |
| 173 | size: [ui::fill(), ui::fit()], | |
| 174 | text: Some("Select a node in the Flow graph to inspect and keyframe its parameters, or press Shift+Space to add a tool."), | |
| 175 | font_size: Some(11.0), | |
| 176 | overflow: ui::Overflow::Wrap, | |
| 177 | color: Some(text_dim), | |
| 178 | ..ui::Spec::default() | |
| 179 | }, | |
| 180 | ); | |
| 181 | self.render_stats_footer(ui, stats, panel_bg, rule, text_dim); | |
| 182 | ui.close(); | |
| 183 | ui.close(); | |
| 184 | return actions; | |
| 185 | }; | |
| 186 | ||
| 187 | // Sync editable name buffer when selection changes or node was renamed externally (e.g. undo) | |
| 188 | let current_name = comp.node(node_id).name.clone(); | |
| 189 | let name_field_id = pane_id.child(("node-name", node_id.0)); | |
| 190 | if self.name_node != Some(node_id) | |
| 191 | || (ui.focused() != Some(name_field_id) && self.name_buf != current_name) | |
| 192 | { | |
| 193 | self.name_node = Some(node_id); | |
| 194 | self.name_buf = current_name.clone(); | |
| 195 | } | |
| 196 | ||
| 197 | // 1. Header: Node name + Class badge + Bypass toggle | |
| 198 | ui.open( | |
| 199 | "insp-header", | |
| 200 | ui::Spec { | |
| 201 | size: [ui::fill(), ui::children()], | |
| 202 | axis: ui::Axis::Y, | |
| 203 | pad: [10.0, 8.0], | |
| 204 | gap: 6.0, | |
| 205 | fill: Some(panel_bg), | |
| 206 | border: Some(rule), | |
| 207 | ..ui::Spec::default() | |
| 208 | }, | |
| 209 | ); | |
| 210 | ||
| 211 | // Row 1: Editable name + Bypass button | |
| 212 | ui.open( | |
| 213 | "insp-name-row", | |
| 214 | ui::Spec { | |
| 215 | size: [ui::fill(), ui::px(26.0)], | |
| 216 | axis: ui::Axis::X, | |
| 217 | gap: 6.0, | |
| 218 | ..ui::Spec::default() | |
| 219 | }, | |
| 220 | ); | |
| 221 | let name_sig = ui::text_field( | |
| 222 | ui, | |
| 223 | name_field_id, | |
| 224 | &mut self.name_buf, | |
| 225 | "Node name", | |
| 226 | ui::Spec { | |
| 227 | size: [ui::fill(), ui::px(24.0)], | |
| 228 | font_size: Some(12.0), | |
| 229 | medium: true, | |
| 230 | fill: Some(bg), | |
| 231 | border: Some(rule), | |
| 232 | hover_border: Some(accent), | |
| 233 | radius: 4.0, | |
| 234 | pad: [6.0, 2.0], | |
| 235 | ..ui::Spec::default() | |
| 236 | }, | |
| 237 | ); | |
| 238 | let enter_pressed = name_sig.events.iter().any(|ev| { | |
| 239 | matches!( | |
| 240 | ev, | |
| 241 | ui::Event::Key { key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter), .. } | |
| 242 | ) | |
| 243 | }); | |
| 244 | if enter_pressed && self.name_buf.trim() != current_name { | |
| 245 | let desired = self.name_buf.trim().to_owned(); | |
| 246 | history.push_snapshot(comp, "Rename Node"); | |
| 247 | match comp.rename_node(node_id, &desired) { | |
| 248 | Ok(applied) => { | |
| 249 | self.name_buf = applied; | |
| 250 | actions.push(InspectorAction::NodeRenamed(node_id)); | |
| 251 | } | |
| 252 | Err(e) => { | |
| 253 | self.name_buf = current_name.clone(); | |
| 254 | actions.push(InspectorAction::StatusNotice(e)); | |
| 255 | } | |
| 256 | } | |
| 257 | ui.set_focus(None); | |
| 258 | } | |
| 259 | ||
| 260 | let bypassed = comp.node(node_id).bypassed; | |
| 261 | let pass_lbl = if bypassed { "Bypassed" } else { "Active" }; | |
| 262 | let pass_fill = if bypassed { | |
| 263 | [0.85, 0.28, 0.28, 0.28] | |
| 264 | } else { | |
| 265 | [0.24, 0.78, 0.48, 0.18] | |
| 266 | }; | |
| 267 | let pass_border = if bypassed { | |
| 268 | [0.92, 0.36, 0.36, 0.75] | |
| 269 | } else { | |
| 270 | [0.28, 0.82, 0.52, 0.55] | |
| 271 | }; | |
| 272 | let pass_sig = ui.leaf( | |
| 273 | ("insp-pass", node_id.0), | |
| 274 | ui::Spec { | |
| 275 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 276 | flags: ui::Flags::CLICKABLE, | |
| 277 | size: [ui::fit(), ui::px(24.0)], | |
| 278 | text: Some(pass_lbl), | |
| 279 | font_size: Some(10.5), | |
| 280 | medium: true, | |
| 281 | color: Some(text_col), | |
| 282 | fill: Some(pass_fill), | |
| 283 | border: Some(pass_border), | |
| 284 | radius: 4.0, | |
| 285 | pad: [8.0, 0.0], | |
| 286 | center: true, | |
| 287 | ..ui::Spec::default() | |
| 288 | }, | |
| 289 | ); | |
| 290 | if pass_sig.clicked { | |
| 291 | history.push_snapshot(comp, "Toggle Passthrough"); | |
| 292 | comp.toggle_passthrough(node_id); | |
| 293 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 294 | } | |
| 295 | ui.close(); // insp-name-row | |
| 296 | ||
| 297 | // Row 2: Tool class badge + supported/preserved indicator + frame badge | |
| 298 | let node_kind = comp.node(node_id).kind.clone(); | |
| 299 | let supported = is_supported_tool(&node_kind); | |
| 300 | let frame_str = format!("f{:.0}", current_frame); | |
| 301 | ui.open( | |
| 302 | "insp-meta-row", | |
| 303 | ui::Spec { | |
| 304 | size: [ui::fill(), ui::px(18.0)], | |
| 305 | axis: ui::Axis::X, | |
| 306 | gap: 6.0, | |
| 307 | ..ui::Spec::default() | |
| 308 | }, | |
| 309 | ); | |
| 310 | ui.leaf( | |
| 311 | "insp-kind-badge", | |
| 312 | ui::Spec { | |
| 313 | size: [ui::fit(), ui::px(18.0)], | |
| 314 | text: Some(&node_kind), | |
| 315 | font_size: Some(10.0), | |
| 316 | medium: true, | |
| 317 | color: Some(accent), | |
| 318 | fill: Some([accent[0], accent[1], accent[2], 0.14]), | |
| 319 | border: Some([accent[0], accent[1], accent[2], 0.40]), | |
| 320 | radius: 3.0, | |
| 321 | pad: [6.0, 0.0], | |
| 322 | center: true, | |
| 323 | ..ui::Spec::default() | |
| 324 | }, | |
| 325 | ); | |
| 326 | if !supported { | |
| 327 | ui.leaf( | |
| 328 | "insp-preserved-badge", | |
| 329 | ui::Spec { | |
| 330 | size: [ui::fit(), ui::px(18.0)], | |
| 331 | text: Some("preserved"), | |
| 332 | font_size: Some(9.5), | |
| 333 | medium: true, | |
| 334 | color: Some([0.98, 0.78, 0.40, 1.0]), | |
| 335 | fill: Some([0.24, 0.16, 0.06, 0.85]), | |
| 336 | border: Some([0.92, 0.64, 0.24, 0.60]), | |
| 337 | radius: 3.0, | |
| 338 | pad: [5.0, 0.0], | |
| 339 | center: true, | |
| 340 | ..ui::Spec::default() | |
| 341 | }, | |
| 342 | ); | |
| 343 | } | |
| 344 | ui.leaf( | |
| 345 | "insp-spacer", | |
| 346 | ui::Spec { | |
| 347 | size: [ui::fill(), ui::px(18.0)], | |
| 348 | ..ui::Spec::default() | |
| 349 | }, | |
| 350 | ); | |
| 351 | ui.leaf( | |
| 352 | "insp-frame", | |
| 353 | ui::Spec { | |
| 354 | size: [ui::fit(), ui::px(18.0)], | |
| 355 | text: Some(&frame_str), | |
| 356 | font_size: Some(10.0), | |
| 357 | color: Some(text_dim), | |
| 358 | center: true, | |
| 359 | ..ui::Spec::default() | |
| 360 | }, | |
| 361 | ); | |
| 362 | ui.close(); // insp-meta-row | |
| 363 | ui.close(); // insp-header | |
| 364 | ||
| 365 | // 2. Scrollable Parameter List | |
| 366 | ui.open( | |
| 367 | "insp-scroll", | |
| 368 | ui::Spec { | |
| 369 | flags: ui::Flags::SCROLL | ui::Flags::CLIP, | |
| 370 | size: [ui::fill(), ui::fill()], | |
| 371 | axis: ui::Axis::Y, | |
| 372 | pad: [10.0, 8.0], | |
| 373 | gap: 6.0, | |
| 374 | ..ui::Spec::default() | |
| 375 | }, | |
| 376 | ); | |
| 377 | ||
| 378 | let schema = schema_for_kind(&node_kind); | |
| 379 | let mut covered: HashSet<&str> = HashSet::new(); | |
| 380 | ||
| 381 | for spec in schema { | |
| 382 | covered.insert(spec.name); | |
| 383 | if let ParamKind::Color { r, g, b, a, .. } = spec.kind { | |
| 384 | covered.insert(r); | |
| 385 | covered.insert(g); | |
| 386 | covered.insert(b); | |
| 387 | if let Some(alpha_k) = a { | |
| 388 | covered.insert(alpha_k); | |
| 389 | } | |
| 390 | } | |
| 391 | self.render_param_spec(ui, pane_id, comp, history, node_id, spec, current_frame, &mut actions); | |
| 392 | } | |
| 393 | ||
| 394 | // Fallback: list all remaining present non-image inputs on the node! | |
| 395 | let mut extra_keys: Vec<String> = comp | |
| 396 | .node(node_id) | |
| 397 | .inputs | |
| 398 | .iter() | |
| 399 | .filter_map(|(k, b)| { | |
| 400 | if covered.contains(k.as_str()) { | |
| 401 | return None; | |
| 402 | } | |
| 403 | // Skip image/node connections unless they also have a value or spline | |
| 404 | let is_node_conn = b | |
| 405 | .source_op | |
| 406 | .as_deref() | |
| 407 | .is_some_and(|op| comp.node_by_name.contains_key(op)); | |
| 408 | if is_node_conn && b.value.is_none() { | |
| 409 | return None; | |
| 410 | } | |
| 411 | if b.value.is_none() && b.expression.is_none() && b.source_op.is_none() { | |
| 412 | return None; | |
| 413 | } | |
| 414 | Some(k.clone()) | |
| 415 | }) | |
| 416 | .collect(); | |
| 417 | extra_keys.sort(); | |
| 418 | ||
| 419 | if !extra_keys.is_empty() { | |
| 420 | ui.leaf( | |
| 421 | "insp-extra-hdr", | |
| 422 | ui::Spec { | |
| 423 | size: [ui::fill(), ui::px(18.0)], | |
| 424 | text: Some("OTHER INPUTS"), | |
| 425 | font_size: Some(9.5), | |
| 426 | medium: true, | |
| 427 | color: Some(text_dim), | |
| 428 | ..ui::Spec::default() | |
| 429 | }, | |
| 430 | ); | |
| 431 | for key in extra_keys { | |
| 432 | self.render_fallback_input(ui, pane_id, comp, history, node_id, &key, current_frame, &mut actions); | |
| 433 | } | |
| 434 | } | |
| 435 | ||
| 436 | // Loader clip info if Loader node | |
| 437 | if node_kind == "Loader" { | |
| 438 | let clip_summary = comp | |
| 439 | .node(node_id) | |
| 440 | .clips | |
| 441 | .first() | |
| 442 | .map(|c| c.filename.clone()) | |
| 443 | .unwrap_or_else(|| "(no clip)".to_owned()); | |
| 444 | ui.open( | |
| 445 | "insp-loader-clip", | |
| 446 | ui::Spec { | |
| 447 | size: [ui::fill(), ui::children()], | |
| 448 | axis: ui::Axis::Y, | |
| 449 | gap: 3.0, | |
| 450 | pad: [6.0, 5.0], | |
| 451 | fill: Some(panel_bg), | |
| 452 | border: Some(rule), | |
| 453 | radius: 4.0, | |
| 454 | ..ui::Spec::default() | |
| 455 | }, | |
| 456 | ); | |
| 457 | ui.leaf( | |
| 458 | "clip-lbl", | |
| 459 | ui::Spec { | |
| 460 | size: [ui::fill(), ui::px(14.0)], | |
| 461 | text: Some("Clip Filename"), | |
| 462 | font_size: Some(10.0), | |
| 463 | color: Some(text_dim), | |
| 464 | ..ui::Spec::default() | |
| 465 | }, | |
| 466 | ); | |
| 467 | ui.leaf( | |
| 468 | "clip-path", | |
| 469 | ui::Spec { | |
| 470 | size: [ui::fill(), ui::fit()], | |
| 471 | text: Some(&clip_summary), | |
| 472 | font_size: Some(10.5), | |
| 473 | overflow: ui::Overflow::Wrap, | |
| 474 | color: Some(text_col), | |
| 475 | ..ui::Spec::default() | |
| 476 | }, | |
| 477 | ); | |
| 478 | ui.close(); | |
| 479 | } | |
| 480 | ||
| 481 | ui.close(); // insp-scroll | |
| 482 | ||
| 483 | // 3. Footer: EvalStats summary | |
| 484 | self.render_stats_footer(ui, stats, panel_bg, rule, text_dim); | |
| 485 | ||
| 486 | ui.close(); // pane_id | |
| 487 | actions | |
| 488 | } | |
| 489 | ||
| 490 | fn render_stats_footer( | |
| 491 | &self, | |
| 492 | ui: &mut ui::Ui, | |
| 493 | stats: &EvalStats, | |
| 494 | panel_bg: [f32; 4], | |
| 495 | rule: [f32; 4], | |
| 496 | text_dim: [f32; 4], | |
| 497 | ) { | |
| 498 | let line = format!( | |
| 499 | "Eval {:.1}ms · {} nodes · cache {}/{} · {:.0}MB", | |
| 500 | stats.eval_ms, | |
| 501 | stats.nodes_evaluated, | |
| 502 | stats.cache_hits, | |
| 503 | stats.cache_hits + stats.cache_misses, | |
| 504 | stats.cached_vram_mb, | |
| 505 | ); | |
| 506 | ui.open( | |
| 507 | "insp-footer", | |
| 508 | ui::Spec { | |
| 509 | size: [ui::fill(), ui::px(24.0)], | |
| 510 | axis: ui::Axis::X, | |
| 511 | pad: [10.0, 4.0], | |
| 512 | fill: Some(panel_bg), | |
| 513 | border: Some(rule), | |
| 514 | ..ui::Spec::default() | |
| 515 | }, | |
| 516 | ); | |
| 517 | ui.leaf( | |
| 518 | "insp-stats-txt", | |
| 519 | ui::Spec { | |
| 520 | size: [ui::fill(), ui::fill()], | |
| 521 | text: Some(&line), | |
| 522 | font_size: Some(10.0), | |
| 523 | color: Some(text_dim), | |
| 524 | ..ui::Spec::default() | |
| 525 | }, | |
| 526 | ); | |
| 527 | ui.close(); | |
| 528 | } | |
| 529 | ||
| 530 | #[allow(clippy::too_many_arguments)] | |
| 531 | fn render_param_spec( | |
| 532 | &mut self, | |
| 533 | ui: &mut ui::Ui, | |
| 534 | pane_id: ui::Id, | |
| 535 | comp: &mut Comp, | |
| 536 | history: &mut CompHistory, | |
| 537 | node_id: NodeId, | |
| 538 | spec: &ParamSpec, | |
| 539 | current_frame: f64, | |
| 540 | actions: &mut Vec<InspectorAction>, | |
| 541 | ) { | |
| 542 | match spec.kind { | |
| 543 | ParamKind::Float { min, max, default, step: _ } => { | |
| 544 | self.render_float_row( | |
| 545 | ui, | |
| 546 | pane_id, | |
| 547 | comp, | |
| 548 | history, | |
| 549 | node_id, | |
| 550 | spec.name, | |
| 551 | spec.label, | |
| 552 | min, | |
| 553 | max, | |
| 554 | default, | |
| 555 | current_frame, | |
| 556 | actions, | |
| 557 | ); | |
| 558 | } | |
| 559 | ParamKind::Vec2 { min, max, default } => { | |
| 560 | self.render_vec2_row( | |
| 561 | ui, | |
| 562 | pane_id, | |
| 563 | comp, | |
| 564 | history, | |
| 565 | node_id, | |
| 566 | spec.name, | |
| 567 | spec.label, | |
| 568 | min, | |
| 569 | max, | |
| 570 | default, | |
| 571 | current_frame, | |
| 572 | actions, | |
| 573 | ); | |
| 574 | } | |
| 575 | ParamKind::Bool { default } => { | |
| 576 | self.render_bool_row(ui, comp, history, node_id, spec.name, spec.label, default, actions); | |
| 577 | } | |
| 578 | ParamKind::Enum { options, default } => { | |
| 579 | self.render_enum_row(ui, comp, history, node_id, spec.name, spec.label, options, default, actions); | |
| 580 | } | |
| 581 | ParamKind::Color { r, g, b, a, default } => { | |
| 582 | self.render_color_group( | |
| 583 | ui, | |
| 584 | pane_id, | |
| 585 | comp, | |
| 586 | history, | |
| 587 | node_id, | |
| 588 | spec.label, | |
| 589 | r, | |
| 590 | g, | |
| 591 | b, | |
| 592 | a, | |
| 593 | default, | |
| 594 | current_frame, | |
| 595 | actions, | |
| 596 | ); | |
| 597 | } | |
| 598 | ParamKind::Text { default } => { | |
| 599 | self.render_text_row(ui, pane_id, comp, history, node_id, spec.name, spec.label, default, actions); | |
| 600 | } | |
| 601 | } | |
| 602 | } | |
| 603 | ||
| 604 | #[allow(clippy::too_many_arguments)] | |
| 605 | fn render_fallback_input( | |
| 606 | &mut self, | |
| 607 | ui: &mut ui::Ui, | |
| 608 | pane_id: ui::Id, | |
| 609 | comp: &mut Comp, | |
| 610 | history: &mut CompHistory, | |
| 611 | node_id: NodeId, | |
| 612 | key: &str, | |
| 613 | current_frame: f64, | |
| 614 | actions: &mut Vec<InspectorAction>, | |
| 615 | ) { | |
| 616 | let val = comp.node(node_id).inputs.get(key).and_then(|b| b.value.clone()); | |
| 617 | match val { | |
| 618 | Some(ParamValue::Point(pt)) => { | |
| 619 | self.render_vec2_row(ui, pane_id, comp, history, node_id, key, key, -1.0, 2.0, pt, current_frame, actions); | |
| 620 | } | |
| 621 | Some(ParamValue::Text(_)) => { | |
| 622 | self.render_text_row(ui, pane_id, comp, history, node_id, key, key, "", actions); | |
| 623 | } | |
| 624 | Some(ParamValue::FuId(cur)) => { | |
| 625 | let text_dim = self.text_dim; | |
| 626 | let text_col = self.text; | |
| 627 | let panel_bg = self.panel_bg; | |
| 628 | let rule = self.rule; | |
| 629 | ui.open( | |
| 630 | ("fb-fuid", node_id.0, key), | |
| 631 | ui::Spec { | |
| 632 | size: [ui::fill(), ui::px(22.0)], | |
| 633 | axis: ui::Axis::X, | |
| 634 | gap: 6.0, | |
| 635 | ..ui::Spec::default() | |
| 636 | }, | |
| 637 | ); | |
| 638 | ui.leaf( | |
| 639 | "lbl", | |
| 640 | ui::Spec { | |
| 641 | size: [ui::px(92.0), ui::fill()], | |
| 642 | text: Some(key), | |
| 643 | font_size: Some(10.5), | |
| 644 | overflow: ui::Overflow::Ellipsis, | |
| 645 | color: Some(text_dim), | |
| 646 | ..ui::Spec::default() | |
| 647 | }, | |
| 648 | ); | |
| 649 | ui.leaf( | |
| 650 | "val", | |
| 651 | ui::Spec { | |
| 652 | size: [ui::fill(), ui::fill()], | |
| 653 | text: Some(&cur), | |
| 654 | font_size: Some(10.5), | |
| 655 | color: Some(text_col), | |
| 656 | fill: Some(panel_bg), | |
| 657 | border: Some(rule), | |
| 658 | radius: 4.0, | |
| 659 | pad: [6.0, 0.0], | |
| 660 | ..ui::Spec::default() | |
| 661 | }, | |
| 662 | ); | |
| 663 | ui.close(); | |
| 664 | } | |
| 665 | Some(ParamValue::Number(v)) => { | |
| 666 | let (min, max) = infer_float_range(key, v); | |
| 667 | self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, v, current_frame, actions); | |
| 668 | } | |
| 669 | None => { | |
| 670 | // Animated spline or expression-only scalar input | |
| 671 | let cur = comp.eval_number(comp.node(node_id), key, current_frame, 0.0, &self.expr); | |
| 672 | let (min, max) = infer_float_range(key, cur); | |
| 673 | self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, 0.0, current_frame, actions); | |
| 674 | } | |
| 675 | _ => {} | |
| 676 | } | |
| 677 | } | |
| 678 | ||
| 679 | #[allow(clippy::too_many_arguments)] | |
| 680 | fn render_float_row( | |
| 681 | &mut self, | |
| 682 | ui: &mut ui::Ui, | |
| 683 | pane_id: ui::Id, | |
| 684 | comp: &mut Comp, | |
| 685 | history: &mut CompHistory, | |
| 686 | node_id: NodeId, | |
| 687 | input_name: &str, | |
| 688 | label: &str, | |
| 689 | min: f64, | |
| 690 | max: f64, | |
| 691 | default: f64, | |
| 692 | current_frame: f64, | |
| 693 | actions: &mut Vec<InspectorAction>, | |
| 694 | ) { | |
| 695 | let cur_val = comp.eval_number(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 696 | let has_expr = comp | |
| 697 | .node(node_id) | |
| 698 | .inputs | |
| 699 | .get(input_name) | |
| 700 | .and_then(|b| b.expression.as_ref()) | |
| 701 | .is_some(); | |
| 702 | let kf_state = keyframe_state(comp, node_id, input_name, current_frame); | |
| 703 | ||
| 704 | ui.open( | |
| 705 | ("row-f", node_id.0, input_name), | |
| 706 | ui::Spec { | |
| 707 | size: [ui::fill(), ui::px(22.0)], | |
| 708 | axis: ui::Axis::X, | |
| 709 | gap: 4.0, | |
| 710 | ..ui::Spec::default() | |
| 711 | }, | |
| 712 | ); | |
| 713 | ||
| 714 | // Label | |
| 715 | ui.leaf( | |
| 716 | "lbl", | |
| 717 | ui::Spec { | |
| 718 | size: [ui::px(86.0), ui::fill()], | |
| 719 | text: Some(label), | |
| 720 | font_size: Some(10.5), | |
| 721 | overflow: ui::Overflow::Ellipsis, | |
| 722 | color: Some(self.text_dim), | |
| 723 | ..ui::Spec::default() | |
| 724 | }, | |
| 725 | ); | |
| 726 | ||
| 727 | // Expression `=` badge if present | |
| 728 | if has_expr { | |
| 729 | ui.leaf( | |
| 730 | "expr-badge", | |
| 731 | ui::Spec { | |
| 732 | size: [ui::px(16.0), ui::px(18.0)], | |
| 733 | text: Some("="), | |
| 734 | font_size: Some(10.5), | |
| 735 | bold: true, | |
| 736 | color: Some([0.36, 0.88, 0.92, 1.0]), | |
| 737 | fill: Some([0.10, 0.24, 0.28, 0.85]), | |
| 738 | border: Some([0.32, 0.78, 0.86, 0.65]), | |
| 739 | radius: 3.0, | |
| 740 | center: true, | |
| 741 | ..ui::Spec::default() | |
| 742 | }, | |
| 743 | ); | |
| 744 | } | |
| 745 | ||
| 746 | // Scrub slider | |
| 747 | let slider_id = pane_id.child(("sld", node_id.0, input_name)); | |
| 748 | let drag_key = format!("{}:{input_name}", node_id.0); | |
| 749 | if let Some(new_val) = self.scrub_slider( | |
| 750 | ui, | |
| 751 | slider_id, | |
| 752 | &drag_key, | |
| 753 | node_id, | |
| 754 | cur_val, | |
| 755 | min, | |
| 756 | max, | |
| 757 | default, | |
| 758 | history, | |
| 759 | comp, | |
| 760 | input_name, | |
| 761 | ) { | |
| 762 | if kf_state != KeyframeState::Static { | |
| 763 | let _ = comp.set_keyframe(node_id, input_name, current_frame, new_val); | |
| 764 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 765 | } else { | |
| 766 | comp.set_input_value(node_id, input_name, ParamValue::Number(new_val)); | |
| 767 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 768 | } | |
| 769 | } | |
| 770 | ||
| 771 | // Reset button (small dot/arrow when non-default) | |
| 772 | let is_default = (cur_val - default).abs() < 1e-4; | |
| 773 | if !is_default { | |
| 774 | let rst = ui.leaf( | |
| 775 | ("rst", node_id.0, input_name), | |
| 776 | ui::Spec { | |
| 777 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 778 | flags: ui::Flags::CLICKABLE, | |
| 779 | size: [ui::px(18.0), ui::px(20.0)], | |
| 780 | text: Some("↺"), | |
| 781 | font_size: Some(10.5), | |
| 782 | color: Some(self.text_dim), | |
| 783 | hover_fill: Some(self.panel_bg), | |
| 784 | radius: 3.0, | |
| 785 | center: true, | |
| 786 | ..ui::Spec::default() | |
| 787 | }, | |
| 788 | ); | |
| 789 | if rst.clicked { | |
| 790 | history.push_snapshot(comp, format!("Reset {label}")); | |
| 791 | if kf_state != KeyframeState::Static { | |
| 792 | let _ = comp.set_keyframe(node_id, input_name, current_frame, default); | |
| 793 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 794 | } else { | |
| 795 | comp.set_input_value(node_id, input_name, ParamValue::Number(default)); | |
| 796 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 797 | } | |
| 798 | } | |
| 799 | } | |
| 800 | ||
| 801 | // Keyframe diamond button (`â—†`) | |
| 802 | if self.keyframe_button(ui, ("kf", node_id.0, input_name), kf_state) { | |
| 803 | self.toggle_keyframe(comp, history, node_id, input_name, current_frame, cur_val, kf_state, actions); | |
| 804 | } | |
| 805 | ||
| 806 | ui.close(); | |
| 807 | } | |
| 808 | ||
| 809 | #[allow(clippy::too_many_arguments)] | |
| 810 | fn render_vec2_row( | |
| 811 | &mut self, | |
| 812 | ui: &mut ui::Ui, | |
| 813 | pane_id: ui::Id, | |
| 814 | comp: &mut Comp, | |
| 815 | history: &mut CompHistory, | |
| 816 | node_id: NodeId, | |
| 817 | input_name: &str, | |
| 818 | label: &str, | |
| 819 | min: f64, | |
| 820 | max: f64, | |
| 821 | default: [f64; 2], | |
| 822 | current_frame: f64, | |
| 823 | actions: &mut Vec<InspectorAction>, | |
| 824 | ) { | |
| 825 | let cur = comp.eval_point(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 826 | let has_expr = comp | |
| 827 | .node(node_id) | |
| 828 | .inputs | |
| 829 | .get(input_name) | |
| 830 | .and_then(|b| b.expression.as_ref()) | |
| 831 | .is_some(); | |
| 832 | ||
| 833 | ui.open( | |
| 834 | ("row-v2", node_id.0, input_name), | |
| 835 | ui::Spec { | |
| 836 | size: [ui::fill(), ui::px(22.0)], | |
| 837 | axis: ui::Axis::X, | |
| 838 | gap: 4.0, | |
| 839 | ..ui::Spec::default() | |
| 840 | }, | |
| 841 | ); | |
| 842 | ui.leaf( | |
| 843 | "lbl", | |
| 844 | ui::Spec { | |
| 845 | size: [ui::px(86.0), ui::fill()], | |
| 846 | text: Some(label), | |
| 847 | font_size: Some(10.5), | |
| 848 | overflow: ui::Overflow::Ellipsis, | |
| 849 | color: Some(self.text_dim), | |
| 850 | ..ui::Spec::default() | |
| 851 | }, | |
| 852 | ); | |
| 853 | if has_expr { | |
| 854 | ui.leaf( | |
| 855 | "expr-badge", | |
| 856 | ui::Spec { | |
| 857 | size: [ui::px(16.0), ui::px(18.0)], | |
| 858 | text: Some("="), | |
| 859 | font_size: Some(10.5), | |
| 860 | bold: true, | |
| 861 | color: Some([0.36, 0.88, 0.92, 1.0]), | |
| 862 | fill: Some([0.10, 0.24, 0.28, 0.85]), | |
| 863 | border: Some([0.32, 0.78, 0.86, 0.65]), | |
| 864 | radius: 3.0, | |
| 865 | center: true, | |
| 866 | ..ui::Spec::default() | |
| 867 | }, | |
| 868 | ); | |
| 869 | } | |
| 870 | ||
| 871 | let x_id = pane_id.child(("v2x", node_id.0, input_name)); | |
| 872 | let x_key = format!("{}:{input_name}:x", node_id.0); | |
| 873 | if let Some(nx) = self.scrub_slider( | |
| 874 | ui, | |
| 875 | x_id, | |
| 876 | &x_key, | |
| 877 | node_id, | |
| 878 | cur[0], | |
| 879 | min, | |
| 880 | max, | |
| 881 | default[0], | |
| 882 | history, | |
| 883 | comp, | |
| 884 | input_name, | |
| 885 | ) { | |
| 886 | comp.set_input_value(node_id, input_name, ParamValue::Point([nx, cur[1]])); | |
| 887 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 888 | } | |
| 889 | ||
| 890 | let y_id = pane_id.child(("v2y", node_id.0, input_name)); | |
| 891 | let y_key = format!("{}:{input_name}:y", node_id.0); | |
| 892 | if let Some(ny) = self.scrub_slider( | |
| 893 | ui, | |
| 894 | y_id, | |
| 895 | &y_key, | |
| 896 | node_id, | |
| 897 | cur[1], | |
| 898 | min, | |
| 899 | max, | |
| 900 | default[1], | |
| 901 | history, | |
| 902 | comp, | |
| 903 | input_name, | |
| 904 | ) { | |
| 905 | comp.set_input_value(node_id, input_name, ParamValue::Point([cur[0], ny])); | |
| 906 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 907 | } | |
| 908 | ||
| 909 | ui.close(); | |
| 910 | } | |
| 911 | ||
| 912 | #[allow(clippy::too_many_arguments)] | |
| 913 | fn render_bool_row( | |
| 914 | &mut self, | |
| 915 | ui: &mut ui::Ui, | |
| 916 | comp: &mut Comp, | |
| 917 | history: &mut CompHistory, | |
| 918 | node_id: NodeId, | |
| 919 | input_name: &str, | |
| 920 | label: &str, | |
| 921 | default: bool, | |
| 922 | actions: &mut Vec<InspectorAction>, | |
| 923 | ) { | |
| 924 | let checked = comp | |
| 925 | .node(node_id) | |
| 926 | .static_number(input_name) | |
| 927 | .map(|v| v >= 0.5) | |
| 928 | .unwrap_or(default); | |
| 929 | ||
| 930 | ui.open( | |
| 931 | ("row-b", node_id.0, input_name), | |
| 932 | ui::Spec { | |
| 933 | size: [ui::fill(), ui::px(22.0)], | |
| 934 | axis: ui::Axis::X, | |
| 935 | gap: 6.0, | |
| 936 | ..ui::Spec::default() | |
| 937 | }, | |
| 938 | ); | |
| 939 | ui.leaf( | |
| 940 | "lbl", | |
| 941 | ui::Spec { | |
| 942 | size: [ui::px(86.0), ui::fill()], | |
| 943 | text: Some(label), | |
| 944 | font_size: Some(10.5), | |
| 945 | overflow: ui::Overflow::Ellipsis, | |
| 946 | color: Some(self.text_dim), | |
| 947 | ..ui::Spec::default() | |
| 948 | }, | |
| 949 | ); | |
| 950 | let fill = if checked { | |
| 951 | [self.accent[0], self.accent[1], self.accent[2], 0.28] | |
| 952 | } else { | |
| 953 | self.panel_bg | |
| 954 | }; | |
| 955 | let border = if checked { self.accent } else { self.rule }; | |
| 956 | let sig = ui.leaf( | |
| 957 | ("chk", node_id.0, input_name), | |
| 958 | ui::Spec { | |
| 959 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 960 | flags: ui::Flags::CLICKABLE, | |
| 961 | size: [ui::px(54.0), ui::px(20.0)], | |
| 962 | text: Some(if checked { "✓ On" } else { "Off" }), | |
| 963 | font_size: Some(10.5), | |
| 964 | medium: checked, | |
| 965 | color: Some(if checked { self.text } else { self.text_dim }), | |
| 966 | fill: Some(fill), | |
| 967 | border: Some(border), | |
| 968 | radius: 4.0, | |
| 969 | center: true, | |
| 970 | ..ui::Spec::default() | |
| 971 | }, | |
| 972 | ); | |
| 973 | if sig.clicked { | |
| 974 | history.push_snapshot(comp, format!("Toggle {label}")); | |
| 975 | let next = if checked { 0.0 } else { 1.0 }; | |
| 976 | comp.set_input_value(node_id, input_name, ParamValue::Number(next)); | |
| 977 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 978 | } | |
| 979 | ui.close(); | |
| 980 | } | |
| 981 | ||
| 982 | #[allow(clippy::too_many_arguments)] | |
| 983 | fn render_enum_row( | |
| 984 | &mut self, | |
| 985 | ui: &mut ui::Ui, | |
| 986 | comp: &mut Comp, | |
| 987 | history: &mut CompHistory, | |
| 988 | node_id: NodeId, | |
| 989 | input_name: &str, | |
| 990 | label: &str, | |
| 991 | options: &'static [&'static str], | |
| 992 | default: &'static str, | |
| 993 | actions: &mut Vec<InspectorAction>, | |
| 994 | ) { | |
| 995 | let cur = comp | |
| 996 | .node(node_id) | |
| 997 | .static_str(input_name) | |
| 998 | .unwrap_or(default) | |
| 999 | .to_owned(); | |
| 1000 | ||
| 1001 | ui.open( | |
| 1002 | ("row-e", node_id.0, input_name), | |
| 1003 | ui::Spec { | |
| 1004 | size: [ui::fill(), ui::px(22.0)], | |
| 1005 | axis: ui::Axis::X, | |
| 1006 | gap: 6.0, | |
| 1007 | ..ui::Spec::default() | |
| 1008 | }, | |
| 1009 | ); | |
| 1010 | ui.leaf( | |
| 1011 | "lbl", | |
| 1012 | ui::Spec { | |
| 1013 | size: [ui::px(86.0), ui::fill()], | |
| 1014 | text: Some(label), | |
| 1015 | font_size: Some(10.5), | |
| 1016 | overflow: ui::Overflow::Ellipsis, | |
| 1017 | color: Some(self.text_dim), | |
| 1018 | ..ui::Spec::default() | |
| 1019 | }, | |
| 1020 | ); | |
| 1021 | let btn_lbl = format!("{cur} ▾"); | |
| 1022 | let sig = ui.leaf( | |
| 1023 | ("enum", node_id.0, input_name), | |
| 1024 | ui::Spec { | |
| 1025 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 1026 | flags: ui::Flags::CLICKABLE, | |
| 1027 | size: [ui::fill(), ui::px(20.0)], | |
| 1028 | text: Some(&btn_lbl), | |
| 1029 | font_size: Some(10.5), | |
| 1030 | color: Some(self.text), | |
| 1031 | fill: Some(self.panel_bg), | |
| 1032 | border: Some(self.rule), | |
| 1033 | hover_border: Some(self.accent), | |
| 1034 | radius: 4.0, | |
| 1035 | pad: [6.0, 0.0], | |
| 1036 | ..ui::Spec::default() | |
| 1037 | }, | |
| 1038 | ); | |
| 1039 | if sig.clicked && !options.is_empty() { | |
| 1040 | let idx = options.iter().position(|&o| o == cur).unwrap_or(0); | |
| 1041 | let next_idx = if ui.modifiers().shift_key() { | |
| 1042 | (idx + options.len() - 1) % options.len() | |
| 1043 | } else { | |
| 1044 | (idx + 1) % options.len() | |
| 1045 | }; | |
| 1046 | history.push_snapshot(comp, format!("Set {label}")); | |
| 1047 | comp.set_input_value(node_id, input_name, ParamValue::FuId(options[next_idx].to_owned())); | |
| 1048 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 1049 | } | |
| 1050 | ui.close(); | |
| 1051 | } | |
| 1052 | ||
| 1053 | #[allow(clippy::too_many_arguments)] | |
| 1054 | fn render_color_group( | |
| 1055 | &mut self, | |
| 1056 | ui: &mut ui::Ui, | |
| 1057 | pane_id: ui::Id, | |
| 1058 | comp: &mut Comp, | |
| 1059 | history: &mut CompHistory, | |
| 1060 | node_id: NodeId, | |
| 1061 | label: &str, | |
| 1062 | r_key: &'static str, | |
| 1063 | g_key: &'static str, | |
| 1064 | b_key: &'static str, | |
| 1065 | a_key: Option<&'static str>, | |
| 1066 | default: [f64; 4], | |
| 1067 | current_frame: f64, | |
| 1068 | actions: &mut Vec<InspectorAction>, | |
| 1069 | ) { | |
| 1070 | let rv = comp.eval_number(comp.node(node_id), r_key, current_frame, default[0], &self.expr); | |
| 1071 | let gv = comp.eval_number(comp.node(node_id), g_key, current_frame, default[1], &self.expr); | |
| 1072 | let bv = comp.eval_number(comp.node(node_id), b_key, current_frame, default[2], &self.expr); | |
| 1073 | let _av = a_key | |
| 1074 | .map(|ak| comp.eval_number(comp.node(node_id), ak, current_frame, default[3], &self.expr)) | |
| 1075 | .unwrap_or(default[3]); | |
| 1076 | let swatch = [ | |
| 1077 | rv.clamp(0.0, 1.0) as f32, | |
| 1078 | gv.clamp(0.0, 1.0) as f32, | |
| 1079 | bv.clamp(0.0, 1.0) as f32, | |
| 1080 | 1.0, | |
| 1081 | ]; | |
| 1082 | ||
| 1083 | ui.open( | |
| 1084 | ("grp-col", node_id.0, label), | |
| 1085 | ui::Spec { | |
| 1086 | size: [ui::fill(), ui::children()], | |
| 1087 | axis: ui::Axis::Y, | |
| 1088 | gap: 4.0, | |
| 1089 | pad: [6.0, 5.0], | |
| 1090 | fill: Some(self.panel_bg), | |
| 1091 | border: Some(self.rule), | |
| 1092 | radius: 4.0, | |
| 1093 | ..ui::Spec::default() | |
| 1094 | }, | |
| 1095 | ); | |
| 1096 | // Swatch header | |
| 1097 | ui.open( | |
| 1098 | "col-hdr", | |
| 1099 | ui::Spec { | |
| 1100 | size: [ui::fill(), ui::px(18.0)], | |
| 1101 | axis: ui::Axis::X, | |
| 1102 | gap: 6.0, | |
| 1103 | ..ui::Spec::default() | |
| 1104 | }, | |
| 1105 | ); | |
| 1106 | ui.leaf( | |
| 1107 | "col-lbl", | |
| 1108 | ui::Spec { | |
| 1109 | size: [ui::px(80.0), ui::fill()], | |
| 1110 | text: Some(label), | |
| 1111 | font_size: Some(10.5), | |
| 1112 | medium: true, | |
| 1113 | color: Some(self.text), | |
| 1114 | ..ui::Spec::default() | |
| 1115 | }, | |
| 1116 | ); | |
| 1117 | ui.leaf( | |
| 1118 | "col-swatch", | |
| 1119 | ui::Spec { | |
| 1120 | size: [ui::fill(), ui::px(16.0)], | |
| 1121 | fill: Some(swatch), | |
| 1122 | border: Some(self.rule), | |
| 1123 | radius: 3.0, | |
| 1124 | ..ui::Spec::default() | |
| 1125 | }, | |
| 1126 | ); | |
| 1127 | ui.close(); | |
| 1128 | ||
| 1129 | self.render_float_row(ui, pane_id, comp, history, node_id, r_key, "Red", 0.0, 1.0, default[0], current_frame, actions); | |
| 1130 | self.render_float_row(ui, pane_id, comp, history, node_id, g_key, "Green", 0.0, 1.0, default[1], current_frame, actions); | |
| 1131 | self.render_float_row(ui, pane_id, comp, history, node_id, b_key, "Blue", 0.0, 1.0, default[2], current_frame, actions); | |
| 1132 | if let Some(ak) = a_key { | |
| 1133 | self.render_float_row(ui, pane_id, comp, history, node_id, ak, "Alpha", 0.0, 1.0, default[3], current_frame, actions); | |
| 1134 | } | |
| 1135 | ui.close(); | |
| 1136 | } | |
| 1137 | ||
| 1138 | #[allow(clippy::too_many_arguments)] | |
| 1139 | fn render_text_row( | |
| 1140 | &mut self, | |
| 1141 | ui: &mut ui::Ui, | |
| 1142 | pane_id: ui::Id, | |
| 1143 | comp: &mut Comp, | |
| 1144 | history: &mut CompHistory, | |
| 1145 | node_id: NodeId, | |
| 1146 | input_name: &str, | |
| 1147 | label: &str, | |
| 1148 | default: &str, | |
| 1149 | actions: &mut Vec<InspectorAction>, | |
| 1150 | ) { | |
| 1151 | let field_id = pane_id.child(("txt", node_id.0, input_name)); | |
| 1152 | let key = (node_id.0, input_name.to_owned()); | |
| 1153 | let model_val = comp | |
| 1154 | .node(node_id) | |
| 1155 | .static_str(input_name) | |
| 1156 | .unwrap_or(default) | |
| 1157 | .to_owned(); | |
| 1158 | let buf = self.text_bufs.entry(key).or_insert_with(|| model_val.clone()); | |
| 1159 | if ui.focused() != Some(field_id) && *buf != model_val { | |
| 1160 | *buf = model_val.clone(); | |
| 1161 | } | |
| 1162 | let prev = buf.clone(); | |
| 1163 | ||
| 1164 | ui.open( | |
| 1165 | ("row-t", node_id.0, input_name), | |
| 1166 | ui::Spec { | |
| 1167 | size: [ui::fill(), ui::px(24.0)], | |
| 1168 | axis: ui::Axis::X, | |
| 1169 | gap: 6.0, | |
| 1170 | ..ui::Spec::default() | |
| 1171 | }, | |
| 1172 | ); | |
| 1173 | ui.leaf( | |
| 1174 | "lbl", | |
| 1175 | ui::Spec { | |
| 1176 | size: [ui::px(86.0), ui::fill()], | |
| 1177 | text: Some(label), | |
| 1178 | font_size: Some(10.5), | |
| 1179 | overflow: ui::Overflow::Ellipsis, | |
| 1180 | color: Some(self.text_dim), | |
| 1181 | ..ui::Spec::default() | |
| 1182 | }, | |
| 1183 | ); | |
| 1184 | ui::text_field( | |
| 1185 | ui, | |
| 1186 | field_id, | |
| 1187 | buf, | |
| 1188 | label, | |
| 1189 | ui::Spec { | |
| 1190 | size: [ui::fill(), ui::px(22.0)], | |
| 1191 | font_size: Some(11.0), | |
| 1192 | fill: Some(self.panel_bg), | |
| 1193 | border: Some(self.rule), | |
| 1194 | hover_border: Some(self.accent), | |
| 1195 | radius: 4.0, | |
| 1196 | pad: [6.0, 2.0], | |
| 1197 | ..ui::Spec::default() | |
| 1198 | }, | |
| 1199 | ); | |
| 1200 | if *buf != prev { | |
| 1201 | history.push_coalesced( | |
| 1202 | comp, | |
| 1203 | format!("Edit {label}"), | |
| 1204 | format!("text:{}:{input_name}", node_id.0), | |
| 1205 | ); | |
| 1206 | comp.set_input_value(node_id, input_name, ParamValue::Text(buf.clone())); | |
| 1207 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 1208 | } | |
| 1209 | ui.close(); | |
| 1210 | } | |
| 1211 | ||
| 1212 | #[allow(clippy::too_many_arguments)] | |
| 1213 | fn scrub_slider( | |
| 1214 | &mut self, | |
| 1215 | ui: &mut ui::Ui, | |
| 1216 | slider_id: ui::Id, | |
| 1217 | drag_key: &str, | |
| 1218 | node_id: NodeId, | |
| 1219 | value: f64, | |
| 1220 | min: f64, | |
| 1221 | max: f64, | |
| 1222 | default: f64, | |
| 1223 | history: &mut CompHistory, | |
| 1224 | comp: &Comp, | |
| 1225 | input_name: &str, | |
| 1226 | ) -> Option<f64> { | |
| 1227 | let sig = ui.signal(slider_id); | |
| 1228 | let rect = ui.laid_out(slider_id); | |
| 1229 | let width = rect.map(|r| (r[2] - r[0]).max(40.0)).unwrap_or(110.0); | |
| 1230 | let mut updated: Option<f64> = None; | |
| 1231 | ||
| 1232 | if let Some(press) = sig.press { | |
| 1233 | let now = Instant::now(); | |
| 1234 | let is_double = self | |
| 1235 | .last_click | |
| 1236 | .get(drag_key) | |
| 1237 | .is_some_and(|prev| now.duration_since(*prev) < Duration::from_millis(320)); | |
| 1238 | self.last_click.insert(drag_key.to_owned(), now); | |
| 1239 | if is_double { | |
| 1240 | history.push_snapshot(comp, format!("Reset {input_name}")); | |
| 1241 | self.drag = None; | |
| 1242 | updated = Some(default); | |
| 1243 | } else { | |
| 1244 | self.drag = Some(DragState { | |
| 1245 | node_id, | |
| 1246 | key: drag_key.to_owned(), | |
| 1247 | start_x: press[0], | |
| 1248 | start_val: value, | |
| 1249 | min, | |
| 1250 | max, | |
| 1251 | width, | |
| 1252 | }); | |
| 1253 | } | |
| 1254 | } else if sig.dragging { | |
| 1255 | if let (Some(d), Some(ptr)) = (&self.drag, ui.pointer()) { | |
| 1256 | if d.key == drag_key && d.node_id == node_id { | |
| 1257 | let fine = if ui.modifiers().shift_key() { 0.15 } else { 1.0 }; | |
| 1258 | let next = compute_scrub_value(d.start_val, ptr[0] - d.start_x, d.width, d.min, d.max, fine); | |
| 1259 | if (next - value).abs() > 1e-6 { | |
| 1260 | history.push_coalesced( | |
| 1261 | comp, | |
| 1262 | format!("Adjust {input_name}"), | |
| 1263 | format!("scrub:{drag_key}"), | |
| 1264 | ); | |
| 1265 | updated = Some(next); | |
| 1266 | } | |
| 1267 | } | |
| 1268 | } | |
| 1269 | } else if self.drag.as_ref().is_some_and(|d| d.key == drag_key) { | |
| 1270 | self.drag = None; | |
| 1271 | history.break_coalescing(); | |
| 1272 | } | |
| 1273 | ||
| 1274 | let display_val = updated.unwrap_or(value); | |
| 1275 | let frac = normalize_slider_fraction(display_val, min, max); | |
| 1276 | let val_text = format_param_number(display_val); | |
| 1277 | let fill_w = (width * frac).clamp(0.0, width); | |
| 1278 | let active_drag = self.drag.as_ref().is_some_and(|d| d.key == drag_key); | |
| 1279 | ||
| 1280 | ui.open_as( | |
| 1281 | slider_id, | |
| 1282 | ui::Spec { | |
| 1283 | cursor: Some(winit::window::CursorIcon::EwResize), | |
| 1284 | flags: ui::Flags::CLICKABLE | ui::Flags::CLIP, | |
| 1285 | size: [ui::fill(), ui::px(20.0)], | |
| 1286 | fill: Some(self.panel_bg), | |
| 1287 | border: Some(if active_drag || sig.hovered { self.accent } else { self.rule }), | |
| 1288 | radius: 4.0, | |
| 1289 | ..ui::Spec::default() | |
| 1290 | }, | |
| 1291 | ); | |
| 1292 | if fill_w > 0.5 { | |
| 1293 | ui.leaf( | |
| 1294 | "fill", | |
| 1295 | ui::Spec { | |
| 1296 | flags: ui::Flags::FLOAT | ui::Flags::INERT, | |
| 1297 | position: [0.0, 0.0], | |
| 1298 | size: [ui::px(fill_w), ui::px(20.0)], | |
| 1299 | fill: Some([self.accent[0], self.accent[1], self.accent[2], if active_drag { 0.34 } else { 0.22 }]), | |
| 1300 | radius: 3.0, | |
| 1301 | ..ui::Spec::default() | |
| 1302 | }, | |
| 1303 | ); | |
| 1304 | } | |
| 1305 | ui.leaf( | |
| 1306 | "txt", | |
| 1307 | ui::Spec { | |
| 1308 | flags: ui::Flags::INERT, | |
| 1309 | size: [ui::fill(), ui::fill()], | |
| 1310 | text: Some(&val_text), | |
| 1311 | font_size: Some(10.5), | |
| 1312 | color: Some(self.text), | |
| 1313 | center: true, | |
| 1314 | ..ui::Spec::default() | |
| 1315 | }, | |
| 1316 | ); | |
| 1317 | ui.close(); | |
| 1318 | ||
| 1319 | updated | |
| 1320 | } | |
| 1321 | ||
| 1322 | fn keyframe_button(&self, ui: &mut ui::Ui, part: impl std::hash::Hash, state: KeyframeState) -> bool { | |
| 1323 | let (ink, fill, border) = match state { | |
| 1324 | KeyframeState::KeyframeAtCurrent => ( | |
| 1325 | [1.0, 0.80, 0.22, 1.0], | |
| 1326 | Some([0.98, 0.72, 0.18, 0.24]), | |
| 1327 | Some([0.98, 0.76, 0.20, 0.85]), | |
| 1328 | ), | |
| 1329 | KeyframeState::AnimatedOtherFrame => ( | |
| 1330 | [0.92, 0.64, 0.24, 0.90], | |
| 1331 | Some([0.92, 0.64, 0.24, 0.12]), | |
| 1332 | Some([0.92, 0.64, 0.24, 0.45]), | |
| 1333 | ), | |
| 1334 | KeyframeState::Static => (self.text_dim, None, None), | |
| 1335 | }; | |
| 1336 | let sig = ui.leaf( | |
| 1337 | part, | |
| 1338 | ui::Spec { | |
| 1339 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 1340 | flags: ui::Flags::CLICKABLE, | |
| 1341 | size: [ui::px(20.0), ui::px(20.0)], | |
| 1342 | text: Some("â—†"), | |
| 1343 | font_size: Some(10.5), | |
| 1344 | color: Some(ink), | |
| 1345 | fill, | |
| 1346 | border, | |
| 1347 | hover_fill: Some([0.98, 0.76, 0.22, 0.18]), | |
| 1348 | radius: 4.0, | |
| 1349 | center: true, | |
| 1350 | ..ui::Spec::default() | |
| 1351 | }, | |
| 1352 | ); | |
| 1353 | sig.clicked | |
| 1354 | } | |
| 1355 | ||
| 1356 | #[allow(clippy::too_many_arguments)] | |
| 1357 | fn toggle_keyframe( | |
| 1358 | &self, | |
| 1359 | comp: &mut Comp, | |
| 1360 | history: &mut CompHistory, | |
| 1361 | node_id: NodeId, | |
| 1362 | input_name: &str, | |
| 1363 | current_frame: f64, | |
| 1364 | cur_val: f64, | |
| 1365 | state: KeyframeState, | |
| 1366 | actions: &mut Vec<InspectorAction>, | |
| 1367 | ) { | |
| 1368 | match state { | |
| 1369 | KeyframeState::KeyframeAtCurrent => { | |
| 1370 | if let Some(sp_name) = comp | |
| 1371 | .node(node_id) | |
| 1372 | .inputs | |
| 1373 | .get(input_name) | |
| 1374 | .and_then(|b| b.source_op.clone()) | |
| 1375 | { | |
| 1376 | history.push_snapshot(comp, format!("Delete Keyframe {input_name}")); | |
| 1377 | if comp.delete_keyframe(&sp_name, current_frame) { | |
| 1378 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 1379 | actions.push(InspectorAction::StatusNotice(format!( | |
| 1380 | "Deleted keyframe on {input_name} @ f{current_frame:.0}" | |
| 1381 | ))); | |
| 1382 | } | |
| 1383 | } | |
| 1384 | } | |
| 1385 | KeyframeState::Static | KeyframeState::AnimatedOtherFrame => { | |
| 1386 | history.push_snapshot(comp, format!("Set Keyframe {input_name}")); | |
| 1387 | match comp.set_keyframe(node_id, input_name, current_frame, cur_val) { | |
| 1388 | Ok(_) => { | |
| 1389 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 1390 | actions.push(InspectorAction::StatusNotice(format!( | |
| 1391 | "Keyframed {input_name} = {:.3} @ f{current_frame:.0}", | |
| 1392 | cur_val | |
| 1393 | ))); | |
| 1394 | } | |
| 1395 | Err(e) => actions.push(InspectorAction::StatusNotice(e)), | |
| 1396 | } | |
| 1397 | } | |
| 1398 | } | |
| 1399 | } | |
| 1400 | } | |
| 1401 | ||
| 1402 | /// Computes whether an input is static, animated on another frame, or has a keyframe at `frame`. | |
| 1403 | pub fn keyframe_state(comp: &Comp, node_id: NodeId, input_name: &str, frame: f64) -> KeyframeState { | |
| 1404 | let Some(sp_name) = comp | |
| 1405 | .node(node_id) | |
| 1406 | .inputs | |
| 1407 | .get(input_name) | |
| 1408 | .and_then(|b| b.source_op.as_deref()) | |
| 1409 | else { | |
| 1410 | return KeyframeState::Static; | |
| 1411 | }; | |
| 1412 | let Some(spline) = comp.splines.get(sp_name) else { | |
| 1413 | return KeyframeState::Static; | |
| 1414 | }; | |
| 1415 | if spline.keyframes.iter().any(|kf| (kf.time - frame).abs() < 1e-4) { | |
| 1416 | KeyframeState::KeyframeAtCurrent | |
| 1417 | } else if !spline.keyframes.is_empty() { | |
| 1418 | KeyframeState::AnimatedOtherFrame | |
| 1419 | } else { | |
| 1420 | KeyframeState::Static | |
| 1421 | } | |
| 1422 | } | |
| 1423 | ||
| 1424 | pub fn normalize_slider_fraction(value: f64, min: f64, max: f64) -> f32 { | |
| 1425 | let span = (max - min).abs().max(1e-6); | |
| 1426 | ((value - min) / span).clamp(0.0, 1.0) as f32 | |
| 1427 | } | |
| 1428 | ||
| 1429 | pub fn compute_scrub_value(start_val: f64, dx: f32, width: f32, min: f64, max: f64, fine_scale: f64) -> f64 { | |
| 1430 | let span = (max - min).max(1e-4); | |
| 1431 | let delta = (f64::from(dx) / f64::from(width.max(40.0))) * span * fine_scale; | |
| 1432 | (start_val + delta).clamp(min, max) | |
| 1433 | } | |
| 1434 | ||
| 1435 | pub fn format_param_number(v: f64) -> String { | |
| 1436 | if v.abs() >= 100.0 { | |
| 1437 | format!("{v:.1}") | |
| 1438 | } else if v.abs() >= 10.0 { | |
| 1439 | format!("{v:.2}") | |
| 1440 | } else { | |
| 1441 | format!("{v:.3}") | |
| 1442 | } | |
| 1443 | } | |
| 1444 | ||
| 1445 | pub fn infer_float_range(name: &str, val: f64) -> (f64, f64) { | |
| 1446 | let lower = name.to_ascii_lowercase(); | |
| 1447 | if lower.contains("angle") || lower.contains("rotate") { | |
| 1448 | (-360.0, 360.0) | |
| 1449 | } else if lower.contains("width") || lower.contains("height") { | |
| 1450 | if val > 2.0 { | |
| 1451 | (1.0, (val * 2.0).max(4096.0)) | |
| 1452 | } else { | |
| 1453 | (0.0, 2.0) | |
| 1454 | } | |
| 1455 | } else if lower.contains("blend") || lower.contains("alpha") || lower.contains("opacity") { | |
| 1456 | (0.0, 1.0) | |
| 1457 | } else if lower.contains("size") || lower.contains("blur") || lower.contains("glow") { | |
| 1458 | (0.0, (val.abs() * 2.0).max(50.0)) | |
| 1459 | } else if val < 0.0 || val > 1.0 { | |
| 1460 | let hi = (val.abs() * 2.0).max(10.0); | |
| 1461 | (-hi, hi) | |
| 1462 | } else { | |
| 1463 | (0.0, 1.0) | |
| 1464 | } | |
| 1465 | } | |
| 1466 | ||
| 1467 | const APPLY_MODES: &[&str] = &[ | |
| 1468 | "Normal", | |
| 1469 | "Screen", | |
| 1470 | "Multiply", | |
| 1471 | "Overlay", | |
| 1472 | "Soft Light", | |
| 1473 | "Hard Light", | |
| 1474 | "Color Dodge", | |
| 1475 | "Color Burn", | |
| 1476 | "Darken", | |
| 1477 | "Lighten", | |
| 1478 | "Difference", | |
| 1479 | "Exclusion", | |
| 1480 | "Hue", | |
| 1481 | "Saturation", | |
| 1482 | "Color", | |
| 1483 | "Luminosity", | |
| 1484 | ]; | |
| 1485 | ||
| 1486 | const BLUR_FILTERS: &[&str] = &["Fast Gaussian", "Gaussian", "Box", "Bartlett", "Multi-box"]; | |
| 1487 | const CLIPPING_MODES: &[&str] = &["Frame", "Domain", "None"]; | |
| 1488 | const EDGES_MODES: &[&str] = &["Canvas", "Wrap", "Duplicate", "Mirror"]; | |
| 1489 | const SHAPE3D_TYPES: &[&str] = &[ | |
| 1490 | "SurfacePlaneInputs", | |
| 1491 | "SurfaceSphereInputs", | |
| 1492 | "SurfaceCubeInputs", | |
| 1493 | "SurfaceCylinderInputs", | |
| 1494 | "SurfaceConeInputs", | |
| 1495 | "SurfaceTorusInputs", | |
| 1496 | ]; | |
| 1497 | ||
| 1498 | const fn flt(name: &'static str, label: &'static str, min: f64, max: f64, default: f64) -> ParamSpec { | |
| 1499 | ParamSpec { | |
| 1500 | name, | |
| 1501 | label, | |
| 1502 | kind: ParamKind::Float { min, max, default, step: 0.01 }, | |
| 1503 | } | |
| 1504 | } | |
| 1505 | ||
| 1506 | const fn v2(name: &'static str, label: &'static str, min: f64, max: f64, default: [f64; 2]) -> ParamSpec { | |
| 1507 | ParamSpec { | |
| 1508 | name, | |
| 1509 | label, | |
| 1510 | kind: ParamKind::Vec2 { min, max, default }, | |
| 1511 | } | |
| 1512 | } | |
| 1513 | ||
| 1514 | const fn boolean(name: &'static str, label: &'static str, default: bool) -> ParamSpec { | |
| 1515 | ParamSpec { | |
| 1516 | name, | |
| 1517 | label, | |
| 1518 | kind: ParamKind::Bool { default }, | |
| 1519 | } | |
| 1520 | } | |
| 1521 | ||
| 1522 | const fn fuid( | |
| 1523 | name: &'static str, | |
| 1524 | label: &'static str, | |
| 1525 | options: &'static [&'static str], | |
| 1526 | default: &'static str, | |
| 1527 | ) -> ParamSpec { | |
| 1528 | ParamSpec { | |
| 1529 | name, | |
| 1530 | label, | |
| 1531 | kind: ParamKind::Enum { options, default }, | |
| 1532 | } | |
| 1533 | } | |
| 1534 | ||
| 1535 | /// Curated Fusion 21.1 parameter schemas for major compositor tools. | |
| 1536 | pub fn schema_for_kind(kind: &str) -> &'static [ParamSpec] { | |
| 1537 | match kind { | |
| 1538 | "Merge" => const { &[ | |
| 1539 | fuid("ApplyMode", "Apply Mode", APPLY_MODES, "Normal"), | |
| 1540 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1541 | flt("SubMerge", "Subtractive", 0.0, 1.0, 0.0), | |
| 1542 | v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), | |
| 1543 | flt("Size", "Size", 0.0, 5.0, 1.0), | |
| 1544 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1545 | boolean("FlipHoriz", "Flip Horiz", false), | |
| 1546 | boolean("FlipVert", "Flip Vert", false), | |
| 1547 | fuid("Edges", "Edges", EDGES_MODES, "Canvas"), | |
| 1548 | boolean("PerformDepthMerge", "Depth Merge", false), | |
| 1549 | ] }, | |
| 1550 | "Transform" => const { &[ | |
| 1551 | v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), | |
| 1552 | v2("Pivot", "Pivot", -1.0, 2.0, [0.5, 0.5]), | |
| 1553 | flt("Size", "Size", 0.0, 5.0, 1.0), | |
| 1554 | flt("Aspect", "Aspect", 0.1, 4.0, 1.0), | |
| 1555 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1556 | boolean("FlipHoriz", "Flip Horiz", false), | |
| 1557 | boolean("FlipVert", "Flip Vert", false), | |
| 1558 | fuid("Edges", "Edges", EDGES_MODES, "Canvas"), | |
| 1559 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1560 | ] }, | |
| 1561 | "Blur" => const { &[ | |
| 1562 | fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), | |
| 1563 | boolean("LockXY", "Lock X/Y", true), | |
| 1564 | flt("XBlurSize", "Blur Size", 0.0, 100.0, 1.0), | |
| 1565 | flt("YBlurSize", "Y Blur Size", 0.0, 100.0, 1.0), | |
| 1566 | fuid("ClippingMode", "Clipping", CLIPPING_MODES, "Frame"), | |
| 1567 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1568 | ] }, | |
| 1569 | "Glow" => const { &[ | |
| 1570 | flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), | |
| 1571 | flt("Glow", "Glow", 0.0, 2.0, 0.8), | |
| 1572 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1573 | fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), | |
| 1574 | ] }, | |
| 1575 | "SoftGlow" => const { &[ | |
| 1576 | fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), | |
| 1577 | flt("Threshold", "Threshold", 0.0, 1.0, 0.0), | |
| 1578 | flt("Gain", "Gain", 0.0, 5.0, 2.0), | |
| 1579 | flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), | |
| 1580 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1581 | ] }, | |
| 1582 | "BrightnessContrast" => const { &[ | |
| 1583 | flt("Gain", "Gain", 0.0, 5.0, 1.0), | |
| 1584 | flt("Lift", "Lift", -1.0, 1.0, 0.0), | |
| 1585 | flt("Gamma", "Gamma", 0.05, 5.0, 1.0), | |
| 1586 | flt("Contrast", "Contrast", -1.0, 1.0, 0.0), | |
| 1587 | flt("Brightness", "Brightness", -1.0, 1.0, 0.0), | |
| 1588 | flt("Saturation", "Saturation", 0.0, 4.0, 1.0), | |
| 1589 | boolean("PreDividePostMultiply", "Pre-Div/Post", false), | |
| 1590 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1591 | ] }, | |
| 1592 | "ColorCorrector" => const { &[ | |
| 1593 | flt("WheelSaturation1", "Saturation", 0.0, 4.0, 1.0), | |
| 1594 | flt("WheelHue1", "Hue", -1.0, 1.0, 0.0), | |
| 1595 | flt("MasterRGBGain", "Gain", 0.0, 5.0, 1.0), | |
| 1596 | flt("MasterRGBGamma", "Gamma", 0.05, 5.0, 1.0), | |
| 1597 | flt("MasterRGBLift", "Lift", -1.0, 1.0, 0.0), | |
| 1598 | flt("MasterRGBContrast", "Contrast", 0.0, 3.0, 1.0), | |
| 1599 | flt("MasterRGBBrightness", "Brightness", -1.0, 1.0, 0.0), | |
| 1600 | boolean("PreDividePostMultiply", "Pre-Div/Post", false), | |
| 1601 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1602 | ] }, | |
| 1603 | "Background" => const { &[ | |
| 1604 | ParamSpec { | |
| 1605 | name: "TopLeftColor", | |
| 1606 | label: "Color", | |
| 1607 | kind: ParamKind::Color { | |
| 1608 | r: "TopLeftRed", | |
| 1609 | g: "TopLeftGreen", | |
| 1610 | b: "TopLeftBlue", | |
| 1611 | a: Some("TopLeftAlpha"), | |
| 1612 | default: [0.0, 0.0, 0.0, 1.0], | |
| 1613 | }, | |
| 1614 | }, | |
| 1615 | flt("Width", "Width", 1.0, 7680.0, 1920.0), | |
| 1616 | flt("Height", "Height", 1.0, 4320.0, 1080.0), | |
| 1617 | ] }, | |
| 1618 | "TextPlus" => const { &[ | |
| 1619 | ParamSpec { | |
| 1620 | name: "StyledText", | |
| 1621 | label: "Styled Text", | |
| 1622 | kind: ParamKind::Text { default: "" }, | |
| 1623 | }, | |
| 1624 | ParamSpec { | |
| 1625 | name: "Font", | |
| 1626 | label: "Font", | |
| 1627 | kind: ParamKind::Text { default: "Open Sans" }, | |
| 1628 | }, | |
| 1629 | flt("Size", "Size", 0.005, 1.0, 0.08), | |
| 1630 | flt("Tracking", "Tracking", 0.0, 4.0, 1.0), | |
| 1631 | v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), | |
| 1632 | ParamSpec { | |
| 1633 | name: "TextColor", | |
| 1634 | label: "Text Color", | |
| 1635 | kind: ParamKind::Color { | |
| 1636 | r: "Red1", | |
| 1637 | g: "Green1", | |
| 1638 | b: "Blue1", | |
| 1639 | a: Some("Alpha1"), | |
| 1640 | default: [1.0, 1.0, 1.0, 1.0], | |
| 1641 | }, | |
| 1642 | }, | |
| 1643 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1644 | ] }, | |
| 1645 | "EllipseMask" => const { &[ | |
| 1646 | flt("Level", "Level", 0.0, 1.0, 1.0), | |
| 1647 | flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), | |
| 1648 | flt("BorderWidth", "Border", -0.2, 0.2, 0.0), | |
| 1649 | boolean("Solid", "Solid", true), | |
| 1650 | boolean("Invert", "Invert", false), | |
| 1651 | v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), | |
| 1652 | flt("Width", "Width", 0.0, 2.0, 0.5), | |
| 1653 | flt("Height", "Height", 0.0, 2.0, 0.5), | |
| 1654 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1655 | ] }, | |
| 1656 | "RectangleMask" => const { &[ | |
| 1657 | flt("Level", "Level", 0.0, 1.0, 1.0), | |
| 1658 | flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), | |
| 1659 | flt("CornerRadius", "Corner", 0.0, 1.0, 0.0), | |
| 1660 | boolean("Solid", "Solid", true), | |
| 1661 | boolean("Invert", "Invert", false), | |
| 1662 | v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), | |
| 1663 | flt("Width", "Width", 0.0, 2.0, 0.5), | |
| 1664 | flt("Height", "Height", 0.0, 2.0, 0.5), | |
| 1665 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1666 | ] }, | |
| 1667 | "Displace" => const { &[ | |
| 1668 | flt("XRefraction", "X Refract", -1.0, 1.0, 0.0), | |
| 1669 | flt("YRefraction", "Y Refract", -1.0, 1.0, 0.0), | |
| 1670 | flt("RefractionStrength", "Strength", -2.0, 2.0, 0.0), | |
| 1671 | flt("LightAngle", "Light Angle", -360.0, 360.0, 0.0), | |
| 1672 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1673 | ] }, | |
| 1674 | "Vortex" => const { &[ | |
| 1675 | v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), | |
| 1676 | flt("Size", "Size", 0.0, 2.0, 0.5), | |
| 1677 | flt("Angle", "Angle", -720.0, 720.0, 90.0), | |
| 1678 | flt("Power", "Power", 0.1, 5.0, 1.0), | |
| 1679 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1680 | ] }, | |
| 1681 | "FastNoise" => const { &[ | |
| 1682 | flt("Detail", "Detail", 0.0, 10.0, 2.0), | |
| 1683 | flt("Contrast", "Contrast", 0.0, 5.0, 1.0), | |
| 1684 | flt("Brightness", "Brightness", -1.0, 1.0, 0.0), | |
| 1685 | flt("XScale", "Scale", 0.01, 50.0, 2.0), | |
| 1686 | flt("Seethe", "Seethe", 0.0, 100.0, 0.0), | |
| 1687 | flt("SeetheRate", "Seethe Rate", 0.0, 2.0, 0.0), | |
| 1688 | v2("Center", "Center", -2.0, 3.0, [0.5, 0.5]), | |
| 1689 | ] }, | |
| 1690 | "UltraKeyer" => const { &[ | |
| 1691 | ParamSpec { | |
| 1692 | name: "KeyColor", | |
| 1693 | label: "Key Color", | |
| 1694 | kind: ParamKind::Color { | |
| 1695 | r: "BackgroundRed", | |
| 1696 | g: "BackgroundGreen", | |
| 1697 | b: "BackgroundBlue", | |
| 1698 | a: None, | |
| 1699 | default: [0.0, 1.0, 0.0, 1.0], | |
| 1700 | }, | |
| 1701 | }, | |
| 1702 | flt("MatteThresholdLow", "Matte Low", 0.0, 1.0, 0.0), | |
| 1703 | flt("MatteThresholdHigh", "Matte High", 0.0, 1.0, 1.0), | |
| 1704 | flt("SpillSuppression", "Spill", 0.0, 1.0, 0.0), | |
| 1705 | boolean("PostMultiplyImage", "Post-Multiply", true), | |
| 1706 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1707 | ] }, | |
| 1708 | "Loader" => const { &[ | |
| 1709 | flt("GlobalIn", "Global In", 0.0, 10000.0, 0.0), | |
| 1710 | flt("GlobalOut", "Global Out", 0.0, 10000.0, 100.0), | |
| 1711 | boolean("Loop", "Loop", false), | |
| 1712 | boolean("Reverse", "Reverse", false), | |
| 1713 | ] }, | |
| 1714 | "Camera3D" => const { &[ | |
| 1715 | flt("FLength", "Focal Len", 5.0, 200.0, 24.0), | |
| 1716 | flt("PlaneOfFocus", "Focus Dist", 0.1, 100.0, 5.0), | |
| 1717 | flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), | |
| 1718 | flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), | |
| 1719 | flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 5.0), | |
| 1720 | flt("Transform3DOp.Rotate.X", "Rot X", -180.0, 180.0, 0.0), | |
| 1721 | flt("Transform3DOp.Rotate.Y", "Rot Y", -180.0, 180.0, 0.0), | |
| 1722 | flt("Transform3DOp.Rotate.Z", "Rot Z", -180.0, 180.0, 0.0), | |
| 1723 | ] }, | |
| 1724 | "Shape3D" => const { &[ | |
| 1725 | fuid("Shape", "Shape", SHAPE3D_TYPES, "SurfacePlaneInputs"), | |
| 1726 | flt("SurfacePlaneInputs.Width", "Width", 0.01, 20.0, 1.0), | |
| 1727 | flt("SurfacePlaneInputs.Height", "Height", 0.01, 20.0, 1.0), | |
| 1728 | flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), | |
| 1729 | flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), | |
| 1730 | flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 0.0), | |
| 1731 | flt("Transform3DOp.Scale.X", "Scale", 0.01, 20.0, 1.0), | |
| 1732 | ] }, | |
| 1733 | _ => &[], | |
| 1734 | } | |
| 1735 | } | |
| 1736 | ||
| 1737 | #[cfg(test)] | |
| 1738 | mod tests { | |
| 1739 | use super::*; | |
| 1740 | ||
| 1741 | #[test] | |
| 1742 | fn curated_schemas_cover_all_major_tools() { | |
| 1743 | let major = [ | |
| 1744 | "Merge", | |
| 1745 | "Transform", | |
| 1746 | "Blur", | |
| 1747 | "Glow", | |
| 1748 | "SoftGlow", | |
| 1749 | "BrightnessContrast", | |
| 1750 | "ColorCorrector", | |
| 1751 | "Background", | |
| 1752 | "TextPlus", | |
| 1753 | "EllipseMask", | |
| 1754 | "RectangleMask", | |
| 1755 | "Displace", | |
| 1756 | "Vortex", | |
| 1757 | "FastNoise", | |
| 1758 | "UltraKeyer", | |
| 1759 | "Loader", | |
| 1760 | "Camera3D", | |
| 1761 | "Shape3D", | |
| 1762 | ]; | |
| 1763 | for tool in major { | |
| 1764 | let s = schema_for_kind(tool); | |
| 1765 | assert!(!s.is_empty(), "expected non-empty curated schema for {tool}"); | |
| 1766 | } | |
| 1767 | assert!(schema_for_kind("UnknownCustomTool").is_empty()); | |
| 1768 | } | |
| 1769 | ||
| 1770 | #[test] | |
| 1771 | fn slider_math_and_clamping() { | |
| 1772 | assert!((normalize_slider_fraction(0.5, 0.0, 1.0) - 0.5).abs() < 1e-6); | |
| 1773 | assert!((normalize_slider_fraction(-5.0, 0.0, 1.0) - 0.0).abs() < 1e-6); | |
| 1774 | assert!((normalize_slider_fraction(10.0, 0.0, 1.0) - 1.0).abs() < 1e-6); | |
| 1775 | ||
| 1776 | let v = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 1.0); | |
| 1777 | assert!((v - 1.0).abs() < 1e-6); | |
| 1778 | let v_fine = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 0.1); | |
| 1779 | assert!((v_fine - 0.55).abs() < 1e-6); | |
| 1780 | } | |
| 1781 | ||
| 1782 | #[test] | |
| 1783 | fn keyframe_state_transitions() { | |
| 1784 | let src = r#"Composition { | |
| 1785 | Tools = ordered() { | |
| 1786 | BC1 = BrightnessContrast { | |
| 1787 | Inputs = { Gain = Input { Value = 1.0 } }, | |
| 1788 | }, | |
| 1789 | }, | |
| 1790 | }"#; | |
| 1791 | let mut comp = Comp::from_fusion_str(src).unwrap(); | |
| 1792 | let bc = comp.find_node("BC1").unwrap(); | |
| 1793 | assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); | |
| 1794 | ||
| 1795 | let sp = comp.set_keyframe(bc, "Gain", 10.0, 1.5).unwrap(); | |
| 1796 | assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::KeyframeAtCurrent); | |
| 1797 | assert_eq!(keyframe_state(&comp, bc, "Gain", 20.0), KeyframeState::AnimatedOtherFrame); | |
| 1798 | ||
| 1799 | assert!(comp.delete_keyframe(&sp, 10.0)); | |
| 1800 | assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); | |
| 1801 | } | |
| 1802 | } |
crates/app/src/keyframes.rs created+1032| ... | ... | @@ -0,0 +1,1032 @@ |
| 1 | //! Docked Keyframes dope-sheet pane: animated node/channel tracks, audio waveform track, | |
| 2 | //! green RAM cache bar, and interactive drag-to-retime keyframes with `CompHistory` undo. | |
| 3 | ||
| 4 | use draw::{Glyphs, Primitive}; | |
| 5 | use model::{Comp, CompHistory, Keyframe, NodeId}; | |
| 6 | use std::collections::{HashMap, HashSet}; | |
| 7 | use std::sync::Arc; | |
| 8 | use winit::event::{ElementState, MouseButton}; | |
| 9 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; | |
| 10 | ||
| 11 | const RULER_H: f32 = 26.0; | |
| 12 | const AUDIO_ROW_H: f32 = 24.0; | |
| 13 | const ROW_H: f32 = 22.0; | |
| 14 | const SIDEBAR_W: f32 = 188.0; | |
| 15 | const FOOTER_H: f32 = 22.0; | |
| 16 | ||
| 17 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
| 18 | pub struct DopeKeyRef { | |
| 19 | pub spline: String, | |
| 20 | pub key_idx: usize, | |
| 21 | } | |
| 22 | ||
| 23 | #[derive(Clone, Debug)] | |
| 24 | pub enum KeyframesAction { | |
| 25 | ScrubFrame(f64), | |
| 26 | SelectNode(NodeId), | |
| 27 | KeyframesEdited, | |
| 28 | } | |
| 29 | ||
| 30 | #[derive(Clone, Debug)] | |
| 31 | pub enum TrackRow { | |
| 32 | NodeHeader { | |
| 33 | node_id: NodeId, | |
| 34 | label: String, | |
| 35 | splines: Vec<String>, | |
| 36 | }, | |
| 37 | Channel { | |
| 38 | node_id: NodeId, | |
| 39 | label: String, | |
| 40 | spline: String, | |
| 41 | color: [f32; 4], | |
| 42 | }, | |
| 43 | } | |
| 44 | ||
| 45 | #[derive(Clone, Debug)] | |
| 46 | struct RetimeDrag { | |
| 47 | start_frame: f64, | |
| 48 | saved_splines: HashMap<String, Vec<Keyframe>>, | |
| 49 | pre_edit_comp: Comp, | |
| 50 | moved: bool, | |
| 51 | } | |
| 52 | ||
| 53 | #[derive(Clone, Copy, Debug)] | |
| 54 | pub struct KeyframesPalette { | |
| 55 | pub bg: [f32; 4], | |
| 56 | pub sidebar: [f32; 4], | |
| 57 | pub ruler: [f32; 4], | |
| 58 | pub rule: [f32; 4], | |
| 59 | pub grid: [f32; 4], | |
| 60 | pub row_alt: [f32; 4], | |
| 61 | pub text: [f32; 4], | |
| 62 | pub text_dim: [f32; 4], | |
| 63 | pub accent: [f32; 4], | |
| 64 | } | |
| 65 | ||
| 66 | impl KeyframesPalette { | |
| 67 | pub fn from_ui(theme: &ui::Theme) -> Self { | |
| 68 | let b = theme.base; | |
| 69 | let lift = |c: [f32; 4], d: f32| { | |
| 70 | [(c[0] + d).clamp(0.0, 1.0), (c[1] + d).clamp(0.0, 1.0), (c[2] + d).clamp(0.0, 1.0), 1.0] | |
| 71 | }; | |
| 72 | Self { | |
| 73 | bg: b, | |
| 74 | sidebar: lift(b, 0.012), | |
| 75 | ruler: lift(b, 0.018), | |
| 76 | rule: theme.rule, | |
| 77 | grid: lift(b, 0.022), | |
| 78 | row_alt: lift(b, 0.010), | |
| 79 | text: theme.text, | |
| 80 | text_dim: theme.text_dim, | |
| 81 | accent: theme.accent, | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | pub struct KeyframesView { | |
| 87 | pub t_range: [f64; 2], | |
| 88 | pub size: [f32; 2], | |
| 89 | pub scroll_y: f32, | |
| 90 | pub selected_only: bool, | |
| 91 | pub selected_keys: HashSet<DopeKeyRef>, | |
| 92 | pub cached_ranges: Vec<(i64, i64)>, | |
| 93 | pub audio_peaks: Vec<f32>, | |
| 94 | pub audio_range: [i64; 2], | |
| 95 | pub palette: KeyframesPalette, | |
| 96 | rows: Vec<TrackRow>, | |
| 97 | collapsed_nodes: HashSet<NodeId>, | |
| 98 | hovered_key: Option<DopeKeyRef>, | |
| 99 | retime_drag: Option<RetimeDrag>, | |
| 100 | box_select: Option<([f32; 2], [f32; 2])>, | |
| 101 | panning: Option<([f32; 2], [f64; 2], f32)>, | |
| 102 | scrubbing: bool, | |
| 103 | pointer_local: [f32; 2], | |
| 104 | fitted: bool, | |
| 105 | text_cache: HashMap<String, text::Layout>, | |
| 106 | } | |
| 107 | ||
| 108 | impl KeyframesView { | |
| 109 | pub fn new(theme: &ui::Theme) -> Self { | |
| 110 | Self { | |
| 111 | t_range: [0.0, 200.0], | |
| 112 | size: [800.0, 320.0], | |
| 113 | scroll_y: 0.0, | |
| 114 | selected_only: false, | |
| 115 | selected_keys: HashSet::new(), | |
| 116 | cached_ranges: Vec::new(), | |
| 117 | audio_peaks: Vec::new(), | |
| 118 | audio_range: [0, 0], | |
| 119 | palette: KeyframesPalette::from_ui(theme), | |
| 120 | rows: Vec::new(), | |
| 121 | collapsed_nodes: HashSet::new(), | |
| 122 | hovered_key: None, | |
| 123 | retime_drag: None, | |
| 124 | box_select: None, | |
| 125 | panning: None, | |
| 126 | scrubbing: false, | |
| 127 | pointer_local: [0.0, 0.0], | |
| 128 | fitted: false, | |
| 129 | text_cache: HashMap::new(), | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | pub fn background(&self) -> [f32; 4] { | |
| 134 | self.palette.bg | |
| 135 | } | |
| 136 | ||
| 137 | pub fn resize(&mut self, size: [f32; 2]) { | |
| 138 | self.size = [size[0].max(1.0), size[1].max(1.0)]; | |
| 139 | } | |
| 140 | ||
| 141 | pub fn fit_comp(&mut self, comp: &Comp) { | |
| 142 | let r0 = comp.render_range[0] as f64; | |
| 143 | let r1 = comp.render_range[1].max(comp.render_range[0] + 10) as f64; | |
| 144 | let pad = ((r1 - r0) * 0.04).max(4.0); | |
| 145 | self.t_range = [r0 - pad, r1 + pad]; | |
| 146 | self.fitted = true; | |
| 147 | } | |
| 148 | ||
| 149 | pub fn sync_tracks(&mut self, comp: &Comp, selected_node: Option<NodeId>) { | |
| 150 | if !self.fitted { | |
| 151 | self.fit_comp(comp); | |
| 152 | } | |
| 153 | self.rows = build_tracks(comp, selected_node, self.selected_only, &self.collapsed_nodes); | |
| 154 | } | |
| 155 | ||
| 156 | fn tracks_top(&self) -> f32 { | |
| 157 | RULER_H + if self.audio_peaks.is_empty() { 0.0 } else { AUDIO_ROW_H } | |
| 158 | } | |
| 159 | ||
| 160 | fn plot_rect(&self) -> [f32; 4] { | |
| 161 | [ | |
| 162 | SIDEBAR_W, | |
| 163 | RULER_H, | |
| 164 | self.size[0].max(SIDEBAR_W + 40.0), | |
| 165 | (self.size[1] - FOOTER_H).max(RULER_H + 40.0), | |
| 166 | ] | |
| 167 | } | |
| 168 | ||
| 169 | fn time_to_x(&self, t: f64) -> f32 { | |
| 170 | let r = self.plot_rect(); | |
| 171 | let frac = (t - self.t_range[0]) / (self.t_range[1] - self.t_range[0]).max(1e-6); | |
| 172 | r[0] + frac as f32 * (r[2] - r[0]) | |
| 173 | } | |
| 174 | ||
| 175 | fn x_to_time(&self, x: f32) -> f64 { | |
| 176 | let r = self.plot_rect(); | |
| 177 | let frac = f64::from((x - r[0]) / (r[2] - r[0]).max(1.0)); | |
| 178 | self.t_range[0] + frac * (self.t_range[1] - self.t_range[0]) | |
| 179 | } | |
| 180 | ||
| 181 | fn row_y(&self, row_idx: usize) -> f32 { | |
| 182 | self.tracks_top() + row_idx as f32 * ROW_H - self.scroll_y | |
| 183 | } | |
| 184 | ||
| 185 | fn hit_key(&self, comp: &Comp, local: [f32; 2]) -> Option<DopeKeyRef> { | |
| 186 | let top = self.tracks_top(); | |
| 187 | if local[0] < SIDEBAR_W || local[1] < top || local[1] > self.size[1] - FOOTER_H { | |
| 188 | return None; | |
| 189 | } | |
| 190 | let mut best: Option<(f32, DopeKeyRef)> = None; | |
| 191 | for (ri, row) in self.rows.iter().enumerate() { | |
| 192 | let cy = self.row_y(ri) + ROW_H * 0.5; | |
| 193 | if (local[1] - cy).abs() > ROW_H * 0.52 { | |
| 194 | continue; | |
| 195 | } | |
| 196 | if let TrackRow::Channel { spline, .. } = row { | |
| 197 | let Some(sp) = comp.splines.get(spline) else { continue }; | |
| 198 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 199 | let kx = self.time_to_x(kf.time); | |
| 200 | let d = ((local[0] - kx).powi(2) + (local[1] - cy).powi(2)).sqrt(); | |
| 201 | if d <= 9.5 && best.as_ref().is_none_or(|(bd, _)| d < *bd) { | |
| 202 | best = Some((d, DopeKeyRef { spline: spline.clone(), key_idx: ki })); | |
| 203 | } | |
| 204 | } | |
| 205 | } | |
| 206 | } | |
| 207 | best.map(|(_, k)| k) | |
| 208 | } | |
| 209 | ||
| 210 | fn hit_span_bar(&self, comp: &Comp, local: [f32; 2]) -> Option<(String, Vec<usize>)> { | |
| 211 | let top = self.tracks_top(); | |
| 212 | if local[0] < SIDEBAR_W || local[1] < top || local[1] > self.size[1] - FOOTER_H { | |
| 213 | return None; | |
| 214 | } | |
| 215 | for (ri, row) in self.rows.iter().enumerate() { | |
| 216 | let y0 = self.row_y(ri); | |
| 217 | if local[1] < y0 || local[1] >= y0 + ROW_H { | |
| 218 | continue; | |
| 219 | } | |
| 220 | if let TrackRow::Channel { spline, .. } = row { | |
| 221 | let Some(sp) = comp.splines.get(spline) else { continue }; | |
| 222 | if sp.keyframes.len() >= 2 { | |
| 223 | let x0 = self.time_to_x(sp.keyframes.first().unwrap().time); | |
| 224 | let x1 = self.time_to_x(sp.keyframes.last().unwrap().time); | |
| 225 | if local[0] >= x0 && local[0] <= x1 { | |
| 226 | return Some((spline.clone(), (0..sp.keyframes.len()).collect())); | |
| 227 | } | |
| 228 | } | |
| 229 | } | |
| 230 | } | |
| 231 | None | |
| 232 | } | |
| 233 | ||
| 234 | fn begin_retime_drag(&mut self, comp: &Comp) { | |
| 235 | if self.selected_keys.is_empty() { | |
| 236 | return; | |
| 237 | } | |
| 238 | let mut saved = HashMap::new(); | |
| 239 | for kref in &self.selected_keys { | |
| 240 | if let Some(sp) = comp.splines.get(&kref.spline) { | |
| 241 | saved.entry(kref.spline.clone()).or_insert_with(|| sp.keyframes.clone()); | |
| 242 | } | |
| 243 | } | |
| 244 | self.retime_drag = Some(RetimeDrag { | |
| 245 | start_frame: self.x_to_time(self.pointer_local[0]).round(), | |
| 246 | saved_splines: saved, | |
| 247 | pre_edit_comp: comp.clone(), | |
| 248 | moved: false, | |
| 249 | }); | |
| 250 | } | |
| 251 | ||
| 252 | fn apply_retime_drag(&mut self, comp: &mut Comp) -> bool { | |
| 253 | let cur_frame = self.x_to_time(self.pointer_local[0]).round(); | |
| 254 | let Some(drag) = &mut self.retime_drag else { return false }; | |
| 255 | let dt = cur_frame - drag.start_frame; | |
| 256 | if dt.abs() < 0.5 && !drag.moved { | |
| 257 | return false; | |
| 258 | } | |
| 259 | drag.moved = true; | |
| 260 | retime_selected_keys(comp, &drag.saved_splines, &self.selected_keys, dt); | |
| 261 | true | |
| 262 | } | |
| 263 | ||
| 264 | fn finish_retime_drag(&mut self, comp: &mut Comp, history: &mut CompHistory) -> bool { | |
| 265 | let Some(drag) = self.retime_drag.take() else { return false }; | |
| 266 | if !drag.moved { | |
| 267 | return false; | |
| 268 | } | |
| 269 | for sname in drag.saved_splines.keys() { | |
| 270 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 271 | sp.sort(); | |
| 272 | } | |
| 273 | } | |
| 274 | comp.revision += 1; | |
| 275 | history.push_snapshot(&drag.pre_edit_comp, "Retime Keyframes"); | |
| 276 | true | |
| 277 | } | |
| 278 | ||
| 279 | fn delete_selected(&mut self, comp: &mut Comp, history: &mut CompHistory) -> bool { | |
| 280 | if self.selected_keys.is_empty() { | |
| 281 | return false; | |
| 282 | } | |
| 283 | let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new(); | |
| 284 | for kref in &self.selected_keys { | |
| 285 | by_spline.entry(kref.spline.clone()).or_default().push(kref.key_idx); | |
| 286 | } | |
| 287 | let pre = comp.clone(); | |
| 288 | let mut changed = false; | |
| 289 | for (sname, mut idxs) in by_spline { | |
| 290 | idxs.sort_unstable(); | |
| 291 | idxs.dedup(); | |
| 292 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 293 | for &idx in idxs.iter().rev() { | |
| 294 | if sp.keyframes.len() > 1 && idx < sp.keyframes.len() { | |
| 295 | sp.keyframes.remove(idx); | |
| 296 | changed = true; | |
| 297 | } | |
| 298 | } | |
| 299 | } | |
| 300 | } | |
| 301 | if changed { | |
| 302 | self.selected_keys.clear(); | |
| 303 | comp.revision += 1; | |
| 304 | history.push_snapshot(&pre, "Delete Keyframes"); | |
| 305 | } | |
| 306 | changed | |
| 307 | } | |
| 308 | ||
| 309 | pub fn input( | |
| 310 | &mut self, | |
| 311 | comp: &mut Comp, | |
| 312 | history: &mut CompHistory, | |
| 313 | selected_node: Option<NodeId>, | |
| 314 | event: ui::Event, | |
| 315 | rect: [f32; 4], | |
| 316 | pointer_window: [f32; 2], | |
| 317 | modifiers: ModifiersState, | |
| 318 | ) -> Vec<KeyframesAction> { | |
| 319 | let mut actions = Vec::new(); | |
| 320 | self.pointer_local = [pointer_window[0] - rect[0], pointer_window[1] - rect[1]]; | |
| 321 | let plot = self.plot_rect(); | |
| 322 | ||
| 323 | match event { | |
| 324 | ui::Event::PointerMoved(p) => { | |
| 325 | self.pointer_local = [p[0] - rect[0], p[1] - rect[1]]; | |
| 326 | if self.scrubbing { | |
| 327 | actions.push(KeyframesAction::ScrubFrame(self.x_to_time(self.pointer_local[0]).round())); | |
| 328 | } else if let Some((sp, tr, sy)) = self.panning { | |
| 329 | let dx = f64::from((self.pointer_local[0] - sp[0]) / (plot[2] - plot[0]).max(1.0)); | |
| 330 | let dt = dx * (tr[1] - tr[0]); | |
| 331 | self.t_range = [tr[0] - dt, tr[1] - dt]; | |
| 332 | self.scroll_y = (sy - (self.pointer_local[1] - sp[1])).max(0.0); | |
| 333 | } else if self.retime_drag.is_some() { | |
| 334 | if self.apply_retime_drag(comp) { | |
| 335 | actions.push(KeyframesAction::KeyframesEdited); | |
| 336 | } | |
| 337 | } else if let Some((_, ref mut cur)) = self.box_select { | |
| 338 | *cur = self.pointer_local; | |
| 339 | } else { | |
| 340 | self.hovered_key = self.hit_key(comp, self.pointer_local); | |
| 341 | } | |
| 342 | } | |
| 343 | ui::Event::Button { button, pressed, .. } => { | |
| 344 | if button == MouseButton::Middle || (button == MouseButton::Left && modifiers.alt_key()) { | |
| 345 | if pressed { | |
| 346 | self.panning = Some((self.pointer_local, self.t_range, self.scroll_y)); | |
| 347 | } else { | |
| 348 | self.panning = None; | |
| 349 | } | |
| 350 | } else if button == MouseButton::Left { | |
| 351 | if pressed { | |
| 352 | // Sidebar header "All / Sel" toggle pill | |
| 353 | if self.pointer_local[0] >= SIDEBAR_W - 48.0 | |
| 354 | && self.pointer_local[0] <= SIDEBAR_W - 6.0 | |
| 355 | && self.pointer_local[1] >= 4.0 | |
| 356 | && self.pointer_local[1] <= RULER_H - 4.0 | |
| 357 | { | |
| 358 | self.selected_only = !self.selected_only; | |
| 359 | self.scroll_y = 0.0; | |
| 360 | self.sync_tracks(comp, selected_node); | |
| 361 | } else if self.pointer_local[0] < SIDEBAR_W && self.pointer_local[1] >= self.tracks_top() { | |
| 362 | let ri = ((self.pointer_local[1] - self.tracks_top() + self.scroll_y) / ROW_H).floor() as isize; | |
| 363 | if ri >= 0 && (ri as usize) < self.rows.len() { | |
| 364 | match &self.rows[ri as usize] { | |
| 365 | TrackRow::NodeHeader { node_id, .. } => { | |
| 366 | let id = *node_id; | |
| 367 | if self.pointer_local[0] < 24.0 { | |
| 368 | if !self.collapsed_nodes.remove(&id) { | |
| 369 | self.collapsed_nodes.insert(id); | |
| 370 | } | |
| 371 | self.sync_tracks(comp, selected_node); | |
| 372 | } else { | |
| 373 | actions.push(KeyframesAction::SelectNode(id)); | |
| 374 | } | |
| 375 | } | |
| 376 | TrackRow::Channel { node_id, .. } => { | |
| 377 | actions.push(KeyframesAction::SelectNode(*node_id)); | |
| 378 | } | |
| 379 | } | |
| 380 | } | |
| 381 | } else if self.pointer_local[0] >= SIDEBAR_W && self.pointer_local[1] < RULER_H { | |
| 382 | self.scrubbing = true; | |
| 383 | actions.push(KeyframesAction::ScrubFrame(self.x_to_time(self.pointer_local[0]).round())); | |
| 384 | } else if let Some(hit) = self.hit_key(comp, self.pointer_local) { | |
| 385 | if modifiers.shift_key() { | |
| 386 | if !self.selected_keys.remove(&hit) { | |
| 387 | self.selected_keys.insert(hit); | |
| 388 | } | |
| 389 | } else if !self.selected_keys.contains(&hit) { | |
| 390 | self.selected_keys.clear(); | |
| 391 | self.selected_keys.insert(hit); | |
| 392 | } | |
| 393 | self.begin_retime_drag(comp); | |
| 394 | } else if let Some((sname, idxs)) = self.hit_span_bar(comp, self.pointer_local) { | |
| 395 | if !modifiers.shift_key() { | |
| 396 | self.selected_keys.clear(); | |
| 397 | } | |
| 398 | for ki in idxs { | |
| 399 | self.selected_keys.insert(DopeKeyRef { spline: sname.clone(), key_idx: ki }); | |
| 400 | } | |
| 401 | self.begin_retime_drag(comp); | |
| 402 | } else if self.pointer_local[0] >= SIDEBAR_W { | |
| 403 | if !modifiers.shift_key() { | |
| 404 | self.selected_keys.clear(); | |
| 405 | } | |
| 406 | self.box_select = Some((self.pointer_local, self.pointer_local)); | |
| 407 | } | |
| 408 | } else { | |
| 409 | self.scrubbing = false; | |
| 410 | if self.finish_retime_drag(comp, history) { | |
| 411 | actions.push(KeyframesAction::KeyframesEdited); | |
| 412 | } | |
| 413 | if let Some((a, b)) = self.box_select.take() { | |
| 414 | let x0 = a[0].min(b[0]); | |
| 415 | let x1 = a[0].max(b[0]); | |
| 416 | let y0 = a[1].min(b[1]); | |
| 417 | let y1 = a[1].max(b[1]); | |
| 418 | if (x1 - x0) > 4.0 || (y1 - y0) > 4.0 { | |
| 419 | for (ri, row) in self.rows.iter().enumerate() { | |
| 420 | let cy = self.row_y(ri) + ROW_H * 0.5; | |
| 421 | if cy < y0 || cy > y1 { | |
| 422 | continue; | |
| 423 | } | |
| 424 | if let TrackRow::Channel { spline, .. } = row { | |
| 425 | if let Some(sp) = comp.splines.get(spline) { | |
| 426 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 427 | let kx = self.time_to_x(kf.time); | |
| 428 | if kx >= x0 && kx <= x1 { | |
| 429 | self.selected_keys.insert(DopeKeyRef { | |
| 430 | spline: spline.clone(), | |
| 431 | key_idx: ki, | |
| 432 | }); | |
| 433 | } | |
| 434 | } | |
| 435 | } | |
| 436 | } | |
| 437 | } | |
| 438 | } | |
| 439 | } | |
| 440 | } | |
| 441 | } | |
| 442 | } | |
| 443 | ui::Event::Wheel(delta) => { | |
| 444 | if self.pointer_local[0] < SIDEBAR_W || modifiers.shift_key() { | |
| 445 | let max_scroll = (self.rows.len() as f32 * ROW_H - (self.size[1] - self.tracks_top() - FOOTER_H)).max(0.0); | |
| 446 | self.scroll_y = (self.scroll_y - delta[1]).clamp(0.0, max_scroll); | |
| 447 | } else { | |
| 448 | let f = f64::from((1.0 - delta[1] * 0.0045).clamp(0.7, 1.4)); | |
| 449 | let p = self.x_to_time(self.pointer_local[0]); | |
| 450 | let t0 = p - (p - self.t_range[0]) * f; | |
| 451 | let t1 = p + (self.t_range[1] - p) * f; | |
| 452 | if (t1 - t0) > 4.0 && (t1 - t0) < 100_000.0 { | |
| 453 | self.t_range = [t0, t1]; | |
| 454 | } | |
| 455 | } | |
| 456 | } | |
| 457 | ui::Event::KeyInput { physical, key, state: ElementState::Pressed, repeat: false, modifiers: kmod, .. } => { | |
| 458 | if physical == PhysicalKey::Code(KeyCode::NumpadDecimal) { | |
| 459 | self.fit_comp(comp); | |
| 460 | } else { | |
| 461 | self.handle_key(comp, history, &key, kmod, &mut actions); | |
| 462 | } | |
| 463 | } | |
| 464 | ui::Event::Key { key, modifiers: kmod, .. } => { | |
| 465 | self.handle_key(comp, history, &key, kmod, &mut actions); | |
| 466 | } | |
| 467 | _ => {} | |
| 468 | } | |
| 469 | ||
| 470 | actions | |
| 471 | } | |
| 472 | ||
| 473 | fn handle_key( | |
| 474 | &mut self, | |
| 475 | comp: &mut Comp, | |
| 476 | history: &mut CompHistory, | |
| 477 | key: &Key, | |
| 478 | kmod: ModifiersState, | |
| 479 | actions: &mut Vec<KeyframesAction>, | |
| 480 | ) { | |
| 481 | match key { | |
| 482 | Key::Named(NamedKey::Delete | NamedKey::Backspace) => { | |
| 483 | if self.delete_selected(comp, history) { | |
| 484 | actions.push(KeyframesAction::KeyframesEdited); | |
| 485 | } | |
| 486 | } | |
| 487 | Key::Character(c) => match c.to_ascii_lowercase().as_str() { | |
| 488 | "f" | "." => self.fit_comp(comp), | |
| 489 | "x" if !kmod.super_key() => { | |
| 490 | if self.delete_selected(comp, history) { | |
| 491 | actions.push(KeyframesAction::KeyframesEdited); | |
| 492 | } | |
| 493 | } | |
| 494 | _ => {} | |
| 495 | }, | |
| 496 | _ => {} | |
| 497 | } | |
| 498 | } | |
| 499 | ||
| 500 | pub fn prepare_labels(&mut self) { | |
| 501 | let font_sm = text::Font { | |
| 502 | families: Arc::from([text::Family::Mono]), | |
| 503 | size: 10.0, | |
| 504 | weight: 500, | |
| 505 | italic: false, | |
| 506 | }; | |
| 507 | let font_row = text::Font { | |
| 508 | families: Arc::from([text::Family::Sans]), | |
| 509 | size: 11.0, | |
| 510 | weight: 500, | |
| 511 | italic: false, | |
| 512 | }; | |
| 513 | let font_hdr = text::Font { | |
| 514 | families: Arc::from([text::Family::Sans]), | |
| 515 | size: 11.0, | |
| 516 | weight: 600, | |
| 517 | italic: false, | |
| 518 | }; | |
| 519 | let make = |s: &str, font: &text::Font| { | |
| 520 | let styles = [text::Style::new(font.clone())]; | |
| 521 | text::Paragraph { | |
| 522 | text: s, | |
| 523 | styles: &styles, | |
| 524 | spans: &[], | |
| 525 | replacements: &[], | |
| 526 | width: None, | |
| 527 | indent: 0.0, | |
| 528 | line_height: 1.2, | |
| 529 | align: text::Align::Left, | |
| 530 | tab: 32.0, | |
| 531 | } | |
| 532 | .layout() | |
| 533 | }; | |
| 534 | ||
| 535 | for (k, txt, f) in [ | |
| 536 | ("hdr:title", "Keyframes", &font_hdr), | |
| 537 | ("hdr:all", "All", &font_sm), | |
| 538 | ("hdr:sel", "Sel", &font_sm), | |
| 539 | ("hdr:audio", "Audio", &font_row), | |
| 540 | ("footer", "Drag keys/spans to retime · Ruler: Scrub · Wheel: Zoom · Del: Remove · F: Fit", &font_sm), | |
| 541 | ] { | |
| 542 | self.text_cache.entry(k.to_owned()).or_insert_with(|| make(txt, f)); | |
| 543 | } | |
| 544 | ||
| 545 | for row in &self.rows { | |
| 546 | match row { | |
| 547 | TrackRow::NodeHeader { node_id, label, .. } => { | |
| 548 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 549 | let text = format!("{glyph} {label}"); | |
| 550 | self.text_cache.entry(format!("nh:{text}")).or_insert_with(|| make(&text, &font_hdr)); | |
| 551 | } | |
| 552 | TrackRow::Channel { label, .. } => { | |
| 553 | self.text_cache.entry(format!("ch:{label}")).or_insert_with(|| make(label, &font_row)); | |
| 554 | } | |
| 555 | } | |
| 556 | } | |
| 557 | ||
| 558 | let plot = self.plot_rect(); | |
| 559 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 560 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 561 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 562 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 563 | for ti in t_start..=t_end { | |
| 564 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); | |
| 565 | self.text_cache.entry(key.clone()).or_insert_with(|| make(&key[2..], &font_sm)); | |
| 566 | } | |
| 567 | } | |
| 568 | ||
| 569 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 570 | let mut out = Vec::with_capacity(512); | |
| 571 | let [w, h] = self.size; | |
| 572 | let plot = self.plot_rect(); | |
| 573 | let top = self.tracks_top(); | |
| 574 | ||
| 575 | // Sidebar + Ruler chrome | |
| 576 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); | |
| 577 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); | |
| 578 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); | |
| 579 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); | |
| 580 | ||
| 581 | if let Some(tl) = self.text_cache.get("hdr:title") { | |
| 582 | out.push(Primitive::Text { | |
| 583 | text: tl as &dyn Glyphs, | |
| 584 | origin: [10.0, 6.0], | |
| 585 | clip: Some([8.0, 0.0, SIDEBAR_W - 52.0, RULER_H]), | |
| 586 | ink: self.palette.text, | |
| 587 | }); | |
| 588 | } | |
| 589 | // "All / Sel" filter pill | |
| 590 | let pill_rect = [SIDEBAR_W - 46.0, 4.0, SIDEBAR_W - 8.0, RULER_H - 4.0]; | |
| 591 | out.push(Primitive::RoundedRect { | |
| 592 | rect: pill_rect, | |
| 593 | radius: 4.0, | |
| 594 | stroke: None, | |
| 595 | color: if self.selected_only { | |
| 596 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] | |
| 597 | } else { | |
| 598 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.7] | |
| 599 | }, | |
| 600 | }); | |
| 601 | out.push(Primitive::RoundedRect { | |
| 602 | rect: pill_rect, | |
| 603 | radius: 4.0, | |
| 604 | stroke: Some(1.0), | |
| 605 | color: if self.selected_only { self.palette.accent } else { self.palette.rule }, | |
| 606 | }); | |
| 607 | let mode_key = if self.selected_only { "hdr:sel" } else { "hdr:all" }; | |
| 608 | if let Some(ml) = self.text_cache.get(mode_key) { | |
| 609 | out.push(Primitive::Text { | |
| 610 | text: ml as &dyn Glyphs, | |
| 611 | origin: [pill_rect[0] + 9.0, 7.0], | |
| 612 | clip: Some(pill_rect), | |
| 613 | ink: if self.selected_only { self.palette.text } else { self.palette.text_dim }, | |
| 614 | }); | |
| 615 | } | |
| 616 | ||
| 617 | // Ruler frame ticks | |
| 618 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 619 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 620 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 621 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 622 | for ti in t_start..=t_end { | |
| 623 | let t = ti as f64 * t_step; | |
| 624 | let x = self.time_to_x(t); | |
| 625 | if x < plot[0] || x > plot[2] { | |
| 626 | continue; | |
| 627 | } | |
| 628 | out.push(Primitive::Segment { from: [x, RULER_H], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); | |
| 629 | let key = format!("t:{}", t.round() as i64); | |
| 630 | if let Some(tl) = self.text_cache.get(&key) { | |
| 631 | out.push(Primitive::Text { | |
| 632 | text: tl as &dyn Glyphs, | |
| 633 | origin: [x + 3.0, 5.0], | |
| 634 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), | |
| 635 | ink: self.palette.text_dim, | |
| 636 | }); | |
| 637 | } | |
| 638 | } | |
| 639 | ||
| 640 | // Green RAM cache bar along bottom edge of ruler | |
| 641 | for &(c0, c1) in &self.cached_ranges { | |
| 642 | let x0 = self.time_to_x(c0 as f64).max(plot[0]); | |
| 643 | let x1 = self.time_to_x((c1 + 1) as f64).min(plot[2]); | |
| 644 | if x1 > x0 { | |
| 645 | out.push(Primitive::Rect { | |
| 646 | rect: [x0, RULER_H - 3.5, x1.max(x0 + 2.0), RULER_H - 0.5], | |
| 647 | color: [0.22, 0.86, 0.48, 0.92], | |
| 648 | }); | |
| 649 | } | |
| 650 | } | |
| 651 | ||
| 652 | // Dedicated Audio Waveform row when audio is present | |
| 653 | if !self.audio_peaks.is_empty() { | |
| 654 | let ay0 = RULER_H; | |
| 655 | let ay1 = RULER_H + AUDIO_ROW_H; | |
| 656 | out.push(Primitive::Rect { rect: [0.0, ay0, w, ay1], color: self.palette.row_alt }); | |
| 657 | out.push(Primitive::Segment { from: [0.0, ay1], to: [w, ay1], width: 1.0, color: self.palette.rule }); | |
| 658 | if let Some(al) = self.text_cache.get("hdr:audio") { | |
| 659 | out.push(Primitive::Text { | |
| 660 | text: al as &dyn Glyphs, | |
| 661 | origin: [12.0, ay0 + 5.0], | |
| 662 | clip: Some([8.0, ay0, SIDEBAR_W - 8.0, ay1]), | |
| 663 | ink: [0.54, 0.80, 1.0, 0.95], | |
| 664 | }); | |
| 665 | } | |
| 666 | out.push(Primitive::PushClip { rect: [plot[0], ay0, plot[2], ay1], radius: 0.0 }); | |
| 667 | let mid_y = 0.5 * (ay0 + ay1); | |
| 668 | let max_half = AUDIO_ROW_H * 0.42; | |
| 669 | let a0 = self.audio_range[0]; | |
| 670 | let n = self.audio_peaks.len() as i64; | |
| 671 | let f_vis0 = (self.t_range[0].floor() as i64).max(a0); | |
| 672 | let f_vis1 = (self.t_range[1].ceil() as i64).min(a0 + n - 1); | |
| 673 | let bar_step = ((plot[2] - plot[0]) / ((self.t_range[1] - self.t_range[0]).max(1.0) as f32)).max(1.5); | |
| 674 | let stride = ((1.8 / bar_step).ceil() as i64).max(1); | |
| 675 | let mut f = f_vis0; | |
| 676 | while f <= f_vis1 { | |
| 677 | let mut peak = 0.0_f32; | |
| 678 | for off in 0..stride { | |
| 679 | let idx = (f + off - a0) as usize; | |
| 680 | if let Some(&v) = self.audio_peaks.get(idx) { | |
| 681 | if v > peak { peak = v; } | |
| 682 | } | |
| 683 | } | |
| 684 | if peak > 0.01 { | |
| 685 | let x = self.time_to_x(f as f64); | |
| 686 | let hh = (peak * max_half).max(1.0); | |
| 687 | let bw = (bar_step * stride as f32 - 0.6).max(1.2); | |
| 688 | out.push(Primitive::Rect { | |
| 689 | rect: [x, mid_y - hh, x + bw, mid_y + hh], | |
| 690 | color: [0.46, 0.74, 0.98, 0.55], | |
| 691 | }); | |
| 692 | } | |
| 693 | f += stride; | |
| 694 | } | |
| 695 | out.push(Primitive::PopClip); | |
| 696 | } | |
| 697 | ||
| 698 | // Render range subtle shading | |
| 699 | let rx0 = self.time_to_x(comp.render_range[0] as f64); | |
| 700 | let rx1 = self.time_to_x(comp.render_range[1] as f64); | |
| 701 | out.push(Primitive::PushClip { rect: [plot[0], top, plot[2], plot[3]], radius: 0.0 }); | |
| 702 | if rx0 > plot[0] { | |
| 703 | out.push(Primitive::Rect { rect: [plot[0], top, rx0.min(plot[2]), plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 704 | } | |
| 705 | if rx1 < plot[2] { | |
| 706 | out.push(Primitive::Rect { rect: [rx1.max(plot[0]), top, plot[2], plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 707 | } | |
| 708 | out.push(Primitive::PopClip); | |
| 709 | ||
| 710 | // Track rows (clipped to track area) | |
| 711 | out.push(Primitive::PushClip { rect: [0.0, top, w, plot[3]], radius: 0.0 }); | |
| 712 | for (ri, row) in self.rows.iter().enumerate() { | |
| 713 | let y0 = self.row_y(ri); | |
| 714 | let y1 = y0 + ROW_H; | |
| 715 | if y1 < top || y0 > plot[3] { | |
| 716 | continue; | |
| 717 | } | |
| 718 | if ri % 2 == 1 { | |
| 719 | out.push(Primitive::Rect { rect: [0.0, y0, w, y1], color: self.palette.row_alt }); | |
| 720 | } | |
| 721 | out.push(Primitive::Segment { from: [0.0, y1], to: [w, y1], width: 1.0, color: self.palette.grid }); | |
| 722 | ||
| 723 | match row { | |
| 724 | TrackRow::NodeHeader { node_id, label, splines } => { | |
| 725 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 726 | let key = format!("nh:{glyph} {label}"); | |
| 727 | if let Some(tl) = self.text_cache.get(&key) { | |
| 728 | out.push(Primitive::Text { | |
| 729 | text: tl as &dyn Glyphs, | |
| 730 | origin: [10.0, y0 + 4.0], | |
| 731 | clip: Some([6.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 732 | ink: self.palette.text, | |
| 733 | }); | |
| 734 | } | |
| 735 | // Summary span + tick dots on node header row | |
| 736 | let mut t_min = f64::INFINITY; | |
| 737 | let mut t_max = f64::NEG_INFINITY; | |
| 738 | for sname in splines { | |
| 739 | if let Some(sp) = comp.splines.get(sname) { | |
| 740 | for kf in &sp.keyframes { | |
| 741 | t_min = t_min.min(kf.time); | |
| 742 | t_max = t_max.max(kf.time); | |
| 743 | } | |
| 744 | } | |
| 745 | } | |
| 746 | if t_min.is_finite() { | |
| 747 | let sx0 = self.time_to_x(t_min).max(plot[0]); | |
| 748 | let sx1 = self.time_to_x(t_max).min(plot[2]); | |
| 749 | if sx1 >= sx0 { | |
| 750 | out.push(Primitive::RoundedRect { | |
| 751 | rect: [sx0, y0 + 6.0, sx1.max(sx0 + 4.0), y1 - 6.0], | |
| 752 | radius: 3.0, | |
| 753 | stroke: None, | |
| 754 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.22], | |
| 755 | }); | |
| 756 | } | |
| 757 | } | |
| 758 | } | |
| 759 | TrackRow::Channel { label, spline, color, .. } => { | |
| 760 | out.push(Primitive::RoundedRect { | |
| 761 | rect: [20.0, y0 + 7.0, 28.0, y0 + 15.0], | |
| 762 | radius: 2.0, | |
| 763 | stroke: None, | |
| 764 | color: *color, | |
| 765 | }); | |
| 766 | if let Some(tl) = self.text_cache.get(&format!("ch:{label}")) { | |
| 767 | out.push(Primitive::Text { | |
| 768 | text: tl as &dyn Glyphs, | |
| 769 | origin: [34.0, y0 + 4.0], | |
| 770 | clip: Some([32.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 771 | ink: self.palette.text_dim, | |
| 772 | }); | |
| 773 | } | |
| 774 | ||
| 775 | if let Some(sp) = comp.splines.get(spline) { | |
| 776 | if sp.keyframes.len() >= 2 { | |
| 777 | let sx0 = self.time_to_x(sp.keyframes.first().unwrap().time).max(plot[0]); | |
| 778 | let sx1 = self.time_to_x(sp.keyframes.last().unwrap().time).min(plot[2]); | |
| 779 | if sx1 > sx0 { | |
| 780 | out.push(Primitive::RoundedRect { | |
| 781 | rect: [sx0, y0 + 8.0, sx1, y1 - 8.0], | |
| 782 | radius: 2.0, | |
| 783 | stroke: None, | |
| 784 | color: [color[0], color[1], color[2], 0.28], | |
| 785 | }); | |
| 786 | } | |
| 787 | } | |
| 788 | let cy = y0 + ROW_H * 0.5; | |
| 789 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 790 | let kx = self.time_to_x(kf.time); | |
| 791 | if kx < plot[0] - 8.0 || kx > plot[2] + 8.0 { | |
| 792 | continue; | |
| 793 | } | |
| 794 | let kref = DopeKeyRef { spline: spline.clone(), key_idx: ki }; | |
| 795 | let sel = self.selected_keys.contains(&kref); | |
| 796 | let hov = self.hovered_key.as_ref() == Some(&kref); | |
| 797 | let r = if sel || hov { 5.0 } else { 4.0 }; | |
| 798 | out.push(Primitive::RoundedRect { | |
| 799 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 800 | radius: 1.5, | |
| 801 | stroke: None, | |
| 802 | color: if sel { | |
| 803 | [1.0, 0.84, 0.24, 1.0] | |
| 804 | } else if hov { | |
| 805 | [1.0, 1.0, 1.0, 1.0] | |
| 806 | } else { | |
| 807 | *color | |
| 808 | }, | |
| 809 | }); | |
| 810 | out.push(Primitive::RoundedRect { | |
| 811 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 812 | radius: 1.5, | |
| 813 | stroke: Some(1.0), | |
| 814 | color: [0.04, 0.04, 0.06, 0.9], | |
| 815 | }); | |
| 816 | } | |
| 817 | } | |
| 818 | } | |
| 819 | } | |
| 820 | } | |
| 821 | ||
| 822 | // Box select overlay | |
| 823 | if let Some((a, b)) = self.box_select { | |
| 824 | let x0 = a[0].min(b[0]); | |
| 825 | let x1 = a[0].max(b[0]); | |
| 826 | let y0 = a[1].min(b[1]); | |
| 827 | let y1 = a[1].max(b[1]); | |
| 828 | out.push(Primitive::RoundedRect { | |
| 829 | rect: [x0, y0, x1, y1], | |
| 830 | radius: 2.0, | |
| 831 | stroke: None, | |
| 832 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 833 | }); | |
| 834 | out.push(Primitive::RoundedRect { | |
| 835 | rect: [x0, y0, x1, y1], | |
| 836 | radius: 2.0, | |
| 837 | stroke: Some(1.2), | |
| 838 | color: self.palette.accent, | |
| 839 | }); | |
| 840 | } | |
| 841 | out.push(Primitive::PopClip); | |
| 842 | ||
| 843 | // Playhead vertical line | |
| 844 | let px = self.time_to_x(current_frame); | |
| 845 | if px >= plot[0] && px <= plot[2] { | |
| 846 | out.push(Primitive::Segment { | |
| 847 | from: [px, 0.0], | |
| 848 | to: [px, plot[3]], | |
| 849 | width: 1.6, | |
| 850 | color: [0.96, 0.36, 0.34, 0.92], | |
| 851 | }); | |
| 852 | } | |
| 853 | ||
| 854 | // Footer bar | |
| 855 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - FOOTER_H, w, h], color: self.palette.ruler }); | |
| 856 | if let Some(fl) = self.text_cache.get("footer") { | |
| 857 | out.push(Primitive::Text { | |
| 858 | text: fl as &dyn Glyphs, | |
| 859 | origin: [SIDEBAR_W + 10.0, h - FOOTER_H + 5.0], | |
| 860 | clip: Some([SIDEBAR_W + 6.0, h - FOOTER_H, w - 6.0, h]), | |
| 861 | ink: self.palette.text_dim, | |
| 862 | }); | |
| 863 | } | |
| 864 | ||
| 865 | out | |
| 866 | } | |
| 867 | } | |
| 868 | ||
| 869 | pub fn build_tracks( | |
| 870 | comp: &Comp, | |
| 871 | selected_node: Option<NodeId>, | |
| 872 | selected_only: bool, | |
| 873 | collapsed: &HashSet<NodeId>, | |
| 874 | ) -> Vec<TrackRow> { | |
| 875 | let mut rows = Vec::new(); | |
| 876 | let palette: &[[f32; 4]] = &[ | |
| 877 | [0.98, 0.44, 0.62, 1.0], | |
| 878 | [0.36, 0.84, 0.96, 1.0], | |
| 879 | [0.50, 0.92, 0.46, 1.0], | |
| 880 | [0.98, 0.78, 0.28, 1.0], | |
| 881 | [0.74, 0.56, 0.98, 1.0], | |
| 882 | ]; | |
| 883 | let mut color_idx = 0usize; | |
| 884 | ||
| 885 | let iter_ids: Vec<NodeId> = if selected_only { | |
| 886 | selected_node.into_iter().collect() | |
| 887 | } else { | |
| 888 | comp.nodes.iter().map(|n| n.id).collect() | |
| 889 | }; | |
| 890 | ||
| 891 | for id in iter_ids { | |
| 892 | if id.0 >= comp.nodes.len() { | |
| 893 | continue; | |
| 894 | } | |
| 895 | let channels: Vec<(String, String)> = comp | |
| 896 | .node_splines(id) | |
| 897 | .into_iter() | |
| 898 | .filter(|(_, sname)| !sname.contains("LUTIn")) | |
| 899 | .collect(); | |
| 900 | if channels.is_empty() { | |
| 901 | continue; | |
| 902 | } | |
| 903 | let node = comp.node(id); | |
| 904 | let spline_names: Vec<String> = channels.iter().map(|(_, s)| s.clone()).collect(); | |
| 905 | rows.push(TrackRow::NodeHeader { | |
| 906 | node_id: id, | |
| 907 | label: node.name.clone(), | |
| 908 | splines: spline_names, | |
| 909 | }); | |
| 910 | if !collapsed.contains(&id) { | |
| 911 | for (label, spline) in channels { | |
| 912 | let col = comp | |
| 913 | .splines | |
| 914 | .get(&spline) | |
| 915 | .map(|sp| { | |
| 916 | let [r, g, b] = sp.color; | |
| 917 | if r as u16 + g as u16 + b as u16 > 120 { | |
| 918 | [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0] | |
| 919 | } else { | |
| 920 | palette[color_idx % palette.len()] | |
| 921 | } | |
| 922 | }) | |
| 923 | .unwrap_or_else(|| palette[color_idx % palette.len()]); | |
| 924 | color_idx += 1; | |
| 925 | rows.push(TrackRow::Channel { | |
| 926 | node_id: id, | |
| 927 | label, | |
| 928 | spline, | |
| 929 | color: col, | |
| 930 | }); | |
| 931 | } | |
| 932 | } | |
| 933 | } | |
| 934 | rows | |
| 935 | } | |
| 936 | ||
| 937 | pub fn retime_selected_keys( | |
| 938 | comp: &mut Comp, | |
| 939 | saved_splines: &HashMap<String, Vec<Keyframe>>, | |
| 940 | selected: &HashSet<DopeKeyRef>, | |
| 941 | dt: f64, | |
| 942 | ) { | |
| 943 | for (sname, orig) in saved_splines { | |
| 944 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 945 | sp.keyframes = orig.clone(); | |
| 946 | } | |
| 947 | } | |
| 948 | for kref in selected { | |
| 949 | let Some(sp) = comp.splines.get_mut(&kref.spline) else { continue }; | |
| 950 | let Some(kf) = sp.keyframes.get_mut(kref.key_idx) else { continue }; | |
| 951 | kf.time += dt; | |
| 952 | if let Some(lh) = &mut kf.lh { | |
| 953 | lh[0] += dt; | |
| 954 | } | |
| 955 | if let Some(rh) = &mut kf.rh { | |
| 956 | rh[0] += dt; | |
| 957 | } | |
| 958 | } | |
| 959 | comp.revision += 1; | |
| 960 | } | |
| 961 | ||
| 962 | fn nice_step(raw: f64) -> f64 { | |
| 963 | let raw = raw.max(1.0); | |
| 964 | let pow = 10.0_f64.powf(raw.log10().floor()); | |
| 965 | let frac = raw / pow; | |
| 966 | 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 }; | |
| 967 | (nice * pow).max(1.0) | |
| 968 | } | |
| 969 | ||
| 970 | #[cfg(test)] | |
| 971 | mod tests { | |
| 972 | use super::*; | |
| 973 | ||
| 974 | fn sample_animated_comp() -> Comp { | |
| 975 | let src = r#"Composition { | |
| 976 | CurrentTime = 10, | |
| 977 | RenderRange = { 0, 100 }, | |
| 978 | GlobalRange = { 0, 100 }, | |
| 979 | Tools = ordered() { | |
| 980 | XF1 = Transform { | |
| 981 | Inputs = { | |
| 982 | Size = Input { SourceOp = "XF1Size", Source = "Value", }, | |
| 983 | }, | |
| 984 | }, | |
| 985 | XF1Size = BezierSpline { | |
| 986 | SplineColor = { Red = 220, Green = 120, Blue = 80 }, | |
| 987 | KeyFrames = { | |
| 988 | [10] = { 1.0, RH = { 20.0, 1.0 }, Flags = { Linear = false } }, | |
| 989 | [40] = { 2.0, LH = { 30.0, 2.0 }, Flags = { Linear = false } }, | |
| 990 | }, | |
| 991 | }, | |
| 992 | }, | |
| 993 | }"#; | |
| 994 | Comp::from_fusion_str(src).expect("parse animated comp") | |
| 995 | } | |
| 996 | ||
| 997 | #[test] | |
| 998 | fn build_tracks_lists_animated_nodes_and_channels() { | |
| 999 | let comp = sample_animated_comp(); | |
| 1000 | let xf = comp.find_node("XF1").unwrap(); | |
| 1001 | let rows = build_tracks(&comp, Some(xf), false, &HashSet::new()); | |
| 1002 | assert_eq!(rows.len(), 2); | |
| 1003 | assert!(matches!(&rows[0], TrackRow::NodeHeader { label, .. } if label == "XF1")); | |
| 1004 | assert!(matches!(&rows[1], TrackRow::Channel { spline, .. } if spline == "XF1Size")); | |
| 1005 | ||
| 1006 | let mut collapsed = HashSet::new(); | |
| 1007 | collapsed.insert(xf); | |
| 1008 | let collapsed_rows = build_tracks(&comp, Some(xf), false, &collapsed); | |
| 1009 | assert_eq!(collapsed_rows.len(), 1); | |
| 1010 | } | |
| 1011 | ||
| 1012 | #[test] | |
| 1013 | fn retime_shifts_key_and_handles_together_with_undo() { | |
| 1014 | let mut comp = sample_animated_comp(); | |
| 1015 | let mut history = CompHistory::new(); | |
| 1016 | let pre = comp.clone(); | |
| 1017 | let mut saved = HashMap::new(); | |
| 1018 | saved.insert("XF1Size".to_owned(), comp.splines["XF1Size"].keyframes.clone()); | |
| 1019 | let mut sel = HashSet::new(); | |
| 1020 | sel.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 1 }); | |
| 1021 | ||
| 1022 | retime_selected_keys(&mut comp, &saved, &sel, 15.0); | |
| 1023 | history.push_snapshot(&pre, "Retime Keyframes"); | |
| 1024 | ||
| 1025 | let kf1 = &comp.splines["XF1Size"].keyframes[1]; | |
| 1026 | assert!((kf1.time - 55.0).abs() < 1e-6); | |
| 1027 | assert!((kf1.lh.unwrap()[0] - 45.0).abs() < 1e-6, "left handle must shift with key"); | |
| 1028 | ||
| 1029 | assert!(history.undo(&mut comp)); | |
| 1030 | assert!((comp.splines["XF1Size"].keyframes[1].time - 40.0).abs() < 1e-6); | |
| 1031 | } | |
| 1032 | } |
crates/app/src/main.rs created+1857| ... | ... | @@ -0,0 +1,1857 @@ |
| 1 | //! Compositor Studio — docked zero-copy `wgpu` Multi-Viewer (`1`/`2`) + Flow + Blender-style Spline Editor | |
| 2 | //! built on Knife's `draw`, `ui`, `dock`, and `theme::KNIFE` (soft toned quiet iris). | |
| 3 | ||
| 4 | mod add_tool; | |
| 5 | mod inspector; | |
| 6 | mod keyframes; | |
| 7 | ||
| 8 | use add_tool::{AddToolLauncher, ToolEntry}; | |
| 9 | use dock::{Axis as DockAxis, Command, Dock, Event, GroupId, Node as DockNode, Part as DockPart, Place, Plus, Side, SplitId, Tab}; | |
| 10 | use engine::{ | |
| 11 | AudioPlayer, AudioTrack, CacheMode, DEFAULT_RAM_CACHE_MB, Engine, ExportOptions, PrefetchJob, | |
| 12 | RamFrameCache, RamPrefetcher, export_saver_png_sequence, | |
| 13 | }; | |
| 14 | use flow::{FlowAction, FlowView}; | |
| 15 | use inspector::{InspectorAction, InspectorState}; | |
| 16 | use keyframes::{KeyframesAction, KeyframesView}; | |
| 17 | use model::{Comp, CompHistory, NodeId, default_main_input_port}; | |
| 18 | use spline::{SplineAction, SplineView}; | |
| 19 | use std::collections::HashMap; | |
| 20 | use std::error::Error; | |
| 21 | use std::fs::File; | |
| 22 | use std::path::{Path, PathBuf}; | |
| 23 | use std::process::Command as ProcessCommand; | |
| 24 | use std::sync::{Arc, mpsc}; | |
| 25 | use std::time::{Duration, Instant}; | |
| 26 | use ui::host::{Accessibility, Clipboard, Surface}; | |
| 27 | use viewer::{PlaybackClock, SyncMode, ViewerAction, ViewerState}; | |
| 28 | use winit::application::ApplicationHandler; | |
| 29 | use winit::dpi::LogicalSize; | |
| 30 | use winit::event::{ElementState, WindowEvent}; | |
| 31 | use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy}; | |
| 32 | use winit::keyboard::{Key, KeyCode, NamedKey, PhysicalKey}; | |
| 33 | use winit::window::{CursorIcon, Window, WindowId}; | |
| 34 | ||
| 35 | const DEFAULT_COMP: &str = "/Volumes/Project/trees/fusion/20_falling.comp"; | |
| 36 | const PANES: &[&str] = &["Viewer 1", "Viewer 2", "Flow", "Spline", "Keyframes", "Inspector"]; | |
| 37 | ||
| 38 | fn viewer_pane_id(slot: usize) -> ui::Id { ui::Id::ROOT.child(("pane-viewer", slot)) } | |
| 39 | fn viewer_canvas_id(slot: usize) -> ui::Id { ui::Id::ROOT.child(("canvas-viewer", slot)) } | |
| 40 | fn flow_pane_id() -> ui::Id { ui::Id::ROOT.child("pane-flow") } | |
| 41 | fn flow_canvas_id() -> ui::Id { ui::Id::ROOT.child("canvas-flow") } | |
| 42 | fn spline_canvas_id() -> ui::Id { ui::Id::ROOT.child("canvas-spline") } | |
| 43 | fn keyframes_canvas_id() -> ui::Id { ui::Id::ROOT.child("canvas-keyframes") } | |
| 44 | fn inspector_pane_id() -> ui::Id { ui::Id::ROOT.child("pane-inspector") } | |
| 45 | ||
| 46 | struct Cli { | |
| 47 | comp_path: PathBuf, | |
| 48 | snapshot: Option<PathBuf>, | |
| 49 | frame: Option<f64>, | |
| 50 | select_node: Option<String>, | |
| 51 | dual_viewer: bool, | |
| 52 | open_add_tool: bool, | |
| 53 | show_keyframes: bool, | |
| 54 | } | |
| 55 | ||
| 56 | fn parse_cli() -> Cli { | |
| 57 | let args: Vec<String> = std::env::args().collect(); | |
| 58 | let mut comp_path = PathBuf::from(DEFAULT_COMP); | |
| 59 | let mut snapshot = None; | |
| 60 | let mut frame = None; | |
| 61 | let mut select_node = None; | |
| 62 | let mut dual_viewer = false; | |
| 63 | let mut open_add_tool = false; | |
| 64 | let mut show_keyframes = false; | |
| 65 | let mut i = 1; | |
| 66 | while i < args.len() { | |
| 67 | match args[i].as_str() { | |
| 68 | "--snapshot" if i + 1 < args.len() => { | |
| 69 | snapshot = Some(PathBuf::from(&args[i + 1])); | |
| 70 | i += 2; | |
| 71 | } | |
| 72 | "--frame" if i + 1 < args.len() => { | |
| 73 | frame = args[i + 1].parse().ok(); | |
| 74 | i += 2; | |
| 75 | } | |
| 76 | "--select" if i + 1 < args.len() => { | |
| 77 | select_node = Some(args[i + 1].clone()); | |
| 78 | i += 2; | |
| 79 | } | |
| 80 | "--dual" => { | |
| 81 | dual_viewer = true; | |
| 82 | i += 1; | |
| 83 | } | |
| 84 | "--open-add-tool" => { | |
| 85 | open_add_tool = true; | |
| 86 | i += 1; | |
| 87 | } | |
| 88 | "--show-keyframes" => { | |
| 89 | show_keyframes = true; | |
| 90 | i += 1; | |
| 91 | } | |
| 92 | other if !other.starts_with("--") => { | |
| 93 | comp_path = PathBuf::from(other); | |
| 94 | i += 1; | |
| 95 | } | |
| 96 | _ => i += 1, | |
| 97 | } | |
| 98 | } | |
| 99 | Cli { comp_path, snapshot, frame, select_node, dual_viewer, open_add_tool, show_keyframes } | |
| 100 | } | |
| 101 | ||
| 102 | fn main() { | |
| 103 | let cli = parse_cli(); | |
| 104 | let res = match &cli.snapshot { | |
| 105 | Some(out) => run_snapshot(&cli, out), | |
| 106 | None => run_window(&cli), | |
| 107 | }; | |
| 108 | if let Err(e) = res { | |
| 109 | eprintln!("compositor error: {e}"); | |
| 110 | std::process::exit(1); | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | struct Studio { | |
| 115 | ui: ui::Ui, | |
| 116 | dock: Dock<String>, | |
| 117 | layout: dock::Layout<String>, | |
| 118 | seen: dock::Seen<String>, | |
| 119 | renderer: draw::Renderer, | |
| 120 | engine: Engine, | |
| 121 | comp: Comp, | |
| 122 | history: CompHistory, | |
| 123 | playback: PlaybackClock, | |
| 124 | enable_audio: bool, | |
| 125 | audio_player: Option<AudioPlayer>, | |
| 126 | audio_rx: Option<mpsc::Receiver<Result<AudioTrack, String>>>, | |
| 127 | status_notice: Option<(String, Instant)>, | |
| 128 | viewers: [ViewerState; 2], | |
| 129 | flow: FlowView, | |
| 130 | spline: SplineView, | |
| 131 | keyframes: KeyframesView, | |
| 132 | inspector: InspectorState, | |
| 133 | add_tool: AddToolLauncher, | |
| 134 | ram_cache: RamFrameCache, | |
| 135 | prefetcher: Option<RamPrefetcher>, | |
| 136 | last_prefetch_sig: Option<(NodeId, i64, u64, u32, bool, bool)>, | |
| 137 | renaming_node: Option<NodeId>, | |
| 138 | rename_buf: String, | |
| 139 | rename_needs_focus: bool, | |
| 140 | size: [f32; 2], | |
| 141 | scale: f32, | |
| 142 | clock: Instant, | |
| 143 | } | |
| 144 | ||
| 145 | impl Studio { | |
| 146 | fn new( | |
| 147 | renderer: draw::Renderer, | |
| 148 | comp: Comp, | |
| 149 | init_frame: Option<f64>, | |
| 150 | select_node: Option<&str>, | |
| 151 | dual_viewer: bool, | |
| 152 | show_keyframes: bool, | |
| 153 | enable_audio: bool, | |
| 154 | ) -> Self { | |
| 155 | let engine = Engine::new(renderer.device().clone(), renderer.queue().clone()); | |
| 156 | // Knife's soft toned Iris quiet-layered theme! | |
| 157 | let ui_theme = ui::Theme::new(&theme::KNIFE, true); | |
| 158 | let mut ui = ui::Ui::new(ui_theme.clone()); | |
| 159 | ui.window_focused = true; | |
| 160 | ||
| 161 | let g_v1 = GroupId(1); | |
| 162 | let g_flow = GroupId(2); | |
| 163 | let g_spline = GroupId(3); | |
| 164 | let g_v2 = GroupId(4); | |
| 165 | let g_inspector = GroupId(5); | |
| 166 | ||
| 167 | let top_node = if dual_viewer { | |
| 168 | DockNode::Split { | |
| 169 | id: SplitId(3), | |
| 170 | axis: DockAxis::X, | |
| 171 | parts: vec![ | |
| 172 | DockPart { | |
| 173 | share: 0.5, | |
| 174 | node: DockNode::Group { id: g_v1, tabs: vec!["Viewer 1".to_owned()], preview: None, rest: Default::default() }, | |
| 175 | rest: Default::default(), | |
| 176 | }, | |
| 177 | DockPart { | |
| 178 | share: 0.5, | |
| 179 | node: DockNode::Group { id: g_v2, tabs: vec!["Viewer 2".to_owned()], preview: None, rest: Default::default() }, | |
| 180 | rest: Default::default(), | |
| 181 | }, | |
| 182 | ], | |
| 183 | rest: Default::default(), | |
| 184 | } | |
| 185 | } else { | |
| 186 | DockNode::Group { id: g_v1, tabs: vec!["Viewer 1".to_owned()], preview: None, rest: Default::default() } | |
| 187 | }; | |
| 188 | ||
| 189 | let left_workspace = DockNode::Split { | |
| 190 | id: SplitId(1), | |
| 191 | axis: DockAxis::Y, | |
| 192 | parts: vec![ | |
| 193 | DockPart { share: 0.54, node: top_node, rest: Default::default() }, | |
| 194 | DockPart { | |
| 195 | share: 0.46, | |
| 196 | node: DockNode::Split { | |
| 197 | id: SplitId(2), | |
| 198 | axis: DockAxis::X, | |
| 199 | parts: vec![ | |
| 200 | DockPart { | |
| 201 | share: 0.52, | |
| 202 | node: DockNode::Group { id: g_flow, tabs: vec!["Flow".to_owned()], preview: None, rest: Default::default() }, | |
| 203 | rest: Default::default(), | |
| 204 | }, | |
| 205 | DockPart { | |
| 206 | share: 0.48, | |
| 207 | node: DockNode::Group { | |
| 208 | id: g_spline, | |
| 209 | tabs: vec!["Spline".to_owned(), "Keyframes".to_owned()], | |
| 210 | preview: None, | |
| 211 | rest: Default::default(), | |
| 212 | }, | |
| 213 | rest: Default::default(), | |
| 214 | }, | |
| 215 | ], | |
| 216 | rest: Default::default(), | |
| 217 | }, | |
| 218 | rest: Default::default(), | |
| 219 | }, | |
| 220 | ], | |
| 221 | rest: Default::default(), | |
| 222 | }; | |
| 223 | ||
| 224 | let layout = dock::Layout { | |
| 225 | root: DockNode::Split { | |
| 226 | id: SplitId(4), | |
| 227 | axis: DockAxis::X, | |
| 228 | parts: vec![ | |
| 229 | DockPart { share: 0.76, node: left_workspace, rest: Default::default() }, | |
| 230 | DockPart { | |
| 231 | share: 0.24, | |
| 232 | node: DockNode::Group { id: g_inspector, tabs: vec!["Inspector".to_owned()], preview: None, rest: Default::default() }, | |
| 233 | rest: Default::default(), | |
| 234 | }, | |
| 235 | ], | |
| 236 | rest: Default::default(), | |
| 237 | }, | |
| 238 | issued: 6, | |
| 239 | rest: Default::default(), | |
| 240 | }; | |
| 241 | let mut seen = dock::Seen::default(); | |
| 242 | seen.active.insert(g_v1, "Viewer 1".to_owned()); | |
| 243 | if dual_viewer { | |
| 244 | seen.active.insert(g_v2, "Viewer 2".to_owned()); | |
| 245 | } | |
| 246 | seen.active.insert(g_flow, "Flow".to_owned()); | |
| 247 | seen.active.insert( | |
| 248 | g_spline, | |
| 249 | if show_keyframes { "Keyframes" } else { "Spline" }.to_owned(), | |
| 250 | ); | |
| 251 | seen.active.insert(g_inspector, "Inspector".to_owned()); | |
| 252 | seen.focused = Some(g_flow); | |
| 253 | let dock = Dock::new(); | |
| 254 | ||
| 255 | let mut playback = PlaybackClock::new(); | |
| 256 | playback.init_from_comp(&comp); | |
| 257 | if let Some(f) = init_frame { | |
| 258 | playback.set_frame(&comp, f, Instant::now()); | |
| 259 | } | |
| 260 | ||
| 261 | let mut v1 = ViewerState::new(0, &ui_theme); | |
| 262 | v1.viewed_node = comp.default_output_node(); | |
| 263 | let mut v2 = ViewerState::new(1, &ui_theme); | |
| 264 | v2.viewed_node = select_node.and_then(|n| comp.find_node(n)).or(v1.viewed_node); | |
| 265 | ||
| 266 | let mut flow = FlowView::new(&ui_theme); | |
| 267 | flow.viewed_nodes = [v1.viewed_node, if dual_viewer { v2.viewed_node } else { None }]; | |
| 268 | ||
| 269 | let default_sel = select_node | |
| 270 | .and_then(|n| comp.find_node(n)) | |
| 271 | .or_else(|| comp.nodes.iter().find(|n| !comp.node_splines(n.id).is_empty()).map(|n| n.id)) | |
| 272 | .or(v1.viewed_node); | |
| 273 | if let Some(id) = default_sel { | |
| 274 | flow.selected.insert(id); | |
| 275 | flow.primary = Some(id); | |
| 276 | } | |
| 277 | ||
| 278 | let mut spline = SplineView::new(&ui_theme); | |
| 279 | spline.set_target_node(&comp, default_sel); | |
| 280 | ||
| 281 | let mut keyframes = KeyframesView::new(&ui_theme); | |
| 282 | keyframes.sync_tracks(&comp, default_sel); | |
| 283 | ||
| 284 | v1.selected_node = default_sel; | |
| 285 | v2.selected_node = default_sel; | |
| 286 | ||
| 287 | let inspector = InspectorState::new(&ui_theme); | |
| 288 | let add_tool = AddToolLauncher::new(); | |
| 289 | let ram_cache = RamFrameCache::new(DEFAULT_RAM_CACHE_MB); | |
| 290 | let prefetcher = if enable_audio { | |
| 291 | Some(RamPrefetcher::new(ram_cache.clone(), engine.vram_budget_mb())) | |
| 292 | } else { | |
| 293 | None | |
| 294 | }; | |
| 295 | ||
| 296 | let (audio_player, audio_rx) = if enable_audio { | |
| 297 | let player = match AudioPlayer::new() { | |
| 298 | Ok(p) => Some(p), | |
| 299 | Err(e) => { | |
| 300 | eprintln!("fission audio disabled: {e}"); | |
| 301 | None | |
| 302 | } | |
| 303 | }; | |
| 304 | let rx = spawn_audio_loader(&comp); | |
| 305 | (player, rx) | |
| 306 | } else { | |
| 307 | (None, None) | |
| 308 | }; | |
| 309 | ||
| 310 | let mut history = CompHistory::new(); | |
| 311 | history.mark_saved(&comp); | |
| 312 | ||
| 313 | Self { | |
| 314 | ui, | |
| 315 | dock, | |
| 316 | layout, | |
| 317 | seen, | |
| 318 | renderer, | |
| 319 | engine, | |
| 320 | comp, | |
| 321 | history, | |
| 322 | playback, | |
| 323 | enable_audio, | |
| 324 | audio_player, | |
| 325 | audio_rx, | |
| 326 | status_notice: None, | |
| 327 | viewers: [v1, v2], | |
| 328 | flow, | |
| 329 | spline, | |
| 330 | keyframes, | |
| 331 | inspector, | |
| 332 | add_tool, | |
| 333 | ram_cache, | |
| 334 | prefetcher, | |
| 335 | last_prefetch_sig: None, | |
| 336 | renaming_node: None, | |
| 337 | rename_buf: String::new(), | |
| 338 | rename_needs_focus: false, | |
| 339 | size: [1440.0, 900.0], | |
| 340 | scale: 2.0, | |
| 341 | clock: Instant::now(), | |
| 342 | } | |
| 343 | } | |
| 344 | ||
| 345 | fn audio_time_secs(&self) -> f64 { | |
| 346 | (self.playback.current_frame - self.comp.audio_offset) / self.comp.fps.max(1.0) | |
| 347 | } | |
| 348 | ||
| 349 | fn poll_audio_load(&mut self) { | |
| 350 | let Some(rx) = &self.audio_rx else { return }; | |
| 351 | match rx.try_recv() { | |
| 352 | Ok(Ok(track)) => { | |
| 353 | eprintln!( | |
| 354 | "Loaded audio {} ({:.2}s @ {}Hz)", | |
| 355 | track.path.display(), | |
| 356 | track.duration_secs, | |
| 357 | track.sample_rate | |
| 358 | ); | |
| 359 | let peaks = track.waveform_peaks( | |
| 360 | self.comp.fps, | |
| 361 | self.comp.audio_offset, | |
| 362 | self.comp.render_range[0], | |
| 363 | self.comp.render_range[1], | |
| 364 | ); | |
| 365 | self.viewers[0].audio_peaks = peaks.clone(); | |
| 366 | self.keyframes.audio_peaks = peaks; | |
| 367 | self.keyframes.audio_range = self.comp.render_range; | |
| 368 | let t = self.audio_time_secs(); | |
| 369 | let playing = self.playback.playing; | |
| 370 | if let Some(player) = &mut self.audio_player { | |
| 371 | player.set_track(Some(Arc::new(track))); | |
| 372 | if playing { | |
| 373 | player.play_from(t); | |
| 374 | } | |
| 375 | } | |
| 376 | self.audio_rx = None; | |
| 377 | } | |
| 378 | Ok(Err(e)) => { | |
| 379 | eprintln!("fission audio load error: {e}"); | |
| 380 | self.audio_rx = None; | |
| 381 | } | |
| 382 | Err(mpsc::TryRecvError::Disconnected) => { | |
| 383 | self.audio_rx = None; | |
| 384 | } | |
| 385 | Err(mpsc::TryRecvError::Empty) => {} | |
| 386 | } | |
| 387 | } | |
| 388 | ||
| 389 | fn sync_audio(&mut self, state_changed: bool, seeked: bool) { | |
| 390 | let t = self.audio_time_secs(); | |
| 391 | let playing = self.playback.playing; | |
| 392 | if let Some(player) = &mut self.audio_player { | |
| 393 | if playing { | |
| 394 | if state_changed || !player.is_playing() { | |
| 395 | player.play_from(t); | |
| 396 | } else if seeked { | |
| 397 | player.seek(t); | |
| 398 | } | |
| 399 | } else if state_changed || player.is_playing() { | |
| 400 | player.pause(); | |
| 401 | } | |
| 402 | } | |
| 403 | } | |
| 404 | ||
| 405 | fn set_notice(&mut self, msg: impl Into<String>) { | |
| 406 | self.status_notice = Some((msg.into(), Instant::now())); | |
| 407 | } | |
| 408 | ||
| 409 | fn invalidate_node(&mut self, node: Option<NodeId>) { | |
| 410 | if let Some(id) = node { | |
| 411 | self.engine.bust_subtree(id); | |
| 412 | } else { | |
| 413 | self.engine.bust_all_caches(); | |
| 414 | } | |
| 415 | self.ram_cache.invalidate_other_revisions(self.comp.revision); | |
| 416 | if let Some(pf) = &self.prefetcher { | |
| 417 | pf.cancel(); | |
| 418 | } | |
| 419 | self.last_prefetch_sig = None; | |
| 420 | for v in &mut self.viewers { | |
| 421 | v.cached_frames.clear(); | |
| 422 | v.dirty = true; | |
| 423 | } | |
| 424 | } | |
| 425 | ||
| 426 | fn sync_ram_ranges_and_prefetch(&mut self) { | |
| 427 | let sel = self.flow.primary; | |
| 428 | for v in &mut self.viewers { | |
| 429 | v.selected_node = sel; | |
| 430 | } | |
| 431 | if let Some(target) = self.viewers[0].viewed_node { | |
| 432 | let ranges = self.ram_cache.cached_frame_ranges( | |
| 433 | target, | |
| 434 | self.comp.revision, | |
| 435 | self.engine.proxy_scale, | |
| 436 | self.engine.hiq, | |
| 437 | self.engine.use_proxy_clips, | |
| 438 | ); | |
| 439 | self.viewers[0].ram_cached_ranges = ranges.clone(); | |
| 440 | self.keyframes.cached_ranges = ranges; | |
| 441 | ||
| 442 | if let Some(pf) = &mut self.prefetcher { | |
| 443 | if self.engine.cache.mode == CacheMode::Enabled { | |
| 444 | let cur_f = self.playback.current_frame.round() as i64; | |
| 445 | let sig = ( | |
| 446 | target, | |
| 447 | cur_f, | |
| 448 | self.comp.revision, | |
| 449 | self.engine.proxy_scale, | |
| 450 | self.engine.hiq, | |
| 451 | self.engine.use_proxy_clips, | |
| 452 | ); | |
| 453 | if self.last_prefetch_sig != Some(sig) { | |
| 454 | self.last_prefetch_sig = Some(sig); | |
| 455 | self.ram_cache.invalidate_other_revisions(self.comp.revision); | |
| 456 | pf.request_lookahead(PrefetchJob { | |
| 457 | comp: self.comp.clone(), | |
| 458 | target, | |
| 459 | center_frame: cur_f, | |
| 460 | range: self.comp.render_range, | |
| 461 | lookahead: 24, | |
| 462 | proxy_scale: self.engine.proxy_scale, | |
| 463 | hiq: self.engine.hiq, | |
| 464 | use_proxy_clips: self.engine.use_proxy_clips, | |
| 465 | }); | |
| 466 | } | |
| 467 | } | |
| 468 | } | |
| 469 | } else { | |
| 470 | self.viewers[0].ram_cached_ranges.clear(); | |
| 471 | self.keyframes.cached_ranges.clear(); | |
| 472 | } | |
| 473 | } | |
| 474 | ||
| 475 | fn undo(&mut self) { | |
| 476 | let label = self.history.undo_label().unwrap_or("Edit").to_owned(); | |
| 477 | if self.history.undo(&mut self.comp) { | |
| 478 | self.flow.sanitize_ids(&self.comp); | |
| 479 | for slot in [0usize, 1] { | |
| 480 | if self.viewers[slot].viewed_node.is_some_and(|id| id.0 >= self.comp.nodes.len()) { | |
| 481 | self.viewers[slot].viewed_node = self.comp.default_output_node(); | |
| 482 | } | |
| 483 | } | |
| 484 | self.flow.viewed_nodes = [self.viewers[0].viewed_node, self.viewers[1].viewed_node]; | |
| 485 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 486 | self.renaming_node = None; | |
| 487 | self.invalidate_node(None); | |
| 488 | self.set_notice(format!("Undo: {label}")); | |
| 489 | } else { | |
| 490 | self.set_notice("Nothing to undo"); | |
| 491 | } | |
| 492 | } | |
| 493 | ||
| 494 | fn redo(&mut self) { | |
| 495 | let label = self.history.redo_label().unwrap_or("Edit").to_owned(); | |
| 496 | if self.history.redo(&mut self.comp) { | |
| 497 | self.flow.sanitize_ids(&self.comp); | |
| 498 | for slot in [0usize, 1] { | |
| 499 | if self.viewers[slot].viewed_node.is_some_and(|id| id.0 >= self.comp.nodes.len()) { | |
| 500 | self.viewers[slot].viewed_node = self.comp.default_output_node(); | |
| 501 | } | |
| 502 | } | |
| 503 | self.flow.viewed_nodes = [self.viewers[0].viewed_node, self.viewers[1].viewed_node]; | |
| 504 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 505 | self.renaming_node = None; | |
| 506 | self.invalidate_node(None); | |
| 507 | self.set_notice(format!("Redo: {label}")); | |
| 508 | } else { | |
| 509 | self.set_notice("Nothing to redo"); | |
| 510 | } | |
| 511 | } | |
| 512 | ||
| 513 | fn save_comp(&mut self) -> Result<String, String> { | |
| 514 | let Some(path) = self.comp.path.clone() else { | |
| 515 | return self.save_comp_as(); | |
| 516 | }; | |
| 517 | self.save_to_path(&path) | |
| 518 | } | |
| 519 | ||
| 520 | fn save_to_path(&mut self, path: &Path) -> Result<String, String> { | |
| 521 | self.comp.current_time = self.playback.current_frame; | |
| 522 | match self.comp.save_to_file(path) { | |
| 523 | Ok(backup) => { | |
| 524 | self.comp.path = Some(path.to_path_buf()); | |
| 525 | if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) { | |
| 526 | self.comp.name = stem.to_owned(); | |
| 527 | } | |
| 528 | self.history.mark_saved(&self.comp); | |
| 529 | let msg = match backup { | |
| 530 | Some(bak) => { | |
| 531 | eprintln!("Saved {} (backup -> {})", path.display(), bak.display()); | |
| 532 | "Saved (backup in temp)".to_owned() | |
| 533 | } | |
| 534 | None => { | |
| 535 | eprintln!("Saved {}", path.display()); | |
| 536 | format!("Saved {}", self.comp.name) | |
| 537 | } | |
| 538 | }; | |
| 539 | self.set_notice(msg.clone()); | |
| 540 | Ok(msg) | |
| 541 | } | |
| 542 | Err(e) => { | |
| 543 | let err = format!("Save failed: {e}"); | |
| 544 | eprintln!("{err}"); | |
| 545 | self.set_notice(err.clone()); | |
| 546 | Err(err) | |
| 547 | } | |
| 548 | } | |
| 549 | } | |
| 550 | ||
| 551 | fn save_comp_as(&mut self) -> Result<String, String> { | |
| 552 | let default_name = format!("{}.comp", self.comp.name); | |
| 553 | let Some(mut path) = native_save_comp_dialog(&default_name) else { | |
| 554 | return Err("Save As cancelled".to_owned()); | |
| 555 | }; | |
| 556 | if path.extension().is_none() { | |
| 557 | path.set_extension("comp"); | |
| 558 | } | |
| 559 | self.save_to_path(&path) | |
| 560 | } | |
| 561 | ||
| 562 | fn open_comp_dialog(&mut self) { | |
| 563 | let Some(path) = native_open_comp_dialog() else { | |
| 564 | return; | |
| 565 | }; | |
| 566 | match Comp::from_fusion_file(&path) { | |
| 567 | Ok(new_comp) => { | |
| 568 | self.reload_comp(new_comp); | |
| 569 | self.set_notice(format!("Opened {}", self.comp.name)); | |
| 570 | } | |
| 571 | Err(e) => { | |
| 572 | let msg = format!("Open failed: {e}"); | |
| 573 | eprintln!("{msg}"); | |
| 574 | self.set_notice(msg); | |
| 575 | } | |
| 576 | } | |
| 577 | } | |
| 578 | ||
| 579 | fn reload_comp(&mut self, new_comp: Comp) { | |
| 580 | if self.playback.playing { | |
| 581 | self.playback.toggle_play(Instant::now()); | |
| 582 | self.sync_audio(true, false); | |
| 583 | } | |
| 584 | self.comp = new_comp; | |
| 585 | self.history.clear(); | |
| 586 | self.history.mark_saved(&self.comp); | |
| 587 | self.engine.bust_all_caches(); | |
| 588 | self.playback.init_from_comp(&self.comp); | |
| 589 | ||
| 590 | let out_node = self.comp.default_output_node(); | |
| 591 | for (slot, v) in self.viewers.iter_mut().enumerate() { | |
| 592 | v.viewed_node = out_node; | |
| 593 | v.cached_frames.clear(); | |
| 594 | v.dirty = true; | |
| 595 | if slot == 0 { | |
| 596 | v.fit(); | |
| 597 | } | |
| 598 | } | |
| 599 | self.flow.reset_for_comp(&self.comp); | |
| 600 | self.flow.viewed_nodes = [self.viewers[0].viewed_node, None]; | |
| 601 | let default_sel = self | |
| 602 | .comp | |
| 603 | .nodes | |
| 604 | .iter() | |
| 605 | .find(|n| !self.comp.node_splines(n.id).is_empty()) | |
| 606 | .map(|n| n.id) | |
| 607 | .or(out_node); | |
| 608 | if let Some(id) = default_sel { | |
| 609 | self.flow.selected.insert(id); | |
| 610 | self.flow.primary = Some(id); | |
| 611 | } | |
| 612 | self.spline.set_target_node(&self.comp, default_sel); | |
| 613 | self.keyframes.audio_peaks.clear(); | |
| 614 | self.keyframes.cached_ranges.clear(); | |
| 615 | self.keyframes.fit_comp(&self.comp); | |
| 616 | self.keyframes.sync_tracks(&self.comp, default_sel); | |
| 617 | self.viewers[0].audio_peaks.clear(); | |
| 618 | self.ram_cache.clear(); | |
| 619 | if let Some(pf) = &self.prefetcher { | |
| 620 | pf.cancel(); | |
| 621 | } | |
| 622 | self.last_prefetch_sig = None; | |
| 623 | self.inspector.reset(); | |
| 624 | self.renaming_node = None; | |
| 625 | ||
| 626 | if self.enable_audio { | |
| 627 | if let Some(player) = &mut self.audio_player { | |
| 628 | player.set_track(None); | |
| 629 | } | |
| 630 | self.audio_rx = spawn_audio_loader(&self.comp); | |
| 631 | } | |
| 632 | } | |
| 633 | ||
| 634 | fn add_tool_from_entry(&mut self, entry: ToolEntry) { | |
| 635 | let prev_sel = self.flow.primary.filter(|id| id.0 < self.comp.nodes.len()); | |
| 636 | let pos = self.flow.spawn_position(&self.comp, entry.is_generator); | |
| 637 | self.history.push_snapshot(&self.comp, format!("Add {}", entry.display)); | |
| 638 | let new_id = self.comp.add_node(entry.kind, None, pos); | |
| 639 | if !entry.is_generator { | |
| 640 | if let Some(src_id) = prev_sel { | |
| 641 | let port = default_main_input_port(entry.kind); | |
| 642 | let _ = self.comp.connect_output(src_id, "Output", new_id, port); | |
| 643 | } | |
| 644 | } | |
| 645 | self.flow.selected.clear(); | |
| 646 | self.flow.selected.insert(new_id); | |
| 647 | self.flow.primary = Some(new_id); | |
| 648 | self.spline.set_target_node(&self.comp, Some(new_id)); | |
| 649 | self.invalidate_node(Some(new_id)); | |
| 650 | self.set_notice(format!("Added {}", self.comp.node(new_id).name)); | |
| 651 | } | |
| 652 | ||
| 653 | fn begin_inline_rename(&mut self, id: NodeId) { | |
| 654 | if id.0 >= self.comp.nodes.len() { | |
| 655 | return; | |
| 656 | } | |
| 657 | self.renaming_node = Some(id); | |
| 658 | self.rename_buf = self.comp.node(id).name.clone(); | |
| 659 | self.rename_needs_focus = true; | |
| 660 | } | |
| 661 | ||
| 662 | fn export_saver_pngs(&mut self) -> Result<String, String> { | |
| 663 | if self.playback.playing { | |
| 664 | self.playback.toggle_play(Instant::now()); | |
| 665 | self.sync_audio(true, false); | |
| 666 | } | |
| 667 | let opts = ExportOptions { | |
| 668 | hiq: self.engine.hiq, | |
| 669 | proxy_scale: self.engine.proxy_scale, | |
| 670 | ..Default::default() | |
| 671 | }; | |
| 672 | let t0 = Instant::now(); | |
| 673 | match export_saver_png_sequence(&mut self.engine, &self.comp, &opts, |p| { | |
| 674 | if p.index == 0 || (p.index + 1) % 10 == 0 || p.index + 1 == p.total { | |
| 675 | let fps = (p.index + 1) as f64 / t0.elapsed().as_secs_f64().max(1e-6); | |
| 676 | eprintln!( | |
| 677 | " [export {}/{}] {} f{:04} ({:.1}ms, {:.1} fps) -> {}", | |
| 678 | p.index + 1, | |
| 679 | p.total, | |
| 680 | p.saver_name, | |
| 681 | p.frame, | |
| 682 | p.eval_ms, | |
| 683 | fps, | |
| 684 | p.out_path.display() | |
| 685 | ); | |
| 686 | } | |
| 687 | }) { | |
| 688 | Ok(summary) => { | |
| 689 | let secs = (summary.total_ms / 1000.0).max(1e-6); | |
| 690 | let msg = format!( | |
| 691 | "Exported {} PNGs ({:.1} fps)", | |
| 692 | summary.frames_written, | |
| 693 | summary.frames_written as f64 / secs | |
| 694 | ); | |
| 695 | eprintln!("{msg} via {}", summary.saver_name); | |
| 696 | self.status_notice = Some((msg.clone(), Instant::now())); | |
| 697 | Ok(msg) | |
| 698 | } | |
| 699 | Err(e) => { | |
| 700 | let err = format!("Export failed: {e}"); | |
| 701 | eprintln!("{err}"); | |
| 702 | self.status_notice = Some((err.clone(), Instant::now())); | |
| 703 | Err(err) | |
| 704 | } | |
| 705 | } | |
| 706 | } | |
| 707 | ||
| 708 | fn window_title(&self) -> String { | |
| 709 | let dirty = if self.history.is_dirty(&self.comp) { "*" } else { "" }; | |
| 710 | if let Some((msg, at)) = &self.status_notice { | |
| 711 | if at.elapsed() < Duration::from_secs(5) { | |
| 712 | return format!("Fission — {}{dirty} — {msg}", self.comp.name); | |
| 713 | } | |
| 714 | } | |
| 715 | format!("Fission — {}{dirty}", self.comp.name) | |
| 716 | } | |
| 717 | ||
| 718 | fn ensure_viewer2_open(&mut self) { | |
| 719 | if self.layout.group_of(&"Viewer 2".to_owned(), String::clone).is_some() { | |
| 720 | return; | |
| 721 | } | |
| 722 | let target_group = self | |
| 723 | .layout | |
| 724 | .group_of(&"Viewer 1".to_owned(), String::clone) | |
| 725 | .unwrap_or_else(|| self.dock.at(&mut self.layout, &mut self.seen, &String::clone).focused()); | |
| 726 | self.dock | |
| 727 | .at(&mut self.layout, &mut self.seen, &String::clone) | |
| 728 | .open("Viewer 2".to_owned(), Place::Beside(target_group, Side::Right)); | |
| 729 | } | |
| 730 | ||
| 731 | fn clear_viewers(&mut self) { | |
| 732 | for v in &mut self.viewers { | |
| 733 | v.clear(); | |
| 734 | } | |
| 735 | self.flow.viewed_nodes = [None, None]; | |
| 736 | } | |
| 737 | ||
| 738 | fn frame(&mut self, now: Instant) -> bool { | |
| 739 | self.clock = now; | |
| 740 | self.poll_audio_load(); | |
| 741 | let prev_frame = self.playback.current_frame; | |
| 742 | let (animating, frame_changed) = self.playback.tick(&self.comp, now); | |
| 743 | if frame_changed { | |
| 744 | // Re-sync audio when playback loops back to start of render range | |
| 745 | if self.playback.playing && self.playback.current_frame < prev_frame { | |
| 746 | self.sync_audio(false, true); | |
| 747 | } | |
| 748 | for v in &mut self.viewers { | |
| 749 | v.dirty = true; | |
| 750 | } | |
| 751 | } | |
| 752 | ||
| 753 | self.sync_ram_ranges_and_prefetch(); | |
| 754 | let shown = self.dock.at(&mut self.layout, &mut self.seen, &String::clone).shown(); | |
| 755 | for (slot, pane_name) in [(0, "Viewer 1"), (1, "Viewer 2")] { | |
| 756 | if shown.contains(&pane_name.to_owned()) { | |
| 757 | self.viewers[slot].ensure_rendered_with_ram( | |
| 758 | &mut self.engine, | |
| 759 | Some(&self.ram_cache), | |
| 760 | &self.comp, | |
| 761 | self.playback.current_frame, | |
| 762 | self.scale, | |
| 763 | ); | |
| 764 | self.viewers[slot].prepare_hud(&self.comp, &self.engine, &self.playback); | |
| 765 | } | |
| 766 | } | |
| 767 | self.flow.prepare_labels(&self.comp); | |
| 768 | self.spline.prepare_labels(&self.comp); | |
| 769 | self.keyframes.sync_tracks(&self.comp, self.flow.primary); | |
| 770 | self.keyframes.prepare_labels(); | |
| 771 | ||
| 772 | self.ui.begin(self.size, self.scale, now); | |
| 773 | ||
| 774 | let choices: Vec<ui::popup::Item<'static>> = PANES | |
| 775 | .iter() | |
| 776 | .map(|&name| ui::popup::Item { text: name, ..Default::default() }) | |
| 777 | .collect(); | |
| 778 | ||
| 779 | let comp_unsaved = self.history.is_dirty(&self.comp); | |
| 780 | let cur_frame = self.playback.current_frame; | |
| 781 | let eval_stats = self.viewers[0].last_stats.clone(); | |
| 782 | let can_undo = self.history.can_undo(); | |
| 783 | let can_redo = self.history.can_redo(); | |
| 784 | let primary_sel = self.flow.primary; | |
| 785 | let Studio { | |
| 786 | ui, | |
| 787 | dock, | |
| 788 | layout, | |
| 789 | seen, | |
| 790 | engine, | |
| 791 | comp, | |
| 792 | history, | |
| 793 | playback, | |
| 794 | viewers, | |
| 795 | flow, | |
| 796 | spline, | |
| 797 | keyframes, | |
| 798 | inspector, | |
| 799 | add_tool, | |
| 800 | renaming_node, | |
| 801 | rename_buf, | |
| 802 | rename_needs_focus, | |
| 803 | size, | |
| 804 | .. | |
| 805 | } = self; | |
| 806 | ||
| 807 | let v1_bg = viewers[0].background(); | |
| 808 | let v2_bg = viewers[1].background(); | |
| 809 | let flow_bg = flow.background(); | |
| 810 | let spline_bg = spline.background(); | |
| 811 | let keyframes_bg = keyframes.background(); | |
| 812 | let inspector_bg = inspector.background(); | |
| 813 | let mut viewer_actions: Vec<(usize, ViewerAction)> = Vec::new(); | |
| 814 | let mut inspector_actions: Vec<InspectorAction> = Vec::new(); | |
| 815 | let mut req_open_add_tool = false; | |
| 816 | let mut req_undo = false; | |
| 817 | let mut req_redo = false; | |
| 818 | let mut req_open = false; | |
| 819 | let mut req_save = false; | |
| 820 | let mut req_save_as = false; | |
| 821 | let mut commit_rename: Option<(NodeId, String)> = None; | |
| 822 | let mut cancel_rename = false; | |
| 823 | ||
| 824 | let mut d = dock.at(layout, seen, &String::clone); | |
| 825 | let events = d.build( | |
| 826 | ui, | |
| 827 | "studio-dock", | |
| 828 | *size, | |
| 829 | |_| Plus::Menu("Open Pane", &choices), | |
| 830 | &(), | |
| 831 | |name: &String, _| { | |
| 832 | let bg = match name.as_str() { | |
| 833 | "Viewer 1" => v1_bg, | |
| 834 | "Viewer 2" => v2_bg, | |
| 835 | "Flow" => flow_bg, | |
| 836 | "Spline" => spline_bg, | |
| 837 | "Keyframes" => keyframes_bg, | |
| 838 | "Inspector" => inspector_bg, | |
| 839 | _ => flow_bg, | |
| 840 | }; | |
| 841 | Tab { | |
| 842 | title: name, | |
| 843 | icon: None, | |
| 844 | unsaved: name == "Flow" && comp_unsaved, | |
| 845 | closable: true, | |
| 846 | action: None, | |
| 847 | background: bg, | |
| 848 | } | |
| 849 | }, | |
| 850 | |ui, name| { | |
| 851 | let name = name?.as_str(); | |
| 852 | match name { | |
| 853 | "Viewer 1" => { | |
| 854 | let acts = viewers[0].build_ui(ui, viewer_pane_id(0), viewer_canvas_id(0), engine, playback); | |
| 855 | for a in acts { viewer_actions.push((0, a)); } | |
| 856 | Some(viewer_canvas_id(0)) | |
| 857 | } | |
| 858 | "Viewer 2" => { | |
| 859 | let acts = viewers[1].build_ui(ui, viewer_pane_id(1), viewer_canvas_id(1), engine, playback); | |
| 860 | for a in acts { viewer_actions.push((1, a)); } | |
| 861 | Some(viewer_canvas_id(1)) | |
| 862 | } | |
| 863 | "Flow" => { | |
| 864 | ui.open_as( | |
| 865 | flow_pane_id(), | |
| 866 | ui::Spec { | |
| 867 | size: [ui::fill(), ui::fill()], | |
| 868 | axis: ui::Axis::Y, | |
| 869 | fill: Some(flow_bg), | |
| 870 | ..ui::Spec::default() | |
| 871 | }, | |
| 872 | ); | |
| 873 | // Compact Flow toolbar with `+ Add`, Undo/Redo, Open/Save/Save As, and inline F2 rename | |
| 874 | let bar_bg = ui.theme.strip; | |
| 875 | ui.open( | |
| 876 | "flow-toolbar", | |
| 877 | ui::Spec { | |
| 878 | size: [ui::fill(), ui::px(28.0)], | |
| 879 | axis: ui::Axis::X, | |
| 880 | pad: [6.0, 3.0], | |
| 881 | gap: 4.0, | |
| 882 | fill: Some(bar_bg), | |
| 883 | ..ui::Spec::default() | |
| 884 | }, | |
| 885 | ); | |
| 886 | if studio_btn(ui, "flow-add-btn", "+ Add", false).clicked { | |
| 887 | req_open_add_tool = true; | |
| 888 | } | |
| 889 | if studio_btn(ui, "flow-undo-btn", "Undo", can_undo).clicked && can_undo { | |
| 890 | req_undo = true; | |
| 891 | } | |
| 892 | if studio_btn(ui, "flow-redo-btn", "Redo", can_redo).clicked && can_redo { | |
| 893 | req_redo = true; | |
| 894 | } | |
| 895 | if studio_btn(ui, "flow-open-btn", "Open", false).clicked { | |
| 896 | req_open = true; | |
| 897 | } | |
| 898 | if studio_btn(ui, "flow-save-btn", if comp_unsaved { "Save*" } else { "Save" }, comp_unsaved).clicked { | |
| 899 | req_save = true; | |
| 900 | } | |
| 901 | if studio_btn(ui, "flow-saveas-btn", "Save As", false).clicked { | |
| 902 | req_save_as = true; | |
| 903 | } | |
| 904 | if let Some(rn_id) = *renaming_node { | |
| 905 | let rn_field_id = flow_pane_id().child(("rename-field", rn_id.0)); | |
| 906 | let rn_sig = ui::text_field( | |
| 907 | ui, | |
| 908 | rn_field_id, | |
| 909 | rename_buf, | |
| 910 | "Rename node…", | |
| 911 | ui::Spec { | |
| 912 | size: [ui::px(160.0), ui::px(22.0)], | |
| 913 | font_size: Some(11.5), | |
| 914 | fill: Some(ui.theme.base), | |
| 915 | border: Some(ui.theme.accent), | |
| 916 | radius: 4.0, | |
| 917 | pad: [6.0, 1.0], | |
| 918 | ..ui::Spec::default() | |
| 919 | }, | |
| 920 | ); | |
| 921 | if *rename_needs_focus { | |
| 922 | ui.set_focus(Some(rn_field_id)); | |
| 923 | *rename_needs_focus = false; | |
| 924 | } | |
| 925 | for ev in &rn_sig.events { | |
| 926 | if let ui::Event::Key { key, .. } = ev { | |
| 927 | match key { | |
| 928 | Key::Named(NamedKey::Enter) => { | |
| 929 | commit_rename = Some((rn_id, rename_buf.trim().to_owned())); | |
| 930 | } | |
| 931 | Key::Named(NamedKey::Escape) => { | |
| 932 | cancel_rename = true; | |
| 933 | } | |
| 934 | _ => {} | |
| 935 | } | |
| 936 | } | |
| 937 | } | |
| 938 | } | |
| 939 | ui.close(); // flow-toolbar | |
| 940 | ||
| 941 | let id = ui.open_as( | |
| 942 | flow_canvas_id(), | |
| 943 | ui::Spec { | |
| 944 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 945 | size: [ui::fill(), ui::fill()], | |
| 946 | ..ui::Spec::default() | |
| 947 | }, | |
| 948 | ); | |
| 949 | ui.close(); | |
| 950 | ui.close(); // flow_pane_id | |
| 951 | Some(id) | |
| 952 | } | |
| 953 | "Spline" => { | |
| 954 | let id = ui.open_as( | |
| 955 | spline_canvas_id(), | |
| 956 | ui::Spec { | |
| 957 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 958 | size: [ui::fill(), ui::fill()], | |
| 959 | ..ui::Spec::default() | |
| 960 | }, | |
| 961 | ); | |
| 962 | ui.close(); | |
| 963 | Some(id) | |
| 964 | } | |
| 965 | "Keyframes" => { | |
| 966 | let id = ui.open_as( | |
| 967 | keyframes_canvas_id(), | |
| 968 | ui::Spec { | |
| 969 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 970 | size: [ui::fill(), ui::fill()], | |
| 971 | ..ui::Spec::default() | |
| 972 | }, | |
| 973 | ); | |
| 974 | ui.close(); | |
| 975 | Some(id) | |
| 976 | } | |
| 977 | "Inspector" => { | |
| 978 | let acts = inspector.build_ui( | |
| 979 | ui, | |
| 980 | inspector_pane_id(), | |
| 981 | comp, | |
| 982 | history, | |
| 983 | primary_sel, | |
| 984 | cur_frame, | |
| 985 | &eval_stats, | |
| 986 | ); | |
| 987 | inspector_actions.extend(acts); | |
| 988 | Some(inspector_pane_id()) | |
| 989 | } | |
| 990 | _ => None, | |
| 991 | } | |
| 992 | }, | |
| 993 | ); | |
| 994 | ||
| 995 | let picked_tool = add_tool.build_ui(ui, *size); | |
| 996 | self.ui.end(); | |
| 997 | ||
| 998 | if req_open_add_tool { | |
| 999 | self.add_tool.open(); | |
| 1000 | } | |
| 1001 | if req_undo { | |
| 1002 | self.undo(); | |
| 1003 | } | |
| 1004 | if req_redo { | |
| 1005 | self.redo(); | |
| 1006 | } | |
| 1007 | if req_open { | |
| 1008 | self.open_comp_dialog(); | |
| 1009 | } | |
| 1010 | if req_save { | |
| 1011 | let _ = self.save_comp(); | |
| 1012 | } | |
| 1013 | if req_save_as { | |
| 1014 | let _ = self.save_comp_as(); | |
| 1015 | } | |
| 1016 | if let Some((rn_id, new_name)) = commit_rename { | |
| 1017 | self.renaming_node = None; | |
| 1018 | self.ui.set_focus(Some(flow_canvas_id())); | |
| 1019 | if !new_name.is_empty() && rn_id.0 < self.comp.nodes.len() && new_name != self.comp.node(rn_id).name { | |
| 1020 | self.history.push_snapshot(&self.comp, "Rename Node"); | |
| 1021 | match self.comp.rename_node(rn_id, &new_name) { | |
| 1022 | Ok(applied) => { | |
| 1023 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 1024 | self.invalidate_node(Some(rn_id)); | |
| 1025 | self.set_notice(format!("Renamed to {applied}")); | |
| 1026 | } | |
| 1027 | Err(e) => self.set_notice(e), | |
| 1028 | } | |
| 1029 | } | |
| 1030 | } else if cancel_rename { | |
| 1031 | self.renaming_node = None; | |
| 1032 | self.ui.set_focus(Some(flow_canvas_id())); | |
| 1033 | } | |
| 1034 | if let Some(entry) = picked_tool { | |
| 1035 | self.add_tool_from_entry(entry); | |
| 1036 | } | |
| 1037 | ||
| 1038 | for a in inspector_actions { | |
| 1039 | match a { | |
| 1040 | InspectorAction::ParamEdited(id) => { | |
| 1041 | self.invalidate_node(Some(id)); | |
| 1042 | } | |
| 1043 | InspectorAction::KeyframeEdited(id) => { | |
| 1044 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 1045 | self.invalidate_node(Some(id)); | |
| 1046 | } | |
| 1047 | InspectorAction::NodeRenamed(id) => { | |
| 1048 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 1049 | self.invalidate_node(Some(id)); | |
| 1050 | } | |
| 1051 | InspectorAction::StatusNotice(msg) => { | |
| 1052 | self.set_notice(msg); | |
| 1053 | } | |
| 1054 | } | |
| 1055 | } | |
| 1056 | ||
| 1057 | for ev in events { | |
| 1058 | match ev { | |
| 1059 | Event::Close(name) => { | |
| 1060 | self.dock.at(&mut self.layout, &mut self.seen, &String::clone).close(&name); | |
| 1061 | } | |
| 1062 | Event::Plus { group, choice: Some(idx), .. } => { | |
| 1063 | if let Some(&name) = PANES.get(idx) { | |
| 1064 | self.dock.at(&mut self.layout, &mut self.seen, &String::clone).open(name.to_owned(), Place::Group(group)); | |
| 1065 | } | |
| 1066 | } | |
| 1067 | _ => {} | |
| 1068 | } | |
| 1069 | } | |
| 1070 | ||
| 1071 | self.route_viewer_actions(viewer_actions, now); | |
| 1072 | ||
| 1073 | for slot in [0usize, 1] { | |
| 1074 | let cid = viewer_canvas_id(slot); | |
| 1075 | if let Some(rect) = self.ui.laid_out(cid) { | |
| 1076 | self.viewers[slot].resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1077 | let sig = self.ui.signal(cid); | |
| 1078 | let ptr = self.ui.last_pointer(); | |
| 1079 | let mods = self.ui.modifiers(); | |
| 1080 | let mut acts = Vec::new(); | |
| 1081 | for ev in sig.events { | |
| 1082 | for a in self.viewers[slot].input(&self.comp, &self.playback, ev, rect, ptr, mods) { | |
| 1083 | acts.push((slot, a)); | |
| 1084 | } | |
| 1085 | } | |
| 1086 | self.route_viewer_actions(acts, now); | |
| 1087 | } | |
| 1088 | } | |
| 1089 | ||
| 1090 | if let Some(rect) = self.ui.laid_out(flow_canvas_id()) { | |
| 1091 | self.flow.resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1092 | self.flow.ensure_fitted(&self.comp); | |
| 1093 | let sig = self.ui.signal(flow_canvas_id()); | |
| 1094 | let ptr = self.ui.last_pointer(); | |
| 1095 | let mods = self.ui.modifiers(); | |
| 1096 | let mut acts = Vec::new(); | |
| 1097 | for ev in sig.events { | |
| 1098 | acts.extend(self.flow.input(&mut self.comp, &mut self.history, ev, rect, ptr, mods)); | |
| 1099 | } | |
| 1100 | for a in acts { | |
| 1101 | match a { | |
| 1102 | FlowAction::SelectionChanged(sel) => { | |
| 1103 | self.spline.set_target_node(&self.comp, sel); | |
| 1104 | } | |
| 1105 | FlowAction::ViewNode(slot, id) => { | |
| 1106 | self.viewers[slot].viewed_node = Some(id); | |
| 1107 | self.viewers[slot].dirty = true; | |
| 1108 | self.flow.viewed_nodes[slot] = Some(id); | |
| 1109 | if slot == 1 { | |
| 1110 | self.ensure_viewer2_open(); | |
| 1111 | } | |
| 1112 | } | |
| 1113 | FlowAction::ClearViewers => { | |
| 1114 | self.clear_viewers(); | |
| 1115 | } | |
| 1116 | FlowAction::GraphMutated(node) => { | |
| 1117 | self.invalidate_node(node); | |
| 1118 | } | |
| 1119 | FlowAction::OpenAddTool => { | |
| 1120 | self.add_tool.open(); | |
| 1121 | } | |
| 1122 | FlowAction::RequestRename(id) => { | |
| 1123 | self.begin_inline_rename(id); | |
| 1124 | } | |
| 1125 | FlowAction::StatusNotice(msg) => { | |
| 1126 | self.set_notice(msg); | |
| 1127 | } | |
| 1128 | } | |
| 1129 | } | |
| 1130 | } | |
| 1131 | ||
| 1132 | if let Some(rect) = self.ui.laid_out(spline_canvas_id()) { | |
| 1133 | self.spline.resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1134 | let sig = self.ui.signal(spline_canvas_id()); | |
| 1135 | let ptr = self.ui.last_pointer(); | |
| 1136 | let mods = self.ui.modifiers(); | |
| 1137 | let mut acts = Vec::new(); | |
| 1138 | for ev in sig.events { | |
| 1139 | acts.extend(self.spline.input_with_history( | |
| 1140 | &mut self.comp, | |
| 1141 | Some(&mut self.history), | |
| 1142 | self.playback.current_frame, | |
| 1143 | ev, | |
| 1144 | rect, | |
| 1145 | ptr, | |
| 1146 | mods, | |
| 1147 | )); | |
| 1148 | } | |
| 1149 | for a in acts { | |
| 1150 | match a { | |
| 1151 | SplineAction::SplineEdited => { | |
| 1152 | if let Some(id) = self.flow.primary { | |
| 1153 | self.engine.bust_subtree(id); | |
| 1154 | } | |
| 1155 | for v in &mut self.viewers { | |
| 1156 | v.cached_frames.clear(); | |
| 1157 | v.dirty = true; | |
| 1158 | } | |
| 1159 | } | |
| 1160 | SplineAction::ScrubFrame(f) => { | |
| 1161 | if self.playback.set_frame(&self.comp, f, now) { | |
| 1162 | self.sync_audio(false, true); | |
| 1163 | for v in &mut self.viewers { | |
| 1164 | v.dirty = true; | |
| 1165 | } | |
| 1166 | } | |
| 1167 | } | |
| 1168 | } | |
| 1169 | } | |
| 1170 | } | |
| 1171 | ||
| 1172 | if let Some(rect) = self.ui.laid_out(keyframes_canvas_id()) { | |
| 1173 | self.keyframes.resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1174 | let sig = self.ui.signal(keyframes_canvas_id()); | |
| 1175 | let ptr = self.ui.last_pointer(); | |
| 1176 | let mods = self.ui.modifiers(); | |
| 1177 | let mut acts = Vec::new(); | |
| 1178 | for ev in sig.events { | |
| 1179 | acts.extend(self.keyframes.input( | |
| 1180 | &mut self.comp, | |
| 1181 | &mut self.history, | |
| 1182 | self.flow.primary, | |
| 1183 | ev, | |
| 1184 | rect, | |
| 1185 | ptr, | |
| 1186 | mods, | |
| 1187 | )); | |
| 1188 | } | |
| 1189 | for a in acts { | |
| 1190 | match a { | |
| 1191 | KeyframesAction::ScrubFrame(f) => { | |
| 1192 | if self.playback.set_frame(&self.comp, f, now) { | |
| 1193 | self.sync_audio(false, true); | |
| 1194 | for v in &mut self.viewers { | |
| 1195 | v.dirty = true; | |
| 1196 | } | |
| 1197 | } | |
| 1198 | } | |
| 1199 | KeyframesAction::SelectNode(id) => { | |
| 1200 | self.flow.selected.clear(); | |
| 1201 | self.flow.selected.insert(id); | |
| 1202 | self.flow.primary = Some(id); | |
| 1203 | self.spline.set_target_node(&self.comp, Some(id)); | |
| 1204 | } | |
| 1205 | KeyframesAction::KeyframesEdited => { | |
| 1206 | self.invalidate_node(None); | |
| 1207 | } | |
| 1208 | } | |
| 1209 | } | |
| 1210 | } | |
| 1211 | ||
| 1212 | // Immediately re-render any dirty visible viewer before painting this frame! | |
| 1213 | self.sync_ram_ranges_and_prefetch(); | |
| 1214 | let shown_now = self.dock.at(&mut self.layout, &mut self.seen, &String::clone).shown(); | |
| 1215 | for (slot, pane_name) in [(0, "Viewer 1"), (1, "Viewer 2")] { | |
| 1216 | if shown_now.contains(&pane_name.to_owned()) && self.viewers[slot].dirty { | |
| 1217 | self.viewers[slot].ensure_rendered_with_ram( | |
| 1218 | &mut self.engine, | |
| 1219 | Some(&self.ram_cache), | |
| 1220 | &self.comp, | |
| 1221 | self.playback.current_frame, | |
| 1222 | self.scale, | |
| 1223 | ); | |
| 1224 | self.viewers[slot].prepare_hud(&self.comp, &self.engine, &self.playback); | |
| 1225 | } | |
| 1226 | } | |
| 1227 | ||
| 1228 | animating || self.ui.wants_frame() || self.dock.moving() | |
| 1229 | } | |
| 1230 | ||
| 1231 | fn route_viewer_actions(&mut self, actions: Vec<(usize, ViewerAction)>, now: Instant) { | |
| 1232 | for (slot, a) in actions { | |
| 1233 | match a { | |
| 1234 | ViewerAction::TogglePlay => { | |
| 1235 | self.playback.toggle_play(now); | |
| 1236 | self.sync_audio(true, false); | |
| 1237 | } | |
| 1238 | ViewerAction::SetFrame(f) => { | |
| 1239 | if self.playback.set_frame(&self.comp, f, now) { | |
| 1240 | self.sync_audio(false, true); | |
| 1241 | for v in &mut self.viewers { | |
| 1242 | v.dirty = true; | |
| 1243 | } | |
| 1244 | } | |
| 1245 | } | |
| 1246 | ViewerAction::ToggleSyncMode => { | |
| 1247 | self.playback.sync_mode = match self.playback.sync_mode { | |
| 1248 | SyncMode::RealtimeDrop => SyncMode::EveryFrame, | |
| 1249 | SyncMode::EveryFrame => SyncMode::RealtimeDrop, | |
| 1250 | }; | |
| 1251 | } | |
| 1252 | ViewerAction::NeedRender => { | |
| 1253 | self.viewers[slot].dirty = true; | |
| 1254 | } | |
| 1255 | ViewerAction::BustFrame(f) => { | |
| 1256 | self.engine.bust_frame(f); | |
| 1257 | self.ram_cache.clear(); | |
| 1258 | self.last_prefetch_sig = None; | |
| 1259 | for v in &mut self.viewers { | |
| 1260 | v.cached_frames.remove(&f); | |
| 1261 | v.dirty = true; | |
| 1262 | } | |
| 1263 | } | |
| 1264 | ViewerAction::BustSubtree => { | |
| 1265 | if let Some(id) = self.flow.primary.or(self.viewers[slot].viewed_node) { | |
| 1266 | self.engine.bust_subtree(id); | |
| 1267 | } | |
| 1268 | self.ram_cache.clear(); | |
| 1269 | self.last_prefetch_sig = None; | |
| 1270 | for v in &mut self.viewers { | |
| 1271 | v.cached_frames.clear(); | |
| 1272 | v.dirty = true; | |
| 1273 | } | |
| 1274 | } | |
| 1275 | ViewerAction::BustGpu => { | |
| 1276 | self.engine.bust_gpu_cache(); | |
| 1277 | self.ram_cache.clear(); | |
| 1278 | self.last_prefetch_sig = None; | |
| 1279 | for v in &mut self.viewers { | |
| 1280 | v.cached_frames.clear(); | |
| 1281 | v.dirty = true; | |
| 1282 | } | |
| 1283 | } | |
| 1284 | ViewerAction::BustAll => { | |
| 1285 | self.engine.bust_all_caches(); | |
| 1286 | self.ram_cache.clear(); | |
| 1287 | self.last_prefetch_sig = None; | |
| 1288 | for v in &mut self.viewers { | |
| 1289 | v.cached_frames.clear(); | |
| 1290 | v.dirty = true; | |
| 1291 | } | |
| 1292 | } | |
| 1293 | ViewerAction::ToggleCacheBypass => { | |
| 1294 | let next = match self.engine.cache.mode { | |
| 1295 | CacheMode::Enabled => CacheMode::Bypass, | |
| 1296 | CacheMode::Bypass => CacheMode::Enabled, | |
| 1297 | }; | |
| 1298 | self.engine.set_cache_mode(next); | |
| 1299 | for v in &mut self.viewers { | |
| 1300 | v.dirty = true; | |
| 1301 | } | |
| 1302 | } | |
| 1303 | ViewerAction::SetProxy(scale) => { | |
| 1304 | if self.engine.proxy_scale != scale { | |
| 1305 | self.engine.proxy_scale = scale; | |
| 1306 | self.engine.bust_gpu_cache(); | |
| 1307 | for v in &mut self.viewers { | |
| 1308 | v.cached_frames.clear(); | |
| 1309 | v.dirty = true; | |
| 1310 | } | |
| 1311 | } | |
| 1312 | } | |
| 1313 | ViewerAction::ToggleHiQ => { | |
| 1314 | self.engine.hiq = !self.engine.hiq; | |
| 1315 | for v in &mut self.viewers { | |
| 1316 | v.cached_frames.clear(); | |
| 1317 | v.dirty = true; | |
| 1318 | } | |
| 1319 | } | |
| 1320 | ViewerAction::ClearViewers => { | |
| 1321 | self.clear_viewers(); | |
| 1322 | } | |
| 1323 | ViewerAction::GizmoEdited(edit) => { | |
| 1324 | self.history.push_coalesced(&self.comp, edit.label, &edit.coalesce_key); | |
| 1325 | self.comp.set_input_value(edit.node_id, edit.input, edit.value); | |
| 1326 | self.invalidate_node(Some(edit.node_id)); | |
| 1327 | } | |
| 1328 | } | |
| 1329 | } | |
| 1330 | } | |
| 1331 | ||
| 1332 | fn paint(&mut self, target: &wgpu::TextureView, pixels: [u32; 2]) -> Result<(), Box<dyn Error>> { | |
| 1333 | let scale = self.scale; | |
| 1334 | let cur_f = self.playback.current_frame; | |
| 1335 | let Studio { ui, renderer, comp, viewers, flow, spline, keyframes, .. } = self; | |
| 1336 | ||
| 1337 | let mut custom_prims: HashMap<ui::Id, ([f32; 4], [f32; 2], Vec<draw::Primitive<'_>>)> = HashMap::new(); | |
| 1338 | for slot in [0usize, 1] { | |
| 1339 | let cid = viewer_canvas_id(slot); | |
| 1340 | if let Some(r) = ui.laid_out(cid) { | |
| 1341 | custom_prims.insert(cid, (viewers[slot].background(), [r[0], r[1]], viewers[slot].paint(comp, cur_f))); | |
| 1342 | } | |
| 1343 | } | |
| 1344 | if let Some(r) = ui.laid_out(flow_canvas_id()) { | |
| 1345 | custom_prims.insert(flow_canvas_id(), (flow.background(), [r[0], r[1]], flow.paint(comp))); | |
| 1346 | } | |
| 1347 | if let Some(r) = ui.laid_out(spline_canvas_id()) { | |
| 1348 | custom_prims.insert(spline_canvas_id(), (spline.background(), [r[0], r[1]], spline.paint(comp, cur_f))); | |
| 1349 | } | |
| 1350 | if let Some(r) = ui.laid_out(keyframes_canvas_id()) { | |
| 1351 | custom_prims.insert(keyframes_canvas_id(), (keyframes.background(), [r[0], r[1]], keyframes.paint(comp, cur_f))); | |
| 1352 | } | |
| 1353 | ||
| 1354 | let interface = ui.layers(); | |
| 1355 | let layers: Vec<draw::Layer> = interface | |
| 1356 | .iter() | |
| 1357 | .filter_map(|layer| match layer { | |
| 1358 | ui::Layer::Primitives(prims) => Some(prims.layer(scale)), | |
| 1359 | ui::Layer::Custom { id, rect, .. } => { | |
| 1360 | let (bg, corner, prims) = custom_prims.get(id)?; | |
| 1361 | Some(draw::Layer { | |
| 1362 | scale, | |
| 1363 | origin: corner.map(|v| v * scale), | |
| 1364 | clip: Some(rect.map(|v| v * scale)), | |
| 1365 | backdrop: Some(*bg), | |
| 1366 | round: None, | |
| 1367 | motion: None, | |
| 1368 | primitives: prims, | |
| 1369 | }) | |
| 1370 | } | |
| 1371 | }) | |
| 1372 | .collect(); | |
| 1373 | ||
| 1374 | renderer | |
| 1375 | .draw(target, pixels, ui.theme.base, &layers) | |
| 1376 | .map_err(|e| format!("{e:?}"))?; | |
| 1377 | Ok(()) | |
| 1378 | } | |
| 1379 | } | |
| 1380 | ||
| 1381 | fn run_snapshot(cli: &Cli, out_path: &Path) -> Result<(), Box<dyn Error>> { | |
| 1382 | theme::fonts::register()?; | |
| 1383 | let comp = Comp::from_fusion_file(&cli.comp_path)?; | |
| 1384 | let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); | |
| 1385 | let adapter = pollster::block_on(instance.request_adapter(&Default::default()))?; | |
| 1386 | let mut req_features = wgpu::Features::empty(); | |
| 1387 | if adapter.features().contains(wgpu::Features::FLOAT32_FILTERABLE) { | |
| 1388 | req_features |= wgpu::Features::FLOAT32_FILTERABLE; | |
| 1389 | } | |
| 1390 | let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { | |
| 1391 | required_features: req_features, | |
| 1392 | ..Default::default() | |
| 1393 | }))?; | |
| 1394 | let renderer = draw::Renderer::new(device, queue, wgpu::TextureFormat::Rgba8Unorm); | |
| 1395 | ||
| 1396 | let mut studio = Studio::new( | |
| 1397 | renderer, | |
| 1398 | comp, | |
| 1399 | cli.frame, | |
| 1400 | cli.select_node.as_deref(), | |
| 1401 | cli.dual_viewer, | |
| 1402 | cli.show_keyframes, | |
| 1403 | false, | |
| 1404 | ); | |
| 1405 | // Load audio waveform peaks if available so snapshot verifies the waveform overlay too | |
| 1406 | if let Some(af) = studio.comp.audio_filename.clone() { | |
| 1407 | let p = PathBuf::from(af); | |
| 1408 | if p.exists() { | |
| 1409 | if let Ok(track) = AudioTrack::load(&p) { | |
| 1410 | let peaks = track.waveform_peaks( | |
| 1411 | studio.comp.fps, | |
| 1412 | studio.comp.audio_offset, | |
| 1413 | studio.comp.render_range[0], | |
| 1414 | studio.comp.render_range[1], | |
| 1415 | ); | |
| 1416 | studio.viewers[0].audio_peaks = peaks.clone(); | |
| 1417 | studio.keyframes.audio_peaks = peaks; | |
| 1418 | studio.keyframes.audio_range = studio.comp.render_range; | |
| 1419 | } | |
| 1420 | } | |
| 1421 | } | |
| 1422 | if cli.open_add_tool { | |
| 1423 | studio.add_tool.open(); | |
| 1424 | } | |
| 1425 | studio.size = [1440.0, 900.0]; | |
| 1426 | studio.scale = 2.0; | |
| 1427 | ||
| 1428 | let now = Instant::now(); | |
| 1429 | studio.frame(now); | |
| 1430 | studio.frame(now + Duration::from_millis(16)); | |
| 1431 | ||
| 1432 | let pixels = studio.size.map(|s| (s * studio.scale) as u32); | |
| 1433 | let tex = studio.renderer.offscreen(pixels); | |
| 1434 | let view = tex.create_view(&Default::default()); | |
| 1435 | studio.paint(&view, pixels)?; | |
| 1436 | let rgba = studio.renderer.read(&tex)?; | |
| 1437 | ||
| 1438 | let mut enc = png::Encoder::new(File::create(out_path)?, pixels[0], pixels[1]); | |
| 1439 | enc.set_color(png::ColorType::Rgba); | |
| 1440 | enc.set_depth(png::BitDepth::Eight); | |
| 1441 | enc.write_header()?.write_image_data(&rgba)?; | |
| 1442 | eprintln!( | |
| 1443 | "Wrote docked Studio snapshot to {} ({}x{}) | eval={:.1}ms", | |
| 1444 | out_path.display(), | |
| 1445 | pixels[0], | |
| 1446 | pixels[1], | |
| 1447 | studio.viewers[0].last_stats.eval_ms | |
| 1448 | ); | |
| 1449 | Ok(()) | |
| 1450 | } | |
| 1451 | ||
| 1452 | #[allow(dead_code)] | |
| 1453 | enum UserEvent { | |
| 1454 | Redraw, | |
| 1455 | #[cfg(target_os = "macos")] | |
| 1456 | Quit, | |
| 1457 | Accessibility(accesskit_winit::Event), | |
| 1458 | } | |
| 1459 | ||
| 1460 | impl From<accesskit_winit::Event> for UserEvent { | |
| 1461 | fn from(event: accesskit_winit::Event) -> Self { | |
| 1462 | Self::Accessibility(event) | |
| 1463 | } | |
| 1464 | } | |
| 1465 | ||
| 1466 | struct WindowHost { | |
| 1467 | window: Arc<Window>, | |
| 1468 | surface: Surface, | |
| 1469 | studio: Studio, | |
| 1470 | access: Accessibility, | |
| 1471 | occluded: bool, | |
| 1472 | } | |
| 1473 | ||
| 1474 | struct App { | |
| 1475 | proxy: EventLoopProxy<UserEvent>, | |
| 1476 | cli: Cli, | |
| 1477 | host: Option<WindowHost>, | |
| 1478 | error: Option<Box<dyn Error>>, | |
| 1479 | } | |
| 1480 | ||
| 1481 | fn run_window(cli: &Cli) -> Result<(), Box<dyn Error>> { | |
| 1482 | theme::fonts::register()?; | |
| 1483 | let mut builder = EventLoop::<UserEvent>::with_user_event(); | |
| 1484 | #[cfg(target_os = "macos")] | |
| 1485 | { | |
| 1486 | use winit::platform::macos::EventLoopBuilderExtMacOS; | |
| 1487 | builder.with_activate_ignoring_other_apps(false); | |
| 1488 | } | |
| 1489 | let event_loop = builder.build()?; | |
| 1490 | let proxy = event_loop.create_proxy(); | |
| 1491 | let mut app = App { | |
| 1492 | proxy, | |
| 1493 | cli: Cli { | |
| 1494 | comp_path: cli.comp_path.clone(), | |
| 1495 | snapshot: None, | |
| 1496 | frame: cli.frame, | |
| 1497 | select_node: cli.select_node.clone(), | |
| 1498 | dual_viewer: cli.dual_viewer, | |
| 1499 | open_add_tool: cli.open_add_tool, | |
| 1500 | show_keyframes: cli.show_keyframes, | |
| 1501 | }, | |
| 1502 | host: None, | |
| 1503 | error: None, | |
| 1504 | }; | |
| 1505 | event_loop.run_app(&mut app)?; | |
| 1506 | match app.error { | |
| 1507 | Some(e) => Err(e), | |
| 1508 | None => Ok(()), | |
| 1509 | } | |
| 1510 | } | |
| 1511 | ||
| 1512 | impl WindowHost { | |
| 1513 | fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<UserEvent>, cli: &Cli) -> Result<Self, Box<dyn Error>> { | |
| 1514 | let comp = Comp::from_fusion_file(&cli.comp_path)?; | |
| 1515 | let title = format!("Fission — {}", comp.name); | |
| 1516 | let attrs = Window::default_attributes() | |
| 1517 | .with_title(title) | |
| 1518 | .with_inner_size(LogicalSize::new(1440.0, 900.0)); | |
| 1519 | let (window, access) = Accessibility::window(event_loop, attrs, [640.0, 420.0], proxy.clone(), ui::Id::ROOT)?; | |
| 1520 | let (surface, renderer) = pollster::block_on(Surface::new(window.clone()))?; | |
| 1521 | let mut studio = Studio::new( | |
| 1522 | renderer, | |
| 1523 | comp, | |
| 1524 | cli.frame, | |
| 1525 | cli.select_node.as_deref(), | |
| 1526 | cli.dual_viewer, | |
| 1527 | cli.show_keyframes, | |
| 1528 | true, | |
| 1529 | ); | |
| 1530 | studio.ui.window_focused = window.has_focus(); | |
| 1531 | studio.ui.clipboard = Clipboard::system(); | |
| 1532 | window.request_redraw(); | |
| 1533 | #[cfg(target_os = "macos")] | |
| 1534 | { | |
| 1535 | let quit = proxy.clone(); | |
| 1536 | ui::host::macos::quit(move || { let _ = quit.send_event(UserEvent::Quit); }); | |
| 1537 | } | |
| 1538 | Ok(Self { window, surface, studio, access, occluded: false }) | |
| 1539 | } | |
| 1540 | ||
| 1541 | fn redraw(&mut self) -> Result<(), Box<dyn Error>> { | |
| 1542 | let now = Instant::now(); | |
| 1543 | let wheel = self.surface.wheel.frame(now, self.studio.ui.reduces_motion()); | |
| 1544 | if wheel != [0.0; 2] { | |
| 1545 | self.studio.ui.event(ui::Event::Wheel(wheel)); | |
| 1546 | } | |
| 1547 | self.studio.scale = self.window.scale_factor() as f32; | |
| 1548 | let pixels = self.surface.fit(&self.studio.renderer); | |
| 1549 | if pixels.contains(&0) { | |
| 1550 | return Ok(()); | |
| 1551 | } | |
| 1552 | self.studio.size = pixels.map(|s| s as f32 / self.studio.scale); | |
| 1553 | let wants_more = self.studio.frame(now); | |
| 1554 | self.window.set_title(&self.studio.window_title()); | |
| 1555 | ||
| 1556 | self.window.set_cursor(self.studio.ui.cursor().unwrap_or(CursorIcon::Default)); | |
| 1557 | self.access.update_views(&mut self.studio.ui, "Fission", f64::from(self.studio.scale), |_| Vec::new()); | |
| 1558 | ||
| 1559 | if !self.occluded && let Some(frame) = self.surface.frame(&self.studio.renderer, Some(&mut self.studio.ui))? { | |
| 1560 | self.studio.paint(&frame.target, pixels)?; | |
| 1561 | self.surface.present(&self.studio.renderer, frame); | |
| 1562 | } | |
| 1563 | if wants_more && !self.occluded { | |
| 1564 | self.window.request_redraw(); | |
| 1565 | } | |
| 1566 | Ok(()) | |
| 1567 | } | |
| 1568 | ||
| 1569 | fn input(&mut self, event: ui::Event) { | |
| 1570 | let typing = self.studio.ui.typing(); | |
| 1571 | // Global file & history shortcuts: Cmd+S, Cmd+Shift+S, Cmd+O, Cmd+Z, Cmd+Shift+Z / Cmd+Y, Cmd+Shift+R | |
| 1572 | let key_cmd = match &event { | |
| 1573 | ui::Event::KeyInput { physical, modifiers, state: ElementState::Pressed, repeat: false, .. } => { | |
| 1574 | let cmd = modifiers.super_key() || modifiers.control_key(); | |
| 1575 | let shift = modifiers.shift_key(); | |
| 1576 | if cmd && !shift && *physical == PhysicalKey::Code(KeyCode::KeyS) { | |
| 1577 | Some("save") | |
| 1578 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyS) { | |
| 1579 | Some("save_as") | |
| 1580 | } else if cmd && !shift && *physical == PhysicalKey::Code(KeyCode::KeyO) { | |
| 1581 | Some("open") | |
| 1582 | } else if cmd && !shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyZ) { | |
| 1583 | Some("undo") | |
| 1584 | } else if cmd && shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyZ) { | |
| 1585 | Some("redo") | |
| 1586 | } else if cmd && !shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyY) { | |
| 1587 | Some("redo") | |
| 1588 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyR) { | |
| 1589 | Some("export") | |
| 1590 | } else { | |
| 1591 | None | |
| 1592 | } | |
| 1593 | } | |
| 1594 | ui::Event::Key { key: Key::Character(c), modifiers, .. } => { | |
| 1595 | let cmd = modifiers.super_key() || modifiers.control_key(); | |
| 1596 | let shift = modifiers.shift_key(); | |
| 1597 | if cmd && !shift && c.eq_ignore_ascii_case("s") { | |
| 1598 | Some("save") | |
| 1599 | } else if cmd && shift && c.eq_ignore_ascii_case("s") { | |
| 1600 | Some("save_as") | |
| 1601 | } else if cmd && !shift && c.eq_ignore_ascii_case("o") { | |
| 1602 | Some("open") | |
| 1603 | } else if cmd && !shift && !typing && c.eq_ignore_ascii_case("z") { | |
| 1604 | Some("undo") | |
| 1605 | } else if cmd && shift && !typing && c.eq_ignore_ascii_case("z") { | |
| 1606 | Some("redo") | |
| 1607 | } else if cmd && !shift && !typing && c.eq_ignore_ascii_case("y") { | |
| 1608 | Some("redo") | |
| 1609 | } else if cmd && shift && c.eq_ignore_ascii_case("r") { | |
| 1610 | Some("export") | |
| 1611 | } else { | |
| 1612 | None | |
| 1613 | } | |
| 1614 | } | |
| 1615 | _ => None, | |
| 1616 | }; | |
| 1617 | if let Some(action) = key_cmd { | |
| 1618 | match action { | |
| 1619 | "save" => { let _ = self.studio.save_comp(); } | |
| 1620 | "save_as" => { let _ = self.studio.save_comp_as(); } | |
| 1621 | "open" => self.studio.open_comp_dialog(), | |
| 1622 | "undo" => self.studio.undo(), | |
| 1623 | "redo" => self.studio.redo(), | |
| 1624 | "export" => { let _ = self.studio.export_saver_pngs(); } | |
| 1625 | _ => {} | |
| 1626 | } | |
| 1627 | self.window.set_title(&self.studio.window_title()); | |
| 1628 | self.window.request_redraw(); | |
| 1629 | return; | |
| 1630 | } | |
| 1631 | ||
| 1632 | // Shift+Space opens Add Tool launcher (when not typing in a text field) | |
| 1633 | if !typing { | |
| 1634 | let is_shift_space = match &event { | |
| 1635 | ui::Event::KeyInput { key: Key::Named(NamedKey::Space), modifiers, state: ElementState::Pressed, repeat: false, .. } | |
| 1636 | | ui::Event::Key { key: Key::Named(NamedKey::Space), modifiers, .. } => { | |
| 1637 | modifiers.shift_key() && !modifiers.super_key() && !modifiers.control_key() | |
| 1638 | } | |
| 1639 | _ => false, | |
| 1640 | }; | |
| 1641 | if is_shift_space { | |
| 1642 | self.studio.add_tool.open(); | |
| 1643 | self.window.request_redraw(); | |
| 1644 | return; | |
| 1645 | } | |
| 1646 | // Global Space toggles playback (synced with audio) and `` ` `` clears both viewers from any focused pane! | |
| 1647 | let is_space = match &event { | |
| 1648 | ui::Event::KeyInput { key: Key::Named(NamedKey::Space), modifiers, state: ElementState::Pressed, repeat: false, .. } | |
| 1649 | | ui::Event::Key { key: Key::Named(NamedKey::Space), modifiers, .. } => { | |
| 1650 | !modifiers.shift_key() && !modifiers.super_key() && !modifiers.control_key() | |
| 1651 | } | |
| 1652 | _ => false, | |
| 1653 | }; | |
| 1654 | if is_space && !self.studio.add_tool.open { | |
| 1655 | self.studio.playback.toggle_play(Instant::now()); | |
| 1656 | self.studio.sync_audio(true, false); | |
| 1657 | self.window.request_redraw(); | |
| 1658 | return; | |
| 1659 | } | |
| 1660 | let is_backquote = match &event { | |
| 1661 | ui::Event::KeyInput { physical: PhysicalKey::Code(KeyCode::Backquote), state: ElementState::Pressed, repeat: false, .. } => true, | |
| 1662 | ui::Event::Key { key: Key::Character(c), .. } if c.as_str() == "`" || c.as_str() == "~" => true, | |
| 1663 | _ => false, | |
| 1664 | }; | |
| 1665 | if is_backquote && !self.studio.add_tool.open { | |
| 1666 | self.studio.clear_viewers(); | |
| 1667 | self.window.request_redraw(); | |
| 1668 | return; | |
| 1669 | } | |
| 1670 | } | |
| 1671 | if !self.studio.ui.popups_open() && !self.studio.add_tool.open && let ui::Event::Key { key, modifiers, .. } = &event { | |
| 1672 | let cmd = match key { | |
| 1673 | Key::Named(NamedKey::Tab) if modifiers.control_key() => { | |
| 1674 | Some(if modifiers.shift_key() { Command::Previous } else { Command::Next }) | |
| 1675 | } | |
| 1676 | _ => None, | |
| 1677 | }; | |
| 1678 | if let Some(cmd) = cmd { | |
| 1679 | let _ = self.studio.dock.at(&mut self.studio.layout, &mut self.studio.seen, &String::clone).run(cmd); | |
| 1680 | self.window.request_redraw(); | |
| 1681 | return; | |
| 1682 | } | |
| 1683 | } | |
| 1684 | self.studio.ui.event(event); | |
| 1685 | self.window.request_redraw(); | |
| 1686 | } | |
| 1687 | } | |
| 1688 | ||
| 1689 | impl ApplicationHandler<UserEvent> for App { | |
| 1690 | fn resumed(&mut self, event_loop: &ActiveEventLoop) { | |
| 1691 | if self.host.is_some() { return; } | |
| 1692 | match WindowHost::new(event_loop, self.proxy.clone(), &self.cli) { | |
| 1693 | Ok(host) => self.host = Some(host), | |
| 1694 | Err(e) => { | |
| 1695 | self.error = Some(e); | |
| 1696 | event_loop.exit(); | |
| 1697 | } | |
| 1698 | } | |
| 1699 | } | |
| 1700 | ||
| 1701 | fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) { | |
| 1702 | #[cfg(not(target_os = "macos"))] | |
| 1703 | let _ = event_loop; | |
| 1704 | let Some(host) = &mut self.host else { return }; | |
| 1705 | match event { | |
| 1706 | UserEvent::Redraw => host.window.request_redraw(), | |
| 1707 | #[cfg(target_os = "macos")] | |
| 1708 | UserEvent::Quit => { | |
| 1709 | ui::host::macos::finish_quit(true); | |
| 1710 | event_loop.exit(); | |
| 1711 | } | |
| 1712 | UserEvent::Accessibility(ev) => match ev.window_event { | |
| 1713 | accesskit_winit::WindowEvent::InitialTreeRequested => { | |
| 1714 | host.access.forget(&mut host.studio.ui); | |
| 1715 | host.window.request_redraw(); | |
| 1716 | } | |
| 1717 | accesskit_winit::WindowEvent::AccessibilityDeactivated => host.access.forget(&mut host.studio.ui), | |
| 1718 | accesskit_winit::WindowEvent::ActionRequested(req) => host.input(ui::Event::Access(req)), | |
| 1719 | }, | |
| 1720 | } | |
| 1721 | } | |
| 1722 | ||
| 1723 | fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) { | |
| 1724 | let Some(host) = &mut self.host else { return }; | |
| 1725 | host.access.adapter.process_event(&host.window, &event); | |
| 1726 | if let Some(Err(e)) = ui::host::draw(&event, || host.redraw()) { | |
| 1727 | self.error = Some(e); | |
| 1728 | event_loop.exit(); | |
| 1729 | return; | |
| 1730 | } | |
| 1731 | let page = host.surface.size[1] as f32 / host.window.scale_factor() as f32; | |
| 1732 | if let Some(wheel) = host.surface.wheel.input(&host.window, &event, page, host.studio.ui.reduces_motion(), Instant::now()) { | |
| 1733 | if wheel != [0.0; 2] { host.input(ui::Event::Wheel(wheel)); } | |
| 1734 | host.window.request_redraw(); | |
| 1735 | return; | |
| 1736 | } | |
| 1737 | match event { | |
| 1738 | WindowEvent::CloseRequested => event_loop.exit(), | |
| 1739 | WindowEvent::Focused(focused) => { | |
| 1740 | ui::host::focus(&mut host.studio.ui, focused); | |
| 1741 | host.window.request_redraw(); | |
| 1742 | } | |
| 1743 | WindowEvent::ScaleFactorChanged { .. } => { | |
| 1744 | host.studio.renderer.clear_glyph_cache(); | |
| 1745 | host.window.request_redraw(); | |
| 1746 | } | |
| 1747 | WindowEvent::Occluded(occluded) => { | |
| 1748 | host.occluded = occluded; | |
| 1749 | if occluded { host.surface.wheel.cancel(); } | |
| 1750 | else { host.window.request_redraw(); } | |
| 1751 | } | |
| 1752 | // Route full physical + logical key events when a custom pane (Viewer/Flow/Spline) has focus! | |
| 1753 | WindowEvent::KeyboardInput { event: key_ev, is_synthetic: false, .. } | |
| 1754 | if host.studio.ui.full_key_input() => | |
| 1755 | { | |
| 1756 | let held = host.studio.ui.modifiers(); | |
| 1757 | host.input(ui::Event::from_key(key_ev, held)); | |
| 1758 | } | |
| 1759 | other => { | |
| 1760 | if let Some(input) = ui::Event::from_window(other, host.window.scale_factor() as f32, host.studio.ui.modifiers()) { | |
| 1761 | host.input(input); | |
| 1762 | } | |
| 1763 | } | |
| 1764 | } | |
| 1765 | } | |
| 1766 | ||
| 1767 | fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { | |
| 1768 | if let Some(host) = &self.host { | |
| 1769 | if !host.occluded { | |
| 1770 | if host.studio.playback.playing { | |
| 1771 | let wake = host.studio.playback.next_wake(&host.studio.comp); | |
| 1772 | event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + wake)); | |
| 1773 | host.window.request_redraw(); | |
| 1774 | return; | |
| 1775 | } | |
| 1776 | if host.studio.prefetcher.as_ref().is_some_and(RamPrefetcher::is_busy) { | |
| 1777 | event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(200))); | |
| 1778 | host.window.request_redraw(); | |
| 1779 | return; | |
| 1780 | } | |
| 1781 | } | |
| 1782 | } | |
| 1783 | event_loop.set_control_flow(ControlFlow::Wait); | |
| 1784 | } | |
| 1785 | } | |
| 1786 | ||
| 1787 | fn spawn_audio_loader(comp: &Comp) -> Option<mpsc::Receiver<Result<AudioTrack, String>>> { | |
| 1788 | comp.audio_filename.as_ref().and_then(|af| { | |
| 1789 | let p = PathBuf::from(af); | |
| 1790 | if !p.exists() { | |
| 1791 | return None; | |
| 1792 | } | |
| 1793 | let (tx, rx) = mpsc::channel(); | |
| 1794 | std::thread::spawn(move || { | |
| 1795 | let _ = tx.send(AudioTrack::load(&p)); | |
| 1796 | }); | |
| 1797 | Some(rx) | |
| 1798 | }) | |
| 1799 | } | |
| 1800 | ||
| 1801 | fn studio_btn(ui: &mut ui::Ui, part: impl std::hash::Hash, label: &str, active: bool) -> ui::Signal { | |
| 1802 | let t = &ui.theme; | |
| 1803 | let fill = if active { | |
| 1804 | [t.accent[0], t.accent[1], t.accent[2], 0.24] | |
| 1805 | } else { | |
| 1806 | [t.base[0], t.base[1], t.base[2], 0.65] | |
| 1807 | }; | |
| 1808 | let border = if active { | |
| 1809 | [t.accent[0], t.accent[1], t.accent[2], 0.65] | |
| 1810 | } else { | |
| 1811 | t.rule | |
| 1812 | }; | |
| 1813 | let ink = if active { t.text } else { t.text_dim }; | |
| 1814 | ui.leaf( | |
| 1815 | part, | |
| 1816 | ui::Spec { | |
| 1817 | cursor: Some(CursorIcon::Pointer), | |
| 1818 | flags: ui::Flags::CLICKABLE, | |
| 1819 | size: [ui::fit(), ui::px(22.0)], | |
| 1820 | text: Some(label), | |
| 1821 | font_size: Some(11.0), | |
| 1822 | medium: true, | |
| 1823 | color: Some(ink), | |
| 1824 | fill: Some(fill), | |
| 1825 | border: Some(border), | |
| 1826 | hover_fill: Some(t.ghost_soft), | |
| 1827 | hover_border: Some(t.accent), | |
| 1828 | radius: 5.0, | |
| 1829 | pad: [7.0, 0.0], | |
| 1830 | center: true, | |
| 1831 | ..ui::Spec::default() | |
| 1832 | }, | |
| 1833 | ) | |
| 1834 | } | |
| 1835 | ||
| 1836 | fn native_open_comp_dialog() -> Option<PathBuf> { | |
| 1837 | let script = r#"POSIX path of (choose file with prompt "Open Fusion Composition" of type {"comp"})"#; | |
| 1838 | let out = ProcessCommand::new("osascript").args(["-e", script]).output().ok()?; | |
| 1839 | if !out.status.success() { | |
| 1840 | return None; | |
| 1841 | } | |
| 1842 | let s = String::from_utf8_lossy(&out.stdout).trim().to_owned(); | |
| 1843 | if s.is_empty() { None } else { Some(PathBuf::from(s)) } | |
| 1844 | } | |
| 1845 | ||
| 1846 | fn native_save_comp_dialog(default_name: &str) -> Option<PathBuf> { | |
| 1847 | let safe = default_name.replace('"', ""); | |
| 1848 | let script = format!( | |
| 1849 | r#"POSIX path of (choose file name with prompt "Save Fusion Composition As" default name "{safe}")"# | |
| 1850 | ); | |
| 1851 | let out = ProcessCommand::new("osascript").args(["-e", &script]).output().ok()?; | |
| 1852 | if !out.status.success() { | |
| 1853 | return None; | |
| 1854 | } | |
| 1855 | let s = String::from_utf8_lossy(&out.stdout).trim().to_owned(); | |
| 1856 | if s.is_empty() { None } else { Some(PathBuf::from(s)) } | |
| 1857 | } |
crates/studio/Cargo.toml deleted-33| ... | ... | @@ -1,33 +0,0 @@ |
| 1 | [package] | |
| 2 | name = "studio" | |
| 3 | edition.workspace = true | |
| 4 | version.workspace = true | |
| 5 | publish.workspace = true | |
| 6 | ||
| 7 | [[bin]] | |
| 8 | name = "compositor" | |
| 9 | path = "src/main.rs" | |
| 10 | ||
| 11 | [dependencies] | |
| 12 | accesskit.workspace = true | |
| 13 | accesskit_winit.workspace = true | |
| 14 | dock.workspace = true | |
| 15 | draw.workspace = true | |
| 16 | engine.workspace = true | |
| 17 | flow.workspace = true | |
| 18 | model.workspace = true | |
| 19 | png.workspace = true | |
| 20 | pollster.workspace = true | |
| 21 | spline.workspace = true | |
| 22 | state.workspace = true | |
| 23 | text.workspace = true | |
| 24 | theme.workspace = true | |
| 25 | ui.workspace = true | |
| 26 | viewer.workspace = true | |
| 27 | wgpu.workspace = true | |
| 28 | winit.workspace = true | |
| 29 | ||
| 30 | [target.'cfg(target_os = "macos")'.dependencies] | |
| 31 | objc2.workspace = true | |
| 32 | objc2-app-kit = { workspace = true, features = ["std", "NSWorkspace", "NSAccessibility", "NSApplication", "NSResponder", "NSMenu", "NSMenuItem"] } | |
| 33 | objc2-foundation = { workspace = true, features = ["std", "NSString"] } |
crates/studio/src/add_tool.rs deleted-557| ... | ... | @@ -1,557 +0,0 @@ |
| 1 | //! `Shift+Space` Add Tool fuzzy-search launcher modal overlay. | |
| 2 | ||
| 3 | use winit::keyboard::{Key, NamedKey}; | |
| 4 | ||
| 5 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 6 | pub enum ToolCategory { | |
| 7 | Generator, | |
| 8 | Composite, | |
| 9 | Color, | |
| 10 | Keyer, | |
| 11 | BlurGlow, | |
| 12 | TransformWarp, | |
| 13 | Mask, | |
| 14 | Particle, | |
| 15 | Scene3D, | |
| 16 | Utility, | |
| 17 | } | |
| 18 | ||
| 19 | impl ToolCategory { | |
| 20 | pub fn label(self) -> &'static str { | |
| 21 | match self { | |
| 22 | Self::Generator => "Generator", | |
| 23 | Self::Composite => "Composite", | |
| 24 | Self::Color => "Color", | |
| 25 | Self::Keyer => "Keyer", | |
| 26 | Self::BlurGlow => "Blur/Glow", | |
| 27 | Self::TransformWarp => "Transform/Warp", | |
| 28 | Self::Mask => "Mask", | |
| 29 | Self::Particle => "Particle", | |
| 30 | Self::Scene3D => "3D", | |
| 31 | Self::Utility => "Utility", | |
| 32 | } | |
| 33 | } | |
| 34 | ||
| 35 | pub fn color(self) -> [f32; 4] { | |
| 36 | match self { | |
| 37 | Self::Generator => [0.36, 0.82, 0.48, 1.0], | |
| 38 | Self::Composite => [0.68, 0.72, 0.82, 1.0], | |
| 39 | Self::Color => [0.94, 0.62, 0.24, 1.0], | |
| 40 | Self::Keyer => [0.30, 0.84, 0.64, 1.0], | |
| 41 | Self::BlurGlow => [0.94, 0.50, 0.28, 1.0], | |
| 42 | Self::TransformWarp => [0.88, 0.72, 0.28, 1.0], | |
| 43 | Self::Mask => [0.34, 0.66, 0.98, 1.0], | |
| 44 | Self::Particle => [0.88, 0.46, 0.78, 1.0], | |
| 45 | Self::Scene3D => [0.30, 0.86, 0.92, 1.0], | |
| 46 | Self::Utility => [0.56, 0.62, 0.72, 1.0], | |
| 47 | } | |
| 48 | } | |
| 49 | } | |
| 50 | ||
| 51 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 52 | pub struct ToolEntry { | |
| 53 | pub kind: &'static str, | |
| 54 | pub display: &'static str, | |
| 55 | pub aliases: &'static str, | |
| 56 | pub category: ToolCategory, | |
| 57 | pub is_generator: bool, | |
| 58 | } | |
| 59 | ||
| 60 | pub const TOOL_CATALOG: &[ToolEntry] = &[ | |
| 61 | // Generator | |
| 62 | ToolEntry { kind: "Background", display: "Background", aliases: "bg solid color", category: ToolCategory::Generator, is_generator: true }, | |
| 63 | ToolEntry { kind: "FastNoise", display: "FastNoise", aliases: "fn noise fractal perlin", category: ToolCategory::Generator, is_generator: true }, | |
| 64 | ToolEntry { kind: "Plasma", display: "Plasma", aliases: "gradient organic", category: ToolCategory::Generator, is_generator: true }, | |
| 65 | ToolEntry { kind: "TextPlus", display: "Text+", aliases: "textplus title font", category: ToolCategory::Generator, is_generator: true }, | |
| 66 | ToolEntry { kind: "Loader", display: "Loader", aliases: "ld read media footage image", category: ToolCategory::Generator, is_generator: true }, | |
| 67 | // Composite | |
| 68 | ToolEntry { kind: "Merge", display: "Merge", aliases: "mrg over blend comp", category: ToolCategory::Composite, is_generator: false }, | |
| 69 | ToolEntry { kind: "Dissolve", display: "Dissolve", aliases: "dx mix crossfade", category: ToolCategory::Composite, is_generator: false }, | |
| 70 | ToolEntry { kind: "ChannelBoolean", display: "Channel Booleans", aliases: "cb channel matte", category: ToolCategory::Composite, is_generator: false }, | |
| 71 | ToolEntry { kind: "Switch", display: "Switch", aliases: "sw select", category: ToolCategory::Composite, is_generator: false }, | |
| 72 | ToolEntry { kind: "Shadow", display: "Shadow", aliases: "drop shadow", category: ToolCategory::Composite, is_generator: false }, | |
| 73 | // Color | |
| 74 | ToolEntry { kind: "BrightnessContrast", display: "Brightness / Contrast", aliases: "bc gain lift gamma", category: ToolCategory::Color, is_generator: false }, | |
| 75 | ToolEntry { kind: "ColorCorrector", display: "Color Corrector", aliases: "cc grade wheel saturation", category: ToolCategory::Color, is_generator: false }, | |
| 76 | ToolEntry { kind: "ColorGain", display: "Color Gain", aliases: "cg rgb gain", category: ToolCategory::Color, is_generator: false }, | |
| 77 | ToolEntry { kind: "ColorCurves", display: "Color Curves", aliases: "curves lut", category: ToolCategory::Color, is_generator: false }, | |
| 78 | ToolEntry { kind: "HueCurves", display: "Hue Curves", aliases: "hue sat", category: ToolCategory::Color, is_generator: false }, | |
| 79 | ToolEntry { kind: "GamutConvert", display: "Gamut", aliases: "colorspace srgb linear", category: ToolCategory::Color, is_generator: false }, | |
| 80 | ToolEntry { kind: "InvertColor", display: "Invert Color", aliases: "negate invert", category: ToolCategory::Color, is_generator: false }, | |
| 81 | ToolEntry { kind: "FilmGrain", display: "Film Grain", aliases: "grain noise", category: ToolCategory::Color, is_generator: false }, | |
| 82 | ToolEntry { kind: "Fuse.Colorize", display: "Colorize", aliases: "tint duotone", category: ToolCategory::Color, is_generator: false }, | |
| 83 | ToolEntry { kind: "Fuse.Outline", display: "Outline", aliases: "stroke edge", category: ToolCategory::Color, is_generator: false }, | |
| 84 | // Keyer | |
| 85 | ToolEntry { kind: "UltraKeyer", display: "Ultra Keyer", aliases: "uk chroma green blue screen", category: ToolCategory::Keyer, is_generator: false }, | |
| 86 | ToolEntry { kind: "LumaKeyer", display: "Luma Keyer", aliases: "lk luminance key", category: ToolCategory::Keyer, is_generator: false }, | |
| 87 | ToolEntry { kind: "Cryptomatte", display: "Cryptomatte", aliases: "crypto id matte", category: ToolCategory::Keyer, is_generator: false }, | |
| 88 | // Blur / Glow | |
| 89 | ToolEntry { kind: "Blur", display: "Blur", aliases: "bl gaussian defocus", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 90 | ToolEntry { kind: "DirectionalBlur", display: "Directional Blur", aliases: "db radial zoom motion blur", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 91 | ToolEntry { kind: "VariBlur", display: "VariBlur", aliases: "vb depth blur", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 92 | ToolEntry { kind: "Glow", display: "Glow", aliases: "gl bloom", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 93 | ToolEntry { kind: "SoftGlow", display: "Soft Glow", aliases: "sg bloom halo", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 94 | ToolEntry { kind: "Fuse.XGlow", display: "XGlow", aliases: "exponential glow", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 95 | ToolEntry { kind: "Sharpen", display: "Sharpen", aliases: "unsharp crisp", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 96 | ToolEntry { kind: "Filter", display: "Filter", aliases: "sobel emboss laplacian", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 97 | ToolEntry { kind: "ErodeDilate", display: "Erode / Dilate", aliases: "ed shrink grow matte", category: ToolCategory::BlurGlow, is_generator: false }, | |
| 98 | // Transform / Warp | |
| 99 | ToolEntry { kind: "Transform", display: "Transform", aliases: "xf move scale rotate", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 100 | ToolEntry { kind: "CameraShake", display: "Camera Shake", aliases: "shake jitter", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 101 | ToolEntry { kind: "CornerPositioner", display: "Corner Positioner", aliases: "cp pin perspective", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 102 | ToolEntry { kind: "DVE", display: "DVE", aliases: "3d card tilt", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 103 | ToolEntry { kind: "Scale", display: "Scale", aliases: "resize", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 104 | ToolEntry { kind: "BetterResize", display: "Resize", aliases: "rs scale format", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 105 | ToolEntry { kind: "Crop", display: "Crop", aliases: "trim canvas", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 106 | ToolEntry { kind: "Displace", display: "Displace", aliases: "dsp refract warp bump", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 107 | ToolEntry { kind: "Vortex", display: "Vortex", aliases: "swirl twist", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 108 | ToolEntry { kind: "Fuse.XfChroma", display: "XfChroma", aliases: "chromatic aberration rgb split", category: ToolCategory::TransformWarp, is_generator: false }, | |
| 109 | // Mask | |
| 110 | ToolEntry { kind: "EllipseMask", display: "Ellipse Mask", aliases: "el circle oval", category: ToolCategory::Mask, is_generator: true }, | |
| 111 | ToolEntry { kind: "RectangleMask", display: "Rectangle Mask", aliases: "rect box square", category: ToolCategory::Mask, is_generator: true }, | |
| 112 | ToolEntry { kind: "PolylineMask", display: "Polygon Mask", aliases: "poly roto spline", category: ToolCategory::Mask, is_generator: true }, | |
| 113 | ToolEntry { kind: "BitmapMask", display: "Bitmap Mask", aliases: "bmp alpha luma", category: ToolCategory::Mask, is_generator: false }, | |
| 114 | // Particle | |
| 115 | ToolEntry { kind: "pEmitter", display: "pEmitter", aliases: "pe particle emit", category: ToolCategory::Particle, is_generator: true }, | |
| 116 | ToolEntry { kind: "pRender", display: "pRender", aliases: "pr particle render", category: ToolCategory::Particle, is_generator: false }, | |
| 117 | ToolEntry { kind: "pTurbulence", display: "pTurbulence", aliases: "pt particle wind", category: ToolCategory::Particle, is_generator: false }, | |
| 118 | ToolEntry { kind: "pDirectionalForce", display: "pDirectionalForce", aliases: "pf gravity force", category: ToolCategory::Particle, is_generator: false }, | |
| 119 | ToolEntry { kind: "pPointForce", display: "pPointForce", aliases: "particle attract", category: ToolCategory::Particle, is_generator: false }, | |
| 120 | ToolEntry { kind: "pDrag", display: "pDrag", aliases: "particle damping", category: ToolCategory::Particle, is_generator: false }, | |
| 121 | ToolEntry { kind: "pFriction", display: "pFriction", aliases: "particle friction", category: ToolCategory::Particle, is_generator: false }, | |
| 122 | ToolEntry { kind: "pKill", display: "pKill", aliases: "particle kill", category: ToolCategory::Particle, is_generator: false }, | |
| 123 | ToolEntry { kind: "pMerge", display: "pMerge", aliases: "particle merge", category: ToolCategory::Particle, is_generator: false }, | |
| 124 | // 3D | |
| 125 | ToolEntry { kind: "Renderer3D", display: "Renderer 3D", aliases: "3rn render 3d", category: ToolCategory::Scene3D, is_generator: false }, | |
| 126 | ToolEntry { kind: "Merge3D", display: "Merge 3D", aliases: "3mg scene 3d", category: ToolCategory::Scene3D, is_generator: false }, | |
| 127 | ToolEntry { kind: "Camera3D", display: "Camera 3D", aliases: "3cm cam", category: ToolCategory::Scene3D, is_generator: true }, | |
| 128 | ToolEntry { kind: "Shape3D", display: "Shape 3D", aliases: "3sh sphere plane cube", category: ToolCategory::Scene3D, is_generator: true }, | |
| 129 | ToolEntry { kind: "ImagePlane3D", display: "Image Plane 3D", aliases: "3im card plane", category: ToolCategory::Scene3D, is_generator: false }, | |
| 130 | ToolEntry { kind: "Transform3D", display: "Transform 3D", aliases: "3xf move 3d", category: ToolCategory::Scene3D, is_generator: false }, | |
| 131 | ToolEntry { kind: "LightAmbient", display: "Ambient Light", aliases: "3la light", category: ToolCategory::Scene3D, is_generator: true }, | |
| 132 | ToolEntry { kind: "LightDirectional", display: "Directional Light", aliases: "3dl sun light", category: ToolCategory::Scene3D, is_generator: true }, | |
| 133 | // Utility | |
| 134 | ToolEntry { kind: "Saver", display: "Saver", aliases: "sv write export png", category: ToolCategory::Utility, is_generator: false }, | |
| 135 | ToolEntry { kind: "PipeRouter", display: "Pipe Router", aliases: "dot router wire", category: ToolCategory::Utility, is_generator: false }, | |
| 136 | ToolEntry { kind: "TimeStretcher", display: "Time Stretcher", aliases: "ts retime freeze", category: ToolCategory::Utility, is_generator: false }, | |
| 137 | ToolEntry { kind: "TimeSpeed", display: "Time Speed", aliases: "tsp speed delay", category: ToolCategory::Utility, is_generator: false }, | |
| 138 | ToolEntry { kind: "ChangeDepth", display: "Change Depth", aliases: "cd bit depth float", category: ToolCategory::Utility, is_generator: false }, | |
| 139 | ToolEntry { kind: "SetDomain", display: "Set Domain", aliases: "dod roi", category: ToolCategory::Utility, is_generator: false }, | |
| 140 | ToolEntry { kind: "Tracker", display: "Tracker", aliases: "tr matchmove", category: ToolCategory::Utility, is_generator: false }, | |
| 141 | ToolEntry { kind: "Note", display: "Sticky Note", aliases: "comment note", category: ToolCategory::Utility, is_generator: true }, | |
| 142 | ToolEntry { kind: "Underlay", display: "Underlay", aliases: "backdrop group", category: ToolCategory::Utility, is_generator: true }, | |
| 143 | ]; | |
| 144 | ||
| 145 | /// Case-insensitive fuzzy/subsequence scoring for tool search. | |
| 146 | /// Higher score is better; returns `None` if `query` does not match `candidate`. | |
| 147 | pub fn fuzzy_score(query: &str, entry: &ToolEntry) -> Option<i32> { | |
| 148 | let q = query.trim().to_ascii_lowercase(); | |
| 149 | if q.is_empty() { | |
| 150 | return Some(0); | |
| 151 | } | |
| 152 | let kind_l = entry.kind.to_ascii_lowercase(); | |
| 153 | let disp_l = entry.display.to_ascii_lowercase(); | |
| 154 | let alias_l = entry.aliases.to_ascii_lowercase(); | |
| 155 | let cat_l = entry.category.label().to_ascii_lowercase(); | |
| 156 | ||
| 157 | let mut best: Option<i32> = None; | |
| 158 | for (field, bonus) in [(&kind_l, 40), (&disp_l, 35), (&alias_l, 15), (&cat_l, 0)] { | |
| 159 | if let Some(s) = score_field(&q, field) { | |
| 160 | let total = s + bonus; | |
| 161 | best = Some(best.map_or(total, |prev| prev.max(total))); | |
| 162 | } | |
| 163 | } | |
| 164 | best | |
| 165 | } | |
| 166 | ||
| 167 | fn score_field(query: &str, target: &str) -> Option<i32> { | |
| 168 | if target == query { | |
| 169 | return Some(1000); | |
| 170 | } | |
| 171 | if target.starts_with(query) { | |
| 172 | return Some(850 - (target.len() as i32 - query.len() as i32)); | |
| 173 | } | |
| 174 | if let Some(idx) = target.find(query) { | |
| 175 | let boundary_bonus = if idx == 0 || matches!(target.as_bytes()[idx - 1], b' ' | b'.' | b'/' | b'_') { | |
| 176 | 120 | |
| 177 | } else { | |
| 178 | 0 | |
| 179 | }; | |
| 180 | return Some(620 + boundary_bonus - idx as i32 * 4); | |
| 181 | } | |
| 182 | ||
| 183 | // Subsequence match with consecutive-run & word-boundary bonuses | |
| 184 | let q_bytes = query.as_bytes(); | |
| 185 | let t_bytes = target.as_bytes(); | |
| 186 | let mut qi = 0usize; | |
| 187 | let mut score = 200i32; | |
| 188 | let mut prev_match: Option<usize> = None; | |
| 189 | ||
| 190 | for (ti, &tb) in t_bytes.iter().enumerate() { | |
| 191 | if qi < q_bytes.len() && tb == q_bytes[qi] { | |
| 192 | if prev_match.is_some_and(|p| p + 1 == ti) { | |
| 193 | score += 28; | |
| 194 | } else if let Some(p) = prev_match { | |
| 195 | score -= ((ti - p) as i32).min(20); | |
| 196 | } | |
| 197 | if ti == 0 || matches!(t_bytes[ti - 1], b' ' | b'.' | b'/' | b'_') { | |
| 198 | score += 22; | |
| 199 | } | |
| 200 | prev_match = Some(ti); | |
| 201 | qi += 1; | |
| 202 | } | |
| 203 | } | |
| 204 | ||
| 205 | if qi == q_bytes.len() { | |
| 206 | Some(score - (t_bytes.len() as i32)) | |
| 207 | } else { | |
| 208 | None | |
| 209 | } | |
| 210 | } | |
| 211 | ||
| 212 | /// Returns matching tool entries ranked from best to worst. | |
| 213 | pub fn search_tools(query: &str) -> Vec<&'static ToolEntry> { | |
| 214 | let mut scored: Vec<(i32, usize, &'static ToolEntry)> = TOOL_CATALOG | |
| 215 | .iter() | |
| 216 | .enumerate() | |
| 217 | .filter_map(|(idx, entry)| fuzzy_score(query, entry).map(|s| (s, idx, entry))) | |
| 218 | .collect(); | |
| 219 | scored.sort_by(|a, b| b.0.cmp(&a.0).then_with(|| a.1.cmp(&b.1))); | |
| 220 | scored.into_iter().map(|(_, _, e)| e).collect() | |
| 221 | } | |
| 222 | ||
| 223 | pub struct AddToolLauncher { | |
| 224 | pub open: bool, | |
| 225 | pub query: String, | |
| 226 | pub selected_idx: usize, | |
| 227 | needs_focus: bool, | |
| 228 | } | |
| 229 | ||
| 230 | impl Default for AddToolLauncher { | |
| 231 | fn default() -> Self { | |
| 232 | Self::new() | |
| 233 | } | |
| 234 | } | |
| 235 | ||
| 236 | impl AddToolLauncher { | |
| 237 | pub fn new() -> Self { | |
| 238 | Self { | |
| 239 | open: false, | |
| 240 | query: String::new(), | |
| 241 | selected_idx: 0, | |
| 242 | needs_focus: false, | |
| 243 | } | |
| 244 | } | |
| 245 | ||
| 246 | pub fn open(&mut self) { | |
| 247 | self.open = true; | |
| 248 | self.query.clear(); | |
| 249 | self.selected_idx = 0; | |
| 250 | self.needs_focus = true; | |
| 251 | } | |
| 252 | ||
| 253 | pub fn close(&mut self) { | |
| 254 | self.open = false; | |
| 255 | self.needs_focus = false; | |
| 256 | } | |
| 257 | ||
| 258 | /// Builds the floating modal overlay when open. Returns `Some(ToolEntry)` if a tool was chosen. | |
| 259 | pub fn build_ui(&mut self, ui: &mut ui::Ui, window_size: [f32; 2]) -> Option<ToolEntry> { | |
| 260 | if !self.open { | |
| 261 | return None; | |
| 262 | } | |
| 263 | ||
| 264 | let theme = ui.theme.clone(); | |
| 265 | let results = search_tools(&self.query); | |
| 266 | if self.selected_idx >= results.len() { | |
| 267 | self.selected_idx = 0; | |
| 268 | } | |
| 269 | ||
| 270 | let modal_id = ui::Id::ROOT.child("add-tool-modal"); | |
| 271 | let input_id = modal_id.child("query"); | |
| 272 | ||
| 273 | // Handle Up / Down / Enter / Escape from the search field before rendering rows | |
| 274 | let mut picked: Option<ToolEntry> = None; | |
| 275 | let mut should_close = false; | |
| 276 | ||
| 277 | let scrim_id = ui::Id::ROOT.child("add-tool-scrim"); | |
| 278 | let scrim_sig = ui.signal(scrim_id); | |
| 279 | if scrim_sig.clicked { | |
| 280 | should_close = true; | |
| 281 | } | |
| 282 | ||
| 283 | // Full-window barrier scrim so clicks behind modal don't leak | |
| 284 | ui.open_as( | |
| 285 | scrim_id, | |
| 286 | ui::Spec { | |
| 287 | flags: ui::Flags::FLOAT | ui::Flags::BARRIER | ui::Flags::CLICKABLE, | |
| 288 | position: [0.0, 0.0], | |
| 289 | size: [ui::px(window_size[0]), ui::px(window_size[1])], | |
| 290 | fill: Some([0.0, 0.0, 0.0, 0.42]), | |
| 291 | ..ui::Spec::default() | |
| 292 | }, | |
| 293 | ); | |
| 294 | ui.close(); | |
| 295 | ||
| 296 | let modal_w = 460.0_f32.min((window_size[0] - 40.0).max(260.0)); | |
| 297 | let modal_h = 380.0_f32.min((window_size[1] - 80.0).max(220.0)); | |
| 298 | let modal_x = ((window_size[0] - modal_w) * 0.5).max(16.0); | |
| 299 | let modal_y = (window_size[1] * 0.14).max(24.0); | |
| 300 | ||
| 301 | ui.open_as( | |
| 302 | modal_id, | |
| 303 | ui::Spec { | |
| 304 | flags: ui::Flags::FLOAT | ui::Flags::BARRIER | ui::Flags::CLICKABLE | ui::Flags::CLIP, | |
| 305 | position: [modal_x, modal_y], | |
| 306 | size: [ui::px(modal_w), ui::px(modal_h)], | |
| 307 | axis: ui::Axis::Y, | |
| 308 | fill: Some(theme.popup), | |
| 309 | border: Some(theme.accent), | |
| 310 | shadow: Some([0.0, 0.0, 0.0, 0.45]), | |
| 311 | radius: 10.0, | |
| 312 | pad: [10.0, 10.0], | |
| 313 | gap: 8.0, | |
| 314 | ..ui::Spec::default() | |
| 315 | }, | |
| 316 | ); | |
| 317 | ||
| 318 | // Header | |
| 319 | ui.open( | |
| 320 | "hdr", | |
| 321 | ui::Spec { | |
| 322 | size: [ui::fill(), ui::px(20.0)], | |
| 323 | axis: ui::Axis::X, | |
| 324 | gap: 6.0, | |
| 325 | ..ui::Spec::default() | |
| 326 | }, | |
| 327 | ); | |
| 328 | ui.leaf( | |
| 329 | "title", | |
| 330 | ui::Spec { | |
| 331 | size: [ui::fill(), ui::fill()], | |
| 332 | text: Some("Add Tool"), | |
| 333 | font_size: Some(12.5), | |
| 334 | medium: true, | |
| 335 | color: Some(theme.text), | |
| 336 | ..ui::Spec::default() | |
| 337 | }, | |
| 338 | ); | |
| 339 | ui.leaf( | |
| 340 | "hint", | |
| 341 | ui::Spec { | |
| 342 | size: [ui::fit(), ui::fill()], | |
| 343 | text: Some("↑↓ select · Enter add · Esc close"), | |
| 344 | font_size: Some(10.0), | |
| 345 | color: Some(theme.text_dim), | |
| 346 | ..ui::Spec::default() | |
| 347 | }, | |
| 348 | ); | |
| 349 | ui.close(); | |
| 350 | ||
| 351 | // Search input | |
| 352 | let prev_query = self.query.clone(); | |
| 353 | let input_sig = ui::text_field( | |
| 354 | ui, | |
| 355 | input_id, | |
| 356 | &mut self.query, | |
| 357 | "Type tool name (e.g. Merge, Blur, BC, xf, Text+)…", | |
| 358 | ui::Spec { | |
| 359 | size: [ui::fill(), ui::px(30.0)], | |
| 360 | font_size: Some(13.0), | |
| 361 | fill: Some(theme.base), | |
| 362 | border: Some(theme.accent), | |
| 363 | radius: 6.0, | |
| 364 | pad: [10.0, 4.0], | |
| 365 | ..ui::Spec::default() | |
| 366 | }, | |
| 367 | ); | |
| 368 | if self.needs_focus { | |
| 369 | ui.set_focus(Some(input_id)); | |
| 370 | self.needs_focus = false; | |
| 371 | } | |
| 372 | if self.query != prev_query { | |
| 373 | self.selected_idx = 0; | |
| 374 | } | |
| 375 | ||
| 376 | for ev in &input_sig.events { | |
| 377 | if let ui::Event::Key { key, .. } = ev { | |
| 378 | match key { | |
| 379 | Key::Named(NamedKey::Escape) => should_close = true, | |
| 380 | Key::Named(NamedKey::ArrowDown) => { | |
| 381 | if !results.is_empty() { | |
| 382 | self.selected_idx = (self.selected_idx + 1) % results.len(); | |
| 383 | } | |
| 384 | } | |
| 385 | Key::Named(NamedKey::ArrowUp) => { | |
| 386 | if !results.is_empty() { | |
| 387 | self.selected_idx = (self.selected_idx + results.len() - 1) % results.len(); | |
| 388 | } | |
| 389 | } | |
| 390 | Key::Named(NamedKey::Enter) => { | |
| 391 | if let Some(&entry) = results.get(self.selected_idx) { | |
| 392 | picked = Some(*entry); | |
| 393 | should_close = true; | |
| 394 | } | |
| 395 | } | |
| 396 | _ => {} | |
| 397 | } | |
| 398 | } | |
| 399 | } | |
| 400 | ||
| 401 | // Scrollable results list | |
| 402 | ui.open( | |
| 403 | "results", | |
| 404 | ui::Spec { | |
| 405 | flags: ui::Flags::SCROLL | ui::Flags::CLIP, | |
| 406 | size: [ui::fill(), ui::fill()], | |
| 407 | axis: ui::Axis::Y, | |
| 408 | gap: 2.0, | |
| 409 | ..ui::Spec::default() | |
| 410 | }, | |
| 411 | ); | |
| 412 | if results.is_empty() { | |
| 413 | ui.leaf( | |
| 414 | "no-match", | |
| 415 | ui::Spec { | |
| 416 | size: [ui::fill(), ui::px(28.0)], | |
| 417 | text: Some("No matching tools"), | |
| 418 | font_size: Some(11.5), | |
| 419 | color: Some(theme.text_dim), | |
| 420 | center: true, | |
| 421 | ..ui::Spec::default() | |
| 422 | }, | |
| 423 | ); | |
| 424 | } else { | |
| 425 | for (idx, entry) in results.iter().enumerate() { | |
| 426 | let is_sel = idx == self.selected_idx; | |
| 427 | let cat_col = entry.category.color(); | |
| 428 | let row_id = ui.open( | |
| 429 | ("row", entry.kind), | |
| 430 | ui::Spec { | |
| 431 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 432 | flags: ui::Flags::CLICKABLE, | |
| 433 | size: [ui::fill(), ui::px(26.0)], | |
| 434 | axis: ui::Axis::X, | |
| 435 | pad: [8.0, 2.0], | |
| 436 | gap: 8.0, | |
| 437 | fill: if is_sel { | |
| 438 | Some([theme.accent[0], theme.accent[1], theme.accent[2], 0.22]) | |
| 439 | } else { | |
| 440 | None | |
| 441 | }, | |
| 442 | hover_fill: Some([theme.accent[0], theme.accent[1], theme.accent[2], 0.14]), | |
| 443 | radius: 5.0, | |
| 444 | ..ui::Spec::default() | |
| 445 | }, | |
| 446 | ); | |
| 447 | let row_sig = ui.signal(row_id); | |
| 448 | if row_sig.clicked { | |
| 449 | picked = Some(**entry); | |
| 450 | should_close = true; | |
| 451 | } | |
| 452 | ||
| 453 | // Category color pip | |
| 454 | ui.leaf( | |
| 455 | "pip", | |
| 456 | ui::Spec { | |
| 457 | size: [ui::px(6.0), ui::px(14.0)], | |
| 458 | fill: Some(cat_col), | |
| 459 | radius: 2.0, | |
| 460 | ..ui::Spec::default() | |
| 461 | }, | |
| 462 | ); | |
| 463 | ui.leaf( | |
| 464 | "name", | |
| 465 | ui::Spec { | |
| 466 | size: [ui::fill(), ui::fill()], | |
| 467 | text: Some(entry.display), | |
| 468 | font_size: Some(12.0), | |
| 469 | medium: is_sel, | |
| 470 | color: Some(theme.text), | |
| 471 | ..ui::Spec::default() | |
| 472 | }, | |
| 473 | ); | |
| 474 | ui.leaf( | |
| 475 | "kind", | |
| 476 | ui::Spec { | |
| 477 | size: [ui::fit(), ui::fill()], | |
| 478 | text: Some(entry.kind), | |
| 479 | font_size: Some(10.0), | |
| 480 | color: Some(theme.text_dim), | |
| 481 | ..ui::Spec::default() | |
| 482 | }, | |
| 483 | ); | |
| 484 | ui.leaf( | |
| 485 | "cat", | |
| 486 | ui::Spec { | |
| 487 | size: [ui::fit(), ui::px(18.0)], | |
| 488 | text: Some(entry.category.label()), | |
| 489 | font_size: Some(9.5), | |
| 490 | color: Some(cat_col), | |
| 491 | fill: Some([cat_col[0], cat_col[1], cat_col[2], 0.14]), | |
| 492 | radius: 3.0, | |
| 493 | pad: [5.0, 0.0], | |
| 494 | center: true, | |
| 495 | ..ui::Spec::default() | |
| 496 | }, | |
| 497 | ); | |
| 498 | ui.close(); | |
| 499 | } | |
| 500 | } | |
| 501 | ui.close(); // results | |
| 502 | ui.close(); // modal_id | |
| 503 | ||
| 504 | if should_close { | |
| 505 | self.close(); | |
| 506 | ui.set_focus(None); | |
| 507 | } | |
| 508 | picked | |
| 509 | } | |
| 510 | } | |
| 511 | ||
| 512 | #[cfg(test)] | |
| 513 | mod tests { | |
| 514 | use super::*; | |
| 515 | ||
| 516 | #[test] | |
| 517 | fn empty_query_returns_all_tools() { | |
| 518 | let all = search_tools(""); | |
| 519 | assert_eq!(all.len(), TOOL_CATALOG.len()); | |
| 520 | assert!(all.len() >= 50); | |
| 521 | } | |
| 522 | ||
| 523 | #[test] | |
| 524 | fn exact_and_prefix_rank_above_subsequence() { | |
| 525 | let res = search_tools("blur"); | |
| 526 | assert!(!res.is_empty()); | |
| 527 | assert_eq!(res[0].kind, "Blur"); | |
| 528 | ||
| 529 | let res_mrg = search_tools("merge"); | |
| 530 | assert_eq!(res_mrg[0].kind, "Merge"); | |
| 531 | } | |
| 532 | ||
| 533 | #[test] | |
| 534 | fn alias_and_subsequence_matching_works() { | |
| 535 | // Alias "bc" -> BrightnessContrast | |
| 536 | let res_bc = search_tools("bc"); | |
| 537 | assert!(res_bc.iter().take(3).any(|e| e.kind == "BrightnessContrast")); | |
| 538 | ||
| 539 | // Alias "xf" -> Transform | |
| 540 | let res_xf = search_tools("xf"); | |
| 541 | assert!(res_xf.iter().take(3).any(|e| e.kind == "Transform")); | |
| 542 | ||
| 543 | // Subsequence "tplus" -> TextPlus | |
| 544 | let res_tp = search_tools("tplus"); | |
| 545 | assert!(res_tp.iter().any(|e| e.kind == "TextPlus")); | |
| 546 | ||
| 547 | // Case-insensitive category match | |
| 548 | let res_3d = search_tools("camera"); | |
| 549 | assert!(res_3d.iter().any(|e| e.kind == "Camera3D")); | |
| 550 | } | |
| 551 | ||
| 552 | #[test] | |
| 553 | fn non_matching_query_returns_empty() { | |
| 554 | let res = search_tools("zzzznotatool999"); | |
| 555 | assert!(res.is_empty()); | |
| 556 | } | |
| 557 | } |
crates/studio/src/inspector.rs deleted-1802| ... | ... | @@ -1,1802 +0,0 @@ |
| 1 | //! Inspector dock pane — Fusion 21.1 parameter editor with scrub sliders, Vec2, RGBA color groups, | |
| 2 | //! booleans, FuID enum cyclers, text fields, expression badges, and keyframe diamonds (`â—†`). | |
| 3 | ||
| 4 | use engine::EvalStats; | |
| 5 | use model::{Comp, CompHistory, ExprEngine, NodeId, ParamValue, is_supported_tool}; | |
| 6 | use std::collections::{HashMap, HashSet}; | |
| 7 | use std::time::{Duration, Instant}; | |
| 8 | ||
| 9 | #[derive(Clone, Copy, Debug, PartialEq)] | |
| 10 | pub enum ParamKind { | |
| 11 | Float { min: f64, max: f64, default: f64, step: f64 }, | |
| 12 | Vec2 { min: f64, max: f64, default: [f64; 2] }, | |
| 13 | Bool { default: bool }, | |
| 14 | Enum { options: &'static [&'static str], default: &'static str }, | |
| 15 | Color { | |
| 16 | r: &'static str, | |
| 17 | g: &'static str, | |
| 18 | b: &'static str, | |
| 19 | a: Option<&'static str>, | |
| 20 | default: [f64; 4], | |
| 21 | }, | |
| 22 | Text { default: &'static str }, | |
| 23 | } | |
| 24 | ||
| 25 | #[derive(Clone, Copy, Debug)] | |
| 26 | pub struct ParamSpec { | |
| 27 | pub name: &'static str, | |
| 28 | pub label: &'static str, | |
| 29 | pub kind: ParamKind, | |
| 30 | } | |
| 31 | ||
| 32 | #[derive(Clone, Debug)] | |
| 33 | pub enum InspectorAction { | |
| 34 | /// A parameter or node property changed; bust subtree & re-render viewers. | |
| 35 | ParamEdited(NodeId), | |
| 36 | /// A keyframe was added or deleted; refresh spline editor + bust subtree. | |
| 37 | KeyframeEdited(NodeId), | |
| 38 | /// Node was renamed. | |
| 39 | NodeRenamed(NodeId), | |
| 40 | /// Status notice to show in window title. | |
| 41 | StatusNotice(String), | |
| 42 | } | |
| 43 | ||
| 44 | #[derive(Clone, Debug)] | |
| 45 | struct DragState { | |
| 46 | node_id: NodeId, | |
| 47 | key: String, | |
| 48 | start_x: f32, | |
| 49 | start_val: f64, | |
| 50 | min: f64, | |
| 51 | max: f64, | |
| 52 | width: f32, | |
| 53 | } | |
| 54 | ||
| 55 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
| 56 | pub enum KeyframeState { | |
| 57 | Static, | |
| 58 | AnimatedOtherFrame, | |
| 59 | KeyframeAtCurrent, | |
| 60 | } | |
| 61 | ||
| 62 | pub struct InspectorState { | |
| 63 | pub bg: [f32; 4], | |
| 64 | pub panel_bg: [f32; 4], | |
| 65 | pub rule: [f32; 4], | |
| 66 | pub text: [f32; 4], | |
| 67 | pub text_dim: [f32; 4], | |
| 68 | pub accent: [f32; 4], | |
| 69 | expr: ExprEngine, | |
| 70 | /// Node name edit buffer synced with the currently selected node. | |
| 71 | name_node: Option<NodeId>, | |
| 72 | name_buf: String, | |
| 73 | /// Text parameter edit buffers keyed by `(node_id, param_name)`. | |
| 74 | text_bufs: HashMap<(usize, String), String>, | |
| 75 | /// Active scrub slider drag. | |
| 76 | drag: Option<DragState>, | |
| 77 | /// Last click timestamp per slider key for double-click reset. | |
| 78 | last_click: HashMap<String, Instant>, | |
| 79 | } | |
| 80 | ||
| 81 | impl InspectorState { | |
| 82 | pub fn new(theme: &ui::Theme) -> Self { | |
| 83 | let b = theme.base; | |
| 84 | let lift = |c: [f32; 4], d: f32| { | |
| 85 | [ | |
| 86 | (c[0] + d).clamp(0.0, 1.0), | |
| 87 | (c[1] + d).clamp(0.0, 1.0), | |
| 88 | (c[2] + d).clamp(0.0, 1.0), | |
| 89 | 1.0, | |
| 90 | ] | |
| 91 | }; | |
| 92 | Self { | |
| 93 | bg: b, | |
| 94 | panel_bg: lift(b, 0.020), | |
| 95 | rule: theme.rule, | |
| 96 | text: theme.text, | |
| 97 | text_dim: theme.text_dim, | |
| 98 | accent: theme.accent, | |
| 99 | expr: ExprEngine::new(), | |
| 100 | name_node: None, | |
| 101 | name_buf: String::new(), | |
| 102 | text_bufs: HashMap::new(), | |
| 103 | drag: None, | |
| 104 | last_click: HashMap::new(), | |
| 105 | } | |
| 106 | } | |
| 107 | ||
| 108 | pub fn background(&self) -> [f32; 4] { | |
| 109 | self.bg | |
| 110 | } | |
| 111 | ||
| 112 | pub fn reset(&mut self) { | |
| 113 | self.name_node = None; | |
| 114 | self.name_buf.clear(); | |
| 115 | self.text_bufs.clear(); | |
| 116 | self.drag = None; | |
| 117 | } | |
| 118 | ||
| 119 | pub fn build_ui( | |
| 120 | &mut self, | |
| 121 | ui: &mut ui::Ui, | |
| 122 | pane_id: ui::Id, | |
| 123 | comp: &mut Comp, | |
| 124 | history: &mut CompHistory, | |
| 125 | selected: Option<NodeId>, | |
| 126 | current_frame: f64, | |
| 127 | stats: &EvalStats, | |
| 128 | ) -> Vec<InspectorAction> { | |
| 129 | let mut actions = Vec::new(); | |
| 130 | let bg = self.bg; | |
| 131 | let panel_bg = self.panel_bg; | |
| 132 | let rule = self.rule; | |
| 133 | let text_col = self.text; | |
| 134 | let text_dim = self.text_dim; | |
| 135 | let accent = self.accent; | |
| 136 | ||
| 137 | ui.open_as( | |
| 138 | pane_id, | |
| 139 | ui::Spec { | |
| 140 | size: [ui::fill(), ui::fill()], | |
| 141 | axis: ui::Axis::Y, | |
| 142 | fill: Some(bg), | |
| 143 | ..ui::Spec::default() | |
| 144 | }, | |
| 145 | ); | |
| 146 | ||
| 147 | let Some(node_id) = selected.filter(|id| id.0 < comp.nodes.len()) else { | |
| 148 | // Empty state | |
| 149 | ui.open( | |
| 150 | "insp-empty", | |
| 151 | ui::Spec { | |
| 152 | size: [ui::fill(), ui::fill()], | |
| 153 | axis: ui::Axis::Y, | |
| 154 | pad: [14.0, 18.0], | |
| 155 | gap: 8.0, | |
| 156 | ..ui::Spec::default() | |
| 157 | }, | |
| 158 | ); | |
| 159 | ui.leaf( | |
| 160 | "empty-title", | |
| 161 | ui::Spec { | |
| 162 | size: [ui::fill(), ui::px(20.0)], | |
| 163 | text: Some("No Node Selected"), | |
| 164 | font_size: Some(12.5), | |
| 165 | medium: true, | |
| 166 | color: Some(text_col), | |
| 167 | ..ui::Spec::default() | |
| 168 | }, | |
| 169 | ); | |
| 170 | ui.leaf( | |
| 171 | "empty-hint", | |
| 172 | ui::Spec { | |
| 173 | size: [ui::fill(), ui::fit()], | |
| 174 | text: Some("Select a node in the Flow graph to inspect and keyframe its parameters, or press Shift+Space to add a tool."), | |
| 175 | font_size: Some(11.0), | |
| 176 | overflow: ui::Overflow::Wrap, | |
| 177 | color: Some(text_dim), | |
| 178 | ..ui::Spec::default() | |
| 179 | }, | |
| 180 | ); | |
| 181 | self.render_stats_footer(ui, stats, panel_bg, rule, text_dim); | |
| 182 | ui.close(); | |
| 183 | ui.close(); | |
| 184 | return actions; | |
| 185 | }; | |
| 186 | ||
| 187 | // Sync editable name buffer when selection changes or node was renamed externally (e.g. undo) | |
| 188 | let current_name = comp.node(node_id).name.clone(); | |
| 189 | let name_field_id = pane_id.child(("node-name", node_id.0)); | |
| 190 | if self.name_node != Some(node_id) | |
| 191 | || (ui.focused() != Some(name_field_id) && self.name_buf != current_name) | |
| 192 | { | |
| 193 | self.name_node = Some(node_id); | |
| 194 | self.name_buf = current_name.clone(); | |
| 195 | } | |
| 196 | ||
| 197 | // 1. Header: Node name + Class badge + Bypass toggle | |
| 198 | ui.open( | |
| 199 | "insp-header", | |
| 200 | ui::Spec { | |
| 201 | size: [ui::fill(), ui::children()], | |
| 202 | axis: ui::Axis::Y, | |
| 203 | pad: [10.0, 8.0], | |
| 204 | gap: 6.0, | |
| 205 | fill: Some(panel_bg), | |
| 206 | border: Some(rule), | |
| 207 | ..ui::Spec::default() | |
| 208 | }, | |
| 209 | ); | |
| 210 | ||
| 211 | // Row 1: Editable name + Bypass button | |
| 212 | ui.open( | |
| 213 | "insp-name-row", | |
| 214 | ui::Spec { | |
| 215 | size: [ui::fill(), ui::px(26.0)], | |
| 216 | axis: ui::Axis::X, | |
| 217 | gap: 6.0, | |
| 218 | ..ui::Spec::default() | |
| 219 | }, | |
| 220 | ); | |
| 221 | let name_sig = ui::text_field( | |
| 222 | ui, | |
| 223 | name_field_id, | |
| 224 | &mut self.name_buf, | |
| 225 | "Node name", | |
| 226 | ui::Spec { | |
| 227 | size: [ui::fill(), ui::px(24.0)], | |
| 228 | font_size: Some(12.0), | |
| 229 | medium: true, | |
| 230 | fill: Some(bg), | |
| 231 | border: Some(rule), | |
| 232 | hover_border: Some(accent), | |
| 233 | radius: 4.0, | |
| 234 | pad: [6.0, 2.0], | |
| 235 | ..ui::Spec::default() | |
| 236 | }, | |
| 237 | ); | |
| 238 | let enter_pressed = name_sig.events.iter().any(|ev| { | |
| 239 | matches!( | |
| 240 | ev, | |
| 241 | ui::Event::Key { key: winit::keyboard::Key::Named(winit::keyboard::NamedKey::Enter), .. } | |
| 242 | ) | |
| 243 | }); | |
| 244 | if enter_pressed && self.name_buf.trim() != current_name { | |
| 245 | let desired = self.name_buf.trim().to_owned(); | |
| 246 | history.push_snapshot(comp, "Rename Node"); | |
| 247 | match comp.rename_node(node_id, &desired) { | |
| 248 | Ok(applied) => { | |
| 249 | self.name_buf = applied; | |
| 250 | actions.push(InspectorAction::NodeRenamed(node_id)); | |
| 251 | } | |
| 252 | Err(e) => { | |
| 253 | self.name_buf = current_name.clone(); | |
| 254 | actions.push(InspectorAction::StatusNotice(e)); | |
| 255 | } | |
| 256 | } | |
| 257 | ui.set_focus(None); | |
| 258 | } | |
| 259 | ||
| 260 | let bypassed = comp.node(node_id).bypassed; | |
| 261 | let pass_lbl = if bypassed { "Bypassed" } else { "Active" }; | |
| 262 | let pass_fill = if bypassed { | |
| 263 | [0.85, 0.28, 0.28, 0.28] | |
| 264 | } else { | |
| 265 | [0.24, 0.78, 0.48, 0.18] | |
| 266 | }; | |
| 267 | let pass_border = if bypassed { | |
| 268 | [0.92, 0.36, 0.36, 0.75] | |
| 269 | } else { | |
| 270 | [0.28, 0.82, 0.52, 0.55] | |
| 271 | }; | |
| 272 | let pass_sig = ui.leaf( | |
| 273 | ("insp-pass", node_id.0), | |
| 274 | ui::Spec { | |
| 275 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 276 | flags: ui::Flags::CLICKABLE, | |
| 277 | size: [ui::fit(), ui::px(24.0)], | |
| 278 | text: Some(pass_lbl), | |
| 279 | font_size: Some(10.5), | |
| 280 | medium: true, | |
| 281 | color: Some(text_col), | |
| 282 | fill: Some(pass_fill), | |
| 283 | border: Some(pass_border), | |
| 284 | radius: 4.0, | |
| 285 | pad: [8.0, 0.0], | |
| 286 | center: true, | |
| 287 | ..ui::Spec::default() | |
| 288 | }, | |
| 289 | ); | |
| 290 | if pass_sig.clicked { | |
| 291 | history.push_snapshot(comp, "Toggle Passthrough"); | |
| 292 | comp.toggle_passthrough(node_id); | |
| 293 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 294 | } | |
| 295 | ui.close(); // insp-name-row | |
| 296 | ||
| 297 | // Row 2: Tool class badge + supported/preserved indicator + frame badge | |
| 298 | let node_kind = comp.node(node_id).kind.clone(); | |
| 299 | let supported = is_supported_tool(&node_kind); | |
| 300 | let frame_str = format!("f{:.0}", current_frame); | |
| 301 | ui.open( | |
| 302 | "insp-meta-row", | |
| 303 | ui::Spec { | |
| 304 | size: [ui::fill(), ui::px(18.0)], | |
| 305 | axis: ui::Axis::X, | |
| 306 | gap: 6.0, | |
| 307 | ..ui::Spec::default() | |
| 308 | }, | |
| 309 | ); | |
| 310 | ui.leaf( | |
| 311 | "insp-kind-badge", | |
| 312 | ui::Spec { | |
| 313 | size: [ui::fit(), ui::px(18.0)], | |
| 314 | text: Some(&node_kind), | |
| 315 | font_size: Some(10.0), | |
| 316 | medium: true, | |
| 317 | color: Some(accent), | |
| 318 | fill: Some([accent[0], accent[1], accent[2], 0.14]), | |
| 319 | border: Some([accent[0], accent[1], accent[2], 0.40]), | |
| 320 | radius: 3.0, | |
| 321 | pad: [6.0, 0.0], | |
| 322 | center: true, | |
| 323 | ..ui::Spec::default() | |
| 324 | }, | |
| 325 | ); | |
| 326 | if !supported { | |
| 327 | ui.leaf( | |
| 328 | "insp-preserved-badge", | |
| 329 | ui::Spec { | |
| 330 | size: [ui::fit(), ui::px(18.0)], | |
| 331 | text: Some("preserved"), | |
| 332 | font_size: Some(9.5), | |
| 333 | medium: true, | |
| 334 | color: Some([0.98, 0.78, 0.40, 1.0]), | |
| 335 | fill: Some([0.24, 0.16, 0.06, 0.85]), | |
| 336 | border: Some([0.92, 0.64, 0.24, 0.60]), | |
| 337 | radius: 3.0, | |
| 338 | pad: [5.0, 0.0], | |
| 339 | center: true, | |
| 340 | ..ui::Spec::default() | |
| 341 | }, | |
| 342 | ); | |
| 343 | } | |
| 344 | ui.leaf( | |
| 345 | "insp-spacer", | |
| 346 | ui::Spec { | |
| 347 | size: [ui::fill(), ui::px(18.0)], | |
| 348 | ..ui::Spec::default() | |
| 349 | }, | |
| 350 | ); | |
| 351 | ui.leaf( | |
| 352 | "insp-frame", | |
| 353 | ui::Spec { | |
| 354 | size: [ui::fit(), ui::px(18.0)], | |
| 355 | text: Some(&frame_str), | |
| 356 | font_size: Some(10.0), | |
| 357 | color: Some(text_dim), | |
| 358 | center: true, | |
| 359 | ..ui::Spec::default() | |
| 360 | }, | |
| 361 | ); | |
| 362 | ui.close(); // insp-meta-row | |
| 363 | ui.close(); // insp-header | |
| 364 | ||
| 365 | // 2. Scrollable Parameter List | |
| 366 | ui.open( | |
| 367 | "insp-scroll", | |
| 368 | ui::Spec { | |
| 369 | flags: ui::Flags::SCROLL | ui::Flags::CLIP, | |
| 370 | size: [ui::fill(), ui::fill()], | |
| 371 | axis: ui::Axis::Y, | |
| 372 | pad: [10.0, 8.0], | |
| 373 | gap: 6.0, | |
| 374 | ..ui::Spec::default() | |
| 375 | }, | |
| 376 | ); | |
| 377 | ||
| 378 | let schema = schema_for_kind(&node_kind); | |
| 379 | let mut covered: HashSet<&str> = HashSet::new(); | |
| 380 | ||
| 381 | for spec in schema { | |
| 382 | covered.insert(spec.name); | |
| 383 | if let ParamKind::Color { r, g, b, a, .. } = spec.kind { | |
| 384 | covered.insert(r); | |
| 385 | covered.insert(g); | |
| 386 | covered.insert(b); | |
| 387 | if let Some(alpha_k) = a { | |
| 388 | covered.insert(alpha_k); | |
| 389 | } | |
| 390 | } | |
| 391 | self.render_param_spec(ui, pane_id, comp, history, node_id, spec, current_frame, &mut actions); | |
| 392 | } | |
| 393 | ||
| 394 | // Fallback: list all remaining present non-image inputs on the node! | |
| 395 | let mut extra_keys: Vec<String> = comp | |
| 396 | .node(node_id) | |
| 397 | .inputs | |
| 398 | .iter() | |
| 399 | .filter_map(|(k, b)| { | |
| 400 | if covered.contains(k.as_str()) { | |
| 401 | return None; | |
| 402 | } | |
| 403 | // Skip image/node connections unless they also have a value or spline | |
| 404 | let is_node_conn = b | |
| 405 | .source_op | |
| 406 | .as_deref() | |
| 407 | .is_some_and(|op| comp.node_by_name.contains_key(op)); | |
| 408 | if is_node_conn && b.value.is_none() { | |
| 409 | return None; | |
| 410 | } | |
| 411 | if b.value.is_none() && b.expression.is_none() && b.source_op.is_none() { | |
| 412 | return None; | |
| 413 | } | |
| 414 | Some(k.clone()) | |
| 415 | }) | |
| 416 | .collect(); | |
| 417 | extra_keys.sort(); | |
| 418 | ||
| 419 | if !extra_keys.is_empty() { | |
| 420 | ui.leaf( | |
| 421 | "insp-extra-hdr", | |
| 422 | ui::Spec { | |
| 423 | size: [ui::fill(), ui::px(18.0)], | |
| 424 | text: Some("OTHER INPUTS"), | |
| 425 | font_size: Some(9.5), | |
| 426 | medium: true, | |
| 427 | color: Some(text_dim), | |
| 428 | ..ui::Spec::default() | |
| 429 | }, | |
| 430 | ); | |
| 431 | for key in extra_keys { | |
| 432 | self.render_fallback_input(ui, pane_id, comp, history, node_id, &key, current_frame, &mut actions); | |
| 433 | } | |
| 434 | } | |
| 435 | ||
| 436 | // Loader clip info if Loader node | |
| 437 | if node_kind == "Loader" { | |
| 438 | let clip_summary = comp | |
| 439 | .node(node_id) | |
| 440 | .clips | |
| 441 | .first() | |
| 442 | .map(|c| c.filename.clone()) | |
| 443 | .unwrap_or_else(|| "(no clip)".to_owned()); | |
| 444 | ui.open( | |
| 445 | "insp-loader-clip", | |
| 446 | ui::Spec { | |
| 447 | size: [ui::fill(), ui::children()], | |
| 448 | axis: ui::Axis::Y, | |
| 449 | gap: 3.0, | |
| 450 | pad: [6.0, 5.0], | |
| 451 | fill: Some(panel_bg), | |
| 452 | border: Some(rule), | |
| 453 | radius: 4.0, | |
| 454 | ..ui::Spec::default() | |
| 455 | }, | |
| 456 | ); | |
| 457 | ui.leaf( | |
| 458 | "clip-lbl", | |
| 459 | ui::Spec { | |
| 460 | size: [ui::fill(), ui::px(14.0)], | |
| 461 | text: Some("Clip Filename"), | |
| 462 | font_size: Some(10.0), | |
| 463 | color: Some(text_dim), | |
| 464 | ..ui::Spec::default() | |
| 465 | }, | |
| 466 | ); | |
| 467 | ui.leaf( | |
| 468 | "clip-path", | |
| 469 | ui::Spec { | |
| 470 | size: [ui::fill(), ui::fit()], | |
| 471 | text: Some(&clip_summary), | |
| 472 | font_size: Some(10.5), | |
| 473 | overflow: ui::Overflow::Wrap, | |
| 474 | color: Some(text_col), | |
| 475 | ..ui::Spec::default() | |
| 476 | }, | |
| 477 | ); | |
| 478 | ui.close(); | |
| 479 | } | |
| 480 | ||
| 481 | ui.close(); // insp-scroll | |
| 482 | ||
| 483 | // 3. Footer: EvalStats summary | |
| 484 | self.render_stats_footer(ui, stats, panel_bg, rule, text_dim); | |
| 485 | ||
| 486 | ui.close(); // pane_id | |
| 487 | actions | |
| 488 | } | |
| 489 | ||
| 490 | fn render_stats_footer( | |
| 491 | &self, | |
| 492 | ui: &mut ui::Ui, | |
| 493 | stats: &EvalStats, | |
| 494 | panel_bg: [f32; 4], | |
| 495 | rule: [f32; 4], | |
| 496 | text_dim: [f32; 4], | |
| 497 | ) { | |
| 498 | let line = format!( | |
| 499 | "Eval {:.1}ms · {} nodes · cache {}/{} · {:.0}MB", | |
| 500 | stats.eval_ms, | |
| 501 | stats.nodes_evaluated, | |
| 502 | stats.cache_hits, | |
| 503 | stats.cache_hits + stats.cache_misses, | |
| 504 | stats.cached_vram_mb, | |
| 505 | ); | |
| 506 | ui.open( | |
| 507 | "insp-footer", | |
| 508 | ui::Spec { | |
| 509 | size: [ui::fill(), ui::px(24.0)], | |
| 510 | axis: ui::Axis::X, | |
| 511 | pad: [10.0, 4.0], | |
| 512 | fill: Some(panel_bg), | |
| 513 | border: Some(rule), | |
| 514 | ..ui::Spec::default() | |
| 515 | }, | |
| 516 | ); | |
| 517 | ui.leaf( | |
| 518 | "insp-stats-txt", | |
| 519 | ui::Spec { | |
| 520 | size: [ui::fill(), ui::fill()], | |
| 521 | text: Some(&line), | |
| 522 | font_size: Some(10.0), | |
| 523 | color: Some(text_dim), | |
| 524 | ..ui::Spec::default() | |
| 525 | }, | |
| 526 | ); | |
| 527 | ui.close(); | |
| 528 | } | |
| 529 | ||
| 530 | #[allow(clippy::too_many_arguments)] | |
| 531 | fn render_param_spec( | |
| 532 | &mut self, | |
| 533 | ui: &mut ui::Ui, | |
| 534 | pane_id: ui::Id, | |
| 535 | comp: &mut Comp, | |
| 536 | history: &mut CompHistory, | |
| 537 | node_id: NodeId, | |
| 538 | spec: &ParamSpec, | |
| 539 | current_frame: f64, | |
| 540 | actions: &mut Vec<InspectorAction>, | |
| 541 | ) { | |
| 542 | match spec.kind { | |
| 543 | ParamKind::Float { min, max, default, step: _ } => { | |
| 544 | self.render_float_row( | |
| 545 | ui, | |
| 546 | pane_id, | |
| 547 | comp, | |
| 548 | history, | |
| 549 | node_id, | |
| 550 | spec.name, | |
| 551 | spec.label, | |
| 552 | min, | |
| 553 | max, | |
| 554 | default, | |
| 555 | current_frame, | |
| 556 | actions, | |
| 557 | ); | |
| 558 | } | |
| 559 | ParamKind::Vec2 { min, max, default } => { | |
| 560 | self.render_vec2_row( | |
| 561 | ui, | |
| 562 | pane_id, | |
| 563 | comp, | |
| 564 | history, | |
| 565 | node_id, | |
| 566 | spec.name, | |
| 567 | spec.label, | |
| 568 | min, | |
| 569 | max, | |
| 570 | default, | |
| 571 | current_frame, | |
| 572 | actions, | |
| 573 | ); | |
| 574 | } | |
| 575 | ParamKind::Bool { default } => { | |
| 576 | self.render_bool_row(ui, comp, history, node_id, spec.name, spec.label, default, actions); | |
| 577 | } | |
| 578 | ParamKind::Enum { options, default } => { | |
| 579 | self.render_enum_row(ui, comp, history, node_id, spec.name, spec.label, options, default, actions); | |
| 580 | } | |
| 581 | ParamKind::Color { r, g, b, a, default } => { | |
| 582 | self.render_color_group( | |
| 583 | ui, | |
| 584 | pane_id, | |
| 585 | comp, | |
| 586 | history, | |
| 587 | node_id, | |
| 588 | spec.label, | |
| 589 | r, | |
| 590 | g, | |
| 591 | b, | |
| 592 | a, | |
| 593 | default, | |
| 594 | current_frame, | |
| 595 | actions, | |
| 596 | ); | |
| 597 | } | |
| 598 | ParamKind::Text { default } => { | |
| 599 | self.render_text_row(ui, pane_id, comp, history, node_id, spec.name, spec.label, default, actions); | |
| 600 | } | |
| 601 | } | |
| 602 | } | |
| 603 | ||
| 604 | #[allow(clippy::too_many_arguments)] | |
| 605 | fn render_fallback_input( | |
| 606 | &mut self, | |
| 607 | ui: &mut ui::Ui, | |
| 608 | pane_id: ui::Id, | |
| 609 | comp: &mut Comp, | |
| 610 | history: &mut CompHistory, | |
| 611 | node_id: NodeId, | |
| 612 | key: &str, | |
| 613 | current_frame: f64, | |
| 614 | actions: &mut Vec<InspectorAction>, | |
| 615 | ) { | |
| 616 | let val = comp.node(node_id).inputs.get(key).and_then(|b| b.value.clone()); | |
| 617 | match val { | |
| 618 | Some(ParamValue::Point(pt)) => { | |
| 619 | self.render_vec2_row(ui, pane_id, comp, history, node_id, key, key, -1.0, 2.0, pt, current_frame, actions); | |
| 620 | } | |
| 621 | Some(ParamValue::Text(_)) => { | |
| 622 | self.render_text_row(ui, pane_id, comp, history, node_id, key, key, "", actions); | |
| 623 | } | |
| 624 | Some(ParamValue::FuId(cur)) => { | |
| 625 | let text_dim = self.text_dim; | |
| 626 | let text_col = self.text; | |
| 627 | let panel_bg = self.panel_bg; | |
| 628 | let rule = self.rule; | |
| 629 | ui.open( | |
| 630 | ("fb-fuid", node_id.0, key), | |
| 631 | ui::Spec { | |
| 632 | size: [ui::fill(), ui::px(22.0)], | |
| 633 | axis: ui::Axis::X, | |
| 634 | gap: 6.0, | |
| 635 | ..ui::Spec::default() | |
| 636 | }, | |
| 637 | ); | |
| 638 | ui.leaf( | |
| 639 | "lbl", | |
| 640 | ui::Spec { | |
| 641 | size: [ui::px(92.0), ui::fill()], | |
| 642 | text: Some(key), | |
| 643 | font_size: Some(10.5), | |
| 644 | overflow: ui::Overflow::Ellipsis, | |
| 645 | color: Some(text_dim), | |
| 646 | ..ui::Spec::default() | |
| 647 | }, | |
| 648 | ); | |
| 649 | ui.leaf( | |
| 650 | "val", | |
| 651 | ui::Spec { | |
| 652 | size: [ui::fill(), ui::fill()], | |
| 653 | text: Some(&cur), | |
| 654 | font_size: Some(10.5), | |
| 655 | color: Some(text_col), | |
| 656 | fill: Some(panel_bg), | |
| 657 | border: Some(rule), | |
| 658 | radius: 4.0, | |
| 659 | pad: [6.0, 0.0], | |
| 660 | ..ui::Spec::default() | |
| 661 | }, | |
| 662 | ); | |
| 663 | ui.close(); | |
| 664 | } | |
| 665 | Some(ParamValue::Number(v)) => { | |
| 666 | let (min, max) = infer_float_range(key, v); | |
| 667 | self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, v, current_frame, actions); | |
| 668 | } | |
| 669 | None => { | |
| 670 | // Animated spline or expression-only scalar input | |
| 671 | let cur = comp.eval_number(comp.node(node_id), key, current_frame, 0.0, &self.expr); | |
| 672 | let (min, max) = infer_float_range(key, cur); | |
| 673 | self.render_float_row(ui, pane_id, comp, history, node_id, key, key, min, max, 0.0, current_frame, actions); | |
| 674 | } | |
| 675 | _ => {} | |
| 676 | } | |
| 677 | } | |
| 678 | ||
| 679 | #[allow(clippy::too_many_arguments)] | |
| 680 | fn render_float_row( | |
| 681 | &mut self, | |
| 682 | ui: &mut ui::Ui, | |
| 683 | pane_id: ui::Id, | |
| 684 | comp: &mut Comp, | |
| 685 | history: &mut CompHistory, | |
| 686 | node_id: NodeId, | |
| 687 | input_name: &str, | |
| 688 | label: &str, | |
| 689 | min: f64, | |
| 690 | max: f64, | |
| 691 | default: f64, | |
| 692 | current_frame: f64, | |
| 693 | actions: &mut Vec<InspectorAction>, | |
| 694 | ) { | |
| 695 | let cur_val = comp.eval_number(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 696 | let has_expr = comp | |
| 697 | .node(node_id) | |
| 698 | .inputs | |
| 699 | .get(input_name) | |
| 700 | .and_then(|b| b.expression.as_ref()) | |
| 701 | .is_some(); | |
| 702 | let kf_state = keyframe_state(comp, node_id, input_name, current_frame); | |
| 703 | ||
| 704 | ui.open( | |
| 705 | ("row-f", node_id.0, input_name), | |
| 706 | ui::Spec { | |
| 707 | size: [ui::fill(), ui::px(22.0)], | |
| 708 | axis: ui::Axis::X, | |
| 709 | gap: 4.0, | |
| 710 | ..ui::Spec::default() | |
| 711 | }, | |
| 712 | ); | |
| 713 | ||
| 714 | // Label | |
| 715 | ui.leaf( | |
| 716 | "lbl", | |
| 717 | ui::Spec { | |
| 718 | size: [ui::px(86.0), ui::fill()], | |
| 719 | text: Some(label), | |
| 720 | font_size: Some(10.5), | |
| 721 | overflow: ui::Overflow::Ellipsis, | |
| 722 | color: Some(self.text_dim), | |
| 723 | ..ui::Spec::default() | |
| 724 | }, | |
| 725 | ); | |
| 726 | ||
| 727 | // Expression `=` badge if present | |
| 728 | if has_expr { | |
| 729 | ui.leaf( | |
| 730 | "expr-badge", | |
| 731 | ui::Spec { | |
| 732 | size: [ui::px(16.0), ui::px(18.0)], | |
| 733 | text: Some("="), | |
| 734 | font_size: Some(10.5), | |
| 735 | bold: true, | |
| 736 | color: Some([0.36, 0.88, 0.92, 1.0]), | |
| 737 | fill: Some([0.10, 0.24, 0.28, 0.85]), | |
| 738 | border: Some([0.32, 0.78, 0.86, 0.65]), | |
| 739 | radius: 3.0, | |
| 740 | center: true, | |
| 741 | ..ui::Spec::default() | |
| 742 | }, | |
| 743 | ); | |
| 744 | } | |
| 745 | ||
| 746 | // Scrub slider | |
| 747 | let slider_id = pane_id.child(("sld", node_id.0, input_name)); | |
| 748 | let drag_key = format!("{}:{input_name}", node_id.0); | |
| 749 | if let Some(new_val) = self.scrub_slider( | |
| 750 | ui, | |
| 751 | slider_id, | |
| 752 | &drag_key, | |
| 753 | node_id, | |
| 754 | cur_val, | |
| 755 | min, | |
| 756 | max, | |
| 757 | default, | |
| 758 | history, | |
| 759 | comp, | |
| 760 | input_name, | |
| 761 | ) { | |
| 762 | if kf_state != KeyframeState::Static { | |
| 763 | let _ = comp.set_keyframe(node_id, input_name, current_frame, new_val); | |
| 764 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 765 | } else { | |
| 766 | comp.set_input_value(node_id, input_name, ParamValue::Number(new_val)); | |
| 767 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 768 | } | |
| 769 | } | |
| 770 | ||
| 771 | // Reset button (small dot/arrow when non-default) | |
| 772 | let is_default = (cur_val - default).abs() < 1e-4; | |
| 773 | if !is_default { | |
| 774 | let rst = ui.leaf( | |
| 775 | ("rst", node_id.0, input_name), | |
| 776 | ui::Spec { | |
| 777 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 778 | flags: ui::Flags::CLICKABLE, | |
| 779 | size: [ui::px(18.0), ui::px(20.0)], | |
| 780 | text: Some("↺"), | |
| 781 | font_size: Some(10.5), | |
| 782 | color: Some(self.text_dim), | |
| 783 | hover_fill: Some(self.panel_bg), | |
| 784 | radius: 3.0, | |
| 785 | center: true, | |
| 786 | ..ui::Spec::default() | |
| 787 | }, | |
| 788 | ); | |
| 789 | if rst.clicked { | |
| 790 | history.push_snapshot(comp, format!("Reset {label}")); | |
| 791 | if kf_state != KeyframeState::Static { | |
| 792 | let _ = comp.set_keyframe(node_id, input_name, current_frame, default); | |
| 793 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 794 | } else { | |
| 795 | comp.set_input_value(node_id, input_name, ParamValue::Number(default)); | |
| 796 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 797 | } | |
| 798 | } | |
| 799 | } | |
| 800 | ||
| 801 | // Keyframe diamond button (`â—†`) | |
| 802 | if self.keyframe_button(ui, ("kf", node_id.0, input_name), kf_state) { | |
| 803 | self.toggle_keyframe(comp, history, node_id, input_name, current_frame, cur_val, kf_state, actions); | |
| 804 | } | |
| 805 | ||
| 806 | ui.close(); | |
| 807 | } | |
| 808 | ||
| 809 | #[allow(clippy::too_many_arguments)] | |
| 810 | fn render_vec2_row( | |
| 811 | &mut self, | |
| 812 | ui: &mut ui::Ui, | |
| 813 | pane_id: ui::Id, | |
| 814 | comp: &mut Comp, | |
| 815 | history: &mut CompHistory, | |
| 816 | node_id: NodeId, | |
| 817 | input_name: &str, | |
| 818 | label: &str, | |
| 819 | min: f64, | |
| 820 | max: f64, | |
| 821 | default: [f64; 2], | |
| 822 | current_frame: f64, | |
| 823 | actions: &mut Vec<InspectorAction>, | |
| 824 | ) { | |
| 825 | let cur = comp.eval_point(comp.node(node_id), input_name, current_frame, default, &self.expr); | |
| 826 | let has_expr = comp | |
| 827 | .node(node_id) | |
| 828 | .inputs | |
| 829 | .get(input_name) | |
| 830 | .and_then(|b| b.expression.as_ref()) | |
| 831 | .is_some(); | |
| 832 | ||
| 833 | ui.open( | |
| 834 | ("row-v2", node_id.0, input_name), | |
| 835 | ui::Spec { | |
| 836 | size: [ui::fill(), ui::px(22.0)], | |
| 837 | axis: ui::Axis::X, | |
| 838 | gap: 4.0, | |
| 839 | ..ui::Spec::default() | |
| 840 | }, | |
| 841 | ); | |
| 842 | ui.leaf( | |
| 843 | "lbl", | |
| 844 | ui::Spec { | |
| 845 | size: [ui::px(86.0), ui::fill()], | |
| 846 | text: Some(label), | |
| 847 | font_size: Some(10.5), | |
| 848 | overflow: ui::Overflow::Ellipsis, | |
| 849 | color: Some(self.text_dim), | |
| 850 | ..ui::Spec::default() | |
| 851 | }, | |
| 852 | ); | |
| 853 | if has_expr { | |
| 854 | ui.leaf( | |
| 855 | "expr-badge", | |
| 856 | ui::Spec { | |
| 857 | size: [ui::px(16.0), ui::px(18.0)], | |
| 858 | text: Some("="), | |
| 859 | font_size: Some(10.5), | |
| 860 | bold: true, | |
| 861 | color: Some([0.36, 0.88, 0.92, 1.0]), | |
| 862 | fill: Some([0.10, 0.24, 0.28, 0.85]), | |
| 863 | border: Some([0.32, 0.78, 0.86, 0.65]), | |
| 864 | radius: 3.0, | |
| 865 | center: true, | |
| 866 | ..ui::Spec::default() | |
| 867 | }, | |
| 868 | ); | |
| 869 | } | |
| 870 | ||
| 871 | let x_id = pane_id.child(("v2x", node_id.0, input_name)); | |
| 872 | let x_key = format!("{}:{input_name}:x", node_id.0); | |
| 873 | if let Some(nx) = self.scrub_slider( | |
| 874 | ui, | |
| 875 | x_id, | |
| 876 | &x_key, | |
| 877 | node_id, | |
| 878 | cur[0], | |
| 879 | min, | |
| 880 | max, | |
| 881 | default[0], | |
| 882 | history, | |
| 883 | comp, | |
| 884 | input_name, | |
| 885 | ) { | |
| 886 | comp.set_input_value(node_id, input_name, ParamValue::Point([nx, cur[1]])); | |
| 887 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 888 | } | |
| 889 | ||
| 890 | let y_id = pane_id.child(("v2y", node_id.0, input_name)); | |
| 891 | let y_key = format!("{}:{input_name}:y", node_id.0); | |
| 892 | if let Some(ny) = self.scrub_slider( | |
| 893 | ui, | |
| 894 | y_id, | |
| 895 | &y_key, | |
| 896 | node_id, | |
| 897 | cur[1], | |
| 898 | min, | |
| 899 | max, | |
| 900 | default[1], | |
| 901 | history, | |
| 902 | comp, | |
| 903 | input_name, | |
| 904 | ) { | |
| 905 | comp.set_input_value(node_id, input_name, ParamValue::Point([cur[0], ny])); | |
| 906 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 907 | } | |
| 908 | ||
| 909 | ui.close(); | |
| 910 | } | |
| 911 | ||
| 912 | #[allow(clippy::too_many_arguments)] | |
| 913 | fn render_bool_row( | |
| 914 | &mut self, | |
| 915 | ui: &mut ui::Ui, | |
| 916 | comp: &mut Comp, | |
| 917 | history: &mut CompHistory, | |
| 918 | node_id: NodeId, | |
| 919 | input_name: &str, | |
| 920 | label: &str, | |
| 921 | default: bool, | |
| 922 | actions: &mut Vec<InspectorAction>, | |
| 923 | ) { | |
| 924 | let checked = comp | |
| 925 | .node(node_id) | |
| 926 | .static_number(input_name) | |
| 927 | .map(|v| v >= 0.5) | |
| 928 | .unwrap_or(default); | |
| 929 | ||
| 930 | ui.open( | |
| 931 | ("row-b", node_id.0, input_name), | |
| 932 | ui::Spec { | |
| 933 | size: [ui::fill(), ui::px(22.0)], | |
| 934 | axis: ui::Axis::X, | |
| 935 | gap: 6.0, | |
| 936 | ..ui::Spec::default() | |
| 937 | }, | |
| 938 | ); | |
| 939 | ui.leaf( | |
| 940 | "lbl", | |
| 941 | ui::Spec { | |
| 942 | size: [ui::px(86.0), ui::fill()], | |
| 943 | text: Some(label), | |
| 944 | font_size: Some(10.5), | |
| 945 | overflow: ui::Overflow::Ellipsis, | |
| 946 | color: Some(self.text_dim), | |
| 947 | ..ui::Spec::default() | |
| 948 | }, | |
| 949 | ); | |
| 950 | let fill = if checked { | |
| 951 | [self.accent[0], self.accent[1], self.accent[2], 0.28] | |
| 952 | } else { | |
| 953 | self.panel_bg | |
| 954 | }; | |
| 955 | let border = if checked { self.accent } else { self.rule }; | |
| 956 | let sig = ui.leaf( | |
| 957 | ("chk", node_id.0, input_name), | |
| 958 | ui::Spec { | |
| 959 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 960 | flags: ui::Flags::CLICKABLE, | |
| 961 | size: [ui::px(54.0), ui::px(20.0)], | |
| 962 | text: Some(if checked { "✓ On" } else { "Off" }), | |
| 963 | font_size: Some(10.5), | |
| 964 | medium: checked, | |
| 965 | color: Some(if checked { self.text } else { self.text_dim }), | |
| 966 | fill: Some(fill), | |
| 967 | border: Some(border), | |
| 968 | radius: 4.0, | |
| 969 | center: true, | |
| 970 | ..ui::Spec::default() | |
| 971 | }, | |
| 972 | ); | |
| 973 | if sig.clicked { | |
| 974 | history.push_snapshot(comp, format!("Toggle {label}")); | |
| 975 | let next = if checked { 0.0 } else { 1.0 }; | |
| 976 | comp.set_input_value(node_id, input_name, ParamValue::Number(next)); | |
| 977 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 978 | } | |
| 979 | ui.close(); | |
| 980 | } | |
| 981 | ||
| 982 | #[allow(clippy::too_many_arguments)] | |
| 983 | fn render_enum_row( | |
| 984 | &mut self, | |
| 985 | ui: &mut ui::Ui, | |
| 986 | comp: &mut Comp, | |
| 987 | history: &mut CompHistory, | |
| 988 | node_id: NodeId, | |
| 989 | input_name: &str, | |
| 990 | label: &str, | |
| 991 | options: &'static [&'static str], | |
| 992 | default: &'static str, | |
| 993 | actions: &mut Vec<InspectorAction>, | |
| 994 | ) { | |
| 995 | let cur = comp | |
| 996 | .node(node_id) | |
| 997 | .static_str(input_name) | |
| 998 | .unwrap_or(default) | |
| 999 | .to_owned(); | |
| 1000 | ||
| 1001 | ui.open( | |
| 1002 | ("row-e", node_id.0, input_name), | |
| 1003 | ui::Spec { | |
| 1004 | size: [ui::fill(), ui::px(22.0)], | |
| 1005 | axis: ui::Axis::X, | |
| 1006 | gap: 6.0, | |
| 1007 | ..ui::Spec::default() | |
| 1008 | }, | |
| 1009 | ); | |
| 1010 | ui.leaf( | |
| 1011 | "lbl", | |
| 1012 | ui::Spec { | |
| 1013 | size: [ui::px(86.0), ui::fill()], | |
| 1014 | text: Some(label), | |
| 1015 | font_size: Some(10.5), | |
| 1016 | overflow: ui::Overflow::Ellipsis, | |
| 1017 | color: Some(self.text_dim), | |
| 1018 | ..ui::Spec::default() | |
| 1019 | }, | |
| 1020 | ); | |
| 1021 | let btn_lbl = format!("{cur} ▾"); | |
| 1022 | let sig = ui.leaf( | |
| 1023 | ("enum", node_id.0, input_name), | |
| 1024 | ui::Spec { | |
| 1025 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 1026 | flags: ui::Flags::CLICKABLE, | |
| 1027 | size: [ui::fill(), ui::px(20.0)], | |
| 1028 | text: Some(&btn_lbl), | |
| 1029 | font_size: Some(10.5), | |
| 1030 | color: Some(self.text), | |
| 1031 | fill: Some(self.panel_bg), | |
| 1032 | border: Some(self.rule), | |
| 1033 | hover_border: Some(self.accent), | |
| 1034 | radius: 4.0, | |
| 1035 | pad: [6.0, 0.0], | |
| 1036 | ..ui::Spec::default() | |
| 1037 | }, | |
| 1038 | ); | |
| 1039 | if sig.clicked && !options.is_empty() { | |
| 1040 | let idx = options.iter().position(|&o| o == cur).unwrap_or(0); | |
| 1041 | let next_idx = if ui.modifiers().shift_key() { | |
| 1042 | (idx + options.len() - 1) % options.len() | |
| 1043 | } else { | |
| 1044 | (idx + 1) % options.len() | |
| 1045 | }; | |
| 1046 | history.push_snapshot(comp, format!("Set {label}")); | |
| 1047 | comp.set_input_value(node_id, input_name, ParamValue::FuId(options[next_idx].to_owned())); | |
| 1048 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 1049 | } | |
| 1050 | ui.close(); | |
| 1051 | } | |
| 1052 | ||
| 1053 | #[allow(clippy::too_many_arguments)] | |
| 1054 | fn render_color_group( | |
| 1055 | &mut self, | |
| 1056 | ui: &mut ui::Ui, | |
| 1057 | pane_id: ui::Id, | |
| 1058 | comp: &mut Comp, | |
| 1059 | history: &mut CompHistory, | |
| 1060 | node_id: NodeId, | |
| 1061 | label: &str, | |
| 1062 | r_key: &'static str, | |
| 1063 | g_key: &'static str, | |
| 1064 | b_key: &'static str, | |
| 1065 | a_key: Option<&'static str>, | |
| 1066 | default: [f64; 4], | |
| 1067 | current_frame: f64, | |
| 1068 | actions: &mut Vec<InspectorAction>, | |
| 1069 | ) { | |
| 1070 | let rv = comp.eval_number(comp.node(node_id), r_key, current_frame, default[0], &self.expr); | |
| 1071 | let gv = comp.eval_number(comp.node(node_id), g_key, current_frame, default[1], &self.expr); | |
| 1072 | let bv = comp.eval_number(comp.node(node_id), b_key, current_frame, default[2], &self.expr); | |
| 1073 | let _av = a_key | |
| 1074 | .map(|ak| comp.eval_number(comp.node(node_id), ak, current_frame, default[3], &self.expr)) | |
| 1075 | .unwrap_or(default[3]); | |
| 1076 | let swatch = [ | |
| 1077 | rv.clamp(0.0, 1.0) as f32, | |
| 1078 | gv.clamp(0.0, 1.0) as f32, | |
| 1079 | bv.clamp(0.0, 1.0) as f32, | |
| 1080 | 1.0, | |
| 1081 | ]; | |
| 1082 | ||
| 1083 | ui.open( | |
| 1084 | ("grp-col", node_id.0, label), | |
| 1085 | ui::Spec { | |
| 1086 | size: [ui::fill(), ui::children()], | |
| 1087 | axis: ui::Axis::Y, | |
| 1088 | gap: 4.0, | |
| 1089 | pad: [6.0, 5.0], | |
| 1090 | fill: Some(self.panel_bg), | |
| 1091 | border: Some(self.rule), | |
| 1092 | radius: 4.0, | |
| 1093 | ..ui::Spec::default() | |
| 1094 | }, | |
| 1095 | ); | |
| 1096 | // Swatch header | |
| 1097 | ui.open( | |
| 1098 | "col-hdr", | |
| 1099 | ui::Spec { | |
| 1100 | size: [ui::fill(), ui::px(18.0)], | |
| 1101 | axis: ui::Axis::X, | |
| 1102 | gap: 6.0, | |
| 1103 | ..ui::Spec::default() | |
| 1104 | }, | |
| 1105 | ); | |
| 1106 | ui.leaf( | |
| 1107 | "col-lbl", | |
| 1108 | ui::Spec { | |
| 1109 | size: [ui::px(80.0), ui::fill()], | |
| 1110 | text: Some(label), | |
| 1111 | font_size: Some(10.5), | |
| 1112 | medium: true, | |
| 1113 | color: Some(self.text), | |
| 1114 | ..ui::Spec::default() | |
| 1115 | }, | |
| 1116 | ); | |
| 1117 | ui.leaf( | |
| 1118 | "col-swatch", | |
| 1119 | ui::Spec { | |
| 1120 | size: [ui::fill(), ui::px(16.0)], | |
| 1121 | fill: Some(swatch), | |
| 1122 | border: Some(self.rule), | |
| 1123 | radius: 3.0, | |
| 1124 | ..ui::Spec::default() | |
| 1125 | }, | |
| 1126 | ); | |
| 1127 | ui.close(); | |
| 1128 | ||
| 1129 | self.render_float_row(ui, pane_id, comp, history, node_id, r_key, "Red", 0.0, 1.0, default[0], current_frame, actions); | |
| 1130 | self.render_float_row(ui, pane_id, comp, history, node_id, g_key, "Green", 0.0, 1.0, default[1], current_frame, actions); | |
| 1131 | self.render_float_row(ui, pane_id, comp, history, node_id, b_key, "Blue", 0.0, 1.0, default[2], current_frame, actions); | |
| 1132 | if let Some(ak) = a_key { | |
| 1133 | self.render_float_row(ui, pane_id, comp, history, node_id, ak, "Alpha", 0.0, 1.0, default[3], current_frame, actions); | |
| 1134 | } | |
| 1135 | ui.close(); | |
| 1136 | } | |
| 1137 | ||
| 1138 | #[allow(clippy::too_many_arguments)] | |
| 1139 | fn render_text_row( | |
| 1140 | &mut self, | |
| 1141 | ui: &mut ui::Ui, | |
| 1142 | pane_id: ui::Id, | |
| 1143 | comp: &mut Comp, | |
| 1144 | history: &mut CompHistory, | |
| 1145 | node_id: NodeId, | |
| 1146 | input_name: &str, | |
| 1147 | label: &str, | |
| 1148 | default: &str, | |
| 1149 | actions: &mut Vec<InspectorAction>, | |
| 1150 | ) { | |
| 1151 | let field_id = pane_id.child(("txt", node_id.0, input_name)); | |
| 1152 | let key = (node_id.0, input_name.to_owned()); | |
| 1153 | let model_val = comp | |
| 1154 | .node(node_id) | |
| 1155 | .static_str(input_name) | |
| 1156 | .unwrap_or(default) | |
| 1157 | .to_owned(); | |
| 1158 | let buf = self.text_bufs.entry(key).or_insert_with(|| model_val.clone()); | |
| 1159 | if ui.focused() != Some(field_id) && *buf != model_val { | |
| 1160 | *buf = model_val.clone(); | |
| 1161 | } | |
| 1162 | let prev = buf.clone(); | |
| 1163 | ||
| 1164 | ui.open( | |
| 1165 | ("row-t", node_id.0, input_name), | |
| 1166 | ui::Spec { | |
| 1167 | size: [ui::fill(), ui::px(24.0)], | |
| 1168 | axis: ui::Axis::X, | |
| 1169 | gap: 6.0, | |
| 1170 | ..ui::Spec::default() | |
| 1171 | }, | |
| 1172 | ); | |
| 1173 | ui.leaf( | |
| 1174 | "lbl", | |
| 1175 | ui::Spec { | |
| 1176 | size: [ui::px(86.0), ui::fill()], | |
| 1177 | text: Some(label), | |
| 1178 | font_size: Some(10.5), | |
| 1179 | overflow: ui::Overflow::Ellipsis, | |
| 1180 | color: Some(self.text_dim), | |
| 1181 | ..ui::Spec::default() | |
| 1182 | }, | |
| 1183 | ); | |
| 1184 | ui::text_field( | |
| 1185 | ui, | |
| 1186 | field_id, | |
| 1187 | buf, | |
| 1188 | label, | |
| 1189 | ui::Spec { | |
| 1190 | size: [ui::fill(), ui::px(22.0)], | |
| 1191 | font_size: Some(11.0), | |
| 1192 | fill: Some(self.panel_bg), | |
| 1193 | border: Some(self.rule), | |
| 1194 | hover_border: Some(self.accent), | |
| 1195 | radius: 4.0, | |
| 1196 | pad: [6.0, 2.0], | |
| 1197 | ..ui::Spec::default() | |
| 1198 | }, | |
| 1199 | ); | |
| 1200 | if *buf != prev { | |
| 1201 | history.push_coalesced( | |
| 1202 | comp, | |
| 1203 | format!("Edit {label}"), | |
| 1204 | format!("text:{}:{input_name}", node_id.0), | |
| 1205 | ); | |
| 1206 | comp.set_input_value(node_id, input_name, ParamValue::Text(buf.clone())); | |
| 1207 | actions.push(InspectorAction::ParamEdited(node_id)); | |
| 1208 | } | |
| 1209 | ui.close(); | |
| 1210 | } | |
| 1211 | ||
| 1212 | #[allow(clippy::too_many_arguments)] | |
| 1213 | fn scrub_slider( | |
| 1214 | &mut self, | |
| 1215 | ui: &mut ui::Ui, | |
| 1216 | slider_id: ui::Id, | |
| 1217 | drag_key: &str, | |
| 1218 | node_id: NodeId, | |
| 1219 | value: f64, | |
| 1220 | min: f64, | |
| 1221 | max: f64, | |
| 1222 | default: f64, | |
| 1223 | history: &mut CompHistory, | |
| 1224 | comp: &Comp, | |
| 1225 | input_name: &str, | |
| 1226 | ) -> Option<f64> { | |
| 1227 | let sig = ui.signal(slider_id); | |
| 1228 | let rect = ui.laid_out(slider_id); | |
| 1229 | let width = rect.map(|r| (r[2] - r[0]).max(40.0)).unwrap_or(110.0); | |
| 1230 | let mut updated: Option<f64> = None; | |
| 1231 | ||
| 1232 | if let Some(press) = sig.press { | |
| 1233 | let now = Instant::now(); | |
| 1234 | let is_double = self | |
| 1235 | .last_click | |
| 1236 | .get(drag_key) | |
| 1237 | .is_some_and(|prev| now.duration_since(*prev) < Duration::from_millis(320)); | |
| 1238 | self.last_click.insert(drag_key.to_owned(), now); | |
| 1239 | if is_double { | |
| 1240 | history.push_snapshot(comp, format!("Reset {input_name}")); | |
| 1241 | self.drag = None; | |
| 1242 | updated = Some(default); | |
| 1243 | } else { | |
| 1244 | self.drag = Some(DragState { | |
| 1245 | node_id, | |
| 1246 | key: drag_key.to_owned(), | |
| 1247 | start_x: press[0], | |
| 1248 | start_val: value, | |
| 1249 | min, | |
| 1250 | max, | |
| 1251 | width, | |
| 1252 | }); | |
| 1253 | } | |
| 1254 | } else if sig.dragging { | |
| 1255 | if let (Some(d), Some(ptr)) = (&self.drag, ui.pointer()) { | |
| 1256 | if d.key == drag_key && d.node_id == node_id { | |
| 1257 | let fine = if ui.modifiers().shift_key() { 0.15 } else { 1.0 }; | |
| 1258 | let next = compute_scrub_value(d.start_val, ptr[0] - d.start_x, d.width, d.min, d.max, fine); | |
| 1259 | if (next - value).abs() > 1e-6 { | |
| 1260 | history.push_coalesced( | |
| 1261 | comp, | |
| 1262 | format!("Adjust {input_name}"), | |
| 1263 | format!("scrub:{drag_key}"), | |
| 1264 | ); | |
| 1265 | updated = Some(next); | |
| 1266 | } | |
| 1267 | } | |
| 1268 | } | |
| 1269 | } else if self.drag.as_ref().is_some_and(|d| d.key == drag_key) { | |
| 1270 | self.drag = None; | |
| 1271 | history.break_coalescing(); | |
| 1272 | } | |
| 1273 | ||
| 1274 | let display_val = updated.unwrap_or(value); | |
| 1275 | let frac = normalize_slider_fraction(display_val, min, max); | |
| 1276 | let val_text = format_param_number(display_val); | |
| 1277 | let fill_w = (width * frac).clamp(0.0, width); | |
| 1278 | let active_drag = self.drag.as_ref().is_some_and(|d| d.key == drag_key); | |
| 1279 | ||
| 1280 | ui.open_as( | |
| 1281 | slider_id, | |
| 1282 | ui::Spec { | |
| 1283 | cursor: Some(winit::window::CursorIcon::EwResize), | |
| 1284 | flags: ui::Flags::CLICKABLE | ui::Flags::CLIP, | |
| 1285 | size: [ui::fill(), ui::px(20.0)], | |
| 1286 | fill: Some(self.panel_bg), | |
| 1287 | border: Some(if active_drag || sig.hovered { self.accent } else { self.rule }), | |
| 1288 | radius: 4.0, | |
| 1289 | ..ui::Spec::default() | |
| 1290 | }, | |
| 1291 | ); | |
| 1292 | if fill_w > 0.5 { | |
| 1293 | ui.leaf( | |
| 1294 | "fill", | |
| 1295 | ui::Spec { | |
| 1296 | flags: ui::Flags::FLOAT | ui::Flags::INERT, | |
| 1297 | position: [0.0, 0.0], | |
| 1298 | size: [ui::px(fill_w), ui::px(20.0)], | |
| 1299 | fill: Some([self.accent[0], self.accent[1], self.accent[2], if active_drag { 0.34 } else { 0.22 }]), | |
| 1300 | radius: 3.0, | |
| 1301 | ..ui::Spec::default() | |
| 1302 | }, | |
| 1303 | ); | |
| 1304 | } | |
| 1305 | ui.leaf( | |
| 1306 | "txt", | |
| 1307 | ui::Spec { | |
| 1308 | flags: ui::Flags::INERT, | |
| 1309 | size: [ui::fill(), ui::fill()], | |
| 1310 | text: Some(&val_text), | |
| 1311 | font_size: Some(10.5), | |
| 1312 | color: Some(self.text), | |
| 1313 | center: true, | |
| 1314 | ..ui::Spec::default() | |
| 1315 | }, | |
| 1316 | ); | |
| 1317 | ui.close(); | |
| 1318 | ||
| 1319 | updated | |
| 1320 | } | |
| 1321 | ||
| 1322 | fn keyframe_button(&self, ui: &mut ui::Ui, part: impl std::hash::Hash, state: KeyframeState) -> bool { | |
| 1323 | let (ink, fill, border) = match state { | |
| 1324 | KeyframeState::KeyframeAtCurrent => ( | |
| 1325 | [1.0, 0.80, 0.22, 1.0], | |
| 1326 | Some([0.98, 0.72, 0.18, 0.24]), | |
| 1327 | Some([0.98, 0.76, 0.20, 0.85]), | |
| 1328 | ), | |
| 1329 | KeyframeState::AnimatedOtherFrame => ( | |
| 1330 | [0.92, 0.64, 0.24, 0.90], | |
| 1331 | Some([0.92, 0.64, 0.24, 0.12]), | |
| 1332 | Some([0.92, 0.64, 0.24, 0.45]), | |
| 1333 | ), | |
| 1334 | KeyframeState::Static => (self.text_dim, None, None), | |
| 1335 | }; | |
| 1336 | let sig = ui.leaf( | |
| 1337 | part, | |
| 1338 | ui::Spec { | |
| 1339 | cursor: Some(winit::window::CursorIcon::Pointer), | |
| 1340 | flags: ui::Flags::CLICKABLE, | |
| 1341 | size: [ui::px(20.0), ui::px(20.0)], | |
| 1342 | text: Some("â—†"), | |
| 1343 | font_size: Some(10.5), | |
| 1344 | color: Some(ink), | |
| 1345 | fill, | |
| 1346 | border, | |
| 1347 | hover_fill: Some([0.98, 0.76, 0.22, 0.18]), | |
| 1348 | radius: 4.0, | |
| 1349 | center: true, | |
| 1350 | ..ui::Spec::default() | |
| 1351 | }, | |
| 1352 | ); | |
| 1353 | sig.clicked | |
| 1354 | } | |
| 1355 | ||
| 1356 | #[allow(clippy::too_many_arguments)] | |
| 1357 | fn toggle_keyframe( | |
| 1358 | &self, | |
| 1359 | comp: &mut Comp, | |
| 1360 | history: &mut CompHistory, | |
| 1361 | node_id: NodeId, | |
| 1362 | input_name: &str, | |
| 1363 | current_frame: f64, | |
| 1364 | cur_val: f64, | |
| 1365 | state: KeyframeState, | |
| 1366 | actions: &mut Vec<InspectorAction>, | |
| 1367 | ) { | |
| 1368 | match state { | |
| 1369 | KeyframeState::KeyframeAtCurrent => { | |
| 1370 | if let Some(sp_name) = comp | |
| 1371 | .node(node_id) | |
| 1372 | .inputs | |
| 1373 | .get(input_name) | |
| 1374 | .and_then(|b| b.source_op.clone()) | |
| 1375 | { | |
| 1376 | history.push_snapshot(comp, format!("Delete Keyframe {input_name}")); | |
| 1377 | if comp.delete_keyframe(&sp_name, current_frame) { | |
| 1378 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 1379 | actions.push(InspectorAction::StatusNotice(format!( | |
| 1380 | "Deleted keyframe on {input_name} @ f{current_frame:.0}" | |
| 1381 | ))); | |
| 1382 | } | |
| 1383 | } | |
| 1384 | } | |
| 1385 | KeyframeState::Static | KeyframeState::AnimatedOtherFrame => { | |
| 1386 | history.push_snapshot(comp, format!("Set Keyframe {input_name}")); | |
| 1387 | match comp.set_keyframe(node_id, input_name, current_frame, cur_val) { | |
| 1388 | Ok(_) => { | |
| 1389 | actions.push(InspectorAction::KeyframeEdited(node_id)); | |
| 1390 | actions.push(InspectorAction::StatusNotice(format!( | |
| 1391 | "Keyframed {input_name} = {:.3} @ f{current_frame:.0}", | |
| 1392 | cur_val | |
| 1393 | ))); | |
| 1394 | } | |
| 1395 | Err(e) => actions.push(InspectorAction::StatusNotice(e)), | |
| 1396 | } | |
| 1397 | } | |
| 1398 | } | |
| 1399 | } | |
| 1400 | } | |
| 1401 | ||
| 1402 | /// Computes whether an input is static, animated on another frame, or has a keyframe at `frame`. | |
| 1403 | pub fn keyframe_state(comp: &Comp, node_id: NodeId, input_name: &str, frame: f64) -> KeyframeState { | |
| 1404 | let Some(sp_name) = comp | |
| 1405 | .node(node_id) | |
| 1406 | .inputs | |
| 1407 | .get(input_name) | |
| 1408 | .and_then(|b| b.source_op.as_deref()) | |
| 1409 | else { | |
| 1410 | return KeyframeState::Static; | |
| 1411 | }; | |
| 1412 | let Some(spline) = comp.splines.get(sp_name) else { | |
| 1413 | return KeyframeState::Static; | |
| 1414 | }; | |
| 1415 | if spline.keyframes.iter().any(|kf| (kf.time - frame).abs() < 1e-4) { | |
| 1416 | KeyframeState::KeyframeAtCurrent | |
| 1417 | } else if !spline.keyframes.is_empty() { | |
| 1418 | KeyframeState::AnimatedOtherFrame | |
| 1419 | } else { | |
| 1420 | KeyframeState::Static | |
| 1421 | } | |
| 1422 | } | |
| 1423 | ||
| 1424 | pub fn normalize_slider_fraction(value: f64, min: f64, max: f64) -> f32 { | |
| 1425 | let span = (max - min).abs().max(1e-6); | |
| 1426 | ((value - min) / span).clamp(0.0, 1.0) as f32 | |
| 1427 | } | |
| 1428 | ||
| 1429 | pub fn compute_scrub_value(start_val: f64, dx: f32, width: f32, min: f64, max: f64, fine_scale: f64) -> f64 { | |
| 1430 | let span = (max - min).max(1e-4); | |
| 1431 | let delta = (f64::from(dx) / f64::from(width.max(40.0))) * span * fine_scale; | |
| 1432 | (start_val + delta).clamp(min, max) | |
| 1433 | } | |
| 1434 | ||
| 1435 | pub fn format_param_number(v: f64) -> String { | |
| 1436 | if v.abs() >= 100.0 { | |
| 1437 | format!("{v:.1}") | |
| 1438 | } else if v.abs() >= 10.0 { | |
| 1439 | format!("{v:.2}") | |
| 1440 | } else { | |
| 1441 | format!("{v:.3}") | |
| 1442 | } | |
| 1443 | } | |
| 1444 | ||
| 1445 | pub fn infer_float_range(name: &str, val: f64) -> (f64, f64) { | |
| 1446 | let lower = name.to_ascii_lowercase(); | |
| 1447 | if lower.contains("angle") || lower.contains("rotate") { | |
| 1448 | (-360.0, 360.0) | |
| 1449 | } else if lower.contains("width") || lower.contains("height") { | |
| 1450 | if val > 2.0 { | |
| 1451 | (1.0, (val * 2.0).max(4096.0)) | |
| 1452 | } else { | |
| 1453 | (0.0, 2.0) | |
| 1454 | } | |
| 1455 | } else if lower.contains("blend") || lower.contains("alpha") || lower.contains("opacity") { | |
| 1456 | (0.0, 1.0) | |
| 1457 | } else if lower.contains("size") || lower.contains("blur") || lower.contains("glow") { | |
| 1458 | (0.0, (val.abs() * 2.0).max(50.0)) | |
| 1459 | } else if val < 0.0 || val > 1.0 { | |
| 1460 | let hi = (val.abs() * 2.0).max(10.0); | |
| 1461 | (-hi, hi) | |
| 1462 | } else { | |
| 1463 | (0.0, 1.0) | |
| 1464 | } | |
| 1465 | } | |
| 1466 | ||
| 1467 | const APPLY_MODES: &[&str] = &[ | |
| 1468 | "Normal", | |
| 1469 | "Screen", | |
| 1470 | "Multiply", | |
| 1471 | "Overlay", | |
| 1472 | "Soft Light", | |
| 1473 | "Hard Light", | |
| 1474 | "Color Dodge", | |
| 1475 | "Color Burn", | |
| 1476 | "Darken", | |
| 1477 | "Lighten", | |
| 1478 | "Difference", | |
| 1479 | "Exclusion", | |
| 1480 | "Hue", | |
| 1481 | "Saturation", | |
| 1482 | "Color", | |
| 1483 | "Luminosity", | |
| 1484 | ]; | |
| 1485 | ||
| 1486 | const BLUR_FILTERS: &[&str] = &["Fast Gaussian", "Gaussian", "Box", "Bartlett", "Multi-box"]; | |
| 1487 | const CLIPPING_MODES: &[&str] = &["Frame", "Domain", "None"]; | |
| 1488 | const EDGES_MODES: &[&str] = &["Canvas", "Wrap", "Duplicate", "Mirror"]; | |
| 1489 | const SHAPE3D_TYPES: &[&str] = &[ | |
| 1490 | "SurfacePlaneInputs", | |
| 1491 | "SurfaceSphereInputs", | |
| 1492 | "SurfaceCubeInputs", | |
| 1493 | "SurfaceCylinderInputs", | |
| 1494 | "SurfaceConeInputs", | |
| 1495 | "SurfaceTorusInputs", | |
| 1496 | ]; | |
| 1497 | ||
| 1498 | const fn flt(name: &'static str, label: &'static str, min: f64, max: f64, default: f64) -> ParamSpec { | |
| 1499 | ParamSpec { | |
| 1500 | name, | |
| 1501 | label, | |
| 1502 | kind: ParamKind::Float { min, max, default, step: 0.01 }, | |
| 1503 | } | |
| 1504 | } | |
| 1505 | ||
| 1506 | const fn v2(name: &'static str, label: &'static str, min: f64, max: f64, default: [f64; 2]) -> ParamSpec { | |
| 1507 | ParamSpec { | |
| 1508 | name, | |
| 1509 | label, | |
| 1510 | kind: ParamKind::Vec2 { min, max, default }, | |
| 1511 | } | |
| 1512 | } | |
| 1513 | ||
| 1514 | const fn boolean(name: &'static str, label: &'static str, default: bool) -> ParamSpec { | |
| 1515 | ParamSpec { | |
| 1516 | name, | |
| 1517 | label, | |
| 1518 | kind: ParamKind::Bool { default }, | |
| 1519 | } | |
| 1520 | } | |
| 1521 | ||
| 1522 | const fn fuid( | |
| 1523 | name: &'static str, | |
| 1524 | label: &'static str, | |
| 1525 | options: &'static [&'static str], | |
| 1526 | default: &'static str, | |
| 1527 | ) -> ParamSpec { | |
| 1528 | ParamSpec { | |
| 1529 | name, | |
| 1530 | label, | |
| 1531 | kind: ParamKind::Enum { options, default }, | |
| 1532 | } | |
| 1533 | } | |
| 1534 | ||
| 1535 | /// Curated Fusion 21.1 parameter schemas for major compositor tools. | |
| 1536 | pub fn schema_for_kind(kind: &str) -> &'static [ParamSpec] { | |
| 1537 | match kind { | |
| 1538 | "Merge" => const { &[ | |
| 1539 | fuid("ApplyMode", "Apply Mode", APPLY_MODES, "Normal"), | |
| 1540 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1541 | flt("SubMerge", "Subtractive", 0.0, 1.0, 0.0), | |
| 1542 | v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), | |
| 1543 | flt("Size", "Size", 0.0, 5.0, 1.0), | |
| 1544 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1545 | boolean("FlipHoriz", "Flip Horiz", false), | |
| 1546 | boolean("FlipVert", "Flip Vert", false), | |
| 1547 | fuid("Edges", "Edges", EDGES_MODES, "Canvas"), | |
| 1548 | boolean("PerformDepthMerge", "Depth Merge", false), | |
| 1549 | ] }, | |
| 1550 | "Transform" => const { &[ | |
| 1551 | v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), | |
| 1552 | v2("Pivot", "Pivot", -1.0, 2.0, [0.5, 0.5]), | |
| 1553 | flt("Size", "Size", 0.0, 5.0, 1.0), | |
| 1554 | flt("Aspect", "Aspect", 0.1, 4.0, 1.0), | |
| 1555 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1556 | boolean("FlipHoriz", "Flip Horiz", false), | |
| 1557 | boolean("FlipVert", "Flip Vert", false), | |
| 1558 | fuid("Edges", "Edges", EDGES_MODES, "Canvas"), | |
| 1559 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1560 | ] }, | |
| 1561 | "Blur" => const { &[ | |
| 1562 | fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), | |
| 1563 | boolean("LockXY", "Lock X/Y", true), | |
| 1564 | flt("XBlurSize", "Blur Size", 0.0, 100.0, 1.0), | |
| 1565 | flt("YBlurSize", "Y Blur Size", 0.0, 100.0, 1.0), | |
| 1566 | fuid("ClippingMode", "Clipping", CLIPPING_MODES, "Frame"), | |
| 1567 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1568 | ] }, | |
| 1569 | "Glow" => const { &[ | |
| 1570 | flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), | |
| 1571 | flt("Glow", "Glow", 0.0, 2.0, 0.8), | |
| 1572 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1573 | fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), | |
| 1574 | ] }, | |
| 1575 | "SoftGlow" => const { &[ | |
| 1576 | fuid("Filter", "Filter", BLUR_FILTERS, "Fast Gaussian"), | |
| 1577 | flt("Threshold", "Threshold", 0.0, 1.0, 0.0), | |
| 1578 | flt("Gain", "Gain", 0.0, 5.0, 2.0), | |
| 1579 | flt("XGlowSize", "Glow Size", 0.0, 100.0, 10.0), | |
| 1580 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1581 | ] }, | |
| 1582 | "BrightnessContrast" => const { &[ | |
| 1583 | flt("Gain", "Gain", 0.0, 5.0, 1.0), | |
| 1584 | flt("Lift", "Lift", -1.0, 1.0, 0.0), | |
| 1585 | flt("Gamma", "Gamma", 0.05, 5.0, 1.0), | |
| 1586 | flt("Contrast", "Contrast", -1.0, 1.0, 0.0), | |
| 1587 | flt("Brightness", "Brightness", -1.0, 1.0, 0.0), | |
| 1588 | flt("Saturation", "Saturation", 0.0, 4.0, 1.0), | |
| 1589 | boolean("PreDividePostMultiply", "Pre-Div/Post", false), | |
| 1590 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1591 | ] }, | |
| 1592 | "ColorCorrector" => const { &[ | |
| 1593 | flt("WheelSaturation1", "Saturation", 0.0, 4.0, 1.0), | |
| 1594 | flt("WheelHue1", "Hue", -1.0, 1.0, 0.0), | |
| 1595 | flt("MasterRGBGain", "Gain", 0.0, 5.0, 1.0), | |
| 1596 | flt("MasterRGBGamma", "Gamma", 0.05, 5.0, 1.0), | |
| 1597 | flt("MasterRGBLift", "Lift", -1.0, 1.0, 0.0), | |
| 1598 | flt("MasterRGBContrast", "Contrast", 0.0, 3.0, 1.0), | |
| 1599 | flt("MasterRGBBrightness", "Brightness", -1.0, 1.0, 0.0), | |
| 1600 | boolean("PreDividePostMultiply", "Pre-Div/Post", false), | |
| 1601 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1602 | ] }, | |
| 1603 | "Background" => const { &[ | |
| 1604 | ParamSpec { | |
| 1605 | name: "TopLeftColor", | |
| 1606 | label: "Color", | |
| 1607 | kind: ParamKind::Color { | |
| 1608 | r: "TopLeftRed", | |
| 1609 | g: "TopLeftGreen", | |
| 1610 | b: "TopLeftBlue", | |
| 1611 | a: Some("TopLeftAlpha"), | |
| 1612 | default: [0.0, 0.0, 0.0, 1.0], | |
| 1613 | }, | |
| 1614 | }, | |
| 1615 | flt("Width", "Width", 1.0, 7680.0, 1920.0), | |
| 1616 | flt("Height", "Height", 1.0, 4320.0, 1080.0), | |
| 1617 | ] }, | |
| 1618 | "TextPlus" => const { &[ | |
| 1619 | ParamSpec { | |
| 1620 | name: "StyledText", | |
| 1621 | label: "Styled Text", | |
| 1622 | kind: ParamKind::Text { default: "" }, | |
| 1623 | }, | |
| 1624 | ParamSpec { | |
| 1625 | name: "Font", | |
| 1626 | label: "Font", | |
| 1627 | kind: ParamKind::Text { default: "Open Sans" }, | |
| 1628 | }, | |
| 1629 | flt("Size", "Size", 0.005, 1.0, 0.08), | |
| 1630 | flt("Tracking", "Tracking", 0.0, 4.0, 1.0), | |
| 1631 | v2("Center", "Center", -1.0, 2.0, [0.5, 0.5]), | |
| 1632 | ParamSpec { | |
| 1633 | name: "TextColor", | |
| 1634 | label: "Text Color", | |
| 1635 | kind: ParamKind::Color { | |
| 1636 | r: "Red1", | |
| 1637 | g: "Green1", | |
| 1638 | b: "Blue1", | |
| 1639 | a: Some("Alpha1"), | |
| 1640 | default: [1.0, 1.0, 1.0, 1.0], | |
| 1641 | }, | |
| 1642 | }, | |
| 1643 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1644 | ] }, | |
| 1645 | "EllipseMask" => const { &[ | |
| 1646 | flt("Level", "Level", 0.0, 1.0, 1.0), | |
| 1647 | flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), | |
| 1648 | flt("BorderWidth", "Border", -0.2, 0.2, 0.0), | |
| 1649 | boolean("Solid", "Solid", true), | |
| 1650 | boolean("Invert", "Invert", false), | |
| 1651 | v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), | |
| 1652 | flt("Width", "Width", 0.0, 2.0, 0.5), | |
| 1653 | flt("Height", "Height", 0.0, 2.0, 0.5), | |
| 1654 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1655 | ] }, | |
| 1656 | "RectangleMask" => const { &[ | |
| 1657 | flt("Level", "Level", 0.0, 1.0, 1.0), | |
| 1658 | flt("SoftEdge", "Soft Edge", 0.0, 0.5, 0.0), | |
| 1659 | flt("CornerRadius", "Corner", 0.0, 1.0, 0.0), | |
| 1660 | boolean("Solid", "Solid", true), | |
| 1661 | boolean("Invert", "Invert", false), | |
| 1662 | v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), | |
| 1663 | flt("Width", "Width", 0.0, 2.0, 0.5), | |
| 1664 | flt("Height", "Height", 0.0, 2.0, 0.5), | |
| 1665 | flt("Angle", "Angle", -360.0, 360.0, 0.0), | |
| 1666 | ] }, | |
| 1667 | "Displace" => const { &[ | |
| 1668 | flt("XRefraction", "X Refract", -1.0, 1.0, 0.0), | |
| 1669 | flt("YRefraction", "Y Refract", -1.0, 1.0, 0.0), | |
| 1670 | flt("RefractionStrength", "Strength", -2.0, 2.0, 0.0), | |
| 1671 | flt("LightAngle", "Light Angle", -360.0, 360.0, 0.0), | |
| 1672 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1673 | ] }, | |
| 1674 | "Vortex" => const { &[ | |
| 1675 | v2("Center", "Center", -0.5, 1.5, [0.5, 0.5]), | |
| 1676 | flt("Size", "Size", 0.0, 2.0, 0.5), | |
| 1677 | flt("Angle", "Angle", -720.0, 720.0, 90.0), | |
| 1678 | flt("Power", "Power", 0.1, 5.0, 1.0), | |
| 1679 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1680 | ] }, | |
| 1681 | "FastNoise" => const { &[ | |
| 1682 | flt("Detail", "Detail", 0.0, 10.0, 2.0), | |
| 1683 | flt("Contrast", "Contrast", 0.0, 5.0, 1.0), | |
| 1684 | flt("Brightness", "Brightness", -1.0, 1.0, 0.0), | |
| 1685 | flt("XScale", "Scale", 0.01, 50.0, 2.0), | |
| 1686 | flt("Seethe", "Seethe", 0.0, 100.0, 0.0), | |
| 1687 | flt("SeetheRate", "Seethe Rate", 0.0, 2.0, 0.0), | |
| 1688 | v2("Center", "Center", -2.0, 3.0, [0.5, 0.5]), | |
| 1689 | ] }, | |
| 1690 | "UltraKeyer" => const { &[ | |
| 1691 | ParamSpec { | |
| 1692 | name: "KeyColor", | |
| 1693 | label: "Key Color", | |
| 1694 | kind: ParamKind::Color { | |
| 1695 | r: "BackgroundRed", | |
| 1696 | g: "BackgroundGreen", | |
| 1697 | b: "BackgroundBlue", | |
| 1698 | a: None, | |
| 1699 | default: [0.0, 1.0, 0.0, 1.0], | |
| 1700 | }, | |
| 1701 | }, | |
| 1702 | flt("MatteThresholdLow", "Matte Low", 0.0, 1.0, 0.0), | |
| 1703 | flt("MatteThresholdHigh", "Matte High", 0.0, 1.0, 1.0), | |
| 1704 | flt("SpillSuppression", "Spill", 0.0, 1.0, 0.0), | |
| 1705 | boolean("PostMultiplyImage", "Post-Multiply", true), | |
| 1706 | flt("Blend", "Blend", 0.0, 1.0, 1.0), | |
| 1707 | ] }, | |
| 1708 | "Loader" => const { &[ | |
| 1709 | flt("GlobalIn", "Global In", 0.0, 10000.0, 0.0), | |
| 1710 | flt("GlobalOut", "Global Out", 0.0, 10000.0, 100.0), | |
| 1711 | boolean("Loop", "Loop", false), | |
| 1712 | boolean("Reverse", "Reverse", false), | |
| 1713 | ] }, | |
| 1714 | "Camera3D" => const { &[ | |
| 1715 | flt("FLength", "Focal Len", 5.0, 200.0, 24.0), | |
| 1716 | flt("PlaneOfFocus", "Focus Dist", 0.1, 100.0, 5.0), | |
| 1717 | flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), | |
| 1718 | flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), | |
| 1719 | flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 5.0), | |
| 1720 | flt("Transform3DOp.Rotate.X", "Rot X", -180.0, 180.0, 0.0), | |
| 1721 | flt("Transform3DOp.Rotate.Y", "Rot Y", -180.0, 180.0, 0.0), | |
| 1722 | flt("Transform3DOp.Rotate.Z", "Rot Z", -180.0, 180.0, 0.0), | |
| 1723 | ] }, | |
| 1724 | "Shape3D" => const { &[ | |
| 1725 | fuid("Shape", "Shape", SHAPE3D_TYPES, "SurfacePlaneInputs"), | |
| 1726 | flt("SurfacePlaneInputs.Width", "Width", 0.01, 20.0, 1.0), | |
| 1727 | flt("SurfacePlaneInputs.Height", "Height", 0.01, 20.0, 1.0), | |
| 1728 | flt("Transform3DOp.Translate.X", "Trans X", -50.0, 50.0, 0.0), | |
| 1729 | flt("Transform3DOp.Translate.Y", "Trans Y", -50.0, 50.0, 0.0), | |
| 1730 | flt("Transform3DOp.Translate.Z", "Trans Z", -50.0, 50.0, 0.0), | |
| 1731 | flt("Transform3DOp.Scale.X", "Scale", 0.01, 20.0, 1.0), | |
| 1732 | ] }, | |
| 1733 | _ => &[], | |
| 1734 | } | |
| 1735 | } | |
| 1736 | ||
| 1737 | #[cfg(test)] | |
| 1738 | mod tests { | |
| 1739 | use super::*; | |
| 1740 | ||
| 1741 | #[test] | |
| 1742 | fn curated_schemas_cover_all_major_tools() { | |
| 1743 | let major = [ | |
| 1744 | "Merge", | |
| 1745 | "Transform", | |
| 1746 | "Blur", | |
| 1747 | "Glow", | |
| 1748 | "SoftGlow", | |
| 1749 | "BrightnessContrast", | |
| 1750 | "ColorCorrector", | |
| 1751 | "Background", | |
| 1752 | "TextPlus", | |
| 1753 | "EllipseMask", | |
| 1754 | "RectangleMask", | |
| 1755 | "Displace", | |
| 1756 | "Vortex", | |
| 1757 | "FastNoise", | |
| 1758 | "UltraKeyer", | |
| 1759 | "Loader", | |
| 1760 | "Camera3D", | |
| 1761 | "Shape3D", | |
| 1762 | ]; | |
| 1763 | for tool in major { | |
| 1764 | let s = schema_for_kind(tool); | |
| 1765 | assert!(!s.is_empty(), "expected non-empty curated schema for {tool}"); | |
| 1766 | } | |
| 1767 | assert!(schema_for_kind("UnknownCustomTool").is_empty()); | |
| 1768 | } | |
| 1769 | ||
| 1770 | #[test] | |
| 1771 | fn slider_math_and_clamping() { | |
| 1772 | assert!((normalize_slider_fraction(0.5, 0.0, 1.0) - 0.5).abs() < 1e-6); | |
| 1773 | assert!((normalize_slider_fraction(-5.0, 0.0, 1.0) - 0.0).abs() < 1e-6); | |
| 1774 | assert!((normalize_slider_fraction(10.0, 0.0, 1.0) - 1.0).abs() < 1e-6); | |
| 1775 | ||
| 1776 | let v = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 1.0); | |
| 1777 | assert!((v - 1.0).abs() < 1e-6); | |
| 1778 | let v_fine = compute_scrub_value(0.5, 50.0, 100.0, 0.0, 1.0, 0.1); | |
| 1779 | assert!((v_fine - 0.55).abs() < 1e-6); | |
| 1780 | } | |
| 1781 | ||
| 1782 | #[test] | |
| 1783 | fn keyframe_state_transitions() { | |
| 1784 | let src = r#"Composition { | |
| 1785 | Tools = ordered() { | |
| 1786 | BC1 = BrightnessContrast { | |
| 1787 | Inputs = { Gain = Input { Value = 1.0 } }, | |
| 1788 | }, | |
| 1789 | }, | |
| 1790 | }"#; | |
| 1791 | let mut comp = Comp::from_fusion_str(src).unwrap(); | |
| 1792 | let bc = comp.find_node("BC1").unwrap(); | |
| 1793 | assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); | |
| 1794 | ||
| 1795 | let sp = comp.set_keyframe(bc, "Gain", 10.0, 1.5).unwrap(); | |
| 1796 | assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::KeyframeAtCurrent); | |
| 1797 | assert_eq!(keyframe_state(&comp, bc, "Gain", 20.0), KeyframeState::AnimatedOtherFrame); | |
| 1798 | ||
| 1799 | assert!(comp.delete_keyframe(&sp, 10.0)); | |
| 1800 | assert_eq!(keyframe_state(&comp, bc, "Gain", 10.0), KeyframeState::Static); | |
| 1801 | } | |
| 1802 | } |
crates/studio/src/keyframes.rs deleted-1032| ... | ... | @@ -1,1032 +0,0 @@ |
| 1 | //! Docked Keyframes dope-sheet pane: animated node/channel tracks, audio waveform track, | |
| 2 | //! green RAM cache bar, and interactive drag-to-retime keyframes with `CompHistory` undo. | |
| 3 | ||
| 4 | use draw::{Glyphs, Primitive}; | |
| 5 | use model::{Comp, CompHistory, Keyframe, NodeId}; | |
| 6 | use std::collections::{HashMap, HashSet}; | |
| 7 | use std::sync::Arc; | |
| 8 | use winit::event::{ElementState, MouseButton}; | |
| 9 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; | |
| 10 | ||
| 11 | const RULER_H: f32 = 26.0; | |
| 12 | const AUDIO_ROW_H: f32 = 24.0; | |
| 13 | const ROW_H: f32 = 22.0; | |
| 14 | const SIDEBAR_W: f32 = 188.0; | |
| 15 | const FOOTER_H: f32 = 22.0; | |
| 16 | ||
| 17 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] | |
| 18 | pub struct DopeKeyRef { | |
| 19 | pub spline: String, | |
| 20 | pub key_idx: usize, | |
| 21 | } | |
| 22 | ||
| 23 | #[derive(Clone, Debug)] | |
| 24 | pub enum KeyframesAction { | |
| 25 | ScrubFrame(f64), | |
| 26 | SelectNode(NodeId), | |
| 27 | KeyframesEdited, | |
| 28 | } | |
| 29 | ||
| 30 | #[derive(Clone, Debug)] | |
| 31 | pub enum TrackRow { | |
| 32 | NodeHeader { | |
| 33 | node_id: NodeId, | |
| 34 | label: String, | |
| 35 | splines: Vec<String>, | |
| 36 | }, | |
| 37 | Channel { | |
| 38 | node_id: NodeId, | |
| 39 | label: String, | |
| 40 | spline: String, | |
| 41 | color: [f32; 4], | |
| 42 | }, | |
| 43 | } | |
| 44 | ||
| 45 | #[derive(Clone, Debug)] | |
| 46 | struct RetimeDrag { | |
| 47 | start_frame: f64, | |
| 48 | saved_splines: HashMap<String, Vec<Keyframe>>, | |
| 49 | pre_edit_comp: Comp, | |
| 50 | moved: bool, | |
| 51 | } | |
| 52 | ||
| 53 | #[derive(Clone, Copy, Debug)] | |
| 54 | pub struct KeyframesPalette { | |
| 55 | pub bg: [f32; 4], | |
| 56 | pub sidebar: [f32; 4], | |
| 57 | pub ruler: [f32; 4], | |
| 58 | pub rule: [f32; 4], | |
| 59 | pub grid: [f32; 4], | |
| 60 | pub row_alt: [f32; 4], | |
| 61 | pub text: [f32; 4], | |
| 62 | pub text_dim: [f32; 4], | |
| 63 | pub accent: [f32; 4], | |
| 64 | } | |
| 65 | ||
| 66 | impl KeyframesPalette { | |
| 67 | pub fn from_ui(theme: &ui::Theme) -> Self { | |
| 68 | let b = theme.base; | |
| 69 | let lift = |c: [f32; 4], d: f32| { | |
| 70 | [(c[0] + d).clamp(0.0, 1.0), (c[1] + d).clamp(0.0, 1.0), (c[2] + d).clamp(0.0, 1.0), 1.0] | |
| 71 | }; | |
| 72 | Self { | |
| 73 | bg: b, | |
| 74 | sidebar: lift(b, 0.012), | |
| 75 | ruler: lift(b, 0.018), | |
| 76 | rule: theme.rule, | |
| 77 | grid: lift(b, 0.022), | |
| 78 | row_alt: lift(b, 0.010), | |
| 79 | text: theme.text, | |
| 80 | text_dim: theme.text_dim, | |
| 81 | accent: theme.accent, | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | pub struct KeyframesView { | |
| 87 | pub t_range: [f64; 2], | |
| 88 | pub size: [f32; 2], | |
| 89 | pub scroll_y: f32, | |
| 90 | pub selected_only: bool, | |
| 91 | pub selected_keys: HashSet<DopeKeyRef>, | |
| 92 | pub cached_ranges: Vec<(i64, i64)>, | |
| 93 | pub audio_peaks: Vec<f32>, | |
| 94 | pub audio_range: [i64; 2], | |
| 95 | pub palette: KeyframesPalette, | |
| 96 | rows: Vec<TrackRow>, | |
| 97 | collapsed_nodes: HashSet<NodeId>, | |
| 98 | hovered_key: Option<DopeKeyRef>, | |
| 99 | retime_drag: Option<RetimeDrag>, | |
| 100 | box_select: Option<([f32; 2], [f32; 2])>, | |
| 101 | panning: Option<([f32; 2], [f64; 2], f32)>, | |
| 102 | scrubbing: bool, | |
| 103 | pointer_local: [f32; 2], | |
| 104 | fitted: bool, | |
| 105 | text_cache: HashMap<String, text::Layout>, | |
| 106 | } | |
| 107 | ||
| 108 | impl KeyframesView { | |
| 109 | pub fn new(theme: &ui::Theme) -> Self { | |
| 110 | Self { | |
| 111 | t_range: [0.0, 200.0], | |
| 112 | size: [800.0, 320.0], | |
| 113 | scroll_y: 0.0, | |
| 114 | selected_only: false, | |
| 115 | selected_keys: HashSet::new(), | |
| 116 | cached_ranges: Vec::new(), | |
| 117 | audio_peaks: Vec::new(), | |
| 118 | audio_range: [0, 0], | |
| 119 | palette: KeyframesPalette::from_ui(theme), | |
| 120 | rows: Vec::new(), | |
| 121 | collapsed_nodes: HashSet::new(), | |
| 122 | hovered_key: None, | |
| 123 | retime_drag: None, | |
| 124 | box_select: None, | |
| 125 | panning: None, | |
| 126 | scrubbing: false, | |
| 127 | pointer_local: [0.0, 0.0], | |
| 128 | fitted: false, | |
| 129 | text_cache: HashMap::new(), | |
| 130 | } | |
| 131 | } | |
| 132 | ||
| 133 | pub fn background(&self) -> [f32; 4] { | |
| 134 | self.palette.bg | |
| 135 | } | |
| 136 | ||
| 137 | pub fn resize(&mut self, size: [f32; 2]) { | |
| 138 | self.size = [size[0].max(1.0), size[1].max(1.0)]; | |
| 139 | } | |
| 140 | ||
| 141 | pub fn fit_comp(&mut self, comp: &Comp) { | |
| 142 | let r0 = comp.render_range[0] as f64; | |
| 143 | let r1 = comp.render_range[1].max(comp.render_range[0] + 10) as f64; | |
| 144 | let pad = ((r1 - r0) * 0.04).max(4.0); | |
| 145 | self.t_range = [r0 - pad, r1 + pad]; | |
| 146 | self.fitted = true; | |
| 147 | } | |
| 148 | ||
| 149 | pub fn sync_tracks(&mut self, comp: &Comp, selected_node: Option<NodeId>) { | |
| 150 | if !self.fitted { | |
| 151 | self.fit_comp(comp); | |
| 152 | } | |
| 153 | self.rows = build_tracks(comp, selected_node, self.selected_only, &self.collapsed_nodes); | |
| 154 | } | |
| 155 | ||
| 156 | fn tracks_top(&self) -> f32 { | |
| 157 | RULER_H + if self.audio_peaks.is_empty() { 0.0 } else { AUDIO_ROW_H } | |
| 158 | } | |
| 159 | ||
| 160 | fn plot_rect(&self) -> [f32; 4] { | |
| 161 | [ | |
| 162 | SIDEBAR_W, | |
| 163 | RULER_H, | |
| 164 | self.size[0].max(SIDEBAR_W + 40.0), | |
| 165 | (self.size[1] - FOOTER_H).max(RULER_H + 40.0), | |
| 166 | ] | |
| 167 | } | |
| 168 | ||
| 169 | fn time_to_x(&self, t: f64) -> f32 { | |
| 170 | let r = self.plot_rect(); | |
| 171 | let frac = (t - self.t_range[0]) / (self.t_range[1] - self.t_range[0]).max(1e-6); | |
| 172 | r[0] + frac as f32 * (r[2] - r[0]) | |
| 173 | } | |
| 174 | ||
| 175 | fn x_to_time(&self, x: f32) -> f64 { | |
| 176 | let r = self.plot_rect(); | |
| 177 | let frac = f64::from((x - r[0]) / (r[2] - r[0]).max(1.0)); | |
| 178 | self.t_range[0] + frac * (self.t_range[1] - self.t_range[0]) | |
| 179 | } | |
| 180 | ||
| 181 | fn row_y(&self, row_idx: usize) -> f32 { | |
| 182 | self.tracks_top() + row_idx as f32 * ROW_H - self.scroll_y | |
| 183 | } | |
| 184 | ||
| 185 | fn hit_key(&self, comp: &Comp, local: [f32; 2]) -> Option<DopeKeyRef> { | |
| 186 | let top = self.tracks_top(); | |
| 187 | if local[0] < SIDEBAR_W || local[1] < top || local[1] > self.size[1] - FOOTER_H { | |
| 188 | return None; | |
| 189 | } | |
| 190 | let mut best: Option<(f32, DopeKeyRef)> = None; | |
| 191 | for (ri, row) in self.rows.iter().enumerate() { | |
| 192 | let cy = self.row_y(ri) + ROW_H * 0.5; | |
| 193 | if (local[1] - cy).abs() > ROW_H * 0.52 { | |
| 194 | continue; | |
| 195 | } | |
| 196 | if let TrackRow::Channel { spline, .. } = row { | |
| 197 | let Some(sp) = comp.splines.get(spline) else { continue }; | |
| 198 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 199 | let kx = self.time_to_x(kf.time); | |
| 200 | let d = ((local[0] - kx).powi(2) + (local[1] - cy).powi(2)).sqrt(); | |
| 201 | if d <= 9.5 && best.as_ref().is_none_or(|(bd, _)| d < *bd) { | |
| 202 | best = Some((d, DopeKeyRef { spline: spline.clone(), key_idx: ki })); | |
| 203 | } | |
| 204 | } | |
| 205 | } | |
| 206 | } | |
| 207 | best.map(|(_, k)| k) | |
| 208 | } | |
| 209 | ||
| 210 | fn hit_span_bar(&self, comp: &Comp, local: [f32; 2]) -> Option<(String, Vec<usize>)> { | |
| 211 | let top = self.tracks_top(); | |
| 212 | if local[0] < SIDEBAR_W || local[1] < top || local[1] > self.size[1] - FOOTER_H { | |
| 213 | return None; | |
| 214 | } | |
| 215 | for (ri, row) in self.rows.iter().enumerate() { | |
| 216 | let y0 = self.row_y(ri); | |
| 217 | if local[1] < y0 || local[1] >= y0 + ROW_H { | |
| 218 | continue; | |
| 219 | } | |
| 220 | if let TrackRow::Channel { spline, .. } = row { | |
| 221 | let Some(sp) = comp.splines.get(spline) else { continue }; | |
| 222 | if sp.keyframes.len() >= 2 { | |
| 223 | let x0 = self.time_to_x(sp.keyframes.first().unwrap().time); | |
| 224 | let x1 = self.time_to_x(sp.keyframes.last().unwrap().time); | |
| 225 | if local[0] >= x0 && local[0] <= x1 { | |
| 226 | return Some((spline.clone(), (0..sp.keyframes.len()).collect())); | |
| 227 | } | |
| 228 | } | |
| 229 | } | |
| 230 | } | |
| 231 | None | |
| 232 | } | |
| 233 | ||
| 234 | fn begin_retime_drag(&mut self, comp: &Comp) { | |
| 235 | if self.selected_keys.is_empty() { | |
| 236 | return; | |
| 237 | } | |
| 238 | let mut saved = HashMap::new(); | |
| 239 | for kref in &self.selected_keys { | |
| 240 | if let Some(sp) = comp.splines.get(&kref.spline) { | |
| 241 | saved.entry(kref.spline.clone()).or_insert_with(|| sp.keyframes.clone()); | |
| 242 | } | |
| 243 | } | |
| 244 | self.retime_drag = Some(RetimeDrag { | |
| 245 | start_frame: self.x_to_time(self.pointer_local[0]).round(), | |
| 246 | saved_splines: saved, | |
| 247 | pre_edit_comp: comp.clone(), | |
| 248 | moved: false, | |
| 249 | }); | |
| 250 | } | |
| 251 | ||
| 252 | fn apply_retime_drag(&mut self, comp: &mut Comp) -> bool { | |
| 253 | let cur_frame = self.x_to_time(self.pointer_local[0]).round(); | |
| 254 | let Some(drag) = &mut self.retime_drag else { return false }; | |
| 255 | let dt = cur_frame - drag.start_frame; | |
| 256 | if dt.abs() < 0.5 && !drag.moved { | |
| 257 | return false; | |
| 258 | } | |
| 259 | drag.moved = true; | |
| 260 | retime_selected_keys(comp, &drag.saved_splines, &self.selected_keys, dt); | |
| 261 | true | |
| 262 | } | |
| 263 | ||
| 264 | fn finish_retime_drag(&mut self, comp: &mut Comp, history: &mut CompHistory) -> bool { | |
| 265 | let Some(drag) = self.retime_drag.take() else { return false }; | |
| 266 | if !drag.moved { | |
| 267 | return false; | |
| 268 | } | |
| 269 | for sname in drag.saved_splines.keys() { | |
| 270 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 271 | sp.sort(); | |
| 272 | } | |
| 273 | } | |
| 274 | comp.revision += 1; | |
| 275 | history.push_snapshot(&drag.pre_edit_comp, "Retime Keyframes"); | |
| 276 | true | |
| 277 | } | |
| 278 | ||
| 279 | fn delete_selected(&mut self, comp: &mut Comp, history: &mut CompHistory) -> bool { | |
| 280 | if self.selected_keys.is_empty() { | |
| 281 | return false; | |
| 282 | } | |
| 283 | let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new(); | |
| 284 | for kref in &self.selected_keys { | |
| 285 | by_spline.entry(kref.spline.clone()).or_default().push(kref.key_idx); | |
| 286 | } | |
| 287 | let pre = comp.clone(); | |
| 288 | let mut changed = false; | |
| 289 | for (sname, mut idxs) in by_spline { | |
| 290 | idxs.sort_unstable(); | |
| 291 | idxs.dedup(); | |
| 292 | if let Some(sp) = comp.splines.get_mut(&sname) { | |
| 293 | for &idx in idxs.iter().rev() { | |
| 294 | if sp.keyframes.len() > 1 && idx < sp.keyframes.len() { | |
| 295 | sp.keyframes.remove(idx); | |
| 296 | changed = true; | |
| 297 | } | |
| 298 | } | |
| 299 | } | |
| 300 | } | |
| 301 | if changed { | |
| 302 | self.selected_keys.clear(); | |
| 303 | comp.revision += 1; | |
| 304 | history.push_snapshot(&pre, "Delete Keyframes"); | |
| 305 | } | |
| 306 | changed | |
| 307 | } | |
| 308 | ||
| 309 | pub fn input( | |
| 310 | &mut self, | |
| 311 | comp: &mut Comp, | |
| 312 | history: &mut CompHistory, | |
| 313 | selected_node: Option<NodeId>, | |
| 314 | event: ui::Event, | |
| 315 | rect: [f32; 4], | |
| 316 | pointer_window: [f32; 2], | |
| 317 | modifiers: ModifiersState, | |
| 318 | ) -> Vec<KeyframesAction> { | |
| 319 | let mut actions = Vec::new(); | |
| 320 | self.pointer_local = [pointer_window[0] - rect[0], pointer_window[1] - rect[1]]; | |
| 321 | let plot = self.plot_rect(); | |
| 322 | ||
| 323 | match event { | |
| 324 | ui::Event::PointerMoved(p) => { | |
| 325 | self.pointer_local = [p[0] - rect[0], p[1] - rect[1]]; | |
| 326 | if self.scrubbing { | |
| 327 | actions.push(KeyframesAction::ScrubFrame(self.x_to_time(self.pointer_local[0]).round())); | |
| 328 | } else if let Some((sp, tr, sy)) = self.panning { | |
| 329 | let dx = f64::from((self.pointer_local[0] - sp[0]) / (plot[2] - plot[0]).max(1.0)); | |
| 330 | let dt = dx * (tr[1] - tr[0]); | |
| 331 | self.t_range = [tr[0] - dt, tr[1] - dt]; | |
| 332 | self.scroll_y = (sy - (self.pointer_local[1] - sp[1])).max(0.0); | |
| 333 | } else if self.retime_drag.is_some() { | |
| 334 | if self.apply_retime_drag(comp) { | |
| 335 | actions.push(KeyframesAction::KeyframesEdited); | |
| 336 | } | |
| 337 | } else if let Some((_, ref mut cur)) = self.box_select { | |
| 338 | *cur = self.pointer_local; | |
| 339 | } else { | |
| 340 | self.hovered_key = self.hit_key(comp, self.pointer_local); | |
| 341 | } | |
| 342 | } | |
| 343 | ui::Event::Button { button, pressed, .. } => { | |
| 344 | if button == MouseButton::Middle || (button == MouseButton::Left && modifiers.alt_key()) { | |
| 345 | if pressed { | |
| 346 | self.panning = Some((self.pointer_local, self.t_range, self.scroll_y)); | |
| 347 | } else { | |
| 348 | self.panning = None; | |
| 349 | } | |
| 350 | } else if button == MouseButton::Left { | |
| 351 | if pressed { | |
| 352 | // Sidebar header "All / Sel" toggle pill | |
| 353 | if self.pointer_local[0] >= SIDEBAR_W - 48.0 | |
| 354 | && self.pointer_local[0] <= SIDEBAR_W - 6.0 | |
| 355 | && self.pointer_local[1] >= 4.0 | |
| 356 | && self.pointer_local[1] <= RULER_H - 4.0 | |
| 357 | { | |
| 358 | self.selected_only = !self.selected_only; | |
| 359 | self.scroll_y = 0.0; | |
| 360 | self.sync_tracks(comp, selected_node); | |
| 361 | } else if self.pointer_local[0] < SIDEBAR_W && self.pointer_local[1] >= self.tracks_top() { | |
| 362 | let ri = ((self.pointer_local[1] - self.tracks_top() + self.scroll_y) / ROW_H).floor() as isize; | |
| 363 | if ri >= 0 && (ri as usize) < self.rows.len() { | |
| 364 | match &self.rows[ri as usize] { | |
| 365 | TrackRow::NodeHeader { node_id, .. } => { | |
| 366 | let id = *node_id; | |
| 367 | if self.pointer_local[0] < 24.0 { | |
| 368 | if !self.collapsed_nodes.remove(&id) { | |
| 369 | self.collapsed_nodes.insert(id); | |
| 370 | } | |
| 371 | self.sync_tracks(comp, selected_node); | |
| 372 | } else { | |
| 373 | actions.push(KeyframesAction::SelectNode(id)); | |
| 374 | } | |
| 375 | } | |
| 376 | TrackRow::Channel { node_id, .. } => { | |
| 377 | actions.push(KeyframesAction::SelectNode(*node_id)); | |
| 378 | } | |
| 379 | } | |
| 380 | } | |
| 381 | } else if self.pointer_local[0] >= SIDEBAR_W && self.pointer_local[1] < RULER_H { | |
| 382 | self.scrubbing = true; | |
| 383 | actions.push(KeyframesAction::ScrubFrame(self.x_to_time(self.pointer_local[0]).round())); | |
| 384 | } else if let Some(hit) = self.hit_key(comp, self.pointer_local) { | |
| 385 | if modifiers.shift_key() { | |
| 386 | if !self.selected_keys.remove(&hit) { | |
| 387 | self.selected_keys.insert(hit); | |
| 388 | } | |
| 389 | } else if !self.selected_keys.contains(&hit) { | |
| 390 | self.selected_keys.clear(); | |
| 391 | self.selected_keys.insert(hit); | |
| 392 | } | |
| 393 | self.begin_retime_drag(comp); | |
| 394 | } else if let Some((sname, idxs)) = self.hit_span_bar(comp, self.pointer_local) { | |
| 395 | if !modifiers.shift_key() { | |
| 396 | self.selected_keys.clear(); | |
| 397 | } | |
| 398 | for ki in idxs { | |
| 399 | self.selected_keys.insert(DopeKeyRef { spline: sname.clone(), key_idx: ki }); | |
| 400 | } | |
| 401 | self.begin_retime_drag(comp); | |
| 402 | } else if self.pointer_local[0] >= SIDEBAR_W { | |
| 403 | if !modifiers.shift_key() { | |
| 404 | self.selected_keys.clear(); | |
| 405 | } | |
| 406 | self.box_select = Some((self.pointer_local, self.pointer_local)); | |
| 407 | } | |
| 408 | } else { | |
| 409 | self.scrubbing = false; | |
| 410 | if self.finish_retime_drag(comp, history) { | |
| 411 | actions.push(KeyframesAction::KeyframesEdited); | |
| 412 | } | |
| 413 | if let Some((a, b)) = self.box_select.take() { | |
| 414 | let x0 = a[0].min(b[0]); | |
| 415 | let x1 = a[0].max(b[0]); | |
| 416 | let y0 = a[1].min(b[1]); | |
| 417 | let y1 = a[1].max(b[1]); | |
| 418 | if (x1 - x0) > 4.0 || (y1 - y0) > 4.0 { | |
| 419 | for (ri, row) in self.rows.iter().enumerate() { | |
| 420 | let cy = self.row_y(ri) + ROW_H * 0.5; | |
| 421 | if cy < y0 || cy > y1 { | |
| 422 | continue; | |
| 423 | } | |
| 424 | if let TrackRow::Channel { spline, .. } = row { | |
| 425 | if let Some(sp) = comp.splines.get(spline) { | |
| 426 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 427 | let kx = self.time_to_x(kf.time); | |
| 428 | if kx >= x0 && kx <= x1 { | |
| 429 | self.selected_keys.insert(DopeKeyRef { | |
| 430 | spline: spline.clone(), | |
| 431 | key_idx: ki, | |
| 432 | }); | |
| 433 | } | |
| 434 | } | |
| 435 | } | |
| 436 | } | |
| 437 | } | |
| 438 | } | |
| 439 | } | |
| 440 | } | |
| 441 | } | |
| 442 | } | |
| 443 | ui::Event::Wheel(delta) => { | |
| 444 | if self.pointer_local[0] < SIDEBAR_W || modifiers.shift_key() { | |
| 445 | let max_scroll = (self.rows.len() as f32 * ROW_H - (self.size[1] - self.tracks_top() - FOOTER_H)).max(0.0); | |
| 446 | self.scroll_y = (self.scroll_y - delta[1]).clamp(0.0, max_scroll); | |
| 447 | } else { | |
| 448 | let f = f64::from((1.0 - delta[1] * 0.0045).clamp(0.7, 1.4)); | |
| 449 | let p = self.x_to_time(self.pointer_local[0]); | |
| 450 | let t0 = p - (p - self.t_range[0]) * f; | |
| 451 | let t1 = p + (self.t_range[1] - p) * f; | |
| 452 | if (t1 - t0) > 4.0 && (t1 - t0) < 100_000.0 { | |
| 453 | self.t_range = [t0, t1]; | |
| 454 | } | |
| 455 | } | |
| 456 | } | |
| 457 | ui::Event::KeyInput { physical, key, state: ElementState::Pressed, repeat: false, modifiers: kmod, .. } => { | |
| 458 | if physical == PhysicalKey::Code(KeyCode::NumpadDecimal) { | |
| 459 | self.fit_comp(comp); | |
| 460 | } else { | |
| 461 | self.handle_key(comp, history, &key, kmod, &mut actions); | |
| 462 | } | |
| 463 | } | |
| 464 | ui::Event::Key { key, modifiers: kmod, .. } => { | |
| 465 | self.handle_key(comp, history, &key, kmod, &mut actions); | |
| 466 | } | |
| 467 | _ => {} | |
| 468 | } | |
| 469 | ||
| 470 | actions | |
| 471 | } | |
| 472 | ||
| 473 | fn handle_key( | |
| 474 | &mut self, | |
| 475 | comp: &mut Comp, | |
| 476 | history: &mut CompHistory, | |
| 477 | key: &Key, | |
| 478 | kmod: ModifiersState, | |
| 479 | actions: &mut Vec<KeyframesAction>, | |
| 480 | ) { | |
| 481 | match key { | |
| 482 | Key::Named(NamedKey::Delete | NamedKey::Backspace) => { | |
| 483 | if self.delete_selected(comp, history) { | |
| 484 | actions.push(KeyframesAction::KeyframesEdited); | |
| 485 | } | |
| 486 | } | |
| 487 | Key::Character(c) => match c.to_ascii_lowercase().as_str() { | |
| 488 | "f" | "." => self.fit_comp(comp), | |
| 489 | "x" if !kmod.super_key() => { | |
| 490 | if self.delete_selected(comp, history) { | |
| 491 | actions.push(KeyframesAction::KeyframesEdited); | |
| 492 | } | |
| 493 | } | |
| 494 | _ => {} | |
| 495 | }, | |
| 496 | _ => {} | |
| 497 | } | |
| 498 | } | |
| 499 | ||
| 500 | pub fn prepare_labels(&mut self) { | |
| 501 | let font_sm = text::Font { | |
| 502 | families: Arc::from([text::Family::Mono]), | |
| 503 | size: 10.0, | |
| 504 | weight: 500, | |
| 505 | italic: false, | |
| 506 | }; | |
| 507 | let font_row = text::Font { | |
| 508 | families: Arc::from([text::Family::Sans]), | |
| 509 | size: 11.0, | |
| 510 | weight: 500, | |
| 511 | italic: false, | |
| 512 | }; | |
| 513 | let font_hdr = text::Font { | |
| 514 | families: Arc::from([text::Family::Sans]), | |
| 515 | size: 11.0, | |
| 516 | weight: 600, | |
| 517 | italic: false, | |
| 518 | }; | |
| 519 | let make = |s: &str, font: &text::Font| { | |
| 520 | let styles = [text::Style::new(font.clone())]; | |
| 521 | text::Paragraph { | |
| 522 | text: s, | |
| 523 | styles: &styles, | |
| 524 | spans: &[], | |
| 525 | replacements: &[], | |
| 526 | width: None, | |
| 527 | indent: 0.0, | |
| 528 | line_height: 1.2, | |
| 529 | align: text::Align::Left, | |
| 530 | tab: 32.0, | |
| 531 | } | |
| 532 | .layout() | |
| 533 | }; | |
| 534 | ||
| 535 | for (k, txt, f) in [ | |
| 536 | ("hdr:title", "Keyframes", &font_hdr), | |
| 537 | ("hdr:all", "All", &font_sm), | |
| 538 | ("hdr:sel", "Sel", &font_sm), | |
| 539 | ("hdr:audio", "Audio", &font_row), | |
| 540 | ("footer", "Drag keys/spans to retime · Ruler: Scrub · Wheel: Zoom · Del: Remove · F: Fit", &font_sm), | |
| 541 | ] { | |
| 542 | self.text_cache.entry(k.to_owned()).or_insert_with(|| make(txt, f)); | |
| 543 | } | |
| 544 | ||
| 545 | for row in &self.rows { | |
| 546 | match row { | |
| 547 | TrackRow::NodeHeader { node_id, label, .. } => { | |
| 548 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 549 | let text = format!("{glyph} {label}"); | |
| 550 | self.text_cache.entry(format!("nh:{text}")).or_insert_with(|| make(&text, &font_hdr)); | |
| 551 | } | |
| 552 | TrackRow::Channel { label, .. } => { | |
| 553 | self.text_cache.entry(format!("ch:{label}")).or_insert_with(|| make(label, &font_row)); | |
| 554 | } | |
| 555 | } | |
| 556 | } | |
| 557 | ||
| 558 | let plot = self.plot_rect(); | |
| 559 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 560 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 561 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 562 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 563 | for ti in t_start..=t_end { | |
| 564 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); | |
| 565 | self.text_cache.entry(key.clone()).or_insert_with(|| make(&key[2..], &font_sm)); | |
| 566 | } | |
| 567 | } | |
| 568 | ||
| 569 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { | |
| 570 | let mut out = Vec::with_capacity(512); | |
| 571 | let [w, h] = self.size; | |
| 572 | let plot = self.plot_rect(); | |
| 573 | let top = self.tracks_top(); | |
| 574 | ||
| 575 | // Sidebar + Ruler chrome | |
| 576 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); | |
| 577 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); | |
| 578 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); | |
| 579 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); | |
| 580 | ||
| 581 | if let Some(tl) = self.text_cache.get("hdr:title") { | |
| 582 | out.push(Primitive::Text { | |
| 583 | text: tl as &dyn Glyphs, | |
| 584 | origin: [10.0, 6.0], | |
| 585 | clip: Some([8.0, 0.0, SIDEBAR_W - 52.0, RULER_H]), | |
| 586 | ink: self.palette.text, | |
| 587 | }); | |
| 588 | } | |
| 589 | // "All / Sel" filter pill | |
| 590 | let pill_rect = [SIDEBAR_W - 46.0, 4.0, SIDEBAR_W - 8.0, RULER_H - 4.0]; | |
| 591 | out.push(Primitive::RoundedRect { | |
| 592 | rect: pill_rect, | |
| 593 | radius: 4.0, | |
| 594 | stroke: None, | |
| 595 | color: if self.selected_only { | |
| 596 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] | |
| 597 | } else { | |
| 598 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.7] | |
| 599 | }, | |
| 600 | }); | |
| 601 | out.push(Primitive::RoundedRect { | |
| 602 | rect: pill_rect, | |
| 603 | radius: 4.0, | |
| 604 | stroke: Some(1.0), | |
| 605 | color: if self.selected_only { self.palette.accent } else { self.palette.rule }, | |
| 606 | }); | |
| 607 | let mode_key = if self.selected_only { "hdr:sel" } else { "hdr:all" }; | |
| 608 | if let Some(ml) = self.text_cache.get(mode_key) { | |
| 609 | out.push(Primitive::Text { | |
| 610 | text: ml as &dyn Glyphs, | |
| 611 | origin: [pill_rect[0] + 9.0, 7.0], | |
| 612 | clip: Some(pill_rect), | |
| 613 | ink: if self.selected_only { self.palette.text } else { self.palette.text_dim }, | |
| 614 | }); | |
| 615 | } | |
| 616 | ||
| 617 | // Ruler frame ticks | |
| 618 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); | |
| 619 | let t_step = nice_step((self.t_range[1] - self.t_range[0]) / max_ticks); | |
| 620 | let t_start = (self.t_range[0] / t_step).floor() as i64; | |
| 621 | let t_end = (self.t_range[1] / t_step).ceil() as i64; | |
| 622 | for ti in t_start..=t_end { | |
| 623 | let t = ti as f64 * t_step; | |
| 624 | let x = self.time_to_x(t); | |
| 625 | if x < plot[0] || x > plot[2] { | |
| 626 | continue; | |
| 627 | } | |
| 628 | out.push(Primitive::Segment { from: [x, RULER_H], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); | |
| 629 | let key = format!("t:{}", t.round() as i64); | |
| 630 | if let Some(tl) = self.text_cache.get(&key) { | |
| 631 | out.push(Primitive::Text { | |
| 632 | text: tl as &dyn Glyphs, | |
| 633 | origin: [x + 3.0, 5.0], | |
| 634 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), | |
| 635 | ink: self.palette.text_dim, | |
| 636 | }); | |
| 637 | } | |
| 638 | } | |
| 639 | ||
| 640 | // Green RAM cache bar along bottom edge of ruler | |
| 641 | for &(c0, c1) in &self.cached_ranges { | |
| 642 | let x0 = self.time_to_x(c0 as f64).max(plot[0]); | |
| 643 | let x1 = self.time_to_x((c1 + 1) as f64).min(plot[2]); | |
| 644 | if x1 > x0 { | |
| 645 | out.push(Primitive::Rect { | |
| 646 | rect: [x0, RULER_H - 3.5, x1.max(x0 + 2.0), RULER_H - 0.5], | |
| 647 | color: [0.22, 0.86, 0.48, 0.92], | |
| 648 | }); | |
| 649 | } | |
| 650 | } | |
| 651 | ||
| 652 | // Dedicated Audio Waveform row when audio is present | |
| 653 | if !self.audio_peaks.is_empty() { | |
| 654 | let ay0 = RULER_H; | |
| 655 | let ay1 = RULER_H + AUDIO_ROW_H; | |
| 656 | out.push(Primitive::Rect { rect: [0.0, ay0, w, ay1], color: self.palette.row_alt }); | |
| 657 | out.push(Primitive::Segment { from: [0.0, ay1], to: [w, ay1], width: 1.0, color: self.palette.rule }); | |
| 658 | if let Some(al) = self.text_cache.get("hdr:audio") { | |
| 659 | out.push(Primitive::Text { | |
| 660 | text: al as &dyn Glyphs, | |
| 661 | origin: [12.0, ay0 + 5.0], | |
| 662 | clip: Some([8.0, ay0, SIDEBAR_W - 8.0, ay1]), | |
| 663 | ink: [0.54, 0.80, 1.0, 0.95], | |
| 664 | }); | |
| 665 | } | |
| 666 | out.push(Primitive::PushClip { rect: [plot[0], ay0, plot[2], ay1], radius: 0.0 }); | |
| 667 | let mid_y = 0.5 * (ay0 + ay1); | |
| 668 | let max_half = AUDIO_ROW_H * 0.42; | |
| 669 | let a0 = self.audio_range[0]; | |
| 670 | let n = self.audio_peaks.len() as i64; | |
| 671 | let f_vis0 = (self.t_range[0].floor() as i64).max(a0); | |
| 672 | let f_vis1 = (self.t_range[1].ceil() as i64).min(a0 + n - 1); | |
| 673 | let bar_step = ((plot[2] - plot[0]) / ((self.t_range[1] - self.t_range[0]).max(1.0) as f32)).max(1.5); | |
| 674 | let stride = ((1.8 / bar_step).ceil() as i64).max(1); | |
| 675 | let mut f = f_vis0; | |
| 676 | while f <= f_vis1 { | |
| 677 | let mut peak = 0.0_f32; | |
| 678 | for off in 0..stride { | |
| 679 | let idx = (f + off - a0) as usize; | |
| 680 | if let Some(&v) = self.audio_peaks.get(idx) { | |
| 681 | if v > peak { peak = v; } | |
| 682 | } | |
| 683 | } | |
| 684 | if peak > 0.01 { | |
| 685 | let x = self.time_to_x(f as f64); | |
| 686 | let hh = (peak * max_half).max(1.0); | |
| 687 | let bw = (bar_step * stride as f32 - 0.6).max(1.2); | |
| 688 | out.push(Primitive::Rect { | |
| 689 | rect: [x, mid_y - hh, x + bw, mid_y + hh], | |
| 690 | color: [0.46, 0.74, 0.98, 0.55], | |
| 691 | }); | |
| 692 | } | |
| 693 | f += stride; | |
| 694 | } | |
| 695 | out.push(Primitive::PopClip); | |
| 696 | } | |
| 697 | ||
| 698 | // Render range subtle shading | |
| 699 | let rx0 = self.time_to_x(comp.render_range[0] as f64); | |
| 700 | let rx1 = self.time_to_x(comp.render_range[1] as f64); | |
| 701 | out.push(Primitive::PushClip { rect: [plot[0], top, plot[2], plot[3]], radius: 0.0 }); | |
| 702 | if rx0 > plot[0] { | |
| 703 | out.push(Primitive::Rect { rect: [plot[0], top, rx0.min(plot[2]), plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 704 | } | |
| 705 | if rx1 < plot[2] { | |
| 706 | out.push(Primitive::Rect { rect: [rx1.max(plot[0]), top, plot[2], plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); | |
| 707 | } | |
| 708 | out.push(Primitive::PopClip); | |
| 709 | ||
| 710 | // Track rows (clipped to track area) | |
| 711 | out.push(Primitive::PushClip { rect: [0.0, top, w, plot[3]], radius: 0.0 }); | |
| 712 | for (ri, row) in self.rows.iter().enumerate() { | |
| 713 | let y0 = self.row_y(ri); | |
| 714 | let y1 = y0 + ROW_H; | |
| 715 | if y1 < top || y0 > plot[3] { | |
| 716 | continue; | |
| 717 | } | |
| 718 | if ri % 2 == 1 { | |
| 719 | out.push(Primitive::Rect { rect: [0.0, y0, w, y1], color: self.palette.row_alt }); | |
| 720 | } | |
| 721 | out.push(Primitive::Segment { from: [0.0, y1], to: [w, y1], width: 1.0, color: self.palette.grid }); | |
| 722 | ||
| 723 | match row { | |
| 724 | TrackRow::NodeHeader { node_id, label, splines } => { | |
| 725 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; | |
| 726 | let key = format!("nh:{glyph} {label}"); | |
| 727 | if let Some(tl) = self.text_cache.get(&key) { | |
| 728 | out.push(Primitive::Text { | |
| 729 | text: tl as &dyn Glyphs, | |
| 730 | origin: [10.0, y0 + 4.0], | |
| 731 | clip: Some([6.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 732 | ink: self.palette.text, | |
| 733 | }); | |
| 734 | } | |
| 735 | // Summary span + tick dots on node header row | |
| 736 | let mut t_min = f64::INFINITY; | |
| 737 | let mut t_max = f64::NEG_INFINITY; | |
| 738 | for sname in splines { | |
| 739 | if let Some(sp) = comp.splines.get(sname) { | |
| 740 | for kf in &sp.keyframes { | |
| 741 | t_min = t_min.min(kf.time); | |
| 742 | t_max = t_max.max(kf.time); | |
| 743 | } | |
| 744 | } | |
| 745 | } | |
| 746 | if t_min.is_finite() { | |
| 747 | let sx0 = self.time_to_x(t_min).max(plot[0]); | |
| 748 | let sx1 = self.time_to_x(t_max).min(plot[2]); | |
| 749 | if sx1 >= sx0 { | |
| 750 | out.push(Primitive::RoundedRect { | |
| 751 | rect: [sx0, y0 + 6.0, sx1.max(sx0 + 4.0), y1 - 6.0], | |
| 752 | radius: 3.0, | |
| 753 | stroke: None, | |
| 754 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.22], | |
| 755 | }); | |
| 756 | } | |
| 757 | } | |
| 758 | } | |
| 759 | TrackRow::Channel { label, spline, color, .. } => { | |
| 760 | out.push(Primitive::RoundedRect { | |
| 761 | rect: [20.0, y0 + 7.0, 28.0, y0 + 15.0], | |
| 762 | radius: 2.0, | |
| 763 | stroke: None, | |
| 764 | color: *color, | |
| 765 | }); | |
| 766 | if let Some(tl) = self.text_cache.get(&format!("ch:{label}")) { | |
| 767 | out.push(Primitive::Text { | |
| 768 | text: tl as &dyn Glyphs, | |
| 769 | origin: [34.0, y0 + 4.0], | |
| 770 | clip: Some([32.0, y0, SIDEBAR_W - 6.0, y1]), | |
| 771 | ink: self.palette.text_dim, | |
| 772 | }); | |
| 773 | } | |
| 774 | ||
| 775 | if let Some(sp) = comp.splines.get(spline) { | |
| 776 | if sp.keyframes.len() >= 2 { | |
| 777 | let sx0 = self.time_to_x(sp.keyframes.first().unwrap().time).max(plot[0]); | |
| 778 | let sx1 = self.time_to_x(sp.keyframes.last().unwrap().time).min(plot[2]); | |
| 779 | if sx1 > sx0 { | |
| 780 | out.push(Primitive::RoundedRect { | |
| 781 | rect: [sx0, y0 + 8.0, sx1, y1 - 8.0], | |
| 782 | radius: 2.0, | |
| 783 | stroke: None, | |
| 784 | color: [color[0], color[1], color[2], 0.28], | |
| 785 | }); | |
| 786 | } | |
| 787 | } | |
| 788 | let cy = y0 + ROW_H * 0.5; | |
| 789 | for (ki, kf) in sp.keyframes.iter().enumerate() { | |
| 790 | let kx = self.time_to_x(kf.time); | |
| 791 | if kx < plot[0] - 8.0 || kx > plot[2] + 8.0 { | |
| 792 | continue; | |
| 793 | } | |
| 794 | let kref = DopeKeyRef { spline: spline.clone(), key_idx: ki }; | |
| 795 | let sel = self.selected_keys.contains(&kref); | |
| 796 | let hov = self.hovered_key.as_ref() == Some(&kref); | |
| 797 | let r = if sel || hov { 5.0 } else { 4.0 }; | |
| 798 | out.push(Primitive::RoundedRect { | |
| 799 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 800 | radius: 1.5, | |
| 801 | stroke: None, | |
| 802 | color: if sel { | |
| 803 | [1.0, 0.84, 0.24, 1.0] | |
| 804 | } else if hov { | |
| 805 | [1.0, 1.0, 1.0, 1.0] | |
| 806 | } else { | |
| 807 | *color | |
| 808 | }, | |
| 809 | }); | |
| 810 | out.push(Primitive::RoundedRect { | |
| 811 | rect: [kx - r, cy - r, kx + r, cy + r], | |
| 812 | radius: 1.5, | |
| 813 | stroke: Some(1.0), | |
| 814 | color: [0.04, 0.04, 0.06, 0.9], | |
| 815 | }); | |
| 816 | } | |
| 817 | } | |
| 818 | } | |
| 819 | } | |
| 820 | } | |
| 821 | ||
| 822 | // Box select overlay | |
| 823 | if let Some((a, b)) = self.box_select { | |
| 824 | let x0 = a[0].min(b[0]); | |
| 825 | let x1 = a[0].max(b[0]); | |
| 826 | let y0 = a[1].min(b[1]); | |
| 827 | let y1 = a[1].max(b[1]); | |
| 828 | out.push(Primitive::RoundedRect { | |
| 829 | rect: [x0, y0, x1, y1], | |
| 830 | radius: 2.0, | |
| 831 | stroke: None, | |
| 832 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], | |
| 833 | }); | |
| 834 | out.push(Primitive::RoundedRect { | |
| 835 | rect: [x0, y0, x1, y1], | |
| 836 | radius: 2.0, | |
| 837 | stroke: Some(1.2), | |
| 838 | color: self.palette.accent, | |
| 839 | }); | |
| 840 | } | |
| 841 | out.push(Primitive::PopClip); | |
| 842 | ||
| 843 | // Playhead vertical line | |
| 844 | let px = self.time_to_x(current_frame); | |
| 845 | if px >= plot[0] && px <= plot[2] { | |
| 846 | out.push(Primitive::Segment { | |
| 847 | from: [px, 0.0], | |
| 848 | to: [px, plot[3]], | |
| 849 | width: 1.6, | |
| 850 | color: [0.96, 0.36, 0.34, 0.92], | |
| 851 | }); | |
| 852 | } | |
| 853 | ||
| 854 | // Footer bar | |
| 855 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - FOOTER_H, w, h], color: self.palette.ruler }); | |
| 856 | if let Some(fl) = self.text_cache.get("footer") { | |
| 857 | out.push(Primitive::Text { | |
| 858 | text: fl as &dyn Glyphs, | |
| 859 | origin: [SIDEBAR_W + 10.0, h - FOOTER_H + 5.0], | |
| 860 | clip: Some([SIDEBAR_W + 6.0, h - FOOTER_H, w - 6.0, h]), | |
| 861 | ink: self.palette.text_dim, | |
| 862 | }); | |
| 863 | } | |
| 864 | ||
| 865 | out | |
| 866 | } | |
| 867 | } | |
| 868 | ||
| 869 | pub fn build_tracks( | |
| 870 | comp: &Comp, | |
| 871 | selected_node: Option<NodeId>, | |
| 872 | selected_only: bool, | |
| 873 | collapsed: &HashSet<NodeId>, | |
| 874 | ) -> Vec<TrackRow> { | |
| 875 | let mut rows = Vec::new(); | |
| 876 | let palette: &[[f32; 4]] = &[ | |
| 877 | [0.98, 0.44, 0.62, 1.0], | |
| 878 | [0.36, 0.84, 0.96, 1.0], | |
| 879 | [0.50, 0.92, 0.46, 1.0], | |
| 880 | [0.98, 0.78, 0.28, 1.0], | |
| 881 | [0.74, 0.56, 0.98, 1.0], | |
| 882 | ]; | |
| 883 | let mut color_idx = 0usize; | |
| 884 | ||
| 885 | let iter_ids: Vec<NodeId> = if selected_only { | |
| 886 | selected_node.into_iter().collect() | |
| 887 | } else { | |
| 888 | comp.nodes.iter().map(|n| n.id).collect() | |
| 889 | }; | |
| 890 | ||
| 891 | for id in iter_ids { | |
| 892 | if id.0 >= comp.nodes.len() { | |
| 893 | continue; | |
| 894 | } | |
| 895 | let channels: Vec<(String, String)> = comp | |
| 896 | .node_splines(id) | |
| 897 | .into_iter() | |
| 898 | .filter(|(_, sname)| !sname.contains("LUTIn")) | |
| 899 | .collect(); | |
| 900 | if channels.is_empty() { | |
| 901 | continue; | |
| 902 | } | |
| 903 | let node = comp.node(id); | |
| 904 | let spline_names: Vec<String> = channels.iter().map(|(_, s)| s.clone()).collect(); | |
| 905 | rows.push(TrackRow::NodeHeader { | |
| 906 | node_id: id, | |
| 907 | label: node.name.clone(), | |
| 908 | splines: spline_names, | |
| 909 | }); | |
| 910 | if !collapsed.contains(&id) { | |
| 911 | for (label, spline) in channels { | |
| 912 | let col = comp | |
| 913 | .splines | |
| 914 | .get(&spline) | |
| 915 | .map(|sp| { | |
| 916 | let [r, g, b] = sp.color; | |
| 917 | if r as u16 + g as u16 + b as u16 > 120 { | |
| 918 | [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0] | |
| 919 | } else { | |
| 920 | palette[color_idx % palette.len()] | |
| 921 | } | |
| 922 | }) | |
| 923 | .unwrap_or_else(|| palette[color_idx % palette.len()]); | |
| 924 | color_idx += 1; | |
| 925 | rows.push(TrackRow::Channel { | |
| 926 | node_id: id, | |
| 927 | label, | |
| 928 | spline, | |
| 929 | color: col, | |
| 930 | }); | |
| 931 | } | |
| 932 | } | |
| 933 | } | |
| 934 | rows | |
| 935 | } | |
| 936 | ||
| 937 | pub fn retime_selected_keys( | |
| 938 | comp: &mut Comp, | |
| 939 | saved_splines: &HashMap<String, Vec<Keyframe>>, | |
| 940 | selected: &HashSet<DopeKeyRef>, | |
| 941 | dt: f64, | |
| 942 | ) { | |
| 943 | for (sname, orig) in saved_splines { | |
| 944 | if let Some(sp) = comp.splines.get_mut(sname) { | |
| 945 | sp.keyframes = orig.clone(); | |
| 946 | } | |
| 947 | } | |
| 948 | for kref in selected { | |
| 949 | let Some(sp) = comp.splines.get_mut(&kref.spline) else { continue }; | |
| 950 | let Some(kf) = sp.keyframes.get_mut(kref.key_idx) else { continue }; | |
| 951 | kf.time += dt; | |
| 952 | if let Some(lh) = &mut kf.lh { | |
| 953 | lh[0] += dt; | |
| 954 | } | |
| 955 | if let Some(rh) = &mut kf.rh { | |
| 956 | rh[0] += dt; | |
| 957 | } | |
| 958 | } | |
| 959 | comp.revision += 1; | |
| 960 | } | |
| 961 | ||
| 962 | fn nice_step(raw: f64) -> f64 { | |
| 963 | let raw = raw.max(1.0); | |
| 964 | let pow = 10.0_f64.powf(raw.log10().floor()); | |
| 965 | let frac = raw / pow; | |
| 966 | 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 }; | |
| 967 | (nice * pow).max(1.0) | |
| 968 | } | |
| 969 | ||
| 970 | #[cfg(test)] | |
| 971 | mod tests { | |
| 972 | use super::*; | |
| 973 | ||
| 974 | fn sample_animated_comp() -> Comp { | |
| 975 | let src = r#"Composition { | |
| 976 | CurrentTime = 10, | |
| 977 | RenderRange = { 0, 100 }, | |
| 978 | GlobalRange = { 0, 100 }, | |
| 979 | Tools = ordered() { | |
| 980 | XF1 = Transform { | |
| 981 | Inputs = { | |
| 982 | Size = Input { SourceOp = "XF1Size", Source = "Value", }, | |
| 983 | }, | |
| 984 | }, | |
| 985 | XF1Size = BezierSpline { | |
| 986 | SplineColor = { Red = 220, Green = 120, Blue = 80 }, | |
| 987 | KeyFrames = { | |
| 988 | [10] = { 1.0, RH = { 20.0, 1.0 }, Flags = { Linear = false } }, | |
| 989 | [40] = { 2.0, LH = { 30.0, 2.0 }, Flags = { Linear = false } }, | |
| 990 | }, | |
| 991 | }, | |
| 992 | }, | |
| 993 | }"#; | |
| 994 | Comp::from_fusion_str(src).expect("parse animated comp") | |
| 995 | } | |
| 996 | ||
| 997 | #[test] | |
| 998 | fn build_tracks_lists_animated_nodes_and_channels() { | |
| 999 | let comp = sample_animated_comp(); | |
| 1000 | let xf = comp.find_node("XF1").unwrap(); | |
| 1001 | let rows = build_tracks(&comp, Some(xf), false, &HashSet::new()); | |
| 1002 | assert_eq!(rows.len(), 2); | |
| 1003 | assert!(matches!(&rows[0], TrackRow::NodeHeader { label, .. } if label == "XF1")); | |
| 1004 | assert!(matches!(&rows[1], TrackRow::Channel { spline, .. } if spline == "XF1Size")); | |
| 1005 | ||
| 1006 | let mut collapsed = HashSet::new(); | |
| 1007 | collapsed.insert(xf); | |
| 1008 | let collapsed_rows = build_tracks(&comp, Some(xf), false, &collapsed); | |
| 1009 | assert_eq!(collapsed_rows.len(), 1); | |
| 1010 | } | |
| 1011 | ||
| 1012 | #[test] | |
| 1013 | fn retime_shifts_key_and_handles_together_with_undo() { | |
| 1014 | let mut comp = sample_animated_comp(); | |
| 1015 | let mut history = CompHistory::new(); | |
| 1016 | let pre = comp.clone(); | |
| 1017 | let mut saved = HashMap::new(); | |
| 1018 | saved.insert("XF1Size".to_owned(), comp.splines["XF1Size"].keyframes.clone()); | |
| 1019 | let mut sel = HashSet::new(); | |
| 1020 | sel.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 1 }); | |
| 1021 | ||
| 1022 | retime_selected_keys(&mut comp, &saved, &sel, 15.0); | |
| 1023 | history.push_snapshot(&pre, "Retime Keyframes"); | |
| 1024 | ||
| 1025 | let kf1 = &comp.splines["XF1Size"].keyframes[1]; | |
| 1026 | assert!((kf1.time - 55.0).abs() < 1e-6); | |
| 1027 | assert!((kf1.lh.unwrap()[0] - 45.0).abs() < 1e-6, "left handle must shift with key"); | |
| 1028 | ||
| 1029 | assert!(history.undo(&mut comp)); | |
| 1030 | assert!((comp.splines["XF1Size"].keyframes[1].time - 40.0).abs() < 1e-6); | |
| 1031 | } | |
| 1032 | } |
crates/studio/src/main.rs deleted-1857| ... | ... | @@ -1,1857 +0,0 @@ |
| 1 | //! Compositor Studio — docked zero-copy `wgpu` Multi-Viewer (`1`/`2`) + Flow + Blender-style Spline Editor | |
| 2 | //! built on Knife's `draw`, `ui`, `dock`, and `theme::KNIFE` (soft toned quiet iris). | |
| 3 | ||
| 4 | mod add_tool; | |
| 5 | mod inspector; | |
| 6 | mod keyframes; | |
| 7 | ||
| 8 | use add_tool::{AddToolLauncher, ToolEntry}; | |
| 9 | use dock::{Axis as DockAxis, Command, Dock, Event, GroupId, Node as DockNode, Part as DockPart, Place, Plus, Side, SplitId, Tab}; | |
| 10 | use engine::{ | |
| 11 | AudioPlayer, AudioTrack, CacheMode, DEFAULT_RAM_CACHE_MB, Engine, ExportOptions, PrefetchJob, | |
| 12 | RamFrameCache, RamPrefetcher, export_saver_png_sequence, | |
| 13 | }; | |
| 14 | use flow::{FlowAction, FlowView}; | |
| 15 | use inspector::{InspectorAction, InspectorState}; | |
| 16 | use keyframes::{KeyframesAction, KeyframesView}; | |
| 17 | use model::{Comp, CompHistory, NodeId, default_main_input_port}; | |
| 18 | use spline::{SplineAction, SplineView}; | |
| 19 | use std::collections::HashMap; | |
| 20 | use std::error::Error; | |
| 21 | use std::fs::File; | |
| 22 | use std::path::{Path, PathBuf}; | |
| 23 | use std::process::Command as ProcessCommand; | |
| 24 | use std::sync::{Arc, mpsc}; | |
| 25 | use std::time::{Duration, Instant}; | |
| 26 | use ui::host::{Accessibility, Clipboard, Surface}; | |
| 27 | use viewer::{PlaybackClock, SyncMode, ViewerAction, ViewerState}; | |
| 28 | use winit::application::ApplicationHandler; | |
| 29 | use winit::dpi::LogicalSize; | |
| 30 | use winit::event::{ElementState, WindowEvent}; | |
| 31 | use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop, EventLoopProxy}; | |
| 32 | use winit::keyboard::{Key, KeyCode, NamedKey, PhysicalKey}; | |
| 33 | use winit::window::{CursorIcon, Window, WindowId}; | |
| 34 | ||
| 35 | const DEFAULT_COMP: &str = "/Volumes/Project/trees/fusion/20_falling.comp"; | |
| 36 | const PANES: &[&str] = &["Viewer 1", "Viewer 2", "Flow", "Spline", "Keyframes", "Inspector"]; | |
| 37 | ||
| 38 | fn viewer_pane_id(slot: usize) -> ui::Id { ui::Id::ROOT.child(("pane-viewer", slot)) } | |
| 39 | fn viewer_canvas_id(slot: usize) -> ui::Id { ui::Id::ROOT.child(("canvas-viewer", slot)) } | |
| 40 | fn flow_pane_id() -> ui::Id { ui::Id::ROOT.child("pane-flow") } | |
| 41 | fn flow_canvas_id() -> ui::Id { ui::Id::ROOT.child("canvas-flow") } | |
| 42 | fn spline_canvas_id() -> ui::Id { ui::Id::ROOT.child("canvas-spline") } | |
| 43 | fn keyframes_canvas_id() -> ui::Id { ui::Id::ROOT.child("canvas-keyframes") } | |
| 44 | fn inspector_pane_id() -> ui::Id { ui::Id::ROOT.child("pane-inspector") } | |
| 45 | ||
| 46 | struct Cli { | |
| 47 | comp_path: PathBuf, | |
| 48 | snapshot: Option<PathBuf>, | |
| 49 | frame: Option<f64>, | |
| 50 | select_node: Option<String>, | |
| 51 | dual_viewer: bool, | |
| 52 | open_add_tool: bool, | |
| 53 | show_keyframes: bool, | |
| 54 | } | |
| 55 | ||
| 56 | fn parse_cli() -> Cli { | |
| 57 | let args: Vec<String> = std::env::args().collect(); | |
| 58 | let mut comp_path = PathBuf::from(DEFAULT_COMP); | |
| 59 | let mut snapshot = None; | |
| 60 | let mut frame = None; | |
| 61 | let mut select_node = None; | |
| 62 | let mut dual_viewer = false; | |
| 63 | let mut open_add_tool = false; | |
| 64 | let mut show_keyframes = false; | |
| 65 | let mut i = 1; | |
| 66 | while i < args.len() { | |
| 67 | match args[i].as_str() { | |
| 68 | "--snapshot" if i + 1 < args.len() => { | |
| 69 | snapshot = Some(PathBuf::from(&args[i + 1])); | |
| 70 | i += 2; | |
| 71 | } | |
| 72 | "--frame" if i + 1 < args.len() => { | |
| 73 | frame = args[i + 1].parse().ok(); | |
| 74 | i += 2; | |
| 75 | } | |
| 76 | "--select" if i + 1 < args.len() => { | |
| 77 | select_node = Some(args[i + 1].clone()); | |
| 78 | i += 2; | |
| 79 | } | |
| 80 | "--dual" => { | |
| 81 | dual_viewer = true; | |
| 82 | i += 1; | |
| 83 | } | |
| 84 | "--open-add-tool" => { | |
| 85 | open_add_tool = true; | |
| 86 | i += 1; | |
| 87 | } | |
| 88 | "--show-keyframes" => { | |
| 89 | show_keyframes = true; | |
| 90 | i += 1; | |
| 91 | } | |
| 92 | other if !other.starts_with("--") => { | |
| 93 | comp_path = PathBuf::from(other); | |
| 94 | i += 1; | |
| 95 | } | |
| 96 | _ => i += 1, | |
| 97 | } | |
| 98 | } | |
| 99 | Cli { comp_path, snapshot, frame, select_node, dual_viewer, open_add_tool, show_keyframes } | |
| 100 | } | |
| 101 | ||
| 102 | fn main() { | |
| 103 | let cli = parse_cli(); | |
| 104 | let res = match &cli.snapshot { | |
| 105 | Some(out) => run_snapshot(&cli, out), | |
| 106 | None => run_window(&cli), | |
| 107 | }; | |
| 108 | if let Err(e) = res { | |
| 109 | eprintln!("compositor error: {e}"); | |
| 110 | std::process::exit(1); | |
| 111 | } | |
| 112 | } | |
| 113 | ||
| 114 | struct Studio { | |
| 115 | ui: ui::Ui, | |
| 116 | dock: Dock<String>, | |
| 117 | layout: dock::Layout<String>, | |
| 118 | seen: dock::Seen<String>, | |
| 119 | renderer: draw::Renderer, | |
| 120 | engine: Engine, | |
| 121 | comp: Comp, | |
| 122 | history: CompHistory, | |
| 123 | playback: PlaybackClock, | |
| 124 | enable_audio: bool, | |
| 125 | audio_player: Option<AudioPlayer>, | |
| 126 | audio_rx: Option<mpsc::Receiver<Result<AudioTrack, String>>>, | |
| 127 | status_notice: Option<(String, Instant)>, | |
| 128 | viewers: [ViewerState; 2], | |
| 129 | flow: FlowView, | |
| 130 | spline: SplineView, | |
| 131 | keyframes: KeyframesView, | |
| 132 | inspector: InspectorState, | |
| 133 | add_tool: AddToolLauncher, | |
| 134 | ram_cache: RamFrameCache, | |
| 135 | prefetcher: Option<RamPrefetcher>, | |
| 136 | last_prefetch_sig: Option<(NodeId, i64, u64, u32, bool, bool)>, | |
| 137 | renaming_node: Option<NodeId>, | |
| 138 | rename_buf: String, | |
| 139 | rename_needs_focus: bool, | |
| 140 | size: [f32; 2], | |
| 141 | scale: f32, | |
| 142 | clock: Instant, | |
| 143 | } | |
| 144 | ||
| 145 | impl Studio { | |
| 146 | fn new( | |
| 147 | renderer: draw::Renderer, | |
| 148 | comp: Comp, | |
| 149 | init_frame: Option<f64>, | |
| 150 | select_node: Option<&str>, | |
| 151 | dual_viewer: bool, | |
| 152 | show_keyframes: bool, | |
| 153 | enable_audio: bool, | |
| 154 | ) -> Self { | |
| 155 | let engine = Engine::new(renderer.device().clone(), renderer.queue().clone()); | |
| 156 | // Knife's soft toned Iris quiet-layered theme! | |
| 157 | let ui_theme = ui::Theme::new(&theme::KNIFE, true); | |
| 158 | let mut ui = ui::Ui::new(ui_theme.clone()); | |
| 159 | ui.window_focused = true; | |
| 160 | ||
| 161 | let g_v1 = GroupId(1); | |
| 162 | let g_flow = GroupId(2); | |
| 163 | let g_spline = GroupId(3); | |
| 164 | let g_v2 = GroupId(4); | |
| 165 | let g_inspector = GroupId(5); | |
| 166 | ||
| 167 | let top_node = if dual_viewer { | |
| 168 | DockNode::Split { | |
| 169 | id: SplitId(3), | |
| 170 | axis: DockAxis::X, | |
| 171 | parts: vec![ | |
| 172 | DockPart { | |
| 173 | share: 0.5, | |
| 174 | node: DockNode::Group { id: g_v1, tabs: vec!["Viewer 1".to_owned()], preview: None, rest: Default::default() }, | |
| 175 | rest: Default::default(), | |
| 176 | }, | |
| 177 | DockPart { | |
| 178 | share: 0.5, | |
| 179 | node: DockNode::Group { id: g_v2, tabs: vec!["Viewer 2".to_owned()], preview: None, rest: Default::default() }, | |
| 180 | rest: Default::default(), | |
| 181 | }, | |
| 182 | ], | |
| 183 | rest: Default::default(), | |
| 184 | } | |
| 185 | } else { | |
| 186 | DockNode::Group { id: g_v1, tabs: vec!["Viewer 1".to_owned()], preview: None, rest: Default::default() } | |
| 187 | }; | |
| 188 | ||
| 189 | let left_workspace = DockNode::Split { | |
| 190 | id: SplitId(1), | |
| 191 | axis: DockAxis::Y, | |
| 192 | parts: vec![ | |
| 193 | DockPart { share: 0.54, node: top_node, rest: Default::default() }, | |
| 194 | DockPart { | |
| 195 | share: 0.46, | |
| 196 | node: DockNode::Split { | |
| 197 | id: SplitId(2), | |
| 198 | axis: DockAxis::X, | |
| 199 | parts: vec![ | |
| 200 | DockPart { | |
| 201 | share: 0.52, | |
| 202 | node: DockNode::Group { id: g_flow, tabs: vec!["Flow".to_owned()], preview: None, rest: Default::default() }, | |
| 203 | rest: Default::default(), | |
| 204 | }, | |
| 205 | DockPart { | |
| 206 | share: 0.48, | |
| 207 | node: DockNode::Group { | |
| 208 | id: g_spline, | |
| 209 | tabs: vec!["Spline".to_owned(), "Keyframes".to_owned()], | |
| 210 | preview: None, | |
| 211 | rest: Default::default(), | |
| 212 | }, | |
| 213 | rest: Default::default(), | |
| 214 | }, | |
| 215 | ], | |
| 216 | rest: Default::default(), | |
| 217 | }, | |
| 218 | rest: Default::default(), | |
| 219 | }, | |
| 220 | ], | |
| 221 | rest: Default::default(), | |
| 222 | }; | |
| 223 | ||
| 224 | let layout = dock::Layout { | |
| 225 | root: DockNode::Split { | |
| 226 | id: SplitId(4), | |
| 227 | axis: DockAxis::X, | |
| 228 | parts: vec![ | |
| 229 | DockPart { share: 0.76, node: left_workspace, rest: Default::default() }, | |
| 230 | DockPart { | |
| 231 | share: 0.24, | |
| 232 | node: DockNode::Group { id: g_inspector, tabs: vec!["Inspector".to_owned()], preview: None, rest: Default::default() }, | |
| 233 | rest: Default::default(), | |
| 234 | }, | |
| 235 | ], | |
| 236 | rest: Default::default(), | |
| 237 | }, | |
| 238 | issued: 6, | |
| 239 | rest: Default::default(), | |
| 240 | }; | |
| 241 | let mut seen = dock::Seen::default(); | |
| 242 | seen.active.insert(g_v1, "Viewer 1".to_owned()); | |
| 243 | if dual_viewer { | |
| 244 | seen.active.insert(g_v2, "Viewer 2".to_owned()); | |
| 245 | } | |
| 246 | seen.active.insert(g_flow, "Flow".to_owned()); | |
| 247 | seen.active.insert( | |
| 248 | g_spline, | |
| 249 | if show_keyframes { "Keyframes" } else { "Spline" }.to_owned(), | |
| 250 | ); | |
| 251 | seen.active.insert(g_inspector, "Inspector".to_owned()); | |
| 252 | seen.focused = Some(g_flow); | |
| 253 | let dock = Dock::new(); | |
| 254 | ||
| 255 | let mut playback = PlaybackClock::new(); | |
| 256 | playback.init_from_comp(&comp); | |
| 257 | if let Some(f) = init_frame { | |
| 258 | playback.set_frame(&comp, f, Instant::now()); | |
| 259 | } | |
| 260 | ||
| 261 | let mut v1 = ViewerState::new(0, &ui_theme); | |
| 262 | v1.viewed_node = comp.default_output_node(); | |
| 263 | let mut v2 = ViewerState::new(1, &ui_theme); | |
| 264 | v2.viewed_node = select_node.and_then(|n| comp.find_node(n)).or(v1.viewed_node); | |
| 265 | ||
| 266 | let mut flow = FlowView::new(&ui_theme); | |
| 267 | flow.viewed_nodes = [v1.viewed_node, if dual_viewer { v2.viewed_node } else { None }]; | |
| 268 | ||
| 269 | let default_sel = select_node | |
| 270 | .and_then(|n| comp.find_node(n)) | |
| 271 | .or_else(|| comp.nodes.iter().find(|n| !comp.node_splines(n.id).is_empty()).map(|n| n.id)) | |
| 272 | .or(v1.viewed_node); | |
| 273 | if let Some(id) = default_sel { | |
| 274 | flow.selected.insert(id); | |
| 275 | flow.primary = Some(id); | |
| 276 | } | |
| 277 | ||
| 278 | let mut spline = SplineView::new(&ui_theme); | |
| 279 | spline.set_target_node(&comp, default_sel); | |
| 280 | ||
| 281 | let mut keyframes = KeyframesView::new(&ui_theme); | |
| 282 | keyframes.sync_tracks(&comp, default_sel); | |
| 283 | ||
| 284 | v1.selected_node = default_sel; | |
| 285 | v2.selected_node = default_sel; | |
| 286 | ||
| 287 | let inspector = InspectorState::new(&ui_theme); | |
| 288 | let add_tool = AddToolLauncher::new(); | |
| 289 | let ram_cache = RamFrameCache::new(DEFAULT_RAM_CACHE_MB); | |
| 290 | let prefetcher = if enable_audio { | |
| 291 | Some(RamPrefetcher::new(ram_cache.clone(), engine.vram_budget_mb())) | |
| 292 | } else { | |
| 293 | None | |
| 294 | }; | |
| 295 | ||
| 296 | let (audio_player, audio_rx) = if enable_audio { | |
| 297 | let player = match AudioPlayer::new() { | |
| 298 | Ok(p) => Some(p), | |
| 299 | Err(e) => { | |
| 300 | eprintln!("fission audio disabled: {e}"); | |
| 301 | None | |
| 302 | } | |
| 303 | }; | |
| 304 | let rx = spawn_audio_loader(&comp); | |
| 305 | (player, rx) | |
| 306 | } else { | |
| 307 | (None, None) | |
| 308 | }; | |
| 309 | ||
| 310 | let mut history = CompHistory::new(); | |
| 311 | history.mark_saved(&comp); | |
| 312 | ||
| 313 | Self { | |
| 314 | ui, | |
| 315 | dock, | |
| 316 | layout, | |
| 317 | seen, | |
| 318 | renderer, | |
| 319 | engine, | |
| 320 | comp, | |
| 321 | history, | |
| 322 | playback, | |
| 323 | enable_audio, | |
| 324 | audio_player, | |
| 325 | audio_rx, | |
| 326 | status_notice: None, | |
| 327 | viewers: [v1, v2], | |
| 328 | flow, | |
| 329 | spline, | |
| 330 | keyframes, | |
| 331 | inspector, | |
| 332 | add_tool, | |
| 333 | ram_cache, | |
| 334 | prefetcher, | |
| 335 | last_prefetch_sig: None, | |
| 336 | renaming_node: None, | |
| 337 | rename_buf: String::new(), | |
| 338 | rename_needs_focus: false, | |
| 339 | size: [1440.0, 900.0], | |
| 340 | scale: 2.0, | |
| 341 | clock: Instant::now(), | |
| 342 | } | |
| 343 | } | |
| 344 | ||
| 345 | fn audio_time_secs(&self) -> f64 { | |
| 346 | (self.playback.current_frame - self.comp.audio_offset) / self.comp.fps.max(1.0) | |
| 347 | } | |
| 348 | ||
| 349 | fn poll_audio_load(&mut self) { | |
| 350 | let Some(rx) = &self.audio_rx else { return }; | |
| 351 | match rx.try_recv() { | |
| 352 | Ok(Ok(track)) => { | |
| 353 | eprintln!( | |
| 354 | "Loaded audio {} ({:.2}s @ {}Hz)", | |
| 355 | track.path.display(), | |
| 356 | track.duration_secs, | |
| 357 | track.sample_rate | |
| 358 | ); | |
| 359 | let peaks = track.waveform_peaks( | |
| 360 | self.comp.fps, | |
| 361 | self.comp.audio_offset, | |
| 362 | self.comp.render_range[0], | |
| 363 | self.comp.render_range[1], | |
| 364 | ); | |
| 365 | self.viewers[0].audio_peaks = peaks.clone(); | |
| 366 | self.keyframes.audio_peaks = peaks; | |
| 367 | self.keyframes.audio_range = self.comp.render_range; | |
| 368 | let t = self.audio_time_secs(); | |
| 369 | let playing = self.playback.playing; | |
| 370 | if let Some(player) = &mut self.audio_player { | |
| 371 | player.set_track(Some(Arc::new(track))); | |
| 372 | if playing { | |
| 373 | player.play_from(t); | |
| 374 | } | |
| 375 | } | |
| 376 | self.audio_rx = None; | |
| 377 | } | |
| 378 | Ok(Err(e)) => { | |
| 379 | eprintln!("fission audio load error: {e}"); | |
| 380 | self.audio_rx = None; | |
| 381 | } | |
| 382 | Err(mpsc::TryRecvError::Disconnected) => { | |
| 383 | self.audio_rx = None; | |
| 384 | } | |
| 385 | Err(mpsc::TryRecvError::Empty) => {} | |
| 386 | } | |
| 387 | } | |
| 388 | ||
| 389 | fn sync_audio(&mut self, state_changed: bool, seeked: bool) { | |
| 390 | let t = self.audio_time_secs(); | |
| 391 | let playing = self.playback.playing; | |
| 392 | if let Some(player) = &mut self.audio_player { | |
| 393 | if playing { | |
| 394 | if state_changed || !player.is_playing() { | |
| 395 | player.play_from(t); | |
| 396 | } else if seeked { | |
| 397 | player.seek(t); | |
| 398 | } | |
| 399 | } else if state_changed || player.is_playing() { | |
| 400 | player.pause(); | |
| 401 | } | |
| 402 | } | |
| 403 | } | |
| 404 | ||
| 405 | fn set_notice(&mut self, msg: impl Into<String>) { | |
| 406 | self.status_notice = Some((msg.into(), Instant::now())); | |
| 407 | } | |
| 408 | ||
| 409 | fn invalidate_node(&mut self, node: Option<NodeId>) { | |
| 410 | if let Some(id) = node { | |
| 411 | self.engine.bust_subtree(id); | |
| 412 | } else { | |
| 413 | self.engine.bust_all_caches(); | |
| 414 | } | |
| 415 | self.ram_cache.invalidate_other_revisions(self.comp.revision); | |
| 416 | if let Some(pf) = &self.prefetcher { | |
| 417 | pf.cancel(); | |
| 418 | } | |
| 419 | self.last_prefetch_sig = None; | |
| 420 | for v in &mut self.viewers { | |
| 421 | v.cached_frames.clear(); | |
| 422 | v.dirty = true; | |
| 423 | } | |
| 424 | } | |
| 425 | ||
| 426 | fn sync_ram_ranges_and_prefetch(&mut self) { | |
| 427 | let sel = self.flow.primary; | |
| 428 | for v in &mut self.viewers { | |
| 429 | v.selected_node = sel; | |
| 430 | } | |
| 431 | if let Some(target) = self.viewers[0].viewed_node { | |
| 432 | let ranges = self.ram_cache.cached_frame_ranges( | |
| 433 | target, | |
| 434 | self.comp.revision, | |
| 435 | self.engine.proxy_scale, | |
| 436 | self.engine.hiq, | |
| 437 | self.engine.use_proxy_clips, | |
| 438 | ); | |
| 439 | self.viewers[0].ram_cached_ranges = ranges.clone(); | |
| 440 | self.keyframes.cached_ranges = ranges; | |
| 441 | ||
| 442 | if let Some(pf) = &mut self.prefetcher { | |
| 443 | if self.engine.cache.mode == CacheMode::Enabled { | |
| 444 | let cur_f = self.playback.current_frame.round() as i64; | |
| 445 | let sig = ( | |
| 446 | target, | |
| 447 | cur_f, | |
| 448 | self.comp.revision, | |
| 449 | self.engine.proxy_scale, | |
| 450 | self.engine.hiq, | |
| 451 | self.engine.use_proxy_clips, | |
| 452 | ); | |
| 453 | if self.last_prefetch_sig != Some(sig) { | |
| 454 | self.last_prefetch_sig = Some(sig); | |
| 455 | self.ram_cache.invalidate_other_revisions(self.comp.revision); | |
| 456 | pf.request_lookahead(PrefetchJob { | |
| 457 | comp: self.comp.clone(), | |
| 458 | target, | |
| 459 | center_frame: cur_f, | |
| 460 | range: self.comp.render_range, | |
| 461 | lookahead: 24, | |
| 462 | proxy_scale: self.engine.proxy_scale, | |
| 463 | hiq: self.engine.hiq, | |
| 464 | use_proxy_clips: self.engine.use_proxy_clips, | |
| 465 | }); | |
| 466 | } | |
| 467 | } | |
| 468 | } | |
| 469 | } else { | |
| 470 | self.viewers[0].ram_cached_ranges.clear(); | |
| 471 | self.keyframes.cached_ranges.clear(); | |
| 472 | } | |
| 473 | } | |
| 474 | ||
| 475 | fn undo(&mut self) { | |
| 476 | let label = self.history.undo_label().unwrap_or("Edit").to_owned(); | |
| 477 | if self.history.undo(&mut self.comp) { | |
| 478 | self.flow.sanitize_ids(&self.comp); | |
| 479 | for slot in [0usize, 1] { | |
| 480 | if self.viewers[slot].viewed_node.is_some_and(|id| id.0 >= self.comp.nodes.len()) { | |
| 481 | self.viewers[slot].viewed_node = self.comp.default_output_node(); | |
| 482 | } | |
| 483 | } | |
| 484 | self.flow.viewed_nodes = [self.viewers[0].viewed_node, self.viewers[1].viewed_node]; | |
| 485 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 486 | self.renaming_node = None; | |
| 487 | self.invalidate_node(None); | |
| 488 | self.set_notice(format!("Undo: {label}")); | |
| 489 | } else { | |
| 490 | self.set_notice("Nothing to undo"); | |
| 491 | } | |
| 492 | } | |
| 493 | ||
| 494 | fn redo(&mut self) { | |
| 495 | let label = self.history.redo_label().unwrap_or("Edit").to_owned(); | |
| 496 | if self.history.redo(&mut self.comp) { | |
| 497 | self.flow.sanitize_ids(&self.comp); | |
| 498 | for slot in [0usize, 1] { | |
| 499 | if self.viewers[slot].viewed_node.is_some_and(|id| id.0 >= self.comp.nodes.len()) { | |
| 500 | self.viewers[slot].viewed_node = self.comp.default_output_node(); | |
| 501 | } | |
| 502 | } | |
| 503 | self.flow.viewed_nodes = [self.viewers[0].viewed_node, self.viewers[1].viewed_node]; | |
| 504 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 505 | self.renaming_node = None; | |
| 506 | self.invalidate_node(None); | |
| 507 | self.set_notice(format!("Redo: {label}")); | |
| 508 | } else { | |
| 509 | self.set_notice("Nothing to redo"); | |
| 510 | } | |
| 511 | } | |
| 512 | ||
| 513 | fn save_comp(&mut self) -> Result<String, String> { | |
| 514 | let Some(path) = self.comp.path.clone() else { | |
| 515 | return self.save_comp_as(); | |
| 516 | }; | |
| 517 | self.save_to_path(&path) | |
| 518 | } | |
| 519 | ||
| 520 | fn save_to_path(&mut self, path: &Path) -> Result<String, String> { | |
| 521 | self.comp.current_time = self.playback.current_frame; | |
| 522 | match self.comp.save_to_file(path) { | |
| 523 | Ok(backup) => { | |
| 524 | self.comp.path = Some(path.to_path_buf()); | |
| 525 | if let Some(stem) = path.file_stem().and_then(|s| s.to_str()) { | |
| 526 | self.comp.name = stem.to_owned(); | |
| 527 | } | |
| 528 | self.history.mark_saved(&self.comp); | |
| 529 | let msg = match backup { | |
| 530 | Some(bak) => { | |
| 531 | eprintln!("Saved {} (backup -> {})", path.display(), bak.display()); | |
| 532 | "Saved (backup in temp)".to_owned() | |
| 533 | } | |
| 534 | None => { | |
| 535 | eprintln!("Saved {}", path.display()); | |
| 536 | format!("Saved {}", self.comp.name) | |
| 537 | } | |
| 538 | }; | |
| 539 | self.set_notice(msg.clone()); | |
| 540 | Ok(msg) | |
| 541 | } | |
| 542 | Err(e) => { | |
| 543 | let err = format!("Save failed: {e}"); | |
| 544 | eprintln!("{err}"); | |
| 545 | self.set_notice(err.clone()); | |
| 546 | Err(err) | |
| 547 | } | |
| 548 | } | |
| 549 | } | |
| 550 | ||
| 551 | fn save_comp_as(&mut self) -> Result<String, String> { | |
| 552 | let default_name = format!("{}.comp", self.comp.name); | |
| 553 | let Some(mut path) = native_save_comp_dialog(&default_name) else { | |
| 554 | return Err("Save As cancelled".to_owned()); | |
| 555 | }; | |
| 556 | if path.extension().is_none() { | |
| 557 | path.set_extension("comp"); | |
| 558 | } | |
| 559 | self.save_to_path(&path) | |
| 560 | } | |
| 561 | ||
| 562 | fn open_comp_dialog(&mut self) { | |
| 563 | let Some(path) = native_open_comp_dialog() else { | |
| 564 | return; | |
| 565 | }; | |
| 566 | match Comp::from_fusion_file(&path) { | |
| 567 | Ok(new_comp) => { | |
| 568 | self.reload_comp(new_comp); | |
| 569 | self.set_notice(format!("Opened {}", self.comp.name)); | |
| 570 | } | |
| 571 | Err(e) => { | |
| 572 | let msg = format!("Open failed: {e}"); | |
| 573 | eprintln!("{msg}"); | |
| 574 | self.set_notice(msg); | |
| 575 | } | |
| 576 | } | |
| 577 | } | |
| 578 | ||
| 579 | fn reload_comp(&mut self, new_comp: Comp) { | |
| 580 | if self.playback.playing { | |
| 581 | self.playback.toggle_play(Instant::now()); | |
| 582 | self.sync_audio(true, false); | |
| 583 | } | |
| 584 | self.comp = new_comp; | |
| 585 | self.history.clear(); | |
| 586 | self.history.mark_saved(&self.comp); | |
| 587 | self.engine.bust_all_caches(); | |
| 588 | self.playback.init_from_comp(&self.comp); | |
| 589 | ||
| 590 | let out_node = self.comp.default_output_node(); | |
| 591 | for (slot, v) in self.viewers.iter_mut().enumerate() { | |
| 592 | v.viewed_node = out_node; | |
| 593 | v.cached_frames.clear(); | |
| 594 | v.dirty = true; | |
| 595 | if slot == 0 { | |
| 596 | v.fit(); | |
| 597 | } | |
| 598 | } | |
| 599 | self.flow.reset_for_comp(&self.comp); | |
| 600 | self.flow.viewed_nodes = [self.viewers[0].viewed_node, None]; | |
| 601 | let default_sel = self | |
| 602 | .comp | |
| 603 | .nodes | |
| 604 | .iter() | |
| 605 | .find(|n| !self.comp.node_splines(n.id).is_empty()) | |
| 606 | .map(|n| n.id) | |
| 607 | .or(out_node); | |
| 608 | if let Some(id) = default_sel { | |
| 609 | self.flow.selected.insert(id); | |
| 610 | self.flow.primary = Some(id); | |
| 611 | } | |
| 612 | self.spline.set_target_node(&self.comp, default_sel); | |
| 613 | self.keyframes.audio_peaks.clear(); | |
| 614 | self.keyframes.cached_ranges.clear(); | |
| 615 | self.keyframes.fit_comp(&self.comp); | |
| 616 | self.keyframes.sync_tracks(&self.comp, default_sel); | |
| 617 | self.viewers[0].audio_peaks.clear(); | |
| 618 | self.ram_cache.clear(); | |
| 619 | if let Some(pf) = &self.prefetcher { | |
| 620 | pf.cancel(); | |
| 621 | } | |
| 622 | self.last_prefetch_sig = None; | |
| 623 | self.inspector.reset(); | |
| 624 | self.renaming_node = None; | |
| 625 | ||
| 626 | if self.enable_audio { | |
| 627 | if let Some(player) = &mut self.audio_player { | |
| 628 | player.set_track(None); | |
| 629 | } | |
| 630 | self.audio_rx = spawn_audio_loader(&self.comp); | |
| 631 | } | |
| 632 | } | |
| 633 | ||
| 634 | fn add_tool_from_entry(&mut self, entry: ToolEntry) { | |
| 635 | let prev_sel = self.flow.primary.filter(|id| id.0 < self.comp.nodes.len()); | |
| 636 | let pos = self.flow.spawn_position(&self.comp, entry.is_generator); | |
| 637 | self.history.push_snapshot(&self.comp, format!("Add {}", entry.display)); | |
| 638 | let new_id = self.comp.add_node(entry.kind, None, pos); | |
| 639 | if !entry.is_generator { | |
| 640 | if let Some(src_id) = prev_sel { | |
| 641 | let port = default_main_input_port(entry.kind); | |
| 642 | let _ = self.comp.connect_output(src_id, "Output", new_id, port); | |
| 643 | } | |
| 644 | } | |
| 645 | self.flow.selected.clear(); | |
| 646 | self.flow.selected.insert(new_id); | |
| 647 | self.flow.primary = Some(new_id); | |
| 648 | self.spline.set_target_node(&self.comp, Some(new_id)); | |
| 649 | self.invalidate_node(Some(new_id)); | |
| 650 | self.set_notice(format!("Added {}", self.comp.node(new_id).name)); | |
| 651 | } | |
| 652 | ||
| 653 | fn begin_inline_rename(&mut self, id: NodeId) { | |
| 654 | if id.0 >= self.comp.nodes.len() { | |
| 655 | return; | |
| 656 | } | |
| 657 | self.renaming_node = Some(id); | |
| 658 | self.rename_buf = self.comp.node(id).name.clone(); | |
| 659 | self.rename_needs_focus = true; | |
| 660 | } | |
| 661 | ||
| 662 | fn export_saver_pngs(&mut self) -> Result<String, String> { | |
| 663 | if self.playback.playing { | |
| 664 | self.playback.toggle_play(Instant::now()); | |
| 665 | self.sync_audio(true, false); | |
| 666 | } | |
| 667 | let opts = ExportOptions { | |
| 668 | hiq: self.engine.hiq, | |
| 669 | proxy_scale: self.engine.proxy_scale, | |
| 670 | ..Default::default() | |
| 671 | }; | |
| 672 | let t0 = Instant::now(); | |
| 673 | match export_saver_png_sequence(&mut self.engine, &self.comp, &opts, |p| { | |
| 674 | if p.index == 0 || (p.index + 1) % 10 == 0 || p.index + 1 == p.total { | |
| 675 | let fps = (p.index + 1) as f64 / t0.elapsed().as_secs_f64().max(1e-6); | |
| 676 | eprintln!( | |
| 677 | " [export {}/{}] {} f{:04} ({:.1}ms, {:.1} fps) -> {}", | |
| 678 | p.index + 1, | |
| 679 | p.total, | |
| 680 | p.saver_name, | |
| 681 | p.frame, | |
| 682 | p.eval_ms, | |
| 683 | fps, | |
| 684 | p.out_path.display() | |
| 685 | ); | |
| 686 | } | |
| 687 | }) { | |
| 688 | Ok(summary) => { | |
| 689 | let secs = (summary.total_ms / 1000.0).max(1e-6); | |
| 690 | let msg = format!( | |
| 691 | "Exported {} PNGs ({:.1} fps)", | |
| 692 | summary.frames_written, | |
| 693 | summary.frames_written as f64 / secs | |
| 694 | ); | |
| 695 | eprintln!("{msg} via {}", summary.saver_name); | |
| 696 | self.status_notice = Some((msg.clone(), Instant::now())); | |
| 697 | Ok(msg) | |
| 698 | } | |
| 699 | Err(e) => { | |
| 700 | let err = format!("Export failed: {e}"); | |
| 701 | eprintln!("{err}"); | |
| 702 | self.status_notice = Some((err.clone(), Instant::now())); | |
| 703 | Err(err) | |
| 704 | } | |
| 705 | } | |
| 706 | } | |
| 707 | ||
| 708 | fn window_title(&self) -> String { | |
| 709 | let dirty = if self.history.is_dirty(&self.comp) { "*" } else { "" }; | |
| 710 | if let Some((msg, at)) = &self.status_notice { | |
| 711 | if at.elapsed() < Duration::from_secs(5) { | |
| 712 | return format!("Fission — {}{dirty} — {msg}", self.comp.name); | |
| 713 | } | |
| 714 | } | |
| 715 | format!("Fission — {}{dirty}", self.comp.name) | |
| 716 | } | |
| 717 | ||
| 718 | fn ensure_viewer2_open(&mut self) { | |
| 719 | if self.layout.group_of(&"Viewer 2".to_owned(), String::clone).is_some() { | |
| 720 | return; | |
| 721 | } | |
| 722 | let target_group = self | |
| 723 | .layout | |
| 724 | .group_of(&"Viewer 1".to_owned(), String::clone) | |
| 725 | .unwrap_or_else(|| self.dock.at(&mut self.layout, &mut self.seen, &String::clone).focused()); | |
| 726 | self.dock | |
| 727 | .at(&mut self.layout, &mut self.seen, &String::clone) | |
| 728 | .open("Viewer 2".to_owned(), Place::Beside(target_group, Side::Right)); | |
| 729 | } | |
| 730 | ||
| 731 | fn clear_viewers(&mut self) { | |
| 732 | for v in &mut self.viewers { | |
| 733 | v.clear(); | |
| 734 | } | |
| 735 | self.flow.viewed_nodes = [None, None]; | |
| 736 | } | |
| 737 | ||
| 738 | fn frame(&mut self, now: Instant) -> bool { | |
| 739 | self.clock = now; | |
| 740 | self.poll_audio_load(); | |
| 741 | let prev_frame = self.playback.current_frame; | |
| 742 | let (animating, frame_changed) = self.playback.tick(&self.comp, now); | |
| 743 | if frame_changed { | |
| 744 | // Re-sync audio when playback loops back to start of render range | |
| 745 | if self.playback.playing && self.playback.current_frame < prev_frame { | |
| 746 | self.sync_audio(false, true); | |
| 747 | } | |
| 748 | for v in &mut self.viewers { | |
| 749 | v.dirty = true; | |
| 750 | } | |
| 751 | } | |
| 752 | ||
| 753 | self.sync_ram_ranges_and_prefetch(); | |
| 754 | let shown = self.dock.at(&mut self.layout, &mut self.seen, &String::clone).shown(); | |
| 755 | for (slot, pane_name) in [(0, "Viewer 1"), (1, "Viewer 2")] { | |
| 756 | if shown.contains(&pane_name.to_owned()) { | |
| 757 | self.viewers[slot].ensure_rendered_with_ram( | |
| 758 | &mut self.engine, | |
| 759 | Some(&self.ram_cache), | |
| 760 | &self.comp, | |
| 761 | self.playback.current_frame, | |
| 762 | self.scale, | |
| 763 | ); | |
| 764 | self.viewers[slot].prepare_hud(&self.comp, &self.engine, &self.playback); | |
| 765 | } | |
| 766 | } | |
| 767 | self.flow.prepare_labels(&self.comp); | |
| 768 | self.spline.prepare_labels(&self.comp); | |
| 769 | self.keyframes.sync_tracks(&self.comp, self.flow.primary); | |
| 770 | self.keyframes.prepare_labels(); | |
| 771 | ||
| 772 | self.ui.begin(self.size, self.scale, now); | |
| 773 | ||
| 774 | let choices: Vec<ui::popup::Item<'static>> = PANES | |
| 775 | .iter() | |
| 776 | .map(|&name| ui::popup::Item { text: name, ..Default::default() }) | |
| 777 | .collect(); | |
| 778 | ||
| 779 | let comp_unsaved = self.history.is_dirty(&self.comp); | |
| 780 | let cur_frame = self.playback.current_frame; | |
| 781 | let eval_stats = self.viewers[0].last_stats.clone(); | |
| 782 | let can_undo = self.history.can_undo(); | |
| 783 | let can_redo = self.history.can_redo(); | |
| 784 | let primary_sel = self.flow.primary; | |
| 785 | let Studio { | |
| 786 | ui, | |
| 787 | dock, | |
| 788 | layout, | |
| 789 | seen, | |
| 790 | engine, | |
| 791 | comp, | |
| 792 | history, | |
| 793 | playback, | |
| 794 | viewers, | |
| 795 | flow, | |
| 796 | spline, | |
| 797 | keyframes, | |
| 798 | inspector, | |
| 799 | add_tool, | |
| 800 | renaming_node, | |
| 801 | rename_buf, | |
| 802 | rename_needs_focus, | |
| 803 | size, | |
| 804 | .. | |
| 805 | } = self; | |
| 806 | ||
| 807 | let v1_bg = viewers[0].background(); | |
| 808 | let v2_bg = viewers[1].background(); | |
| 809 | let flow_bg = flow.background(); | |
| 810 | let spline_bg = spline.background(); | |
| 811 | let keyframes_bg = keyframes.background(); | |
| 812 | let inspector_bg = inspector.background(); | |
| 813 | let mut viewer_actions: Vec<(usize, ViewerAction)> = Vec::new(); | |
| 814 | let mut inspector_actions: Vec<InspectorAction> = Vec::new(); | |
| 815 | let mut req_open_add_tool = false; | |
| 816 | let mut req_undo = false; | |
| 817 | let mut req_redo = false; | |
| 818 | let mut req_open = false; | |
| 819 | let mut req_save = false; | |
| 820 | let mut req_save_as = false; | |
| 821 | let mut commit_rename: Option<(NodeId, String)> = None; | |
| 822 | let mut cancel_rename = false; | |
| 823 | ||
| 824 | let mut d = dock.at(layout, seen, &String::clone); | |
| 825 | let events = d.build( | |
| 826 | ui, | |
| 827 | "studio-dock", | |
| 828 | *size, | |
| 829 | |_| Plus::Menu("Open Pane", &choices), | |
| 830 | &(), | |
| 831 | |name: &String, _| { | |
| 832 | let bg = match name.as_str() { | |
| 833 | "Viewer 1" => v1_bg, | |
| 834 | "Viewer 2" => v2_bg, | |
| 835 | "Flow" => flow_bg, | |
| 836 | "Spline" => spline_bg, | |
| 837 | "Keyframes" => keyframes_bg, | |
| 838 | "Inspector" => inspector_bg, | |
| 839 | _ => flow_bg, | |
| 840 | }; | |
| 841 | Tab { | |
| 842 | title: name, | |
| 843 | icon: None, | |
| 844 | unsaved: name == "Flow" && comp_unsaved, | |
| 845 | closable: true, | |
| 846 | action: None, | |
| 847 | background: bg, | |
| 848 | } | |
| 849 | }, | |
| 850 | |ui, name| { | |
| 851 | let name = name?.as_str(); | |
| 852 | match name { | |
| 853 | "Viewer 1" => { | |
| 854 | let acts = viewers[0].build_ui(ui, viewer_pane_id(0), viewer_canvas_id(0), engine, playback); | |
| 855 | for a in acts { viewer_actions.push((0, a)); } | |
| 856 | Some(viewer_canvas_id(0)) | |
| 857 | } | |
| 858 | "Viewer 2" => { | |
| 859 | let acts = viewers[1].build_ui(ui, viewer_pane_id(1), viewer_canvas_id(1), engine, playback); | |
| 860 | for a in acts { viewer_actions.push((1, a)); } | |
| 861 | Some(viewer_canvas_id(1)) | |
| 862 | } | |
| 863 | "Flow" => { | |
| 864 | ui.open_as( | |
| 865 | flow_pane_id(), | |
| 866 | ui::Spec { | |
| 867 | size: [ui::fill(), ui::fill()], | |
| 868 | axis: ui::Axis::Y, | |
| 869 | fill: Some(flow_bg), | |
| 870 | ..ui::Spec::default() | |
| 871 | }, | |
| 872 | ); | |
| 873 | // Compact Flow toolbar with `+ Add`, Undo/Redo, Open/Save/Save As, and inline F2 rename | |
| 874 | let bar_bg = ui.theme.strip; | |
| 875 | ui.open( | |
| 876 | "flow-toolbar", | |
| 877 | ui::Spec { | |
| 878 | size: [ui::fill(), ui::px(28.0)], | |
| 879 | axis: ui::Axis::X, | |
| 880 | pad: [6.0, 3.0], | |
| 881 | gap: 4.0, | |
| 882 | fill: Some(bar_bg), | |
| 883 | ..ui::Spec::default() | |
| 884 | }, | |
| 885 | ); | |
| 886 | if studio_btn(ui, "flow-add-btn", "+ Add", false).clicked { | |
| 887 | req_open_add_tool = true; | |
| 888 | } | |
| 889 | if studio_btn(ui, "flow-undo-btn", "Undo", can_undo).clicked && can_undo { | |
| 890 | req_undo = true; | |
| 891 | } | |
| 892 | if studio_btn(ui, "flow-redo-btn", "Redo", can_redo).clicked && can_redo { | |
| 893 | req_redo = true; | |
| 894 | } | |
| 895 | if studio_btn(ui, "flow-open-btn", "Open", false).clicked { | |
| 896 | req_open = true; | |
| 897 | } | |
| 898 | if studio_btn(ui, "flow-save-btn", if comp_unsaved { "Save*" } else { "Save" }, comp_unsaved).clicked { | |
| 899 | req_save = true; | |
| 900 | } | |
| 901 | if studio_btn(ui, "flow-saveas-btn", "Save As", false).clicked { | |
| 902 | req_save_as = true; | |
| 903 | } | |
| 904 | if let Some(rn_id) = *renaming_node { | |
| 905 | let rn_field_id = flow_pane_id().child(("rename-field", rn_id.0)); | |
| 906 | let rn_sig = ui::text_field( | |
| 907 | ui, | |
| 908 | rn_field_id, | |
| 909 | rename_buf, | |
| 910 | "Rename node…", | |
| 911 | ui::Spec { | |
| 912 | size: [ui::px(160.0), ui::px(22.0)], | |
| 913 | font_size: Some(11.5), | |
| 914 | fill: Some(ui.theme.base), | |
| 915 | border: Some(ui.theme.accent), | |
| 916 | radius: 4.0, | |
| 917 | pad: [6.0, 1.0], | |
| 918 | ..ui::Spec::default() | |
| 919 | }, | |
| 920 | ); | |
| 921 | if *rename_needs_focus { | |
| 922 | ui.set_focus(Some(rn_field_id)); | |
| 923 | *rename_needs_focus = false; | |
| 924 | } | |
| 925 | for ev in &rn_sig.events { | |
| 926 | if let ui::Event::Key { key, .. } = ev { | |
| 927 | match key { | |
| 928 | Key::Named(NamedKey::Enter) => { | |
| 929 | commit_rename = Some((rn_id, rename_buf.trim().to_owned())); | |
| 930 | } | |
| 931 | Key::Named(NamedKey::Escape) => { | |
| 932 | cancel_rename = true; | |
| 933 | } | |
| 934 | _ => {} | |
| 935 | } | |
| 936 | } | |
| 937 | } | |
| 938 | } | |
| 939 | ui.close(); // flow-toolbar | |
| 940 | ||
| 941 | let id = ui.open_as( | |
| 942 | flow_canvas_id(), | |
| 943 | ui::Spec { | |
| 944 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 945 | size: [ui::fill(), ui::fill()], | |
| 946 | ..ui::Spec::default() | |
| 947 | }, | |
| 948 | ); | |
| 949 | ui.close(); | |
| 950 | ui.close(); // flow_pane_id | |
| 951 | Some(id) | |
| 952 | } | |
| 953 | "Spline" => { | |
| 954 | let id = ui.open_as( | |
| 955 | spline_canvas_id(), | |
| 956 | ui::Spec { | |
| 957 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 958 | size: [ui::fill(), ui::fill()], | |
| 959 | ..ui::Spec::default() | |
| 960 | }, | |
| 961 | ); | |
| 962 | ui.close(); | |
| 963 | Some(id) | |
| 964 | } | |
| 965 | "Keyframes" => { | |
| 966 | let id = ui.open_as( | |
| 967 | keyframes_canvas_id(), | |
| 968 | ui::Spec { | |
| 969 | flags: ui::Flags::CUSTOM | ui::Flags::FOCUSABLE | ui::Flags::KEY_INPUT, | |
| 970 | size: [ui::fill(), ui::fill()], | |
| 971 | ..ui::Spec::default() | |
| 972 | }, | |
| 973 | ); | |
| 974 | ui.close(); | |
| 975 | Some(id) | |
| 976 | } | |
| 977 | "Inspector" => { | |
| 978 | let acts = inspector.build_ui( | |
| 979 | ui, | |
| 980 | inspector_pane_id(), | |
| 981 | comp, | |
| 982 | history, | |
| 983 | primary_sel, | |
| 984 | cur_frame, | |
| 985 | &eval_stats, | |
| 986 | ); | |
| 987 | inspector_actions.extend(acts); | |
| 988 | Some(inspector_pane_id()) | |
| 989 | } | |
| 990 | _ => None, | |
| 991 | } | |
| 992 | }, | |
| 993 | ); | |
| 994 | ||
| 995 | let picked_tool = add_tool.build_ui(ui, *size); | |
| 996 | self.ui.end(); | |
| 997 | ||
| 998 | if req_open_add_tool { | |
| 999 | self.add_tool.open(); | |
| 1000 | } | |
| 1001 | if req_undo { | |
| 1002 | self.undo(); | |
| 1003 | } | |
| 1004 | if req_redo { | |
| 1005 | self.redo(); | |
| 1006 | } | |
| 1007 | if req_open { | |
| 1008 | self.open_comp_dialog(); | |
| 1009 | } | |
| 1010 | if req_save { | |
| 1011 | let _ = self.save_comp(); | |
| 1012 | } | |
| 1013 | if req_save_as { | |
| 1014 | let _ = self.save_comp_as(); | |
| 1015 | } | |
| 1016 | if let Some((rn_id, new_name)) = commit_rename { | |
| 1017 | self.renaming_node = None; | |
| 1018 | self.ui.set_focus(Some(flow_canvas_id())); | |
| 1019 | if !new_name.is_empty() && rn_id.0 < self.comp.nodes.len() && new_name != self.comp.node(rn_id).name { | |
| 1020 | self.history.push_snapshot(&self.comp, "Rename Node"); | |
| 1021 | match self.comp.rename_node(rn_id, &new_name) { | |
| 1022 | Ok(applied) => { | |
| 1023 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 1024 | self.invalidate_node(Some(rn_id)); | |
| 1025 | self.set_notice(format!("Renamed to {applied}")); | |
| 1026 | } | |
| 1027 | Err(e) => self.set_notice(e), | |
| 1028 | } | |
| 1029 | } | |
| 1030 | } else if cancel_rename { | |
| 1031 | self.renaming_node = None; | |
| 1032 | self.ui.set_focus(Some(flow_canvas_id())); | |
| 1033 | } | |
| 1034 | if let Some(entry) = picked_tool { | |
| 1035 | self.add_tool_from_entry(entry); | |
| 1036 | } | |
| 1037 | ||
| 1038 | for a in inspector_actions { | |
| 1039 | match a { | |
| 1040 | InspectorAction::ParamEdited(id) => { | |
| 1041 | self.invalidate_node(Some(id)); | |
| 1042 | } | |
| 1043 | InspectorAction::KeyframeEdited(id) => { | |
| 1044 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 1045 | self.invalidate_node(Some(id)); | |
| 1046 | } | |
| 1047 | InspectorAction::NodeRenamed(id) => { | |
| 1048 | self.spline.set_target_node(&self.comp, self.flow.primary); | |
| 1049 | self.invalidate_node(Some(id)); | |
| 1050 | } | |
| 1051 | InspectorAction::StatusNotice(msg) => { | |
| 1052 | self.set_notice(msg); | |
| 1053 | } | |
| 1054 | } | |
| 1055 | } | |
| 1056 | ||
| 1057 | for ev in events { | |
| 1058 | match ev { | |
| 1059 | Event::Close(name) => { | |
| 1060 | self.dock.at(&mut self.layout, &mut self.seen, &String::clone).close(&name); | |
| 1061 | } | |
| 1062 | Event::Plus { group, choice: Some(idx), .. } => { | |
| 1063 | if let Some(&name) = PANES.get(idx) { | |
| 1064 | self.dock.at(&mut self.layout, &mut self.seen, &String::clone).open(name.to_owned(), Place::Group(group)); | |
| 1065 | } | |
| 1066 | } | |
| 1067 | _ => {} | |
| 1068 | } | |
| 1069 | } | |
| 1070 | ||
| 1071 | self.route_viewer_actions(viewer_actions, now); | |
| 1072 | ||
| 1073 | for slot in [0usize, 1] { | |
| 1074 | let cid = viewer_canvas_id(slot); | |
| 1075 | if let Some(rect) = self.ui.laid_out(cid) { | |
| 1076 | self.viewers[slot].resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1077 | let sig = self.ui.signal(cid); | |
| 1078 | let ptr = self.ui.last_pointer(); | |
| 1079 | let mods = self.ui.modifiers(); | |
| 1080 | let mut acts = Vec::new(); | |
| 1081 | for ev in sig.events { | |
| 1082 | for a in self.viewers[slot].input(&self.comp, &self.playback, ev, rect, ptr, mods) { | |
| 1083 | acts.push((slot, a)); | |
| 1084 | } | |
| 1085 | } | |
| 1086 | self.route_viewer_actions(acts, now); | |
| 1087 | } | |
| 1088 | } | |
| 1089 | ||
| 1090 | if let Some(rect) = self.ui.laid_out(flow_canvas_id()) { | |
| 1091 | self.flow.resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1092 | self.flow.ensure_fitted(&self.comp); | |
| 1093 | let sig = self.ui.signal(flow_canvas_id()); | |
| 1094 | let ptr = self.ui.last_pointer(); | |
| 1095 | let mods = self.ui.modifiers(); | |
| 1096 | let mut acts = Vec::new(); | |
| 1097 | for ev in sig.events { | |
| 1098 | acts.extend(self.flow.input(&mut self.comp, &mut self.history, ev, rect, ptr, mods)); | |
| 1099 | } | |
| 1100 | for a in acts { | |
| 1101 | match a { | |
| 1102 | FlowAction::SelectionChanged(sel) => { | |
| 1103 | self.spline.set_target_node(&self.comp, sel); | |
| 1104 | } | |
| 1105 | FlowAction::ViewNode(slot, id) => { | |
| 1106 | self.viewers[slot].viewed_node = Some(id); | |
| 1107 | self.viewers[slot].dirty = true; | |
| 1108 | self.flow.viewed_nodes[slot] = Some(id); | |
| 1109 | if slot == 1 { | |
| 1110 | self.ensure_viewer2_open(); | |
| 1111 | } | |
| 1112 | } | |
| 1113 | FlowAction::ClearViewers => { | |
| 1114 | self.clear_viewers(); | |
| 1115 | } | |
| 1116 | FlowAction::GraphMutated(node) => { | |
| 1117 | self.invalidate_node(node); | |
| 1118 | } | |
| 1119 | FlowAction::OpenAddTool => { | |
| 1120 | self.add_tool.open(); | |
| 1121 | } | |
| 1122 | FlowAction::RequestRename(id) => { | |
| 1123 | self.begin_inline_rename(id); | |
| 1124 | } | |
| 1125 | FlowAction::StatusNotice(msg) => { | |
| 1126 | self.set_notice(msg); | |
| 1127 | } | |
| 1128 | } | |
| 1129 | } | |
| 1130 | } | |
| 1131 | ||
| 1132 | if let Some(rect) = self.ui.laid_out(spline_canvas_id()) { | |
| 1133 | self.spline.resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1134 | let sig = self.ui.signal(spline_canvas_id()); | |
| 1135 | let ptr = self.ui.last_pointer(); | |
| 1136 | let mods = self.ui.modifiers(); | |
| 1137 | let mut acts = Vec::new(); | |
| 1138 | for ev in sig.events { | |
| 1139 | acts.extend(self.spline.input_with_history( | |
| 1140 | &mut self.comp, | |
| 1141 | Some(&mut self.history), | |
| 1142 | self.playback.current_frame, | |
| 1143 | ev, | |
| 1144 | rect, | |
| 1145 | ptr, | |
| 1146 | mods, | |
| 1147 | )); | |
| 1148 | } | |
| 1149 | for a in acts { | |
| 1150 | match a { | |
| 1151 | SplineAction::SplineEdited => { | |
| 1152 | if let Some(id) = self.flow.primary { | |
| 1153 | self.engine.bust_subtree(id); | |
| 1154 | } | |
| 1155 | for v in &mut self.viewers { | |
| 1156 | v.cached_frames.clear(); | |
| 1157 | v.dirty = true; | |
| 1158 | } | |
| 1159 | } | |
| 1160 | SplineAction::ScrubFrame(f) => { | |
| 1161 | if self.playback.set_frame(&self.comp, f, now) { | |
| 1162 | self.sync_audio(false, true); | |
| 1163 | for v in &mut self.viewers { | |
| 1164 | v.dirty = true; | |
| 1165 | } | |
| 1166 | } | |
| 1167 | } | |
| 1168 | } | |
| 1169 | } | |
| 1170 | } | |
| 1171 | ||
| 1172 | if let Some(rect) = self.ui.laid_out(keyframes_canvas_id()) { | |
| 1173 | self.keyframes.resize([rect[2] - rect[0], rect[3] - rect[1]]); | |
| 1174 | let sig = self.ui.signal(keyframes_canvas_id()); | |
| 1175 | let ptr = self.ui.last_pointer(); | |
| 1176 | let mods = self.ui.modifiers(); | |
| 1177 | let mut acts = Vec::new(); | |
| 1178 | for ev in sig.events { | |
| 1179 | acts.extend(self.keyframes.input( | |
| 1180 | &mut self.comp, | |
| 1181 | &mut self.history, | |
| 1182 | self.flow.primary, | |
| 1183 | ev, | |
| 1184 | rect, | |
| 1185 | ptr, | |
| 1186 | mods, | |
| 1187 | )); | |
| 1188 | } | |
| 1189 | for a in acts { | |
| 1190 | match a { | |
| 1191 | KeyframesAction::ScrubFrame(f) => { | |
| 1192 | if self.playback.set_frame(&self.comp, f, now) { | |
| 1193 | self.sync_audio(false, true); | |
| 1194 | for v in &mut self.viewers { | |
| 1195 | v.dirty = true; | |
| 1196 | } | |
| 1197 | } | |
| 1198 | } | |
| 1199 | KeyframesAction::SelectNode(id) => { | |
| 1200 | self.flow.selected.clear(); | |
| 1201 | self.flow.selected.insert(id); | |
| 1202 | self.flow.primary = Some(id); | |
| 1203 | self.spline.set_target_node(&self.comp, Some(id)); | |
| 1204 | } | |
| 1205 | KeyframesAction::KeyframesEdited => { | |
| 1206 | self.invalidate_node(None); | |
| 1207 | } | |
| 1208 | } | |
| 1209 | } | |
| 1210 | } | |
| 1211 | ||
| 1212 | // Immediately re-render any dirty visible viewer before painting this frame! | |
| 1213 | self.sync_ram_ranges_and_prefetch(); | |
| 1214 | let shown_now = self.dock.at(&mut self.layout, &mut self.seen, &String::clone).shown(); | |
| 1215 | for (slot, pane_name) in [(0, "Viewer 1"), (1, "Viewer 2")] { | |
| 1216 | if shown_now.contains(&pane_name.to_owned()) && self.viewers[slot].dirty { | |
| 1217 | self.viewers[slot].ensure_rendered_with_ram( | |
| 1218 | &mut self.engine, | |
| 1219 | Some(&self.ram_cache), | |
| 1220 | &self.comp, | |
| 1221 | self.playback.current_frame, | |
| 1222 | self.scale, | |
| 1223 | ); | |
| 1224 | self.viewers[slot].prepare_hud(&self.comp, &self.engine, &self.playback); | |
| 1225 | } | |
| 1226 | } | |
| 1227 | ||
| 1228 | animating || self.ui.wants_frame() || self.dock.moving() | |
| 1229 | } | |
| 1230 | ||
| 1231 | fn route_viewer_actions(&mut self, actions: Vec<(usize, ViewerAction)>, now: Instant) { | |
| 1232 | for (slot, a) in actions { | |
| 1233 | match a { | |
| 1234 | ViewerAction::TogglePlay => { | |
| 1235 | self.playback.toggle_play(now); | |
| 1236 | self.sync_audio(true, false); | |
| 1237 | } | |
| 1238 | ViewerAction::SetFrame(f) => { | |
| 1239 | if self.playback.set_frame(&self.comp, f, now) { | |
| 1240 | self.sync_audio(false, true); | |
| 1241 | for v in &mut self.viewers { | |
| 1242 | v.dirty = true; | |
| 1243 | } | |
| 1244 | } | |
| 1245 | } | |
| 1246 | ViewerAction::ToggleSyncMode => { | |
| 1247 | self.playback.sync_mode = match self.playback.sync_mode { | |
| 1248 | SyncMode::RealtimeDrop => SyncMode::EveryFrame, | |
| 1249 | SyncMode::EveryFrame => SyncMode::RealtimeDrop, | |
| 1250 | }; | |
| 1251 | } | |
| 1252 | ViewerAction::NeedRender => { | |
| 1253 | self.viewers[slot].dirty = true; | |
| 1254 | } | |
| 1255 | ViewerAction::BustFrame(f) => { | |
| 1256 | self.engine.bust_frame(f); | |
| 1257 | self.ram_cache.clear(); | |
| 1258 | self.last_prefetch_sig = None; | |
| 1259 | for v in &mut self.viewers { | |
| 1260 | v.cached_frames.remove(&f); | |
| 1261 | v.dirty = true; | |
| 1262 | } | |
| 1263 | } | |
| 1264 | ViewerAction::BustSubtree => { | |
| 1265 | if let Some(id) = self.flow.primary.or(self.viewers[slot].viewed_node) { | |
| 1266 | self.engine.bust_subtree(id); | |
| 1267 | } | |
| 1268 | self.ram_cache.clear(); | |
| 1269 | self.last_prefetch_sig = None; | |
| 1270 | for v in &mut self.viewers { | |
| 1271 | v.cached_frames.clear(); | |
| 1272 | v.dirty = true; | |
| 1273 | } | |
| 1274 | } | |
| 1275 | ViewerAction::BustGpu => { | |
| 1276 | self.engine.bust_gpu_cache(); | |
| 1277 | self.ram_cache.clear(); | |
| 1278 | self.last_prefetch_sig = None; | |
| 1279 | for v in &mut self.viewers { | |
| 1280 | v.cached_frames.clear(); | |
| 1281 | v.dirty = true; | |
| 1282 | } | |
| 1283 | } | |
| 1284 | ViewerAction::BustAll => { | |
| 1285 | self.engine.bust_all_caches(); | |
| 1286 | self.ram_cache.clear(); | |
| 1287 | self.last_prefetch_sig = None; | |
| 1288 | for v in &mut self.viewers { | |
| 1289 | v.cached_frames.clear(); | |
| 1290 | v.dirty = true; | |
| 1291 | } | |
| 1292 | } | |
| 1293 | ViewerAction::ToggleCacheBypass => { | |
| 1294 | let next = match self.engine.cache.mode { | |
| 1295 | CacheMode::Enabled => CacheMode::Bypass, | |
| 1296 | CacheMode::Bypass => CacheMode::Enabled, | |
| 1297 | }; | |
| 1298 | self.engine.set_cache_mode(next); | |
| 1299 | for v in &mut self.viewers { | |
| 1300 | v.dirty = true; | |
| 1301 | } | |
| 1302 | } | |
| 1303 | ViewerAction::SetProxy(scale) => { | |
| 1304 | if self.engine.proxy_scale != scale { | |
| 1305 | self.engine.proxy_scale = scale; | |
| 1306 | self.engine.bust_gpu_cache(); | |
| 1307 | for v in &mut self.viewers { | |
| 1308 | v.cached_frames.clear(); | |
| 1309 | v.dirty = true; | |
| 1310 | } | |
| 1311 | } | |
| 1312 | } | |
| 1313 | ViewerAction::ToggleHiQ => { | |
| 1314 | self.engine.hiq = !self.engine.hiq; | |
| 1315 | for v in &mut self.viewers { | |
| 1316 | v.cached_frames.clear(); | |
| 1317 | v.dirty = true; | |
| 1318 | } | |
| 1319 | } | |
| 1320 | ViewerAction::ClearViewers => { | |
| 1321 | self.clear_viewers(); | |
| 1322 | } | |
| 1323 | ViewerAction::GizmoEdited(edit) => { | |
| 1324 | self.history.push_coalesced(&self.comp, edit.label, &edit.coalesce_key); | |
| 1325 | self.comp.set_input_value(edit.node_id, edit.input, edit.value); | |
| 1326 | self.invalidate_node(Some(edit.node_id)); | |
| 1327 | } | |
| 1328 | } | |
| 1329 | } | |
| 1330 | } | |
| 1331 | ||
| 1332 | fn paint(&mut self, target: &wgpu::TextureView, pixels: [u32; 2]) -> Result<(), Box<dyn Error>> { | |
| 1333 | let scale = self.scale; | |
| 1334 | let cur_f = self.playback.current_frame; | |
| 1335 | let Studio { ui, renderer, comp, viewers, flow, spline, keyframes, .. } = self; | |
| 1336 | ||
| 1337 | let mut custom_prims: HashMap<ui::Id, ([f32; 4], [f32; 2], Vec<draw::Primitive<'_>>)> = HashMap::new(); | |
| 1338 | for slot in [0usize, 1] { | |
| 1339 | let cid = viewer_canvas_id(slot); | |
| 1340 | if let Some(r) = ui.laid_out(cid) { | |
| 1341 | custom_prims.insert(cid, (viewers[slot].background(), [r[0], r[1]], viewers[slot].paint(comp, cur_f))); | |
| 1342 | } | |
| 1343 | } | |
| 1344 | if let Some(r) = ui.laid_out(flow_canvas_id()) { | |
| 1345 | custom_prims.insert(flow_canvas_id(), (flow.background(), [r[0], r[1]], flow.paint(comp))); | |
| 1346 | } | |
| 1347 | if let Some(r) = ui.laid_out(spline_canvas_id()) { | |
| 1348 | custom_prims.insert(spline_canvas_id(), (spline.background(), [r[0], r[1]], spline.paint(comp, cur_f))); | |
| 1349 | } | |
| 1350 | if let Some(r) = ui.laid_out(keyframes_canvas_id()) { | |
| 1351 | custom_prims.insert(keyframes_canvas_id(), (keyframes.background(), [r[0], r[1]], keyframes.paint(comp, cur_f))); | |
| 1352 | } | |
| 1353 | ||
| 1354 | let interface = ui.layers(); | |
| 1355 | let layers: Vec<draw::Layer> = interface | |
| 1356 | .iter() | |
| 1357 | .filter_map(|layer| match layer { | |
| 1358 | ui::Layer::Primitives(prims) => Some(prims.layer(scale)), | |
| 1359 | ui::Layer::Custom { id, rect, .. } => { | |
| 1360 | let (bg, corner, prims) = custom_prims.get(id)?; | |
| 1361 | Some(draw::Layer { | |
| 1362 | scale, | |
| 1363 | origin: corner.map(|v| v * scale), | |
| 1364 | clip: Some(rect.map(|v| v * scale)), | |
| 1365 | backdrop: Some(*bg), | |
| 1366 | round: None, | |
| 1367 | motion: None, | |
| 1368 | primitives: prims, | |
| 1369 | }) | |
| 1370 | } | |
| 1371 | }) | |
| 1372 | .collect(); | |
| 1373 | ||
| 1374 | renderer | |
| 1375 | .draw(target, pixels, ui.theme.base, &layers) | |
| 1376 | .map_err(|e| format!("{e:?}"))?; | |
| 1377 | Ok(()) | |
| 1378 | } | |
| 1379 | } | |
| 1380 | ||
| 1381 | fn run_snapshot(cli: &Cli, out_path: &Path) -> Result<(), Box<dyn Error>> { | |
| 1382 | theme::fonts::register()?; | |
| 1383 | let comp = Comp::from_fusion_file(&cli.comp_path)?; | |
| 1384 | let instance = wgpu::Instance::new(wgpu::InstanceDescriptor::new_without_display_handle()); | |
| 1385 | let adapter = pollster::block_on(instance.request_adapter(&Default::default()))?; | |
| 1386 | let mut req_features = wgpu::Features::empty(); | |
| 1387 | if adapter.features().contains(wgpu::Features::FLOAT32_FILTERABLE) { | |
| 1388 | req_features |= wgpu::Features::FLOAT32_FILTERABLE; | |
| 1389 | } | |
| 1390 | let (device, queue) = pollster::block_on(adapter.request_device(&wgpu::DeviceDescriptor { | |
| 1391 | required_features: req_features, | |
| 1392 | ..Default::default() | |
| 1393 | }))?; | |
| 1394 | let renderer = draw::Renderer::new(device, queue, wgpu::TextureFormat::Rgba8Unorm); | |
| 1395 | ||
| 1396 | let mut studio = Studio::new( | |
| 1397 | renderer, | |
| 1398 | comp, | |
| 1399 | cli.frame, | |
| 1400 | cli.select_node.as_deref(), | |
| 1401 | cli.dual_viewer, | |
| 1402 | cli.show_keyframes, | |
| 1403 | false, | |
| 1404 | ); | |
| 1405 | // Load audio waveform peaks if available so snapshot verifies the waveform overlay too | |
| 1406 | if let Some(af) = studio.comp.audio_filename.clone() { | |
| 1407 | let p = PathBuf::from(af); | |
| 1408 | if p.exists() { | |
| 1409 | if let Ok(track) = AudioTrack::load(&p) { | |
| 1410 | let peaks = track.waveform_peaks( | |
| 1411 | studio.comp.fps, | |
| 1412 | studio.comp.audio_offset, | |
| 1413 | studio.comp.render_range[0], | |
| 1414 | studio.comp.render_range[1], | |
| 1415 | ); | |
| 1416 | studio.viewers[0].audio_peaks = peaks.clone(); | |
| 1417 | studio.keyframes.audio_peaks = peaks; | |
| 1418 | studio.keyframes.audio_range = studio.comp.render_range; | |
| 1419 | } | |
| 1420 | } | |
| 1421 | } | |
| 1422 | if cli.open_add_tool { | |
| 1423 | studio.add_tool.open(); | |
| 1424 | } | |
| 1425 | studio.size = [1440.0, 900.0]; | |
| 1426 | studio.scale = 2.0; | |
| 1427 | ||
| 1428 | let now = Instant::now(); | |
| 1429 | studio.frame(now); | |
| 1430 | studio.frame(now + Duration::from_millis(16)); | |
| 1431 | ||
| 1432 | let pixels = studio.size.map(|s| (s * studio.scale) as u32); | |
| 1433 | let tex = studio.renderer.offscreen(pixels); | |
| 1434 | let view = tex.create_view(&Default::default()); | |
| 1435 | studio.paint(&view, pixels)?; | |
| 1436 | let rgba = studio.renderer.read(&tex)?; | |
| 1437 | ||
| 1438 | let mut enc = png::Encoder::new(File::create(out_path)?, pixels[0], pixels[1]); | |
| 1439 | enc.set_color(png::ColorType::Rgba); | |
| 1440 | enc.set_depth(png::BitDepth::Eight); | |
| 1441 | enc.write_header()?.write_image_data(&rgba)?; | |
| 1442 | eprintln!( | |
| 1443 | "Wrote docked Studio snapshot to {} ({}x{}) | eval={:.1}ms", | |
| 1444 | out_path.display(), | |
| 1445 | pixels[0], | |
| 1446 | pixels[1], | |
| 1447 | studio.viewers[0].last_stats.eval_ms | |
| 1448 | ); | |
| 1449 | Ok(()) | |
| 1450 | } | |
| 1451 | ||
| 1452 | #[allow(dead_code)] | |
| 1453 | enum UserEvent { | |
| 1454 | Redraw, | |
| 1455 | #[cfg(target_os = "macos")] | |
| 1456 | Quit, | |
| 1457 | Accessibility(accesskit_winit::Event), | |
| 1458 | } | |
| 1459 | ||
| 1460 | impl From<accesskit_winit::Event> for UserEvent { | |
| 1461 | fn from(event: accesskit_winit::Event) -> Self { | |
| 1462 | Self::Accessibility(event) | |
| 1463 | } | |
| 1464 | } | |
| 1465 | ||
| 1466 | struct WindowHost { | |
| 1467 | window: Arc<Window>, | |
| 1468 | surface: Surface, | |
| 1469 | studio: Studio, | |
| 1470 | access: Accessibility, | |
| 1471 | occluded: bool, | |
| 1472 | } | |
| 1473 | ||
| 1474 | struct App { | |
| 1475 | proxy: EventLoopProxy<UserEvent>, | |
| 1476 | cli: Cli, | |
| 1477 | host: Option<WindowHost>, | |
| 1478 | error: Option<Box<dyn Error>>, | |
| 1479 | } | |
| 1480 | ||
| 1481 | fn run_window(cli: &Cli) -> Result<(), Box<dyn Error>> { | |
| 1482 | theme::fonts::register()?; | |
| 1483 | let mut builder = EventLoop::<UserEvent>::with_user_event(); | |
| 1484 | #[cfg(target_os = "macos")] | |
| 1485 | { | |
| 1486 | use winit::platform::macos::EventLoopBuilderExtMacOS; | |
| 1487 | builder.with_activate_ignoring_other_apps(false); | |
| 1488 | } | |
| 1489 | let event_loop = builder.build()?; | |
| 1490 | let proxy = event_loop.create_proxy(); | |
| 1491 | let mut app = App { | |
| 1492 | proxy, | |
| 1493 | cli: Cli { | |
| 1494 | comp_path: cli.comp_path.clone(), | |
| 1495 | snapshot: None, | |
| 1496 | frame: cli.frame, | |
| 1497 | select_node: cli.select_node.clone(), | |
| 1498 | dual_viewer: cli.dual_viewer, | |
| 1499 | open_add_tool: cli.open_add_tool, | |
| 1500 | show_keyframes: cli.show_keyframes, | |
| 1501 | }, | |
| 1502 | host: None, | |
| 1503 | error: None, | |
| 1504 | }; | |
| 1505 | event_loop.run_app(&mut app)?; | |
| 1506 | match app.error { | |
| 1507 | Some(e) => Err(e), | |
| 1508 | None => Ok(()), | |
| 1509 | } | |
| 1510 | } | |
| 1511 | ||
| 1512 | impl WindowHost { | |
| 1513 | fn new(event_loop: &ActiveEventLoop, proxy: EventLoopProxy<UserEvent>, cli: &Cli) -> Result<Self, Box<dyn Error>> { | |
| 1514 | let comp = Comp::from_fusion_file(&cli.comp_path)?; | |
| 1515 | let title = format!("Fission — {}", comp.name); | |
| 1516 | let attrs = Window::default_attributes() | |
| 1517 | .with_title(title) | |
| 1518 | .with_inner_size(LogicalSize::new(1440.0, 900.0)); | |
| 1519 | let (window, access) = Accessibility::window(event_loop, attrs, [640.0, 420.0], proxy.clone(), ui::Id::ROOT)?; | |
| 1520 | let (surface, renderer) = pollster::block_on(Surface::new(window.clone()))?; | |
| 1521 | let mut studio = Studio::new( | |
| 1522 | renderer, | |
| 1523 | comp, | |
| 1524 | cli.frame, | |
| 1525 | cli.select_node.as_deref(), | |
| 1526 | cli.dual_viewer, | |
| 1527 | cli.show_keyframes, | |
| 1528 | true, | |
| 1529 | ); | |
| 1530 | studio.ui.window_focused = window.has_focus(); | |
| 1531 | studio.ui.clipboard = Clipboard::system(); | |
| 1532 | window.request_redraw(); | |
| 1533 | #[cfg(target_os = "macos")] | |
| 1534 | { | |
| 1535 | let quit = proxy.clone(); | |
| 1536 | ui::host::macos::quit(move || { let _ = quit.send_event(UserEvent::Quit); }); | |
| 1537 | } | |
| 1538 | Ok(Self { window, surface, studio, access, occluded: false }) | |
| 1539 | } | |
| 1540 | ||
| 1541 | fn redraw(&mut self) -> Result<(), Box<dyn Error>> { | |
| 1542 | let now = Instant::now(); | |
| 1543 | let wheel = self.surface.wheel.frame(now, self.studio.ui.reduces_motion()); | |
| 1544 | if wheel != [0.0; 2] { | |
| 1545 | self.studio.ui.event(ui::Event::Wheel(wheel)); | |
| 1546 | } | |
| 1547 | self.studio.scale = self.window.scale_factor() as f32; | |
| 1548 | let pixels = self.surface.fit(&self.studio.renderer); | |
| 1549 | if pixels.contains(&0) { | |
| 1550 | return Ok(()); | |
| 1551 | } | |
| 1552 | self.studio.size = pixels.map(|s| s as f32 / self.studio.scale); | |
| 1553 | let wants_more = self.studio.frame(now); | |
| 1554 | self.window.set_title(&self.studio.window_title()); | |
| 1555 | ||
| 1556 | self.window.set_cursor(self.studio.ui.cursor().unwrap_or(CursorIcon::Default)); | |
| 1557 | self.access.update_views(&mut self.studio.ui, "Compositor", f64::from(self.studio.scale), Vec::new); | |
| 1558 | ||
| 1559 | if !self.occluded && let Some(frame) = self.surface.frame(&self.studio.renderer, Some(&mut self.studio.ui))? { | |
| 1560 | self.studio.paint(&frame.target, pixels)?; | |
| 1561 | self.surface.present(&self.studio.renderer, frame); | |
| 1562 | } | |
| 1563 | if wants_more && !self.occluded { | |
| 1564 | self.window.request_redraw(); | |
| 1565 | } | |
| 1566 | Ok(()) | |
| 1567 | } | |
| 1568 | ||
| 1569 | fn input(&mut self, event: ui::Event) { | |
| 1570 | let typing = self.studio.ui.typing(); | |
| 1571 | // Global file & history shortcuts: Cmd+S, Cmd+Shift+S, Cmd+O, Cmd+Z, Cmd+Shift+Z / Cmd+Y, Cmd+Shift+R | |
| 1572 | let key_cmd = match &event { | |
| 1573 | ui::Event::KeyInput { physical, modifiers, state: ElementState::Pressed, repeat: false, .. } => { | |
| 1574 | let cmd = modifiers.super_key() || modifiers.control_key(); | |
| 1575 | let shift = modifiers.shift_key(); | |
| 1576 | if cmd && !shift && *physical == PhysicalKey::Code(KeyCode::KeyS) { | |
| 1577 | Some("save") | |
| 1578 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyS) { | |
| 1579 | Some("save_as") | |
| 1580 | } else if cmd && !shift && *physical == PhysicalKey::Code(KeyCode::KeyO) { | |
| 1581 | Some("open") | |
| 1582 | } else if cmd && !shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyZ) { | |
| 1583 | Some("undo") | |
| 1584 | } else if cmd && shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyZ) { | |
| 1585 | Some("redo") | |
| 1586 | } else if cmd && !shift && !typing && *physical == PhysicalKey::Code(KeyCode::KeyY) { | |
| 1587 | Some("redo") | |
| 1588 | } else if cmd && shift && *physical == PhysicalKey::Code(KeyCode::KeyR) { | |
| 1589 | Some("export") | |
| 1590 | } else { | |
| 1591 | None | |
| 1592 | } | |
| 1593 | } | |
| 1594 | ui::Event::Key { key: Key::Character(c), modifiers, .. } => { | |
| 1595 | let cmd = modifiers.super_key() || modifiers.control_key(); | |
| 1596 | let shift = modifiers.shift_key(); | |
| 1597 | if cmd && !shift && c.eq_ignore_ascii_case("s") { | |
| 1598 | Some("save") | |
| 1599 | } else if cmd && shift && c.eq_ignore_ascii_case("s") { | |
| 1600 | Some("save_as") | |
| 1601 | } else if cmd && !shift && c.eq_ignore_ascii_case("o") { | |
| 1602 | Some("open") | |
| 1603 | } else if cmd && !shift && !typing && c.eq_ignore_ascii_case("z") { | |
| 1604 | Some("undo") | |
| 1605 | } else if cmd && shift && !typing && c.eq_ignore_ascii_case("z") { | |
| 1606 | Some("redo") | |
| 1607 | } else if cmd && !shift && !typing && c.eq_ignore_ascii_case("y") { | |
| 1608 | Some("redo") | |
| 1609 | } else if cmd && shift && c.eq_ignore_ascii_case("r") { | |
| 1610 | Some("export") | |
| 1611 | } else { | |
| 1612 | None | |
| 1613 | } | |
| 1614 | } | |
| 1615 | _ => None, | |
| 1616 | }; | |
| 1617 | if let Some(action) = key_cmd { | |
| 1618 | match action { | |
| 1619 | "save" => { let _ = self.studio.save_comp(); } | |
| 1620 | "save_as" => { let _ = self.studio.save_comp_as(); } | |
| 1621 | "open" => self.studio.open_comp_dialog(), | |
| 1622 | "undo" => self.studio.undo(), | |
| 1623 | "redo" => self.studio.redo(), | |
| 1624 | "export" => { let _ = self.studio.export_saver_pngs(); } | |
| 1625 | _ => {} | |
| 1626 | } | |
| 1627 | self.window.set_title(&self.studio.window_title()); | |
| 1628 | self.window.request_redraw(); | |
| 1629 | return; | |
| 1630 | } | |
| 1631 | ||
| 1632 | // Shift+Space opens Add Tool launcher (when not typing in a text field) | |
| 1633 | if !typing { | |
| 1634 | let is_shift_space = match &event { | |
| 1635 | ui::Event::KeyInput { key: Key::Named(NamedKey::Space), modifiers, state: ElementState::Pressed, repeat: false, .. } | |
| 1636 | | ui::Event::Key { key: Key::Named(NamedKey::Space), modifiers, .. } => { | |
| 1637 | modifiers.shift_key() && !modifiers.super_key() && !modifiers.control_key() | |
| 1638 | } | |
| 1639 | _ => false, | |
| 1640 | }; | |
| 1641 | if is_shift_space { | |
| 1642 | self.studio.add_tool.open(); | |
| 1643 | self.window.request_redraw(); | |
| 1644 | return; | |
| 1645 | } | |
| 1646 | // Global Space toggles playback (synced with audio) and `` ` `` clears both viewers from any focused pane! | |
| 1647 | let is_space = match &event { | |
| 1648 | ui::Event::KeyInput { key: Key::Named(NamedKey::Space), modifiers, state: ElementState::Pressed, repeat: false, .. } | |
| 1649 | | ui::Event::Key { key: Key::Named(NamedKey::Space), modifiers, .. } => { | |
| 1650 | !modifiers.shift_key() && !modifiers.super_key() && !modifiers.control_key() | |
| 1651 | } | |
| 1652 | _ => false, | |
| 1653 | }; | |
| 1654 | if is_space && !self.studio.add_tool.open { | |
| 1655 | self.studio.playback.toggle_play(Instant::now()); | |
| 1656 | self.studio.sync_audio(true, false); | |
| 1657 | self.window.request_redraw(); | |
| 1658 | return; | |
| 1659 | } | |
| 1660 | let is_backquote = match &event { | |
| 1661 | ui::Event::KeyInput { physical: PhysicalKey::Code(KeyCode::Backquote), state: ElementState::Pressed, repeat: false, .. } => true, | |
| 1662 | ui::Event::Key { key: Key::Character(c), .. } if c.as_str() == "`" || c.as_str() == "~" => true, | |
| 1663 | _ => false, | |
| 1664 | }; | |
| 1665 | if is_backquote && !self.studio.add_tool.open { | |
| 1666 | self.studio.clear_viewers(); | |
| 1667 | self.window.request_redraw(); | |
| 1668 | return; | |
| 1669 | } | |
| 1670 | } | |
| 1671 | if !self.studio.ui.popups_open() && !self.studio.add_tool.open && let ui::Event::Key { key, modifiers, .. } = &event { | |
| 1672 | let cmd = match key { | |
| 1673 | Key::Named(NamedKey::Tab) if modifiers.control_key() => { | |
| 1674 | Some(if modifiers.shift_key() { Command::Previous } else { Command::Next }) | |
| 1675 | } | |
| 1676 | _ => None, | |
| 1677 | }; | |
| 1678 | if let Some(cmd) = cmd { | |
| 1679 | let _ = self.studio.dock.at(&mut self.studio.layout, &mut self.studio.seen, &String::clone).run(cmd); | |
| 1680 | self.window.request_redraw(); | |
| 1681 | return; | |
| 1682 | } | |
| 1683 | } | |
| 1684 | self.studio.ui.event(event); | |
| 1685 | self.window.request_redraw(); | |
| 1686 | } | |
| 1687 | } | |
| 1688 | ||
| 1689 | impl ApplicationHandler<UserEvent> for App { | |
| 1690 | fn resumed(&mut self, event_loop: &ActiveEventLoop) { | |
| 1691 | if self.host.is_some() { return; } | |
| 1692 | match WindowHost::new(event_loop, self.proxy.clone(), &self.cli) { | |
| 1693 | Ok(host) => self.host = Some(host), | |
| 1694 | Err(e) => { | |
| 1695 | self.error = Some(e); | |
| 1696 | event_loop.exit(); | |
| 1697 | } | |
| 1698 | } | |
| 1699 | } | |
| 1700 | ||
| 1701 | fn user_event(&mut self, event_loop: &ActiveEventLoop, event: UserEvent) { | |
| 1702 | #[cfg(not(target_os = "macos"))] | |
| 1703 | let _ = event_loop; | |
| 1704 | let Some(host) = &mut self.host else { return }; | |
| 1705 | match event { | |
| 1706 | UserEvent::Redraw => host.window.request_redraw(), | |
| 1707 | #[cfg(target_os = "macos")] | |
| 1708 | UserEvent::Quit => { | |
| 1709 | ui::host::macos::finish_quit(true); | |
| 1710 | event_loop.exit(); | |
| 1711 | } | |
| 1712 | UserEvent::Accessibility(ev) => match ev.window_event { | |
| 1713 | accesskit_winit::WindowEvent::InitialTreeRequested => { | |
| 1714 | host.access.forget(&mut host.studio.ui); | |
| 1715 | host.window.request_redraw(); | |
| 1716 | } | |
| 1717 | accesskit_winit::WindowEvent::AccessibilityDeactivated => host.access.forget(&mut host.studio.ui), | |
| 1718 | accesskit_winit::WindowEvent::ActionRequested(req) => host.input(ui::Event::Access(req)), | |
| 1719 | }, | |
| 1720 | } | |
| 1721 | } | |
| 1722 | ||
| 1723 | fn window_event(&mut self, event_loop: &ActiveEventLoop, _: WindowId, event: WindowEvent) { | |
| 1724 | let Some(host) = &mut self.host else { return }; | |
| 1725 | host.access.adapter.process_event(&host.window, &event); | |
| 1726 | if let Some(Err(e)) = ui::host::draw(&event, || host.redraw()) { | |
| 1727 | self.error = Some(e); | |
| 1728 | event_loop.exit(); | |
| 1729 | return; | |
| 1730 | } | |
| 1731 | let page = host.surface.size[1] as f32 / host.window.scale_factor() as f32; | |
| 1732 | if let Some(wheel) = host.surface.wheel.input(&host.window, &event, page, host.studio.ui.reduces_motion(), Instant::now()) { | |
| 1733 | if wheel != [0.0; 2] { host.input(ui::Event::Wheel(wheel)); } | |
| 1734 | host.window.request_redraw(); | |
| 1735 | return; | |
| 1736 | } | |
| 1737 | match event { | |
| 1738 | WindowEvent::CloseRequested => event_loop.exit(), | |
| 1739 | WindowEvent::Focused(focused) => { | |
| 1740 | ui::host::focus(&mut host.studio.ui, focused); | |
| 1741 | host.window.request_redraw(); | |
| 1742 | } | |
| 1743 | WindowEvent::ScaleFactorChanged { .. } => { | |
| 1744 | host.studio.renderer.clear_glyph_cache(); | |
| 1745 | host.window.request_redraw(); | |
| 1746 | } | |
| 1747 | WindowEvent::Occluded(occluded) => { | |
| 1748 | host.occluded = occluded; | |
| 1749 | if occluded { host.surface.wheel.cancel(); } | |
| 1750 | else { host.window.request_redraw(); } | |
| 1751 | } | |
| 1752 | // Route full physical + logical key events when a custom pane (Viewer/Flow/Spline) has focus! | |
| 1753 | WindowEvent::KeyboardInput { event: key_ev, is_synthetic: false, .. } | |
| 1754 | if host.studio.ui.full_key_input() => | |
| 1755 | { | |
| 1756 | let held = host.studio.ui.modifiers(); | |
| 1757 | host.input(ui::Event::from_key(key_ev, held)); | |
| 1758 | } | |
| 1759 | other => { | |
| 1760 | if let Some(input) = ui::Event::from_window(other, host.window.scale_factor() as f32, host.studio.ui.modifiers()) { | |
| 1761 | host.input(input); | |
| 1762 | } | |
| 1763 | } | |
| 1764 | } | |
| 1765 | } | |
| 1766 | ||
| 1767 | fn about_to_wait(&mut self, event_loop: &ActiveEventLoop) { | |
| 1768 | if let Some(host) = &self.host { | |
| 1769 | if !host.occluded { | |
| 1770 | if host.studio.playback.playing { | |
| 1771 | let wake = host.studio.playback.next_wake(&host.studio.comp); | |
| 1772 | event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + wake)); | |
| 1773 | host.window.request_redraw(); | |
| 1774 | return; | |
| 1775 | } | |
| 1776 | if host.studio.prefetcher.as_ref().is_some_and(RamPrefetcher::is_busy) { | |
| 1777 | event_loop.set_control_flow(ControlFlow::WaitUntil(Instant::now() + Duration::from_millis(200))); | |
| 1778 | host.window.request_redraw(); | |
| 1779 | return; | |
| 1780 | } | |
| 1781 | } | |
| 1782 | } | |
| 1783 | event_loop.set_control_flow(ControlFlow::Wait); | |
| 1784 | } | |
| 1785 | } | |
| 1786 | ||
| 1787 | fn spawn_audio_loader(comp: &Comp) -> Option<mpsc::Receiver<Result<AudioTrack, String>>> { | |
| 1788 | comp.audio_filename.as_ref().and_then(|af| { | |
| 1789 | let p = PathBuf::from(af); | |
| 1790 | if !p.exists() { | |
| 1791 | return None; | |
| 1792 | } | |
| 1793 | let (tx, rx) = mpsc::channel(); | |
| 1794 | std::thread::spawn(move || { | |
| 1795 | let _ = tx.send(AudioTrack::load(&p)); | |
| 1796 | }); | |
| 1797 | Some(rx) | |
| 1798 | }) | |
| 1799 | } | |
| 1800 | ||
| 1801 | fn studio_btn(ui: &mut ui::Ui, part: impl std::hash::Hash, label: &str, active: bool) -> ui::Signal { | |
| 1802 | let t = &ui.theme; | |
| 1803 | let fill = if active { | |
| 1804 | [t.accent[0], t.accent[1], t.accent[2], 0.24] | |
| 1805 | } else { | |
| 1806 | [t.base[0], t.base[1], t.base[2], 0.65] | |
| 1807 | }; | |
| 1808 | let border = if active { | |
| 1809 | [t.accent[0], t.accent[1], t.accent[2], 0.65] | |
| 1810 | } else { | |
| 1811 | t.rule | |
| 1812 | }; | |
| 1813 | let ink = if active { t.text } else { t.text_dim }; | |
| 1814 | ui.leaf( | |
| 1815 | part, | |
| 1816 | ui::Spec { | |
| 1817 | cursor: Some(CursorIcon::Pointer), | |
| 1818 | flags: ui::Flags::CLICKABLE, | |
| 1819 | size: [ui::fit(), ui::px(22.0)], | |
| 1820 | text: Some(label), | |
| 1821 | font_size: Some(11.0), | |
| 1822 | medium: true, | |
| 1823 | color: Some(ink), | |
| 1824 | fill: Some(fill), | |
| 1825 | border: Some(border), | |
| 1826 | hover_fill: Some(t.ghost_soft), | |
| 1827 | hover_border: Some(t.accent), | |
| 1828 | radius: 5.0, | |
| 1829 | pad: [7.0, 0.0], | |
| 1830 | center: true, | |
| 1831 | ..ui::Spec::default() | |
| 1832 | }, | |
| 1833 | ) | |
| 1834 | } | |
| 1835 | ||
| 1836 | fn native_open_comp_dialog() -> Option<PathBuf> { | |
| 1837 | let script = r#"POSIX path of (choose file with prompt "Open Fusion Composition" of type {"comp"})"#; | |
| 1838 | let out = ProcessCommand::new("osascript").args(["-e", script]).output().ok()?; | |
| 1839 | if !out.status.success() { | |
| 1840 | return None; | |
| 1841 | } | |
| 1842 | let s = String::from_utf8_lossy(&out.stdout).trim().to_owned(); | |
| 1843 | if s.is_empty() { None } else { Some(PathBuf::from(s)) } | |
| 1844 | } | |
| 1845 | ||
| 1846 | fn native_save_comp_dialog(default_name: &str) -> Option<PathBuf> { | |
| 1847 | let safe = default_name.replace('"', ""); | |
| 1848 | let script = format!( | |
| 1849 | r#"POSIX path of (choose file name with prompt "Save Fusion Composition As" default name "{safe}")"# | |
| 1850 | ); | |
| 1851 | let out = ProcessCommand::new("osascript").args(["-e", &script]).output().ok()?; | |
| 1852 | if !out.status.success() { | |
| 1853 | return None; | |
| 1854 | } | |
| 1855 | let s = String::from_utf8_lossy(&out.stdout).trim().to_owned(); | |
| 1856 | if s.is_empty() { None } else { Some(PathBuf::from(s)) } | |
| 1857 | } |
readme.md created+31| ... | ... | @@ -0,0 +1,31 @@ |
| 1 | # fission | |
| 2 | ||
| 3 | After using Fusion Studio for 8 years or so for my ... non-VFX artsy projects, I | |
| 4 | continue to hit extreme instability with the way I push the program. Fed up with | |
| 5 | crashes and slow comps, I've set AI to save my day. Fission is my experiment on | |
| 6 | a new compositor that you can easily drop in to bring higher performance. | |
| 7 | ||
| 8 | Currently targetting only the tools needed to render | |
| 9 | [paper clover](https://paperclover.net) videos. But more to come if that | |
| 10 | succeeds. | |
| 11 | ||
| 12 | ## Running | |
| 13 | ||
| 14 | ```sh | |
| 15 | # Desktop app | |
| 16 | cargo run --release --bin fission -- /path/to/shot.comp | |
| 17 | ||
| 18 | # Headless verification & export CLI | |
| 19 | cargo run --release --bin render-check -- /path/to/shot.comp --frame 0 --out /tmp/out.png | |
| 20 | ``` | |
| 21 | ||
| 22 | ## Architecture | |
| 23 | ||
| 24 | - `crates/model` — Lossless Fusion `.comp` parser/writer, splines, expressions, undo/redo history | |
| 25 | - `crates/engine` — `wgpu` DAG node evaluator (`eval/`), GPU compute shaders, 3D renderer, particles, optical flow, Cryptomatte, VRAM/RAM caches, audio, and export | |
| 26 | - `crates/flow` — Interactive Flow node-graph editor | |
| 27 | - `crates/spline` — Blender-style Bezier spline editor | |
| 28 | - `crates/viewer` — GPU Viewer pane, scrubber, and on-screen gizmos | |
| 29 | - `crates/app` — Docked desktop app (`fission` binary) built on Knife's UI kit | |
| 30 | ||
| 31 | See [compatibility.md](compatibility.md) for the auto-generated Fusion 21.1 tool matrix and [Shale](https://shale.paperclover.net/fission/issues) for tracked issues. |