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
use map_gui::tools::percentage_bar;
use map_model::{PathRequest, NORMAL_LANE_THICKNESS};
use widgetry::mapspace::ToggleZoomed;
use widgetry::{EventCtx, GfxCtx, Key, Line, Outcome, Panel, State, Text, TextExt, Widget};

use crate::edit::{EditNeighborhood, Tab};
use crate::shortcuts::{find_shortcuts, Shortcuts};
use crate::{colors, App, Neighborhood, NeighborhoodID, Transition};

pub struct BrowseShortcuts {
    top_panel: Panel,
    left_panel: Panel,
    shortcuts: Shortcuts,
    current_idx: usize,

    draw_path: ToggleZoomed,
    edit: EditNeighborhood,
    neighborhood: Neighborhood,
}

impl BrowseShortcuts {
    pub fn new_state(
        ctx: &mut EventCtx,
        app: &App,
        id: NeighborhoodID,
        start_with_request: Option<PathRequest>,
    ) -> Box<dyn State<App>> {
        let neighborhood = Neighborhood::new(ctx, app, id);

        let shortcuts = ctx.loading_screen("find shortcuts", |_, timer| {
            find_shortcuts(app, &neighborhood, timer)
        });
        let edit = EditNeighborhood::new(ctx, app, &neighborhood, &shortcuts);

        let mut state = BrowseShortcuts {
            top_panel: crate::components::TopPanel::panel(ctx, app),
            left_panel: Panel::empty(ctx),
            shortcuts,
            current_idx: 0,
            draw_path: ToggleZoomed::empty(ctx),
            neighborhood,
            edit,
        };

        if let Some(req) = start_with_request {
            if let Some(idx) = state
                .shortcuts
                .paths
                .iter()
                .position(|path| path.get_req() == &req)
            {
                state.current_idx = idx;
            }
        }

        state.recalculate(ctx, app);

        Box::new(state)
    }

    fn recalculate(&mut self, ctx: &mut EventCtx, app: &App) {
        let (quiet_streets, total_streets) =
            self.shortcuts.quiet_and_total_streets(&self.neighborhood);

        if self.shortcuts.paths.is_empty() {
            self.left_panel = self
                .edit
                .panel_builder(
                    ctx,
                    app,
                    Tab::Shortcuts,
                    &self.top_panel,
                    percentage_bar(
                        ctx,
                        Text::from(Line(format!(
                            "{} / {} streets have no shortcuts",
                            quiet_streets, total_streets
                        ))),
                        1.0,
                    ),
                )
                .build(ctx);
            return;
        }

        // Optimization to avoid recalculating the whole panel
        if self.left_panel.has_widget("prev/next controls") {
            let controls = self.prev_next_controls(ctx);
            self.left_panel.replace(ctx, "prev/next controls", controls);
        } else {
            self.left_panel = self
                .edit
                .panel_builder(
                    ctx,
                    app,
                    Tab::Shortcuts,
                    &self.top_panel,
                    Widget::col(vec![
                        percentage_bar(
                            ctx,
                            Text::from(Line(format!(
                                "{} / {} streets have no shortcuts",
                                quiet_streets, total_streets
                            ))),
                            (quiet_streets as f64) / (total_streets as f64),
                        ),
                        "Browse possible shortcuts through the neighborhood.".text_widget(ctx),
                        self.prev_next_controls(ctx),
                    ]),
                )
                .build(ctx);
        }

        let mut draw_path = ToggleZoomed::builder();
        if let Some(pl) = self.shortcuts.paths[self.current_idx].trace(&app.map) {
            let color = colors::SHORTCUT_PATH;
            let shape = pl.make_polygons(3.0 * NORMAL_LANE_THICKNESS);
            draw_path.unzoomed.push(color.alpha(0.8), shape.clone());
            draw_path.zoomed.push(color.alpha(0.5), shape);

            draw_path
                .unzoomed
                .append(map_gui::tools::start_marker(ctx, pl.first_pt(), 2.0));
            draw_path
                .zoomed
                .append(map_gui::tools::start_marker(ctx, pl.first_pt(), 0.5));

            draw_path
                .unzoomed
                .append(map_gui::tools::goal_marker(ctx, pl.last_pt(), 2.0));
            draw_path
                .zoomed
                .append(map_gui::tools::goal_marker(ctx, pl.last_pt(), 0.5));
        }
        self.draw_path = draw_path.build(ctx);
    }

    fn prev_next_controls(&self, ctx: &EventCtx) -> Widget {
        Widget::row(vec![
            ctx.style()
                .btn_prev()
                .disabled(self.current_idx == 0)
                .hotkey(Key::LeftArrow)
                .build_widget(ctx, "previous shortcut"),
            Text::from(
                Line(format!(
                    "{}/{}",
                    self.current_idx + 1,
                    self.shortcuts.paths.len()
                ))
                .secondary(),
            )
            .into_widget(ctx)
            .centered_vert(),
            ctx.style()
                .btn_next()
                .disabled(self.current_idx == self.shortcuts.paths.len() - 1)
                .hotkey(Key::RightArrow)
                .build_widget(ctx, "next shortcut"),
        ])
        .named("prev/next controls")
    }
}

impl State<App> for BrowseShortcuts {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition {
        if let Some(t) = crate::components::TopPanel::event(ctx, app, &mut self.top_panel, help) {
            return t;
        }
        match self.left_panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "previous shortcut" => {
                    self.current_idx -= 1;
                    self.recalculate(ctx, app);
                }
                "next shortcut" => {
                    self.current_idx += 1;
                    self.recalculate(ctx, app);
                }
                x => {
                    if let Some(t) = self.edit.handle_panel_action(
                        ctx,
                        app,
                        x,
                        &self.neighborhood,
                        &self.left_panel,
                    ) {
                        return t;
                    }
                    let current_request = if self.shortcuts.paths.is_empty() {
                        None
                    } else {
                        Some(self.shortcuts.paths[self.current_idx].get_req().clone())
                    };
                    return crate::save::AltProposals::handle_action(
                        ctx,
                        app,
                        crate::save::PreserveState::Shortcuts(
                            current_request,
                            app.session
                                .partitioning
                                .all_blocks_in_neighborhood(self.neighborhood.id),
                        ),
                        x,
                    )
                    .unwrap();
                }
            },
            _ => {}
        }

        if self.edit.event(ctx, app) {
            // Reset state, but if possible, preserve the current individual shortcut.
            let current_request = self.shortcuts.paths[self.current_idx].get_req().clone();
            return Transition::Replace(BrowseShortcuts::new_state(
                ctx,
                app,
                self.neighborhood.id,
                Some(current_request),
            ));
        }

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        self.top_panel.draw(g);
        self.left_panel.draw(g);

        self.edit.world.draw(g);
        self.draw_path.draw(g);

        g.redraw(&self.neighborhood.fade_irrelevant);
        app.session.draw_all_filters.draw(g);
        if g.canvas.is_unzoomed() {
            self.neighborhood.labels.draw(g, app);
        }
    }

    fn recreate(&mut self, ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
        let current_request = if self.shortcuts.paths.is_empty() {
            None
        } else {
            Some(self.shortcuts.paths[self.current_idx].get_req().clone())
        };
        Self::new_state(ctx, app, self.neighborhood.id, current_request)
    }
}

fn help() -> Vec<&'static str> {
    vec![
        "This shows every possible path a driver could take through the neighborhood.",
        "Not all paths may be realistic -- small service roads and alleyways are possible, but unlikely.",
    ]
}