Skip to content

Commit

Permalink
Mkirk/switch buildings (#397)
Browse files Browse the repository at this point in the history
for #393
  • Loading branch information
michaelkirk authored Nov 21, 2020
1 parent 29da713 commit b076165
Showing 1 changed file with 51 additions and 27 deletions.
78 changes: 51 additions & 27 deletions game/src/devtools/fifteen_min/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -15,6 +16,7 @@ use widgetry::{
use self::isochrone::Isochrone;
use crate::app::App;
use crate::game::Transition;
use crate::helpers::ID;

mod isochrone;

Expand All @@ -36,45 +38,39 @@ impl Viewer {
pub fn new(ctx: &mut EventCtx, app: &App, start: BuildingID) -> Box<dyn State<App>> {
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<App> 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" => {
Expand All @@ -94,3 +90,31 @@ impl State<App> 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)
}

0 comments on commit b076165

Please sign in to comment.