Skip to main content

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