1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
use std::collections::BTreeSet;

use map_gui::tools::checkbox_per_mode;
use map_model::{Path, NORMAL_LANE_THICKNESS};
use synthpop::{Scenario, TripMode};
use widgetry::tools::{FileLoader, PopupMsg};
use widgetry::{
    Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Slider,
    State, Text, TextExt, Toggle, VerticalAlignment, Widget,
};

use crate::impact::{end_of_day, Filters, Impact};
use crate::{colors, App, BrowseNeighborhoods, Transition};

// TODO Share structure or pieces with Ungap's predict mode
// ... can't we just produce data of a certain shape, and have a UI pretty tuned for that?

pub struct ShowResults {
    top_panel: Panel,
    left_panel: Panel,
    draw_all_neighborhoods: Drawable,
}

impl ShowResults {
    pub fn new_state(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
        let map_name = app.map.get_name().clone();
        if app.session.impact.map != map_name {
            let scenario_name = Scenario::default_scenario_for_map(&map_name);
            return FileLoader::<App, Scenario>::new_state(
                ctx,
                abstio::path_scenario(&map_name, &scenario_name),
                Box::new(move |ctx, app, timer, maybe_scenario| {
                    // TODO Handle corrupt files
                    let scenario = maybe_scenario.unwrap();
                    app.session.impact = Impact::from_scenario(ctx, app, scenario, timer);
                    Transition::Replace(ShowResults::new_state(ctx, app))
                }),
            );
        }

        if app.session.impact.change_key != app.session.modal_filters.get_change_key() {
            ctx.loading_screen("recalculate impact", |ctx, timer| {
                // Avoid a double borrow
                let mut impact = std::mem::replace(&mut app.session.impact, Impact::empty(ctx));
                impact.map_edits_changed(ctx, app, timer);
                app.session.impact = impact;
            });
        }

        let contents = Widget::col(vec![
            ctx.style()
                .btn_back("Browse neighborhoods")
                .hotkey(Key::Escape)
                .build_def(ctx),
            Line("Impact prediction").small_heading().into_widget(ctx),
            Text::from(Line("This tool starts with a travel demand model, calculates the route every trip takes before and after changes, and displays volumes along roads and intersections")).wrap_to_pct(ctx, 20).into_widget(ctx),
            // TODO Dropdown for the scenario, and explain its source/limitations
            app.session.impact.filters.to_panel(ctx, app),
            app.session.impact.compare_counts.get_panel_widget(ctx).named("compare counts"),
            ctx.style().btn_outline.text("Save before/after counts to files").build_def(ctx),
        ]);
        let top_panel = crate::common::app_top_panel(ctx, app);
        let left_panel = crate::common::left_panel_builder(ctx, &top_panel, contents).build(ctx);

        let mut batch = GeomBatch::new();
        for (_, (block, color)) in app.session.partitioning.all_neighborhoods() {
            batch.push(color.alpha(0.2), block.polygon.clone());
        }
        let draw_all_neighborhoods = batch.upload(ctx);
        Box::new(Self {
            top_panel,
            left_panel,
            draw_all_neighborhoods,
        })
    }
}
impl State<App> for ShowResults {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Some(t) = crate::common::handle_top_panel(ctx, app, &mut self.top_panel, help) {
            return t;
        }
        match self.left_panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "Browse neighborhoods" => {
                    // Don't just Pop; if we updated the results, the UI won't warn the user about a slow
                    // loading
                    return Transition::Replace(BrowseNeighborhoods::new_state(ctx, app));
                }
                "Save before/after counts to files" => {
                    let path1 = "counts_a.json";
                    let path2 = "counts_b.json";
                    abstio::write_json(
                        path1.to_string(),
                        &app.session.impact.compare_counts.counts_a,
                    );
                    abstio::write_json(
                        path2.to_string(),
                        &app.session.impact.compare_counts.counts_b,
                    );
                    return Transition::Push(PopupMsg::new_state(
                        ctx,
                        "Saved",
                        vec![format!("Saved {} and {}", path1, path2)],
                    ));
                }
                x => {
                    // Avoid a double borrow
                    let mut impact = std::mem::replace(&mut app.session.impact, Impact::empty(ctx));
                    let widget = impact
                        .compare_counts
                        .on_click(ctx, app, x)
                        .expect("button click didn't belong to CompareCounts");
                    app.session.impact = impact;
                    self.left_panel.replace(ctx, "compare counts", widget);
                    return Transition::Keep;
                }
            },
            Outcome::Changed(_) => {
                // TODO The sliders should only trigger updates when the user lets go; way too slow
                // otherwise
                let filters = Filters::from_panel(&self.left_panel);
                if filters == app.session.impact.filters {
                    return Transition::Keep;
                }

                // Avoid a double borrow
                let mut impact = std::mem::replace(&mut app.session.impact, Impact::empty(ctx));
                impact.filters = Filters::from_panel(&self.left_panel);
                ctx.loading_screen("update filters", |ctx, timer| {
                    impact.trips_changed(ctx, app, timer);
                });
                app.session.impact = impact;
                return Transition::Keep;
            }
            _ => {}
        }

        if let Some(r) = app.session.impact.compare_counts.other_event(ctx) {
            let results = ctx.loading_screen("find changed routes", |_, timer| {
                app.session.impact.find_changed_routes(app, r, timer)
            });
            return Transition::Push(ChangedRoutes::new_state(ctx, app, results));
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.top_panel.draw(g);
        self.left_panel.draw(g);
        g.redraw(&self.draw_all_neighborhoods);
        app.session.impact.compare_counts.draw(g);
        app.session.draw_all_filters.draw(g);
    }
}

impl Filters {
    fn from_panel(panel: &Panel) -> Filters {
        let (p1, p2) = (
            panel.slider("depart from").get_percent(),
            panel.slider("depart until").get_percent(),
        );
        let departure_time = (end_of_day().percent_of(p1), end_of_day().percent_of(p2));
        let modes = TripMode::all()
            .into_iter()
            .filter(|m| panel.is_checked(m.ongoing_verb()))
            .collect::<BTreeSet<_>>();
        Filters {
            modes,
            include_borders: panel.is_checked("include borders"),
            departure_time,
        }
    }

    fn to_panel(&self, ctx: &mut EventCtx, app: &App) -> Widget {
        Widget::col(vec![
            "Filter trips".text_widget(ctx),
            Toggle::switch(ctx, "include borders", None, self.include_borders),
            Widget::row(vec![
                "Departing from:".text_widget(ctx).margin_right(20),
                Slider::area(
                    ctx,
                    0.15 * ctx.canvas.window_width,
                    self.departure_time.0.to_percent(end_of_day()),
                    "depart from",
                ),
            ]),
            Widget::row(vec![
                "Departing until:".text_widget(ctx).margin_right(20),
                Slider::area(
                    ctx,
                    0.15 * ctx.canvas.window_width,
                    self.departure_time.1.to_percent(end_of_day()),
                    "depart until",
                ),
            ]),
            checkbox_per_mode(ctx, app, &self.modes),
            // TODO Filter by trip purpose
        ])
        .section(ctx)
    }
}

fn help() -> Vec<&'static str> {
    vec![
        "This tool is still experimental.",
        "Until better travel demand models are available, we can't predict where most detours will occur,",
        "because we don't know where trips begin and end.",
        "",
        "And note this tool doesn't predict traffic dissipation as people decide to not drive.",
    ]
}

struct ChangedRoutes {
    panel: Panel,
    // TODO Not sure what to precompute. Smallest memory would be the PathRequest.
    paths: Vec<(Path, Path)>,
    current: usize,
    draw_paths: Drawable,
}

impl ChangedRoutes {
    fn new_state(ctx: &mut EventCtx, app: &App, paths: Vec<(Path, Path)>) -> Box<dyn State<App>> {
        if paths.is_empty() {
            return PopupMsg::new_state(
                ctx,
                "No changes",
                vec!["No routes changed near this road"],
            );
        }

        let mut state = ChangedRoutes {
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Line("Routes that changed near a road")
                        .small_heading()
                        .into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                Widget::row(vec![
                    ctx.style()
                        .btn_prev()
                        .hotkey(Key::LeftArrow)
                        .build_widget(ctx, "previous"),
                    "path X/Y".text_widget(ctx).named("pointer").centered_vert(),
                    ctx.style()
                        .btn_next()
                        .hotkey(Key::RightArrow)
                        .build_widget(ctx, "next"),
                ])
                .evenly_spaced(),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
            paths,
            current: 0,
            draw_paths: Drawable::empty(ctx),
        };
        state.recalculate(ctx, app);
        Box::new(state)
    }

    fn recalculate(&mut self, ctx: &mut EventCtx, app: &App) {
        self.panel.replace(
            ctx,
            "pointer",
            format!("path {}/{}", self.current + 1, self.paths.len()).text_widget(ctx),
        );

        let mut batch = GeomBatch::new();
        if let Some(pl) = self.paths[self.current].0.trace(&app.map) {
            batch.push(
                *colors::PLAN_ROUTE_BEFORE,
                pl.make_polygons(5.0 * NORMAL_LANE_THICKNESS),
            );
        }
        if let Some(pl) = self.paths[self.current].1.trace(&app.map) {
            batch.push(
                *colors::PLAN_ROUTE_AFTER,
                pl.make_polygons(5.0 * NORMAL_LANE_THICKNESS),
            );
        }
        self.draw_paths = ctx.upload(batch);
    }
}

impl State<App> for ChangedRoutes {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        ctx.canvas_movement();

        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "previous" => {
                    if self.current != 0 {
                        self.current -= 1;
                    }
                    self.recalculate(ctx, app);
                }
                "next" => {
                    if self.current != self.paths.len() - 1 {
                        self.current += 1;
                    }
                    self.recalculate(ctx, app);
                }
                _ => unreachable!(),
            }
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        g.redraw(&self.draw_paths);
        app.session.draw_all_filters.draw(g);
    }
}