-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (59 loc) · 1.9 KB
/
index.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
import $ from "jquery";
import jsGamepad from "./src/index";
jsGamepad.on("connected", (gamepad) => {
const $gamepad = $("<div />")
.addClass("gamepad")
.addClass("gamepad-" + gamepad.index);
$("<h2 />")
.text("Gamepad #" + gamepad.index + " " + gamepad.id + "")
.appendTo($gamepad);
$("<h3 />")
.addClass("secondary")
.text("Mapping: " + gamepad.mapping)
.appendTo($gamepad);
const $buttons = $("<div />")
.addClass("gamepad-buttons")
.appendTo($gamepad);
gamepad.buttons.forEach((button, buttonIndex) => {
const $gamepadButton = $("<div />")
.addClass("gamepad-button")
.addClass("gamepad-button-" + buttonIndex)
.text(buttonIndex)
.appendTo($buttons);
$("<div />")
.addClass("gamepad-button-value")
.appendTo($gamepadButton);
});
const $axes = $("<div />")
.addClass("gamepad-axes")
.appendTo($gamepad);
gamepad.axes.forEach((axis, axisIndex) => {
const $gamepadAxis = $("<div />")
.addClass("gamepad-axis")
.addClass("gamepad-axis-" + axisIndex)
.text(axisIndex)
.appendTo($axes);
$("<div />")
.addClass("gamepad-axis-value")
.appendTo($gamepadAxis);
});
$gamepad.appendTo(document.body);
}).on("disconnected", (gamepad) => {
$(".gamepad-" + gamepad.index).remove();
}).on("buttonPressed", ({ button, buttonIndex, gamepad }) => {
$(".gamepad-button-" + buttonIndex)
.addClass("pressed");
}).on("buttonChanged", ({ button, buttonIndex, gamepad }) => {
$(".gamepad-button-" + buttonIndex)
.find(".gamepad-button-value")
.text(button.value);
}).on("buttonReleased", ({ button, buttonIndex, gamepad }) => {
$(".gamepad-button-" + buttonIndex)
.removeClass("changed")
.removeClass("pressed");
}).on("axisChanged", ({ axis, axisIndex, gamepad }) => {
$(".gamepad-axis-" + axisIndex)
.find(".gamepad-axis-value")
.text(axis);
});
jsGamepad.watch();