mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
Add support to get gamepad button/trigger values using Axis<GamepadButton> (#683)
This commit is contained in:
parent
76cc25823d
commit
dd91f8e116
3 changed files with 45 additions and 10 deletions
|
@ -9,6 +9,7 @@ pub fn gilrs_startup_system(_world: &mut World, resources: &mut Resources) {
|
|||
let mut gamepad_event = resources.get_mut::<Events<GamepadEvent>>().unwrap();
|
||||
let mut inputs = resources.get_mut::<Input<GamepadButton>>().unwrap();
|
||||
let mut axes = resources.get_mut::<Axis<GamepadAxis>>().unwrap();
|
||||
let mut button_axes = resources.get_mut::<Axis<GamepadButton>>().unwrap();
|
||||
gamepad_event.update();
|
||||
inputs.update();
|
||||
for (gilrs_id, gilrs_gamepad) in gilrs.gamepads() {
|
||||
|
@ -18,6 +19,7 @@ pub fn gilrs_startup_system(_world: &mut World, resources: &mut Resources) {
|
|||
&mut gamepad_event,
|
||||
&mut inputs,
|
||||
&mut axes,
|
||||
&mut button_axes,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -27,6 +29,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) {
|
|||
let mut gamepad_event = resources.get_mut::<Events<GamepadEvent>>().unwrap();
|
||||
let mut inputs = resources.get_mut::<Input<GamepadButton>>().unwrap();
|
||||
let mut axes = resources.get_mut::<Axis<GamepadAxis>>().unwrap();
|
||||
let mut button_axes = resources.get_mut::<Axis<GamepadButton>>().unwrap();
|
||||
|
||||
gamepad_event.update();
|
||||
inputs.update();
|
||||
|
@ -39,6 +42,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) {
|
|||
&mut gamepad_event,
|
||||
&mut inputs,
|
||||
&mut axes,
|
||||
&mut button_axes,
|
||||
);
|
||||
}
|
||||
EventType::Disconnected => {
|
||||
|
@ -47,6 +51,7 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) {
|
|||
&mut gamepad_event,
|
||||
&mut inputs,
|
||||
&mut axes,
|
||||
&mut button_axes,
|
||||
);
|
||||
}
|
||||
EventType::ButtonPressed(gilrs_button, _) => {
|
||||
|
@ -65,6 +70,14 @@ pub fn gilrs_update_system(_world: &mut World, resources: &mut Resources) {
|
|||
));
|
||||
}
|
||||
}
|
||||
EventType::ButtonChanged(gilrs_button, value, _) => {
|
||||
if let Some(button_type) = convert_button(gilrs_button) {
|
||||
button_axes.set(
|
||||
GamepadButton(convert_gamepad_id(gilrs_event.id), button_type),
|
||||
value,
|
||||
);
|
||||
}
|
||||
}
|
||||
EventType::AxisChanged(gilrs_axis, value, _) => {
|
||||
if let Some(axis_type) = convert_axis(gilrs_axis) {
|
||||
axes.set(
|
||||
|
@ -118,13 +131,17 @@ fn connect_gamepad(
|
|||
events: &mut Events<GamepadEvent>,
|
||||
inputs: &mut Input<GamepadButton>,
|
||||
axes: &mut Axis<GamepadAxis>,
|
||||
button_axes: &mut Axis<GamepadButton>,
|
||||
) {
|
||||
for gilrs_button in ALL_GILRS_BUTTONS.iter() {
|
||||
if let Some(button_type) = convert_button(*gilrs_button) {
|
||||
let gamepad_button = GamepadButton(gamepad, button_type);
|
||||
inputs.reset(gamepad_button);
|
||||
if gilrs_gamepad.is_pressed(*gilrs_button) {
|
||||
inputs.press(gamepad_button);
|
||||
if let Some(button_data) = gilrs_gamepad.button_data(*gilrs_button) {
|
||||
let gamepad_button = GamepadButton(gamepad, button_type);
|
||||
inputs.reset(gamepad_button);
|
||||
if button_data.is_pressed() {
|
||||
inputs.press(gamepad_button);
|
||||
}
|
||||
button_axes.set(gamepad_button, button_data.value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -142,11 +159,13 @@ fn disconnect_gamepad(
|
|||
events: &mut Events<GamepadEvent>,
|
||||
inputs: &mut Input<GamepadButton>,
|
||||
axes: &mut Axis<GamepadAxis>,
|
||||
button_axes: &mut Axis<GamepadButton>,
|
||||
) {
|
||||
for gilrs_button in ALL_GILRS_BUTTONS.iter() {
|
||||
if let Some(button_type) = convert_button(*gilrs_button) {
|
||||
let gamepad_button = GamepadButton(gamepad, button_type);
|
||||
inputs.reset(gamepad_button);
|
||||
button_axes.remove(&gamepad_button);
|
||||
}
|
||||
}
|
||||
for gilrs_axis in ALL_GILRS_AXES.iter() {
|
||||
|
|
|
@ -49,6 +49,7 @@ impl Plugin for InputPlugin {
|
|||
)
|
||||
.add_event::<GamepadEvent>()
|
||||
.init_resource::<Input<GamepadButton>>()
|
||||
.init_resource::<Axis<GamepadAxis>>();
|
||||
.init_resource::<Axis<GamepadAxis>>()
|
||||
.init_resource::<Axis<GamepadButton>>();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -34,7 +34,11 @@ fn connection_system(mut lobby: ResMut<Lobby>, gamepad_event: Res<Events<Gamepad
|
|||
}
|
||||
}
|
||||
|
||||
fn button_system(manager: Res<Lobby>, inputs: Res<Input<GamepadButton>>) {
|
||||
fn button_system(
|
||||
manager: Res<Lobby>,
|
||||
inputs: Res<Input<GamepadButton>>,
|
||||
button_axes: Res<Axis<GamepadButton>>,
|
||||
) {
|
||||
let button_types = [
|
||||
GamepadButtonType::South,
|
||||
GamepadButtonType::East,
|
||||
|
@ -63,6 +67,15 @@ fn button_system(manager: Res<Lobby>, inputs: Res<Input<GamepadButton>>) {
|
|||
} else if inputs.just_released(GamepadButton(*gamepad, *button_type)) {
|
||||
println!("Released {:?}", GamepadButton(*gamepad, *button_type));
|
||||
}
|
||||
if let Some(value) = button_axes.get(&GamepadButton(*gamepad, *button_type)) {
|
||||
if value_check(value) {
|
||||
println!(
|
||||
"Button as Axis {:?} is {}",
|
||||
GamepadButton(*gamepad, *button_type),
|
||||
value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -81,13 +94,15 @@ fn axis_system(manager: Res<Lobby>, axes: Res<Axis<GamepadAxis>>) {
|
|||
for gamepad in manager.gamepad.iter() {
|
||||
for axis_type in axis_types.iter() {
|
||||
if let Some(value) = axes.get(&GamepadAxis(*gamepad, *axis_type)) {
|
||||
if value.abs() > 0.01f32
|
||||
&& (value - 1.0f32).abs() > 0.01f32
|
||||
&& (value + 1.0f32).abs() > 0.01f32
|
||||
{
|
||||
if value_check(value) {
|
||||
println!("Axis {:?} is {}", GamepadAxis(*gamepad, *axis_type), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn value_check(value: f32) -> bool {
|
||||
let value = value.abs();
|
||||
value > 0.1f32 && value < 0.9f32
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue