From ead79601124159722724fe3f4c66f2db837d14da Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 20 Oct 2020 18:24:37 -0700 Subject: [PATCH] Collapse one case of possible nested loading screens. Slightly simpler code, but this doesn't solve the bug of a second squished loading screen for scenarios. --- game/src/load.rs | 40 +++++++++++++++++++-------------------- game/src/sandbox/mod.rs | 4 ++-- widgetry/src/event_ctx.rs | 4 ++-- 3 files changed, 24 insertions(+), 24 deletions(-) diff --git a/game/src/load.rs b/game/src/load.rs index aa74bf6f0f..de94b0e029 100644 --- a/game/src/load.rs +++ b/game/src/load.rs @@ -3,6 +3,7 @@ use serde::de::DeserializeOwned; +use abstutil::Timer; use sim::Sim; use widgetry::{Color, EventCtx, GfxCtx}; @@ -34,21 +35,20 @@ impl MapLoader { FileLoader::::new( ctx, abstutil::path_map(&name), - Box::new(move |ctx, app, map| { + Box::new(move |ctx, app, timer, map| { // TODO corrupt_err let mut map = map.unwrap(); // Kind of a hack. We can't generically call Map::new with the FileLoader. map.map_loaded_directly(); - ctx.loading_screen("finish loading map", |ctx, timer| { - let sim = Sim::new( - &map, - app.primary.current_flags.sim_flags.opts.clone(), - timer, - ); - app.map_switched(ctx, map, sim, timer); - }); + let sim = Sim::new( + &map, + app.primary.current_flags.sim_flags.opts.clone(), + timer, + ); + app.map_switched(ctx, map, sim, timer); + (on_load)(ctx, app) }), ) @@ -73,14 +73,15 @@ mod native_loader { path: String, // Wrapped in an Option just to make calling from event() work. Technically this is unsafe // if a caller fails to pop the FileLoader state in their transitions! - on_load: Option) -> Transition>>, + on_load: + Option) -> Transition>>, } impl FileLoader { pub fn new( _: &mut EventCtx, path: String, - on_load: Box) -> Transition>, + on_load: Box) -> Transition>, ) -> Box { Box::new(FileLoader { path, @@ -93,11 +94,8 @@ mod native_loader { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { ctx.loading_screen(format!("load {}", self.path), |ctx, timer| { // Assumes a binary file - (self.on_load.take().unwrap())( - ctx, - app, - abstutil::maybe_read_binary(self.path.clone(), timer).ok(), - ) + let file = abstutil::maybe_read_binary(self.path.clone(), timer).ok(); + (self.on_load.take().unwrap())(ctx, app, timer, file) }) } @@ -125,7 +123,8 @@ mod wasm_loader { // compatible with winit's event loop. pub struct FileLoader { response: oneshot::Receiver>, - on_load: Option) -> Transition>>, + on_load: + Option) -> Transition>>, panel: Panel, started: Instant, url: String, @@ -135,7 +134,7 @@ mod wasm_loader { pub fn new( ctx: &mut EventCtx, path: String, - on_load: Box) -> Transition>, + on_load: Box) -> Transition>, ) -> Box { let url = if cfg!(feature = "wasm_s3") { format!( @@ -187,13 +186,14 @@ mod wasm_loader { // while. Any way to make it still be nonblockingish? Maybe put some of the work // inside that spawn_local? + let mut timer = Timer::new(format!("Loading {}...", self.url)); match abstutil::from_binary(&resp) { Ok(obj) => { - return (self.on_load.take().unwrap())(ctx, app, Some(obj)); + return (self.on_load.take().unwrap())(ctx, app, &mut timer, Some(obj)); } Err(err) => { error!("{}: {}", self.url, err); - return (self.on_load.take().unwrap())(ctx, app, None); + return (self.on_load.take().unwrap())(ctx, app, &mut timer, None); } } } diff --git a/game/src/sandbox/mod.rs b/game/src/sandbox/mod.rs index d055d4fdea..3b93ca28c4 100644 --- a/game/src/sandbox/mod.rs +++ b/game/src/sandbox/mod.rs @@ -583,7 +583,7 @@ impl State for SandboxLoader { return Transition::Push(FileLoader::::new( ctx, path, - Box::new(|_, _, scenario| { + Box::new(|_, _, _, scenario| { // TODO Handle corrupt files let scenario = scenario.unwrap(); Transition::Multi(vec![ @@ -636,7 +636,7 @@ impl State for SandboxLoader { return Transition::Push(FileLoader::::new( ctx, abstutil::path_prebaked_results(app.primary.map.get_name(), &scenario_name), - Box::new(move |_, _, prebaked| { + Box::new(move |_, _, _, prebaked| { Transition::Multi(vec![ Transition::Pop, Transition::ModifyState(Box::new(move |state, _, _| { diff --git a/widgetry/src/event_ctx.rs b/widgetry/src/event_ctx.rs index fcc3b41cae..9ff058ab7b 100644 --- a/widgetry/src/event_ctx.rs +++ b/widgetry/src/event_ctx.rs @@ -183,7 +183,7 @@ impl<'a> EventCtx<'a> { } } -pub struct LoadingScreen<'a> { +struct LoadingScreen<'a> { canvas: Canvas, style: Style, prerender: &'a Prerender, @@ -194,7 +194,7 @@ pub struct LoadingScreen<'a> { } impl<'a> LoadingScreen<'a> { - pub fn new( + fn new( prerender: &'a Prerender, style: Style, initial_size: ScreenDims,