Skip to content

Commit 583dbff

Browse files
authored
Merge pull request #8 from IBM/fix-add-more-ts
Fix add more ts
2 parents 9f29ad7 + 7d652ce commit 583dbff

File tree

9 files changed

+353
-87
lines changed

9 files changed

+353
-87
lines changed

docs/API.md

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
- [rumble(low_frequency_rumble, high_frequency_rumble, duration_ms, player)](#rumble)
3030
- [rumbleTriggers(left_rumble, right_rumble, duration_ms, player)](#rumbleTriggers)
3131

32+
---
33+
3234
## error
3335

3436
Emitted if something goes wrong. The `message` is set from [SDL_GetError](https://wiki.libsdl.org/SDL_GetError)
@@ -106,8 +108,8 @@ These names are defined in [SDL source code](https://github.com/libsdl-org/SDL/b
106108
For example:
107109

108110
```js
109-
gamecontroller.on("a:down", (data) => console.log("a pressed", data));
110-
gamecontroller.on("b", (data) => console.log("b up or down", data));
111+
gamecontroller.on('a:down', (data) => console.log('a pressed', data));
112+
gamecontroller.on('b', (data) => console.log('b up or down', data));
111113
```
112114

113115
## controller-button-up
@@ -130,8 +132,8 @@ See [controller-button-down](#controller-button-down)
130132
For example:
131133

132134
```js
133-
gamecontroller.on("a:up", (data) => console.log("a pressed", data));
134-
gamecontroller.on("b", (data) => console.log("b up or down", data));
135+
gamecontroller.on('a:up', (data) => console.log('a pressed', data));
136+
gamecontroller.on('b', (data) => console.log('b up or down', data));
135137
```
136138

137139
## controller-axis-motion
@@ -162,8 +164,8 @@ These names are defined in [SDL source code](https://github.com/libsdl-org/SDL/b
162164
For example:
163165

164166
```js
165-
gamecontroller.on("leftx", (data) =>
166-
console.log("left stick moved in the x direction", data)
167+
gamecontroller.on('leftx', (data) =>
168+
console.log('left stick moved in the x direction', data),
167169
);
168170
```
169171

@@ -183,7 +185,7 @@ Emitted when a new game controller has been inserted into the system
183185
serial_number: 'none', // SDL 2.0.14+
184186
effects_supported: false,
185187
haptic: false,
186-
has_leds: 'false', // SDL 2.0.14+
188+
has_leds: false, // SDL 2.0.14+
187189
num_touchpads: 0, // SDL 2.0.14+
188190
has_accelerometer: false, // SDL 2.0.14+
189191
has_gyroscope: false, // SDL 2.0.14+
@@ -237,8 +239,8 @@ An alias for this event is also emitted with the event name set to either `gyros
237239
For example:
238240

239241
```js
240-
gamecontroller.on("gyroscope", (data) =>
241-
console.log("gyroscope updated", data)
242+
gamecontroller.on('gyroscope', (data) =>
243+
console.log('gyroscope updated', data),
242244
);
243245
```
244246

@@ -371,6 +373,10 @@ Emitted when the controller triggers are successfully rumbled.
371373
}
372374
```
373375

376+
## Functions
377+
378+
---
379+
374380
## enableGyroscope
375381

376382
`enableGyroscope(enable, player)`
@@ -382,13 +388,13 @@ Emitted when the controller triggers are successfully rumbled.
382388

383389
```js
384390
// Enable Gyroscope (if supported) when X button is pressed
385-
gamecontroller.on("x:down", (data) => {
391+
gamecontroller.on('x:down', (data) => {
386392
console.log(`player ${data.player} pressed X`);
387393
gamecontroller.enableGyroscope(true);
388394
});
389395

390396
// Disable Gyroscope (if supported) when X button is released
391-
gamecontroller.on("x:up", (data) => {
397+
gamecontroller.on('x:up', (data) => {
392398
console.log(`player ${data.player} released X`);
393399
gamecontroller.enableGyroscope(false);
394400
});
@@ -405,13 +411,13 @@ gamecontroller.on("x:up", (data) => {
405411

406412
```js
407413
// Enable Accelerometer (if supported) when Y button is pressed
408-
gamecontroller.on("y:down", (data) => {
414+
gamecontroller.on('y:down', (data) => {
409415
console.log(`player ${data.player} pressed Y`);
410416
gamecontroller.enableAccelerometer(true, data.player);
411417
});
412418

413419
// Disable Accelerometer (if supported) when Y button is released
414-
gamecontroller.on("y:up", (data) => {
420+
gamecontroller.on('y:up', (data) => {
415421
console.log(`player ${data.player} released Y`);
416422
gamecontroller.enableAccelerometer(false, data.player);
417423
});
@@ -431,7 +437,7 @@ gamecontroller.on("y:up", (data) => {
431437
Example:
432438

433439
```js
434-
gamecontroller.on("leftstick:down", (data) => {
440+
gamecontroller.on('leftstick:down', (data) => {
435441
gamecontroller.setLeds(0x0f, 0x62, 0xfe);
436442
});
437443
```
@@ -451,7 +457,7 @@ Example:
451457

452458
```js
453459
// Rumble (if supported) when A button is pressed
454-
gamecontroller.on("a:down", (data) => {
460+
gamecontroller.on('a:down', (data) => {
455461
gamecontroller.rumble(60000, 40000, 100, data.player);
456462
});
457463
```
@@ -471,7 +477,7 @@ Example:
471477

472478
```js
473479
// Rumble triggers (if supported) when B button is pressed
474-
gamecontroller.on("b:down", (data) => {
480+
gamecontroller.on('b:down', (data) => {
475481
gamecontroller.rumbleTriggers(40000, 40000, 100, data.player);
476482
});
477483
```

index.d.ts

Lines changed: 119 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,82 @@ declare const gamecontroller: Gamecontroller;
22

33
export default gamecontroller;
44

5-
export type Message = { message: string };
6-
export type Player = { player: number };
7-
export type Error = Message & Player & { operation: string };
8-
export type Value = { value: number };
9-
export type TimeStamp = { timestamp: number };
10-
export type OnButtonPress = Message &
11-
Player & { button: ButtonType; pressed: boolean };
5+
export type Message = {message: string};
6+
export type Player = {player?: number};
7+
export type Error = Message & Player & {operation: string};
8+
export type Warning = Message & {
9+
operation: string;
10+
elapsed_ms?: number;
11+
poll_number?: number;
12+
};
13+
export type SdlInit = {
14+
compiled_against_SDL_version: string;
15+
linkeded_against_SDL_version: string;
16+
};
17+
export type DeviceAdded = Message &
18+
Player & {
19+
which: number;
20+
name: string;
21+
vendor_id: number;
22+
product_id: number;
23+
serial_number?: string;
24+
effects_supported: boolean;
25+
haptic: boolean;
26+
has_leds?: boolean;
27+
num_touchpads?: number;
28+
has_accelerometer?: boolean;
29+
has_gyroscope?: boolean;
30+
has_rumble?: boolean;
31+
has_rumble_trigger?: boolean;
32+
};
33+
export type SensorUpdate = Message & {
34+
sensor: 'gyroscope' | 'accelerometer';
35+
x: number;
36+
y: number;
37+
z: number;
38+
};
39+
export type SensorUpdateEvents =
40+
| 'controller-sensor-update'
41+
| 'gyroscope'
42+
| 'accelerometer';
43+
export type SensorStateEvents =
44+
| 'accelerometer:enabled'
45+
| 'accelerometer:disabled'
46+
| 'gyroscope:enabled'
47+
| 'gyroscope:disabled';
48+
49+
export type TouchpadUpdate = Message & {
50+
touchpad: number;
51+
finger: number;
52+
x: number;
53+
y: number;
54+
pressure: number;
55+
};
56+
export type TouchpadEvents =
57+
| 'controller-touchpad-down'
58+
| 'controller-touchpad-up'
59+
| 'controller-touchpad-motion';
1260

13-
export type OnTriggerPress = Omit<OnButtonPress, 'pressed'> & {
14-
value: number;
15-
timestamp: number;
61+
export type DeviceUpdated = Message & {
62+
which: number;
1663
};
17-
export type StickType = 'leftx' | 'lefty' | 'rightx' | 'righty';
64+
export type DeviceUpdateEvents =
65+
| 'controller-device-removed'
66+
| 'controller-device-remapped';
1867

19-
export type OnStickMoveData = Message &
20-
Player &
21-
Value &
22-
TimeStamp & { button: StickType };
68+
export type AxisType =
69+
| 'leftx'
70+
| 'lefty'
71+
| 'rightx'
72+
| 'righty'
73+
| 'lefttrigger'
74+
| 'righttrigger';
75+
76+
export type AxisMotionData = Message &
77+
Player & {button: AxisType; value: number; timestamp: number};
78+
79+
export type ButtonPress = Message &
80+
Player & {button: ButtonType; pressed: boolean};
2381

2482
export type ButtonType =
2583
| 'a'
@@ -42,38 +100,68 @@ export type ButtonType =
42100
export type ButtonTypeWithUpsAndDowns =
43101
| `${ButtonType}:up`
44102
| `${ButtonType}:down`
45-
| ButtonType;
103+
| ButtonType
104+
| 'controller-button-up'
105+
| 'controller-button-down';
46106

47107
export type ControllerButtonDown = Message &
48-
Player & { button: ButtonType; pressed: boolean };
108+
Player & {button: ButtonType; pressed: boolean};
49109

50-
export type FieldUpdateHandler = (update: any) => unknown;
51-
export type Handlers = Record<string, FieldUpdateHandler>;
52110
export type CallBack<T = Record<string, unknown>> = (data: T) => void;
53111

54112
type ON<TEventName, TCallBack> = (
55113
eventName: TEventName,
56-
callBack: CallBack<TCallBack>
114+
callBack: CallBack<TCallBack>,
57115
) => void;
58116

59-
type OnStickMove = ON<StickType, OnStickMoveData>;
60-
type OnButtonPressCall = ON<ButtonTypeWithUpsAndDowns, OnButtonPress>;
61117
type OnErrorCall = ON<'error', Error>;
62-
type OnControllerButtonUp = ON<'controller-button-up', OnButtonPress>;
63-
type OnControllerButtonDown = ON<'controller-button-down', OnButtonPress>;
64-
type OnTrigger = ON<'righttrigger' | 'lefttrigger', OnTriggerPress>;
118+
type OnWarningCall = ON<'warning', Warning>;
119+
type OnSdlInitCall = ON<'sdl-init', SdlInit>;
120+
type OnDeviceAddedCall = ON<'controller-device-added', DeviceAdded>;
121+
type OnDeviceUpdated = ON<DeviceUpdateEvents, DeviceUpdated>;
122+
type OnAxisUpdate = ON<AxisType, AxisMotionData>;
123+
type OnButtonPressCall = ON<ButtonTypeWithUpsAndDowns, ButtonPress>;
124+
type OnSensorUpdate = ON<SensorUpdateEvents, SensorUpdate>;
125+
type OnSensorStateChange = ON<SensorStateEvents, Player>;
126+
type OnTouchpadUpdate = ON<TouchpadEvents, TouchpadUpdate>;
127+
type OnLed = ON<'led', Player>;
128+
type OnRumbled = ON<'rumbled', Player>;
129+
type OnRumbledTriggers = ON<'rumbled-triggers', Player>;
65130

66131
type AllOnOptions = OnButtonPressCall &
67-
OnStickMove &
132+
OnAxisUpdate &
68133
OnErrorCall &
69-
OnControllerButtonUp &
70-
OnControllerButtonDown &
71-
OnTrigger;
134+
OnWarningCall &
135+
OnSdlInitCall &
136+
OnDeviceAddedCall &
137+
OnDeviceUpdated &
138+
OnSensorUpdate &
139+
OnSensorStateChange &
140+
OnTouchpadUpdate &
141+
OnLed &
142+
OnRumbled &
143+
OnRumbledTriggers;
72144

73145
type Gamecontroller = {
146+
enableGyroscope: (enable?: boolean, player?: number) => void;
147+
enableAccelerometer: (enable?: boolean, player?: number) => void;
148+
setLeds: (
149+
red?: number,
150+
green?: number,
151+
blue?: number,
152+
player?: number,
153+
) => void;
154+
rumble: (
155+
low_frequency_rumble?: number,
156+
high_frequency_rumble?: number,
157+
duration_ms?: number,
158+
player?: number,
159+
) => void;
74160
rumbleTriggers: (
75-
buttonType: ButtonType,
76-
callBack: CallBack<OnTriggerPress>
161+
left_rumble?: number,
162+
right_rumble?: number,
163+
duration_ms?: number,
164+
player?: number,
77165
) => void;
78166
on: AllOnOptions;
79167
};

package-lock.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
{
22
"name": "sdl2-gamecontroller",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"scripts": {
5-
"install": "cmake-js compile"
5+
"install": "cmake-js compile",
6+
"lint": "prettier --write *.js *.ts test/*js test/*.ts"
67
},
78
"dependencies": {
89
"bindings": "^1.5.0",
@@ -23,5 +24,10 @@
2324
"dualshock"
2425
],
2526
"author": "David Nixon",
26-
"license": "apache-2.0"
27+
"license": "apache-2.0",
28+
"devDependencies": {
29+
"prettier": "^2.5.1",
30+
"prettier-config-ibm": "^1.0.1"
31+
},
32+
"prettier": "prettier-config-ibm"
2733
}

0 commit comments

Comments
 (0)