Skip to main content

mln_runtime/
gestures.rs

1//! A map as the camera that `mln_gestures`' recognizer moves: it reads the
2//! map's published snapshot and ground, and sends camera commands, without
3//! waiting for them.
4
5use mln_gestures::{GestureCamera, PendingEnd};
6use mln_map::{CameraDelta, CameraOptions, CameraUpdate, MapSnapshot};
7use mln_util::LatLng;
8use mln_util::geo::{LatLngBounds, ScreenCoordinate, WrapMode};
9
10use crate::log_target;
11use crate::map::Map;
12
13impl Map {
14    /// The map's snapshot for the recognizer, which reads nothing of a map
15    /// that is gone.
16    fn gesture_snapshot(&self) -> Option<MapSnapshot> {
17        self.snapshot().ok()
18    }
19}
20
21/// Logs a command that a released map refused, which the recognizer can't
22/// act on.
23fn sent<T>(result: Result<T, mln_util::Error>) -> Option<T> {
24    result.map_err(|error| log::warn!(target: log_target::GENERAL, "gesture camera command: {error}")).ok()
25}
26
27impl GestureCamera for Map {
28    fn viewport(&self) -> ScreenCoordinate {
29        let size = self.gesture_snapshot().map(|snapshot| snapshot.view.extent.size).unwrap_or_default();
30        ScreenCoordinate::new(f64::from(size.width), f64::from(size.height))
31    }
32
33    fn camera(&self) -> CameraOptions {
34        self.gesture_snapshot().map(|snapshot| CameraOptions::from(snapshot.view.camera)).unwrap_or_default()
35    }
36
37    fn zoom_range(&self) -> (f64, f64) {
38        let Some(snapshot) = self.gesture_snapshot() else { return (0.0, 0.0) };
39        let Ok(mut state) = snapshot.view.to_state() else {
40            return (snapshot.bounds.min_zoom, snapshot.bounds.max_zoom);
41        };
42        let bounds = snapshot.bounds;
43        let limits = mln_map::CameraLimits {
44            bounds: bounds.bounds.unwrap_or_else(LatLngBounds::infinite),
45            min_zoom: bounds.min_zoom,
46            max_zoom: bounds.max_zoom,
47            ..*state.limits()
48        };
49        if state.set_limits(limits).is_err() {
50            return (bounds.min_zoom, bounds.max_zoom);
51        }
52        mln_gestures::zoom_range(&state, snapshot.view.camera.center.latitude())
53    }
54
55    /// On the ground the map draws, so that a box zoom over terrain centers
56    /// on the ground it boxed.
57    fn lat_lng_for_pixel(&self, pixel: ScreenCoordinate) -> Option<LatLng> {
58        self.projection(None).ok()?.lat_lng_for_pixel(&pixel, WrapMode::Unwrapped).ok()
59    }
60
61    fn apply_camera_delta(&mut self, delta: &CameraDelta) {
62        sent(Map::apply_camera_delta(self, delta.clone()));
63    }
64
65    fn release(&mut self, delta: &CameraDelta) -> PendingEnd {
66        let end = PendingEnd::default();
67        let ended = end.clone();
68        sent(self.apply_camera_delta_then(delta.clone(), move |result| {
69            if let Ok(result) = result {
70                ended.set(result);
71            }
72        }));
73        end
74    }
75
76    fn update_camera(&mut self, update: &CameraUpdate) {
77        sent(Map::update_camera(self, update.clone()));
78    }
79
80    fn cancel_transitions_with_id(&mut self, transition_id: u64) {
81        sent(self.cancel_camera_transitions_with_id(transition_id));
82    }
83}