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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
use crate::app::App;
use crate::game::{DrawBaselayer, State, Transition};
use crate::helpers::nice_map_name;
use crate::render::DrawArea;
use ezgui::{
    hotkey, Btn, Color, Composite, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, ScreenPt, Text,
    Widget,
};
use geom::{Distance, Polygon, Pt2D};
use map_model::City;

pub struct CityPicker {
    composite: Composite,
    // In untranslated screen-space
    regions: Vec<(String, Color, Polygon)>,
    selected: Option<usize>,
    on_load: Box<dyn Fn(&mut EventCtx, &mut App) -> Transition>,
}

impl CityPicker {
    pub fn new(
        ctx: &mut EventCtx,
        app: &mut App,
        on_load: Box<dyn Fn(&mut EventCtx, &mut App) -> Transition>,
    ) -> Box<dyn State> {
        app.primary.current_selection = None;

        let mut batch = GeomBatch::new();
        let mut regions = Vec::new();

        if let Ok(city) = abstutil::maybe_read_binary::<City>(
            abstutil::path(format!(
                "system/cities/{}.bin",
                app.primary.map.get_city_name()
            )),
            &mut abstutil::Timer::throwaway(),
        ) {
            let bounds = city.boundary.get_bounds();

            let zoom = (0.8 * ctx.canvas.window_width / bounds.width())
                .min(0.8 * ctx.canvas.window_height / bounds.height());

            batch.push(app.cs.map_background, city.boundary);
            for (area_type, polygon) in city.areas {
                batch.push(DrawArea::color(area_type, &app.cs), polygon);
            }

            for (name, polygon) in city.regions {
                // For example, the huge_seattle map isn't bundled in releases.
                if !abstutil::file_exists(abstutil::path_map(&name)) {
                    continue;
                }
                let color = app.cs.rotating_color_agents(regions.len());
                if &name == app.primary.map.get_name() {
                    batch.push(color.alpha(0.5), polygon.clone());
                } else {
                    batch.push(color, polygon.to_outline(Distance::meters(200.0)).unwrap());
                }
                regions.push((name, color, polygon.scale(zoom)));
            }
            batch = batch.scale(zoom);
        }

        let mut other_cities = vec![Line("Other cities").draw(ctx)];
        let mut this_city = vec![];
        for name in abstutil::list_all_objects(abstutil::path_all_maps()) {
            if let Some((_, color, _)) = regions.iter().find(|(n, _, _)| &name == n) {
                let btn = Btn::txt(&name, Text::from(Line(nice_map_name(&name)).fg(*color)))
                    .tooltip(Text::new());
                this_city.push(if &name == app.primary.map.get_name() {
                    btn.inactive(ctx)
                } else {
                    btn.build_def(ctx, None)
                });
            } else {
                other_cities.push(
                    Btn::txt(&name, Text::from(Line(nice_map_name(&name))))
                        .tooltip(Text::new())
                        .build_def(ctx, None),
                );
            }
        }

        Box::new(CityPicker {
            regions,
            selected: None,
            on_load,
            composite: Composite::new(
                Widget::col(vec![
                    Widget::row(vec![
                        Line("Select a region").small_heading().draw(ctx),
                        Btn::plaintext("X")
                            .build(ctx, "close", hotkey(Key::Escape))
                            .align_right(),
                    ]),
                    Widget::row(vec![
                        Widget::col(other_cities).centered_vert(),
                        Widget::draw_batch(ctx, batch).named("picker"),
                        Widget::col(this_city).centered_vert(),
                    ]),
                ])
                .outline(2.0, Color::WHITE),
            )
            .build(ctx),
        })
    }
}

impl State for CityPicker {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        match self.composite.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                name => {
                    return ctx.loading_screen("switch map", |ctx, _| {
                        app.switch_map(ctx, abstutil::path_map(name));
                        (self.on_load)(ctx, app)
                    });
                }
            },
            _ => {}
        }

        if ctx.redo_mouseover() {
            self.selected = None;
            if let Some(cursor) = ctx.canvas.get_cursor_in_screen_space() {
                let rect = self.composite.rect_of("picker");
                if rect.contains(cursor) {
                    let pt = Pt2D::new(cursor.x - rect.x1, cursor.y - rect.y1);
                    for (idx, (name, _, poly)) in self.regions.iter().enumerate() {
                        if name != app.primary.map.get_name() && poly.contains_pt(pt) {
                            self.selected = Some(idx);
                            break;
                        }
                    }
                } else if let Some(btn) = self.composite.currently_hovering() {
                    for (idx, (name, _, _)) in self.regions.iter().enumerate() {
                        if name != app.primary.map.get_name() && name == btn {
                            self.selected = Some(idx);
                            break;
                        }
                    }
                }
            }
        }
        if let Some(idx) = self.selected {
            let name = &self.regions[idx].0;
            if app
                .per_obj
                .left_click(ctx, format!("switch to {}", nice_map_name(name)))
            {
                return ctx.loading_screen("switch map", |ctx, _| {
                    app.switch_map(ctx, abstutil::path_map(name));
                    (self.on_load)(ctx, app)
                });
            }
        }

        Transition::Keep
    }

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

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

        if let Some(idx) = self.selected {
            let (name, color, poly) = &self.regions[idx];
            let rect = self.composite.rect_of("picker");
            g.fork(
                Pt2D::new(0.0, 0.0),
                ScreenPt::new(rect.x1, rect.y1),
                1.0,
                None,
            );
            g.draw_polygon(color.alpha(0.5), poly.clone());
            g.unfork();

            g.draw_mouse_tooltip(Text::from(Line(nice_map_name(name))));
        }
    }
}