From e137fcf15cdb67d33a5574bc384b978fd115706b Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 24 Jun 2019 13:26:00 -0700 Subject: [PATCH] refactor mouseover recalculation for the common case --- editor/src/abtest/mod.rs | 12 ++------ editor/src/debug/bus_explorer.rs | 11 ++------ editor/src/debug/mod.rs | 6 ++-- editor/src/edit/mod.rs | 7 +---- editor/src/mission/all_trips.rs | 9 ++---- editor/src/mission/individ_trips.rs | 9 ++---- editor/src/sandbox/mod.rs | 42 ++++------------------------ editor/src/sandbox/route_explorer.rs | 10 ++----- editor/src/sandbox/spawner.rs | 17 ++--------- editor/src/ui.rs | 8 +++++- 10 files changed, 32 insertions(+), 99 deletions(-) diff --git a/editor/src/abtest/mod.rs b/editor/src/abtest/mod.rs index c88cf5565c..5ff8a28b0c 100644 --- a/editor/src/abtest/mod.rs +++ b/editor/src/abtest/mod.rs @@ -4,7 +4,7 @@ pub mod setup; use crate::common::{CommonState, SpeedControls}; use crate::game::{State, Transition}; use crate::render::MIN_ZOOM_FOR_DETAIL; -use crate::ui::{PerMapUI, ShowEverything, UI}; +use crate::ui::{PerMapUI, UI}; use ezgui::{hotkey, Color, EventCtx, EventLoopMode, GeomBatch, GfxCtx, Key, ModalMenu, Text}; use geom::{Circle, Distance, Duration, Line, PolyLine}; use map_model::{Map, LANE_THICKNESS}; @@ -73,12 +73,7 @@ impl State for ABTestMode { ctx.canvas.handle_event(ctx.input); if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } if let Some(t) = self.common.event(ctx, ui, &mut self.menu) { return t; @@ -202,8 +197,7 @@ impl ABTestMode { )); } - ui.primary.current_selection = - ui.recalculate_current_selection(ctx, &ui.primary.sim, &ShowEverything::new(), false); + ui.recalculate_current_selection(ctx); } fn savestate(&mut self, ui: &mut UI) { diff --git a/editor/src/debug/bus_explorer.rs b/editor/src/debug/bus_explorer.rs index d5d14025dd..0390f51bd9 100644 --- a/editor/src/debug/bus_explorer.rs +++ b/editor/src/debug/bus_explorer.rs @@ -1,7 +1,7 @@ use crate::common::CommonState; use crate::game::{State, Transition}; use crate::helpers::ID; -use crate::ui::{ShowEverything, UI}; +use crate::ui::UI; use ezgui::{EventCtx, GfxCtx, Key, Text, WarpingItemSlider}; use geom::Pt2D; use map_model::BusStopID; @@ -47,13 +47,8 @@ impl BusRouteExplorer { impl State for BusRouteExplorer { fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition { if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - // TODO Or use what debug mode is showing? - &ShowEverything::new(), - false, - ); + // TODO Or use what debug mode is showing? + ui.recalculate_current_selection(ctx); } ctx.canvas.handle_event(ctx.input); diff --git a/editor/src/debug/mod.rs b/editor/src/debug/mod.rs index 881a609bf5..3e2983f702 100644 --- a/editor/src/debug/mod.rs +++ b/editor/src/debug/mod.rs @@ -91,7 +91,7 @@ impl State for DebugMode { fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition { if ctx.redo_mouseover() { ui.primary.current_selection = - ui.recalculate_current_selection(ctx, &ui.primary.sim, self, true); + ui.calculate_current_selection(ctx, &ui.primary.sim, self, true); } let mut txt = Text::prompt("Debug Mode"); @@ -174,7 +174,7 @@ impl State for DebugMode { if !self.hidden.is_empty() && self.menu.action("unhide everything") { self.hidden.clear(); ui.primary.current_selection = - ui.recalculate_current_selection(ctx, &ui.primary.sim, self, true); + ui.calculate_current_selection(ctx, &ui.primary.sim, self, true); } } _ => {} @@ -228,7 +228,7 @@ impl State for DebugMode { if changed { ui.primary.current_selection = - ui.recalculate_current_selection(ctx, &ui.primary.sim, self, true); + ui.calculate_current_selection(ctx, &ui.primary.sim, self, true); } } diff --git a/editor/src/edit/mod.rs b/editor/src/edit/mod.rs index 9391022102..ab90633eff 100644 --- a/editor/src/edit/mod.rs +++ b/editor/src/edit/mod.rs @@ -77,12 +77,7 @@ impl State for EditMode { // directly. // TODO Only mouseover lanes and intersections? if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } if let Some(t) = self.common.event(ctx, ui, &mut self.menu) { return t; diff --git a/editor/src/mission/all_trips.rs b/editor/src/mission/all_trips.rs index cdf8f48239..e61462f9b2 100644 --- a/editor/src/mission/all_trips.rs +++ b/editor/src/mission/all_trips.rs @@ -1,7 +1,7 @@ use crate::common::{CommonState, SpeedControls}; use crate::game::{State, Transition}; use crate::mission::trips::{clip_trips, Trip}; -use crate::ui::{ShowEverything, UI}; +use crate::ui::UI; use abstutil::prettyprint_usize; use ezgui::{ hotkey, EventCtx, EventLoopMode, GeomBatch, GfxCtx, Key, ModalMenu, ScreenPt, Slider, Text, @@ -99,12 +99,7 @@ impl State for TripsVisualizer { ctx.canvas.handle_event(ctx.input); if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } let last_time = Duration::parse("23:59:59.9").unwrap(); diff --git a/editor/src/mission/individ_trips.rs b/editor/src/mission/individ_trips.rs index 22e8d71036..6f02794ff8 100644 --- a/editor/src/mission/individ_trips.rs +++ b/editor/src/mission/individ_trips.rs @@ -2,7 +2,7 @@ use crate::common::CommonState; use crate::game::{State, Transition}; use crate::helpers::ID; use crate::mission::trips::{clip_trips, Trip, TripEndpt}; -use crate::ui::{ShowEverything, UI}; +use crate::ui::UI; use ezgui::{hotkey, Color, EventCtx, GfxCtx, ItemSlider, Key, Text}; use geom::{Circle, Distance, Line, Speed}; use map_model::BuildingID; @@ -61,12 +61,7 @@ impl State for TripsVisualizer { ctx.canvas.handle_event(ctx.input); if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } if self.slider.action("quit") { diff --git a/editor/src/sandbox/mod.rs b/editor/src/sandbox/mod.rs index c34d10df48..1450ec83dc 100644 --- a/editor/src/sandbox/mod.rs +++ b/editor/src/sandbox/mod.rs @@ -98,12 +98,7 @@ impl State for SandboxMode { ctx.canvas.handle_event(ctx.input); if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } if let Some(t) = self.common.event(ctx, ui, &mut self.menu) { return t; @@ -170,12 +165,7 @@ impl State for SandboxMode { ui.primary .sim .time_limited_step(&ui.primary.map, dt, Duration::seconds(0.1)); - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } if self.speed.is_paused() { @@ -197,12 +187,7 @@ impl State for SandboxMode { { Some(new_sim) => { ui.primary.sim = new_sim; - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } None => println!("Couldn't load previous savestate {:?}", prev_state), } @@ -215,12 +200,7 @@ impl State for SandboxMode { { Some(new_sim) => { ui.primary.sim = new_sim; - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } None => println!("Couldn't load next savestate {:?}", next_state), } @@ -228,24 +208,14 @@ impl State for SandboxMode { if self.menu.action("step forwards 0.1s") { ui.primary.sim.step(&ui.primary.map, Duration::seconds(0.1)); - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } else if self.menu.action("step forwards 10 mins") { ctx.loading_screen("step forwards 10 minutes", |_, mut timer| { ui.primary .sim .timed_step(&ui.primary.map, Duration::minutes(10), &mut timer); }); - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } else if self.menu.action("jump to specific time") { return Transition::Push(Box::new(JumpingToTime { wizard: Wizard::new(), diff --git a/editor/src/sandbox/route_explorer.rs b/editor/src/sandbox/route_explorer.rs index c6352a63fe..e63ee09b82 100644 --- a/editor/src/sandbox/route_explorer.rs +++ b/editor/src/sandbox/route_explorer.rs @@ -1,7 +1,7 @@ use crate::common::CommonState; use crate::game::{State, Transition}; use crate::render::DrawTurn; -use crate::ui::{ShowEverything, UI}; +use crate::ui::UI; use ezgui::{Color, EventCtx, GfxCtx, Key, Text, WarpingItemSlider}; use geom::{Distance, Polygon, Pt2D}; use map_model::{Traversable, LANE_THICKNESS}; @@ -68,13 +68,7 @@ impl RouteExplorer { impl State for RouteExplorer { fn event(&mut self, ctx: &mut EventCtx, ui: &mut UI) -> Transition { if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - // TODO Or use what debug mode is showing? - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } ctx.canvas.handle_event(ctx.input); diff --git a/editor/src/sandbox/spawner.rs b/editor/src/sandbox/spawner.rs index 6efc2271b5..ff7914a869 100644 --- a/editor/src/sandbox/spawner.rs +++ b/editor/src/sandbox/spawner.rs @@ -125,12 +125,7 @@ impl State for AgentSpawner { ctx.canvas.handle_event(ctx.input); if ctx.redo_mouseover() { - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); } let map = &ui.primary.map; @@ -284,12 +279,7 @@ impl State for AgentSpawner { }; sim.spawn_all_trips(map, &mut Timer::new("spawn trip"), false); sim.step(map, SMALL_DT); - ui.primary.current_selection = ui.recalculate_current_selection( - ctx, - &ui.primary.sim, - &ShowEverything::new(), - false, - ); + ui.recalculate_current_selection(ctx); return Transition::Pop; } @@ -375,6 +365,5 @@ fn spawn_agents_around(i: IntersectionID, ui: &mut UI, ctx: &EventCtx) { sim.spawn_all_trips(map, &mut Timer::throwaway(), false); sim.step(map, SMALL_DT); - ui.primary.current_selection = - ui.recalculate_current_selection(ctx, &ui.primary.sim, &ShowEverything::new(), false); + ui.recalculate_current_selection(ctx); } diff --git a/editor/src/ui.rs b/editor/src/ui.rs index d063ed01f4..93b8c485e9 100644 --- a/editor/src/ui.rs +++ b/editor/src/ui.rs @@ -197,10 +197,16 @@ impl UI { } } + // Assumes some defaults. + pub fn recalculate_current_selection(&mut self, ctx: &EventCtx) { + self.primary.current_selection = + self.calculate_current_selection(ctx, &self.primary.sim, &ShowEverything::new(), false); + } + // 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 recalculate_current_selection( + pub fn calculate_current_selection( &self, ctx: &EventCtx, source: &GetDrawAgents,