From 14db88a6a76a62c0ec83515761bdca54af20e3f5 Mon Sep 17 00:00:00 2001 From: Jakob Meier Date: Sat, 25 Feb 2023 20:31:12 +0100 Subject: [PATCH] Added mouse-wheel camera move gesture --- src/screens/build.rs | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/screens/build.rs b/src/screens/build.rs index 7c7a2fe..924b0c6 100644 --- a/src/screens/build.rs +++ b/src/screens/build.rs @@ -63,7 +63,8 @@ pub struct BuildScreen { grid: HashMap, cam: Camera, show_inv: bool, - inv: Inventory + inv: Inventory, + mouse_position: Option<(f32, f32)> } impl BuildScreen { pub fn new() -> Self { @@ -80,7 +81,8 @@ impl BuildScreen { grid: HashMap::new(), cam: Camera::new(), show_inv: false, - inv: Inventory::new() + inv: Inventory::new(), + mouse_position: None }; this.grid.insert(Pos3::new(0, 0, 0), (Block::default(), Direction::North)); @@ -396,10 +398,10 @@ impl GameComponent for BuildScreen { // keyboard control if is_key_down(KeyCode::Down) { - self.cam.center.y += 1.0 * (1.0/self.cam.scale); + self.cam.center.y -= 1.0 * (1.0/self.cam.scale); } if is_key_down(KeyCode::Up) { - self.cam.center.y -= 1.0 * (1.0/self.cam.scale); + self.cam.center.y += 1.0 * (1.0/self.cam.scale); } if is_key_down(KeyCode::Left) { self.cam.center.x += 1.0 * (1.0/self.cam.scale); @@ -407,6 +409,24 @@ impl GameComponent for BuildScreen { if is_key_down(KeyCode::Right) { self.cam.center.x -= 1.0 * (1.0/self.cam.scale); } + if is_key_down(KeyCode::C) { + self.cam.center = CVec2::new(0.0, 0.0); + } + if is_mouse_button_down(MouseButton::Middle) { + let (nx, ny) = mouse_position(); + if let Some((ox, oy)) = self.mouse_position { + + let dx = nx - ox; + let dy = ny - oy; + + self.cam.center.x += dx; + self.cam.center.y += dy; + } + self.mouse_position = Some((nx, ny)); + } + if is_mouse_button_released(MouseButton::Middle) { + self.mouse_position = None; + } // change block direction if is_key_pressed(KeyCode::W) { -- 2.38.5