Skip to main content

mln_runtime/
gestures.rs

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