From 875f311f555e63cc921fba6945090ccb591c5dae Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 14 Jan 2020 14:05:21 -0800 Subject: [PATCH] preserve scenario when changing maps --- game/src/sandbox/gameplay/freeform.rs | 37 ++++++++++++++++++++++----- game/src/sandbox/gameplay/mod.rs | 22 ---------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/game/src/sandbox/gameplay/freeform.rs b/game/src/sandbox/gameplay/freeform.rs index 4637f5361e..046432488c 100644 --- a/game/src/sandbox/gameplay/freeform.rs +++ b/game/src/sandbox/gameplay/freeform.rs @@ -1,9 +1,10 @@ use crate::edit::EditMode; -use crate::game::{msg, Transition, WizardState}; +use crate::game::{msg, State, Transition, WizardState}; use crate::helpers::ID; use crate::managed::Composite; -use crate::sandbox::gameplay::{change_scenario, load_map, spawner, GameplayMode, GameplayState}; +use crate::sandbox::gameplay::{change_scenario, spawner, GameplayMode, GameplayState}; use crate::sandbox::overlays::Overlays; +use crate::sandbox::SandboxMode; use crate::ui::UI; use ezgui::{ hotkey, lctrl, Color, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, ManagedWidget, @@ -110,10 +111,10 @@ pub fn freeform_controller( .aligned(HorizontalAlignment::Center, VerticalAlignment::Top) .build(ctx), ) - .cb( - "change map", - Box::new(|_, _| Some(Transition::Push(WizardState::new(Box::new(load_map))))), - ) + .cb("change map", { + let gameplay = gameplay.clone(); + Box::new(move |_, _| Some(Transition::Push(make_load_map(gameplay.clone())))) + }) .cb( "change scenario", Box::new(|_, _| { @@ -133,3 +134,27 @@ pub fn freeform_controller( }), ) } + +fn make_load_map(gameplay: GameplayMode) -> Box { + WizardState::new(Box::new(move |wiz, ctx, ui| { + if let Some(name) = wiz.wrap(ctx).choose_string("Load which map?", || { + let current_map = ui.primary.map.get_name(); + abstutil::list_all_objects(abstutil::path_all_maps()) + .into_iter() + .filter(|n| n != current_map) + .collect() + }) { + ui.switch_map(ctx, abstutil::path_map(&name)); + // Assume a scenario with the same name exists. + Some(Transition::PopThenReplace(Box::new(SandboxMode::new( + ctx, + ui, + gameplay.clone(), + )))) + } else if wiz.aborted() { + Some(Transition::Pop) + } else { + None + } + })) +} diff --git a/game/src/sandbox/gameplay/mod.rs b/game/src/sandbox/gameplay/mod.rs index 138de990e9..d8d9433b75 100644 --- a/game/src/sandbox/gameplay/mod.rs +++ b/game/src/sandbox/gameplay/mod.rs @@ -234,28 +234,6 @@ fn change_scenario(wiz: &mut Wizard, ctx: &mut EventCtx, ui: &mut UI) -> Option< )))) } -fn load_map(wiz: &mut Wizard, ctx: &mut EventCtx, ui: &mut UI) -> Option { - if let Some(name) = wiz.wrap(ctx).choose_string("Load which map?", || { - let current_map = ui.primary.map.get_name(); - abstutil::list_all_objects(abstutil::path_all_maps()) - .into_iter() - .filter(|n| n != current_map) - .collect() - }) { - ui.switch_map(ctx, abstutil::path_map(&name)); - Some(Transition::PopThenReplace(Box::new(SandboxMode::new( - ctx, - ui, - // TODO If we were playing a scenario, load that one... - GameplayMode::Freeform, - )))) - } else if wiz.aborted() { - Some(Transition::Pop) - } else { - None - } -} - // Must call menu.event first. Returns true if the caller should set the overlay to the custom // thing. fn manage_overlays(