Skip to main content

mln_map/
map_event.rs

1//! What a map reports to its owner: camera changes, loading, frames, and
2//! the activity of its style, glyphs and tiles. The map queues the events,
3//! and its owner drains them with [`Map::drain_events`](crate::Map::drain_events).
4
5use std::sync::Arc;
6
7use mln_renderer::{FontStack, GlyphRange, OverscaledTileID, RenderMode, RenderingStats, TileOperation};
8use mln_style::StyleLoadError;
9use mln_style::sprite::Sprite;
10use mln_util::error::Error;
11
12#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
13#[repr(u32)]
14pub enum CameraChangeMode {
15    #[default]
16    Immediate,
17    Animated,
18}
19
20/// What moved the camera, as the C API's
21/// `mln_camera_change_source` reports it.
22#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
23#[repr(u32)]
24pub enum CameraChangeSource {
25    /// A camera command outside a gesture, or the map itself, as when a
26    /// resize keeps the camera within its bounds.
27    #[default]
28    Command,
29    /// A camera command with a [`GesturePhase`](crate::GesturePhase) other
30    /// than `None`.
31    Gesture,
32    /// The momentum, and the snaps, that a gesture's end started.
33    Momentum,
34}
35
36/// A camera change: whether it jumps or animates, and what made it.
37///
38/// [`MapEvent::CameraWillChange`] reports the command that starts. Each
39/// [`MapEvent::CameraDidChange`] ends one command and reports that command's
40/// source, so a frame that ends a command started by a gesture reports
41/// `Gesture` even while a momentum keeps moving the camera.
42#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
43pub struct CameraChange {
44    pub mode: CameraChangeMode,
45    pub source: CameraChangeSource,
46}
47
48#[derive(Clone, Debug, PartialEq)]
49pub struct RenderFrameStatus {
50    pub mode: RenderMode,
51    /// In continuous mode, shows that there are ongoing transitions.
52    pub needs_repaint: bool,
53    pub placement_changed: bool,
54    /// The render context's counters after this frame: per-frame ones such
55    /// as `num_draw_calls` count this frame, running ones such as
56    /// `total_draw_calls` and `num_frames` count every frame so far.
57    pub rendering_stats: Arc<RenderingStats>,
58}
59
60/// An event of a map. The variants match the C API's
61/// `mln_runtime_event_type` values for maps.
62///
63/// The render events come in continuous mode only. A static map reports
64/// its still images through the callback of
65/// [`Map::render_still`](crate::Map::render_still).
66#[derive(Clone, Debug)]
67pub enum MapEvent {
68    /// A camera command starts.
69    CameraWillChange(CameraChange),
70    /// An animated camera command moved the camera.
71    CameraIsChanging,
72    /// A camera command ended.
73    CameraDidChange(CameraChange),
74
75    /// The style loaded and the map applied its default camera, unless a
76    /// camera command came first.
77    StyleLoaded,
78    /// The map started loading a new style.
79    LoadingStarted,
80    /// The map rendered everything that the style it started loading needs.
81    LoadingFinished,
82    /// The style failed to load.
83    LoadingFailed(StyleLoadError),
84    /// Everything has loaded and no transition runs.
85    Idle,
86    /// A layer or a drawable failed to render.
87    RenderError(Error),
88    RenderFrameStarted,
89    RenderFrameFinished(RenderFrameStatus),
90    /// The renderer is about to draw the first frame of the map.
91    RenderMapStarted,
92    /// The renderer drew a frame with everything loaded, the first since the
93    /// map started loading its style.
94    RenderMapFinished(RenderMode),
95    /// A layer uses an image that the style lacks. Adding it to the style
96    /// lays out the layer again.
97    StyleImageMissing {
98        id: String,
99    },
100    /// A tile of the source `source_id` went through `op`.
101    TileAction {
102        op: TileOperation,
103        id: OverscaledTileID,
104        source_id: String,
105    },
106    /// A style source changed its description, as when its TileJSON
107    /// arrives or its GeoJSON data is replaced. The map's style already
108    /// holds the change.
109    SourceChanged {
110        source_id: String,
111    },
112    GlyphsRequested {
113        font_stack: FontStack,
114        range: GlyphRange,
115    },
116    GlyphsLoaded {
117        font_stack: FontStack,
118        range: GlyphRange,
119    },
120    /// A glyph range of a font stack failed to load or parse.
121    GlyphsError {
122        font_stack: FontStack,
123        range: GlyphRange,
124        error: Error,
125    },
126    /// The style requested a sprite. `None` stands for a style without one.
127    SpriteRequested(Option<Sprite>),
128    /// A sprite's image and JSON loaded and parsed. `None` stands for a
129    /// style without a sprite.
130    SpriteLoaded(Option<Sprite>),
131    /// A sprite's image or JSON failed to load, or the sprite failed to
132    /// parse. `None` stands for a sprite URL that no file source serves.
133    SpriteError(Option<Sprite>, Error),
134}