Skip to main content

concinnity_core/components/
loading_overlay.rs

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