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
use geom::Distance;
use map_gui::tools::{CityPicker, DrawRoadLabels};
use map_model::{Block, RoadID};
use widgetry::mapspace::{ObjectID, World, WorldOutcome};
use widgetry::{
Color, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Outcome, Panel, State, TextExt,
Toggle, VerticalAlignment, Widget,
};
use super::{BrowseNeighborhoods, Neighborhood};
use crate::app::{App, Transition};
pub struct Viewer {
panel: Panel,
neighborhood: Neighborhood,
world: World<Obj>,
labels: DrawRoadLabels,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
enum Obj {
InteriorRoad(RoadID),
}
impl ObjectID for Obj {}
impl Viewer {
pub fn new_state(ctx: &mut EventCtx, app: &App, block: &Block) -> Box<dyn State<App>> {
let panel = Panel::new_builder(Widget::col(vec![
map_gui::tools::app_header(ctx, app, "Low traffic neighborhoods"),
ctx.style()
.btn_outline
.text("Browse neighborhoods")
.hotkey(Key::Escape)
.build_def(ctx),
ctx.style()
.btn_outline
.text("Browse rat-runs")
.hotkey(Key::R)
.build_def(ctx),
Widget::row(vec![
"Draw traffic cells as".text_widget(ctx).centered_vert(),
Toggle::choice(ctx, "draw cells", "areas", "streets", Key::C, true),
]),
"Click a road to add or remove a modal filter".text_widget(ctx),
]))
.aligned(HorizontalAlignment::Left, VerticalAlignment::Top)
.build(ctx);
let neighborhood = Neighborhood::new(ctx, app, block.perimeter.clone());
let mut label_roads = neighborhood.perimeter.clone();
label_roads.extend(neighborhood.orig_perimeter.interior.clone());
let world = make_world(ctx, app, &neighborhood, panel.is_checked("draw cells"));
Box::new(Viewer {
panel,
neighborhood,
world,
labels: DrawRoadLabels::new(Box::new(move |r| label_roads.contains(&r.id))),
})
}
}
impl State<App> for Viewer {
fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
match self.panel.event(ctx) {
Outcome::Clicked(x) => match x.as_ref() {
"Home" => {
return Transition::Clear(vec![crate::pregame::TitleScreen::new_state(
ctx, app,
)]);
}
"change map" => {
return Transition::Push(CityPicker::new_state(
ctx,
app,
Box::new(|ctx, app| {
Transition::Replace(BrowseNeighborhoods::new_state(ctx, app))
}),
));
}
"Browse neighborhoods" => {
return Transition::Pop;
}
"Browse rat-runs" => {
return Transition::Push(super::rat_run_viewer::BrowseRatRuns::new_state(
ctx,
app,
&self.neighborhood,
));
}
_ => unreachable!(),
},
Outcome::Changed(_) => {
self.world = make_world(
ctx,
app,
&self.neighborhood,
self.panel.is_checked("draw cells"),
);
}
_ => {}
}
if let WorldOutcome::ClickedObject(Obj::InteriorRoad(r)) = self.world.event(ctx) {
if app.session.modal_filters.roads.contains_key(&r) {
app.session.modal_filters.roads.remove(&r);
} else {
let road = app.primary.map.get_r(r);
let pct_along = if self.neighborhood.borders.contains(&road.src_i) {
0.1
} else if self.neighborhood.borders.contains(&road.dst_i) {
0.9
} else {
0.5
};
app.session
.modal_filters
.roads
.insert(r, pct_along * road.length());
}
self.neighborhood =
Neighborhood::new(ctx, app, self.neighborhood.orig_perimeter.clone());
self.world = make_world(
ctx,
app,
&self.neighborhood,
self.panel.is_checked("draw cells"),
);
}
Transition::Keep
}
fn draw(&self, g: &mut GfxCtx, app: &App) {
self.panel.draw(g);
g.redraw(&self.neighborhood.fade_irrelevant);
self.world.draw(g);
g.redraw(&self.neighborhood.draw_filters);
if g.canvas.is_unzoomed() {
self.labels.draw(g, app);
}
}
}
fn make_world(
ctx: &mut EventCtx,
app: &App,
neighborhood: &Neighborhood,
draw_cells_as_areas: bool,
) -> World<Obj> {
let map = &app.primary.map;
let mut world = World::bounded(map.get_bounds());
if draw_cells_as_areas {
for (_, cells) in neighborhood.cells.iter().enumerate() {
for r in cells {
world
.add(Obj::InteriorRoad(*r))
.hitbox(map.get_r(*r).get_thick_polygon())
.drawn_in_master_batch()
.hover_outline(Color::BLACK, Distance::meters(5.0))
.clickable()
.build(ctx);
}
}
world.draw_master_batch(ctx, super::draw_cells::draw_cells(map, neighborhood));
} else {
let mut draw_intersections = GeomBatch::new();
for (idx, cells) in neighborhood.cells.iter().enumerate() {
let color = super::draw_cells::COLORS[idx % super::draw_cells::COLORS.len()].alpha(0.9);
for r in cells {
world
.add(Obj::InteriorRoad(*r))
.hitbox(map.get_r(*r).get_thick_polygon())
.draw_color(color)
.hover_outline(Color::BLACK, Distance::meters(5.0))
.clickable()
.build(ctx);
}
for i in crate::common::intersections_from_roads(cells, map) {
draw_intersections.push(color, map.get_i(i).polygon.clone());
}
}
world.draw_master_batch(ctx, draw_intersections);
}
world.initialize_hover(ctx);
world
}