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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
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::{Color, Drawable, EventCtx, GfxCtx, Line, Panel, Text, TextExt, Widget};

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

pub struct BikeActivity {
    panel: Panel,
    time: Time,
    unzoomed: Drawable,
    zoomed: Drawable,
    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.cam_zoom < app.opts.min_zoom_for_detail {
            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;
        }

        Layer::simple_event(ctx, &mut self.panel)
    }
    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            g.redraw(&self.unzoomed);
        } else {
            g.redraw(&self.zoomed);
        }
        if let Some(ref txt) = self.tooltip {
            g.draw_mouse_tooltip(txt.clone());
        }
    }
    fn draw_minimap(&self, g: &mut GfxCtx) {
        g.redraw(&self.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().values() {
            if l.is_biking() {
                on_bike_lanes.add(l.parent, 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_ltr()
                    .into_iter()
                    .any(|(_, _, lt)| lt == 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(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);
        let (unzoomed, zoomed) = colorer.build(ctx);

        BikeActivity {
            panel,
            time: app.primary.sim.time(),
            unzoomed,
            zoomed,
            tooltip: None,
        }
    }
}

pub struct Static {
    panel: Panel,
    pub unzoomed: Drawable,
    pub zoomed: Drawable,
    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> {
        Layer::simple_event(ctx, &mut self.panel)
    }
    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            g.redraw(&self.unzoomed);
        } else {
            g.redraw(&self.zoomed);
        }
    }
    fn draw_minimap(&self, g: &mut GfxCtx) {
        g.redraw(&self.unzoomed);
    }
}

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

        Static {
            panel,
            unzoomed,
            zoomed,
            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 mut colorer = ColorDiscrete::new(
            app,
            vec![
                (AmenityType::Groceries.to_string(), Color::BLACK),
                (AmenityType::Food.to_string(), Color::RED),
                (AmenityType::Bar.to_string(), Color::BLUE),
                (AmenityType::Medical.to_string(), Color::PURPLE),
                (AmenityType::Religious.to_string(), Color::GREEN),
                (AmenityType::Education.to_string(), Color::CYAN),
                (AmenityType::Financial.to_string(), Color::YELLOW),
                (AmenityType::PostOffice.to_string(), Color::YELLOW),
                (AmenityType::Culture.to_string(), Color::PINK),
                (AmenityType::Childcare.to_string(), Color::ORANGE),
                (AmenityType::Shopping.to_string(), Color::WHITE),
                ("other".to_string(), Color::hex("#96322F")),
            ],
        );

        for b in app.primary.map.all_buildings() {
            let mut other = false;
            for a in &b.amenities {
                if let Some(t) = AmenityType::categorize(&a.amenity_type) {
                    colorer.add_b(b.id, t.to_string());
                } else {
                    other = true;
                }
            }
            if other {
                colorer.add_b(b.id, "other");
            }
        }

        Static::new(
            ctx,
            colorer,
            "amenities",
            "Amenities".to_string(),
            Widget::nothing(),
        )
    }

    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().values() {
            if l.is_shoulder() && !app.primary.map.get_r(l.parent).is_cycleway() {
                colorer.add_r(l.parent, "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().values() {
            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 struct CongestionCaps {
    panel: Panel,
    time: Time,
    unzoomed: Drawable,
    zoomed: Drawable,
    tooltip: Option<Text>,
}

impl Layer for CongestionCaps {
    fn name(&self) -> Option<&'static str> {
        Some("congestion caps")
    }
    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 = CongestionCaps::new(ctx, app);
            recalc_tooltip = true;
        }

        // Show a tooltip with count, only when unzoomed
        if ctx.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            if ctx.redo_mouseover() || recalc_tooltip {
                self.tooltip = None;
                match app.mouseover_unzoomed_roads_and_intersections(ctx) {
                    Some(ID::Road(r)) => {
                        if let Some(cap) = app
                            .primary
                            .map
                            .get_r(r)
                            .access_restrictions
                            .cap_vehicles_per_hour
                        {
                            self.tooltip = Some(Text::from(format!(
                                "Cap of {} vehicles per hour",
                                prettyprint_usize(cap)
                            )));
                        }
                    }
                    _ => {}
                }
            }
        } else {
            self.tooltip = None;
        }

        Layer::simple_event(ctx, &mut self.panel)
    }
    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            g.redraw(&self.unzoomed);
        } else {
            g.redraw(&self.zoomed);
        }
        if let Some(ref txt) = self.tooltip {
            g.draw_mouse_tooltip(txt.clone());
        }
    }
    fn draw_minimap(&self, g: &mut GfxCtx) {
        g.redraw(&self.unzoomed);
    }
}

impl CongestionCaps {
    pub fn new(ctx: &mut EventCtx, app: &App) -> CongestionCaps {
        let mut colorer = ColorNetwork::new(app);
        let map = &app.primary.map;

        let mut num_roads = 0;
        for r in map.all_roads() {
            if let Some(cap) = r.access_restrictions.cap_vehicles_per_hour {
                num_roads += 1;
                let current = app.primary.sim.get_cap_counter(r.id);
                let pct = ((current as f64) / (cap as f64)).min(1.0);
                colorer.add_r(r.id, app.cs.good_to_bad_red.eval(pct));
            }
        }

        let panel = Panel::new(Widget::col(vec![
            header(ctx, "Congestion caps"),
            format!("{} roads have caps", prettyprint_usize(num_roads)).text_widget(ctx),
            ColorLegend::gradient(ctx, &app.cs.good_to_bad_red, vec!["available", "full"]),
        ]))
        .aligned_pair(PANEL_PLACEMENT)
        .build(ctx);

        let (unzoomed, zoomed) = colorer.build(ctx);

        CongestionCaps {
            panel,
            time: app.primary.sim.time(),
            unzoomed,
            zoomed,
            tooltip: None,
        }
    }
}