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 sends camera commands, without waiting for
3//! them.
4
5use mln_gestures::{GestureCamera, PendingEnd};
6use mln_map::{CameraDelta, CameraOptions, CameraUpdate, MapProjection, MapSnapshot};
7use mln_util::LatLng;
8use mln_util::geo::{LatLngBounds, ScreenCoordinate, WrapMode};
9use mln_util::logging::{Event, Log};
10
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::warning(Event::General, format!("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        state.set_lat_lng_bounds(bounds.bounds.unwrap_or_else(LatLngBounds::infinite));
44        state.set_min_zoom(bounds.min_zoom);
45        state.set_max_zoom(bounds.max_zoom);
46        mln_gestures::zoom_range(&state, snapshot.view.camera.center.latitude())
47    }
48
49    fn lat_lng_for_pixel(&self, pixel: ScreenCoordinate) -> Option<LatLng> {
50        let projection = MapProjection::from_view(&self.gesture_snapshot()?.view).ok()?;
51        projection.lat_lng_for_pixel(&pixel, WrapMode::Unwrapped).ok()
52    }
53
54    fn apply_camera_delta(&mut self, delta: &CameraDelta) {
55        sent(Map::apply_camera_delta(self, delta.clone()));
56    }
57
58    fn release(&mut self, delta: &CameraDelta) -> PendingEnd {
59        let end = PendingEnd::default();
60        let ended = end.clone();
61        sent(self.apply_camera_delta_then(delta.clone(), move |result| {
62            if let Ok(result) = result {
63                ended.set(result);
64            }
65        }));
66        end
67    }
68
69    fn update_camera(&mut self, update: &CameraUpdate) {
70        sent(Map::update_camera(self, update.clone()));
71    }
72
73    fn cancel_transitions_with_id(&mut self, transition_id: u64) {
74        sent(self.cancel_camera_transitions_with_id(transition_id));
75    }
76}