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
use std::collections::BTreeSet;

use geom::{Angle, ArrowCap, Circle, Distance, Duration, Line, PolyLine, Pt2D};
use map_model::{
    Intersection, IntersectionID, Movement, Stage, StageType, TurnPriority, SIDEWALK_THICKNESS,
};
use widgetry::{Color, GeomBatch, Line, Prerender, RewriteColor, Text};

use crate::options::TrafficSignalStyle;
use crate::render::intersection::make_crosswalk;
use crate::render::BIG_ARROW_THICKNESS;
use crate::AppLike;

pub fn draw_signal_stage(
    prerender: &Prerender,
    stage: &Stage,
    idx: usize,
    i: IntersectionID,
    time_left: Option<Duration>,
    batch: &mut GeomBatch,
    app: &dyn AppLike,
    signal_style: TrafficSignalStyle,
) {
    let i = app.map().get_i(i);

    match signal_style {
        TrafficSignalStyle::Brian => {
            let mut dont_walk = BTreeSet::new();
            let mut crossed_roads = BTreeSet::new();
            for m in i.movements.keys() {
                if m.crosswalk {
                    dont_walk.insert(m);
                    // TODO This is incorrect; some crosswalks hop over intermediate roads. How do
                    // we detect or plumb that?
                    crossed_roads.insert((m.from.road, m.parent));
                    crossed_roads.insert((m.to.road, m.parent));
                }
            }

            let (yellow_light, percent) = if let Some(t) = time_left {
                if stage.stage_type.simple_duration() > Duration::ZERO {
                    (
                        t <= Duration::seconds(5.0),
                        (t / stage.stage_type.simple_duration()) as f32,
                    )
                } else {
                    (true, 1.0)
                }
            } else {
                (false, 1.0)
            };
            let arrow_body_color = if yellow_light {
                // The warning color for fixed is yellow, for anything else its orange to clue the
                // user into it possibly extending.
                if let StageType::Fixed(_) = stage.stage_type {
                    Color::YELLOW
                } else {
                    Color::ORANGE
                }
            } else {
                app.cs().signal_protected_turn.alpha(percent)
            };

            for m in &stage.yield_movements {
                assert!(!m.crosswalk);
                let pl = &i.movements[m].geom;
                // TODO Make dashed_arrow draw the last polygon without an awkward overlap. Then we
                // can just make one call here and control the outline thickness just using
                // to_outline.
                if let Ok(slice) = pl.maybe_exact_slice(
                    SIDEWALK_THICKNESS - Distance::meters(0.1),
                    pl.length() - SIDEWALK_THICKNESS + Distance::meters(0.1),
                ) {
                    batch.extend(
                        Color::BLACK,
                        slice.dashed_arrow(
                            BIG_ARROW_THICKNESS,
                            Distance::meters(1.2),
                            Distance::meters(0.3),
                            ArrowCap::Triangle,
                        ),
                    );
                }
                if let Ok(slice) =
                    pl.maybe_exact_slice(SIDEWALK_THICKNESS, pl.length() - SIDEWALK_THICKNESS)
                {
                    batch.extend(
                        arrow_body_color,
                        slice.dashed_arrow(
                            BIG_ARROW_THICKNESS / 2.0,
                            Distance::meters(1.0),
                            Distance::meters(0.5),
                            ArrowCap::Triangle,
                        ),
                    );
                }
            }

            for m in &stage.protected_movements {
                if !m.crosswalk {
                    // TODO Maybe less if shoulders meet
                    let slice_start = if crossed_roads.contains(&(m.from.road, m.parent)) {
                        SIDEWALK_THICKNESS
                    } else {
                        Distance::ZERO
                    };
                    let slice_end = if crossed_roads.contains(&(m.to.road, m.parent)) {
                        SIDEWALK_THICKNESS
                    } else {
                        Distance::ZERO
                    };

                    let pl = &i.movements[m].geom;
                    if let Ok(pl) = pl.maybe_exact_slice(slice_start, pl.length() - slice_end) {
                        let arrow = pl.make_arrow(BIG_ARROW_THICKNESS, ArrowCap::Triangle);
                        batch.push(arrow_body_color, arrow.clone());
                        batch.push(Color::BLACK, arrow.to_outline(Distance::meters(0.2)));
                    }
                } else {
                    batch.append(
                        walk_icon(&i.movements[m], prerender)
                            .color(RewriteColor::ChangeAlpha(percent)),
                    );
                    dont_walk.remove(m);
                }
            }

            for m in dont_walk {
                batch.append(dont_walk_icon(&i.movements[m], prerender));
            }

            draw_stage_number(prerender, i, idx, batch);
        }
        TrafficSignalStyle::Yuwen => {
            for m in &stage.yield_movements {
                assert!(!m.crosswalk);
                let arrow = i.movements[m]
                    .geom
                    .make_arrow(BIG_ARROW_THICKNESS * 2.0, ArrowCap::Triangle);
                batch.push(app.cs().signal_permitted_turn.alpha(0.3), arrow.clone());
                batch.push(
                    app.cs().signal_permitted_turn,
                    arrow.to_outline(BIG_ARROW_THICKNESS / 2.0),
                );
            }
            for m in &stage.protected_movements {
                if m.crosswalk {
                    // TODO This only works on the side panel. On the full map, the crosswalks are
                    // always drawn, so this awkwardly doubles some of them.
                    make_crosswalk(
                        batch,
                        app.map().get_t(i.movements[m].members[0]),
                        app.map(),
                        app.cs(),
                    );
                } else {
                    batch.push(
                        app.cs().signal_protected_turn,
                        i.movements[m]
                            .geom
                            .make_arrow(BIG_ARROW_THICKNESS * 2.0, ArrowCap::Triangle),
                    );
                }
            }
            if let Some(t) = time_left {
                draw_time_left(app, prerender, stage, i, idx, t, batch);
            }
        }
        TrafficSignalStyle::IndividualTurnArrows => {
            for turn in &i.turns {
                if turn.between_sidewalks() {
                    continue;
                }
                match stage.get_priority_of_turn(turn.id, i) {
                    TurnPriority::Protected => {
                        batch.push(
                            app.cs().signal_protected_turn,
                            turn.geom
                                .make_arrow(BIG_ARROW_THICKNESS * 2.0, ArrowCap::Triangle),
                        );
                    }
                    TurnPriority::Yield => {
                        batch.push(
                            app.cs().signal_permitted_turn,
                            turn.geom
                                .make_arrow(BIG_ARROW_THICKNESS * 2.0, ArrowCap::Triangle)
                                .to_outline(BIG_ARROW_THICKNESS / 2.0),
                        );
                    }
                    TurnPriority::Banned => {}
                }
            }
            if let Some(t) = time_left {
                draw_time_left(app, prerender, stage, i, idx, t, batch);
            }
        }
    }
}

pub fn draw_stage_number(
    prerender: &Prerender,
    i: &Intersection,
    idx: usize,
    batch: &mut GeomBatch,
) {
    let radius = Distance::meters(1.0);
    let center = i.polygon.polylabel();
    batch.push(
        Color::hex("#5B5B5B"),
        Circle::new(center, radius).to_polygon(),
    );
    batch.append(
        Text::from(Line(format!("{}", idx + 1)).fg(Color::WHITE))
            .render_autocropped(prerender)
            .scale(0.075)
            .centered_on(center),
    );
}

fn draw_time_left(
    app: &dyn AppLike,
    prerender: &Prerender,
    stage: &Stage,
    i: &Intersection,
    idx: usize,
    time_left: Duration,
    batch: &mut GeomBatch,
) {
    let radius = Distance::meters(2.0);
    let center = i.polygon.center();
    let duration = stage.stage_type.simple_duration();
    let percent = if duration > Duration::ZERO {
        time_left / duration
    } else {
        1.0
    };
    batch.push(
        app.cs().signal_box,
        Circle::new(center, 1.2 * radius).to_polygon(),
    );
    batch.push(
        app.cs().signal_spinner.alpha(0.3),
        Circle::new(center, radius).to_polygon(),
    );
    batch.push(
        app.cs().signal_spinner,
        Circle::new(center, radius).to_partial_tessellation(percent),
    );
    batch.append(
        Text::from(format!("{}", idx + 1))
            .render_autocropped(prerender)
            .scale(0.1)
            .centered_on(center),
    );
}

pub fn walk_icon(movement: &Movement, prerender: &Prerender) -> GeomBatch {
    let (center, angle) = crosswalk_icon(&movement.geom);
    GeomBatch::load_svg(prerender, "system/assets/map/walk.svg")
        .scale(0.07)
        .centered_on(center)
        .rotate(angle)
}
pub fn dont_walk_icon(movement: &Movement, prerender: &Prerender) -> GeomBatch {
    let (center, angle) = crosswalk_icon(&movement.geom);
    GeomBatch::load_svg(prerender, "system/assets/map/dont_walk.svg")
        .scale(0.07)
        .centered_on(center)
        .rotate(angle)
}

// TODO Kind of a hack to know that the second point is a better center.
// Returns (center, angle)
fn crosswalk_icon(geom: &PolyLine) -> (Pt2D, Angle) {
    let l = Line::must_new(geom.points()[1], geom.points()[2]);
    (
        l.dist_along(Distance::meters(1.0))
            .unwrap_or_else(|_| l.pt1()),
        l.angle().shortest_rotation_towards(Angle::degrees(90.0)),
    )
}