Skip to content

Commit

Permalink
Trigger gamepad connection event at start up
Browse files Browse the repository at this point in the history
  • Loading branch information
simpuid committed Oct 25, 2020
1 parent 2f9d598 commit 47d5b57
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
11 changes: 11 additions & 0 deletions crates/bevy_gilrs/src/gilrs_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ use bevy_ecs::{Resources, World};
use bevy_input::{gamepad::GamepadEventRaw, prelude::*};
use gilrs::{EventType, Gilrs};

pub fn gilrs_event_startup_system(_world: &mut World, resources: &mut Resources) {
let gilrs = resources.get_thread_local::<Gilrs>().unwrap();
let mut event = resources.get_mut::<Events<GamepadEventRaw>>().unwrap();
for (id, _) in gilrs.gamepads() {
event.send(GamepadEventRaw(
convert_gamepad_id(id),
GamepadEventType::Connected,
));
}
}

pub fn gilrs_event_system(_world: &mut World, resources: &mut Resources) {
let mut gilrs = resources.get_thread_local_mut::<Gilrs>().unwrap();
let mut event = resources.get_mut::<Events<GamepadEventRaw>>().unwrap();
Expand Down
8 changes: 6 additions & 2 deletions crates/bevy_gilrs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ mod converter;
mod gilrs_system;

use bevy_app::prelude::*;
use bevy_app::startup_stage::PRE_STARTUP;
use bevy_ecs::prelude::*;
use gilrs::GilrsBuilder;
use gilrs_system::gilrs_event_system;
use gilrs_system::{gilrs_event_startup_system, gilrs_event_system};

#[derive(Default)]
pub struct GilrsPlugin;
Expand All @@ -18,7 +19,10 @@ impl Plugin for GilrsPlugin {
{
Ok(gilrs) => {
app.add_thread_local_resource(gilrs)
.add_startup_system(gilrs_event_system.thread_local_system())
.add_startup_system_to_stage(
PRE_STARTUP,
gilrs_event_startup_system.thread_local_system(),
)
.add_system_to_stage(
stage::PRE_EVENT,
gilrs_event_system.thread_local_system(),
Expand Down
2 changes: 2 additions & 0 deletions crates/bevy_input/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use keyboard::{keyboard_input_system, KeyCode, KeyboardInput};
use mouse::{mouse_button_input_system, MouseButton, MouseButtonInput, MouseMotion, MouseWheel};
use touch::{touch_screen_input_system, TouchInput, Touches};

use bevy_app::startup_stage::STARTUP;
use bevy_ecs::IntoQuerySystem;
use gamepad::{
gamepad_event_system, GamepadAxis, GamepadButton, GamepadEvent, GamepadEventRaw,
Expand Down Expand Up @@ -53,6 +54,7 @@ impl Plugin for InputPlugin {
.init_resource::<Axis<GamepadAxis>>()
.init_resource::<Axis<GamepadButton>>()
.add_system_to_stage(bevy_app::stage::EVENT, gamepad_event_system.system())
.add_startup_system_to_stage(STARTUP, gamepad_event_system.system())
.add_event::<TouchInput>()
.init_resource::<Touches>()
.add_system_to_stage(bevy_app::stage::EVENT, touch_screen_input_system.system());
Expand Down
3 changes: 2 additions & 1 deletion examples/input/gamepad_input.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use bevy::prelude::*;
use bevy_app::startup_stage::POST_STARTUP;
use bevy_input::gamepad::{Gamepad, GamepadButton, GamepadEvent, GamepadEventType};
use bevy_utils::HashSet;

fn main() {
App::build()
.add_default_plugins()
.init_resource::<GamepadLobby>()
.add_startup_system(connection_system.system())
.add_startup_system_to_stage(POST_STARTUP, connection_system.system())
.add_system(connection_system.system())
.add_system(gamepad_system.system())
.run();
Expand Down

0 comments on commit 47d5b57

Please sign in to comment.