From d3c035ffb3e6b4cee6eec8390281acfd74fa4470 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Fri, 30 Apr 2021 12:36:24 -0700 Subject: [PATCH] Remember the last map loaded, and start the game with that, unless using --dev or some other override --- game/src/lib.rs | 13 ++++++++++++- map_gui/src/simple_app.rs | 8 ++++++++ map_gui/src/tools/camera.rs | 21 ++++++++++++++++++++- map_gui/src/tools/mod.rs | 2 +- 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/game/src/lib.rs b/game/src/lib.rs index 76dfe52cb0..e6964e0efe 100644 --- a/game/src/lib.rs +++ b/game/src/lib.rs @@ -140,7 +140,7 @@ fn run(mut settings: Settings) { fn setup_app( ctx: &mut EventCtx, - flags: Flags, + mut flags: Flags, mut opts: Options, start_with_edits: Option, maybe_mode: Option, @@ -152,6 +152,17 @@ fn setup_app( && !flags.sim_flags.load.contains("player/save") && !flags.sim_flags.load.contains("/scenarios/") && maybe_mode.is_none(); + + // Load the map used previously if we're starting on the title screen without any overrides. + if title && flags.sim_flags.load == MapName::seattle("montlake").path() { + if let Ok(default) = abstio::maybe_read_json::( + abstio::path_player("maps.json"), + &mut Timer::throwaway(), + ) { + flags.sim_flags.load = default.last_map.path(); + } + } + // If we're starting directly in a challenge mode, the tutorial, or by playing a scenario, // usually time is midnight, so save some effort and start with the correct color scheme. If // we're loading a savestate and it's actually daytime, we'll pay a small penalty to switch diff --git a/map_gui/src/simple_app.rs b/map_gui/src/simple_app.rs index 5bfc663450..366695bf32 100644 --- a/map_gui/src/simple_app.rs +++ b/map_gui/src/simple_app.rs @@ -40,6 +40,14 @@ impl SimpleApp { let map_name = args .optional_free() .map(|path| MapName::from_path(&path).expect(&format!("bad map path: {}", path))) + .or_else(|| { + abstio::maybe_read_json::( + abstio::path_player("maps.json"), + &mut Timer::throwaway(), + ) + .ok() + .map(|x| x.last_map) + }) .unwrap_or(MapName::seattle("montlake")); let center_camera = args.optional("--cam"); args.done(); diff --git a/map_gui/src/tools/camera.rs b/map_gui/src/tools/camera.rs index aa8cf086c2..eb99c967f7 100644 --- a/map_gui/src/tools/camera.rs +++ b/map_gui/src/tools/camera.rs @@ -3,6 +3,7 @@ use widgetry::{Canvas, EventCtx}; use abstio::MapName; use abstutil::Timer; +use map_model::Map; /// Represents the state of a widgetry Canvas. #[derive(Serialize, Deserialize, Debug)] @@ -12,15 +13,33 @@ pub struct CameraState { cam_zoom: f64, } +/// Track the last map used, to resume next session. +#[derive(Serialize, Deserialize, Debug)] +pub struct DefaultMap { + pub last_map: MapName, +} + impl CameraState { - /// Save the camera's configuration for the specified map. + /// Save the camera's configuration for the specified map, and also remember this map was the + /// last to be used. pub fn save(canvas: &Canvas, name: &MapName) { + if name == Map::blank().get_name() { + return; + } + let state = CameraState { cam_x: canvas.cam_x, cam_y: canvas.cam_y, cam_zoom: canvas.cam_zoom, }; abstio::write_json(abstio::path_camera_state(name), &state); + + abstio::write_json( + abstio::path_player("maps.json"), + &DefaultMap { + last_map: name.clone(), + }, + ); } /// Load the camera's configuration for the specified map. Returns true if successful, has no diff --git a/map_gui/src/tools/mod.rs b/map_gui/src/tools/mod.rs index 875a494e30..9355425a98 100644 --- a/map_gui/src/tools/mod.rs +++ b/map_gui/src/tools/mod.rs @@ -4,7 +4,7 @@ use abstio::MapName; use geom::Polygon; use widgetry::{GfxCtx, Line, Text}; -pub use self::camera::CameraState; +pub use self::camera::{CameraState, DefaultMap}; pub use self::city_picker::CityPicker; pub use self::colors::{ColorDiscrete, ColorLegend, ColorNetwork, ColorScale, DivergingScale}; pub use self::heatmap::{make_heatmap, Grid, HeatmapOptions};