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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use widgetry::tools::{open_browser, PopupMsg, URLManager};
use widgetry::{
    EventCtx, Image, Key, Line, Panel, RewriteColor, SimpleState, State, Transition, Widget,
};

use crate::AppLike;

/// A title screen shared among all of the A/B Street apps.
pub struct TitleScreen<A: AppLike + 'static> {
    current_exe: Executable,
    enter_state: Box<dyn Fn(&mut EventCtx, &mut A, Vec<&str>) -> Box<dyn State<A>>>,
}

#[derive(Clone, Copy, PartialEq)]
pub enum Executable {
    ABStreet,
    FifteenMin,
    OSMViewer,
    ParkingMapper,
    Santa,
    RawMapEditor,
    LTN,
}

impl<A: AppLike + 'static> TitleScreen<A> {
    pub fn new_state(
        ctx: &mut EventCtx,
        app: &A,
        current_exe: Executable,
        enter_state: Box<dyn Fn(&mut EventCtx, &mut A, Vec<&str>) -> Box<dyn State<A>>>,
    ) -> Box<dyn State<A>> {
        let panel = Panel::new_builder(Widget::col(vec![
            Image::from_path("system/assets/pregame/logo.svg")
                .untinted()
                .dims(150.0)
                .into_widget(ctx),
            Widget::row(vec![
                Widget::col(vec![
                    Line("Games").small_heading().into_widget(ctx),
                    Widget::row(vec![
                        Image::from_path("system/assets/pregame/tutorial.svg")
                            .untinted()
                            .dims(100.0)
                            .into_widget(ctx),
                        ctx.style()
                            .btn_outline
                            .text("Traffic simulation tutorial")
                            .hotkey(Key::T)
                            .tooltip("Learn the basic controls")
                            .build_def(ctx)
                            .centered_vert(),
                    ]),
                    Widget::row(vec![
                        Image::from_path("system/assets/pregame/challenges.svg")
                            .untinted()
                            .dims(100.0)
                            .into_widget(ctx),
                        ctx.style()
                            .btn_outline
                            .text("Traffic simulation challenges")
                            .tooltip("Complete specific objectives in the traffic simulator")
                            .build_def(ctx)
                            .centered_vert(),
                    ]),
                    Widget::row(vec![
                        Image::from_path("system/assets/santa/bike1.svg")
                            .untinted()
                            .dims(100.0)
                            .into_widget(ctx),
                        ctx.style()
                            .btn_outline
                            .text("15-minute Santa")
                            .tooltip("Deliver presents as efficiently as possible")
                            .build_def(ctx)
                            .centered_vert(),
                    ]),
                ])
                .section(ctx),
                Widget::col(vec![
                    Line("Planning").small_heading().into_widget(ctx),
                    Widget::row(vec![
                        Image::from_path("system/assets/pregame/sandbox.svg")
                            .untinted()
                            .dims(100.0)
                            .into_widget(ctx),
                        ctx.style()
                            .btn_outline
                            .text("Traffic simulation sandbox")
                            .hotkey(Key::S)
                            .tooltip("Simulate traffic, edit streets, measure effects")
                            .build_def(ctx)
                            .centered_vert(),
                    ]),
                    Widget::row(vec![
                        Image::from_path("system/assets/edit/bike.svg")
                            .color(RewriteColor::ChangeAll(app.cs().bike_trip))
                            .dims(100.0)
                            .into_widget(ctx),
                        ctx.style()
                            .btn_outline
                            .text("Ungap the Map")
                            .tooltip("Improve a city's bike network")
                            .build_def(ctx)
                            .centered_vert(),
                    ]),
                    ctx.style()
                        .btn_outline
                        .text("15-minute neighborhoods")
                        .tooltip("Explore what places residents can easily reach")
                        .build_def(ctx),
                    ctx.style()
                        .btn_outline
                        .text("Low traffic neighborhoods")
                        .tooltip("Reduce vehicle shortcuts through residential streets")
                        .build_def(ctx),
                    ctx.style()
                        .btn_outline
                        .text("ActDev")
                        .tooltip("Explore mobility patterns around new residential development")
                        .build_def(ctx),
                ])
                .section(ctx),
                Widget::col(vec![
                    Line("Other").small_heading().into_widget(ctx),
                    ctx.style()
                        .btn_outline
                        .text("Community proposals")
                        .tooltip("Try out proposals for changing different cities")
                        .build_def(ctx),
                    ctx.style()
                        .btn_outline
                        .text("Advanced tools")
                        .build_def(ctx),
                    ctx.style().btn_outline.text("About").build_def(ctx),
                ])
                .section(ctx),
            ]),
            Widget::col(vec![
                ctx.style()
                    .btn_outline
                    .text("Created by Dustin Carlino, Yuwen Li, & Michael Kirk")
                    .build_widget(ctx, "Credits"),
                built_info::maybe_update(ctx),
            ])
            .centered_horiz()
            .align_bottom(),
        ]))
        .build(ctx);
        <dyn SimpleState<_>>::new_state(
            panel,
            Box::new(TitleScreen {
                current_exe,
                enter_state,
            }),
        )
    }

    fn run(
        &self,
        ctx: &mut EventCtx,
        app: &mut A,
        exe: Executable,
        args: Vec<&str>,
    ) -> Transition<A> {
        if exe == self.current_exe {
            Transition::Push((self.enter_state)(ctx, app, args))
        } else {
            exe.replace_process(ctx, app, args);
            // On most platforms, this is unreachable. But on Windows, just keep the current app
            // open.
            Transition::Keep
        }
    }
}

impl Executable {
    /// Run the given executable with some arguments. On Mac and Linux, this replaces the current
    /// process. On Windows, this launches a new child process and leaves the current alone. On
    /// web, this makes the browser go to a new page.
    pub fn replace_process<A: AppLike + 'static>(
        self,
        ctx: &mut EventCtx,
        app: &A,
        args: Vec<&str>,
    ) -> Transition<A> {
        let mut args: Vec<String> = args.into_iter().map(|a| a.to_string()).collect();
        // Usually pass in the current map's path
        match self {
            Executable::Santa => {}
            Executable::RawMapEditor => {
                args.push(abstio::path_raw_map(app.map().get_name()));
                args.push(format!(
                    "--cam={}",
                    URLManager::get_cam_param(ctx, app.map().get_gps_bounds())
                ));
            }
            _ => {
                args.push(app.map().get_name().path());
            }
        }

        // On native, end the current process and start another.
        #[cfg(not(target_arch = "wasm32"))]
        {
            use std::process::Command;

            // TODO find_exe panics; should return error instead
            let binary = crate::tools::find_exe(match self {
                Executable::ABStreet => "game",
                Executable::FifteenMin => "fifteen_min",
                Executable::OSMViewer => "osm_viewer",
                Executable::ParkingMapper => "parking_mapper",
                Executable::Santa => "santa",
                Executable::RawMapEditor => "map_editor",
                Executable::LTN => "ltn",
            });

            // We can only replace the current process on Linux/Mac
            #[cfg(not(windows))]
            {
                use std::os::unix::process::CommandExt;
                let err = Command::new(binary).args(args).exec();
                // We only get here if something broke
                Transition::Push(PopupMsg::new_state(ctx, "Error", vec![err.to_string()]))
            }

            // On Windows, all we can do is open a new child process. Not sure how to end the
            // current or detach.
            #[cfg(windows)]
            {
                abstutil::must_run_cmd(Command::new(binary).args(args));
                Transition::Keep
            }
        }

        // On web, leave the current page and go to another.
        #[cfg(target_arch = "wasm32")]
        {
            fn set_href(url: &str) -> anyhow::Result<()> {
                let window = web_sys::window().ok_or(anyhow!("no window?"))?;
                window.location().set_href(url).map_err(|err| {
                    anyhow!(err
                        .as_string()
                        .unwrap_or("window.location.set_href failed".to_string()))
                })
            }

            let page = match self {
                Executable::ABStreet => "abstreet",
                Executable::FifteenMin => "fifteen_min",
                Executable::OSMViewer => "osm_viewer",
                // This only works on native
                Executable::ParkingMapper => unreachable!(),
                Executable::Santa => "santa",
                Executable::RawMapEditor => "map_editor",
                Executable::LTN => "ltn",
            };
            let url = format!("{}.html{}", page, abstutil::args_to_query_string(args));
            if let Err(err) = set_href(&url) {
                return Transition::Push(PopupMsg::new_state(
                    ctx,
                    "Error",
                    vec![format!("Couldn't redirect to {}: {}", url, err)],
                ));
            }
            Transition::Keep
        }
    }
}

impl<A: AppLike + 'static> SimpleState<A> for TitleScreen<A> {
    fn on_click(
        &mut self,
        ctx: &mut EventCtx,
        app: &mut A,
        x: &str,
        _: &mut Panel,
    ) -> Transition<A> {
        match x {
            "Traffic simulation tutorial" => {
                self.run(ctx, app, Executable::ABStreet, vec!["--tutorial-intro"])
            }
            "Traffic simulation challenges" => {
                self.run(ctx, app, Executable::ABStreet, vec!["--challenges"])
            }
            "15-minute Santa" => self.run(ctx, app, Executable::Santa, vec![]),
            "Traffic simulation sandbox" => {
                self.run(ctx, app, Executable::ABStreet, vec!["--sandbox"])
            }
            "Community proposals" => self.run(ctx, app, Executable::ABStreet, vec!["--proposals"]),
            "Ungap the Map" => self.run(ctx, app, Executable::ABStreet, vec!["--ungap"]),
            "15-minute neighborhoods" => self.run(ctx, app, Executable::FifteenMin, vec![]),
            "Low traffic neighborhoods" => self.run(ctx, app, Executable::LTN, vec![]),
            "ActDev" => {
                open_browser("https://actdev.cyipt.bike");
                Transition::Keep
            }
            "Advanced tools" => self.run(ctx, app, Executable::ABStreet, vec!["--devtools"]),
            "About" => Transition::Push(PopupMsg::new_state(
                ctx,
                "About A/B Street",
                vec![
                    "Disclaimer: This software is based on imperfect data, heuristics concocted",
                    "under the influence of cold brew, a simplified traffic simulation model,",
                    "and a deeply flawed understanding of how much articulated buses can bend",
                    "around tight corners. Use this as a conversation starter with your city",
                    "government, not a final decision maker. Any resemblance of in-game",
                    "characters to real people is probably coincidental, unless of course you",
                    "stumble across the elusive \"Dustin Bikelino\". Have the appropriate",
                    "amount of fun.",
                ],
            )),
            "Credits" => {
                open_browser("https://a-b-street.github.io/docs/project/team.html");
                Transition::Keep
            }
            "Download the new release" => {
                open_browser("https://github.com/a-b-street/abstreet/releases");
                Transition::Keep
            }
            _ => unreachable!(),
        }
    }
}

#[cfg(not(target_arch = "wasm32"))]
#[allow(unused, clippy::logic_bug)]
mod built_info {
    use super::*;

    include!(concat!(env!("OUT_DIR"), "/built.rs"));

    pub fn maybe_update(ctx: &mut EventCtx) -> Widget {
        let t = built::util::strptime(BUILT_TIME_UTC);

        let txt = widgetry::Text::from(format!("This version built on {}", t.date().naive_local()))
            .into_widget(ctx);
        // Disable this warning; no promise about a release schedule anymore
        if false && (chrono::Utc::now() - t).num_days() > 15 {
            Widget::row(vec![
                txt.centered_vert(),
                ctx.style()
                    .btn_outline
                    .text("Download the new release")
                    .build_def(ctx),
            ])
        } else {
            txt
        }
    }
}

#[cfg(target_arch = "wasm32")]
mod built_info {
    use super::*;

    pub fn maybe_update(_: &mut EventCtx) -> Widget {
        Widget::nothing()
    }
}