Skeleton hands can't grasp the tip of tools, but they can refract the

gaze of the living...
This commit is contained in:
Dustin Carlino 2021-08-04 15:56:44 -07:00
parent 78f37e68e9
commit 3bddceb275
2 changed files with 88 additions and 71 deletions

View File

@ -0,0 +1,49 @@
use geom::{Bounds, Pt2D};
use map_gui::render::DrawOptions;
use widgetry::{GfxCtx, ScreenPt, ScreenRectangle};
use crate::app::App;
// TODO We could stylize more by fading the map out, then adding a circle of light with an outline
pub fn draw(g: &mut GfxCtx, app: &App, rect: &ScreenRectangle) {
let zoom = 8.0;
if let Some(pt) = g.canvas.get_cursor_in_map_space() {
// This is some of the math from screen_to_map and center_on_map_pt.
let mut bounds = Bounds::new();
let cam_x = (pt.x() * zoom) - (rect.width() / 2.0);
let cam_y = (pt.y() * zoom) - (rect.height() / 2.0);
// Top-left
bounds.update(Pt2D::new(cam_x / zoom, cam_y / zoom));
// Bottom-right
bounds.update(Pt2D::new(
(rect.width() + cam_x) / zoom,
(rect.height() + cam_y) / zoom,
));
g.fork(
Pt2D::new(bounds.min_x, bounds.min_y),
ScreenPt::new(rect.x1, rect.y1),
zoom,
None,
);
g.enable_clipping(rect.clone());
g.redraw(&app.primary.draw_map.boundary_polygon);
g.redraw(&app.primary.draw_map.draw_all_areas);
g.redraw(&app.primary.draw_map.draw_all_buildings);
let opts = DrawOptions::new();
for obj in app
.primary
.draw_map
.get_renderables_back_to_front(bounds, &app.primary.map)
{
obj.draw(g, app, &opts);
}
g.disable_clipping();
g.unfork();
}
}

View File

@ -1,12 +1,13 @@
mod edit;
mod magnifying;
use geom::{Circle, Distance, Pt2D};
use map_gui::tools::{nice_map_name, CityPicker, PopupMsg};
use map_gui::ID;
use map_model::{LaneType, PathConstraints, Road, RoadID};
use map_model::{LaneType, PathConstraints, Road};
use widgetry::{
lctrl, Cached, Color, Drawable, EventCtx, GeomBatch, GeomBatchStack, GfxCtx,
HorizontalAlignment, Key, Line, Outcome, Panel, State, Text, Toggle, VerticalAlignment, Widget,
lctrl, Color, Drawable, EventCtx, Filler, GeomBatch, GfxCtx, HorizontalAlignment, Key, Line,
Outcome, Panel, ScreenDims, State, Text, Toggle, VerticalAlignment, Widget,
};
use crate::app::{App, Transition};
@ -20,7 +21,7 @@ const GREENWAY: Color = Color::BLUE;
pub struct ExploreMap {
top_panel: Panel,
legend: Panel,
tooltip: Cached<RoadID, (RoadID, Drawable, Drawable)>,
magnifying_glass: Panel,
unzoomed_layer: Drawable,
elevation: bool,
}
@ -32,7 +33,7 @@ impl ExploreMap {
Box::new(ExploreMap {
top_panel: make_top_panel(ctx),
legend: make_legend(ctx, app, false),
tooltip: Cached::new(),
magnifying_glass: make_magnifying_glass(ctx),
unzoomed_layer: make_unzoomed_layer(ctx, app),
elevation: false,
})
@ -44,11 +45,21 @@ impl State<App> for ExploreMap {
ctx.canvas_movement();
if ctx.redo_mouseover() {
let road = match app.mouseover_unzoomed_roads_and_intersections(ctx) {
Some(ID::Road(r)) => Some(r),
_ => None,
};
self.tooltip.update(road, |r| make_tooltip(ctx, app, r));
let mut label = Text::new();
if let Some(ID::Road(r)) = app.mouseover_unzoomed_roads_and_intersections(ctx) {
let road = app.primary.map.get_r(r);
label.add_line(Line(road.get_name(app.opts.language.as_ref())).small_heading());
// TODO Indicate which direction is uphill
label.add_line(Line(format!(
"{}% incline",
(road.percent_incline.abs() * 100.0).round()
)));
} else {
// TODO Jittery panel
}
label.add_line(Line("Click for details").secondary());
let label = label.into_widget(ctx);
self.magnifying_glass.replace(ctx, "label", label);
if self.elevation {
let mut label = Text::new().into_widget(ctx);
@ -72,13 +83,11 @@ impl State<App> for ExploreMap {
}
}
if let Some((r, _, _)) = self.tooltip.value() {
if let Some(pt) = ctx.canvas.get_cursor_in_map_space() {
if ctx.normal_left_click() {
let r = *r;
self.tooltip.clear();
return Transition::Push(Warping::new_state(
ctx,
app.primary.map.get_r(r).center_pts.middle(),
pt,
Some(10.0),
None,
&mut app.primary,
@ -152,21 +161,17 @@ impl State<App> for ExploreMap {
fn draw(&self, g: &mut GfxCtx, app: &App) {
self.top_panel.draw(g);
self.legend.draw(g);
if let Some((_, draw_on_map, draw_tooltip)) = self.tooltip.value() {
g.redraw(draw_on_map);
// Like fork_screenspace, but centered by the cursor
g.fork(Pt2D::new(0.0, 0.0), g.canvas.get_cursor(), 1.0, None);
g.redraw(draw_tooltip);
g.unfork();
}
if g.canvas.cam_zoom < app.opts.min_zoom_for_detail {
g.redraw(&self.unzoomed_layer);
if self.elevation {
if let Some((_, ref draw)) = app.session.elevation_contours.value() {
g.redraw(draw);
}
}
self.magnifying_glass.draw(g);
magnifying::draw(g, app, self.magnifying_glass.rect_of("glass"));
}
}
}
@ -236,7 +241,7 @@ fn make_legend(ctx: &mut EventCtx, app: &App, elevation: bool) -> Panel {
.icon("system/assets/tools/info.svg")
.build_widget(ctx, "about the elevation data")
.centered_vert(),
Text::new()
Text::from(Line("Click for details").secondary())
.into_widget(ctx)
.named("current elevation")
.centered_vert(),
@ -263,54 +268,17 @@ fn legend(ctx: &mut EventCtx, color: Color, label: &str) -> Widget {
])
}
// Returns a batch to draw directly on the map, and another to draw as a tooltip.
fn make_tooltip(ctx: &mut EventCtx, app: &App, r: RoadID) -> (RoadID, Drawable, Drawable) {
let mut map_batch = GeomBatch::new();
let road = app.primary.map.get_r(r);
map_batch.push(
Color::BLACK.alpha(0.5),
road.get_thick_polygon(&app.primary.map),
);
let mut screen_batch = GeomBatch::new();
for (l, _, _) in road.lanes_ltr() {
screen_batch.append(app.primary.draw_map.get_l(l).render(ctx, app));
}
screen_batch.append(app.primary.draw_map.get_r(r).render_center_line(app));
screen_batch = screen_batch.autocrop();
let bounds = screen_batch.get_bounds();
let fit_dims = 300.0;
let zoom = (fit_dims / bounds.width()).min(fit_dims / bounds.height());
screen_batch = screen_batch.scale(zoom);
let label = Text::from_multiline(vec![
Line(road.get_name(app.opts.language.as_ref())).small_heading(),
// TODO Indicate which direction is uphill
Line(format!(
"{}% incline",
(road.percent_incline.abs() * 100.0).round()
)),
])
.render_autocropped(ctx);
screen_batch = GeomBatchStack::vertical(vec![label, screen_batch]).batch();
screen_batch = magnifying_glass(screen_batch);
(r, map_batch.upload(ctx), screen_batch.upload(ctx))
}
fn magnifying_glass(batch: GeomBatch) -> GeomBatch {
let bounds = batch.get_bounds();
// TODO The radius isn't guaranteed to fit...
let radius = Distance::meters(1.3 * bounds.width().max(bounds.height())) / 2.0;
let circle = Circle::new(bounds.center(), radius);
let mut new_batch = GeomBatch::new();
new_batch.push(Color::WHITE, circle.to_polygon());
if let Ok(p) = circle.to_outline(Distance::meters(3.0)) {
new_batch.push(Color::BLACK, p);
}
new_batch.append(batch);
new_batch
fn make_magnifying_glass(ctx: &mut EventCtx) -> Panel {
Panel::new_builder(
Widget::col(vec![
Filler::fixed_dims(ScreenDims::new(300.0, 300.0)).named("glass"),
Text::new().into_widget(ctx).named("label"),
])
.padding(16)
.outline((4.0, Color::BLACK)),
)
.aligned(HorizontalAlignment::LeftInset, VerticalAlignment::TopInset)
.build(ctx)
}
pub fn make_unzoomed_layer(ctx: &mut EventCtx, app: &App) -> Drawable {