-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathController.js
85 lines (64 loc) · 2.29 KB
/
Controller.js
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
import { Log } from './utils.js';
import { Angles } from './index.js';
const element = document.querySelector('#Controls');
const Title = element.querySelector('h1');
const Buttons = element.querySelector('#Buttons');
let Meters;
const _Meters = _Meters => Meters = _Meters;
export { _Meters as Meters };
const axes = '0 1 5 6'.split(' ');
let Last = [];
let Disconnected;
function Gamepad() {
Disconnected || requestAnimationFrame(Gamepad);
const [gamepad] = navigator.getGamepads();
gamepad.buttons.forEach(({ pressed }, i) =>
Buttons.childNodes[i].classList[pressed ? 'add' : 'remove']('bg-yellow-500'));
//console.log(gamepad.axes);
const Angles = axes.reduce((accum, cur, i) => {
let Axis = gamepad.axes[cur];
if (i === 3)
Axis = (1 - Axis) / 2;
const element = Meters[i];
element.value = Axis.toFixed(4);
accum.push(Axis);
return accum;
}, []);
if (!Angles().every((x, i) => Last[i] === x)) {
Last = Angles;
}
}
window.addEventListener("gamepadconnected", ({ gamepad }) => {
console.log(gamepad);
Title.innerText = gamepad.id;
gamepad.buttons.forEach((x, i) => {
const span = document.createElement("span");
span.innerText = i + ' ';
Buttons.appendChild(span);
});
Gamepad();
window.addEventListener("gamepadconnected", Gamepad);
}, { once: true });
window.addEventListener("gamepaddisconnected", () => Disconnected = true);
const Pressed = new Map();
//'keydown keyup'.split(' ').forEach(x => window[x] = event =>
//'INPUT TEXTAREA'.split(' ').includes(event.target.nodeName) && event.stopPropagation());
window.addEventListener("keydown", ({ key }) => {
const Up = 'qwertyuiop['.indexOf(key);
const Down = 'asdfghjkl;'.indexOf(key);
const Index = [Up, Down].find(x => x >= 0);
if (Index === undefined) return;
if (!Pressed.has(key)) {
const Axis = Meters[Index];
Axis.value = Axis[Up < 0 ? 'min' : 'max'];
Pressed.set(key, Axis);
}
console.log(key);
Angles(Array.prototype.map.call(Meters, x => x.value));
});
window.addEventListener("keyup", ({ key }) => {
if (!Pressed.has(key)) return;
const Axis = Pressed.get(key);
Axis.value = (Axis.min + Axis.max) / 2;
Pressed.delete(key);
});