concinnity_asset/loading_overlay.rs
1// Scene-loading progress overlay schema.
2
3use crate::{AssetId, de_opt_asset_ref};
4
5/// Requests the scene-loading overlay: a full-window backdrop with a progress
6/// bar, shown while a scene jump waits for its streamed content and faded out
7/// once the destination scene is fully resident.
8///
9/// The overlay is assembled from ordinary UI elements the fields reference: a
10/// [Screen](#screen) that hosts them, a backdrop [Sprite](#sprite) covering
11/// the canvas, a progress-bar track and fill [Sprite](#sprite) pair, and a
12/// [TextLabel](#textlabel) above the bar. Each frame the engine widens the
13/// fill to the destination scene's load progress and rewrites the label with
14/// the percentage; restyle any piece by declaring it yourself under the same
15/// name.
16///
17/// While the overlay's screen is up the world pauses and its render is
18/// skipped, exactly like an opaque menu, but streaming keeps running so the
19/// load it reports can finish. Scenes whose content is already resident jump
20/// without the overlay ever appearing.
21///
22/// Every rendering world that declares [Scene](#scene)s and a
23/// [StreamingConfig](#streamingconfig) receives a `LoadingOverlay` and its
24/// pieces at build time when it declares none, so the example below is only
25/// needed to restyle them. Declare an [EngineDefaults](#enginedefaults) with
26/// `"loading_overlay": false` to remove it from the build entirely.
27#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
28#[serde(default)]
29pub struct LoadingOverlay {
30 /// [Screen](#screen) the overlay shows while a scene loads. Its
31 /// `pauses_world` (on by default) freezes the world beneath the overlay so
32 /// the destination scene starts fresh when revealed.
33 #[serde(deserialize_with = "de_opt_asset_ref")]
34 pub screen: Option<AssetId>,
35 /// Backdrop [Sprite](#sprite) covering the canvas. Its tint alpha is
36 /// animated to reveal the scene once loading completes; an opaque tint
37 /// hides the still-loading world completely.
38 #[serde(deserialize_with = "de_opt_asset_ref")]
39 pub backdrop: Option<AssetId>,
40 /// Progress-bar track [Sprite](#sprite); its width is the bar's full
41 /// extent the fill is measured against.
42 #[serde(deserialize_with = "de_opt_asset_ref")]
43 pub track: Option<AssetId>,
44 /// Progress-bar fill [Sprite](#sprite); the engine sets its width to the
45 /// track width times the destination scene's load progress each frame.
46 #[serde(deserialize_with = "de_opt_asset_ref")]
47 pub fill: Option<AssetId>,
48 /// [TextLabel](#textlabel) rewritten each frame with the load percentage.
49 #[serde(deserialize_with = "de_opt_asset_ref")]
50 pub label: Option<AssetId>,
51}
52
53#[cfg(test)]
54mod tests {
55 use super::*;
56
57 #[test]
58 fn a_blank_overlay_claims_no_pieces() {
59 let o = LoadingOverlay::default();
60 assert!(o.screen.is_none());
61 assert!(o.backdrop.is_none());
62 assert!(o.track.is_none());
63 assert!(o.fill.is_none());
64 assert!(o.label.is_none());
65 }
66
67 #[test]
68 fn each_piece_binds_its_own_asset_and_round_trips_through_postcard() {
69 crate::test_support::install_resolvers();
70 let o: LoadingOverlay = serde_json::from_str(
71 r#"{"screen":"load","backdrop":"dim","track":"bar_bg","fill":"bar","label":"pct"}"#,
72 )
73 .unwrap();
74 assert_eq!(o.screen, Some(AssetId(4)));
75 assert_eq!(o.backdrop, Some(AssetId(3)));
76 assert_eq!(o.track, Some(AssetId(6)));
77 assert_eq!(o.fill, Some(AssetId(3)));
78 assert_eq!(o.label, Some(AssetId(3)));
79
80 let bytes = postcard::to_allocvec(&o).unwrap();
81 let back: LoadingOverlay = postcard::from_bytes(&bytes).unwrap();
82 assert_eq!(back.track, Some(AssetId(6)));
83 assert_eq!(back.screen, Some(AssetId(4)));
84 }
85}