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
use std::collections::BTreeSet;
use map_gui::tools::checkbox_per_mode;
use synthpop::{Scenario, TripMode};
use widgetry::tools::{FileLoader, PopupMsg};
use widgetry::{
Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Panel, SimpleState,
Slider, State, Text, TextExt, Toggle, VerticalAlignment, Widget,
};
use super::{end_of_day, Filters, Impact};
use crate::{App, BrowseNeighborhoods, Transition};
pub struct ShowResults {
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| {
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.change_key {
ctx.loading_screen("recalculate impact", |ctx, timer| {
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 panel = Panel::new_builder(Widget::col(vec![
crate::app_header(ctx, app),
"Impact prediction".text_widget(ctx),
ctx.style().btn_back("Browse neighborhoods").hotkey(Key::Escape).build_def(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),
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),
]))
.aligned(HorizontalAlignment::Left, VerticalAlignment::Top)
.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);
<dyn SimpleState<_>>::new_state(
panel,
Box::new(ShowResults {
draw_all_neighborhoods,
}),
)
}
}
impl SimpleState<App> for ShowResults {
fn on_click(
&mut self,
ctx: &mut EventCtx,
app: &mut App,
x: &str,
panel: &mut Panel,
) -> Transition {
match x {
"Browse neighborhoods" => {
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,
);
Transition::Push(PopupMsg::new_state(
ctx,
"Saved",
vec![format!("Saved {} and {}", path1, path2)],
))
}
x => {
if let Some(t) = crate::handle_app_header_click(ctx, app, x) {
return t;
}
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;
panel.replace(ctx, "compare counts", widget);
Transition::Keep
}
}
}
fn other_event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
app.session.impact.compare_counts.other_event(ctx);
Transition::Keep
}
fn panel_changed(
&mut self,
ctx: &mut EventCtx,
app: &mut App,
panel: &mut Panel,
) -> Option<Transition> {
let filters = Filters::from_panel(panel);
if filters == app.session.impact.filters {
return None;
}
let mut impact = std::mem::replace(&mut app.session.impact, Impact::empty(ctx));
impact.filters = Filters::from_panel(panel);
ctx.loading_screen("update filters", |ctx, timer| {
impact.trips_changed(ctx, app, timer);
});
app.session.impact = impact;
None
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
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: &mut 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),
])
.section(ctx)
}
}