Class XBoxController

java.lang.Object
edu.cornell.gdiac.util.XBoxController
All Implemented Interfaces:
com.badlogic.gdx.controllers.Controller, com.badlogic.gdx.controllers.ControllerListener

public class XBoxController extends Object implements com.badlogic.gdx.controllers.Controller, com.badlogic.gdx.controllers.ControllerListener
Class to support an XBox (360/One) controller

This is a wrapper class, which wraps around a Controller object to provide concrete mappings for the buttons, joysticks and triggers. It is simpler than having to remember the exact device numbers (even as constants) for each button. It is particularly important because different operating systems have different mappings for the buttons.

Each controller must have its own instance. The constructor automatically determines what OS this controller is running on before assigning the mappings. The constructor DOES NOT verify that the controller is indeed an XBox 360 controller.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    This enumerations supports D-pad directional controls.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    com.badlogic.gdx.controllers.Controller
    Reference to base controller object wrapped by this instance.

    Fields inherited from interface com.badlogic.gdx.controllers.Controller

    PLAYER_IDX_UNSET
  • Constructor Summary

    Constructors
    Constructor
    Description
    XBoxController(com.badlogic.gdx.controllers.Controller controller)
    Creates a new XBox interface for the given controller.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addListener(com.badlogic.gdx.controllers.ControllerListener listener)
    Adds a new ControllerListener to this Controller.
    boolean
    axisMoved(com.badlogic.gdx.controllers.Controller controller, int axisCode, float value)
    An axis on the Controller moved.
    boolean
    buttonDown(com.badlogic.gdx.controllers.Controller controller, int buttonCode)
    A button on the Controller was pressed.
    boolean
    buttonUp(com.badlogic.gdx.controllers.Controller controller, int buttonCode)
    A button on the Controller was released.
    void
    Cancels any running vibration.
    boolean
    Returns whether the controller can rumble.
    void
    connected(com.badlogic.gdx.controllers.Controller controller)
    A Controller got connected.
    void
    disconnected(com.badlogic.gdx.controllers.Controller controller)
    A Controller got disconnected.
    boolean
    Returns true if the A button is currently pressed.
    float
    getAxis(int axisCode)
    Returns the value of the axis, between -1 and 1
    int
    Returns the number of axes of this controller.
    boolean
    Returns true if the B button is currently pressed.
    boolean
    Returns true if the back button is currently pressed.
    boolean
    getButton(int buttonCode)
    Returns whether the button is pressed.
    float
    Returns the deadzone value for the joysticks.
    Returns the current direction of the DPad
    boolean
    Returns true if the DPad Down button currently pressed.
    boolean
    Returns true if the DPad Left button currently pressed.
    boolean
    Returns true if the DPad Right button currently pressed.
    boolean
    Returns true if the DPad Up button currently pressed.
    boolean
    Returns true if the left bumper is currently pressed.
    float
    Returns the value of the left trigger.
    float
    Returns the X axis value of the left analog stick.
    float
    Returns the Y axis value of the left analog stick.
    boolean
    Returns true if the left analog stick is currently pressed.
    com.badlogic.gdx.controllers.ControllerMapping
    Returns button and axis mapping for this controller (or platform).
    int
    Returns the maximum button index code that can be queried
    int
    Returns the minimum button index code that can be queried
    Returns the device name
    int
    Returns 0-based player index of this controller
    com.badlogic.gdx.controllers.ControllerPowerLevel
    Returns the value of enum ControllerPowerLevel
    boolean
    Returns true if the right bumper is currently pressed.
    float
    Returns the value of the right trigger.
    float
    Returns the X axis value of the right analog stick.
    float
    Returns the Y axis value of the right analog stick.
    boolean
    Returns true if the right analog stick is currently pressed.
    boolean
    Returns true if the start button is currently pressed.
    Returns the unique ID for this controller.
    boolean
    Returns true if the X button is currently pressed.
    boolean
    Returns true if the Y button is currently pressed.
    boolean
    Returns true if this Controller is still connected
    boolean
    Returns true if the controller is currently rumbling
    void
    removeListener(com.badlogic.gdx.controllers.ControllerListener listener)
    Removes the given ControllerListener
    void
    setDeadZone(float tol)
    Sets the deadzone value for the joysticks.
    void
    setPlayerIndex(int index)
    Sets the player index of this controller.
    void
    startVibration(int duration, float strength)
    Starts vibrating this controller, if possible.
    boolean
    Returns whether the controller can return and set the player index

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • controller

      public com.badlogic.gdx.controllers.Controller controller
      Reference to base controller object wrapped by this instance.
  • Constructor Details

    • XBoxController

      public XBoxController(com.badlogic.gdx.controllers.Controller controller)
      Creates a new XBox interface for the given controller.

      An XBox controller must have an OS supported mapping for its buttons.

      Parameters:
      controller - The controller to wrap
      Throws:
      IllegalStateException - if the controller does not have a supported mapping
  • Method Details

    • getDeadZone

      public float getDeadZone()
      Returns the deadzone value for the joysticks.

      Due to joystick sensitivity, we allow some minor dead zone settings. Any joystick value within this distance of 0 will register as 0.

      Returns:
      the deadzone value for the joysticks.
    • setDeadZone

      public void setDeadZone(float tol)
      Sets the deadzone value for the joysticks.

      Due to joystick sensitivity, we allow some minor dead zone settings. Any joystick value within this distance of 0 will register as 0.

      Parameters:
      tol - The deadzone value for the joysticks.
    • getStart

      public boolean getStart()
      Returns true if the start button is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the start button is currently pressed
    • getBack

      public boolean getBack()
      Returns true if the back button is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the back button is currently pressed
    • getX

      public boolean getX()
      Returns true if the X button is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the X button is currently pressed
    • getY

      public boolean getY()
      Returns true if the Y button is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the Y button is currently pressed
    • getA

      public boolean getA()
      Returns true if the A button is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the A button is currently pressed
    • getB

      public boolean getB()
      Returns true if the B button is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the Y button is currently pressed
    • getLBumper

      public boolean getLBumper()
      Returns true if the left bumper is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the left bumper is currently pressed
    • getLStick

      public boolean getLStick()
      Returns true if the left analog stick is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the left analog stick is currently pressed
    • getRBumper

      public boolean getRBumper()
      Returns true if the right bumper is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the right bumper is currently pressed
    • getRStick

      public boolean getRStick()
      Returns true if the right analog stick is currently pressed.

      This method returns false if the controller is disconnected.

      Returns:
      true if the right analog stick is currently pressed
    • getDPadUp

      public boolean getDPadUp()
      Returns true if the DPad Up button currently pressed.

      This is method only returns true if the up button is pressed by itself. If the up button is combined with left or right, it will return false. For more flexible usage of the DPad, you should use the method getDPadDirection().

      This method returns false if the controller is disconnected.

      Returns:
      true if the DPad Up button is currently pressed
    • getDPadDown

      public boolean getDPadDown()
      Returns true if the DPad Down button currently pressed.

      This is method only returns true if the up button is pressed by itself. If the up button is combined with left or right, it will return false. For more flexible usage of the DPad, you should use the method getDPadDirection().

      This method returns false if the controller is disconnected.

      Returns:
      true if the DPad Down button is currently pressed
    • getDPadLeft

      public boolean getDPadLeft()
      Returns true if the DPad Left button currently pressed.

      This is method only returns true if the up button is pressed by itself. If the up button is combined with left or right, it will return false. For more flexible usage of the DPad, you should use the method getDPadDirection().

      This method returns false if the controller is disconnected.

      Returns:
      true if the DPad Left button is currently pressed
    • getDPadRight

      public boolean getDPadRight()
      Returns true if the DPad Right button currently pressed.

      This is method only returns true if the up button is pressed by itself. If the up button is combined with left or right, it will return false. For more flexible usage of the DPad, you should use the method getDPadDirection().

      This method returns false if the controller is disconnected.

      Returns:
      true if the DPad Right button is currently pressed
    • getDPadDirection

      public XBoxController.Direction getDPadDirection()
      Returns the current direction of the DPad

      The result will be one of the eight cardinal directions, or center if the DPad is not actively pressed.

      This method returns XBoxController.Direction.NEUTRAL if the controller is disconnected.

      Returns:
      the current direction of the DPad
    • getLeftX

      public float getLeftX()
      Returns the X axis value of the left analog stick.

      This is a value between -1 and 1, where -1 is to the left.

      This method returns 0 if the controller is disconnected.

      Returns:
      the X axis value of the left analog stick.
    • getLeftY

      public float getLeftY()
      Returns the Y axis value of the left analog stick.

      This is a value between -1 and 1, where -1 is towards the bottom.

      This method returns 0 if the controller is disconnected.

      Returns:
      the Y axis value of the left analog stick.
    • getLeftTrigger

      public float getLeftTrigger()
      Returns the value of the left trigger.

      This is a value between 0 and 1, where 0 is no pressure.

      This method returns 0 if the controller is disconnected.

      Returns:
      the value of the left trigger.
    • getRightX

      public float getRightX()
      Returns the X axis value of the right analog stick.

      This is a value between -1 and 1, where -1 is to the left.

      This method returns 0 if the controller is disconnected.

      Returns:
      the X axis value of the right analog stick.
    • getRightY

      public float getRightY()
      Returns the Y axis value of the right analog stick.

      This is a value between -1 and 1, where -1 is towards the bottom.

      This method returns 0 if the controller is disconnected.

      Returns:
      the Y axis value of the right analog stick.
    • getRightTrigger

      public float getRightTrigger()
      Returns the value of the right trigger.

      This is a value between 0 and 1, where 0 is no pressure.

      This method returns 0 if the controller is disconnected.

      Returns:
      the value of the right trigger.
    • getButton

      public boolean getButton(int buttonCode)
      Returns whether the button is pressed.

      This method returns false if the controller is disconnected.

      Specified by:
      getButton in interface com.badlogic.gdx.controllers.Controller
      Parameters:
      buttonCode - The button code
      Returns:
      whether the button is pressed.
    • getAxis

      public float getAxis(int axisCode)
      Returns the value of the axis, between -1 and 1

      This method returns 0 if the controller is disconnected.

      Specified by:
      getAxis in interface com.badlogic.gdx.controllers.Controller
      Parameters:
      axisCode - The axis code
      Returns:
      the value of the axis, between -1 and 1
    • getName

      public String getName()
      Returns the device name

      This method returns null if the controller is disconnected.

      Specified by:
      getName in interface com.badlogic.gdx.controllers.Controller
      Returns:
      the device name
    • getUniqueId

      public String getUniqueId()
      Returns the unique ID for this controller.

      This ID is used to recognize this controller if more than one of the same controller models are connected. Use this to map a controller to a player, but do not use it to save a button mapping.

      This method returns null if the controller is disconnected.

      Specified by:
      getUniqueId in interface com.badlogic.gdx.controllers.Controller
      Returns:
      the unique ID for this controller.
    • getMinButtonIndex

      public int getMinButtonIndex()
      Returns the minimum button index code that can be queried

      This method returns 0 if the controller is disconnected.

      Specified by:
      getMinButtonIndex in interface com.badlogic.gdx.controllers.Controller
      Returns:
      the minimum button index code that can be queried
    • getMaxButtonIndex

      public int getMaxButtonIndex()
      Returns the maximum button index code that can be queried

      This method returns 0 if the controller is disconnected.

      Specified by:
      getMaxButtonIndex in interface com.badlogic.gdx.controllers.Controller
      Returns:
      the maximum button index code that can be queried
    • getAxisCount

      public int getAxisCount()
      Returns the number of axes of this controller.

      Axis indices start at 0, so the maximum axis index is one under this value. On XBoxes, the high end axes are the triggers.

      This method returns 0 if the controller is not connected.

      Specified by:
      getAxisCount in interface com.badlogic.gdx.controllers.Controller
      Returns:
      the number of axes of this controller.
    • isConnected

      public boolean isConnected()
      Returns true if this Controller is still connected

      This method returns false if the controller is not connected.

      Specified by:
      isConnected in interface com.badlogic.gdx.controllers.Controller
      Returns:
      true if this Controller is still connected
    • canVibrate

      public boolean canVibrate()
      Returns whether the controller can rumble.

      Note that this is no guarantee that the connected controller itself can vibrate. Simply that the system believes that it can.

      This method returns false if the controller is not connected.

      Specified by:
      canVibrate in interface com.badlogic.gdx.controllers.Controller
      Returns:
      whether the controller can rumble.
    • isVibrating

      public boolean isVibrating()
      Returns true if the controller is currently rumbling
      Specified by:
      isVibrating in interface com.badlogic.gdx.controllers.Controller
      Returns:
      true if the controller is currently rumbling
    • startVibration

      public void startVibration(int duration, float strength)
      Starts vibrating this controller, if possible.

      This method does nothing if the controller is not connected.

      Specified by:
      startVibration in interface com.badlogic.gdx.controllers.Controller
      Parameters:
      duration - duration, in milliseconds
      strength - value between 0f and 1f
    • cancelVibration

      public void cancelVibration()
      Cancels any running vibration.

      This method is not be supported by some implementations. This method does nothing if the controller is not connected.

      Specified by:
      cancelVibration in interface com.badlogic.gdx.controllers.Controller
    • supportsPlayerIndex

      public boolean supportsPlayerIndex()
      Returns whether the controller can return and set the player index

      This method returns false if the controller is not connected.

      Specified by:
      supportsPlayerIndex in interface com.badlogic.gdx.controllers.Controller
      Returns:
      whether the controller can return and set the player index
    • getPlayerIndex

      public int getPlayerIndex()
      Returns 0-based player index of this controller

      This returns Controller.PLAYER_IDX_UNSET if none is set, or if the controller is disconnected.

      Specified by:
      getPlayerIndex in interface com.badlogic.gdx.controllers.Controller
      Returns:
      0-based player index of this controller
    • setPlayerIndex

      public void setPlayerIndex(int index)
      Sets the player index of this controller.

      Please note that this does not always set indication lights of controllers. It is just an internal representation on some platforms. This value is typically 0 to 3 on most systems. Set this value to Controller.PLAYER_IDX_UNSET to unset the index.

      This method does nothing if the controller is disconnected.

      Specified by:
      setPlayerIndex in interface com.badlogic.gdx.controllers.Controller
      Parameters:
      index - the player index
    • getMapping

      public com.badlogic.gdx.controllers.ControllerMapping getMapping()
      Returns button and axis mapping for this controller (or platform).

      The connected controller might not support all features. This method returns null if the controller is not connected.

      Specified by:
      getMapping in interface com.badlogic.gdx.controllers.Controller
      Returns:
      button and axis mapping for this controller (or platform).
    • getPowerLevel

      public com.badlogic.gdx.controllers.ControllerPowerLevel getPowerLevel()
      Returns the value of enum ControllerPowerLevel

      This value indicates the battery state of the connected controller, or ControllerPowerLevel.POWER_UNKNOWN if information is not present (including when the controller is disconnected)

      Specified by:
      getPowerLevel in interface com.badlogic.gdx.controllers.Controller
      Returns:
      the value of enum ControllerPowerLevel
    • addListener

      public void addListener(com.badlogic.gdx.controllers.ControllerListener listener)
      Adds a new ControllerListener to this Controller.

      The listener will receive calls in case the state of the controller changes. The listener will be invoked on the rendering thread.

      This method does nothing if the controller is disconnected.

      Specified by:
      addListener in interface com.badlogic.gdx.controllers.Controller
      Parameters:
      listener - The listener to add
    • removeListener

      public void removeListener(com.badlogic.gdx.controllers.ControllerListener listener)
      Removes the given ControllerListener

      This method does nothing if the controller is disconnected or the given listener is not registered.

      Specified by:
      removeListener in interface com.badlogic.gdx.controllers.Controller
      Parameters:
      listener - The listener to remove
    • connected

      public void connected(com.badlogic.gdx.controllers.Controller controller)
      A Controller got connected.
      Specified by:
      connected in interface com.badlogic.gdx.controllers.ControllerListener
      Parameters:
      controller - The controller interface
    • disconnected

      public void disconnected(com.badlogic.gdx.controllers.Controller controller)
      A Controller got disconnected.
      Specified by:
      disconnected in interface com.badlogic.gdx.controllers.ControllerListener
      Parameters:
      controller - The controller interface
    • buttonDown

      public boolean buttonDown(com.badlogic.gdx.controllers.Controller controller, int buttonCode)
      A button on the Controller was pressed.

      The buttonCode is controller specific. The package com.badlogic.gdx.controllers.mapping hosts button constants for known controllers.

      Specified by:
      buttonDown in interface com.badlogic.gdx.controllers.ControllerListener
      Parameters:
      controller - The controller interface
      buttonCode - The button pressed
      Returns:
      whether to hand the event to other listeners.
    • buttonUp

      public boolean buttonUp(com.badlogic.gdx.controllers.Controller controller, int buttonCode)
      A button on the Controller was released.

      The buttonCode is controller specific. The package com.badlogic.gdx.controllers.mapping hosts button constants for known controllers.

      Specified by:
      buttonUp in interface com.badlogic.gdx.controllers.ControllerListener
      Parameters:
      controller - The controller interface
      buttonCode - The button released
      Returns:
      whether to hand the event to other listeners.
    • axisMoved

      public boolean axisMoved(com.badlogic.gdx.controllers.Controller controller, int axisCode, float value)
      An axis on the Controller moved.

      The axisCode is controller specific. The axis value is in the range [-1, 1]. The package com.badlogic.gdx.controllers.mapping hosts axes constants for known controllers.

      Specified by:
      axisMoved in interface com.badlogic.gdx.controllers.ControllerListener
      Parameters:
      controller - The controller interface
      axisCode - The axis identifier
      value - The axis value, -1 to 1
      Returns:
      whether to hand the event to other listeners.