Remove ambigious object

This commit is contained in:
Isaac Marovitz 2023-09-19 10:50:01 -04:00 committed by Isaac Marovitz
parent c7f52388c9
commit a0a74d3d10
7 changed files with 111 additions and 69 deletions

View file

@ -1,14 +1,8 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives; using Avalonia.Controls.Primitives;
using Avalonia.Data;
using Avalonia.LogicalTree;
using Avalonia.Threading; using Avalonia.Threading;
using Ryujinx.Ava.Common.Locale;
using Ryujinx.Input; using Ryujinx.Input;
using Ryujinx.Input.Assigner; using Ryujinx.Input.Assigner;
using System; using System;
using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace Ryujinx.Ava.UI.Helpers namespace Ryujinx.Ava.UI.Helpers
@ -19,13 +13,13 @@ namespace Ryujinx.Ava.UI.Helpers
{ {
public ToggleButton Button { get; } public ToggleButton Button { get; }
public bool IsAssigned { get; } public bool IsAssigned { get; }
public object Key { get; } public ButtonValue? ButtonValue { get; }
public ButtonAssignedEventArgs(ToggleButton button, bool isAssigned, object key) public ButtonAssignedEventArgs(ToggleButton button, bool isAssigned, ButtonValue? buttonValue)
{ {
Button = button; Button = button;
IsAssigned = isAssigned; IsAssigned = isAssigned;
Key = key; ButtonValue = buttonValue;
} }
} }
@ -83,7 +77,7 @@ namespace Ryujinx.Ava.UI.Helpers
await Dispatcher.UIThread.InvokeAsync(() => await Dispatcher.UIThread.InvokeAsync(() =>
{ {
object pressedButton = assigner.GetPressedButton(); ButtonValue? pressedButton = assigner.GetPressedButton();
if (_shouldUnbind) if (_shouldUnbind)
{ {
@ -95,7 +89,7 @@ namespace Ryujinx.Ava.UI.Helpers
ToggledButton.IsChecked = false; ToggledButton.IsChecked = false;
ButtonAssigned?.Invoke(this, new ButtonAssignedEventArgs(ToggledButton, pressedButton != null, pressedButton)); ButtonAssigned?.Invoke(this, new ButtonAssignedEventArgs(ToggledButton, pressedButton.HasValue, pressedButton));
}); });
} }

View file

@ -9,7 +9,6 @@ using Ryujinx.Ava.UI.ViewModels.Input;
using Ryujinx.Common.Configuration.Hid.Controller; using Ryujinx.Common.Configuration.Hid.Controller;
using Ryujinx.Input; using Ryujinx.Input;
using Ryujinx.Input.Assigner; using Ryujinx.Input.Assigner;
using StickInputId = Ryujinx.Common.Configuration.Hid.Controller.StickInputId;
namespace Ryujinx.Ava.UI.Views.Input namespace Ryujinx.Ava.UI.Views.Input
{ {
@ -69,75 +68,76 @@ namespace Ryujinx.Ava.UI.Views.Input
if (e.IsAssigned) if (e.IsAssigned)
{ {
var viewModel = (DataContext as ControllerInputViewModel); var viewModel = (DataContext as ControllerInputViewModel);
var buttonValue = e.ButtonValue.Value;
viewModel.parentModel.IsModified = true; viewModel.parentModel.IsModified = true;
switch (button.Name) switch (button.Name)
{ {
case "ButtonZl": case "ButtonZl":
viewModel.Config.ButtonZl = (GamepadInputId)e.Key; viewModel.Config.ButtonZl = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonL": case "ButtonL":
viewModel.Config.ButtonL = (GamepadInputId)e.Key; viewModel.Config.ButtonL = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonMinus": case "ButtonMinus":
viewModel.Config.ButtonMinus = (GamepadInputId)e.Key; viewModel.Config.ButtonMinus = buttonValue.AsGamepadButtonInputId();
break; break;
case "LeftStickButton": case "LeftStickButton":
viewModel.Config.LeftStickButton = (GamepadInputId)e.Key; viewModel.Config.LeftStickButton = buttonValue.AsGamepadButtonInputId();
break; break;
case "LeftJoystick": case "LeftJoystick":
viewModel.Config.LeftJoystick = (StickInputId)e.Key; viewModel.Config.LeftJoystick = buttonValue.AsGamepadStickId();
break; break;
case "DpadUp": case "DpadUp":
viewModel.Config.DpadUp = (GamepadInputId)e.Key; viewModel.Config.DpadUp = buttonValue.AsGamepadButtonInputId();
break; break;
case "DpadDown": case "DpadDown":
viewModel.Config.DpadDown = (GamepadInputId)e.Key; viewModel.Config.DpadDown = buttonValue.AsGamepadButtonInputId();
break; break;
case "DpadLeft": case "DpadLeft":
viewModel.Config.DpadLeft = (GamepadInputId)e.Key; viewModel.Config.DpadLeft = buttonValue.AsGamepadButtonInputId();
break; break;
case "DpadRight": case "DpadRight":
viewModel.Config.DpadRight = (GamepadInputId)e.Key; viewModel.Config.DpadRight = buttonValue.AsGamepadButtonInputId();
break; break;
case "LeftButtonSr": case "LeftButtonSr":
viewModel.Config.LeftButtonSr = (GamepadInputId)e.Key; viewModel.Config.LeftButtonSr = buttonValue.AsGamepadButtonInputId();
break; break;
case "LeftButtonSl": case "LeftButtonSl":
viewModel.Config.LeftButtonSl = (GamepadInputId)e.Key; viewModel.Config.LeftButtonSl = buttonValue.AsGamepadButtonInputId();
break; break;
case "RightButtonSr": case "RightButtonSr":
viewModel.Config.RightButtonSr = (GamepadInputId)e.Key; viewModel.Config.RightButtonSr = buttonValue.AsGamepadButtonInputId();
break; break;
case "RightButtonSl": case "RightButtonSl":
viewModel.Config.RightButtonSl = (GamepadInputId)e.Key; viewModel.Config.RightButtonSl = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonZr": case "ButtonZr":
viewModel.Config.ButtonZr = (GamepadInputId)e.Key; viewModel.Config.ButtonZr = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonR": case "ButtonR":
viewModel.Config.ButtonR = (GamepadInputId)e.Key; viewModel.Config.ButtonR = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonPlus": case "ButtonPlus":
viewModel.Config.ButtonPlus = (GamepadInputId)e.Key; viewModel.Config.ButtonPlus = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonA": case "ButtonA":
viewModel.Config.ButtonA = (GamepadInputId)e.Key; viewModel.Config.ButtonA = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonB": case "ButtonB":
viewModel.Config.ButtonB = (GamepadInputId)e.Key; viewModel.Config.ButtonB = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonX": case "ButtonX":
viewModel.Config.ButtonX = (GamepadInputId)e.Key; viewModel.Config.ButtonX = buttonValue.AsGamepadButtonInputId();
break; break;
case "ButtonY": case "ButtonY":
viewModel.Config.ButtonY = (GamepadInputId)e.Key; viewModel.Config.ButtonY = buttonValue.AsGamepadButtonInputId();
break; break;
case "RightStickButton": case "RightStickButton":
viewModel.Config.RightStickButton = (GamepadInputId)e.Key; viewModel.Config.RightStickButton = buttonValue.AsGamepadButtonInputId();
break; break;
case "RightJoystick": case "RightJoystick":
viewModel.Config.RightJoystick = (StickInputId)e.Key; viewModel.Config.RightJoystick = buttonValue.AsGamepadStickId();
break; break;
} }
} }

View file

@ -8,7 +8,6 @@ using Ryujinx.Ava.UI.Helpers;
using Ryujinx.Ava.UI.ViewModels.Input; using Ryujinx.Ava.UI.ViewModels.Input;
using Ryujinx.Input; using Ryujinx.Input;
using Ryujinx.Input.Assigner; using Ryujinx.Input.Assigner;
using Key = Ryujinx.Common.Configuration.Hid.Key;
namespace Ryujinx.Ava.UI.Views.Input namespace Ryujinx.Ava.UI.Views.Input
{ {
@ -68,93 +67,94 @@ namespace Ryujinx.Ava.UI.Views.Input
if (e.IsAssigned) if (e.IsAssigned)
{ {
var viewModel = (DataContext as KeyboardInputViewModel); var viewModel = (DataContext as KeyboardInputViewModel);
var buttonValue = e.ButtonValue.Value;
viewModel.parentModel.IsModified = true; viewModel.parentModel.IsModified = true;
switch (button.Name) switch (button.Name)
{ {
case "ButtonZl": case "ButtonZl":
viewModel.Config.ButtonZl = (Key)e.Key; viewModel.Config.ButtonZl = buttonValue.AsKey();
break; break;
case "ButtonL": case "ButtonL":
viewModel.Config.ButtonL = (Key)e.Key; viewModel.Config.ButtonL = buttonValue.AsKey();
break; break;
case "ButtonMinus": case "ButtonMinus":
viewModel.Config.ButtonMinus = (Key)e.Key; viewModel.Config.ButtonMinus = buttonValue.AsKey();
break; break;
case "LeftStickButton": case "LeftStickButton":
viewModel.Config.LeftStickButton = (Key)e.Key; viewModel.Config.LeftStickButton = buttonValue.AsKey();
break; break;
case "LeftStickUp": case "LeftStickUp":
viewModel.Config.LeftStickUp = (Key)e.Key; viewModel.Config.LeftStickUp = buttonValue.AsKey();
break; break;
case "LeftStickDown": case "LeftStickDown":
viewModel.Config.LeftStickDown = (Key)e.Key; viewModel.Config.LeftStickDown = buttonValue.AsKey();
break; break;
case "LeftStickRight": case "LeftStickRight":
viewModel.Config.LeftStickRight = (Key)e.Key; viewModel.Config.LeftStickRight = buttonValue.AsKey();
break; break;
case "LeftStickLeft": case "LeftStickLeft":
viewModel.Config.LeftStickLeft = (Key)e.Key; viewModel.Config.LeftStickLeft = buttonValue.AsKey();
break; break;
case "DpadUp": case "DpadUp":
viewModel.Config.DpadUp = (Key)e.Key; viewModel.Config.DpadUp = buttonValue.AsKey();
break; break;
case "DpadDown": case "DpadDown":
viewModel.Config.DpadDown = (Key)e.Key; viewModel.Config.DpadDown = buttonValue.AsKey();
break; break;
case "DpadLeft": case "DpadLeft":
viewModel.Config.DpadLeft = (Key)e.Key; viewModel.Config.DpadLeft = buttonValue.AsKey();
break; break;
case "DpadRight": case "DpadRight":
viewModel.Config.DpadRight = (Key)e.Key; viewModel.Config.DpadRight = buttonValue.AsKey();
break; break;
case "LeftButtonSr": case "LeftButtonSr":
viewModel.Config.LeftButtonSr = (Key)e.Key; viewModel.Config.LeftButtonSr = buttonValue.AsKey();
break; break;
case "LeftButtonSl": case "LeftButtonSl":
viewModel.Config.LeftButtonSl = (Key)e.Key; viewModel.Config.LeftButtonSl = buttonValue.AsKey();
break; break;
case "RightButtonSr": case "RightButtonSr":
viewModel.Config.RightButtonSr = (Key)e.Key; viewModel.Config.RightButtonSr = buttonValue.AsKey();
break; break;
case "RightButtonSl": case "RightButtonSl":
viewModel.Config.RightButtonSl = (Key)e.Key; viewModel.Config.RightButtonSl = buttonValue.AsKey();
break; break;
case "ButtonZr": case "ButtonZr":
viewModel.Config.ButtonZr = (Key)e.Key; viewModel.Config.ButtonZr = buttonValue.AsKey();
break; break;
case "ButtonR": case "ButtonR":
viewModel.Config.ButtonR = (Key)e.Key; viewModel.Config.ButtonR = buttonValue.AsKey();
break; break;
case "ButtonPlus": case "ButtonPlus":
viewModel.Config.ButtonPlus = (Key)e.Key; viewModel.Config.ButtonPlus = buttonValue.AsKey();
break; break;
case "ButtonA": case "ButtonA":
viewModel.Config.ButtonA = (Key)e.Key; viewModel.Config.ButtonA = buttonValue.AsKey();
break; break;
case "ButtonB": case "ButtonB":
viewModel.Config.ButtonB = (Key)e.Key; viewModel.Config.ButtonB = buttonValue.AsKey();
break; break;
case "ButtonX": case "ButtonX":
viewModel.Config.ButtonX = (Key)e.Key; viewModel.Config.ButtonX = buttonValue.AsKey();
break; break;
case "ButtonY": case "ButtonY":
viewModel.Config.ButtonY = (Key)e.Key; viewModel.Config.ButtonY = buttonValue.AsKey();
break; break;
case "RightStickButton": case "RightStickButton":
viewModel.Config.RightStickButton = (Key)e.Key; viewModel.Config.RightStickButton = buttonValue.AsKey();
break; break;
case "RightStickUp": case "RightStickUp":
viewModel.Config.RightStickUp = (Key)e.Key; viewModel.Config.RightStickUp = buttonValue.AsKey();
break; break;
case "RightStickDown": case "RightStickDown":
viewModel.Config.RightStickDown = (Key)e.Key; viewModel.Config.RightStickDown = buttonValue.AsKey();
break; break;
case "RightStickRight": case "RightStickRight":
viewModel.Config.RightStickRight = (Key)e.Key; viewModel.Config.RightStickRight = buttonValue.AsKey();
break; break;
case "RightStickLeft": case "RightStickLeft":
viewModel.Config.RightStickLeft = (Key)e.Key; viewModel.Config.RightStickLeft = buttonValue.AsKey();
break; break;
} }
} }

View file

@ -59,13 +59,13 @@ namespace Ryujinx.Input.Assigner
return _gamepad == null || !_gamepad.IsConnected; return _gamepad == null || !_gamepad.IsConnected;
} }
public object GetPressedButton() public ButtonValue? GetPressedButton()
{ {
IEnumerable<GamepadButtonInputId> pressedButtons = _detector.GetPressedButtons(); IEnumerable<GamepadButtonInputId> pressedButtons = _detector.GetPressedButtons();
if (pressedButtons.Any()) if (pressedButtons.Any())
{ {
return !_forStick ? pressedButtons.First() : ((StickInputId)pressedButtons.First()); return !_forStick ? new(pressedButtons.First()) : new(((StickInputId)pressedButtons.First()));
} }
return null; return null;

View file

@ -31,6 +31,6 @@ namespace Ryujinx.Input.Assigner
/// Get the pressed button that was read in <see cref="ReadInput"/> by the button assigner. /// Get the pressed button that was read in <see cref="ReadInput"/> by the button assigner.
/// </summary> /// </summary>
/// <returns>The pressed button that was read</returns> /// <returns>The pressed button that was read</returns>
object GetPressedButton(); ButtonValue? GetPressedButton();
} }
} }

View file

@ -31,15 +31,15 @@ namespace Ryujinx.Input.Assigner
return _keyboardState.IsPressed(Key.Escape); return _keyboardState.IsPressed(Key.Escape);
} }
public object GetPressedButton() public ButtonValue? GetPressedButton()
{ {
object keyPressed = null; ButtonValue? keyPressed = null;
for (Key key = Key.Unknown; key < Key.Count; key++) for (Key key = Key.Unknown; key < Key.Count; key++)
{ {
if (_keyboardState.IsPressed(key)) if (_keyboardState.IsPressed(key))
{ {
keyPressed = key; keyPressed = new(key);
break; break;
} }
} }

View file

@ -0,0 +1,48 @@
using System.Diagnostics;
namespace Ryujinx.Input
{
public enum ButtonValueType { Key, GamepadButtonInputId, StickId }
public struct ButtonValue
{
public ButtonValueType Type;
public uint RawValue;
public ButtonValue(Key key)
{
Type = ButtonValueType.Key;
RawValue = (uint)key;
}
public ButtonValue(GamepadButtonInputId gamepad)
{
Type = ButtonValueType.GamepadButtonInputId;
RawValue = (uint)gamepad;
}
public ButtonValue(StickInputId stick)
{
Type = ButtonValueType.StickId;
RawValue = (uint)stick;
}
public Common.Configuration.Hid.Key AsKey()
{
Debug.Assert(Type == ButtonValueType.Key);
return (Common.Configuration.Hid.Key)RawValue;
}
public Common.Configuration.Hid.Controller.GamepadInputId AsGamepadButtonInputId()
{
Debug.Assert(Type == ButtonValueType.GamepadButtonInputId);
return (Common.Configuration.Hid.Controller.GamepadInputId)RawValue;
}
public Common.Configuration.Hid.Controller.StickInputId AsGamepadStickId()
{
Debug.Assert(Type == ButtonValueType.StickId);
return (Common.Configuration.Hid.Controller.StickInputId)RawValue;
}
}
}