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
use maplit::btreeset;

use abstutil::{prettyprint_usize, Counter};
use geom::{Distance, Time};
use map_gui::tools::{ColorDiscrete, ColorLegend, ColorNetwork};
use map_gui::ID;
use map_model::{AmenityType, LaneType};
use sim::AgentType;
use widgetry::mapspace::ToggleZoomed;
use widgetry::{Color, EventCtx, GfxCtx, Line, Panel, Text, Widget};

use crate::app::App;
use crate::layer::{header, Layer, LayerOutcome, PANEL_PLACEMENT};

pub struct BikeActivity {
    panel: Panel,
    time: Time,
    draw: ToggleZoomed,
    tooltip: Option<Text>,
}

impl Layer for BikeActivity {
    fn name(&self) -> Option<&'static str> {
        Some("cycling activity")
    }
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Option<LayerOutcome> {
        let mut recalc_tooltip = false;
        if app.primary.sim.time() != self.time {
            *self = BikeActivity::new(ctx, app);
            recalc_tooltip = true;
        }

        // Show a tooltip with count, only when unzoomed
        if ctx.canvas.is_unzoomed() {
            if ctx.redo_mouseover() || recalc_tooltip {
                self.tooltip = None;
                if let Some(ID::Road(r)) = app.mouseover_unzoomed_roads_and_intersections(ctx) {
                    let cnt = app
                        .primary
                        .sim
                        .get_analytics()
                        .road_thruput
                        .total_for_with_agent_types(r, btreeset! { AgentType::Bike });
                    if cnt > 0 {
                        self.tooltip = Some(Text::from(prettyprint_usize(cnt)));
                    }
                }
            }
        } else {
            self.tooltip = None;
        }

        <dyn Layer>::simple_event(ctx, &mut self.panel)
    }
    fn draw(&self, g: &mut GfxCtx, _: &App) {
        self.panel.draw(g);
        self.draw.draw(g);
        if let Some(ref txt) = self.tooltip {
            g.draw_mouse_tooltip(txt.clone());
        }
    }
    fn draw_minimap(&self, g: &mut GfxCtx) {
        g.redraw(&self.draw.unzoomed);
    }
}

impl BikeActivity {
    pub fn new(ctx: &mut EventCtx, app: &App) -> BikeActivity {
        let mut num_lanes = 0;
        let mut total_dist = Distance::ZERO;
        let mut on_bike_lanes = Counter::new();
        let mut off_bike_lanes = Counter::new();
        let mut intersections_on = Counter::new();
        let mut intersections_off = Counter::new();
        // Make sure all bikes lanes show up no matter what
        for l in app.primary.map.all_lanes() {
            if l.is_biking() {
                on_bike_lanes.add(l.id.road, 0);
                intersections_on.add(l.src_i, 0);
                intersections_on.add(l.src_i, 0);
                num_lanes += 1;
                total_dist += l.length();
            }
        }

        // Show throughput, broken down by bike lanes or not
        for ((r, agent_type, _), count) in &app.primary.sim.get_analytics().road_thruput.counts {
            if *agent_type == AgentType::Bike {
                if app
                    .primary
                    .map
                    .get_r(*r)
                    .lanes
                    .iter()
                    .any(|l| l.lane_type == LaneType::Biking)
                {
                    on_bike_lanes.add(*r, *count);
                } else {
                    off_bike_lanes.add(*r, *count);
                }
            }
        }

        // Use intersection data too, but bin as on bike lanes or not based on connecting roads
        for ((i, agent_type, _), count) in
            &app.primary.sim.get_analytics().intersection_thruput.counts
        {
            if *agent_type == AgentType::Bike {
                if app
                    .primary
                    .map
                    .get_i(*i)
                    .roads
                    .iter()
                    .any(|r| on_bike_lanes.get(*r) > 0)
                {
                    intersections_on.add(*i, *count);
                } else {
                    intersections_off.add(*i, *count);
                }
            }
        }

        let panel = Panel::new_builder(Widget::col(vec![
            header(ctx, "Cycling activity"),
            Text::from_multiline(vec![
                Line(format!("{} bike lanes", num_lanes)),
                Line(format!(
                    "total distance of {}",
                    total_dist.to_string(&app.opts.units)
                )),
            ])
            .into_widget(ctx),
            Line("Throughput on bike lanes").into_widget(ctx),
            ColorLegend::gradient(
                ctx,
                &app.cs.good_to_bad_green,
                vec!["lowest count", "highest"],
            ),
            Line("Throughput on unprotected roads").into_widget(ctx),
            ColorLegend::gradient(
                ctx,
                &app.cs.good_to_bad_red,
                vec!["lowest count", "highest"],
            ),
        ]))
        .aligned_pair(PANEL_PLACEMENT)
        .build(ctx);

        let mut colorer = ColorNetwork::new(app);
        colorer.ranked_roads(on_bike_lanes, &app.cs.good_to_bad_green);
        colorer.ranked_roads(off_bike_lanes, &app.cs.good_to_bad_red);
        colorer.ranked_intersections(intersections_on, &app.cs.good_to_bad_green);
        colorer.ranked_intersections(intersections_off, &app.cs.good_to_bad_red);

        BikeActivity {
            panel,
            time: app.primary.sim.time(),
            draw: colorer.build(ctx),
            tooltip: None,
        }
    }
}

pub struct Static {
    panel: Panel,
    pub draw: ToggleZoomed,
    name: &'static str,
}

impl Layer for Static {
    fn name(&self) -> Option<&'static str> {
        Some(self.name)
    }
    fn event(&mut self, ctx: &mut EventCtx, _: &mut App) -> Option<LayerOutcome> {
        <dyn Layer>::simple_event(ctx, &mut self.panel)
    }
    fn draw(&self, g: &mut GfxCtx, _: &App) {
        self.panel.draw(g);
        self.draw.draw(g);
    }
    fn draw_minimap(&self, g: &mut GfxCtx) {
        g.redraw(&self.draw.unzoomed);
    }
}

impl Static {
    fn new(
        ctx: &mut EventCtx,
        colorer: ColorDiscrete,
        name: &'static str,
        title: String,
        extra: Widget,
    ) -> Static {
        let (draw, legend) = colorer.build(ctx);
        let panel = Panel::new_builder(Widget::col(vec![header(ctx, &title), extra, legend]))
            .aligned_pair(PANEL_PLACEMENT)
            .build(ctx);

        Static { panel, draw, name }
    }

    pub fn edits(ctx: &mut EventCtx, app: &App) -> Static {
        let mut colorer = ColorDiscrete::new(
            app,
            vec![("modified road/intersection", app.cs.edits_layer)],
        );

        let edits = app.primary.map.get_edits();
        let (lanes, roads) = edits.changed_lanes(&app.primary.map);
        for l in lanes {
            colorer.add_l(l, "modified road/intersection");
        }
        for r in roads {
            colorer.add_r(r, "modified road/intersection");
        }
        for i in edits.original_intersections.keys() {
            colorer.add_i(*i, "modified road/intersection");
        }

        Static::new(
            ctx,
            colorer,
            "map edits",
            format!("Map edits ({})", edits.edits_name),
            Text::from_multiline(vec![
                Line(format!("{} roads changed", edits.changed_roads.len())),
                Line(format!(
                    "{} intersections changed",
                    edits.original_intersections.len()
                )),
            ])
            .into_widget(ctx),
        )
    }

    pub fn amenities(ctx: &mut EventCtx, app: &App) -> Static {
        let food = Color::RED;
        let school = Color::CYAN;
        let shopping = Color::PURPLE;
        let other = Color::GREEN;

        let mut draw = ToggleZoomed::builder();
        for b in app.primary.map.all_buildings() {
            if b.amenities.is_empty() {
                continue;
            }
            let mut color = None;
            for a in &b.amenities {
                if let Some(t) = AmenityType::categorize(&a.amenity_type) {
                    color = Some(match t {
                        AmenityType::Food => food,
                        AmenityType::School => school,
                        AmenityType::Shopping => shopping,
                        _ => other,
                    });
                    break;
                }
            }
            let color = color.unwrap_or(other);
            draw.unzoomed.push(color, b.polygon.clone());
            draw.zoomed.push(color.alpha(0.4), b.polygon.clone());
        }

        let panel = Panel::new_builder(Widget::col(vec![
            header(ctx, "Amenities"),
            ColorLegend::row(ctx, food, AmenityType::Food.to_string()),
            ColorLegend::row(ctx, school, AmenityType::School.to_string()),
            ColorLegend::row(ctx, shopping, AmenityType::Shopping.to_string()),
            ColorLegend::row(ctx, other, "other".to_string()),
        ]))
        .aligned_pair(PANEL_PLACEMENT)
        .build(ctx);

        Static {
            panel,
            draw: draw.build(ctx),
            name: "amenities",
        }
    }

    pub fn no_sidewalks(ctx: &mut EventCtx, app: &App) -> Static {
        let mut colorer = ColorDiscrete::new(app, vec![("no sidewalks", Color::RED)]);
        for l in app.primary.map.all_lanes() {
            if l.is_shoulder() && !app.primary.map.get_parent(l.id).is_cycleway() {
                colorer.add_r(l.id.road, "no sidewalks");
            }
        }
        Static::new(
            ctx,
            colorer,
            "no sidewalks",
            "No sidewalks".to_string(),
            Widget::nothing(),
        )
    }

    pub fn blackholes(ctx: &mut EventCtx, app: &App) -> Static {
        let mut colorer = ColorDiscrete::new(
            app,
            vec![
                ("driving blackhole", Color::RED),
                ("biking blackhole", Color::GREEN),
                ("driving + biking blackhole", Color::BLUE),
            ],
        );
        for l in app.primary.map.all_lanes() {
            if l.driving_blackhole && l.biking_blackhole {
                colorer.add_l(l.id, "driving + biking blackhole");
            } else if l.driving_blackhole {
                colorer.add_l(l.id, "driving blackhole");
            } else if l.biking_blackhole {
                colorer.add_l(l.id, "biking blackhole");
            }
        }
        Static::new(
            ctx,
            colorer,
            "blackholes",
            "blackholes".to_string(),
            Widget::nothing(),
        )
    }

    pub fn high_stress(ctx: &mut EventCtx, app: &App) -> Static {
        let mut colorer = ColorDiscrete::new(app, vec![("high stress", app.cs.edits_layer)]);

        for r in app.primary.map.all_roads() {
            if r.high_stress_for_bikes(&app.primary.map) {
                colorer.add_r(r.id, "high stress");
            }
        }

        Static::new(
            ctx,
            colorer,
            "high stress",
            "High stress roads for biking".to_string(),
            Text::from_multiline(vec![
                Line("High stress defined as:"),
                Line("- arterial classification"),
                Line("- no dedicated cycle lane"),
            ])
            .into_widget(ctx),
        )
    }
}