mln_engine/gestures/input.rs
1//! What hosts feed the recognizer, and the gestures it reports back.
2//!
3//! Hosts translate each platform event on its own, with no memory: which
4//! buttons and keys map to a pointer's [`DragAction`], [`PointerFlags`] and
5//! [`Modes`], and how far a wheel, dial or key moves the camera
6//! ([`Input::Delta`]). The recognizer interprets them over time.
7
8use crate::geo::ScreenCoordinate;
9use crate::gestures::options::Axis;
10
11/// What kind of device a pointer is. Determines which slop applies and pairs
12/// double taps by device kind; touches may also track multiple pointers.
13#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
14pub enum PointerKind {
15 Mouse,
16 Touch,
17 Pen,
18}
19
20/// What a single-pointer drag does once the pointer crosses its slop.
21#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
22pub enum DragAction {
23 /// Pans the map, unless held [`Modes`] override it.
24 #[default]
25 Pan,
26 /// Never starts a single-pointer drag; taps, long presses, and any
27 /// gestures opted into via [`PointerFlags`] (pinch, double-tap drag) still
28 /// run.
29 None,
30 /// Sideways turns the map and up and down tilts it.
31 Rotate,
32 /// Draws a box that the map zooms to when the pointer lifts.
33 BoxZoom,
34 /// Holds the drag when the pointer crosses slop and emits
35 /// [`GestureEvent::DragPending`] until the host supplies the resolved
36 /// action with [`Input::DragDecision`] (which may also arrive before slop
37 /// is crossed). [`PointerFlags::allow_tap_drag`] still takes precedence
38 /// over `drag_action`. Hosts must use a fresh pointer `id` for each
39 /// deferred press while an earlier press's decision may still be in flight.
40 Defer,
41}
42
43/// Per-pointer gesture capabilities on [`Input::PointerDown`].
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
45pub struct PointerFlags {
46 /// May join a two-pointer pinch, twist, or tilt (independent of
47 /// `drag_action`).
48 pub allow_pinch: bool,
49 /// May start a double-tap drag (quick zoom) when pressed right after a tap
50 /// (takes precedence over `drag_action`, including [`DragAction::None`]).
51 pub allow_tap_drag: bool,
52 /// A double tap by this pointer zooms out instead of in.
53 pub zoom_out: bool,
54}
55
56impl PointerFlags {
57 /// The default flags for `kind`: touches allow pinch and double-tap drag;
58 /// mouse and pen pointers start with none.
59 #[must_use]
60 pub const fn for_kind(kind: PointerKind) -> Self {
61 match kind {
62 PointerKind::Touch => Self { allow_pinch: true, allow_tap_drag: true, zoom_out: false },
63 PointerKind::Mouse | PointerKind::Pen => {
64 Self { allow_pinch: false, allow_tap_drag: false, zoom_out: false }
65 }
66 }
67 }
68}
69
70/// What held keys select for [`DragAction::Pan`] drags and double taps
71/// ([`Input::Modes`]). A drag reads the modes when it crosses slop (or when
72/// [`Input::DragDecision`] resolves a pending drag to [`DragAction::Pan`])
73/// and keeps them until it ends, and a double tap when it lifts; rotating wins
74/// over a box zoom.
75#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
76pub struct Modes {
77 /// Sideways turns the map and up and down tilts it.
78 pub rotate: bool,
79 /// Draws a box that the map zooms to when the pointer lifts.
80 pub box_zoom: bool,
81 /// A double tap zooms out instead of in.
82 pub zoom_out: bool,
83}
84
85/// One input, in logical pixels from the viewport's top-left corner.
86///
87/// When the view loses focus, hosts cancel the pointers they sent and send
88/// empty modes.
89#[derive(Clone, Copy, Debug, PartialEq)]
90pub enum Input {
91 /// A pointer went down with the host's chosen `drag_action` and `flags`.
92 /// Hosts send no pointer whose buttons mean nothing, such as a mouse's
93 /// middle button.
94 PointerDown {
95 id: u32,
96 kind: PointerKind,
97 position: ScreenCoordinate,
98 drag_action: DragAction,
99 flags: PointerFlags,
100 },
101 PointerMove {
102 id: u32,
103 position: ScreenCoordinate,
104 },
105 PointerUp {
106 id: u32,
107 position: ScreenCoordinate,
108 },
109 /// The platform or the host took the pointer away (a system gesture,
110 /// lost capture, Escape during a box zoom): no tap and no momentum.
111 PointerCancel {
112 id: u32,
113 },
114 /// The modes that held keys select, for every pointer.
115 Modes(Modes),
116 /// Camera movement from anything but a pointer (a wheel, a trackpad
117 /// scroll, a dial, a crown, keys, buttons, a pinch or a pan the platform
118 /// recognized), which the host converts into camera units: `pan` moves
119 /// the map's content on screen in logical pixels, `zoom` adds levels,
120 /// `bearing` turns clockwise in degrees, and `pitch` tilts in degrees.
121 /// Zoom and turns hold `anchor` in place, or the padded camera center when
122 /// `anchor` is `None`. A pinch the platform recognized, which scales by
123 /// `s` and turns its fingers `r` degrees clockwise, is a zoom of `log2(s)`
124 /// and a bearing of `-r` around its center: the map turns with the
125 /// fingers.
126 ///
127 /// A continuous delta (a trackpad scroll, a platform pinch) applies at
128 /// once. A discrete one (a wheel detent, a key press) eases, and a quick
129 /// run of them merges into one ease that speeds up. Deltas are one
130 /// gesture until they idle, or until [`Input::DeltaEnd`] or
131 /// [`Input::DeltaCancel`] ends them. During a pointer gesture they join
132 /// it, and a pointer that goes down takes over from them.
133 Delta {
134 anchor: Option<ScreenCoordinate>,
135 discrete: bool,
136 pan: ScreenCoordinate,
137 zoom: f64,
138 bearing: f64,
139 pitch: f64,
140 },
141 /// The platform ended the deltas under way, as when the fingers of a
142 /// pinch or a pan it recognized lift: they glide on at `fling`, or
143 /// without one at the speed their continuous deltas had over their last
144 /// 100 ms, by the options' momentum. With no deltas under way, a fling
145 /// starts and releases a gesture of its own, such as an Android Auto
146 /// `onFling`. Discrete deltas still easing finish their ease. During a
147 /// pointer gesture it does nothing: the pointers end the gesture.
148 DeltaEnd {
149 fling: Option<Fling>,
150 },
151 /// The platform took the deltas under way away: nothing continues, and
152 /// the camera's animations stop. During a pointer gesture it does
153 /// nothing.
154 DeltaCancel,
155 /// Resolves a pointer's [`DragAction::Defer`] with `drag_action` (any
156 /// action other than [`DragAction::Defer`]). May be sent either before
157 /// the pointer crosses slop or in response to
158 /// [`GestureEvent::DragPending`]; ignored if the pointer is no longer
159 /// waiting on a decision. Because decisions match by `id`, do not reuse a
160 /// pointer `id` across deferred presses while an earlier decision may
161 /// still arrive.
162 DragDecision {
163 id: u32,
164 drag_action: DragAction,
165 },
166}
167
168/// The speeds of a release the platform measured, per second in
169/// [`Input::Delta`]'s units. Zooms and turns hold `anchor` in place, or the
170/// padded camera center when `None`.
171#[derive(Clone, Copy, Debug, Default, PartialEq)]
172pub struct Fling {
173 pub anchor: Option<ScreenCoordinate>,
174 pub pan: ScreenCoordinate,
175 pub zoom: f64,
176 pub bearing: f64,
177 pub pitch: f64,
178}
179
180/// What a gesture means beyond moving the camera. Double taps and two-finger
181/// taps also zoom, as the options say.
182#[derive(Clone, Copy, Debug, PartialEq)]
183pub enum GestureEvent {
184 /// The pointer `id` pressed and lifted without moving the map, before a
185 /// long press. Emitted immediately on lift, including for each tap of a
186 /// double tap.
187 Tap { id: u32, position: ScreenCoordinate },
188 /// A [`Tap`](Self::Tap) confirmed not to be the first tap of a double tap
189 /// or double-tap drag, once `metrics.double_tap_interval` elapses (or
190 /// immediately on lift when that interval is zero, or when a later press
191 /// cannot pair with it).
192 SingleTap { id: u32, position: ScreenCoordinate },
193 /// The pointer `id` stayed pressed and still for the long-press timeout;
194 /// its lift makes no tap.
195 LongPress { id: u32, position: ScreenCoordinate },
196 /// The second tap of a double tap, by pointer `id`.
197 DoubleTap { id: u32, position: ScreenCoordinate },
198 /// Two fingers tapped together, around `position`.
199 TwoFingerTap { position: ScreenCoordinate },
200 /// A gesture, its glide or its snap moved `axis` to or across the
201 /// [`Notch`](crate::gestures::Notch) of `kind` at `value`, in the axis's unit, a
202 /// bearing in [0°, 360°): the host plays its feedback. A gesture that
203 /// starts at a notch reports it only once it leaves and comes back, an
204 /// axis reports at most one notch every 50 ms, and box zooms report none.
205 Notch { axis: Axis, value: f64, kind: u32 },
206 /// The pointer `id` went down with [`DragAction::Defer`] at `origin` and
207 /// crossed its slop at `position`. The recognizer holds the drag until the
208 /// host sends [`Input::DragDecision`], a second finger starts a pinch, or
209 /// the pointer ends.
210 DragPending { id: u32, origin: ScreenCoordinate, position: ScreenCoordinate },
211}