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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
use maplit::btreeset;

use abstutil::{prettyprint_usize, Timer};
use geom::Speed;
use map_gui::options::OptionsPanel;
use map_gui::render::DrawMap;
use map_gui::tools::{grey_out_map, ChooseSomething, ColorLegend};
use map_gui::ID;
use map_model::{EditCmd, IntersectionID, LaneID, MapEdits};
use widgetry::mapspace::ToggleZoomed;
use widgetry::tools::PopupMsg;
use widgetry::{
    lctrl, Choice, Color, ControlState, EventCtx, GfxCtx, HorizontalAlignment, Image, Key, Line,
    Menu, Outcome, Panel, State, Text, TextBox, TextExt, VerticalAlignment, Widget,
};

pub use self::roads::RoadEditor;
pub use self::routes::RouteEditor;
pub use self::stop_signs::StopSignEditor;
pub use self::traffic_signals::TrafficSignalEditor;
pub use self::validate::{check_blackholes, check_sidewalk_connectivity};
use crate::app::{App, Transition};
use crate::common::{tool_panel, CommonState, Warping};
use crate::debug::DebugMode;
use crate::sandbox::{GameplayMode, SandboxMode, TimeWarpScreen};

mod heuristics;
mod multiple_roads;
mod roads;
mod routes;
mod stop_signs;
mod traffic_signals;
mod validate;
mod zones;

pub struct EditMode {
    tool_panel: Panel,
    top_center: Panel,
    changelist: Panel,
    orig_edits: MapEdits,
    orig_dirty: bool,

    // Retained state from the SandboxMode that spawned us
    mode: GameplayMode,

    map_edit_key: usize,

    draw: ToggleZoomed,
}

impl EditMode {
    pub fn new_state(ctx: &mut EventCtx, app: &mut App, mode: GameplayMode) -> Box<dyn State<App>> {
        let orig_dirty = app.primary.dirty_from_edits;
        assert!(app.primary.suspended_sim.is_none());
        app.primary.suspended_sim = Some(app.primary.clear_sim());
        let layer = crate::layer::map::Static::edits(ctx, app);
        Box::new(EditMode {
            tool_panel: tool_panel(ctx),
            top_center: make_topcenter(ctx, app),
            changelist: make_changelist(ctx, app),
            orig_edits: app.primary.map.get_edits().clone(),
            orig_dirty,
            mode,
            map_edit_key: app.primary.map.get_edits_change_key(),
            draw: layer.draw,
        })
    }

    fn quit(&self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        let old_sim = app.primary.suspended_sim.take().unwrap();

        // If nothing changed, short-circuit
        if app.primary.map.get_edits() == &self.orig_edits {
            app.primary.sim = old_sim;
            app.primary.dirty_from_edits = self.orig_dirty;
            // Could happen if we load some edits, then load whatever we entered edit mode with.
            ctx.loading_screen("apply edits", |_, timer| {
                app.primary.map.recalculate_pathfinding_after_edits(timer);
            });
            return Transition::Pop;
        }

        ctx.loading_screen("apply edits", move |ctx, timer| {
            app.primary.map.recalculate_pathfinding_after_edits(timer);
            if GameplayMode::FixTrafficSignals == self.mode {
                app.primary.sim = old_sim;
                app.primary.dirty_from_edits = true;
                app.primary
                    .sim
                    .handle_live_edited_traffic_signals(&app.primary.map);
                Transition::Pop
            } else if app.primary.current_flags.live_map_edits {
                app.primary.sim = old_sim;
                app.primary.dirty_from_edits = true;
                app.primary
                    .sim
                    .handle_live_edited_traffic_signals(&app.primary.map);
                let (trips, parked_cars) =
                    app.primary.sim.handle_live_edits(&app.primary.map, timer);
                if trips == 0 && parked_cars == 0 {
                    Transition::Pop
                } else {
                    Transition::Replace(PopupMsg::new_state(
                        ctx,
                        "Map changes complete",
                        vec![
                            format!(
                                "Your edits interrupted {} trips and displaced {} parked cars",
                                prettyprint_usize(trips),
                                prettyprint_usize(parked_cars)
                            ),
                            "Simulation results won't be finalized unless you restart from \
                             midnight with your changes"
                                .to_string(),
                        ],
                    ))
                }
            } else {
                Transition::Multi(vec![
                    Transition::Pop,
                    Transition::Replace(SandboxMode::async_new(
                        app,
                        self.mode.clone(),
                        Box::new(move |ctx, app| {
                            vec![Transition::Push(TimeWarpScreen::new_state(
                                ctx,
                                app,
                                old_sim.time(),
                                None,
                            ))]
                        }),
                    )),
                ])
            }
        })
    }
}

impl State<App> for EditMode {
    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.changelist = make_changelist(ctx, app);
                let layer = crate::layer::map::Static::edits(ctx, app);
                self.draw = layer.draw;
            }
        }

        if let Some(t) = CommonState::debug_actions(ctx, app) {
            return t;
        }

        ctx.canvas_movement();
        // Restrict what can be selected.
        if ctx.redo_mouseover() {
            app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx);
            if match app.primary.current_selection {
                Some(ID::Lane(l)) => !self.mode.can_edit_roads() || !can_edit_lane(app, l),
                Some(ID::Intersection(i)) => {
                    !self.mode.can_edit_stop_signs()
                        && app.primary.map.maybe_get_stop_sign(i).is_some()
                }
                Some(ID::Road(_)) => false,
                _ => true,
            } {
                app.primary.current_selection = None;
            }
        }

        if app.opts.dev && ctx.input.pressed(lctrl(Key::D)) {
            return Transition::Push(DebugMode::new_state(ctx, app));
        }

        if let Outcome::Clicked(x) = self.top_center.event(ctx) {
            match x.as_ref() {
                "finish editing" => {
                    return self.quit(ctx, app);
                }
                _ => unreachable!(),
            }
        }
        if let Outcome::Clicked(x) = self.changelist.event(ctx) {
            match x.as_ref() {
                "manage proposals" => {
                    let mode = self.mode.clone();
                    return Transition::Push(ChooseSomething::new_state(
                        ctx,
                        "Manage proposals",
                        vec![
                            Choice::string("rename current proposal"),
                            Choice::string("open a saved proposal").multikey(lctrl(Key::L)),
                            Choice::string("create a blank proposal"),
                            Choice::string("save this proposal as..."),
                            // TODO Disable if empty edits
                            Choice::string("share proposal"),
                            Choice::string("delete this proposal and remove all edits")
                                .fg(ctx.style().text_destructive_color),
                        ],
                        Box::new(move |choice, ctx, app| match choice.as_ref() {
                            "rename current proposal" => {
                                let old_name = app.primary.map.get_edits().edits_name.clone();
                                Transition::Replace(SaveEdits::new_state(
                                    ctx,
                                    app,
                                    format!("Rename \"{}\"", old_name),
                                    false,
                                    Some(Transition::Pop),
                                    Box::new(move |_, app| {
                                        abstio::delete_file(abstio::path_edits(
                                            app.primary.map.get_name(),
                                            &old_name,
                                        ));
                                    }),
                                ))
                            }
                            "open a saved proposal" => {
                                if app.primary.map.unsaved_edits() {
                                    Transition::Multi(vec![
                                        Transition::Replace(LoadEdits::new_state(ctx, app, mode)),
                                        Transition::Push(SaveEdits::new_state(
                                            ctx,
                                            app,
                                            "Do you want to save your proposal first?",
                                            true,
                                            Some(Transition::Multi(vec![
                                                Transition::Pop,
                                                Transition::Pop,
                                            ])),
                                            Box::new(|_, _| {}),
                                        )),
                                    ])
                                } else {
                                    Transition::Replace(LoadEdits::new_state(ctx, app, mode))
                                }
                            }
                            "create a blank proposal" => {
                                if app.primary.map.unsaved_edits() {
                                    Transition::Replace(SaveEdits::new_state(
                                        ctx,
                                        app,
                                        "Do you want to save your proposal first?",
                                        true,
                                        Some(Transition::Pop),
                                        Box::new(|ctx, app| {
                                            apply_map_edits(ctx, app, app.primary.map.new_edits());
                                        }),
                                    ))
                                } else {
                                    apply_map_edits(ctx, app, app.primary.map.new_edits());
                                    Transition::Pop
                                }
                            }
                            "save this proposal as..." => {
                                Transition::Replace(SaveEdits::new_state(
                                    ctx,
                                    app,
                                    format!(
                                        "Save \"{}\" as",
                                        app.primary.map.get_edits().edits_name
                                    ),
                                    false,
                                    Some(Transition::Pop),
                                    Box::new(|_, _| {}),
                                ))
                            }
                            "share proposal" => {
                                // TODO This'll always set or share a URL with the map, losing any
                                // info about the current scenario.
                                // https://github.com/a-b-street/abstreet/issues/766
                                Transition::Replace(crate::common::share::ShareProposal::new_state(
                                    ctx, app, "--dev",
                                ))
                            }
                            "delete this proposal and remove all edits" => {
                                abstio::delete_file(abstio::path_edits(
                                    app.primary.map.get_name(),
                                    &app.primary.map.get_edits().edits_name,
                                ));
                                apply_map_edits(ctx, app, app.primary.map.new_edits());
                                Transition::Pop
                            }
                            _ => unreachable!(),
                        }),
                    ));
                }
                "load proposal" => {}
                "undo" => {
                    let mut edits = app.primary.map.get_edits().clone();
                    let maybe_id = cmd_to_id(&edits.commands.pop().unwrap());
                    apply_map_edits(ctx, app, edits);
                    if let Some(id) = maybe_id {
                        return Transition::Push(Warping::new_state(
                            ctx,
                            app.primary.canonical_point(id.clone()).unwrap(),
                            Some(10.0),
                            Some(id),
                            &mut app.primary,
                        ));
                    }
                }
                x => {
                    let idx = x["change #".len()..].parse::<usize>().unwrap();
                    if let Some(id) = cmd_to_id(&app.primary.map.get_edits().commands[idx - 1]) {
                        return Transition::Push(Warping::new_state(
                            ctx,
                            app.primary.canonical_point(id.clone()).unwrap(),
                            Some(10.0),
                            Some(id),
                            &mut app.primary,
                        ));
                    }
                }
            }
        }

        // So useful that the hotkey should work even before opening the menu
        if ctx.input.pressed(lctrl(Key::L)) {
            if app.primary.map.unsaved_edits() {
                return Transition::Multi(vec![
                    Transition::Push(LoadEdits::new_state(ctx, app, self.mode.clone())),
                    Transition::Push(SaveEdits::new_state(
                        ctx,
                        app,
                        "Do you want to save your proposal first?",
                        true,
                        Some(Transition::Multi(vec![Transition::Pop, Transition::Pop])),
                        Box::new(|_, _| {}),
                    )),
                ]);
            } else {
                return Transition::Push(LoadEdits::new_state(ctx, app, self.mode.clone()));
            }
        }

        if ctx.canvas.is_unzoomed() {
            if let Some(id) = app.primary.current_selection.clone() {
                if app.per_obj.left_click(ctx, "edit this") {
                    return Transition::Push(Warping::new_state(
                        ctx,
                        app.primary.canonical_point(id).unwrap(),
                        Some(10.0),
                        None,
                        &mut app.primary,
                    ));
                }
            }
        } else {
            if let Some(ID::Intersection(id)) = app.primary.current_selection {
                if let Some(state) = maybe_edit_intersection(ctx, app, id, &self.mode) {
                    return Transition::Push(state);
                }
            }
            if let Some(ID::Lane(l)) = app.primary.current_selection {
                if app.per_obj.left_click(ctx, "edit lane") {
                    return Transition::Push(RoadEditor::new_state(ctx, app, l));
                }
            }
        }

        match self.tool_panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "back" => self.quit(ctx, app),
                "settings" => Transition::Push(OptionsPanel::new_state(ctx, app)),
                _ => unreachable!(),
            },
            _ => Transition::Keep,
        }
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.tool_panel.draw(g);
        self.top_center.draw(g);
        self.changelist.draw(g);
        self.draw.draw(g);
        CommonState::draw_osd(g, app);
    }
}

pub struct SaveEdits {
    panel: Panel,
    current_name: String,
    cancel: Option<Transition>,
    on_success: Box<dyn Fn(&mut EventCtx, &mut App)>,
    reset: bool,
}

impl SaveEdits {
    pub fn new_state<I: Into<String>>(
        ctx: &mut EventCtx,
        app: &App,
        title: I,
        discard: bool,
        cancel: Option<Transition>,
        on_success: Box<dyn Fn(&mut EventCtx, &mut App)>,
    ) -> Box<dyn State<App>> {
        let initial_name = if app.primary.map.unsaved_edits() {
            String::new()
        } else {
            format!("copy of {}", app.primary.map.get_edits().edits_name)
        };
        let mut save = SaveEdits {
            current_name: initial_name.clone(),
            panel: Panel::new_builder(Widget::col(vec![
                Line(title).small_heading().into_widget(ctx),
                Widget::row(vec![
                    "Name:".text_widget(ctx).centered_vert(),
                    TextBox::default_widget(ctx, "filename", initial_name),
                ]),
                // TODO Want this to always consistently be one line high, but it isn't for a blank
                // line
                Text::new().into_widget(ctx).named("warning"),
                Widget::row(vec![
                    if discard {
                        ctx.style()
                            .btn_solid_destructive
                            .text("Discard proposal")
                            .build_def(ctx)
                    } else {
                        Widget::nothing()
                    },
                    if cancel.is_some() {
                        ctx.style()
                            .btn_outline
                            .text("Cancel")
                            .hotkey(Key::Escape)
                            .build_def(ctx)
                    } else {
                        Widget::nothing()
                    },
                    ctx.style()
                        .btn_solid_primary
                        .text("Save")
                        .disabled(true)
                        .build_def(ctx),
                ])
                .align_right(),
            ]))
            .build(ctx),
            cancel,
            on_success,
            reset: discard,
        };
        save.recalc_btn(ctx, app);
        Box::new(save)
    }

    fn recalc_btn(&mut self, ctx: &mut EventCtx, app: &App) {
        if self.current_name.is_empty() {
            self.panel.replace(
                ctx,
                "Save",
                ctx.style()
                    .btn_solid_primary
                    .text("Save")
                    .disabled(true)
                    .build_def(ctx),
            );
            self.panel
                .replace(ctx, "warning", Text::new().into_widget(ctx));
        } else if abstio::file_exists(abstio::path_edits(
            app.primary.map.get_name(),
            &self.current_name,
        )) {
            self.panel.replace(
                ctx,
                "Save",
                ctx.style()
                    .btn_solid_primary
                    .text("Save")
                    .disabled(true)
                    .build_def(ctx),
            );
            self.panel.replace(
                ctx,
                "warning",
                Line("A proposal with this name already exists")
                    .fg(Color::hex("#FF5E5E"))
                    .into_widget(ctx),
            );
        } else {
            self.panel.replace(
                ctx,
                "Save",
                ctx.style()
                    .btn_solid_primary
                    .text("Save")
                    .hotkey(Key::Enter)
                    .build_def(ctx),
            );
            self.panel
                .replace(ctx, "warning", Text::new().into_widget(ctx));
        }
    }
}

impl State<App> for SaveEdits {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "Save" => {
                    let mut edits = app.primary.map.get_edits().clone();
                    edits.edits_name = self.current_name.clone();
                    app.primary
                        .map
                        .must_apply_edits(edits, &mut Timer::throwaway());
                    app.primary.map.save_edits();
                    if self.reset {
                        apply_map_edits(ctx, app, app.primary.map.new_edits());
                    }
                    (self.on_success)(ctx, app);
                    return Transition::Pop;
                }
                "Discard proposal" => {
                    apply_map_edits(ctx, app, app.primary.map.new_edits());
                    return Transition::Pop;
                }
                "Cancel" => {
                    return self.cancel.take().unwrap();
                }
                _ => unreachable!(),
            }
        }
        let name = self.panel.text_box("filename");
        if name != self.current_name {
            self.current_name = name;
            self.recalc_btn(ctx, app);
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        grey_out_map(g, app);
        self.panel.draw(g);
    }
}

pub struct LoadEdits {
    panel: Panel,
    mode: GameplayMode,
}

impl LoadEdits {
    /// Mode is just used for `allows`.
    pub fn new_state(ctx: &mut EventCtx, app: &App, mode: GameplayMode) -> Box<dyn State<App>> {
        let current_edits_name = &app.primary.map.get_edits().edits_name;

        let mut your_proposals =
            abstio::list_all_objects(abstio::path_all_edits(app.primary.map.get_name()))
                .into_iter()
                .map(|name| Choice::new(name.clone(), ()).active(&name != current_edits_name))
                .collect::<Vec<_>>();
        // These're sorted alphabetically, but the "Untitled Proposal"s wind up before lowercase
        // names!
        your_proposals.sort_by_key(|x| (x.label.starts_with("Untitled Proposal"), x.label.clone()));

        let your_edits = vec![
            Line("Your proposals").small_heading().into_widget(ctx),
            Menu::widget(ctx, your_proposals),
        ];
        // widgetry can't toggle keyboard focus between two menus, so just use buttons for the less
        // common use case.
        let mut proposals = vec![Line("Community proposals").small_heading().into_widget(ctx)];
        // Up-front filter out proposals that definitely don't fit the current map
        for name in abstio::list_all_objects(abstio::path("system/proposals")) {
            let path = abstio::path(format!("system/proposals/{}.json", name));
            if let Ok(edits) =
                MapEdits::load_from_file(&app.primary.map, path.clone(), &mut Timer::throwaway())
            {
                proposals.push(
                    ctx.style()
                        .btn_outline
                        .text(edits.get_title())
                        .build_widget(ctx, &path),
                );
            }
        }

        Box::new(LoadEdits {
            mode,
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Line("Load proposal").small_heading().into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                ctx.style()
                    .btn_outline
                    .text("Start over with blank proposal")
                    .build_def(ctx),
                Widget::row(vec![Widget::col(your_edits), Widget::col(proposals)]).evenly_spaced(),
            ]))
            .exact_size_percent(50, 50)
            .build(ctx),
        })
    }
}

impl State<App> for LoadEdits {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => {
                match x.as_ref() {
                    "close" => Transition::Pop,
                    "Start over with blank proposal" => {
                        apply_map_edits(ctx, app, app.primary.map.new_edits());
                        Transition::Pop
                    }
                    path => {
                        // TODO Kind of a hack. If it ends with .json, it's already a path.
                        // Otherwise it's a result from the menu.
                        let path = if path.ends_with(".json") {
                            path.to_string()
                        } else {
                            abstio::path_edits(app.primary.map.get_name(), path)
                        };

                        match MapEdits::load_from_file(
                            &app.primary.map,
                            path.clone(),
                            &mut Timer::throwaway(),
                        )
                        .and_then(|edits| {
                            if self.mode.allows(&edits) {
                                Ok(edits)
                            } else {
                                Err(anyhow!(
                                    "The current gameplay mode restricts edits. This proposal has \
                                     a banned command."
                                ))
                            }
                        }) {
                            Ok(edits) => {
                                apply_map_edits(ctx, app, edits);
                                app.primary
                                    .sim
                                    .handle_live_edited_traffic_signals(&app.primary.map);
                                Transition::Pop
                            }
                            // TODO Hack. Have to replace ourselves, because the Menu might be
                            // invalidated now that something was chosen.
                            Err(err) => {
                                println!("Can't load {}: {}", path, err);
                                Transition::Multi(vec![
                                    Transition::Replace(LoadEdits::new_state(
                                        ctx,
                                        app,
                                        self.mode.clone(),
                                    )),
                                    // TODO Menu draws at a weird Z-order to deal with tooltips, so
                                    // now the menu underneath
                                    // bleeds through
                                    Transition::Push(PopupMsg::new_state(
                                        ctx,
                                        "Error",
                                        vec![format!("Can't load {}", path), err.to_string()],
                                    )),
                                ])
                            }
                        }
                    }
                }
            }
            _ => Transition::Keep,
        }
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        grey_out_map(g, app);
        self.panel.draw(g);
    }
}

fn make_topcenter(ctx: &mut EventCtx, app: &App) -> Panel {
    Panel::new_builder(Widget::col(vec![
        Line("Editing map")
            .small_heading()
            .into_widget(ctx)
            .centered_horiz(),
        ctx.style()
            .btn_solid_primary
            .text(format!(
                "Finish & resume from {}",
                app.primary
                    .suspended_sim
                    .as_ref()
                    .unwrap()
                    .time()
                    .ampm_tostring()
            ))
            .hotkey(Key::Escape)
            .build_widget(ctx, "finish editing"),
    ]))
    .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
    .build(ctx)
}

pub fn apply_map_edits(ctx: &mut EventCtx, app: &mut App, edits: MapEdits) {
    ctx.loading_screen("apply map edits", |ctx, timer| {
        if !app.store_unedited_map_in_secondary && app.primary.unedited_map.is_none() {
            timer.start("save unedited map");
            assert!(app.primary.map.get_edits().commands.is_empty());
            app.primary.unedited_map = Some(app.primary.map.clone());
            timer.stop("save unedited map");
        }
        if app.store_unedited_map_in_secondary && app.secondary.is_none() {
            timer.start("save unedited map for toggling");
            assert!(app.primary.map.get_edits().commands.is_empty());
            let mut per_map = crate::app::PerMap::map_loaded(
                app.primary.map.clone(),
                app.primary.sim.clone(),
                app.primary.current_flags.clone(),
                &app.opts,
                &app.cs,
                ctx,
                timer,
            );
            // is_secondary indicates the unedited map
            per_map.is_secondary = true;
            app.secondary = Some(per_map);
            timer.stop("save unedited map for toggling");
        }

        timer.start("edit map");
        let effects = app.primary.map.must_apply_edits(edits, timer);
        timer.stop("edit map");

        if !effects.changed_roads.is_empty() || !effects.changed_intersections.is_empty() {
            app.primary
                .draw_map
                .draw_all_unzoomed_roads_and_intersections =
                DrawMap::regenerate_unzoomed_layer(&app.primary.map, &app.cs, ctx, timer);
        }

        for r in effects.changed_roads {
            let road = app.primary.map.get_r(r);
            app.primary.draw_map.recreate_road(road, &app.primary.map);
        }

        for i in effects.changed_intersections {
            app.primary
                .draw_map
                .recreate_intersection(i, &app.primary.map);
        }

        for pl in effects.changed_parking_lots {
            app.primary.draw_map.get_pl(pl).clear_rendering();
        }

        if app.primary.layer.as_ref().and_then(|l| l.name()) == Some("map edits") {
            app.primary.layer = Some(Box::new(crate::layer::map::Static::edits(ctx, app)));
        }
        // Other parts of the UI poll map.get_edits_change_key() to recalculate things based on
        // edits.

        // Autosave
        app.primary.map.save_edits();
    });
}

pub fn can_edit_lane(app: &App, l: LaneID) -> bool {
    let map = &app.primary.map;
    if map.get_l(l).is_light_rail() {
        return false;
    }
    let r = map.get_parent(l);
    // Some bus-only roads are marked as service roads; allow editing those.
    if r.is_service() && r.lanes.iter().all(|l| !l.is_bus()) {
        return false;
    }

    true
}

pub fn speed_limit_choices(app: &App, preset: Option<Speed>) -> Vec<Choice<Speed>> {
    // Don't need anything higher than 70mph. Though now I kind of miss 3am drives on TX-71...
    let mut speeds = (10..=70)
        .step_by(5)
        .map(|mph| Speed::miles_per_hour(mph as f64))
        .collect::<Vec<_>>();
    if let Some(preset) = preset {
        if !speeds.contains(&preset) {
            speeds.push(preset);
            speeds.sort();
        }
    }
    speeds
        .into_iter()
        .map(|x| Choice::new(x.to_string(&app.opts.units), x))
        .collect()
}

pub fn maybe_edit_intersection(
    ctx: &mut EventCtx,
    app: &mut App,
    id: IntersectionID,
    mode: &GameplayMode,
) -> Option<Box<dyn State<App>>> {
    if app.primary.map.maybe_get_stop_sign(id).is_some()
        && mode.can_edit_stop_signs()
        && app.per_obj.left_click(ctx, "edit stop signs")
    {
        return Some(StopSignEditor::new_state(ctx, app, id, mode.clone()));
    }

    if app.primary.map.maybe_get_traffic_signal(id).is_some()
        && app.per_obj.left_click(ctx, "edit traffic signal")
    {
        return Some(TrafficSignalEditor::new_state(
            ctx,
            app,
            btreeset! {id},
            mode.clone(),
        ));
    }

    if app.primary.map.get_i(id).is_closed()
        && app.per_obj.left_click(ctx, "re-open closed intersection")
    {
        // This resets to the original state; it doesn't undo the closure to the last
        // state. Seems reasonable to me.
        let mut edits = app.primary.map.get_edits().clone();
        edits.commands.push(EditCmd::ChangeIntersection {
            i: id,
            old: app.primary.map.get_i_edit(id),
            new: edits.original_intersections[&id].clone(),
        });
        apply_map_edits(ctx, app, edits);
    }

    None
}

fn make_changelist(ctx: &mut EventCtx, app: &App) -> Panel {
    // TODO Support redo. Bit harder here to reset the redo_stack when the edits
    // change, because nested other places modify it too.
    let edits = app.primary.map.get_edits();
    let mut col = vec![
        Widget::row(vec![
            ctx.style()
                .btn_outline
                .popup(&edits.edits_name)
                .hotkey(lctrl(Key::P))
                .build_widget(ctx, "manage proposals"),
            "autosaved"
                .text_widget(ctx)
                .container()
                .padding(10)
                .bg(Color::hex("#5D9630")),
        ]),
        ColorLegend::row(
            ctx,
            app.cs.edits_layer,
            format!(
                "{} roads, {} intersections changed",
                edits.changed_roads.len(),
                edits.original_intersections.len()
            ),
        ),
    ];

    if edits.commands.len() > 5 {
        col.push(format!("{} more...", edits.commands.len() - 5).text_widget(ctx));
    }
    for idx in edits.commands.len().max(5) - 5..edits.commands.len() {
        let (summary, details) = edits.commands[idx].describe(&app.primary.map);
        let mut txt = Text::from(format!("{}) {}", idx + 1, summary));
        for line in details {
            txt.add_line(Line(line).secondary());
        }
        let btn = ctx
            .style()
            .btn_plain
            .btn()
            .label_styled_text(txt, ControlState::Default)
            .build_widget(ctx, format!("change #{}", idx + 1));
        if idx == edits.commands.len() - 1 {
            col.push(
                Widget::row(vec![
                    btn,
                    ctx.style()
                        .btn_close()
                        .hotkey(lctrl(Key::Z))
                        .build_widget(ctx, "undo"),
                ])
                .padding(16)
                .outline(ctx.style().btn_outline.outline),
            );
        } else {
            col.push(btn);
        }
    }

    Panel::new_builder(Widget::col(col))
        .aligned(HorizontalAlignment::Right, VerticalAlignment::Center)
        .build(ctx)
}

// TODO Ideally a Tab.
fn cmd_to_id(cmd: &EditCmd) -> Option<ID> {
    match cmd {
        EditCmd::ChangeRoad { r, .. } => Some(ID::Road(*r)),
        EditCmd::ChangeIntersection { i, .. } => Some(ID::Intersection(*i)),
        EditCmd::ChangeRouteSchedule { .. } => None,
    }
}

pub struct ConfirmDiscard {
    panel: Panel,
    discard: Box<dyn Fn(&mut App)>,
}

impl ConfirmDiscard {
    pub fn new_state(ctx: &mut EventCtx, discard: Box<dyn Fn(&mut App)>) -> Box<dyn State<App>> {
        Box::new(ConfirmDiscard {
            discard,
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Image::from_path("system/assets/tools/alert.svg")
                        .untinted()
                        .into_widget(ctx)
                        .container()
                        .padding_top(6),
                    Line("Alert").small_heading().into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                "Are you sure you want to discard changes you made?".text_widget(ctx),
                Widget::row(vec![
                    ctx.style()
                        .btn_outline
                        .text("Cancel")
                        .hotkey(Key::Escape)
                        .build_def(ctx),
                    ctx.style()
                        .btn_solid_destructive
                        .text("Yes, discard")
                        .build_def(ctx),
                ])
                .align_right(),
            ]))
            .build(ctx),
        })
    }
}

impl State<App> for ConfirmDiscard {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" | "Cancel" => Transition::Pop,
                "Yes, discard" => {
                    (self.discard)(app);
                    Transition::Multi(vec![Transition::Pop, Transition::Pop])
                }
                _ => unreachable!(),
            },
            _ => Transition::Keep,
        }
    }

    fn draw(&self, g: &mut GfxCtx, _: &App) {
        self.panel.draw(g);
    }
}