From a95c8f4bf2069ec237b415969ada01dfbb666a53 Mon Sep 17 00:00:00 2001 From: Piotrek Date: Thu, 15 Apr 2021 09:48:19 +0200 Subject: [PATCH] camera control and cursor catching --- src/app.rs | 2 +- src/camera_controller.rs | 19 +++++++++++++------ src/main.rs | 25 +++++++++++++------------ 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/src/app.rs b/src/app.rs index dad7791..84c6df0 100644 --- a/src/app.rs +++ b/src/app.rs @@ -41,7 +41,7 @@ pub struct App { bind_group: wgpu::BindGroup, // Camera camera: Camera, - camera_controller: CameraController, + pub camera_controller: CameraController, uniforms: Uniforms } diff --git a/src/camera_controller.rs b/src/camera_controller.rs index 36d6e11..b158618 100644 --- a/src/camera_controller.rs +++ b/src/camera_controller.rs @@ -21,33 +21,38 @@ struct CameraRotation { pub pitch: f32, // horizontal axis pub sensitivity: f32, } - impl CameraRotation { - fn yaw_rad(&mut self) -> Rad { Rad::from(Deg(self.yaw)) } fn pitch_rad(&mut self) -> Rad { Rad::from(Deg(self.pitch)) } - fn yaw_rot(&mut self) -> Matrix4 { Matrix4::from_angle_y(-self.yaw_rad()) } fn pitch_rot(&mut self)-> Matrix4 { Matrix4::from_angle_x(-self.pitch_rad()) } } + pub struct CameraController { + enabled: bool, movement: CameraMovement, rotation: CameraRotation, } impl CameraController { - pub fn new() -> Self { + pub fn new() -> Self { let mut movement = CameraMovement::default(); movement.speed = 2.0; let mut rotation = CameraRotation::default(); rotation.sensitivity = 0.2; - - Self { movement, rotation } + Self { enabled: false, movement, rotation } + } + + pub fn set_enabled(&mut self, enabled: bool) { + self.enabled = enabled; } pub fn handle_input(&mut self, event: &Event<()>) -> bool { + // Abort if not enabled + if !self.enabled { return false; } + match event { Event::WindowEvent { ref event, .. } => { match event { @@ -88,6 +93,8 @@ impl CameraController { } pub fn update_camera(&mut self, camera: &mut Camera, delta_time: f32) { + // Abort if not enabled + if !self.enabled { return; } // Rotation let yaw_rot = self.rotation.yaw_rot(); diff --git a/src/main.rs b/src/main.rs index f914360..bb9ccbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::sync::Arc; use winit::{ - event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode}, + event::{Event, WindowEvent, KeyboardInput, MouseButton, ElementState, VirtualKeyCode}, event_loop::{ControlFlow, EventLoop}, window::WindowBuilder, dpi::LogicalSize @@ -45,20 +45,21 @@ fn main() { // Window event Event::WindowEvent { ref event, .. } => { match event { - // Window closed + // Window closed (ALT+F4,) WindowEvent::CloseRequested => { *c = ControlFlow::Exit; } - // Keyboard input - WindowEvent::KeyboardInput { input, .. } => { - match input { - // Escape key pressed - KeyboardInput { state: ElementState::Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. } => { - *c = ControlFlow::Exit; - } - // Other keys - _ => () - } + // Escape key + WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. }, .. } => { + window.set_cursor_grab(false).unwrap(); + window.set_cursor_visible(true); + app.camera_controller.set_enabled(false); + } + // Window clicked + WindowEvent::MouseInput { state: ElementState::Pressed, button: MouseButton::Left, .. } => { + window.set_cursor_grab(true).unwrap(); + window.set_cursor_visible(false); + app.camera_controller.set_enabled(true); } // Window resized WindowEvent::Resized(physical_size) => {