diff options
| -rw-r--r-- | src/main.rs | 4 | ||||
| -rw-r--r-- | src/transform.rs | 19 |
2 files changed, 18 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index 9ba63e0..d44131b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,9 +27,9 @@ fn main() { // Handle scrolling of the canvas if rl.get_mouse_wheel_move() > 0 { - transform.try_zoom_in(); + transform.try_zoom_in(rl.get_mouse_position().into()); } else if rl.get_mouse_wheel_move() < 0 { - transform.try_zoom_out(); + transform.try_zoom_out(rl.get_mouse_position().into()); } editor.update(&rl, &transform); 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<f32>) -> 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<f32>) -> 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 |
