From c60dbbb3aeca9f38e96c31ab51b4c9d8ec0b4ed0 Mon Sep 17 00:00:00 2001 From: ferferga Date: Wed, 29 Jan 2020 15:08:17 +0100 Subject: [PATCH 01/12] Load gamepadtokey.js only when is necessary --- .../serverNotifications/gamepadtokey.js | 6 +--- .../serverNotifications.js | 28 +++++++++---------- 2 files changed, 15 insertions(+), 19 deletions(-) diff --git a/src/components/serverNotifications/gamepadtokey.js b/src/components/serverNotifications/gamepadtokey.js index abf3ddb38b..9f2c5942fb 100644 --- a/src/components/serverNotifications/gamepadtokey.js +++ b/src/components/serverNotifications/gamepadtokey.js @@ -254,11 +254,7 @@ require(['apphost'], function (appHost) { function runInputLoop() { // Get the latest gamepad state. var gamepads; - if (navigator.getGamepads) { - gamepads = navigator.getGamepads(); - } else if (navigator.webkitGetGamepads) { - gamepads = navigator.webkitGetGamepads(); - } + gamepads = navigator.getGamepads(); gamepads = gamepads || []; var i; var j; diff --git a/src/components/serverNotifications/serverNotifications.js b/src/components/serverNotifications/serverNotifications.js index 1941881b7a..1dfa72ce3a 100644 --- a/src/components/serverNotifications/serverNotifications.js +++ b/src/components/serverNotifications/serverNotifications.js @@ -197,30 +197,30 @@ define(['connectionManager', 'playbackManager', 'events', 'inputManager', 'focus events.on(apiClient, "message", onMessageReceived); } - function enableNativeGamepadKeyMapping() { - if (window.navigator && "string" == typeof window.navigator.gamepadInputEmulation) { - window.navigator.gamepadInputEmulation = "keyboard"; - return true; + function attachgamepad(e) { + if (navigator.getGamepads.length > 0) { + require(["components/serverNotifications/gamepadtokey"]); + console.log("Gamepad connected! Attaching gamepadtokey.js script"); } - - return false; } - function isGamepadSupported() { - return "ongamepadconnected" in window || navigator.getGamepads || navigator.webkitGetGamepads; + function dettachgamepad(e) { + delete require.cache[require(["components/serverNotifications/gamepadtokey"])]; + console.log("Gamepad disconnected! Dettaching gamepadtokey.js"); } connectionManager.getApiClients().forEach(bindEvents); - events.on(connectionManager, 'apiclientcreated', function (e, newApiClient) { bindEvents(newApiClient); }); - - if (!enableNativeGamepadKeyMapping() && isGamepadSupported()) { + if (navigator.getGamepads()[0] != null) { require(["components/serverNotifications/gamepadtokey"]); + console.log("Gamepad connected! Attaching gamepadtokey.js script"); + window.addEventListener("gamepaddisconnected", dettachgamepad); + } else { + window.addEventListener("gamepadconnected", attachgamepad); + console.log("No gamepad connected to this device"); } - - require(["components/serverNotifications/mouseManager"]); - + // require(["components/serverNotifications/mouseManager"]); return serverNotifications; }); From ab0a9ef065bb1dbb5231788ad9e067f6cfc42e2e Mon Sep 17 00:00:00 2001 From: ferferga Date: Wed, 5 Feb 2020 14:13:28 +0100 Subject: [PATCH 02/12] Moved gamepad to input and serverNotofications cleanup --- .../gamepadtokey.js | 166 +++++++++--------- .../mouseManager.js | 0 src/components/keyboardnavigation.js | 39 ++++ .../serverNotifications.js | 22 --- 4 files changed, 120 insertions(+), 107 deletions(-) rename src/components/{serverNotifications => input}/gamepadtokey.js (70%) rename src/components/{serverNotifications => input}/mouseManager.js (100%) diff --git a/src/components/serverNotifications/gamepadtokey.js b/src/components/input/gamepadtokey.js similarity index 70% rename from src/components/serverNotifications/gamepadtokey.js rename to src/components/input/gamepadtokey.js index 9f2c5942fb..865a37f101 100644 --- a/src/components/serverNotifications/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -255,96 +255,92 @@ require(['apphost'], function (appHost) { // Get the latest gamepad state. var gamepads; gamepads = navigator.getGamepads(); - gamepads = gamepads || []; var i; var j; var len; for (i = 0, len = gamepads.length; i < len; i++) { var gamepad = gamepads[i]; - if (gamepad) { - // Iterate through the axes - var axes = gamepad.axes; - var leftStickX = axes[0]; - var leftStickY = axes[1]; - if (leftStickX > _THUMB_STICK_THRESHOLD) { // Right - _ButtonPressedState.setleftThumbstickRight(true); - } else if (leftStickX < -_THUMB_STICK_THRESHOLD) { // Left - _ButtonPressedState.setleftThumbstickLeft(true); - } else if (leftStickY < -_THUMB_STICK_THRESHOLD) { // Up - _ButtonPressedState.setleftThumbstickUp(true); - } else if (leftStickY > _THUMB_STICK_THRESHOLD) { // Down - _ButtonPressedState.setleftThumbstickDown(true); - } else { - _ButtonPressedState.setleftThumbstickLeft(false); - _ButtonPressedState.setleftThumbstickRight(false); - _ButtonPressedState.setleftThumbstickUp(false); - _ButtonPressedState.setleftThumbstickDown(false); - } - // Iterate through the buttons to see if Left thumbstick, DPad, A and B are pressed. - var buttons = gamepad.buttons; - for (j = 0, len = buttons.length; j < len; j++) { - if (ProcessedButtons.indexOf(j) !== -1) { - - if (buttons[j].pressed) { - switch (j) { - case _GAMEPAD_DPAD_UP_BUTTON_INDEX: - _ButtonPressedState.setdPadUp(true); - break; - case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX: - _ButtonPressedState.setdPadDown(true); - break; - case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX: - _ButtonPressedState.setdPadLeft(true); - break; - case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX: - _ButtonPressedState.setdPadRight(true); - break; - case _GAMEPAD_A_BUTTON_INDEX: - _ButtonPressedState.setgamepadA(true); - break; - case _GAMEPAD_B_BUTTON_INDEX: - _ButtonPressedState.setgamepadB(true); - break; - default: - // No-op - break; - } - } else { - switch (j) { - case _GAMEPAD_DPAD_UP_BUTTON_INDEX: - if (_ButtonPressedState.getdPadUp()) { - _ButtonPressedState.setdPadUp(false); - } - break; - case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX: - if (_ButtonPressedState.getdPadDown()) { - _ButtonPressedState.setdPadDown(false); - } - break; - case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX: - if (_ButtonPressedState.getdPadLeft()) { - _ButtonPressedState.setdPadLeft(false); - } - break; - case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX: - if (_ButtonPressedState.getdPadRight()) { - _ButtonPressedState.setdPadRight(false); - } - break; - case _GAMEPAD_A_BUTTON_INDEX: - if (_ButtonPressedState.getgamepadA()) { - _ButtonPressedState.setgamepadA(false); - } - break; - case _GAMEPAD_B_BUTTON_INDEX: - if (_ButtonPressedState.getgamepadB()) { - _ButtonPressedState.setgamepadB(false); - } - break; - default: - // No-op - break; - } + // Iterate through the axes + var axes = gamepad.axes; + var leftStickX = axes[0]; + var leftStickY = axes[1]; + if (leftStickX > _THUMB_STICK_THRESHOLD) { // Right + _ButtonPressedState.setleftThumbstickRight(true); + } else if (leftStickX < -_THUMB_STICK_THRESHOLD) { // Left + _ButtonPressedState.setleftThumbstickLeft(true); + } else if (leftStickY < -_THUMB_STICK_THRESHOLD) { // Up + _ButtonPressedState.setleftThumbstickUp(true); + } else if (leftStickY > _THUMB_STICK_THRESHOLD) { // Down + _ButtonPressedState.setleftThumbstickDown(true); + } else { + _ButtonPressedState.setleftThumbstickLeft(false); + _ButtonPressedState.setleftThumbstickRight(false); + _ButtonPressedState.setleftThumbstickUp(false); + _ButtonPressedState.setleftThumbstickDown(false); + } + // Iterate through the buttons to see if Left thumbstick, DPad, A and B are pressed. + var buttons = gamepad.buttons; + for (j = 0, len = buttons.length; j < len; j++) { + if (ProcessedButtons.indexOf(j) !== -1) { + if (buttons[j].pressed) { + switch (j) { + case _GAMEPAD_DPAD_UP_BUTTON_INDEX: + _ButtonPressedState.setdPadUp(true); + break; + case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX: + _ButtonPressedState.setdPadDown(true); + break; + case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX: + _ButtonPressedState.setdPadLeft(true); + break; + case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX: + _ButtonPressedState.setdPadRight(true); + break; + case _GAMEPAD_A_BUTTON_INDEX: + _ButtonPressedState.setgamepadA(true); + break; + case _GAMEPAD_B_BUTTON_INDEX: + _ButtonPressedState.setgamepadB(true); + break; + default: + // No-op + break; + } + } else { + switch (j) { + case _GAMEPAD_DPAD_UP_BUTTON_INDEX: + if (_ButtonPressedState.getdPadUp()) { + _ButtonPressedState.setdPadUp(false); + } + break; + case _GAMEPAD_DPAD_DOWN_BUTTON_INDEX: + if (_ButtonPressedState.getdPadDown()) { + _ButtonPressedState.setdPadDown(false); + } + break; + case _GAMEPAD_DPAD_LEFT_BUTTON_INDEX: + if (_ButtonPressedState.getdPadLeft()) { + _ButtonPressedState.setdPadLeft(false); + } + break; + case _GAMEPAD_DPAD_RIGHT_BUTTON_INDEX: + if (_ButtonPressedState.getdPadRight()) { + _ButtonPressedState.setdPadRight(false); + } + break; + case _GAMEPAD_A_BUTTON_INDEX: + if (_ButtonPressedState.getgamepadA()) { + _ButtonPressedState.setgamepadA(false); + } + break; + case _GAMEPAD_B_BUTTON_INDEX: + if (_ButtonPressedState.getgamepadB()) { + _ButtonPressedState.setgamepadB(false); + } + break; + default: + // No-op + break; } } } diff --git a/src/components/serverNotifications/mouseManager.js b/src/components/input/mouseManager.js similarity index 100% rename from src/components/serverNotifications/mouseManager.js rename to src/components/input/mouseManager.js diff --git a/src/components/keyboardnavigation.js b/src/components/keyboardnavigation.js index d1ed03138c..2490a72974 100644 --- a/src/components/keyboardnavigation.js +++ b/src/components/keyboardnavigation.js @@ -126,6 +126,45 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager) }); } + function isGamepadConnected() { + var gamepads = navigator.getGamepads(); + var i, len; + for (i = 0, len = gamepads.length; i < len; i++) { + var gamepad = gamepads[i]; + if (gamepad) { + console.log(gamepad); + return true; + } + } + return false; + } + + function attachGamepad(e) { + if (isGamepadConnected()) { + require(["components/input/gamepadtokey"]); + console.log("Gamepad connected! Attaching gamepadtokey.js script"); + } + } + + function dettachGamepad(e) { + if (!isGamepadConnected()) { + delete require.cache[require(["components/input/gamepadtokey"])]; + console.log("Gamepad disconnected! No other gamepads are connected, dettaching gamepadtokey.js"); + } else { + console.log("Gamepad disconnected! There are gamepads still connected."); + } + } + + if (isGamepadConnected()) { + console.log("Gamepad connected! Attaching gamepadtokey.js script"); + } else { + console.log("No gamepad connected to this device"); + } + // No need to check for gamepads manually at load time, the eventhandler will be fired at load time as well + window.addEventListener("gamepaddisconnected", dettachGamepad); + window.addEventListener("gamepadconnected", attachGamepad); + require(["components/input/mouseManager"]); + return { enable: enable, getKeyName: getKeyName diff --git a/src/components/serverNotifications/serverNotifications.js b/src/components/serverNotifications/serverNotifications.js index 1dfa72ce3a..9945941d28 100644 --- a/src/components/serverNotifications/serverNotifications.js +++ b/src/components/serverNotifications/serverNotifications.js @@ -191,36 +191,14 @@ define(['connectionManager', 'playbackManager', 'events', 'inputManager', 'focus events.trigger(serverNotifications, msg.MessageType, [apiClient, msg.Data]); } } - function bindEvents(apiClient) { events.off(apiClient, "message", onMessageReceived); events.on(apiClient, "message", onMessageReceived); } - function attachgamepad(e) { - if (navigator.getGamepads.length > 0) { - require(["components/serverNotifications/gamepadtokey"]); - console.log("Gamepad connected! Attaching gamepadtokey.js script"); - } - } - - function dettachgamepad(e) { - delete require.cache[require(["components/serverNotifications/gamepadtokey"])]; - console.log("Gamepad disconnected! Dettaching gamepadtokey.js"); - } - connectionManager.getApiClients().forEach(bindEvents); events.on(connectionManager, 'apiclientcreated', function (e, newApiClient) { bindEvents(newApiClient); }); - if (navigator.getGamepads()[0] != null) { - require(["components/serverNotifications/gamepadtokey"]); - console.log("Gamepad connected! Attaching gamepadtokey.js script"); - window.addEventListener("gamepaddisconnected", dettachgamepad); - } else { - window.addEventListener("gamepadconnected", attachgamepad); - console.log("No gamepad connected to this device"); - } - // require(["components/serverNotifications/mouseManager"]); return serverNotifications; }); From 2b4cf6f5b732d2088f46588e9e3d6aadb227d23e Mon Sep 17 00:00:00 2001 From: ferferga Date: Wed, 5 Feb 2020 14:17:03 +0100 Subject: [PATCH 03/12] Fix lint --- src/components/keyboardnavigation.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/keyboardnavigation.js b/src/components/keyboardnavigation.js index 2490a72974..fb66335514 100644 --- a/src/components/keyboardnavigation.js +++ b/src/components/keyboardnavigation.js @@ -128,7 +128,8 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager) function isGamepadConnected() { var gamepads = navigator.getGamepads(); - var i, len; + var i; + var len; for (i = 0, len = gamepads.length; i < len; i++) { var gamepad = gamepads[i]; if (gamepad) { @@ -154,7 +155,7 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager) console.log("Gamepad disconnected! There are gamepads still connected."); } } - + if (isGamepadConnected()) { console.log("Gamepad connected! Attaching gamepadtokey.js script"); } else { From 417e7dae8876fdfb80ed3d247ba0cd1e4fc349b2 Mon Sep 17 00:00:00 2001 From: ferferga Date: Thu, 6 Feb 2020 20:06:57 +0100 Subject: [PATCH 04/12] Removed unnecessary check --- src/components/keyboardnavigation.js | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/components/keyboardnavigation.js b/src/components/keyboardnavigation.js index fb66335514..8e0bc2f9d4 100644 --- a/src/components/keyboardnavigation.js +++ b/src/components/keyboardnavigation.js @@ -156,12 +156,7 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager) } } - if (isGamepadConnected()) { - console.log("Gamepad connected! Attaching gamepadtokey.js script"); - } else { - console.log("No gamepad connected to this device"); - } - // No need to check for gamepads manually at load time, the eventhandler will be fired at load time as well + // 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"]); From 145324a2097490af1349d3ba0996536436565c77 Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 7 Feb 2020 14:32:45 +0100 Subject: [PATCH 05/12] Removed logs for testing and changed attempt --- src/components/input/gamepadtokey.js | 13 +++++++------ src/components/{ => input}/keyboardnavigation.js | 6 +----- .../serverNotifications.js | 0 src/scripts/site.js | 4 ++-- 4 files changed, 10 insertions(+), 13 deletions(-) rename src/components/{ => input}/keyboardnavigation.js (95%) rename src/components/{serverNotifications => }/serverNotifications.js (100%) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index 865a37f101..0b17981f55 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -253,13 +253,13 @@ require(['apphost'], function (appHost) { function runInputLoop() { // Get the latest gamepad state. - var gamepads; - gamepads = navigator.getGamepads(); - var i; - var j; - var len; - for (i = 0, len = gamepads.length; i < len; i++) { + var gamepads = navigator.getGamepads(); + for (var i = 0, len = gamepads.length; i < len; i++) { var gamepad = gamepads[i]; + if (!gamepad) + { + return; + } // Iterate through the axes var axes = gamepad.axes; var leftStickX = axes[0]; @@ -280,6 +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++) { if (ProcessedButtons.indexOf(j) !== -1) { if (buttons[j].pressed) { diff --git a/src/components/keyboardnavigation.js b/src/components/input/keyboardnavigation.js similarity index 95% rename from src/components/keyboardnavigation.js rename to src/components/input/keyboardnavigation.js index 8e0bc2f9d4..3840c08e8a 100644 --- a/src/components/keyboardnavigation.js +++ b/src/components/input/keyboardnavigation.js @@ -128,12 +128,9 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager) function isGamepadConnected() { var gamepads = navigator.getGamepads(); - var i; - var len; - for (i = 0, len = gamepads.length; i < len; i++) { + for (var i = 0, len = gamepads.length; i < len; i++) { var gamepad = gamepads[i]; if (gamepad) { - console.log(gamepad); return true; } } @@ -149,7 +146,6 @@ define(["inputManager", "layoutManager"], function (inputManager, layoutManager) function dettachGamepad(e) { if (!isGamepadConnected()) { - delete require.cache[require(["components/input/gamepadtokey"])]; console.log("Gamepad disconnected! No other gamepads are connected, dettaching gamepadtokey.js"); } else { console.log("Gamepad disconnected! There are gamepads still connected."); diff --git a/src/components/serverNotifications/serverNotifications.js b/src/components/serverNotifications.js similarity index 100% rename from src/components/serverNotifications/serverNotifications.js rename to src/components/serverNotifications.js diff --git a/src/scripts/site.js b/src/scripts/site.js index 3a10dc85a1..4ad1d10352 100644 --- a/src/scripts/site.js +++ b/src/scripts/site.js @@ -882,9 +882,9 @@ var AppInfo = {}; define("htmlMediaHelper", [componentsPath + "/htmlMediaHelper"], returnFirstDependency); define("viewContainer", [componentsPath + "/viewContainer"], returnFirstDependency); define("dialogHelper", [componentsPath + "/dialogHelper/dialogHelper"], returnFirstDependency); - define("serverNotifications", [componentsPath + "/serverNotifications/serverNotifications"], returnFirstDependency); + define("serverNotifications", [componentsPath + "/serverNotifications"], returnFirstDependency); define("skinManager", [componentsPath + "/skinManager"], returnFirstDependency); - define("keyboardnavigation", [componentsPath + "/keyboardnavigation"], returnFirstDependency); + define("keyboardnavigation", [componentsPath + "/input/keyboardnavigation"], returnFirstDependency); define("scrollManager", [componentsPath + "/scrollManager"], returnFirstDependency); define("autoFocuser", [componentsPath + "/autoFocuser"], returnFirstDependency); define("connectionManager", [], function () { From f8c544c1cff732981a19dbd229414bd7ed9e3daf Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 7 Feb 2020 14:35:31 +0100 Subject: [PATCH 06/12] Fix lint --- src/components/input/gamepadtokey.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index 0b17981f55..2bd5f94f7a 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -256,8 +256,7 @@ require(['apphost'], function (appHost) { var gamepads = navigator.getGamepads(); for (var i = 0, len = gamepads.length; i < len; i++) { var gamepad = gamepads[i]; - if (!gamepad) - { + if (!gamepad) { return; } // Iterate through the axes From 8a200e54af1b49e5158bfb3e9562fdf171ebb3f3 Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 28 Feb 2020 10:09:46 +0100 Subject: [PATCH 07/12] 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, From 440675fbee70074258a54a275c9c5a4fc0555c77 Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 28 Feb 2020 13:51:41 +0100 Subject: [PATCH 08/12] Apply suggestions --- src/components/input/keyboardnavigation.js | 23 ++++------------------ src/scripts/site.js | 2 ++ 2 files changed, 6 insertions(+), 19 deletions(-) diff --git a/src/components/input/keyboardnavigation.js b/src/components/input/keyboardnavigation.js index b3c68e604f..0359ee7430 100644 --- a/src/components/input/keyboardnavigation.js +++ b/src/components/input/keyboardnavigation.js @@ -145,28 +145,13 @@ 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 + // Gamepad initialisation. No script is required if no gamepads are present at init time, saving a bit of resources. // 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++) { - var gamepad = gamepads[i]; - if (gamepad) { - return true; - } - } - return false; - } - function attachGamepadScript(e) { - if (isGamepadConnected()) { - console.log("Gamepad connected! Attaching gamepadtokey.js script"); - window.removeEventListener("gamepadconnected", attachGamepadScript); - require(["components/input/gamepadtokey"]); - } + console.log("Gamepad connected! Attaching gamepadtokey.js script"); + 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 diff --git a/src/scripts/site.js b/src/scripts/site.js index 280addc2f9..fc18ae9504 100644 --- a/src/scripts/site.js +++ b/src/scripts/site.js @@ -492,6 +492,7 @@ var AppInfo = {}; require(["keyboardnavigation"], function(keyboardnavigation) { keyboardnavigation.enable(); }); + require(["mouseManager"]); require(["focusPreventScroll"]); require(["autoFocuser"], function(autoFocuser) { autoFocuser.enable(); @@ -907,6 +908,7 @@ var AppInfo = {}; define("serverNotifications", [componentsPath + "/serverNotifications"], returnFirstDependency); define("skinManager", [componentsPath + "/skinManager"], returnFirstDependency); define("keyboardnavigation", [componentsPath + "/input/keyboardnavigation"], returnFirstDependency); + define("mouseManager", [componentsPath + "/input/mouseManager"], returnFirstDependency); define("scrollManager", [componentsPath + "/scrollManager"], returnFirstDependency); define("autoFocuser", [componentsPath + "/autoFocuser"], returnFirstDependency); define("connectionManager", [], function () { From 9c84b8268f76a1e89606e0766385584e64b7abff Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 28 Feb 2020 17:19:03 +0100 Subject: [PATCH 09/12] Handle gamepads more gracefully based in window focus --- src/components/input/gamepadtokey.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index bb4d0fffd6..9d80a3c27e 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -373,14 +373,14 @@ require(['apphost'], function (appHost) { } function attachGamepad(e) { - if (isGamepadConnected()) { + if (isGamepadConnected() && document.hasFocus()) { console.log("Gamepad connected! Starting input loop"); startInputLoop(); } } function dettachGamepad(e) { - if (!isGamepadConnected()) { + if (!isGamepadConnected() || !document.hasFocus()) { console.log("Gamepad disconnected! No other gamepads are connected, stopping input loop"); stopInputLoop(); } else { @@ -391,6 +391,8 @@ require(['apphost'], function (appHost) { // Event Listeners for any change in gamepads' state. window.addEventListener("gamepaddisconnected", dettachGamepad); window.addEventListener("gamepadconnected", attachGamepad); + window.addEventListener("blur", dettachGamepad); + window.addEventListener("focus", 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. From 1f7b41e303c3c3f985227f4fd879ca1837eff306 Mon Sep 17 00:00:00 2001 From: ferferga Date: Fri, 28 Feb 2020 17:30:52 +0100 Subject: [PATCH 10/12] Fire input loop --- src/components/input/gamepadtokey.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index 9d80a3c27e..2feff55747 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -394,6 +394,8 @@ require(['apphost'], function (appHost) { window.addEventListener("blur", dettachGamepad); window.addEventListener("focus", attachGamepad); + 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. if (window.navigator && typeof window.navigator.gamepadInputEmulation === "string") { From 3310d5b239bf1ac4c15fb3b9ece2a07ac10ecd59 Mon Sep 17 00:00:00 2001 From: ferferga Date: Sat, 29 Feb 2020 22:44:08 +0100 Subject: [PATCH 11/12] Fix for Firefox --- src/components/input/gamepadtokey.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index 2feff55747..e430bd9891 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -365,7 +365,7 @@ require(['apphost'], function (appHost) { var gamepads = navigator.getGamepads(); for (var i = 0, len = gamepads.length; i < len; i++) { var gamepad = gamepads[i]; - if (gamepad) { + if (gamepad && gamepad.connected) { return true; } } @@ -388,7 +388,7 @@ require(['apphost'], function (appHost) { } } - // Event Listeners for any change in gamepads' state. + // Event listeners for any change in gamepads' state. window.addEventListener("gamepaddisconnected", dettachGamepad); window.addEventListener("gamepadconnected", attachGamepad); window.addEventListener("blur", dettachGamepad); From c8836c61f35e6134faafc851c2d3fac6a6db7d05 Mon Sep 17 00:00:00 2001 From: ferferga Date: Tue, 3 Mar 2020 17:56:59 +0100 Subject: [PATCH 12/12] Function rename --- src/components/input/gamepadtokey.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/components/input/gamepadtokey.js b/src/components/input/gamepadtokey.js index e430bd9891..5356bcbb45 100644 --- a/src/components/input/gamepadtokey.js +++ b/src/components/input/gamepadtokey.js @@ -372,14 +372,14 @@ require(['apphost'], function (appHost) { return false; } - function attachGamepad(e) { + function onFocusOrGamepadAttach(e) { if (isGamepadConnected() && document.hasFocus()) { console.log("Gamepad connected! Starting input loop"); startInputLoop(); } } - function dettachGamepad(e) { + function onFocusOrGamepadDetach(e) { if (!isGamepadConnected() || !document.hasFocus()) { console.log("Gamepad disconnected! No other gamepads are connected, stopping input loop"); stopInputLoop(); @@ -389,12 +389,12 @@ require(['apphost'], function (appHost) { } // Event listeners for any change in gamepads' state. - window.addEventListener("gamepaddisconnected", dettachGamepad); - window.addEventListener("gamepadconnected", attachGamepad); - window.addEventListener("blur", dettachGamepad); - window.addEventListener("focus", attachGamepad); + window.addEventListener("gamepaddisconnected", onFocusOrGamepadDetach); + window.addEventListener("gamepadconnected", onFocusOrGamepadAttach); + window.addEventListener("blur", onFocusOrGamepadDetach); + window.addEventListener("focus", onFocusOrGamepadAttach); - attachGamepad(); + onFocusOrGamepadAttach(); // 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.