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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::collections::HashSet;
use abstutil::Counter;
use map_gui::tools::{ColorNetwork, DrawSimpleRoadLabels};
use widgetry::mapspace::{World, WorldOutcome};
use widgetry::{
Choice, Color, DrawBaselayer, Drawable, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, Panel,
State, TextExt, Toggle, Widget,
};
use crate::edit::EditMode;
use crate::filters::auto::Heuristic;
use crate::{colors, App, Neighbourhood, NeighbourhoodID, Transition};
pub struct BrowseNeighbourhoods {
top_panel: Panel,
left_panel: Panel,
world: World<NeighbourhoodID>,
draw_over_roads: Drawable,
labels: DrawSimpleRoadLabels,
draw_boundary_roads: Drawable,
}
impl BrowseNeighbourhoods {
pub fn new_state(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
map_gui::tools::update_url_map_name(app);
if let EditMode::Shortcuts(ref mut maybe_focus) = app.session.edit_mode {
*maybe_focus = None;
}
if let EditMode::FreehandFilters(_) = app.session.edit_mode {
app.session.edit_mode = EditMode::Filters;
}
let (world, draw_over_roads) =
ctx.loading_screen("calculate neighbourhoods", |ctx, timer| {
if &app.session.partitioning.map != app.map.get_name() {
app.session.alt_proposals = crate::save::AltProposals::new();
crate::clear_current_proposal(ctx, app, timer);
}
(make_world(ctx, app), draw_over_roads(ctx, app))
});
let top_panel = crate::components::TopPanel::panel(ctx, app);
let left_panel = crate::components::LeftPanel::builder(
ctx,
&top_panel,
Widget::col(vec![
app.session.alt_proposals.to_widget(ctx, app),
crate::route_planner::RoutePlanner::button(ctx),
Toggle::checkbox(ctx, "Advanced features", None, app.opts.dev),
advanced_panel(ctx, app),
]),
)
.build(ctx);
Box::new(BrowseNeighbourhoods {
top_panel,
left_panel,
world,
draw_over_roads,
labels: DrawSimpleRoadLabels::only_major_roads(colors::ROAD_LABEL),
draw_boundary_roads: draw_boundary_roads(ctx, app),
})
}
pub fn button(ctx: &EventCtx, app: &App) -> Widget {
ctx.style()
.btn_back("Browse neighbourhoods")
.hotkey(Key::Escape)
.build_def(ctx)
.hide(app.session.consultation.is_some())
}
}
impl State<App> for BrowseNeighbourhoods {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
if let Some(t) = crate::components::TopPanel::event(ctx, app, &mut self.top_panel, help) {
return t;
}
match self.left_panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"Calculate" | "Show impact" => {
return Transition::Push(crate::impact::ShowResults::new_state(ctx, app));
}
"Plan a route" => {
return Transition::Push(crate::route_planner::RoutePlanner::new_state(
ctx, app,
));
}
"Automatically place filters" => {
ctx.loading_screen("automatically filter all neighbourhoods", |ctx, timer| {
timer.start_iter(
"filter neighbourhood",
app.session.partitioning.all_neighbourhoods().len(),
);
for id in app
.session
.partitioning
.all_neighbourhoods()
.keys()
.cloned()
.collect::<Vec<_>>()
{
timer.next();
let neighbourhood = Neighbourhood::new(ctx, app, id);
let _ = app.session.heuristic.apply(ctx, app, &neighbourhood, timer);
}
});
return Transition::Replace(BrowseNeighbourhoods::new_state(ctx, app));
}
x => {
return crate::save::AltProposals::handle_action(
ctx,
app,
crate::save::PreserveState::BrowseNeighbourhoods,
x,
)
.unwrap();
}
},
Outcome::Changed(x) => {
if x == "Advanced features" {
app.opts.dev = self.left_panel.is_checked("Advanced features");
return Transition::Replace(BrowseNeighbourhoods::new_state(ctx, app));
}
if x == "heuristic" {
app.session.heuristic = self.left_panel.dropdown_value("heuristic");
} else if x == "style" {
app.session.draw_neighbourhood_style = self.left_panel.dropdown_value("style");
ctx.loading_screen("change style", |ctx, _| {
self.world = make_world(ctx, app);
self.draw_over_roads = draw_over_roads(ctx, app);
});
}
}
_ => {}
}
if let WorldOutcome::ClickedObject(id) = self.world.event(ctx) {
return Transition::Push(crate::connectivity::Viewer::new_state(ctx, app, id));
}
Transition::Keep
}
fn draw_baselayer(&self) -> DrawBaselayer {
DrawBaselayer::Custom
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
crate::draw_with_layering(g, app, |g| self.world.draw(g));
self.draw_over_roads.draw(g);
self.top_panel.draw(g);
self.left_panel.draw(g);
self.draw_boundary_roads.draw(g);
self.labels.draw(g, app);
app.session.draw_all_filters.draw(g);
}
}
fn make_world(ctx: &mut EventCtx, app: &App) -> World<NeighbourhoodID> {
let mut world = World::bounded(app.map.get_bounds());
let map = &app.map;
for (id, info) in app.session.partitioning.all_neighbourhoods() {
match app.session.draw_neighbourhood_style {
Style::Simple => {
world
.add(*id)
.hitbox(info.block.polygon.clone())
.drawn_in_master_batch()
.draw_hovered(GeomBatch::from(vec![(
Color::YELLOW.alpha(0.5),
info.block.polygon.clone(),
)]))
.clickable()
.build(ctx);
}
Style::Cells => {
let neighbourhood = Neighbourhood::new(ctx, app, *id);
let render_cells = crate::draw_cells::RenderCells::new(map, &neighbourhood);
let hovered_batch = render_cells.draw_colored_areas();
world
.add(*id)
.hitbox(info.block.polygon.clone())
.drawn_in_master_batch()
.draw_hovered(hovered_batch)
.clickable()
.build(ctx);
}
Style::Quietness => {
let neighbourhood = Neighbourhood::new(ctx, app, *id);
let (quiet_streets, total_streets) = neighbourhood
.shortcuts
.quiet_and_total_streets(&neighbourhood);
let pct = if total_streets == 0 {
0.0
} else {
1.0 - (quiet_streets as f64 / total_streets as f64)
};
let color = app.cs.good_to_bad_red.eval(pct);
world
.add(*id)
.hitbox(info.block.polygon.clone())
.draw_color(color.alpha(0.5))
.hover_color(colors::HOVER)
.clickable()
.build(ctx);
}
Style::Shortcuts => {
world
.add(*id)
.hitbox(info.block.polygon.clone())
.drawn_in_master_batch()
.hover_color(colors::HOVER)
.clickable()
.build(ctx);
}
}
}
world
}
fn draw_over_roads(ctx: &mut EventCtx, app: &App) -> Drawable {
if app.session.draw_neighbourhood_style != Style::Shortcuts {
return Drawable::empty(ctx);
}
let mut count_per_road = Counter::new();
let mut count_per_intersection = Counter::new();
for id in app.session.partitioning.all_neighbourhoods().keys() {
let neighbourhood = Neighbourhood::new(ctx, app, *id);
count_per_road.extend(neighbourhood.shortcuts.count_per_road);
count_per_intersection.extend(neighbourhood.shortcuts.count_per_intersection);
}
let mut colorer = ColorNetwork::no_fading(app);
colorer.ranked_roads(count_per_road, &app.cs.good_to_bad_red);
colorer.ranked_intersections(count_per_intersection, &app.cs.good_to_bad_red);
colorer.build(ctx).unzoomed
}
pub fn draw_boundary_roads(ctx: &EventCtx, app: &App) -> Drawable {
let mut seen_roads = HashSet::new();
let mut seen_borders = HashSet::new();
let mut batch = GeomBatch::new();
for info in app.session.partitioning.all_neighbourhoods().values() {
for id in &info.block.perimeter.roads {
let r = id.road;
if seen_roads.contains(&r) {
continue;
}
seen_roads.insert(r);
let road = app.map.get_r(r);
batch.push(colors::HIGHLIGHT_BOUNDARY, road.get_thick_polygon());
for i in [road.src_i, road.dst_i] {
if seen_borders.contains(&i) {
continue;
}
seen_borders.insert(i);
batch.push(colors::HIGHLIGHT_BOUNDARY, app.map.get_i(i).polygon.clone());
}
}
}
batch.build(ctx)
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Style {
Simple,
Cells,
Quietness,
Shortcuts,
}
fn impact_widget(ctx: &EventCtx, app: &App) -> Widget {
if &app.session.impact.map == app.map.get_name()
&& app.session.impact.change_key == app.session.modal_filters.get_change_key()
{
return ctx.style().btn_outline.text("Show impact").build_def(ctx);
}
Widget::col(vec![
Line("The app may freeze while calculating this.")
.small()
.into_widget(ctx),
ctx.style().btn_outline.text("Calculate").build_def(ctx),
])
}
fn help() -> Vec<&'static str> {
vec![
"Basic map navigation: click and drag to pan, swipe or scroll to zoom",
"",
"Click a neighbourhood to analyze it. You can adjust boundaries there.",
]
}
fn advanced_panel(ctx: &EventCtx, app: &App) -> Widget {
if !app.opts.dev {
return Widget::nothing();
}
Widget::col(vec![
Line("Advanced features").small_heading().into_widget(ctx),
Widget::col(vec![Widget::row(vec![
"Draw neighbourhoods:".text_widget(ctx).centered_vert(),
Widget::dropdown(
ctx,
"style",
app.session.draw_neighbourhood_style,
vec![
Choice::new("simple", Style::Simple),
Choice::new("cells", Style::Cells),
Choice::new("quietness", Style::Quietness),
Choice::new("all shortcuts", Style::Shortcuts),
],
),
])])
.section(ctx),
Widget::col(vec![
"Predict proposal impact".text_widget(ctx),
impact_widget(ctx, app),
])
.section(ctx),
Widget::col(vec![
ctx.style()
.btn_outline
.text("Automatically place filters")
.build_def(ctx),
Widget::dropdown(
ctx,
"heuristic",
app.session.heuristic,
Heuristic::choices(),
),
])
.section(ctx),
])
}