Skip to content

Commit

Permalink
Clean up gamepad stuff a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
17cupsofcoffee committed Dec 4, 2024
1 parent 2073d54 commit 8a22573
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
9 changes: 6 additions & 3 deletions examples/bunnymark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use nova::app::{App, EventHandler};
use nova::graphics::{Batcher, Color, DrawParams, Texture};
use nova::input::{Key, MouseButton};
use nova::input::{GamepadButton, Key, MouseButton};
use nova::math::Vec2;
use rand::rngs::ThreadRng;
use rand::{self, Rng};
Expand Down Expand Up @@ -83,8 +83,11 @@ impl EventHandler for GameState {
self.auto_spawn = !self.auto_spawn;
}

let should_spawn = self.spawn_timer == 0
&& (app.input.is_mouse_button_down(MouseButton::Left) || self.auto_spawn);
let button_down = app.input.is_key_down(Key::Space)
|| app.input.is_mouse_button_down(MouseButton::Left)
|| app.input.is_gamepad_button_down(0, GamepadButton::A);

let should_spawn = self.spawn_timer == 0 && (button_down || self.auto_spawn);

if should_spawn {
for _ in 0..INITIAL_BUNNIES {
Expand Down
35 changes: 7 additions & 28 deletions src/input/event.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,8 @@
use glam::Vec2;
use sdl3_sys::events::*;
use sdl3_sys::gamepad::*;
use sdl3_sys::joystick::*;

/// This is a unique ID for a joystick for the time it is connected to the
/// system.
///
/// It is never reused for the lifetime of the application. If the joystick is
/// disconnected and reconnected, it will get a new ID.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct JoystickID(SDL_JoystickID);

impl JoystickID {
fn from_raw(id: SDL_JoystickID) -> JoystickID {
JoystickID(id)
}

fn from_controller_handle(handle: *mut SDL_Gamepad) -> JoystickID {
unsafe { JoystickID(SDL_GetJoystickID(SDL_GetGamepadJoystick(handle))) }
}
}

use super::{Gamepad, GamepadAxis, GamepadButton, Key, MouseButton};
use super::{Gamepad, GamepadAxis, GamepadButton, JoystickID, Key, MouseButton};

#[derive(Debug, Clone, PartialEq)]
pub enum Event {
Expand Down Expand Up @@ -105,23 +85,22 @@ impl Event {
}

SDL_EVENT_GAMEPAD_ADDED => {
let handle = SDL_OpenGamepad(event.cdevice.which);
let handle = SDL_OpenGamepad(event.gdevice.which);

if handle.is_null() {
// TODO: Should probably log here
return None;
}

let joystick = JoystickID::from_controller_handle(handle);

let joystick = JoystickID::from_raw(event.gdevice.which);
let gamepad = Gamepad::from_raw(handle);

return Some(Event::ControllerDeviceAdded { joystick, gamepad });
}

SDL_EVENT_GAMEPAD_REMOVED => {
return Some(Event::ControllerDeviceRemoved {
joystick: JoystickID::from_raw(event.cdevice.which),
joystick: JoystickID::from_raw(event.gdevice.which),
});
}

Expand All @@ -130,7 +109,7 @@ impl Event {
GamepadButton::from_raw(SDL_GamepadButton(event.gbutton.button as i32))
{
return Some(Event::ControllerButtonDown {
joystick: JoystickID::from_raw(event.cdevice.which),
joystick: JoystickID::from_raw(event.gdevice.which),
button,
});
}
Expand All @@ -141,7 +120,7 @@ impl Event {
GamepadButton::from_raw(SDL_GamepadButton(event.gbutton.button as i32))
{
return Some(Event::ControllerButtonUp {
joystick: JoystickID::from_raw(event.cdevice.which),
joystick: JoystickID::from_raw(event.gdevice.which),
button,
});
}
Expand All @@ -162,7 +141,7 @@ impl Event {
value = 0.0;
}
return Some(Event::ControllerAxisMotion {
joystick: JoystickID::from_raw(event.cdevice.which),
joystick: JoystickID::from_raw(event.gdevice.which),
axis,
value,
});
Expand Down
18 changes: 17 additions & 1 deletion src/input/gamepad.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
use std::{fmt, rc::Rc};

use sdl3_sys::gamepad::*;
use sdl3_sys::joystick::*;

/// This is a unique ID for a joystick for the time it is connected to the
/// system.
///
/// It is never reused for the lifetime of the application. If the joystick is
/// disconnected and reconnected, it will get a new ID.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct JoystickID(SDL_JoystickID);

impl JoystickID {
pub fn from_raw(id: SDL_JoystickID) -> JoystickID {
JoystickID(id)
}
}

#[derive(Clone)]
pub struct Gamepad(#[allow(dead_code)] Rc<GamepadInner>);
pub struct Gamepad(Rc<GamepadInner>);

impl PartialEq for Gamepad {
fn eq(&self, other: &Self) -> bool {
Expand Down

0 comments on commit 8a22573

Please sign in to comment.