diff --git a/loading-screen/Cargo.toml b/loading-screen/Cargo.toml index 9ce2071..cedd656 100644 --- a/loading-screen/Cargo.toml +++ b/loading-screen/Cargo.toml @@ -7,3 +7,4 @@ edition = "2024" [dependencies] context = { path = "../context" } anyhow = { workspace = true } +ecs.workspace = true diff --git a/loading-screen/src/loadingscreen.rs b/loading-screen/src/loadingscreen.rs index 3df443b..50aef93 100644 --- a/loading-screen/src/loadingscreen.rs +++ b/loading-screen/src/loadingscreen.rs @@ -1,4 +1,5 @@ use context::prelude::*; +use ecs::World; use std::{ sync::{ @@ -8,35 +9,41 @@ use std::{ thread, }; -use anyhow::Result; +use anyhow::{Result, anyhow}; -pub struct LoadingScreen { +pub struct LoadingScreen +where + T: Send + Sync + 'static, + G: TopLevelGui + TopGui + Send + Sync + 'static, +{ result: Option, receiver: Receiver, + gui: Arc, } -impl LoadingScreen { - pub fn load(gui_handler: &mut GuiHandler, gui: Arc, loader: L) -> Result +impl LoadingScreen +where + T: Send + Sync + 'static, + G: TopLevelGui + TopGui + Send + Sync + 'static, +{ + pub fn load(world: &mut World, gui: Arc, loader: L) -> Result where R: Fn(T) -> Result<()> + Send + Sync + 'static, T: Send + Sync + 'static, L: FnOnce() -> T + Send + Sync + 'static, - G: TopLevelGui + TopGui + Send + Sync + 'static, { - gui.enable(gui_handler)?; + gui.enable(world)?; let (sender, receiver) = channel(); thread::spawn(move || { let _ = sender.send(loader()); - - // TODO: disable loading screen gui - // let _ = gui.disable(); }); Ok(Self { result: None, receiver, + gui, }) } @@ -54,9 +61,10 @@ impl LoadingScreen { } } - /// Will panic if there is no result. - /// Use `check_ready` before calling this. - pub fn take_result(mut self) -> T { - self.result.take().unwrap() + pub fn take_result(mut self, world: &mut World) -> Result { + self.gui.disable(world)?; + self.result + .take() + .ok_or(anyhow!("missing loading screen payload!")) } }