From e32de26dcb5e1498f2b2fa19cacb593bec518ef4 Mon Sep 17 00:00:00 2001 From: metamuffin Date: Tue, 7 Jan 2025 15:15:19 +0100 Subject: camera controller broken --- client/src/window.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'client/src/window.rs') diff --git a/client/src/window.rs b/client/src/window.rs index 6ee7fa4..ac90f71 100644 --- a/client/src/window.rs +++ b/client/src/window.rs @@ -1,10 +1,12 @@ use crate::state::State; +use glam::Vec3; use log::{info, warn}; use std::net::TcpStream; use winit::{ application::ApplicationHandler, - event::WindowEvent, + event::{DeviceEvent, ElementState, WindowEvent}, event_loop::ActiveEventLoop, + keyboard::{KeyCode, PhysicalKey}, window::{Window, WindowAttributes, WindowId}, }; @@ -47,6 +49,21 @@ impl ApplicationHandler for WindowState { sta.draw(); win.request_redraw(); } + WindowEvent::KeyboardInput { event, .. } => { + if event.repeat { + return; + } + sta.delta.move_dir += match event.physical_key { + PhysicalKey::Code(KeyCode::KeyW) => Vec3::X, + PhysicalKey::Code(KeyCode::KeyS) => Vec3::NEG_X, + PhysicalKey::Code(KeyCode::KeyA) => Vec3::NEG_Z, + PhysicalKey::Code(KeyCode::KeyD) => Vec3::Z, + _ => Vec3::ZERO, + } * match event.state { + ElementState::Pressed => 1., + ElementState::Released => -1., + }; + } WindowEvent::CloseRequested => { event_loop.exit(); } @@ -54,6 +71,22 @@ impl ApplicationHandler for WindowState { } } } + fn device_event( + &mut self, + _event_loop: &ActiveEventLoop, + _device_id: winit::event::DeviceId, + event: winit::event::DeviceEvent, + ) { + if let Some((_win, sta)) = &mut self.window { + match event { + DeviceEvent::MouseMotion { delta } => { + sta.delta.mouse_acc.x += delta.0 as f32; + sta.delta.mouse_acc.y += delta.1 as f32; + } + _ => (), + } + } + } fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { if let Some((_win, sta)) = &mut self.window { if let Err(e) = sta.update() { -- cgit v1.2.3-70-g09d2