move cycles up/down, delete them, add them

This commit is contained in:
Dustin Carlino 2018-12-04 14:51:49 -08:00
parent b8e33bb3a2
commit 06bd34b1f4
3 changed files with 51 additions and 22 deletions

View File

@ -1,7 +1,7 @@
use dimensioned::si;
use ezgui::{Color, GfxCtx, Text, Wizard};
use geom::{Bounds, Polygon, Pt2D};
use map_model::{IntersectionID, TurnID, TurnPriority, TurnType};
use map_model::{Cycle, IntersectionID, TurnID, TurnPriority, TurnType};
use objects::{Ctx, ID};
use piston::input::Key;
use plugins::{Plugin, PluginCtx};
@ -164,6 +164,30 @@ impl Plugin for TrafficSignalEditor {
if input.key_pressed(Key::D, "change cycle duration") {
*cycle_duration_wizard = Some(Wizard::new());
}
let mut signal = ctx.primary.map.get_traffic_signal(*i).clone();
if *current_cycle != 0 && input.key_pressed(Key::K, "move current cycle up") {
signal.cycles.swap(*current_cycle, *current_cycle - 1);
*current_cycle -= 1;
ctx.primary.map.edit_traffic_signal(signal);
} else if *current_cycle != signal.cycles.len() - 1
&& input.key_pressed(Key::J, "move current cycle down")
{
signal.cycles.swap(*current_cycle, *current_cycle + 1);
*current_cycle += 1;
ctx.primary.map.edit_traffic_signal(signal);
} else if signal.cycles.len() > 1
&& input.key_pressed(Key::Backspace, "delete current cycle")
{
signal.cycles.remove(*current_cycle);
if *current_cycle == signal.cycles.len() {
*current_cycle -= 1;
}
ctx.primary.map.edit_traffic_signal(signal);
} else if input.key_pressed(Key::N, "add a new empty cycle") {
signal.cycles.insert(*current_cycle, Cycle::new(*i));
ctx.primary.map.edit_traffic_signal(signal);
}
}
}
};

View File

@ -354,7 +354,8 @@ impl Map {
self.stop_signs.insert(sign.id, sign);
}
pub fn edit_traffic_signal(&mut self, signal: ControlTrafficSignal) {
pub fn edit_traffic_signal(&mut self, mut signal: ControlTrafficSignal) {
signal.changed = true;
self.edits.traffic_signals.insert(signal.id, signal.clone());
self.traffic_signals.insert(signal.id, signal);
}

View File

@ -13,6 +13,7 @@ const CYCLE_DURATION: si::Second<f64> = si::Second {
pub struct ControlTrafficSignal {
pub id: IntersectionID,
pub cycles: Vec<Cycle>,
pub(crate) changed: bool,
}
impl ControlTrafficSignal {
@ -23,7 +24,7 @@ impl ControlTrafficSignal {
}
pub fn is_changed(&self) -> bool {
self.cycles.iter().find(|c| c.changed).is_some()
self.changed
}
pub fn current_cycle_and_remaining_time(
@ -87,11 +88,19 @@ pub struct Cycle {
pub parent: IntersectionID,
pub priority_turns: BTreeSet<TurnID>,
pub yield_turns: BTreeSet<TurnID>,
changed: bool,
pub duration: si::Second<f64>,
}
impl Cycle {
pub fn new(parent: IntersectionID) -> Cycle {
Cycle {
parent,
priority_turns: BTreeSet::new(),
yield_turns: BTreeSet::new(),
duration: CYCLE_DURATION,
}
}
pub fn could_be_priority_turn(&self, t1: TurnID, map: &Map) -> bool {
let turn1 = map.get_t(t1);
for t2 in &self.priority_turns {
@ -145,11 +154,9 @@ impl Cycle {
}
TurnPriority::Stop => {}
}
self.changed = true;
}
pub fn edit_duration(&mut self, new_duration: si::Second<f64>) {
self.changed = true;
self.duration = new_duration;
}
@ -180,13 +187,7 @@ fn greedy_assignment(map: &Map, intersection: IntersectionID) -> ControlTrafficS
.iter()
.map(|t| t.id)
.collect();
let mut current_cycle = Cycle {
parent: intersection,
priority_turns: BTreeSet::new(),
yield_turns: BTreeSet::new(),
changed: false,
duration: CYCLE_DURATION,
};
let mut current_cycle = Cycle::new(intersection);
loop {
let add_turn = remaining_turns
.iter()
@ -212,6 +213,7 @@ fn greedy_assignment(map: &Map, intersection: IntersectionID) -> ControlTrafficS
ControlTrafficSignal {
id: intersection,
cycles,
changed: false,
}
}
@ -303,7 +305,11 @@ fn four_way(map: &Map, i: IntersectionID) -> ControlTrafficSignal {
],
);
ControlTrafficSignal { id: i, cycles }
ControlTrafficSignal {
id: i,
cycles,
changed: false,
}
}
fn three_way(map: &Map, i: IntersectionID) -> ControlTrafficSignal {
@ -344,7 +350,11 @@ fn three_way(map: &Map, i: IntersectionID) -> ControlTrafficSignal {
],
);
ControlTrafficSignal { id: i, cycles }
ControlTrafficSignal {
id: i,
cycles,
changed: false,
}
}
fn make_cycles(
@ -355,13 +365,7 @@ fn make_cycles(
let mut cycles: Vec<Cycle> = Vec::new();
for specs in cycle_specs.into_iter() {
let mut cycle = Cycle {
parent: i,
priority_turns: BTreeSet::new(),
yield_turns: BTreeSet::new(),
changed: false,
duration: CYCLE_DURATION,
};
let mut cycle = Cycle::new(i);
for (roads, turn_type, protected) in specs.into_iter() {
for turn in map.get_turns_in_intersection(i) {