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
use std::collections::{HashMap, HashSet};

use geom::{Angle, ArrowCap, Circle, Distance, PolyLine, Polygon};
use map_model::{IntersectionID, LaneID, Map, MovementID, TurnPriority, SIDEWALK_THICKNESS};
use widgetry::{Color, GeomBatch, Prerender};

use crate::colors::ColorScheme;
use crate::render::{traffic_signal, BIG_ARROW_THICKNESS};
use crate::AppLike;

const TURN_ICON_ARROW_LENGTH: Distance = Distance::const_meters(1.5);

pub struct DrawMovement {
    pub id: MovementID,
    pub hitbox: Polygon,
}

impl DrawMovement {
    // Also returns the stuff to draw each movement
    pub fn for_i(
        prerender: &Prerender,
        map: &Map,
        cs: &ColorScheme,
        i: IntersectionID,
        idx: usize,
    ) -> Vec<(DrawMovement, GeomBatch)> {
        let signal = map.get_traffic_signal(i);
        let stage = &signal.stages[idx];

        // TODO Sort by angle here if we want some consistency
        let mut offset_per_lane: HashMap<LaneID, usize> = HashMap::new();
        let mut results = Vec::new();
        for movement in signal.movements.values() {
            let mut batch = GeomBatch::new();
            // TODO Refactor the slice_start/slice_end stuff from draw_signal_stage.
            let hitbox = if stage.protected_movements.contains(&movement.id) {
                if movement.id.crosswalk {
                    batch = traffic_signal::walk_icon(movement, prerender);
                    batch.unioned_polygon()
                } else {
                    let arrow = movement
                        .geom
                        .make_arrow(BIG_ARROW_THICKNESS, ArrowCap::Triangle);
                    batch.push(cs.signal_protected_turn, arrow.clone());
                    arrow
                }
            } else if stage.yield_movements.contains(&movement.id) {
                let pl = &movement.geom;
                // We currently always assume the turn intersects a crosswalk at the beginning and
                // end, so draw without overlaps if the polyline is long enough.
                if pl.length() >= 2.0 * SIDEWALK_THICKNESS {
                    batch.extend(
                        Color::BLACK,
                        pl.exact_slice(
                            SIDEWALK_THICKNESS - Distance::meters(0.1),
                            pl.length() - SIDEWALK_THICKNESS + Distance::meters(0.1),
                        )
                        .dashed_arrow(
                            BIG_ARROW_THICKNESS,
                            Distance::meters(1.2),
                            Distance::meters(0.3),
                            ArrowCap::Triangle,
                        ),
                    );
                    let arrow = pl
                        .exact_slice(SIDEWALK_THICKNESS, pl.length() - SIDEWALK_THICKNESS)
                        .dashed_arrow(
                            BIG_ARROW_THICKNESS / 2.0,
                            Distance::meters(1.0),
                            Distance::meters(0.5),
                            ArrowCap::Triangle,
                        );
                    batch.extend(cs.signal_protected_turn, arrow.clone());
                } else {
                    // TODO These turns are often too small to even dash the arrow. So they'll just
                    // look like solid protected turns...
                    warn!("{:?} is too short to render as a yield movement", movement.id);
                    batch.extend(
                        cs.signal_protected_turn,
                        pl.dashed_arrow(
                            BIG_ARROW_THICKNESS / 2.0,
                            Distance::meters(1.0),
                            Distance::meters(0.5),
                            ArrowCap::Triangle,
                        ),
                    );
                }
                // Bit weird, but don't use the dashed arrow as the hitbox. The gaps in between
                // should still be clickable.
                movement
                    .geom
                    .make_arrow(BIG_ARROW_THICKNESS, ArrowCap::Triangle)
            } else if movement.id.crosswalk {
                batch = traffic_signal::dont_walk_icon(movement, prerender);
                batch.unioned_polygon()
            } else {
                // Use circular icons for banned turns
                let offset = movement
                    .members
                    .iter()
                    .map(|t| *offset_per_lane.entry(t.src).or_insert(0))
                    .max()
                    .unwrap();
                let (pl, _) = movement.src_center_and_width(map);
                let (circle, arrow) = make_circle_geom(offset as f64, pl, movement.angle);
                let mut seen_lanes = HashSet::new();
                for t in &movement.members {
                    if !seen_lanes.contains(&t.src) {
                        *offset_per_lane.get_mut(&t.src).unwrap() = offset + 1;
                        seen_lanes.insert(t.src);
                    }
                }
                batch.push(Color::hex("#7C7C7C"), circle.clone());
                batch.push(Color::WHITE, arrow);
                circle
            };
            results.push((
                DrawMovement {
                    id: movement.id,
                    hitbox,
                },
                batch,
            ));
        }
        results
    }

    pub fn draw_selected_movement(
        &self,
        app: &dyn AppLike,
        batch: &mut GeomBatch,
        next_priority: Option<TurnPriority>,
    ) {
        let movement = &app.map().get_traffic_signal(self.id.parent).movements[&self.id];
        let pl = &movement.geom;

        let green = Color::hex("#72CE36");
        match next_priority {
            Some(TurnPriority::Protected) => {
                let arrow = pl.make_arrow(BIG_ARROW_THICKNESS, ArrowCap::Triangle);
                batch.push(green.alpha(0.5), arrow.clone());
                if let Ok(p) = arrow.to_outline(Distance::meters(0.1)) {
                    batch.push(green, p);
                }
            }
            Some(TurnPriority::Yield) => {
                batch.extend(
                    // TODO Ideally the inner part would be the lower opacity green, but can't yet
                    // express that it should cover up the thicker solid blue beneath it
                    Color::BLACK.alpha(0.8),
                    pl.dashed_arrow(
                        BIG_ARROW_THICKNESS,
                        Distance::meters(1.2),
                        Distance::meters(0.3),
                        ArrowCap::Triangle,
                    ),
                );
                batch.extend(
                    green.alpha(0.8),
                    pl.exact_slice(Distance::meters(0.1), pl.length() - Distance::meters(0.1))
                        .dashed_arrow(
                            BIG_ARROW_THICKNESS / 2.0,
                            Distance::meters(1.0),
                            Distance::meters(0.5),
                            ArrowCap::Triangle,
                        ),
                );
            }
            Some(TurnPriority::Banned) => {
                let red = Color::hex("#EB3223");
                batch.extend(
                    Color::BLACK.alpha(0.8),
                    pl.dashed_arrow(
                        BIG_ARROW_THICKNESS,
                        Distance::meters(1.2),
                        Distance::meters(0.3),
                        ArrowCap::Triangle,
                    ),
                );
                batch.extend(
                    red.alpha(0.8),
                    pl.exact_slice(Distance::meters(0.1), pl.length() - Distance::meters(0.1))
                        .dashed_arrow(
                            BIG_ARROW_THICKNESS / 2.0,
                            Distance::meters(1.0),
                            Distance::meters(0.5),
                            ArrowCap::Triangle,
                        ),
                );
            }
            None => {}
        }
    }
}

// Produces (circle, arrow)
fn make_circle_geom(offset: f64, pl: PolyLine, angle: Angle) -> (Polygon, Polygon) {
    let height = 2.0 * TURN_ICON_ARROW_LENGTH;
    // Always extend the pl first to handle short entry lanes
    let extension = PolyLine::must_new(vec![
        pl.last_pt(),
        pl.last_pt()
            .project_away(Distance::meters(500.0), pl.last_line().angle()),
    ]);
    let pl = pl.must_extend(extension);
    let slice = pl.exact_slice(offset * height, (offset + 1.0) * height);
    let center = slice.middle();
    let block = Circle::new(center, TURN_ICON_ARROW_LENGTH).to_polygon();

    let arrow = PolyLine::must_new(vec![
        center.project_away(TURN_ICON_ARROW_LENGTH / 2.0, angle.opposite()),
        center.project_away(TURN_ICON_ARROW_LENGTH / 2.0, angle),
    ])
    .make_arrow(Distance::meters(0.5), ArrowCap::Triangle);

    (block, arrow)
}