From 2bc6898b15a3d2d32d31ddba1164919f97d93346 Mon Sep 17 00:00:00 2001 From: Wynd Date: Sun, 10 Nov 2024 11:11:16 +0200 Subject: [PATCH] Hiding the cursor while dragging the screen --- src/controller.rs | 12 ++++++++++-- src/state.rs | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/controller.rs b/src/controller.rs index 4027a7f..36e54ba 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use winit::{ event::{ElementState, MouseButton, MouseScrollDelta, WindowEvent}, keyboard::{Key, NamedKey}, @@ -8,13 +10,15 @@ use crate::camera::Camera; const MAX_ZOOM_LEVEL: f32 = 15.0; pub struct CameraController { + window: Arc, + zoom_speed: f32, movement_speed: f32, zoom: f32, cursor_start: (f32, f32), movement: (f32, f32), - is_moving: bool, + pub is_moving: bool, is_ctrl_pressed: bool, movement_total: (f32, f32), @@ -22,8 +26,10 @@ pub struct CameraController { } impl CameraController { - pub fn new(zoom_speed: f32, movement_speed: f32) -> Self { + pub fn new(zoom_speed: f32, movement_speed: f32, window: Arc) -> Self { Self { + window, + zoom_speed, movement_speed, @@ -43,10 +49,12 @@ impl CameraController { WindowEvent::MouseInput { state, button, .. } => { if state.is_pressed() && *button == MouseButton::Left { self.is_moving = true; + self.window.set_cursor_visible(false); return true; } else { self.is_moving = false; + self.window.set_cursor_visible(true); self.movement = (0.0, 0.0); } false diff --git a/src/state.rs b/src/state.rs index 553c669..f2e4701 100644 --- a/src/state.rs +++ b/src/state.rs @@ -118,7 +118,7 @@ impl<'a> State<'a> { }; let (camera, camera_info) = Camera::new(&device); - let camera_controller = CameraController::new(ZOOM_SPEED, MOVE_SPEED); + let camera_controller = CameraController::new(ZOOM_SPEED, MOVE_SPEED, window.clone()); let (spotlight, spotlight_info) = Spotlight::new(&device, window.clone());