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
mod layers;
mod magnifying;
mod nearby;
mod quick_sketch;

use abstutil::prettyprint_usize;
use geom::Distance;
use map_gui::load::FutureLoader;
use map_gui::tools::{nice_map_name, CityPicker, ColorLegend, PopupMsg};
use map_gui::ID;
use map_model::{EditCmd, LaneType};
use widgetry::{
    lctrl, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, State, Text,
    TextExt, Toggle, VerticalAlignment, Widget,
};

use self::layers::{legend, render_edits, DrawNetworkLayer};
use self::magnifying::MagnifyingGlass;
use self::nearby::Nearby;
use crate::app::{App, Transition};
use crate::common::Warping;
use crate::edit::{LoadEdits, RoadEditor, SaveEdits};
use crate::sandbox::gameplay::GameplayMode;

pub struct ExploreMap {
    top_panel: Panel,
    legend: Panel,
    magnifying_glass: MagnifyingGlass,
    network_layer: DrawNetworkLayer,
    edits_layer: Drawable,
    elevation: bool,
    // TODO Also cache Nearby, but recalculate it after edits
    nearby: Option<Nearby>,

    map_edit_key: usize,
}

impl ExploreMap {
    pub fn new_state(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
        app.opts.show_building_driveways = false;

        Box::new(ExploreMap {
            top_panel: make_top_panel(ctx, app),
            legend: make_legend(ctx, app, false, false),
            magnifying_glass: MagnifyingGlass::new(ctx),
            network_layer: DrawNetworkLayer::new(),
            edits_layer: render_edits(ctx, app),
            elevation: false,
            nearby: None,

            map_edit_key: app.primary.map.get_edits_change_key(),
        })
    }
}

impl State<App> for ExploreMap {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        {
            // We would normally use Cached, but so many values depend on one key, so this is more
            // clear.
            let key = app.primary.map.get_edits_change_key();
            if self.map_edit_key != key {
                self.map_edit_key = key;
                self.network_layer.clear();
                self.edits_layer = render_edits(ctx, app);
                self.top_panel = make_top_panel(ctx, app);
            }
        }

        ctx.canvas_movement();

        self.magnifying_glass.event(ctx, app);

        if ctx.redo_mouseover() && self.elevation {
            let mut label = Text::new().into_widget(ctx);

            if ctx.canvas.cam_zoom < app.opts.min_zoom_for_detail {
                if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
                    if let Some((elevation, _)) = app
                        .session
                        .elevation_contours
                        .value()
                        .unwrap()
                        .0
                        .closest_pt(pt, Distance::meters(300.0))
                    {
                        label =
                            Line(format!("{} ft", elevation.to_feet().round())).into_widget(ctx);
                    }
                }
            }
            self.legend.replace(ctx, "current elevation", label);
        }

        // Click to edit a road in detail
        if ctx.redo_mouseover() {
            app.primary.current_selection =
                match app.mouseover_unzoomed_roads_and_intersections(ctx) {
                    Some(ID::Road(r)) => Some(r),
                    Some(ID::Lane(l)) => Some(app.primary.map.get_l(l).parent),
                    _ => None,
                }
                .and_then(|r| {
                    if app.primary.map.get_r(r).is_light_rail() {
                        None
                    } else {
                        Some(ID::Road(r))
                    }
                });
        }
        if let Some(ID::Road(r)) = app.primary.current_selection {
            if ctx.normal_left_click() {
                return Transition::Multi(vec![
                    Transition::Push(RoadEditor::new_state_without_lane(ctx, app, r)),
                    Transition::Push(Warping::new_state(
                        ctx,
                        ctx.canvas.get_cursor_in_map_space().unwrap(),
                        Some(10.0),
                        None,
                        &mut app.primary,
                    )),
                ]);
            }
        }

        if let Outcome::Clicked(x) = self.top_panel.event(ctx) {
            match x.as_ref() {
                "about A/B Street" => {
                    return Transition::Push(PopupMsg::new_state(ctx, "TODO", vec!["TODO"]));
                }
                "Open a proposal" => {
                    // Dummy mode, just to allow all edits
                    // TODO Actually, should we make one to express that only road edits are
                    // relevant?
                    let mode = GameplayMode::Freeform(app.primary.map.get_name().clone());

                    // TODO Do we want to do SaveEdits first if unsaved_edits()? We have
                    // auto-saving... and after loading an old "untitled proposal", it looks
                    // unsaved.
                    return Transition::Push(LoadEdits::new_state(ctx, app, mode));
                }
                "Save this proposal" => {
                    return Transition::Push(SaveEdits::new_state(
                        ctx,
                        app,
                        format!("Save \"{}\" as", app.primary.map.get_edits().edits_name),
                        false,
                        Some(Transition::Pop),
                        Box::new(|_, _| {}),
                    ));
                }
                "Share proposal" => {
                    return Transition::Push(share_proposal(ctx, app));
                }
                "Sketch a route" => {
                    app.primary.current_selection = None;
                    return Transition::Push(crate::ungap::quick_sketch::QuickSketch::new_state(
                        ctx, app,
                    ));
                }
                _ => unreachable!(),
            }
        }

        match self.legend.event(ctx) {
            Outcome::Clicked(x) => {
                return Transition::Push(match x.as_ref() {
                    "change map" => CityPicker::new_state(
                        ctx,
                        app,
                        Box::new(|ctx, app| {
                            Transition::Multi(vec![Transition::Pop, Transition::Replace(ExploreMap::new_state(ctx, app))])
                        }),
                    ),
                    // TODO Add physical picture examples
                    "highway" => PopupMsg::new_state(ctx, "Highways", vec!["Unless there's a separate trail (like on the 520 or I90 bridge), highways aren't accessible to biking"]),
                    "major street" => PopupMsg::new_state(ctx, "Major streets", vec!["Arterials have more traffic, but are often where businesses are located"]),
                    "minor street" => PopupMsg::new_state(ctx, "Minor streets", vec!["Local streets have a low volume of traffic and are usually comfortable for biking, even without dedicated infrastructure"]),
                    "trail" => PopupMsg::new_state(ctx, "Trails", vec!["Trails like the Burke Gilman are usually well-separated from vehicle traffic. The space is usually shared between people walking, cycling, and rolling."]),
                    "protected bike lane" => PopupMsg::new_state(ctx, "Protected bike lanes", vec!["Bike lanes separated from vehicle traffic by physical barriers or a few feet of striping"]),
                    "painted bike lane" => PopupMsg::new_state(ctx, "Painted bike lanes", vec!["Bike lanes without any separation from vehicle traffic. Often uncomfortably close to the \"door zone\" of parked cars."]),
                    "Stay Healthy Street / greenway" => PopupMsg::new_state(ctx, "Stay Healthy Streets and neighborhood greenways", vec!["Residential streets with additional signage and light barriers. These are intended to be low traffic, dedicated for people walking and biking."]),
                    // TODO Add URLs
                    "about the elevation data" => PopupMsg::new_state(ctx, "About the elevation data", vec!["Biking uphill next to traffic without any dedicated space isn't fun.", "Biking downhill next to traffic, especially in the door-zone of parked cars, and especially on Seattle's bumpy roads... is downright terrifying.", "", "Note the elevation data is incorrect near bridges.", "Thanks to King County LIDAR for the data, and Eldan Goldenberg for processing it."]),
                    "about the things nearby" => PopupMsg::new_state(ctx, "About the things nearby", vec!["Population daa from ?", "Amenities from OpenStreetMap", "A 1-minute biking buffer around the bike network is shown.", "Note 1 minutes depends on direction, especially with steep hills -- this starts FROM the network."]),
                    _ => unreachable!(),
            });
            }
            Outcome::Changed(x) => match x.as_ref() {
                "elevation" => {
                    self.elevation = self.legend.is_checked("elevation");
                    self.legend = make_legend(ctx, app, self.elevation, self.nearby.is_some());
                    if self.elevation {
                        let name = app.primary.map.get_name().clone();
                        if app.session.elevation_contours.key() != Some(name.clone()) {
                            let mut low = Distance::ZERO;
                            let mut high = Distance::ZERO;
                            for i in app.primary.map.all_intersections() {
                                low = low.min(i.elevation);
                                high = high.max(i.elevation);
                            }
                            // TODO Maybe also draw the uphill arrows on the steepest streets?
                            let value = crate::layer::elevation::make_elevation_contours(
                                ctx, app, low, high,
                            );
                            app.session.elevation_contours.set(name, value);
                        }
                    }
                }
                "things nearby" => {
                    if self.legend.is_checked("things nearby") {
                        let nearby = Nearby::new(ctx, app);
                        let label = Text::from(Line(format!(
                            "{} residents, {} shops",
                            prettyprint_usize(nearby.population),
                            prettyprint_usize(nearby.total_amenities)
                        )))
                        .into_widget(ctx);
                        self.legend.replace(ctx, "nearby info", label);
                        self.nearby = Some(nearby);
                    } else {
                        let label = Text::new().into_widget(ctx);
                        self.legend.replace(ctx, "nearby info", label);
                        self.nearby = None;
                    }
                }
                _ => unreachable!(),
            },
            _ => {}
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.top_panel.draw(g);
        self.legend.draw(g);
        if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            self.network_layer.draw(g, app);

            if self.elevation {
                if let Some((_, ref draw)) = app.session.elevation_contours.value() {
                    g.redraw(draw);
                }
            }
            if let Some(ref nearby) = self.nearby {
                g.redraw(&nearby.draw_buffer);
            }

            self.magnifying_glass.draw(g, app);
        }
        g.redraw(&self.edits_layer);
    }
}

fn make_top_panel(ctx: &mut EventCtx, app: &App) -> Panel {
    let mut file_management = Vec::new();
    let edits = app.primary.map.get_edits();

    let total_mileage = {
        // Look for the new lanes...
        let mut total = Distance::ZERO;
        // TODO We're assuming the edits have been compressed.
        for cmd in &edits.commands {
            if let EditCmd::ChangeRoad { r, old, new } = cmd {
                let num_before = old
                    .lanes_ltr
                    .iter()
                    .filter(|spec| spec.lt == LaneType::Biking)
                    .count();
                let num_after = new
                    .lanes_ltr
                    .iter()
                    .filter(|spec| spec.lt == LaneType::Biking)
                    .count();
                if num_before != num_after {
                    let multiplier = (num_after as f64) - (num_before) as f64;
                    total += multiplier * app.primary.map.get_r(*r).center_pts.length();
                }
            }
        }
        total
    };
    if edits.commands.is_empty() {
        file_management.push("Today's network".text_widget(ctx));
    } else {
        file_management.push(Line(&edits.edits_name).into_widget(ctx));
    }
    file_management.push(
        Line(format!(
            "{:.1} miles of new bike lanes",
            total_mileage.to_miles()
        ))
        .secondary()
        .into_widget(ctx),
    );
    file_management.push(ColorLegend::row(
        ctx,
        *crate::ungap::layers::EDITED_COLOR,
        "changed road",
    ));
    file_management.push(Widget::row(vec![
        ctx.style()
            .btn_outline
            .text("Open a proposal")
            .hotkey(lctrl(Key::O))
            .build_def(ctx),
        ctx.style()
            .btn_outline
            .text("Save this proposal")
            .hotkey(lctrl(Key::S))
            .disabled(edits.commands.is_empty())
            .build_def(ctx),
    ]));
    // TODO Rethink UI of this, probably fold into save dialog
    file_management.push(
        ctx.style()
            .btn_outline
            .text("Share proposal")
            .disabled(edits.commands.is_empty())
            .build_def(ctx),
    );
    // TODO Should undo/redo, save, share functionality also live here?

    Panel::new_builder(Widget::col(vec![
        Widget::row(vec![
            ctx.style()
                .btn_plain
                .btn()
                .image_path("system/assets/pregame/logo.svg")
                .image_dims(50.0)
                .build_widget(ctx, "about A/B Street"),
            Line("Draw your ideal bike network")
                .small_heading()
                .into_widget(ctx)
                .centered_vert(),
        ]),
        Widget::col(file_management).bg(ctx.style().section_bg),
        Widget::row(vec![
            "Click a road to edit in detail"
                .text_widget(ctx)
                .centered_vert(),
            ctx.style()
                .btn_solid_primary
                .text("Sketch a route")
                .hotkey(Key::S)
                .build_def(ctx),
        ])
        .evenly_spaced(),
    ]))
    .aligned(HorizontalAlignment::Right, VerticalAlignment::Top)
    .build(ctx)
}

fn make_legend(ctx: &mut EventCtx, app: &App, elevation: bool, nearby: bool) -> Panel {
    Panel::new_builder(Widget::col(vec![
        Widget::custom_row(vec![
            Line("Bike Network")
                .small_heading()
                .into_widget(ctx)
                .margin_right(18),
            ctx.style()
                .btn_popup_icon_text(
                    "system/assets/tools/map.svg",
                    nice_map_name(app.primary.map.get_name()),
                )
                .hotkey(lctrl(Key::L))
                .build_widget(ctx, "change map")
                .margin_right(8),
        ]),
        // TODO Looks too close to access restrictions
        legend(ctx, app.cs.unzoomed_highway, "highway"),
        legend(ctx, app.cs.unzoomed_arterial, "major street"),
        legend(ctx, app.cs.unzoomed_residential, "minor street"),
        legend(ctx, *layers::DEDICATED_TRAIL, "trail"),
        legend(ctx, *layers::PROTECTED_BIKE_LANE, "protected bike lane"),
        legend(ctx, *layers::PAINTED_BIKE_LANE, "painted bike lane"),
        legend(ctx, *layers::GREENWAY, "Stay Healthy Street / greenway"),
        // TODO Distinguish door-zone bike lanes?
        // TODO Call out bike turning boxes?
        // TODO Call out bike signals?
        Widget::row(vec![
            Toggle::checkbox(ctx, "elevation", Key::E, elevation),
            ctx.style()
                .btn_plain
                .icon("system/assets/tools/info.svg")
                .build_widget(ctx, "about the elevation data")
                .centered_vert(),
            Text::new()
                .into_widget(ctx)
                .named("current elevation")
                .centered_vert(),
        ]),
        Widget::row(vec![
            Toggle::checkbox(ctx, "things nearby", None, nearby),
            ctx.style()
                .btn_plain
                .icon("system/assets/tools/info.svg")
                .build_widget(ctx, "about the things nearby")
                .centered_vert(),
            Text::new()
                .into_widget(ctx)
                .named("nearby info")
                .centered_vert(),
        ]),
    ]))
    .aligned(HorizontalAlignment::Right, VerticalAlignment::Bottom)
    .build(ctx)
}

fn share_proposal(ctx: &mut EventCtx, app: &App) -> Box<dyn State<App>> {
    let (_, outer_progress_rx) = futures_channel::mpsc::channel(1);
    let (_, inner_progress_rx) = futures_channel::mpsc::channel(1);
    let edits_json =
        abstutil::to_json_terse(&app.primary.map.get_edits().to_permanent(&app.primary.map));
    let url = "http://localhost:8080/v1/create";
    FutureLoader::<App, String>::new_state(
        ctx,
        Box::pin(async move {
            let uuid = abstio::http_post(url, edits_json).await?;
            // TODO I'm so lost in this type magic
            let wrapper: Box<dyn Send + FnOnce(&App) -> String> = Box::new(move |_| uuid);
            Ok(wrapper)
        }),
        outer_progress_rx,
        inner_progress_rx,
        "Uploading proposal",
        Box::new(|ctx, _, result| {
            Transition::Replace(match result {
                Ok(uuid) => PopupMsg::new_state(ctx, "Success", vec![format!("ID is {}", uuid)]),
                Err(err) => PopupMsg::new_state(
                    ctx,
                    "Failure",
                    vec![format!("Couldn't upload proposal: {}", err)],
                ),
            })
        }),
    )
}