-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Gamepad): GamepadAdded/Removed for WASM
- Loading branch information
1 parent
dd0a1b2
commit 1160c40
Showing
4 changed files
with
167 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace Windows.Gaming.Input { | ||
export class Gamepad { | ||
|
||
private static dispatchGamepadAdded: (id: string) => number; | ||
private static dispatchGamepadRemoved: (id: string) => number; | ||
|
||
public static getConnectedGamepadIds(): string { | ||
const gamepads = navigator.getGamepads(); | ||
const separator = ";"; | ||
var result = ''; | ||
for (var i = 0; i < gamepads.length; i++) { | ||
if (gamepads[i]) { | ||
result += gamepads[i].index + separator; | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
public static startGamepadAdded() { | ||
window.addEventListener("gamepadconnected", Gamepad.onGamepadConnected); | ||
} | ||
|
||
public static endGamepadAdded() { | ||
window.removeEventListener("gamepadconnected", Gamepad.onGamepadConnected); | ||
} | ||
|
||
public static startGamepadRemoved() { | ||
window.addEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected); | ||
} | ||
|
||
public static endGamepadRemoved() { | ||
window.removeEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected); | ||
} | ||
|
||
private static onGamepadConnected(e : any) { | ||
if (!Gamepad.dispatchGamepadAdded) { | ||
Gamepad.dispatchGamepadAdded = (<any>Module).mono_bind_static_method( | ||
"[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadAdded"); | ||
} | ||
Gamepad.dispatchGamepadAdded(e.gamepad.index.toString()); | ||
} | ||
|
||
private static onGamepadDisconnected(e : any) { | ||
if (!Gamepad.dispatchGamepadRemoved) { | ||
Gamepad.dispatchGamepadRemoved = (<any>Module).mono_bind_static_method( | ||
"[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadRemoved"); | ||
} | ||
Gamepad.dispatchGamepadRemoved(e.gamepad.index.toString()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#if __WASM__ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Uno; | ||
using Uno.Extensions; | ||
using Uno.Foundation; | ||
|
||
namespace Windows.Gaming.Input | ||
{ | ||
public partial class Gamepad | ||
{ | ||
private const string JsType = "Windows.Gaming.Input.Gamepad"; | ||
private const char IdSeparator = ';'; | ||
|
||
private readonly static Dictionary<string, Gamepad> _gamepadCache = | ||
new Dictionary<string, Gamepad>(); | ||
|
||
private readonly string _id; | ||
|
||
private Gamepad(string id) | ||
{ | ||
_id = id; | ||
} | ||
|
||
[Preserve] | ||
public static int DispatchGamepadAdded(string id) | ||
{ | ||
Gamepad gamepad; | ||
lock (_gamepadCache) | ||
{ | ||
if (!_gamepadCache.TryGetValue(id, out gamepad)) | ||
{ | ||
gamepad = new Gamepad(id); | ||
_gamepadCache.Add(id, gamepad); | ||
} | ||
} | ||
_gamepadAdded?.Invoke(null, gamepad); | ||
return 0; | ||
} | ||
|
||
[Preserve] | ||
public static int DispatchGamepadRemoved(string id) | ||
{ | ||
Gamepad gamepad; | ||
lock (_gamepadCache) | ||
{ | ||
if (!_gamepadCache.TryGetValue(id, out gamepad)) | ||
{ | ||
gamepad = new Gamepad(id); | ||
_gamepadCache.Add(id, gamepad); | ||
} | ||
} | ||
_gamepadRemoved?.Invoke(null, gamepad); | ||
return 0; | ||
} | ||
|
||
private static IReadOnlyList<Gamepad> GetGamepadsInternal() | ||
{ | ||
var getConnectedGamepadIdsCommand = $"{JsType}.getConnectedGamepadIds()"; | ||
var serializedIds = WebAssemblyRuntime.InvokeJS(getConnectedGamepadIdsCommand); | ||
var connectedGamepadIds = serializedIds.Split(new[] { IdSeparator }, StringSplitOptions.RemoveEmptyEntries); | ||
|
||
lock (_gamepadCache) | ||
{ | ||
var cachedGCControllers = _gamepadCache.Keys.ToArray(); | ||
|
||
//remove disconnected | ||
var disconnectedDevices = cachedGCControllers.Except(connectedGamepadIds); | ||
_gamepadCache.RemoveKeys(disconnectedDevices); | ||
|
||
//add newly connected | ||
foreach (var id in connectedGamepadIds) | ||
{ | ||
if (!_gamepadCache.TryGetValue(id, out var gamepad)) | ||
{ | ||
gamepad = new Gamepad(id); | ||
_gamepadCache.Add(id, gamepad); | ||
} | ||
} | ||
|
||
return _gamepadCache.Values.OrderBy(g => g._id).ToArray(); | ||
} | ||
} | ||
|
||
private static void StartGamepadAdded() | ||
{ | ||
var startGamepadAddedCommand = $"{JsType}.startGamepadAdded()"; | ||
WebAssemblyRuntime.InvokeJS(startGamepadAddedCommand); | ||
} | ||
|
||
private static void EndGamepadAdded() | ||
{ | ||
var endGamepadAddedCommand = $"{JsType}.endGamepadAdded()"; | ||
WebAssemblyRuntime.InvokeJS(endGamepadAddedCommand); | ||
} | ||
|
||
private static void StartGamepadRemoved() | ||
{ | ||
var startGamepadRemovedCommand = $"{JsType}.startGamepadRemoved()"; | ||
WebAssemblyRuntime.InvokeJS(startGamepadRemovedCommand); | ||
} | ||
|
||
private static void EndGamepadRemoved() | ||
{ | ||
var endGamepadRemovedCommand = $"{JsType}.endGamepadRemoved()"; | ||
WebAssemblyRuntime.InvokeJS(endGamepadRemovedCommand); | ||
} | ||
} | ||
} | ||
#endif |