From 32f4a9d1d437b27f971ecab20740a02cf2341429 Mon Sep 17 00:00:00 2001 From: Alex Van Camp Date: Sun, 21 Oct 2018 18:21:47 -0500 Subject: [PATCH] Add typings for node-gamepad --- types/node-gamepad/index.d.ts | 33 ++++++++++++ types/node-gamepad/node-gamepad-tests.ts | 69 ++++++++++++++++++++++++ types/node-gamepad/tsconfig.json | 23 ++++++++ types/node-gamepad/tslint.json | 3 ++ 4 files changed, 128 insertions(+) create mode 100644 types/node-gamepad/index.d.ts create mode 100644 types/node-gamepad/node-gamepad-tests.ts create mode 100644 types/node-gamepad/tsconfig.json create mode 100644 types/node-gamepad/tslint.json diff --git a/types/node-gamepad/index.d.ts b/types/node-gamepad/index.d.ts new file mode 100644 index 0000000000..77570a5218 --- /dev/null +++ b/types/node-gamepad/index.d.ts @@ -0,0 +1,33 @@ +// Type definitions for node-gamepad 1.5 +// Project: https://github.com/creationix/node-gamepad#readme +// Definitions by: Alex Van Camp +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.4 + +import { EventEmitter } from 'events'; + +declare const nodeGamepad: NodeGamepad; +export = nodeGamepad; + +interface NodeGamepad extends EventEmitter { + init(): void; + shutdown(): void; + numDevices(): number; + deviceAtIndex(deviceIndex: number): GamepadInstance; + detectDevices(): void; + processEvents(): void; + + on(event: 'attach', listener: (deviceID: number, device: GamepadInstance) => void): this; + on(event: 'remove', listener: (deviceID: number) => void): this; + on(event: 'down' | 'up', listener: (deviceID: number, buttonID: number, timestamp: number) => void): this; + on(event: 'move', listener: (deviceID: number, axisID: number, value: number, lastValue: number, timestamp: number) => void): this; +} + +interface GamepadInstance { + deviceID: number; + description: string; + vendorID: number; + productID: number; + axisStates: number[]; + buttonStates: boolean[]; +} diff --git a/types/node-gamepad/node-gamepad-tests.ts b/types/node-gamepad/node-gamepad-tests.ts new file mode 100644 index 0000000000..1d2d24e788 --- /dev/null +++ b/types/node-gamepad/node-gamepad-tests.ts @@ -0,0 +1,69 @@ +/// +import gamepad = require('node-gamepad'); + +// Initialize the library +gamepad.init(); + +// List the state of all currently attached devices +for (let i = 0, l = gamepad.numDevices(); i < l; i++) { + console.log(i, gamepad.deviceAtIndex(i)); +} + +// Create a game loop and poll for events +setInterval(gamepad.processEvents, 16); +// Scan for new gamepads as a slower rate +setInterval(gamepad.detectDevices, 500); + +// Listen for new gamepads being attached +gamepad.on('attach', (deviceID, device) => { + console.log('attach', { + deviceID, + device: { + deviceID: device.deviceID, + description: device.description, + vendorID: device.vendorID, + productID: device.productID, + axisStates: device.axisStates, + buttonStates: device.buttonStates, + } + }); +}); + +// Listen for new gamepads being removed +gamepad.on('remove', deviceID => { + console.log('remove', { + deviceID + }); +}); + +// Listen for button down events on all gamepads +gamepad.on('up', (deviceID, buttonID, timestamp) => { + console.log('up', { + deviceID, + buttonID, + timestamp + }); +}); + +// Listen for button down events on all gamepads +gamepad.on('down', (deviceID, buttonID, timestamp) => { + console.log('down', { + deviceID, + buttonID, + timestamp + }); +}); + +// Listen for move events on all gamepads +gamepad.on('move', (deviceID, axisID, value, lastValue, timestamp) => { + console.log('move', { + deviceID, + axisID, + value, + lastValue, + timestamp + }); +}); + +// Shutdown the library +gamepad.shutdown(); diff --git a/types/node-gamepad/tsconfig.json b/types/node-gamepad/tsconfig.json new file mode 100644 index 0000000000..7546894e6d --- /dev/null +++ b/types/node-gamepad/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "node-gamepad-tests.ts" + ] +} diff --git a/types/node-gamepad/tslint.json b/types/node-gamepad/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/node-gamepad/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}