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 mean which [`Modes`], and how far a wheel, dial or key
5//! moves the camera ([`Input::Delta`]). The recognizer interprets them over
6//! time.
7
8use std::ops::BitOr;
9
10use crate::geo::ScreenCoordinate;
11use crate::gestures::options::Axis;
12
13/// What kind of device a pointer is. Touches pinch, twist and tilt with two
14/// fingers, and a held second tap zooms as it drags.
15#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
16pub enum PointerKind {
17 Mouse,
18 Touch,
19 Pen,
20}
21
22/// What a one-pointer drag or a double tap does instead of its default, from
23/// the pointer's held buttons (a mouse's secondary button rotates on most
24/// platforms) or from held keys ([`Input::Modes`]). A drag reads the modes
25/// when it starts and keeps them until it ends, and a double tap when it
26/// lifts; rotating wins over a box zoom.
27#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
28pub struct Modes {
29 /// Sideways turns the map and up and down tilts it.
30 pub rotate: bool,
31 /// Draws a box that the map zooms to when the pointer lifts.
32 pub box_zoom: bool,
33 /// A double tap zooms out instead of in.
34 pub zoom_out: bool,
35}
36
37impl BitOr for Modes {
38 type Output = Modes;
39
40 fn bitor(self, other: Modes) -> Modes {
41 Modes {
42 rotate: self.rotate || other.rotate,
43 box_zoom: self.box_zoom || other.box_zoom,
44 zoom_out: self.zoom_out || other.zoom_out,
45 }
46 }
47}
48
49/// One input, in logical pixels from the viewport's top-left corner.
50///
51/// When the view loses focus, hosts cancel the pointers they sent and send
52/// empty modes.
53#[derive(Clone, Copy, Debug, PartialEq)]
54pub enum Input {
55 /// A pointer whose buttons the host maps to `modes`. Hosts send no
56 /// pointer whose buttons mean nothing, such as a mouse's middle button.
57 PointerDown {
58 id: u32,
59 kind: PointerKind,
60 position: ScreenCoordinate,
61 modes: Modes,
62 },
63 PointerMove {
64 id: u32,
65 position: ScreenCoordinate,
66 },
67 PointerUp {
68 id: u32,
69 position: ScreenCoordinate,
70 },
71 /// The platform or the host took the pointer away (a system gesture,
72 /// lost capture, Escape during a box zoom): no tap and no momentum.
73 PointerCancel {
74 id: u32,
75 },
76 /// The modes that held keys select, for every pointer.
77 Modes(Modes),
78 /// Camera movement from anything but a pointer (a wheel, a trackpad
79 /// scroll, a dial, a crown, keys, buttons), which the host converts into
80 /// camera units: `pan` moves the map's content on screen in logical
81 /// pixels, `zoom` adds levels, `bearing` turns clockwise in degrees, and
82 /// `pitch` tilts in degrees. Zoom and turns hold `position` in place.
83 ///
84 /// A continuous delta (a trackpad scroll) applies at once. A discrete one
85 /// (a wheel detent, a key press) eases, and a quick run of them merges
86 /// into one ease that speeds up. Deltas are one gesture until they idle.
87 Delta {
88 position: ScreenCoordinate,
89 discrete: bool,
90 pan: ScreenCoordinate,
91 zoom: f64,
92 bearing: f64,
93 pitch: f64,
94 },
95 /// A pinch the platform recognized, such as a macOS trackpad's magnify
96 /// and rotate or a browser's Ctrl+wheel: each update zooms by `scale`
97 /// (above 1 spreads the fingers, zooming in) and turns `rotation` degrees
98 /// clockwise on screen since the last, around `center`. It glides on when
99 /// it ends, as a touch pinch does. An update without a begin begins;
100 /// pointers already down keep the map, and a touch that goes down takes
101 /// over from the pinch.
102 Pinch {
103 center: ScreenCoordinate,
104 scale: f64,
105 rotation: f64,
106 phase: PinchPhase,
107 },
108}
109
110/// Where a platform-recognized pinch is.
111#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
112pub enum PinchPhase {
113 Begin,
114 Update,
115 /// The fingers lifted: the pinch glides on.
116 End,
117 /// The platform took the pinch away: nothing continues, and the camera's
118 /// animations stop.
119 Cancel,
120}
121
122/// What a gesture means beyond moving the camera. Double taps and two-finger
123/// taps also zoom, as the options say.
124#[derive(Clone, Copy, Debug, PartialEq)]
125pub enum GestureEvent {
126 /// The pointer `id` pressed and lifted without moving the map, before a
127 /// long press.
128 Tap { id: u32, position: ScreenCoordinate },
129 /// The pointer `id` stayed pressed and still for 500 ms; its lift makes
130 /// no tap.
131 LongPress { id: u32, position: ScreenCoordinate },
132 /// The second tap of a double tap, by pointer `id`.
133 DoubleTap { id: u32, position: ScreenCoordinate },
134 /// Two fingers tapped together, around `position`.
135 TwoFingerTap { position: ScreenCoordinate },
136 /// A gesture, its glide or its snap moved `axis` to or across the
137 /// [`Notch`](crate::gestures::Notch) of `kind` at `value`, in the axis's unit, a
138 /// bearing in [0°, 360°): the host plays its feedback. A gesture that
139 /// starts at a notch reports it only once it leaves and comes back, an
140 /// axis reports at most one notch every 50 ms, and box zooms report none.
141 Notch { axis: Axis, value: f64, kind: u32 },
142}