From bcc65941f37701033801ed7215f679002ac703c1 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Wed, 19 Sep 2018 07:02:17 -0700 Subject: [PATCH] saving polygon areas --- .gitignore | 1 + abstutil/src/lib.rs | 6 ++++++ editor/src/plugins/draw_polygon.rs | 22 +++++++++++++++++++--- editor/src/ui.rs | 2 +- sim/src/sim.rs | 2 -- 5 files changed, 27 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index a3edd0bc01..2c7edd3918 100644 --- a/.gitignore +++ b/.gitignore @@ -5,5 +5,6 @@ editor/editor_state editor/road_edits.json data/*.abst +data/polygons/* data/input/* data/save/* diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index efb4937399..c886efcad9 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -16,6 +16,9 @@ pub fn to_json(obj: &T) -> String { } pub fn write_json(path: &str, obj: &T) -> Result<(), Error> { + std::fs::create_dir_all(std::path::Path::new(path).parent().unwrap()) + .expect("Creating parent dir failed"); + let mut file = File::create(path)?; file.write_all(to_json(obj).as_bytes())?; Ok(()) @@ -30,6 +33,9 @@ pub fn read_json(path: &str) -> Result { } pub fn write_binary(path: &str, obj: &T) -> Result<(), Error> { + std::fs::create_dir_all(std::path::Path::new(path).parent().unwrap()) + .expect("Creating parent dir failed"); + let mut file = File::create(path)?; serde_cbor::to_writer(&mut file, obj).map_err(|err| Error::new(ErrorKind::Other, err)) } diff --git a/editor/src/plugins/draw_polygon.rs b/editor/src/plugins/draw_polygon.rs index 667707023f..b151b063b1 100644 --- a/editor/src/plugins/draw_polygon.rs +++ b/editor/src/plugins/draw_polygon.rs @@ -1,5 +1,7 @@ +use abstutil; use ezgui::{Canvas, GfxCtx, TextBox, TextOSD, UserInput}; use geom::{Circle, Line, Polygon, Pt2D}; +use map_model::Map; use piston::input::{Button, Key, ReleaseEvent}; use plugins::Colorizer; @@ -18,7 +20,7 @@ impl DrawPolygonState { DrawPolygonState::Empty } - pub fn event(&mut self, input: &mut UserInput, canvas: &Canvas) -> bool { + pub fn event(&mut self, input: &mut UserInput, canvas: &Canvas, map: &Map) -> bool { let mut new_state: Option = None; match self { DrawPolygonState::Empty => { @@ -61,7 +63,15 @@ impl DrawPolygonState { } DrawPolygonState::NamingPolygon(tb, pts) => { if tb.event(input.use_event_directly()) { - println!("TODO: save neighborhood {} with points {:?}", tb.line, pts); + let path = format!("../data/polygons/{}/{}", map.get_name(), tb.line); + abstutil::write_json( + &path, + &PolygonSelection { + name: tb.line.clone(), + points: pts.clone(), + }, + ).expect("Saving polygon selection failed"); + println!("Saved {}", path); new_state = Some(DrawPolygonState::Empty); } input.consume_event(); @@ -87,7 +97,7 @@ impl DrawPolygonState { // TODO add colorscheme entries let red = [1.0, 0.0, 0.0, 1.0]; let green = [0.0, 1.0, 0.0, 1.0]; - let blue = [0.0, 0.0, 1.0, 1.0]; + let blue = [0.0, 0.0, 1.0, 0.6]; let cyan = [0.0, 1.0, 1.0, 1.0]; let (pts, current_idx) = match self { @@ -121,3 +131,9 @@ impl DrawPolygonState { } impl Colorizer for DrawPolygonState {} + +#[derive(Serialize, Deserialize, Debug)] +struct PolygonSelection { + name: String, + points: Vec, +} diff --git a/editor/src/ui.rs b/editor/src/ui.rs index d395161b73..275169eb46 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -236,7 +236,7 @@ impl UIWrapper { .event(input, &mut ui.canvas, &ui.map, &ui.draw_map) }), Box::new(|ui, input| ui.turn_cycler.event(input, ui.current_selection)), - Box::new(|ui, input| ui.draw_polygon.event(input, &ui.canvas)), + Box::new(|ui, input| ui.draw_polygon.event(input, &ui.canvas, &ui.map)), ], } } diff --git a/sim/src/sim.rs b/sim/src/sim.rs index b74e0ea6a5..24680939e2 100644 --- a/sim/src/sim.rs +++ b/sim/src/sim.rs @@ -326,8 +326,6 @@ impl Sim { self.scenario_name, self.time.as_filename() ); - std::fs::create_dir_all(std::path::Path::new(&path).parent().unwrap()) - .expect("Creating parent dir failed"); abstutil::write_json(&path, &self).expect("Writing sim state failed"); println!("Saved to {}", path); path