From b58cb470f6d11c1353f7df14c5af9a5196b9a94a Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 24 Mar 2020 23:36:38 -0700 Subject: [PATCH] cleanup settings page by using the new type-safe dropdowns --- game/src/options.rs | 202 ++++++++++++++------------------------------ 1 file changed, 63 insertions(+), 139 deletions(-) diff --git a/game/src/options.rs b/game/src/options.rs index c2e2af3aab..2ad55fed10 100644 --- a/game/src/options.rs +++ b/game/src/options.rs @@ -1,6 +1,6 @@ use crate::app::App; use crate::colors; -use crate::game::{State, Transition, WizardState}; +use crate::game::{State, Transition}; use ezgui::{ hotkey, Btn, Choice, Composite, EventCtx, GfxCtx, Key, Line, Outcome, TextExt, Widget, }; @@ -21,17 +21,6 @@ impl Options { dev: false, } } - - fn cs_name(&self) -> &'static str { - match self.color_scheme { - Some(ref x) => match x.as_ref() { - "../data/system/override_colors.json" => "overridden", - "../data/system/night_colors.json" => "night mode", - _ => unreachable!(), - }, - None => "default", - } - } } #[derive(Clone, PartialEq)] @@ -41,29 +30,9 @@ pub enum TrafficSignalStyle { Icons, IndividualTurnArrows, } -impl abstutil::Cloneable for TrafficSignalStyle {} - -impl TrafficSignalStyle { - fn describe(&self) -> &'static str { - match self { - TrafficSignalStyle::GroupArrows => { - "arrows showing the protected and permitted movements" - } - TrafficSignalStyle::Sidewalks => { - "arrows showing the protected and permitted movements, with sidewalks" - } - TrafficSignalStyle::Icons => "icons for movements (like the editor UI)", - TrafficSignalStyle::IndividualTurnArrows => { - "arrows showing individual turns (to debug)" - } - } - } -} pub struct OptionsPanel { composite: Composite, - traffic_signal_style: TrafficSignalStyle, - color_scheme: Option, } impl OptionsPanel { @@ -92,18 +61,51 @@ impl OptionsPanel { ctx.canvas.touchpad_to_move, ) .margin(5), - // TODO Refactor this pattern somehow, using drop-down menus or radio buttons Widget::row(vec![ "Traffic signal rendering:".draw_text(ctx).margin(5), - Btn::text_fg(format!("{} ▼", app.opts.traffic_signal_style.describe())) - .build(ctx, "change traffic signal style", None) - .margin(5), + Widget::dropdown( + ctx, + "Traffic signal rendering", + app.opts.traffic_signal_style.clone(), + vec![ + 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).margin(5), - Btn::text_fg(format!("{} ▼", app.opts.cs_name())) - .build(ctx, "change color scheme", None) - .margin(5), + Widget::dropdown( + ctx, + "Color scheme", + app.opts.color_scheme.clone(), + vec![ + Choice::new("default", None), + Choice::new( + "overridden colors", + Some("../data/system/override_colors.json".to_string()), + ), + Choice::new( + "night mode", + Some("../data/system/night_colors.json".to_string()), + ), + ], + ), ]), Widget::row(vec![ format!( @@ -112,9 +114,17 @@ impl OptionsPanel { ) .draw_text(ctx) .margin(5), - Btn::text_fg(format!("{} ▼", ctx.get_scale_factor())) - .build(ctx, "change scale factor", None) - .margin(5), + Widget::dropdown( + ctx, + "Scale factor", + ctx.get_scale_factor(), + vec![ + Choice::new("0.5", 0.5), + Choice::new("1.0", 1.0), + Choice::new("1.5", 1.5), + Choice::new("2.0", 2.0), + ], + ), ]), Btn::text_bg2("Apply") .build_def(ctx, hotkey(Key::Enter)) @@ -124,8 +134,6 @@ impl OptionsPanel { .bg(colors::PANEL_BG), ) .build(ctx), - traffic_signal_style: app.opts.traffic_signal_style.clone(), - color_scheme: app.opts.color_scheme.clone(), } } } @@ -146,112 +154,28 @@ impl State for OptionsPanel { .is_checked("Use touchpad to pan and hold Control to zoom"); app.opts.dev = self.composite.is_checked("Enable developer mode"); - if app.opts.traffic_signal_style != self.traffic_signal_style { - app.opts.traffic_signal_style = self.traffic_signal_style.clone(); + 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 app.primary.draw_map.intersections.iter_mut() { *i.draw_traffic_signal.borrow_mut() = None; } } - if app.opts.color_scheme != self.color_scheme { - app.opts.color_scheme = self.color_scheme.take(); + 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()); } + let factor = self.composite.dropdown_value("Scale factor"); + if ctx.get_scale_factor() != factor { + ctx.set_scale_factor(factor); + } + return Transition::Pop; } - "change traffic signal style" => { - return Transition::Push(WizardState::new(Box::new(|wiz, ctx, _| { - let (_, style) = - wiz.wrap(ctx) - .choose("How should traffic signals be drawn?", || { - vec![ - Choice::new( - TrafficSignalStyle::GroupArrows.describe(), - TrafficSignalStyle::GroupArrows, - ), - Choice::new( - TrafficSignalStyle::Sidewalks.describe(), - TrafficSignalStyle::Sidewalks, - ), - Choice::new( - TrafficSignalStyle::Icons.describe(), - TrafficSignalStyle::Icons, - ), - Choice::new( - TrafficSignalStyle::IndividualTurnArrows.describe(), - TrafficSignalStyle::IndividualTurnArrows, - ), - ] - })?; - Some(Transition::PopWithData(Box::new(move |state, _, ctx| { - let mut panel = state.downcast_mut::().unwrap(); - panel.composite.replace( - ctx, - "change traffic signal style", - Btn::text_fg(format!("{} ▼", style.describe())) - .build(ctx, "change traffic signal style", None) - .margin(5), - ); - panel.traffic_signal_style = style; - }))) - }))); - } - "change color scheme" => { - return Transition::Push(WizardState::new(Box::new(|wiz, ctx, _| { - let (descr, path) = wiz.wrap(ctx).choose("What color scheme?", || { - vec![ - Choice::new("default", None), - Choice::new( - "overridden colors", - Some("../data/system/override_colors.json".to_string()), - ), - Choice::new( - "night mode", - Some("../data/system/night_colors.json".to_string()), - ), - ] - })?; - Some(Transition::PopWithData(Box::new(move |state, _, ctx| { - let mut panel = state.downcast_mut::().unwrap(); - panel.composite.replace( - ctx, - "change color scheme", - Btn::text_fg(format!("{} ▼", descr)) - .build(ctx, "change color scheme", None) - .margin(5), - ); - panel.color_scheme = path; - }))) - }))); - } - "change scale factor" => { - return Transition::Push(WizardState::new(Box::new(|wiz, ctx, _| { - let (_, scale) = wiz.wrap(ctx).choose( - "What scale factor for text / UI elements?", - || { - vec![ - Choice::new("0.5", 0.5), - Choice::new("1.0", 1.0), - Choice::new("1.5", 1.5), - Choice::new("2.0", 2.0), - ] - }, - )?; - Some(Transition::PopWithData(Box::new(move |state, _, ctx| { - let panel = state.downcast_mut::().unwrap(); - panel.composite.replace( - ctx, - "change scale factor", - Btn::text_fg(format!("{} ▼", scale)) - .build(ctx, "change scale factor", None) - .margin(5), - ); - ctx.set_scale_factor(scale); - }))) - }))); - } _ => unreachable!(), }, None => {}