From ec7cddec8dc1c0ec30af44069591f9d020d3af1a Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Sun, 21 May 2023 04:23:10 +0200 Subject: [PATCH] allow gilrs to fail (required for android support) --- kubi/src/input.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/kubi/src/input.rs b/kubi/src/input.rs index 8f6fd27..37eb0fb 100644 --- a/kubi/src/input.rs +++ b/kubi/src/input.rs @@ -25,7 +25,7 @@ pub struct RawKbmInputState { } #[derive(Unique)] -pub struct GilrsWrapper(Gilrs); +pub struct GilrsWrapper(Option); #[derive(Unique, Default, Clone, Copy)] pub struct ActiveGamepad(Option); @@ -65,8 +65,10 @@ fn process_gilrs_events( mut gilrs: NonSendSync>, mut active_gamepad: UniqueViewMut ) { - while let Some(Event { id, event: _, time: _ }) = gilrs.0.next_event() { - active_gamepad.0 = Some(id); + if let Some(gilrs) = &mut gilrs.0 { + while let Some(Event { id, event: _, time: _ }) = gilrs.next_event() { + active_gamepad.0 = Some(id); + } } } @@ -98,13 +100,15 @@ fn update_input_state_gamepad ( active_gamepad: UniqueView, mut inputs: UniqueViewMut, ) { - if let Some(gamepad) = active_gamepad.0.map(|id| gilrs.0.gamepad(id)) { - let left_stick = vec2(gamepad.value(Axis::LeftStickX), gamepad.value(Axis::LeftStickY)); - let right_stick = vec2(gamepad.value(Axis::RightStickX), -gamepad.value(Axis::RightStickY)); - inputs.movement += left_stick; - inputs.look += right_stick; - inputs.action_a |= gamepad.is_pressed(Button::South); - inputs.action_b |= gamepad.is_pressed(Button::East); + if let Some(gilrs) = &gilrs.0 { + if let Some(gamepad) = active_gamepad.0.map(|id| gilrs.gamepad(id)) { + let left_stick = vec2(gamepad.value(Axis::LeftStickX), gamepad.value(Axis::LeftStickY)); + let right_stick = vec2(gamepad.value(Axis::RightStickX), -gamepad.value(Axis::RightStickY)); + inputs.movement += left_stick; + inputs.look += right_stick; + inputs.action_a |= gamepad.is_pressed(Button::South); + inputs.action_b |= gamepad.is_pressed(Button::East); + } } } @@ -119,7 +123,12 @@ fn input_end( pub fn init_input ( storages: AllStoragesView ) { - storages.add_unique_non_send_sync(GilrsWrapper(Gilrs::new().expect("Failed to initialize Gilrs"))); + storages.add_unique_non_send_sync(GilrsWrapper( + Gilrs::new().map_err(|x| { + log::error!("Failed to initialize Gilrs"); + x + }).ok() + )); storages.add_unique(ActiveGamepad::default()); storages.add_unique(Inputs::default()); storages.add_unique(PrevInputs::default());