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
26        .map_err(
27            |error| tracing::warn!(target: log_target::GENERAL, error = %error, "A gesture's camera command failed"),
28        )
29        .ok()
30}
31
32impl GestureCamera for Map {
33    fn viewport(&self) -> ScreenCoordinate {
34        let size = self.gesture_snapshot().map(|snapshot| snapshot.view.extent.size).unwrap_or_default();
35        ScreenCoordinate::new(f64::from(size.width), f64::from(size.height))
36    }
37
38    fn camera(&self) -> CameraOptions {
39        self.gesture_snapshot().map(|snapshot| CameraOptions::from(snapshot.view.camera)).unwrap_or_default()
40    }
41
42    fn zoom_range(&self) -> (f64, f64) {
43        let Some(snapshot) = self.gesture_snapshot() else { return (0.0, 0.0) };
44        let Ok(mut state) = snapshot.view.to_state() else {
45            return (snapshot.bounds.min_zoom, snapshot.bounds.max_zoom);
46        };
47        let bounds = snapshot.bounds;
48        let limits = mln_engine::camera::CameraLimits {
49            bounds: bounds.bounds.unwrap_or_else(LatLngBounds::infinite),
50            min_zoom: bounds.min_zoom,
51            max_zoom: bounds.max_zoom,
52            ..*state.limits()
53        };
54        if state.set_limits(limits).is_err() {
55            return (bounds.min_zoom, bounds.max_zoom);
56        }
57        mln_engine::gestures::zoom_range(&state, snapshot.view.camera.center.latitude())
58    }
59
60    /// On the ground the map draws, so that a box zoom over terrain centers
61    /// on the ground it boxed, and in the projection it draws in.
62    fn lat_lng_for_pixel(&self, pixel: ScreenCoordinate) -> Option<LatLng> {
63        self.projection(None).ok()?.lat_lng_for_pixel(&pixel, WrapMode::Unwrapped).ok()
64    }
65
66    fn apply_camera_delta(&mut self, delta: &CameraDelta) {
67        sent(Map::apply_camera_delta(self, delta.clone()));
68    }
69
70    fn release(&mut self, delta: &CameraDelta) -> PendingEnd {
71        let end = PendingEnd::default();
72        let ended = end.clone();
73        sent(self.apply_camera_delta_then(delta.clone(), move |result| {
74            if let Ok(result) = result {
75                ended.set(result);
76            }
77        }));
78        end
79    }
80
81    fn update_camera(&mut self, update: &CameraUpdate) {
82        sent(Map::update_camera(self, update.clone()));
83    }
84
85    fn cancel_transitions_with_id(&mut self, transition_id: u64) {
86        sent(self.cancel_camera_transitions_with_id(transition_id));
87    }
88}