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,
// Camera
camera: Camera,
camera_controller: CameraController,
pub camera_controller: CameraController,
uniforms: Uniforms
}
+13 -6
View File
@@ -21,33 +21,38 @@ struct CameraRotation {
pub pitch: f32, // horizontal axis
pub sensitivity: f32,
}
impl CameraRotation {
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 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()) }
}
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();
+13 -12
View File
@@ -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) => {