# Skewed settings menu - Gamepad support

> Many modern games incorporate gamepad compatibility, enabling players to engage with the game, user interface (UI), and menus seamlessly. Gameface supports the integration of gamepads into your UI.

> Documentation index: https://coherent-labs.com/llms.txt lists every page of this documentation as Markdown; the full text is in https://coherent-labs.com/llms-full.txt.

Source: https://coherent-labs.com/blog/uitutorials/skewed-menu-gamepad/

## Skewed settings menu - Gamepad support

ui tutorials

5/16/2024

Kaloyan Geshev

1. Showcase Overview
2. Source location and sample preview
3. Sample in details
   1. Used assets
   2. Interaction manager
   3. Define the gamepad controls mapping
   4. Webpack configuration change
   5. Enabling the gamepad in Gameface
   6. Disabling UI elements
   7. Navigating options using gamepad controls
   8. Adjusting option values using a gamepad
   9. Navigating settings menus and submenus using a gamepad
   10. Creating a gamepad mappings page
   11. Creating the gamepad scheme page

Many modern games incorporate gamepad compatibility, enabling players to engage with the game, user interface (UI), and menus seamlessly.

Gameface supports the integration of gamepads into your UI. We provide a dedicated library for Gameface, known as the [Interaction Manager ](https://coherentlabs.github.io/GameUIComponents/en/interaction-manager/introduction/). This library is capable of managing various interactions with the UI, including keyboard inputs, gamepad commands, touch gestures, and more.

## Showcase Overview

To illustrate the process of integrating gamepad support into your UI, we will expand upon the Skewed settings menu sample.

We will utilize the [interaction manager ](https://coherentlabs.github.io/GameUIComponents/en/interaction-manager/introduction/)to handle gamepad input and add our own logic to handle various actions. Notable additions in this sample include:

- Navigating through menus using gamepad controls.
- Selecting different options and adjusting their values via the gamepad.
- Introduction of a new page for remapping in-game gamepad controls using the gamepad itself.
- Introduction of a new page displaying a graphic schema of the gamepad and providing tips for each button’s function in the game.

## Source location and sample preview

You can find the complete sample source within the `${Gameface package}/Samples/uiresources/UITutorials/SkewedSettingsMenu` directory.

To enable gamepad controls for the sample, follow these steps:

1. Connect a gamepad to your PC.
2. Build, run the sample, and play around using the gamepad. You can build and preview the sample in various ways:
   1. Run `npm run watch`. This command starts a development server with a live reload. After running the `watch` command, launch the Player using the `Player.bat` file and provide the following `url` argument: `"--url=http://localhost:9000"`.
   2. Run `npm run build`. This command generates a production (minified) build in the `dist` folder. Then you can run the `index.html` from the `dist` folder in the Player.
   3. Run `npm run build:dev`. This command creates a development build (non-minified) in the `dist` folder. Then you can run the `index.html` from the `dist` folder in the Player.

## Sample in details

### Used assets

To showcase the gamepad buttons, we’ve utilized assets from the following free Figma [file ](https://www.figma.com/community/file/1271153059120916114).

### Interaction manager

To begin, we must install the interaction manager by running `npm i coherent-gameface-interaction-manager` within the `${Gameface package}/Samples/uiresources/UITutorials/SkewedSettingsMenu directory`.

### Define the gamepad controls mapping

The mapping for gamepad controls within the game is established through the `gamepad-controls-model.js` file. This file follows a structured format:

```js
[
    {
        actionName: 'Move forward',
        control: {
            alias: 'pad-up',
            icon: './gamepad-controls/dpad-up.svg'
        },
    },
    // other mappings
]
```

In this structure:

- `actionName` specifies the function of the control.
- `control.alias` is utilized to designate the control alias, as defined in the interaction manager gamepad documentation.
- `control.icon` provides the path to the control icon within the `assets/gamepad-controls` directory.

Additionally, within this file, a flag named `gamepadControls` is defined. This flag toggles the gamepad support within the sample when it is built using relevant commands. When set to `true`, the sample will be built with gamepad support enabled. Moreover, the `Controls/Keyboard mappings` page will be hidden, and in its place, the `Controls/Gamepad` mappings and `Controls/Gamepad schema` pages will be visible.

### Webpack configuration change

To enable the utilization of gamepad icons introduced in `gamepad-controls-model.js`, we must modify the webpack configuration slightly and integrate the `copy-webpack-plugin`. Follow these steps:

1. Install the `copy-webpack-plugin` by running: `npm i --save-dev copy-webpack-plugin`
2. Update the webpack configuration:

```diff
...
 const CopyPlugin = require("copy-webpack-plugin");
...
new MiniCssExtractPlugin(),
new CopyPlugin({
    patterns: [
       { from: "assets/gamepad-controls", to: "gamepad-controls" },
   ],
}),],
module: {
...
```

### Enabling the gamepad in Gameface

Once the setup is complete, we can enable the gamepad in Gameface when `gamepadControls` variable is set to `true`.

`src/index.js`

```javascript
import { gamepad } from 'coherent-gameface-interaction-manager';
import { gamepadControls } from './gamepad-controls-model';

if (gamepadControls) {
    gamepad.enabled = true;
    gamepad.lessSensitive = true;
}
```

### Disabling UI elements

When controlling the menu with or without a gamepad, it’s necessary to provide an option to disable parts of the UI that aren’t relevant. For instance, the `Keyboard mappings` submenu isn’t needed when a gamepad is connected. Instead, we require the ability to modify gamepad mappings, necessitating the enabling of two different submenus: `Gamepad mappings` and `Gamepad schema`. The same applies when the menu is controlled without a gamepad - in that case, we enable `Keyboard mappings` and disable the gamepad-related submenus.

To achieve this, upon initialization of the `settings-page`, we identify the elements to be hidden and remove them from the `Document Object Model (DOM)` using the `.remove()` method.

`src/pages/settings-page/script.js`

```javascript
import { gamepadControls } from '../../../../gamepad-controls-model';
...
removeNonRelevantElements() {
    if (gamepadControls) {
        this.querySelectorAll('[keyboard]').forEach((el) => el.remove());
    } else {
        this.querySelectorAll('[gamepad]').forEach((el) => el.remove());
    }
}
...
init(data) {
    this.setupTemplate(data, () => {
        components.renderOnce(this);
        // attach event handlers here
        window.requestAnimationFrame(() => {
            ...
            this.removeNonRelevantElements();
            ...
        });
    });
}
```

### Navigating options using gamepad controls

To facilitate option navigation within each page, a custom method is required to handle this functionality and trigger it upon pressing specific buttons on the gamepad. In this example, we’ll configure this operation for the `pad-up` and `pad-down` buttons (or the up/down arrows of the gamepad).

First, we need to implement an action in the interaction manager to manage the gamepad’s `pad-up/down` buttons. This is accomplished within the `toggleGamepadActions` method of the `settings-page` component.

```javascript
gamepad.on({
    actions: ['pad-up'],
    callback: () => this.selectOption(-1),
});
gamepad.on({
    actions: ['pad-down'],
    callback: () => this.selectOption(1),
});
```

When selectOption is called with `1` or `-1`, it signifies the direction in which the next option should be selected. `-1` indicates upward movement, while `1` indicates downward movement.

```javascript
selectOption(direction) {
    const activeSettingItem = this.activeOption;
    // If there is no selected option we need to select the first one
    if (!activeSettingItem) return this.activeSubPanel.querySelector('settings-item').classList.toggle('active', true);

    // We get like that the next options we need to select based on the direction - up or down
    const optionToSelect = direction === 1 ? activeSettingItem.nextElementSibling : activeSettingItem.previousElementSibling;
    if (!optionToSelect) return;

    activeSettingItem.classList.toggle('active', false);
    optionToSelect.classList.toggle('active', true);
    // If the option is not visible we need to scroll to it
    this.scrollToElement(optionToSelect);
}
```

Using the native `nextElementSibling` and `previousElementSibling`, we can obtain the previous and next elements to be selected. Since the options are arranged vertically within a `gameface-scrollable-container`, it’s possible to select an option that isn’t visible. Hence, scrolling the scrollable container is necessary to make the option visible. This is achieved by calling the `scrollToElement` method, which performs the following:

```javascript
scrollToElement(element) {
    // The element with class .guic-scrollable-container has overflow scroll so we need it.
    const scrollableContainer = this.scrollableContainerComponent.querySelector('.guic-scrollable-container');
    // element.offsetTop + element.offsetHeight will make the element visible at the bottom of the scrollable container so we multiply the element height by 3 so
    // it is scrolled in the middle of the container.
    const diff = element.offsetTop + element.offsetHeight * 3 - scrollableContainer.offsetHeight;
    scrollableContainer.scrollTop = diff >= 0 ? diff + element.offsetHeight : 0;
    // Call the onScroll method of the scrollable container to visually reflect the scrollTop changes
    this.scrollableContainerComponent.onScroll();
}
```

### Adjusting option values using a gamepad

When navigating through menu options, it becomes necessary to facilitate the modification of their values using a gamepad. To achieve this functionality, the `pad-left` and `pad-right` actions (or the left/right arrows on the gamepad) are utilized within the provided sample.

Each option within the menu may consist of various components such as rangeslider, stepper, or radio buttons, each requiring a different approach for value adjustment when the gamepad arrows are pressed.

To begin, an interaction manager action is added to handle the gamepad’s `pad-left/right` buttons. This is implemented within the `toggleGamepadActions` method of the `settings-page` component.

```javascript
gamepad.on({
    actions: ['pad-left'],
    callback: () => this.updateOptionValue(-1),
});
gamepad.on({
    actions: ['pad-right'],
    callback: () => this.updateOptionValue(1),
});
```

The `updateOptionValue` method performs the following actions:

```javascript
updateOptionValue(direction) {
    if (!this.activeOption) return;
    // Retrieves the option - all options are gameface components.
    const option = this.activeOption.querySelector('[slot="option"]').children[0];
    switch (option.tagName) {
        case 'GAMEFACE-STEPPER': {
            option[direction === 1 ? 'next' : 'prev']();
            break;
        }
        case 'GAMEFACE-RANGESLIDER': {
            if (direction === 1) option.value += 1;
            else option.value -= 1;
            break;
        }
        case 'GAMEFACE-RADIO-GROUP': {
            option[direction === 1 ? 'setCheckedToNextItem' : 'setCheckedToPreviousItem'](option.querySelector('[aria-checked="true"]'));
            break;
        }
    }
}
```

1. The stepper component provides methods `stepper.next` and `stepper.prev` for adjusting its value.
2. To adjust the value of a rangeslider, the `rangeslider.value += 1; or -= 1` syntax is utilized.
3. The radio group offers `setCheckedToNextItem` and `setCheckedToPreviousItem` methods, that accept the currently selected radio button as an argument. It can be retrieved through `radiogroup.querySelector('[aria-checked="true"]')`.

### Navigating settings menus and submenus using a gamepad

To enable interaction with all menus using a gamepad involves implementing an interaction manager action to handle gamepad buttons such as `left/right-shoulder-bottom` and `left/right-shoulder`. The implementation is located within the `toggleGamepadActions` method of the `settings-page` component.

```javascript
// Actions for changing the main tabs
gamepad.on({
    actions: ['left-sholder-bottom'],
    callback: () => this.selectMainTab(-1),
});
gamepad.on({
    actions: ['right-sholder-bottom'],
    callback: () => this.selectMainTab(1),
});

// Actions for changing the subtabs of the current main tab
gamepad.on({
    actions: ['left-sholder'],
    callback: () => this.selectSubTab(-1),
});
gamepad.on({
    actions: ['right-sholder'],
    callback: () => this.selectSubTab(1),
});
```

The `selectMainTab` and `selectSubTab` methods perform the following tasks:

```javascript
selectMainTab(direction) {
    this.tabsComponent.selectTab(this.tabsComponent[direction === -1 ? 'getPrevTab' : 'getNextTab']());
    // If there is no active sub menu tab, select the first one
    if (!this.activeSubPanel) {
        const subTabs = this.activeMainPanel.querySelector('gameface-tabs');
        subTabs.selectTab(subTabs.getFirstTab());
    }
}

selectSubTab(direction) {
    const subGamefaceTabs = this.activeMainPanel.querySelector('gameface-tabs');
    subGamefaceTabs.selectTab(subGamefaceTabs[direction === -1 ? 'getPrevTab' : 'getNextTab']());
}
```

Utilizing the methods `selectTab`, `getPrevTab`, and `getNextTab` from the `gameface-tabs` component, we can efficiently manipulate the main and sub tabs programmatically via JavaScript.

### Creating a gamepad mappings page

In the `${Gameface package}/Samples/uiresources/UITutorials/SkewedSettingsMenu/src/pages/settings/controls` directory, we’ve implemented a page named `Gamepad mappings`. This page allows users to remap their gamepad controls for use within the game. Navigation through the options is facilitated by using the gamepad’s `pad-up` and `pad-down` controls. Upon selecting an option by pressing the `X` button, users can then press any button on the gamepad to replace the existing one.

Here’s how we accomplished this:

1. Dynamically generated options based on the model outlined in `gamepad-controls-model.js`.
2. Developed a `gamepad-select` component, visually indicating when a key is being changed.
3. Implemented logic to handle gamepad input for remapping keys.

The dynamic options are generated by the `setOptions` method within the `gamepad-mapping` component, located in `/SkewedSettingsMenu/src/pages/settings/controls/gamepad/script.js`.

```javascript
setOptions() {
    const itemsContainer = this.querySelector('[slot="items"]');
    itemsContainer.innerHTML = '';

    model.forEach(({ actionName, control }) => {
        const settingsItem = document.createElement('settings-item');
        settingsItem.innerHTML = `
        <span slot="label">${actionName}</span>
        <div slot="option">
            <gamepad-select>
                <div class="gamepad-btn-icon" style="background-image:url(${control.icon})" slot="btn-icon"></div>
            </gamepad-select>
        </div>
        `;
        settingsItem.id = control.alias;
        itemsContainer.appendChild(settingsItem);
    });
}
```

To enable control changes, we’ve added an action for when the gamepad’s `X` button is pressed. This interaction is managed through the interaction manager:

```javascript
gamepad.on({
    actions: ['face-button-left'],
    callback: () => this.selectOption(),
});
```

In the `selectOption` method, the selected control becomes active, awaiting input from another button on the gamepad to replace it.

```javascript
selectOption() {
    const option = this.querySelector(`settings-item.active`);
    if (!option) return;

    this.activeControl = option.id;
    option.querySelector('gamepad-select').classList.add('active');
    // A brief delay is implemented to prevent accidental selection due to fast polling in interaction manager.
    setTimeout(() => this.toggleButtonsGamepadActions(true), 300);
}
```

The `toggleButtonsGamepadActions` method is responsible for adding actions for all buttons on the gamepad defined in `gamepad-controls-model.js`. Its purpose is to detect the next clicked button from the gamepad to be chosen as the new one.

```javascript
toggleButtonsGamepadActions(on) {
    model.map((control) => control.control.alias).forEach((button) => {
        if (on) {
            gamepad.on({
                actions: [button],
                callback: () => this.onGamepadButtonPressed(button),
            });
        } else {
            gamepad.off([button]);
        }
    });
}
```

Additionally, when a new button is selected, we need to deactivate the actions from the interaction manager to prevent their execution when unnecessary.

Once a new gamepad key is chosen, the old control is swapped with the new one both in the UI and in the model.

```javascript
onGamepadButtonPressed(button) {
    this.swapControls(button, this.activeControl);
    this.toggleButtonsGamepadActions(false);
}

swapControls(newBtn, oldBtn) {
    const oldBtnIndex = model.findIndex(({ control }) => control.alias === oldBtn);
    const newBtnIndex = model.findIndex(({ control }) => control.alias === newBtn);
    const oldOption = this.querySelector(`#${oldBtn}`);
    const newOption = this.querySelector(`#${newBtn}`);

    if (newBtn === oldBtn) {
        oldOption.querySelector('gamepad-select').classList.remove('active');
        return;
    }

    oldOption.id = model[newBtnIndex].control.alias;
    oldOption.querySelector('.gamepad-btn-icon').style.backgroundImage = `url(${model[newBtnIndex].control.icon})`;
    oldOption.querySelector('gamepad-select').classList.remove('active');
    newOption.querySelector('.gamepad-btn-icon').style.backgroundImage = `url(${model[oldBtnIndex].control.icon})`;
    newOption.id = model[oldBtnIndex].control.alias;

    const tempIcon = model[oldBtnIndex].control.icon;
    const tempAlias = model[oldBtnIndex].control.alias;
    model[oldBtnIndex].control.icon = model[newBtnIndex].control.icon;
    model[oldBtnIndex].control.alias = model[newBtnIndex].control.alias;
    model[newBtnIndex].control.icon = tempIcon;
    model[newBtnIndex].control.alias = tempAlias;
}
```

### Creating the gamepad scheme page

In Gameface, generating a detailed breakdown of each gamepad key’s function upon clicking is easily achievable with a small amount of JavaScript and SVG.

You can find the page showcasing this breakdown at `/SkewedSettingsMenu/src/pages/settings/controls/gamepad-schema`.

#### SVG gamepad preparation

To create this breakdown, we first need to craft the gamepad in SVG format. We utilized Figma for this task because it allows direct export of textures into SVG while preserving element IDs. This feature is incredibly powerful as it enables dynamic control changes for each gamepad key.

You can directly access our design document on Figma [here ](https://www.figma.com/file/ZxGDOzVasLwNHLGR4Fqt2W/Gamepad-controls-schema?type=design\&node-id=0%3A1\&mode=dev\&t=PTjqY2J9HtUGNsnW-1).

Once you’ve opened the file, select the frame, and navigate to `Design` -> `Export`. Change the export mode to `SVG`, then from the three dots, ensure that `Include "id"` attribute is checked and `Outline text` is unchecked. Afterward, export the frame into SVG. You can preview the SVG file’s content into `/SkewedSettingsMenu/src/pages/settings/controls/gamepad-schema/template.html`.

Following this step, some modifications are necessary to ensure everything functions properly:

1. Add `x` and `y` attributes to the `text` elements to position them correctly within the `SVG` in Gameface. This task is easily accomplished using the inspector in Gameface.
2. Change the `fill` to white.
3. Remove the `tspan` elements as they are unnecessary.
4. Remove the `xml:space="preserve" style="white-space: pre" font-family="Inter" font-size="12" letter-spacing="0em"` attributes from the text, as they are redundant.
5. Add the appropriate `text-anchor` attribute (`middle`, `start`, or `end`) to each text element based on its position. This ensures that when new text is dynamically added, the space expands in the correct direction relative to the text element.

#### Adding the controls

After obtaining the SVG schema, the next step involves dynamically integrating controls into the SVG text elements associated with gamepad keys.

```javascript
init(data) {
    this.setupTemplate(data, () => {
        components.renderOnce(this);
        this.setControls();
        document.addEventListener('gamepad-controls-changed', this.setControls);
    });
}

setControls() {
    model.forEach(({ control, actionName }) => {
        const svgTextElement = this.querySelector(`#${control.alias}`);
        if (!svgTextElement) return;

        svgTextElement.textContent = actionName;
    });
}
```

In the `setControls` function, the script locates the SVG `text` element corresponding to each gamepad control from the model and updates its `textContent` with the relevant action name.

Furthermore, to ensure that the schema updates dynamically when the gamepad mappings change, a custom event listener is added via `document.addEventListener('gamepad-controls-changed', this.setControls)`. This listener triggers the `setControls` function whenever the gamepad controls model is altered from the `Gamepad mappings` page.

## On this page
