1use std::time::Duration;
5
6use crate::geo::ScreenCoordinate;
7
8#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct MomentumEasing {
11 pub x1: f64,
12 pub y1: f64,
13 pub x2: f64,
14 pub y2: f64,
15}
16
17impl MomentumEasing {
18 pub(crate) const DEFAULT: MomentumEasing = MomentumEasing { x1: 0.0, y1: 0.0, x2: 0.25, y2: 1.0 };
20 pub(crate) const ANDROID_FLING: MomentumEasing = MomentumEasing { x1: 0.175, y1: 0.5, x2: 0.35, y2: 1.0 };
22}
23
24impl Default for MomentumEasing {
25 fn default() -> Self {
26 MomentumEasing::DEFAULT
27 }
28}
29
30#[derive(Clone, Copy, Debug, PartialEq)]
32pub enum MomentumDecayModel {
33 Friction { half_life: Duration, stop_speed: f64 },
37 Curve { reference_speed: f64, duration: Duration, exponent: f64, easing: MomentumEasing },
43}
44
45#[derive(Clone, Copy, Debug, PartialEq)]
52pub struct MomentumDecay {
53 pub model: MomentumDecayModel,
54 pub min_speed: f64,
56 pub max_speed: f64,
58 pub max_distance: f64,
61}
62
63impl MomentumDecay {
64 pub(crate) const DEFAULT: MomentumDecay = MomentumDecay {
67 model: MomentumDecayModel::Friction { half_life: Duration::from_millis(140), stop_speed: 1.0 },
68 min_speed: 0.0,
69 max_speed: f64::INFINITY,
70 max_distance: f64::INFINITY,
71 };
72 pub const PAN: MomentumDecay = MomentumDecay {
75 model: MomentumDecayModel::Friction { half_life: Duration::from_millis(140), stop_speed: 10.0 },
76 min_speed: 200.0,
77 max_speed: 10_000.0,
78 ..MomentumDecay::DEFAULT
79 };
80 pub const ZOOM: MomentumDecay = MomentumDecay {
82 model: MomentumDecayModel::Friction { half_life: Duration::from_millis(140), stop_speed: 0.01 },
83 max_distance: 2.5,
84 ..MomentumDecay::DEFAULT
85 };
86 pub const BEARING: MomentumDecay = MomentumDecay { min_speed: 60.0, ..MomentumDecay::DEFAULT };
88 pub const PITCH: MomentumDecay = MomentumDecay { min_speed: 5.0, ..MomentumDecay::DEFAULT };
90 pub const ANDROID_FLING: MomentumDecay = MomentumDecay {
95 model: MomentumDecayModel::Curve {
96 reference_speed: 1000.0,
97 duration: Duration::from_micros(555_182),
98 exponent: 0.736_267_6,
99 easing: MomentumEasing::ANDROID_FLING,
100 },
101 min_speed: 50.0,
102 max_speed: 8000.0,
103 max_distance: f64::INFINITY,
104 };
105
106 pub fn validate(&self) -> Result<(), InvalidMomentum> {
110 let positive = |value: f64| value.is_finite() && value > 0.0;
111 let limits =
112 self.min_speed.is_finite() && self.min_speed >= 0.0 && self.max_speed > 0.0 && self.max_distance > 0.0;
113 let model = match self.model {
114 MomentumDecayModel::Friction { half_life, stop_speed } => !half_life.is_zero() && positive(stop_speed),
115 MomentumDecayModel::Curve { reference_speed, duration, exponent, easing } => {
116 let MomentumEasing { x1, y1, x2, y2 } = easing;
117 positive(reference_speed)
118 && !duration.is_zero()
119 && exponent.is_finite()
120 && exponent > -1.0
121 && [x1, y1, x2, y2].iter().all(|value| (0.0..=1.0).contains(value))
122 && x1 > 0.0
123 && y1 > 0.0
124 }
125 };
126 if limits && model {
127 Ok(())
128 } else {
129 Err(InvalidMomentum(
130 "a momentum decay needs positive speeds, distances and durations, a minimum speed that isn't \
131 negative, and a curve within [0, 1] that starts rising",
132 ))
133 }
134 }
135}
136
137impl Default for MomentumDecay {
138 fn default() -> Self {
139 MomentumDecay::DEFAULT
140 }
141}
142
143#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
145#[error("{0}")]
146pub struct InvalidMomentum(pub &'static str);
147
148#[derive(Clone, Copy, Debug, PartialEq)]
153pub struct MomentumSnap {
154 pub spacing: f64,
155 pub offset: f64,
156 pub tolerance: f64,
157 pub duration: Duration,
159}
160
161impl MomentumSnap {
162 pub const ZOOM: MomentumSnap =
164 MomentumSnap { spacing: 1.0, offset: 0.0, tolerance: 0.5, duration: Duration::from_millis(300) };
165 pub const BEARING: MomentumSnap =
167 MomentumSnap { spacing: 360.0, offset: 0.0, tolerance: 7.0, duration: Duration::from_millis(300) };
168 pub const PITCH: MomentumSnap =
170 MomentumSnap { spacing: 90.0, offset: 0.0, tolerance: 7.0, duration: Duration::from_millis(300) };
171
172 pub fn validate(&self) -> Result<(), InvalidMomentum> {
175 if self.spacing.is_finite()
176 && self.spacing > 0.0
177 && self.offset.is_finite()
178 && self.tolerance.is_finite()
179 && self.tolerance >= 0.0
180 {
181 Ok(())
182 } else {
183 Err(InvalidMomentum(
184 "a momentum snap needs a positive spacing, a finite offset, and a tolerance that isn't negative",
185 ))
186 }
187 }
188}
189
190#[derive(Clone, Copy, Debug, PartialEq)]
193pub struct MomentumGlide<T> {
194 pub(crate) velocity: T,
195 pub(crate) decay: MomentumDecay,
196}
197
198#[derive(Clone, Copy, Debug, Default, PartialEq)]
200pub struct AnchoredMomentum {
201 pub(crate) glide: Option<MomentumGlide<f64>>,
203 pub(crate) anchor: Option<ScreenCoordinate>,
206 pub(crate) snap: Option<MomentumSnap>,
209}
210
211#[derive(Clone, Copy, Debug, Default, PartialEq)]
223pub struct CameraMomentum {
224 pub(crate) pan: Option<MomentumGlide<ScreenCoordinate>>,
226 pub(crate) zoom: AnchoredMomentum,
227 pub(crate) bearing: AnchoredMomentum,
228 pub(crate) pitch: AnchoredMomentum,
229}