Fix gamepad removal not working properly

This commit is contained in:
2024-05-09 22:00:38 +10:00
parent 06046cd163
commit b62cd056b5
3 changed files with 31 additions and 15 deletions

View File

@ -84,7 +84,7 @@ public class Application
for idx in 0...SDL_NumJoysticks() for idx in 0...SDL_NumJoysticks()
{ {
if SDL_IsGameController(idx) != SDL_TRUE { continue } if SDL_IsGameController(idx) != SDL_TRUE { continue }
Input.instance.padConnectedEvent(index: idx) Input.instance.padConnectedEvent(deviceIndex: idx)
} }
do do
@ -129,9 +129,9 @@ public class Application
Input.instance.releaseEvent(scan: event.key.keysym.scancode) Input.instance.releaseEvent(scan: event.key.keysym.scancode)
case SDL_CONTROLLERDEVICEADDED: case SDL_CONTROLLERDEVICEADDED:
if SDL_IsGameController(event.cdevice.which) != SDL_TRUE { break } if SDL_IsGameController(event.cdevice.which) != SDL_TRUE { break }
Input.instance.padConnectedEvent(index: event.cdevice.which) Input.instance.padConnectedEvent(deviceIndex: event.cdevice.which)
case SDL_CONTROLLERDEVICEREMOVED: case SDL_CONTROLLERDEVICEREMOVED:
Input.instance.padRemovedEvent(index: event.cdevice.which) Input.instance.padRemovedEvent(id: event.cdevice.which)
case SDL_CONTROLLERBUTTONDOWN: case SDL_CONTROLLERBUTTONDOWN:
Input.instance.padButtonPressEvent(id: event.cbutton.which, btn: event.cbutton.button) Input.instance.padButtonPressEvent(id: event.cbutton.which, btn: event.cbutton.button)
case SDL_CONTROLLERBUTTONUP: case SDL_CONTROLLERBUTTONUP:

View File

@ -69,10 +69,21 @@ public class GamePad
private var _btns = [UInt8](repeating: 0, count: Int(SDL_CONTROLLER_BUTTON_MAX.rawValue)) private var _btns = [UInt8](repeating: 0, count: Int(SDL_CONTROLLER_BUTTON_MAX.rawValue))
private var _axes = [Int16](repeating: 0, count: Int(SDL_CONTROLLER_AXIS_MAX.rawValue)) private var _axes = [Int16](repeating: 0, count: Int(SDL_CONTROLLER_AXIS_MAX.rawValue))
private var _sdlPad: OpaquePointer private var _joyID: SDL_JoystickID, _sdlPad: OpaquePointer
internal init(pad: OpaquePointer) internal var instanceID: SDL_JoystickID { _joyID }
public var name: String { String(cString: SDL_GameControllerName(_sdlPad)) }
internal static func open(device: Int32) -> GamePad?
{ {
guard let sdlPad = SDL_GameControllerOpen(device) else { return nil }
let joyID = SDL_JoystickGetDeviceInstanceID(device)
return GamePad(instance: joyID, pad: sdlPad)
}
private init(instance: SDL_JoystickID, pad: OpaquePointer)
{
self._joyID = instance
self._sdlPad = pad self._sdlPad = pad
} }

View File

@ -19,20 +19,25 @@ public class Input
internal func pressEvent(scan: SDL_Scancode, repeat r: Bool) { keyboard.pressEvent(scan: scan, repeat: r) } internal func pressEvent(scan: SDL_Scancode, repeat r: Bool) { keyboard.pressEvent(scan: scan, repeat: r) }
internal func releaseEvent(scan: SDL_Scancode) { keyboard.releaseEvent(scan: scan) } internal func releaseEvent(scan: SDL_Scancode) { keyboard.releaseEvent(scan: scan) }
internal func padConnectedEvent(index: Int32) internal func padConnectedEvent(deviceIndex: Int32)
{ {
guard let sdlPad = SDL_GameControllerOpen(index) if let pad = GamePad.open(device: deviceIndex)
else { return } {
let id = SDL_JoystickGetDeviceInstanceID(index) print("Using gamepad #\(pad.instanceID), \"\(pad.name)\"")
_pads[id] = GamePad(pad: sdlPad) if _firstJoyID < 0
print("Using gamepad #\(id), \"\(String(cString: SDL_GameControllerName(sdlPad)))\"") {
if _firstJoyID < 0 { _firstJoyID = id } _firstJoyID = pad.instanceID
}
_pads[pad.instanceID] = pad
}
} }
internal func padRemovedEvent(index: Int32) internal func padRemovedEvent(id: Int32)
{ {
let id = SDL_JoystickGetDeviceInstanceID(index) if let pad = _pads.removeValue(forKey: id)
_pads.removeValue(forKey: id) {
pad.close()
}
if id == _firstJoyID if id == _firstJoyID
{ {
_firstJoyID = _pads.keys.first ?? -1 _firstJoyID = _pads.keys.first ?? -1