| 1 | //! Label layout and primitive painting for [`KeyframesView`]. |
| 2 | |
| 3 | use super::*; |
| 4 | |
| 5 | impl KeyframesView { |
| 6 | pub fn prepare_labels(&mut self) { |
| 7 | self.sanitize_view(); |
| 8 | let cap = TEXT_CACHE_CAP.max(self.rows.len() * 2 + 128); |
| 9 | if self.text_cache.len() > cap { |
| 10 | self.text_cache.clear(); |
| 11 | } |
| 12 | let font_sm = text::Font { |
| 13 | families: Arc::from([text::Family::Mono]), |
| 14 | size: 10.0, |
| 15 | weight: 500, |
| 16 | italic: false, |
| 17 | }; |
| 18 | let font_row = text::Font { |
| 19 | families: Arc::from([text::Family::Sans]), |
| 20 | size: 11.0, |
| 21 | weight: 500, |
| 22 | italic: false, |
| 23 | }; |
| 24 | let font_hdr = text::Font { |
| 25 | families: Arc::from([text::Family::Sans]), |
| 26 | size: 11.0, |
| 27 | weight: 600, |
| 28 | italic: false, |
| 29 | }; |
| 30 | let make = |s: &str, font: &text::Font| { |
| 31 | let styles = [text::Style::new(font.clone())]; |
| 32 | text::Paragraph { |
| 33 | text: s, |
| 34 | styles: &styles, |
| 35 | spans: &[], |
| 36 | replacements: &[], |
| 37 | width: None, |
| 38 | indent: 0.0, |
| 39 | line_height: 1.2, |
| 40 | align: text::Align::Left, |
| 41 | tab: 32.0, |
| 42 | } |
| 43 | .layout() |
| 44 | }; |
| 45 | |
| 46 | for (k, txt, f) in [ |
| 47 | ("hdr:title", "Keyframes", &font_hdr), |
| 48 | ("hdr:all", "All", &font_sm), |
| 49 | ("hdr:sel", "Sel", &font_sm), |
| 50 | ("hdr:audio", "Audio", &font_row), |
| 51 | ("footer", "Drag keys/spans to retime · Ruler: Scrub · Wheel: Zoom · Del: Remove · F: Fit", &font_sm), |
| 52 | ] { |
| 53 | self.text_cache.entry(k.to_owned()).or_insert_with(|| make(txt, f)); |
| 54 | } |
| 55 | |
| 56 | for row in &self.rows { |
| 57 | match row { |
| 58 | TrackRow::NodeHeader { node_id, label, .. } => { |
| 59 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; |
| 60 | let text = format!("{glyph} {label}"); |
| 61 | self.text_cache.entry(format!("nh:{text}")).or_insert_with(|| make(&text, &font_hdr)); |
| 62 | } |
| 63 | TrackRow::Channel { label, .. } => { |
| 64 | self.text_cache.entry(format!("ch:{label}")).or_insert_with(|| make(label, &font_row)); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | let plot = self.plot_rect(); |
| 70 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); |
| 71 | if let Some((t_step, t_start, t_end)) = grid_ticks(self.t_range, max_ticks) { |
| 72 | for ti in t_start..=t_end { |
| 73 | let key = format!("t:{}", (ti as f64 * t_step).round() as i64); |
| 74 | self.text_cache.entry(key.clone()).or_insert_with(|| make(&key[2..], &font_sm)); |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | pub fn paint<'a>(&'a self, comp: &'a Comp, current_frame: f64) -> Vec<Primitive<'a>> { |
| 80 | let mut out = Vec::with_capacity(512); |
| 81 | let w = if self.size[0].is_finite() { self.size[0].max(1.0) } else { 1.0 }; |
| 82 | let h = if self.size[1].is_finite() { self.size[1].max(1.0) } else { 1.0 }; |
| 83 | let plot = self.plot_rect(); |
| 84 | let top = self.tracks_top(); |
| 85 | |
| 86 | // Sidebar + Ruler chrome |
| 87 | out.push(Primitive::Rect { rect: [0.0, 0.0, SIDEBAR_W, h], color: self.palette.sidebar }); |
| 88 | out.push(Primitive::Rect { rect: [SIDEBAR_W, 0.0, w, RULER_H], color: self.palette.ruler }); |
| 89 | out.push(Primitive::Segment { from: [SIDEBAR_W, 0.0], to: [SIDEBAR_W, h], width: 1.0, color: self.palette.rule }); |
| 90 | out.push(Primitive::Segment { from: [0.0, RULER_H], to: [w, RULER_H], width: 1.0, color: self.palette.rule }); |
| 91 | |
| 92 | if let Some(tl) = self.text_cache.get("hdr:title") { |
| 93 | out.push(Primitive::Text { |
| 94 | text: tl as &dyn Glyphs, |
| 95 | origin: [10.0, 6.0], |
| 96 | clip: Some([8.0, 0.0, SIDEBAR_W - 52.0, RULER_H]), |
| 97 | ink: self.palette.text, |
| 98 | }); |
| 99 | } |
| 100 | // "All / Sel" filter pill |
| 101 | let pill_rect = [SIDEBAR_W - 46.0, 4.0, SIDEBAR_W - 8.0, RULER_H - 4.0]; |
| 102 | out.push(Primitive::RoundedRect { |
| 103 | rect: pill_rect, |
| 104 | radius: 4.0, |
| 105 | stroke: None, |
| 106 | color: if self.selected_only { |
| 107 | [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.28] |
| 108 | } else { |
| 109 | [self.palette.bg[0], self.palette.bg[1], self.palette.bg[2], 0.7] |
| 110 | }, |
| 111 | }); |
| 112 | out.push(Primitive::RoundedRect { |
| 113 | rect: pill_rect, |
| 114 | radius: 4.0, |
| 115 | stroke: Some(1.0), |
| 116 | color: if self.selected_only { self.palette.accent } else { self.palette.rule }, |
| 117 | }); |
| 118 | let mode_key = if self.selected_only { "hdr:sel" } else { "hdr:all" }; |
| 119 | if let Some(ml) = self.text_cache.get(mode_key) { |
| 120 | out.push(Primitive::Text { |
| 121 | text: ml as &dyn Glyphs, |
| 122 | origin: [pill_rect[0] + 9.0, 7.0], |
| 123 | clip: Some(pill_rect), |
| 124 | ink: if self.selected_only { self.palette.text } else { self.palette.text_dim }, |
| 125 | }); |
| 126 | } |
| 127 | |
| 128 | // Ruler frame ticks |
| 129 | let max_ticks = f64::from(((plot[2] - plot[0]) / 56.0).max(2.0)); |
| 130 | if let Some((t_step, t_start, t_end)) = grid_ticks(self.t_range, max_ticks) { |
| 131 | for ti in t_start..=t_end { |
| 132 | let t = ti as f64 * t_step; |
| 133 | let x = self.time_to_x(t); |
| 134 | if x < plot[0] || x > plot[2] { |
| 135 | continue; |
| 136 | } |
| 137 | out.push(Primitive::Segment { from: [x, RULER_H], to: [x, plot[3]], width: 1.0, color: self.palette.grid }); |
| 138 | let key = format!("t:{}", t.round() as i64); |
| 139 | if let Some(tl) = self.text_cache.get(&key) { |
| 140 | out.push(Primitive::Text { |
| 141 | text: tl as &dyn Glyphs, |
| 142 | origin: [x + 3.0, 5.0], |
| 143 | clip: Some([plot[0], 0.0, plot[2], RULER_H]), |
| 144 | ink: self.palette.text_dim, |
| 145 | }); |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Green RAM cache bar along bottom edge of ruler |
| 151 | for &(c0, c1) in &self.cached_ranges { |
| 152 | let x0 = self.time_to_x(c0 as f64).max(plot[0]); |
| 153 | let x1 = self.time_to_x((c1 + 1) as f64).min(plot[2]); |
| 154 | if x1 > x0 { |
| 155 | out.push(Primitive::Rect { |
| 156 | rect: [x0, RULER_H - 3.5, x1.max(x0 + 2.0), RULER_H - 0.5], |
| 157 | color: [0.22, 0.86, 0.48, 0.92], |
| 158 | }); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Dedicated Audio Waveform row when audio is present |
| 163 | if !self.audio_peaks.is_empty() { |
| 164 | let ay0 = RULER_H; |
| 165 | let ay1 = RULER_H + AUDIO_ROW_H; |
| 166 | out.push(Primitive::Rect { rect: [0.0, ay0, w, ay1], color: self.palette.row_alt }); |
| 167 | out.push(Primitive::Segment { from: [0.0, ay1], to: [w, ay1], width: 1.0, color: self.palette.rule }); |
| 168 | if let Some(al) = self.text_cache.get("hdr:audio") { |
| 169 | out.push(Primitive::Text { |
| 170 | text: al as &dyn Glyphs, |
| 171 | origin: [12.0, ay0 + 5.0], |
| 172 | clip: Some([8.0, ay0, SIDEBAR_W - 8.0, ay1]), |
| 173 | ink: [0.54, 0.80, 1.0, 0.95], |
| 174 | }); |
| 175 | } |
| 176 | out.push(Primitive::PushClip { rect: [plot[0], ay0, plot[2], ay1], radius: 0.0 }); |
| 177 | let mid_y = 0.5 * (ay0 + ay1); |
| 178 | let max_half = AUDIO_ROW_H * 0.42; |
| 179 | let a0 = self.audio_range[0]; |
| 180 | let n = self.audio_peaks.len() as i64; |
| 181 | let [tr0, tr1] = sanitize_t_range(self.t_range); |
| 182 | let f_vis0 = (tr0.floor() as i64).max(a0); |
| 183 | let f_vis1 = (tr1.ceil() as i64).min(a0.saturating_add(n - 1)); |
| 184 | let bar_step = ((plot[2] - plot[0]) / ((tr1 - tr0).max(1.0) as f32)).max(1.5); |
| 185 | let stride = ((1.8 / bar_step).ceil() as i64).max(1); |
| 186 | let mut f = f_vis0; |
| 187 | while f <= f_vis1 { |
| 188 | let mut peak = 0.0_f32; |
| 189 | for off in 0..stride { |
| 190 | if let Some(idx) = f.saturating_add(off).checked_sub(a0).and_then(|d| usize::try_from(d).ok()) { |
| 191 | if let Some(&v) = self.audio_peaks.get(idx) { |
| 192 | if v > peak { peak = v; } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | if peak > 0.01 { |
| 197 | let x = self.time_to_x(f as f64); |
| 198 | let hh = (peak * max_half).max(1.0); |
| 199 | let bw = (bar_step * stride as f32 - 0.6).max(1.2); |
| 200 | out.push(Primitive::Rect { |
| 201 | rect: [x, mid_y - hh, x + bw, mid_y + hh], |
| 202 | color: [0.46, 0.74, 0.98, 0.55], |
| 203 | }); |
| 204 | } |
| 205 | f += stride; |
| 206 | } |
| 207 | out.push(Primitive::PopClip); |
| 208 | } |
| 209 | |
| 210 | // Render range subtle shading |
| 211 | let rx0 = self.time_to_x(comp.render_range[0] as f64); |
| 212 | let rx1 = self.time_to_x(comp.render_range[1] as f64); |
| 213 | out.push(Primitive::PushClip { rect: [plot[0], top, plot[2], plot[3]], radius: 0.0 }); |
| 214 | if rx0 > plot[0] { |
| 215 | out.push(Primitive::Rect { rect: [plot[0], top, rx0.min(plot[2]), plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); |
| 216 | } |
| 217 | if rx1 < plot[2] { |
| 218 | out.push(Primitive::Rect { rect: [rx1.max(plot[0]), top, plot[2], plot[3]], color: [0.0, 0.0, 0.0, 0.16] }); |
| 219 | } |
| 220 | out.push(Primitive::PopClip); |
| 221 | |
| 222 | // Track rows (clipped to track area) |
| 223 | out.push(Primitive::PushClip { rect: [0.0, top, w, plot[3]], radius: 0.0 }); |
| 224 | for (ri, row) in self.rows.iter().enumerate() { |
| 225 | let y0 = self.row_y(ri); |
| 226 | let y1 = y0 + ROW_H; |
| 227 | if y1 < top || y0 > plot[3] { |
| 228 | continue; |
| 229 | } |
| 230 | if ri % 2 == 1 { |
| 231 | out.push(Primitive::Rect { rect: [0.0, y0, w, y1], color: self.palette.row_alt }); |
| 232 | } |
| 233 | out.push(Primitive::Segment { from: [0.0, y1], to: [w, y1], width: 1.0, color: self.palette.grid }); |
| 234 | |
| 235 | match row { |
| 236 | TrackRow::NodeHeader { node_id, label, splines } => { |
| 237 | let glyph = if self.collapsed_nodes.contains(node_id) { "▸" } else { "▾" }; |
| 238 | let key = format!("nh:{glyph} {label}"); |
| 239 | if let Some(tl) = self.text_cache.get(&key) { |
| 240 | out.push(Primitive::Text { |
| 241 | text: tl as &dyn Glyphs, |
| 242 | origin: [10.0, y0 + 4.0], |
| 243 | clip: Some([6.0, y0, SIDEBAR_W - 6.0, y1]), |
| 244 | ink: self.palette.text, |
| 245 | }); |
| 246 | } |
| 247 | // Summary span + tick dots on node header row |
| 248 | let mut t_min = f64::INFINITY; |
| 249 | let mut t_max = f64::NEG_INFINITY; |
| 250 | for sname in splines { |
| 251 | if let Some(sp) = comp.splines.get(sname) { |
| 252 | for kf in &sp.keyframes { |
| 253 | t_min = t_min.min(kf.time); |
| 254 | t_max = t_max.max(kf.time); |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | if t_min.is_finite() { |
| 259 | let sx0 = self.time_to_x(t_min).max(plot[0]); |
| 260 | let sx1 = self.time_to_x(t_max).min(plot[2]); |
| 261 | if sx1 >= sx0 { |
| 262 | out.push(Primitive::RoundedRect { |
| 263 | rect: [sx0, y0 + 6.0, sx1.max(sx0 + 4.0), y1 - 6.0], |
| 264 | radius: 3.0, |
| 265 | stroke: None, |
| 266 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.22], |
| 267 | }); |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | TrackRow::Channel { label, spline, color, .. } => { |
| 272 | out.push(Primitive::RoundedRect { |
| 273 | rect: [20.0, y0 + 7.0, 28.0, y0 + 15.0], |
| 274 | radius: 2.0, |
| 275 | stroke: None, |
| 276 | color: *color, |
| 277 | }); |
| 278 | if let Some(tl) = self.text_cache.get(&format!("ch:{label}")) { |
| 279 | out.push(Primitive::Text { |
| 280 | text: tl as &dyn Glyphs, |
| 281 | origin: [34.0, y0 + 4.0], |
| 282 | clip: Some([32.0, y0, SIDEBAR_W - 6.0, y1]), |
| 283 | ink: self.palette.text_dim, |
| 284 | }); |
| 285 | } |
| 286 | |
| 287 | if let Some(sp) = comp.splines.get(spline) { |
| 288 | if sp.keyframes.len() >= 2 { |
| 289 | let sx0 = self.time_to_x(sp.keyframes.first().unwrap().time).max(plot[0]); |
| 290 | let sx1 = self.time_to_x(sp.keyframes.last().unwrap().time).min(plot[2]); |
| 291 | if sx1 > sx0 { |
| 292 | out.push(Primitive::RoundedRect { |
| 293 | rect: [sx0, y0 + 8.0, sx1, y1 - 8.0], |
| 294 | radius: 2.0, |
| 295 | stroke: None, |
| 296 | color: [color[0], color[1], color[2], 0.28], |
| 297 | }); |
| 298 | } |
| 299 | } |
| 300 | let cy = y0 + ROW_H * 0.5; |
| 301 | for (ki, kf) in sp.keyframes.iter().enumerate() { |
| 302 | let kx = self.time_to_x(kf.time); |
| 303 | if kx < plot[0] - 8.0 || kx > plot[2] + 8.0 { |
| 304 | continue; |
| 305 | } |
| 306 | let kref = DopeKeyRef { spline: spline.clone(), key_idx: ki }; |
| 307 | let sel = self.selected_keys.contains(&kref); |
| 308 | let hov = self.hovered_key.as_ref() == Some(&kref); |
| 309 | let r = if sel || hov { 5.0 } else { 4.0 }; |
| 310 | out.push(Primitive::RoundedRect { |
| 311 | rect: [kx - r, cy - r, kx + r, cy + r], |
| 312 | radius: 1.5, |
| 313 | stroke: None, |
| 314 | color: if sel { |
| 315 | [1.0, 0.84, 0.24, 1.0] |
| 316 | } else if hov { |
| 317 | [1.0, 1.0, 1.0, 1.0] |
| 318 | } else { |
| 319 | *color |
| 320 | }, |
| 321 | }); |
| 322 | out.push(Primitive::RoundedRect { |
| 323 | rect: [kx - r, cy - r, kx + r, cy + r], |
| 324 | radius: 1.5, |
| 325 | stroke: Some(1.0), |
| 326 | color: [0.04, 0.04, 0.06, 0.9], |
| 327 | }); |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // Box select overlay |
| 335 | if let Some((a, b)) = self.box_select { |
| 336 | let x0 = a[0].min(b[0]); |
| 337 | let x1 = a[0].max(b[0]); |
| 338 | let y0 = a[1].min(b[1]); |
| 339 | let y1 = a[1].max(b[1]); |
| 340 | out.push(Primitive::RoundedRect { |
| 341 | rect: [x0, y0, x1, y1], |
| 342 | radius: 2.0, |
| 343 | stroke: None, |
| 344 | color: [self.palette.accent[0], self.palette.accent[1], self.palette.accent[2], 0.16], |
| 345 | }); |
| 346 | out.push(Primitive::RoundedRect { |
| 347 | rect: [x0, y0, x1, y1], |
| 348 | radius: 2.0, |
| 349 | stroke: Some(1.2), |
| 350 | color: self.palette.accent, |
| 351 | }); |
| 352 | } |
| 353 | out.push(Primitive::PopClip); |
| 354 | |
| 355 | // Playhead vertical line |
| 356 | let px = self.time_to_x(current_frame); |
| 357 | if px >= plot[0] && px <= plot[2] { |
| 358 | out.push(Primitive::Segment { |
| 359 | from: [px, 0.0], |
| 360 | to: [px, plot[3]], |
| 361 | width: 1.6, |
| 362 | color: [0.96, 0.36, 0.34, 0.92], |
| 363 | }); |
| 364 | } |
| 365 | |
| 366 | // Footer bar |
| 367 | out.push(Primitive::Rect { rect: [SIDEBAR_W, h - FOOTER_H, w, h], color: self.palette.ruler }); |
| 368 | if let Some(fl) = self.text_cache.get("footer") { |
| 369 | out.push(Primitive::Text { |
| 370 | text: fl as &dyn Glyphs, |
| 371 | origin: [SIDEBAR_W + 10.0, h - FOOTER_H + 5.0], |
| 372 | clip: Some([SIDEBAR_W + 6.0, h - FOOTER_H, w - 6.0, h]), |
| 373 | ink: self.palette.text_dim, |
| 374 | }); |
| 375 | } |
| 376 | |
| 377 | out |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | fn grid_ticks(range: [f64; 2], divs: f64) -> Option<(f64, i64, i64)> { |
| 382 | let [lo, hi] = sanitize_t_range(range); |
| 383 | let span = hi - lo; |
| 384 | if !span.is_finite() || span <= 0.0 { |
| 385 | return None; |
| 386 | } |
| 387 | let step = nice_step(span / divs); |
| 388 | if !step.is_finite() || step <= 0.0 { |
| 389 | return None; |
| 390 | } |
| 391 | let start = (lo / step).floor() as i64; |
| 392 | let end = (hi / step).ceil() as i64; |
| 393 | (end.saturating_sub(start) <= 256).then_some((step, start, end)) |
| 394 | } |
| 395 | |
| 396 | fn nice_step(raw: f64) -> f64 { |
| 397 | if !raw.is_finite() { |
| 398 | return 1.0; |
| 399 | } |
| 400 | let raw = raw.max(1.0); |
| 401 | let pow = 10.0_f64.powf(raw.log10().floor()); |
| 402 | let frac = raw / pow; |
| 403 | 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 }; |
| 404 | (nice * pow).max(1.0) |
| 405 | } |