1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00

Final overhaul to gamepad handling

This commit is contained in:
ferferga 2020-02-28 10:09:46 +01:00
parent 04a3505672
commit 8a200e54af
2 changed files with 53 additions and 18 deletions

View file

@ -251,13 +251,14 @@ require(['apphost'], function (appHost) {
} }
} }
var inputLoopTimer;
function runInputLoop() { function runInputLoop() {
// Get the latest gamepad state. // Get the latest gamepad state.
var gamepads = navigator.getGamepads(); var gamepads = navigator.getGamepads();
for (var i = 0, len = gamepads.length; i < len; i++) { for (var i = 0, len = gamepads.length; i < len; i++) {
var gamepad = gamepads[i]; var gamepad = gamepads[i];
if (!gamepad) { if (!gamepad) {
return; continue;
} }
// Iterate through the axes // Iterate through the axes
var axes = gamepad.axes; var axes = gamepad.axes;
@ -279,8 +280,7 @@ require(['apphost'], function (appHost) {
} }
// Iterate through the buttons to see if Left thumbstick, DPad, A and B are pressed. // Iterate through the buttons to see if Left thumbstick, DPad, A and B are pressed.
var buttons = gamepad.buttons; var buttons = gamepad.buttons;
var j; for (var j = 0, len = buttons.length; j < len; j++) {
for (j = 0, len = buttons.length; j < len; j++) {
if (ProcessedButtons.indexOf(j) !== -1) { if (ProcessedButtons.indexOf(j) !== -1) {
if (buttons[j].pressed) { if (buttons[j].pressed) {
switch (j) { switch (j) {
@ -347,10 +347,50 @@ require(['apphost'], function (appHost) {
} }
} }
// Schedule the next one // Schedule the next one
requestAnimationFrame(runInputLoop); inputLoopTimer = requestAnimationFrame(runInputLoop);
} }
runInputLoop(); function startInputLoop() {
if (!inputLoopTimer) {
runInputLoop();
}
}
function stopInputLoop() {
cancelAnimationFrame(inputLoopTimer);
inputLoopTimer = undefined;
}
function isGamepadConnected() {
var gamepads = navigator.getGamepads();
for (var i = 0, len = gamepads.length; i < len; i++) {
var gamepad = gamepads[i];
if (gamepad) {
return true;
}
}
return false;
}
function attachGamepad(e) {
if (isGamepadConnected()) {
console.log("Gamepad connected! Starting input loop");
startInputLoop();
}
}
function dettachGamepad(e) {
if (!isGamepadConnected()) {
console.log("Gamepad disconnected! No other gamepads are connected, stopping input loop");
stopInputLoop();
} else {
console.log("Gamepad disconnected! There are gamepads still connected.");
}
}
// Event Listeners for any change in gamepads' state.
window.addEventListener("gamepaddisconnected", dettachGamepad);
window.addEventListener("gamepadconnected", attachGamepad);
// The gamepadInputEmulation is a string property that exists in JavaScript UWAs and in WebViews in UWAs. // The gamepadInputEmulation is a string property that exists in JavaScript UWAs and in WebViews in UWAs.
// It won't exist in Win8.1 style apps or browsers. // It won't exist in Win8.1 style apps or browsers.

View file

@ -145,7 +145,11 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager)
} }
}); });
} }
// For hiding the mouse while playbackç
require(["components/input/mouseManager"]);
// Gamepad initialization. No script is required if no gamepads are at boot time, saving a bit of resources._10k
// Whenever the gamepad is connected, we hand all the control of the gamepad to gamepadtokey.js by removing the event handler
function isGamepadConnected() { function isGamepadConnected() {
var gamepads = navigator.getGamepads(); var gamepads = navigator.getGamepads();
for (var i = 0, len = gamepads.length; i < len; i++) { for (var i = 0, len = gamepads.length; i < len; i++) {
@ -157,25 +161,16 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager)
return false; return false;
} }
function attachGamepad(e) { function attachGamepadScript(e) {
if (isGamepadConnected()) { if (isGamepadConnected()) {
require(["components/input/gamepadtokey"]);
console.log("Gamepad connected! Attaching gamepadtokey.js script"); console.log("Gamepad connected! Attaching gamepadtokey.js script");
} window.removeEventListener("gamepadconnected", attachGamepadScript);
} require(["components/input/gamepadtokey"]);
function dettachGamepad(e) {
if (!isGamepadConnected()) {
console.log("Gamepad disconnected! No other gamepads are connected, dettaching gamepadtokey.js");
} else {
console.log("Gamepad disconnected! There are gamepads still connected.");
} }
} }
// No need to check for gamepads manually at load time, the eventhandler will be fired for that // No need to check for gamepads manually at load time, the eventhandler will be fired for that
window.addEventListener("gamepaddisconnected", dettachGamepad); window.addEventListener("gamepadconnected", attachGamepadScript);
window.addEventListener("gamepadconnected", attachGamepad);
require(["components/input/mouseManager"]);
return { return {
enable: enable, enable: enable,