Skip to content

Commit

Permalink
Setup gilrs properly + integrated into existing input fetching flow
Browse files Browse the repository at this point in the history
  • Loading branch information
RockasMockas committed Oct 6, 2024
1 parent c95b83d commit 2d94bac
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 47 deletions.
35 changes: 4 additions & 31 deletions framework_crates/bones_bevy_renderer/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// framework_crates/bones_bevy_renderer/src/input.rs

use super::*;
use bevy::{
input::{
gamepad::GamepadEvent,
keyboard::KeyboardInput,
mouse::{MouseButtonInput, MouseMotion, MouseWheel},
},
window::PrimaryWindow,
};
use bones::{MouseScreenPosition, MouseWorldPosition};
use bones_framework::input::gilrs::process_gamepad_events;

pub fn insert_bones_input(
In((mouse_inputs, keyboard_inputs, gamepad_inputs)): In<(
Expand All @@ -28,7 +30,6 @@ pub fn get_bones_input(
mut mouse_motion_events: EventReader<MouseMotion>,
mut mouse_wheel_events: EventReader<MouseWheel>,
mut keyboard_events: EventReader<KeyboardInput>,
mut gamepad_events: EventReader<GamepadEvent>,
) -> (
bones::MouseInputs,
bones::KeyboardInputs,
Expand Down Expand Up @@ -67,35 +68,7 @@ pub fn get_bones_input(
})
.collect(),
},
bones::GamepadInputs {
gamepad_events: gamepad_events
.iter()
.map(|event| match event {
GamepadEvent::Connection(c) => {
bones::GamepadEvent::Connection(bones::GamepadConnectionEvent {
gamepad: c.gamepad.id as u32,
event: if c.connected() {
bones::GamepadConnectionEventKind::Connected
} else {
bones::GamepadConnectionEventKind::Disconnected
},
})
}
GamepadEvent::Button(b) => {
bones::GamepadEvent::Button(bones::GamepadButtonEvent {
gamepad: b.gamepad.id as u32,
button: b.button_type.into_bones(),
value: b.value,
})
}
GamepadEvent::Axis(a) => bones::GamepadEvent::Axis(bones::GamepadAxisEvent {
gamepad: a.gamepad.id as u32,
axis: a.axis_type.into_bones(),
value: a.value,
}),
})
.collect(),
},
process_gamepad_events(),
)
}

Expand Down
20 changes: 4 additions & 16 deletions framework_crates/bones_framework/src/input/gilrs.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
//! Gamepad input session, systems, and resources.
// framework_crates/bones_framework/src/input/gilrs.rs
use crate::prelude::*;
use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs as GilrsContext};
use once_cell::sync::Lazy;
use std::sync::{Arc, Mutex};

/// Name of the default bones input session
pub const DEFAULT_BONES_INPUT_SESSION: &str = "BONES_INPUT";

/// Lazy-initialized GilrsContext
static GILRS_CONTEXT: Lazy<Arc<Mutex<GilrsContext>>> = Lazy::new(|| {
Arc::new(Mutex::new(
GilrsContext::new().expect("Failed to initialize GilrsContext"),
))
});

/// Sets up gamepad-related resources and the default bones input session
pub fn game_plugin(game: &mut Game) {
let session = game.sessions.create(DEFAULT_BONES_INPUT_SESSION);
// Input doesn't do any rendering
session.visible = false;
session
.stages
.add_system_to_stage(CoreStage::First, process_gamepad_events);
}

fn process_gamepad_events(mut gamepad_inputs: ResMut<GamepadInputs>) {
pub fn process_gamepad_events() -> GamepadInputs {
let mut gamepad_inputs = GamepadInputs::default();
let mut gilrs = GILRS_CONTEXT.lock().unwrap();
while let Some(gilrs_event) = gilrs
.next_event()
Expand Down Expand Up @@ -77,6 +64,7 @@ fn process_gamepad_events(mut gamepad_inputs: ResMut<GamepadInputs>) {
_ => (),
};
}
gamepad_inputs
}

fn convert_button(button: gilrs::Button) -> Option<GamepadButton> {
Expand Down

0 comments on commit 2d94bac

Please sign in to comment.