From 5ea6e0ea18d451ab3e0daa76d9da92fce8d58f72 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 4 Dec 2018 12:58:01 -0800 Subject: [PATCH] adjust possible controls --- editor/src/plugins/traffic_signal_editor.rs | 28 +++++-------- map_model/src/traffic_signals.rs | 45 ++++++++++----------- 2 files changed, 33 insertions(+), 40 deletions(-) diff --git a/editor/src/plugins/traffic_signal_editor.rs b/editor/src/plugins/traffic_signal_editor.rs index dcc5e0f6ee..e30d87cd64 100644 --- a/editor/src/plugins/traffic_signal_editor.rs +++ b/editor/src/plugins/traffic_signal_editor.rs @@ -136,26 +136,20 @@ impl Plugin for TrafficSignalEditor { let mut signal = ctx.primary.map.get_traffic_signal(*i).clone(); { let cycle = &mut signal.cycles[*current_cycle]; - if cycle.get_priority(id) == TurnPriority::Priority { - if input.key_pressed( - Key::Backspace, - "remove this turn from this cycle", - ) { - cycle.remove(id, &ctx.primary.map); - } - } else if cycle.could_be_priority_turn(id, &ctx.primary.map) { - if input.key_pressed( + if cycle.get_priority(id) != TurnPriority::Stop && input + .key_pressed(Key::Backspace, "remove this turn from this cycle") + { + cycle.edit_turn(id, TurnPriority::Stop, &ctx.primary.map); + } else if cycle.could_be_priority_turn(id, &ctx.primary.map) + && input.key_pressed( Key::Space, "add this turn to this cycle as priority", ) { - cycle.add(id, TurnPriority::Priority, &ctx.primary.map); - } - } else if cycle.get_priority(id) == TurnPriority::Stop { - if input - .key_pressed(Key::Y, "add this turn to this cycle as yield") - { - cycle.add(id, TurnPriority::Yield, &ctx.primary.map); - } + cycle.edit_turn(id, TurnPriority::Priority, &ctx.primary.map); + } else if cycle.could_be_yield_turn(id, &ctx.primary.map) && input + .key_pressed(Key::Y, "add this turn to this cycle as yield") + { + cycle.edit_turn(id, TurnPriority::Yield, &ctx.primary.map); } } diff --git a/map_model/src/traffic_signals.rs b/map_model/src/traffic_signals.rs index 2d685ba5b4..8326b8d26c 100644 --- a/map_model/src/traffic_signals.rs +++ b/map_model/src/traffic_signals.rs @@ -78,14 +78,19 @@ pub struct Cycle { impl Cycle { pub fn could_be_priority_turn(&self, t1: TurnID, map: &Map) -> bool { + let turn1 = map.get_t(t1); for t2 in &self.priority_turns { - if map.get_t(t1).conflicts_with(map.get_t(*t2)) { + if t1 == *t2 || turn1.conflicts_with(map.get_t(*t2)) { return false; } } true } + pub fn could_be_yield_turn(&self, t: TurnID, map: &Map) -> bool { + !self.yield_turns.contains(&t) && map.get_t(t).turn_type != TurnType::Crosswalk + } + pub fn get_priority(&self, t: TurnID) -> TurnPriority { if self.priority_turns.contains(&t) { TurnPriority::Priority @@ -96,12 +101,25 @@ impl Cycle { } } - pub fn add(&mut self, t: TurnID, pri: TurnPriority, map: &Map) { + pub fn edit_turn(&mut self, t: TurnID, pri: TurnPriority, map: &Map) { let turn = map.get_t(t); assert_eq!(t.parent, self.parent); - // TODO assert not in the current (maybe other) set + // First remove the turn from the old set, if present. + if self.priority_turns.contains(&t) { + assert_ne!(pri, TurnPriority::Priority); + self.priority_turns.remove(&t); + if turn.turn_type == TurnType::Crosswalk { + self.priority_turns.remove(&turn.other_crosswalk_id()); + } + } else if self.yield_turns.contains(&t) { + assert_ne!(pri, TurnPriority::Yield); + self.yield_turns.remove(&t); + } else { + assert_ne!(pri, TurnPriority::Stop); + } + // Now add to the new set match pri { TurnPriority::Priority => { // TODO assert compatible @@ -114,30 +132,11 @@ impl Cycle { assert_ne!(turn.turn_type, TurnType::Crosswalk); self.yield_turns.insert(t); } - TurnPriority::Stop => { - panic!("add {} with Stop priority to a Cycle doesn't make sense", t); - } + TurnPriority::Stop => {} } self.changed = true; } - pub fn remove(&mut self, t: TurnID, map: &Map) { - if self.priority_turns.contains(&t) { - self.priority_turns.remove(&t); - let turn = map.get_t(t); - if turn.turn_type == TurnType::Crosswalk { - self.priority_turns.remove(&turn.other_crosswalk_id()); - } - } else if self.yield_turns.contains(&t) { - self.yield_turns.remove(&t); - } else { - panic!( - "Cycle {:?} doesn't have {} as a priority or yield turn; why remove it?", - self, t - ); - } - } - pub fn edit_duration(&mut self, new_duration: si::Second) { self.changed = true; self.duration = new_duration;