diff --git a/game/src/layer/bus.rs b/game/src/layer/bus.rs index 51dcf931f8..6a0a160df1 100644 --- a/game/src/layer/bus.rs +++ b/game/src/layer/bus.rs @@ -3,7 +3,7 @@ use crate::common::ColorDiscrete; use crate::layer::{Layer, LayerOutcome}; use ezgui::{ hotkey, Btn, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, - Line, Outcome, Text, TextExt, VerticalAlignment, Widget, + Line, Text, TextExt, VerticalAlignment, Widget, }; use geom::{Circle, Distance, Pt2D, Time}; use map_model::{BusRouteID, PathConstraints, PathRequest, PathStep}; @@ -34,17 +34,7 @@ impl Layer for ShowBusRoute { *self = ShowBusRoute::new(ctx, app, self.route); } - self.composite.align_above(ctx, minimap); - match self.composite.event(ctx) { - Some(Outcome::Clicked(x)) => match x.as_ref() { - "close" => { - return Some(LayerOutcome::Close); - } - _ => unreachable!(), - }, - None => {} - } - None + Layer::simple_event(ctx, minimap, &mut self.composite) } fn draw(&self, g: &mut GfxCtx, app: &App) { if g.canvas.cam_zoom < app.opts.min_zoom_for_detail { diff --git a/game/src/layer/elevation.rs b/game/src/layer/elevation.rs index 9226b8038c..ad5105ab46 100644 --- a/game/src/layer/elevation.rs +++ b/game/src/layer/elevation.rs @@ -3,7 +3,7 @@ use crate::common::{ColorLegend, ColorNetwork}; use crate::layer::{Layer, LayerOutcome}; use ezgui::{ hotkey, Btn, Color, Composite, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Key, - Outcome, TextExt, VerticalAlignment, Widget, + TextExt, VerticalAlignment, Widget, }; use geom::{ArrowCap, Distance, PolyLine}; @@ -23,17 +23,7 @@ impl Layer for Elevation { _: &mut App, minimap: &Composite, ) -> Option { - self.composite.align_above(ctx, minimap); - match self.composite.event(ctx) { - Some(Outcome::Clicked(x)) => match x.as_ref() { - "close" => { - return Some(LayerOutcome::Close); - } - _ => unreachable!(), - }, - None => {} - } - None + Layer::simple_event(ctx, minimap, &mut self.composite) } fn draw(&self, g: &mut GfxCtx, app: &App) { self.composite.draw(g); diff --git a/game/src/layer/map.rs b/game/src/layer/map.rs index a47eefbc26..efb5b548a6 100644 --- a/game/src/layer/map.rs +++ b/game/src/layer/map.rs @@ -5,7 +5,7 @@ use crate::layer::{Layer, LayerOutcome}; use abstutil::Counter; use ezgui::{ hotkey, Btn, Color, Composite, Drawable, EventCtx, GfxCtx, HorizontalAlignment, Key, Line, - Outcome, Text, TextExt, VerticalAlignment, Widget, + Text, TextExt, VerticalAlignment, Widget, }; use geom::{Distance, Time}; use map_model::LaneType; @@ -32,17 +32,7 @@ impl Layer for BikeNetwork { *self = BikeNetwork::new(ctx, app); } - self.composite.align_above(ctx, minimap); - match self.composite.event(ctx) { - Some(Outcome::Clicked(x)) => match x.as_ref() { - "close" => { - return Some(LayerOutcome::Close); - } - _ => unreachable!(), - }, - None => {} - } - None + Layer::simple_event(ctx, minimap, &mut self.composite) } fn draw(&self, g: &mut GfxCtx, app: &App) { self.composite.draw(g); @@ -166,17 +156,7 @@ impl Layer for Static { _: &mut App, minimap: &Composite, ) -> Option { - self.composite.align_above(ctx, minimap); - match self.composite.event(ctx) { - Some(Outcome::Clicked(x)) => match x.as_ref() { - "close" => { - return Some(LayerOutcome::Close); - } - _ => unreachable!(), - }, - None => {} - } - None + Layer::simple_event(ctx, minimap, &mut self.composite) } fn draw(&self, g: &mut GfxCtx, app: &App) { self.composite.draw(g); diff --git a/game/src/layer/mod.rs b/game/src/layer/mod.rs index d46832da80..37db0c964f 100644 --- a/game/src/layer/mod.rs +++ b/game/src/layer/mod.rs @@ -15,9 +15,6 @@ use ezgui::{hotkey, Btn, Composite, EventCtx, GfxCtx, Key, Line, Outcome, TextEx // TODO Good ideas in // https://towardsdatascience.com/top-10-map-types-in-data-visualization-b3a80898ea70 -// TODO Easy refactoring: -// - boilerplate for event and draw given Composite, unzoomed, zoomed -// - Standard header with the icon pub trait Layer { fn name(&self) -> Option<&'static str>; fn event( @@ -32,6 +29,23 @@ pub trait Layer { fn draw_minimap(&self, g: &mut GfxCtx); } +impl dyn Layer { + fn simple_event( + ctx: &mut EventCtx, + minimap: &Composite, + composite: &mut Composite, + ) -> Option { + composite.align_above(ctx, minimap); + match composite.event(ctx) { + Some(Outcome::Clicked(x)) => match x.as_ref() { + "close" => Some(LayerOutcome::Close), + _ => unreachable!(), + }, + None => None, + } + } +} + // TODO Just return a bool for closed? Less readable... pub enum LayerOutcome { Close, diff --git a/game/src/layer/traffic.rs b/game/src/layer/traffic.rs index e5428295e3..bd9bee0304 100644 --- a/game/src/layer/traffic.rs +++ b/game/src/layer/traffic.rs @@ -32,17 +32,7 @@ impl Layer for Backpressure { *self = Backpressure::new(ctx, app); } - self.composite.align_above(ctx, minimap); - match self.composite.event(ctx) { - Some(Outcome::Clicked(x)) => match x.as_ref() { - "close" => { - return Some(LayerOutcome::Close); - } - _ => unreachable!(), - }, - None => {} - } - None + Layer::simple_event(ctx, minimap, &mut self.composite) } fn draw(&self, g: &mut GfxCtx, app: &App) { self.composite.draw(g); @@ -488,17 +478,7 @@ impl Layer for TrafficJams { *self = TrafficJams::new(ctx, app); } - self.composite.align_above(ctx, minimap); - match self.composite.event(ctx) { - Some(Outcome::Clicked(x)) => match x.as_ref() { - "close" => { - return Some(LayerOutcome::Close); - } - _ => unreachable!(), - }, - None => {} - } - None + Layer::simple_event(ctx, minimap, &mut self.composite) } fn draw(&self, g: &mut GfxCtx, app: &App) { self.composite.draw(g);