From b670299b8955c88c9753eb0c4582254aedc4e20b Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sun, 17 Nov 2019 11:35:37 -0800 Subject: [PATCH] support un-closing an intersection. prevent closing borders. --- game/src/edit/lanes.rs | 2 +- game/src/edit/mod.rs | 17 ++++++++-------- map_model/src/edits.rs | 12 +++++++++++ map_model/src/map.rs | 46 +++++++++++++++++++++++++----------------- 4 files changed, 49 insertions(+), 28 deletions(-) diff --git a/game/src/edit/lanes.rs b/game/src/edit/lanes.rs index b574ef9591..3cdd578d46 100644 --- a/game/src/edit/lanes.rs +++ b/game/src/edit/lanes.rs @@ -237,7 +237,7 @@ impl LaneEditor { .contextual_action(Key::Space, &self.brushes[self.construction_idx].label) { let it = ui.primary.map.get_i(i).intersection_type; - if it != IntersectionType::Construction { + if it != IntersectionType::Construction && it != IntersectionType::Border { let mut edits = ui.primary.map.get_edits().clone(); edits .commands diff --git a/game/src/edit/mod.rs b/game/src/edit/mod.rs index e182c325e1..2c1323380f 100644 --- a/game/src/edit/mod.rs +++ b/game/src/edit/mod.rs @@ -17,7 +17,7 @@ use ezgui::{ hotkey, lctrl, Choice, Color, EventCtx, EventLoopMode, GfxCtx, Key, Line, MenuUnderButton, ModalMenu, Text, Wizard, }; -use map_model::{IntersectionID, LaneID, MapEdits, TurnID, TurnType}; +use map_model::{EditCmd, IntersectionID, LaneID, MapEdits, TurnID, TurnType}; use std::collections::{BTreeSet, HashMap}; pub struct EditMode { @@ -184,15 +184,14 @@ impl State for EditMode { apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits); }*/ } - /*if orig_edits - .intersections_under_construction - .contains_key(&id) - && ctx.input.contextual_action(Key::R, "revert") + if ui.primary.map.get_i(id).is_closed() && ctx.input.contextual_action(Key::R, "revert") { - let mut new_edits = orig_edits.clone(); - new_edits.intersections_under_construction.remove(&id); - apply_map_edits(&mut ui.primary, &ui.cs, ctx, new_edits); - }*/ + let mut edits = ui.primary.map.get_edits().clone(); + edits + .commands + .push(EditCmd::UncloseIntersection(id, edits.original_it(id))); + apply_map_edits(&mut ui.primary, &ui.cs, ctx, edits); + } } Transition::Keep diff --git a/map_model/src/edits.rs b/map_model/src/edits.rs index 2f9a43abbe..424173e6b6 100644 --- a/map_model/src/edits.rs +++ b/map_model/src/edits.rs @@ -33,6 +33,7 @@ pub enum EditCmd { id: IntersectionID, orig_it: IntersectionType, }, + UncloseIntersection(IntersectionID, IntersectionType), } pub struct EditEffects { @@ -72,6 +73,17 @@ impl MapEdits { abstutil::save_json_object(abstutil::EDITS, &self.map_name, &self.edits_name, self); self.dirty = false; } + + pub fn original_it(&self, i: IntersectionID) -> IntersectionType { + for cmd in &self.commands { + if let EditCmd::CloseIntersection { id, orig_it } = cmd { + if *id == i { + return *orig_it; + } + } + } + panic!("{} isn't closed", i); + } } impl EditEffects { diff --git a/map_model/src/map.rs b/map_model/src/map.rs index 66dca89064..c17fb42e72 100644 --- a/map_model/src/map.rs +++ b/map_model/src/map.rs @@ -1137,6 +1137,28 @@ impl EditCmd { recalculate_turns(*id, map, effects, timer); true } + EditCmd::UncloseIntersection(id, orig_it) => { + let id = *id; + let orig_it = *orig_it; + if map.intersections[id.0].intersection_type == orig_it { + return false; + } + + map.intersections[id.0].intersection_type = orig_it; + recalculate_turns(id, map, effects, timer); + match orig_it { + IntersectionType::StopSign => { + map.stop_signs.insert(id, ControlStopSign::new(map, id)); + } + IntersectionType::TrafficSignal => { + map.traffic_signals + .insert(id, ControlTrafficSignal::new(map, id, timer)); + } + IntersectionType::Border | IntersectionType::Construction => unreachable!(), + } + effects.changed_intersections.insert(id); + true + } } } @@ -1170,25 +1192,13 @@ impl EditCmd { .apply(effects, map, timer) } EditCmd::CloseIntersection { id, orig_it } => { - if map.intersections[id.0].intersection_type == *orig_it { - return false; - } - - map.intersections[id.0].intersection_type = *orig_it; - recalculate_turns(*id, map, effects, timer); - match *orig_it { - IntersectionType::StopSign => { - map.stop_signs.insert(*id, ControlStopSign::new(map, *id)); - } - IntersectionType::TrafficSignal => { - map.traffic_signals - .insert(*id, ControlTrafficSignal::new(map, *id, timer)); - } - IntersectionType::Border | IntersectionType::Construction => unreachable!(), - } - effects.changed_intersections.insert(*id); - true + EditCmd::UncloseIntersection(*id, *orig_it).apply(effects, map, timer) } + EditCmd::UncloseIntersection(id, orig_it) => EditCmd::CloseIntersection { + id: *id, + orig_it: *orig_it, + } + .apply(effects, map, timer), } } }