Skip to content

Commit

Permalink
change: improve real gamepad buttons support
Browse files Browse the repository at this point in the history
  • Loading branch information
RodrigoDornelles committed Jan 2, 2025
1 parent 48590e1 commit 3b88021
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
9 changes: 8 additions & 1 deletion src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ function core2dAxis (self: ObjectGpz) {
const core = {
Joy: core2dAxis,
Dpad: core2dAxis,
Btn4: core2dAxis,
Btn4(self: ObjectGpz) {
if (self.from == EventGpz.Gamepad) {
self.stateOld = [...self.stateNew]
self.stateNew = self.buttons ?? []
} else {
core2dAxis(self)
}
},
Btn(self: ObjectGpz) {
self.stateOld = [...self.stateNew]
if (self.from == EventGpz.Touch) {
Expand Down
22 changes: 11 additions & 11 deletions src/event_gamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ function installEventGamepad(device: Window, pads: Array<ObjectGpz>)
{
let gamepadState: GamepadFSM = GamepadFSM.Offline

const joypads = pads.filter(self => self.type == ClassGpz.Joy)
const buttonpads = pads.filter(self => self.type == ClassGpz.Btn)
const joypads = pads.filter(self => self.type == ClassGpz.Joy || self.type == ClassGpz.Dpad)
const buttonpads = pads.filter(self => self.type == ClassGpz.Btn || self.type == ClassGpz.Btn4)

function processButtons(buttons: Array<number>) {
function processButtons(buttons: Array<boolean>) {
buttonpads.forEach(self => {
if (!self.stateNew[0] && buttons.length > 0) {
self.from = EventGpz.Gamepad
self.buttons = [true]
process(self)
if (self.buttons == null) {
self.buttons = new Array(self.stateNew.length).fill(false)
}
if (self.stateNew[0] && buttons.length == 0) {
self.stateNew.forEach((_, index) => {
if (self.buttons && buttons[index] != self.stateNew[index]) {
self.from = EventGpz.Gamepad
self.buttons = [false]
process(self)
}
self.buttons[index] = buttons[index]
}
})
process(self)
})
}

Expand Down
2 changes: 1 addition & 1 deletion src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function handleGamepadAxis(gamepad: Gamepad) {
}

function handleGamepadButtons(gamepad: Gamepad) {
return gamepad.buttons.map((is, button) => is.pressed && button).filter(id => id !== false) as Array<number>
return gamepad.buttons.map(is => is.pressed) as Array<boolean>
}

export {handleMouse, handleTouch, handleGamepadAxis, handleGamepadButtons}

0 comments on commit 3b88021

Please sign in to comment.