mln_engine/gestures/options.rs
1//! How gestures move each part of the camera, and the recognizer's rates and
2//! thresholds.
3
4use crate::camera::{InvalidMomentum, MomentumDecay, MomentumSnap};
5
6/// Values every `spacing` from `offset`, in the part's unit, that report a
7/// [`GestureEvent::Notch`](crate::gestures::GestureEvent::Notch) as a gesture moves the
8/// part to or across one, for the host to play as haptic feedback. Bearing
9/// notches lie within one turn from the offset.
10#[derive(Clone, Copy, Debug, PartialEq)]
11pub struct Notch {
12 /// Positive.
13 pub spacing: f64,
14 pub offset: f64,
15 /// The host's name for the feedback, such as which haptic to play, which
16 /// the notch's events carry.
17 pub kind: u32,
18}
19
20impl Notch {
21 /// North.
22 pub(crate) const NORTH: Notch = Notch { spacing: 360.0, offset: 0.0, kind: 0 };
23}
24
25/// How gestures move the map's content.
26#[derive(Clone, Copy, Debug, PartialEq)]
27pub struct PanOptions {
28 /// Whether gestures pan: drags, two fingers moving together, and deltas.
29 pub enabled: bool,
30 /// How a release glides; without it, the pan stops where the gesture
31 /// leaves it.
32 pub momentum: Option<MomentumDecay>,
33}
34
35/// How gestures move the zoom, the bearing or the pitch.
36#[derive(Clone, Debug, PartialEq)]
37pub struct AxisOptions {
38 /// Whether gestures move the part.
39 pub enabled: bool,
40 /// How a release glides; without it, the part stops where the gesture
41 /// leaves it.
42 pub momentum: Option<MomentumDecay>,
43 /// Where the part settles once its glide ends, after a gesture that
44 /// changed it.
45 pub snap: Option<MomentumSnap>,
46 /// Where the part reports notches while the recognizer's gestures, their
47 /// glides and their snaps move it. Several notches at one value report
48 /// one event, with the kind of the first in the list.
49 pub notches: Vec<Notch>,
50}
51
52/// One part of the camera that a gesture moves along a single value.
53#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
54pub enum Axis {
55 /// In zoom levels.
56 Zoom,
57 /// In degrees clockwise from north.
58 Bearing,
59 /// In degrees from looking straight down.
60 Pitch,
61}
62
63/// Why [`GestureOptions`] are out of range.
64#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
65pub enum InvalidGestureOptions {
66 #[error("the momentum of {0}: {1}")]
67 Momentum(&'static str, InvalidMomentum),
68 #[error("the snap of {0}: {1}")]
69 Snap(&'static str, InvalidMomentum),
70 #[error("a notch of {0} needs a positive spacing and a finite offset")]
71 Notch(&'static str),
72 #[error("a tap's zoom must be finite")]
73 TapZoom,
74}
75
76/// How the recognizer moves each part of the camera. By default every part
77/// moves and glides with its default decay ([`MomentumDecay::PAN`] and the
78/// rest); the bearing settles on north within 7° and notches there; zoom and
79/// pitch stay where gestures leave them, without notches
80/// ([`MomentumSnap::ZOOM`] and [`MomentumSnap::PITCH`] settle them on a whole
81/// level and flat); and a double tap zooms in a level and a two-finger tap
82/// out.
83///
84/// Distances are in logical pixels, and times in milliseconds (`_ms`).
85#[derive(Clone, Debug, PartialEq)]
86pub struct GestureOptions {
87 pub pan: PanOptions,
88 pub zoom: AxisOptions,
89 pub bearing: AxisOptions,
90 pub pitch: AxisOptions,
91 /// Zoom levels that a double tap zooms in, eased; the zoom-out mode zooms
92 /// out instead. 0 leaves double taps to the host.
93 pub double_tap_zoom: f64,
94 /// Zoom levels that a two-finger tap zooms, eased; negative zooms out.
95 /// 0 leaves two-finger taps to the host.
96 pub two_finger_tap_zoom: f64,
97
98 /// How far a press may move and still be a tap; drags start past it.
99 pub(crate) click_tolerance: f64,
100 /// How far apart two taps of a double tap may be, and the most time
101 /// between them. A two-finger tap's fingers may move this far.
102 pub(crate) tap_tolerance: f64,
103 pub(crate) tap_interval_ms: f64,
104 /// How long a press stays still to be a long press (Android's and iOS's
105 /// 500 ms).
106 pub(crate) long_press_ms: f64,
107 /// Degrees of bearing per pixel of a sideways rotate drag: ffi's demo's
108 /// 0.5 (GL JS, Compose and Orbis turn 0.8).
109 pub(crate) drag_rotate: f64,
110 /// Degrees of pitch per pixel of an upward rotate drag or two-finger
111 /// drag.
112 pub(crate) drag_pitch: f64,
113 /// Zoom levels per pixel of a double-tap drag downward.
114 pub(crate) tap_drag_zoom: f64,
115 /// Zoom levels a pinch spreads or closes before it zooms.
116 pub(crate) pinch_zoom_threshold: f64,
117 /// Pixels along the fingers' circle a twist travels before it rotates.
118 pub(crate) rotate_threshold: f64,
119 /// How long a discrete delta eases (GL JS's wheel's 200 ms).
120 pub(crate) step_duration_ms: f64,
121 /// The most a delta zooms in one step, in levels: GL JS's
122 /// `maxScalePerFrame` of 2. A discrete delta carries the rest to later
123 /// frames.
124 pub(crate) max_step_zoom: f64,
125 /// Deltas are one gesture until this long after their last input and
126 /// ease (Compose's and GL JS's 200 ms for scrolls).
127 pub(crate) delta_idle_ms: f64,
128
129 /// Whether a release that catches the recognizer's last glide (a flick
130 /// that interrupts a glide going its way) adds that glide's speed, as
131 /// `UIScrollView`, Android's scroll flywheel and Compose do. Each part's
132 /// decay still caps the speed. No map app measured in
133 /// maplibre-native-ffi#1705 does it (Apple Maps, Google Maps), nor do GL
134 /// JS and ffi's demo.
135 pub(crate) carry_caught_glide: bool,
136 /// Whether a press stops every camera animation, as GL JS and the
137 /// MapLibre Native SDKs do, or only the recognizer's own.
138 pub(crate) interrupt_animations: bool,
139 /// The [`AnimationOptions::transition_id`](crate::camera::AnimationOptions::transition_id)
140 /// of the eases and momentum the recognizer starts.
141 pub(crate) transition_id: u64,
142}
143
144impl GestureOptions {
145 /// The options of `axis`.
146 #[must_use]
147 pub(crate) fn axis(&self, axis: Axis) -> &AxisOptions {
148 match axis {
149 Axis::Zoom => &self.zoom,
150 Axis::Bearing => &self.bearing,
151 Axis::Pitch => &self.pitch,
152 }
153 }
154
155 /// Checks every part's momentum, snap and notches, and the taps' zooms.
156 pub fn validate(&self) -> Result<(), InvalidGestureOptions> {
157 if !self.double_tap_zoom.is_finite() || !self.two_finger_tap_zoom.is_finite() {
158 return Err(InvalidGestureOptions::TapZoom);
159 }
160 let momentum = |name, momentum: Option<MomentumDecay>| {
161 momentum
162 .map_or(Ok(()), |decay| decay.validate().map_err(|error| InvalidGestureOptions::Momentum(name, error)))
163 };
164 momentum("pan", self.pan.momentum)?;
165 for (name, part) in [("zoom", &self.zoom), ("bearing", &self.bearing), ("pitch", &self.pitch)] {
166 momentum(name, part.momentum)?;
167 if let Some(snap) = part.snap {
168 snap.validate().map_err(|error| InvalidGestureOptions::Snap(name, error))?;
169 }
170 let valid = |notch: &Notch| notch.spacing.is_finite() && notch.spacing > 0.0 && notch.offset.is_finite();
171 if !part.notches.iter().all(valid) {
172 return Err(InvalidGestureOptions::Notch(name));
173 }
174 }
175 Ok(())
176 }
177}
178
179impl Default for GestureOptions {
180 fn default() -> Self {
181 let axis = |momentum: MomentumDecay, snap: Option<MomentumSnap>, notches: Vec<Notch>| AxisOptions {
182 enabled: true,
183 momentum: Some(momentum),
184 snap,
185 notches,
186 };
187 GestureOptions {
188 pan: PanOptions { enabled: true, momentum: Some(MomentumDecay::PAN) },
189 zoom: axis(MomentumDecay::ZOOM, None, Vec::new()),
190 bearing: axis(MomentumDecay::BEARING, Some(MomentumSnap::BEARING), vec![Notch::NORTH]),
191 pitch: axis(MomentumDecay::PITCH, None, Vec::new()),
192 double_tap_zoom: 1.0,
193 two_finger_tap_zoom: -1.0,
194 click_tolerance: 3.0,
195 tap_tolerance: 30.0,
196 tap_interval_ms: 500.0,
197 long_press_ms: 500.0,
198 drag_rotate: 0.5,
199 drag_pitch: 0.5,
200 tap_drag_zoom: 1.0 / 128.0,
201 pinch_zoom_threshold: 0.1,
202 rotate_threshold: 25.0,
203 step_duration_ms: 200.0,
204 max_step_zoom: 1.0,
205 delta_idle_ms: 200.0,
206 carry_caught_glide: true,
207 interrupt_animations: true,
208 // "gestures" in ASCII.
209 transition_id: 0x6765_7374_7572_6573,
210 }
211 }
212}