Skip to content

Button operable Scene2d

Benjamin Schulte edited this page Nov 29, 2017 · 15 revisions

Installation

Add the dependency to your core project:

compile "de.golfgl.gdxcontrollerutils:gdx-controllerutils-scene2d:$cuversion"

Usage

Despite its name, you can use ControllerMenuStage and the other classes even without game controllers, but to make your game's GUI fully keyboard operable.

ControllerMenuStage

Use ControllerMenuStage instead of Stage in your game. Every Actor that should be operable by buttons must be posted to the Stage by calling addFocussableActor:

    stage.addActor(playButton);
    stage.addFocussableActor(playButton);

The initially focused Actor must be set by calling

    stage.setFocussedActor(playButton);

That's all.

More options

You can also set an actor that is automatically hit when Escape (or back on Android) is pressed:

    stage.setEscapeActor(exitButton);

You can override most methods in ControllerMenuStage to adjust its behaviour to your needs. Interesting methods for this are isXxxKeycode and onFocusLost/onFocusGained.

ControllerMenuDialog

ControllerMenuDialog is a dialog adjusted to use with ControllerMenuStage: it automatically focuses its first button when shown, and restores the focus to the last Actor of the parent stage when hidden.

When you add own buttons to the dialog without calling the button() methods, make sure to add it to buttonsToAdd array. All Actors listed in this array will be posted as focusable Actors to the Stage when ControllerMenuDialog is shown.