quick round of refactoring layer code

This commit is contained in:
Dustin Carlino 2020-06-17 10:56:06 -07:00
parent 1d252bbc40
commit 82ba62df29
5 changed files with 26 additions and 72 deletions

View File

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

View File

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

View File

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

View File

@ -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<LayerOutcome> {
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,

View File

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