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
use crate::app::{App, ShowEverything};
use crate::common::ColorLegend;
use crate::game::{DrawBaselayer, State, Transition};
use crate::render::{DrawOptions, BIG_ARROW_THICKNESS};
use geom::{ArrowCap, Distance, Time};
use map_model::{LaneID, TurnType};
use sim::{AgentID, DontDrawAgents};
use widgetry::{
    Btn, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome,
    Panel, Text, TextExt, VerticalAlignment, Widget,
};

pub struct RoutePreview {
    preview: Option<(AgentID, Time, Drawable)>,
}

impl RoutePreview {
    pub fn new() -> RoutePreview {
        RoutePreview { preview: None }
    }
}

impl RoutePreview {
    pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Option<Transition> {
        if let Some(agent) = app
            .primary
            .current_selection
            .as_ref()
            .and_then(|id| id.agent_id())
        {
            let now = app.primary.sim.time();
            if self
                .preview
                .as_ref()
                .map(|(a, t, _)| agent != *a || now != *t)
                .unwrap_or(true)
            {
                if let Some(trace) = app.primary.sim.trace_route(agent, &app.primary.map, None) {
                    let mut batch = GeomBatch::new();
                    batch.extend(
                        app.cs.route,
                        trace.dashed_lines(
                            Distance::meters(0.75),
                            Distance::meters(1.0),
                            Distance::meters(0.4),
                        ),
                    );
                    self.preview = Some((agent, now, batch.upload(ctx)));
                }
            }
            return None;
        }
        self.preview = None;

        None
    }

    pub fn draw(&self, g: &mut GfxCtx) {
        if let Some((_, _, ref d)) = self.preview {
            g.redraw(d);
        }
    }
}

pub struct TurnExplorer {
    l: LaneID,
    // 0 means all turns, otherwise one particular turn
    idx: usize,
    panel: Panel,
}

impl TurnExplorer {
    pub fn new(ctx: &mut EventCtx, app: &App, l: LaneID) -> Box<dyn State> {
        Box::new(TurnExplorer {
            l,
            idx: 0,
            panel: TurnExplorer::make_panel(ctx, app, l, 0),
        })
    }
}

impl State for TurnExplorer {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        ctx.canvas_movement();

        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "previous turn" => {
                    self.idx -= 1;
                    self.panel = TurnExplorer::make_panel(ctx, app, self.l, self.idx);
                }
                "next turn" => {
                    self.idx += 1;
                    self.panel = TurnExplorer::make_panel(ctx, app, self.l, self.idx);
                }
                _ => unreachable!(),
            },
            _ => {}
        }

        Transition::Keep
    }

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

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        let mut opts = DrawOptions::new();
        {
            let l = app.primary.map.get_l(self.l);
            opts.suppress_traffic_signal_details.push(l.src_i);
            opts.suppress_traffic_signal_details.push(l.dst_i);
        }
        app.draw(g, opts, &DontDrawAgents {}, &ShowEverything::new());

        if self.idx == 0 {
            for turn in &app.primary.map.get_turns_from_lane(self.l) {
                g.draw_polygon(
                    color_turn_type(turn.turn_type).alpha(0.5),
                    turn.geom
                        .make_arrow(BIG_ARROW_THICKNESS, ArrowCap::Triangle),
                );
            }
        } else {
            let current = &app.primary.map.get_turns_from_lane(self.l)[self.idx - 1];

            let mut batch = GeomBatch::new();
            for t in app.primary.map.get_turns_in_intersection(current.id.parent) {
                if current.conflicts_with(t) {
                    batch.extend(
                        CONFLICTING_TURN,
                        t.geom.dashed_arrow(
                            BIG_ARROW_THICKNESS,
                            Distance::meters(1.0),
                            Distance::meters(0.5),
                            ArrowCap::Triangle,
                        ),
                    );
                }
            }
            batch.push(
                CURRENT_TURN,
                current
                    .geom
                    .make_arrow(BIG_ARROW_THICKNESS, ArrowCap::Triangle),
            );
            batch.draw(g);
        }

        self.panel.draw(g);
    }
}

impl TurnExplorer {
    fn make_panel(ctx: &mut EventCtx, app: &App, l: LaneID, idx: usize) -> Panel {
        let turns = app.primary.map.get_turns_from_lane(l);

        let mut col = vec![Widget::row(vec![
            Text::from(
                Line(format!(
                    "Turns from {}",
                    app.primary
                        .map
                        .get_parent(l)
                        .get_name(app.opts.language.as_ref())
                ))
                .small_heading(),
            )
            .draw(ctx),
            Widget::vert_separator(ctx, 50.0),
            if idx == 0 {
                Btn::text_fg("<").inactive(ctx)
            } else {
                Btn::text_fg("<").build(ctx, "previous turn", Key::LeftArrow)
            },
            Text::from(Line(format!("{}/{}", idx, turns.len())).secondary())
                .draw(ctx)
                .centered_vert(),
            if idx == turns.len() {
                Btn::text_fg(">").inactive(ctx)
            } else {
                Btn::text_fg(">").build(ctx, "next turn", Key::RightArrow)
            },
            Btn::text_fg("X").build(ctx, "close", Key::Escape),
        ])];
        if idx == 0 {
            if app.primary.map.get_l(l).is_walkable() {
                col.push(ColorLegend::row(
                    ctx,
                    color_turn_type(TurnType::Crosswalk),
                    "crosswalk",
                ));
                col.push(ColorLegend::row(
                    ctx,
                    color_turn_type(TurnType::SharedSidewalkCorner),
                    "sidewalk connection",
                ));
            } else {
                col.push(ColorLegend::row(
                    ctx,
                    color_turn_type(TurnType::Straight),
                    "straight",
                ));
                col.push(ColorLegend::row(
                    ctx,
                    color_turn_type(TurnType::Right),
                    "right turn",
                ));
                col.push(ColorLegend::row(
                    ctx,
                    color_turn_type(TurnType::Left),
                    "left turn",
                ));
            }
        } else {
            let (lt, lc, slow_lane) = turns[idx - 1].penalty(&app.primary.map);
            let (vehicles, bike) = app
                .primary
                .sim
                .target_lane_penalty(app.primary.map.get_l(turns[idx - 1].id.dst));
            col.push(
                format!(
                    "Penalties: {} for lane types, {} for lane changing, {} for keeping to the \
                     slow lane, {} for vehicles, {} for slow bikes",
                    lt, lc, slow_lane, vehicles, bike
                )
                .draw_text(ctx),
            );
            col.push(ColorLegend::row(ctx, CURRENT_TURN, "current turn"));
            col.push(ColorLegend::row(ctx, CONFLICTING_TURN, "conflicting turn"));
        }

        Panel::new(Widget::col(col))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx)
    }
}

// Since this is extremely localized and probably changing, not going to put this in ColorScheme.
fn color_turn_type(t: TurnType) -> Color {
    match t {
        TurnType::SharedSidewalkCorner => Color::BLACK,
        TurnType::Crosswalk => Color::WHITE,
        TurnType::Straight => Color::BLUE,
        TurnType::Right => Color::GREEN,
        TurnType::Left => Color::RED,
    }
}

const CURRENT_TURN: Color = Color::GREEN;
const CONFLICTING_TURN: Color = Color::RED.alpha(0.8);