From b8bb2667c23a279d073d7b9a04e4ab30a07b8bce Mon Sep 17 00:00:00 2001 From: Dustin Carlino Date: Sun, 24 Nov 2019 07:21:23 -0800 Subject: [PATCH] click minimap to warp --- game/src/common/minimap.rs | 49 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/game/src/common/minimap.rs b/game/src/common/minimap.rs index 5b4c41f49d..974484bde5 100644 --- a/game/src/common/minimap.rs +++ b/game/src/common/minimap.rs @@ -3,14 +3,57 @@ use crate::ui::UI; use ezgui::{Color, EventCtx, GfxCtx, ScreenPt, ScreenRectangle}; use geom::{Distance, Polygon, Pt2D, Ring}; -pub struct Minimap {} +pub struct Minimap { + dragging: bool, +} impl Minimap { pub fn new() -> Minimap { - Minimap {} + Minimap { dragging: false } } - pub fn event(&mut self, _: &mut UI, _: &mut EventCtx) {} + pub fn event(&mut self, ui: &mut UI, ctx: &mut EventCtx) { + // TODO duplicate some stuff for now, until we figure out what to cache + let square_len = 0.15 * ctx.canvas.window_width; + let top_left = ScreenPt::new( + ctx.canvas.window_width - square_len - 50.0, + ctx.canvas.window_height - square_len - 50.0, + ); + let padding = 10.0; + let inner_rect = ScreenRectangle { + x1: top_left.x + padding, + x2: top_left.x + square_len - padding, + y1: top_left.y + padding, + y2: top_left.y + square_len - padding, + }; + + let mut pt = ctx.canvas.get_cursor_in_screen_space(); + if self.dragging { + if ctx.input.left_mouse_button_released() { + self.dragging = false; + } + // Don't drag out of inner_rect + pt.x = clamp(pt.x, inner_rect.x1, inner_rect.x2); + pt.y = clamp(pt.y, inner_rect.y1, inner_rect.y2); + } else if inner_rect.contains(pt) && ctx.input.left_mouse_button_pressed() { + self.dragging = true; + } else { + return; + } + + let percent_x = (pt.x - inner_rect.x1) / (inner_rect.x2 - inner_rect.x1); + let percent_y = (pt.y - inner_rect.y1) / (inner_rect.y2 - inner_rect.y1); + + let bounds = ui.primary.map.get_bounds(); + let zoom = (square_len - (padding * 2.0)) / (bounds.max_x - bounds.min_x); + + // We're stretching to fit the entire width, so... + let map_x = percent_x * (bounds.max_x - bounds.min_x); + // The y2 on the map that we're currently displaying + let map_y2 = bounds.min_y + (inner_rect.y2 - inner_rect.y1) / zoom; + let map_pt = Pt2D::new(map_x, percent_y * (map_y2 - bounds.min_y)); + ctx.canvas.center_on_map_pt(map_pt); + } pub fn draw(&self, g: &mut GfxCtx, ui: &UI) { if g.canvas.cam_zoom < MIN_ZOOM_FOR_DETAIL {