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
use crate::app::App;
use crate::game::{DrawBaselayer, State, Transition};
use crate::helpers::color_for_trip_phase;
use crate::info::{OpenTrip, Tab};
use crate::sandbox::dashboards::table::Table;
use crate::sandbox::dashboards::DashTab;
use crate::sandbox::SandboxMode;
use geom::{Distance, Pt2D};
use sim::{TripEndpoint, TripID};
use widgetry::{Color, EventCtx, GeomBatch, GfxCtx, Outcome, Panel, RewriteColor, ScreenPt};

pub struct GenericTripTable<T, F, P: 'static + Fn(&mut EventCtx, &App, &Table<T, F>) -> Panel> {
    table: Table<T, F>,
    panel: Panel,
    make_panel: P,
    tab: DashTab,
}

impl<T: 'static, F: 'static, P: 'static + Fn(&mut EventCtx, &App, &Table<T, F>) -> Panel>
    GenericTripTable<T, F, P>
{
    pub fn new(
        ctx: &mut EventCtx,
        app: &App,
        tab: DashTab,
        table: Table<T, F>,
        make_panel: P,
    ) -> Box<dyn State> {
        let panel = (make_panel)(ctx, app, &table);
        Box::new(GenericTripTable {
            table,
            panel,
            make_panel,
            tab,
        })
    }

    fn recalc(&mut self, ctx: &mut EventCtx, app: &App) {
        let mut new = (self.make_panel)(ctx, app, &self.table);
        new.restore(ctx, &self.panel);
        self.panel = new;
    }
}

impl<T: 'static, F: 'static, P: 'static + Fn(&mut EventCtx, &App, &Table<T, F>) -> Panel> State
    for GenericTripTable<T, F, P>
{
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => {
                if self.table.clicked(&x) {
                    self.recalc(ctx, app);
                } else if let Ok(idx) = x.parse::<usize>() {
                    let trip = TripID(idx);
                    let person = app.primary.sim.trip_to_person(trip);
                    return Transition::Multi(vec![
                        Transition::Pop,
                        Transition::ModifyState(Box::new(move |state, ctx, app| {
                            let sandbox = state.downcast_mut::<SandboxMode>().unwrap();
                            let mut actions = sandbox.contextual_actions();
                            sandbox.controls.common.as_mut().unwrap().launch_info_panel(
                                ctx,
                                app,
                                Tab::PersonTrips(person, OpenTrip::single(trip)),
                                &mut actions,
                            );
                        })),
                    ]);
                } else {
                    return self.tab.transition(ctx, app, &x);
                }
            }
            Outcome::Changed => {
                self.table.panel_changed(&self.panel);
                self.recalc(ctx, app);
            }
            _ => {}
        }

        Transition::Keep
    }

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

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        g.clear(app.cs.dialog_bg);
        self.panel.draw(g);
        preview_trip(g, app, &self.panel);
    }
}

fn preview_trip(g: &mut GfxCtx, app: &App, panel: &Panel) {
    let inner_rect = panel.rect_of("preview").clone();
    let map_bounds = app.primary.map.get_bounds().clone();
    let zoom = 0.15 * g.canvas.window_width / map_bounds.width().max(map_bounds.height());
    g.fork(
        Pt2D::new(map_bounds.min_x, map_bounds.min_y),
        ScreenPt::new(inner_rect.x1, inner_rect.y1),
        zoom,
        None,
    );
    g.enable_clipping(inner_rect);

    g.redraw(&app.primary.draw_map.boundary_polygon);
    g.redraw(&app.primary.draw_map.draw_all_areas);
    g.redraw(
        &app.primary
            .draw_map
            .draw_all_unzoomed_roads_and_intersections,
    );

    if let Some(x) = panel.currently_hovering() {
        if let Ok(idx) = x.parse::<usize>() {
            let trip = TripID(idx);
            preview_route(g, app, trip).draw(g);
        }
    }

    g.disable_clipping();
    g.unfork();
}

fn preview_route(g: &mut GfxCtx, app: &App, id: TripID) -> GeomBatch {
    let mut batch = GeomBatch::new();
    for p in app
        .primary
        .sim
        .get_analytics()
        .get_trip_phases(id, &app.primary.map)
    {
        if let Some((dist, ref path)) = p.path {
            if let Some(trace) = path.trace(&app.primary.map, dist, None) {
                batch.push(
                    color_for_trip_phase(app, p.phase_type),
                    trace.make_polygons(Distance::meters(20.0)),
                );
            }
        }
    }

    let trip = app.primary.sim.trip_info(id);
    batch.append(
        GeomBatch::load_svg(g.prerender, "system/assets/timeline/start_pos.svg")
            .scale(10.0)
            .color(RewriteColor::Change(Color::WHITE, Color::BLACK))
            .color(RewriteColor::Change(
                Color::hex("#5B5B5B"),
                Color::hex("#CC4121"),
            ))
            .centered_on(match trip.start {
                TripEndpoint::Bldg(b) => app.primary.map.get_b(b).label_center,
                TripEndpoint::Border(i, _) => app.primary.map.get_i(i).polygon.center(),
            }),
    );
    batch.append(
        GeomBatch::load_svg(g.prerender, "system/assets/timeline/goal_pos.svg")
            .scale(10.0)
            .color(RewriteColor::Change(Color::WHITE, Color::BLACK))
            .color(RewriteColor::Change(
                Color::hex("#5B5B5B"),
                Color::hex("#CC4121"),
            ))
            .centered_on(match trip.end {
                TripEndpoint::Bldg(b) => app.primary.map.get_b(b).label_center,
                TripEndpoint::Border(i, _) => app.primary.map.get_i(i).polygon.center(),
            }),
    );

    batch
}