Skip to content

Commit

Permalink
Start of gilrs.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
RockasMockas committed Oct 6, 2024
1 parent 5fa19bf commit c95b83d
Show file tree
Hide file tree
Showing 3 changed files with 184 additions and 10 deletions.
76 changes: 66 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions framework_crates/bones_framework/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use bones_schema::HasSchema;
use self::prelude::{GamepadInputs, KeyboardInputs};

pub mod gamepad;
pub mod gilrs;
pub mod keyboard;
pub mod mouse;
pub mod window;
Expand Down
117 changes: 117 additions & 0 deletions framework_crates/bones_framework/src/input/gilrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
//! Gamepad input session, systems, and resources.
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>) {
let mut gilrs = GILRS_CONTEXT.lock().unwrap();
while let Some(gilrs_event) = gilrs
.next_event()
.filter_ev(&axis_dpad_to_button, &mut gilrs)
{
gilrs.update(&gilrs_event);

let gamepad = usize::from(gilrs_event.id) as u32;
match gilrs_event.event {
EventType::Connected => {
let _pad = gilrs.gamepad(gilrs_event.id);
gamepad_inputs.gamepad_events.push(GamepadEvent::Connection(
GamepadConnectionEvent {
gamepad,
event: GamepadConnectionEventKind::Connected,
},
));
}
EventType::Disconnected => {
gamepad_inputs.gamepad_events.push(GamepadEvent::Connection(
GamepadConnectionEvent {
gamepad,
event: GamepadConnectionEventKind::Disconnected,
},
));
}
EventType::ButtonChanged(gilrs_button, value, _) => {
if let Some(button) = convert_button(gilrs_button) {
gamepad_inputs
.gamepad_events
.push(GamepadEvent::Button(GamepadButtonEvent {
gamepad,
button,
value,
}));
}
}
EventType::AxisChanged(gilrs_axis, value, _) => {
if let Some(axis) = convert_axis(gilrs_axis) {
gamepad_inputs
.gamepad_events
.push(GamepadEvent::Axis(GamepadAxisEvent {
gamepad,
axis,
value,
}));
}
}
_ => (),
};
}
}

fn convert_button(button: gilrs::Button) -> Option<GamepadButton> {
match button {
gilrs::Button::South => Some(GamepadButton::South),
gilrs::Button::East => Some(GamepadButton::East),
gilrs::Button::North => Some(GamepadButton::North),
gilrs::Button::West => Some(GamepadButton::West),
gilrs::Button::C => Some(GamepadButton::C),
gilrs::Button::Z => Some(GamepadButton::Z),
gilrs::Button::LeftTrigger => Some(GamepadButton::LeftTrigger),
gilrs::Button::LeftTrigger2 => Some(GamepadButton::LeftTrigger2),
gilrs::Button::RightTrigger => Some(GamepadButton::RightTrigger),
gilrs::Button::RightTrigger2 => Some(GamepadButton::RightTrigger2),
gilrs::Button::Select => Some(GamepadButton::Select),
gilrs::Button::Start => Some(GamepadButton::Start),
gilrs::Button::Mode => Some(GamepadButton::Mode),
gilrs::Button::LeftThumb => Some(GamepadButton::LeftThumb),
gilrs::Button::RightThumb => Some(GamepadButton::RightThumb),
gilrs::Button::DPadUp => Some(GamepadButton::DPadUp),
gilrs::Button::DPadDown => Some(GamepadButton::DPadDown),
gilrs::Button::DPadLeft => Some(GamepadButton::DPadLeft),
gilrs::Button::DPadRight => Some(GamepadButton::DPadRight),
gilrs::Button::Unknown => None,
}
}

fn convert_axis(axis: gilrs::Axis) -> Option<GamepadAxis> {
match axis {
gilrs::Axis::LeftStickX => Some(GamepadAxis::LeftStickX),
gilrs::Axis::LeftStickY => Some(GamepadAxis::LeftStickY),
gilrs::Axis::LeftZ => Some(GamepadAxis::LeftZ),
gilrs::Axis::RightStickX => Some(GamepadAxis::RightStickX),
gilrs::Axis::RightStickY => Some(GamepadAxis::RightStickY),
gilrs::Axis::RightZ => Some(GamepadAxis::RightZ),
gilrs::Axis::Unknown | gilrs::Axis::DPadX | gilrs::Axis::DPadY => None,
}
}

0 comments on commit c95b83d

Please sign in to comment.