using UnityEngine; using CarbonInput; /// /// Represents the current state of a specific gamepad. /// The state of any button can be accessed by the attributes , ,... or via the method . /// returns true during the frame it was pressed. /// returns true during the frame it was released. /// , and will give you direct access to the corresponding axes. /// The trigger can be accessed by and . /// // ReSharper disable once CheckNamespace // ReSharper disable InconsistentNaming public class GamePadState { /// /// Any axis is being considered "pressed" if it's absolute value is greater than this threshold. /// private const float AxisPressedThreshold = 0.3f; #region Buttons /// /// Stores the state of all buttons from the last frame. /// private bool[] LastFrameButtons = new bool[CarbonController.ButtonCount]; /// /// Stores the state of all buttons from this frame. /// private bool[] Buttons = new bool[CarbonController.ButtonCount]; /// /// Is true while is pressed. /// public bool A { get { return Buttons[(int)CButton.A]; } } /// /// Is true while is pressed. /// public bool B { get { return Buttons[(int)CButton.B]; } } /// /// Is true while is pressed. /// public bool X { get { return Buttons[(int)CButton.X]; } } /// /// Is true while is pressed. /// public bool Y { get { return Buttons[(int)CButton.Y]; } } /// /// Is true while is pressed. /// public bool Back { get { return Buttons[(int)CButton.Back]; } } /// /// Is true while is pressed. /// public bool Start { get { return Buttons[(int)CButton.Start]; } } /// /// Is true while is pressed. /// public bool LB { get { return Buttons[(int)CButton.LB]; } } /// /// Is true while is pressed. /// public bool RB { get { return Buttons[(int)CButton.RB]; } } /// /// Is true while is pressed. /// public bool LS { get { return Buttons[(int)CButton.LS]; } } /// /// Is true while is pressed. /// public bool RS { get { return Buttons[(int)CButton.RS]; } } #endregion #region Axis /// /// Stores the state of all axis values from the last frame. /// private float[] LastAxis = new float[CarbonController.AxisCount]; /// /// Stores the state of all axis values from this frame. /// private float[] Axis = new float[CarbonController.AxisCount]; /// /// X and Y axis of the left thumbstick. /// public Vector2 Left { get { return new Vector2(Axis[(int)CAxis.LX], Axis[(int)CAxis.LY]); } } /// /// X and Y axis of the right thumbstick. /// public Vector2 Right { get { return new Vector2(Axis[(int)CAxis.RX], Axis[(int)CAxis.RY]); } } /// /// Left trigger. /// public float LT { get { return Axis[(int)CAxis.LT]; } } /// /// Right trigger. /// public float RT { get { return Axis[(int)CAxis.RT]; } } /// /// X and Y axis of the dpad. /// public Vector2 DPad { get { return new Vector2(Axis[(int)CAxis.DX], Axis[(int)CAxis.DY]); } } #endregion /// /// Defines the owner of this . /// private readonly PlayerIndex Index; /// /// Number of the last frame, used to determine if we're in a new frame or not. /// private int LastFrame; /// /// Returns true if the button state has changed since the last frame. /// /// /// public bool HasChanged(CButton btn) { return Buttons[(int)btn] != LastFrameButtons[(int)btn]; } /// /// Returns true while the button is pressed. /// /// /// public bool Button(CButton btn) { return Buttons[(int)btn]; } /// /// Returns true during the frame the user pressed the button. /// /// /// public bool Pressed(CButton btn) { return Buttons[(int)btn] && !LastFrameButtons[(int)btn]; } /// /// Returns true during the frame the user released the button. /// /// /// public bool Released(CButton btn) { return !Buttons[(int)btn] && LastFrameButtons[(int)btn]; } /// /// Returns true while the axis is "pressed", which is if the absolute value of this axis is greater than a certain threshold. /// /// /// public bool Button(CAxis axis) { return Mathf.Abs(Axis[(int)axis]) > AxisPressedThreshold; } /// /// Returns true during the frame the axis is "pressed", which is if the absolute value of this axis is greater than a certain threshold. /// /// /// public bool Pressed(CAxis axis) { bool pressedNow = Mathf.Abs(Axis[(int)axis]) > AxisPressedThreshold; bool pressedLastFrame = Mathf.Abs(LastAxis[(int)axis]) > AxisPressedThreshold; return pressedNow && !pressedLastFrame; } /// /// Returns true during the frame the axis is no longer "pressed", which is if the absolute value of this axis is greater than a certain threshold. /// /// /// public bool Released(CAxis axis) { bool pressedNow = Mathf.Abs(Axis[(int)axis]) > AxisPressedThreshold; bool pressedLastFrame = Mathf.Abs(LastAxis[(int)axis]) > AxisPressedThreshold; return !pressedNow && pressedLastFrame; } /// /// Returns the value of the given axis. /// /// /// public float GetAxis(CAxis axis) { return Axis[(int)axis]; } #region PSButton /// /// Returns true while the button is pressed. /// /// /// public bool Button(PSButton btn) { return Button((CButton)btn); } /// /// Returns true during the frame the user pressed the button. /// /// /// public bool Pressed(PSButton btn) { return Pressed((CButton)btn); } /// /// Returns true during the frame the user released the button. /// /// /// public bool Released(PSButton btn) { return Released((CButton)btn); } #endregion /// /// Returns true if any button is currently pressed. /// public bool AnyButton { get; private set; } /// /// Returns true if any axis is currently not zero. /// public bool AnyAxis { get; private set; } /// /// Returns true if any button is currently pressed or if any axis is currently not zero. /// public bool AnyButtonOrAxis { get { return AnyButton || AnyAxis; } } /// /// Returns a button that is currently pressed or null if no buttons are pressed. /// /// public CButton? GetAnyButton() { for(int i = 0; i < CarbonController.ButtonCount; i++) if(Buttons[i]) return (CButton)i; return null; } /// /// Returns an axis that is not zero or null if all axis are zero. /// /// public CAxis? GetAnyAxis() { for(int i = 0; i < CarbonController.AxisCount; i++) if(Mathf.Abs(Axis[i]) > AxisPressedThreshold) return (CAxis)i; return null; } public GamePadState(PlayerIndex id) { Index = id; } /// /// This will update all buttons and axes of this instance. /// Multiple calls in the same frame won't have any effect. /// public void Update() { if(LastFrame == Time.frameCount) return; LastFrame = Time.frameCount; SwapArrays(); AnyButton = false; for(int i = 0; i < Buttons.Length; i++) { AnyButton |= (Buttons[i] = GamePad.GetButton((CButton)i, Index)); } AnyAxis = false; for(int i = 0; i < Axis.Length; i++) { AnyAxis |= Mathf.Abs(Axis[i] = GamePad.GetAxis((CAxis)i, Index)) > AxisPressedThreshold; } } private void SwapArrays() { bool[] tmp = LastFrameButtons; LastFrameButtons = Buttons; Buttons = tmp; float[] axis = LastAxis; LastAxis = Axis; Axis = axis; } }