using System; using UnityEngine.InputSystem.Controls; using UnityEngine.InputSystem.Layouts; ////TODO: expose user index ////TODO: set displayNames of the controls according to Xbox controller standards namespace UnityEngine.InputSystem.XInput { /// /// An XInput-compatible game controller. /// /// /// Note that on non-Microsoft platforms, XInput controllers will not actually use the XInput interface /// but will rather be interfaced with through different APIs -- on OSX, for example, HID is used to /// interface with Xbox controlllers. In those cases, XInput-specific functionality (like ) /// will not be available. /// /// On Windows, XInput controllers will be reported with /// set to "XInput" and with a JSON representation of XINPUT_CAPABILITIES /// available in . This means that you match on those /// subType and/or flags for example. /// /// /// /// // Create an XInput-specific guitar layout subtype. /// // NOTE: Works only on Windows. /// InputSystem.RegisterLayout(@" /// { /// ""name"" : ""XInputGuitar"", /// ""displayName"" : ""Guitar"", /// ""extend"" : ""XInputController"", /// ""device"" : { /// ""interface"" : ""XInput"", /// ""capabilities"" : [ /// { ""path"" : ""subType"", ""value"" : ""6"" } /// ] /// } /// } /// "); /// /// /// /// Now, when an XInput controller is connected and reports itself with the /// subtype "Guitar", it is turned into an "XInputGuitar" instead of an /// "XInputController". /// [InputControlLayout(displayName = "Xbox Controller")] public class XInputController : Gamepad { /// /// Same as . /// /// Same control as . // Change the display names for the buttons to conform to Xbox conventions. [InputControl(name = "buttonSouth", displayName = "A")] [InputControl(name = "buttonEast", displayName = "B")] [InputControl(name = "buttonWest", displayName = "X")] [InputControl(name = "buttonNorth", displayName = "Y")] [InputControl(name = "leftShoulder", displayName = "Left Bumper", shortDisplayName = "LB")] [InputControl(name = "rightShoulder", displayName = "Right Bumper", shortDisplayName = "RB")] [InputControl(name = "leftTrigger", shortDisplayName = "LT")] [InputControl(name = "rightTrigger", shortDisplayName = "RT")] // This follows Xbox One conventions; on Xbox 360, this is start=start and select=back. [InputControl(name = "start", displayName = "Menu", alias = "menu")] [InputControl(name = "select", displayName = "View", alias = "view")] public ButtonControl menu { get; protected set; } /// /// Same as /// /// Same control as . public ButtonControl view { get; protected set; } /// /// What specific kind of XInput controller this is. /// /// XInput device subtype. /// /// When the controller is picked up through interfaces other than XInput or through old versions of /// XInput, this will always be . Put another way, this value is /// meaningful only on recent Microsoft platforms. /// /// public DeviceSubType subType { get { if (!m_HaveParsedCapabilities) ParseCapabilities(); return m_SubType; } } /// /// Return the device flags as reported by XInput. /// /// XInput device flags. /// public DeviceFlags flags { get { if (!m_HaveParsedCapabilities) ParseCapabilities(); return m_Flags; } } /// protected override void FinishSetup() { base.FinishSetup(); menu = startButton; view = selectButton; } private bool m_HaveParsedCapabilities; private DeviceSubType m_SubType; private DeviceFlags m_Flags; private void ParseCapabilities() { if (!string.IsNullOrEmpty(description.capabilities)) { var capabilities = JsonUtility.FromJson(description.capabilities); m_SubType = capabilities.subType; m_Flags = capabilities.flags; } m_HaveParsedCapabilities = true; } /// /// Controller type enumeration in Type field of XINPUT_CAPABILITIES. /// /// /// See MSDN. /// internal enum DeviceType { Gamepad = 0x00 } /// /// Controller subtype enumeration in SubType field of XINPUT_CAPABILITIES. /// /// /// Provides additional detail about the underlying hardware being used and how it maps physical to logical /// controls. /// /// See MSDN /// for additional details. /// public enum DeviceSubType { /// /// The controller type is unknown. /// Unknown = 0x00, /// /// Gamepad controller. /// /// /// Includes left and right stick as and , /// left and right trigger as and , /// directional pad as , /// and all standard buttons (, , /// , , /// , , /// , , /// , ). /// Gamepad = 0x01, /// /// Racing wheel controller. /// /// /// x-axis reports the wheel rotation, /// is the acceleration pedal, and /// Left Trigger is the brake pedal. /// Includes Directional Pad as and most standard buttons /// (, , /// , , /// , , /// , ). /// and are optional. /// Wheel = 0x02, /// /// Arcade stick controller. /// /// /// Includes a Digital Stick that reports as a (up, down, left, right), /// and most standard buttons (, , /// , , /// , ). /// The and are implemented as digital /// buttons and report either 0.0f or 1.0f. /// The , and /// , are optional. /// ArcadeStick = 0x03, /// /// Flight stick controller. /// /// /// Includes a pitch and roll stick that reports as the , a POV Hat which /// reports as the , a rudder (handle twist or rocker) that reports as /// , and a throttle control as the . /// Includes support for a primary weapon (), secondary weapon /// (), and other standard buttons (, /// , , /// ). /// , and /// , are optional. /// FlightStick = 0x04, /// /// Dance pad controller. /// /// /// Includes the and standard buttons (, /// , , /// ) on the pad, plus and /// . /// DancePad = 0x05, /// /// Guitar controller. /// /// /// The strum bar maps to (up and down), and the frets are assigned to /// (green), (red), /// (yellow), (blue), and /// (orange). /// y-axis is associated with a vertical orientation sensor; /// x-axis is the whammy bar. /// Includes support for , , /// (left, right). /// (pickup selector), , /// , (fret modifier), /// are optional. /// Guitar = 0x06, /// /// Alternate guitar controller. /// /// /// Similar to but supports a larger range of movement for the vertical orientation /// sensor. /// GuitarAlternate = 0x07, /// /// Drum kit controller. /// /// /// The drum pads are assigned to buttons: for green (Floor Tom), /// for red (Snare Drum), /// for blue (Low Tom), /// for yellow (High Tom), /// and for the pedal (Bass Drum). /// Includes , , and /// . , /// , and are optional. /// DrumKit = 0x08, /// /// Bass guitar controller. /// /// /// Identical to , with the distinct subtype to simplify setup. /// GuitarBass = 0x0B, /// /// Arcade pad controller. /// /// /// Includes Directional Pad and most standard buttons /// (, , /// , , /// , , /// , ). /// The , are implemented as digital /// buttons and report either 0.0f or 1.0f. /// , , /// and are optional. /// ArcadePad = 0x13 } /// /// Controller flags in Flags field of XINPUT_CAPABILITIES. /// /// /// See MSDN. /// [Flags] public new enum DeviceFlags { ForceFeedbackSupported = 0x01, Wireless = 0x02, VoiceSupported = 0x04, PluginModulesSupported = 0x08, NoNavigation = 0x10, } [Serializable] internal struct Capabilities { public DeviceType type; public DeviceSubType subType; public DeviceFlags flags; } } }