Skip to content

Advanced Controller interface

Benjamin Schulte edited this page Jul 21, 2019 · 14 revisions

The AdvancedController interface gives you access to controller features that are not accessible by libGDX' core Controller interface - if the platform-dependant controller implementation you are using implements this interface. This way, you can query a button mapping for the controller, get game controllers to vibrate on desktop, but there are more features available.

Installation

Add the dependency to the core project:

    compile "de.golfgl.gdxcontrollerutils:gdx-controllers-advanced:$cuversion:sources"

And of course, you need to add the sources to your HTML project:

    compile "de.golfgl.gdxcontrollerutils:gdx-controllers-advanced:$cuversion:sources"

After doing so, you check if a Controller is an instance of AdvancedController from your game to access the new features.

Features

The following features are available through the advanced interface. See the JavaDocs for further documentation.

Check if the returned controller is implementing the inteface before using it:

if (Controllers.getControllers().get(0) instancoOf AdvancedController {
    // you are safe to use the new methods here
}

Button mapping

ControllerMapping getMapping();

Returns an immutable ControllerMapping objects holding constants for the axis and button indices of the underlying platform, implementation or controller. For example, you can query the state of Button A platform and Controller independant with the following code:

controller.getButton(controller.getMapping().buttonA));

Note that some buttons or axis can be set to ControllerMapping.UNDEFINED.

Query available features

int getMinButtonIndex();
int getMaxButtonIndex();
int getAxisCount();
int getPovCount();
boolean canVibrate();

Vibration

boolean isVibrating();
void startVibration(float strength);
void stopVibration();

Connection state

String getUniqueId();

If more than one controller of the same type is connected, this unique ID helps you identifying a certain controller.

boolean isConnected();

If you hold references to a controller in your game, you might want to know if this controller is already disconnected without having to look it up in the controllers array.

Player Index

Some MFI controllers can show the player index (like the Wii Remotes).

boolean supportsPlayerIndex();
int getPlayerIndex();
void setPlayerIndex(int index);