mirror of
https://github.com/a-b-street/abstreet.git
synced 2025-01-04 04:23:25 +03:00
moving selectable polygons to common place
This commit is contained in:
parent
726adb6249
commit
2c8edd07f8
@ -30,6 +30,7 @@ mod colors;
|
|||||||
mod kml;
|
mod kml;
|
||||||
mod objects;
|
mod objects;
|
||||||
mod plugins;
|
mod plugins;
|
||||||
|
mod polygons;
|
||||||
mod render;
|
mod render;
|
||||||
mod ui;
|
mod ui;
|
||||||
|
|
||||||
|
@ -4,8 +4,8 @@ use geom::{Circle, Line, Polygon, Pt2D};
|
|||||||
use map_model::Map;
|
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 polygons;
|
||||||
use std::collections::HashMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
const POINT_RADIUS: f64 = 2.0;
|
const POINT_RADIUS: f64 = 2.0;
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ pub enum DrawPolygonState {
|
|||||||
MovingPoint(Vec<Pt2D>, usize, String),
|
MovingPoint(Vec<Pt2D>, usize, String),
|
||||||
NamingPolygon(TextBox, Vec<Pt2D>),
|
NamingPolygon(TextBox, Vec<Pt2D>),
|
||||||
// String name to each choice, pre-loaded
|
// String name to each choice, pre-loaded
|
||||||
ListingPolygons(Menu, HashMap<String, PolygonSelection>),
|
ListingPolygons(Menu, BTreeMap<String, polygons::PolygonSelection>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DrawPolygonState {
|
impl DrawPolygonState {
|
||||||
@ -48,12 +48,12 @@ impl DrawPolygonState {
|
|||||||
osd.add_line(format!("Currently editing {}", name));
|
osd.add_line(format!("Currently editing {}", name));
|
||||||
|
|
||||||
if input.key_pressed(Key::Tab, "list existing polygons") {
|
if input.key_pressed(Key::Tab, "list existing polygons") {
|
||||||
let (names, polygons) = load_all_polygons(map.get_name());
|
let polygons = polygons::load_all_polygons(map.get_name());
|
||||||
if names.is_empty() {
|
if polygons.is_empty() {
|
||||||
println!("Sorry, no existing polygons");
|
println!("Sorry, no existing polygons");
|
||||||
} else {
|
} else {
|
||||||
new_state = Some(DrawPolygonState::ListingPolygons(
|
new_state = Some(DrawPolygonState::ListingPolygons(
|
||||||
Menu::new(names),
|
Menu::new(polygons.keys().cloned().collect()),
|
||||||
polygons,
|
polygons,
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@ -108,7 +108,7 @@ impl DrawPolygonState {
|
|||||||
let path = format!("../data/polygons/{}/{}", map.get_name(), tb.line);
|
let path = format!("../data/polygons/{}/{}", map.get_name(), tb.line);
|
||||||
abstutil::write_json(
|
abstutil::write_json(
|
||||||
&path,
|
&path,
|
||||||
&PolygonSelection {
|
&polygons::PolygonSelection {
|
||||||
name: tb.line.clone(),
|
name: tb.line.clone(),
|
||||||
points: pts.clone(),
|
points: pts.clone(),
|
||||||
},
|
},
|
||||||
@ -188,23 +188,3 @@ impl DrawPolygonState {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Colorizer for DrawPolygonState {}
|
impl Colorizer for DrawPolygonState {}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
|
||||||
pub struct PolygonSelection {
|
|
||||||
name: String,
|
|
||||||
points: Vec<Pt2D>,
|
|
||||||
}
|
|
||||||
|
|
||||||
fn load_all_polygons(map_name: &str) -> (Vec<String>, HashMap<String, PolygonSelection>) {
|
|
||||||
let mut names: Vec<String> = Vec::new();
|
|
||||||
let mut polygons: HashMap<String, PolygonSelection> = 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)
|
|
||||||
}
|
|
||||||
|
22
editor/src/polygons.rs
Normal file
22
editor/src/polygons.rs
Normal file
@ -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<Pt2D>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load_all_polygons(map_name: &str) -> BTreeMap<String, PolygonSelection> {
|
||||||
|
let mut results: BTreeMap<String, PolygonSelection> = 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user