Skip to content

Commit

Permalink
fix: Gamepad readings improvements on WASM and Android
Browse files Browse the repository at this point in the history
- Y-axis was flipped on Android and WASM as compared to WinUI
- When the controller had no axes or no buttons, WASM readings crashed
  • Loading branch information
MartinZikmund committed Jun 7, 2022
1 parent 7e45416 commit 7036e6c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@

<Grid RowSpacing="8" Padding="8">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Button Command="{x:Bind ViewModel.UpdateGamepadsCommand}">Get gamepads list</Button>
<Button Command="{x:Bind ViewModel.UpdateReadingsCommand}">Get current readings</Button>
<ToggleButton IsChecked="{x:Bind ViewModel.IsCheckingAutomatically, Mode=TwoWay}">Check automatically</ToggleButton>
<ListView ItemsSource="{x:Bind ViewModel.Gamepads}" SelectionMode="None">
<Button Grid.Row="1" Command="{x:Bind ViewModel.UpdateReadingsCommand}">Get current readings</Button>
<ToggleButton Grid.Row="2" IsChecked="{x:Bind ViewModel.IsCheckingAutomatically, Mode=TwoWay}">Check automatically</ToggleButton>
<ListView Grid.Row="3" ItemsSource="{x:Bind ViewModel.Gamepads}" SelectionMode="None">
<ListView.ItemTemplate>
<DataTemplate x:DataType="local:GamepadReadingViewModel" x:DefaultBindMode="OneWay">
<StackPanel>
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/Gaming/Input/Gamepad.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ internal static bool OnGenericMotionEvent(MotionEvent motionEvent)
var inputDevice = motionEvent.Device!;

reading.LeftThumbstickX = GetCenteredAxis(motionEvent, inputDevice, Axis.X);
reading.LeftThumbstickY = GetCenteredAxis(motionEvent, inputDevice, Axis.Y);
reading.LeftThumbstickY = -1 * GetCenteredAxis(motionEvent, inputDevice, Axis.Y);
reading.RightThumbstickX = GetCenteredAxis(motionEvent, inputDevice, Axis.Z);
reading.RightThumbstickY = GetCenteredAxis(motionEvent, inputDevice, Axis.Rz);
reading.RightThumbstickY = -1 * GetCenteredAxis(motionEvent, inputDevice, Axis.Rz);

var leftTrigger = GetCenteredAxis(motionEvent, inputDevice, Axis.Brake);
if (leftTrigger == 0)
Expand Down
57 changes: 32 additions & 25 deletions src/Uno.UWP/Gaming/Input/Gamepad.wasm.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using Uno;
using Uno.Extensions;
Expand Down Expand Up @@ -37,42 +38,48 @@ public GamepadReading GetCurrentReading()
var axesPart = parts[1];
var buttonsPart = parts[2];

var timestampDouble = double.Parse(timestampPart);
var timestampDouble = double.Parse(timestampPart, CultureInfo.InvariantCulture);
reading.Timestamp = (ulong)(timestampDouble * 1000); // JS timestamp is in milliseconds

var axes = axesPart.Split('|').Select(double.Parse).ToArray();
if (!string.IsNullOrEmpty(axesPart))
{
var axes = axesPart.Split('|').Select(p => double.Parse(p, CultureInfo.InvariantCulture)).ToArray();

reading.LeftThumbstickX = GetGamepadValueIfExists(ref axes, 0);
reading.LeftThumbstickY = GetGamepadValueIfExists(ref axes, 1);
reading.RightThumbstickX = GetGamepadValueIfExists(ref axes, 2);
reading.RightThumbstickY = GetGamepadValueIfExists(ref axes, 3);
reading.LeftThumbstickX = GetGamepadValueIfExists(ref axes, 0);
reading.LeftThumbstickY = -1 * GetGamepadValueIfExists(ref axes, 1);
reading.RightThumbstickX = GetGamepadValueIfExists(ref axes, 2);
reading.RightThumbstickY = -1 * GetGamepadValueIfExists(ref axes, 3);
}

var buttons = buttonsPart.Split('|').Select(double.Parse).ToArray();
if (!string.IsNullOrEmpty(buttonsPart))
{
var buttons = buttonsPart.Split('|').Select(p => double.Parse(p, CultureInfo.InvariantCulture)).ToArray();

var pressedButtons = GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 0) ? GamepadButtons.A : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 1) ? GamepadButtons.B : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 2) ? GamepadButtons.X : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 3) ? GamepadButtons.Y : GamepadButtons.None;
var pressedButtons = GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 0) ? GamepadButtons.A : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 1) ? GamepadButtons.B : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 2) ? GamepadButtons.X : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 3) ? GamepadButtons.Y : GamepadButtons.None;

pressedButtons |= IsButtonPressed(ref buttons, 4) ? GamepadButtons.LeftShoulder : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 5) ? GamepadButtons.RightShoulder : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 4) ? GamepadButtons.LeftShoulder : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 5) ? GamepadButtons.RightShoulder : GamepadButtons.None;

pressedButtons |= IsButtonPressed(ref buttons, 8) ? GamepadButtons.View : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 9) ? GamepadButtons.Menu : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 8) ? GamepadButtons.View : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 9) ? GamepadButtons.Menu : GamepadButtons.None;

pressedButtons |= IsButtonPressed(ref buttons, 10) ? GamepadButtons.LeftThumbstick : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 11) ? GamepadButtons.RightThumbstick : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 10) ? GamepadButtons.LeftThumbstick : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 11) ? GamepadButtons.RightThumbstick : GamepadButtons.None;

pressedButtons |= IsButtonPressed(ref buttons, 12) ? GamepadButtons.DPadUp : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 13) ? GamepadButtons.DPadDown : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 14) ? GamepadButtons.DPadLeft : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 15) ? GamepadButtons.DPadRight : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 12) ? GamepadButtons.DPadUp : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 13) ? GamepadButtons.DPadDown : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 14) ? GamepadButtons.DPadLeft : GamepadButtons.None;
pressedButtons |= IsButtonPressed(ref buttons, 15) ? GamepadButtons.DPadRight : GamepadButtons.None;

reading.Buttons = pressedButtons;
reading.Buttons = pressedButtons;

reading.LeftTrigger = GetGamepadValueIfExists(ref buttons, 6);
reading.RightTrigger = GetGamepadValueIfExists(ref buttons, 7);
reading.LeftTrigger = GetGamepadValueIfExists(ref buttons, 6);
reading.RightTrigger = GetGamepadValueIfExists(ref buttons, 7);
}

return reading;
}
Expand Down

0 comments on commit 7036e6c

Please sign in to comment.