Make signal demand explorer much nicer, with selectable movements. #331

This commit is contained in:
Dustin Carlino 2020-09-19 10:51:27 -07:00
parent 83ff6db16c
commit 57e5183c2c
2 changed files with 74 additions and 31 deletions

View File

@ -53,7 +53,7 @@ impl DashTab {
]) ])
} }
pub fn transition(self, ctx: &mut EventCtx, app: &App, action: &str) -> Transition { pub fn transition(self, ctx: &mut EventCtx, app: &mut App, action: &str) -> Transition {
match action { match action {
"close" => Transition::Pop, "close" => Transition::Pop,
"trip table" => Transition::Replace(TripTable::new(ctx, app)), "trip table" => Transition::Replace(TripTable::new(ctx, app)),

View File

@ -1,10 +1,11 @@
use crate::app::{App, ShowEverything}; use crate::app::{App, ShowEverything};
use crate::common::CommonState; use crate::common::CommonState;
use crate::game::{DrawBaselayer, State, Transition}; use crate::game::{DrawBaselayer, State, Transition};
use crate::helpers::ID;
use crate::render::DrawOptions; use crate::render::DrawOptions;
use abstutil::{prettyprint_usize, Counter, Parallelism, Timer}; use abstutil::{prettyprint_usize, Counter, Parallelism, Timer};
use geom::{ArrowCap, Distance, Duration, Time}; use geom::{ArrowCap, Distance, Duration, Polygon, Time};
use map_model::{IntersectionID, MovementID, PathStep, TurnType}; use map_model::{ControlTrafficSignal, IntersectionID, MovementID, PathStep, TurnType};
use sim::{DontDrawAgents, TripEndpoint}; use sim::{DontDrawAgents, TripEndpoint};
use std::collections::HashMap; use std::collections::HashMap;
use widgetry::{ use widgetry::{
@ -17,10 +18,13 @@ pub struct TrafficSignalDemand {
all_demand: HashMap<IntersectionID, Demand>, all_demand: HashMap<IntersectionID, Demand>,
hour: Time, hour: Time,
draw_all: Drawable, draw_all: Drawable,
selected: Option<(Drawable, Text)>,
} }
impl TrafficSignalDemand { impl TrafficSignalDemand {
pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State> { pub fn new(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State> {
app.primary.current_selection = None;
let all_demand = ctx.loading_screen("predict all demand", |_, timer| { let all_demand = ctx.loading_screen("predict all demand", |_, timer| {
Demand::all_demand(app, timer) Demand::all_demand(app, timer)
}); });
@ -30,6 +34,7 @@ impl TrafficSignalDemand {
all_demand, all_demand,
hour, hour,
draw_all, draw_all,
selected: None,
panel: Panel::new(Widget::col(vec![ panel: Panel::new(Widget::col(vec![
Widget::row(vec![ Widget::row(vec![
Line("Traffic signal demand over time") Line("Traffic signal demand over time")
@ -61,6 +66,39 @@ impl TrafficSignalDemand {
impl State for TrafficSignalDemand { impl State for TrafficSignalDemand {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
ctx.canvas_movement(); ctx.canvas_movement();
// TODO DrawWithTooltips works great in screenspace; make a similar tool for mapspace?
if ctx.redo_mouseover() {
self.selected = None;
if let Some(ID::Intersection(i)) = app.calculate_current_selection(
ctx,
&DontDrawAgents {},
&ShowEverything::new(),
false,
false,
false,
) {
if let Some(ts) = app.primary.map.maybe_get_traffic_signal(i) {
// If we're mousing over something, the cursor is on the map.
let pt = ctx.canvas.get_cursor_in_map_space().unwrap();
for (arrow, count) in self.all_demand[&i].make_arrows(ts, self.hour) {
if arrow.contains_pt(pt) {
let mut batch = GeomBatch::new();
batch.push(Color::hex("#EE702E"), arrow.clone());
if let Ok(p) = arrow.to_outline(Distance::meters(0.1)) {
batch.push(Color::WHITE, p);
}
let txt = Text::from(Line(format!(
"{} / {}",
prettyprint_usize(count),
self.all_demand[&i].count(self.hour).sum()
)));
self.selected = Some((ctx.upload(batch), txt));
break;
}
}
}
}
}
let mut changed = false; let mut changed = false;
match self.panel.event(ctx) { match self.panel.event(ctx) {
@ -96,14 +134,16 @@ impl State for TrafficSignalDemand {
} }
fn draw(&self, g: &mut GfxCtx, app: &App) { fn draw(&self, g: &mut GfxCtx, app: &App) {
app.draw( let mut opts = DrawOptions::new();
g, opts.suppress_traffic_signal_details
DrawOptions::new(), .extend(self.all_demand.keys().cloned());
&DontDrawAgents {}, app.draw(g, opts, &DontDrawAgents {}, &ShowEverything::new());
&ShowEverything::new(),
);
g.redraw(&self.draw_all); g.redraw(&self.draw_all);
if let Some((ref draw, ref count)) = self.selected {
g.redraw(draw);
g.draw_mouse_tooltip(count.clone());
}
self.panel.draw(g); self.panel.draw(g);
CommonState::draw_osd(g, app); CommonState::draw_osd(g, app);
@ -176,35 +216,38 @@ impl Demand {
cnt cnt
} }
fn make_arrows(&self, ts: &ControlTrafficSignal, hour: Time) -> Vec<(Polygon, usize)> {
let cnt = self.count(hour);
let total_demand = cnt.sum() as f64;
let mut arrows = Vec::new();
for (m, demand) in cnt.consume() {
let percent = (demand as f64) / total_demand;
let arrow = ts.movements[&m]
.geom
.make_arrow(percent * Distance::meters(3.0), ArrowCap::Triangle);
arrows.push((arrow, demand));
}
arrows
}
fn draw_demand( fn draw_demand(
ctx: &mut EventCtx, ctx: &mut EventCtx,
app: &App, app: &App,
all_demand: &HashMap<IntersectionID, Demand>, all_demand: &HashMap<IntersectionID, Demand>,
hour: Time, hour: Time,
) -> Drawable { ) -> Drawable {
let mut arrow_batch = GeomBatch::new(); let mut batch = GeomBatch::new();
let mut txt_batch = GeomBatch::new();
for (i, demand) in all_demand { for (i, demand) in all_demand {
let cnt = demand.count(hour); let mut outlines = Vec::new();
let total_demand = cnt.sum() as f64; for (arrow, _) in demand.make_arrows(app.primary.map.get_traffic_signal(*i), hour) {
if let Ok(p) = arrow.to_outline(Distance::meters(0.1)) {
// TODO Refactor with info/intersection after deciding exactly how to draw this outlines.push(p);
for (m, demand) in cnt.consume() { }
let percent = (demand as f64) / total_demand; batch.push(Color::hex("#A3A3A3"), arrow);
let pl = &app.primary.map.get_traffic_signal(*i).movements[&m].geom; }
arrow_batch.push( batch.extend(Color::WHITE, outlines);
Color::hex("#A3A3A3"), }
pl.make_arrow(percent * Distance::meters(3.0), ArrowCap::Triangle), ctx.upload(batch)
);
txt_batch.append(
Text::from(Line(prettyprint_usize(demand)).fg(Color::RED))
.render_ctx(ctx)
.scale(0.15)
.centered_on(pl.middle()),
);
}
}
arrow_batch.append(txt_batch);
ctx.upload(arrow_batch)
} }
} }