mln_engine/placement/mod.rs
1//! The placement contract: which of the frame's labels and other marks
2//! show, each where it collides with nothing placed before it, and how they
3//! fade between placements. A tile-mode placement can record what it placed
4//! ([`PlacedSymbolData`]).
5//!
6//! A layer type whose layers place registers a participant, a [`Placement`],
7//! with
8//! [`RegistryBuilder::add_placing_layer_type::<P>()`](crate::RegistryBuilder::add_placing_layer_type),
9//! which registers its layer type too. Each renderer makes one of each
10//! type's and calls it for the layers of the type that the frame draws.
11//!
12//! The engine owns what is the same for every participant: the schedule,
13//! which decides when to place and keeps the clock that what shows fades
14//! on; each placement's [`CollisionIndex`], with its grids, padding, overlap
15//! rule ([`Overlap`]), collision groups and the query data of the buckets
16//! whose shapes it holds; the order the layers place in; committing a
17//! placement; tile mode's records, and the shapes a placement tested, which
18//! a frame's layer can ask for ([`TestedShape`]). A participant owns what its type
19//! places: it turns its layers' features into [`Shape`]s, decides which
20//! show, and keeps what each showed as, which its buckets draw. It reads and
21//! changes the frame's buckets by their [`BucketKey`]s
22//! ([`PlacementBuckets`], [`LaidBuckets`]), and where terrain draws, stands
23//! its marks on the ground the frame drew
24//! ([`TerrainElevation`]).
25//!
26//! Each frame, the engine calls every participant:
27//!
28//! 1. [`collect`](Placement::collect), in the frame's update of each layer
29//! of its type, once the layer's own update found its tiles.
30//! 2. [`prepare`](Placement::prepare), with the layers that place, which
31//! brings what the participant keeps up to date with the frame's buckets
32//! and says whether its own inputs changed since the current placement.
33//! 3. When the schedule places: [`begin`](Placement::begin); in tile mode
34//! [`place_crossing`](Placement::place_crossing); for each layer of every
35//! type, topmost first, [`place`](Placement::place); and then
36//! [`commit`](Placement::commit). In a continuous map, a type with
37//! nothing to place forgets its buckets and its placement instead.
38//! 4. [`update`](Placement::update), which brings its buckets to the
39//! current placement.
40//!
41//! A still image places every frame, and every type forgets its buckets after
42//! it. The layers place topmost first, so a layer's shapes give way to those
43//! of the layers above it, whatever their types. Without the map's
44//! cross-source collisions, they give way only to those of layers on their
45//! source ([`PlacingLayer::group`]).
46//!
47//! A continuous map places when its schedule is due and placing would show
48//! something else: the view, the map mode, the style's transitions,
49//! cross-source collisions or the ground changed; the sources hold tiles that
50//! wait for placement to fade them out; what showed was still fading; or a
51//! participant says so ([`Prepared::changed`]).
52
53mod grid;
54mod index;
55mod run;
56mod schedule;
57
58use std::time::Duration;
59
60pub use self::grid::{Aabb, Shape};
61pub use self::index::{CollisionGroup, CollisionIndex, Item, Overlap, RetainedQueryData};
62pub(crate) use self::run::{ErasedPlacement, Placements, participant};
63#[cfg(feature = "bench")]
64pub(crate) use self::run::{place_alone, placing_layers};
65pub(crate) use self::schedule::{PlacementController, PlacementInputs};
66use crate::camera::View;
67use crate::draw::identity::SimpleIdentity;
68use crate::layer::bucket::{Bucket, ErasedBucket};
69pub(crate) use crate::layer::layer_drawing::ChangedBuckets;
70use crate::layer::layer_type::EvaluatedLayer;
71use crate::layer::render_layer::Layer;
72use crate::map::MapMode;
73use crate::render::terrain::TerrainElevation;
74use crate::render::tile::FrameTile;
75use crate::tile::OverscaledTileID;
76use crate::worker::job::SourceKey;
77
78/// A placing layer type's part of placement. Per layer, it turns the
79/// features of the layer's tiles into [`Shape`]s, shows those that collide
80/// with nothing in the placement's [`CollisionIndex`] and inserts them, and
81/// it keeps what each of its items showed as, which its buckets draw. The
82/// module's comment says when the engine calls each hook.
83pub trait Placement: Default + 'static {
84 /// The layer type whose layers place.
85 type Layer: Layer;
86
87 /// Collects what the participant places and draws of `tiles`, the
88 /// frame's tiles of `layer` in the order the layer draws them, each with
89 /// what it holds for the layer. Returns whether the layer places: the
90 /// engine then gives it to [`place`](Self::place) by `layer.key`. What
91 /// the participant collected is the frame's; it drops the last frame's
92 /// in [`begin_frame`](Self::begin_frame).
93 fn collect(&mut self, layer: &CollectCx<'_, Self::Layer>, tiles: &[FrameTile<'_, Self::Layer>]) -> bool;
94
95 /// Drops what the participant collected of the last frame's layers,
96 /// before the frame's layers update.
97 fn begin_frame(&mut self);
98
99 /// Readies the frame's placement, before the schedule decides whether
100 /// to place: brings what the participant keeps of `layers`, those of its
101 /// layers that place, bottom to top, up to date with the frame's
102 /// buckets, and says what of its own inputs changed since the current
103 /// placement.
104 fn prepare(&mut self, layers: &[PlacingLayer], cx: &mut PrepareCx<'_>) -> Prepared;
105
106 /// Begins a placement: in a continuous map it places against the
107 /// current one, which it replaces; in a still image against none.
108 fn begin(&mut self, cx: &mut PlaceCx<'_>);
109
110 /// Tile mode's first round: places what of `layers`' items crosses the
111 /// borders of its tile, or a neighbor's, so that the tiles on either
112 /// side agree on it. [`place`](Self::place) then places the rest.
113 fn place_crossing(&mut self, _layers: &[PlacingLayer], _cx: &mut PlaceCx<'_>) {}
114
115 /// Places `layer`'s items: each shows where its shapes collide with
116 /// nothing of the layer's [group](PlacingLayer::group) in `cx.index`,
117 /// which it then inserts them in.
118 fn place(&mut self, layer: &PlacingLayer, cx: &mut PlaceCx<'_>);
119
120 /// Ends the placement: it is now the current one. Returns what it
121 /// changed of what shows.
122 fn commit(&mut self, cx: &mut CommitCx<'_>) -> Placed;
123
124 /// Brings the buckets of the frame's layers of the type to the current
125 /// placement, each frame after the schedule placed or didn't.
126 fn update(&mut self, cx: &mut UpdateCx<'_>);
127
128 /// Forgets what the participant keeps of its layers' buckets from frame
129 /// to frame: the renderer's tiles went, or the frame is a still image's,
130 /// which keeps none.
131 fn forget_buckets(&mut self);
132
133 /// Forgets the current placement: the engine dropped it, so what it
134 /// showed is gone.
135 fn forget_placement(&mut self);
136}
137
138/// A layer that a [`Placement`] collects in the frame's update.
139pub struct CollectCx<'a, L: Layer> {
140 /// What the engine gives the layer to [`Placement::place`] by.
141 pub key: usize,
142 /// The layer's values of the frame.
143 pub values: &'a EvaluatedLayer<L::Style>,
144 /// The tiles of the layer's source, if it has tiles.
145 pub source: Option<SourceKey>,
146}
147
148/// A layer that places in the frame, as the engine gives it to a
149/// [`Placement`].
150#[derive(Clone, Copy, Debug, PartialEq, Eq)]
151pub struct PlacingLayer {
152 /// The layer's [`CollectCx::key`].
153 pub key: usize,
154 /// The layer's position among the frame's placing layers of every
155 /// type, topmost first, which the order of their shapes follows.
156 pub position: usize,
157 /// The shapes the layer's shapes collide with, and are in.
158 pub group: CollisionGroup,
159}
160
161/// What a [`Placement::prepare`] reads and changes.
162pub struct PrepareCx<'a> {
163 /// The view the frame draws.
164 pub view: &'a View,
165 /// The ground the frame's marks stand on, where terrain draws.
166 pub ground: Option<&'a TerrainElevation>,
167 /// The buckets of the frame's tiles.
168 pub buckets: PlacementBuckets<'a>,
169}
170
171/// What a [`Placement`] reads and changes while it places.
172pub struct PlaceCx<'a> {
173 /// The placement's shapes so far, which those placed now may not
174 /// collide with.
175 pub index: &'a mut CollisionIndex,
176 /// The view placed in.
177 pub view: &'a View,
178 /// The map's mode: in [`MapMode::Tile`], the placement has a crossing
179 /// round ([`Placement::place_crossing`]) before the rest.
180 pub mode: MapMode,
181 /// The ground the marks stand on, where terrain draws.
182 pub ground: Option<&'a TerrainElevation>,
183 /// The buckets of the frame's tiles.
184 pub buckets: &'a dyn LaidBuckets,
185 /// The records of what a tile-mode placement placed, while the
186 /// renderer collects them.
187 pub records: Option<&'a mut Vec<PlacedSymbolData>>,
188 /// The shapes the placement tests, while a layer of the frame reads
189 /// them ([`Evaluation::reads_collision_shapes`]).
190 ///
191 /// [`Evaluation::reads_collision_shapes`]: crate::layer::render_layer::Evaluation::reads_collision_shapes
192 pub tested: Option<&'a mut Vec<TestedShape>>,
193}
194
195/// What a [`Placement::commit`] reads and changes.
196pub struct CommitCx<'a> {
197 /// The map's mode: a continuous map fades what shows from the placement
198 /// before, a still image shows it at once.
199 pub mode: MapMode,
200 /// How far the fade of the placement this one replaces had come: 0 as
201 /// it started, 1 and past once it was done.
202 pub fade: f32,
203 /// The buckets of the frame's tiles.
204 pub buckets: PlacementBuckets<'a>,
205}
206
207/// What a [`Placement::update`] reads and changes.
208pub struct UpdateCx<'a> {
209 /// The view the frame draws.
210 pub view: &'a View,
211 /// The map's mode.
212 pub mode: MapMode,
213 /// Whether the frame placed, or dropped the placement.
214 pub placed: bool,
215 /// The ground the current placement's marks stand on, where terrain
216 /// draws.
217 pub ground: Option<&'a TerrainElevation>,
218 /// The current placement's index, whose query data the participant
219 /// keeps up to date with how its buckets sort their features.
220 pub index: &'a mut CollisionIndex,
221 /// The buckets of the frame's tiles.
222 pub buckets: PlacementBuckets<'a>,
223 /// The buckets whose vertices changed, which the draws that copied them
224 /// copy again.
225 pub changed: &'a mut ChangedBuckets,
226}
227
228/// What a [`Placement::prepare`] says of the participant's own inputs: the
229/// engine compares the rest (the module's comment lists it).
230#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
231pub struct Prepared {
232 /// Whether the participant can place items it couldn't before, which in
233 /// a flat view shortens the wait of a continuous map for its next
234 /// placement to `Prepared::SOON`.
235 pub added: bool,
236 /// Whether placing now would place from other inputs than the current
237 /// placement did, which a continuous map places again for.
238 pub changed: bool,
239}
240
241impl Prepared {
242 /// How soon after a placement a flat view places again where a
243 /// participant [added](Self::added) items: new items usually come from
244 /// several tiles in a row, which the short wait places together.
245 pub(crate) const SOON: Duration = Duration::from_millis(30);
246}
247
248/// What a placement changed of what shows, which the schedule of a
249/// continuous map reads.
250#[derive(Clone, Copy, Debug, PartialEq, Eq)]
251pub struct Placed {
252 /// Whether what shows changed from the placement it replaces, which
253 /// starts its fade.
254 pub changed: bool,
255 /// Whether everything that shows or hides was at the end of its fade
256 /// when the placement was made. A placement that wasn't places again,
257 /// once due, to bring it there.
258 pub settled: bool,
259}
260
261/// A placement that changed nothing and is settled.
262impl Default for Placed {
263 fn default() -> Self {
264 Placed { changed: false, settled: true }
265 }
266}
267
268impl std::ops::BitOrAssign for Placed {
269 /// The placement of both: changed where either changed, settled where
270 /// both are.
271 fn bitor_assign(&mut self, other: Placed) {
272 self.changed |= other.changed;
273 self.settled &= other.settled;
274 }
275}
276
277/// A shape that a placement tested for a mark, which a debug overlay draws.
278#[derive(Clone, Copy, Debug, PartialEq)]
279pub struct TestedShape {
280 /// The shape, in pixels from the view's top-left corner
281 /// ([`CollisionIndex::to_viewport`]).
282 pub shape: Shape,
283 /// Whether the mark placed. A mark that collided, or that hides for
284 /// another reason, such as a part it waits for, didn't.
285 pub placed: bool,
286}
287
288/// What a tile-mode placement placed of a symbol, which a tool reads to
289/// check that neighboring tiles agree.
290#[derive(Clone, Debug, PartialEq)]
291pub struct PlacedSymbolData {
292 /// Contents of the label
293 pub key: String,
294 /// If symbol contains text, text collision box in viewport coordinates
295 pub text_collision_box: Option<Aabb>,
296 /// If symbol contains icon, icon collision box in viewport coordinates
297 pub icon_collision_box: Option<Aabb>,
298 /// Symbol text was placed
299 pub text_placed: bool,
300 /// Symbol icon was placed
301 pub icon_placed: bool,
302 /// Symbol text or icon collision box intersects tile borders
303 pub intersects_tile_border: bool,
304 /// The viewport padding, which is where the tile's top-left corner is,
305 /// in x and in y.
306 pub viewport_padding: f32,
307 /// Layer id (leader of the symbol layout group)
308 pub layer: String,
309}
310
311/// Where a bucket is: its tile, in the tile store, with where the tile is
312/// among its source's tiles as the frame's render tiles found it
313/// ([`RenderTile::slot`](crate::render::tile::RenderTile::slot)), and its
314/// index among the tile's buckets.
315#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
316pub struct BucketKey {
317 pub bucket: usize,
318 pub slot: Option<usize>,
319 pub tile: OverscaledTileID,
320 pub source: SourceKey,
321}
322
323/// The buckets of the frame's tiles, by key.
324pub trait LaidBuckets {
325 /// The identity of the bucket at `key`, which its draws' changes name.
326 fn id(&self, key: &BucketKey) -> Option<SimpleIdentity>;
327 fn bucket(&self, key: &BucketKey) -> Option<&dyn ErasedBucket>;
328 fn bucket_mut(&mut self, key: &BucketKey) -> Option<&mut dyn ErasedBucket>;
329}
330
331impl dyn LaidBuckets + '_ {
332 /// The bucket at `key`, if it is there and is a `B`.
333 pub fn get<B: Bucket>(&self, key: &BucketKey) -> Option<&B> {
334 self.bucket(key)?.downcast_ref()
335 }
336}
337
338/// The buckets of the frame's tiles as a [`Placement`] reads and changes
339/// them, by their [`BucketKey`]s.
340pub struct PlacementBuckets<'a> {
341 store: &'a mut dyn LaidBuckets,
342}
343
344impl<'a> PlacementBuckets<'a> {
345 pub(crate) fn new(store: &'a mut dyn LaidBuckets) -> Self {
346 PlacementBuckets { store }
347 }
348
349 /// The identity of the bucket at `key`, which its draws' changes name.
350 #[must_use]
351 pub fn id(&self, key: &BucketKey) -> Option<SimpleIdentity> {
352 self.store.id(key)
353 }
354
355 /// The bucket at `key`, if it is there and is a `B`, to change.
356 pub fn get_mut<B: Bucket>(&mut self, key: &BucketKey) -> Option<&mut B> {
357 self.store.bucket_mut(key)?.downcast_mut()
358 }
359
360 /// The buckets, to read.
361 #[must_use]
362 pub fn store(&self) -> &dyn LaidBuckets {
363 self.store
364 }
365}