refactoring focus pt per object

This commit is contained in:
Dustin Carlino 2018-10-08 15:56:30 -07:00
parent fcf0366e90
commit 7ca7f08ef7
7 changed files with 77 additions and 110 deletions

View File

@ -1,12 +1,13 @@
use colors::ColorScheme;
use control::ControlMap;
use ezgui::Canvas;
use geom::Pt2D;
use kml::ExtraShapeID;
use map_model::{AreaID, BuildingID, BusStopID, IntersectionID, LaneID, Map, ParcelID, TurnID};
use render::DrawMap;
use sim::{CarID, PedestrianID, Sim};
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug)]
#[derive(Clone, Copy, Hash, PartialEq, Eq, Debug, PartialOrd, Ord)]
pub enum ID {
Lane(LaneID),
Intersection(IntersectionID),
@ -53,6 +54,24 @@ impl ID {
}
}
pub fn canonical_point(&self, map: &Map, sim: &Sim, draw_map: &DrawMap) -> Option<Pt2D> {
match *self {
ID::Lane(id) => map.maybe_get_l(id).map(|l| l.first_pt()),
ID::Intersection(id) => map.maybe_get_i(id).map(|i| i.point),
ID::Turn(id) => map.maybe_get_i(id.parent).map(|i| i.point),
ID::Building(id) => map.maybe_get_b(id).map(|b| Pt2D::center(&b.points)),
ID::Car(id) => sim.get_draw_car(id, map).map(|c| c.front),
ID::Pedestrian(id) => sim.get_draw_ped(id, map).map(|p| p.pos),
// TODO maybe_get_es
ID::ExtraShape(id) => Some(draw_map.get_es(id).center()),
ID::Parcel(id) => map.maybe_get_p(id).map(|p| Pt2D::center(&p.points)),
ID::BusStop(id) => map
.maybe_get_bs(id)
.map(|bs| map.get_l(id.sidewalk).dist_along(bs.dist_along).0),
ID::Area(id) => map.maybe_get_a(id).map(|a| Pt2D::center(&a.points)),
}
}
pub fn tooltip_lines(&self, map: &Map, draw_map: &DrawMap, sim: &Sim) -> Vec<String> {
match *self {
ID::Car(id) => sim.car_tooltip(id),

View File

@ -2,21 +2,13 @@ use ezgui::{Canvas, UserInput};
use generator;
use geo;
use geo::prelude::Intersects;
use geom::{Polygon, Pt2D};
use map_model::{BuildingID, IntersectionID, LaneID, Map, ParcelID};
use objects::DEBUG;
use geom::Polygon;
use map_model::Map;
use objects::{DEBUG, ID};
use piston::input::Key;
use plugins::Colorizer;
use render::DrawMap;
// TODO just have one of these
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug)]
pub enum ID {
Lane(LaneID),
Intersection(IntersectionID),
Building(BuildingID),
Parcel(ParcelID),
}
use sim::Sim;
// Eventually this should be part of an interactive map fixing pipeline. Find problems, jump to
// them, ask for the resolution, record it.
@ -95,6 +87,7 @@ impl Validator {
input: &mut UserInput,
canvas: &mut Canvas,
map: &Map,
sim: &Sim,
draw_map: &DrawMap,
) -> bool {
let mut new_state: Option<Validator> = None;
@ -115,7 +108,7 @@ impl Validator {
if let Some((id1, id2)) = current_problem {
info!("{:?} and {:?} intersect", id1, id2);
canvas.center_on_map_pt(get_pt(map, *id1));
canvas.center_on_map_pt(id1.canonical_point(map, sim, draw_map).unwrap());
// TODO also modify selection state to highlight stuff?
} else {
info!("No more problems!");
@ -150,13 +143,3 @@ fn make_polys(p: &Polygon) -> Vec<geo::Polygon<f64>> {
}
result
}
// TODO duplicated with warp. generic handling of object types?
fn get_pt(map: &Map, id: ID) -> Pt2D {
match id {
ID::Lane(id) => map.get_l(id).first_pt(),
ID::Intersection(id) => map.get_i(id).point,
ID::Building(id) => Pt2D::center(&map.get_b(id).points),
ID::Parcel(id) => Pt2D::center(&map.get_p(id).points),
}
}

View File

@ -1,9 +1,9 @@
use ezgui::{Canvas, GfxCtx, InputResult, TextBox, UserInput};
use geom::Pt2D;
use map_model::{AreaID, BuildingID, IntersectionID, LaneID, Map, ParcelID, RoadID};
use objects::{DEBUG, ID};
use piston::input::Key;
use plugins::Colorizer;
use render::DrawMap;
use sim::{CarID, PedestrianID, Sim};
use std::usize;
@ -18,6 +18,7 @@ impl WarpState {
input: &mut UserInput,
map: &Map,
sim: &Sim,
draw_map: &DrawMap,
canvas: &mut Canvas,
selected: &mut Option<ID>,
) -> bool {
@ -37,7 +38,7 @@ impl WarpState {
new_state = Some(WarpState::Empty);
}
InputResult::Done(to, _) => {
warp(to, map, sim, canvas, selected);
warp(to, map, sim, draw_map, canvas, selected);
new_state = Some(WarpState::Empty);
}
InputResult::StillActive => {}
@ -61,103 +62,37 @@ impl WarpState {
impl Colorizer for WarpState {}
fn warp(line: String, map: &Map, sim: &Sim, canvas: &mut Canvas, selected: &mut Option<ID>) {
fn warp(
line: String,
map: &Map,
sim: &Sim,
draw_map: &DrawMap,
canvas: &mut Canvas,
selected: &mut Option<ID>,
) {
if line.is_empty() {
return;
}
let pt = match usize::from_str_radix(&line[1..line.len()], 10) {
// TODO express this more succinctly
let id = match usize::from_str_radix(&line[1..line.len()], 10) {
Ok(idx) => match line.chars().next().unwrap() {
'r' => {
let id = RoadID(idx);
if let Some(r) = map.maybe_get_r(id) {
let l = map.get_l(r.children_forwards[0].0);
info!("Warping to {}, which belongs to {}", l.id, id);
*selected = Some(ID::Lane(l.id));
l.first_pt()
} else {
warn!("{} doesn't exist", id);
return;
}
}
'l' => {
let id = LaneID(idx);
if let Some(l) = map.maybe_get_l(id) {
info!("Warping to {}", id);
*selected = Some(ID::Lane(id));
l.first_pt()
} else {
warn!("{} doesn't exist", id);
return;
}
}
'i' => {
let id = IntersectionID(idx);
if let Some(i) = map.maybe_get_i(id) {
info!("Warping to {}", id);
*selected = Some(ID::Intersection(id));
i.point
} else {
warn!("{} doesn't exist", id);
return;
}
}
'b' => {
let id = BuildingID(idx);
if let Some(b) = map.maybe_get_b(id) {
info!("Warping to {}", id);
*selected = Some(ID::Building(id));
Pt2D::center(&b.points)
} else {
warn!("{} doesn't exist", id);
return;
}
}
'a' => {
let id = AreaID(idx);
if let Some(a) = map.maybe_get_a(id) {
info!("Warping to {}", id);
*selected = Some(ID::Area(id));
Pt2D::center(&a.points)
ID::Lane(r.children_forwards[0].0)
} else {
warn!("{} doesn't exist", id);
return;
}
}
'l' => ID::Lane(LaneID(idx)),
'i' => ID::Intersection(IntersectionID(idx)),
'b' => ID::Building(BuildingID(idx)),
'a' => ID::Area(AreaID(idx)),
// TODO ideally "pa" prefix?
'e' => {
let id = ParcelID(idx);
if let Some(p) = map.maybe_get_p(id) {
info!("Warping to {}", id);
Pt2D::center(&p.points)
} else {
warn!("{} doesn't exist", id);
return;
}
}
'p' => {
let id = PedestrianID(idx);
if let Some(p) = sim.get_draw_ped(id, map) {
info!("Warping to {}", id);
*selected = Some(ID::Pedestrian(id));
p.pos
} else {
warn!("{} doesn't exist", id);
return;
}
}
'c' => {
let id = CarID(idx);
if let Some(c) = sim.get_draw_car(id, map) {
info!("Warping to {}", id);
*selected = Some(ID::Car(id));
c.front
} else {
warn!("{} doesn't exist", id);
return;
}
}
'e' => ID::Parcel(ParcelID(idx)),
'p' => ID::Pedestrian(PedestrianID(idx)),
'c' => ID::Car(CarID(idx)),
_ => {
warn!("{} isn't a valid ID; Should be [libepc][0-9]+", line);
return;
@ -167,5 +102,11 @@ fn warp(line: String, map: &Map, sim: &Sim, canvas: &mut Canvas, selected: &mut
return;
}
};
canvas.center_on_map_pt(pt);
if let Some(pt) = id.canonical_point(map, sim, draw_map) {
info!("Warping to {:?}", id);
*selected = Some(id);
canvas.center_on_map_pt(pt);
} else {
warn!("{:?} doesn't exist", id);
}
}

View File

@ -35,6 +35,13 @@ impl DrawExtraShape {
attributes: s.attributes,
}
}
pub fn center(&self) -> Pt2D {
match self.shape {
Shape::Polygon(ref p) => Pt2D::center(&p.points()),
Shape::Circle(ref c) => c.center,
}
}
}
impl Renderable for DrawExtraShape {

View File

@ -163,6 +163,7 @@ impl UIWrapper {
ctx.input,
&ctx.ui.primary.map,
&ctx.ui.primary.sim,
&ctx.ui.primary.draw_map,
&mut ctx.ui.canvas,
&mut ctx.ui.primary.current_selection,
)
@ -217,6 +218,7 @@ impl UIWrapper {
ctx.input,
&mut ctx.ui.canvas,
&ctx.ui.primary.map,
&ctx.ui.primary.sim,
&ctx.ui.primary.draw_map,
)
}),

View File

@ -117,6 +117,17 @@ impl Polygon {
}
}
// Lots of repeats...
pub fn points(&self) -> Vec<Pt2D> {
let mut points = Vec::new();
for t in &self.triangles {
points.push(t.pt1);
points.push(t.pt2);
points.push(t.pt3);
}
points
}
pub fn regular_polygon(center: Pt2D, sides: usize, length: f64) -> Polygon {
let mut pts = Vec::new();
for i in 0..sides {

View File

@ -289,6 +289,10 @@ impl Map {
self.areas.get(id.0)
}
pub fn maybe_get_bs(&self, id: BusStopID) -> Option<&BusStop> {
self.bus_stops.get(&id)
}
pub fn get_r(&self, id: RoadID) -> &Road {
&self.roads[id.0]
}