Skip to content

Commit

Permalink
feat(Gamepad): GetCurrentReading on WASM
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Jun 1, 2022
1 parent b4cdd33 commit 52dab51
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,42 @@

<StackPanel Spacing="8" Padding="8">
<Button Command="{x:Bind ViewModel.GetCurrentReadingCommand}">Get current reading</Button>

<TextBlock>
<Run Text="Timestamp:" />
<Run Text="{x:Bind ViewModel.CurrentReading.Timestamp}" />
<Run Text="{x:Bind ViewModel.Timestamp}" />
</TextBlock>

<TextBlock>
<Run Text="Left thumbstick X:" />
<Run Text="{x:Bind ViewModel.CurrentReading.LeftThumbstickX}" />
<Run Text="{x:Bind ViewModel.LeftThumbstickX}" />
</TextBlock>
<TextBlock>
<Run Text="Left thumbstick Y:" />
<Run Text="{x:Bind ViewModel.CurrentReading.LeftThumbstickY}" />
<Run Text="{x:Bind ViewModel.LeftThumbstickY}" />
</TextBlock>

<TextBlock>
<Run Text="Right thumbstick X:" />
<Run Text="{x:Bind ViewModel.CurrentReading.RightThumbstickX}" />
<Run Text="{x:Bind ViewModel.RightThumbstickX}" />
</TextBlock>
<TextBlock>
<Run Text="Right thumbstick Y:" />
<Run Text="{x:Bind ViewModel.CurrentReading.RightThumbstickY}" />
<Run Text="{x:Bind ViewModel.RightThumbstickY}" />
</TextBlock>

<TextBlock>
<Run Text="Left trigger:" />
<Run Text="{x:Bind ViewModel.CurrentReading.LeftTrigger}" />
<Run Text="{x:Bind ViewModel.LeftTrigger}" />
</TextBlock>
<TextBlock>
<Run Text="Right trigger:" />
<Run Text="{x:Bind ViewModel.CurrentReading.RightTrigger}" />
<Run Text="{x:Bind ViewModel.RightTrigger}" />
</TextBlock>

<TextBlock>
<Run Text="Buttons:" />
<Run Text="{x:Bind ViewModel.CurrentReading.Buttons}" />
<Run Text="{x:Bind ViewModel.Buttons}" />
</TextBlock>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace UITests.Windows_Gaming
{
[Sample("Windows.Gaming", Name = "Gamepad_CurrentReading", ViewModelType = typeof(GamepadReadingTestViewModel))]

public sealed partial class GamepadReadingTest : Page
{
public GamepadReadingTest()
Expand All @@ -35,20 +34,48 @@ public GamepadReadingTestViewModel(CoreDispatcher dispatcher) : base(dispatcher)

public ICommand GetCurrentReadingCommand => GetOrCreateCommand(GetCurrentReading);

public ulong Timestamp => CurrentReading.Timestamp;

public GamepadButtons Buttons => CurrentReading.Buttons;

public double LeftTrigger => CurrentReading.LeftTrigger;

public double RightTrigger => CurrentReading.RightTrigger;

public double LeftThumbstickX => CurrentReading.LeftThumbstickX;

public double LeftThumbstickY => CurrentReading.LeftThumbstickY;

public double RightThumbstickX => CurrentReading.RightThumbstickX;

public double RightThumbstickY => CurrentReading.RightThumbstickY;

public GamepadReading CurrentReading { get; set; }

private void GetCurrentReading()
{
var gamepad = Gamepad.Gamepads.FirstOrDefault();
if (gamepad != null)
{
{
CurrentReading = gamepad.GetCurrentReading();
}
else
{
CurrentReading = new GamepadReading();
}
RaisePropertyChanged(nameof(CurrentReading));
Refresh();
}

private void Refresh()
{
RaisePropertyChanged(nameof(Timestamp));
RaisePropertyChanged(nameof(Buttons));
RaisePropertyChanged(nameof(LeftTrigger));
RaisePropertyChanged(nameof(RightTrigger));
RaisePropertyChanged(nameof(LeftThumbstickX));
RaisePropertyChanged(nameof(LeftThumbstickY));
RaisePropertyChanged(nameof(RightThumbstickX));
RaisePropertyChanged(nameof(RightThumbstickY));
}
}
}
14 changes: 14 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,20 @@ declare namespace Windows.Devices.Sensors {
private static readingChangedHandler;
}
}
declare namespace Windows.Gaming.Input {
class Gamepad {
private static dispatchGamepadAdded;
private static dispatchGamepadRemoved;
static getConnectedGamepadIds(): string;
static getReading(id: number): string;
static startGamepadAdded(): void;
static endGamepadAdded(): void;
static startGamepadRemoved(): void;
static endGamepadRemoved(): void;
private static onGamepadConnected;
private static onGamepadDisconnected;
}
}
declare namespace Windows.Graphics.Display {
class DisplayInformation {
private static readonly DpiCheckInterval;
Expand Down
70 changes: 70 additions & 0 deletions src/Uno.UI/WasmScripts/Uno.UI.js
Original file line number Diff line number Diff line change
Expand Up @@ -2666,6 +2666,76 @@ var Windows;
})(Devices = Windows.Devices || (Windows.Devices = {}));
})(Windows || (Windows = {}));
var Windows;
(function (Windows) {
var Gaming;
(function (Gaming) {
var Input;
(function (Input) {
class Gamepad {
static getConnectedGamepadIds() {
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;
}
static getReading(id) {
var gamepad = navigator.getGamepads()[id];
if (!gamepad) {
return "";
}
var result = "";
result += gamepad.timestamp;
result += '*';
for (var axisId = 0; axisId < gamepad.axes.length; axisId++) {
if (axisId != 0) {
result += '|';
}
result += gamepad.axes[axisId];
}
result += '*';
for (var buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {
if (buttonId != 0) {
result += '|';
}
result += gamepad.buttons[buttonId].value;
}
return result;
}
static startGamepadAdded() {
window.addEventListener("gamepadconnected", Gamepad.onGamepadConnected);
}
static endGamepadAdded() {
window.removeEventListener("gamepadconnected", Gamepad.onGamepadConnected);
}
static startGamepadRemoved() {
window.addEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected);
}
static endGamepadRemoved() {
window.removeEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected);
}
static onGamepadConnected(e) {
if (!Gamepad.dispatchGamepadAdded) {
Gamepad.dispatchGamepadAdded = Module.mono_bind_static_method("[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadAdded");
}
Gamepad.dispatchGamepadAdded(e.gamepad.index.toString());
}
static onGamepadDisconnected(e) {
if (!Gamepad.dispatchGamepadRemoved) {
Gamepad.dispatchGamepadRemoved = Module.mono_bind_static_method("[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadRemoved");
}
Gamepad.dispatchGamepadRemoved(e.gamepad.index.toString());
}
}
Input.Gamepad = Gamepad;
})(Input = Gaming.Input || (Gaming.Input = {}));
})(Gaming = Windows.Gaming || (Windows.Gaming = {}));
})(Windows || (Windows = {}));
var Windows;
(function (Windows) {
var Graphics;
(function (Graphics) {
Expand Down
35 changes: 33 additions & 2 deletions src/Uno.UI/ts/Windows/Gaming/Input/Gamepad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@
return result;
}

public static getReading(id: number): string {
var gamepad = navigator.getGamepads()[id];
if (!gamepad) {
return "";
}

var result = "";

result += gamepad.timestamp;

result += '*';

for (var axisId = 0; axisId < gamepad.axes.length; axisId++) {
if (axisId != 0) {
result += '|';
}
result += gamepad.axes[axisId];
}

result += '*';

for (var buttonId = 0; buttonId < gamepad.buttons.length; buttonId++) {
if (buttonId != 0) {
result += '|';
}
result += gamepad.buttons[buttonId].value;
}

return result;
}

public static startGamepadAdded() {
window.addEventListener("gamepadconnected", Gamepad.onGamepadConnected);
}
Expand All @@ -32,15 +63,15 @@
window.removeEventListener("gamepaddisconnected", Gamepad.onGamepadDisconnected);
}

private static onGamepadConnected(e : any) {
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) {
private static onGamepadDisconnected(e: any) {
if (!Gamepad.dispatchGamepadRemoved) {
Gamepad.dispatchGamepadRemoved = (<any>Module).mono_bind_static_method(
"[Uno] Windows.Gaming.Input.Gamepad:DispatchGamepadRemoved");
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/Gaming/Input/Gamepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Windows.Gaming.Input
{
public partial class Gamepad
public partial class Gamepad : IGameController
{
private readonly static object _syncLock = new object();

Expand Down Expand Up @@ -68,7 +68,7 @@ public static event EventHandler<Gamepad> GamepadRemoved
}
}
}
}
}

internal static void OnGamepadAdded(Gamepad gamepad) =>
_gamepadAdded?.Invoke(null, gamepad);
Expand Down
Loading

0 comments on commit 52dab51

Please sign in to comment.