fix zoom
authoralfadur
Fri, 22 Mar 2019 21:44:06 +0300
changeset 14709 65c971417780
parent 14708 19358c313ebb
child 14710 946df0bb3b28
fix zoom
rust/hwrunner/src/main.rs
rust/lib-hedgewars-engine/src/render/camera.rs
rust/lib-hedgewars-engine/src/world.rs
--- a/rust/hwrunner/src/main.rs	Fri Mar 22 21:09:17 2019 +0300
+++ b/rust/hwrunner/src/main.rs	Fri Mar 22 21:44:06 2019 +0300
@@ -64,13 +64,10 @@
                 }
                 WindowEvent::MouseInput { button, state, .. } => {
                     if let MouseButton::Right = button {
-                        if let ElementState::Pressed = state {
-                            dragging = true;
-                        } else {
-                            dragging = false;
-                        }
+                        dragging = state == ElementState::Pressed;
                     }
                 }
+
                 WindowEvent::MouseWheel { delta, .. } => {
                     let zoom_change = match delta {
                         MouseScrollDelta::LineDelta(x, y) => y as f32 * 0.1f32,
--- a/rust/lib-hedgewars-engine/src/render/camera.rs	Fri Mar 22 21:09:17 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/render/camera.rs	Fri Mar 22 21:44:06 2019 +0300
@@ -21,6 +21,17 @@
     }
 
     pub fn viewport(&self) -> Rect {
-        Rect::from_size(self.position, self.size)
+        #[inline]
+        fn scale(value: usize, zoom: f32) -> i32 {
+            (value as f32 / zoom / 2.0) as i32
+        }
+        let half_width = scale(self.size.width, self.zoom);
+        let half_height = scale(self.size.height, self.zoom);
+        Rect::from_box(
+            self.position.x - half_width,
+            self.position.x + half_width,
+            self.position.y - half_height,
+            self.position.y + half_height,
+        )
     }
 }
--- a/rust/lib-hedgewars-engine/src/world.rs	Fri Mar 22 21:09:17 2019 +0300
+++ b/rust/lib-hedgewars-engine/src/world.rs	Fri Mar 22 21:44:06 2019 +0300
@@ -99,8 +99,11 @@
     }
 
     pub fn move_camera(&mut self, position_shift: Point, zoom_shift: f32) {
-        self.camera.position += position_shift;
         self.camera.zoom += zoom_shift;
+        self.camera.position += Point::new(
+            (position_shift.x as f32 / self.camera.zoom) as i32,
+            (position_shift.y as f32 / self.camera.zoom) as i32,
+        );
     }
 
     pub fn render(&mut self) {