camera control and cursor catching

This commit is contained in:
Piotrek
2021-04-15 09:48:19 +02:00
parent b52193df28
commit a95c8f4bf2
3 changed files with 27 additions and 19 deletions
+1 -1
View File
@@ -41,7 +41,7 @@ pub struct App {
bind_group: wgpu::BindGroup, bind_group: wgpu::BindGroup,
// Camera // Camera
camera: Camera, camera: Camera,
camera_controller: CameraController, pub camera_controller: CameraController,
uniforms: Uniforms uniforms: Uniforms
} }
+12 -5
View File
@@ -21,33 +21,38 @@ struct CameraRotation {
pub pitch: f32, // horizontal axis pub pitch: f32, // horizontal axis
pub sensitivity: f32, pub sensitivity: f32,
} }
impl CameraRotation { impl CameraRotation {
fn yaw_rad(&mut self) -> Rad<f32> { Rad::from(Deg(self.yaw)) } fn yaw_rad(&mut self) -> Rad<f32> { Rad::from(Deg(self.yaw)) }
fn pitch_rad(&mut self) -> Rad<f32> { Rad::from(Deg(self.pitch)) } fn pitch_rad(&mut self) -> Rad<f32> { Rad::from(Deg(self.pitch)) }
fn yaw_rot(&mut self) -> Matrix4<f32> { Matrix4::from_angle_y(-self.yaw_rad()) } fn yaw_rot(&mut self) -> Matrix4<f32> { Matrix4::from_angle_y(-self.yaw_rad()) }
fn pitch_rot(&mut self)-> Matrix4<f32> { Matrix4::from_angle_x(-self.pitch_rad()) } fn pitch_rot(&mut self)-> Matrix4<f32> { Matrix4::from_angle_x(-self.pitch_rad()) }
} }
pub struct CameraController { pub struct CameraController {
enabled: bool,
movement: CameraMovement, movement: CameraMovement,
rotation: CameraRotation, rotation: CameraRotation,
} }
impl CameraController { impl CameraController {
pub fn new() -> Self {
pub fn new() -> Self {
let mut movement = CameraMovement::default(); let mut movement = CameraMovement::default();
movement.speed = 2.0; movement.speed = 2.0;
let mut rotation = CameraRotation::default(); let mut rotation = CameraRotation::default();
rotation.sensitivity = 0.2; rotation.sensitivity = 0.2;
Self { enabled: false, movement, rotation }
}
Self { movement, rotation } pub fn set_enabled(&mut self, enabled: bool) {
self.enabled = enabled;
} }
pub fn handle_input(&mut self, event: &Event<()>) -> bool { pub fn handle_input(&mut self, event: &Event<()>) -> bool {
// Abort if not enabled
if !self.enabled { return false; }
match event { match event {
Event::WindowEvent { ref event, .. } => { Event::WindowEvent { ref event, .. } => {
match event { match event {
@@ -88,6 +93,8 @@ impl CameraController {
} }
pub fn update_camera(&mut self, camera: &mut Camera, delta_time: f32) { pub fn update_camera(&mut self, camera: &mut Camera, delta_time: f32) {
// Abort if not enabled
if !self.enabled { return; }
// Rotation // Rotation
let yaw_rot = self.rotation.yaw_rot(); let yaw_rot = self.rotation.yaw_rot();
+13 -12
View File
@@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use winit::{ use winit::{
event::{Event, WindowEvent, KeyboardInput, ElementState, VirtualKeyCode}, event::{Event, WindowEvent, KeyboardInput, MouseButton, ElementState, VirtualKeyCode},
event_loop::{ControlFlow, EventLoop}, event_loop::{ControlFlow, EventLoop},
window::WindowBuilder, window::WindowBuilder,
dpi::LogicalSize dpi::LogicalSize
@@ -45,20 +45,21 @@ fn main() {
// Window event // Window event
Event::WindowEvent { ref event, .. } => { Event::WindowEvent { ref event, .. } => {
match event { match event {
// Window closed // Window closed (ALT+F4,)
WindowEvent::CloseRequested => { WindowEvent::CloseRequested => {
*c = ControlFlow::Exit; *c = ControlFlow::Exit;
} }
// Keyboard input // Escape key
WindowEvent::KeyboardInput { input, .. } => { WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. }, .. } => {
match input { window.set_cursor_grab(false).unwrap();
// Escape key pressed window.set_cursor_visible(true);
KeyboardInput { state: ElementState::Pressed, virtual_keycode: Some(VirtualKeyCode::Escape), .. } => { app.camera_controller.set_enabled(false);
*c = ControlFlow::Exit; }
} // Window clicked
// Other keys 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 // Window resized
WindowEvent::Resized(physical_size) => { WindowEvent::Resized(physical_size) => {