dualsense-ts 5.0.4 → 5.0.27
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,19 +8,17 @@ This module provides a natural interface for your DualSense controller.
|
|
|
8
8
|
|
|
9
9
|
[This package is distributed via npm](https://npmjs.org/package.dualsense-ts). Install it the usual way:
|
|
10
10
|
|
|
11
|
-
- `yarn add dualsense-ts`
|
|
12
11
|
- `npm add dualsense-ts`
|
|
13
12
|
|
|
14
|
-
In the browser, `dualsense-ts` has zero dependencies and relies on the
|
|
13
|
+
In the browser, `dualsense-ts` has zero dependencies and relies on the [WebHID API](https://developer.mozilla.org/en-US/docs/Web/API/WebHID_API). At this time, only Chrome, Edge, and Opera are compatible.
|
|
15
14
|
|
|
16
|
-
In
|
|
15
|
+
In node.js, `dualsense-ts` relies on `node-hid` as a peer dependency, so you'll need to add it as well:
|
|
17
16
|
|
|
18
|
-
- `yarn add node-hid`
|
|
19
17
|
- `npm add node-hid`
|
|
20
18
|
|
|
21
19
|
### Connecting
|
|
22
20
|
|
|
23
|
-
When you construct a `new Dualsense()`,
|
|
21
|
+
When you construct a `new Dualsense()`, it will begin searching for a controller. If it finds one, it will connect automatically.
|
|
24
22
|
|
|
25
23
|
```typescript
|
|
26
24
|
import { Dualsense } from "dualsense-ts";
|
|
@@ -29,7 +27,7 @@ import { Dualsense } from "dualsense-ts";
|
|
|
29
27
|
const controller = new Dualsense();
|
|
30
28
|
```
|
|
31
29
|
|
|
32
|
-
If
|
|
30
|
+
If the device becomes disconnected, `dualsense-ts` will quietly wait for it to come back. You can monitor the connection status with `controller.connection` using any of the Input APIs listed in the next section.
|
|
33
31
|
|
|
34
32
|
```typescript
|
|
35
33
|
const connected = controller.connection.active
|
|
@@ -112,40 +110,73 @@ for await (const { pressure } of controller.left.trigger) {
|
|
|
112
110
|
}
|
|
113
111
|
```
|
|
114
112
|
|
|
115
|
-
### React
|
|
113
|
+
### With React
|
|
116
114
|
|
|
117
|
-
|
|
115
|
+
Creating a controller interface with `new Dualsense()` is much too expensive to be done at render time. You can create a [Context](https://reactjs.org/docs/context.html) to share the controller state with your components:
|
|
118
116
|
|
|
119
117
|
```typescript
|
|
120
|
-
//
|
|
121
|
-
import
|
|
118
|
+
// DualsenseContext.tsx
|
|
119
|
+
import { createContext } from "react";
|
|
122
120
|
import { Dualsense } from "dualsense-ts";
|
|
123
121
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
122
|
+
const controller = new Dualsense();
|
|
123
|
+
export const DualsenseContext = createContext(controller);
|
|
124
|
+
DualsenseContext.displayName = "DualsenseContext";
|
|
127
125
|
```
|
|
128
126
|
|
|
129
|
-
|
|
127
|
+
The user will need to grant permission before we can access new devices using the WebHID API. The `Dualsense` class provides a callback that can be used as a handler for `onClick` or [other user-triggered events](https://developer.mozilla.org/en-US/docs/Web/Security/User_activation):
|
|
130
128
|
|
|
131
129
|
```typescript
|
|
132
|
-
//
|
|
133
|
-
import
|
|
134
|
-
import {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
130
|
+
// PermissionComponent.tsx
|
|
131
|
+
import { useContext } from "react";
|
|
132
|
+
import { DualsenseContext } from "./DualsenseContext";
|
|
133
|
+
|
|
134
|
+
export const RequestController = () => {
|
|
135
|
+
const controller = useContext(DualsenseContext);
|
|
136
|
+
return (
|
|
137
|
+
<button
|
|
138
|
+
text="Grant Permission"
|
|
139
|
+
onClick={controller.hid.provider.getRequest()}
|
|
140
|
+
/>
|
|
143
141
|
);
|
|
142
|
+
};
|
|
143
|
+
```
|
|
144
144
|
|
|
145
|
-
|
|
146
|
-
|
|
145
|
+
Now components with access to this context can enjoy the shared `dualsense-ts` interface:
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
// ConnectionComponent.tsx
|
|
149
|
+
import { useContext, useEffect, useState } from "react";
|
|
150
|
+
import { DualsenseContext } from "./DualsenseContext";
|
|
151
|
+
|
|
152
|
+
export const ControllerConnection = () => {
|
|
153
|
+
const controller = useContext(DualsenseContext);
|
|
154
|
+
const [connected, setConnected] = useState(controller.connection.state);
|
|
155
|
+
const [triangle, setTriangle] = useState(controller.triangle.state);
|
|
156
|
+
|
|
157
|
+
useEffect(() => {
|
|
158
|
+
controller.connection.on("change", ({ state }) => setConnected(state));
|
|
159
|
+
controller.triangle.on("change", ({ state }) => setTriangle(state));
|
|
160
|
+
}, []);
|
|
161
|
+
|
|
162
|
+
return (
|
|
163
|
+
<p dir={triangle ? "ltr" : "rtl"}>{`Controller ${
|
|
164
|
+
state ? "" : "dis"
|
|
165
|
+
}connected`}</p>
|
|
166
|
+
);
|
|
167
|
+
};
|
|
147
168
|
```
|
|
148
169
|
|
|
170
|
+
### It's not working
|
|
171
|
+
|
|
172
|
+
`controller.hid` manages the connection to the device and provides useful error events:
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
controller.hid.on("error", console.error);
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Please open an issue Github if you're having trouble.
|
|
179
|
+
|
|
149
180
|
## Migration Guide
|
|
150
181
|
|
|
151
182
|
`dualsense-ts` uses semantic versioning. For more info on breaking changes, [check out the migration guide](MIGRATION_GUIDE.md).
|