From 2c8edd07f8a25712b81bcc881d41c1c577e9ee58 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 20 Sep 2018 17:22:07 -0700 Subject: [PATCH] moving selectable polygons to common place --- editor/src/main.rs | 1 + editor/src/plugins/draw_polygon.rs | 34 ++++++------------------------ editor/src/polygons.rs | 22 +++++++++++++++++++ 3 files changed, 30 insertions(+), 27 deletions(-) create mode 100644 editor/src/polygons.rs diff --git a/editor/src/main.rs b/editor/src/main.rs index b2bd5220f4..8c4d59ef3f 100644 --- a/editor/src/main.rs +++ b/editor/src/main.rs @@ -30,6 +30,7 @@ mod colors; mod kml; mod objects; mod plugins; +mod polygons; mod render; mod ui; diff --git a/editor/src/plugins/draw_polygon.rs b/editor/src/plugins/draw_polygon.rs index 8244cb57c4..6e0ea4bbca 100644 --- a/editor/src/plugins/draw_polygon.rs +++ b/editor/src/plugins/draw_polygon.rs @@ -4,8 +4,8 @@ use geom::{Circle, Line, Polygon, Pt2D}; use map_model::Map; use piston::input::{Button, Key, ReleaseEvent}; use plugins::Colorizer; -use std; -use std::collections::HashMap; +use polygons; +use std::collections::BTreeMap; const POINT_RADIUS: f64 = 2.0; @@ -17,7 +17,7 @@ pub enum DrawPolygonState { MovingPoint(Vec, usize, String), NamingPolygon(TextBox, Vec), // String name to each choice, pre-loaded - ListingPolygons(Menu, HashMap), + ListingPolygons(Menu, BTreeMap), } impl DrawPolygonState { @@ -48,12 +48,12 @@ impl DrawPolygonState { osd.add_line(format!("Currently editing {}", name)); if input.key_pressed(Key::Tab, "list existing polygons") { - let (names, polygons) = load_all_polygons(map.get_name()); - if names.is_empty() { + let polygons = polygons::load_all_polygons(map.get_name()); + if polygons.is_empty() { println!("Sorry, no existing polygons"); } else { new_state = Some(DrawPolygonState::ListingPolygons( - Menu::new(names), + Menu::new(polygons.keys().cloned().collect()), polygons, )); } @@ -108,7 +108,7 @@ impl DrawPolygonState { let path = format!("../data/polygons/{}/{}", map.get_name(), tb.line); abstutil::write_json( &path, - &PolygonSelection { + &polygons::PolygonSelection { name: tb.line.clone(), points: pts.clone(), }, @@ -188,23 +188,3 @@ impl DrawPolygonState { } impl Colorizer for DrawPolygonState {} - -#[derive(Serialize, Deserialize, Debug)] -pub struct PolygonSelection { - name: String, - points: Vec, -} - -fn load_all_polygons(map_name: &str) -> (Vec, HashMap) { - let mut names: Vec = Vec::new(); - let mut polygons: HashMap = HashMap::new(); - for entry in std::fs::read_dir(format!("../data/polygons/{}/", map_name)).unwrap() { - let name = entry.unwrap().file_name().into_string().unwrap(); - names.push(name.clone()); - let load: PolygonSelection = - abstutil::read_json(&format!("../data/polygons/{}/{}", map_name, name)).unwrap(); - polygons.insert(name, load); - } - names.sort(); - (names, polygons) -} diff --git a/editor/src/polygons.rs b/editor/src/polygons.rs new file mode 100644 index 0000000000..dce9d3e616 --- /dev/null +++ b/editor/src/polygons.rs @@ -0,0 +1,22 @@ +use abstutil; +use geom::Pt2D; +use std; +use std::collections::BTreeMap; + +// Named polygonal regions +#[derive(Serialize, Deserialize, Debug)] +pub struct PolygonSelection { + pub name: String, + pub points: Vec, +} + +pub fn load_all_polygons(map_name: &str) -> BTreeMap { + let mut results: BTreeMap = BTreeMap::new(); + for entry in std::fs::read_dir(format!("../data/polygons/{}/", map_name)).unwrap() { + let name = entry.unwrap().file_name().into_string().unwrap(); + let load: PolygonSelection = + abstutil::read_json(&format!("../data/polygons/{}/{}", map_name, name)).unwrap(); + results.insert(name, load); + } + results +}