From 851237bed52a03e87fb8af77be5409046ce6f44f Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Mon, 15 Jun 2020 14:25:04 -0700 Subject: [PATCH] clamp minimap recentering a bit better [rebuild] --- .github/workflows/main.yml | 2 +- game/src/common/minimap.rs | 21 ++++++++++++++------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8aa8368fdb..1bf99c03b9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: uses: actions/upload-artifact@v2 with: name: abst_windows - path: abst_windows + path: abst_windows.zip build-macos: runs-on: macos-latest if: "contains(github.event.head_commit.message, '[rebuild]')" diff --git a/game/src/common/minimap.rs b/game/src/common/minimap.rs index e5e5efd9e2..31adaed48a 100644 --- a/game/src/common/minimap.rs +++ b/game/src/common/minimap.rs @@ -44,7 +44,7 @@ impl Minimap { offset_y: 0.0, }; if m.zoomed { - m.recenter(ctx); + m.recenter(ctx, app); } m } @@ -72,13 +72,19 @@ impl Minimap { self.offset_y = map_center.y() * self.zoom - pct_y * inner_rect.height(); } - fn recenter(&mut self, ctx: &EventCtx) { + fn recenter(&mut self, ctx: &EventCtx, app: &App) { // Recenter the minimap on the screen bounds let map_center = ctx.canvas.center_to_map_pt(); let rect = self.composite.rect_of("minimap"); - self.offset_x = map_center.x() * self.zoom - rect.width() / 2.0; - self.offset_y = map_center.y() * self.zoom - rect.height() / 2.0; - // TODO Adjust to not go out of the map boundary + let off_x = map_center.x() * self.zoom - rect.width() / 2.0; + let off_y = map_center.y() * self.zoom - rect.height() / 2.0; + + // Don't go out of bounds. + let bounds = app.primary.map.get_bounds(); + // TODO For boundaries without rectangular shapes, it'd be even nicer to clamp to the + // boundary. + self.offset_x = off_x.max(0.0).min(bounds.max_x * self.zoom - rect.width()); + self.offset_y = off_y.max(0.0).min(bounds.max_y * self.zoom - rect.height()); } pub fn event(&mut self, ctx: &mut EventCtx, app: &mut App) -> Option { @@ -92,10 +98,11 @@ impl Minimap { self.composite = make_minimap_panel(ctx, app, self.zoom_lvl); if just_zoomed_in { - self.recenter(ctx); + self.recenter(ctx, app); } } else if self.zoomed && !self.dragging { // If either corner of the cursor is out of bounds on the minimap, recenter. + // TODO This means clicking the pan buttons while along the boundary won't work. let mut ok = true; for pt in vec![ ScreenPt::new(0.0, 0.0), @@ -108,7 +115,7 @@ impl Minimap { } } if !ok { - self.recenter(ctx); + self.recenter(ctx, app); } }