| 1 | //! Modal keyframe transforms (`G` grab, `R` rotate, `S` scale) and spline sorting for [`SplineView`]. |
| 2 | |
| 3 | use super::{HandlePart, PointRef, SplineView}; |
| 4 | use model::{BezierSpline, Comp, CompHistory, HandleMode, Keyframe}; |
| 5 | use presets::{display_to_value, enforce_aligned_handle, value_to_display}; |
| 6 | use std::collections::{HashMap, HashSet}; |
| 7 | |
| 8 | use super::presets; |
| 9 | |
| 10 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 11 | pub enum TransformMode { |
| 12 | Grab, |
| 13 | Rotate, |
| 14 | Scale, |
| 15 | } |
| 16 | |
| 17 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 18 | pub enum AxisLock { |
| 19 | None, |
| 20 | X, |
| 21 | Y, |
| 22 | } |
| 23 | |
| 24 | #[derive(Clone, Debug)] |
| 25 | pub(super) struct ModalEdit { |
| 26 | pub(super) mode: TransformMode, |
| 27 | pub(super) axis: AxisLock, |
| 28 | /// Pivot in display coordinates `(time, display_value)`. |
| 29 | pub(super) pivot: [f64; 2], |
| 30 | /// Pointer at modal start in display coordinates `(time, display_value)`. |
| 31 | pub(super) start_pointer: [f64; 2], |
| 32 | pub(super) saved_splines: HashMap<String, Vec<Keyframe>>, |
| 33 | /// Selection when the modal began; indices refer to `saved_splines`, while `SplineView::selected` |
| 34 | /// follows the keys through each re-sort. |
| 35 | pub(super) orig_selected: HashSet<PointRef>, |
| 36 | /// Frozen per-spline `(min_v, max_v)` bounds captured when the modal began so normalized |
| 37 | /// drags stay stable without shifting scale mid-drag. |
| 38 | pub(super) saved_bounds: HashMap<String, (f64, f64)>, |
| 39 | /// Optional pre-edit `Comp` snapshot captured when a `CompHistory` is active. |
| 40 | pub(super) pre_edit_snapshot: Option<Comp>, |
| 41 | } |
| 42 | |
| 43 | impl SplineView { |
| 44 | pub(super) fn begin_modal(&mut self, comp: &Comp, capture_history: bool, mode: TransformMode) { |
| 45 | if self.selected.is_empty() { |
| 46 | return; |
| 47 | } |
| 48 | let mut sum_t = 0.0; |
| 49 | let mut sum_v = 0.0; |
| 50 | let mut count = 0usize; |
| 51 | let mut saved = HashMap::new(); |
| 52 | let mut saved_bounds = HashMap::new(); |
| 53 | |
| 54 | for pref in &self.selected { |
| 55 | if let Some(sp) = comp.splines.get(&pref.spline) { |
| 56 | saved.entry(pref.spline.clone()).or_insert_with(|| sp.keyframes.clone()); |
| 57 | let bounds = *saved_bounds |
| 58 | .entry(pref.spline.clone()) |
| 59 | .or_insert_with(|| presets::compute_curve_bounds(sp)); |
| 60 | if let Some(kf) = sp.keyframes.get(pref.key_idx) { |
| 61 | let pt = match pref.part { |
| 62 | HandlePart::Key => [kf.time, kf.value], |
| 63 | HandlePart::LeftHandle => kf.lh.unwrap_or([kf.time, kf.value]), |
| 64 | HandlePart::RightHandle => kf.rh.unwrap_or([kf.time, kf.value]), |
| 65 | }; |
| 66 | let piv_pt = if self.selected.len() == 1 && pref.part != HandlePart::Key { |
| 67 | [kf.time, kf.value] |
| 68 | } else { |
| 69 | pt |
| 70 | }; |
| 71 | sum_t += piv_pt[0]; |
| 72 | sum_v += value_to_display(piv_pt[1], bounds, self.normalized); |
| 73 | count += 1; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | if count == 0 { |
| 78 | return; |
| 79 | } |
| 80 | self.modal = Some(ModalEdit { |
| 81 | mode, |
| 82 | axis: AxisLock::None, |
| 83 | pivot: [sum_t / count as f64, sum_v / count as f64], |
| 84 | start_pointer: self.local_to_data(self.pointer_local), |
| 85 | saved_splines: saved, |
| 86 | orig_selected: self.selected.clone(), |
| 87 | saved_bounds, |
| 88 | pre_edit_snapshot: capture_history.then(|| comp.clone()), |
| 89 | }); |
| 90 | } |
| 91 | |
| 92 | pub(super) fn apply_modal(&mut self, comp: &mut Comp, fine: bool) -> bool { |
| 93 | let Some(m) = &self.modal else { return false }; |
| 94 | let cur = self.local_to_data(self.pointer_local); |
| 95 | let dt = cur[0] - m.start_pointer[0]; |
| 96 | let dv_disp = cur[1] - m.start_pointer[1]; |
| 97 | let piv = m.pivot; |
| 98 | let normalized = self.normalized; |
| 99 | |
| 100 | for (sname, orig_kfs) in &m.saved_splines { |
| 101 | if let Some(sp) = comp.splines.get_mut(sname) { |
| 102 | sp.keyframes = orig_kfs.clone(); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // A selected key carries its handles, so a handle also in the selection must not move twice. |
| 107 | let selected = &m.orig_selected; |
| 108 | let moves = |pref: &PointRef| { |
| 109 | pref.part == HandlePart::Key |
| 110 | || !selected.contains(&PointRef { spline: pref.spline.clone(), key_idx: pref.key_idx, part: HandlePart::Key }) |
| 111 | }; |
| 112 | |
| 113 | let snap_t = |t: f64| if fine { t } else { t.round() }; |
| 114 | |
| 115 | // Helper: convert Auto/Vector handles to Aligned/Free when manually dragged (Blender behavior), |
| 116 | // and mirror opposite handle if Aligned (and opposite handle isn't also explicitly selected). |
| 117 | let finalize_handle_drag = |kf: &mut Keyframe, pref: &PointRef, selected: &HashSet<PointRef>| { |
| 118 | kf.flags.linear = false; |
| 119 | match kf.handle_mode { |
| 120 | HandleMode::Auto => kf.handle_mode = HandleMode::Aligned, |
| 121 | HandleMode::Vector => kf.handle_mode = HandleMode::Free, |
| 122 | HandleMode::Aligned | HandleMode::Free => {} |
| 123 | } |
| 124 | if kf.handle_mode == HandleMode::Aligned { |
| 125 | let opp_part = match pref.part { |
| 126 | HandlePart::LeftHandle => HandlePart::RightHandle, |
| 127 | HandlePart::RightHandle => HandlePart::LeftHandle, |
| 128 | HandlePart::Key => return, |
| 129 | }; |
| 130 | let opp_selected = selected.contains(&PointRef { |
| 131 | spline: pref.spline.clone(), |
| 132 | key_idx: pref.key_idx, |
| 133 | part: opp_part, |
| 134 | }); |
| 135 | if !opp_selected { |
| 136 | enforce_aligned_handle(kf, pref.part); |
| 137 | } |
| 138 | } |
| 139 | }; |
| 140 | |
| 141 | match m.mode { |
| 142 | TransformMode::Grab => { |
| 143 | let (mt, mv_disp) = match m.axis { |
| 144 | AxisLock::None => (dt, dv_disp), |
| 145 | AxisLock::X => (dt, 0.0), |
| 146 | AxisLock::Y => (0.0, dv_disp), |
| 147 | }; |
| 148 | for pref in selected.iter().filter(|p| moves(p)) { |
| 149 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; |
| 150 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); |
| 151 | let mv = if normalized { |
| 152 | mv_disp * (bounds.1 - bounds.0).max(1e-9) |
| 153 | } else { |
| 154 | mv_disp |
| 155 | }; |
| 156 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; |
| 157 | match pref.part { |
| 158 | HandlePart::Key => { |
| 159 | let new_t = snap_t(kf.time + mt); |
| 160 | let actual_dt = new_t - kf.time; |
| 161 | kf.time = new_t; |
| 162 | kf.value += mv; |
| 163 | if let Some(lh) = &mut kf.lh { |
| 164 | lh[0] += actual_dt; |
| 165 | lh[1] += mv; |
| 166 | } |
| 167 | if let Some(rh) = &mut kf.rh { |
| 168 | rh[0] += actual_dt; |
| 169 | rh[1] += mv; |
| 170 | } |
| 171 | } |
| 172 | HandlePart::LeftHandle => { |
| 173 | let mut h = kf.lh.unwrap_or([kf.time - 5.0, kf.value]); |
| 174 | h[0] = (h[0] + mt).min(kf.time - 0.01); |
| 175 | h[1] += mv; |
| 176 | kf.lh = Some(h); |
| 177 | finalize_handle_drag(kf, pref, selected); |
| 178 | } |
| 179 | HandlePart::RightHandle => { |
| 180 | let mut h = kf.rh.unwrap_or([kf.time + 5.0, kf.value]); |
| 181 | h[0] = (h[0] + mt).max(kf.time + 0.01); |
| 182 | h[1] += mv; |
| 183 | kf.rh = Some(h); |
| 184 | finalize_handle_drag(kf, pref, selected); |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | TransformMode::Scale => { |
| 190 | let s0 = self.data_to_local(m.start_pointer[0], m.start_pointer[1]); |
| 191 | let s1 = self.pointer_local; |
| 192 | let sp_px = self.data_to_local(piv[0], piv[1]); |
| 193 | let d0 = hypot2(s0, sp_px).max(10.0); |
| 194 | let d1 = hypot2(s1, sp_px); |
| 195 | let f = f64::from((d1 / d0).clamp(0.05, 10.0)); |
| 196 | let (st, sv) = match m.axis { |
| 197 | AxisLock::None => (f, f), |
| 198 | AxisLock::X => (f, 1.0), |
| 199 | AxisLock::Y => (1.0, f), |
| 200 | }; |
| 201 | for pref in selected.iter().filter(|p| moves(p)) { |
| 202 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; |
| 203 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); |
| 204 | let piv_val = display_to_value(piv[1], bounds, normalized); |
| 205 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; |
| 206 | match pref.part { |
| 207 | HandlePart::Key => { |
| 208 | let raw_t = piv[0] + (kf.time - piv[0]) * st; |
| 209 | kf.time = snap_t(raw_t); |
| 210 | let snap = kf.time - raw_t; |
| 211 | kf.value = piv_val + (kf.value - piv_val) * sv; |
| 212 | if let Some(lh) = &mut kf.lh { |
| 213 | lh[0] = (piv[0] + (lh[0] - piv[0]) * st + snap).min(kf.time - 0.01); |
| 214 | lh[1] = piv_val + (lh[1] - piv_val) * sv; |
| 215 | } |
| 216 | if let Some(rh) = &mut kf.rh { |
| 217 | rh[0] = (piv[0] + (rh[0] - piv[0]) * st + snap).max(kf.time + 0.01); |
| 218 | rh[1] = piv_val + (rh[1] - piv_val) * sv; |
| 219 | } |
| 220 | } |
| 221 | HandlePart::LeftHandle => { |
| 222 | if let Some(lh) = &mut kf.lh { |
| 223 | lh[0] = (kf.time + (lh[0] - kf.time) * st).min(kf.time - 0.01); |
| 224 | lh[1] = kf.value + (lh[1] - kf.value) * sv; |
| 225 | } |
| 226 | finalize_handle_drag(kf, pref, selected); |
| 227 | } |
| 228 | HandlePart::RightHandle => { |
| 229 | if let Some(rh) = &mut kf.rh { |
| 230 | rh[0] = (kf.time + (rh[0] - kf.time) * st).max(kf.time + 0.01); |
| 231 | rh[1] = kf.value + (rh[1] - kf.value) * sv; |
| 232 | } |
| 233 | finalize_handle_drag(kf, pref, selected); |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | TransformMode::Rotate => { |
| 239 | let sp_px = self.data_to_local(piv[0], piv[1]); |
| 240 | let s0_px = self.data_to_local(m.start_pointer[0], m.start_pointer[1]); |
| 241 | let a0 = (s0_px[1] - sp_px[1]).atan2(s0_px[0] - sp_px[0]); |
| 242 | let a1 = (self.pointer_local[1] - sp_px[1]).atan2(self.pointer_local[0] - sp_px[0]); |
| 243 | let da = a1 - a0; |
| 244 | let (c, s) = (da.cos(), da.sin()); |
| 245 | |
| 246 | let rot = |pt: [f64; 2], center_px: [f32; 2], bounds: (f64, f64)| -> [f64; 2] { |
| 247 | let p = self.spline_to_local(pt[0], pt[1], bounds); |
| 248 | let rx = p[0] - center_px[0]; |
| 249 | let ry = p[1] - center_px[1]; |
| 250 | let np = [center_px[0] + rx * c - ry * s, center_px[1] + rx * s + ry * c]; |
| 251 | let disp = self.local_to_data(np); |
| 252 | [disp[0], display_to_value(disp[1], bounds, normalized)] |
| 253 | }; |
| 254 | |
| 255 | for pref in selected.iter().filter(|p| moves(p)) { |
| 256 | let Some(sp) = comp.splines.get_mut(&pref.spline) else { continue }; |
| 257 | let bounds = m.saved_bounds.get(&pref.spline).copied().unwrap_or((0.0, 1.0)); |
| 258 | let Some(kf) = sp.keyframes.get_mut(pref.key_idx) else { continue }; |
| 259 | match pref.part { |
| 260 | HandlePart::Key => { |
| 261 | if selected.len() == 1 { |
| 262 | let kp = self.spline_to_local(kf.time, kf.value, bounds); |
| 263 | ensure_handles(kf); |
| 264 | if let Some(lh) = kf.lh { |
| 265 | let nl = rot(lh, kp, bounds); |
| 266 | kf.lh = Some([nl[0].min(kf.time - 0.01), nl[1]]); |
| 267 | } |
| 268 | if let Some(rh) = kf.rh { |
| 269 | let nr = rot(rh, kp, bounds); |
| 270 | kf.rh = Some([nr[0].max(kf.time + 0.01), nr[1]]); |
| 271 | } |
| 272 | kf.flags.linear = false; |
| 273 | } else { |
| 274 | let nk = rot([kf.time, kf.value], sp_px, bounds); |
| 275 | let lh = kf.lh.map(|h| rot(h, sp_px, bounds)); |
| 276 | let rh = kf.rh.map(|h| rot(h, sp_px, bounds)); |
| 277 | kf.time = snap_t(nk[0]); |
| 278 | let snap = kf.time - nk[0]; |
| 279 | kf.value = nk[1]; |
| 280 | kf.lh = lh.map(|h| [(h[0] + snap).min(kf.time - 0.01), h[1]]); |
| 281 | kf.rh = rh.map(|h| [(h[0] + snap).max(kf.time + 0.01), h[1]]); |
| 282 | } |
| 283 | } |
| 284 | HandlePart::LeftHandle => { |
| 285 | let kp = self.spline_to_local(kf.time, kf.value, bounds); |
| 286 | if let Some(lh) = kf.lh { |
| 287 | let nl = rot(lh, kp, bounds); |
| 288 | kf.lh = Some([nl[0].min(kf.time - 0.01), nl[1]]); |
| 289 | finalize_handle_drag(kf, pref, selected); |
| 290 | } |
| 291 | } |
| 292 | HandlePart::RightHandle => { |
| 293 | let kp = self.spline_to_local(kf.time, kf.value, bounds); |
| 294 | if let Some(rh) = kf.rh { |
| 295 | let nr = rot(rh, kp, bounds); |
| 296 | kf.rh = Some([nr[0].max(kf.time + 0.01), nr[1]]); |
| 297 | finalize_handle_drag(kf, pref, selected); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | } |
| 303 | } |
| 304 | // Keep curves sorted while dragging; selection follows its keys to their new slots. |
| 305 | let remaps: HashMap<&String, Vec<usize>> = m |
| 306 | .saved_splines |
| 307 | .keys() |
| 308 | .filter_map(|sname| Some((sname, sort_keyframes(comp.splines.get_mut(sname)?)))) |
| 309 | .collect(); |
| 310 | let new_selected: HashSet<PointRef> = selected |
| 311 | .iter() |
| 312 | .map(|p| PointRef { |
| 313 | key_idx: remaps.get(&p.spline).and_then(|r| r.get(p.key_idx)).copied().unwrap_or(p.key_idx), |
| 314 | ..p.clone() |
| 315 | }) |
| 316 | .collect(); |
| 317 | self.selected = new_selected; |
| 318 | comp.revision += 1; |
| 319 | true |
| 320 | } |
| 321 | |
| 322 | pub(super) fn cancel_modal(&mut self, comp: &mut Comp) { |
| 323 | if let Some(m) = self.modal.take() { |
| 324 | for (sname, orig) in m.saved_splines { |
| 325 | if let Some(sp) = comp.splines.get_mut(&sname) { |
| 326 | sp.keyframes = orig; |
| 327 | } |
| 328 | } |
| 329 | self.selected = m.orig_selected; |
| 330 | comp.revision += 1; |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | pub(super) fn confirm_modal(&mut self, comp: &mut Comp, history: Option<&mut CompHistory>) { |
| 335 | if let Some(m) = self.modal.take() { |
| 336 | comp.revision += 1; |
| 337 | if let (Some(h), Some(snap)) = (history, m.pre_edit_snapshot) { |
| 338 | let label = match m.mode { |
| 339 | TransformMode::Grab => "Grab Keyframes", |
| 340 | TransformMode::Rotate => "Rotate Keyframes", |
| 341 | TransformMode::Scale => "Scale Keyframes", |
| 342 | }; |
| 343 | h.push_snapshot(&snap, label); |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | /// Sorts `sp.keyframes` by time (stable) and returns each old index's new index. |
| 350 | pub fn sort_keyframes(sp: &mut BezierSpline) -> Vec<usize> { |
| 351 | let mut order: Vec<usize> = (0..sp.keyframes.len()).collect(); |
| 352 | order.sort_by(|&a, &b| sp.keyframes[a].time.total_cmp(&sp.keyframes[b].time)); |
| 353 | let mut new_index = vec![0; order.len()]; |
| 354 | for (new, &old) in order.iter().enumerate() { |
| 355 | new_index[old] = new; |
| 356 | } |
| 357 | sp.keyframes = order.iter().map(|&old| sp.keyframes[old].clone()).collect(); |
| 358 | new_index |
| 359 | } |
| 360 | |
| 361 | pub(super) fn ensure_handles(kf: &mut Keyframe) { |
| 362 | if kf.lh.is_none() { |
| 363 | kf.lh = Some([kf.time - 8.0, kf.value]); |
| 364 | } |
| 365 | if kf.rh.is_none() { |
| 366 | kf.rh = Some([kf.time + 8.0, kf.value]); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | pub(super) fn hypot2(a: [f32; 2], b: [f32; 2]) -> f32 { |
| 371 | ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2)).sqrt() |
| 372 | } |
| 373 | |
| 374 | #[cfg(test)] |
| 375 | mod tests { |
| 376 | use super::*; |
| 377 | use crate::tests::{key_ref, test_comp}; |
| 378 | |
| 379 | #[test] |
| 380 | fn dragging_a_key_past_its_neighbour_keeps_curve_sorted_and_selection_on_the_key() { |
| 381 | let mut comp = test_comp(); |
| 382 | let mut view = SplineView::new(&ui::Theme::dark()); |
| 383 | view.resize([800.0, 360.0]); |
| 384 | view.set_target_node(&comp, None); |
| 385 | view.selected.insert(key_ref(0, HandlePart::Key)); |
| 386 | |
| 387 | view.pointer_local = view.spline_to_local(0.0, 10.0, (10.0, 110.0)); |
| 388 | view.begin_modal(&comp, false, TransformMode::Grab); |
| 389 | view.pointer_local = view.spline_to_local(70.0, 10.0, (10.0, 110.0)); |
| 390 | assert!(view.apply_modal(&mut comp, false)); |
| 391 | |
| 392 | let times: Vec<f64> = comp.splines["Gain"].keyframes.iter().map(|k| k.time).collect(); |
| 393 | assert_eq!(times, [50.0, 70.0, 100.0]); |
| 394 | assert_eq!(view.selected, HashSet::from([key_ref(1, HandlePart::Key)])); |
| 395 | |
| 396 | // Cancel puts both the keys and the selection back. |
| 397 | view.cancel_modal(&mut comp); |
| 398 | assert_eq!(comp.splines["Gain"].keyframes[0].time, 0.0); |
| 399 | assert_eq!(view.selected, HashSet::from([key_ref(0, HandlePart::Key)])); |
| 400 | } |
| 401 | |
| 402 | #[test] |
| 403 | fn grabbing_a_key_and_its_own_handle_moves_the_handle_once() { |
| 404 | let mut comp = test_comp(); |
| 405 | comp.splines.get_mut("Gain").unwrap().keyframes[1].lh = Some([40.0, 110.0]); |
| 406 | let mut view = SplineView::new(&ui::Theme::dark()); |
| 407 | view.resize([800.0, 360.0]); |
| 408 | view.set_target_node(&comp, None); |
| 409 | view.selected.insert(key_ref(1, HandlePart::Key)); |
| 410 | view.selected.insert(key_ref(1, HandlePart::LeftHandle)); |
| 411 | |
| 412 | view.pointer_local = view.spline_to_local(50.0, 110.0, (10.0, 110.0)); |
| 413 | view.begin_modal(&comp, false, TransformMode::Grab); |
| 414 | view.pointer_local = view.spline_to_local(60.0, 110.0, (10.0, 110.0)); |
| 415 | view.apply_modal(&mut comp, false); |
| 416 | |
| 417 | let kf = &comp.splines["Gain"].keyframes[1]; |
| 418 | assert!((kf.time - 60.0).abs() < 1e-6); |
| 419 | assert!((kf.lh.unwrap()[0] - 50.0).abs() < 1e-6, "handle shifted {:?}", kf.lh); |
| 420 | } |
| 421 | |
| 422 | #[test] |
| 423 | fn multi_key_rotate_carries_handles_with_their_keys() { |
| 424 | let mut comp = test_comp(); |
| 425 | { |
| 426 | let kfs = &mut comp.splines.get_mut("Gain").unwrap().keyframes; |
| 427 | kfs[0].lh = Some([-10.0, 10.0]); |
| 428 | kfs[0].rh = Some([10.0, 10.0]); |
| 429 | kfs[1].lh = Some([40.0, 110.0]); |
| 430 | kfs[1].rh = Some([60.0, 110.0]); |
| 431 | } |
| 432 | let orig = comp.splines["Gain"].keyframes.clone(); |
| 433 | let mut view = SplineView::new(&ui::Theme::dark()); |
| 434 | view.resize([800.0, 360.0]); |
| 435 | view.set_target_node(&comp, None); |
| 436 | view.selected.insert(key_ref(0, HandlePart::Key)); |
| 437 | view.selected.insert(key_ref(1, HandlePart::Key)); |
| 438 | |
| 439 | let bounds = (10.0, 110.0); |
| 440 | let pivot_data = [25.0, 60.0]; |
| 441 | let piv = view.spline_to_local(pivot_data[0], pivot_data[1], bounds); |
| 442 | view.pointer_local = [piv[0] + 100.0, piv[1]]; |
| 443 | view.begin_modal(&comp, false, TransformMode::Rotate); |
| 444 | let theta = 0.1_f32; |
| 445 | view.pointer_local = [piv[0] + 100.0 * theta.cos(), piv[1] + 100.0 * theta.sin()]; |
| 446 | assert!(view.apply_modal(&mut comp, true)); |
| 447 | |
| 448 | let rotated = |pt: [f64; 2]| { |
| 449 | let p = view.spline_to_local(pt[0], pt[1], bounds); |
| 450 | let (rx, ry) = (p[0] - piv[0], p[1] - piv[1]); |
| 451 | let np = [piv[0] + rx * theta.cos() - ry * theta.sin(), piv[1] + rx * theta.sin() + ry * theta.cos()]; |
| 452 | let d = view.local_to_data(np); |
| 453 | [d[0], display_to_value(d[1], bounds, false)] |
| 454 | }; |
| 455 | let kfs = &comp.splines["Gain"].keyframes; |
| 456 | for (i, o) in orig.iter().take(2).enumerate() { |
| 457 | for (got, want) in [(kfs[i].lh, o.lh), (kfs[i].rh, o.rh)] { |
| 458 | let want = rotated(want.unwrap()); |
| 459 | let got = got.unwrap(); |
| 460 | assert!((got[0] - want[0]).abs() < 1e-2 && (got[1] - want[1]).abs() < 1e-2, "key {i}: {got:?} vs {want:?}"); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | } |