Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Direct Gilrs integration #475

Merged
merged 8 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 28 additions & 33 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion framework_crates/bones_bevy_renderer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ anyhow = "1.0"

[dependencies.bevy]
default-features = false
features = ["bevy_render", "bevy_core_pipeline", "bevy_sprite", "x11", "bevy_gilrs"]
features = ["bevy_render", "bevy_core_pipeline", "bevy_sprite", "x11"]
version = "0.11"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
33 changes: 2 additions & 31 deletions framework_crates/bones_bevy_renderer/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
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 +28,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 +66,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
3 changes: 3 additions & 0 deletions framework_crates/bones_framework/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ instant = { version = "0.1", features = ["wasm-bindgen"] }
noise = "0.9"
once_cell = "1.17"
thiserror = "1.0"
gilrs = "0.11.0"
send_wrapper = "0.6.0"


# Tracing
tracing = "0.1"
Expand Down
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
109 changes: 109 additions & 0 deletions framework_crates/bones_framework/src/input/gilrs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
//! Gilrs integration.
use crate::prelude::*;
use gilrs::{ev::filter::axis_dpad_to_button, EventType, Filter, Gilrs as GilrsContext};
use once_cell::sync::Lazy;
use send_wrapper::SendWrapper;
use std::sync::{Arc, Mutex};

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

/// Processes gilrs gamepad events into Bones-native 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()
.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,
}));
}
}
_ => (),
};
}
gamepad_inputs
}

/// Converts a gilrs button to a bones-native button
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,
}
}

/// Converts a gilrs axis to a bones-native axis
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,
}
}
Loading