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
use crate::app::{App, ShowEverything};
use crate::common::{make_heatmap, HeatmapOptions};
use crate::game::{State, Transition};
use crate::helpers::{amenity_type, ID};
use abstutil::Counter;
use ezgui::{
    hotkey, Btn, Checkbox, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx,
    HorizontalAlignment, Key, Line, Outcome, Text, VerticalAlignment, Widget,
};
use map_model::BuildingID;
use sim::{DontDrawAgents, Scenario, TripEndpoint};

pub struct PopularDestinations {
    per_bldg: Counter<BuildingID>,
    composite: Composite,
    draw: Drawable,
}

impl PopularDestinations {
    pub fn new(ctx: &mut EventCtx, app: &App, scenario: &Scenario) -> Box<dyn State> {
        let mut per_bldg = Counter::new();
        for p in &scenario.people {
            for trip in &p.trips {
                if let TripEndpoint::Bldg(b) = trip.trip.end(&app.primary.map) {
                    per_bldg.inc(b);
                }
            }
        }
        PopularDestinations::make(ctx, app, per_bldg, None)
    }

    fn make(
        ctx: &mut EventCtx,
        app: &App,
        per_bldg: Counter<BuildingID>,
        opts: Option<HeatmapOptions>,
    ) -> Box<dyn State> {
        let map = &app.primary.map;
        let mut batch = GeomBatch::new();
        let controls = if let Some(ref o) = opts {
            let mut pts = Vec::new();
            for (b, cnt) in per_bldg.borrow() {
                let pt = map.get_b(*b).label_center;
                for _ in 0..*cnt {
                    pts.push(pt);
                }
            }
            // TODO Er, the heatmap actually looks terrible.
            let legend = make_heatmap(ctx, &mut batch, map.get_bounds(), pts, o);
            Widget::col(o.to_controls(ctx, legend))
        } else {
            let max = per_bldg.max();
            let gradient = colorous::REDS;
            for (b, cnt) in per_bldg.borrow() {
                let c = gradient.eval_rational(*cnt, max);
                batch.push(
                    Color::rgb(c.r as usize, c.g as usize, c.b as usize),
                    map.get_b(*b).polygon.clone(),
                );
            }
            Widget::nothing()
        };

        let mut by_type = Counter::new();
        for (b, cnt) in per_bldg.borrow() {
            let mut other = true;
            for (_, amenity) in &map.get_b(*b).amenities {
                if let Some(t) = amenity_type(amenity) {
                    by_type.add(t, *cnt);
                    other = false;
                }
            }
            if other {
                by_type.add("other", *cnt);
            }
        }
        let mut breakdown = Text::from(Line("Breakdown by type"));
        let mut list = by_type.consume().into_iter().collect::<Vec<_>>();
        list.sort_by_key(|(_, cnt)| *cnt);
        list.reverse();
        let sum = per_bldg.sum() as f64;
        for (category, cnt) in list {
            breakdown.add(Line(format!(
                "{}: {}%",
                category,
                ((cnt as f64) / sum * 100.0) as usize
            )));
        }

        Box::new(PopularDestinations {
            per_bldg,
            draw: ctx.upload(batch),
            composite: Composite::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Most popular destinations").small_heading().draw(ctx),
                    Btn::text_fg("X")
                        .build(ctx, "close", hotkey(Key::Escape))
                        .align_right(),
                ]),
                Checkbox::switch(ctx, "Show heatmap", None, opts.is_some()),
                controls,
                breakdown.draw(ctx),
            ]))
            .aligned(HorizontalAlignment::Right, VerticalAlignment::Top)
            .build(ctx),
        })
    }
}

impl State for PopularDestinations {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        ctx.canvas_movement();
        if ctx.redo_mouseover() {
            app.primary.current_selection = app.calculate_current_selection(
                ctx,
                &DontDrawAgents {},
                &ShowEverything::new(),
                false,
                false,
                true,
            );
            if let Some(ID::Building(_)) = app.primary.current_selection {
            } else {
                app.primary.current_selection = None;
            }
        }

        match self.composite.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                _ => unreachable!(),
            },
            Outcome::Changed => {
                return Transition::Replace(PopularDestinations::make(
                    ctx,
                    app,
                    self.per_bldg.clone(),
                    if self.composite.is_checked("Show heatmap") {
                        Some(HeatmapOptions::from_controls(&self.composite))
                    } else {
                        None
                    },
                ));
            }
            _ => {}
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        g.redraw(&self.draw);
        self.composite.draw(g);

        if let Some(ID::Building(b)) = app.primary.current_selection {
            let mut txt = Text::new();
            txt.add(Line(format!(
                "{} trips to here",
                abstutil::prettyprint_usize(self.per_bldg.get(b))
            )));
            for (name, amenity) in &app.primary.map.get_b(b).amenities {
                txt.add(Line(format!("- {} ({})", name, amenity)));
            }
            g.draw_mouse_tooltip(txt);
        }
    }
}