forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiBff99ad6Tahinli6 days ago1055 commits

keyframes.rs234 lines, 9.2 KiBHistoryRawDownload
1//! Keyframes on the property cards: the stopwatch diamond, the easing chip, and
2//! the key-aware read and write the sliders go through once a clip is keyed.
3
4use crate::*;
5use engine::project::KeyKind;
6use engine::transform::KeyEase;
7
8impl Player {
9 /// The timeline frame the playhead sits on: where a key "at the playhead" goes.
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 /// The clip the open card edits, for keys of `kind`: the transform card owns
18 /// placement and opacity, the colour card the grade.
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 /// Whether the clip of the `kind` card is keyed at all.
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 /// The ease of the key on the playhead, `None` with no key there.
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 /// The stopwatch: a key on the playhead holding what the clip shows there, or
42 /// the key that is there taken off.
43 pub(crate) fn toggle_key_here(&mut self, kind: KeyKind, cx: &mut Context<Self>) {
44 // The stroke is the card's own: with the card shut it opens it first,
45 // on the selected clip.
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 /// Which key the easing list is about: with the transform card open that is the
72 /// placement key, or the opacity key when only that one is there.
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 /// Opens the list of easings for the key on the playhead, at `at`: Linear, Ease in,
84 /// Ease out, Ease in-out, Hold, the one in force ticked. Nothing steps on its own.
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 /// Sets the key on the playhead to `ease`.
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 /// Sets the opacity of the transform card's clip: a key when opacity is
109 /// keyed, the placement's own value otherwise.
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/// The clip-relative offset (`0..frames`) whose source frame is `at`, found by
118/// bisection over `pos`, the clip's offset-to-source map. `pos` moves one way
119/// only, so the first offset that reaches `at` is the key's own. `None` when no
120/// offset lands within a frame of `at` -- a key on footage the clip no longer
121/// shows (trimmed away) has no place on the clip to mark.
122pub(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/// The indices of `xs` (positions, ascending) that keep a mark: one closer than
140/// `min_gap` to the mark kept before it is dropped, so a run of keys a few
141/// frames apart draws one diamond.
142pub(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
152impl Player {
153 /// Timeline frames of every key on the clip at (`lane`, `idx`), sorted and
154 /// unique: the placement, opacity, colour and volume keys (kept on the
155 /// footage, so mapped back onto the clip) and the speed ramp's keys (kept
156 /// clip-relative already). Keys the clip no longer shows are left out.
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 // Constant speed: the same arithmetic the engine plays the clip by.
166 // With a ramp the engine's own source map is the only true one.
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)]
198mod tests {
199 use super::*;
200
201 /// A constant-speed map from offset to source frame, 2x speed from source 100.
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 // No offset lands on source 111: the first one past it (112) is within a frame, so it counts.
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}