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

use geom::{Distance, Time};
use map_gui::ID;
use map_model::IntersectionID;
use sim::AgentID;
use widgetry::{
    Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel,
    State, VerticalAlignment, Widget,
};

use crate::app::{App, Transition};
use crate::common::CommonState;

/// Draws a preview of the path for the agent under the mouse cursor.
pub struct RoutePreview {
    // (the agent we're hovering on, the sim time, whether we're zoomed in, the drawn path)
    preview: Option<(AgentID, Time, bool, 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();
            let zoomed = ctx.canvas.cam_zoom >= app.opts.min_zoom_for_detail;
            if self
                .preview
                .as_ref()
                .map(|(a, t, z, _)| agent != *a || now != *t || zoomed != *z)
                .unwrap_or(true)
            {
                let mut batch = GeomBatch::new();
                // Only draw the preview when zoomed in. If we wanted to do this unzoomed, we'd
                // want a different style; the dashed lines don't show up well.
                if zoomed {
                    if let Some(trace) = app.primary.sim.trace_route(agent, &app.primary.map) {
                        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, zoomed, 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);
        }
    }
}

// TODO Refactor with SignalPicker
pub struct TrafficRecorder {
    members: BTreeSet<IntersectionID>,
    panel: Panel,
}

impl TrafficRecorder {
    pub fn new_state(ctx: &mut EventCtx, members: BTreeSet<IntersectionID>) -> Box<dyn State<App>> {
        Box::new(TrafficRecorder {
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Line("Select the bounding intersections for recording traffic")
                        .small_heading()
                        .into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                make_btn(ctx, members.len()),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
            members,
        })
    }
}

impl State<App> for TrafficRecorder {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        ctx.canvas_movement();
        if ctx.redo_mouseover() {
            app.primary.current_selection = app.mouseover_unzoomed_intersections(ctx);
        }
        if let Some(ID::Intersection(i)) = app.primary.current_selection {
            if !self.members.contains(&i) && app.per_obj.left_click(ctx, "add this intersection") {
                self.members.insert(i);
                let btn = make_btn(ctx, self.members.len());
                self.panel.replace(ctx, "record", btn);
            } else if self.members.contains(&i)
                && app.per_obj.left_click(ctx, "remove this intersection")
            {
                self.members.remove(&i);
                let btn = make_btn(ctx, self.members.len());
                self.panel.replace(ctx, "record", btn);
            }
        }

        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "record" => {
                    app.primary.sim.record_traffic_for(self.members.clone());
                    return Transition::Pop;
                }
                _ => unreachable!(),
            }
        }

        Transition::Keep
    }

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

        let mut batch = GeomBatch::new();
        for i in &self.members {
            batch.push(
                Color::RED.alpha(0.8),
                app.primary.map.get_i(*i).polygon.clone(),
            );
        }
        let draw = g.upload(batch);
        g.redraw(&draw);
    }
}

fn make_btn(ctx: &mut EventCtx, num: usize) -> Widget {
    let title = match num {
        0 => "Record 0 intersections".to_string(),
        1 => "Record 1 intersection".to_string(),
        _ => format!("Record {} intersections", num),
    };
    ctx.style()
        .btn_solid_primary
        .text(title)
        .disabled(num == 0)
        .hotkey(Key::Enter)
        .build_widget(ctx, "record")
}