Skip to main content

mln_map/
map_snapshot.rs

1//! A copy of a map's state, which [`Map::snapshot`](crate::Map::snapshot)
2//! takes.
3
4use glam::{DQuat, DVec3};
5use mln_map_types::{BoundOptions, FreeCameraOptions, MapView, TileLodMode};
6use mln_util::geo::LatLngBounds;
7
8/// A map's state at one moment.
9#[derive(Clone, Copy, Debug, PartialEq)]
10pub struct MapSnapshot {
11    /// Grows with each change to the map's state. Two snapshots with the same
12    /// generation show the same state.
13    pub generation: u64,
14    /// Counts the updates that the map has sent to its renderer. A frame shows
15    /// the map's state as of this snapshot once it renders this update.
16    pub update_generation: u64,
17    /// Whether the style and every resource that the view needs have loaded,
18    /// and the renderer has drawn them.
19    pub fully_loaded: bool,
20    pub gesture_in_progress: bool,
21    /// Whether any camera command is still animating.
22    pub transition_in_progress: bool,
23    pub tile: TileSettings,
24    pub bounds: CameraBounds,
25    pub free_camera: FreeCamera,
26    pub view: MapView,
27    /// Whether the map draws as a globe, alone or blending into Mercator.
28    pub globe_rendering: bool,
29}
30
31/// How a map picks the tiles to load.
32#[derive(Clone, Copy, Debug, PartialEq)]
33pub struct TileSettings {
34    /// How many zoom levels lower the map also loads tiles for, so it can show
35    /// a coarse map while the current tiles load.
36    pub prefetch_zoom_delta: u8,
37    /// The radius around the view point, in tiles, that keeps full detail.
38    pub lod_min_radius: f64,
39    /// Scales the distance to the view point. Above 1 lowers the detail.
40    pub lod_scale: f64,
41    /// The pitch, in degrees, above which the map lowers the detail.
42    pub lod_pitch_threshold: f64,
43    /// Zoom levels to add for picking the detail. Below 0 loads fewer tiles.
44    pub lod_zoom_shift: f64,
45    pub lod_mode: TileLodMode,
46}
47
48/// All the limits on a map's camera.
49#[derive(Clone, Copy, Debug, PartialEq)]
50pub struct CameraBounds {
51    /// The area that the camera's center stays within, or `None` when no area
52    /// limits it.
53    pub bounds: Option<LatLngBounds>,
54    pub min_zoom: f64,
55    pub max_zoom: f64,
56    /// The lowest pitch, in degrees.
57    pub min_pitch: f64,
58    /// The highest pitch, in degrees.
59    pub max_pitch: f64,
60}
61
62impl From<CameraBounds> for BoundOptions {
63    /// Options that set every limit.
64    fn from(bounds: CameraBounds) -> Self {
65        BoundOptions {
66            bounds: Some(bounds.bounds.unwrap_or_else(LatLngBounds::infinite)),
67            min_zoom: Some(bounds.min_zoom),
68            max_zoom: Some(bounds.max_zoom),
69            min_pitch: Some(bounds.min_pitch),
70            max_pitch: Some(bounds.max_pitch),
71        }
72    }
73}
74
75/// A camera's whole position and orientation in three dimensions.
76#[derive(Clone, Copy, Debug, Default, PartialEq)]
77pub struct FreeCamera {
78    /// The position in Web Mercator units, where the world is 1 wide, (0, 0)
79    /// is its northwest corner and (1, 1) its southeast corner. `z` is the
80    /// altitude in the same units.
81    pub position: DVec3,
82    /// The rotation from looking straight down with north at the top of the
83    /// screen, as a unit quaternion.
84    pub orientation: DQuat,
85}
86
87impl From<FreeCamera> for FreeCameraOptions {
88    /// Options that set the position and the orientation.
89    fn from(camera: FreeCamera) -> Self {
90        FreeCameraOptions { position: Some(camera.position), orientation: Some(camera.orientation) }
91    }
92}