Class XBoxController
- All Implemented Interfaces:
com.badlogic.gdx.controllers.Controller,com.badlogic.gdx.controllers.ControllerListener
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 ClassesModifier and TypeClassDescriptionstatic enumThis enumerations supports D-pad directional controls. -
Field Summary
FieldsModifier and TypeFieldDescriptioncom.badlogic.gdx.controllers.ControllerReference to base controller object wrapped by this instance.Fields inherited from interface com.badlogic.gdx.controllers.Controller
PLAYER_IDX_UNSET -
Constructor Summary
ConstructorsConstructorDescriptionXBoxController(com.badlogic.gdx.controllers.Controller controller) Creates a new XBox interface for the given controller. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddListener(com.badlogic.gdx.controllers.ControllerListener listener) Adds a newControllerListenerto thisController.booleanaxisMoved(com.badlogic.gdx.controllers.Controller controller, int axisCode, float value) An axis on the Controller moved.booleanbuttonDown(com.badlogic.gdx.controllers.Controller controller, int buttonCode) A button on the Controller was pressed.booleanbuttonUp(com.badlogic.gdx.controllers.Controller controller, int buttonCode) A button on the Controller was released.voidCancels any running vibration.booleanReturns whether the controller can rumble.voidconnected(com.badlogic.gdx.controllers.Controller controller) A Controller got connected.voiddisconnected(com.badlogic.gdx.controllers.Controller controller) A Controller got disconnected.booleangetA()Returns true if the A button is currently pressed.floatgetAxis(int axisCode) Returns the value of the axis, between -1 and 1intReturns the number of axes of this controller.booleangetB()Returns true if the B button is currently pressed.booleangetBack()Returns true if the back button is currently pressed.booleangetButton(int buttonCode) Returns whether the button is pressed.floatReturns the deadzone value for the joysticks.Returns the current direction of the DPadbooleanReturns true if the DPad Down button currently pressed.booleanReturns true if the DPad Left button currently pressed.booleanReturns true if the DPad Right button currently pressed.booleanReturns true if the DPad Up button currently pressed.booleanReturns true if the left bumper is currently pressed.floatReturns the value of the left trigger.floatgetLeftX()Returns the X axis value of the left analog stick.floatgetLeftY()Returns the Y axis value of the left analog stick.booleanReturns true if the left analog stick is currently pressed.com.badlogic.gdx.controllers.ControllerMappingReturns button and axis mapping for this controller (or platform).intReturns the maximum button index code that can be queriedintReturns the minimum button index code that can be queriedgetName()Returns the device nameintReturns 0-based player index of this controllercom.badlogic.gdx.controllers.ControllerPowerLevelReturns the value of enumControllerPowerLevelbooleanReturns true if the right bumper is currently pressed.floatReturns the value of the right trigger.floatReturns the X axis value of the right analog stick.floatReturns the Y axis value of the right analog stick.booleanReturns true if the right analog stick is currently pressed.booleangetStart()Returns true if the start button is currently pressed.Returns the unique ID for this controller.booleangetX()Returns true if the X button is currently pressed.booleangetY()Returns true if the Y button is currently pressed.booleanReturns true if this Controller is still connectedbooleanReturns true if the controller is currently rumblingvoidremoveListener(com.badlogic.gdx.controllers.ControllerListener listener) Removes the givenControllerListenervoidsetDeadZone(float tol) Sets the deadzone value for the joysticks.voidsetPlayerIndex(int index) Sets the player index of this controller.voidstartVibration(int duration, float strength) Starts vibrating this controller, if possible.booleanReturns whether the controller can return and set the player index
-
Field Details
-
controller
public com.badlogic.gdx.controllers.Controller controllerReference 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
Returns the current direction of the DPadThe result will be one of the eight cardinal directions, or center if the DPad is not actively pressed.
This method returns
XBoxController.Direction.NEUTRALif 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:
getButtonin interfacecom.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 1This method returns 0 if the controller is disconnected.
- Specified by:
getAxisin interfacecom.badlogic.gdx.controllers.Controller- Parameters:
axisCode- The axis code- Returns:
- the value of the axis, between -1 and 1
-
getName
Returns the device nameThis method returns null if the controller is disconnected.
- Specified by:
getNamein interfacecom.badlogic.gdx.controllers.Controller- Returns:
- the device name
-
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:
getUniqueIdin interfacecom.badlogic.gdx.controllers.Controller- Returns:
- the unique ID for this controller.
-
getMinButtonIndex
public int getMinButtonIndex()Returns the minimum button index code that can be queriedThis method returns 0 if the controller is disconnected.
- Specified by:
getMinButtonIndexin interfacecom.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 queriedThis method returns 0 if the controller is disconnected.
- Specified by:
getMaxButtonIndexin interfacecom.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:
getAxisCountin interfacecom.badlogic.gdx.controllers.Controller- Returns:
- the number of axes of this controller.
-
isConnected
public boolean isConnected()Returns true if this Controller is still connectedThis method returns false if the controller is not connected.
- Specified by:
isConnectedin interfacecom.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:
canVibratein interfacecom.badlogic.gdx.controllers.Controller- Returns:
- whether the controller can rumble.
-
isVibrating
public boolean isVibrating()Returns true if the controller is currently rumbling- Specified by:
isVibratingin interfacecom.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:
startVibrationin interfacecom.badlogic.gdx.controllers.Controller- Parameters:
duration- duration, in millisecondsstrength- 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:
cancelVibrationin interfacecom.badlogic.gdx.controllers.Controller
-
supportsPlayerIndex
public boolean supportsPlayerIndex()Returns whether the controller can return and set the player indexThis method returns false if the controller is not connected.
- Specified by:
supportsPlayerIndexin interfacecom.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 controllerThis returns
Controller.PLAYER_IDX_UNSETif none is set, or if the controller is disconnected.- Specified by:
getPlayerIndexin interfacecom.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_UNSETto unset the index.This method does nothing if the controller is disconnected.
- Specified by:
setPlayerIndexin interfacecom.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:
getMappingin interfacecom.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 enumControllerPowerLevelThis value indicates the battery state of the connected controller, or
ControllerPowerLevel.POWER_UNKNOWNif information is not present (including when the controller is disconnected)- Specified by:
getPowerLevelin interfacecom.badlogic.gdx.controllers.Controller- Returns:
- the value of enum
ControllerPowerLevel
-
addListener
public void addListener(com.badlogic.gdx.controllers.ControllerListener listener) Adds a newControllerListenerto thisController.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:
addListenerin interfacecom.badlogic.gdx.controllers.Controller- Parameters:
listener- The listener to add
-
removeListener
public void removeListener(com.badlogic.gdx.controllers.ControllerListener listener) Removes the givenControllerListenerThis method does nothing if the controller is disconnected or the given listener is not registered.
- Specified by:
removeListenerin interfacecom.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:
connectedin interfacecom.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:
disconnectedin interfacecom.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.mappinghosts button constants for known controllers.- Specified by:
buttonDownin interfacecom.badlogic.gdx.controllers.ControllerListener- Parameters:
controller- The controller interfacebuttonCode- 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.mappinghosts button constants for known controllers.- Specified by:
buttonUpin interfacecom.badlogic.gdx.controllers.ControllerListener- Parameters:
controller- The controller interfacebuttonCode- 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.mappinghosts axes constants for known controllers.- Specified by:
axisMovedin interfacecom.badlogic.gdx.controllers.ControllerListener- Parameters:
controller- The controller interfaceaxisCode- The axis identifiervalue- The axis value, -1 to 1- Returns:
- whether to hand the event to other listeners.
-