Skip to main content

mln_engine/map/
snapshot.rs

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