From 71854bc959e285abdbaa55ae45ff9a63963b8a8f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Tue, 3 Dec 2019 10:56:47 -0800 Subject: [PATCH] toggle color scheme in options panel --- abstutil/src/lib.rs | 4 ---- game/src/game.rs | 3 ++- game/src/helpers.rs | 16 +++++++++++----- game/src/options.rs | 28 ++++++++++++++++++++++++++++ game/src/ui.rs | 8 ++++---- 5 files changed, 45 insertions(+), 14 deletions(-) diff --git a/abstutil/src/lib.rs b/abstutil/src/lib.rs index 71365d140d..183ecbeedb 100644 --- a/abstutil/src/lib.rs +++ b/abstutil/src/lib.rs @@ -124,10 +124,6 @@ pub fn path_ab_test_save(map_name: &str, test_name: &str, time: String) -> Strin ) } -pub fn path_color_scheme() -> String { - format!("../data/color_scheme.json") -} - pub fn path_popdat() -> String { format!("../data/shapes/popdat.bin") } diff --git a/game/src/game.rs b/game/src/game.rs index a37bfb0550..2b5bc7675c 100644 --- a/game/src/game.rs +++ b/game/src/game.rs @@ -1,3 +1,4 @@ +use crate::options::Options; use crate::pregame::TitleScreen; use crate::render::DrawOptions; use crate::sandbox::{GameplayMode, SandboxMode}; @@ -20,7 +21,7 @@ impl Game { let title = !flags.dev && !flags.sim_flags.load.contains("data/save") && !flags.sim_flags.load.contains("data/scenarios"); - let mut ui = UI::new(flags, ctx, title); + let mut ui = UI::new(flags, Options::default(), ctx, title); let states: Vec> = if title { vec![Box::new(TitleScreen::new(ctx, &ui))] } else { diff --git a/game/src/helpers.rs b/game/src/helpers.rs index 4ed30d3359..fbe47d5d0f 100644 --- a/game/src/helpers.rs +++ b/game/src/helpers.rs @@ -78,6 +78,8 @@ pub struct ColorScheme { // A subset of map modified: ModifiedColors, + + path: String, } #[derive(Serialize, Deserialize)] @@ -86,18 +88,22 @@ struct ModifiedColors { } impl ColorScheme { - pub fn load() -> ColorScheme { - let modified: ModifiedColors = - abstutil::read_json(abstutil::path_color_scheme(), &mut Timer::throwaway()); + // TODO When we quit with this, it'll save and overwrite it... remember the name too + pub fn load(path: String) -> ColorScheme { + let modified: ModifiedColors = abstutil::read_json(path.clone(), &mut Timer::throwaway()); let mut map: HashMap = default_colors(); for (name, c) in &modified.map { map.insert(name.clone(), *c); } - ColorScheme { map, modified } + ColorScheme { + map, + modified, + path, + } } pub fn save(&self) { - abstutil::write_json(abstutil::path_color_scheme(), &self.modified); + abstutil::write_json(self.path.clone(), &self.modified); } // Get, but specify the default inline. The default is extracted before compilation by a script diff --git a/game/src/options.rs b/game/src/options.rs index 1fcb2c2e41..a59b9fb497 100644 --- a/game/src/options.rs +++ b/game/src/options.rs @@ -1,14 +1,18 @@ use crate::game::{State, Transition, WizardState}; use ezgui::Choice; +// TODO SimOptions stuff too +#[derive(Clone)] pub struct Options { pub traffic_signal_style: TrafficSignalStyle, + pub color_scheme: String, } impl Options { pub fn default() -> Options { Options { traffic_signal_style: TrafficSignalStyle::GroupArrows, + color_scheme: "../data/color_scheme.json".to_string(), } } } @@ -41,6 +45,23 @@ pub fn open_panel() -> Box { ), ] })?; + let (_, color_scheme) = wizard.choose("What color scheme?", || { + vec![ + Choice::new("default", "../data/color_scheme.json".to_string()), + Choice::new("night mode", "../data/night_colors.json".to_string()), + ] + })?; + + if ui.opts.color_scheme != color_scheme { + wizard.acknowledge("Changing color scheme", || { + vec![ + "Changing color scheme will reset the simulation", + "Also, some colors don't completely change immediately", + "Please file a bug if you notice anything weird", + ] + })?; + } + if ui.opts.traffic_signal_style != traffic_signal_style { ui.opts.traffic_signal_style = traffic_signal_style; println!("Rerendering traffic signals..."); @@ -48,6 +69,13 @@ pub fn open_panel() -> Box { *i.draw_traffic_signal.borrow_mut() = None; } } + + if ui.opts.color_scheme != color_scheme { + ui.opts.color_scheme = color_scheme.clone(); + let map_name = ui.primary.map.get_name().clone(); + ui.switch_map(ctx, &map_name); + } + Some(Transition::Pop) })) } diff --git a/game/src/ui.rs b/game/src/ui.rs index 8ef20c93ca..64efd6d645 100644 --- a/game/src/ui.rs +++ b/game/src/ui.rs @@ -22,8 +22,8 @@ pub struct UI { } impl UI { - pub fn new(flags: Flags, ctx: &mut EventCtx, splash: bool) -> UI { - let cs = ColorScheme::load(); + pub fn new(flags: Flags, opts: Options, ctx: &mut EventCtx, splash: bool) -> UI { + let cs = ColorScheme::load(opts.color_scheme.clone()); let (primary, prebaked) = ctx.loading_screen("load map", |ctx, mut timer| { // Always load some small icons. let mut textures = vec![ @@ -122,7 +122,7 @@ impl UI { cs, agent_cs: AgentColorScheme::VehicleTypes, prebaked, - opts: Options::default(), + opts, } } @@ -130,7 +130,7 @@ impl UI { ctx.canvas.save_camera_state(self.primary.map.get_name()); let mut flags = self.primary.current_flags.clone(); flags.sim_flags.load = abstutil::path_map(name); - *self = UI::new(flags, ctx, false); + *self = UI::new(flags, self.opts.clone(), ctx, false); } pub fn draw_ctx(&self) -> DrawCtx<'_> {