allow gilrs to fail (required for android support)

This commit is contained in:
griffi-gh 2023-05-21 04:23:10 +02:00
parent 8aaf64904b
commit 3eddda0886

View file

@ -25,7 +25,7 @@ pub struct RawKbmInputState {
}
#[derive(Unique)]
pub struct GilrsWrapper(Gilrs);
pub struct GilrsWrapper(Option<Gilrs>);
#[derive(Unique, Default, Clone, Copy)]
pub struct ActiveGamepad(Option<GamepadId>);
@ -65,10 +65,12 @@ fn process_gilrs_events(
mut gilrs: NonSendSync<UniqueViewMut<GilrsWrapper>>,
mut active_gamepad: UniqueViewMut<ActiveGamepad>
) {
while let Some(Event { id, event: _, time: _ }) = gilrs.0.next_event() {
if let Some(gilrs) = &mut gilrs.0 {
while let Some(Event { id, event: _, time: _ }) = gilrs.next_event() {
active_gamepad.0 = Some(id);
}
}
}
fn input_start(
mut inputs: UniqueViewMut<Inputs>,
@ -98,7 +100,8 @@ fn update_input_state_gamepad (
active_gamepad: UniqueView<ActiveGamepad>,
mut inputs: UniqueViewMut<Inputs>,
) {
if let Some(gamepad) = active_gamepad.0.map(|id| gilrs.0.gamepad(id)) {
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;
@ -107,6 +110,7 @@ fn update_input_state_gamepad (
inputs.action_b |= gamepad.is_pressed(Button::East);
}
}
}
fn input_end(
mut inputs: UniqueViewMut<Inputs>,
@ -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());