-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5fa19bf
commit c95b83d
Showing
3 changed files
with
184 additions
and
10 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} |