| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | |
| 18 | |
| 19 | |
| 20 | #[derive(Clone, Copy, PartialEq, Eq, Debug)] |
| 21 | pub struct Clip { |
| 22 | pub in_frame: u32, |
| 23 | pub out_frame: u32, |
| 24 | } |
| 25 | |
| 26 | impl Clip { |
| 27 | |
| 28 | pub fn len(&self) -> u32 { |
| 29 | self.out_frame - self.in_frame |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | |
| 34 | #[derive(Clone, Debug)] |
| 35 | pub struct Project { |
| 36 | clips: Vec<Clip>, |
| 37 | |
| 38 | history: Vec<Vec<Clip>>, |
| 39 | } |
| 40 | |
| 41 | impl Project { |
| 42 | |
| 43 | |
| 44 | |
| 45 | pub fn single(frame_count: u32) -> Self { |
| 46 | Self { |
| 47 | clips: vec![Clip { |
| 48 | in_frame: 0, |
| 49 | out_frame: frame_count.max(1), |
| 50 | }], |
| 51 | history: Vec::new(), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | pub fn clips(&self) -> &[Clip] { |
| 56 | &self.clips |
| 57 | } |
| 58 | |
| 59 | |
| 60 | pub fn timeline_frames(&self) -> u32 { |
| 61 | self.clips.iter().map(Clip::len).sum() |
| 62 | } |
| 63 | |
| 64 | |
| 65 | |
| 66 | pub fn clip_spans(&self) -> Vec<(u32, u32)> { |
| 67 | let mut start = 0; |
| 68 | self.clips |
| 69 | .iter() |
| 70 | .map(|c| { |
| 71 | let span = (start, c.len()); |
| 72 | start += c.len(); |
| 73 | span |
| 74 | }) |
| 75 | .collect() |
| 76 | } |
| 77 | |
| 78 | |
| 79 | pub fn map_timeline(&self, timeline_frame: u32) -> Option<(usize, u32)> { |
| 80 | let mut start = 0; |
| 81 | for (i, c) in self.clips.iter().enumerate() { |
| 82 | if timeline_frame < start + c.len() { |
| 83 | return Some((i, c.in_frame + (timeline_frame - start))); |
| 84 | } |
| 85 | start += c.len(); |
| 86 | } |
| 87 | None |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | pub fn cut(&mut self, timeline_frame: u32) -> bool { |
| 95 | let Some((idx, source)) = self.map_timeline(timeline_frame) else { |
| 96 | return false; |
| 97 | }; |
| 98 | if source == self.clips[idx].in_frame { |
| 99 | return false; |
| 100 | } |
| 101 | self.history.push(self.clips.clone()); |
| 102 | let out = self.clips[idx].out_frame; |
| 103 | self.clips[idx].out_frame = source; |
| 104 | self.clips.insert( |
| 105 | idx + 1, |
| 106 | Clip { |
| 107 | in_frame: source, |
| 108 | out_frame: out, |
| 109 | }, |
| 110 | ); |
| 111 | true |
| 112 | } |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | pub fn paste(&mut self, timeline_frame: u32, clip: Clip) -> bool { |
| 126 | if clip.out_frame <= clip.in_frame { |
| 127 | return false; |
| 128 | } |
| 129 | self.history.push(self.clips.clone()); |
| 130 | match self.map_timeline(timeline_frame) { |
| 131 | Some((idx, source)) if source > self.clips[idx].in_frame => { |
| 132 | let out = self.clips[idx].out_frame; |
| 133 | self.clips[idx].out_frame = source; |
| 134 | self.clips.splice( |
| 135 | idx + 1..idx + 1, |
| 136 | [ |
| 137 | clip, |
| 138 | Clip { |
| 139 | in_frame: source, |
| 140 | out_frame: out, |
| 141 | }, |
| 142 | ], |
| 143 | ); |
| 144 | } |
| 145 | Some((idx, _)) => self.clips.insert(idx, clip), |
| 146 | None => self.clips.push(clip), |
| 147 | } |
| 148 | true |
| 149 | } |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | pub fn delete(&mut self, idx: usize) -> bool { |
| 155 | if idx >= self.clips.len() || self.clips.len() == 1 { |
| 156 | return false; |
| 157 | } |
| 158 | self.history.push(self.clips.clone()); |
| 159 | self.clips.remove(idx); |
| 160 | true |
| 161 | } |
| 162 | |
| 163 | |
| 164 | |
| 165 | pub fn undo(&mut self) -> bool { |
| 166 | match self.history.pop() { |
| 167 | Some(prev) => { |
| 168 | self.clips = prev; |
| 169 | true |
| 170 | } |
| 171 | None => false, |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | pub fn segments_from(&self, timeline_frame: u32, fps: f64) -> Vec<(f64, f64)> { |
| 180 | if !(fps.is_finite() && fps > 0.0) { |
| 181 | return Vec::new(); |
| 182 | } |
| 183 | let Some((idx, source)) = self.map_timeline(timeline_frame) else { |
| 184 | return Vec::new(); |
| 185 | }; |
| 186 | let mut segs = vec![(source as f64 / fps, self.clips[idx].out_frame as f64 / fps)]; |
| 187 | segs.extend( |
| 188 | self.clips[idx + 1..] |
| 189 | .iter() |
| 190 | .map(|c| (c.in_frame as f64 / fps, c.out_frame as f64 / fps)), |
| 191 | ); |
| 192 | segs |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | #[cfg(test)] |
| 197 | mod tests { |
| 198 | use super::*; |
| 199 | |
| 200 | const FPS: f64 = 30.0; |
| 201 | |
| 202 | |
| 203 | |
| 204 | fn three() -> Project { |
| 205 | let mut p = Project::single(9); |
| 206 | assert!(p.cut(3)); |
| 207 | assert!(p.cut(5)); |
| 208 | assert_eq!( |
| 209 | p.clips(), |
| 210 | [ |
| 211 | Clip { |
| 212 | in_frame: 0, |
| 213 | out_frame: 3 |
| 214 | }, |
| 215 | Clip { |
| 216 | in_frame: 3, |
| 217 | out_frame: 5 |
| 218 | }, |
| 219 | Clip { |
| 220 | in_frame: 5, |
| 221 | out_frame: 9 |
| 222 | }, |
| 223 | ] |
| 224 | ); |
| 225 | p |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn single_is_the_whole_file() { |
| 230 | let p = Project::single(150); |
| 231 | assert_eq!(p.clips().len(), 1); |
| 232 | assert_eq!(p.timeline_frames(), 150); |
| 233 | assert_eq!(p.clip_spans(), vec![(0, 150)]); |
| 234 | |
| 235 | assert_eq!(p.map_timeline(0), Some((0, 0))); |
| 236 | assert_eq!(p.map_timeline(149), Some((0, 149))); |
| 237 | assert_eq!(p.map_timeline(150), None); |
| 238 | |
| 239 | assert_eq!(Project::single(0).timeline_frames(), 1); |
| 240 | } |
| 241 | |
| 242 | #[test] |
| 243 | fn cut_mid_clip_splits() { |
| 244 | let mut p = Project::single(9); |
| 245 | assert!(p.cut(4)); |
| 246 | assert_eq!( |
| 247 | p.clips(), |
| 248 | [ |
| 249 | Clip { |
| 250 | in_frame: 0, |
| 251 | out_frame: 4 |
| 252 | }, |
| 253 | Clip { |
| 254 | in_frame: 4, |
| 255 | out_frame: 9 |
| 256 | }, |
| 257 | ] |
| 258 | ); |
| 259 | assert_eq!(p.timeline_frames(), 9); |
| 260 | } |
| 261 | |
| 262 | #[test] |
| 263 | fn cut_refused_at_zero_boundary_and_end() { |
| 264 | let mut p = three(); |
| 265 | let before = p.clips().to_vec(); |
| 266 | assert!(!p.cut(0), "timeline 0 has nothing before it"); |
| 267 | assert!(!p.cut(3), "existing boundary"); |
| 268 | assert!(!p.cut(5), "existing boundary"); |
| 269 | assert!(!p.cut(9), "one past the last frame"); |
| 270 | assert!(!p.cut(1_000)); |
| 271 | assert_eq!(p.clips(), before.as_slice()); |
| 272 | |
| 273 | assert!(p.undo()); |
| 274 | assert_eq!(p.clips().len(), 2, "refused cuts push no history"); |
| 275 | } |
| 276 | |
| 277 | #[test] |
| 278 | fn cut_never_changes_the_mapping() { |
| 279 | let p = three(); |
| 280 | let before: Vec<_> = (0..9).map(|f| p.map_timeline(f).unwrap().1).collect(); |
| 281 | let mut after = p.clone(); |
| 282 | assert!(after.cut(7)); |
| 283 | let now: Vec<_> = (0..9).map(|f| after.map_timeline(f).unwrap().1).collect(); |
| 284 | assert_eq!(before, now); |
| 285 | } |
| 286 | |
| 287 | #[test] |
| 288 | fn map_timeline_sweeps_every_boundary() { |
| 289 | let p = three(); |
| 290 | assert_eq!(p.timeline_frames(), 9); |
| 291 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4)]); |
| 292 | |
| 293 | let expect = [ |
| 294 | (0, (0, 0)), |
| 295 | (1, (0, 1)), |
| 296 | (2, (0, 2)), |
| 297 | (3, (1, 3)), |
| 298 | (4, (1, 4)), |
| 299 | (5, (2, 5)), |
| 300 | (6, (2, 6)), |
| 301 | (7, (2, 7)), |
| 302 | (8, (2, 8)), |
| 303 | ]; |
| 304 | for (t, want) in expect { |
| 305 | assert_eq!(p.map_timeline(t), Some(want), "timeline frame {t}"); |
| 306 | } |
| 307 | assert_eq!(p.map_timeline(9), None); |
| 308 | } |
| 309 | |
| 310 | |
| 311 | |
| 312 | const PASTED: Clip = Clip { |
| 313 | in_frame: 100, |
| 314 | out_frame: 102, |
| 315 | }; |
| 316 | |
| 317 | #[test] |
| 318 | fn paste_mid_clip_splits_around_it() { |
| 319 | let mut p = three(); |
| 320 | assert!(p.paste(6, PASTED)); |
| 321 | assert_eq!(p.timeline_frames(), 11); |
| 322 | assert_eq!( |
| 323 | p.clips(), |
| 324 | [ |
| 325 | Clip { |
| 326 | in_frame: 0, |
| 327 | out_frame: 3 |
| 328 | }, |
| 329 | Clip { |
| 330 | in_frame: 3, |
| 331 | out_frame: 5 |
| 332 | }, |
| 333 | Clip { |
| 334 | in_frame: 5, |
| 335 | out_frame: 6 |
| 336 | }, |
| 337 | PASTED, |
| 338 | Clip { |
| 339 | in_frame: 6, |
| 340 | out_frame: 9 |
| 341 | }, |
| 342 | ] |
| 343 | ); |
| 344 | |
| 345 | |
| 346 | assert_eq!(p.map_timeline(6), Some((3, 100))); |
| 347 | assert_eq!(p.map_timeline(7), Some((3, 101))); |
| 348 | assert_eq!(p.map_timeline(8), Some((4, 6))); |
| 349 | } |
| 350 | |
| 351 | #[test] |
| 352 | fn paste_at_boundary_zero_and_end() { |
| 353 | let mut p = three(); |
| 354 | assert!(p.paste(3, PASTED), "existing boundary: no split"); |
| 355 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 2), (7, 4)]); |
| 356 | assert_eq!(p.map_timeline(3), Some((1, 100))); |
| 357 | |
| 358 | let mut p = three(); |
| 359 | assert!(p.paste(0, PASTED)); |
| 360 | assert_eq!(p.map_timeline(0), Some((0, 100))); |
| 361 | assert_eq!(p.map_timeline(2), Some((1, 0))); |
| 362 | |
| 363 | |
| 364 | let mut p = three(); |
| 365 | assert!(p.paste(9, PASTED)); |
| 366 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 2), (5, 4), (9, 2)]); |
| 367 | assert!(p.paste(1_000, PASTED)); |
| 368 | assert_eq!(p.timeline_frames(), 13); |
| 369 | |
| 370 | |
| 371 | let mut p = three(); |
| 372 | assert!(!p.paste( |
| 373 | 4, |
| 374 | Clip { |
| 375 | in_frame: 7, |
| 376 | out_frame: 7 |
| 377 | } |
| 378 | )); |
| 379 | assert_eq!(p.clips().len(), 3); |
| 380 | } |
| 381 | |
| 382 | #[test] |
| 383 | fn paste_undoes_in_one_step() { |
| 384 | let mut p = three(); |
| 385 | let before = p.clips().to_vec(); |
| 386 | assert!(p.paste(6, PASTED)); |
| 387 | assert!(p.undo()); |
| 388 | assert_eq!(p.clips(), before.as_slice(), "a paste is one undo step"); |
| 389 | } |
| 390 | |
| 391 | #[test] |
| 392 | fn a_copy_outlives_the_clip_it_came_from() { |
| 393 | let mut p = three(); |
| 394 | let copied = p.clips()[1]; |
| 395 | assert!(p.delete(1)); |
| 396 | |
| 397 | assert!(p.paste(0, copied)); |
| 398 | assert_eq!(p.map_timeline(0), Some((0, 3))); |
| 399 | assert_eq!(p.timeline_frames(), 9); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn delete_closes_the_gap() { |
| 404 | let mut p = three(); |
| 405 | assert!(p.delete(1)); |
| 406 | assert_eq!(p.timeline_frames(), 7); |
| 407 | assert_eq!(p.clip_spans(), vec![(0, 3), (3, 4)]); |
| 408 | |
| 409 | assert_eq!(p.map_timeline(3), Some((1, 5))); |
| 410 | assert_eq!(p.map_timeline(6), Some((1, 8))); |
| 411 | assert_eq!(p.map_timeline(7), None); |
| 412 | } |
| 413 | |
| 414 | #[test] |
| 415 | fn delete_first_last_and_only() { |
| 416 | let mut p = three(); |
| 417 | assert!(p.delete(0)); |
| 418 | assert_eq!(p.map_timeline(0), Some((0, 3))); |
| 419 | assert_eq!(p.timeline_frames(), 6); |
| 420 | |
| 421 | let mut p = three(); |
| 422 | assert!(p.delete(2)); |
| 423 | assert_eq!(p.timeline_frames(), 5); |
| 424 | assert_eq!(p.map_timeline(4), Some((1, 4))); |
| 425 | |
| 426 | assert!(!p.delete(2), "index past the end"); |
| 427 | assert!(p.delete(1)); |
| 428 | assert!(!p.delete(0), "the last remaining clip stays"); |
| 429 | assert_eq!(p.clips().len(), 1); |
| 430 | } |
| 431 | |
| 432 | #[test] |
| 433 | fn undo_restores_exactly() { |
| 434 | let mut p = three(); |
| 435 | let after_cuts = p.clips().to_vec(); |
| 436 | assert!(p.delete(1)); |
| 437 | assert!(p.undo()); |
| 438 | assert_eq!(p.clips(), after_cuts.as_slice()); |
| 439 | |
| 440 | |
| 441 | assert!(p.undo()); |
| 442 | assert_eq!(p.clips().len(), 2); |
| 443 | assert!(p.undo()); |
| 444 | assert_eq!( |
| 445 | p.clips(), |
| 446 | [Clip { |
| 447 | in_frame: 0, |
| 448 | out_frame: 9 |
| 449 | }] |
| 450 | ); |
| 451 | assert!(!p.undo(), "empty history"); |
| 452 | assert_eq!( |
| 453 | p.clips(), |
| 454 | [Clip { |
| 455 | in_frame: 0, |
| 456 | out_frame: 9 |
| 457 | }] |
| 458 | ); |
| 459 | } |
| 460 | |
| 461 | #[test] |
| 462 | fn segments_from_trims_the_first_entry() { |
| 463 | let p = three(); |
| 464 | |
| 465 | assert_eq!( |
| 466 | p.segments_from(4, FPS), |
| 467 | vec![(4.0 / 30.0, 5.0 / 30.0), (5.0 / 30.0, 9.0 / 30.0)] |
| 468 | ); |
| 469 | |
| 470 | assert_eq!( |
| 471 | p.segments_from(3, FPS), |
| 472 | vec![(3.0 / 30.0, 5.0 / 30.0), (5.0 / 30.0, 9.0 / 30.0)] |
| 473 | ); |
| 474 | |
| 475 | assert_eq!(p.segments_from(0, FPS).len(), 3); |
| 476 | |
| 477 | assert_eq!(p.segments_from(8, FPS), vec![(8.0 / 30.0, 0.3)]); |
| 478 | |
| 479 | assert!(p.segments_from(9, FPS).is_empty()); |
| 480 | assert!(p.segments_from(0, 0.0).is_empty()); |
| 481 | assert!(p.segments_from(0, f64::NAN).is_empty()); |
| 482 | } |
| 483 | |
| 484 | #[test] |
| 485 | fn segments_follow_deletes_and_fps() { |
| 486 | let mut p = three(); |
| 487 | assert!(p.delete(0)); |
| 488 | |
| 489 | let segs = p.segments_from(0, 25.0); |
| 490 | assert_eq!(segs, vec![(3.0 / 25.0, 0.2), (0.2, 9.0 / 25.0)]); |
| 491 | let played: f64 = segs.iter().map(|(a, b)| b - a).sum(); |
| 492 | assert!( |
| 493 | (played - p.timeline_frames() as f64 / 25.0).abs() < 1e-9, |
| 494 | "segments must cover exactly the timeline: {played}" |
| 495 | ); |
| 496 | } |
| 497 | } |