I have been working on a controller plugin. The plugin consists of three classes which I’ll explain you step by step.
GamepadManager, is the plugin class. You’ll need to register this class via FlxG::addPlugin(). This class manages the controllers and pass the input events to the correct Gamepad. It also does auto mapping.
GamepadMapping, contains a bunch of indices for each gamepad button. If you want to map a certain gamepad, you need to change the indices. Read further how to do this.
Gamepad, it’s like the Keyboard class. It tracks what keys are pressed, tracks analog and accelerometer data.
Mapping and adding gamepad
The gamepad uses the default mapping that is shipped with libGDX. Hooking a gamepad like Xbox 360 won’t work. You’ll need to create a mapping first. Click here for the mapping for Xbox 360. There are different models of controllers with different IDs. The ID is crucial for doing auto mapping. Via GamepadManager you can add the mapping.
Adding a gamepad is very simple, just add new Gamepad to the manager. When you do this, the manager will automatically set the correct mapping to the gamepad. It reads the ID of the controller. So be sure the ID is correct written.
// Xbox 360 Controller (ID varies).
// Create gamepads. You can add as many as you like.
GamepadManager.addGamepad(gamepad = new Gamepad());
Getting the state of the buttons, you can get it like this: gamepad.justPressed(“Button_X”). Look at this example.
If you want to add your own mapping, you’ll need to read the buttonCode. If you develop from source you can add a log in GamepadManager::buttonDown and axisMoved.
I’ve tested the plugin with two different controllers: Sony Dual Shock 2 with adapter and PSP with FuSa plugin. This means I couldn’t test this with Android.
Because of this, I couldn’t test two crucial methods in GamepadManager which are connected and disconnected. I just filled the methods which I think it should work, but I’m unsure with the connected part. If you got an Ouya, please test this for me (and fix it if it’s broken).
There are two more methods which I couldn’t test either, xSliderMoved and ySliderMoved. I have no idea what they are and what they do. Both got sliderCode, which means a gamepad can have multiple sliders. It’s unused for now.
The plugin requires GDX Controller extension. No problem when it’s in the core, but it’s now also included in desktop and Android libs folder. This will increase the size of your app. I tried to move this to the example folder, but the game couldn’t be run. If you don’t need the controller extension, simply delete it from the desktop and android libs folder.