1use std::time::Duration;
5
6use crate::camera::{InvalidMomentum, MomentumDecay, MomentumSnap};
7
8#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct Notch {
14 pub spacing: f64,
16 pub offset: f64,
17 pub kind: u32,
20}
21
22impl Notch {
23 pub(crate) const NORTH: Notch = Notch { spacing: 360.0, offset: 0.0, kind: 0 };
25}
26
27#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
29pub enum GestureAnchor {
30 #[default]
33 FocalPoint,
34 CameraCenter,
36}
37
38#[derive(Clone, Copy, Debug, PartialEq)]
40pub struct PanOptions {
41 pub enabled: bool,
43 pub momentum: Option<MomentumDecay>,
46}
47
48#[derive(Clone, Debug, PartialEq)]
50pub struct AxisOptions {
51 pub enabled: bool,
53 pub momentum: Option<MomentumDecay>,
56 pub snap: Option<MomentumSnap>,
59 pub notches: Vec<Notch>,
63}
64
65#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
67pub enum Axis {
68 Zoom,
70 Bearing,
72 Pitch,
74}
75
76#[derive(Clone, Debug, PartialEq)]
80pub struct GestureAxes {
81 pub pan: PanOptions,
82 pub zoom: AxisOptions,
83 pub bearing: AxisOptions,
84 pub pitch: AxisOptions,
85}
86
87impl Default for GestureAxes {
88 fn default() -> Self {
89 let axis = |momentum: MomentumDecay, snap: Option<MomentumSnap>, notches: Vec<Notch>| AxisOptions {
90 enabled: true,
91 momentum: Some(momentum),
92 snap,
93 notches,
94 };
95 GestureAxes {
96 pan: PanOptions { enabled: true, momentum: Some(MomentumDecay::PAN) },
97 zoom: axis(MomentumDecay::ZOOM, None, Vec::new()),
98 bearing: axis(MomentumDecay::BEARING, Some(MomentumSnap::BEARING), vec![Notch::NORTH]),
99 pitch: axis(MomentumDecay::PITCH, None, Vec::new()),
100 }
101 }
102}
103
104#[derive(Clone, Copy, Debug, PartialEq)]
106pub struct DragOptions {
107 pub pan: bool,
109 pub rotate_bearing_per_px: f64,
112 pub rotate_pitch_per_px: f64,
115 pub rotate_anchor: GestureAnchor,
117}
118
119impl Default for DragOptions {
120 fn default() -> Self {
121 DragOptions {
122 pan: true,
123 rotate_bearing_per_px: 0.5,
124 rotate_pitch_per_px: -0.5,
125 rotate_anchor: GestureAnchor::CameraCenter,
126 }
127 }
128}
129
130#[derive(Clone, Copy, Debug, PartialEq)]
132pub struct PinchOptions {
133 pub pan: bool,
135 pub zoom: bool,
137 pub rotate: bool,
139 pub pitch: bool,
141 pub rotate_during_zoom: bool,
143 pub zoom_anchor: GestureAnchor,
145 pub rotate_anchor: GestureAnchor,
147 pub pitch_per_px: f64,
150}
151
152impl Default for PinchOptions {
153 fn default() -> Self {
154 PinchOptions {
155 pan: true,
156 zoom: true,
157 rotate: true,
158 pitch: true,
159 rotate_during_zoom: true,
160 zoom_anchor: GestureAnchor::FocalPoint,
161 rotate_anchor: GestureAnchor::FocalPoint,
162 pitch_per_px: -0.5,
163 }
164 }
165}
166
167#[derive(Clone, Copy, Debug, PartialEq)]
169pub struct TapDragOptions {
170 pub enabled: bool,
173 pub zoom_per_px: f64,
176 pub anchor: GestureAnchor,
178}
179
180impl Default for TapDragOptions {
181 fn default() -> Self {
182 TapDragOptions { enabled: true, zoom_per_px: 1.0 / 128.0, anchor: GestureAnchor::CameraCenter }
183 }
184}
185
186#[derive(Clone, Copy, Debug, PartialEq)]
188pub struct TapZoomOptions {
189 pub enabled: bool,
192 pub zoom_levels: f64,
194 pub anchor: GestureAnchor,
196}
197
198impl TapZoomOptions {
199 pub const DOUBLE_TAP: Self = Self { enabled: true, zoom_levels: 1.0, anchor: GestureAnchor::FocalPoint };
201 pub const TWO_FINGER_TAP: Self = Self { enabled: true, zoom_levels: -1.0, anchor: GestureAnchor::FocalPoint };
203}
204
205#[derive(Clone, Copy, Debug, PartialEq)]
207pub struct BoxZoomOptions {
208 pub enabled: bool,
210 pub duration: Duration,
212}
213
214impl Default for BoxZoomOptions {
215 fn default() -> Self {
216 BoxZoomOptions { enabled: true, duration: Duration::from_millis(500) }
217 }
218}
219
220#[derive(Clone, Copy, Debug, PartialEq)]
223pub struct DeltaOptions {
224 pub step_duration: Duration,
227 pub max_step_zoom: f64,
232 pub idle: Duration,
235}
236
237impl Default for DeltaOptions {
238 fn default() -> Self {
239 DeltaOptions { step_duration: Duration::from_millis(200), max_step_zoom: 1.0, idle: Duration::from_millis(200) }
240 }
241}
242
243#[derive(Clone, Copy, Debug, PartialEq)]
245pub struct GestureBehavior {
246 pub drag: DragOptions,
247 pub pinch: PinchOptions,
248 pub tap_drag: TapDragOptions,
249 pub double_tap: TapZoomOptions,
250 pub two_finger_tap: TapZoomOptions,
251 pub box_zoom: BoxZoomOptions,
252 pub deltas: DeltaOptions,
253}
254
255impl Default for GestureBehavior {
256 fn default() -> Self {
257 GestureBehavior {
258 drag: DragOptions::default(),
259 pinch: PinchOptions::default(),
260 tap_drag: TapDragOptions::default(),
261 double_tap: TapZoomOptions::DOUBLE_TAP,
262 two_finger_tap: TapZoomOptions::TWO_FINGER_TAP,
263 box_zoom: BoxZoomOptions::default(),
264 deltas: DeltaOptions::default(),
265 }
266 }
267}
268
269#[derive(Clone, Copy, Debug, PartialEq)]
272pub struct GestureMetrics {
273 pub touch_slop: f64,
275 pub pointer_slop: f64,
278 pub double_tap_slop: f64,
281 pub double_tap_interval: Duration,
284 pub long_press: Duration,
286 pub pinch_zoom_threshold: f64,
288 pub pinch_rotate_threshold: f64,
291 pub pinch_pitch_slop: f64,
294}
295
296impl Default for GestureMetrics {
297 fn default() -> Self {
298 GestureMetrics {
299 touch_slop: 8.0,
300 pointer_slop: 3.0,
301 double_tap_slop: 40.0,
302 double_tap_interval: Duration::from_millis(300),
303 long_press: Duration::from_millis(500),
304 pinch_zoom_threshold: 0.1,
305 pinch_rotate_threshold: 25.0,
306 pinch_pitch_slop: 2.0,
307 }
308 }
309}
310
311#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
313pub enum InvalidGestureOptions {
314 #[error("the momentum of {0}: {1}")]
315 Momentum(&'static str, InvalidMomentum),
316 #[error("the snap of {0}: {1}")]
317 Snap(&'static str, InvalidMomentum),
318 #[error("a notch of {0} needs a positive spacing and a finite offset")]
319 Notch(&'static str),
320 #[error("gesture rates and tap zoom levels must be finite")]
321 Rate,
322 #[error("deltas.max_step_zoom must be finite and at least 1e-6")]
323 MaxStepZoom,
324 #[error("gesture metric {0} must be finite and not negative")]
325 Metric(&'static str),
326 #[error("long_press must be positive")]
327 LongPress,
328}
329
330#[derive(Clone, Debug, PartialEq)]
333pub struct GestureOptions {
334 pub axes: GestureAxes,
335 pub gestures: GestureBehavior,
336 pub metrics: GestureMetrics,
337
338 pub(crate) carry_caught_glide: bool,
343 pub(crate) interrupt_animations: bool,
346 pub(crate) transition_id: u64,
349}
350
351impl GestureOptions {
352 #[must_use]
354 pub(crate) fn axis(&self, axis: Axis) -> &AxisOptions {
355 match axis {
356 Axis::Zoom => &self.axes.zoom,
357 Axis::Bearing => &self.axes.bearing,
358 Axis::Pitch => &self.axes.pitch,
359 }
360 }
361
362 pub fn validate(&self) -> Result<(), InvalidGestureOptions> {
365 let momentum = |name, momentum: Option<MomentumDecay>| {
366 momentum
367 .map_or(Ok(()), |decay| decay.validate().map_err(|error| InvalidGestureOptions::Momentum(name, error)))
368 };
369 momentum("pan", self.axes.pan.momentum)?;
370 for (name, part) in [("zoom", &self.axes.zoom), ("bearing", &self.axes.bearing), ("pitch", &self.axes.pitch)] {
371 momentum(name, part.momentum)?;
372 if let Some(snap) = part.snap {
373 snap.validate().map_err(|error| InvalidGestureOptions::Snap(name, error))?;
374 }
375 let valid = |notch: &Notch| notch.spacing.is_finite() && notch.spacing > 0.0 && notch.offset.is_finite();
376 if !part.notches.iter().all(valid) {
377 return Err(InvalidGestureOptions::Notch(name));
378 }
379 }
380 let rates = [
381 self.gestures.drag.rotate_bearing_per_px,
382 self.gestures.drag.rotate_pitch_per_px,
383 self.gestures.pinch.pitch_per_px,
384 self.gestures.tap_drag.zoom_per_px,
385 self.gestures.double_tap.zoom_levels,
386 self.gestures.two_finger_tap.zoom_levels,
387 ];
388 if !rates.iter().all(|value| value.is_finite()) {
389 return Err(InvalidGestureOptions::Rate);
390 }
391 if !self.gestures.deltas.max_step_zoom.is_finite() || self.gestures.deltas.max_step_zoom < 1e-6 {
392 return Err(InvalidGestureOptions::MaxStepZoom);
393 }
394 for (name, value) in [
395 ("touch_slop", self.metrics.touch_slop),
396 ("pointer_slop", self.metrics.pointer_slop),
397 ("double_tap_slop", self.metrics.double_tap_slop),
398 ("pinch_zoom_threshold", self.metrics.pinch_zoom_threshold),
399 ("pinch_rotate_threshold", self.metrics.pinch_rotate_threshold),
400 ("pinch_pitch_slop", self.metrics.pinch_pitch_slop),
401 ] {
402 if !value.is_finite() || value < 0.0 {
403 return Err(InvalidGestureOptions::Metric(name));
404 }
405 }
406 if self.metrics.long_press.is_zero() {
407 return Err(InvalidGestureOptions::LongPress);
408 }
409 Ok(())
410 }
411}
412
413impl Default for GestureOptions {
414 fn default() -> Self {
415 GestureOptions {
416 axes: GestureAxes::default(),
417 gestures: GestureBehavior::default(),
418 metrics: GestureMetrics::default(),
419 carry_caught_glide: true,
420 interrupt_animations: true,
421 transition_id: 0x6765_7374_7572_6573,
423 }
424 }
425}