use crate::camera::{InvalidMomentum, MomentumDecay, MomentumSnap};
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Notch {
pub spacing: f64,
pub offset: f64,
pub kind: u32,
}
impl Notch {
pub const NORTH: Notch = Notch { spacing: 360.0, offset: 0.0, kind: 0 };
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct PanOptions {
pub enabled: bool,
pub momentum: Option<MomentumDecay>,
}
#[derive(Clone, Debug, PartialEq)]
pub struct AxisOptions {
pub enabled: bool,
pub momentum: Option<MomentumDecay>,
pub snap: Option<MomentumSnap>,
pub notches: Vec<Notch>,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Axis {
Zoom,
Bearing,
Pitch,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)]
pub enum InvalidGestureOptions {
#[error("the momentum of {0}: {1}")]
Momentum(&'static str, InvalidMomentum),
#[error("the snap of {0}: {1}")]
Snap(&'static str, InvalidMomentum),
#[error("a notch of {0} needs a positive spacing and a finite offset")]
Notch(&'static str),
#[error("a tap's zoom must be finite")]
TapZoom,
}
#[derive(Clone, Debug, PartialEq)]
pub struct GestureOptions {
pub pan: PanOptions,
pub zoom: AxisOptions,
pub bearing: AxisOptions,
pub pitch: AxisOptions,
pub double_tap_zoom: f64,
pub two_finger_tap_zoom: f64,
pub(crate) click_tolerance: f64,
pub(crate) tap_tolerance: f64,
pub(crate) tap_interval_ms: f64,
pub(crate) long_press_ms: f64,
pub(crate) drag_rotate: f64,
pub(crate) drag_pitch: f64,
pub(crate) tap_drag_zoom: f64,
pub(crate) pinch_zoom_threshold: f64,
pub(crate) rotate_threshold: f64,
pub(crate) step_duration_ms: f64,
pub(crate) max_step_zoom: f64,
pub(crate) delta_idle_ms: f64,
pub(crate) carry_caught_glide: bool,
pub(crate) interrupt_animations: bool,
pub(crate) transition_id: u64,
}
impl GestureOptions {
#[must_use]
pub fn axis(&self, axis: Axis) -> &AxisOptions {
match axis {
Axis::Zoom => &self.zoom,
Axis::Bearing => &self.bearing,
Axis::Pitch => &self.pitch,
}
}
pub fn validate(&self) -> Result<(), InvalidGestureOptions> {
if !self.double_tap_zoom.is_finite() || !self.two_finger_tap_zoom.is_finite() {
return Err(InvalidGestureOptions::TapZoom);
}
let momentum = |name, momentum: Option<MomentumDecay>| {
momentum
.map_or(Ok(()), |decay| decay.validate().map_err(|error| InvalidGestureOptions::Momentum(name, error)))
};
momentum("pan", self.pan.momentum)?;
for (name, part) in [("zoom", &self.zoom), ("bearing", &self.bearing), ("pitch", &self.pitch)] {
momentum(name, part.momentum)?;
if let Some(snap) = part.snap {
snap.validate().map_err(|error| InvalidGestureOptions::Snap(name, error))?;
}
let valid = |notch: &Notch| notch.spacing.is_finite() && notch.spacing > 0.0 && notch.offset.is_finite();
if !part.notches.iter().all(valid) {
return Err(InvalidGestureOptions::Notch(name));
}
}
Ok(())
}
}
impl Default for GestureOptions {
fn default() -> Self {
let axis = |momentum: MomentumDecay, snap: Option<MomentumSnap>, notches: Vec<Notch>| AxisOptions {
enabled: true,
momentum: Some(momentum),
snap,
notches,
};
GestureOptions {
pan: PanOptions { enabled: true, momentum: Some(MomentumDecay::PAN) },
zoom: axis(MomentumDecay::ZOOM, None, Vec::new()),
bearing: axis(MomentumDecay::BEARING, Some(MomentumSnap::BEARING), vec![Notch::NORTH]),
pitch: axis(MomentumDecay::PITCH, None, Vec::new()),
double_tap_zoom: 1.0,
two_finger_tap_zoom: -1.0,
click_tolerance: 3.0,
tap_tolerance: 30.0,
tap_interval_ms: 500.0,
long_press_ms: 500.0,
drag_rotate: 0.5,
drag_pitch: 0.5,
tap_drag_zoom: 1.0 / 128.0,
pinch_zoom_threshold: 0.1,
rotate_threshold: 25.0,
step_duration_ms: 200.0,
max_step_zoom: 1.0,
delta_idle_ms: 200.0,
carry_caught_glide: true,
interrupt_animations: true,
transition_id: 0x6765_7374_7572_6573,
}
}
}