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
361
362
363
364
365
366
367
368
369
370
use abstutil::CmdArgs;
use geom::{Duration, UnitFmt};
use widgetry::{
    Btn, Checkbox, Choice, EventCtx, GeomBatch, GfxCtx, Key, Line, Outcome, Panel, Spinner, State,
    TextExt, Widget,
};

use crate::colors::ColorSchemeChoice;
use crate::render::DrawBuilding;
use crate::tools::grey_out_map;
use crate::AppLike;

/// Options controlling the UI. Some of the options are common to all map-based apps, and some are
/// specific to A/B Street.
// TODO SimOptions stuff too
#[derive(Clone)]
pub struct Options {
    /// Dev mode exposes experimental tools useful for debugging, but that'd likely confuse most
    /// players.
    pub dev: bool,
    /// Every time we draw, render all agents zoomed in. Extremely slow. Just used to flush out
    /// drawing bugs.
    pub debug_all_agents: bool,

    /// How traffic signals should be rendered.
    pub traffic_signal_style: TrafficSignalStyle,
    /// The color scheme for map elements, agents, and the UI.
    pub color_scheme: ColorSchemeChoice,
    /// Automatically change color_scheme based on simulation time to reflect day/night
    pub toggle_day_night_colors: bool,
    /// Map elements are drawn differently when unzoomed and zoomed. This specifies the canvas zoom
    /// level where they switch.
    pub min_zoom_for_detail: f64,
    /// Draw buildings in different perspectives
    pub camera_angle: CameraAngle,

    /// How much to advance the sim with one of the speed controls
    pub time_increment: Duration,
    /// When time warping, don't draw to speed up simulation
    pub dont_draw_time_warp: bool,
    /// The delay threshold to halt on when jumping to the next delay
    pub jump_to_delay: Duration,

    /// Display roads and buildings in an alternate language, if possible. None means to use the
    /// OSM native name.
    pub language: Option<String>,
    /// How to render geometric units
    pub units: UnitFmt,
}

impl Options {
    pub fn default() -> Options {
        Options {
            dev: false,
            debug_all_agents: false,

            traffic_signal_style: TrafficSignalStyle::BAP,
            color_scheme: ColorSchemeChoice::Standard,
            toggle_day_night_colors: false,
            min_zoom_for_detail: 4.0,
            camera_angle: CameraAngle::TopDown,

            time_increment: Duration::minutes(10),
            dont_draw_time_warp: false,
            jump_to_delay: Duration::minutes(5),

            language: None,
            units: UnitFmt {
                round_durations: true,
                // TODO Should default be based on the map?
                metric: false,
            },
        }
    }

    /// Update the options using command-line flags.
    pub fn update_from_args(&mut self, args: &mut CmdArgs) {
        self.dev = args.enabled("--dev");
        if args.enabled("--lowzoom") {
            self.min_zoom_for_detail = 1.0;
        }
        if let Some(x) = args.optional("--color_scheme") {
            let mut ok = false;
            let mut options = Vec::new();
            for c in ColorSchemeChoice::choices() {
                options.push(c.label.clone());
                if c.label == x {
                    self.color_scheme = c.data;
                    ok = true;
                    break;
                }
            }
            if !ok {
                panic!(
                    "Invalid --color_scheme={}. Choices: {}",
                    x,
                    options.join(", ")
                );
            }
        }
    }
}

/// Different ways of drawing traffic signals. The names of these aren't super meaningful...
#[derive(Clone, PartialEq, Debug)]
pub enum TrafficSignalStyle {
    BAP,
    Yuwen,
    IndividualTurnArrows,
}

#[derive(Clone, PartialEq, Debug)]
pub enum CameraAngle {
    TopDown,
    IsometricNE,
    IsometricNW,
    IsometricSE,
    IsometricSW,
    Abstract,
}

pub struct OptionsPanel {
    panel: Panel,
}

impl OptionsPanel {
    pub fn new<A: AppLike>(ctx: &mut EventCtx, app: &A) -> Box<dyn State<A>> {
        Box::new(OptionsPanel {
            panel: Panel::new(Widget::col(vec![
                Widget::custom_row(vec![
                    Line("Settings").small_heading().draw(ctx),
                    Btn::close(ctx),
                ]),
                "Camera controls".draw_text(ctx),
                Widget::col(vec![
                    Checkbox::checkbox(
                        ctx,
                        "Invert direction of vertical scrolling",
                        None,
                        ctx.canvas.invert_scroll,
                    ),
                    Checkbox::checkbox(
                        ctx,
                        "Pan map when cursor is at edge of screen",
                        None,
                        ctx.canvas.edge_auto_panning,
                    )
                    .named("autopan"),
                    Checkbox::checkbox(
                        ctx,
                        "Use touchpad to pan and hold Control to zoom",
                        None,
                        ctx.canvas.touchpad_to_move,
                    ),
                    Checkbox::checkbox(
                        ctx,
                        "Use arrow keys to pan and Q/W to zoom",
                        None,
                        ctx.canvas.keys_to_pan,
                    ),
                    Widget::row(vec![
                        "Scroll speed for menus".draw_text(ctx).centered_vert(),
                        Spinner::new(ctx, (1, 50), ctx.canvas.gui_scroll_speed as isize)
                            .named("gui_scroll_speed"),
                    ]),
                ])
                .bg(app.cs().section_bg)
                .padding(8),
                "Appearance".draw_text(ctx),
                Widget::col(vec![
                    Widget::row(vec![
                        "Traffic signal rendering:".draw_text(ctx),
                        Widget::dropdown(
                            ctx,
                            "Traffic signal rendering",
                            app.opts().traffic_signal_style.clone(),
                            vec![
                                Choice::new("Default (Brian's style)", TrafficSignalStyle::BAP),
                                Choice::new("Yuwen's style", TrafficSignalStyle::Yuwen),
                                Choice::new(
                                    "arrows showing individual turns (to debug)",
                                    TrafficSignalStyle::IndividualTurnArrows,
                                ),
                            ],
                        ),
                    ]),
                    Widget::row(vec![
                        "Camera angle:".draw_text(ctx),
                        Widget::dropdown(
                            ctx,
                            "Camera angle",
                            app.opts().camera_angle.clone(),
                            vec![
                                Choice::new("Top-down", CameraAngle::TopDown),
                                Choice::new("Isometric (northeast)", CameraAngle::IsometricNE),
                                Choice::new("Isometric (northwest)", CameraAngle::IsometricNW),
                                Choice::new("Isometric (southeast)", CameraAngle::IsometricSE),
                                Choice::new("Isometric (southwest)", CameraAngle::IsometricSW),
                                Choice::new("Abstract (just symbols)", CameraAngle::Abstract),
                            ],
                        ),
                    ]),
                    Widget::row(vec![
                        "Color scheme:".draw_text(ctx),
                        Widget::dropdown(
                            ctx,
                            "Color scheme",
                            app.opts().color_scheme,
                            ColorSchemeChoice::choices(),
                        ),
                    ]),
                    Widget::row(vec![
                        "Camera zoom to switch to unzoomed view".draw_text(ctx),
                        Widget::dropdown(
                            ctx,
                            "min zoom",
                            app.opts().min_zoom_for_detail,
                            vec![
                                Choice::new("1.0", 1.0),
                                Choice::new("2.0", 2.0),
                                Choice::new("3.0", 3.0),
                                Choice::new("4.0", 4.0),
                                Choice::new("5.0", 5.0),
                                Choice::new("6.0", 6.0),
                            ],
                        ),
                    ]),
                    Widget::row(vec![
                        "Language".draw_text(ctx),
                        Widget::dropdown(ctx, "language", app.opts().language.clone(), {
                            let mut choices = Vec::new();
                            choices.push(Choice::new("Map native language", None));
                            for lang in app.map().get_languages() {
                                choices.push(Choice::new(lang, Some(lang.to_string())));
                            }
                            choices
                        }),
                    ]),
                    Checkbox::toggle(
                        ctx,
                        "metric / imperial units",
                        "metric",
                        "imperial",
                        None,
                        app.opts().units.metric,
                    ),
                ])
                .bg(app.cs().section_bg)
                .padding(8),
                "Debug".draw_text(ctx),
                Widget::col(vec![
                    Checkbox::checkbox(ctx, "Enable developer mode", None, app.opts().dev),
                    Checkbox::checkbox(
                        ctx,
                        "Draw all agents to debug geometry (Slow!)",
                        None,
                        app.opts().debug_all_agents,
                    ),
                ])
                .bg(app.cs().section_bg)
                .padding(8),
                Btn::text_bg2("Apply")
                    .build_def(ctx, Key::Enter)
                    .centered_horiz(),
            ]))
            .build(ctx),
        })
    }
}

impl<A: AppLike> State<A> for OptionsPanel {
    fn event(&mut self, ctx: &mut EventCtx, app: &mut A) -> widgetry::Transition<A> {
        match self.panel.event(ctx) {
            Outcome::Clicked(x) => match x.as_ref() {
                "close" => {
                    return widgetry::Transition::Pop;
                }
                "Apply" => {
                    let mut opts = app.opts().clone();
                    opts.dev = self.panel.is_checked("Enable developer mode");
                    opts.debug_all_agents = self
                        .panel
                        .is_checked("Draw all agents to debug geometry (Slow!)");

                    ctx.canvas.invert_scroll = self
                        .panel
                        .is_checked("Invert direction of vertical scrolling");
                    ctx.canvas.touchpad_to_move = self
                        .panel
                        .is_checked("Use touchpad to pan and hold Control to zoom");
                    ctx.canvas.keys_to_pan = self
                        .panel
                        .is_checked("Use arrow keys to pan and Q/W to zoom");
                    ctx.canvas.edge_auto_panning = self.panel.is_checked("autopan");
                    ctx.canvas.gui_scroll_speed = self.panel.spinner("gui_scroll_speed") as usize;

                    let style = self.panel.dropdown_value("Traffic signal rendering");
                    if opts.traffic_signal_style != style {
                        opts.traffic_signal_style = style;
                        println!("Rerendering traffic signals...");
                        for i in &mut app.mut_draw_map().intersections {
                            *i.draw_traffic_signal.borrow_mut() = None;
                        }
                    }

                    let camera_angle = self.panel.dropdown_value("Camera angle");
                    if opts.camera_angle != camera_angle {
                        opts.camera_angle = camera_angle;
                        ctx.loading_screen("rerendering buildings", |ctx, timer| {
                            let mut all_buildings = GeomBatch::new();
                            let mut all_building_paths = GeomBatch::new();
                            let mut all_building_outlines = GeomBatch::new();
                            timer
                                .start_iter("rendering buildings", app.map().all_buildings().len());
                            for b in app.map().all_buildings() {
                                timer.next();
                                DrawBuilding::new(
                                    ctx,
                                    b,
                                    app.map(),
                                    app.cs(),
                                    &opts,
                                    &mut all_buildings,
                                    &mut all_building_paths,
                                    &mut all_building_outlines,
                                );
                            }
                            timer.start("upload geometry");
                            app.mut_draw_map().draw_all_buildings = all_buildings.upload(ctx);
                            app.mut_draw_map().draw_all_building_paths =
                                all_building_paths.upload(ctx);
                            app.mut_draw_map().draw_all_building_outlines =
                                all_building_outlines.upload(ctx);
                            timer.stop("upload geometry");
                        });
                    }

                    if app.change_color_scheme(ctx, self.panel.dropdown_value("Color scheme")) {
                        // If the player picks a different scheme, don't undo it later.
                        opts.toggle_day_night_colors = false;
                    }

                    opts.min_zoom_for_detail = self.panel.dropdown_value("min zoom");
                    opts.units.metric = self.panel.is_checked("metric / imperial units");

                    let language = self.panel.dropdown_value("language");
                    if language != opts.language {
                        opts.language = language;
                        for r in &mut app.mut_draw_map().roads {
                            r.clear_rendering();
                        }
                    }

                    *app.mut_opts() = opts;

                    return widgetry::Transition::Pop;
                }
                _ => unreachable!(),
            },
            _ => {}
        }

        widgetry::Transition::Keep
    }

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