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, the map changes after this frame without new
52    /// commands: a transition runs, or the symbols wait for a placement
53    /// ([`Map::repaint_at`](crate::Map::repaint_at)).
54    pub needs_repaint: bool,
55    pub placement_changed: bool,
56    /// The render context's counters after this frame: per-frame ones such
57    /// as `num_draw_calls` count this frame, running ones such as
58    /// `total_draw_calls` and `num_frames` count every frame so far.
59    pub rendering_stats: Arc<RenderingStats>,
60}
61
62/// An event of a map. The variants match the C API's
63/// `mln_runtime_event_type` values for maps.
64///
65/// The render events come in continuous mode only. A static map reports
66/// its still images through the callback of
67/// [`Map::render_still`](crate::Map::render_still).
68#[derive(Clone, Debug)]
69pub enum MapEvent {
70    /// A camera command starts.
71    CameraWillChange(CameraChange),
72    /// An animated camera command moved the camera.
73    CameraIsChanging,
74    /// A camera command ended.
75    CameraDidChange(CameraChange),
76
77    /// The style loaded and the map applied its default camera, unless a
78    /// camera command came first.
79    StyleLoaded,
80    /// The map started loading a new style.
81    LoadingStarted,
82    /// The map rendered everything that the style it started loading needs.
83    LoadingFinished,
84    /// The style failed to load.
85    LoadingFailed(StyleLoadError),
86    /// A complete frame of the map's last update drew: everything has
87    /// loaded, no transition runs, and no frame follows until something
88    /// changes the map.
89    Idle,
90    /// A frame failed on the GPU, and lacks what failed. The still image it
91    /// was drawn for fails with the error. A continuous map reports a run of
92    /// failed frames once, and soon draws again, in case the failure passes.
93    RenderError(Error),
94    RenderFrameStarted,
95    RenderFrameFinished(RenderFrameStatus),
96    /// The renderer is about to draw the first frame of the map.
97    RenderMapStarted,
98    /// The renderer drew a frame with everything loaded, the first since the
99    /// map started loading its style.
100    RenderMapFinished(RenderMode),
101    /// A layer uses an image that the style lacks. Adding it to the style
102    /// lays out the layer again.
103    StyleImageMissing {
104        id: String,
105    },
106    /// A tile of the source `source_id` went through `op`.
107    TileAction {
108        op: TileOperation,
109        id: OverscaledTileID,
110        source_id: String,
111    },
112    /// A style source changed its description, as when its TileJSON
113    /// arrives or its GeoJSON data is replaced. The map's style already
114    /// holds the change.
115    SourceChanged {
116        source_id: String,
117    },
118    GlyphsRequested {
119        font_stack: FontStack,
120        range: GlyphRange,
121    },
122    GlyphsLoaded {
123        font_stack: FontStack,
124        range: GlyphRange,
125    },
126    /// A glyph range of a font stack failed to load or parse.
127    GlyphsError {
128        font_stack: FontStack,
129        range: GlyphRange,
130        error: Error,
131    },
132    /// The style requested a sprite. `None` stands for a style without one.
133    SpriteRequested(Option<Sprite>),
134    /// A sprite's image and JSON loaded and parsed. `None` stands for a
135    /// style without a sprite.
136    SpriteLoaded(Option<Sprite>),
137    /// A sprite's image or JSON failed to load, or the sprite failed to
138    /// parse. `None` stands for a sprite URL that no file source serves.
139    SpriteError(Option<Sprite>, Error),
140}