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
#![allow(clippy::type_complexity)]

use structopt::StructOpt;

use abstio::MapName;
use abstutil::Timer;
use widgetry::{EventCtx, GfxCtx, Settings};

pub use browse::BrowseNeighbourhoods;
use filters::Toggle3Zoomed;
pub use filters::{DiagonalFilter, ModalFilters};
pub use neighbourhood::{Cell, DistanceInterval, Neighbourhood};
pub use partition::{NeighbourhoodID, Partitioning};

#[macro_use]
extern crate anyhow;
#[macro_use]
extern crate log;

mod browse;
mod colors;
mod components;
mod connectivity;
mod customize_boundary;
mod draw_cells;
mod edit;
mod export;
mod filters;
mod impact;
mod neighbourhood;
mod partition;
mod route_planner;
mod save;
mod select_boundary;
mod shortcut_viewer;
mod shortcuts;

type App = map_gui::SimpleApp<Session>;
type Transition = widgetry::Transition<App>;

pub fn main() {
    let settings = Settings::new("Low traffic neighbourhoods");
    run(settings);
}

#[derive(StructOpt)]
struct Args {
    /// Load a previously saved proposal with this name. Note this takes a name, not a full path.
    #[structopt(long)]
    proposal: Option<String>,
    /// Lock the user into one fixed neighbourhood, and remove many controls
    #[structopt(long)]
    consultation: Option<String>,
    #[structopt(flatten)]
    app_args: map_gui::SimpleAppArgs,
}

fn run(mut settings: Settings) {
    let mut opts = map_gui::options::Options::load_or_default();
    opts.color_scheme = map_gui::colors::ColorSchemeChoice::LTN;
    opts.show_building_driveways = false;
    // TODO Ideally we would have a better map model in the first place. The next best thing would
    // be to change these settings based on the map's country, but that's a bit tricky to do early
    // enough (before map_switched). So for now, assume primary use of this tool is in the UK,
    // where these settings are most appropriate.
    opts.show_stop_signs = false;
    opts.show_crosswalks = false;
    opts.show_traffic_signal_icon = true;
    opts.simplify_basemap = true;

    let args = Args::from_iter(abstutil::cli_args());
    args.app_args.override_options(&mut opts);

    settings = args
        .app_args
        .update_widgetry_settings(settings)
        .canvas_settings(opts.canvas_settings.clone());
    widgetry::run(settings, move |ctx| {
        let session = Session {
            proposal_name: None,
            partitioning: Partitioning::empty(),
            modal_filters: ModalFilters::default(),

            alt_proposals: save::AltProposals::new(),
            draw_all_filters: Toggle3Zoomed::empty(ctx),
            impact: impact::Impact::empty(ctx),

            edit_filters: true,

            draw_neighbourhood_style: browse::Style::Simple,
            draw_cells_as_areas: false,
            heuristic: filters::auto::Heuristic::SplitCells,
            main_road_penalty: 1.0,

            current_trip_name: None,

            consultation: None,
            consultation_proposal_path: None,
        };
        map_gui::SimpleApp::new(
            ctx,
            opts,
            args.app_args.map_name(),
            args.app_args.cam,
            session,
            move |ctx, app| {
                // Restore the partitioning from a file before calling BrowseNeighbourhoods
                let popup_state = args.proposal.as_ref().and_then(|name| {
                    crate::save::Proposal::load(
                        ctx,
                        app,
                        abstio::path_ltn_proposals(app.map.get_name(), name),
                    )
                });

                let mut states = Vec::new();
                if let Some(ref consultation) = args.consultation {
                    if app.map.get_name() != &MapName::new("gb", "bristol", "east") {
                        panic!("Consultation mode not supported on this map");
                    }

                    let focus_on_street = match consultation.as_ref() {
                        "pt1" => "Gregory Street",
                        "pt2" => {
                            // Start from a baked-in proposal with special boundaries
                            app.session.consultation_proposal_path = Some(abstio::path(
                                "system/ltn_proposals/bristol_beaufort_road.json.gz",
                            ));
                            "Jubilee Road"
                        }
                        _ => panic!("Unknown Bristol consultation mode {consultation}"),
                    };

                    app.session.alt_proposals = crate::save::AltProposals::new();
                    ctx.loading_screen("initialize", |ctx, timer| {
                        crate::clear_current_proposal(ctx, app, timer);
                    });

                    // Look for the neighbourhood containing one small street
                    let r = app
                        .map
                        .all_roads()
                        .iter()
                        .find(|r| r.get_name(None) == focus_on_street)
                        .expect(&format!("Can't find {focus_on_street}"))
                        .id;
                    let (neighbourhood, _) = app
                        .session
                        .partitioning
                        .all_neighbourhoods()
                        .iter()
                        .find(|(_, info)| info.block.perimeter.interior.contains(&r))
                        .expect(&format!(
                            "Can't find neighbourhood containing {focus_on_street}"
                        ));
                    app.session.consultation = Some(*neighbourhood);

                    // TODO Maybe center the camera, ignoring any saved values

                    states.push(connectivity::Viewer::new_state(
                        ctx,
                        app,
                        app.session.consultation.unwrap(),
                    ));
                } else {
                    states.push(map_gui::tools::TitleScreen::new_state(
                        ctx,
                        app,
                        map_gui::tools::Executable::LTN,
                        Box::new(|ctx, app, _| BrowseNeighbourhoods::new_state(ctx, app)),
                    ));
                    states.push(BrowseNeighbourhoods::new_state(ctx, app));
                }
                if let Some(state) = popup_state {
                    states.push(state);
                }
                states
            },
        )
    });
}

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen(js_name = "run")]
pub fn run_wasm(root_dom_id: String, assets_base_url: String, assets_are_gzipped: bool) {
    let settings = Settings::new("Low traffic neighbourhoods")
        .root_dom_element_id(root_dom_id)
        .assets_base_url(assets_base_url)
        .assets_are_gzipped(assets_are_gzipped);

    run(settings);
}

// TODO Tension: Many of these are per-map. game::App nicely wraps these up. Time to stop abusing
// SimpleApp?
pub struct Session {
    // These come from a save::Proposal
    pub proposal_name: Option<String>,
    pub partitioning: Partitioning,
    pub modal_filters: ModalFilters,

    pub alt_proposals: save::AltProposals,
    pub draw_all_filters: Toggle3Zoomed,
    pub impact: impact::Impact,

    // True if we're editing filters, false if we're editing one-ways. (An enum is overkill)
    pub edit_filters: bool,

    // Remember form settings in different tabs.
    // Browse neighbourhoods:
    pub draw_neighbourhood_style: browse::Style,
    // Connectivity:
    pub draw_cells_as_areas: bool,
    pub heuristic: filters::auto::Heuristic,
    // Pathfinding
    pub main_road_penalty: f64,

    current_trip_name: Option<String>,

    consultation: Option<NeighbourhoodID>,
    // The current consultation should always be based off a built-in proposal
    consultation_proposal_path: Option<String>,
}

/// Do the equivalent of `SimpleApp::draw_unzoomed` or `draw_zoomed`, but after the water/park
/// areas layer, draw something custom.
fn draw_with_layering<F: Fn(&mut GfxCtx)>(g: &mut GfxCtx, app: &App, custom: F) {
    g.clear(app.cs.void_background);
    g.redraw(&app.draw_map.boundary_polygon);
    g.redraw(&app.draw_map.draw_all_areas);
    custom(g);

    if g.canvas.is_unzoomed() {
        g.redraw(&app.draw_map.draw_all_unzoomed_parking_lots);
        g.redraw(&app.draw_map.draw_all_unzoomed_roads_and_intersections);
        g.redraw(&app.draw_map.draw_all_buildings);
        g.redraw(&app.draw_map.draw_all_building_outlines);
    } else {
        let options = map_gui::render::DrawOptions::new();
        let objects = app
            .draw_map
            .get_renderables_back_to_front(g.get_screen_bounds(), &app.map);

        let mut drawn_all_buildings = false;

        for obj in objects {
            obj.draw(g, app, &options);

            if matches!(obj.get_id(), map_gui::ID::Building(_)) && !drawn_all_buildings {
                g.redraw(&app.draw_map.draw_all_buildings);
                g.redraw(&app.draw_map.draw_all_building_outlines);
                drawn_all_buildings = true;
            }
        }
    }
}

pub fn after_edit(ctx: &EventCtx, app: &mut App) {
    app.session.draw_all_filters = app.session.modal_filters.draw(ctx, &app.map);
}

pub fn clear_current_proposal(ctx: &mut EventCtx, app: &mut App, timer: &mut Timer) {
    if let Some(path) = app.session.consultation_proposal_path.clone() {
        if crate::save::Proposal::load(ctx, app, path.clone()).is_some() {
            panic!("Consultation mode broken; go fix {path} manually");
        }
        return;
    }

    app.session.proposal_name = None;
    // Reset this first. transform_existing_filters will fill some out.
    app.session.modal_filters = ModalFilters::default();
    crate::filters::transform_existing_filters(ctx, app, timer);
    app.session.partitioning = Partitioning::seed_using_heuristics(app, timer);
    app.session.draw_all_filters = app.session.modal_filters.draw(ctx, &app.map);
}