-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchPad.jsx
323 lines (287 loc) · 10.7 KB
/
TouchPad.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { useRef, useState } from "react";
import mqtt from "precompiled-mqtt";
import Dropdown from 'react-dropdown';
const LEFT = "left";
const RIGHT = "right";
class Touch {
constructor(threshold) {
this.threshold = threshold;
}
newTouch(startx, starty) {
this.startx = startx;
this.starty = starty;
this.lastx = startx;
this.lasty = starty;
}
setClientXY(x, y) {
let sendMessage = false;
if (Math.abs(x - this.lastx) > this.threshold)
{
sendMessage = true;
this.lastx = x;
}
if (Math.abs(y - this.lasty) > this.threshold)
{
sendMessage = true;
this.lasty = y;
}
return sendMessage;
}
}
class TouchMessage {
constructor(threshold, messageCallback)
{
this.serverAddress = "127.0.0.1";
this.port = 80;
this.protocol = "ws";
this.messagCallback = messageCallback;
this.mqttClient = null;
this.useSSL = true;
this.leftTouch = new Touch(threshold);
this.rightTouch = new Touch(threshold);
}
connect()
{
const us = {
clientId: Math.random().toString(16),
/* username: mqtt.username, */
/* password: mqtt.password, */
useSSL: this.useSSL,
/* onSuccess: onAction, */
/* onFailure: onAction, */
/* protocolId: 'MQTT', */
/* protocolVersion: 3, */
rejectUnauthorized: false,
clean: true,
/* reconnectPeriod: 20000, */
/* connectTimeout: 30 * 1000, */
protocol: this.protocol,
};
console.log("Connecting with: " + JSON.stringify(us));
// If client exists, make sure it's disconnected
if (this.mqttClient !== null)
{
this.mqttClient.end(true);
}
this.mqttClient = mqtt.connect(this.getFullAddress(), us);
// publish messages to UI
["connect", "end", "error", "close", "reconnect", "disconnect", "message"].forEach((val) => {
this.mqttClient.on(val, () => {
this.messagCallback(val);
});
});
}
newTouch(startx, starty, side)
{
if (side === LEFT) {
this.leftTouch.newTouch(startx, starty);
} else if (side === RIGHT) {
this.rightTouch.newTouch(startx, starty);
}
}
setClientXY(x, y, side)
{
let touch;
if (side === LEFT) {
touch = this.leftTouch;
} else if (side === RIGHT) {
touch = this.rightTouch;
}
let sendMessage = touch.setClientXY(x, y);
if (sendMessage)
{
let _x = (touch.lasty - touch.starty) / 100;
let _y = (touch.lastx - touch.startx) / 100;
// mqtt send with lastx and lasty
console.log(`${side} ${this.serverAddress} ${this.port} ${_y} ${_x}`)
if (this.mqttClient !== null && this.mqttClient.connected)
{
if (side === LEFT) {
this.mqttClient.publish("getReal3D/strafeAxis", _y.toString(5));
this.mqttClient.publish("getReal3D/forwardAxis", _x.toString(5));
} else if (side === RIGHT) {
this.mqttClient.publish("getReal3D/yawAxis", _y.toString(5));
this.mqttClient.publish("getReal3D/pitchAxis", _x.toString(5));
}
}
}
}
wandButtonUp()
{
this.mqttClient.publish("getReal3D/wandButtonUp", "True");
}
wandButtonDown()
{
this.mqttClient.publish("getReal3D/wandButtonDown", "True");
}
setProtcol(protocol)
{
this.protocol = protocol;
this.useSSL = this.protocol === "wss";
}
setPort(port) {
this.port = port;
}
setAddress(address) {
this.serverAddress = address;
}
getFullAddress() {
return `${this.protocol}://${this.serverAddress}:${this.port}`;
}
}
function TouchPad()
{
const [message, setMessage] = useState("");
const [touchMessage] = useState(new TouchMessage(5, setMessage));
const [textFeedback, setTextFeedback] = useState("Touch");
const [workingAddress, setWorkingAddress] = useState(touchMessage.getFullAddress());
const [startedRight, setStartedRight] = useState(false);
const [startedLeft, setStartedLeft] = useState(false);
const touchPadRef1 = useRef();
const touchPadRef2 = useRef();
function setProtocol(val)
{
touchMessage.setProtcol(val.value);
setWorkingAddress(touchMessage.getFullAddress());
}
function setPort()
{
let val = prompt("Port", 80);
touchMessage.setPort(Number(val));
setWorkingAddress(touchMessage.getFullAddress());
}
function setAddress()
{
let val = prompt("Address", "127.0.0.1");
touchMessage.setAddress(val);
setWorkingAddress(touchMessage.getFullAddress());
}
function connect()
{
touchMessage.connect();
}
function wandButtonDown()
{
touchMessage.wandButtonDown();
}
function wandButtonUp()
{
touchMessage.wandButtonUp();
}
function getClientXY(e)
{
let x = 0, y = 0 ;
if (e.type === "touchstart" || e.type === "touchmove")
{
let touch = e.touches[0];
x = touch.clientX;
y = touch.clientY;
}
else
{
x = e.clientX;
y = e.clientY;
}
return [x, y];
}
function onStartRight(e)
{
// Adding this since preventDefault cannot be used and avoid this being called again.
// Also, setting this as a non-passive callback intefers with other callbacks.
if (startedRight === false)
{
setStartedRight(true);
let [x, y] = getClientXY(e);
setTextFeedback(`Touch at ${x} ${y} `);
touchMessage.newTouch(x, y, RIGHT);
}
}
function onEndRight()
{
if (startedRight)
{
setStartedRight(false);
setTextFeedback("Touch");
}
}
function onMoveRight(e)
{
if (startedRight)
{
let [x, y] = getClientXY(e);
setTextFeedback(`Touch at ${x} ${y} `);
touchMessage.setClientXY(x, y, RIGHT);
}
}
function onStartLeft(e)
{
// Adding this since preventDefault cannot be used and avoid this being called again.
// Also, setting this as a non-passive callback intefers with other callbacks.
if (startedLeft === false)
{
setStartedLeft(true);
let [x, y] = getClientXY(e);
setTextFeedback(`Touch at ${x} ${y} `);
touchMessage.newTouch(x, y, LEFT);
}
}
function onEndLeft()
{
if (startedLeft)
{
setStartedLeft(false);
setTextFeedback("Touch");
}
}
function onMoveLeft(e)
{
if (startedLeft)
{
let [x, y] = getClientXY(e);
setTextFeedback(`Touch at ${x} ${y} `);
touchMessage.setClientXY(x, y, LEFT);
}
}
return (
<div className="w-full overscroll-contain">
<p className="center">{textFeedback}</p>
<p className="center">Connection state: {message}</p>
<p className="center">{workingAddress}</p>
<div className="flex space-x-2 center place-content-center p-2">
<div className="">
Set Protocol
<Dropdown placeholder={touchMessage.protocol} options={["wss", "ws"]} placeholderClassName="inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out" onChange={setProtocol} />
</div>
<button className="inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out" onClick={setAddress}> Set Address</button>
<button className="inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out" onClick={setPort}>Set Port</button>
<button className="inline-block px-6 py-2.5 bg-blue-600 text-white font-medium text-xs rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-blue-800 active:shadow-lg transition duration-150 ease-in-out" onClick={connect}>Connect</button>
</div>
<div className="flex space-x-2 p-2">
<div ref={touchPadRef1}
className="border-solid border-2 border-sky-500 w-1/2 h-96 overscroll-contain overscroll-y-contain"
onTouchMove={onMoveLeft}
onTouchStart={onStartLeft}
onTouchEnd={onEndLeft}
onMouseDown={onStartLeft}
onMouseUp={onEndLeft}
onMouseMove={onMoveLeft}>
<div className="select-none">left touch pad (forward/strafe)</div>
</div>
<div ref={touchPadRef2}
className="border-solid border-2 border-sky-500 w-1/2 h-96 overscroll-contain overscroll-y-contain"
onTouchMove={onMoveRight}
onTouchStart={onStartRight}
onTouchEnd={onEndRight}
onMouseDown={onStartRight}
onMouseUp={onEndRight}
onMouseMove={onMoveRight}>
<div className="select-none">right touch pad (pitch/yaw)</div>
</div >
</div>
<div className="flex space-x-2 center place-content-center p-2">
<button className="inline-block w-48 h-16 px-6 py-2.5 bg-green-800 text-white font-large text-s rounded shadow-md hover:bg-blue-700 hover:shadow-lg focus:bg-blue-700 focus:shadow-lg focus:outline-none focus:ring-0 active:bg-green-900 active:shadow-lg transition duration-150 ease-in-out" onMouseDown={wandButtonDown} onMouseUp={wandButtonUp} onTouchStart={wandButtonDown} onTouchEnd={wandButtonUp}>Wand button</button>
</div>
</div>
);
}
export default TouchPad;