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
use crate::app::App;
use crate::colors::ColorSchemeChoice;
use crate::game::{State, Transition};
use ezgui::{
    hotkey, Btn, Checkbox, Choice, Composite, EventCtx, GfxCtx, Key, Line, Outcome, Spinner,
    TextExt, Widget,
};
use geom::Duration;

// TODO SimOptions stuff too
#[derive(Clone)]
pub struct Options {
    pub dev: bool,
    pub debug_all_agents: bool,

    pub label_roads: bool,
    pub traffic_signal_style: TrafficSignalStyle,
    pub color_scheme: ColorSchemeChoice,
    pub min_zoom_for_detail: f64,
    pub large_unzoomed_agents: bool,

    pub time_increment: Duration,
    pub resume_after_edit: bool,
    pub dont_draw_time_warp: bool,

    pub language: Option<String>,
}

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

            label_roads: true,
            traffic_signal_style: TrafficSignalStyle::BAP,
            color_scheme: ColorSchemeChoice::Standard,
            min_zoom_for_detail: 4.0,
            large_unzoomed_agents: false,

            time_increment: Duration::minutes(10),
            resume_after_edit: true,
            dont_draw_time_warp: false,

            language: None,
        }
    }
}

#[derive(Clone, PartialEq, Debug)]
pub enum TrafficSignalStyle {
    BAP,
    GroupArrows,
    Sidewalks,
    Icons,
    IndividualTurnArrows,
}

pub struct OptionsPanel {
    composite: Composite,
}

impl OptionsPanel {
    pub fn new(ctx: &mut EventCtx, app: &App) -> Box<dyn State> {
        Box::new(OptionsPanel {
            composite: Composite::new(Widget::col(vec![
                Widget::custom_row(vec![
                    Line("Settings").small_heading().draw(ctx),
                    Btn::plaintext("X")
                        .build(ctx, "close", hotkey(Key::Escape))
                        .align_right(),
                ]),
                "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![
                    Checkbox::checkbox(ctx, "Draw road names", None, app.opts.label_roads),
                    Widget::row(vec![
                        "Traffic signal rendering:".draw_text(ctx),
                        Widget::dropdown(
                            ctx,
                            "Traffic signal rendering",
                            app.opts.traffic_signal_style.clone(),
                            vec![
                                Choice::new(
                                    "Brian's variation of arrows showing the protected and \
                                     permitted movements",
                                    TrafficSignalStyle::BAP,
                                ),
                                Choice::new(
                                    "arrows showing the protected and permitted movements",
                                    TrafficSignalStyle::GroupArrows,
                                ),
                                Choice::new(
                                    "arrows showing the protected and permitted movements, with \
                                     sidewalks",
                                    TrafficSignalStyle::Sidewalks,
                                ),
                                Choice::new(
                                    "icons for movements (like the editor UI)",
                                    TrafficSignalStyle::Icons,
                                ),
                                Choice::new(
                                    "arrows showing individual turns (to debug)",
                                    TrafficSignalStyle::IndividualTurnArrows,
                                ),
                            ],
                        ),
                    ]),
                    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),
                            ],
                        ),
                    ]),
                    Checkbox::checkbox(
                        ctx,
                        "Draw enlarged unzoomed agents",
                        None,
                        app.opts.large_unzoomed_agents,
                    ),
                    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.primary.map.get_languages() {
                                choices.push(Choice::new(lang, Some(lang.to_string())));
                            }
                            choices
                        }),
                    ]),
                ])
                .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, hotkey(Key::Enter))
                    .centered_horiz(),
            ]))
            .build(ctx),
        })
    }
}

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

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

                    app.opts.label_roads = self.composite.is_checked("Draw road names");
                    let style = self.composite.dropdown_value("Traffic signal rendering");
                    if app.opts.traffic_signal_style != style {
                        app.opts.traffic_signal_style = style;
                        println!("Rerendering traffic signals...");
                        for i in &mut app.primary.draw_map.intersections {
                            *i.draw_traffic_signal.borrow_mut() = None;
                        }
                    }

                    let scheme = self.composite.dropdown_value("Color scheme");
                    if app.opts.color_scheme != scheme {
                        app.opts.color_scheme = scheme;
                        app.switch_map(ctx, app.primary.current_flags.sim_flags.load.clone());
                    }

                    app.opts.min_zoom_for_detail = self.composite.dropdown_value("min zoom");
                    app.opts.large_unzoomed_agents =
                        self.composite.is_checked("Draw enlarged unzoomed agents");

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

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

        Transition::Keep
    }

    fn draw(&self, g: &mut GfxCtx, app: &App) {
        State::grey_out_map(g, app);
        self.composite.draw(g);
    }
}