aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorArne Dußin2020-11-02 00:53:18 +0100
committerArne Dußin2020-11-02 00:53:18 +0100
commit81b531aaec7ad12b138dbf1bd2c24d2bd4352024 (patch)
treeafbc9a7671a0b15e295ce9d65dc33b009f3cd174 /src
parenta3513e6ba22a00899b6c63ea7ce768e2f9fcd462 (diff)
downloadgraf_karto-81b531aaec7ad12b138dbf1bd2c24d2bd4352024.tar.gz
graf_karto-81b531aaec7ad12b138dbf1bd2c24d2bd4352024.zip
Tether on zoom
Diffstat (limited to 'src')
-rw-r--r--src/main.rs4
-rw-r--r--src/transform.rs19
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