From 1ae4c6e41fe7dbe364a5d95f6c18526c563fd818 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 9 Oct 2020 14:02:08 -0700 Subject: [PATCH] Make the web CityPicker do all of the same stuff that app.switch_map does --- game/src/app.rs | 24 ++++++++++++++++++++---- game/src/common/city_picker.rs | 19 +++++++++---------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/game/src/app.rs b/game/src/app.rs index ca3bd8801b..f9dcc3418b 100644 --- a/game/src/app.rs +++ b/game/src/app.rs @@ -124,13 +124,29 @@ impl App { } } + // TODO Refactor the CityPicker trick as a general loading screen for native or web, and change + // all callers of this to organize control flow around a State. pub fn switch_map(&mut self, ctx: &mut EventCtx, load: String) { - ctx.canvas.save_camera_state(self.primary.map.get_name()); let mut flags = self.primary.current_flags.clone(); flags.sim_flags.load = load; - let session = std::mem::replace(&mut self.session, SessionState::empty()); - *self = App::new(flags, self.opts.clone(), ctx, false); - self.session = session; + + ctx.loading_screen("load map", |ctx, timer| { + let (map, sim, _) = flags.sim_flags.load(timer); + self.map_switched(ctx, map, sim, timer); + }); + } + pub fn map_switched(&mut self, ctx: &mut EventCtx, map: Map, sim: Sim, timer: &mut Timer) { + ctx.canvas.save_camera_state(self.primary.map.get_name()); + self.primary = PerMap::map_loaded( + map, + sim, + false, + self.primary.current_flags.clone(), + &self.opts, + &self.cs, + ctx, + timer, + ) } pub fn draw( diff --git a/game/src/common/city_picker.rs b/game/src/common/city_picker.rs index f38b4f10eb..3c30aa124d 100644 --- a/game/src/common/city_picker.rs +++ b/game/src/common/city_picker.rs @@ -296,19 +296,18 @@ mod loader { impl State for AsyncFileLoader { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { if let Some(resp) = self.response.try_recv().unwrap() { - // TODO We stop drawing the timer and start blocking at this point. It can take a + // TODO We stop drawing and start blocking at this point. It can take a // while. Any way to make it still be nonblockingish? Maybe put some of the work // inside that spawn_local? - let map: Map = abstutil::from_binary(&resp).unwrap(); - // TODO This is a hack, repeating only some parts of app.switch_map. Refactor. - let bounds = map.get_bounds(); - ctx.canvas.map_dims = (bounds.width(), bounds.height()); - app.primary.map = map; - let mut timer = Timer::new("switch maps"); - app.primary.draw_map = - DrawMap::new(&app.primary.map, &app.opts, &app.cs, ctx, &mut timer); - app.primary.clear_sim(); + let mut timer = Timer::new("finish loading map"); + let map: Map = abstutil::from_binary(&resp).unwrap(); + let sim = Sim::new( + &map, + app.primary.current_flags.sim_flags.opts.clone(), + &mut timer, + ); + app.map_switched(ctx, map, sim, &mut timer); return (self.on_load)(ctx, app); }