saving polygon areas

This commit is contained in:
Dustin Carlino 2018-09-19 07:02:17 -07:00
parent ce91b77ad6
commit bcc65941f3
5 changed files with 27 additions and 6 deletions

1
.gitignore vendored
View File

@ -5,5 +5,6 @@
editor/editor_state
editor/road_edits.json
data/*.abst
data/polygons/*
data/input/*
data/save/*

View File

@ -16,6 +16,9 @@ pub fn to_json<T: Serialize>(obj: &T) -> String {
}
pub fn write_json<T: Serialize>(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<T: DeserializeOwned>(path: &str) -> Result<T, Error> {
}
pub fn write_binary<T: Serialize>(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))
}

View File

@ -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<DrawPolygonState> = 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<Pt2D>,
}

View File

@ -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)),
],
}
}

View File

@ -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