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