From 81b531aaec7ad12b138dbf1bd2c24d2bd4352024 Mon Sep 17 00:00:00 2001 From: Arne Dußin Date: Mon, 2 Nov 2020 00:53:18 +0100 Subject: Tether on zoom --- src/transform.rs | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src/transform.rs') diff --git a/src/transform.rs b/src/transform.rs index ca359f0..b0b0e0a 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -96,11 +96,17 @@ impl Transform { /// Attempts to zoom in a step and return true. /// If the maximum zoom is reached, this function changes nothing and returns false. - pub fn try_zoom_in(&mut self) -> bool { - // TODO: Zoom in on mouse pointer position + pub fn try_zoom_in(&mut self, mouse_pos_px: Vec2) -> bool { if self.pixels_per_m < MAX_PIXELS_PER_M { + // Save the absolute mouse position for tethering later + let mouse_pos_m = self.point_px_to_m(mouse_pos_px); + self.pixels_per_m *= 1.2; self.normalise_zoom(); + + // Perform tethering (zoom in on mouse position) + self.translation_px += mouse_pos_px - self.point_m_to_px(mouse_pos_m); + true } else { false @@ -109,11 +115,18 @@ impl Transform { /// Attempts to zoom out a step and return true. /// If the minimum zoom is reached, this function changes nothing and returns false. - pub fn try_zoom_out(&mut self) -> bool { + pub fn try_zoom_out(&mut self, mouse_pos_px: Vec2) -> bool { // TODO: Zoom out at mouse pointer position if self.pixels_per_m > MIN_PIXELS_PER_M { + // Save the absolute mouse position for tethering later + let mouse_pos_m = self.point_px_to_m(mouse_pos_px); + self.pixels_per_m /= 1.2; self.normalise_zoom(); + + // Perform tethering (zoom out at mouse position) + self.translation_px += mouse_pos_px - self.point_m_to_px(mouse_pos_m); + true } else { false -- cgit v1.2.3-70-g09d2