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

[Merged by Bors] - Add Gamepads resource #3257

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 31 additions & 5 deletions crates/bevy_input/src/gamepad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,34 @@ pub struct Gamepad(pub usize);
/// Container of unique connected [Gamepad]s
///
/// [Gamepad]s are registered and deregistered in [gamepad_connection_system]
pub struct GamepadLobby {
pub gamepads: HashSet<Gamepad>,
pub struct Gamepads {
gamepads: HashSet<Gamepad>,
}

impl Gamepads {
/// Returns true if the [Gamepads] contains a [Gamepad].
pub fn contains(&self, gamepad: &Gamepad) -> bool {
self.gamepads.contains(gamepad)
}

/// Iterates over registered [Gamepad]s
pub fn iter(&self) -> std::collections::hash_set::Iter<Gamepad> {
cart marked this conversation as resolved.
Show resolved Hide resolved
self.gamepads.iter()
}

/// Registers [Gamepad].
///
/// If this [Gamepad] was present, `true` is returned.
///
/// If this [Gamepad] wasn't present, `false` is returned.
fn register(&mut self, gamepad: Gamepad) -> bool {
self.gamepads.insert(gamepad)
}

/// Deregisters [Gamepad] and returns whether the value was registered
fn deregister(&mut self, gamepad: &Gamepad) -> bool {
self.gamepads.remove(gamepad)
}
cart marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -213,17 +239,17 @@ impl ButtonAxisSettings {
///
/// By default, runs during `CoreStage::PreUpdate` when added via [InputPlugin].
pub fn gamepad_connection_system(
mut lobby: ResMut<GamepadLobby>,
mut gamepads: ResMut<Gamepads>,
mut gamepad_event: EventReader<GamepadEvent>,
) {
for event in gamepad_event.iter() {
match &event {
GamepadEvent(gamepad, GamepadEventType::Connected) => {
lobby.gamepads.insert(*gamepad);
gamepads.register(*gamepad);
info!("{:?} Connected", gamepad);
}
GamepadEvent(gamepad, GamepadEventType::Disconnected) => {
lobby.gamepads.remove(gamepad);
gamepads.deregister(gamepad);
info!("{:?} Disconnected", gamepad);
}
_ => (),
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod prelude {
pub use crate::{
gamepad::{
Gamepad, GamepadAxis, GamepadAxisType, GamepadButton, GamepadButtonType, GamepadEvent,
GamepadEventType, GamepadLobby,
GamepadEventType, Gamepads,
},
keyboard::KeyCode,
mouse::MouseButton,
Expand All @@ -27,7 +27,7 @@ pub mod prelude {
use bevy_app::prelude::*;
use keyboard::{keyboard_input_system, KeyCode, KeyboardInput};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
use prelude::GamepadLobby;
use prelude::Gamepads;
use touch::{touch_screen_input_system, TouchInput, Touches};

use gamepad::{
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Plugin for InputPlugin {
.add_event::<GamepadEvent>()
.add_event::<GamepadEventRaw>()
.init_resource::<GamepadSettings>()
.init_resource::<GamepadLobby>()
.init_resource::<Gamepads>()
.init_resource::<Input<GamepadButton>>()
.init_resource::<Axis<GamepadAxis>>()
.init_resource::<Axis<GamepadButton>>()
Expand Down
4 changes: 2 additions & 2 deletions examples/input/gamepad_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ fn main() {
}

fn gamepad_system(
lobby: Res<GamepadLobby>,
gamepads: Res<Gamepads>,
button_inputs: Res<Input<GamepadButton>>,
button_axes: Res<Axis<GamepadButton>>,
axes: Res<Axis<GamepadAxis>>,
) {
for gamepad in lobby.gamepads.iter().cloned() {
for gamepad in gamepads.iter().cloned() {
if button_inputs.just_pressed(GamepadButton(gamepad, GamepadButtonType::South)) {
info!("{:?} just pressed South", gamepad);
} else if button_inputs.just_released(GamepadButton(gamepad, GamepadButtonType::South)) {
Expand Down