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
// A state to count the number of trips that will cross different roads.

use abstutil::Counter;
use map_model::{IntersectionID, PathStep, RoadID, Traversable};
use widgetry::{
    Btn, Color, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Text,
    VerticalAlignment, Widget,
};

use crate::app::App;
use crate::common::{ColorLegend, ColorNetwork, CommonState};
use crate::game::{State, Transition};
use crate::helpers::ID;

pub struct PathCounter {
    panel: Panel,
    unzoomed: Drawable,
    zoomed: Drawable,
    cnt: Counter<RoadID>,
    tooltip: Option<Text>,
}

impl PathCounter {
    pub fn demand_across_intersection(
        ctx: &mut EventCtx,
        app: &App,
        i: IntersectionID,
    ) -> Box<dyn State> {
        let map = &app.primary.map;
        let sim = &app.primary.sim;
        let mut cnt = Counter::new();
        // Find all active agents whose path crosses this intersection
        for agent in sim.active_agents() {
            if let Some(path) = sim.get_path(agent) {
                if path.get_steps().iter().any(|step| match step {
                    PathStep::Turn(t) => t.parent == i,
                    _ => false,
                }) {
                    // Count what lanes they'll cross
                    for step in path.get_steps() {
                        if let Traversable::Lane(l) = step.as_traversable() {
                            cnt.inc(map.get_l(l).parent);
                        }
                    }
                }
            }
        }

        let mut colorer = ColorNetwork::new(app);
        // Highlight the intersection
        colorer
            .unzoomed
            .push(Color::CYAN, map.get_i(i).polygon.clone());
        colorer
            .zoomed
            .push(Color::CYAN.alpha(0.5), map.get_i(i).polygon.clone());

        colorer.pct_roads(cnt.clone(), &app.cs.good_to_bad_red);
        let (unzoomed, zoomed) = colorer.build(ctx);

        Box::new(PathCounter {
            unzoomed,
            zoomed,
            tooltip: None,
            cnt,
            panel: Panel::new(Widget::col(vec![
                Widget::row(vec![
                    Line(format!("Paths across {}", i))
                        .small_heading()
                        .draw(ctx),
                    Btn::text_fg("X")
                        .build(ctx, "close", Key::Escape)
                        .align_right(),
                ]),
                ColorLegend::gradient(
                    ctx,
                    &app.cs.good_to_bad_red,
                    vec!["lowest count", "highest"],
                ),
            ]))
            .aligned(HorizontalAlignment::Center, VerticalAlignment::Top)
            .build(ctx),
        })
    }
}

impl State for PathCounter {
    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_roads_and_intersections(ctx);
            self.tooltip = None;
            if let Some(r) = match app.primary.current_selection {
                Some(ID::Lane(l)) => Some(app.primary.map.get_l(l).parent),
                Some(ID::Road(r)) => Some(r),
                _ => None,
            } {
                let n = self.cnt.get(r);
                if n > 0 {
                    self.tooltip = Some(Text::from(Line(abstutil::prettyprint_usize(n))));
                }
            } else {
                app.primary.current_selection = None;
            }
        }

        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                _ => unreachable!(),
            },
            _ => {}
        }

        Transition::Keep
    }

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

        if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
            g.redraw(&self.unzoomed);
        } else {
            g.redraw(&self.zoomed);
        }

        if let Some(ref txt) = self.tooltip {
            g.draw_mouse_tooltip(txt.clone());
        }
    }
}