From 2770b59f85a650a90fbb562954667fc539991883 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Thu, 21 Jun 2018 17:02:18 -0700 Subject: [PATCH] make the color tuner actually change things live --- TODO.md | 3 +-- editor/src/colors.rs | 6 ++++- editor/src/plugins/color_picker.rs | 42 +++++++++++++++++++++++------- editor/src/ui.rs | 6 ++++- 4 files changed, 43 insertions(+), 14 deletions(-) diff --git a/TODO.md b/TODO.md index 6bcf698b5d..6ecd8c6c33 100644 --- a/TODO.md +++ b/TODO.md @@ -45,8 +45,7 @@ - https://gis-kingcounty.opendata.arcgis.com/datasets/traffic-signs--sign-point/ - multiple lanes - - live color tuner - - mutate color scheme + - make a smaller GUI where it's easy to explore solving the multiline problem - model bikes in driving lanes (as slow cars) - add random bike lanes, figure out how turns would work diff --git a/editor/src/colors.rs b/editor/src/colors.rs index efaa1b6430..4f9a55712f 100644 --- a/editor/src/colors.rs +++ b/editor/src/colors.rs @@ -10,7 +10,7 @@ use std::io::{Error, Read, Write}; use strum::IntoEnumIterator; #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, EnumIter, EnumString, ToString, - PartialOrd, Ord)] + PartialOrd, Ord, Clone, Copy)] pub enum Colors { Debug, BrightDebug, @@ -88,4 +88,8 @@ impl ColorScheme { // TODO make sure this isn't slow; maybe back this with an array *self.map.get(&c).unwrap() } + + pub fn set(&mut self, c: Colors, value: Color) { + self.map.insert(c, value); + } } diff --git a/editor/src/plugins/color_picker.rs b/editor/src/plugins/color_picker.rs index ff5a02322c..e778e27cd5 100644 --- a/editor/src/plugins/color_picker.rs +++ b/editor/src/plugins/color_picker.rs @@ -1,6 +1,6 @@ // Copyright 2018 Google LLC, licensed under http://www.apache.org/licenses/LICENSE-2.0 -use colors::Colors; +use colors::{ColorScheme, Colors}; use ezgui::canvas::{Canvas, GfxCtx}; use ezgui::input::UserInput; use ezgui::menu; @@ -20,7 +20,8 @@ const TILE_DIMS: u32 = 2; pub enum ColorPicker { Inactive, Choosing(menu::Menu), - PickingColor(Colors), + // Remember the original color, in case we revert + PickingColor(Colors, graphics::types::Color), } impl ColorPicker { @@ -28,7 +29,12 @@ impl ColorPicker { ColorPicker::Inactive } - pub fn handle_event(self, input: &mut UserInput, window_size: &Size) -> ColorPicker { + pub fn handle_event( + self, + input: &mut UserInput, + window_size: &Size, + cs: &mut ColorScheme, + ) -> ColorPicker { match self { ColorPicker::Inactive => { if input.unimportant_key_pressed(Key::D8, "Press 8 to configure colors") { @@ -44,13 +50,28 @@ impl ColorPicker { menu::Result::Canceled => ColorPicker::Inactive, menu::Result::StillActive => ColorPicker::Choosing(menu), menu::Result::Done(choice) => { - ColorPicker::PickingColor(Colors::from_str(&choice).unwrap()) + let c = Colors::from_str(&choice).unwrap(); + ColorPicker::PickingColor(c, cs.get(c)) } } } - ColorPicker::PickingColor(color) => { - // TODO be able to confirm a choice and edit the ColorScheme - if input.key_pressed(Key::D8, "Press 8 to stop configuring colors") { + ColorPicker::PickingColor(c, orig_color) => { + if input.key_pressed( + Key::Escape, + &format!( + "Press escape to stop configuring color for {:?} and revert", + c + ), + ) { + cs.set(c, orig_color); + return ColorPicker::Inactive; + } + + if input.key_pressed( + Key::Return, + &format!("Press enter to finalize new color for {:?}", c), + ) { + println!("Setting color for {:?}", c); return ColorPicker::Inactive; } @@ -60,11 +81,12 @@ impl ColorPicker { let x = (pos[0] - (start_x as f64)) / (TILE_DIMS as f64) / 255.0; let y = (pos[1] - (start_y as f64)) / (TILE_DIMS as f64) / 255.0; if x >= 0.0 && x <= 1.0 && y >= 0.0 && y <= 1.0 { - println!("current color is {:?}", get_color(x as f32, y as f32)); + cs.set(c, get_color(x as f32, y as f32)); + return ColorPicker::PickingColor(c, orig_color); } } - ColorPicker::PickingColor(color) + ColorPicker::PickingColor(c, orig_color) } } } @@ -78,7 +100,7 @@ impl ColorPicker { // TODO would be nice to display the text in the current color canvas.draw_mouse_tooltip(g, &menu.lines_to_display()); } - ColorPicker::PickingColor(_) => { + ColorPicker::PickingColor(_, _) => { let (start_x, start_y) = get_screen_offset(&g.window_size); for x in 0..WIDTH { diff --git a/editor/src/ui.rs b/editor/src/ui.rs index 3542692833..1a4c6df2ca 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -189,6 +189,11 @@ impl UI { } } + if !edit_mode { + self.color_picker = self.color_picker + .handle_event(input, window_size, &mut self.cs); + } + self.current_search_state = self.current_search_state.event(input); if !edit_mode @@ -235,7 +240,6 @@ impl UI { self.steepness_active.handle_event(input); self.osm_classifier_active.handle_event(input); self.debug_mode.handle_event(input); - self.color_picker = self.color_picker.handle_event(input, window_size); } if old_zoom >= MIN_ZOOM_FOR_MOUSEOVER && new_zoom < MIN_ZOOM_FOR_MOUSEOVER {