Skip to content

Commit

Permalink
feat(gamepad): reject non-standard gamepads
Browse files Browse the repository at this point in the history
When a gamepad connects, Contro will now check whether its `mapping`
equals `standard` and reject it if it doesn't.

Closes #2
  • Loading branch information
niklashigi committed Jan 5, 2018
1 parent 4bccee8 commit 5407f15
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
1 change: 1 addition & 0 deletions src/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ export interface IGamepad {
axes: number[]
connected: boolean
timestamp: number
mapping: string
}
50 changes: 25 additions & 25 deletions src/inputs/gamepad.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from 'chai'
import * as Mocha from 'mocha'
import { IGamepad, INavigator, IWindow } from '../apis'
import { IGamepad, IGamepadButton, INavigator, IWindow } from '../apis'
import { store } from '../index'
import { Vector2 } from '../utils/math'
import { MockEventTarget } from '../utils/mock'
Expand All @@ -18,6 +18,17 @@ class MockNavigator extends MockEventTarget implements INavigator {

}

class MockGamepad implements IGamepad {

public index: number = 0
public buttons: IGamepadButton[] = []
public axes: number[] = []
public connected: boolean = true
public timestamp: number = 0
public mapping: string = 'standard'

}

function mockPack() {
const win = new MockWindow()
const nav = new MockNavigator()
Expand All @@ -44,19 +55,8 @@ describe('The `Gamepad` class', () => {
})

it('returns `true` after a gamepad was connected', () => {
nav.gamepads[0] = {
index: 0,
buttons: [
{
pressed: false,
},
],
axes: [],
connected: true,
timestamp: 0,
}

win.listeners.gamepadconnected({ gamepad: { index: 0 } })
nav.gamepads[0] = new MockGamepad()
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
expect(gamepad.isConnected()).to.equal(true)
})

Expand All @@ -66,16 +66,16 @@ describe('The `Gamepad` class', () => {
expect(gamepad.isConnected()).to.equal(false)
})

it('returns `true` again after a gamepad was connected', () => {
nav.gamepads[0] = {
index: 0,
buttons: [],
axes: [],
connected: true,
timestamp: 0,
}
it('returns `false` after a non-standard gamepad was connected', () => {
nav.gamepads[0] = new MockGamepad()
nav.gamepads[0].mapping = 'non-standard'
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
expect(gamepad.isConnected()).to.equal(false)
})

win.listeners.gamepadconnected({ gamepad: { index: 0 } })
it('returns `true` again after a gamepad was connected', () => {
nav.gamepads[0] = new MockGamepad()
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
expect(gamepad.isConnected()).to.equal(true)
})

Expand Down Expand Up @@ -123,9 +123,9 @@ describe('The `Gamepad` class', () => {
axes: [0, 0, 0, 0],
connected: true,
timestamp: 0,
mapping: 'standard',
}

win.listeners.gamepadconnected({ gamepad: { index: 0 } })
win.listeners.gamepadconnected({ gamepad: nav.gamepads[0] })
})

it('should set `store.preferGamepad` to `true`', () => {
Expand Down
6 changes: 4 additions & 2 deletions src/inputs/gamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ export class Gamepad {
this.window.addEventListener('gamepadconnected', ({ gamepad }) => {
/* istanbul ignore else */
if (!this.isConnected()) {
this.gamepadIndex = gamepad.index
store.preferGamepad = true
if (gamepad.mapping === 'standard') {
this.gamepadIndex = gamepad.index
store.preferGamepad = true
}
}
})

Expand Down

0 comments on commit 5407f15

Please sign in to comment.