Gamepad Support

Introduction

Gum Forms supports using a gamepad to control the UI. In general, when using a gamepad a single UI control has focus. Pressing up or down on the gamepad moves focus to the next item. Each forms control can support different interaction. For example, Buttons can be clicked, but a ListBox supports selecting items within the ListBox.

For information on using gamepads for tabbing, see the Tabbing page.

Enabling Gamepad Controls

To enable gamepad support in your screen:

  1. Be sure to have a gamepad plugged in. Any gamepad that is usable in MonoGame will also work as a gamepad in Gum Forms

  2. Add the gamepad to the FrameworkElement.GamepadsForUiControl. You can add multiple gamepads for multiplayer games.

  3. Set the initial control to have focus by setting its IsFocused = true

For example, the following code enables gamepad control for a game assuming MyButton is a valid button:

// Initialize
// The first gamepad:
var gamepad = GumUI.Gamepads[0];
// If this code is run multiple times then the gamepad
// may get added multiple times as well. To be safe, clear
// the list:
FrameworkElement.GamePadsForUiControl.Clear();
FrameworkElement.GamePadsForUiControl.Add(gamepad);
MyButton.IsFocused = true;

Button Control Click

By default a gamepad's A button can be used to select the focused control. If the focused control is a Button then pressing a gamepad's A button. The following example code shows how to detect clicks on a button which happen with the gamepad:

Pressing the A button raises the focused button's Click event.

Pressing the A button clicks the highlighted Button

Handling buttons specifically can be handled by subscribing to ControllerButtonPushed.

Buttons can respond to any gamepad button push

The Click event may be raised with InputEventArgs containing the gamepad. Remember, clicks can happen a variety of ways including the mouse or even being directly invoked, so you need to check whether the second parameter is of type InputEventArgs and if the device is a GamePad.

Output from clicking on a button with a gamepad

If additional flexibility is needed, gamepad events can be polled in an Update method.

ListBox Navigation

When a ListBox has focus, pressing up or down on the D-pad (or tilting the left stick up or down) moves the selection through the list. The SelectionChanged event fires each time the selected item changes.

The following example adds items to a ListBox, enables gamepad input, and reacts to selection changes:

Try on XnaFiddle.NETarrow-up-right

Pressing down selects the next item and pressing up selects the previous item. The selection does not wrap when it reaches the first or last item.

Slider Adjustment

When a Slider has focus, pressing left or right on the D-pad (or tilting the left stick left or right) decreases or increases the slider's Value by SmallChange. By default, a Slider sets IsUsingLeftAndRightGamepadDirectionsForNavigation to false so that left and right inputs adjust the value rather than moving focus to another control.

The following example creates a Slider with a range of 0–100, enables gamepad input, and displays the current value:

Try on XnaFiddle.NETarrow-up-right

Each left or right press changes Value by the amount set in SmallChange. You can also set LargeChange for larger increments when clicking the track area with a mouse or cursor.

Last updated

Was this helpful?