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
use abstutil::prettyprint_usize;
use map_gui::tools::{MinimapControls, Navigator};
use widgetry::{
    ControlState, EventCtx, GfxCtx, HorizontalAlignment, Image, Key, Line, Panel, ScreenDims, Text,
    VerticalAlignment, Widget,
};

use crate::app::App;
use crate::app::Transition;
use crate::common::Warping;
use crate::layer::PickLayer;

pub struct MinimapController;

impl MinimapControls<App> for MinimapController {
    fn has_zorder(&self, app: &App) -> bool {
        app.opts.dev
    }
    fn has_layer(&self, app: &App) -> bool {
        app.primary.layer.is_some()
    }

    fn draw_extra(&self, g: &mut GfxCtx, app: &App) {
        if let Some(ref l) = app.primary.layer {
            l.draw_minimap(g);
        }

        let mut cache = app.primary.agents.borrow_mut();
        cache.draw_unzoomed_agents(g, &app.primary.map, &app.primary.sim, &app.cs, &app.opts);
    }

    fn make_unzoomed_panel(&self, ctx: &mut EventCtx, app: &App) -> Panel {
        let unzoomed_agents = &app.primary.agents.borrow().unzoomed_agents;
        let is_enabled = [
            unzoomed_agents.cars(),
            unzoomed_agents.bikes(),
            unzoomed_agents.buses_and_trains(),
            unzoomed_agents.peds(),
        ];
        Panel::new_builder(Widget::row(vec![
            make_tool_panel(ctx, app).align_right(),
            Widget::col(make_agent_toggles(ctx, app, is_enabled))
                .bg(app.cs.panel_bg)
                .padding(16),
        ]))
        .aligned(
            HorizontalAlignment::Right,
            VerticalAlignment::BottomAboveOSD,
        )
        .build_custom(ctx)
    }
    fn make_legend(&self, ctx: &mut EventCtx, app: &App) -> Widget {
        let unzoomed_agents = &app.primary.agents.borrow().unzoomed_agents;
        let is_enabled = [
            unzoomed_agents.cars(),
            unzoomed_agents.bikes(),
            unzoomed_agents.buses_and_trains(),
            unzoomed_agents.peds(),
        ];

        Widget::custom_row(make_agent_toggles(ctx, app, is_enabled))
            // nudge to left-align with the map edge
            .margin_left(26)
    }

    fn make_zoomed_side_panel(&self, ctx: &mut EventCtx, app: &App) -> Widget {
        make_tool_panel(ctx, app)
    }

    fn panel_clicked(&self, ctx: &mut EventCtx, app: &mut App, action: &str) -> Option<Transition> {
        match action {
            "search" => {
                return Some(Transition::Push(Navigator::new_state(ctx, app)));
            }
            "zoom out fully" => {
                return Some(Transition::Push(Warping::new_state(
                    ctx,
                    app.primary.map.get_bounds().get_rectangle().center(),
                    Some(ctx.canvas.min_zoom()),
                    None,
                    &mut app.primary,
                )));
            }
            "zoom in fully" => {
                return Some(Transition::Push(Warping::new_state(
                    ctx,
                    ctx.canvas.center_to_map_pt(),
                    Some(10.0),
                    None,
                    &mut app.primary,
                )));
            }
            "change layers" => {
                return Some(Transition::Push(PickLayer::pick(ctx, app)));
            }
            "more data" => Some(Transition::Push(app.session.dash_tab.launch(ctx, app))),
            _ => unreachable!(),
        }
    }
    fn panel_changed(&self, _: &mut EventCtx, app: &mut App, panel: &Panel) {
        if panel.has_widget("Car") {
            app.primary
                .agents
                .borrow_mut()
                .unzoomed_agents
                .update(panel);
        }
    }
}

/// `is_enabled`: are (car, bike, bus, pedestrian) toggles enabled
/// returns Widgets for (car, bike, bus, pedestrian)
fn make_agent_toggles(ctx: &mut EventCtx, app: &App, is_enabled: [bool; 4]) -> Vec<Widget> {
    use widgetry::{include_labeled_bytes, Color, GeomBatchStack, RewriteColor, Toggle};
    let [is_car_enabled, is_bike_enabled, is_bus_enabled, is_pedestrian_enabled] = is_enabled;

    pub fn colored_checkbox(
        ctx: &EventCtx,
        action: &str,
        is_enabled: bool,
        color: Color,
        icon: &str,
        label: &str,
        tooltip: Text,
    ) -> Widget {
        let buttons = ctx
            .style()
            .btn_plain
            .btn()
            .label_text(label)
            .padding(4.0)
            .tooltip(tooltip)
            .image_color(RewriteColor::NoOp, ControlState::Default);

        let icon_batch = Image::from_path(icon)
            .build_batch(ctx)
            .expect("invalid svg")
            .0;
        let false_btn = {
            let checkbox = Image::from_bytes(include_labeled_bytes!(
                "../../../../widgetry/icons/checkbox_no_border_unchecked.svg"
            ))
            .color(RewriteColor::Change(Color::BLACK, color.alpha(0.3)));
            let mut row = GeomBatchStack::horizontal(vec![
                checkbox.build_batch(ctx).expect("invalid svg").0,
                icon_batch.clone(),
            ]);
            row.set_spacing(8.0);

            let row_batch = row.batch();
            let bounds = row_batch.get_bounds();
            buttons.clone().image_batch(row_batch, bounds)
        };

        // For typical checkboxes buttons, the checkbox *is* the image, but for the agent toggles
        // we need both a checkbox *and* an additional icon. To do that, we combine the checkbox
        // and icon into a single batch, and use that combined batch as the button's image.
        let true_btn = {
            let checkbox = Image::from_bytes(include_labeled_bytes!(
                "../../../../widgetry/icons/checkbox_no_border_checked.svg"
            ))
            .color(RewriteColor::Change(Color::BLACK, color));

            let mut row = GeomBatchStack::horizontal(vec![
                checkbox.build_batch(ctx).expect("invalid svg").0,
                icon_batch,
            ]);
            row.set_spacing(8.0);

            let row_batch = row.batch();
            let bounds = row_batch.get_bounds();
            buttons.image_batch(row_batch, bounds)
        };

        Toggle::new_widget(
            is_enabled,
            false_btn.build(ctx, action),
            true_btn.build(ctx, action),
        )
        .named(action)
        .container()
        // avoid horizontal resize jitter as numbers fluctuate
        .force_width(137.0)
    }

    let counts = app.primary.sim.num_commuters_vehicles();

    let pedestrian_details = {
        let tooltip = Text::from_multiline(vec![
            Line("Pedestrians"),
            Line(format!(
                "Walking commuters: {}",
                prettyprint_usize(counts.walking_commuters)
            ))
            .secondary(),
            Line(format!(
                "To/from public transit: {}",
                prettyprint_usize(counts.walking_to_from_transit)
            ))
            .secondary(),
            Line(format!(
                "To/from a car: {}",
                prettyprint_usize(counts.walking_to_from_car)
            ))
            .secondary(),
            Line(format!(
                "To/from a bike: {}",
                prettyprint_usize(counts.walking_to_from_bike)
            ))
            .secondary(),
        ]);

        let count = prettyprint_usize(
            counts.walking_commuters
                + counts.walking_to_from_transit
                + counts.walking_to_from_car
                + counts.walking_to_from_bike,
        );

        colored_checkbox(
            ctx,
            "Walk",
            is_pedestrian_enabled,
            app.cs.unzoomed_pedestrian,
            "system/assets/meters/pedestrian.svg",
            &count,
            tooltip,
        )
    };

    let bike_details = {
        let tooltip = Text::from_multiline(vec![
            Line("Cyclists"),
            Line(prettyprint_usize(counts.cyclists)).secondary(),
        ]);

        colored_checkbox(
            ctx,
            "Bike",
            is_bike_enabled,
            app.cs.unzoomed_bike,
            "system/assets/meters/bike.svg",
            &prettyprint_usize(counts.cyclists),
            tooltip,
        )
    };

    let car_details = {
        let tooltip = Text::from_multiline(vec![
            Line("Cars"),
            Line(format!(
                "Single-occupancy vehicles: {}",
                prettyprint_usize(counts.sov_drivers)
            ))
            .secondary(),
        ]);
        colored_checkbox(
            ctx,
            "Car",
            is_car_enabled,
            app.cs.unzoomed_car,
            "system/assets/meters/car.svg",
            &prettyprint_usize(counts.sov_drivers),
            tooltip,
        )
    };

    let bus_details = {
        let tooltip = Text::from_multiline(vec![
            Line("Public transit"),
            Line(format!(
                "{} passengers on {} buses",
                prettyprint_usize(counts.bus_riders),
                prettyprint_usize(counts.buses)
            ))
            .secondary(),
            Line(format!(
                "{} passengers on {} trains",
                prettyprint_usize(counts.train_riders),
                prettyprint_usize(counts.trains)
            ))
            .secondary(),
        ]);

        colored_checkbox(
            ctx,
            "Bus",
            is_bus_enabled,
            app.cs.unzoomed_bus,
            "system/assets/meters/bus.svg",
            &prettyprint_usize(counts.bus_riders + counts.train_riders),
            tooltip,
        )
    };

    vec![car_details, bike_details, bus_details, pedestrian_details]
}

fn make_tool_panel(ctx: &mut EventCtx, app: &App) -> Widget {
    let buttons = ctx
        .style()
        .btn_floating
        .btn()
        .image_dims(ScreenDims::square(20.0))
        // the default transparent button background is jarring for these buttons which are floating
        // in a transparent panel.
        .bg_color(app.cs.inner_panel_bg, ControlState::Default)
        .padding(8);

    Widget::col(vec![
        (if ctx.canvas.is_zoomed() {
            buttons
                .clone()
                .image_path("system/assets/minimap/zoom_out_fully.svg")
                .build_widget(ctx, "zoom out fully")
        } else {
            buttons
                .clone()
                .image_path("system/assets/minimap/zoom_in_fully.svg")
                .build_widget(ctx, "zoom in fully")
        }),
        buttons
            .clone()
            .image_path("system/assets/tools/layers.svg")
            .hotkey(Key::L)
            .build_widget(ctx, "change layers"),
        buttons
            .clone()
            .image_path("system/assets/tools/search.svg")
            .hotkey(Key::K)
            .build_widget(ctx, "search"),
        buttons
            .image_path("system/assets/meters/trip_histogram.svg")
            .hotkey(Key::Q)
            .build_widget(ctx, "more data"),
    ])
}