From 8a200e54af1b49e5158bfb3e9562fdf171ebb3f3 Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 28 Feb 2020 10:09:46 +0100 Subject: [PATCH] Final overhaul to gamepad handling --- src/components/input/gamepadtokey.js | 50 +++++++++++++++++++--- src/components/input/keyboardnavigation.js | 21 ++++----- 2 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index 73f587f43b..bb4d0fffd6 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -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. diff --git a/src/components/input/keyboardnavigation.js b/src/components/input/keyboardnavigation.js index 956c847a51..b3c68e604f 100644 --- a/src/components/input/keyboardnavigation.js +++ b/src/components/input/keyboardnavigation.js @@ -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,