From b076165e0b3a8c981da403ce1f344c54eb8df5d6 Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Sat, 21 Nov 2020 12:23:37 -0800 Subject: [PATCH] Mkirk/switch buildings (#397) for #393 --- game/src/devtools/fifteen_min/mod.rs | 78 ++++++++++++++++++---------- 1 file changed, 51 insertions(+), 27 deletions(-) diff --git a/game/src/devtools/fifteen_min/mod.rs b/game/src/devtools/fifteen_min/mod.rs index 7f501c2275..b3c5ca1648 100644 --- a/game/src/devtools/fifteen_min/mod.rs +++ b/game/src/devtools/fifteen_min/mod.rs @@ -6,7 +6,8 @@ use rand::seq::SliceRandom; -use map_model::BuildingID; +use geom::Pt2D; +use map_model::{Building, BuildingID}; use widgetry::{ Btn, Color, Drawable, EventCtx, GeomBatch, GfxCtx, HorizontalAlignment, Line, Outcome, Panel, RewriteColor, State, Text, VerticalAlignment, Widget, @@ -15,6 +16,7 @@ use widgetry::{ use self::isochrone::Isochrone; use crate::app::App; use crate::game::Transition; +use crate::helpers::ID; mod isochrone; @@ -36,45 +38,39 @@ impl Viewer { pub fn new(ctx: &mut EventCtx, app: &App, start: BuildingID) -> Box> { let start = app.primary.map.get_b(start); let isochrone = Isochrone::new(ctx, app, start.id); - - let mut rows = Vec::new(); - rows.push(Widget::row(vec![ - Line("15-minute neighborhood explorer") - .small_heading() - .draw(ctx), - Btn::close(ctx), - ])); - let mut txt = Text::from_all(vec![ - Line("Starting from: ").secondary(), - Line(&start.address), - ]); - for (amenity, buildings) in isochrone.amenities_reachable.borrow() { - txt.add(Line(format!("{}: {}", amenity, buildings.len()))); - } - rows.push(txt.draw(ctx)); - - let panel = Panel::new(Widget::col(rows)) - .aligned(HorizontalAlignment::Center, VerticalAlignment::Top) - .build(ctx); - // Draw a star on the start building. - let highlight_start = GeomBatch::load_svg(ctx.prerender, "system/assets/tools/star.svg") - .centered_on(start.polygon.center()) - .color(RewriteColor::ChangeAll(Color::YELLOW)); + let highlight_start_batch = draw_star(ctx, start.polygon.center()); + let highlight_start = ctx.upload(highlight_start_batch); + let panel = build_panel(ctx, start, &isochrone); Box::new(Viewer { panel, - highlight_start: ctx.upload(highlight_start), + highlight_start: highlight_start, isochrone, }) } } impl State for Viewer { - fn event(&mut self, ctx: &mut EventCtx, _: &mut App) -> Transition { + fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Transition { // Allow panning and zooming ctx.canvas_movement(); + if ctx.input.left_mouse_button_pressed() { + if let Some(ID::Building(start)) = app.mouseover_unzoomed_buildings(ctx) { + let start = app.primary.map.get_b(start); + let isochrone = Isochrone::new(ctx, app, start.id); + // Draw a star on the start building. + let highlight_start_batch = draw_star(ctx, start.polygon.center()); + let highlight_start = ctx.upload(highlight_start_batch); + let panel = build_panel(ctx, start, &isochrone); + + self.highlight_start = highlight_start; + self.panel = panel; + self.isochrone = isochrone; + } + } + match self.panel.event(ctx) { Outcome::Clicked(x) => match x.as_ref() { "close" => { @@ -94,3 +90,31 @@ impl State for Viewer { self.panel.draw(g); } } + +fn draw_star(ctx: &mut EventCtx, center: Pt2D) -> GeomBatch { + GeomBatch::load_svg(ctx.prerender, "system/assets/tools/star.svg") + .centered_on(center) + .color(RewriteColor::ChangeAll(Color::BLACK)) +} + +fn build_panel(ctx: &mut EventCtx, start: &Building, isochrone: &Isochrone) -> Panel { + let mut rows = Vec::new(); + rows.push(Widget::row(vec![ + Line("15-minute neighborhood explorer") + .small_heading() + .draw(ctx), + Btn::close(ctx), + ])); + let mut txt = Text::from_all(vec![ + Line("Starting from: ").secondary(), + Line(&start.address), + ]); + for (amenity, buildings) in isochrone.amenities_reachable.borrow() { + txt.add(Line(format!("{}: {}", amenity, buildings.len()))); + } + rows.push(txt.draw(ctx)); + + Panel::new(Widget::col(rows)) + .aligned(HorizontalAlignment::Center, VerticalAlignment::Top) + .build(ctx) +}