Skip to main content

mln_engine/gestures/
camera.rs

1//! The camera the recognizer moves: `mln_runtime::Map`, and in this crate's
2//! tests a bare transform.
3
4use std::sync::Arc;
5
6use mln_style::geo::LatLng;
7use parking_lot::Mutex;
8
9use crate::camera::{CameraDelta, CameraOptions, CameraUpdate, TransformState, TransitionEnd};
10use crate::geo::ScreenCoordinate;
11
12/// What the recognizer needs from a camera: to read it, and
13/// the map's two camera calls, which carry each change's gesture
14/// phase. The calls may apply later, as a runtime's commands do: the
15/// recognizer reads what the camera shows by then. It is the recognizer's
16/// only tie to the map.
17pub trait GestureCamera {
18    /// The viewport's width and height, in logical pixels.
19    fn viewport(&self) -> ScreenCoordinate;
20    fn camera(&self) -> CameraOptions;
21    /// The least and greatest zoom the camera may take at its center (see
22    /// [`zoom_range`]).
23    fn zoom_range(&self) -> (f64, f64);
24    fn lat_lng_for_pixel(&self, pixel: ScreenCoordinate) -> Option<LatLng>;
25    /// Applies a delta as [`Map::apply_camera_delta`](crate::map::Map::apply_camera_delta)
26    /// does. The recognizer only sends valid deltas.
27    fn apply_camera_delta(&mut self, delta: &CameraDelta);
28    /// Applies a gesture's last delta, as `apply_camera_delta` does, and
29    /// returns how its command ends: the end of the momentum it starts,
30    /// whose speed a press that stops it can carry.
31    fn release(&mut self, delta: &CameraDelta) -> PendingEnd;
32    fn update_camera(&mut self, update: &CameraUpdate);
33    fn cancel_transitions_with_id(&mut self, transition_id: u64);
34}
35
36/// Where a camera command's end arrives: the camera keeps a clone and sets
37/// it when the command ends, and the recognizer takes it. A command that
38/// fails leaves it empty.
39#[derive(Clone, Debug, Default)]
40pub struct PendingEnd(Arc<Mutex<Option<TransitionEnd>>>);
41
42impl PendingEnd {
43    pub fn set(&self, end: TransitionEnd) {
44        *self.0.lock() = Some(end);
45    }
46
47    /// The command's end, once it has come; `None` before.
48    #[must_use]
49    pub(crate) fn take(&self) -> Option<TransitionEnd> {
50        self.0.lock().take()
51    }
52
53    /// Whether the command's end has come and not been taken.
54    #[must_use]
55    pub(crate) fn has_ended(&self) -> bool {
56        self.0.lock().is_some()
57    }
58}
59
60/// The least and greatest zoom that `state` may take at `latitude`: on the
61/// globe the least falls toward the poles, and `ConstrainMode::Screen` bounds
62/// raise it to the zoom they fill the screen at.
63#[must_use]
64pub fn zoom_range(state: &TransformState, latitude: f64) -> (f64, f64) {
65    let min = state.min_zoom_at_latitude(latitude);
66    let min = state.min_zoom_for_screen_bounds().map_or(min, |bounds| min.max(bounds));
67    let max = state.limits().max_zoom;
68    // Bounds too small to fill the screen at the greatest zoom hold the
69    // camera there.
70    (min.min(max), max)
71}