| 1 | |
| 2 | |
| 3 | |
| 4 | use crate::*; |
| 5 | use engine::project::KeyKind; |
| 6 | use engine::transform::KeyEase; |
| 7 | |
| 8 | impl Player { |
| 9 | |
| 10 | pub(crate) fn key_frame(&self) -> u32 { |
| 11 | let fps = self.active_fps(); |
| 12 | self.session |
| 13 | .as_ref() |
| 14 | .map_or(0, |session| crate::subs::frame_at(session.now(), fps)) |
| 15 | } |
| 16 | |
| 17 | |
| 18 | |
| 19 | pub(crate) fn key_clip(&self, kind: KeyKind) -> Option<(Lane, usize)> { |
| 20 | match kind { |
| 21 | KeyKind::Placement | KeyKind::Opacity => self.transform_open, |
| 22 | KeyKind::Color => self.color_open, |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | |
| 27 | pub(crate) fn key_active(&self, kind: KeyKind) -> bool { |
| 28 | self.key_clip(kind) |
| 29 | .zip(self.session.as_ref()) |
| 30 | .is_some_and(|((lane, idx), session)| session.is_keyed(lane, idx, kind)) |
| 31 | } |
| 32 | |
| 33 | |
| 34 | pub(crate) fn key_here(&self, kind: KeyKind) -> Option<KeyEase> { |
| 35 | let (lane, idx) = self.key_clip(kind)?; |
| 36 | self.session |
| 37 | .as_ref()? |
| 38 | .key_ease_at(lane, idx, kind, self.key_frame()) |
| 39 | } |
| 40 | |
| 41 | |
| 42 | |
| 43 | pub(crate) fn toggle_key_here(&mut self, kind: KeyKind, cx: &mut Context<Self>) { |
| 44 | |
| 45 | |
| 46 | if self.key_clip(kind).is_none() { |
| 47 | match kind { |
| 48 | KeyKind::Color => self.open_color(cx), |
| 49 | _ => self.open_transform(cx), |
| 50 | } |
| 51 | } |
| 52 | let frame = self.key_frame(); |
| 53 | let Some((lane, idx)) = self.key_clip(kind) else { |
| 54 | return; |
| 55 | }; |
| 56 | let Some(session) = &mut self.session else { |
| 57 | return; |
| 58 | }; |
| 59 | self.pending_transform = None; |
| 60 | self.pending_color = None; |
| 61 | match session.toggle_key(lane, idx, kind, frame) { |
| 62 | Some(_) => { |
| 63 | self.mark_dirty(); |
| 64 | self.reset_after_reseek(); |
| 65 | } |
| 66 | None => self.notify_user("The playhead is not on this clip".into()), |
| 67 | } |
| 68 | cx.notify(); |
| 69 | } |
| 70 | |
| 71 | |
| 72 | |
| 73 | pub(crate) fn ease_kind(&self) -> KeyKind { |
| 74 | if self.color_open.is_some() { |
| 75 | KeyKind::Color |
| 76 | } else if self.key_here(KeyKind::Placement).is_none() && self.key_here(KeyKind::Opacity).is_some() { |
| 77 | KeyKind::Opacity |
| 78 | } else { |
| 79 | KeyKind::Placement |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | |
| 84 | |
| 85 | pub(crate) fn open_key_easing(&mut self, at: Point<Pixels>, cx: &mut Context<Self>) { |
| 86 | let kind = self.ease_kind(); |
| 87 | if self.key_here(kind).is_none() { |
| 88 | self.notify_user("No key on the playhead — add one with the diamond".into()); |
| 89 | cx.notify(); |
| 90 | return; |
| 91 | } |
| 92 | self.open_picker(Pick::KeyEase(kind), at, cx); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | pub(crate) fn set_key_ease_here(&mut self, kind: KeyKind, ease: KeyEase, cx: &mut Context<Self>) { |
| 97 | let frame = self.key_frame(); |
| 98 | if let Some((lane, idx)) = self.key_clip(kind) |
| 99 | && let Some(session) = &mut self.session |
| 100 | && session.set_key_ease_at(lane, idx, kind, frame, ease) |
| 101 | { |
| 102 | self.mark_dirty(); |
| 103 | self.reset_after_reseek(); |
| 104 | } |
| 105 | cx.notify(); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | |
| 110 | pub(crate) fn set_opacity(&mut self, opacity: f32, cx: &mut Context<Self>) { |
| 111 | let mut params = self.transform_params(); |
| 112 | params.opacity = opacity.clamp(0., 1.); |
| 113 | self.set_transform(params, cx); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | pub(crate) fn offset_of_source(frames: u32, at: u32, pos: impl Fn(u32) -> Option<u32>) -> Option<u32> { |
| 123 | let last = frames.checked_sub(1)?; |
| 124 | let rising = pos(last)? >= pos(0)?; |
| 125 | let (mut lo, mut hi) = (0, last); |
| 126 | while lo < hi { |
| 127 | let mid = lo + (hi - lo) / 2; |
| 128 | let here = pos(mid)?; |
| 129 | let reached = if rising { here >= at } else { here <= at }; |
| 130 | if reached { |
| 131 | hi = mid; |
| 132 | } else { |
| 133 | lo = mid + 1; |
| 134 | } |
| 135 | } |
| 136 | (pos(lo)?.abs_diff(at) <= 1).then_some(lo) |
| 137 | } |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | pub(crate) fn merge_mark_px(xs: &[f32], min_gap: f32) -> Vec<usize> { |
| 143 | let mut kept: Vec<usize> = Vec::with_capacity(xs.len()); |
| 144 | for (n, &x) in xs.iter().enumerate() { |
| 145 | if kept.last().is_none_or(|&last| x - xs[last] >= min_gap) { |
| 146 | kept.push(n); |
| 147 | } |
| 148 | } |
| 149 | kept |
| 150 | } |
| 151 | |
| 152 | impl Player { |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | pub(crate) fn clip_key_frames(&self, lane: Lane, idx: usize) -> Vec<u32> { |
| 158 | let Some(session) = self.session.as_ref() else { |
| 159 | return Vec::new(); |
| 160 | }; |
| 161 | let Some(clip) = session.lane_clips(lane).get(idx) else { |
| 162 | return Vec::new(); |
| 163 | }; |
| 164 | let frames = clip.frames(); |
| 165 | |
| 166 | |
| 167 | let ramped = !clip.speed_keys.is_empty(); |
| 168 | let pos = |off: u32| -> Option<u32> { |
| 169 | if ramped { |
| 170 | return session.key_source_frame(lane, idx, clip.start + off); |
| 171 | } |
| 172 | let speed = clip.speed.as_f64(); |
| 173 | let src = match clip.reversed { |
| 174 | true => f64::from(clip.out_frame.saturating_sub(1)) - f64::from(off) * speed, |
| 175 | false => f64::from(clip.in_frame) + f64::from(off) * speed, |
| 176 | }; |
| 177 | Some(src.round().max(0.) as u32) |
| 178 | }; |
| 179 | let source_keys = clip |
| 180 | .transform_keys |
| 181 | .iter() |
| 182 | .map(|k| k.at) |
| 183 | .chain(clip.opacity_keys.iter().map(|k| k.at)) |
| 184 | .chain(clip.color_keys.iter().map(|k| k.at)) |
| 185 | .chain(clip.gain_keys.iter().map(|k| k.at)) |
| 186 | .chain(clip.effects.iter().flat_map(|e| e.keys.iter().map(|k| k.at))); |
| 187 | let mut offsets: Vec<u32> = source_keys |
| 188 | .filter_map(|at| offset_of_source(frames, at, pos)) |
| 189 | .collect(); |
| 190 | offsets.extend(clip.speed_keys.iter().map(|k| k.at).filter(|&off| off < frames)); |
| 191 | offsets.sort_unstable(); |
| 192 | offsets.dedup(); |
| 193 | offsets.into_iter().map(|off| clip.start + off).collect() |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | #[cfg(test)] |
| 198 | mod tests { |
| 199 | use super::*; |
| 200 | |
| 201 | |
| 202 | fn linear(off: u32) -> Option<u32> { |
| 203 | Some(100 + off * 2) |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn a_key_is_found_at_its_own_offset() { |
| 208 | assert_eq!(offset_of_source(50, 110, linear), Some(5)); |
| 209 | assert_eq!(offset_of_source(50, 100, linear), Some(0)); |
| 210 | |
| 211 | assert_eq!(offset_of_source(50, 111, linear), Some(6)); |
| 212 | } |
| 213 | |
| 214 | #[test] |
| 215 | fn a_key_off_the_clip_is_not_marked() { |
| 216 | assert_eq!(offset_of_source(50, 50, linear), None, "before the clip's footage"); |
| 217 | assert_eq!(offset_of_source(50, 300, linear), None, "past the clip's footage"); |
| 218 | assert_eq!(offset_of_source(0, 100, linear), None, "an empty clip"); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn a_reversed_map_is_found_too() { |
| 223 | let back = |off: u32| Some(200 - off * 2); |
| 224 | assert_eq!(offset_of_source(50, 190, back), Some(5)); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn close_marks_merge_and_far_ones_stay() { |
| 229 | assert_eq!(merge_mark_px(&[10., 12., 13., 20.], 4.), vec![0, 3]); |
| 230 | assert_eq!(merge_mark_px(&[10., 14., 18.], 4.), vec![0, 1, 2]); |
| 231 | assert_eq!(merge_mark_px(&[10., 13.9, 14.], 4.), vec![0, 2], "merged against the kept mark, not the last one"); |
| 232 | assert_eq!(merge_mark_px(&[], 4.), Vec::<usize>::new()); |
| 233 | } |
| 234 | } |