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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
use crate::app::App;
use crate::common::Warping;
use crate::game::{State, Transition};
use crate::helpers::ID;
use ezgui::{
    hotkey, Autocomplete, Btn, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, Key, Line,
    Outcome, Text, Widget,
};
use map_model::RoadID;
use std::collections::HashSet;

// TODO Canonicalize names, handling abbreviations like east/e and street/st
pub struct Navigator {
    composite: Composite,
}

impl Navigator {
    pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State> {
        Box::new(Navigator {
            composite: Composite::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Enter a street name").small_heading().draw(ctx),
                    Btn::plaintext("X")
                        .build(ctx, "close", hotkey(Key::Escape))
                        .align_right(),
                ]),
                Autocomplete::new(
                    ctx,
                    app.primary
                        .map
                        .all_roads()
                        .iter()
                        .map(|r| (r.get_name(app.opts.language.as_ref()), r.id))
                        .collect(),
                )
                .named("street"),
                Btn::text_fg("Search by business name or address").build_def(ctx, hotkey(Key::Tab)),
            ]))
            .build(ctx),
        })
    }
}

impl State for Navigator {
    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;
                }
                "Search by business name or address" => {
                    return Transition::Replace(SearchBuildings::new(ctx, app));
                }
                _ => unreachable!(),
            },
            _ => {}
        }
        if let Some(roads) = self.composite.autocomplete_done("street") {
            if roads.is_empty() {
                return Transition::Pop;
            }
            return Transition::Replace(CrossStreet::new(ctx, app, roads));
        }

        if self.composite.clicked_outside(ctx) {
            return Transition::Pop;
        }

        Transition::Keep
    }

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

struct CrossStreet {
    first: Vec<RoadID>,
    composite: Composite,
    draw: Drawable,
}

impl CrossStreet {
    fn new(ctx: &mut EventCtx, app: &App, first: Vec<RoadID>) -> Box<dyn State> {
        let map = &app.primary.map;
        let mut cross_streets = HashSet::new();
        let mut batch = GeomBatch::new();
        for r in &first {
            let road = map.get_r(*r);
            batch.push(Color::RED, road.get_thick_polygon(&app.primary.map));
            for i in &[road.src_i, road.dst_i] {
                for cross in &map.get_i(*i).roads {
                    cross_streets.insert(*cross);
                }
            }
        }
        // Roads share intersections, so of course there'll be overlap here.
        for r in &first {
            cross_streets.remove(r);
        }

        Box::new(CrossStreet {
            composite: Composite::new(Widget::col(vec![
                Widget::row(vec![
                    {
                        let mut txt = Text::from(Line("What cross street?").small_heading());
                        // TODO This isn't so clear...
                        txt.add(Line(format!(
                            "(Or just quit to go to {})",
                            map.get_r(first[0]).get_name(app.opts.language.as_ref()),
                        )));
                        txt.draw(ctx)
                    },
                    Btn::plaintext("X")
                        .build(ctx, "close", hotkey(Key::Escape))
                        .align_right(),
                ]),
                Autocomplete::new(
                    ctx,
                    cross_streets
                        .into_iter()
                        .map(|r| (map.get_r(r).get_name(app.opts.language.as_ref()), r))
                        .collect(),
                )
                .named("street"),
            ]))
            .build(ctx),
            first,
            draw: ctx.upload(batch),
        })
    }
}

impl State for CrossStreet {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        let map = &app.primary.map;

        match self.composite.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    // Just warp to somewhere on the first road
                    let road = map.get_r(self.first[0]);
                    return Transition::Replace(Warping::new(
                        ctx,
                        road.center_pts.middle(),
                        Some(app.opts.min_zoom_for_detail),
                        None,
                        &mut app.primary,
                    ));
                }
                _ => unreachable!(),
            },
            _ => {}
        }
        if let Some(roads) = self.composite.autocomplete_done("street") {
            // Find the best match
            let mut found = None;
            'OUTER: for r1 in &self.first {
                let r1 = map.get_r(*r1);
                for i in vec![r1.src_i, r1.dst_i] {
                    if map.get_i(i).roads.iter().any(|r2| roads.contains(r2)) {
                        found = Some(i);
                        break 'OUTER;
                    }
                }
            }
            if let Some(i) = found {
                return Transition::Replace(Warping::new(
                    ctx,
                    map.get_i(i).polygon.center(),
                    Some(app.opts.min_zoom_for_detail),
                    Some(ID::Intersection(i)),
                    &mut app.primary,
                ));
            } else {
                return Transition::Pop;
            }
        }

        if self.composite.clicked_outside(ctx) {
            return Transition::Pop;
        }

        Transition::Keep
    }

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

struct SearchBuildings {
    composite: Composite,
}

impl SearchBuildings {
    pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State> {
        Box::new(SearchBuildings {
            composite: Composite::new(Widget::col(vec![
                Widget::row(vec![
                    Line("Enter a business name or address")
                        .small_heading()
                        .draw(ctx),
                    Btn::plaintext("X")
                        .build(ctx, "close", hotkey(Key::Escape))
                        .align_right(),
                ]),
                Autocomplete::new(
                    ctx,
                    app.primary
                        .map
                        .all_buildings()
                        .iter()
                        .flat_map(|b| {
                            let mut results = Vec::new();
                            if !b.address.starts_with("???") {
                                results.push((b.address.clone(), b.id));
                            }
                            if let Some(ref n) = b.name {
                                results.push((n.clone(), b.id));
                            }
                            for (a, _) in &b.amenities {
                                results.push((format!("{} (at {})", a, b.address), b.id));
                            }
                            results
                        })
                        .collect(),
                )
                .named("bldg"),
                Btn::text_fg("Search for streets").build_def(ctx, hotkey(Key::Tab)),
            ]))
            .build(ctx),
        })
    }
}

impl State for SearchBuildings {
    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;
                }
                "Search for streets" => {
                    return Transition::Replace(Navigator::new(ctx, app));
                }
                _ => unreachable!(),
            },
            _ => {}
        }
        if let Some(bldgs) = self.composite.autocomplete_done("bldg") {
            if bldgs.is_empty() {
                return Transition::Pop;
            }
            let b = app.primary.map.get_b(bldgs[0]);
            return Transition::Replace(Warping::new(
                ctx,
                b.label_center,
                Some(app.opts.min_zoom_for_detail),
                Some(ID::Building(b.id)),
                &mut app.primary,
            ));
        }

        if self.composite.clicked_outside(ctx) {
            return Transition::Pop;
        }

        Transition::Keep
    }

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