/on_screen_gamepad01

this is a template that has an on screen gamepad and also binds automatically with physical gamepads

Primary LanguageKotlin

Libktx Template With On Screen Gamepad and Physical Gamepad Support

This is the same as a different template project I have, but it has an input 'module' (not really a module, but it's a package called input that can easily be dropped into a project). It relies on some pngs (the images for the buttons), which are in the android/assets/input folder.

gampad.gif

Being a little more complex than the ktx_template project, there's a few structural things that are implemented here to support a background/ foreground.

  • the App class contains instances of a set of camera, stage, and viewport for the background and foreground. They are exposed to different parts of the game using an argument.

  • the 'hud stage' is the top layer and in the Game screen's init block the Gdx.input.InputProcessor is bound to the 'hud stage'

  • note that if you connect a gamepad it'll 'just work'. Also note that in AppObj there's an unused instance of GamepadCtl. That is the class that makes it work and there being an instance of it alone makes it listen for gamepad input - see https://github.com/libgdx/libgdx/wiki/Controllers

  • instead of binding actions to actual input events (ie Keys.SPACE -> player.jump), the keys and events are all mapped to button states in the InputCtl class. You can poll the states of the buttons (whether they were keyboard, gamepad, or onscreen presses) by using AppObj.ic.aButton

I've re-used this input package so many times that I wanted to put it in an example so it's accessible to others - hope it helps you out!