Skip to content

Commit 52dab51

Browse files
committed
feat(Gamepad): GetCurrentReading on WASM
1 parent b4cdd33 commit 52dab51

File tree

14 files changed

+390
-87
lines changed

14 files changed

+390
-87
lines changed

src/SamplesApp/UITests.Shared/Windows_Gaming/GamepadReadingTest.xaml

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,42 @@
1111

1212
<StackPanel Spacing="8" Padding="8">
1313
<Button Command="{x:Bind ViewModel.GetCurrentReadingCommand}">Get current reading</Button>
14+
1415
<TextBlock>
1516
<Run Text="Timestamp:" />
16-
<Run Text="{x:Bind ViewModel.CurrentReading.Timestamp}" />
17+
<Run Text="{x:Bind ViewModel.Timestamp}" />
1718
</TextBlock>
1819

1920
<TextBlock>
2021
<Run Text="Left thumbstick X:" />
21-
<Run Text="{x:Bind ViewModel.CurrentReading.LeftThumbstickX}" />
22+
<Run Text="{x:Bind ViewModel.LeftThumbstickX}" />
2223
</TextBlock>
2324
<TextBlock>
2425
<Run Text="Left thumbstick Y:" />
25-
<Run Text="{x:Bind ViewModel.CurrentReading.LeftThumbstickY}" />
26+
<Run Text="{x:Bind ViewModel.LeftThumbstickY}" />
2627
</TextBlock>
2728

2829
<TextBlock>
2930
<Run Text="Right thumbstick X:" />
30-
<Run Text="{x:Bind ViewModel.CurrentReading.RightThumbstickX}" />
31+
<Run Text="{x:Bind ViewModel.RightThumbstickX}" />
3132
</TextBlock>
3233
<TextBlock>
3334
<Run Text="Right thumbstick Y:" />
34-
<Run Text="{x:Bind ViewModel.CurrentReading.RightThumbstickY}" />
35+
<Run Text="{x:Bind ViewModel.RightThumbstickY}" />
3536
</TextBlock>
3637

3738
<TextBlock>
3839
<Run Text="Left trigger:" />
39-
<Run Text="{x:Bind ViewModel.CurrentReading.LeftTrigger}" />
40+
<Run Text="{x:Bind ViewModel.LeftTrigger}" />
4041
</TextBlock>
4142
<TextBlock>
4243
<Run Text="Right trigger:" />
43-
<Run Text="{x:Bind ViewModel.CurrentReading.RightTrigger}" />
44+
<Run Text="{x:Bind ViewModel.RightTrigger}" />
4445
</TextBlock>
4546

4647
<TextBlock>
4748
<Run Text="Buttons:" />
48-
<Run Text="{x:Bind ViewModel.CurrentReading.Buttons}" />
49+
<Run Text="{x:Bind ViewModel.Buttons}" />
4950
</TextBlock>
5051
</StackPanel>
5152
</Page>

src/SamplesApp/UITests.Shared/Windows_Gaming/GamepadReadingTest.xaml.cs

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
namespace UITests.Windows_Gaming
1111
{
1212
[Sample("Windows.Gaming", Name = "Gamepad_CurrentReading", ViewModelType = typeof(GamepadReadingTestViewModel))]
13-
1413
public sealed partial class GamepadReadingTest : Page
1514
{
1615
public GamepadReadingTest()
@@ -35,20 +34,48 @@ public GamepadReadingTestViewModel(CoreDispatcher dispatcher) : base(dispatcher)
3534

3635
public ICommand GetCurrentReadingCommand => GetOrCreateCommand(GetCurrentReading);
3736

37+
public ulong Timestamp => CurrentReading.Timestamp;
38+
39+
public GamepadButtons Buttons => CurrentReading.Buttons;
40+
41+
public double LeftTrigger => CurrentReading.LeftTrigger;
42+
43+
public double RightTrigger => CurrentReading.RightTrigger;
44+
45+
public double LeftThumbstickX => CurrentReading.LeftThumbstickX;
46+
47+
public double LeftThumbstickY => CurrentReading.LeftThumbstickY;
48+
49+
public double RightThumbstickX => CurrentReading.RightThumbstickX;
50+
51+
public double RightThumbstickY => CurrentReading.RightThumbstickY;
52+
3853
public GamepadReading CurrentReading { get; set; }
3954

4055
private void GetCurrentReading()
4156
{
4257
var gamepad = Gamepad.Gamepads.FirstOrDefault();
4358
if (gamepad != null)
44-
{
59+
{
4560
CurrentReading = gamepad.GetCurrentReading();
4661
}
4762
else
4863
{
4964
CurrentReading = new GamepadReading();
5065
}
51-
RaisePropertyChanged(nameof(CurrentReading));
66+
Refresh();
67+
}
68+
69+
private void Refresh()
70+
{
71+
RaisePropertyChanged(nameof(Timestamp));
72+
RaisePropertyChanged(nameof(Buttons));
73+
RaisePropertyChanged(nameof(LeftTrigger));
74+
RaisePropertyChanged(nameof(RightTrigger));
75+
RaisePropertyChanged(nameof(LeftThumbstickX));
76+
RaisePropertyChanged(nameof(LeftThumbstickY));
77+
RaisePropertyChanged(nameof(RightThumbstickX));
78+
RaisePropertyChanged(nameof(RightThumbstickY));
5279
}
5380
}
5481
}

src/Uno.UI/WasmScripts/Uno.UI.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,20 @@ declare namespace Windows.Devices.Sensors {
842842
private static readingChangedHandler;
843843
}
844844
}
845+
declare namespace Windows.Gaming.Input {
846+
class Gamepad {
847+
private static dispatchGamepadAdded;
848+
private static dispatchGamepadRemoved;
849+
static getConnectedGamepadIds(): string;
850+
static getReading(id: number): string;
851+
static startGamepadAdded(): void;
852+
static endGamepadAdded(): void;
853+
static startGamepadRemoved(): void;
854+
static endGamepadRemoved(): void;
855+
private static onGamepadConnected;
856+
private static onGamepadDisconnected;
857+
}
858+
}
845859
declare namespace Windows.Graphics.Display {
846860
class DisplayInformation {
847861
private static readonly DpiCheckInterval;

src/Uno.UI/WasmScripts/Uno.UI.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2666,6 +2666,76 @@ var Windows;
26662666
})(Devices = Windows.Devices || (Windows.Devices = {}));
26672667
})(Windows || (Windows = {}));
26682668
var Windows;
2669+
(function (Windows) {
2670+
var Gaming;
2671+
(function (Gaming) {
2672+
var Input;
2673+
(function (Input) {
2674+
class Gamepad {
2675+
static getConnectedGamepadIds() {
2676+
const gamepads = navigator.getGamepads();
2677+
const separator = ";";
2678+
var result = '';
2679+
for (var i = 0; i < gamepads.length; i++) {
2680+
if (gamepads[i]) {
2681+
result += gamepads[i].index + separator;
2682+
}
2683+
}
2684+
return result;
2685+
}
2686+
static getReading(id) {
2687+
var gamepad = navigator.getGamepads()[id];
2688+
if (!gamepad) {
2689+
return "";
2690+
}
2691+
var result = "";
2692+
result += gamepad.timestamp;
2693+
result += '*';
2694+
for (var axisId = 0; axisId < gamepad.axes.length; axisId++) {
2695+
if (axisId != 0) {
2696+
result += '|';
2697+
}
2698+
result += gamepad.axes[axisId];
2699+
}
2700+
result += '*';
2701+
for (var buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {
2702+
if (buttonId != 0) {
2703+
result += '|';
2704+
}
2705+
result += gamepad.buttons[buttonId].value;
2706+
}
2707+
return result;
2708+
}
2709+
static startGamepadAdded() {
2710+
window.addEventListener("gamepadconnected", Gamepad.onGamepadConnected);
2711+
}
2712+
static endGamepadAdded() {
2713+
window.removeEventListener("gamepadconnected", Gamepad.onGamepadConnected);
2714+
}
2715+
static startGamepadRemoved() {
2716+
window.addEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected);
2717+
}
2718+
static endGamepadRemoved() {
2719+
window.removeEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected);
2720+
}
2721+
static onGamepadConnected(e) {
2722+
if (!Gamepad.dispatchGamepadAdded) {
2723+
Gamepad.dispatchGamepadAdded = Module.mono_bind_static_method("[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadAdded");
2724+
}
2725+
Gamepad.dispatchGamepadAdded(e.gamepad.index.toString());
2726+
}
2727+
static onGamepadDisconnected(e) {
2728+
if (!Gamepad.dispatchGamepadRemoved) {
2729+
Gamepad.dispatchGamepadRemoved = Module.mono_bind_static_method("[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadRemoved");
2730+
}
2731+
Gamepad.dispatchGamepadRemoved(e.gamepad.index.toString());
2732+
}
2733+
}
2734+
Input.Gamepad = Gamepad;
2735+
})(Input = Gaming.Input || (Gaming.Input = {}));
2736+
})(Gaming = Windows.Gaming || (Windows.Gaming = {}));
2737+
})(Windows || (Windows = {}));
2738+
var Windows;
26692739
(function (Windows) {
26702740
var Graphics;
26712741
(function (Graphics) {

src/Uno.UI/ts/Windows/Gaming/Input/Gamepad.ts

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@
1616
return result;
1717
}
1818

19+
public static getReading(id: number): string {
20+
var gamepad = navigator.getGamepads()[id];
21+
if (!gamepad) {
22+
return "";
23+
}
24+
25+
var result = "";
26+
27+
result += gamepad.timestamp;
28+
29+
result += '*';
30+
31+
for (var axisId = 0; axisId < gamepad.axes.length; axisId++) {
32+
if (axisId != 0) {
33+
result += '|';
34+
}
35+
result += gamepad.axes[axisId];
36+
}
37+
38+
result += '*';
39+
40+
for (var buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {
41+
if (buttonId != 0) {
42+
result += '|';
43+
}
44+
result += gamepad.buttons[buttonId].value;
45+
}
46+
47+
return result;
48+
}
49+
1950
public static startGamepadAdded() {
2051
window.addEventListener("gamepadconnected", Gamepad.onGamepadConnected);
2152
}
@@ -32,15 +63,15 @@
3263
window.removeEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected);
3364
}
3465

35-
private static onGamepadConnected(e : any) {
66+
private static onGamepadConnected(e: any) {
3667
if (!Gamepad.dispatchGamepadAdded) {
3768
Gamepad.dispatchGamepadAdded = (<any>Module).mono_bind_static_method(
3869
"[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadAdded");
3970
}
4071
Gamepad.dispatchGamepadAdded(e.gamepad.index.toString());
4172
}
4273

43-
private static onGamepadDisconnected(e : any) {
74+
private static onGamepadDisconnected(e: any) {
4475
if (!Gamepad.dispatchGamepadRemoved) {
4576
Gamepad.dispatchGamepadRemoved = (<any>Module).mono_bind_static_method(
4677
"[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadRemoved");

src/Uno.UWP/Gaming/Input/Gamepad.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Windows.Gaming.Input
99
{
10-
public partial class Gamepad
10+
public partial class Gamepad : IGameController
1111
{
1212
private readonly static object _syncLock = new object();
1313

@@ -68,7 +68,7 @@ public static event EventHandler<Gamepad> GamepadRemoved
6868
}
6969
}
7070
}
71-
}
71+
}
7272

7373
internal static void OnGamepadAdded(Gamepad gamepad) =>
7474
_gamepadAdded?.Invoke(null, gamepad);

0 commit comments

Comments
 (0)