Fix loading screen
This commit is contained in:
parent
f646c11d21
commit
37206c9e9d
2 changed files with 22 additions and 13 deletions
|
@ -7,3 +7,4 @@ edition = "2024"
|
|||
[dependencies]
|
||||
context = { path = "../context" }
|
||||
anyhow = { workspace = true }
|
||||
ecs.workspace = true
|
||||
|
|
|
@ -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<T: Send + Sync + 'static> {
|
||||
pub struct LoadingScreen<T, G>
|
||||
where
|
||||
T: Send + Sync + 'static,
|
||||
G: TopLevelGui + TopGui + Send + Sync + 'static,
|
||||
{
|
||||
result: Option<T>,
|
||||
receiver: Receiver<T>,
|
||||
gui: Arc<G>,
|
||||
}
|
||||
|
||||
impl<T: Send + Sync + 'static> LoadingScreen<T> {
|
||||
pub fn load<R, L, G>(gui_handler: &mut GuiHandler, gui: Arc<G>, loader: L) -> Result<Self>
|
||||
impl<T, G> LoadingScreen<T, G>
|
||||
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
|
||||
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<T: Send + Sync + 'static> LoadingScreen<T> {
|
|||
}
|
||||
}
|
||||
|
||||
/// 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<T> {
|
||||
self.gui.disable(world)?;
|
||||
self.result
|
||||
.take()
|
||||
.ok_or(anyhow!("missing loading screen payload!"))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue