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
968
969
use std::collections::{BTreeSet, VecDeque};

use anyhow::Result;

use abstutil::Timer;
use geom::{Distance, Line, Polygon, Pt2D};
use map_gui::options::TrafficSignalStyle;
use map_gui::render::{traffic_signal, DrawMovement, DrawOptions};
use map_gui::tools::PopupMsg;
use map_model::{
    ControlTrafficSignal, EditCmd, EditIntersection, IntersectionID, MovementID, Stage, StageType,
    TurnPriority,
};
use widgetry::{
    include_labeled_bytes, lctrl, Color, ControlState, DragDrop, DrawBaselayer, Drawable, EventCtx,
    GeomBatch, GeomBatchStack, GfxCtx, HorizontalAlignment, Image, Key, Line, Outcome, Panel,
    RewriteColor, StackAxis, State, Text, TextExt, VerticalAlignment, Widget,
};

use crate::app::{App, ShowEverything, Transition};
use crate::common::CommonState;
use crate::edit::{apply_map_edits, ConfirmDiscard};
use crate::sandbox::GameplayMode;

mod edits;
mod gmns;
mod offsets;
mod picker;
mod preview;

// Welcome to one of the most overwhelmingly complicated parts of the UI...

pub struct TrafficSignalEditor {
    side_panel: Panel,
    top_panel: Panel,

    mode: GameplayMode,
    members: BTreeSet<IntersectionID>,
    current_stage: usize,

    movements: Vec<DrawMovement>,
    // And the next priority to toggle to
    movement_selected: Option<(MovementID, Option<TurnPriority>)>,
    draw_current: Drawable,
    tooltip: Option<Text>,

    command_stack: Vec<BundleEdits>,
    redo_stack: Vec<BundleEdits>,
    // Before synchronizing the number of stages
    original: BundleEdits,
    warn_changed: bool,

    fade_irrelevant: Drawable,
}

// For every member intersection, the full state of that signal
#[derive(Clone, PartialEq)]
pub struct BundleEdits {
    signals: Vec<ControlTrafficSignal>,
}

impl TrafficSignalEditor {
    pub fn new_state(
        ctx: &mut EventCtx,
        app: &mut App,
        members: BTreeSet<IntersectionID>,
        mode: GameplayMode,
    ) -> Box<dyn State<App>> {
        app.primary.current_selection = None;

        let original = BundleEdits::get_current(app, &members);
        let synced = BundleEdits::synchronize(app, &members);
        let warn_changed = original != synced;
        synced.apply(app);

        let mut editor = TrafficSignalEditor {
            side_panel: make_side_panel(ctx, app, &members, 0),
            top_panel: make_top_panel(ctx, app, false, false),
            mode,
            current_stage: 0,
            movements: Vec::new(),
            movement_selected: None,
            draw_current: Drawable::empty(ctx),
            tooltip: None,
            command_stack: Vec::new(),
            redo_stack: Vec::new(),
            warn_changed,
            original,
            fade_irrelevant: fade_irrelevant(app, &members).upload(ctx),
            members,
        };
        editor.recalc_draw_current(ctx, app);
        Box::new(editor)
    }

    fn change_stage(&mut self, ctx: &mut EventCtx, app: &App, idx: usize) {
        if self.current_stage == idx {
            let mut new = make_side_panel(ctx, app, &self.members, self.current_stage);
            new.restore(ctx, &self.side_panel);
            self.side_panel = new;
        } else {
            self.current_stage = idx;
            self.side_panel = make_side_panel(ctx, app, &self.members, self.current_stage);
        }

        self.recalc_draw_current(ctx, app);
    }

    fn add_new_edit<F: Fn(&mut ControlTrafficSignal)>(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut App,
        idx: usize,
        fxn: F,
    ) {
        let mut bundle = BundleEdits::get_current(app, &self.members);
        self.command_stack.push(bundle.clone());
        self.redo_stack.clear();
        for ts in &mut bundle.signals {
            fxn(ts);
        }
        bundle.apply(app);

        self.top_panel = make_top_panel(ctx, app, true, false);
        self.change_stage(ctx, app, idx);
    }

    fn recalc_draw_current(&mut self, ctx: &mut EventCtx, app: &App) {
        let mut batch = GeomBatch::new();
        let mut movements = Vec::new();
        for i in &self.members {
            let stage = &app.primary.map.get_traffic_signal(*i).stages[self.current_stage];
            for (m, draw) in DrawMovement::for_i(
                ctx.prerender,
                &app.primary.map,
                &app.cs,
                *i,
                self.current_stage,
            ) {
                if self
                    .movement_selected
                    .map(|(x, _)| x != m.id)
                    .unwrap_or(true)
                    || m.id.crosswalk
                {
                    batch.append(draw);
                } else if !stage.protected_movements.contains(&m.id)
                    && !stage.yield_movements.contains(&m.id)
                {
                    // Still draw the icon, but highlight it
                    batch.append(draw.color(RewriteColor::Change(
                        app.cs.signal_banned_turn.alpha(0.5),
                        Color::hex("#72CE36"),
                    )));
                }
                movements.push(m);
            }
            traffic_signal::draw_stage_number(
                ctx.prerender,
                app.primary.map.get_i(*i),
                self.current_stage,
                &mut batch,
            );
        }

        // Draw the selected thing on top of everything else
        if let Some((selected, next_priority)) = self.movement_selected {
            for m in &movements {
                if m.id == selected {
                    m.draw_selected_movement(app, &mut batch, next_priority);
                    break;
                }
            }
        }

        self.draw_current = ctx.upload(batch);
        self.movements = movements;
    }

    // We may have imported the signal configuration without validating it.
    fn validate_all_members(&self, app: &App) -> Result<()> {
        for i in &self.members {
            app.primary
                .map
                .get_traffic_signal(*i)
                .validate(app.primary.map.get_i(*i))?;
        }
        Ok(())
    }
}

impl State<App> for TrafficSignalEditor {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        self.tooltip = None;

        if self.warn_changed {
            self.warn_changed = false;
            return Transition::Push(PopupMsg::new_state(
                ctx,
                "Note",
                vec!["Some signals were modified to match the number and duration of stages"],
            ));
        }

        ctx.canvas_movement();

        let canonical_signal = app
            .primary
            .map
            .get_traffic_signal(*self.members.iter().next().unwrap());
        let num_stages = canonical_signal.stages.len();

        match self.side_panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "Edit entire signal" => {
                    return Transition::Push(edits::edit_entire_signal(
                        ctx,
                        app,
                        canonical_signal.id,
                        self.mode.clone(),
                        self.original.clone(),
                    ));
                }
                "Tune offsets between signals" => {
                    return Transition::Push(offsets::ShowAbsolute::new_state(
                        ctx,
                        app,
                        self.members.clone(),
                    ));
                }
                "Add a new stage" => {
                    self.add_new_edit(ctx, app, num_stages, |ts| {
                        ts.stages.push(Stage::new());
                    });
                    return Transition::Keep;
                }
                "change duration" => {
                    return Transition::Push(edits::ChangeDuration::new_state(
                        ctx,
                        app,
                        canonical_signal,
                        self.current_stage,
                    ));
                }
                "delete stage" => {
                    let idx = self.current_stage;
                    self.add_new_edit(ctx, app, 0, |ts| {
                        ts.stages.remove(idx);
                    });
                    return Transition::Keep;
                }
                "previous stage" => {
                    self.change_stage(ctx, app, self.current_stage - 1);
                    return Transition::Keep;
                }
                "next stage" => {
                    self.change_stage(ctx, app, self.current_stage + 1);
                    return Transition::Keep;
                }
                x => {
                    if let Some(x) = x.strip_prefix("stage ") {
                        let idx = x.parse::<usize>().unwrap() - 1;
                        self.change_stage(ctx, app, idx);
                        return Transition::Keep;
                    } else {
                        unreachable!()
                    }
                }
            },
            Outcome::DragDropReleased(_, old_idx, new_idx) => {
                self.add_new_edit(ctx, app, new_idx, |ts| {
                    ts.stages.swap(old_idx, new_idx);
                });
            }
            _ => {}
        }

        if let Outcome::Clicked(x) = self.top_panel.event(ctx) {
            match x.as_ref() {
                "Finish" => {
                    if let Some(bundle) = check_for_missing_turns(app, &self.members) {
                        bundle.apply(app);
                        self.command_stack.push(bundle);
                        self.redo_stack.clear();

                        self.top_panel = make_top_panel(ctx, app, true, false);
                        self.change_stage(ctx, app, 0);

                        return Transition::Push(PopupMsg::new_state(
                            ctx,
                            "Error: missing turns",
                            vec![
                                "Some turns are missing from this traffic signal",
                                "They've all been added as a new first stage. Please update your \
                                changes to include them.",
                            ],
                        ));
                    } else if let Err(err) = self.validate_all_members(app) {
                        // TODO There's some crash between usvg and lyon trying to tesellate the
                        // error text!
                        error!("{}", err);
                        return Transition::Push(PopupMsg::new_state(
                            ctx,
                            "Error",
                            vec!["This signal configuration is somehow invalid; check the console logs"]
                        ));
                    } else {
                        let changes = BundleEdits::get_current(app, &self.members);
                        self.original.apply(app);
                        changes.commit(ctx, app);
                        return Transition::Pop;
                    }
                }
                "Cancel" => {
                    if BundleEdits::get_current(app, &self.members) == self.original {
                        self.original.apply(app);
                        return Transition::Pop;
                    }
                    let original = self.original.clone();
                    return Transition::Push(ConfirmDiscard::new_state(
                        ctx,
                        Box::new(move |app| {
                            original.apply(app);
                        }),
                    ));
                }
                "Edit multiple signals" => {
                    if let Err(err) = self.validate_all_members(app) {
                        error!("{}", err);
                        return Transition::Push(PopupMsg::new_state(
                            ctx,
                            "Error",
                            vec!["This signal configuration is somehow invalid; check the console logs"]
                        ));
                    }

                    // First commit the current changes, so we enter SignalPicker with clean state.
                    // This UX flow is a little unintuitive.
                    let changes = check_for_missing_turns(app, &self.members)
                        .unwrap_or_else(|| BundleEdits::get_current(app, &self.members));
                    self.original.apply(app);
                    changes.commit(ctx, app);
                    return Transition::Replace(picker::SignalPicker::new_state(
                        ctx,
                        self.members.clone(),
                        self.mode.clone(),
                    ));
                }
                "Export" => {
                    for signal in BundleEdits::get_current(app, &self.members).signals {
                        let ts = signal.export(&app.primary.map);
                        abstio::write_json(
                            format!("traffic_signal_data/{}.json", ts.intersection_osm_node_id),
                            &ts,
                        );
                    }
                }
                "Preview" => {
                    // Might have to do this first!
                    app.primary
                        .map
                        .recalculate_pathfinding_after_edits(&mut Timer::throwaway());

                    return Transition::Push(preview::make_previewer(
                        ctx,
                        app,
                        self.members.clone(),
                        self.current_stage,
                    ));
                }
                "undo" => {
                    self.redo_stack
                        .push(BundleEdits::get_current(app, &self.members));
                    self.command_stack.pop().unwrap().apply(app);
                    self.top_panel = make_top_panel(ctx, app, !self.command_stack.is_empty(), true);
                    self.change_stage(ctx, app, 0);
                    return Transition::Keep;
                }
                "redo" => {
                    self.command_stack
                        .push(BundleEdits::get_current(app, &self.members));
                    self.redo_stack.pop().unwrap().apply(app);
                    self.top_panel = make_top_panel(ctx, app, true, !self.redo_stack.is_empty());
                    self.change_stage(ctx, app, 0);
                    return Transition::Keep;
                }
                _ => unreachable!(),
            }
        }

        {
            if self.current_stage != 0 && ctx.input.pressed(Key::LeftArrow) {
                self.change_stage(ctx, app, self.current_stage - 1);
            }

            if self.current_stage != num_stages - 1 && ctx.input.pressed(Key::RightArrow) {
                self.change_stage(ctx, app, self.current_stage + 1);
            }
        }

        if ctx.redo_mouseover() {
            let old = self.movement_selected;

            self.movement_selected = None;
            if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
                for m in &self.movements {
                    let signal = app.primary.map.get_traffic_signal(m.id.parent);
                    let i = app.primary.map.get_i(signal.id);
                    if m.hitbox.contains_pt(pt) {
                        let stage = &signal.stages[self.current_stage];
                        let next_priority = match stage.get_priority_of_movement(m.id) {
                            TurnPriority::Banned => {
                                if stage.could_be_protected(m.id, i) {
                                    Some(TurnPriority::Protected)
                                } else if m.id.crosswalk {
                                    None
                                } else {
                                    Some(TurnPriority::Yield)
                                }
                            }
                            TurnPriority::Yield => Some(TurnPriority::Banned),
                            TurnPriority::Protected => {
                                if m.id.crosswalk {
                                    Some(TurnPriority::Banned)
                                } else {
                                    Some(TurnPriority::Yield)
                                }
                            }
                        };
                        self.movement_selected = Some((m.id, next_priority));
                        break;
                    }
                }
            }

            if self.movement_selected != old {
                self.change_stage(ctx, app, self.current_stage);
            }
        }

        if let Some((id, Some(pri))) = self.movement_selected {
            let signal = app.primary.map.get_traffic_signal(id.parent);
            let mut txt = Text::new();
            txt.add_line(Line(format!(
                "{} {}",
                match signal.stages[self.current_stage].get_priority_of_movement(id) {
                    TurnPriority::Protected => "Protected",
                    TurnPriority::Yield => "Yielding",
                    TurnPriority::Banned => "Forbidden",
                },
                if id.crosswalk { "crosswalk" } else { "turn" },
            )));
            txt.add_appended(vec![
                Line("Click").fg(ctx.style().text_hotkey_color),
                Line(format!(
                    " to {}",
                    match pri {
                        TurnPriority::Protected => "add it as protected",
                        TurnPriority::Yield => "allow it after yielding",
                        TurnPriority::Banned => "forbid it",
                    }
                )),
            ]);
            self.tooltip = Some(txt);
            if app.per_obj.left_click(
                ctx,
                format!(
                    "toggle from {:?} to {:?}",
                    signal.stages[self.current_stage].get_priority_of_movement(id),
                    pri
                ),
            ) {
                let idx = self.current_stage;
                let movement = app.primary.map.get_i(id.parent).movements[&id].clone();
                self.add_new_edit(ctx, app, idx, |ts| {
                    if ts.id == id.parent {
                        ts.stages[idx].edit_movement(&movement, pri);
                    }
                });
                return Transition::KeepWithMouseover;
            }
        }

        Transition::Keep
    }

    fn draw_baselayer(&self) -> DrawBaselayer {
        DrawBaselayer::Custom
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        {
            let mut opts = DrawOptions::new();
            opts.suppress_traffic_signal_details
                .extend(self.members.clone());
            app.draw(g, opts, &ShowEverything::new());
        }
        g.redraw(&self.fade_irrelevant);
        g.redraw(&self.draw_current);

        self.top_panel.draw(g);
        self.side_panel.draw(g);

        if let Some((id, _)) = self.movement_selected {
            let osd = if id.crosswalk {
                Text::from(format!(
                    "Crosswalk across {}",
                    app.primary
                        .map
                        .get_r(id.from.id)
                        .get_name(app.opts.language.as_ref())
                ))
            } else {
                Text::from(format!(
                    "Turn from {} to {}",
                    app.primary
                        .map
                        .get_r(id.from.id)
                        .get_name(app.opts.language.as_ref()),
                    app.primary
                        .map
                        .get_r(id.to.id)
                        .get_name(app.opts.language.as_ref())
                ))
            };
            CommonState::draw_custom_osd(g, app, osd);
        } else {
            CommonState::draw_osd(g, app);
        }

        if let Some(txt) = self.tooltip.clone() {
            g.draw_mouse_tooltip(txt);
        }
    }
}

fn make_top_panel(ctx: &mut EventCtx, app: &App, can_undo: bool, can_redo: bool) -> Panel {
    let row = vec![
        ctx.style()
            .btn_solid_primary
            .text("Finish")
            .hotkey(Key::Enter)
            .build_def(ctx),
        ctx.style()
            .btn_outline
            .text("Preview")
            .hotkey(lctrl(Key::P))
            .build_def(ctx),
        ctx.style()
            .btn_plain
            .icon("system/assets/tools/undo.svg")
            .disabled(!can_undo)
            .hotkey(lctrl(Key::Z))
            .build_widget(ctx, "undo"),
        ctx.style()
            .btn_plain
            .icon("system/assets/tools/redo.svg")
            .disabled(!can_redo)
            // TODO ctrl+shift+Z!
            .hotkey(lctrl(Key::Y))
            .build_widget(ctx, "redo"),
        ctx.style()
            .btn_plain_destructive
            .text("Cancel")
            .hotkey(Key::Escape)
            .build_def(ctx)
            .align_right(),
    ];
    Panel::new_builder(Widget::col(vec![
        Widget::row(vec![
            Line("Traffic signal editor")
                .small_heading()
                .into_widget(ctx),
            ctx.style()
                .btn_plain
                .text("+ Edit multiple")
                .label_color(Color::hex("#4CA7E9"), ControlState::Default)
                .hotkey(Key::M)
                .build_widget(ctx, "Edit multiple signals"),
        ]),
        Widget::row(row),
        if app.opts.dev {
            ctx.style()
                .btn_outline
                .text("Export")
                .tooltip(Text::from_multiline(vec![
                    Line("This will create a JSON file in traffic_signal_data/.").small(),
                    Line(
                        "Contribute this to map how this traffic signal is currently timed in \
                         real life.",
                    )
                    .small(),
                ]))
                .build_def(ctx)
        } else {
            Widget::nothing()
        },
    ]))
    .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
    .build(ctx)
}

fn make_side_panel(
    ctx: &mut EventCtx,
    app: &App,
    members: &BTreeSet<IntersectionID>,
    selected: usize,
) -> Panel {
    let map = &app.primary.map;
    // Use any member for stage duration
    let canonical_signal = map.get_traffic_signal(*members.iter().next().unwrap());

    let mut txt = Text::new();
    if members.len() == 1 {
        let i = *members.iter().next().unwrap();
        txt.add_line(Line(i.to_string()).big_heading_plain());

        let mut road_names = BTreeSet::new();
        for r in &app.primary.map.get_i(i).roads {
            road_names.insert(
                app.primary
                    .map
                    .get_r(*r)
                    .get_name(app.opts.language.as_ref()),
            );
        }
        for r in road_names {
            txt.add_line(Line(format!("  {}", r)).secondary());
        }
    } else {
        txt.add_line(Line(format!("{} intersections", members.len())).big_heading_plain());
        txt.add_line(
            Line(
                members
                    .iter()
                    .map(|i| format!("#{}", i.0))
                    .collect::<Vec<_>>()
                    .join(", "),
            )
            .secondary(),
        );
    }
    let mut col = vec![txt.into_widget(ctx)];

    // Stage controls
    col.push(
        Widget::row(vec![
            ctx.style()
                .btn_plain
                .icon_bytes(include_labeled_bytes!(
                    "../../../../widgetry/icons/arrow_left.svg"
                ))
                .disabled(selected == 0)
                .build_widget(ctx, "previous stage"),
            ctx.style()
                .btn_plain
                .icon_bytes(include_labeled_bytes!(
                    "../../../../widgetry/icons/arrow_right.svg"
                ))
                .disabled(selected == canonical_signal.stages.len() - 1)
                .build_widget(ctx, "next stage"),
            match canonical_signal.stages[selected].stage_type {
                StageType::Fixed(d) => format!("Stage duration: {}", d),
                StageType::Variable(min, delay, additional) => format!(
                    "Stage duration: {}, {}, {} (variable)",
                    min, delay, additional
                ),
            }
            .text_widget(ctx)
            .centered_vert(),
            ctx.style()
                .btn_plain
                .icon("system/assets/tools/pencil.svg")
                .hotkey(Key::X)
                .build_widget(ctx, "change duration"),
            if canonical_signal.stages.len() > 1 {
                ctx.style()
                    .btn_solid_destructive
                    .icon("system/assets/tools/trash.svg")
                    .build_widget(ctx, "delete stage")
            } else {
                Widget::nothing()
            },
            ctx.style()
                .btn_plain
                .icon("system/assets/speed/plus.svg")
                .build_widget(ctx, "Add a new stage"),
        ])
        .padding(10)
        .bg(app.cs.inner_panel_bg),
    );

    let translations = squish_polygons_together(
        members
            .iter()
            .map(|i| app.primary.map.get_i(*i).polygon.clone())
            .collect(),
    );

    let mut drag_drop = DragDrop::new(ctx, "stage cards", StackAxis::Horizontal);
    for idx in 0..canonical_signal.stages.len() {
        let mut stack = GeomBatchStack::vertical(vec![
            Text::from(Line(format!(
                "Stage {}: {}",
                idx + 1,
                match canonical_signal.stages[idx].stage_type {
                    StageType::Fixed(d) => format!("{}", d),
                    StageType::Variable(min, _, _) => format!("{} (v)", min),
                },
            )))
            .render(ctx),
            draw_multiple_signals(ctx, app, members, idx, &translations),
        ]);
        stack.set_spacing(10.0);
        let icon_batch = stack.batch();
        let icon_bounds = icon_batch.get_bounds();
        let image = Image::from_batch(icon_batch, icon_bounds)
            .dims(150.0)
            .untinted()
            .padding(16);
        let (default_batch, bounds) = image.clone().build_batch(ctx).unwrap();
        let (hovering_batch, _) = image
            .clone()
            .bg_color(ctx.style().btn_tab.bg_disabled.dull(0.3))
            .build_batch(ctx)
            .unwrap();
        let (selected_batch, _) = image
            .bg_color(ctx.style().btn_solid_primary.bg)
            .build_batch(ctx)
            .unwrap();

        drag_drop.push_card(
            idx,
            bounds.into(),
            default_batch,
            hovering_batch,
            selected_batch,
        );
    }
    drag_drop.set_initial_state(Some(selected), None);

    col.push(drag_drop.into_widget(ctx));

    col.push(Widget::row(vec![
        // TODO Say "normally" to account for variable stages?
        format!(
            "One full cycle lasts {}",
            canonical_signal.simple_cycle_duration()
        )
        .text_widget(ctx)
        .centered_vert(),
        if members.len() == 1 {
            ctx.style()
                .btn_outline
                .text("Edit entire signal")
                .hotkey(Key::E)
                .build_def(ctx)
        } else {
            ctx.style()
                .btn_outline
                .text("Tune offsets between signals")
                .hotkey(Key::O)
                .build_def(ctx)
        },
    ]));

    Panel::new_builder(Widget::col(col))
        .aligned(HorizontalAlignment::Left, VerticalAlignment::Center)
        // Hovering on a stage card after dropping it produces Outcome::Changed
        .ignore_initial_events()
        .build(ctx)
}

impl BundleEdits {
    fn apply(&self, app: &mut App) {
        for s in &self.signals {
            app.primary.map.incremental_edit_traffic_signal(s.clone());
        }
    }

    fn commit(self, ctx: &mut EventCtx, app: &mut App) {
        // Skip if there's no change
        if self == BundleEdits::get_current(app, &self.signals.iter().map(|s| s.id).collect()) {
            return;
        }

        let mut edits = app.primary.map.get_edits().clone();
        // TODO Can we batch these commands somehow, so undo/redo in edit mode behaves properly?
        for signal in self.signals {
            edits.commands.push(EditCmd::ChangeIntersection {
                i: signal.id,
                old: app.primary.map.get_i_edit(signal.id),
                new: EditIntersection::TrafficSignal(signal.export(&app.primary.map)),
            });
        }
        apply_map_edits(ctx, app, edits);
    }

    fn get_current(app: &App, members: &BTreeSet<IntersectionID>) -> BundleEdits {
        let signals = members
            .iter()
            .map(|i| app.primary.map.get_traffic_signal(*i).clone())
            .collect();
        BundleEdits { signals }
    }

    // If the intersections haven't been edited together before, the number of stages and the
    // durations might not match up. Just initially force them to align somehow.
    fn synchronize(app: &App, members: &BTreeSet<IntersectionID>) -> BundleEdits {
        let map = &app.primary.map;
        // Pick one of the members with the most stages as canonical.
        let canonical = map.get_traffic_signal(
            *members
                .iter()
                .max_by_key(|i| map.get_traffic_signal(**i).stages.len())
                .unwrap(),
        );

        let mut signals = Vec::new();
        for i in members {
            let mut signal = map.get_traffic_signal(*i).clone();
            for (idx, canonical_stage) in canonical.stages.iter().enumerate() {
                if signal.stages.len() == idx {
                    signal.stages.push(Stage::new());
                }
                signal.stages[idx].stage_type = canonical_stage.stage_type.clone();
            }
            signals.push(signal);
        }

        BundleEdits { signals }
    }
}

// If None, nothing missing.
fn check_for_missing_turns(app: &App, members: &BTreeSet<IntersectionID>) -> Option<BundleEdits> {
    let mut all_missing = BTreeSet::new();
    for i in members {
        all_missing.extend(
            app.primary
                .map
                .get_traffic_signal(*i)
                .missing_turns(app.primary.map.get_i(*i)),
        );
    }
    if all_missing.is_empty() {
        return None;
    }

    let mut bundle = BundleEdits::get_current(app, members);
    // Stick all the missing turns in a new stage at the beginning.
    for signal in &mut bundle.signals {
        let mut stage = Stage::new();
        // TODO Could do this more efficiently
        for m in &all_missing {
            if m.parent != signal.id {
                continue;
            }
            if m.crosswalk {
                stage.protected_movements.insert(*m);
            } else {
                stage.yield_movements.insert(*m);
            }
        }
        signal.stages.insert(0, stage);
    }
    Some(bundle)
}

fn draw_multiple_signals(
    ctx: &mut EventCtx,
    app: &App,
    members: &BTreeSet<IntersectionID>,
    idx: usize,
    translations: &[(f64, f64)],
) -> GeomBatch {
    let mut batch = GeomBatch::new();
    for (i, (dx, dy)) in members.iter().zip(translations) {
        let mut piece = GeomBatch::new();
        piece.push(
            app.cs.normal_intersection,
            app.primary.map.get_i(*i).polygon.clone(),
        );
        traffic_signal::draw_signal_stage(
            ctx.prerender,
            &app.primary.map.get_traffic_signal(*i).stages[idx],
            idx,
            *i,
            None,
            &mut piece,
            app,
            TrafficSignalStyle::Yuwen,
        );
        batch.append(piece.translate(*dx, *dy));
    }

    // Make the whole thing fit a fixed width
    let square_dims = 150.0;
    batch = batch.autocrop();
    let bounds = batch.get_bounds();
    let zoom = (square_dims / bounds.width()).min(square_dims / bounds.height());
    batch.scale(zoom)
}

// TODO Move to geom?
fn squish_polygons_together(mut polygons: Vec<Polygon>) -> Vec<(f64, f64)> {
    if polygons.len() == 1 {
        return vec![(0.0, 0.0)];
    }

    // Can't be too big, or polygons could silently swap places. To be careful, pick something a
    // bit smaller than the smallest polygon.
    let step_size = 0.8
        * polygons.iter().fold(std::f64::MAX, |x, p| {
            x.min(p.get_bounds().width()).min(p.get_bounds().height())
        });

    let mut translations: Vec<(f64, f64)> =
        std::iter::repeat((0.0, 0.0)).take(polygons.len()).collect();
    // Once a polygon hits another while moving, stop adjusting it. Otherwise, go round-robin.
    let mut indices: VecDeque<usize> = (0..polygons.len()).collect();

    let mut attempts = 0;
    while !indices.is_empty() {
        let idx = indices.pop_front().unwrap();
        let center = Pt2D::center(&polygons.iter().map(|p| p.center()).collect::<Vec<_>>());
        let angle = Line::must_new(polygons[idx].center(), center).angle();
        let pt = Pt2D::new(0.0, 0.0).project_away(Distance::meters(step_size), angle);

        // Do we hit anything if we move this way?
        let translated = polygons[idx].translate(pt.x(), pt.y());
        if polygons
            .iter()
            .enumerate()
            .any(|(i, p)| i != idx && !translated.intersection(p).is_empty())
        {
            // Stop moving this polygon
        } else {
            translations[idx].0 += pt.x();
            translations[idx].1 += pt.y();
            polygons[idx] = translated;
            indices.push_back(idx);
        }

        attempts += 1;
        if attempts == 100 {
            break;
        }
    }

    translations
}

pub fn fade_irrelevant(app: &App, members: &BTreeSet<IntersectionID>) -> GeomBatch {
    let mut holes = Vec::new();
    for i in members {
        let i = app.primary.map.get_i(*i);
        holes.push(i.polygon.clone());
        for r in &i.roads {
            holes.push(app.primary.map.get_r(*r).get_thick_polygon());
        }
    }
    // The convex hull illuminates a bit more of the surrounding area, looks better
    let fade_area = Polygon::with_holes(
        app.primary.map.get_boundary_polygon().clone().into_ring(),
        vec![Polygon::convex_hull(holes).into_ring()],
    );
    GeomBatch::from(vec![(app.cs.fade_map_dark, fade_area)])
}