Fix loading screen

This commit is contained in:
Michael Hübner 2025-03-05 08:13:41 +01:00
parent f646c11d21
commit 37206c9e9d
2 changed files with 22 additions and 13 deletions

View file

@ -7,3 +7,4 @@ edition = "2024"
[dependencies] [dependencies]
context = { path = "../context" } context = { path = "../context" }
anyhow = { workspace = true } anyhow = { workspace = true }
ecs.workspace = true

View file

@ -1,4 +1,5 @@
use context::prelude::*; use context::prelude::*;
use ecs::World;
use std::{ use std::{
sync::{ sync::{
@ -8,35 +9,41 @@ use std::{
thread, thread,
}; };
use anyhow::Result; use anyhow::{Result, anyhow};
pub struct LoadingScreen<T: Send + Sync + 'static> { pub struct LoadingScreen<T, G>
where
T: Send + Sync + 'static,
G: TopLevelGui + TopGui + Send + Sync + 'static,
{
result: Option<T>, result: Option<T>,
receiver: Receiver<T>, receiver: Receiver<T>,
gui: Arc<G>,
} }
impl<T: Send + Sync + 'static> LoadingScreen<T> { impl<T, G> LoadingScreen<T, G>
pub fn load<R, L, G>(gui_handler: &mut GuiHandler, gui: Arc<G>, loader: L) -> Result<Self> where
T: Send + Sync + 'static,
G: TopLevelGui + TopGui + Send + Sync + 'static,
{
pub fn load<R, L>(world: &mut World, gui: Arc<G>, loader: L) -> Result<Self>
where where
R: Fn(T) -> Result<()> + Send + Sync + 'static, R: Fn(T) -> Result<()> + Send + Sync + 'static,
T: Send + Sync + 'static, T: Send + Sync + 'static,
L: FnOnce() -> 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(); let (sender, receiver) = channel();
thread::spawn(move || { thread::spawn(move || {
let _ = sender.send(loader()); let _ = sender.send(loader());
// TODO: disable loading screen gui
// let _ = gui.disable();
}); });
Ok(Self { Ok(Self {
result: None, result: None,
receiver, receiver,
gui,
}) })
} }
@ -54,9 +61,10 @@ impl<T: Send + Sync + 'static> LoadingScreen<T> {
} }
} }
/// Will panic if there is no result. pub fn take_result(mut self, world: &mut World) -> Result<T> {
/// Use `check_ready` before calling this. self.gui.disable(world)?;
pub fn take_result(mut self) -> T { self.result
self.result.take().unwrap() .take()
.ok_or(anyhow!("missing loading screen payload!"))
} }
} }