game/devtools/
compare_counts.rs1use abstutil::Timer;
2use map_gui::tools::compare_counts::{CompareCounts, Layer};
3use synthpop::TrafficCounts;
4use widgetry::tools::PopupMsg;
5use widgetry::{
6 EventCtx, GfxCtx, HorizontalAlignment, Line, Panel, SimpleState, State, VerticalAlignment,
7 Widget,
8};
9
10use crate::app::{App, Transition};
11
12pub struct GenericCompareCounts {
13 compare: CompareCounts,
14}
15
16impl GenericCompareCounts {
17 pub fn new_state(
18 ctx: &mut EventCtx,
19 app: &mut App,
20 path1: String,
21 path2: String,
22 ) -> Box<dyn State<App>> {
23 let mut timer = Timer::throwaway();
24 let counts_a = match abstio::maybe_read_json::<TrafficCounts>(path1, &mut timer) {
26 Ok(c) => c,
27 Err(err) => {
28 return PopupMsg::new_state(ctx, "Error", vec![err.to_string()]);
29 }
30 };
31 let counts_b = match abstio::maybe_read_json::<TrafficCounts>(path2, &mut timer) {
32 Ok(c) => c,
33 Err(err) => {
34 return PopupMsg::new_state(ctx, "Error", vec![err.to_string()]);
35 }
36 };
37 let clickable_roads = false;
38 let mut compare =
39 CompareCounts::new(ctx, app, counts_a, counts_b, Layer::A, clickable_roads);
40 compare.autoselect_layer();
41
42 let panel = Panel::new_builder(Widget::col(vec![
43 Line("Traffic count comparator")
44 .small_heading()
45 .into_widget(ctx),
46 compare.get_panel_widget(ctx).named("compare counts"),
47 ]))
48 .aligned(HorizontalAlignment::Left, VerticalAlignment::Top)
49 .build(ctx);
50
51 <dyn SimpleState<_>>::new_state(panel, Box::new(GenericCompareCounts { compare }))
52 }
53}
54
55impl SimpleState<App> for GenericCompareCounts {
56 fn on_click(
57 &mut self,
58 ctx: &mut EventCtx,
59 app: &mut App,
60 x: &str,
61 panel: &mut Panel,
62 ) -> Transition {
63 let widget = self
64 .compare
65 .on_click(ctx, app, x)
66 .expect("button click didn't belong to CompareCounts");
67 panel.replace(ctx, "compare counts", widget);
68 Transition::Keep
69 }
70
71 fn other_event(&mut self, ctx: &mut EventCtx, _: &mut App) -> Transition {
72 self.compare.other_event(ctx);
73 Transition::Keep
74 }
75
76 fn draw(&self, g: &mut GfxCtx, app: &App) {
77 self.compare.draw(g, app);
78 }
79}