From 5afea588bb209b07290ab9c44c0df8f2f903124b Mon Sep 17 00:00:00 2001 From: Wynd Date: Wed, 6 Nov 2024 13:18:54 +0200 Subject: [PATCH] Movement limits --- src/controller.rs | 27 ++++++++++++++++++++------- src/state.rs | 3 +-- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index e2978e1..b736d13 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,7 +1,6 @@ -use cgmath::Point3; use winit::event::{MouseButton, MouseScrollDelta, WindowEvent}; -use crate::camera::{Camera, DEFAULT_CAMERA_ZOOM}; +use crate::camera::Camera; const MAX_ZOOM_LEVEL: f32 = 15.0; @@ -11,6 +10,7 @@ pub struct CameraController { cursor_start: (f32, f32), movement: (f32, f32), is_moving: bool, + movement_level: (f32, f32), zoom_level: f32, } @@ -22,6 +22,7 @@ impl CameraController { cursor_start: (0.0, 0.0), movement: (0.0, 0.0), is_moving: false, + movement_level: (0.0, 0.0), zoom_level: 1.0, } } @@ -44,7 +45,7 @@ impl CameraController { self.cursor_start = (position.x as f32, position.y as f32); } - self.movement = match self.is_moving { + let (x, y) = match self.is_moving { true => { let x = -self.cursor_start.0 + position.x as f32; let x = x * 0.000001; @@ -69,6 +70,8 @@ impl CameraController { } }; + self.movement = (x, y); + true } WindowEvent::MouseWheel { delta, .. } => match delta { @@ -99,11 +102,21 @@ impl CameraController { camera.eye -= forward_norm * self.speed; } - camera.eye.x += self.movement.0; - camera.eye.y += self.movement.1; + self.movement_level.0 += self.movement.0; + self.movement_level.1 += self.movement.1; - camera.target.x += self.movement.0; - camera.target.y += self.movement.1; + if self.movement_level.0 > -1.0 && self.movement_level.0 < 1.0 { + camera.eye.x += self.movement.0; + camera.target.x += self.movement.0; + } + + if self.movement_level.1 > -1.0 && self.movement_level.1 < 1.0 { + camera.eye.y += self.movement.1; + camera.target.y += self.movement.1; + } + + self.movement_level.0 = self.movement_level.0.clamp(-1.0, 1.0); + self.movement_level.1 = self.movement_level.1.clamp(-1.0, 1.0); self.forward = 0.0; } diff --git a/src/state.rs b/src/state.rs index 910d736..d020d81 100644 --- a/src/state.rs +++ b/src/state.rs @@ -4,8 +4,7 @@ use pollster::FutureExt; use wgpu::util::DeviceExt; use winit::{ event::{ElementState, WindowEvent}, - keyboard::{Key, KeyCode, NamedKey}, - platform::modifier_supplement::KeyEventExtModifierSupplement, + keyboard::Key, window::Window, };