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
280
281
282
283
284
285
286
287
288
289
use std::collections::HashSet;

use map_model::RoadID;
use widgetry::{
    Autocomplete, Color, Drawable, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, Panel, State,
    Text, Transition, Widget,
};

use crate::tools::grey_out_map;
use crate::{AppLike, ID};

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

impl Navigator {
    pub fn new_state<A: AppLike + 'static>(ctx: &mut EventCtx, app: &A) -> Box<dyn State<A>> {
        Box::new(Navigator {
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Line("Enter a street name").small_heading().into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                Autocomplete::new_widget(
                    ctx,
                    app.map()
                        .all_roads()
                        .iter()
                        .map(|r| (r.get_name(app.opts().language.as_ref()), r.id))
                        .collect(),
                )
                .named("street"),
                ctx.style()
                    .btn_outline
                    .text("Search by business name or address")
                    .hotkey(Key::Tab)
                    .build_def(ctx),
            ]))
            .build(ctx),
        })
    }
}

impl<A: AppLike + 'static> State<A> for Navigator {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "Search by business name or address" => {
                    return Transition::Replace(SearchBuildings::new_state(ctx, app));
                }
                _ => unreachable!(),
            }
        }
        if let Some(roads) = self.panel.autocomplete_done("street") {
            if roads.is_empty() {
                return Transition::Pop;
            }
            return Transition::Replace(CrossStreet::new_state(ctx, app, roads));
        }

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

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &A) {
        grey_out_map(g, app);
        self.panel.draw(g);
    }
}

struct CrossStreet {
    first: Vec<RoadID>,
    panel: Panel,
    draw: Drawable,
}

impl CrossStreet {
    fn new_state<A: AppLike + 'static>(
        ctx: &mut EventCtx,
        app: &A,
        first: Vec<RoadID>,
    ) -> Box<dyn State<A>> {
        let map = app.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(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 {
            panel: Panel::new_builder(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.into_widget(ctx)
                    },
                    ctx.style().btn_close_widget(ctx),
                ]),
                Autocomplete::new_widget(
                    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<A: AppLike + 'static> State<A> for CrossStreet {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
        let map = app.map();

        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "close" => {
                    // Just warp to somewhere on the first road
                    let pt = map.get_r(self.first[0]).center_pts.middle();
                    return Transition::Replace(app.make_warper(
                        ctx,
                        pt,
                        Some(app.opts().min_zoom_for_detail),
                        None,
                    ));
                }
                _ => unreachable!(),
            }
        }
        if let Some(roads) = self.panel.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 [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 {
                let pt = map.get_i(i).polygon.center();
                return Transition::Replace(app.make_warper(
                    ctx,
                    pt,
                    Some(app.opts().min_zoom_for_detail),
                    Some(ID::Intersection(i)),
                ));
            } else {
                return Transition::Pop;
            }
        }

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

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &A) {
        g.redraw(&self.draw);
        grey_out_map(g, app);
        self.panel.draw(g);
    }
}

struct SearchBuildings {
    panel: Panel,
}

impl SearchBuildings {
    pub fn new_state<A: AppLike + 'static>(ctx: &mut EventCtx, app: &A) -> Box<dyn State<A>> {
        Box::new(SearchBuildings {
            panel: Panel::new_builder(Widget::col(vec![
                Widget::row(vec![
                    Line("Enter a business name or address")
                        .small_heading()
                        .into_widget(ctx),
                    ctx.style().btn_close_widget(ctx),
                ]),
                Autocomplete::new_widget(
                    ctx,
                    app.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 names) = b.name {
                                results.push((
                                    names.get(app.opts().language.as_ref()).to_string(),
                                    b.id,
                                ));
                            }
                            for a in &b.amenities {
                                results.push((
                                    format!(
                                        "{} (at {})",
                                        a.names.get(app.opts().language.as_ref()),
                                        b.address
                                    ),
                                    b.id,
                                ));
                            }
                            results
                        })
                        .collect(),
                )
                .named("bldg"),
                ctx.style()
                    .btn_outline
                    .text("Search for streets")
                    .hotkey(Key::Tab)
                    .build_def(ctx),
            ]))
            .build(ctx),
        })
    }
}

impl<A: AppLike + 'static> State<A> for SearchBuildings {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> Transition<A> {
        if let Outcome::Clicked(x) = self.panel.event(ctx) {
            match x.as_ref() {
                "close" => {
                    return Transition::Pop;
                }
                "Search for streets" => {
                    return Transition::Replace(Navigator::new_state(ctx, app));
                }
                _ => unreachable!(),
            }
        }
        if let Some(bldgs) = self.panel.autocomplete_done("bldg") {
            if bldgs.is_empty() {
                return Transition::Pop;
            }
            let b = app.map().get_b(bldgs[0]);
            let pt = b.label_center;
            return Transition::Replace(app.make_warper(
                ctx,
                pt,
                Some(app.opts().min_zoom_for_detail),
                Some(ID::Building(bldgs[0])),
            ));
        }

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

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &A) {
        grey_out_map(g, app);
        self.panel.draw(g);
    }
}