| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | use crate::*; |
| 6 | |
| 7 | |
| 8 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 9 | pub(crate) enum PanTarget { |
| 10 | Clip(Lane, usize), |
| 11 | Lane(Lane), |
| 12 | } |
| 13 | |
| 14 | |
| 15 | #[derive(Clone, Copy, Debug, PartialEq)] |
| 16 | pub(crate) struct PanScrub { |
| 17 | pub(crate) target: PanTarget, |
| 18 | pub(crate) x0: f32, |
| 19 | pub(crate) v0: f32, |
| 20 | pub(crate) cur: f32, |
| 21 | |
| 22 | pub(crate) per_px: f32, |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
| 27 | pub(crate) fn scrub_pan_at(v0: f32, dx: f32, per_px: f32) -> f32 { |
| 28 | let v = ((v0 + dx * per_px).clamp(-1., 1.) * 100.).round() / 100.; |
| 29 | if v.abs() < 0.03 { 0. } else { v } |
| 30 | } |
| 31 | |
| 32 | |
| 33 | |
| 34 | pub(crate) fn pan_text(pan: f32) -> String { |
| 35 | let pct = (pan.abs() * 100.).round() as u32; |
| 36 | match (pct, pan < 0.) { |
| 37 | (0, _) => "Centre".into(), |
| 38 | (n, true) => format!("{n}% L"), |
| 39 | (n, false) => format!("{n}% R"), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl Player { |
| 44 | |
| 45 | pub(crate) fn pan_of(&self, target: PanTarget) -> f32 { |
| 46 | let Some(s) = self.session.as_ref() else { |
| 47 | return 0.; |
| 48 | }; |
| 49 | match target { |
| 50 | PanTarget::Clip(lane, idx) => s.clip_pan_of(lane, idx).unwrap_or(0.), |
| 51 | PanTarget::Lane(lane) => s.lane_pan(lane), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | |
| 56 | pub(crate) fn pan_shown(&self, target: PanTarget) -> f32 { |
| 57 | match self.pan_scrub { |
| 58 | Some(s) if s.target == target => s.cur, |
| 59 | _ => self.pan_of(target), |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | |
| 64 | |
| 65 | pub(crate) fn begin_pan_press(&mut self, target: PanTarget, x: f32, along: f32, width: f32) { |
| 66 | let v0 = self.pan_of(target); |
| 67 | let per_px = 2. / width.max(1.); |
| 68 | let dx = (along.clamp(0., 1.) * 2. - 1. - v0) / per_px; |
| 69 | let cur = scrub_pan_at(v0, dx, per_px); |
| 70 | self.pan_scrub = Some(PanScrub { target, x0: x - dx, v0, cur, per_px }); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | pub(crate) fn pan_scrub_move(&mut self, x: f32, cx: &mut Context<Self>) { |
| 75 | let Some(s) = self.pan_scrub else { |
| 76 | return; |
| 77 | }; |
| 78 | let cur = scrub_pan_at(s.v0, x - s.x0, s.per_px); |
| 79 | if cur != s.cur { |
| 80 | self.pan_scrub = Some(PanScrub { cur, ..s }); |
| 81 | cx.notify(); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | pub(crate) fn pan_scrub_end(&mut self, cx: &mut Context<Self>) -> bool { |
| 87 | let Some(s) = self.pan_scrub.take() else { |
| 88 | return false; |
| 89 | }; |
| 90 | if s.cur != s.v0 { |
| 91 | self.write_pan(s.target, s.cur); |
| 92 | } |
| 93 | cx.notify(); |
| 94 | true |
| 95 | } |
| 96 | |
| 97 | |
| 98 | pub(crate) fn reset_pan(&mut self, target: PanTarget, cx: &mut Context<Self>) { |
| 99 | self.write_pan(target, 0.); |
| 100 | cx.notify(); |
| 101 | } |
| 102 | |
| 103 | fn write_pan(&mut self, target: PanTarget, pan: f32) { |
| 104 | let Some(s) = self.session.as_mut() else { |
| 105 | return; |
| 106 | }; |
| 107 | match target { |
| 108 | PanTarget::Clip(lane, idx) => s.set_clip_pan(lane, idx, pan), |
| 109 | PanTarget::Lane(lane) => s.set_lane_pan(lane, pan), |
| 110 | }; |
| 111 | } |
| 112 | } |