2022-02-23 05:14:32 +00:00
|
|
|
|
using System;
|
2022-03-18 18:46:44 +00:00
|
|
|
|
using System.Collections.Generic;
|
2021-03-10 05:48:31 +00:00
|
|
|
|
using MoonWorks.Math;
|
2021-01-19 07:29:07 +00:00
|
|
|
|
using SDL2;
|
|
|
|
|
|
2021-01-22 08:20:07 +00:00
|
|
|
|
namespace MoonWorks.Input
|
2021-01-19 07:29:07 +00:00
|
|
|
|
{
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public class Gamepad
|
|
|
|
|
{
|
|
|
|
|
internal IntPtr Handle;
|
2022-07-13 18:53:09 +00:00
|
|
|
|
internal int JoystickInstanceID;
|
|
|
|
|
|
|
|
|
|
public int Slot { get; internal set; }
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public GamepadButton A { get; }
|
|
|
|
|
public GamepadButton B { get; }
|
|
|
|
|
public GamepadButton X { get; }
|
|
|
|
|
public GamepadButton Y { get; }
|
|
|
|
|
public GamepadButton Back { get; }
|
|
|
|
|
public GamepadButton Guide { get; }
|
|
|
|
|
public GamepadButton Start { get; }
|
|
|
|
|
public GamepadButton LeftStick { get; }
|
|
|
|
|
public GamepadButton RightStick { get; }
|
|
|
|
|
public GamepadButton LeftShoulder { get; }
|
|
|
|
|
public GamepadButton RightShoulder { get; }
|
|
|
|
|
public GamepadButton DpadUp { get; }
|
|
|
|
|
public GamepadButton DpadDown { get; }
|
|
|
|
|
public GamepadButton DpadLeft { get; }
|
|
|
|
|
public GamepadButton DpadRight { get; }
|
|
|
|
|
|
|
|
|
|
public Axis LeftX { get; }
|
|
|
|
|
public Axis LeftY { get; }
|
|
|
|
|
public Axis RightX { get; }
|
|
|
|
|
public Axis RightY { get; }
|
|
|
|
|
|
|
|
|
|
public AxisButton LeftXLeft { get; }
|
|
|
|
|
public AxisButton LeftXRight { get; }
|
|
|
|
|
public AxisButton LeftYUp { get; }
|
|
|
|
|
public AxisButton LeftYDown { get; }
|
|
|
|
|
|
|
|
|
|
public AxisButton RightXLeft { get; }
|
|
|
|
|
public AxisButton RightXRight { get; }
|
|
|
|
|
public AxisButton RightYUp { get; }
|
|
|
|
|
public AxisButton RightYDown { get; }
|
|
|
|
|
|
|
|
|
|
public Trigger TriggerLeft { get; }
|
|
|
|
|
public Trigger TriggerRight { get; }
|
|
|
|
|
|
|
|
|
|
public TriggerButton TriggerLeftButton { get; }
|
|
|
|
|
public TriggerButton TriggerRightButton { get; }
|
2022-03-18 18:46:44 +00:00
|
|
|
|
|
2022-03-18 19:11:00 +00:00
|
|
|
|
public bool IsDummy => Handle == IntPtr.Zero;
|
|
|
|
|
|
2022-07-08 23:47:12 +00:00
|
|
|
|
public bool AnyPressed { get; private set; }
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public VirtualButton AnyPressedButton { get; private set; }
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
private Dictionary<SDL.SDL_GameControllerButton, GamepadButton> EnumToButton;
|
2022-03-18 18:46:44 +00:00
|
|
|
|
private Dictionary<SDL.SDL_GameControllerAxis, Axis> EnumToAxis;
|
|
|
|
|
private Dictionary<SDL.SDL_GameControllerAxis, Trigger> EnumToTrigger;
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
private Dictionary<AxisButtonCode, AxisButton> AxisButtonCodeToAxisButton;
|
|
|
|
|
private Dictionary<TriggerCode, TriggerButton> TriggerCodeToTriggerButton;
|
|
|
|
|
|
|
|
|
|
private VirtualButton[] VirtualButtons;
|
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
internal Gamepad(IntPtr handle, int slot)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
|
|
|
|
Handle = handle;
|
2022-07-13 18:53:09 +00:00
|
|
|
|
Slot = slot;
|
|
|
|
|
|
|
|
|
|
IntPtr joystickHandle = SDL.SDL_GameControllerGetJoystick(Handle);
|
|
|
|
|
JoystickInstanceID = SDL.SDL_JoystickInstanceID(joystickHandle);
|
2022-03-18 18:46:44 +00:00
|
|
|
|
|
2022-07-08 23:47:12 +00:00
|
|
|
|
AnyPressed = false;
|
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
A = new GamepadButton(this, GamepadButtonCode.A, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A);
|
|
|
|
|
B = new GamepadButton(this, GamepadButtonCode.B, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B);
|
|
|
|
|
X = new GamepadButton(this, GamepadButtonCode.X, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X);
|
|
|
|
|
Y = new GamepadButton(this, GamepadButtonCode.Y, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
Back = new GamepadButton(this, GamepadButtonCode.Back, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK);
|
|
|
|
|
Guide = new GamepadButton(this, GamepadButtonCode.Guide, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE);
|
|
|
|
|
Start = new GamepadButton(this, GamepadButtonCode.Start, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
LeftStick = new GamepadButton(this, GamepadButtonCode.LeftStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK);
|
|
|
|
|
RightStick = new GamepadButton(this, GamepadButtonCode.RightStick, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
LeftShoulder = new GamepadButton(this, GamepadButtonCode.LeftShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER);
|
|
|
|
|
RightShoulder = new GamepadButton(this, GamepadButtonCode.RightShoulder, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
DpadUp = new GamepadButton(this, GamepadButtonCode.DpadUp, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP);
|
|
|
|
|
DpadDown = new GamepadButton(this, GamepadButtonCode.DpadDown, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN);
|
|
|
|
|
DpadLeft = new GamepadButton(this, GamepadButtonCode.DpadLeft, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT);
|
|
|
|
|
DpadRight = new GamepadButton(this, GamepadButtonCode.DpadRight, SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
LeftX = new Axis(this, AxisCode.LeftX, SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX);
|
|
|
|
|
LeftY = new Axis(this, AxisCode.LeftY, SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY);
|
|
|
|
|
RightX = new Axis(this, AxisCode.RightX, SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTX);
|
|
|
|
|
RightY = new Axis(this, AxisCode.RightY, SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTY);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
|
|
|
|
LeftXLeft = new AxisButton(LeftX, false);
|
|
|
|
|
LeftXRight = new AxisButton(LeftX, true);
|
2023-06-29 01:19:34 +00:00
|
|
|
|
LeftYUp = new AxisButton(LeftY, true);
|
|
|
|
|
LeftYDown = new AxisButton(LeftY, false);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
|
|
|
|
RightXLeft = new AxisButton(RightX, false);
|
|
|
|
|
RightXRight = new AxisButton(RightX, true);
|
2023-06-29 01:19:34 +00:00
|
|
|
|
RightYUp = new AxisButton(RightY, true);
|
|
|
|
|
RightYDown = new AxisButton(RightY, false);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
2022-07-13 18:53:09 +00:00
|
|
|
|
TriggerLeft = new Trigger(this, TriggerCode.Left, SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT);
|
|
|
|
|
TriggerRight = new Trigger(this, TriggerCode.Right, SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT);
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
|
|
|
|
TriggerLeftButton = new TriggerButton(TriggerLeft);
|
|
|
|
|
TriggerRightButton = new TriggerButton(TriggerRight);
|
|
|
|
|
|
|
|
|
|
EnumToButton = new Dictionary<SDL.SDL_GameControllerButton, GamepadButton>
|
2022-03-18 18:46:44 +00:00
|
|
|
|
{
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_A, A },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_B, B },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_X, X },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_Y, Y },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_BACK, Back },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_GUIDE, Guide },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_START, Start },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSTICK, LeftStick },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSTICK, RightStick },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_LEFTSHOULDER, LeftShoulder },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER, RightShoulder },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_UP, DpadUp },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_DOWN, DpadDown },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_LEFT, DpadLeft },
|
|
|
|
|
{ SDL.SDL_GameControllerButton.SDL_CONTROLLER_BUTTON_DPAD_RIGHT, DpadRight }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EnumToAxis = new Dictionary<SDL.SDL_GameControllerAxis, Axis>
|
|
|
|
|
{
|
|
|
|
|
{ SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTX, LeftX },
|
|
|
|
|
{ SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_LEFTY, LeftY },
|
|
|
|
|
{ SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTX, RightX },
|
|
|
|
|
{ SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_RIGHTY, RightY }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
EnumToTrigger = new Dictionary<SDL.SDL_GameControllerAxis, Trigger>
|
|
|
|
|
{
|
|
|
|
|
{ SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERLEFT, TriggerLeft },
|
|
|
|
|
{ SDL.SDL_GameControllerAxis.SDL_CONTROLLER_AXIS_TRIGGERRIGHT, TriggerRight }
|
|
|
|
|
};
|
2022-07-12 23:09:23 +00:00
|
|
|
|
|
|
|
|
|
AxisButtonCodeToAxisButton = new Dictionary<AxisButtonCode, AxisButton>
|
|
|
|
|
{
|
|
|
|
|
{ AxisButtonCode.LeftX_Left, LeftXLeft },
|
|
|
|
|
{ AxisButtonCode.LeftX_Right, LeftXRight },
|
|
|
|
|
{ AxisButtonCode.LeftY_Down, LeftYDown },
|
|
|
|
|
{ AxisButtonCode.LeftY_Up, LeftYUp },
|
|
|
|
|
{ AxisButtonCode.RightX_Left, RightXLeft },
|
|
|
|
|
{ AxisButtonCode.RightX_Right, RightXRight },
|
|
|
|
|
{ AxisButtonCode.RightY_Up, RightYUp },
|
|
|
|
|
{ AxisButtonCode.RightY_Down, RightYDown }
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-02 21:04:12 +00:00
|
|
|
|
TriggerCodeToTriggerButton = new Dictionary<TriggerCode, TriggerButton>
|
|
|
|
|
{
|
|
|
|
|
{ TriggerCode.Left, TriggerLeftButton },
|
|
|
|
|
{ TriggerCode.Right, TriggerRightButton }
|
|
|
|
|
};
|
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
VirtualButtons = new VirtualButton[]
|
|
|
|
|
{
|
|
|
|
|
A,
|
|
|
|
|
B,
|
|
|
|
|
X,
|
|
|
|
|
Y,
|
|
|
|
|
Back,
|
|
|
|
|
Guide,
|
|
|
|
|
Start,
|
|
|
|
|
LeftStick,
|
|
|
|
|
RightStick,
|
|
|
|
|
LeftShoulder,
|
|
|
|
|
RightShoulder,
|
|
|
|
|
DpadUp,
|
|
|
|
|
DpadDown,
|
|
|
|
|
DpadLeft,
|
|
|
|
|
DpadRight,
|
|
|
|
|
LeftXLeft,
|
|
|
|
|
LeftXRight,
|
|
|
|
|
LeftYUp,
|
|
|
|
|
LeftYDown,
|
|
|
|
|
RightXLeft,
|
|
|
|
|
RightXRight,
|
|
|
|
|
RightYUp,
|
|
|
|
|
RightYDown,
|
|
|
|
|
TriggerLeftButton,
|
|
|
|
|
TriggerRightButton
|
|
|
|
|
};
|
2022-03-18 18:46:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-05 23:52:36 +00:00
|
|
|
|
public void Register(IntPtr handle)
|
|
|
|
|
{
|
|
|
|
|
Handle = handle;
|
|
|
|
|
|
|
|
|
|
IntPtr joystickHandle = SDL.SDL_GameControllerGetJoystick(Handle);
|
|
|
|
|
JoystickInstanceID = SDL.SDL_JoystickInstanceID(joystickHandle);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Unregister()
|
|
|
|
|
{
|
|
|
|
|
Handle = IntPtr.Zero;
|
|
|
|
|
JoystickInstanceID = -1;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 18:46:44 +00:00
|
|
|
|
internal void Update()
|
|
|
|
|
{
|
2022-07-08 23:47:12 +00:00
|
|
|
|
AnyPressed = false;
|
|
|
|
|
|
2022-03-18 19:11:00 +00:00
|
|
|
|
if (!IsDummy)
|
2022-03-18 18:46:44 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
foreach (var button in EnumToButton.Values)
|
2022-03-18 19:11:00 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
button.Update();
|
|
|
|
|
}
|
2022-07-08 23:47:12 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
foreach (var axis in EnumToAxis.Values)
|
|
|
|
|
{
|
|
|
|
|
axis.Update();
|
2022-03-18 19:11:00 +00:00
|
|
|
|
}
|
2022-03-18 18:46:44 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
foreach (var trigger in EnumToTrigger.Values)
|
2022-03-18 19:11:00 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
trigger.Update();
|
2022-03-18 19:11:00 +00:00
|
|
|
|
}
|
2022-03-18 18:46:44 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
LeftXLeft.Update();
|
|
|
|
|
LeftXRight.Update();
|
|
|
|
|
LeftYUp.Update();
|
|
|
|
|
LeftYDown.Update();
|
|
|
|
|
RightXLeft.Update();
|
|
|
|
|
RightXRight.Update();
|
|
|
|
|
RightYUp.Update();
|
|
|
|
|
RightYDown.Update();
|
|
|
|
|
|
|
|
|
|
TriggerLeftButton.Update();
|
|
|
|
|
TriggerRightButton.Update();
|
|
|
|
|
|
|
|
|
|
foreach (var button in VirtualButtons)
|
2022-03-18 19:11:00 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
if (button.IsPressed)
|
|
|
|
|
{
|
|
|
|
|
AnyPressed = true;
|
|
|
|
|
AnyPressedButton = button;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-03-18 19:11:00 +00:00
|
|
|
|
}
|
2022-03-18 18:46:44 +00:00
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-03-18 18:58:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Sets vibration values on the left and right motors.
|
|
|
|
|
/// </summary>
|
2022-02-23 05:14:32 +00:00
|
|
|
|
public bool SetVibration(float leftMotor, float rightMotor, uint durationInMilliseconds)
|
|
|
|
|
{
|
|
|
|
|
return SDL.SDL_GameControllerRumble(
|
|
|
|
|
Handle,
|
|
|
|
|
(ushort) (MathHelper.Clamp(leftMotor, 0f, 1f) * 0xFFFF),
|
|
|
|
|
(ushort) (MathHelper.Clamp(rightMotor, 0f, 1f) * 0xFFFF),
|
|
|
|
|
durationInMilliseconds
|
|
|
|
|
) == 0;
|
|
|
|
|
}
|
2021-03-10 05:48:31 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public GamepadButton Button(GamepadButtonCode buttonCode)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
return EnumToButton[(SDL.SDL_GameControllerButton) buttonCode];
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public AxisButton Button(AxisButtonCode axisButtonCode)
|
2022-02-23 05:14:32 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
return AxisButtonCodeToAxisButton[axisButtonCode];
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
|
2022-07-12 23:09:23 +00:00
|
|
|
|
public TriggerButton Button(TriggerCode triggerCode)
|
2022-03-18 18:46:44 +00:00
|
|
|
|
{
|
2022-07-12 23:09:23 +00:00
|
|
|
|
return TriggerCodeToTriggerButton[triggerCode];
|
2022-03-18 18:46:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 18:58:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Obtains the axis value given an AxisCode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A value between -1 and 1.</returns>
|
2022-03-18 18:46:44 +00:00
|
|
|
|
public float AxisValue(AxisCode axisCode)
|
|
|
|
|
{
|
|
|
|
|
return EnumToAxis[(SDL.SDL_GameControllerAxis) axisCode].Value;
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-18 18:58:10 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Obtains the trigger value given an TriggerCode.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A value between 0 and 1.</returns>
|
2022-03-18 18:46:44 +00:00
|
|
|
|
public float TriggerValue(TriggerCode triggerCode)
|
|
|
|
|
{
|
|
|
|
|
return EnumToTrigger[(SDL.SDL_GameControllerAxis) triggerCode].Value;
|
|
|
|
|
}
|
2022-02-23 05:14:32 +00:00
|
|
|
|
}
|
2021-01-19 07:29:07 +00:00
|
|
|
|
}
|