show current selected polygon as preview

This commit is contained in:
Dustin Carlino 2018-09-19 18:35:05 -07:00
parent c7f2c50e25
commit 4b26411794
2 changed files with 32 additions and 25 deletions

View File

@ -5,17 +5,19 @@ use map_model::Map;
use piston::input::{Button, Key, ReleaseEvent}; use piston::input::{Button, Key, ReleaseEvent};
use plugins::Colorizer; use plugins::Colorizer;
use std; use std;
use std::collections::HashMap;
const POINT_RADIUS: f64 = 2.0; const POINT_RADIUS: f64 = 2.0;
pub enum DrawPolygonState { pub(crate) enum DrawPolygonState {
Empty, Empty,
// Option<usize> is the point currently being hovered over, String is the possibly empty // Option<usize> is the point currently being hovered over, String is the possibly empty
// pre-chosen name // pre-chosen name
DrawingPoints(Vec<Pt2D>, Option<usize>, String), DrawingPoints(Vec<Pt2D>, Option<usize>, String),
MovingPoint(Vec<Pt2D>, usize, String), MovingPoint(Vec<Pt2D>, usize, String),
NamingPolygon(TextBox, Vec<Pt2D>), NamingPolygon(TextBox, Vec<Pt2D>),
ListingPolygons(Menu), // String name to each choice, pre-loaded
ListingPolygons(Menu, HashMap<String, PolygonSelection>),
} }
impl DrawPolygonState { impl DrawPolygonState {
@ -37,11 +39,14 @@ impl DrawPolygonState {
} }
DrawPolygonState::DrawingPoints(ref mut pts, ref mut current_idx, name) => { DrawPolygonState::DrawingPoints(ref mut pts, ref mut current_idx, name) => {
if input.key_pressed(Key::Tab, "list existing polygons") { if input.key_pressed(Key::Tab, "list existing polygons") {
let list = list_polygons(map.get_name()).expect("couldn't list polygons"); let (names, polygons) = load_all_polygons(map.get_name());
if list.is_empty() { if names.is_empty() {
println!("Oops, no existing polygons"); println!("Sorry, no existing polygons");
} else { } else {
new_state = Some(DrawPolygonState::ListingPolygons(Menu::new(list))); new_state = Some(DrawPolygonState::ListingPolygons(
Menu::new(names),
polygons,
));
} }
} else if input.key_pressed(Key::Escape, "throw away this neighborhood polygon") { } else if input.key_pressed(Key::Escape, "throw away this neighborhood polygon") {
new_state = Some(DrawPolygonState::Empty); new_state = Some(DrawPolygonState::Empty);
@ -101,22 +106,17 @@ impl DrawPolygonState {
} }
input.consume_event(); input.consume_event();
} }
DrawPolygonState::ListingPolygons(ref mut menu) => { DrawPolygonState::ListingPolygons(ref mut menu, polygons) => {
match menu.event(input.use_event_directly()) { match menu.event(input.use_event_directly()) {
MenuResult::Canceled => { MenuResult::Canceled => {
new_state = Some(DrawPolygonState::Empty); new_state = Some(DrawPolygonState::Empty);
} }
MenuResult::StillActive => {} MenuResult::StillActive => {}
MenuResult::Done(choice) => { MenuResult::Done(choice) => {
let load: PolygonSelection = abstutil::read_json(&format!(
"../data/polygons/{}/{}",
map.get_name(),
choice
)).unwrap();
new_state = Some(DrawPolygonState::DrawingPoints( new_state = Some(DrawPolygonState::DrawingPoints(
load.points, polygons[&choice].points.clone(),
None, None,
load.name, choice,
)); ));
} }
}; };
@ -163,10 +163,9 @@ impl DrawPolygonState {
g.draw_polygon(blue, &Polygon::new(pts)); g.draw_polygon(blue, &Polygon::new(pts));
return; return;
} }
DrawPolygonState::ListingPolygons(menu) => { DrawPolygonState::ListingPolygons(menu, polygons) => {
canvas.draw_centered_text(g, menu.get_osd()); canvas.draw_centered_text(g, menu.get_osd());
// TODO display the current polygon (&polygons[menu.current_choice()].points, None)
return;
} }
}; };
@ -191,17 +190,21 @@ impl DrawPolygonState {
impl Colorizer for DrawPolygonState {} impl Colorizer for DrawPolygonState {}
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
struct PolygonSelection { pub(crate) struct PolygonSelection {
name: String, name: String,
points: Vec<Pt2D>, points: Vec<Pt2D>,
} }
fn list_polygons(map_name: &str) -> Result<Vec<String>, std::io::Error> { fn load_all_polygons(map_name: &str) -> (Vec<String>, HashMap<String, PolygonSelection>) {
let mut results: Vec<String> = Vec::new(); let mut names: Vec<String> = Vec::new();
for entry in std::fs::read_dir(format!("../data/polygons/{}/", map_name))? { let mut polygons: HashMap<String, PolygonSelection> = HashMap::new();
let entry = entry?; for entry in std::fs::read_dir(format!("../data/polygons/{}/", map_name)).unwrap() {
results.push(entry.file_name().into_string().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);
} }
results.sort(); names.sort();
Ok(results) (names, polygons)
} }

View File

@ -57,4 +57,8 @@ impl Menu {
} }
osd osd
} }
pub fn current_choice(&self) -> &String {
&self.choices[self.current_idx]
}
} }