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
use abstutil::Counter;
use map_gui::tools::{make_heatmap, HeatmapOptions};
use map_gui::ID;
use map_model::{AmenityType, BuildingID};
use sim::{Scenario, TripEndpoint};
use widgetry::{
    Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Line, Outcome, Panel, State,
    Text, Toggle, VerticalAlignment, Widget,
};

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

pub struct PopularDestinations {
    per_bldg: Counter<BuildingID>,
    panel: Panel,
    draw: Drawable,
}

impl PopularDestinations {
    pub fn new(ctx: &mut EventCtx, app: &App, scenario: &Scenario) -> Box<dyn State<App>> {
        let mut per_bldg = Counter::new();
        for p in &scenario.people {
            for trip in &p.trips {
                if let TripEndpoint::Bldg(b) = trip.destination {
                    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<App>> {
        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 a in &map.get_b(*b).amenities {
                if let Some(t) = AmenityType::categorize(&a.amenity_type) {
                    by_type.add(Some(t), *cnt);
                    other = false;
                }
            }
            if other {
                by_type.add(None, *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
                    .map(|x| x.to_string())
                    .unwrap_or("other".to_string()),
                ((cnt as f64) / sum * 100.0) as usize
            )));
        }

        Box::new(PopularDestinations {
            per_bldg,
            draw: ctx.upload(batch),
            panel: Panel::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Most popular destinations")
                        .small_heading()
                        .into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                Toggle::switch(ctx, "Show heatmap", None, opts.is_some()),
                controls,
                breakdown.into_widget(ctx),
            ]))
            .aligned(HorizontalAlignment::Right, VerticalAlignment::Top)
            .build(ctx),
        })
    }
}

impl State<App> 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.mouseover_unzoomed_buildings(ctx);
            if let Some(ID::Building(_)) = app.primary.current_selection {
            } else {
                app.primary.current_selection = None;
            }
        }

        match self.panel.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.panel.is_checked("Show heatmap") {
                        Some(HeatmapOptions::from_controls(&self.panel))
                    } else {
                        None
                    },
                ));
            }
            _ => {}
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        g.redraw(&self.draw);
        self.panel.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 a in &app.primary.map.get_b(b).amenities {
                txt.add(Line(format!(
                    "  {} ({})",
                    a.names.get(app.opts.language.as_ref()),
                    a.amenity_type
                )));
            }
            g.draw_mouse_tooltip(txt);
        }
    }
}