hold a key to show traffic signal diagram

This commit is contained in:
Dustin Carlino 2019-01-13 10:24:02 -08:00
parent f1536fa260
commit 66006618df

View File

@ -5,7 +5,12 @@ use dimensioned::si;
use ezgui::{Color, GfxCtx, Key, TOP_MENU_HEIGHT};
use map_model::{IntersectionID, LaneID, TurnType};
pub enum TurnCyclerState {
pub struct TurnCyclerState {
state: State,
shift_key_held: bool,
}
enum State {
Inactive,
ShowLane(LaneID),
CycleTurns(LaneID, usize),
@ -14,7 +19,10 @@ pub enum TurnCyclerState {
impl TurnCyclerState {
pub fn new() -> TurnCyclerState {
TurnCyclerState::Inactive
TurnCyclerState {
state: State::Inactive,
shift_key_held: false,
}
}
}
@ -22,66 +30,82 @@ impl Plugin for TurnCyclerState {
fn ambient_event(&mut self, ctx: &mut PluginCtx) {
match ctx.primary.current_selection {
Some(ID::Lane(id)) => {
if let TurnCyclerState::CycleTurns(current, idx) = self {
if *current != id {
*self = TurnCyclerState::ShowLane(id);
if let State::CycleTurns(current, idx) = self.state {
if current != id {
self.state = State::ShowLane(id);
} else if ctx
.input
.key_pressed(Key::Tab, "cycle through this lane's turns")
{
*self = TurnCyclerState::CycleTurns(id, *idx + 1);
self.state = State::CycleTurns(id, idx + 1);
}
} else {
*self = TurnCyclerState::ShowLane(id);
self.state = State::ShowLane(id);
if !ctx.primary.map.get_turns_from_lane(id).is_empty()
&& ctx
.input
.key_pressed(Key::Tab, "cycle through this lane's turns")
{
*self = TurnCyclerState::CycleTurns(id, 0);
self.state = State::CycleTurns(id, 0);
}
}
ctx.hints.suppress_traffic_signal_details = Some(ctx.primary.map.get_l(id).dst_i);
}
Some(ID::Intersection(id)) => {
*self = TurnCyclerState::ShowIntersection(id);
self.state = State::ShowIntersection(id);
}
_ => {
*self = TurnCyclerState::Inactive;
self.state = State::Inactive;
}
};
}
if self.shift_key_held {
if ctx.input.key_released(Key::LeftShift) {
self.shift_key_held = false;
}
} else {
// TODO How to tell the user that holding control and shift is sometimes useful?
if ctx
.input
.unimportant_key_pressed(Key::LeftShift, "show full traffic signal diagram")
{
self.shift_key_held = true;
}
}
}
fn draw(&self, g: &mut GfxCtx, ctx: &Ctx) {
match self {
TurnCyclerState::Inactive => {}
TurnCyclerState::ShowLane(l) => {
for turn in &ctx.map.get_turns_from_lane(*l) {
match self.state {
State::Inactive => {}
State::ShowLane(l) => {
for turn in &ctx.map.get_turns_from_lane(l) {
DrawTurn::draw_full(turn, g, color_turn_type(turn.turn_type, ctx).alpha(0.5));
}
}
TurnCyclerState::CycleTurns(l, idx) => {
let turns = ctx.map.get_turns_from_lane(*l);
let t = turns[*idx % turns.len()];
State::CycleTurns(l, idx) => {
let turns = ctx.map.get_turns_from_lane(l);
let t = turns[idx % turns.len()];
DrawTurn::draw_full(t, g, color_turn_type(t.turn_type, ctx));
}
TurnCyclerState::ShowIntersection(i) => {
if let Some(signal) = ctx.map.maybe_get_traffic_signal(*i) {
let (cycle, mut time_left) =
signal.current_cycle_and_remaining_time(ctx.sim.time.as_time());
if ctx.sim.is_in_overtime(*i) {
// TODO Hacky way of indicating overtime. Should make a 3-case enum.
time_left = -1.0 * si::S;
State::ShowIntersection(i) => {
if self.shift_key_held {
if let Some(signal) = ctx.map.maybe_get_traffic_signal(i) {
let (cycle, mut time_left) =
signal.current_cycle_and_remaining_time(ctx.sim.time.as_time());
if ctx.sim.is_in_overtime(i) {
// TODO Hacky way of indicating overtime. Should make a 3-case enum.
time_left = -1.0 * si::S;
}
draw_signal_diagram(
i,
cycle.idx,
Some(time_left),
TOP_MENU_HEIGHT + 10.0,
g,
ctx,
);
}
draw_signal_diagram(
*i,
cycle.idx,
Some(time_left),
TOP_MENU_HEIGHT + 10.0,
g,
ctx,
);
}
}
}