use std::ops::{Add, AddAssign, Div, Mul, Sub};
use std::time::Duration;
use mln_style::unitbezier::UnitBezier;
use crate::camera::{
AnchoredMomentum, AnimationOptions, CameraDelta, CameraMomentum, CameraOptions, CameraUpdate, CameraUpdateMode,
CameraVelocity, GesturePhase, MomentumGlide, TransitionOutcome,
};
use crate::geo::ScreenCoordinate;
use crate::gestures::camera::{GestureCamera, PendingEnd};
use crate::gestures::input::{GestureEvent, Input, Modes, PinchPhase, PointerKind};
use crate::gestures::notches::NotchWatch;
use crate::gestures::options::{AxisOptions, GestureOptions, InvalidGestureOptions};
use crate::gestures::velocity::VelocityTracker;
const BOX_EASE: Duration = Duration::from_millis(500);
const EASE_X1: f64 = 0.25;
const EASE_Y1: f64 = 0.1;
const EASE_X2: f64 = 0.25;
const EASE_Y2: f64 = 1.0;
const EASE: UnitBezier = UnitBezier::new(EASE_X1, EASE_Y1, EASE_X2, EASE_Y2);
const STAGED_RELEASE_MS: f64 = 100.0;
const PITCH_DECIDE_PX: f64 = 2.0;
const PITCH_DECIDE_MS: f64 = 100.0;
const STEP_EASE_HANDLE: f64 = 0.27;
const STEP_EASE_DONE: f64 = 1e-9;
const DELTA_PARTS: usize = 5;
const DELTA_ZOOM: usize = 2;
#[derive(Clone, Copy, Debug, Default, PartialEq)]
struct V2 {
x: f64,
y: f64,
}
impl V2 {
const ZERO: V2 = V2 { x: 0.0, y: 0.0 };
fn new(x: f64, y: f64) -> V2 {
V2 { x, y }
}
fn length(self) -> f64 {
self.x.hypot(self.y)
}
fn distance(self, other: V2) -> f64 {
(self - other).length()
}
fn dot(self, other: V2) -> f64 {
self.x * other.x + self.y * other.y
}
fn abs(self) -> V2 {
V2::new(self.x.abs(), self.y.abs())
}
}
impl From<ScreenCoordinate> for V2 {
fn from(p: ScreenCoordinate) -> V2 {
V2::new(p.x, p.y)
}
}
impl From<V2> for ScreenCoordinate {
fn from(v: V2) -> ScreenCoordinate {
ScreenCoordinate::new(v.x, v.y)
}
}
impl Add for V2 {
type Output = V2;
fn add(self, o: V2) -> V2 {
V2::new(self.x + o.x, self.y + o.y)
}
}
impl AddAssign for V2 {
fn add_assign(&mut self, o: V2) {
*self = *self + o;
}
}
impl Sub for V2 {
type Output = V2;
fn sub(self, o: V2) -> V2 {
V2::new(self.x - o.x, self.y - o.y)
}
}
impl Mul<f64> for V2 {
type Output = V2;
fn mul(self, s: f64) -> V2 {
V2::new(self.x * s, self.y * s)
}
}
impl Div<f64> for V2 {
type Output = V2;
fn div(self, s: f64) -> V2 {
V2::new(self.x / s, self.y / s)
}
}
#[derive(Clone, Copy, Debug)]
struct Pointer {
id: u32,
kind: PointerKind,
position: V2,
down: V2,
down_at: f64,
modes: Modes,
}
#[derive(Clone, Copy, Debug)]
struct Pinch {
last: [V2; 2],
start_distance: f64,
start_vector: V2,
min_diameter: f64,
zooming: bool,
rotating: bool,
pitch: Option<bool>,
single_since: Option<f64>,
panning: bool,
tap: bool,
}
#[derive(Clone, Copy, Debug)]
enum Mode {
Idle,
Pressed {
long_pressed: bool,
},
Pan,
BoxZoom {
origin: V2,
},
Rotate,
Pinch(Pinch),
TapDrag {
origin: V2,
moved: bool,
long_pressed: bool,
},
Platform,
Ignored,
}
#[derive(Clone, Copy, Debug, Default)]
struct Step {
pan: V2,
zoom: f64,
bearing: f64,
pitch: f64,
around: Option<V2>,
}
#[derive(Clone, Copy, Debug, Default)]
struct Release {
pan: Option<V2>,
zoom: Option<f64>,
bearing: Option<f64>,
pitch: Option<f64>,
zoom_anchor: Option<V2>,
bearing_anchor: Option<V2>,
}
#[derive(Debug, Default)]
struct Tracking {
pan: V2,
zoom: f64,
bearing: f64,
pitch: f64,
trackers: [VelocityTracker; 5],
zoom_anchor: Option<V2>,
bearing_anchor: Option<V2>,
staged: Option<(f64, Release)>,
not_before: f64,
}
impl Tracking {
fn reset(&mut self, now: f64) {
*self = Self::default();
self.record(now, Step::default());
}
fn record(&mut self, now: f64, step: Step) {
self.pan += step.pan;
self.zoom += step.zoom;
self.bearing += step.bearing;
self.pitch += step.pitch;
if step.zoom != 0.0 {
self.zoom_anchor = step.around;
}
if step.bearing != 0.0 {
self.bearing_anchor = step.around;
}
if now < self.not_before {
return;
}
let values = [self.pan.x, self.pan.y, self.zoom, self.bearing, self.pitch];
let moved =
[step.pan != V2::ZERO, step.pan != V2::ZERO, step.zoom != 0.0, step.bearing != 0.0, step.pitch != 0.0];
let start = self.trackers.iter().all(VelocityTracker::is_empty);
for ((tracker, value), moved) in self.trackers.iter_mut().zip(values).zip(moved) {
if moved || start || !tracker.is_empty() {
tracker.add(now, value);
}
}
}
fn release(&mut self, now: f64) -> Release {
let [x, y, zoom, bearing, pitch] = self.trackers.each_ref().map(|t| t.velocity(now));
let some = |v: f64| (v != 0.0 && v.is_finite()).then_some(v);
let mut release = Release {
pan: ((x != 0.0 || y != 0.0) && x.is_finite() && y.is_finite()).then_some(V2::new(x, y)),
zoom: some(zoom),
bearing: some(bearing),
pitch: some(pitch),
zoom_anchor: self.zoom_anchor,
bearing_anchor: self.bearing_anchor,
};
if let Some((at, staged)) = self.staged.take()
&& now - at <= STAGED_RELEASE_MS
{
if release.pan.is_none() {
release.pan = staged.pan;
}
if release.zoom.is_none() {
release.zoom = staged.zoom;
release.zoom_anchor = staged.zoom_anchor;
}
if release.bearing.is_none() {
release.bearing = staged.bearing;
release.bearing_anchor = staged.bearing_anchor;
}
if release.pitch.is_none() {
release.pitch = staged.pitch;
}
}
release
}
fn stage(&mut self, now: f64) {
let release = self.release(now);
self.staged = Some((now, release));
for tracker in &mut self.trackers {
tracker.reset();
}
self.not_before = now + STAGED_RELEASE_MS;
}
}
impl Release {
fn into_momentum(self, caught_glide: Option<CameraVelocity>, options: &GestureOptions) -> CameraMomentum {
let caught = caught_glide.unwrap_or_default();
let pan = options.pan.momentum.and_then(|decay| {
let velocity = self.pan.filter(|v| v.length() >= decay.min_speed)?;
let caught = V2::from(caught.pan);
let velocity = if velocity.dot(caught) > 0.0 { velocity + caught } else { velocity };
Some(MomentumGlide { velocity: velocity.into(), decay })
});
let scalar = |part: &AxisOptions, release: Option<f64>, anchor: Option<V2>, caught: f64| AnchoredMomentum {
glide: part.momentum.and_then(|decay| {
let velocity = release.filter(|v| v.abs() >= decay.min_speed)?;
let velocity =
if caught != 0.0 && caught.signum() == velocity.signum() { velocity + caught } else { velocity };
Some(MomentumGlide { velocity, decay })
}),
anchor: anchor.map(ScreenCoordinate::from),
snap: part.snap,
};
CameraMomentum {
pan,
zoom: scalar(&options.zoom, self.zoom, self.zoom_anchor, caught.zoom),
bearing: scalar(&options.bearing, self.bearing, self.bearing_anchor, caught.bearing),
pitch: scalar(&options.pitch, self.pitch, None, caught.pitch),
}
}
}
#[derive(Clone, Copy, Debug)]
struct DeltaGesture {
anchor: V2,
idle_until: f64,
eases: [Option<StepEase>; DELTA_PARTS],
}
#[derive(Clone, Copy, Debug)]
struct StepEase {
start: f64,
distance: f64,
applied: f64,
x1: f64,
y1: f64,
}
impl StepEase {
fn retarget(ease: Option<StepEase>, step: f64, now: f64, duration: f64, limit: impl Fn(f64) -> f64) -> StepEase {
let (distance, (x1, y1)) = match ease {
Some(ease) if ease.progress(now, duration) < 1.0 => {
let distance = limit(ease.distance - ease.applied + step);
let slope = if distance == 0.0 { 0.0 } else { ease.speed(now, duration) * duration / distance };
let x1 = STEP_EASE_HANDLE / (1.0 + slope * slope).sqrt();
(distance, (x1, slope * x1))
}
Some(ease) => (limit(ease.distance - ease.applied + step), (EASE_X1, EASE_Y1)),
None => (limit(step), (EASE_X1, EASE_Y1)),
};
StepEase { start: now, distance, applied: 0.0, x1, y1 }
}
fn curve(&self) -> UnitBezier {
UnitBezier::new(self.x1, self.y1, EASE_X2, EASE_Y2)
}
fn progress(&self, now: f64, duration: f64) -> f64 {
((now - self.start) / duration).clamp(0.0, 1.0)
}
fn value(&self, now: f64, duration: f64) -> f64 {
let t = self.progress(now, duration);
self.distance * if t >= 1.0 { 1.0 } else { self.curve().solve(t, 1e-6) }
}
fn speed(&self, now: f64, duration: f64) -> f64 {
let dt = 1.0;
(self.value(now + dt, duration) - self.value(now, duration)) / dt
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum NextTick {
Frame,
At(f64),
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Finish {
Cancel,
Stop,
Settle,
Glide,
}
#[derive(Debug)]
pub struct GestureRecognizer<C> {
camera: C,
options: GestureOptions,
pointers: Vec<Pointer>,
mode: Mode,
tracking: Tracking,
begun: bool,
release_end: Option<PendingEnd>,
caught_glide: Option<PendingEnd>,
last_tap: Option<(V2, f64, PointerKind)>,
modes: Modes,
deltas: Option<DeltaGesture>,
notch_watch: Option<NotchWatch>,
events: Vec<GestureEvent>,
}
impl<C: GestureCamera> GestureRecognizer<C> {
pub fn new(camera: C) -> Self {
GestureRecognizer::with(camera, GestureOptions::default())
}
pub fn with_options(camera: C, options: GestureOptions) -> Result<Self, InvalidGestureOptions> {
options.validate()?;
Ok(GestureRecognizer::with(camera, options))
}
fn with(camera: C, options: GestureOptions) -> Self {
GestureRecognizer {
camera,
options,
pointers: Vec::new(),
mode: Mode::Idle,
tracking: Tracking::default(),
begun: false,
release_end: None,
caught_glide: None,
last_tap: None,
modes: Modes::default(),
deltas: None,
notch_watch: None,
events: Vec::new(),
}
}
#[must_use]
pub fn camera(&self) -> &C {
&self.camera
}
#[must_use]
pub fn options(&self) -> &GestureOptions {
&self.options
}
pub fn set_options(&mut self, options: GestureOptions) -> Result<(), InvalidGestureOptions> {
options.validate()?;
self.options = options;
Ok(())
}
#[cfg(test)]
#[must_use]
pub(crate) fn is_active(&self) -> bool {
!matches!(self.mode, Mode::Idle) || self.deltas.is_some()
}
#[must_use]
pub fn box_zoom(&self) -> Option<[ScreenCoordinate; 2]> {
match self.mode {
Mode::BoxZoom { origin } => Some([origin.into(), self.pointers.first()?.position.into()]),
_ => None,
}
}
#[must_use]
pub fn next_tick(&self) -> Option<NextTick> {
let easing = self.deltas.is_some_and(|deltas| deltas.eases.iter().any(Option::is_some));
let held_still = matches!(self.mode, Mode::Pressed { .. } | Mode::TapDrag { moved: false, .. });
if easing || (self.notch_watch.is_some() && !held_still) {
return Some(NextTick::Frame);
}
let long_press = match self.pointers[..] {
[pointer] if self.awaits_long_press() => Some(pointer.down_at + self.options.long_press_ms),
_ => None,
};
let idle = self.deltas.map(|deltas| deltas.idle_until);
long_press.into_iter().chain(idle).min_by(f64::total_cmp).map(NextTick::At)
}
fn awaits_long_press(&self) -> bool {
matches!(
self.mode,
Mode::Pressed { long_pressed: false } | Mode::TapDrag { moved: false, long_pressed: false, .. }
)
}
pub fn take_events(&mut self) -> Vec<GestureEvent> {
std::mem::take(&mut self.events)
}
pub fn step_to(&mut self, update: &CameraUpdate) {
let set = &update.camera;
let center = set.center.is_some();
self.stop_step_eases([center, center, set.zoom.is_some(), set.bearing.is_some(), set.pitch.is_some()]);
if !self.begun {
self.notch_watch = None;
}
let phase = if self.begun { GesturePhase::Update } else { GesturePhase::End };
self.camera.update_camera(&CameraUpdate { phase, ..*update });
}
pub fn tick(&mut self, now: f64) -> Option<NextTick> {
self.long_press(now);
self.delta_frame(now);
if let Some(watch) = &mut self.notch_watch {
watch.sample(&self.camera.camera(), &self.options, now, &mut self.events);
if !self.begun && self.release_end.as_ref().is_none_or(PendingEnd::has_ended) {
self.notch_watch = None;
}
}
self.next_tick()
}
fn long_press(&mut self, now: f64) {
if self.awaits_long_press()
&& let [pointer] = self.pointers[..]
&& now - pointer.down_at >= self.options.long_press_ms
{
match &mut self.mode {
Mode::Pressed { long_pressed } | Mode::TapDrag { long_pressed, .. } => *long_pressed = true,
_ => {}
}
self.last_tap = None;
self.events.push(GestureEvent::LongPress { id: pointer.id, position: pointer.position.into() });
}
}
fn delta_frame(&mut self, now: f64) -> bool {
let Some(mut gesture) = self.deltas else {
return false;
};
let duration = self.options.step_duration_ms;
let max_zoom = self.options.max_step_zoom;
let mut amounts = [0.0; DELTA_PARTS];
for (part, slot) in gesture.eases.iter_mut().enumerate() {
let Some(mut ease) = *slot else { continue };
let mut amount = ease.value(now, duration) - ease.applied;
if part == DELTA_ZOOM {
amount = amount.clamp(-max_zoom, max_zoom);
}
ease.applied += amount;
amounts[part] = amount;
let done = ease.progress(now, duration) >= 1.0 && (ease.distance - ease.applied).abs() <= STEP_EASE_DONE;
*slot = (!done).then_some(ease);
if done {
gesture.idle_until = gesture.idle_until.max(now + self.options.delta_idle_ms);
}
}
if !self.begun && amounts.iter().any(|amount| *amount != 0.0) {
self.begun = true;
self.camera.apply_camera_delta(&CameraDelta { phase: GesturePhase::Update, ..CameraDelta::default() });
}
self.move_by(amounts, gesture.anchor);
if gesture.eases.iter().all(Option::is_none) && now >= gesture.idle_until {
self.deltas = None;
if matches!(self.mode, Mode::Idle) {
self.end_phase(None);
}
return false;
}
self.deltas = Some(gesture);
true
}
pub fn handle(&mut self, input: Input, now: f64) -> bool {
match input {
Input::PointerDown { id, kind, position, modes } => {
let position = position.into();
self.down(Pointer { id, kind, position, down: position, down_at: now, modes })
}
Input::PointerMove { id, position } => self.moved(id, position.into(), now),
Input::PointerUp { id, position } => self.up(id, Some(position.into()), now),
Input::PointerCancel { id } => self.up(id, None, now),
Input::Modes(modes) => {
self.modes = modes;
false
}
Input::Delta { position, discrete, pan, zoom, bearing, pitch } => {
self.delta(position.into(), discrete, [pan.x, pan.y, zoom, bearing, pitch], now)
}
Input::Pinch { center, scale, rotation, phase } => {
self.platform_pinch(center.into(), scale, rotation, phase, now)
}
}
}
fn animation(&self, duration: Duration, easing: UnitBezier) -> AnimationOptions {
AnimationOptions {
duration: Some(duration),
easing: Some(easing),
transition_id: Some(self.options.transition_id),
..AnimationOptions::default()
}
}
fn begin(&mut self) {
if self.begun {
return;
}
self.begun = true;
self.caught_glide = self.release_end.take().filter(|end| end.take().is_none());
let notches = [&self.options.zoom, &self.options.bearing, &self.options.pitch]
.iter()
.any(|part| part.enabled && !part.notches.is_empty());
self.notch_watch = notches.then(|| NotchWatch::new(&self.camera.camera(), &self.options));
let phase = if self.options.interrupt_animations {
GesturePhase::Begin
} else {
self.camera.cancel_transitions_with_id(self.options.transition_id);
GesturePhase::Update
};
self.camera.apply_camera_delta(&CameraDelta { phase, ..CameraDelta::default() });
}
fn update(&mut self, delta: &CameraDelta) {
self.begin();
self.camera.apply_camera_delta(&CameraDelta { phase: GesturePhase::Update, ..*delta });
}
fn end_phase(&mut self, momentum: Option<&CameraMomentum>) {
if !std::mem::replace(&mut self.begun, false) {
return;
}
let animation =
AnimationOptions { transition_id: Some(self.options.transition_id), ..AnimationOptions::default() };
let phase = GesturePhase::End;
let delta = CameraDelta { momentum: momentum.copied(), animation, phase, ..CameraDelta::default() };
let end = self.camera.release(&delta);
if momentum.is_some() {
self.release_end = Some(end);
}
}
fn cancel_phase(&mut self) {
self.notch_watch = None;
if !std::mem::replace(&mut self.begun, false) {
return;
}
let phase = if self.options.interrupt_animations {
GesturePhase::Cancel
} else {
self.camera.cancel_transitions_with_id(self.options.transition_id);
GesturePhase::End
};
self.camera.apply_camera_delta(&CameraDelta { phase, ..CameraDelta::default() });
}
fn stop_step_eases(&mut self, parts: [bool; DELTA_PARTS]) {
if let Some(gesture) = self.deltas.as_mut() {
for (ease, stop) in gesture.eases.iter_mut().zip(parts) {
if stop {
*ease = None;
}
}
}
}
fn down(&mut self, pointer: Pointer) -> bool {
let Pointer { id, kind, position, down_at: now, .. } = pointer;
if self.pointers.iter().any(|p| p.id == id) {
return false;
}
let touch = kind == PointerKind::Touch;
if !touch && !self.pointers.is_empty() {
return false;
}
if matches!(self.mode, Mode::Platform) {
self.finish(now, Finish::Cancel);
}
self.pointers.push(pointer);
self.deltas = None;
self.begin();
match (self.mode, self.pointers.len()) {
(Mode::Idle, 1) => {
self.mode = if touch && self.follows_tap(kind, position, now) {
Mode::TapDrag { origin: position, moved: false, long_pressed: false }
} else {
Mode::Pressed { long_pressed: false }
};
self.tracking.reset(now);
}
(Mode::Pressed { .. } | Mode::Pan | Mode::TapDrag { .. }, 2) if touch => {
let [a, b] = [self.pointers[0].position, self.pointers[1].position];
let vector = b - a;
self.mode = Mode::Pinch(Pinch {
last: [a, b],
start_distance: vector.length(),
start_vector: vector,
min_diameter: vector.length(),
zooming: false,
rotating: false,
pitch: (!self.options.pitch.enabled || vector.y.abs() > vector.x.abs()).then_some(false),
single_since: None,
panning: false,
tap: now - self.pointers[0].down_at < self.options.tap_interval_ms
&& matches!(self.mode, Mode::Pressed { .. }),
});
self.tracking.record(now, Step::default());
}
_ => self.mode = Mode::Ignored,
}
true
}
fn moved(&mut self, id: u32, position: V2, now: f64) -> bool {
let Some(index) = self.pointers.iter().position(|p| p.id == id) else {
return false;
};
let last = self.pointers[index].position;
self.pointers[index].position = position;
match &mut self.mode {
Mode::Pressed { .. } => {
let pointer = self.pointers[index];
let down = pointer.down;
if position.distance(down) > self.options.click_tolerance {
let modes = pointer.modes | self.modes;
self.mode = if modes.rotate {
Mode::Rotate
} else if modes.box_zoom && self.options.zoom.enabled {
Mode::BoxZoom { origin: down }
} else {
Mode::Pan
};
self.drag_step(down, position, now);
}
}
Mode::Pan | Mode::Rotate => self.drag_step(last, position, now),
Mode::TapDrag { origin, moved, .. } => {
let (origin, dy) = (*origin, position.y - last.y);
*moved = *moved || position.distance(origin) > self.options.click_tolerance;
if *moved && self.options.zoom.enabled {
let zoom = dy * self.options.tap_drag_zoom;
let delta =
CameraDelta { scale: Some(zoom.exp2()), anchor: Some(origin.into()), ..CameraDelta::default() };
self.update(&delta);
self.tracking.record(now, Step { zoom, around: Some(origin), ..Step::default() });
}
}
Mode::Pinch(_) if self.pointers.len() < 2 => {
self.mode = Mode::Pan;
self.drag_step(last, position, now);
}
Mode::Pinch(pinch) => {
if index > 1 {
return true;
}
let mut pinch = *pinch;
let points = [self.pointers[0].position, self.pointers[1].position];
self.pinch_step(&mut pinch, points, now);
self.mode = Mode::Pinch(pinch);
}
Mode::BoxZoom { .. } | Mode::Idle | Mode::Ignored | Mode::Platform => {}
}
true
}
fn drag_step(&mut self, from: V2, to: V2, now: f64) {
let delta = to - from;
match self.mode {
Mode::Rotate => {
let bearing = if self.options.bearing.enabled { delta.x * self.options.drag_rotate } else { 0.0 };
let pitch = if self.options.pitch.enabled { -delta.y * self.options.drag_pitch } else { 0.0 };
let step = CameraDelta { bearing: Some(bearing), pitch: Some(pitch), ..CameraDelta::default() };
self.update(&step);
self.tracking.record(now, Step { bearing, pitch, ..Step::default() });
}
Mode::BoxZoom { .. } => {}
_ if !self.options.pan.enabled => {}
_ => {
let step =
CameraDelta { offset: Some(delta.into()), anchor: Some(to.into()), ..CameraDelta::default() };
self.update(&step);
self.tracking.record(now, Step { pan: delta, ..Step::default() });
}
}
}
fn pinch_step(&mut self, pinch: &mut Pinch, points: [V2; 2], now: f64) {
let [a, b] = points;
let [la, lb] = pinch.last;
let (va, vb) = (a - la, b - lb);
let vector = b - a;
let tap_tolerance = self.options.tap_tolerance;
if pinch.tap
&& (a.distance(self.pointers[0].down) > tap_tolerance || b.distance(self.pointers[1].down) > tap_tolerance)
{
pinch.tap = false;
}
if pinch.pitch.is_none() {
let (moved_a, moved_b) = (va.length() >= PITCH_DECIDE_PX, vb.length() >= PITCH_DECIDE_PX);
if moved_a && moved_b {
let vertical = |v: V2| v.y.abs() > v.x.abs();
pinch.pitch = Some(vertical(va) && vertical(vb) && (va.y > 0.0) == (vb.y > 0.0));
} else if moved_a || moved_b {
let since = *pinch.single_since.get_or_insert(now);
if now - since >= PITCH_DECIDE_MS {
pinch.pitch = Some(false);
}
}
if pinch.pitch.is_none() {
return;
}
}
if pinch.pitch == Some(true) {
let pitch = f64::midpoint(va.y, vb.y) * -self.options.drag_pitch;
self.update(&CameraDelta { pitch: Some(pitch), ..CameraDelta::default() });
pinch.last = points;
pinch.tap = false;
self.tracking.record(now, Step { pitch, ..Step::default() });
return;
}
let (mid, last_mid) = ((a + b) / 2.0, (la + lb) / 2.0);
let distance = vector.length();
let last_distance = (lb - la).length();
pinch.min_diameter = pinch.min_diameter.min(distance);
if !pinch.zooming
&& self.options.zoom.enabled
&& (distance / pinch.start_distance).log2().abs() >= self.options.pinch_zoom_threshold
{
pinch.zooming = true;
}
if !pinch.rotating && self.options.bearing.enabled {
let threshold = self.options.rotate_threshold / (std::f64::consts::PI * pinch.min_diameter) * 360.0;
if angle(pinch.start_vector, vector).abs() >= threshold {
pinch.rotating = true;
}
}
if !pinch.panning
&& self.options.pan.enabled
&& (mid - (self.pointers[0].down + self.pointers[1].down) / 2.0).length() > self.options.click_tolerance
{
pinch.panning = true;
}
let pan = if pinch.panning { mid - last_mid } else { V2::ZERO };
let zoom = if pinch.zooming && last_distance > 0.0 { (distance / last_distance).log2() } else { 0.0 };
let bearing = if pinch.rotating { -angle(lb - la, vector) } else { 0.0 };
pinch.last = points;
if pan == V2::ZERO && zoom == 0.0 && bearing == 0.0 {
return;
}
let anchored = zoom != 0.0 || bearing != 0.0 || pan != V2::ZERO;
self.update(&CameraDelta {
offset: (pan != V2::ZERO).then_some(pan.into()),
scale: (zoom != 0.0).then_some(zoom.exp2()),
bearing: (bearing != 0.0).then_some(bearing),
anchor: anchored.then_some(mid.into()),
..CameraDelta::default()
});
pinch.tap = false;
self.tracking.record(now, Step { pan, zoom, bearing, around: Some(mid), ..Step::default() });
}
fn up(&mut self, id: u32, position: Option<V2>, now: f64) -> bool {
let Some(index) = self.pointers.iter().position(|p| p.id == id) else {
return false;
};
let pointer = self.pointers.remove(index);
let cancelled = position.is_none();
if cancelled && !self.pointers.is_empty() {
self.finish(now, Finish::Cancel);
self.mode = Mode::Ignored;
return true;
}
let unless_cancelled = |finish: Finish| if cancelled { Finish::Cancel } else { finish };
match self.mode {
Mode::Pinch(pinch) => {
if !self.pointers.is_empty() {
let tap = pinch.tap && now - pointer.down_at < self.options.tap_interval_ms;
self.mode = if tap {
Mode::Pinch(pinch)
} else {
self.tracking.stage(now);
Mode::Pan
};
return true;
}
self.finish(now, unless_cancelled(if pinch.tap { Finish::Settle } else { Finish::Glide }));
if pinch.tap && !cancelled {
let mid = (pinch.last[0] + pinch.last[1]) / 2.0;
self.events.push(GestureEvent::TwoFingerTap { position: mid.into() });
self.tap_zoom(mid, self.options.two_finger_tap_zoom, now);
}
}
Mode::Pan if !self.pointers.is_empty() => {}
Mode::Pressed { long_pressed } => {
if self.pointers.is_empty() {
self.finish(now, unless_cancelled(Finish::Settle));
if !cancelled && !long_pressed && now - pointer.down_at >= self.options.long_press_ms {
let position = pointer.position.into();
self.events.push(GestureEvent::LongPress { id: pointer.id, position });
self.last_tap = None;
} else if !cancelled && !long_pressed {
self.tap(pointer, now);
}
}
}
Mode::BoxZoom { origin } => {
self.finish(now, unless_cancelled(Finish::Stop));
if !cancelled {
self.zoom_to_box(origin, position.unwrap_or(pointer.position));
}
}
Mode::TapDrag { origin, moved, long_pressed } => {
if self.pointers.is_empty() {
self.last_tap = None;
self.finish(now, unless_cancelled(if moved { Finish::Glide } else { Finish::Settle }));
if !moved && !cancelled && !long_pressed {
let (id, position) = (pointer.id, origin.into());
if now - pointer.down_at >= self.options.long_press_ms {
self.events.push(GestureEvent::LongPress { id, position });
} else {
self.events
.extend([GestureEvent::Tap { id, position }, GestureEvent::DoubleTap { id, position }]);
self.double_tap_zoom(pointer, origin, now);
}
}
}
}
Mode::Pan | Mode::Rotate => {
self.finish(now, unless_cancelled(Finish::Glide));
}
Mode::Ignored | Mode::Idle | Mode::Platform => {
if self.pointers.is_empty() {
self.finish(now, unless_cancelled(Finish::Stop));
}
}
}
true
}
fn tap(&mut self, pointer: Pointer, now: f64) {
let (id, position) = (pointer.id, pointer.position);
self.events.push(GestureEvent::Tap { id, position: position.into() });
if self.follows_tap(pointer.kind, position, now) {
self.last_tap = None;
self.events.push(GestureEvent::DoubleTap { id, position: position.into() });
self.double_tap_zoom(pointer, position, now);
} else {
self.last_tap = Some((position, now, pointer.kind));
}
}
fn double_tap_zoom(&mut self, pointer: Pointer, at: V2, now: f64) {
let levels = self.options.double_tap_zoom;
let out = (pointer.modes | self.modes).zoom_out;
self.tap_zoom(at, if out { -levels } else { levels }, now);
}
fn tap_zoom(&mut self, at: V2, levels: f64, now: f64) {
if levels != 0.0 {
self.delta(at, true, [0.0, 0.0, levels, 0.0, 0.0], now);
}
}
fn finish(&mut self, now: f64, finish: Finish) {
self.mode = Mode::Idle;
let caught_glide = self
.caught_glide
.take()
.and_then(|end| end.take())
.filter(|end| self.options.carry_caught_glide && end.outcome == TransitionOutcome::Cancelled)
.map(|end| end.velocity);
match finish {
Finish::Cancel => {
self.deltas = None;
self.cancel_phase();
}
Finish::Stop => self.end_phase(None),
Finish::Settle | Finish::Glide => {
let release = if finish == Finish::Glide { self.tracking.release(now) } else { Release::default() };
let momentum = release.into_momentum(caught_glide, &self.options);
self.end_phase(Some(&momentum));
}
}
}
fn follows_tap(&self, kind: PointerKind, position: V2, now: f64) -> bool {
self.last_tap.is_some_and(|(at, time, k)| {
k == kind && now - time < self.options.tap_interval_ms && at.distance(position) < self.options.tap_tolerance
})
}
fn zoom_to_box(&mut self, a: V2, b: V2) {
let size = (b - a).abs();
if size.x < 1.0 || size.y < 1.0 {
return;
}
let Some(center) = self.camera.lat_lng_for_pixel(((a + b) / 2.0).into()) else {
return;
};
let viewport = V2::from(self.camera.viewport());
let zoom = (viewport.x / size.x).min(viewport.y / size.y).log2();
let target = clamp_zoom(&self.camera, zoom_of(&self.camera) + zoom);
let update = CameraUpdate {
mode: CameraUpdateMode::Ease,
camera: CameraOptions { center: Some(center), zoom: Some(target), ..Default::default() },
animation: self.animation(BOX_EASE, EASE),
phase: GesturePhase::None,
};
self.step_to(&update);
}
fn delta(&mut self, position: V2, discrete: bool, mut amounts: [f64; DELTA_PARTS], now: f64) -> bool {
let options = &self.options;
let on = [
options.pan.enabled,
options.pan.enabled,
options.zoom.enabled,
options.bearing.enabled,
options.pitch.enabled,
];
for (amount, on) in amounts.iter_mut().zip(on) {
if !on || !amount.is_finite() {
*amount = 0.0;
}
}
if amounts.iter().all(|amount| *amount == 0.0) {
return false;
}
let (duration, max_zoom) = (options.step_duration_ms, options.max_step_zoom);
let mut gesture =
self.deltas.unwrap_or(DeltaGesture { anchor: position, idle_until: now, eases: [None; DELTA_PARTS] });
gesture.anchor = position;
gesture.idle_until = now + options.delta_idle_ms;
self.begin();
if discrete {
for (part, amount) in amounts.into_iter().enumerate() {
if amount == 0.0 {
continue;
}
let ease = gesture.eases[part];
gesture.eases[part] = Some(if part == DELTA_ZOOM {
StepEase::retarget(ease, amount, now, duration, |levels| within_zoom_range(&self.camera, levels))
} else {
StepEase::retarget(ease, amount, now, duration, |amount| amount)
});
}
} else {
amounts[DELTA_ZOOM] = amounts[DELTA_ZOOM].clamp(-max_zoom, max_zoom);
self.move_by(amounts, position);
}
self.deltas = Some(gesture);
true
}
fn move_by(&mut self, amounts: [f64; DELTA_PARTS], at: V2) {
let [x, y, zoom, bearing, pitch] = amounts;
if amounts.iter().all(|amount| *amount == 0.0) {
return;
}
let delta = CameraDelta {
offset: (x != 0.0 || y != 0.0).then(|| ScreenCoordinate::new(x, y)),
scale: (zoom != 0.0).then(|| zoom.exp2()),
bearing: (bearing != 0.0).then_some(bearing),
pitch: (pitch != 0.0).then_some(pitch),
anchor: Some(at.into()),
..CameraDelta::default()
};
self.update(&delta);
}
fn platform_pinch(&mut self, center: V2, scale: f64, rotation: f64, phase: PinchPhase, now: f64) -> bool {
let pinching = matches!(self.mode, Mode::Platform);
match phase {
PinchPhase::End | PinchPhase::Cancel => {
if !pinching {
return false;
}
let finish = if phase == PinchPhase::End { Finish::Glide } else { Finish::Cancel };
self.finish(now, finish);
return true;
}
PinchPhase::Begin | PinchPhase::Update if !pinching => {
if !matches!(self.mode, Mode::Idle) {
return false;
}
self.deltas = None;
self.begin();
self.tracking.reset(now);
self.mode = Mode::Platform;
}
PinchPhase::Begin | PinchPhase::Update => {}
}
let zoom = if scale > 0.0 && scale.is_finite() && self.options.zoom.enabled { scale.log2() } else { 0.0 };
let bearing = if rotation.is_finite() && self.options.bearing.enabled { -rotation } else { 0.0 };
if zoom == 0.0 && bearing == 0.0 {
return true;
}
self.update(&CameraDelta {
scale: (zoom != 0.0).then_some(scale),
bearing: (bearing != 0.0).then_some(bearing),
anchor: Some(center.into()),
..CameraDelta::default()
});
self.tracking.record(now, Step { zoom, bearing, around: Some(center), ..Step::default() });
true
}
}
fn zoom_of(camera: &impl GestureCamera) -> f64 {
camera.camera().zoom.unwrap_or(0.0)
}
fn clamp_zoom(camera: &impl GestureCamera, zoom: f64) -> f64 {
let (min, max) = camera.zoom_range();
zoom.max(min).min(max)
}
fn within_zoom_range(camera: &impl GestureCamera, levels: f64) -> f64 {
let zoom = zoom_of(camera);
let (min, max) = camera.zoom_range();
levels.clamp((min - zoom).min(0.0), (max - zoom).max(0.0))
}
fn angle(a: V2, b: V2) -> f64 {
(a.x * b.y - a.y * b.x).atan2(a.dot(b)).to_degrees()
}
#[cfg(test)]
#[path = "recognizer_test.rs"]
mod recognizer_test;