Skip to main content

FlixelGamepadInputManager

View source

class

org.flixelgdx.input.gamepad.FlixelGamepadInputManager

public class FlixelGamepadInputManager implements FlixelInputManager, ControllerListener

Global gamepad manager backed by gdx-controllers. Polls controllers each frame and mirrors the keyboard and mouse frame contract from FlixelGame.

The gamepad system is disabled by default. Set FlixelGamepadInputManager.enabled to true before the game loop starts to opt in:

Flixel.gamepads.enabled = true;

Use logical button and axis constants from FlixelGamepadButton (for example FlixelGamepadButton.A) with FlixelGamepadInputManager.pressed(int, int); each Controller.getMapping() supplies native indices. FlixelGamepadDevice is optional; call FlixelGamepadInputManager.ensureDevice(int) once per slot you want a facade for.

Constructors

FlixelGamepadInputManager()

public FlixelGamepadInputManager()

Fields

MAX_GAMEPADS

public static final int MAX_GAMEPADS = 8

Maximum supported simultaneous controllers.


numActiveGamepads

public int numActiveGamepads

Number of controllers mapped to IDs 0 .. numActiveGamepads-1 this frame.


globalDeadZone

public Float globalDeadZone

Optional analog dead zone applied to all axis reads when non-null. When null, only exact zeroing for true zero input is skipped.


deviceConnected

public final FlixelSignal<GamepadConnectedEvent> deviceConnected

deviceDisconnected

public final FlixelSignal<GamepadDisconnectedEvent> deviceDisconnected

enabled

public boolean enabled

Whether the gamepad system is active. When false, all queries return inactive state and no hardware is polled. Defaults to false; set to true to opt in before the game loop starts.


Methods

attach()

public void attach()

Registers a ControllerListener with gdx-controllers. Safe to call more than once.


detach()

public void detach()

Unregisters listeners and clears internal slot state.


reset()

public void reset()

update()

public void update()

endFrame()

public void endFrame()

getById(int)

public FlixelGamepadDevice getById(int id)

Returns the cached FlixelGamepadDevice for a slot if the game previously called FlixelGamepadInputManager.ensureDevice(int).

Parameters:

NameDescription
idSlot id.

Returns: Cached device, or null.


ensureDevice(int)

public FlixelGamepadDevice ensureDevice(int id)

Ensures a FlixelGamepadDevice exists for the given slot. At most one instance is created per id for the lifetime of this manager (until FlixelGamepadInputManager.reset()).

Parameters:

NameDescription
idSlot id.

Returns: Non-null device facade.


getFirstActiveGamepadId()

public int getFirstActiveGamepadId()

First slot with any button beyond dead zone or analog movement this frame, or -1.

Returns: Active id, or -1 when none.


getFirstActiveGamepad()

public FlixelGamepadDevice getFirstActiveGamepad()

Returns a device for FlixelGamepadInputManager.getFirstActiveGamepadId() only when that slot was already ensured.

Returns: Device, or null.


getActiveGamepadIds(int[])

public int getActiveGamepadIds(int[] reuseOut)

Writes active slot ids in order to reuseOut[0 .. count-1].

Parameters:

NameDescription
reuseOutCaller buffer; length should be at least FlixelGamepadInputManager.MAX_GAMEPADS.

Returns: Number of ids written.


getActiveGamepads(FlixelGamepadDevice[])

public int getActiveGamepads(FlixelGamepadDevice[] reuseOut)

Fills reuseOut with FlixelGamepadDevice instances that were previously ensured and are still connected.

Parameters:

NameDescription
reuseOutCaller buffer.

Returns: Number of entries written.


anyPressed(int)

public boolean anyPressed(int logicalButton)

Returns true when any active gamepad currently holds the given button.

Parameters:

NameDescription
logicalButtonA logical button constant from FlixelGamepadButton, such as FlixelGamepadButton.A.

Returns: true when at least one gamepad is pressing the button this frame.


anyJustPressed(int)

public boolean anyJustPressed(int logicalButton)

Returns true when any active gamepad first pressed the given button this frame.

Parameters:

NameDescription
logicalButtonA logical button constant from FlixelGamepadButton.

Returns: true when at least one gamepad transitioned to pressed this frame.


anyJustReleased(int)

public boolean anyJustReleased(int logicalButton)

Returns true when any active gamepad released the given button this frame.

Parameters:

NameDescription
logicalButtonA logical button constant from FlixelGamepadButton.

Returns: true when at least one gamepad transitioned to released this frame.


anyInput()

public boolean anyInput()

Returns true when any active gamepad has a button pressed or an analog input beyond the dead zone this frame.

Returns: true when at least one gamepad is producing input.


anyMovedXAxis()

public boolean anyMovedXAxis()

Returns true when any active gamepad is moving a left or right stick horizontally beyond the dead zone this frame.

Returns: true when at least one gamepad has horizontal stick movement.


anyMovedYAxis()

public boolean anyMovedYAxis()

Returns true when any active gamepad is moving a left or right stick vertically beyond the dead zone this frame.

Returns: true when at least one gamepad has vertical stick movement.


pressed(int, int)

public boolean pressed(int gamepadId, int logicalButton)

Returns true when the given slot is currently pressing the given button.

Pass FlixelGamepadButton.ANY as the button to check whether the slot has any button pressed or meaningful analog movement this frame.

Parameters:

NameDescription
gamepadIdSlot index.
logicalButtonA logical button constant from FlixelGamepadButton.

Returns: true when that button is pressed on the given slot this frame.


justPressed(int, int)

public boolean justPressed(int gamepadId, int logicalButton)

Returns true when the given slot first pressed the button this frame (was not pressed last frame).

Parameters:

NameDescription
gamepadIdSlot index.
logicalButtonA logical button constant from FlixelGamepadButton.

Returns: true on the first frame the button is pressed.


justReleased(int, int)

public boolean justReleased(int gamepadId, int logicalButton)

Returns true when the given slot released the button this frame (was pressed last frame, not pressed now).

Parameters:

NameDescription
gamepadIdSlot index.
logicalButtonA logical button constant from FlixelGamepadButton.

Returns: true on the first frame the button is no longer pressed.


firstPressed(int)

public int firstPressed(int gamepadId)

Returns the logical button code of the button that has been held the longest on the given slot, or FlixelGamepadButton.NONE when no button is currently held.

Press order is tracked across frames: the first physical button detected as pressed since the slot was connected (or last cleared) is always returned first, regardless of how many buttons are held simultaneously.

int btn = Flixel.gamepads.firstPressed(0);
if (btn != FlixelGamepadButton.NONE) {
System.out.println("Oldest held button: " + FlixelGamepadButton.toString(btn));
}

Parameters:

NameDescription
gamepadIdSlot index.

Returns: Logical button code from FlixelGamepadButton, or FlixelGamepadButton.NONE when no button is held or the slot is inactive.


firstJustPressed(int)

public int firstJustPressed(int gamepadId)

Returns the logical button code of the first button that transitioned to pressed this frame on the given slot, or FlixelGamepadButton.NONE when no button was just pressed.

When multiple buttons are pressed in the same frame, the one with the lowest native index is returned. Unmapped native buttons (those not in FlixelGamepadButton) are skipped.

int btn = Flixel.gamepads.firstJustPressed(0);
if (btn == FlixelGamepadButton.A) {
jump();
}

Parameters:

NameDescription
gamepadIdSlot index.

Returns: Logical button code from FlixelGamepadButton, or FlixelGamepadButton.NONE when no button was just pressed or the slot is inactive.


firstJustReleased(int)

public int firstJustReleased(int gamepadId)

Returns the logical button code of the first button that was released this frame on the given slot, or FlixelGamepadButton.NONE when no button was just released.

When multiple buttons are released in the same frame, the one with the lowest native index is returned. Unmapped native buttons (those not in FlixelGamepadButton) are skipped.

int btn = Flixel.gamepads.firstJustReleased(0);
if (btn == FlixelGamepadButton.A) {
stopCharge();
}

Parameters:

NameDescription
gamepadIdSlot index.

Returns: Logical button code from FlixelGamepadButton, or FlixelGamepadButton.NONE when no button was just released or the slot is inactive.


getAxis(int, int)

public float getAxis(int gamepadId, int logicalAxis)

Returns the current value of a logical axis on the given slot, after applying the global dead zone. Returns 0f when the value is within the dead zone or the slot is out of range.

Parameters:

NameDescription
gamepadIdSlot index.
logicalAxisA logical axis constant from FlixelGamepadButton, such as FlixelGamepadButton.AXIS_LEFT_X.

Returns: Axis value in the range [-1, 1], or 0f when inactive.


getModel(int)

public FlixelGamepadModel getModel(int gamepadId)

Model detected for the given slot the last time the slot was (re)bound.

Parameters:

NameDescription
gamepadIdSlot index.

Returns: Detected model, or FlixelGamepadModel.UNKNOWN when out of range or unrecognized.


setHapticsProvider(FlixelHapticsProvider)

public void setHapticsProvider(FlixelHapticsProvider provider)

Replaces the haptics backend used by all vibration calls on this manager.

Each platform launcher installs a provider automatically: FlixelLwjgl3Launcher installs FlixelLwjgl3HapticsProvider (Jamepad/SDL, true dual-motor), and FlixelTeaVMLauncher installs FlixelTeaVMHapticsProvider (W3C Gamepad Haptics API, true dual-motor). Only override this when you need platform-specific features that the built-in providers do not cover, such as DualSense adaptive triggers or haptic patterns.

Parameters:

NameDescription
providerNon-null replacement provider.

Throws:

TypeDescription
NullPointerExceptionIf provider is null.

setAnalogButtonReader(FlixelAnalogButtonReader)

public void setAnalogButtonReader(FlixelAnalogButtonReader reader)

Installs a platform-specific reader for analog button values, used to populate FlixelGamepadButton.AXIS_TRIGGER_L and FlixelGamepadButton.AXIS_TRIGGER_R on backends where triggers are exposed as buttons rather than axes (for example, the web W3C Gamepad API).

FlixelTeaVMAnalogButtonReader (installed automatically by FlixelTeaVMLauncher) is the only built-in implementation. Pass null to disable analog button reading and fall back to the axis-only trigger behavior.

Parameters:

NameDescription
readerReader to install, or null to clear any existing reader.

canVibrate(int)

public boolean canVibrate(int slot)

Returns whether the controller in the given slot reports vibration support.

Parameters:

NameDescription
slotSlot index.

Returns: true when the system is enabled, the slot is in range, and the hardware reports haptics capability.


vibrate(int, float)

public void vibrate(int slot, float durationSecs)

Vibrates the controller in the given slot at full intensity on both motors for the given duration.

// Short full-strength rumble on the first controller.
Flixel.gamepads.vibrate(0, 0.3f);

Parameters:

NameDescription
slotSlot index.
durationSecsHow long to vibrate in seconds.

vibrate(int, float, float)

public void vibrate(int slot, float intensity, float durationSecs)

Vibrates the controller in the given slot at the given intensity on both motors.

Parameters:

NameDescription
slotSlot index.
intensityMotor strength in the range [0, 1].
durationSecsHow long to vibrate in seconds.

vibrate(int, float, float, float)

public void vibrate(int slot, float leftIntensity, float rightIntensity, float durationSecs)

Vibrates the controller in the given slot with independent left and right motor intensities.

// Rumble only the left (low-frequency) motor at half strength for half a second.
Flixel.gamepads.vibrate(0, 0.5f, 0f, 0.5f);

Parameters:

NameDescription
slotSlot index.
leftIntensityStrength for the left (low-frequency) motor, in the range [0, 1].
rightIntensityStrength for the right (high-frequency) motor, in the range [0, 1].
durationSecsHow long to vibrate in seconds.

getTriggerL(int)

public float getTriggerL(int gamepadId)

Returns the current analog pressure of the left trigger (L2) on the given slot, in the range [0, 1], after applying the global dead zone.

On the Jamepad/SDL desktop backend, triggers are reported as axes, so this reads the raw trigger axis directly. On web (TeaVM/W3C Gamepad API), triggers are digital buttons; this method returns 0 there; for web, use FlixelGamepadInputManager.pressed(int, int) with FlixelGamepadButton.L2 instead.

float howHardL2 = Flixel.gamepads.getTriggerL(0);

Parameters:

NameDescription
gamepadIdSlot index.

Returns: Trigger pressure in [0, 1], or 0f when inactive or within the dead zone.


getTriggerR(int)

public float getTriggerR(int gamepadId)

Returns the current analog pressure of the right trigger (R2) on the given slot, in the range [0, 1], after applying the global dead zone.

On the Jamepad/SDL desktop backend, triggers are reported as axes, so this reads the raw trigger axis directly. On web (TeaVM/W3C Gamepad API), triggers are digital buttons; this method returns 0 there; for web, use FlixelGamepadInputManager.pressed(int, int) with FlixelGamepadButton.R2 instead.

float howHardR2 = Flixel.gamepads.getTriggerR(0);

Parameters:

NameDescription
gamepadIdSlot index.

Returns: Trigger pressure in [0, 1], or 0f when inactive or within the dead zone.


stopVibration(int)

public void stopVibration(int slot)

Stops any active vibration on the controller in the given slot immediately.

Parameters:

NameDescription
slotSlot index.

controllerAt(int)

public Controller controllerAt(int slot)

Returns the raw, underlying gdx-controllers Controller object at the given index.

Parameters:

NameDescription
slotSlot index.

Returns: The gdx-controllers Controller object from the provided index, or null if it could not be found.


connected(Controller)

public void connected(Controller controller)

disconnected(Controller)

public void disconnected(Controller controller)

buttonDown(Controller, int)

public boolean buttonDown(Controller controller, int buttonIndex)

buttonUp(Controller, int)

public boolean buttonUp(Controller controller, int buttonIndex)

axisMoved(Controller, int, float)

public boolean axisMoved(Controller controller, int axisIndex, float value)

GamepadConnectedEvent

class

org.flixelgdx.input.gamepad.FlixelGamepadInputManager.GamepadConnectedEvent

public static final class GamepadConnectedEvent

Mutable payload reused for FlixelGamepadInputManager.deviceConnected; do not retain past the callback.

Constructors

GamepadConnectedEvent()

public GamepadConnectedEvent()

Methods

gamepadId()

public int gamepadId()

model()

public FlixelGamepadModel model()

GamepadDisconnectedEvent

class

org.flixelgdx.input.gamepad.FlixelGamepadInputManager.GamepadDisconnectedEvent

public static final class GamepadDisconnectedEvent

Mutable payload reused for FlixelGamepadInputManager.deviceDisconnected; do not retain past the callback.

Constructors

GamepadDisconnectedEvent()

public GamepadDisconnectedEvent()

Methods

gamepadId()

public int gamepadId()