ltn/components/
layers.rs

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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
use geom::Polygon;
use map_gui::colors::ColorScheme;
use map_model::{CrossingType, FilterType};
use widgetry::tools::ColorLegend;
use widgetry::{
    ButtonBuilder, Color, ControlState, EdgeInsets, EventCtx, GeomBatch, GfxCtx,
    HorizontalAlignment, Image, Key, Line, Outcome, Panel, RoundedF64, Spinner, TextExt, Toggle,
    VerticalAlignment, Widget,
};

use crate::components::Mode;
use crate::render::{colors, filter_svg_path};
use crate::{pages, App, Transition};

// Partly copied from ungap/layers.s

pub struct Layers {
    panel: Panel,
    minimized: bool,
    // (Mode, max zoom, min zoom, bottom bar position)
    panel_cache_key: (Mode, bool, bool, Option<f64>),
    show_bus_routes: bool,
    show_turn_restrictions: bool,
    pub show_crossing_time: bool,

    // For the design LTN mode
    pub autofix_bus_gates: bool,
    pub autofix_one_ways: bool,
}

impl Layers {
    /// Panel won't be initialized, must call `event` first
    pub fn new(ctx: &mut EventCtx) -> Layers {
        Self {
            panel: Panel::empty(ctx),
            minimized: true,
            panel_cache_key: (Mode::Impact, false, false, None),
            show_bus_routes: false,
            show_turn_restrictions: true,
            show_crossing_time: false,

            autofix_bus_gates: false,
            autofix_one_ways: false,
        }
    }

    pub fn event(
        &mut self,
        ctx: &mut EventCtx,
        cs: &ColorScheme,
        mode: Mode,
        bottom_panel: Option<&Panel>,
    ) -> Option<Transition> {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => {
                match x.as_ref() {
                    "zoom map out" => {
                        ctx.canvas.center_zoom(-8.0);
                    }
                    "zoom map in" => {
                        ctx.canvas.center_zoom(8.0);
                    }
                    "hide layers" => {
                        self.minimized = true;
                    }
                    "show layers" => {
                        self.minimized = false;
                    }
                    _ => unreachable!(),
                }
                self.update_panel(ctx, cs, bottom_panel);
                return Some(Transition::Keep);
            }
            Outcome::Changed(x) => {
                if x == "show bus routes" {
                    self.show_bus_routes = self.panel.is_checked(&x);
                    self.update_panel(ctx, cs, bottom_panel);
                    return Some(Transition::Keep);
                } else if x == "show turn restrictions" {
                    self.show_turn_restrictions = self.panel.is_checked(&x);
                    self.update_panel(ctx, cs, bottom_panel);
                    return Some(Transition::Keep);
                } else if x == "show time to nearest crossing" {
                    self.show_crossing_time = self.panel.is_checked(&x);
                    self.update_panel(ctx, cs, bottom_panel);
                    return Some(Transition::Keep);
                } else if x == "Use bus gates when needed" {
                    self.autofix_bus_gates = self.panel.is_checked(&x);
                    self.update_panel(ctx, cs, bottom_panel);
                    return Some(Transition::Keep);
                } else if x == "Fix one-way streets when needed" {
                    self.autofix_one_ways = self.panel.is_checked(&x);
                    self.update_panel(ctx, cs, bottom_panel);
                    return Some(Transition::Keep);
                }

                ctx.set_scale_factor(self.panel.spinner::<RoundedF64>("scale_factor").0);
                // TODO This doesn't seem to do mark_covered_area correctly, so using the scroll
                // wheel on the spinner just scrolls the canvas
                self.update_panel(ctx, cs, bottom_panel);
                return Some(Transition::Recreate);
            }
            _ => {}
        }

        let cache_key = (
            mode,
            ctx.canvas.is_max_zoom(),
            ctx.canvas.is_min_zoom(),
            bottom_panel.map(|p| p.panel_rect().y1),
        );
        if self.panel_cache_key != cache_key {
            self.panel_cache_key = cache_key;
            self.update_panel(ctx, cs, bottom_panel);
        }

        None
    }

    // Draw after road labels
    pub fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.panel.draw(g);
        if self.show_bus_routes {
            g.redraw(&app.per_map.draw_bus_routes);
        }
        if self.show_turn_restrictions {
            g.redraw(&app.per_map.draw_turn_restrictions);
        }
    }

    pub fn show_bus_routes(
        &mut self,
        ctx: &mut EventCtx,
        cs: &ColorScheme,
        bottom_panel: Option<&Panel>,
    ) {
        self.minimized = false;
        self.show_bus_routes = true;
        self.update_panel(ctx, cs, bottom_panel);
    }

    pub fn show_panel(
        &mut self,
        ctx: &mut EventCtx,
        cs: &ColorScheme,
        bottom_panel: Option<&Panel>,
    ) {
        self.minimized = false;
        self.update_panel(ctx, cs, bottom_panel);
    }

    fn update_panel(&mut self, ctx: &mut EventCtx, cs: &ColorScheme, bottom_panel: Option<&Panel>) {
        let mut builder = Panel::new_builder(
            Widget::col(vec![
                make_zoom_controls(ctx).align_right(),
                self.make_legend(ctx, cs).bg(ctx.style().panel_bg),
            ])
            .padding_right(16),
        )
        .aligned(HorizontalAlignment::Right, VerticalAlignment::Bottom);
        if let Some(bottom_panel) = bottom_panel {
            let buffer = 5.0;
            builder = builder.aligned(
                HorizontalAlignment::Right,
                VerticalAlignment::Above(bottom_panel.panel_rect().y1 - buffer),
            );
        }
        self.panel = builder.build_custom(ctx);
    }

    fn make_legend(&self, ctx: &mut EventCtx, cs: &ColorScheme) -> Widget {
        if self.minimized {
            return ctx
                .style()
                .btn_plain
                .icon("system/assets/tools/layers.svg")
                .hotkey(Key::L)
                .build_widget(ctx, "show layers")
                .centered_horiz();
        }

        Widget::col(vec![
            Widget::row(vec![
                Image::from_path("system/assets/tools/layers.svg")
                    .dims(30.0)
                    .into_widget(ctx)
                    .centered_vert()
                    .named("layer icon"),
                ctx.style()
                    .btn_plain
                    .icon("system/assets/tools/minimize.svg")
                    .hotkey(Key::L)
                    .build_widget(ctx, "hide layers")
                    .align_right(),
            ]),
            self.panel_cache_key.0.legend(ctx, cs, self),
            {
                let checkbox = Toggle::checkbox(ctx, "show bus routes", None, self.show_bus_routes);
                if self.show_bus_routes {
                    checkbox.outline((1.0, *colors::BUS_ROUTE))
                } else {
                    checkbox
                }
            },
            Toggle::checkbox(
                ctx,
                "show turn restrictions",
                None,
                self.show_turn_restrictions,
            ),
            if self.panel_cache_key.0 == Mode::Crossings {
                Widget::col(vec![
                    Toggle::checkbox(
                        ctx,
                        "show time to nearest crossing",
                        None,
                        self.show_crossing_time,
                    ),
                    Widget::row(vec![
                        // TODO White = none
                        "Time:".text_widget(ctx),
                        ColorLegend::gradient_with_width(
                            ctx,
                            &cs.good_to_bad_red,
                            vec!["< 1 min", "> 5 mins"],
                            150.0,
                        ),
                    ])
                    .hide(!self.show_crossing_time),
                ])
            } else {
                Widget::nothing()
            },
            Widget::row(vec![
                "Adjust the size of text:".text_widget(ctx).centered_vert(),
                Spinner::f64_widget(
                    ctx,
                    "scale_factor",
                    (0.5, 2.5),
                    ctx.prerender.get_scale_factor(),
                    0.1,
                ),
            ]),
        ])
        .padding(16)
    }
}

fn make_zoom_controls(ctx: &mut EventCtx) -> Widget {
    let builder = ctx
        .style()
        .btn_floating
        .btn()
        .image_dims(30.0)
        .outline((1.0, ctx.style().btn_plain.fg), ControlState::Default)
        .padding(12.0);

    Widget::custom_col(vec![
        builder
            .clone()
            .image_path("system/assets/speed/plus.svg")
            .corner_rounding(geom::CornerRadii {
                top_left: 16.0,
                top_right: 16.0,
                bottom_right: 0.0,
                bottom_left: 0.0,
            })
            .disabled(ctx.canvas.is_max_zoom())
            .build_widget(ctx, "zoom map in"),
        builder
            .image_path("system/assets/speed/minus.svg")
            .image_dims(30.0)
            .padding(12.0)
            .corner_rounding(geom::CornerRadii {
                top_left: 0.0,
                top_right: 0.0,
                bottom_right: 16.0,
                bottom_left: 16.0,
            })
            .disabled(ctx.canvas.is_min_zoom())
            .build_widget(ctx, "zoom map out"),
    ])
}

impl Mode {
    fn legend(&self, ctx: &mut EventCtx, cs: &ColorScheme, layers: &Layers) -> Widget {
        // TODO Light/dark buildings? Traffic signals?

        Widget::col(match self {
            Mode::PickArea => vec![
                entry_tooltip(
                    ctx,
                    Color::BLACK,
                    "main road",
                    "Classified as non-local, designed for through-traffic",
                ),
                entry_tooltip(
                    ctx,
                    Color::YELLOW.alpha(0.2),
                    "neighbourhood",
                    "Analyze through-traffic here",
                ),
            ],
            Mode::ModifyNeighbourhood => vec![
                Widget::row(vec![
                    // TODO White = none
                    "Shortcuts:".text_widget(ctx),
                    ColorLegend::gradient_with_width(
                        ctx,
                        &cs.good_to_bad_red,
                        vec!["low", "high"],
                        150.0,
                    ),
                ]),
                Widget::row(vec!["Cells:".text_widget(ctx), color_grid(ctx)]),
                Widget::row(vec![
                    "Modal filters:".text_widget(ctx),
                    Image::from_path(filter_svg_path(FilterType::WalkCycleOnly))
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                    Image::from_path(filter_svg_path(FilterType::NoEntry))
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                    Image::from_path(filter_svg_path(FilterType::BusGate))
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                    Image::from_path(filter_svg_path(FilterType::SchoolStreet))
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                ]),
                Line("Faded filters exist already").small().into_widget(ctx),
                Widget::row(vec![
                    "Private road:".text_widget(ctx),
                    Image::from_path("system/assets/map/private_road.svg")
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                ]),
                // TODO Entry/exit arrows?
                // TODO Dashed roads are walk/bike
                Toggle::checkbox(
                    ctx,
                    "Use bus gates when needed",
                    None,
                    layers.autofix_bus_gates,
                ),
                Toggle::checkbox(
                    ctx,
                    "Fix one-way streets when needed",
                    None,
                    layers.autofix_one_ways,
                ),
            ],
            Mode::SelectBoundary => vec![],
            Mode::FreehandBoundary => vec![],
            Mode::PerResidentImpact => vec![],
            Mode::RoutePlanner => vec![
                entry(
                    ctx,
                    *colors::PLAN_ROUTE_BEFORE,
                    "driving route before changes",
                ),
                entry(
                    ctx,
                    *colors::PLAN_ROUTE_AFTER,
                    "driving route after changes",
                ),
                entry(ctx, *colors::PLAN_ROUTE_BIKE, "cycling route"),
                // TODO Should we invert text color? This gets hard to read
                entry(ctx, *colors::PLAN_ROUTE_WALK, "walking route"),
            ],
            Mode::Crossings => vec![
                Widget::row(vec![
                    Image::from_path(pages::Crossings::svg_path(CrossingType::Unsignalized))
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                    "Unsignalized crossing".text_widget(ctx),
                ]),
                Widget::row(vec![
                    Image::from_path(pages::Crossings::svg_path(CrossingType::Signalized))
                        .untinted()
                        .dims(30.0)
                        .into_widget(ctx),
                    "Signalized crossing".text_widget(ctx),
                ]),
                entry(ctx, *colors::IMPERMEABLE, "impermeable (no crossings)"),
                entry(ctx, *colors::SEMI_PERMEABLE, "semi-permeable (1 crossing)"),
                entry(ctx, *colors::POROUS, "porous (≥2 crossings)"),
            ],
            Mode::Impact => vec![
                map_gui::tools::compare_counts::CompareCounts::relative_scale()
                    .make_legend(ctx, vec!["less", "same", "more"]),
            ],
            Mode::CycleNetwork => vec![
                entry(
                    ctx,
                    *colors::NETWORK_SEGREGATED_LANE,
                    "segregated cycle lane",
                ),
                entry(ctx, *colors::NETWORK_QUIET_STREET, "quiet local street"),
                entry(
                    ctx,
                    *colors::NETWORK_PAINTED_LANE,
                    "painted cycle lane or shared bus lane",
                ),
                entry(
                    ctx,
                    *colors::NETWORK_THROUGH_TRAFFIC_STREET,
                    "local street with cut-through traffic",
                ),
            ],
            Mode::Census => vec![],
        })
    }
}

fn entry_builder<'a, 'c>(color: Color, label: &'static str) -> ButtonBuilder<'a, 'c> {
    let mut btn = ButtonBuilder::new()
        .label_text(label)
        .bg_color(color, ControlState::Disabled)
        .disabled(true)
        .padding(EdgeInsets {
            top: 10.0,
            bottom: 10.0,
            left: 20.0,
            right: 20.0,
        })
        .corner_rounding(0.0);
    if color == Color::BLACK {
        btn = btn.label_color(Color::WHITE, ControlState::Disabled);
    }
    btn
}

fn entry(ctx: &EventCtx, color: Color, label: &'static str) -> Widget {
    entry_builder(color, label).build_def(ctx)
}

pub fn legend_entry(ctx: &EventCtx, color: Color, label: &'static str) -> Widget {
    entry(ctx, color, label)
}

fn entry_tooltip(
    ctx: &mut EventCtx,
    color: Color,
    label: &'static str,
    tooltip: &'static str,
) -> Widget {
    entry_builder(color, label)
        .disabled_tooltip(tooltip)
        .build_def(ctx)
}

fn color_grid(ctx: &mut EventCtx) -> Widget {
    let size = 16.0;
    let columns = 3;
    let mut batch = GeomBatch::new();

    for (i, color) in colors::CELLS.iter().enumerate() {
        let row = (i / columns) as f64;
        let column = (i % columns) as f64;
        batch.push(
            *color,
            Polygon::rectangle(size, size).translate(size * column, size * row),
        );
    }

    batch.into_widget(ctx)
}