Skip to content

Commit

Permalink
refactor(Gamepad class): adapt and merge is- and wasPressed()
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `isPressed(x)` is now `button(x).query()`,
`wasPressed(x)` is now `button(x, true).query()`.
  • Loading branch information
niklashigi committed Dec 26, 2017
1 parent 21d8151 commit 7f3c697
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 35 deletions.
44 changes: 24 additions & 20 deletions src/inputs/gamepad.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,16 @@ describe('The `Gamepad` class', () => {

const { win, nav, gamepad } = mockPack()

it('should have an `isPressed()` method that returns `false`', () => {
expect(gamepad.isPressed(0)).to.equal(false)
})
describe('should have an `button()` method that returns a component that', () => {

it('when queried `false`', () => {
expect(gamepad.button(0).query()).to.equal(false)
})

it('should have a `wasPressed()` method that returns `false`', () => {
expect(gamepad.button(0, true).query()).to.equal(false)
})

it('should have a `wasPressed()` method that returns `false`', () => {
expect(gamepad.isPressed(0)).to.equal(false)
})

})
Expand All @@ -122,33 +126,33 @@ describe('The `Gamepad` class', () => {

win.listeners.gamepadconnected({ gamepad: { index: 0 } })

describe('should have an `isPressed()` method that', () => {
describe('should have a `button()` method that returns a component that', () => {

it('returns `false` when the button is not pressed', () => {
expect(gamepad.isPressed(0)).to.equal(false)
expect(gamepad.button(0).query()).to.equal(false)
})

it('returns `true` when the button is pressed', () => {
nav.gamepads[0].buttons[0].pressed = true
expect(gamepad.isPressed(0)).to.equal(true)
expect(gamepad.button(0).query()).to.equal(true)
})

})
describe('when initialized with `trigger = true`', () => {

describe('should have a `wasPressed()` method that', () => {
it('returns `false` when the key is not pressed', () => {
nav.gamepads[0].buttons[0].pressed = false
expect(gamepad.button(0, true).query()).to.equal(false)
})

it('returns `false` when the key is not pressed', () => {
nav.gamepads[0].buttons[0].pressed = false
expect(gamepad.wasPressed(0)).to.equal(false)
})
it('returns `true` once after the key was pressed', () => {
nav.gamepads[0].buttons[0].pressed = true
expect(gamepad.button(0, true).query()).to.equal(true)
})

it('returns `true` once after the key was not pressed', () => {
nav.gamepads[0].buttons[0].pressed = true
expect(gamepad.wasPressed(0)).to.equal(true)
})
it('returns `false` after the key state was queried', () => {
expect(gamepad.button(0, true).query()).to.equal(false)
})

it('returns `false` after the key state was queried', () => {
expect(gamepad.wasPressed(0)).to.equal(false)
})

})
Expand Down
37 changes: 22 additions & 15 deletions src/inputs/gamepad.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IGamepad, INavigator, IWindow } from '../apis'
import { Control } from '../models/control'

export class Gamepad {

Expand Down Expand Up @@ -41,23 +42,29 @@ export class Gamepad {
return this.navigator.getGamepads()[this.gamepadIndex]
}

public isPressed(button: number) {
return this.isConnected() && this.gamepad.buttons[button].pressed
}

public wasPressed(button: number) {
/* istanbul ignore else */
if (this.isConnected()) {
if (this.gamepad.buttons[button].pressed) {
if (!this.pressedButtons.has(button)) {
this.pressedButtons.add(button)
return true
public button(button: number, trigger = false): Control {
return {
label: `(${button})`,
icons: ['gamepad-button-' + button],
query: () => {
if (trigger) {
/* istanbul ignore else */
if (this.isConnected()) {
if (this.gamepad.buttons[button].pressed) {
if (!this.pressedButtons.has(button)) {
this.pressedButtons.add(button)
return true
}
} else {
this.pressedButtons.delete(button)
}
}
return false
} else {
return this.isConnected() && this.gamepad.buttons[button].pressed
}
} else {
this.pressedButtons.delete(button)
}
},
}
return false
}

}

0 comments on commit 7f3c697

Please sign in to comment.