game: fix some unhandled device events causing panics

This commit is contained in:
SeanOMik 2024-09-28 22:05:57 -04:00
parent 8fb686b7fe
commit 02f0c93aa2
Signed by: SeanOMik
GPG Key ID: FEC9E2FC15235964
3 changed files with 11 additions and 5 deletions

View File

@ -72,7 +72,7 @@ impl App {
t.with(filter::Targets::new() t.with(filter::Targets::new()
// done by prefix, so it includes all lyra subpackages // done by prefix, so it includes all lyra subpackages
.with_target("lyra", Level::DEBUG) .with_target("lyra", Level::DEBUG)
.with_target("wgsl_preprocessor", Level::DEBUG) .with_target("wgsl_preprocessor", Level::INFO)
.with_target("wgpu", Level::WARN) .with_target("wgpu", Level::WARN)
.with_target("winit", Level::DEBUG) .with_target("winit", Level::DEBUG)
.with_default(Level::INFO)) .with_default(Level::INFO))

View File

@ -21,6 +21,12 @@ fn write_scroll_delta(mouse_scroll_ev: &mut EventWriter<MouseScroll>, delta: &Mo
mouse_scroll_ev.write(event); mouse_scroll_ev.write(event);
} }
fn write_key_event(key_buttons: &mut ResMut<InputButtons<winit::keyboard::KeyCode>>, physical_key: winit::keyboard::PhysicalKey, state: winit::event::ElementState) {
if let PhysicalKey::Code(code) = physical_key {
key_buttons.add_input_from_winit(code, state);
}
}
pub fn input_system( pub fn input_system(
mut key_code_res: ResMut<InputButtons<winit::keyboard::KeyCode>>, mut key_code_res: ResMut<InputButtons<winit::keyboard::KeyCode>>,
mut mouse_btn_res: ResMut<InputButtons<MouseButton>>, mut mouse_btn_res: ResMut<InputButtons<MouseButton>>,
@ -37,9 +43,7 @@ pub fn input_system(
while let Some(event) = window_ev.read() { while let Some(event) = window_ev.read() {
match event.deref() { match event.deref() {
WindowEvent::KeyboardInput { event, .. } => { WindowEvent::KeyboardInput { event, .. } => {
if let PhysicalKey::Code(code) = event.physical_key { write_key_event(&mut key_code_res, event.physical_key, event.state);
key_code_res.add_input_from_winit(code, event.state);
}
}, },
WindowEvent::CursorMoved { position, .. } => { WindowEvent::CursorMoved { position, .. } => {
let exact = MouseExact { let exact = MouseExact {
@ -100,6 +104,9 @@ pub fn input_system(
winit::event::DeviceEvent::MouseWheel { delta } => { winit::event::DeviceEvent::MouseWheel { delta } => {
write_scroll_delta(&mut mouse_scroll_ev, delta); write_scroll_delta(&mut mouse_scroll_ev, delta);
}, },
winit::event::DeviceEvent::Key(key) => {
write_key_event(&mut key_code_res, key.physical_key, key.state);
},
_ => { _ => {
todo!("unhandled device event: {:?}", device.event); todo!("unhandled device event: {:?}", device.event);
} }

View File

@ -188,7 +188,6 @@ pub(crate) fn lua_wrap_handle_impl(input: proc_macro::TokenStream) -> proc_macro
impl mlua::UserData for #wrapper_name { impl mlua::UserData for #wrapper_name {
fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) { fn add_fields<'lua, F: mlua::UserDataFields<'lua, Self>>(fields: &mut F) {
fields.add_field("__name", stringify!(#handle_name));
fields.add_field_method_get("path", |_, this| Ok(this.path())); fields.add_field_method_get("path", |_, this| Ok(this.path()));
fields.add_field_method_get("version", |_, this| Ok(this.version())); fields.add_field_method_get("version", |_, this| Ok(this.version()));
fields.add_field_method_get("uuid", |_, this| Ok(this.uuid().to_string())); fields.add_field_method_get("uuid", |_, this| Ok(this.uuid().to_string()));