add/delete turn restrictions, without saving in fixes yet. adding is

buggy.
This commit is contained in:
Dustin Carlino 2019-09-24 18:35:57 -07:00
parent 02858944f0
commit dd9827df72
3 changed files with 124 additions and 22 deletions

View File

@ -4,6 +4,7 @@ use abstutil::CmdArgs;
use ezgui::{Color, EventCtx, EventLoopMode, GfxCtx, Key, Line, Text, Wizard, GUI};
use geom::{Distance, Line, Polygon, Pt2D};
use map_model::raw::{StableBuildingID, StableIntersectionID, StableRoadID};
use map_model::LANE_THICKNESS;
use model::{Direction, Model, ID};
use std::process;
@ -27,6 +28,8 @@ enum State {
SavingModel(Wizard),
// bool is if key is down
SelectingRectangle(Pt2D, Pt2D, bool),
CreatingTurnRestrictionPt1(StableRoadID),
CreatingTurnRestrictionPt2(StableRoadID, StableRoadID, Wizard),
}
impl UI {
@ -213,6 +216,11 @@ impl GUI for UI {
} else if ctx.input.key_pressed(Key::M, "merge road") {
self.model.merge_r(r, ctx.prerender);
self.model.handle_mouseover(ctx);
} else if ctx
.input
.key_pressed(Key::R, "create turn restriction from here")
{
self.state = State::CreatingTurnRestrictionPt1(r);
}
} else if let Some(ID::RoadPoint(r, idx)) = self.model.get_selection() {
if ctx.input.key_pressed(Key::LeftControl, "move point") {
@ -221,6 +229,15 @@ impl GUI for UI {
self.model.delete_r_pt(r, idx, ctx.prerender);
self.model.handle_mouseover(ctx);
}
} else if let Some(ID::TurnRestriction(from, to, idx)) = self.model.get_selection()
{
if ctx
.input
.key_pressed(Key::Backspace, "delete turn restriction")
{
self.model.delete_tr(from, to, idx, ctx.prerender);
self.model.handle_mouseover(ctx);
}
} else if ctx.input.unimportant_key_pressed(Key::Escape, "quit") {
process::exit(0);
} else if ctx.input.key_pressed(Key::S, "save") {
@ -272,6 +289,25 @@ impl GUI for UI {
self.state = State::Viewing;
}
}
State::CreatingTurnRestrictionPt1(from) => {
if let Some(ID::Lane(to, _, _)) = self.model.get_selection() {
if ctx
.input
.key_pressed(Key::R, "create turn restriction to here")
{
self.state = State::CreatingTurnRestrictionPt2(from, to, Wizard::new());
}
}
}
State::CreatingTurnRestrictionPt2(from, to, ref mut wizard) => {
// TODO choose from enums
if let Some(restriction) = wizard.wrap(ctx).input_string("What turn restriction?") {
self.model.add_tr(from, restriction, to, ctx.prerender);
self.state = State::Viewing;
} else if wizard.aborted() {
self.state = State::Viewing;
}
}
}
self.osd = Text::new();
@ -308,12 +344,12 @@ impl GUI for UI {
Line(v).fg(Color::CYAN),
]);
}
for (restriction, dst) in self.model.get_turn_restrictions(id) {
for (_, restriction, dst) in self.model.get_turn_restrictions(id) {
txt.add_appended(vec![
Line("Restriction: "),
Line(restriction).fg(Color::RED),
Line(" to "),
Line(dst).fg(Color::CYAN),
Line(format!("way {}", dst)).fg(Color::CYAN),
]);
}
g.draw_blocking_text(
@ -333,6 +369,21 @@ impl GUI for UI {
g.draw_polygon(Color::BLUE.alpha(0.5), &rect);
}
}
State::CreatingTurnRestrictionPt1(from) => {
if let Some(cursor) = g.get_cursor_in_map_space() {
if let Some(l) = Line::maybe_new(self.model.get_r_center(from), cursor) {
g.draw_arrow(Color::PURPLE, LANE_THICKNESS, &l);
}
}
}
State::CreatingTurnRestrictionPt2(from, to, ref wizard) => {
if let Some(l) =
Line::maybe_new(self.model.get_r_center(from), self.model.get_r_center(to))
{
g.draw_arrow(Color::PURPLE, LANE_THICKNESS, &l);
}
wizard.draw(g);
}
};
g.draw_blocking_text(&self.osd, ezgui::BOTTOM_LEFT);

View File

@ -519,7 +519,7 @@ impl Model {
result.push(obj.tooltip(tooltip.clone()));
}
for (idx, (restriction, to)) in self.map.get_turn_restrictions(id).into_iter().enumerate() {
for (turn_id, restriction, to) in self.get_turn_restrictions(id) {
let polygon = if id == to {
// TODO Ideally a hollow circle with an arrow
Circle::new(
@ -528,17 +528,13 @@ impl Model {
)
.to_polygon()
} else {
PolyLine::new(vec![
PolyLine::new(self.map.roads[&id].center_points.clone()).middle(),
PolyLine::new(self.map.roads[&to].center_points.clone()).middle(),
])
.make_arrow(LANE_THICKNESS)
.unwrap()
PolyLine::new(vec![self.get_r_center(id), self.get_r_center(to)])
.make_arrow(LANE_THICKNESS)
.unwrap()
};
result.push(
Object::new(ID::TurnRestriction(id, to, idx), Color::PURPLE, polygon)
.tooltip(Text::from(Line(restriction))),
Object::new(turn_id, Color::PURPLE, polygon).tooltip(Text::from(Line(restriction))),
);
}
@ -639,22 +635,51 @@ impl Model {
}
}
pub fn get_turn_restrictions(&self, id: StableRoadID) -> Vec<(String, String)> {
pub fn get_r_center(&self, id: StableRoadID) -> Pt2D {
PolyLine::new(self.map.roads[&id].center_points.clone()).middle()
}
}
// Turn restrictions
impl Model {
pub fn get_turn_restrictions(&self, id: StableRoadID) -> Vec<(ID, String, StableRoadID)> {
self.map
.get_turn_restrictions(id)
.into_iter()
.map(|(r, to)| {
let tags = &self.map.roads[&to].osm_tags;
(
r,
tags.get(osm::NAME)
.or_else(|| tags.get("ref"))
.cloned()
.unwrap_or_else(|| format!("way {}", to)),
)
})
.enumerate()
.map(|(idx, (r, to))| (ID::TurnRestriction(id, to, idx), r, to))
.collect()
}
pub fn add_tr(
&mut self,
from: StableRoadID,
restriction: String,
to: StableRoadID,
prerender: &Prerender,
) {
self.road_deleted(from);
self.map.add_turn_restriction(from, restriction, to);
self.road_added(from, prerender);
}
pub fn delete_tr(
&mut self,
from: StableRoadID,
to: StableRoadID,
idx: usize,
prerender: &Prerender,
) {
self.road_deleted(from);
let (_, ref restriction, _) = self.get_turn_restrictions(from)[idx];
self.map
.delete_turn_restriction(from, restriction.clone(), to);
self.road_added(from, prerender);
}
}
// Buildings

View File

@ -427,6 +427,32 @@ impl RawMap {
pub fn delete_building(&mut self, id: StableBuildingID) {
self.buildings.remove(&id);
}
pub fn delete_turn_restriction(
&mut self,
from: StableRoadID,
restriction: String,
to: StableRoadID,
) {
let to_way_id = self.roads[&to].orig_id.osm_way_id;
let list = self
.turn_restrictions
.get_mut(&self.roads[&from].orig_id.osm_way_id)
.unwrap();
list.retain(|(r, way_id)| r != &restriction || *way_id != to_way_id);
}
pub fn add_turn_restriction(
&mut self,
from: StableRoadID,
restriction: String,
to: StableRoadID,
) {
self.turn_restrictions
.entry(self.roads[&from].orig_id.osm_way_id)
.or_insert_with(Vec::new)
.push((restriction, self.roads[&to].orig_id.osm_way_id));
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]