moving turn cycler to CommonState

This commit is contained in:
Dustin Carlino 2019-04-28 17:11:46 -07:00
parent 78fc973611
commit db67c734ef
8 changed files with 51 additions and 32 deletions

View File

@ -269,6 +269,7 @@ impl ABTestMode {
&state.ui.state.primary.sim,
&ShowEverything::new(),
);
mode.common.draw(g, &state.ui);
if let Some(ref diff) = mode.diff_trip {
diff.draw(g, &state.ui);

View File

@ -1,4 +1,5 @@
mod associated;
mod turn_cycler;
use crate::objects::ID;
use crate::ui::UI;
@ -7,12 +8,14 @@ use std::collections::HashMap;
pub struct CommonState {
associated: associated::ShowAssociatedState,
turn_cycler: turn_cycler::TurnCyclerState,
}
impl CommonState {
pub fn new() -> CommonState {
CommonState {
associated: associated::ShowAssociatedState::Inactive,
turn_cycler: turn_cycler::TurnCyclerState::new(),
}
}
@ -20,10 +23,13 @@ impl CommonState {
// happening.
pub fn event(&mut self, ctx: &mut EventCtx, ui: &UI) -> Option<EventLoopMode> {
self.associated.event(ui);
self.turn_cycler.event(ctx, ui);
None
}
pub fn draw(&self, g: &mut GfxCtx, ui: &UI) {}
pub fn draw(&self, g: &mut GfxCtx, ui: &UI) {
self.turn_cycler.draw(g, ui);
}
pub fn override_colors(&self, ui: &UI) -> HashMap<ID, Color> {
let mut colors = HashMap::new();

View File

@ -1,7 +1,7 @@
use crate::objects::{DrawCtx, ID};
use crate::plugins::{AmbientPlugin, PluginCtx};
use crate::render::{draw_signal_diagram, DrawTurn};
use ezgui::{Color, GfxCtx, Key};
use crate::ui::UI;
use ezgui::{Color, EventCtx, GfxCtx, Key};
use geom::Duration;
use map_model::{IntersectionID, LaneID, TurnType};
@ -24,11 +24,9 @@ impl TurnCyclerState {
shift_key_held: false,
}
}
}
impl AmbientPlugin for TurnCyclerState {
fn ambient_event(&mut self, ctx: &mut PluginCtx) {
match ctx.primary.current_selection {
pub fn event(&mut self, ctx: &mut EventCtx, ui: &UI) {
match ui.state.primary.current_selection {
Some(ID::Lane(id)) => {
if let State::CycleTurns(current, idx) = self.state {
if current != id {
@ -41,7 +39,7 @@ impl AmbientPlugin for TurnCyclerState {
}
} else {
self.state = State::ShowLane(id);
if !ctx.primary.map.get_turns_from_lane(id).is_empty()
if !ui.state.primary.map.get_turns_from_lane(id).is_empty()
&& ctx
.input
.key_pressed(Key::Tab, "cycle through this lane's turns")
@ -50,7 +48,8 @@ impl AmbientPlugin for TurnCyclerState {
}
}
ctx.hints.suppress_traffic_signal_details = Some(ctx.primary.map.get_l(id).dst_i);
// TODO...
//ctx.hints.suppress_traffic_signal_details = Some(ctx.primary.map.get_l(id).dst_i);
}
Some(ID::Intersection(id)) => {
self.state = State::ShowIntersection(id);
@ -60,6 +59,8 @@ impl AmbientPlugin for TurnCyclerState {
}
}
// TODO I think it's possible for this state to get out of sync with reality, by holding
// the key while changing to a mode that doesn't invoke CommonState.
if self.shift_key_held {
if ctx.input.key_released(Key::LeftShift) {
self.shift_key_held = false;
@ -75,35 +76,47 @@ impl AmbientPlugin for TurnCyclerState {
}
}
fn draw(&self, g: &mut GfxCtx, ctx: &DrawCtx) {
pub fn draw(&self, g: &mut GfxCtx, ui: &UI) {
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));
for turn in &ui.state.primary.map.get_turns_from_lane(l) {
DrawTurn::draw_full(turn, g, color_turn_type(turn.turn_type, ui).alpha(0.5));
}
}
State::CycleTurns(l, idx) => {
let turns = ctx.map.get_turns_from_lane(l);
let turns = ui.state.primary.map.get_turns_from_lane(l);
let t = turns[idx % turns.len()];
DrawTurn::draw_full(t, g, color_turn_type(t.turn_type, ctx));
DrawTurn::draw_full(t, g, color_turn_type(t.turn_type, ui));
}
State::ShowIntersection(i) => {
if self.shift_key_held {
if let Some(signal) = ctx.map.maybe_get_traffic_signal(i) {
if let Some(signal) = ui.state.primary.map.maybe_get_traffic_signal(i) {
let (cycle, mut time_left) =
signal.current_cycle_and_remaining_time(ctx.sim.time());
if ctx.sim.is_in_overtime(i, ctx.map) {
signal.current_cycle_and_remaining_time(ui.state.primary.sim.time());
if ui
.state
.primary
.sim
.is_in_overtime(i, &ui.state.primary.map)
{
// TODO Hacky way of indicating overtime. Should make a 3-case enum.
time_left = Duration::seconds(-1.0);
}
let ctx = DrawCtx {
cs: &ui.state.cs,
map: &ui.state.primary.map,
draw_map: &ui.state.primary.draw_map,
sim: &ui.state.primary.sim,
hints: &ui.hints,
};
draw_signal_diagram(
i,
cycle.idx,
Some(time_left),
g.canvas.top_menu_height() + 10.0,
g,
ctx,
&ctx,
);
}
}
@ -112,14 +125,15 @@ impl AmbientPlugin for TurnCyclerState {
}
}
fn color_turn_type(t: TurnType, ctx: &DrawCtx) -> Color {
fn color_turn_type(t: TurnType, ui: &UI) -> Color {
match t {
TurnType::SharedSidewalkCorner => {
ctx.cs.get_def("shared sidewalk corner turn", Color::BLACK)
}
TurnType::Crosswalk => ctx.cs.get_def("crosswalk turn", Color::WHITE),
TurnType::Straight => ctx.cs.get_def("straight turn", Color::BLUE),
TurnType::Right => ctx.cs.get_def("right turn", Color::GREEN),
TurnType::Left => ctx.cs.get_def("left turn", Color::RED),
TurnType::SharedSidewalkCorner => ui
.state
.cs
.get_def("shared sidewalk corner turn", Color::BLACK),
TurnType::Crosswalk => ui.state.cs.get_def("crosswalk turn", Color::WHITE),
TurnType::Straight => ui.state.cs.get_def("straight turn", Color::BLUE),
TurnType::Right => ui.state.cs.get_def("right turn", Color::GREEN),
TurnType::Left => ui.state.cs.get_def("left turn", Color::RED),
}
}

View File

@ -299,6 +299,7 @@ impl DebugMode {
state
.ui
.new_draw(g, None, color_overrides, &state.ui.state.primary.sim, mode);
mode.common.draw(g, &state.ui);
for id in &mode.show_original_roads {
let r = state.ui.state.primary.map.get_r(*id);

View File

@ -211,6 +211,7 @@ impl EditMode {
&state.ui.state.primary.sim,
&ShowEverything::new(),
);
common.draw(g, &state.ui);
// TODO Similar to drawing areas with traffic or not -- would be convenient to just
// supply a set of things to highlight and have something else take care of drawing

View File

@ -1,2 +1 @@
pub mod turn_cycler;
pub mod warp;

View File

@ -324,6 +324,7 @@ impl SandboxMode {
&state.ui.state.primary.sim,
&ShowEverything::new(),
);
mode.common.draw(g, &state.ui);
mode.route_viewer.draw(g, &state.ui);
mode.show_activity.draw(g, &state.ui);
}

View File

@ -164,11 +164,7 @@ impl PerMapUI {
current_flags: flags.clone(),
};
let plugins = PluginsPerMap {
ambient_plugins: vec![
// TODO Could be a little simpler to instantiate this lazily, stop representing
// inactive state.
Box::new(view::turn_cycler::TurnCyclerState::new()),
],
ambient_plugins: Vec::new(),
};
(state, plugins)
}