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/// The parts of the camera that a gesture, or its momentum, moves, as
39/// [`GestureOptions`](crate::gestures::GestureOptions) names them. A part
40/// that a limit holds, such as the zoom at its maximum, still reports that
41/// the gesture moves it. A zoom, a rotation or a tilt about an anchor also
42/// moves the center to keep the anchor in place, but reports only its own
43/// part.
44#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
45pub struct CameraParts {
46 /// The center moves with the map's content, as a drag does.
47 pub pan: bool,
48 pub zoom: bool,
49 pub bearing: bool,
50 pub pitch: bool,
51}
52
53impl CameraParts {
54 #[must_use]
55 pub fn is_empty(self) -> bool {
56 self == CameraParts::default()
57 }
58}
59
60impl std::ops::BitOr for CameraParts {
61 type Output = CameraParts;
62
63 fn bitor(self, other: CameraParts) -> CameraParts {
64 CameraParts {
65 pan: self.pan || other.pan,
66 zoom: self.zoom || other.zoom,
67 bearing: self.bearing || other.bearing,
68 pitch: self.pitch || other.pitch,
69 }
70 }
71}
72
73/// A camera change: whether it jumps or animates, what made it, and for a
74/// gesture or a momentum, what it moves.
75///
76/// [`MapEvent::CameraWillChange`] reports the command that starts. Each
77/// [`MapEvent::CameraDidChange`] ends one command and reports that command's
78/// source, so a frame that ends a command started by a gesture reports
79/// `Gesture` even while a momentum keeps moving the camera.
80#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
81pub struct CameraChange {
82 pub mode: CameraChangeMode,
83 pub source: CameraChangeSource,
84 /// Empty for [`CameraChangeSource::Command`].
85 pub parts: CameraParts,
86}
87
88#[derive(Clone, Debug, PartialEq)]
89pub struct RenderFrameStatus {
90 pub mode: RenderMode,
91 /// In continuous mode, the map changes after this frame without new
92 /// commands: a transition runs, or the symbols wait for a placement
93 /// ([`Map::repaint_at`](crate::map::Map::repaint_at)).
94 pub needs_repaint: bool,
95 pub placement_changed: bool,
96 /// The render context's counters after this frame: per-frame ones such
97 /// as `num_draw_calls` count this frame, running ones such as
98 /// `total_draw_calls` and `num_frames` count every frame so far.
99 pub rendering_stats: Arc<RenderingStats>,
100}
101
102/// An event of a map. The variants match the C API's
103/// `mln_runtime_event_type` values for maps.
104///
105/// The render events come in continuous mode only. A static map reports
106/// its still images through the callback of
107/// [`Map::render_still`](crate::map::Map::render_still).
108#[derive(Clone, Debug)]
109pub enum MapEvent {
110 /// A camera command starts.
111 CameraWillChange(CameraChange),
112 /// An animated camera command moved the camera.
113 CameraIsChanging,
114 /// A camera command ended.
115 CameraDidChange(CameraChange),
116
117 /// The style loaded and the map applied its default camera, unless a
118 /// camera command came first.
119 StyleLoaded,
120 /// The map started loading a new style.
121 LoadingStarted,
122 /// The map rendered everything that the style it started loading needs.
123 LoadingFinished,
124 /// The style failed to load.
125 LoadingFailed(StyleLoadError),
126 /// A complete frame of the map's last update drew: everything has
127 /// loaded, no transition runs, and no frame follows until something
128 /// changes the map.
129 Idle,
130 /// A frame failed on the GPU, and lacks what failed. The still image it
131 /// was drawn for fails with the error. A continuous map reports a run of
132 /// failed frames once, and soon draws again, in case the failure passes.
133 RenderError(Error),
134 RenderFrameStarted,
135 RenderFrameFinished(RenderFrameStatus),
136 /// The renderer is about to draw the first frame of the map.
137 RenderMapStarted,
138 /// The renderer drew a frame with everything loaded, the first since the
139 /// map started loading its style.
140 RenderMapFinished(RenderMode),
141 /// A layer uses an image that the style lacks. Adding it to the style
142 /// lays out the layer again.
143 StyleImageMissing {
144 id: String,
145 },
146 /// A tile of the source `source_id` went through `op`.
147 TileAction {
148 op: TileOperation,
149 id: OverscaledTileID,
150 source_id: String,
151 },
152 /// A style source changed its description, as when its TileJSON
153 /// arrives or its GeoJSON data is replaced. The map's style already
154 /// holds the change.
155 SourceChanged {
156 source_id: String,
157 },
158 GlyphsRequested {
159 font_stack: FontStack,
160 range: GlyphRange,
161 },
162 GlyphsLoaded {
163 font_stack: FontStack,
164 range: GlyphRange,
165 },
166 /// A glyph range of a font stack failed to load or parse.
167 GlyphsError {
168 font_stack: FontStack,
169 range: GlyphRange,
170 error: Error,
171 },
172 /// The style requested a sprite. `None` stands for a style without one.
173 SpriteRequested(Option<Sprite>),
174 /// A sprite's image and JSON loaded and parsed. `None` stands for a
175 /// style without a sprite.
176 SpriteLoaded(Option<Sprite>),
177 /// A sprite's image or JSON failed to load, or the sprite failed to
178 /// parse. `None` stands for a sprite URL that no file source serves.
179 SpriteError(Option<Sprite>, Error),
180}