2018-07-29 04:35:36 +00:00
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Input;
|
|
|
|
|
using Ryujinx.HLE.Input;
|
|
|
|
|
using System;
|
2018-07-02 22:08:54 +00:00
|
|
|
|
|
|
|
|
|
namespace Ryujinx.UI.Input
|
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
public enum ControllerInputId
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
|
|
|
|
Invalid,
|
|
|
|
|
LStick,
|
2019-02-11 12:00:32 +00:00
|
|
|
|
RStick,
|
|
|
|
|
LShoulder,
|
|
|
|
|
RShoulder,
|
|
|
|
|
LTrigger,
|
|
|
|
|
RTrigger,
|
|
|
|
|
LJoystick,
|
|
|
|
|
RJoystick,
|
2018-07-29 04:35:36 +00:00
|
|
|
|
DPadUp,
|
|
|
|
|
DPadDown,
|
|
|
|
|
DPadLeft,
|
|
|
|
|
DPadRight,
|
2019-02-11 12:00:32 +00:00
|
|
|
|
Start,
|
2018-07-29 04:35:36 +00:00
|
|
|
|
Back,
|
|
|
|
|
A,
|
|
|
|
|
B,
|
|
|
|
|
X,
|
2019-02-11 12:00:32 +00:00
|
|
|
|
Y
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 00:01:36 +00:00
|
|
|
|
public struct NpadControllerLeft
|
2018-07-02 22:08:54 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
public ControllerInputId Stick;
|
|
|
|
|
public ControllerInputId StickButton;
|
|
|
|
|
public ControllerInputId DPadUp;
|
|
|
|
|
public ControllerInputId DPadDown;
|
|
|
|
|
public ControllerInputId DPadLeft;
|
|
|
|
|
public ControllerInputId DPadRight;
|
|
|
|
|
public ControllerInputId ButtonMinus;
|
|
|
|
|
public ControllerInputId ButtonL;
|
|
|
|
|
public ControllerInputId ButtonZl;
|
2018-07-02 22:08:54 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-20 00:01:36 +00:00
|
|
|
|
public struct NpadControllerRight
|
2018-07-02 22:08:54 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
public ControllerInputId Stick;
|
|
|
|
|
public ControllerInputId StickButton;
|
|
|
|
|
public ControllerInputId ButtonA;
|
|
|
|
|
public ControllerInputId ButtonB;
|
|
|
|
|
public ControllerInputId ButtonX;
|
|
|
|
|
public ControllerInputId ButtonY;
|
|
|
|
|
public ControllerInputId ButtonPlus;
|
|
|
|
|
public ControllerInputId ButtonR;
|
|
|
|
|
public ControllerInputId ButtonZr;
|
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-02-11 12:00:32 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Enables or disables controller support
|
|
|
|
|
/// </summary>
|
|
|
|
|
public bool Enabled { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controller Device Index
|
|
|
|
|
/// </summary>
|
|
|
|
|
public int Index { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controller Analog Stick Deadzone
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float Deadzone { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Controller Trigger Threshold
|
|
|
|
|
/// </summary>
|
|
|
|
|
public float TriggerThreshold { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Left JoyCon Controller Bindings
|
|
|
|
|
/// </summary>
|
|
|
|
|
public NpadControllerLeft LeftJoycon { get; private set; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Right JoyCon Controller Bindings
|
|
|
|
|
/// </summary>
|
|
|
|
|
public NpadControllerRight RightJoycon { get; private set; }
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2018-11-20 00:01:36 +00:00
|
|
|
|
public NpadController(
|
2019-02-11 12:00:32 +00:00
|
|
|
|
bool enabled,
|
|
|
|
|
int index,
|
|
|
|
|
float deadzone,
|
|
|
|
|
float triggerThreshold,
|
|
|
|
|
NpadControllerLeft leftJoycon,
|
|
|
|
|
NpadControllerRight rightJoycon)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
Enabled = enabled;
|
|
|
|
|
Index = index;
|
|
|
|
|
Deadzone = deadzone;
|
|
|
|
|
TriggerThreshold = triggerThreshold;
|
2019-02-11 12:00:32 +00:00
|
|
|
|
LeftJoycon = leftJoycon;
|
|
|
|
|
RightJoycon = rightJoycon;
|
|
|
|
|
}
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2019-02-11 12:00:32 +00:00
|
|
|
|
public void SetEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
Enabled = enabled;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HidControllerButtons GetButtons()
|
|
|
|
|
{
|
|
|
|
|
if (!Enabled)
|
|
|
|
|
{
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
GamePadState gpState = GamePad.GetState(Index);
|
|
|
|
|
|
|
|
|
|
HidControllerButtons buttons = 0;
|
|
|
|
|
|
2019-02-11 12:00:32 +00:00
|
|
|
|
if (IsPressed(gpState, LeftJoycon.DPadUp)) buttons |= HidControllerButtons.DpadUp;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.DPadDown)) buttons |= HidControllerButtons.DpadDown;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.DPadLeft)) buttons |= HidControllerButtons.DpadLeft;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.DPadRight)) buttons |= HidControllerButtons.DPadRight;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.StickButton)) buttons |= HidControllerButtons.StickLeft;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.ButtonMinus)) buttons |= HidControllerButtons.Minus;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.ButtonL)) buttons |= HidControllerButtons.L;
|
|
|
|
|
if (IsPressed(gpState, LeftJoycon.ButtonZl)) buttons |= HidControllerButtons.Zl;
|
|
|
|
|
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonA)) buttons |= HidControllerButtons.A;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonB)) buttons |= HidControllerButtons.B;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonX)) buttons |= HidControllerButtons.X;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonY)) buttons |= HidControllerButtons.Y;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.StickButton)) buttons |= HidControllerButtons.StickRight;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonPlus)) buttons |= HidControllerButtons.Plus;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonR)) buttons |= HidControllerButtons.R;
|
|
|
|
|
if (IsPressed(gpState, RightJoycon.ButtonZr)) buttons |= HidControllerButtons.Zr;
|
2018-10-31 01:43:02 +00:00
|
|
|
|
|
|
|
|
|
return buttons;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (short, short) GetLeftStick()
|
|
|
|
|
{
|
|
|
|
|
if (!Enabled)
|
|
|
|
|
{
|
|
|
|
|
return (0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 12:00:32 +00:00
|
|
|
|
return GetStick(LeftJoycon.Stick);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (short, short) GetRightStick()
|
|
|
|
|
{
|
|
|
|
|
if (!Enabled)
|
|
|
|
|
{
|
|
|
|
|
return (0, 0);
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-11 12:00:32 +00:00
|
|
|
|
return GetStick(RightJoycon.Stick);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private (short, short) GetStick(ControllerInputId joystick)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
GamePadState gpState = GamePad.GetState(Index);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
switch (joystick)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
case ControllerInputId.LJoystick:
|
|
|
|
|
return ApplyDeadzone(gpState.ThumbSticks.Left);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
case ControllerInputId.RJoystick:
|
|
|
|
|
return ApplyDeadzone(gpState.ThumbSticks.Right);
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return (0, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
private (short, short) ApplyDeadzone(Vector2 axis)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
return (ClampAxis(MathF.Abs(axis.X) > Deadzone ? axis.X : 0f),
|
|
|
|
|
ClampAxis(MathF.Abs(axis.Y) > 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-10-31 01:43:02 +00:00
|
|
|
|
private bool IsPressed(GamePadState gpState, ControllerInputId button)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
switch (button)
|
2018-07-29 04:35:36 +00:00
|
|
|
|
{
|
2018-10-31 01:43:02 +00:00
|
|
|
|
case ControllerInputId.A: return gpState.Buttons.A == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.B: return gpState.Buttons.B == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.X: return gpState.Buttons.X == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.Y: return gpState.Buttons.Y == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.LStick: return gpState.Buttons.LeftStick == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.RStick: return gpState.Buttons.RightStick == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.LShoulder: return gpState.Buttons.LeftShoulder == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.RShoulder: return gpState.Buttons.RightShoulder == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.DPadUp: return gpState.DPad.Up == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.DPadDown: return gpState.DPad.Down == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.DPadLeft: return gpState.DPad.Left == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.DPadRight: return gpState.DPad.Right == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.Start: return gpState.Buttons.Start == ButtonState.Pressed;
|
|
|
|
|
case ControllerInputId.Back: return gpState.Buttons.Back == ButtonState.Pressed;
|
|
|
|
|
|
|
|
|
|
case ControllerInputId.LTrigger: return gpState.Triggers.Left >= TriggerThreshold;
|
|
|
|
|
case ControllerInputId.RTrigger: return gpState.Triggers.Right >= TriggerThreshold;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
|
|
|
|
//Using thumbsticks as buttons is not common, but it would be nice not to ignore them
|
2018-10-31 01:43:02 +00:00
|
|
|
|
case ControllerInputId.LJoystick:
|
|
|
|
|
return gpState.ThumbSticks.Left.X >= Deadzone ||
|
|
|
|
|
gpState.ThumbSticks.Left.Y >= Deadzone;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
2018-10-31 01:43:02 +00:00
|
|
|
|
case ControllerInputId.RJoystick:
|
|
|
|
|
return gpState.ThumbSticks.Right.X >= Deadzone ||
|
|
|
|
|
gpState.ThumbSticks.Right.Y >= Deadzone;
|
2018-07-29 04:35:36 +00:00
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-02 22:08:54 +00:00
|
|
|
|
}
|
|
|
|
|
}
|