| 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 | mod paint; |
| 5 | |
| 6 | use draw::{Glyphs, Primitive}; |
| 7 | use model::{Comp, CompHistory, Keyframe, NodeId}; |
| 8 | use std::collections::{HashMap, HashSet}; |
| 9 | use std::hash::{Hash, Hasher}; |
| 10 | use std::sync::Arc; |
| 11 | use winit::event::{ElementState, MouseButton}; |
| 12 | use winit::keyboard::{Key, KeyCode, ModifiersState, NamedKey, PhysicalKey}; |
| 13 | |
| 14 | /// Layout entries kept before the label cache is rebuilt. |
| 15 | const TEXT_CACHE_CAP: usize = 512; |
| 16 | const RULER_H: f32 = 26.0; |
| 17 | const AUDIO_ROW_H: f32 = 24.0; |
| 18 | const ROW_H: f32 = 22.0; |
| 19 | const SIDEBAR_W: f32 = 188.0; |
| 20 | const FOOTER_H: f32 = 22.0; |
| 21 | |
| 22 | #[derive(Clone, Debug, PartialEq, Eq, Hash)] |
| 23 | pub struct DopeKeyRef { |
| 24 | pub spline: String, |
| 25 | pub key_idx: usize, |
| 26 | } |
| 27 | |
| 28 | #[derive(Clone, Debug)] |
| 29 | pub enum KeyframesAction { |
| 30 | ScrubFrame(f64), |
| 31 | SelectNode(NodeId), |
| 32 | KeyframesEdited, |
| 33 | } |
| 34 | |
| 35 | #[derive(Clone, Debug)] |
| 36 | pub enum TrackRow { |
| 37 | NodeHeader { |
| 38 | node_id: NodeId, |
| 39 | label: String, |
| 40 | splines: Vec<String>, |
| 41 | }, |
| 42 | Channel { |
| 43 | node_id: NodeId, |
| 44 | label: String, |
| 45 | spline: String, |
| 46 | color: [f32; 4], |
| 47 | }, |
| 48 | } |
| 49 | |
| 50 | #[derive(Clone, Debug)] |
| 51 | struct RetimeDrag { |
| 52 | start_frame: f64, |
| 53 | saved_splines: HashMap<String, Vec<Keyframe>>, |
| 54 | /// Selection when the drag began; indices refer to `saved_splines`. |
| 55 | orig_selected: HashSet<DopeKeyRef>, |
| 56 | pre_edit_comp: Comp, |
| 57 | moved: bool, |
| 58 | } |
| 59 | |
| 60 | #[derive(Clone, Copy, Debug)] |
| 61 | pub struct KeyframesPalette { |
| 62 | pub bg: [f32; 4], |
| 63 | pub sidebar: [f32; 4], |
| 64 | pub ruler: [f32; 4], |
| 65 | pub rule: [f32; 4], |
| 66 | pub grid: [f32; 4], |
| 67 | pub row_alt: [f32; 4], |
| 68 | pub text: [f32; 4], |
| 69 | pub text_dim: [f32; 4], |
| 70 | pub accent: [f32; 4], |
| 71 | } |
| 72 | |
| 73 | impl KeyframesPalette { |
| 74 | pub fn from_ui(theme: &ui::Theme) -> Self { |
| 75 | let b = theme.base; |
| 76 | let lift = |c: [f32; 4], d: f32| { |
| 77 | [(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] |
| 78 | }; |
| 79 | Self { |
| 80 | bg: b, |
| 81 | sidebar: lift(b, 0.012), |
| 82 | ruler: lift(b, 0.018), |
| 83 | rule: theme.rule, |
| 84 | grid: lift(b, 0.022), |
| 85 | row_alt: lift(b, 0.010), |
| 86 | text: theme.text, |
| 87 | text_dim: theme.text_dim, |
| 88 | accent: theme.accent, |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 94 | struct TracksSyncKey { |
| 95 | revision: u64, |
| 96 | node_count: usize, |
| 97 | spline_count: usize, |
| 98 | comp_hash: u64, |
| 99 | selected_only: bool, |
| 100 | selected_node: Option<NodeId>, |
| 101 | collapsed_epoch: u64, |
| 102 | } |
| 103 | |
| 104 | fn comp_identity_hash(comp: &Comp) -> u64 { |
| 105 | let mut h = std::collections::hash_map::DefaultHasher::new(); |
| 106 | comp.name.hash(&mut h); |
| 107 | comp.path.hash(&mut h); |
| 108 | h.finish() |
| 109 | } |
| 110 | |
| 111 | pub struct KeyframesView { |
| 112 | pub t_range: [f64; 2], |
| 113 | pub size: [f32; 2], |
| 114 | pub scroll_y: f32, |
| 115 | pub selected_only: bool, |
| 116 | pub selected_keys: HashSet<DopeKeyRef>, |
| 117 | pub cached_ranges: Vec<(i64, i64)>, |
| 118 | pub audio_peaks: Vec<f32>, |
| 119 | pub audio_range: [i64; 2], |
| 120 | pub palette: KeyframesPalette, |
| 121 | rows: Vec<TrackRow>, |
| 122 | collapsed_nodes: HashSet<NodeId>, |
| 123 | collapsed_epoch: u64, |
| 124 | tracks_key: Option<TracksSyncKey>, |
| 125 | hovered_key: Option<DopeKeyRef>, |
| 126 | retime_drag: Option<RetimeDrag>, |
| 127 | box_select: Option<([f32; 2], [f32; 2])>, |
| 128 | panning: Option<([f32; 2], [f64; 2], f32)>, |
| 129 | scrubbing: bool, |
| 130 | pointer_local: [f32; 2], |
| 131 | fitted: bool, |
| 132 | text_cache: HashMap<String, text::Layout>, |
| 133 | } |
| 134 | |
| 135 | impl KeyframesView { |
| 136 | pub fn new(theme: &ui::Theme) -> Self { |
| 137 | Self { |
| 138 | t_range: [0.0, 200.0], |
| 139 | size: [800.0, 320.0], |
| 140 | scroll_y: 0.0, |
| 141 | selected_only: false, |
| 142 | selected_keys: HashSet::new(), |
| 143 | cached_ranges: Vec::new(), |
| 144 | audio_peaks: Vec::new(), |
| 145 | audio_range: [0, 0], |
| 146 | palette: KeyframesPalette::from_ui(theme), |
| 147 | rows: Vec::new(), |
| 148 | collapsed_nodes: HashSet::new(), |
| 149 | collapsed_epoch: 0, |
| 150 | tracks_key: None, |
| 151 | hovered_key: None, |
| 152 | retime_drag: None, |
| 153 | box_select: None, |
| 154 | panning: None, |
| 155 | scrubbing: false, |
| 156 | pointer_local: [0.0, 0.0], |
| 157 | fitted: false, |
| 158 | text_cache: HashMap::new(), |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | pub fn background(&self) -> [f32; 4] { |
| 163 | self.palette.bg |
| 164 | } |
| 165 | |
| 166 | pub fn resize(&mut self, size: [f32; 2]) { |
| 167 | self.size = [ |
| 168 | if size[0].is_finite() { size[0].max(1.0) } else { 1.0 }, |
| 169 | if size[1].is_finite() { size[1].max(1.0) } else { 1.0 }, |
| 170 | ]; |
| 171 | } |
| 172 | |
| 173 | pub fn fit_comp(&mut self, comp: &Comp) { |
| 174 | let r0 = comp.render_range[0] as f64; |
| 175 | let r1 = comp.render_range[1].max(comp.render_range[0].saturating_add(10)) as f64; |
| 176 | let pad = ((r1 - r0) * 0.04).max(4.0); |
| 177 | self.t_range = sanitize_t_range([r0 - pad, r1 + pad]); |
| 178 | self.fitted = true; |
| 179 | } |
| 180 | |
| 181 | fn sanitize_view(&mut self) { |
| 182 | self.resize(self.size); |
| 183 | self.t_range = sanitize_t_range(self.t_range); |
| 184 | if !self.scroll_y.is_finite() || self.scroll_y < 0.0 { |
| 185 | self.scroll_y = 0.0; |
| 186 | } |
| 187 | if !self.pointer_local[0].is_finite() { |
| 188 | self.pointer_local[0] = 0.0; |
| 189 | } |
| 190 | if !self.pointer_local[1].is_finite() { |
| 191 | self.pointer_local[1] = 0.0; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | fn sanitize_stale(&mut self, comp: &Comp) { |
| 196 | self.sanitize_view(); |
| 197 | let before = self.collapsed_nodes.len(); |
| 198 | self.collapsed_nodes.retain(|id| id.0 < comp.nodes.len()); |
| 199 | if self.collapsed_nodes.len() != before { |
| 200 | self.collapsed_epoch += 1; |
| 201 | self.tracks_key = None; |
| 202 | } |
| 203 | self.selected_keys |
| 204 | .retain(|k| comp.splines.get(&k.spline).is_some_and(|sp| k.key_idx < sp.keyframes.len())); |
| 205 | if self.hovered_key.as_ref().is_some_and(|k| { |
| 206 | comp.splines.get(&k.spline).is_none_or(|sp| k.key_idx >= sp.keyframes.len()) |
| 207 | }) { |
| 208 | self.hovered_key = None; |
| 209 | } |
| 210 | if self.retime_drag.as_ref().is_some_and(|d| { |
| 211 | d.saved_splines.iter().any(|(sname, orig)| { |
| 212 | comp.splines.get(sname).is_none_or(|sp| sp.keyframes.len() != orig.len()) |
| 213 | }) |
| 214 | }) { |
| 215 | self.retime_drag = None; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// Drops node ids and key refs that no longer exist after undo, redo, delete, or reload. |
| 220 | pub fn sanitize_ids(&mut self, comp: &Comp) { |
| 221 | self.sanitize_stale(comp); |
| 222 | self.hovered_key = None; |
| 223 | self.retime_drag = None; |
| 224 | self.box_select = None; |
| 225 | } |
| 226 | |
| 227 | pub fn sync_tracks(&mut self, comp: &Comp, selected_node: Option<NodeId>) { |
| 228 | self.sanitize_stale(comp); |
| 229 | if !self.fitted { |
| 230 | self.fit_comp(comp); |
| 231 | } |
| 232 | let key = TracksSyncKey { |
| 233 | revision: comp.revision, |
| 234 | node_count: comp.nodes.len(), |
| 235 | spline_count: comp.splines.len(), |
| 236 | comp_hash: comp_identity_hash(comp), |
| 237 | selected_only: self.selected_only, |
| 238 | selected_node: if self.selected_only { selected_node } else { None }, |
| 239 | collapsed_epoch: self.collapsed_epoch, |
| 240 | }; |
| 241 | if self.tracks_key == Some(key) { |
| 242 | return; |
| 243 | } |
| 244 | self.rows = build_tracks(comp, selected_node, self.selected_only, &self.collapsed_nodes); |
| 245 | self.tracks_key = Some(key); |
| 246 | } |
| 247 | |
| 248 | fn tracks_top(&self) -> f32 { |
| 249 | RULER_H + if self.audio_peaks.is_empty() { 0.0 } else { AUDIO_ROW_H } |
| 250 | } |
| 251 | |
| 252 | fn plot_rect(&self) -> [f32; 4] { |
| 253 | [ |
| 254 | SIDEBAR_W, |
| 255 | RULER_H, |
| 256 | self.size[0].max(SIDEBAR_W + 40.0), |
| 257 | (self.size[1] - FOOTER_H).max(RULER_H + 40.0), |
| 258 | ] |
| 259 | } |
| 260 | |
| 261 | fn time_to_x(&self, t: f64) -> f32 { |
| 262 | let r = self.plot_rect(); |
| 263 | let frac = (t - self.t_range[0]) / (self.t_range[1] - self.t_range[0]).max(1e-6); |
| 264 | r[0] + frac as f32 * (r[2] - r[0]) |
| 265 | } |
| 266 | |
| 267 | fn x_to_time(&self, x: f32) -> f64 { |
| 268 | let r = self.plot_rect(); |
| 269 | let frac = f64::from((x - r[0]) / (r[2] - r[0]).max(1.0)); |
| 270 | self.t_range[0] + frac * (self.t_range[1] - self.t_range[0]) |
| 271 | } |
| 272 | |
| 273 | fn row_y(&self, row_idx: usize) -> f32 { |
| 274 | self.tracks_top() + row_idx as f32 * ROW_H - self.scroll_y |
| 275 | } |
| 276 | |
| 277 | fn hit_key(&self, comp: &Comp, local: [f32; 2]) -> Option<DopeKeyRef> { |
| 278 | let top = self.tracks_top(); |
| 279 | if local[0] < SIDEBAR_W || local[1] < top || local[1] > self.size[1] - FOOTER_H { |
| 280 | return None; |
| 281 | } |
| 282 | let mut best: Option<(f32, DopeKeyRef)> = None; |
| 283 | for (ri, row) in self.rows.iter().enumerate() { |
| 284 | let cy = self.row_y(ri) + ROW_H * 0.5; |
| 285 | if (local[1] - cy).abs() > ROW_H * 0.52 { |
| 286 | continue; |
| 287 | } |
| 288 | if let TrackRow::Channel { spline, .. } = row { |
| 289 | let Some(sp) = comp.splines.get(spline) else { continue }; |
| 290 | for (ki, kf) in sp.keyframes.iter().enumerate() { |
| 291 | let kx = self.time_to_x(kf.time); |
| 292 | let d = ((local[0] - kx).powi(2) + (local[1] - cy).powi(2)).sqrt(); |
| 293 | if d <= 9.5 && best.as_ref().is_none_or(|(bd, _)| d < *bd) { |
| 294 | best = Some((d, DopeKeyRef { spline: spline.clone(), key_idx: ki })); |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | best.map(|(_, k)| k) |
| 300 | } |
| 301 | |
| 302 | fn hit_span_bar(&self, comp: &Comp, local: [f32; 2]) -> Option<(String, Vec<usize>)> { |
| 303 | let top = self.tracks_top(); |
| 304 | if local[0] < SIDEBAR_W || local[1] < top || local[1] > self.size[1] - FOOTER_H { |
| 305 | return None; |
| 306 | } |
| 307 | for (ri, row) in self.rows.iter().enumerate() { |
| 308 | let y0 = self.row_y(ri); |
| 309 | if local[1] < y0 || local[1] >= y0 + ROW_H { |
| 310 | continue; |
| 311 | } |
| 312 | if let TrackRow::Channel { spline, .. } = row { |
| 313 | let Some(sp) = comp.splines.get(spline) else { continue }; |
| 314 | if sp.keyframes.len() >= 2 { |
| 315 | let x0 = self.time_to_x(sp.keyframes.first().unwrap().time); |
| 316 | let x1 = self.time_to_x(sp.keyframes.last().unwrap().time); |
| 317 | if local[0] >= x0 && local[0] <= x1 { |
| 318 | return Some((spline.clone(), (0..sp.keyframes.len()).collect())); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | None |
| 324 | } |
| 325 | |
| 326 | fn begin_retime_drag(&mut self, comp: &Comp) { |
| 327 | if self.selected_keys.is_empty() { |
| 328 | return; |
| 329 | } |
| 330 | let mut saved = HashMap::new(); |
| 331 | for kref in &self.selected_keys { |
| 332 | if let Some(sp) = comp.splines.get(&kref.spline) { |
| 333 | saved.entry(kref.spline.clone()).or_insert_with(|| sp.keyframes.clone()); |
| 334 | } |
| 335 | } |
| 336 | self.retime_drag = Some(RetimeDrag { |
| 337 | start_frame: self.x_to_time(self.pointer_local[0]).round(), |
| 338 | saved_splines: saved, |
| 339 | orig_selected: self.selected_keys.clone(), |
| 340 | pre_edit_comp: comp.clone(), |
| 341 | moved: false, |
| 342 | }); |
| 343 | } |
| 344 | |
| 345 | fn apply_retime_drag(&mut self, comp: &mut Comp) -> bool { |
| 346 | let cur_frame = self.x_to_time(self.pointer_local[0]).round(); |
| 347 | let Some(drag) = &mut self.retime_drag else { return false }; |
| 348 | let dt = cur_frame - drag.start_frame; |
| 349 | if dt.abs() < 0.5 && !drag.moved { |
| 350 | return false; |
| 351 | } |
| 352 | drag.moved = true; |
| 353 | self.selected_keys = retime_selected_keys(comp, &drag.saved_splines, &drag.orig_selected, dt); |
| 354 | true |
| 355 | } |
| 356 | |
| 357 | fn finish_retime_drag(&mut self, comp: &mut Comp, history: &mut CompHistory) -> bool { |
| 358 | let Some(drag) = self.retime_drag.take() else { return false }; |
| 359 | if !drag.moved { |
| 360 | return false; |
| 361 | } |
| 362 | comp.revision += 1; |
| 363 | history.push_snapshot(&drag.pre_edit_comp, "Retime Keyframes"); |
| 364 | true |
| 365 | } |
| 366 | |
| 367 | fn cancel_retime_drag(&mut self, comp: &mut Comp) -> bool { |
| 368 | let Some(drag) = self.retime_drag.take() else { return false }; |
| 369 | for (sname, keyframes) in drag.saved_splines { |
| 370 | if let Some(sp) = comp.splines.get_mut(&sname) { |
| 371 | sp.keyframes = keyframes; |
| 372 | } |
| 373 | } |
| 374 | self.selected_keys = drag.orig_selected; |
| 375 | if drag.moved { |
| 376 | comp.revision += 1; |
| 377 | } |
| 378 | drag.moved |
| 379 | } |
| 380 | |
| 381 | fn delete_selected(&mut self, comp: &mut Comp, history: &mut CompHistory) -> bool { |
| 382 | if self.selected_keys.is_empty() { |
| 383 | return false; |
| 384 | } |
| 385 | let mut by_spline: HashMap<String, Vec<usize>> = HashMap::new(); |
| 386 | for kref in &self.selected_keys { |
| 387 | by_spline.entry(kref.spline.clone()).or_default().push(kref.key_idx); |
| 388 | } |
| 389 | let pre = comp.clone(); |
| 390 | let mut changed = false; |
| 391 | for (sname, mut idxs) in by_spline { |
| 392 | idxs.sort_unstable(); |
| 393 | idxs.dedup(); |
| 394 | if let Some(sp) = comp.splines.get_mut(&sname) { |
| 395 | for &idx in idxs.iter().rev() { |
| 396 | if sp.keyframes.len() > 1 && idx < sp.keyframes.len() { |
| 397 | sp.keyframes.remove(idx); |
| 398 | changed = true; |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | } |
| 403 | if changed { |
| 404 | self.selected_keys.clear(); |
| 405 | comp.revision += 1; |
| 406 | history.push_snapshot(&pre, "Delete Keyframes"); |
| 407 | } |
| 408 | changed |
| 409 | } |
| 410 | |
| 411 | pub fn input( |
| 412 | &mut self, |
| 413 | comp: &mut Comp, |
| 414 | history: &mut CompHistory, |
| 415 | selected_node: Option<NodeId>, |
| 416 | event: ui::Event, |
| 417 | rect: [f32; 4], |
| 418 | pointer_window: [f32; 2], |
| 419 | modifiers: ModifiersState, |
| 420 | ) -> Vec<KeyframesAction> { |
| 421 | let mut actions = Vec::new(); |
| 422 | self.pointer_local = [pointer_window[0] - rect[0], pointer_window[1] - rect[1]]; |
| 423 | self.sync_tracks(comp, selected_node); |
| 424 | let plot = self.plot_rect(); |
| 425 | |
| 426 | match event { |
| 427 | ui::Event::PointerMoved(p) => { |
| 428 | self.pointer_local = [p[0] - rect[0], p[1] - rect[1]]; |
| 429 | if self.scrubbing { |
| 430 | actions.push(KeyframesAction::ScrubFrame(self.x_to_time(self.pointer_local[0]).round())); |
| 431 | } else if let Some((sp, tr, sy)) = self.panning { |
| 432 | let dx = f64::from((self.pointer_local[0] - sp[0]) / (plot[2] - plot[0]).max(1.0)); |
| 433 | let dt = dx * (tr[1] - tr[0]); |
| 434 | self.t_range = [tr[0] - dt, tr[1] - dt]; |
| 435 | self.scroll_y = (sy - (self.pointer_local[1] - sp[1])).max(0.0); |
| 436 | } else if self.retime_drag.is_some() { |
| 437 | if self.apply_retime_drag(comp) { |
| 438 | actions.push(KeyframesAction::KeyframesEdited); |
| 439 | } |
| 440 | } else if let Some((_, ref mut cur)) = self.box_select { |
| 441 | *cur = self.pointer_local; |
| 442 | } else { |
| 443 | self.hovered_key = self.hit_key(comp, self.pointer_local); |
| 444 | } |
| 445 | } |
| 446 | ui::Event::Button { button, pressed, .. } => { |
| 447 | if button == MouseButton::Middle || (button == MouseButton::Left && modifiers.alt_key()) { |
| 448 | if pressed { |
| 449 | self.panning = Some((self.pointer_local, self.t_range, self.scroll_y)); |
| 450 | } else { |
| 451 | self.panning = None; |
| 452 | } |
| 453 | } else if button == MouseButton::Left { |
| 454 | if pressed { |
| 455 | // Sidebar header "All / Sel" toggle pill |
| 456 | if self.pointer_local[0] >= SIDEBAR_W - 48.0 |
| 457 | && self.pointer_local[0] <= SIDEBAR_W - 6.0 |
| 458 | && self.pointer_local[1] >= 4.0 |
| 459 | && self.pointer_local[1] <= RULER_H - 4.0 |
| 460 | { |
| 461 | self.selected_only = !self.selected_only; |
| 462 | self.scroll_y = 0.0; |
| 463 | self.sync_tracks(comp, selected_node); |
| 464 | } else if self.pointer_local[0] < SIDEBAR_W && self.pointer_local[1] >= self.tracks_top() { |
| 465 | let ri = ((self.pointer_local[1] - self.tracks_top() + self.scroll_y) / ROW_H).floor() as isize; |
| 466 | if ri >= 0 && (ri as usize) < self.rows.len() { |
| 467 | match &self.rows[ri as usize] { |
| 468 | TrackRow::NodeHeader { node_id, .. } => { |
| 469 | let id = *node_id; |
| 470 | if self.pointer_local[0] < 24.0 { |
| 471 | if !self.collapsed_nodes.remove(&id) { |
| 472 | self.collapsed_nodes.insert(id); |
| 473 | } |
| 474 | self.collapsed_epoch += 1; |
| 475 | self.sync_tracks(comp, selected_node); |
| 476 | } else { |
| 477 | actions.push(KeyframesAction::SelectNode(id)); |
| 478 | } |
| 479 | } |
| 480 | TrackRow::Channel { node_id, .. } => { |
| 481 | actions.push(KeyframesAction::SelectNode(*node_id)); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | } else if self.pointer_local[0] >= SIDEBAR_W && self.pointer_local[1] < RULER_H { |
| 486 | self.scrubbing = true; |
| 487 | actions.push(KeyframesAction::ScrubFrame(self.x_to_time(self.pointer_local[0]).round())); |
| 488 | } else if let Some(hit) = self.hit_key(comp, self.pointer_local) { |
| 489 | if modifiers.shift_key() { |
| 490 | if !self.selected_keys.remove(&hit) { |
| 491 | self.selected_keys.insert(hit); |
| 492 | } |
| 493 | } else if !self.selected_keys.contains(&hit) { |
| 494 | self.selected_keys.clear(); |
| 495 | self.selected_keys.insert(hit); |
| 496 | } |
| 497 | self.begin_retime_drag(comp); |
| 498 | } else if let Some((sname, idxs)) = self.hit_span_bar(comp, self.pointer_local) { |
| 499 | if !modifiers.shift_key() { |
| 500 | self.selected_keys.clear(); |
| 501 | } |
| 502 | for ki in idxs { |
| 503 | self.selected_keys.insert(DopeKeyRef { spline: sname.clone(), key_idx: ki }); |
| 504 | } |
| 505 | self.begin_retime_drag(comp); |
| 506 | } else if self.pointer_local[0] >= SIDEBAR_W { |
| 507 | if !modifiers.shift_key() { |
| 508 | self.selected_keys.clear(); |
| 509 | } |
| 510 | self.box_select = Some((self.pointer_local, self.pointer_local)); |
| 511 | } |
| 512 | } else { |
| 513 | self.scrubbing = false; |
| 514 | if self.finish_retime_drag(comp, history) { |
| 515 | actions.push(KeyframesAction::KeyframesEdited); |
| 516 | } |
| 517 | if let Some((a, b)) = self.box_select.take() { |
| 518 | let x0 = a[0].min(b[0]); |
| 519 | let x1 = a[0].max(b[0]); |
| 520 | let y0 = a[1].min(b[1]); |
| 521 | let y1 = a[1].max(b[1]); |
| 522 | if (x1 - x0) > 4.0 || (y1 - y0) > 4.0 { |
| 523 | for (ri, row) in self.rows.iter().enumerate() { |
| 524 | let cy = self.row_y(ri) + ROW_H * 0.5; |
| 525 | if cy < y0 || cy > y1 { |
| 526 | continue; |
| 527 | } |
| 528 | if let TrackRow::Channel { spline, .. } = row { |
| 529 | if let Some(sp) = comp.splines.get(spline) { |
| 530 | for (ki, kf) in sp.keyframes.iter().enumerate() { |
| 531 | let kx = self.time_to_x(kf.time); |
| 532 | if kx >= x0 && kx <= x1 { |
| 533 | self.selected_keys.insert(DopeKeyRef { |
| 534 | spline: spline.clone(), |
| 535 | key_idx: ki, |
| 536 | }); |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | ui::Event::Wheel(delta) => { |
| 548 | if self.pointer_local[0] < SIDEBAR_W || modifiers.shift_key() { |
| 549 | let max_scroll = (self.rows.len() as f32 * ROW_H - (self.size[1] - self.tracks_top() - FOOTER_H)).max(0.0); |
| 550 | self.scroll_y = (self.scroll_y - delta[1]).clamp(0.0, max_scroll); |
| 551 | } else { |
| 552 | let f = f64::from((1.0 - delta[1] * 0.0045).clamp(0.7, 1.4)); |
| 553 | let p = self.x_to_time(self.pointer_local[0]); |
| 554 | let t0 = p - (p - self.t_range[0]) * f; |
| 555 | let t1 = p + (self.t_range[1] - p) * f; |
| 556 | if (t1 - t0) > 4.0 && (t1 - t0) < 100_000.0 { |
| 557 | self.t_range = [t0, t1]; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | ui::Event::KeyInput { physical, key, state: ElementState::Pressed, repeat: false, modifiers: kmod, .. } => { |
| 562 | if physical == PhysicalKey::Code(KeyCode::NumpadDecimal) { |
| 563 | self.fit_comp(comp); |
| 564 | } else { |
| 565 | self.handle_key(comp, history, &key, kmod, &mut actions); |
| 566 | } |
| 567 | } |
| 568 | ui::Event::Key { key, modifiers: kmod, .. } => { |
| 569 | self.handle_key(comp, history, &key, kmod, &mut actions); |
| 570 | } |
| 571 | _ => {} |
| 572 | } |
| 573 | |
| 574 | actions |
| 575 | } |
| 576 | |
| 577 | fn handle_key( |
| 578 | &mut self, |
| 579 | comp: &mut Comp, |
| 580 | history: &mut CompHistory, |
| 581 | key: &Key, |
| 582 | kmod: ModifiersState, |
| 583 | actions: &mut Vec<KeyframesAction>, |
| 584 | ) { |
| 585 | let plain = !kmod.super_key() && !kmod.control_key() && !kmod.alt_key(); |
| 586 | match key { |
| 587 | Key::Named(NamedKey::Escape) => { |
| 588 | if self.cancel_retime_drag(comp) { |
| 589 | actions.push(KeyframesAction::KeyframesEdited); |
| 590 | } |
| 591 | } |
| 592 | Key::Named(NamedKey::Delete | NamedKey::Backspace) => { |
| 593 | if self.delete_selected(comp, history) { |
| 594 | actions.push(KeyframesAction::KeyframesEdited); |
| 595 | } |
| 596 | } |
| 597 | Key::Character(c) => match c.to_ascii_lowercase().as_str() { |
| 598 | "f" | "." if plain => self.fit_comp(comp), |
| 599 | "x" if plain => { |
| 600 | if self.delete_selected(comp, history) { |
| 601 | actions.push(KeyframesAction::KeyframesEdited); |
| 602 | } |
| 603 | } |
| 604 | _ => {} |
| 605 | }, |
| 606 | _ => {} |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | fn sanitize_t_range(r: [f64; 2]) -> [f64; 2] { |
| 612 | if !r[0].is_finite() || !r[1].is_finite() || r[0] >= r[1] || !(r[1] - r[0]).is_finite() { |
| 613 | return [0.0, 200.0]; |
| 614 | } |
| 615 | if r[1] - r[0] < 1e-3 { |
| 616 | let mid = 0.5 * (r[0] + r[1]); |
| 617 | if (mid - 0.5).is_finite() && (mid + 0.5).is_finite() { |
| 618 | return [mid - 0.5, mid + 0.5]; |
| 619 | } |
| 620 | return [0.0, 200.0]; |
| 621 | } |
| 622 | r |
| 623 | } |
| 624 | |
| 625 | pub fn build_tracks( |
| 626 | comp: &Comp, |
| 627 | selected_node: Option<NodeId>, |
| 628 | selected_only: bool, |
| 629 | collapsed: &HashSet<NodeId>, |
| 630 | ) -> Vec<TrackRow> { |
| 631 | let mut rows = Vec::new(); |
| 632 | let palette: &[[f32; 4]] = &[ |
| 633 | [0.98, 0.44, 0.62, 1.0], |
| 634 | [0.36, 0.84, 0.96, 1.0], |
| 635 | [0.50, 0.92, 0.46, 1.0], |
| 636 | [0.98, 0.78, 0.28, 1.0], |
| 637 | [0.74, 0.56, 0.98, 1.0], |
| 638 | ]; |
| 639 | let mut color_idx = 0usize; |
| 640 | |
| 641 | let mut push_node = |id: NodeId| { |
| 642 | // Group-internal tools are not on the root flow, so they cannot be selected from here. |
| 643 | if comp.nodes.get(id.0).is_none_or(|n| n.internal) { |
| 644 | return; |
| 645 | } |
| 646 | let channels: Vec<(String, String)> = comp |
| 647 | .node_splines(id) |
| 648 | .into_iter() |
| 649 | .filter(|(_, sname)| !sname.contains("LUTIn")) |
| 650 | .collect(); |
| 651 | if channels.is_empty() { |
| 652 | return; |
| 653 | } |
| 654 | let node = comp.node(id); |
| 655 | let spline_names: Vec<String> = channels.iter().map(|(_, s)| s.clone()).collect(); |
| 656 | rows.push(TrackRow::NodeHeader { |
| 657 | node_id: id, |
| 658 | label: node.name.clone(), |
| 659 | splines: spline_names, |
| 660 | }); |
| 661 | if !collapsed.contains(&id) { |
| 662 | for (label, spline) in channels { |
| 663 | let col = comp |
| 664 | .splines |
| 665 | .get(&spline) |
| 666 | .map(|sp| { |
| 667 | let [r, g, b] = sp.color; |
| 668 | if r as u16 + g as u16 + b as u16 > 120 { |
| 669 | [r as f32 / 255.0, g as f32 / 255.0, b as f32 / 255.0, 1.0] |
| 670 | } else { |
| 671 | palette[color_idx % palette.len()] |
| 672 | } |
| 673 | }) |
| 674 | .unwrap_or_else(|| palette[color_idx % palette.len()]); |
| 675 | color_idx += 1; |
| 676 | rows.push(TrackRow::Channel { |
| 677 | node_id: id, |
| 678 | label, |
| 679 | spline, |
| 680 | color: col, |
| 681 | }); |
| 682 | } |
| 683 | } |
| 684 | }; |
| 685 | |
| 686 | if selected_only { |
| 687 | if let Some(id) = selected_node { |
| 688 | push_node(id); |
| 689 | } |
| 690 | } else { |
| 691 | for n in &comp.nodes { |
| 692 | push_node(n.id); |
| 693 | } |
| 694 | } |
| 695 | rows |
| 696 | } |
| 697 | |
| 698 | pub fn retime_selected_keys( |
| 699 | comp: &mut Comp, |
| 700 | saved_splines: &HashMap<String, Vec<Keyframe>>, |
| 701 | selected: &HashSet<DopeKeyRef>, |
| 702 | dt: f64, |
| 703 | ) -> HashSet<DopeKeyRef> { |
| 704 | for (sname, orig) in saved_splines { |
| 705 | if let Some(sp) = comp.splines.get_mut(sname) { |
| 706 | sp.keyframes = orig.clone(); |
| 707 | } |
| 708 | } |
| 709 | for kref in selected { |
| 710 | let Some(sp) = comp.splines.get_mut(&kref.spline) else { continue }; |
| 711 | let Some(kf) = sp.keyframes.get_mut(kref.key_idx) else { continue }; |
| 712 | kf.time += dt; |
| 713 | if let Some(lh) = &mut kf.lh { |
| 714 | lh[0] += dt; |
| 715 | } |
| 716 | if let Some(rh) = &mut kf.rh { |
| 717 | rh[0] += dt; |
| 718 | } |
| 719 | } |
| 720 | // Keep curves sorted while dragging; the returned selection follows its keys to their new slots. |
| 721 | let remaps: HashMap<&String, Vec<usize>> = saved_splines |
| 722 | .keys() |
| 723 | .filter_map(|sname| Some((sname, spline::sort_keyframes(comp.splines.get_mut(sname)?)))) |
| 724 | .collect(); |
| 725 | comp.revision += 1; |
| 726 | selected |
| 727 | .iter() |
| 728 | .map(|k| DopeKeyRef { |
| 729 | spline: k.spline.clone(), |
| 730 | key_idx: remaps.get(&k.spline).and_then(|r| r.get(k.key_idx)).copied().unwrap_or(k.key_idx), |
| 731 | }) |
| 732 | .collect() |
| 733 | } |
| 734 | |
| 735 | #[cfg(test)] |
| 736 | mod tests { |
| 737 | use super::*; |
| 738 | |
| 739 | fn sample_animated_comp() -> Comp { |
| 740 | let src = r#"Composition { |
| 741 | CurrentTime = 10, |
| 742 | RenderRange = { 0, 100 }, |
| 743 | GlobalRange = { 0, 100 }, |
| 744 | Tools = ordered() { |
| 745 | XF1 = Transform { |
| 746 | Inputs = { |
| 747 | Size = Input { SourceOp = "XF1Size", Source = "Value", }, |
| 748 | }, |
| 749 | }, |
| 750 | XF1Size = BezierSpline { |
| 751 | SplineColor = { Red = 220, Green = 120, Blue = 80 }, |
| 752 | KeyFrames = { |
| 753 | [10] = { 1.0, RH = { 20.0, 1.0 }, Flags = { Linear = false } }, |
| 754 | [40] = { 2.0, LH = { 30.0, 2.0 }, Flags = { Linear = false } }, |
| 755 | }, |
| 756 | }, |
| 757 | }, |
| 758 | }"#; |
| 759 | Comp::from_fusion_str(src).expect("parse animated comp") |
| 760 | } |
| 761 | |
| 762 | #[test] |
| 763 | fn build_tracks_lists_animated_nodes_and_channels() { |
| 764 | let comp = sample_animated_comp(); |
| 765 | let xf = comp.find_node("XF1").unwrap(); |
| 766 | let rows = build_tracks(&comp, Some(xf), false, &HashSet::new()); |
| 767 | assert_eq!(rows.len(), 2); |
| 768 | assert!(matches!(&rows[0], TrackRow::NodeHeader { label, .. } if label == "XF1")); |
| 769 | assert!(matches!(&rows[1], TrackRow::Channel { spline, .. } if spline == "XF1Size")); |
| 770 | |
| 771 | let mut collapsed = HashSet::new(); |
| 772 | collapsed.insert(xf); |
| 773 | let collapsed_rows = build_tracks(&comp, Some(xf), false, &collapsed); |
| 774 | assert_eq!(collapsed_rows.len(), 1); |
| 775 | } |
| 776 | |
| 777 | #[test] |
| 778 | fn retime_shifts_key_and_handles_together_with_undo() { |
| 779 | let mut comp = sample_animated_comp(); |
| 780 | let mut history = CompHistory::new(); |
| 781 | let pre = comp.clone(); |
| 782 | let mut saved = HashMap::new(); |
| 783 | saved.insert("XF1Size".to_owned(), comp.splines["XF1Size"].keyframes.clone()); |
| 784 | let mut sel = HashSet::new(); |
| 785 | sel.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 1 }); |
| 786 | |
| 787 | retime_selected_keys(&mut comp, &saved, &sel, 15.0); |
| 788 | history.push_snapshot(&pre, "Retime Keyframes"); |
| 789 | |
| 790 | let kf1 = &comp.splines["XF1Size"].keyframes[1]; |
| 791 | assert!((kf1.time - 55.0).abs() < 1e-6); |
| 792 | assert!((kf1.lh.unwrap()[0] - 45.0).abs() < 1e-6, "left handle must shift with key"); |
| 793 | |
| 794 | assert!(history.undo(&mut comp)); |
| 795 | assert!((comp.splines["XF1Size"].keyframes[1].time - 40.0).abs() < 1e-6); |
| 796 | } |
| 797 | |
| 798 | #[test] |
| 799 | fn escape_cancels_an_active_retime_drag() { |
| 800 | let mut comp = sample_animated_comp(); |
| 801 | let mut history = CompHistory::new(); |
| 802 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 803 | view.resize([800.0, 300.0]); |
| 804 | let key = DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 0 }; |
| 805 | view.selected_keys.insert(key.clone()); |
| 806 | view.begin_retime_drag(&comp); |
| 807 | view.pointer_local[0] += 60.0; |
| 808 | assert!(view.apply_retime_drag(&mut comp)); |
| 809 | assert_ne!(comp.splines["XF1Size"].keyframes[0].time, 10.0); |
| 810 | |
| 811 | let mut actions = Vec::new(); |
| 812 | view.handle_key(&mut comp, &mut history, &Key::Named(NamedKey::Escape), ModifiersState::empty(), &mut actions); |
| 813 | |
| 814 | assert_eq!(comp.splines["XF1Size"].keyframes[0].time, 10.0); |
| 815 | assert!(view.retime_drag.is_none()); |
| 816 | assert_eq!(view.selected_keys, HashSet::from([key])); |
| 817 | assert_eq!(actions.len(), 1); |
| 818 | } |
| 819 | |
| 820 | #[test] |
| 821 | fn retime_past_a_neighbour_keeps_curve_sorted_and_selection_on_the_key() { |
| 822 | let mut comp = sample_animated_comp(); |
| 823 | let saved = HashMap::from([("XF1Size".to_owned(), comp.splines["XF1Size"].keyframes.clone())]); |
| 824 | let sel = HashSet::from([DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 0 }]); |
| 825 | |
| 826 | let moved = retime_selected_keys(&mut comp, &saved, &sel, 50.0); |
| 827 | |
| 828 | let times: Vec<f64> = comp.splines["XF1Size"].keyframes.iter().map(|k| k.time).collect(); |
| 829 | assert_eq!(times, [40.0, 60.0]); |
| 830 | assert_eq!(moved, HashSet::from([DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 1 }])); |
| 831 | } |
| 832 | |
| 833 | #[test] |
| 834 | fn group_internal_tools_get_no_tracks() { |
| 835 | let comp = Comp::from_fusion_str( |
| 836 | r#"Composition { |
| 837 | Tools = ordered() { |
| 838 | Outer = GroupOperator { |
| 839 | ViewInfo = GroupInfo { Pos = { 0, 0 } }, |
| 840 | Tools = ordered() { |
| 841 | Inner = Blur { |
| 842 | Inputs = { Size = Input { SourceOp = "InnerSize", Source = "Value", }, }, |
| 843 | }, |
| 844 | InnerSize = BezierSpline { |
| 845 | KeyFrames = { |
| 846 | [0] = { 1.0, Flags = { Linear = true } }, |
| 847 | [10] = { 2.0, Flags = { Linear = true } }, |
| 848 | }, |
| 849 | }, |
| 850 | }, |
| 851 | }, |
| 852 | }, |
| 853 | }"#, |
| 854 | ) |
| 855 | .expect("group comp"); |
| 856 | let inner = comp.nodes.iter().find(|n| n.internal && n.name == "Inner").expect("internal tool").id; |
| 857 | assert!(!comp.node_splines(inner).is_empty()); |
| 858 | |
| 859 | assert!(build_tracks(&comp, None, false, &HashSet::new()).is_empty()); |
| 860 | assert!(build_tracks(&comp, Some(inner), true, &HashSet::new()).is_empty()); |
| 861 | } |
| 862 | |
| 863 | #[test] |
| 864 | fn sanitize_drops_refs_to_vanished_nodes_and_keys() { |
| 865 | let comp = sample_animated_comp(); |
| 866 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 867 | view.collapsed_nodes.insert(NodeId(0)); |
| 868 | view.collapsed_nodes.insert(NodeId(7)); |
| 869 | view.selected_keys.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 1 }); |
| 870 | view.selected_keys.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 5 }); |
| 871 | view.selected_keys.insert(DopeKeyRef { spline: "Gone".to_owned(), key_idx: 0 }); |
| 872 | |
| 873 | view.sanitize_ids(&comp); |
| 874 | |
| 875 | assert_eq!(view.collapsed_nodes, HashSet::from([NodeId(0)])); |
| 876 | assert_eq!(view.selected_keys, HashSet::from([DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 1 }])); |
| 877 | } |
| 878 | |
| 879 | #[test] |
| 880 | fn letter_shortcuts_ignore_ctrl_alt_and_cmd() { |
| 881 | let mut comp = sample_animated_comp(); |
| 882 | let mut history = CompHistory::new(); |
| 883 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 884 | view.selected_keys.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 0 }); |
| 885 | for mods in [ModifiersState::CONTROL, ModifiersState::ALT, ModifiersState::SUPER] { |
| 886 | let mut acts = Vec::new(); |
| 887 | view.handle_key(&mut comp, &mut history, &Key::Character("x".into()), mods, &mut acts); |
| 888 | assert!(acts.is_empty(), "x with {mods:?}"); |
| 889 | } |
| 890 | assert_eq!(comp.splines["XF1Size"].keyframes.len(), 2); |
| 891 | } |
| 892 | |
| 893 | #[test] |
| 894 | fn sync_tracks_is_stable_across_unchanged_frames() { |
| 895 | let mut comp = sample_animated_comp(); |
| 896 | let xf = comp.find_node("XF1").unwrap(); |
| 897 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 898 | view.sync_tracks(&comp, Some(xf)); |
| 899 | let ptr0 = view.rows.as_ptr(); |
| 900 | let len0 = view.rows.len(); |
| 901 | assert_eq!(len0, 2); |
| 902 | |
| 903 | // Unchanged comp.revision and mode: rows buffer pointer must stay identical (zero rebuild/alloc). |
| 904 | for _ in 0..10 { |
| 905 | view.sync_tracks(&comp, Some(xf)); |
| 906 | assert_eq!(view.rows.as_ptr(), ptr0); |
| 907 | assert_eq!(view.rows.len(), len0); |
| 908 | } |
| 909 | // In All mode, changing selected_node does not affect tracks and must not rebuild. |
| 910 | view.sync_tracks(&comp, None); |
| 911 | assert_eq!(view.rows.as_ptr(), ptr0); |
| 912 | |
| 913 | // Toggling collapsed state or bumping comp.revision invalidates and rebuilds. |
| 914 | view.collapsed_nodes.insert(xf); |
| 915 | view.collapsed_epoch += 1; |
| 916 | view.sync_tracks(&comp, Some(xf)); |
| 917 | assert_eq!(view.rows.len(), 1); |
| 918 | |
| 919 | view.collapsed_nodes.clear(); |
| 920 | view.collapsed_epoch += 1; |
| 921 | comp.revision += 1; |
| 922 | view.sync_tracks(&comp, Some(xf)); |
| 923 | assert_eq!(view.rows.len(), 2); |
| 924 | |
| 925 | // Switching to selected_only with None clears tracks, then Some(xf) restores them. |
| 926 | view.selected_only = true; |
| 927 | view.sync_tracks(&comp, None); |
| 928 | assert!(view.rows.is_empty()); |
| 929 | view.sync_tracks(&comp, Some(xf)); |
| 930 | assert_eq!(view.rows.len(), 2); |
| 931 | } |
| 932 | |
| 933 | #[test] |
| 934 | fn paint_culls_offscreen_keyframe_diamonds() { |
| 935 | let comp = Comp::from_fusion_str( |
| 936 | r#"Composition { |
| 937 | RenderRange = { 0, 200 }, |
| 938 | Tools = ordered() { |
| 939 | XF1 = Transform { |
| 940 | Inputs = { Size = Input { SourceOp = "XF1Size", Source = "Value" } }, |
| 941 | }, |
| 942 | XF1Size = BezierSpline { |
| 943 | KeyFrames = { |
| 944 | [0] = { 1.0, Flags = { Linear = true } }, |
| 945 | [50] = { 2.0, Flags = { Linear = true } }, |
| 946 | [150] = { 3.0, Flags = { Linear = true } }, |
| 947 | }, |
| 948 | }, |
| 949 | }, |
| 950 | }"#, |
| 951 | ) |
| 952 | .unwrap(); |
| 953 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 954 | view.resize([800.0, 300.0]); |
| 955 | view.sync_tracks(&comp, None); |
| 956 | view.t_range = [40.0, 60.0]; |
| 957 | view.prepare_labels(); |
| 958 | let prims = view.paint(&comp, 50.0); |
| 959 | let plot = view.plot_rect(); |
| 960 | // Every keyframe diamond (radius 4 or 5 => width/height 8..10) must be within visible plot x-bounds. |
| 961 | let diamond_xs: Vec<f32> = prims |
| 962 | .iter() |
| 963 | .filter_map(|p| match p { |
| 964 | Primitive::RoundedRect { rect, stroke: Some(_), .. } |
| 965 | if (rect[2] - rect[0] - 8.0).abs() < 0.1 || (rect[2] - rect[0] - 10.0).abs() < 0.1 => |
| 966 | { |
| 967 | Some(0.5 * (rect[0] + rect[2])) |
| 968 | } |
| 969 | _ => None, |
| 970 | }) |
| 971 | .collect(); |
| 972 | assert_eq!(diamond_xs.len(), 1, "only frame 50 keyframe should be painted"); |
| 973 | assert!(diamond_xs[0] >= plot[0] - 8.0 && diamond_xs[0] <= plot[2] + 8.0); |
| 974 | } |
| 975 | |
| 976 | #[test] |
| 977 | fn large_row_count_does_not_thrash_text_cache() { |
| 978 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 979 | for i in 0..600 { |
| 980 | view.rows.push(TrackRow::Channel { |
| 981 | node_id: NodeId(0), |
| 982 | label: format!("Chan{i}"), |
| 983 | spline: format!("Sp{i}"), |
| 984 | color: [1.0, 1.0, 1.0, 1.0], |
| 985 | }); |
| 986 | } |
| 987 | view.prepare_labels(); |
| 988 | let len_after_first = view.text_cache.len(); |
| 989 | assert!(len_after_first > TEXT_CACHE_CAP); |
| 990 | let first_ptr = view.text_cache.get("ch:Chan0").unwrap() as *const text::Layout; |
| 991 | |
| 992 | view.prepare_labels(); |
| 993 | assert_eq!(view.text_cache.len(), len_after_first); |
| 994 | assert_eq!(view.text_cache.get("ch:Chan0").unwrap() as *const text::Layout, first_ptr); |
| 995 | } |
| 996 | |
| 997 | #[test] |
| 998 | fn bench_keyframes_sync_and_paint() { |
| 999 | let path = "/Volumes/Project/trees/fusion/30_ground_is_crazy001.comp"; |
| 1000 | let comp = if std::path::Path::new(path).exists() { |
| 1001 | Comp::from_fusion_str(&std::fs::read_to_string(path).unwrap()).unwrap() |
| 1002 | } else { |
| 1003 | sample_animated_comp() |
| 1004 | }; |
| 1005 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 1006 | view.resize([1280.0, 320.0]); |
| 1007 | view.sync_tracks(&comp, None); |
| 1008 | view.prepare_labels(); |
| 1009 | let mid = 0.5 * (view.t_range[0] + view.t_range[1]); |
| 1010 | std::hint::black_box(view.paint(&comp, mid).len()); |
| 1011 | |
| 1012 | let frames = 500; |
| 1013 | let t0 = std::time::Instant::now(); |
| 1014 | let mut total_prims = 0usize; |
| 1015 | for _ in 0..frames { |
| 1016 | view.sync_tracks(&comp, None); |
| 1017 | view.prepare_labels(); |
| 1018 | let prims = view.paint(&comp, mid); |
| 1019 | total_prims += prims.len(); |
| 1020 | std::hint::black_box(prims.len()); |
| 1021 | } |
| 1022 | let dt = t0.elapsed(); |
| 1023 | eprintln!( |
| 1024 | "[BENCH keyframes] rows={} frames={} total_ms={:.2} us_per_frame={:.2} avg_prims={}", |
| 1025 | view.rows.len(), |
| 1026 | frames, |
| 1027 | dt.as_secs_f64() * 1000.0, |
| 1028 | dt.as_secs_f64() * 1e6 / frames as f64, |
| 1029 | total_prims / frames |
| 1030 | ); |
| 1031 | } |
| 1032 | |
| 1033 | #[test] |
| 1034 | fn stale_retime_drag_empty_spline_zero_viewport_and_extreme_ranges_do_not_panic() { |
| 1035 | let comp = sample_animated_comp(); |
| 1036 | let mut history = CompHistory::new(); |
| 1037 | let mut view = KeyframesView::new(&ui::Theme::dark()); |
| 1038 | view.sync_tracks(&comp, None); |
| 1039 | view.selected_keys.insert(DopeKeyRef { spline: "XF1Size".to_owned(), key_idx: 0 }); |
| 1040 | view.begin_retime_drag(&comp); |
| 1041 | assert!(view.retime_drag.is_some()); |
| 1042 | |
| 1043 | // Replace comp mid-retime drag with an empty comp + 0-keyframe spline. |
| 1044 | let mut empty = Comp::from_fusion_str("Composition {}").expect("empty"); |
| 1045 | empty.splines.insert("Empty".to_owned(), model::BezierSpline { |
| 1046 | name: "Empty".to_owned(), |
| 1047 | color: [200, 200, 200], |
| 1048 | keyframes: Vec::new(), |
| 1049 | }); |
| 1050 | view.resize([0.0, 0.0]); |
| 1051 | view.t_range = [200.0, 0.0]; // inverted |
| 1052 | view.input( |
| 1053 | &mut empty, |
| 1054 | &mut history, |
| 1055 | Some(NodeId(99)), |
| 1056 | ui::Event::PointerMoved([220.0, 40.0]), |
| 1057 | [0.0, 0.0, 0.0, 0.0], |
| 1058 | [220.0, 40.0], |
| 1059 | ModifiersState::empty(), |
| 1060 | ); |
| 1061 | assert!(view.retime_drag.is_none()); |
| 1062 | assert!(view.t_range[0] < view.t_range[1]); |
| 1063 | |
| 1064 | view.t_range = [-1e308, 1e308]; |
| 1065 | view.prepare_labels(); |
| 1066 | let _ = view.paint(&empty, f64::NAN); |
| 1067 | } |
| 1068 | } |