make a per-map editor_state, and move a few more things into data/

This commit is contained in:
Dustin Carlino 2019-08-05 11:36:49 -07:00
parent 3b164df9b0
commit c82413fca5
9 changed files with 48 additions and 34 deletions

3
.gitignore vendored
View File

@ -2,10 +2,9 @@
/target/
*.swp
editor_state.json
data/ab_test_saves/*
data/ab_tests/*
data/editor_state/*
data/edits/*
data/input/*
data/maps/*

View File

@ -38,6 +38,9 @@ macro_rules! skip_fail {
};
}
// TODO It might be nice to organize stuff in data/per_map/. Except it makes looping over all maps
// a bit tougher, and it's unclear how to represent singletons like maps/foo.bin.
pub const AB_TESTS: &str = "ab_tests";
pub const AB_TEST_SAVES: &str = "ab_test_saves";
pub const EDITS: &str = "edits";
@ -75,6 +78,10 @@ pub fn path_raw_map(map_name: &str) -> String {
format!("../data/raw_maps/{}.bin", map_name)
}
pub fn path_editor_state(map_name: &str) -> String {
format!("../data/editor_state/{}.json", map_name)
}
pub fn path_pending_screenshots(map_name: &str) -> String {
format!("../data/screenshots/pending_{}", map_name)
}

View File

@ -1,7 +1,7 @@
use crate::render::DrawOptions;
use crate::sandbox::SandboxMode;
use crate::splash_screen::SplashScreen;
use crate::ui::{EditorState, Flags, ShowEverything, UI};
use crate::ui::{Flags, ShowEverything, UI};
use ezgui::{Canvas, EventCtx, EventLoopMode, GfxCtx, GUI};
// This is the top-level of the GUI logic. This module should just manage interactions between the
@ -38,19 +38,6 @@ impl Game {
idx_draw_base,
}
}
fn save_editor_state(&self, canvas: &Canvas) {
let state = EditorState {
map_name: self.ui.primary.map.get_name().clone(),
cam_x: canvas.cam_x,
cam_y: canvas.cam_y,
cam_zoom: canvas.cam_zoom,
};
// TODO maybe make state line up with the map, so loading from a new map doesn't break
abstutil::write_json("../editor_state.json", &state)
.expect("Saving editor_state.json failed");
println!("Saved editor_state.json");
}
}
impl GUI for Game {
@ -157,13 +144,13 @@ impl GUI for Game {
println!("Secondary sim:");
s.sim.dump_before_abort();
}
self.save_editor_state(canvas);
self.ui.save_editor_state(canvas);
}
fn before_quit(&self, canvas: &Canvas) {
self.save_editor_state(canvas);
self.ui.save_editor_state(canvas);
self.ui.cs.save();
println!("Saved color_scheme.json");
println!("Saved data/color_scheme.json");
}
fn profiling_enabled(&self) -> bool {

View File

@ -81,7 +81,7 @@ struct ModifiedColors {
impl ColorScheme {
pub fn load() -> Result<ColorScheme, Error> {
let modified: ModifiedColors = abstutil::read_json("../color_scheme.json")?;
let modified: ModifiedColors = abstutil::read_json("../data/color_scheme.json")?;
let mut map: HashMap<String, Color> = default_colors();
for (name, c) in &modified.map {
map.insert(name.clone(), *c);
@ -91,8 +91,8 @@ impl ColorScheme {
}
pub fn save(&self) {
abstutil::write_json("../color_scheme.json", &self.modified)
.expect("Saving color_scheme.json failed");
abstutil::write_json("../data/color_scheme.json", &self.modified)
.expect("Saving data/color_scheme.json failed");
}
// Get, but specify the default inline. The default is extracted before compilation by a script

View File

@ -169,6 +169,7 @@ fn splash_screen(
.collect()
}),
) {
ui.save_editor_state(ctx.canvas);
// This retains no state, but that's probably fine.
let mut flags = ui.primary.current_flags.clone();
flags.sim_flags.load = PathBuf::from(abstutil::path_map(&name));

View File

@ -5,7 +5,7 @@ use crate::render::{
};
use abstutil;
use abstutil::{MeasureMemory, Timer};
use ezgui::{Color, EventCtx, GeomBatch, GfxCtx, Prerender};
use ezgui::{Canvas, Color, EventCtx, GeomBatch, GfxCtx, Prerender};
use geom::{Bounds, Circle, Distance, Duration};
use map_model::{Map, Traversable};
use rand::seq::SliceRandom;
@ -45,15 +45,19 @@ impl UI {
if splash {
ctx.canvas.center_on_map_pt(rand_focus_pt);
} else {
match abstutil::read_json::<EditorState>("../editor_state.json") {
Ok(ref loaded) if primary.map.get_name() == &loaded.map_name => {
println!("Loaded previous editor_state.json");
let path = abstutil::path_editor_state(primary.map.get_name());
match abstutil::read_json::<EditorState>(&path) {
Ok(ref loaded) => {
println!("Loaded {}", path);
ctx.canvas.cam_x = loaded.cam_x;
ctx.canvas.cam_y = loaded.cam_y;
ctx.canvas.cam_zoom = loaded.cam_zoom;
}
_ => {
println!("Couldn't load editor_state.json or it's for a different map, so just focusing on an arbitrary building");
println!(
"Couldn't load {}, so just focusing on an arbitrary building",
path
);
ctx.canvas.center_on_map_pt(rand_focus_pt);
}
}
@ -353,10 +357,22 @@ impl UI {
borrows
}
pub fn save_editor_state(&self, canvas: &Canvas) {
let state = EditorState {
map_name: self.primary.map.get_name().clone(),
cam_x: canvas.cam_x,
cam_y: canvas.cam_y,
cam_zoom: canvas.cam_zoom,
};
let path = abstutil::path_editor_state(&state.map_name);
abstutil::write_json(&path, &state).unwrap();
println!("Saved {}", path);
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct EditorState {
struct EditorState {
pub map_name: String,
pub cam_x: f64,
pub cam_y: f64,

View File

@ -11,16 +11,18 @@ fi
rm -rfv $OUT
mkdir $OUT
cp color_scheme.json docs/INSTRUCTIONS.md release/play_abstreet.sh $OUT
cp docs/INSTRUCTIONS.md release/play_abstreet.sh $OUT
mkdir $OUT/data
cp data/color_scheme.json $OUT/data
mkdir -p $OUT/data/maps
mkdir $OUT/data/maps
for map in 23rd ballard caphill downtown montlake; do
cp -v data/maps/$map.bin $OUT/data/maps/
mkdir -p $OUT/data/scenarios/$map
cp -v data/scenarios/$map/psrc* $OUT/data/scenarios/$map/
done
mkdir -p $OUT/data/shapes
mkdir $OUT/data/shapes
cp -v data/shapes/popdat.bin $OUT/data/shapes
mkdir $OUT/editor

View File

@ -7,16 +7,18 @@ OUT=abstreet_windows
rm -rfv $OUT
mkdir $OUT
cp color_scheme.json docs/INSTRUCTIONS.md release/play_abstreet.bat $OUT
cp docs/INSTRUCTIONS.md release/play_abstreet.bat $OUT
mkdir $OUT/data
cp data/color_scheme.json $OUT/data
mkdir -p $OUT/data/maps
mkdir $OUT/data/maps
for map in 23rd ballard caphill downtown montlake; do
cp -v data/maps/$map.bin $OUT/data/maps/
mkdir -p $OUT/data/scenarios/$map
cp -v data/scenarios/$map/psrc* $OUT/data/scenarios/$map/
done
mkdir -p $OUT/data/shapes
mkdir $OUT/data/shapes
cp -v data/shapes/popdat.bin $OUT/data/shapes
mkdir $OUT/editor