mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-10-06 07:47:04 +00:00
Merge pull request #29948 from Lange/node-gamepad
Add typings for node-gamepad
This commit is contained in:
Vendored
+33
@@ -0,0 +1,33 @@
|
||||
// Type definitions for node-gamepad 1.5
|
||||
// Project: https://github.com/creationix/node-gamepad#readme
|
||||
// Definitions by: Alex Van Camp <https://github.com/Lange>
|
||||
// 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[];
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
/// <reference types="node" />
|
||||
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();
|
||||
@@ -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"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "dtslint/dt.json"
|
||||
}
|
||||
Reference in New Issue
Block a user