-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamepad-support.user.js
65 lines (57 loc) · 1.73 KB
/
gamepad-support.user.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
// ==UserScript==
// @name STBG Controller Support Script
// @namespace https://stb-gaming.github.io
// @version 0.2.5
// @description A script that uses the JS Gamepad API to add controller support to Denki's online Sky Games
// @author cobaltgit
// @run-at document-start
// @match https://denki.co.uk/sky/*
// @match https://beehive-bedlam.com/*
// @match https://stb-gaming.github.io/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=denki.co.uk
// @require https://github.com/STB-Gaming/userscripts/raw/master/sky-remote.user.js
// ==/UserScript==
(function () {
'use strict';
const
uWindow = typeof unsafeWindow != 'undefined' ? unsafeWindow : window,
buttonMapping = {
12: "up",
13: "down",
14: "left",
15: "right",
9: "backup",
0: "select",
1: "red",
2: "blue",
3: "yellow",
8: "green",
11: "help"
};
let
start,
gamepad,
lastPressed = []; // track button states
function mainLoop() { // todo: add analogue stick support (axes should map to arrow buttons)
let gamepads = navigator.getGamepads();
if (!gamepads) return;
gamepad = gamepads[0];
for (const index of Object.keys(buttonMapping)) {
if (lastPressed[index] != gamepad.buttons[index].pressed) {
if (gamepad.buttons[index].pressed) {
SkyRemote.holdButton(buttonMapping[index]);
} else {
SkyRemote.releaseButton(buttonMapping[index]);
}
lastPressed[index] = gamepad.buttons[index].pressed;
}
}
start = uWindow.requestAnimationFrame(mainLoop);
}
uWindow.addEventListener("gamepadconnected", event => {
mainLoop();
});
uWindow.addEventListener("gamepaddisconnected", event => {
uWindow.cancelAnimationFrame(start);
});
})();