XInputControllerWindows.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_WSA
  2. using System.Runtime.InteropServices;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.LowLevel;
  5. using UnityEngine.InputSystem.XInput.LowLevel;
  6. using UnityEngine.InputSystem.Utilities;
  7. using UnityEngine.Scripting;
  8. namespace UnityEngine.InputSystem.XInput.LowLevel
  9. {
  10. // IMPORTANT: State layout is XINPUT_GAMEPAD
  11. [StructLayout(LayoutKind.Explicit, Size = 4)]
  12. internal struct XInputControllerWindowsState : IInputStateTypeInfo
  13. {
  14. public FourCC format => new FourCC('X', 'I', 'N', 'P');
  15. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1027:MarkEnumsWithFlags", Justification = "False positive")]
  16. public enum Button
  17. {
  18. DPadUp = 0,
  19. DPadDown = 1,
  20. DPadLeft = 2,
  21. DPadRight = 3,
  22. Start = 4,
  23. Select = 5,
  24. LeftThumbstickPress = 6,
  25. RightThumbstickPress = 7,
  26. LeftShoulder = 8,
  27. RightShoulder = 9,
  28. A = 12,
  29. B = 13,
  30. X = 14,
  31. Y = 15,
  32. }
  33. [InputControl(name = "dpad", layout = "Dpad", sizeInBits = 4, bit = 0)]
  34. [InputControl(name = "dpad/up", bit = (uint)Button.DPadUp)]
  35. [InputControl(name = "dpad/down", bit = (uint)Button.DPadDown)]
  36. [InputControl(name = "dpad/left", bit = (uint)Button.DPadLeft)]
  37. [InputControl(name = "dpad/right", bit = (uint)Button.DPadRight)]
  38. [InputControl(name = "start", bit = (uint)Button.Start, displayName = "Start")]
  39. [InputControl(name = "select", bit = (uint)Button.Select, displayName = "Select")]
  40. [InputControl(name = "leftStickPress", bit = (uint)Button.LeftThumbstickPress)]
  41. [InputControl(name = "rightStickPress", bit = (uint)Button.RightThumbstickPress)]
  42. [InputControl(name = "leftShoulder", bit = (uint)Button.LeftShoulder)]
  43. [InputControl(name = "rightShoulder", bit = (uint)Button.RightShoulder)]
  44. [InputControl(name = "buttonSouth", bit = (uint)Button.A, displayName = "A")]
  45. [InputControl(name = "buttonEast", bit = (uint)Button.B, displayName = "B")]
  46. [InputControl(name = "buttonWest", bit = (uint)Button.X, displayName = "X")]
  47. [InputControl(name = "buttonNorth", bit = (uint)Button.Y, displayName = "Y")]
  48. [FieldOffset(0)]
  49. public ushort buttons;
  50. [InputControl(name = "leftTrigger", format = "BYTE")]
  51. [FieldOffset(2)] public byte leftTrigger;
  52. [InputControl(name = "rightTrigger", format = "BYTE")]
  53. [FieldOffset(3)] public byte rightTrigger;
  54. [InputControl(name = "leftStick", layout = "Stick", format = "VC2S")]
  55. [InputControl(name = "leftStick/x", offset = 0, format = "SHRT", parameters = "clamp=false,invert=false,normalize=false")]
  56. [InputControl(name = "leftStick/left", offset = 0, format = "SHRT")]
  57. [InputControl(name = "leftStick/right", offset = 0, format = "SHRT")]
  58. [InputControl(name = "leftStick/y", offset = 2, format = "SHRT", parameters = "clamp=false,invert=false,normalize=false")]
  59. [InputControl(name = "leftStick/up", offset = 2, format = "SHRT")]
  60. [InputControl(name = "leftStick/down", offset = 2, format = "SHRT")]
  61. [FieldOffset(4)] public short leftStickX;
  62. [FieldOffset(6)] public short leftStickY;
  63. [InputControl(name = "rightStick", layout = "Stick", format = "VC2S")]
  64. [InputControl(name = "rightStick/x", offset = 0, format = "SHRT", parameters = "clamp=false,invert=false,normalize=false")]
  65. [InputControl(name = "rightStick/left", offset = 0, format = "SHRT")]
  66. [InputControl(name = "rightStick/right", offset = 0, format = "SHRT")]
  67. [InputControl(name = "rightStick/y", offset = 2, format = "SHRT", parameters = "clamp=false,invert=false,normalize=false")]
  68. [InputControl(name = "rightStick/up", offset = 2, format = "SHRT")]
  69. [InputControl(name = "rightStick/down", offset = 2, format = "SHRT")]
  70. [FieldOffset(8)] public short rightStickX;
  71. [FieldOffset(10)] public short rightStickY;
  72. public XInputControllerWindowsState WithButton(Button button)
  73. {
  74. buttons |= (ushort)((uint)1 << (int)button);
  75. return this;
  76. }
  77. }
  78. }
  79. namespace UnityEngine.InputSystem.XInput
  80. {
  81. /// <summary>
  82. /// An <see cref="XInputController"/> compatible game controller connected to a Windows desktop machine.
  83. /// </summary>
  84. [InputControlLayout(stateType = typeof(XInputControllerWindowsState), hideInUI = true)]
  85. [Preserve]
  86. public class XInputControllerWindows : XInputController
  87. {
  88. }
  89. }
  90. #endif // UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN || UNITY_WSA