diff options
| author | Arne Dußin | 2020-11-02 11:51:18 +0100 |
|---|---|---|
| committer | Arne Dußin | 2020-11-02 11:51:18 +0100 |
| commit | b5603648a4c88585764ac70db9614504b9b57515 (patch) | |
| tree | b67f96ccb06c079f93d2cb871dcb15d2689e9a41 /src | |
| parent | d2bd3e0e20f269c35dc74486af0578958fe21315 (diff) | |
| download | graf_karto-b5603648a4c88585764ac70db9614504b9b57515.tar.gz graf_karto-b5603648a4c88585764ac70db9614504b9b57515.zip | |
Combine zooming in and zooming out into one function
There was a lot of duplicate and hacky code in the zooming functions, so
I made them cleaner while hopefully staying true to the idea.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 13 | ||||
| -rw-r--r-- | src/transform.rs | 74 |
2 files changed, 36 insertions, 51 deletions
diff --git a/src/main.rs b/src/main.rs index f116561..f3838a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,11 +26,14 @@ fn main() { transform.move_by_px((rl.get_mouse_position() - last_mouse_pos).into()); } - // Handle scrolling of the canvas - if rl.get_mouse_wheel_move() > 0 { - transform.try_zoom_in(rl.get_mouse_position().into()); - } else if rl.get_mouse_wheel_move() < 0 { - transform.try_zoom_out(rl.get_mouse_position().into()); + let mouse_wheel_move = rl.get_mouse_wheel_move(); + if mouse_wheel_move != 0 { + // Zoom in for positive and zoom out for negative mouse wheel rotations. + let factor = if mouse_wheel_move > 0 { 1.2 } else { 1. / 1.2 }; + transform.try_zoom( + rl.get_mouse_position().into(), + mouse_wheel_move.abs() as f32 * factor, + ); } editor.update(&rl, &transform); diff --git a/src/transform.rs b/src/transform.rs index d074c03..2e9ea0b 100644 --- a/src/transform.rs +++ b/src/transform.rs @@ -77,59 +77,41 @@ impl Transform { ) } - /* Helper function to make sure the standard zoom factor is always exact. This helps - * normalising zoom levels even when the user zooms in and out extremely often, assuming they - * pass the standard zoom factor. - */ - fn normalise_zoom(&mut self) { - self.pixels_per_m = self.pixels_per_m as u32 as f32; - if self.pixels_per_m < MIN_PIXELS_PER_M { - self.pixels_per_m = MIN_PIXELS_PER_M; - } - - if self.pixels_per_m > STANDARD_PIXELS_PER_M - 5. - && self.pixels_per_m < STANDARD_PIXELS_PER_M + 5. + /// Attempts to zoom the pixels per meter by the amount of factor. + /// + /// # Arguments + /// `factor`: A number greater than one means zooming in, a number less than one means zooming out. What happens when you try to + /// zoom with a negative factor you'll have to figure out yourself. + /// `mouse_pos_px`: Position of the mouse cursor, this time not in meters, but in screen + /// pixels. This will be used to tether zoom on that point. + pub fn try_zoom(&mut self, mouse_pos_px: Vec2<f32>, factor: f32) -> bool { + // Abort zooming when the scale would not be in the min-max-bounds anymore. + let desired_px_per_m = self.pixels_per_m * factor; + if (factor < 1. && desired_px_per_m <= MIN_PIXELS_PER_M) + || (factor > 1. && desired_px_per_m >= MAX_PIXELS_PER_M) { - self.pixels_per_m = STANDARD_PIXELS_PER_M; + return false; } - } - /// 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, 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); + // Save the absolute mouse position in meters 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 + // Make sure the desired scale stays within the bounds and in whole numbers + let desired_px_per_m = if desired_px_per_m < MIN_PIXELS_PER_M { + MIN_PIXELS_PER_M as u32 as f32 + } else if desired_px_per_m > MAX_PIXELS_PER_M { + MAX_PIXELS_PER_M as u32 as f32 } else { - false - } - } - - /// 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, mouse_pos_px: Vec2<f32>) -> bool { - 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(); + desired_px_per_m as u32 as f32 + }; - // Perform tethering (zoom out at mouse position) - self.translation_px += mouse_pos_px - self.point_m_to_px(mouse_pos_m); + /* Adjust to the desired scale and bring the map back to its desired position according to + * the mouse pointer position. + */ + self.pixels_per_m = desired_px_per_m; + self.translation_px += mouse_pos_px - self.point_m_to_px(mouse_pos_m); - true - } else { - false - } + true } /// Move the canvas by the vector in pixels. |
