| 1 | use std::cell::Cell; |
| 2 | use std::rc::Rc; |
| 3 | |
| 4 | pub const ZOOM_MIN: f32 = 1.; |
| 5 | pub const ZOOM_MAX: f32 = 4.; |
| 6 | const ZOOM_STEP: f32 = 1.25; |
| 7 | const WHEEL_ZOOM_PER_PIXEL: f32 = 0.0015; |
| 8 | pub const STRIP_MIN_WIDTH: f32 = 160.; |
| 9 | pub const STRIP_MAX_WIDTH: f32 = 400.; |
| 10 | pub const STRIP_DEFAULT_WIDTH: f32 = 232.; |
| 11 | pub const STRIP_WIDTH_META_KEY: &str = "call_strip_width"; |
| 12 | pub const SHARE_MOTION_META_KEY: &str = "call_share_motion"; |
| 13 | pub const SHARE_STANDOUT_META_KEY: &str = "call_share_standout"; |
| 14 | |
| 15 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 16 | pub struct ShareView { |
| 17 | zoom: f32, |
| 18 | offset: (f32, f32), |
| 19 | } |
| 20 | |
| 21 | impl Default for ShareView { |
| 22 | fn default() -> Self { |
| 23 | ShareView { zoom: ZOOM_MIN, offset: (0., 0.) } |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn clamp_axis(offset: f32, extent: f32, zoom: f32) -> f32 { |
| 28 | offset.clamp(extent - extent * zoom, 0.) |
| 29 | } |
| 30 | |
| 31 | impl ShareView { |
| 32 | pub fn zoom(&self) -> f32 { |
| 33 | self.zoom |
| 34 | } |
| 35 | |
| 36 | pub fn is_zoomed(&self) -> bool { |
| 37 | self.zoom > ZOOM_MIN |
| 38 | } |
| 39 | |
| 40 | pub fn percent(&self) -> u32 { |
| 41 | (self.zoom * 100.).round() as u32 |
| 42 | } |
| 43 | |
| 44 | pub fn offset(&self, stage: (f32, f32)) -> (f32, f32) { |
| 45 | (clamp_axis(self.offset.0, stage.0, self.zoom), clamp_axis(self.offset.1, stage.1, self.zoom)) |
| 46 | } |
| 47 | |
| 48 | pub fn reset(&mut self) { |
| 49 | *self = ShareView::default(); |
| 50 | } |
| 51 | |
| 52 | pub fn zoom_around(&mut self, factor: f32, pointer: (f32, f32), stage: (f32, f32)) { |
| 53 | let target = (self.zoom * factor).clamp(ZOOM_MIN, ZOOM_MAX); |
| 54 | if target <= ZOOM_MIN { |
| 55 | self.reset(); |
| 56 | return; |
| 57 | } |
| 58 | let (offset_x, offset_y) = self.offset(stage); |
| 59 | let ratio = target / self.zoom; |
| 60 | self.zoom = target; |
| 61 | self.offset = (pointer.0 - (pointer.0 - offset_x) * ratio, pointer.1 - (pointer.1 - offset_y) * ratio); |
| 62 | self.offset = self.offset(stage); |
| 63 | } |
| 64 | |
| 65 | pub fn zoom_wheel(&mut self, scroll_pixels: f32, pointer: (f32, f32), stage: (f32, f32)) { |
| 66 | self.zoom_around((scroll_pixels * WHEEL_ZOOM_PER_PIXEL).exp(), pointer, stage); |
| 67 | } |
| 68 | |
| 69 | pub fn step_in(&mut self, stage: (f32, f32)) { |
| 70 | self.zoom_around(ZOOM_STEP, (stage.0 / 2., stage.1 / 2.), stage); |
| 71 | } |
| 72 | |
| 73 | pub fn step_out(&mut self, stage: (f32, f32)) { |
| 74 | self.zoom_around(1. / ZOOM_STEP, (stage.0 / 2., stage.1 / 2.), stage); |
| 75 | } |
| 76 | |
| 77 | pub fn pan_by(&mut self, delta: (f32, f32), stage: (f32, f32)) { |
| 78 | if !self.is_zoomed() { |
| 79 | return; |
| 80 | } |
| 81 | let (offset_x, offset_y) = self.offset(stage); |
| 82 | self.offset = (offset_x + delta.0, offset_y + delta.1); |
| 83 | self.offset = self.offset(stage); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | pub fn clamp_strip_width(width: f32) -> f32 { |
| 88 | width.clamp(STRIP_MIN_WIDTH, STRIP_MAX_WIDTH) |
| 89 | } |
| 90 | |
| 91 | pub fn load_strip_width(store: &store::Store) -> f32 { |
| 92 | store |
| 93 | .meta(STRIP_WIDTH_META_KEY) |
| 94 | .ok() |
| 95 | .flatten() |
| 96 | .and_then(|text| text.parse().ok()) |
| 97 | .map_or(STRIP_DEFAULT_WIDTH, clamp_strip_width) |
| 98 | } |
| 99 | |
| 100 | pub fn load_flag(store: &store::Store, key: &str) -> bool { |
| 101 | store.meta(key).ok().flatten().as_deref() == Some("1") |
| 102 | } |
| 103 | |
| 104 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 105 | pub struct StripDrag { |
| 106 | pub start_pointer: f32, |
| 107 | pub start_width: f32, |
| 108 | } |
| 109 | |
| 110 | impl StripDrag { |
| 111 | pub fn width_at(&self, pointer: f32) -> f32 { |
| 112 | clamp_strip_width(self.start_width + self.start_pointer - pointer) |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | #[derive(Default)] |
| 117 | pub struct ShareStage { |
| 118 | pub view: ShareView, |
| 119 | pub pan_from: Option<(f32, f32)>, |
| 120 | pub strip_drag: Option<StripDrag>, |
| 121 | pub popped: bool, |
| 122 | pub bounds: Rc<Cell<(f32, f32, f32, f32)>>, |
| 123 | } |
| 124 | |
| 125 | impl ShareStage { |
| 126 | pub fn size(&self) -> (f32, f32) { |
| 127 | let (_, _, width, height) = self.bounds.get(); |
| 128 | (width, height) |
| 129 | } |
| 130 | |
| 131 | pub fn pointer(&self, position: (f32, f32)) -> (f32, f32) { |
| 132 | let (left, top, _, _) = self.bounds.get(); |
| 133 | (position.0 - left, position.1 - top) |
| 134 | } |
| 135 | |
| 136 | pub fn end(&mut self) { |
| 137 | self.view.reset(); |
| 138 | self.pan_from = None; |
| 139 | self.popped = false; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | #[cfg(test)] |
| 144 | mod tests { |
| 145 | use super::*; |
| 146 | |
| 147 | const STAGE: (f32, f32) = (800., 450.); |
| 148 | |
| 149 | #[test] |
| 150 | fn zooming_keeps_the_point_under_the_pointer_still() { |
| 151 | let mut view = ShareView::default(); |
| 152 | let pointer = (600., 100.); |
| 153 | view.zoom_around(2., pointer, STAGE); |
| 154 | let (offset_x, offset_y) = view.offset(STAGE); |
| 155 | let content = ((pointer.0 - offset_x) / view.zoom(), (pointer.1 - offset_y) / view.zoom()); |
| 156 | assert!((content.0 - 600.).abs() < 0.01 && (content.1 - 100.).abs() < 0.01); |
| 157 | } |
| 158 | |
| 159 | #[test] |
| 160 | fn zoom_stays_between_one_and_four_times() { |
| 161 | let mut view = ShareView::default(); |
| 162 | view.zoom_around(100., (400., 225.), STAGE); |
| 163 | assert_eq!(view.zoom(), ZOOM_MAX); |
| 164 | view.zoom_around(0.0001, (400., 225.), STAGE); |
| 165 | assert!(!view.is_zoomed()); |
| 166 | assert_eq!(view.offset(STAGE), (0., 0.)); |
| 167 | } |
| 168 | |
| 169 | #[test] |
| 170 | fn panning_never_leaves_the_content() { |
| 171 | let mut view = ShareView::default(); |
| 172 | view.pan_by((50., 50.), STAGE); |
| 173 | assert_eq!(view.offset(STAGE), (0., 0.)); |
| 174 | view.zoom_around(2., (0., 0.), STAGE); |
| 175 | view.pan_by((-5000., -5000.), STAGE); |
| 176 | assert_eq!(view.offset(STAGE), (-800., -450.)); |
| 177 | view.pan_by((5000., 5000.), STAGE); |
| 178 | assert_eq!(view.offset(STAGE), (0., 0.)); |
| 179 | } |
| 180 | |
| 181 | #[test] |
| 182 | fn keyboard_steps_zoom_around_the_center_and_zero_resets() { |
| 183 | let mut view = ShareView::default(); |
| 184 | view.step_in(STAGE); |
| 185 | view.step_in(STAGE); |
| 186 | assert_eq!(view.percent(), 156); |
| 187 | view.step_out(STAGE); |
| 188 | assert_eq!(view.percent(), 125); |
| 189 | view.reset(); |
| 190 | assert_eq!(view.percent(), 100); |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn wheel_up_zooms_in_and_down_zooms_out() { |
| 195 | let mut view = ShareView::default(); |
| 196 | view.zoom_wheel(120., (400., 225.), STAGE); |
| 197 | assert!(view.zoom() > 1.); |
| 198 | let zoomed = view.zoom(); |
| 199 | view.zoom_wheel(-60., (400., 225.), STAGE); |
| 200 | assert!(view.zoom() < zoomed); |
| 201 | } |
| 202 | |
| 203 | #[test] |
| 204 | fn the_strip_width_stays_between_160_and_400_and_follows_the_pointer() { |
| 205 | let drag = StripDrag { start_pointer: 900., start_width: 232. }; |
| 206 | assert_eq!(drag.width_at(880.), 252.); |
| 207 | assert_eq!(drag.width_at(0.), STRIP_MAX_WIDTH); |
| 208 | assert_eq!(drag.width_at(2000.), STRIP_MIN_WIDTH); |
| 209 | assert_eq!(clamp_strip_width(10.), STRIP_MIN_WIDTH); |
| 210 | } |
| 211 | } |