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:
parent
04a3505672
commit
8a200e54af
2 changed files with 53 additions and 18 deletions
|
@ -251,13 +251,14 @@ require(['apphost'], function (appHost) {
|
|||
}
|
||||
}
|
||||
|
||||
var inputLoopTimer;
|
||||
function runInputLoop() {
|
||||
// Get the latest gamepad state.
|
||||
var gamepads = navigator.getGamepads();
|
||||
for (var i = 0, len = gamepads.length; i < len; i++) {
|
||||
var gamepad = gamepads[i];
|
||||
if (!gamepad) {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
// Iterate through the 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.
|
||||
var buttons = gamepad.buttons;
|
||||
var j;
|
||||
for (j = 0, len = buttons.length; j < len; j++) {
|
||||
for (var j = 0, len = buttons.length; j < len; j++) {
|
||||
if (ProcessedButtons.indexOf(j) !== -1) {
|
||||
if (buttons[j].pressed) {
|
||||
switch (j) {
|
||||
|
@ -347,10 +347,50 @@ require(['apphost'], function (appHost) {
|
|||
}
|
||||
}
|
||||
// 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.
|
||||
// It won't exist in Win8.1 style apps or browsers.
|
||||
|
|
|
@ -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() {
|
||||
var gamepads = navigator.getGamepads();
|
||||
for (var i = 0, len = gamepads.length; i < len; i++) {
|
||||
|
@ -157,25 +161,16 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager)
|
|||
return false;
|
||||
}
|
||||
|
||||
function attachGamepad(e) {
|
||||
function attachGamepadScript(e) {
|
||||
if (isGamepadConnected()) {
|
||||
require(["components/input/gamepadtokey"]);
|
||||
console.log("Gamepad connected! Attaching gamepadtokey.js script");
|
||||
}
|
||||
}
|
||||
|
||||
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.");
|
||||
window.removeEventListener("gamepadconnected", attachGamepadScript);
|
||||
require(["components/input/gamepadtokey"]);
|
||||
}
|
||||
}
|
||||
|
||||
// No need to check for gamepads manually at load time, the eventhandler will be fired for that
|
||||
window.addEventListener("gamepaddisconnected", dettachGamepad);
|
||||
window.addEventListener("gamepadconnected", attachGamepad);
|
||||
require(["components/input/mouseManager"]);
|
||||
window.addEventListener("gamepadconnected", attachGamepadScript);
|
||||
|
||||
return {
|
||||
enable: enable,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue