update winit

This commit is contained in:
aiden 2023-06-03 20:07:35 +01:00
parent a12183fd3c
commit 095cca9fe7
Signed by: aiden
GPG Key ID: EFA9C74AEBF806E0
4 changed files with 29 additions and 21 deletions

@ -11,4 +11,4 @@ cgmath = "0.18.0"
futures = "0.3.28"
tobj = "4.0.0"
wgpu = "0.16.0"
winit = "0.28.4"
winit = { path = "../winit" }

@ -1,4 +1,4 @@
use winit::event::{VirtualKeyCode, ElementState};
use winit::{event::ElementState, keyboard::KeyCode};
#[derive(Debug)]
pub struct Input {
@ -28,25 +28,25 @@ impl Input {
};
}
pub fn process_key(&mut self, key: VirtualKeyCode, state: ElementState) {
pub fn process_key(&mut self, key: KeyCode, state: ElementState) {
let amount = if state == ElementState::Pressed { 1.0 } else { 0.0 };
match key {
VirtualKeyCode::I | VirtualKeyCode::Up => {
KeyCode::KeyI => {
self.amount_forward = amount;
}
VirtualKeyCode::J | VirtualKeyCode::Left => {
KeyCode::KeyJ => {
self.amount_left = amount;
}
VirtualKeyCode::K | VirtualKeyCode::Down => {
KeyCode::KeyK => {
self.amount_backward = amount;
}
VirtualKeyCode::L | VirtualKeyCode::Right => {
KeyCode::KeyL => {
self.amount_right = amount;
}
VirtualKeyCode::Space => {
KeyCode::Space => {
self.amount_up = amount;
}
VirtualKeyCode::Semicolon => {
KeyCode::Semicolon => {
self.amount_down = amount;
}
_ => (),

@ -45,7 +45,8 @@ use {
winit::{
event_loop::{ControlFlow, EventLoop},
window::WindowBuilder,
event::{VirtualKeyCode, ElementState, WindowEvent}
event::{ElementState, WindowEvent},
keyboard::KeyCode,
},
};
@ -59,7 +60,7 @@ use state::State;
fn handle_window_event(state: &mut State, event: WindowEvent) -> ControlFlow {
use WindowEvent::{*, KeyboardInput as KeyboardInputEvent};
use winit::event::{KeyboardInput, MouseButton};
use winit::event::{KeyEvent, MouseButton};
match event {
MouseInput { state: elem_state, button, .. } => {
@ -86,15 +87,15 @@ fn handle_window_event(state: &mut State, event: WindowEvent) -> ControlFlow {
}
KeyboardInputEvent {
input:
KeyboardInput {
virtual_keycode: Some(key),
event:
KeyEvent {
physical_key: key,
state: elem_state,
..
},
..
} => match key {
VirtualKeyCode::Escape if elem_state == ElementState::Pressed => {
KeyCode::Escape if elem_state == ElementState::Pressed => {
// toggle fullscreen
state.set_fullscreen(!state.is_fullscreen());
}
@ -116,13 +117,14 @@ fn real_main() -> Result<(), &'static str> {
.map_err(|_| "failed to create window")?;
let window_id = window.id();
let mut state = State::new(window, Input::new(1.0, 7368.0))?;
let mut state = State::new(window, Input::new(1.0, 9.21 * 800.0))?;
let mut total_elapsed = 0.0;
let mut frames = 0;
let mut prev_render = Instant::now();
//let mut c = 0.0;
event_loop.run(move |event, _, flow| {
use winit::event::{Event::*, DeviceEvent::MouseMotion};
*flow = ControlFlow::Poll;
@ -136,7 +138,13 @@ fn real_main() -> Result<(), &'static str> {
DeviceEvent {
event: MouseMotion { delta, }, ..
} if state.is_focused() => {
// https://github.com/rust-windowing/winit/issues/2846
state.add_mouse_motion(delta);
/*c += value.abs();
while c > 800.0 {
println!("inch");
c -= 800.0;
}*/
}
MainEventsCleared => {

@ -5,7 +5,7 @@ use wgpu::{util::DeviceExt, BufferAddress};
use crate::{camera::*, Input, player::Player};
pub struct State {
input: Input,
pub input: Input,
window: Window,
size: winit::dpi::PhysicalSize<u32>,
@ -448,13 +448,13 @@ impl State {
self.camera.update_pos(&(self.player));
return;
}
pub fn process_key(&mut self, key: winit::event::VirtualKeyCode, state: winit::event::ElementState) {
pub fn process_key(&mut self, key: winit::keyboard::KeyCode, state: winit::event::ElementState) {
return self.input.process_key(key, state);
}
pub fn add_mouse_motion(&mut self, delta: (f64, f64)) {
// kinda cringe
let (x, y) = self.input.mouse_moved;
return self.input.set_mouse_motion((x as f64 + delta.0, y as f64 + delta.1));
self.input.mouse_moved.0 += delta.0 as f32;
self.input.mouse_moved.1 += delta.1 as f32;
return;
}
pub fn set_mouse_motion(&mut self, delta: (f64, f64)) {
return self.input.set_mouse_motion(delta);