edit existing polygons

This commit is contained in:
Dustin Carlino 2020-05-08 12:01:08 -07:00
parent f403e0c65c
commit e97795044d
4 changed files with 56 additions and 10 deletions

View File

@ -153,11 +153,6 @@ pub fn path_pending_screenshots(map_name: &str) -> String {
format!("../data/input/screenshots/pending_{}", map_name)
}
// TODO Few callers, and importer just manually builds this path anyway
pub fn path_polygon(city: &str, polygon_name: &str) -> String {
format!("../data/input/{}/polygons/{}.poly", city, polygon_name)
}
pub fn path_popdat() -> String {
format!("../data/input/seattle/popdat.bin")
}

View File

@ -12,21 +12,32 @@ pub struct DevToolsMode;
impl DevToolsMode {
pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State> {
ManagedGUIState::over_map(
ManagedGUIState::fullscreen(
WrappedComposite::new(WrappedComposite::quick_menu(
ctx,
app,
"Internal dev tools",
vec![],
vec![
(hotkey(Key::E), "edit a polygon"),
(hotkey(Key::P), "draw a polygon"),
(hotkey(Key::W), "load scenario"),
],
))
.cb("X", Box::new(|_, _| Some(Transition::Pop)))
.cb(
"edit a polygon",
Box::new(|_, _| Some(Transition::Push(WizardState::new(Box::new(choose_polygon))))),
)
.cb(
"draw a polygon",
Box::new(|ctx, app| Some(Transition::Push(polygon::PolygonEditor::new(ctx, app)))),
Box::new(|ctx, app| {
Some(Transition::Push(polygon::PolygonEditor::new(
ctx,
app,
Vec::new(),
)))
}),
)
.cb(
"load scenario",
@ -49,3 +60,17 @@ fn load_scenario(wiz: &mut Wizard, ctx: &mut EventCtx, app: &mut App) -> Option<
scenario::ScenarioManager::new(scenario, ctx, app),
)))
}
fn choose_polygon(wiz: &mut Wizard, ctx: &mut EventCtx, app: &mut App) -> Option<Transition> {
// TODO Sorry, Seattle only right now
let name = wiz.wrap(ctx).choose_string("Edit which polygon?", || {
abstutil::list_all_objects("../data/input/seattle/polygons/".to_string())
})?;
match polygon::read_from_osmosis(format!("../data/input/seattle/polygons/{}.poly", name)) {
Ok(pts) => Some(Transition::Push(polygon::PolygonEditor::new(ctx, app, pts))),
Err(err) => {
println!("Bad polygon {}: {}", name, err);
Some(Transition::Pop)
}
}
}

View File

@ -5,7 +5,7 @@ use crate::managed::WrappedComposite;
use ezgui::{hotkey, Color, Composite, EventCtx, GfxCtx, Key, Line, Outcome, Text};
use geom::{Circle, Distance, LonLat, Polygon, Pt2D};
use std::fs::File;
use std::io::{Error, Write};
use std::io::{BufRead, BufReader, Error, ErrorKind, Write};
const POINT_RADIUS: Distance = Distance::const_meters(10.0);
// Localized and internal, so don't put in ColorScheme.
@ -22,7 +22,7 @@ pub struct PolygonEditor {
}
impl PolygonEditor {
pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State> {
pub fn new(ctx: &mut EventCtx, app: &App, points: Vec<LonLat>) -> Box<dyn State> {
Box::new(PolygonEditor {
composite: WrappedComposite::quick_menu(
ctx,
@ -31,7 +31,7 @@ impl PolygonEditor {
vec![],
vec![(hotkey(Key::X), "export as an Osmosis polygon filter")],
),
points: Vec::new(),
points,
mouseover_pt: None,
moving_pt: false,
})
@ -163,3 +163,28 @@ fn save_as_osmosis(pts: &Vec<LonLat>) -> Result<(), Error> {
println!("Exported {}", path);
Ok(())
}
pub fn read_from_osmosis(path: String) -> Result<Vec<LonLat>, Error> {
let f = File::open(&path)?;
let mut pts = Vec::new();
for (idx, line) in BufReader::new(f).lines().enumerate() {
if idx < 2 {
continue;
}
let line = line?;
if line == "END" {
break;
}
let parts = line.trim().split(" ").collect::<Vec<_>>();
pts.push(LonLat::new(
parts[0]
.parse::<f64>()
.map_err(|err| Error::new(ErrorKind::Other, err))?,
parts[1]
.parse::<f64>()
.map_err(|err| Error::new(ErrorKind::Other, err))?,
));
}
pts.pop();
Ok(pts)
}

View File

@ -135,6 +135,7 @@ impl Manifest {
let path = entry.path().display().to_string();
if path.contains("system/assets/")
|| path.contains("system/fonts")
|| path.contains("system/proposals")
|| path.contains("system/synthetic_maps")
|| path.contains("/polygons/")
{