From 9cfd22d5e080f9c8558041e77d103f28d02227d6 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 26 Oct 2019 16:32:01 -0700 Subject: [PATCH] allow creating new internal points for roads in the editor --- map_editor/src/main.rs | 12 ++++++++++-- map_editor/src/model.rs | 35 +++++++++++++++++++++++++++++++++-- map_editor/src/world.rs | 4 ++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/map_editor/src/main.rs b/map_editor/src/main.rs index e86370da81..75521fe0d2 100644 --- a/map_editor/src/main.rs +++ b/map_editor/src/main.rs @@ -243,6 +243,14 @@ impl GUI for UI { .cloned() .unwrap_or_else(String::new), ); + } else if cursor.is_some() + && ctx.input.key_pressed(Key::P, "create new point") + { + if let Some(id) = + self.model.insert_r_pt(r, cursor.unwrap(), ctx.prerender) + { + self.model.world.force_set_selection(id); + } } } Some(ID::RoadPoint(r, idx)) => { @@ -283,8 +291,8 @@ impl GUI for UI { // dead-center messes up the precomputed triangles. } else if ctx.input.key_pressed(Key::B, "create building") { if let Some(pt) = cursor { - self.model.create_b(pt, ctx.prerender); - self.model.world.handle_mouseover(ctx); + let id = self.model.create_b(pt, ctx.prerender); + self.model.world.force_set_selection(id); } } else if ctx.input.key_pressed(Key::LeftShift, "select area") { if let Some(pt) = cursor { diff --git a/map_editor/src/model.rs b/map_editor/src/model.rs index 7c71788266..46597a3202 100644 --- a/map_editor/src/model.rs +++ b/map_editor/src/model.rs @@ -1,7 +1,7 @@ use crate::world::{Object, ObjectID, World}; use abstutil::{read_binary, Timer}; use ezgui::{Color, Line, Prerender, Text}; -use geom::{Bounds, Circle, Distance, PolyLine, Polygon, Pt2D}; +use geom::{Bounds, Circle, Distance, FindClosest, PolyLine, Polygon, Pt2D}; use map_model::raw::{ MapFixes, OriginalIntersection, OriginalRoad, RawBuilding, RawIntersection, RawMap, RawRoad, RestrictionType, StableBuildingID, StableIntersectionID, StableRoadID, @@ -825,6 +825,36 @@ impl Model { self.show_r_points(id, prerender); } + pub fn insert_r_pt(&mut self, id: StableRoadID, pt: Pt2D, prerender: &Prerender) -> Option { + assert_eq!(self.showing_pts, Some(id)); + + self.stop_showing_pts(id); + self.road_deleted(id); + self.world.delete(ID::Intersection(self.map.roads[&id].i1)); + self.world.delete(ID::Intersection(self.map.roads[&id].i2)); + + let mut pts = self.map.roads[&id].center_points.clone(); + let mut closest = FindClosest::new(&self.compute_bounds()); + for (idx, pair) in pts.windows(2).enumerate() { + closest.add(idx + 1, &vec![pair[0], pair[1]]); + } + let new_id = if let Some((idx, _)) = closest.closest_pt(pt, Distance::meters(5.0)) { + pts.insert(idx, pt); + Some(ID::RoadPoint(id, idx)) + } else { + println!("Couldn't figure out where to insert new point"); + None + }; + self.map.override_road_points(id, pts); + + self.road_added(id, prerender); + self.intersection_added(self.map.roads[&id].i1, prerender); + self.intersection_added(self.map.roads[&id].i2, prerender); + self.show_r_points(id, prerender); + + new_id + } + // TODO Need to show_r_points of the thing we wind up selecting after this. pub fn merge_r(&mut self, id: StableRoadID, prerender: &Prerender) { if let Err(e) = self.map.can_merge_short_road(id) { @@ -899,7 +929,7 @@ impl Model { ); } - pub fn create_b(&mut self, center: Pt2D, prerender: &Prerender) { + pub fn create_b(&mut self, center: Pt2D, prerender: &Prerender) -> ID { let id = self .map .create_building(RawBuilding { @@ -910,6 +940,7 @@ impl Model { }) .unwrap(); self.bldg_added(id, prerender); + ID::Building(id) } pub fn move_b(&mut self, id: StableBuildingID, new_center: Pt2D, prerender: &Prerender) { diff --git a/map_editor/src/world.rs b/map_editor/src/world.rs index 84d3395f1c..ccc6deee7c 100644 --- a/map_editor/src/world.rs +++ b/map_editor/src/world.rs @@ -123,6 +123,10 @@ impl World { } } + pub fn force_set_selection(&mut self, id: ID) { + self.current_selection = Some(id); + } + pub fn get_selection(&self) -> Option { self.current_selection }