refactor mouseover recalculation for the common case

This commit is contained in:
Dustin Carlino 2019-06-24 13:26:00 -07:00
parent 17460b1deb
commit e137fcf15c
10 changed files with 32 additions and 99 deletions

View File

@ -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) {

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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();

View File

@ -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") {

View File

@ -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(),

View File

@ -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);

View File

@ -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);
}

View File

@ -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<ID> 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,