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
//! This directory contains extra/experimental tools not directly related to A/B Street the game.
//! Eventually some might be split into separate crates.

use abstutil::Timer;
use geom::{LonLat, Percent};
use map_gui::colors::ColorSchemeChoice;
use map_gui::tools::{ChooseSomething, CityPicker};
use map_gui::AppLike;
use widgetry::{Choice, EventCtx, Key, Line, Panel, SimpleState, State, Widget};

use crate::app::{App, Transition};

mod collisions;
pub mod compare_counts;
mod destinations;
pub mod kml;
mod polygon;
mod scenario;
mod story;

pub struct DevToolsMode;

impl DevToolsMode {
    pub fn new_state(ctx: &mut EventCtx, app: &mut App) -> Box<dyn State<App>> {
        app.change_color_scheme(ctx, ColorSchemeChoice::DayMode);

        let panel = Panel::new_builder(Widget::col(vec![
            Widget::row(vec![
                Line("Advanced tools").small_heading().into_widget(ctx),
                ctx.style().btn_close_widget(ctx),
            ]),
            map_gui::tools::change_map_btn(ctx, app),
            Widget::custom_row(vec![
                ctx.style()
                    .btn_outline
                    .text("edit a polygon")
                    .hotkey(Key::E)
                    .build_def(ctx),
                ctx.style()
                    .btn_outline
                    .text("draw a polygon")
                    .hotkey(Key::P)
                    .build_def(ctx),
                ctx.style()
                    .btn_outline
                    .text("load scenario")
                    .hotkey(Key::W)
                    .build_def(ctx),
                ctx.style()
                    .btn_outline
                    .text("view KML")
                    .hotkey(Key::K)
                    .build_def(ctx),
                ctx.style()
                    .btn_outline
                    .text("story maps")
                    .hotkey(Key::S)
                    .build_def(ctx),
                if abstio::file_exists(app.primary.map.get_city_name().input_path("collisions.bin"))
                {
                    ctx.style()
                        .btn_outline
                        .text("collisions")
                        .hotkey(Key::C)
                        .build_def(ctx)
                } else {
                    Widget::nothing()
                },
            ])
            .flex_wrap(ctx, Percent::int(60)),
            Widget::row(vec![
                ctx.style()
                    .btn_solid_primary
                    .text("OpenStreetMap viewer")
                    .build_def(ctx),
                if cfg!(not(target_arch = "wasm32")) {
                    ctx.style()
                        .btn_solid_primary
                        .text("Parking mapper")
                        .build_def(ctx)
                } else {
                    Widget::nothing()
                },
                if abstio::file_exists(abstio::path_raw_map(app.primary.map.get_name())) {
                    ctx.style()
                        .btn_solid_primary
                        .text("RawMap editor")
                        .build_def(ctx)
                } else {
                    Widget::nothing()
                },
            ]),
        ]))
        .build(ctx);
        <dyn SimpleState<_>>::new_state(panel, Box::new(DevToolsMode))
    }
}

impl SimpleState<App> for DevToolsMode {
    fn on_click(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut App,
        x: &str,
        _: &mut Panel,
    ) -> Transition {
        match x {
            "close" => Transition::Pop,
            "edit a polygon" => {
                Transition::Push(ChooseSomething::new_state(
                    ctx,
                    "Choose a polygon",
                    // This directory won't exist on the web or for binary releases, only for
                    // people building from source. Also, abstio::path is abused to find the
                    // importer/ directory.
                    abstio::list_dir(abstio::path(format!(
                        "../importer/config/{}/{}",
                        app.primary.map.get_city_name().country,
                        app.primary.map.get_city_name().city
                    )))
                    .into_iter()
                    .filter(|path| path.ends_with(".poly"))
                    .map(|path| Choice::new(abstutil::basename(&path), path))
                    .collect(),
                    Box::new(|path, ctx, app| match LonLat::read_osmosis_polygon(&path) {
                        Ok(pts) => Transition::Replace(polygon::PolygonEditor::new_state(
                            ctx,
                            app,
                            abstutil::basename(path),
                            pts,
                        )),
                        Err(err) => {
                            println!("Bad polygon {}: {}", path, err);
                            Transition::Pop
                        }
                    }),
                ))
            }
            "draw a polygon" => Transition::Push(polygon::PolygonEditor::new_state(
                ctx,
                app,
                "name goes here".to_string(),
                Vec::new(),
            )),
            "load scenario" => Transition::Push(ChooseSomething::new_state(
                ctx,
                "Choose a scenario",
                Choice::strings(abstio::list_all_objects(abstio::path_all_scenarios(
                    app.primary.map.get_name(),
                ))),
                Box::new(|s, ctx, app| {
                    let scenario = abstio::read_binary(
                        abstio::path_scenario(app.primary.map.get_name(), &s),
                        &mut Timer::throwaway(),
                    );
                    Transition::Replace(scenario::ScenarioManager::new_state(scenario, ctx, app))
                }),
            )),
            "view KML" => Transition::Push(kml::ViewKML::new_state(ctx, app, None)),
            "story maps" => Transition::Push(story::StoryMapEditor::new_state(ctx, app)),
            "collisions" => Transition::Push(collisions::CollisionsViewer::new_state(ctx, app)),
            "OpenStreetMap viewer" => {
                map_gui::tools::Executable::OSMViewer.replace_process(ctx, app, vec![])
            }
            "Parking mapper" => {
                map_gui::tools::Executable::ParkingMapper.replace_process(ctx, app, vec![])
            }
            "RawMap editor" => {
                map_gui::tools::Executable::RawMapEditor.replace_process(ctx, app, vec![])
            }
            "change map" => Transition::Push(CityPicker::new_state(
                ctx,
                app,
                Box::new(|ctx, app| {
                    Transition::Multi(vec![
                        Transition::Pop,
                        Transition::Replace(DevToolsMode::new_state(ctx, app)),
                    ])
                }),
            )),
            _ => unreachable!(),
        }
    }
}