No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IOSGameController.cs 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #if UNITY_EDITOR || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS || PACKAGE_DOCS_GENERATION
  2. using System.Runtime.InteropServices;
  3. using UnityEngine.InputSystem.DualShock;
  4. using UnityEngine.InputSystem.Layouts;
  5. using UnityEngine.InputSystem.LowLevel;
  6. using UnityEngine.InputSystem.iOS.LowLevel;
  7. using UnityEngine.InputSystem.Utilities;
  8. namespace UnityEngine.InputSystem.iOS.LowLevel
  9. {
  10. internal enum iOSButton
  11. {
  12. DpadUp,
  13. DpadDown,
  14. DpadLeft,
  15. DpadRight,
  16. LeftStick,
  17. RightStick,
  18. LeftShoulder,
  19. RightShoulder,
  20. LeftTrigger,
  21. RightTrigger,
  22. X,
  23. Y,
  24. A,
  25. B,
  26. Start,
  27. Select
  28. // Note: If you'll add an element here, be sure to update kMaxButtons const below
  29. };
  30. internal enum iOSAxis
  31. {
  32. LeftStickX,
  33. LeftStickY,
  34. RightStickX,
  35. RightStickY
  36. // Note: If you'll add an element here, be sure to update kMaxAxis const below
  37. };
  38. [StructLayout(LayoutKind.Sequential)]
  39. internal unsafe struct iOSGameControllerState : IInputStateTypeInfo
  40. {
  41. public static FourCC kFormat = new FourCC('I', 'G', 'C', ' ');
  42. public const int MaxButtons = (int)iOSButton.Select + 1;
  43. public const int MaxAxis = (int)iOSAxis.RightStickY + 1;
  44. [InputControl(name = "dpad")]
  45. [InputControl(name = "dpad/up", bit = (uint)iOSButton.DpadUp)]
  46. [InputControl(name = "dpad/right", bit = (uint)iOSButton.DpadRight)]
  47. [InputControl(name = "dpad/down", bit = (uint)iOSButton.DpadDown)]
  48. [InputControl(name = "dpad/left", bit = (uint)iOSButton.DpadLeft)]
  49. [InputControl(name = "buttonSouth", bit = (uint)iOSButton.A)]
  50. [InputControl(name = "buttonWest", bit = (uint)iOSButton.X)]
  51. [InputControl(name = "buttonNorth", bit = (uint)iOSButton.Y)]
  52. [InputControl(name = "buttonEast", bit = (uint)iOSButton.B)]
  53. [InputControl(name = "leftStickPress", bit = (uint)iOSButton.LeftStick)]
  54. [InputControl(name = "rightStickPress", bit = (uint)iOSButton.RightStick)]
  55. [InputControl(name = "leftShoulder", bit = (uint)iOSButton.LeftShoulder)]
  56. [InputControl(name = "rightShoulder", bit = (uint)iOSButton.RightShoulder)]
  57. [InputControl(name = "start", bit = (uint)iOSButton.Start)]
  58. [InputControl(name = "select", bit = (uint)iOSButton.Select)]
  59. public uint buttons;
  60. [InputControl(name = "leftTrigger", offset = sizeof(uint) + sizeof(float) * (uint)iOSButton.LeftTrigger)]
  61. [InputControl(name = "rightTrigger", offset = sizeof(uint) + sizeof(float) * (uint)iOSButton.RightTrigger)]
  62. public fixed float buttonValues[MaxButtons];
  63. private const uint kAxisOffset = sizeof(uint) + sizeof(float) * MaxButtons;
  64. [InputControl(name = "leftStick", offset = (uint)iOSAxis.LeftStickX * sizeof(float) + kAxisOffset)]
  65. [InputControl(name = "rightStick", offset = (uint)iOSAxis.RightStickX * sizeof(float) + kAxisOffset)]
  66. public fixed float axisValues[MaxAxis];
  67. public FourCC format => kFormat;
  68. public iOSGameControllerState WithButton(iOSButton button, bool value = true, float rawValue = 1.0f)
  69. {
  70. buttonValues[(int)button] = rawValue;
  71. Debug.Assert((int)button < 32, $"Expected button < 32, so we fit into the 32 bit wide bitmask");
  72. var bit = 1U << (int)button;
  73. if (value)
  74. buttons |= bit;
  75. else
  76. buttons &= ~bit;
  77. return this;
  78. }
  79. public iOSGameControllerState WithAxis(iOSAxis axis, float value)
  80. {
  81. axisValues[(int)axis] = value;
  82. return this;
  83. }
  84. }
  85. }
  86. namespace UnityEngine.InputSystem.iOS
  87. {
  88. /// <summary>
  89. /// A generic Gamepad connected to an iOS device.
  90. /// </summary>
  91. /// <remarks>
  92. /// Any MFi-certified Gamepad which is not an <see cref="XboxOneGampadiOS"/> or <see cref="DualShock4GampadiOS"/> will
  93. /// be represented as an iOSGameController.
  94. /// </remarks>
  95. [InputControlLayout(stateType = typeof(iOSGameControllerState), displayName = "iOS Gamepad")]
  96. public class iOSGameController : Gamepad
  97. {
  98. }
  99. /// <summary>
  100. /// An Xbox One Bluetooth controller connected to an iOS device.
  101. /// </summary>
  102. [InputControlLayout(stateType = typeof(iOSGameControllerState), displayName = "iOS Xbox One Gamepad")]
  103. public class XboxOneGampadiOS : XInput.XInputController
  104. {
  105. }
  106. /// <summary>
  107. /// A PlayStation DualShock 4 controller connected to an iOS device.
  108. /// </summary>
  109. [InputControlLayout(stateType = typeof(iOSGameControllerState), displayName = "iOS DualShock 4 Gamepad")]
  110. public class DualShock4GampadiOS : DualShockGamepad
  111. {
  112. }
  113. /// <summary>
  114. /// A PlayStation DualSense controller connected to an iOS device.
  115. /// </summary>
  116. [InputControlLayout(stateType = typeof(iOSGameControllerState), displayName = "iOS DualSense Gamepad")]
  117. public class DualSenseGampadiOS : DualShockGamepad
  118. {
  119. }
  120. }
  121. #endif // UNITY_EDITOR || UNITY_IOS || UNITY_TVOS || UNITY_VISIONOS