menu to load any savestate

This commit is contained in:
Dustin Carlino 2019-08-06 13:08:07 -07:00
parent b1c92fa867
commit bf5c45cd72
3 changed files with 61 additions and 1 deletions

View File

@ -62,6 +62,10 @@ pub fn path1_bin(map_name: &str, category: &str, instance: &str) -> String {
format!("../data/{}/{}/{}.bin", category, map_name, instance)
}
pub fn path2_dir(map_name: &str, category: &str, dir: &str) -> String {
format!("../data/{}/{}/{}/", category, map_name, dir)
}
pub fn path2_bin(map_name: &str, category: &str, dir: &str, instance: &str) -> String {
format!("../data/{}/{}/{}/{}.bin", category, map_name, dir, instance)
}

View File

@ -10,7 +10,9 @@ use crate::debug::DebugMode;
use crate::edit::EditMode;
use crate::game::{State, Transition};
use crate::ui::{ShowEverything, UI};
use ezgui::{hotkey, lctrl, EventCtx, EventLoopMode, GfxCtx, Key, ModalMenu, Text};
use ezgui::{
hotkey, lctrl, EventCtx, EventLoopMode, GfxCtx, Key, ModalMenu, Text, Wizard, WrappedWizard,
};
use geom::Duration;
use sim::Sim;
@ -46,6 +48,7 @@ impl SandboxMode {
(hotkey(Key::O), "save sim state"),
(hotkey(Key::Y), "load previous sim state"),
(hotkey(Key::U), "load next sim state"),
(None, "pick a savestate to load"),
(hotkey(Key::X), "reset sim"),
(hotkey(Key::S), "start a scenario"),
],
@ -180,6 +183,12 @@ impl State for SandboxMode {
}
});
}
if self.menu.action("pick a savestate to load") {
return Transition::Push(Box::new(LoadSavestate {
path: ui.primary.sim.save_dir(),
wizard: Wizard::new(),
}));
}
if let Some(t) = time_controls(ctx, ui, &mut self.menu) {
return t;
@ -213,3 +222,42 @@ impl State for SandboxMode {
self.speed.pause();
}
}
struct LoadSavestate {
path: String,
wizard: Wizard,
}
impl State for LoadSavestate {
fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition {
if let Some(ss) = pick_savestate(&self.path, &mut self.wizard.wrap(ctx)) {
ctx.loading_screen("load savestate", |ctx, mut timer| {
ui.primary.sim = Sim::load_savestate(ss, &mut timer).unwrap();
ui.recalculate_current_selection(ctx);
});
return Transition::Pop;
} else if self.wizard.aborted() {
return Transition::Pop;
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, _: &UI) {
self.wizard.draw(g);
}
}
fn pick_savestate(path: &str, wizard: &mut WrappedWizard) -> Option<String> {
let path_copy = path.to_string();
wizard
.choose_something_no_keys::<()>(
"Load which savestate?",
Box::new(move || {
abstutil::list_dir(std::path::Path::new(&path_copy))
.into_iter()
.map(|f| (f, ()))
.collect()
}),
)
.map(|(f, _)| f)
}

View File

@ -567,6 +567,14 @@ impl Sim {
// Savestating
impl Sim {
pub fn save_dir(&self) -> String {
abstutil::path2_dir(
&self.map_name,
abstutil::SAVE,
&format!("{}_{}", self.edits_name, self.run_name),
)
}
fn save_path(&self, base_time: Duration) -> String {
// If we wanted to be even more reproducible, we'd encode RNG seed, version of code, etc,
// but that's overkill right now.