From 98d26d8979b7d524c99f600fd74f7c96a250c68f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sat, 3 Oct 2020 16:37:56 -0700 Subject: [PATCH] Refactor calls to calculate_current_selection --- game/src/app.rs | 41 ++++++++++++++++--- game/src/debug/mod.rs | 12 +----- game/src/debug/path_counter.rs | 12 +----- game/src/devtools/destinations.rs | 13 ++---- game/src/devtools/mapping.rs | 12 +----- game/src/edit/mod.rs | 12 +----- game/src/edit/select.rs | 12 +----- game/src/edit/traffic_signals/offsets.rs | 22 ++-------- game/src/edit/traffic_signals/picker.rs | 12 +----- game/src/layer/traffic.rs | 13 ++---- .../src/sandbox/dashboards/traffic_signals.rs | 10 +---- game/src/sandbox/gameplay/freeform.rs | 15 ++----- 12 files changed, 64 insertions(+), 122 deletions(-) diff --git a/game/src/app.rs b/game/src/app.rs index 475578e7b4..20a6f51f6e 100644 --- a/game/src/app.rs +++ b/game/src/app.rs @@ -10,7 +10,7 @@ use geom::{Bounds, Circle, Distance, Duration, Pt2D, Time}; use map_model::{IntersectionID, Map, Traversable}; use maplit::btreemap; use rand::seq::SliceRandom; -use sim::{Analytics, GetDrawAgents, Sim, SimCallback, SimFlags}; +use sim::{Analytics, DontDrawAgents, GetDrawAgents, Sim, SimCallback, SimFlags}; use std::collections::BTreeMap; use widgetry::{EventCtx, GfxCtx, Prerender}; @@ -298,10 +298,41 @@ impl App { ); } - // Because we have to sometimes borrow part of self for GetDrawAgents, this just returns the - // Option that the caller should assign. When this monolithic UI nonsense is dismantled, - // this weirdness goes away. - pub fn calculate_current_selection( + pub fn mouseover_unzoomed_roads_and_intersections(&self, ctx: &EventCtx) -> Option { + self.calculate_current_selection( + ctx, + &DontDrawAgents {}, + &ShowEverything::new(), + false, + true, + false, + ) + } + pub fn mouseover_unzoomed_buildings(&self, ctx: &EventCtx) -> Option { + self.calculate_current_selection( + ctx, + &DontDrawAgents {}, + &ShowEverything::new(), + false, + false, + true, + ) + } + pub fn mouseover_unzoomed_everything(&self, ctx: &EventCtx) -> Option { + self.calculate_current_selection( + ctx, + &DontDrawAgents {}, + &ShowEverything::new(), + false, + true, + true, + ) + } + pub fn mouseover_debug_mode(&self, ctx: &EventCtx, show_objs: &dyn ShowObject) -> Option { + self.calculate_current_selection(ctx, &self.primary.sim, show_objs, true, false, false) + } + + fn calculate_current_selection( &self, ctx: &EventCtx, source: &dyn GetDrawAgents, diff --git a/game/src/debug/mod.rs b/game/src/debug/mod.rs index 8b0c6fee88..c466b12ac0 100644 --- a/game/src/debug/mod.rs +++ b/game/src/debug/mod.rs @@ -112,8 +112,7 @@ impl State for DebugMode { ctx.canvas_movement(); if ctx.redo_mouseover() { - app.primary.current_selection = - app.calculate_current_selection(ctx, &app.primary.sim, self, true, false, false); + app.primary.current_selection = app.mouseover_debug_mode(ctx, self); } match self.panel.event(ctx) { @@ -200,14 +199,7 @@ impl State for DebugMode { } "unhide everything" => { self.hidden.clear(); - app.primary.current_selection = app.calculate_current_selection( - ctx, - &app.primary.sim, - self, - true, - false, - false, - ); + app.primary.current_selection = app.mouseover_debug_mode(ctx, self); self.reset_info(ctx); } "search OSM metadata" => { diff --git a/game/src/debug/path_counter.rs b/game/src/debug/path_counter.rs index a9bb1b7008..9ef312b0d2 100644 --- a/game/src/debug/path_counter.rs +++ b/game/src/debug/path_counter.rs @@ -1,12 +1,11 @@ // A state to count the number of trips that will cross different roads. -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::{ColorLegend, ColorNetwork, CommonState}; use crate::game::{State, Transition}; use crate::helpers::ID; use abstutil::Counter; use map_model::{IntersectionID, PathStep, RoadID, Traversable}; -use sim::DontDrawAgents; use widgetry::{ Btn, Color, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Text, VerticalAlignment, Widget, @@ -88,14 +87,7 @@ impl State for PathCounter { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { ctx.canvas_movement(); if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ); + app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx); self.tooltip = None; if let Some(r) = match app.primary.current_selection { Some(ID::Lane(l)) => Some(app.primary.map.get_l(l).parent), diff --git a/game/src/devtools/destinations.rs b/game/src/devtools/destinations.rs index 7e8b742cab..f61680b164 100644 --- a/game/src/devtools/destinations.rs +++ b/game/src/devtools/destinations.rs @@ -1,10 +1,10 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::{make_heatmap, HeatmapOptions}; use crate::game::{State, Transition}; use crate::helpers::{amenity_type, ID}; use abstutil::Counter; use map_model::BuildingID; -use sim::{DontDrawAgents, Scenario, TripEndpoint}; +use sim::{Scenario, TripEndpoint}; use widgetry::{ Btn, Checkbox, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, Text, VerticalAlignment, Widget, @@ -111,14 +111,7 @@ impl State for PopularDestinations { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { ctx.canvas_movement(); if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - false, - true, - ); + app.primary.current_selection = app.mouseover_unzoomed_buildings(ctx); if let Some(ID::Building(_)) = app.primary.current_selection { } else { app.primary.current_selection = None; diff --git a/game/src/devtools/mapping.rs b/game/src/devtools/mapping.rs index af221a1ec6..1678dccc8a 100644 --- a/game/src/devtools/mapping.rs +++ b/game/src/devtools/mapping.rs @@ -1,4 +1,4 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::{CityPicker, ColorLegend}; use crate::game::{PopupMsg, State, Transition}; use crate::helpers::{nice_map_name, open_browser, ID}; @@ -6,7 +6,6 @@ use abstutil::{prettyprint_usize, Tags, Timer}; use geom::{Distance, FindClosest, PolyLine, Polygon}; use map_model::{osm, RoadID}; use osm::WayID; -use sim::DontDrawAgents; use std::collections::{BTreeMap, HashSet}; use std::error::Error; use std::fs::File; @@ -192,14 +191,7 @@ impl State for ParkingMapper { ctx.canvas_movement(); if ctx.redo_mouseover() { - let mut maybe_r = match app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ) { + let mut maybe_r = match app.mouseover_unzoomed_roads_and_intersections(ctx) { Some(ID::Road(r)) => Some(r), Some(ID::Lane(l)) => Some(map.get_l(l).parent), _ => None, diff --git a/game/src/edit/mod.rs b/game/src/edit/mod.rs index 2a0cd08106..d9d0ea22f9 100644 --- a/game/src/edit/mod.rs +++ b/game/src/edit/mod.rs @@ -14,7 +14,7 @@ pub use self::routes::RouteEditor; pub use self::stop_signs::StopSignEditor; pub use self::traffic_signals::TrafficSignalEditor; pub use self::validate::{check_blackholes, check_sidewalk_connectivity, try_change_lt}; -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::{tool_panel, ColorLegend, CommonState, Warping}; use crate::debug::DebugMode; use crate::game::{ChooseSomething, PopupMsg, State, Transition}; @@ -26,7 +26,6 @@ use abstutil::Timer; use geom::Speed; use map_model::{EditCmd, IntersectionID, LaneID, LaneType, MapEdits}; use maplit::btreeset; -use sim::DontDrawAgents; use std::collections::BTreeSet; use widgetry::{ lctrl, Btn, Choice, Color, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Menu, @@ -127,14 +126,7 @@ impl State for EditMode { ctx.canvas_movement(); // Restrict what can be selected. if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ); + app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx); if let Some(ID::Lane(l)) = app.primary.current_selection { if !can_edit_lane(&self.mode, l, app) { app.primary.current_selection = None; diff --git a/game/src/edit/select.rs b/game/src/edit/select.rs index 5e8ba6c69a..f979232b1b 100644 --- a/game/src/edit/select.rs +++ b/game/src/edit/select.rs @@ -1,8 +1,7 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::CommonState; use crate::helpers::{intersections_from_roads, ID}; use map_model::{IntersectionID, RoadID}; -use sim::DontDrawAgents; use std::collections::BTreeSet; use widgetry::{Btn, Color, Drawable, EventCtx, GeomBatch, GfxCtx, Key, RewriteColor, Widget}; @@ -105,14 +104,7 @@ impl RoadSelector { // Pass None. Returns true if anything changed. pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App, clicked: Option<&str>) -> bool { if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ); + app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx); match self.mode { Mode::Pan => { app.primary.current_selection = None; diff --git a/game/src/edit/traffic_signals/offsets.rs b/game/src/edit/traffic_signals/offsets.rs index 6aed2cc1e1..37e8a84b6c 100644 --- a/game/src/edit/traffic_signals/offsets.rs +++ b/game/src/edit/traffic_signals/offsets.rs @@ -1,4 +1,4 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::CommonState; use crate::edit::traffic_signals::fade_irrelevant; use crate::game::{State, Transition}; @@ -6,7 +6,7 @@ use crate::helpers::ID; use geom::{Distance, Duration}; use map_model::IntersectionID; use maplit::btreeset; -use sim::{DontDrawAgents, Scenario}; +use sim::Scenario; use std::collections::BTreeSet; use widgetry::{ Btn, Color, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, @@ -59,14 +59,7 @@ impl State for ShowAbsolute { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { ctx.canvas_movement(); if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ); + app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx); } if let Some(ID::Intersection(i)) = app.primary.current_selection { if self.members.contains(&i) { @@ -168,14 +161,7 @@ impl State for ShowRelative { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { ctx.canvas_movement(); if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ); + app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx); } if let Some(ID::Intersection(i)) = app.primary.current_selection { if self.members.contains(&i) && i != self.base { diff --git a/game/src/edit/traffic_signals/picker.rs b/game/src/edit/traffic_signals/picker.rs index ab73aa9671..df2088578a 100644 --- a/game/src/edit/traffic_signals/picker.rs +++ b/game/src/edit/traffic_signals/picker.rs @@ -1,11 +1,10 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::CommonState; use crate::edit::TrafficSignalEditor; use crate::game::{State, Transition}; use crate::helpers::ID; use crate::sandbox::gameplay::GameplayMode; use map_model::IntersectionID; -use sim::DontDrawAgents; use std::collections::BTreeSet; use widgetry::{ hotkeys, Btn, Color, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, Outcome, @@ -48,14 +47,7 @@ impl State for SignalPicker { fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { ctx.canvas_movement(); if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ); + app.primary.current_selection = app.mouseover_unzoomed_roads_and_intersections(ctx); } if let Some(ID::Intersection(i)) = app.primary.current_selection { if app.primary.map.maybe_get_traffic_signal(i).is_some() { diff --git a/game/src/layer/traffic.rs b/game/src/layer/traffic.rs index 793a076af9..0465a9267c 100644 --- a/game/src/layer/traffic.rs +++ b/game/src/layer/traffic.rs @@ -1,4 +1,4 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::{ColorLegend, ColorNetwork, DivergingScale}; use crate::helpers::ID; use crate::layer::{Layer, LayerOutcome}; @@ -6,7 +6,7 @@ use abstutil::{prettyprint_usize, Counter}; use geom::{Circle, Distance, Duration, Polygon, Pt2D, Time}; use map_model::{IntersectionID, Map, Traversable, NORMAL_LANE_THICKNESS, SIDEWALK_THICKNESS}; use maplit::btreeset; -use sim::{DontDrawAgents, GetDrawAgents}; +use sim::GetDrawAgents; use std::collections::BTreeSet; use widgetry::{ Btn, Checkbox, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line, @@ -132,14 +132,7 @@ impl Layer for Throughput { if ctx.canvas.cam_zoom < app.opts.min_zoom_for_detail { if ctx.redo_mouseover() || recalc_tooltip { self.tooltip = None; - match app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - false, - ) { + match app.mouseover_unzoomed_roads_and_intersections(ctx) { Some(ID::Road(r)) => { let cnt = app.primary.sim.get_analytics().road_thruput.total_for(r); if cnt > 0 { diff --git a/game/src/sandbox/dashboards/traffic_signals.rs b/game/src/sandbox/dashboards/traffic_signals.rs index b8c351685c..4064b9d55a 100644 --- a/game/src/sandbox/dashboards/traffic_signals.rs +++ b/game/src/sandbox/dashboards/traffic_signals.rs @@ -69,14 +69,8 @@ impl State for TrafficSignalDemand { // TODO DrawWithTooltips works great in screenspace; make a similar tool for mapspace? if ctx.redo_mouseover() { self.selected = None; - if let Some(ID::Intersection(i)) = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - false, - false, - ) { + app.recalculate_current_selection(ctx); + if let Some(ID::Intersection(i)) = app.primary.current_selection.take() { if let Some(ts) = app.primary.map.maybe_get_traffic_signal(i) { // If we're mousing over something, the cursor is on the map. let pt = ctx.canvas.get_cursor_in_map_space().unwrap(); diff --git a/game/src/sandbox/gameplay/freeform.rs b/game/src/sandbox/gameplay/freeform.rs index 8d21d6c66f..1f43f380ba 100644 --- a/game/src/sandbox/gameplay/freeform.rs +++ b/game/src/sandbox/gameplay/freeform.rs @@ -1,4 +1,4 @@ -use crate::app::{App, ShowEverything}; +use crate::app::App; use crate::common::{CityPicker, CommonState}; use crate::edit::EditMode; use crate::game::{ChooseSomething, PopupMsg, PromptInput, State, Transition}; @@ -11,8 +11,8 @@ use map_model::{BuildingID, IntersectionID, Position, NORMAL_LANE_THICKNESS}; use rand::seq::SliceRandom; use rand::Rng; use sim::{ - DontDrawAgents, DrivingGoal, IndividTrip, PersonID, PersonSpec, Scenario, SidewalkSpot, - SpawnTrip, TripEndpoint, TripMode, TripPurpose, TripSpec, + DrivingGoal, IndividTrip, PersonID, PersonSpec, Scenario, SidewalkSpot, SpawnTrip, + TripEndpoint, TripMode, TripPurpose, TripSpec, }; use widgetry::{ lctrl, Btn, Choice, Color, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, Outcome, Panel, @@ -357,14 +357,7 @@ impl State for AgentSpawner { } if ctx.redo_mouseover() { - app.primary.current_selection = app.calculate_current_selection( - ctx, - &DontDrawAgents {}, - &ShowEverything::new(), - false, - true, - true, - ); + app.primary.current_selection = app.mouseover_unzoomed_everything(ctx); if let Some(ID::Intersection(i)) = app.primary.current_selection { if !app.primary.map.get_i(i).is_border() { app.primary.current_selection = None;