2018-07-29 04:35:36 +00:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Input;
|
2019-12-21 19:52:31 +00:00
|
|
|
|
using Ryujinx.Common.Configuration.Hid;
|
2020-04-03 00:10:02 +00:00
|
|
|
|
using Ryujinx.HLE.HOS.Services.Hid;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
using System;
|
2018-07-02 22:08:54 +00:00
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
using InnerNpadController = Ryujinx.Common.Configuration.Hid.NpadController;
|
|
|
|
|
|
2019-11-29 04:32:51 +00:00
|
|
|
|
namespace Ryujinx.Ui.Input
|
2018-07-02 22:08:54 +00:00
|
|
|
|
{
|
2018-11-20 00:01:36 +00:00
|
|
|
|
public class NpadController
|
2018-07-02 22:08:54 +00:00
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
private InnerNpadController _inner;
|
|
|
|
|
|
|
|
|
|
// NOTE: This should be initialized AFTER GTK for compat reasons with OpenTK SDL2 backend and GTK on Linux.
|
|
|
|
|
// BODY: Usage of Joystick.GetState must be defer to after GTK full initialization. Otherwise, GTK will segfault because SDL2 was already init *sighs*
|
|
|
|
|
public NpadController(InnerNpadController inner)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
_inner = inner;
|
2019-02-11 12:00:32 +00:00
|
|
|
|
}
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
private bool IsEnabled()
|
2019-02-11 12:00:32 +00:00
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
return _inner.Enabled && Joystick.GetState(_inner.Index).IsConnected;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 00:10:02 +00:00
|
|
|
|
public ControllerKeys GetButtons()
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
if (!IsEnabled())
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
JoystickState joystickState = Joystick.GetState(_inner.Index);
|
2018-10-31 01:43:02 +00:00
|
|
|
|
|
2020-04-03 00:10:02 +00:00
|
|
|
|
ControllerKeys buttons = 0;
|
2019-07-22 17:15:46 +00:00
|
|
|
|
|
2020-04-03 00:10:02 +00:00
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.DPadUp)) buttons |= ControllerKeys.DpadUp;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.DPadDown)) buttons |= ControllerKeys.DpadDown;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.DPadLeft)) buttons |= ControllerKeys.DpadLeft;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.DPadRight)) buttons |= ControllerKeys.DpadRight;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.StickButton)) buttons |= ControllerKeys.LStick;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.ButtonMinus)) buttons |= ControllerKeys.Minus;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.ButtonL)) buttons |= ControllerKeys.L | ControllerKeys.Sl;
|
|
|
|
|
if (IsActivated(joystickState, _inner.LeftJoycon.ButtonZl)) buttons |= ControllerKeys.Zl;
|
2019-12-21 19:52:31 +00:00
|
|
|
|
|
2020-04-03 00:10:02 +00:00
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonA)) buttons |= ControllerKeys.A;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonB)) buttons |= ControllerKeys.B;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonX)) buttons |= ControllerKeys.X;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonY)) buttons |= ControllerKeys.Y;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.StickButton)) buttons |= ControllerKeys.RStick;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonPlus)) buttons |= ControllerKeys.Plus;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonR)) buttons |= ControllerKeys.R | ControllerKeys.Sr;
|
|
|
|
|
if (IsActivated(joystickState, _inner.RightJoycon.ButtonZr)) buttons |= ControllerKeys.Zr;
|
2018-10-31 01:43:02 +00:00
|
|
|
|
|
|
|
|
|
return buttons;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2020-04-03 00:10:02 +00:00
|
|
|
|
private bool IsActivated(JoystickState joystickState, ControllerInputId controllerInputId)
|
2019-08-05 18:58:27 +00:00
|
|
|
|
{
|
|
|
|
|
if (controllerInputId <= ControllerInputId.Button20)
|
|
|
|
|
{
|
|
|
|
|
return joystickState.IsButtonDown((int)controllerInputId);
|
|
|
|
|
}
|
|
|
|
|
else if (controllerInputId <= ControllerInputId.Axis5)
|
|
|
|
|
{
|
|
|
|
|
int axis = controllerInputId - ControllerInputId.Axis0;
|
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
return joystickState.GetAxis(axis) > _inner.TriggerThreshold;
|
2019-08-05 18:58:27 +00:00
|
|
|
|
}
|
|
|
|
|
else if (controllerInputId <= ControllerInputId.Hat2Right)
|
|
|
|
|
{
|
|
|
|
|
int hat = (controllerInputId - ControllerInputId.Hat0Up) / 4;
|
|
|
|
|
|
|
|
|
|
int baseHatId = (int)ControllerInputId.Hat0Up + (hat * 4);
|
|
|
|
|
|
|
|
|
|
JoystickHatState hatState = joystickState.GetHat((JoystickHat)hat);
|
|
|
|
|
|
|
|
|
|
if (hatState.IsUp && ((int)controllerInputId % baseHatId == 0)) return true;
|
|
|
|
|
if (hatState.IsDown && ((int)controllerInputId % baseHatId == 1)) return true;
|
|
|
|
|
if (hatState.IsLeft && ((int)controllerInputId % baseHatId == 2)) return true;
|
|
|
|
|
if (hatState.IsRight && ((int)controllerInputId % baseHatId == 3)) return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-29 04:35:36 +00:00
|
|
|
|
public (short, short) GetLeftStick()
|
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
if (!IsEnabled())
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
|
|
|
|
return (0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
return GetStick(_inner.LeftJoycon.Stick);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (short, short) GetRightStick()
|
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
if (!IsEnabled())
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
|
|
|
|
return (0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
return GetStick(_inner.RightJoycon.Stick);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-08-05 18:58:27 +00:00
|
|
|
|
private (short, short) GetStick(ControllerInputId stickInputId)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2019-08-05 18:58:27 +00:00
|
|
|
|
if (stickInputId < ControllerInputId.Axis0 || stickInputId > ControllerInputId.Axis5)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2019-08-05 18:58:27 +00:00
|
|
|
|
return (0, 0);
|
|
|
|
|
}
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2019-12-21 19:52:31 +00:00
|
|
|
|
JoystickState jsState = Joystick.GetState(_inner.Index);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2019-08-05 18:58:27 +00:00
|
|
|
|
int xAxis = stickInputId - ControllerInputId.Axis0;
|
|
|
|
|
|
|
|
|
|
float xValue = jsState.GetAxis(xAxis);
|
|
|
|
|
float yValue = 0 - jsState.GetAxis(xAxis + 1); // Invert Y-axis
|
|
|
|
|
|
|
|
|
|
return ApplyDeadzone(new Vector2(xValue, yValue));
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private (short, short) ApplyDeadzone(Vector2 axis)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2019-12-21 19:52:31 +00:00
|
|
|
|
return (ClampAxis(MathF.Abs(axis.X) > _inner.Deadzone ? axis.X : 0f),
|
|
|
|
|
ClampAxis(MathF.Abs(axis.Y) > _inner.Deadzone ? axis.Y : 0f));
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private static short ClampAxis(float value)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
if (value <= -short.MaxValue)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
|
|
|
|
return -short.MaxValue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
return (short)(value * short.MaxValue);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-02 22:08:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|