From 35141dee40f363a742420f0f667a2f99603ecc65 Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 15 Sep 2018 16:50:19 -0700 Subject: [PATCH] at last, the satisfying collapsing of all the boilerplate color functions --- docs/design.md | 9 ++- editor/src/ui.rs | 207 ++++++----------------------------------------- 2 files changed, 31 insertions(+), 185 deletions(-) diff --git a/docs/design.md b/docs/design.md index f5a526cd2c..c72fbd17b2 100644 --- a/docs/design.md +++ b/docs/design.md @@ -993,11 +993,12 @@ so it feels like we implicitly have a big enum of active plugin, with each of th Alright, replan yet again. -- deal with overlapping keys that still kinda happen (sim ctrl, escape game) -- bug: do need to recalculate current_selection whenever anything potentially changes camera, like follow = then rethink colors, with simplified single plugin = plugin trait, color(id) -> Option. parallel list of box plugins (or, a fxn that takes the idx) - - refactor to one color_blah method - - handle the two color things... just buildings? + = refactor to one color_blah method + = handle the two color things... just buildings? +- deal with overlapping keys that still kinda happen (sim ctrl, escape game) +- bug: do need to recalculate current_selection whenever anything potentially changes camera, like follow - and see how much boilerplate a new type would need, by adding bus stops and water/parks - consider merging control map into map +- see how hard it is to render textures onto cars or something diff --git a/editor/src/ui.rs b/editor/src/ui.rs index 6f73025cc7..76352bc3fc 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -35,7 +35,7 @@ use plugins::warp::WarpState; use plugins::Colorizer; use render::{DrawMap, RenderOptions}; use sim; -use sim::{CarID, PedestrianID, Sim}; +use sim::Sim; use std::collections::HashMap; use std::process; @@ -314,166 +314,6 @@ impl UI { None } - fn color_lane(&self, id: map_model::LaneID) -> Option { - if let Some(c) = self.color_for_selected(ID::Lane(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Lane(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - None - } - - fn color_intersection(&self, id: map_model::IntersectionID) -> Option { - if let Some(c) = self.color_for_selected(ID::Intersection(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Intersection(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - None - } - - fn color_turn_icon(&self, id: map_model::TurnID) -> Option { - if let Some(c) = self.color_for_selected(ID::Turn(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Turn(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - - None - } - - fn color_building(&self, id: map_model::BuildingID) -> Option { - if let Some(c) = self.color_for_selected(ID::Building(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Building(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - - None - } - - fn color_parcel(&self, id: map_model::ParcelID) -> Option { - if let Some(c) = self.color_for_selected(ID::Parcel(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Parcel(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - - None - } - - fn color_car(&self, id: CarID) -> Option { - if let Some(c) = self.color_for_selected(ID::Car(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Car(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - - None - } - - fn color_ped(&self, id: PedestrianID) -> Option { - if let Some(c) = self.color_for_selected(ID::Pedestrian(id)) { - return Some(c); - } - if let Some(p) = self.get_active_plugin() { - if let Some(c) = p.color_for( - ID::Pedestrian(id), - Ctx { - cs: &self.cs, - map: &self.map, - control_map: &self.control_map, - canvas: &self.canvas, - sim: &self.sim, - }, - ) { - return Some(c); - } - } - - None - } - - fn color_for_selected(&self, id: ID) -> Option { - if Some(id) == self.current_selection { - Some(self.cs.get(Colors::Selected)) - } else { - None - } - } - fn event( &mut self, input: &mut UserInput, @@ -556,21 +396,8 @@ impl UI { self, ); for obj in statics.into_iter() { - let color = match obj.get_id() { - ID::Parcel(id) => self.color_parcel(id), - ID::Lane(id) => self.color_lane(id), - ID::Intersection(id) => self.color_intersection(id), - ID::Turn(id) => self.color_turn_icon(id), - ID::Building(id) => self.color_building(id), - // TODO do the whole generic thing too - ID::ExtraShape(id) => self.color_for_selected(ID::ExtraShape(id)), - - ID::Car(_) | ID::Pedestrian(_) => { - panic!("Dynamic {:?} in statics list", obj.get_id()) - } - }; let opts = RenderOptions { - color, + color: self.color_obj(obj.get_id()), cam_zoom: self.canvas.cam_zoom, debug_mode: self.layers.debug_mode.is_enabled(), }; @@ -587,13 +414,8 @@ impl UI { ); } for obj in dynamics.into_iter() { - let color = match obj.get_id() { - ID::Car(id) => self.color_car(id), - ID::Pedestrian(id) => self.color_ped(id), - _ => panic!("Static {:?} in dynamics list", obj.get_id()), - }; let opts = RenderOptions { - color, + color: self.color_obj(obj.get_id()), cam_zoom: self.canvas.cam_zoom, debug_mode: self.layers.debug_mode.is_enabled(), }; @@ -642,6 +464,29 @@ impl UI { self.canvas.draw_osd_notification(g, &osd_lines); } + fn color_obj(&self, id: ID) -> Option { + if Some(id) == self.current_selection { + return Some(self.cs.get(Colors::Selected)); + } + + if let Some(p) = self.get_active_plugin() { + if let Some(c) = p.color_for( + id, + Ctx { + cs: &self.cs, + map: &self.map, + control_map: &self.control_map, + canvas: &self.canvas, + sim: &self.sim, + }, + ) { + return Some(c); + } + } + + None + } + fn get_active_plugin(&self) -> Option> { let idx = self.active_plugin?; // Match instead of array, because can't move the Box out of the temporary vec. :\