mln-rs-engine 0.0.22

Part of mln-rs, a MapLibre map renderer written in Rust. Depend on mln-rs instead.
Documentation
//! What hosts feed the recognizer, and the gestures it reports back.
//!
//! Hosts translate each platform event on its own, with no memory: which
//! buttons and keys map to a pointer's [`DragAction`], [`PointerFlags`] and
//! [`Modes`], and how far a wheel, dial or key moves the camera
//! ([`Input::Delta`]). The recognizer interprets them over time.

use crate::geo::ScreenCoordinate;
use crate::gestures::options::Axis;

/// What kind of device a pointer is. Determines which slop applies and pairs
/// double taps by device kind; touches may also track multiple pointers.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum PointerKind {
    Mouse,
    Touch,
    Pen,
}

/// What a single-pointer drag does once the pointer crosses its slop.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum DragAction {
    /// Pans the map, unless held [`Modes`] override it.
    #[default]
    Pan,
    /// Never starts a single-pointer drag; taps, long presses, and any
    /// gestures opted into via [`PointerFlags`] (pinch, double-tap drag) still
    /// run.
    None,
    /// Sideways turns the map and up and down tilts it.
    Rotate,
    /// Draws a box that the map zooms to when the pointer lifts.
    BoxZoom,
    /// Holds the drag when the pointer crosses slop and emits
    /// [`GestureEvent::DragPending`] until the host supplies the resolved
    /// action with [`Input::DragDecision`] (which may also arrive before slop
    /// is crossed). [`PointerFlags::allow_tap_drag`] still takes precedence
    /// over `drag_action`. Hosts must use a fresh pointer `id` for each
    /// deferred press while an earlier press's decision may still be in flight.
    Defer,
}

/// Per-pointer gesture capabilities on [`Input::PointerDown`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PointerFlags {
    /// May join a two-pointer pinch, twist, or tilt (independent of
    /// `drag_action`).
    pub allow_pinch: bool,
    /// May start a double-tap drag (quick zoom) when pressed right after a tap
    /// (takes precedence over `drag_action`, including [`DragAction::None`]).
    pub allow_tap_drag: bool,
    /// A double tap by this pointer zooms out instead of in.
    pub zoom_out: bool,
}

impl PointerFlags {
    /// The default flags for `kind`: touches allow pinch and double-tap drag;
    /// mouse and pen pointers start with none.
    #[must_use]
    pub const fn for_kind(kind: PointerKind) -> Self {
        match kind {
            PointerKind::Touch => Self { allow_pinch: true, allow_tap_drag: true, zoom_out: false },
            PointerKind::Mouse | PointerKind::Pen => {
                Self { allow_pinch: false, allow_tap_drag: false, zoom_out: false }
            }
        }
    }
}

/// What held keys select for [`DragAction::Pan`] drags and double taps
/// ([`Input::Modes`]). A drag reads the modes when it crosses slop (or when
/// [`Input::DragDecision`] resolves a pending drag to [`DragAction::Pan`])
/// and keeps them until it ends, and a double tap when it lifts; rotating wins
/// over a box zoom.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Modes {
    /// Sideways turns the map and up and down tilts it.
    pub rotate: bool,
    /// Draws a box that the map zooms to when the pointer lifts.
    pub box_zoom: bool,
    /// A double tap zooms out instead of in.
    pub zoom_out: bool,
}

/// One input, in logical pixels from the viewport's top-left corner.
///
/// When the view loses focus, hosts cancel the pointers they sent and send
/// empty modes.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Input {
    /// A pointer went down with the host's chosen `drag_action` and `flags`.
    /// Hosts send no pointer whose buttons mean nothing, such as a mouse's
    /// middle button.
    PointerDown {
        id: u32,
        kind: PointerKind,
        position: ScreenCoordinate,
        drag_action: DragAction,
        flags: PointerFlags,
    },
    PointerMove {
        id: u32,
        position: ScreenCoordinate,
    },
    PointerUp {
        id: u32,
        position: ScreenCoordinate,
    },
    /// The platform or the host took the pointer away (a system gesture,
    /// lost capture, Escape during a box zoom): no tap and no momentum.
    PointerCancel {
        id: u32,
    },
    /// The modes that held keys select, for every pointer.
    Modes(Modes),
    /// Camera movement from anything but a pointer (a wheel, a trackpad
    /// scroll, a dial, a crown, keys, buttons, a pinch or a pan the platform
    /// recognized), which the host converts into camera units: `pan` moves
    /// the map's content on screen in logical pixels, `zoom` adds levels,
    /// `bearing` turns clockwise in degrees, and `pitch` tilts in degrees.
    /// Zoom and turns hold `anchor` in place, or the padded camera center when
    /// `anchor` is `None`. A pinch the platform recognized, which scales by
    /// `s` and turns its fingers `r` degrees clockwise, is a zoom of `log2(s)`
    /// and a bearing of `-r` around its center: the map turns with the
    /// fingers.
    ///
    /// A continuous delta (a trackpad scroll, a platform pinch) applies at
    /// once. A discrete one (a wheel detent, a key press) eases, and a quick
    /// run of them merges into one ease that speeds up. Deltas are one
    /// gesture until they idle, or until [`Input::DeltaEnd`] or
    /// [`Input::DeltaCancel`] ends them. During a pointer gesture they join
    /// it, and a pointer that goes down takes over from them.
    Delta {
        anchor: Option<ScreenCoordinate>,
        discrete: bool,
        pan: ScreenCoordinate,
        zoom: f64,
        bearing: f64,
        pitch: f64,
    },
    /// The platform ended the deltas under way, as when the fingers of a
    /// pinch or a pan it recognized lift: they glide on at `fling`, or
    /// without one at the speed their continuous deltas had over their last
    /// 100 ms, by the options' momentum. With no deltas under way, a fling
    /// starts and releases a gesture of its own, such as an Android Auto
    /// `onFling`. Discrete deltas still easing finish their ease. During a
    /// pointer gesture it does nothing: the pointers end the gesture.
    DeltaEnd {
        fling: Option<Fling>,
    },
    /// The platform took the deltas under way away: nothing continues, and
    /// the camera's animations stop. During a pointer gesture it does
    /// nothing.
    DeltaCancel,
    /// Resolves a pointer's [`DragAction::Defer`] with `drag_action` (any
    /// action other than [`DragAction::Defer`]). May be sent either before
    /// the pointer crosses slop or in response to
    /// [`GestureEvent::DragPending`]; ignored if the pointer is no longer
    /// waiting on a decision. Because decisions match by `id`, do not reuse a
    /// pointer `id` across deferred presses while an earlier decision may
    /// still arrive.
    DragDecision {
        id: u32,
        drag_action: DragAction,
    },
}

/// The speeds of a release the platform measured, per second in
/// [`Input::Delta`]'s units. Zooms and turns hold `anchor` in place, or the
/// padded camera center when `None`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Fling {
    pub anchor: Option<ScreenCoordinate>,
    pub pan: ScreenCoordinate,
    pub zoom: f64,
    pub bearing: f64,
    pub pitch: f64,
}

/// What a gesture means beyond moving the camera. Double taps and two-finger
/// taps also zoom, as the options say.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum GestureEvent {
    /// The pointer `id` pressed and lifted without moving the map, before a
    /// long press. Emitted immediately on lift, including for each tap of a
    /// double tap.
    Tap { id: u32, position: ScreenCoordinate },
    /// A [`Tap`](Self::Tap) confirmed not to be the first tap of a double tap
    /// or double-tap drag, once `metrics.double_tap_interval` elapses (or
    /// immediately on lift when that interval is zero, or when a later press
    /// cannot pair with it).
    SingleTap { id: u32, position: ScreenCoordinate },
    /// The pointer `id` stayed pressed and still for the long-press timeout;
    /// its lift makes no tap.
    LongPress { id: u32, position: ScreenCoordinate },
    /// The second tap of a double tap, by pointer `id`.
    DoubleTap { id: u32, position: ScreenCoordinate },
    /// Two fingers tapped together, around `position`.
    TwoFingerTap { position: ScreenCoordinate },
    /// A gesture, its glide or its snap moved `axis` to or across the
    /// [`Notch`](crate::gestures::Notch) of `kind` at `value`, in the axis's unit, a
    /// bearing in [0°, 360°): the host plays its feedback. A gesture that
    /// starts at a notch reports it only once it leaves and comes back, an
    /// axis reports at most one notch every 50 ms, and box zooms report none.
    Notch { axis: Axis, value: f64, kind: u32 },
    /// The pointer `id` went down with [`DragAction::Defer`] at `origin` and
    /// crossed its slop at `position`. The recognizer holds the drag until the
    /// host sends [`Input::DragDecision`], a second finger starts a pinch, or
    /// the pointer ends.
    DragPending { id: u32, origin: ScreenCoordinate, position: ScreenCoordinate },
}