moving show_activity viz to sandbox mode

This commit is contained in:
Dustin Carlino 2019-04-25 14:59:26 -07:00
parent e763171bc9
commit e93bf0f357
5 changed files with 46 additions and 65 deletions

View File

@ -1,7 +1,6 @@
pub mod legend;
pub mod neighborhood_summary;
pub mod search;
pub mod show_activity;
pub mod show_associated;
pub mod turn_cycler;
pub mod warp;

View File

@ -1,4 +1,5 @@
mod route_viewer;
mod show_activity;
mod spawner;
use crate::game::{GameState, Mode};
@ -15,6 +16,7 @@ pub struct SandboxMode {
desired_speed: f64, // sim seconds per real second
following: Option<TripID>,
route_viewer: route_viewer::RouteViewer,
show_activity: show_activity::ShowActivity,
state: State,
}
@ -35,6 +37,7 @@ impl SandboxMode {
state: State::Paused,
following: None,
route_viewer: route_viewer::RouteViewer::Inactive,
show_activity: show_activity::ShowActivity::Inactive,
}
}
@ -77,6 +80,12 @@ impl SandboxMode {
}
_ => {}
}
match mode.show_activity {
show_activity::ShowActivity::Inactive => {}
_ => {
txt.add_line("Showing active traffic".to_string());
}
}
ctx.input
.set_mode_with_new_prompt("Sandbox Mode", txt, ctx.canvas);
@ -122,6 +131,7 @@ impl SandboxMode {
}
}
mode.route_viewer.event(ctx, &mut state.ui);
mode.show_activity.event(ctx, &mut state.ui);
if ctx.input.modal_action("quit") {
// TODO This shouldn't be necessary when we plumb state around instead of
@ -268,6 +278,7 @@ impl SandboxMode {
_ => {
state.ui.new_draw(g, None, HashMap::new());
mode.route_viewer.draw(g, &state.ui);
mode.show_activity.draw(g, &state.ui);
}
},
_ => unreachable!(),

View File

@ -1,75 +1,67 @@
use crate::objects::DrawCtx;
use crate::plugins::{AmbientPlugin, PluginCtx};
use crate::render::MIN_ZOOM_FOR_DETAIL;
use ezgui::{Color, GfxCtx};
use crate::ui::UI;
use ezgui::{Color, EventCtx, GfxCtx};
use geom::{Bounds, Duration, Polygon, Pt2D};
use map_model::{RoadID, Traversable};
use std::collections::HashMap;
pub enum ShowActivityState {
pub enum ShowActivity {
Inactive,
Unzoomed(Duration, RoadHeatmap),
Zoomed(Duration, Heatmap),
}
impl ShowActivityState {
pub fn new() -> ShowActivityState {
ShowActivityState::Inactive
}
}
impl AmbientPlugin for ShowActivityState {
fn ambient_event(&mut self, ctx: &mut PluginCtx) {
impl ShowActivity {
pub fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) {
let zoomed = ctx.canvas.cam_zoom >= MIN_ZOOM_FOR_DETAIL;
// If we survive past this, recompute current state.
match self {
ShowActivityState::Inactive => {
if !ctx.input.action_chosen("show lanes with active traffic") {
ShowActivity::Inactive => {
if !ctx.input.modal_action("show/hide active traffic") {
return;
}
}
ShowActivityState::Zoomed(time, ref heatmap) => {
ctx.input.set_mode("Active Traffic Visualizer", &ctx.canvas);
if ctx.input.modal_action("quit") {
*self = ShowActivityState::Inactive;
ShowActivity::Zoomed(time, ref heatmap) => {
if ctx.input.modal_action("show/hide active traffic") {
*self = ShowActivity::Inactive;
return;
}
if *time == ctx.primary.sim.time()
if *time == ui.state.primary.sim.time()
&& ctx.canvas.get_screen_bounds() == heatmap.bounds
&& zoomed
{
return;
}
}
ShowActivityState::Unzoomed(time, _) => {
ctx.input.set_mode("Active Traffic Visualizer", &ctx.canvas);
if ctx.input.modal_action("quit") {
*self = ShowActivityState::Inactive;
ShowActivity::Unzoomed(time, _) => {
if ctx.input.modal_action("show/hide active traffic") {
*self = ShowActivity::Inactive;
return;
}
if *time == ctx.primary.sim.time() && !zoomed {
if *time == ui.state.primary.sim.time() && !zoomed {
return;
}
}
};
if zoomed {
*self = ShowActivityState::Zoomed(ctx.primary.sim.time(), active_agent_heatmap(ctx));
*self =
ShowActivity::Zoomed(ui.state.primary.sim.time(), active_agent_heatmap(ctx, ui));
} else {
*self = ShowActivityState::Unzoomed(ctx.primary.sim.time(), RoadHeatmap::new(ctx));
*self = ShowActivity::Unzoomed(ui.state.primary.sim.time(), RoadHeatmap::new(ui));
}
}
fn draw(&self, g: &mut GfxCtx, ctx: &DrawCtx) {
pub fn draw(&self, g: &mut GfxCtx, ui: &UI) {
match self {
ShowActivityState::Zoomed(_, ref heatmap) => {
ShowActivity::Zoomed(_, ref heatmap) => {
heatmap.draw(g);
}
ShowActivityState::Unzoomed(_, ref road_heatmap) => {
road_heatmap.draw(g, ctx);
ShowActivity::Unzoomed(_, ref road_heatmap) => {
road_heatmap.draw(g, ui);
}
ShowActivityState::Inactive => {}
ShowActivity::Inactive => {}
}
}
}
@ -138,9 +130,9 @@ impl Heatmap {
}
}
fn active_agent_heatmap(ctx: &mut PluginCtx) -> Heatmap {
fn active_agent_heatmap(ctx: &EventCtx, ui: &mut UI) -> Heatmap {
let mut h = Heatmap::new(ctx.canvas.get_screen_bounds());
let stats = ctx.primary.sim.get_stats(&ctx.primary.map);
let stats = ui.state.primary.sim.get_stats(&ui.state.primary.map);
for pt in stats.canonical_pt_per_trip.values() {
h.add(*pt);
}
@ -154,14 +146,14 @@ pub struct RoadHeatmap {
}
impl RoadHeatmap {
fn new(ctx: &mut PluginCtx) -> RoadHeatmap {
fn new(ui: &UI) -> RoadHeatmap {
let mut h = RoadHeatmap {
count_per_road: HashMap::new(),
max_count: 0,
};
let map = &ctx.primary.map;
for a in ctx.primary.sim.active_agents() {
let r = match ctx.primary.sim.location_for_agent(a, map) {
let map = &ui.state.primary.map;
for a in ui.state.primary.sim.active_agents() {
let r = match ui.state.primary.sim.location_for_agent(a, map) {
Traversable::Lane(l) => map.get_l(l).parent,
// Count the destination
Traversable::Turn(t) => map.get_l(t.dst).parent,
@ -174,7 +166,7 @@ impl RoadHeatmap {
h
}
fn draw(&self, g: &mut GfxCtx, ctx: &DrawCtx) {
fn draw(&self, g: &mut GfxCtx, ui: &UI) {
for (r, count) in &self.count_per_road {
let percent = (*count as f32) / (self.max_count as f32);
// TODO Map percent to hot/cold colors. For now, just bucket into 3 categories.
@ -186,7 +178,10 @@ impl RoadHeatmap {
Color::RED
};
// TODO Inefficient!
g.draw_polygon(color, &ctx.map.get_r(*r).get_thick_polygon().unwrap());
g.draw_polygon(
color,
&ui.state.primary.map.get_r(*r).get_thick_polygon().unwrap(),
);
}
}
}

View File

@ -431,7 +431,6 @@ impl PluginsPerMap {
)),
// TODO Could be a little simpler to instantiate this lazily, stop representing
// inactive state.
Box::new(view::show_activity::ShowActivityState::new()),
Box::new(view::show_associated::ShowAssociatedState::new()),
Box::new(view::turn_cycler::TurnCyclerState::new()),
],

View File

@ -68,7 +68,6 @@ impl GUI for UI {
vec![
(None, "show neighborhood summaries"),
(Some(Key::Slash), "search for something"),
(Some(Key::A), "show lanes with active traffic"),
(Some(Key::J), "warp to an object"),
],
),
@ -110,35 +109,12 @@ impl GUI for UI {
(Key::Dot, "forwards"),
],
),
ModalMenu::new(
"Simple Model",
vec![
(Key::Enter, "quit"),
(Key::Comma, "rewind"),
(Key::Dot, "forwards"),
(Key::Space, "toggle forwards play"),
(Key::M, "toggle backwards play"),
(Key::T, "toggle tooltips"),
(Key::E, "exhaustively test instantiation everywhere"),
(Key::D, "debug"),
],
),
ModalMenu::new(
"Even Simpler Model",
vec![
(Key::Enter, "quit"),
(Key::Dot, "forwards"),
(Key::Space, "toggle forwards play"),
(Key::E, "spawn tons of cars everywhere"),
],
),
ModalMenu::new("Original Roads", vec![(Key::Enter, "quit")]),
ModalMenu::new("Chokepoints Debugger", vec![(Key::Enter, "quit")]),
ModalMenu::new("A/B Trip Explorer", vec![(Key::Enter, "quit")]),
ModalMenu::new("A/B All Trips Explorer", vec![(Key::Enter, "quit")]),
ModalMenu::new("Search", vec![(Key::Enter, "quit")]),
ModalMenu::new("Neighborhood Summaries", vec![(Key::Enter, "quit")]),
ModalMenu::new("Active Traffic Visualizer", vec![(Key::A, "quit")]),
ModalMenu::new("Object Hider", vec![(Key::K, "unhide everything")]),
// TODO F1?
ModalMenu::new("Legend", vec![(Key::L, "quit")]),
@ -198,6 +174,7 @@ impl GUI for UI {
(Key::R, "stop showing agent's route"),
// TODO This should probably be a debug thing instead
(Key::L, "show/hide route for all agents"),
(Key::A, "show/hide active traffic"),
],
),
ModalMenu::new("Agent Spawner", vec![(Key::Escape, "quit")]),