# Radial menus in Gameface

> Radial menus, integrated into game user interfaces, provide players with access to a variety of commands or items arranged in a circular layout. Users can effortlessly navigate through these options by moving in directions relative to the center of the circle. This intuitive approach not only simplifies interaction but also enhances the visual appeal of the game interface, providing players with quick access to actions without overwhelming them with complex menus.

> 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/radial-menu/

## Radial menus in Gameface

ui tutorials

5/13/2024

Kaloyan Geshev

1. Showcase overview
   1. Source location
   2. Sample demonstration
2. Radial menu in details
   1. Setting up the radial menu
   2. Adding perspective view to the radial menu
   3. Update dynamically the information in the central circle
   4. Customizing default radial menu styles

Radial menus, integrated into game user interfaces, provide players with access to a variety of commands or items arranged in a circular layout. Users can effortlessly navigate through these options by moving in directions relative to the center of the circle. This intuitive approach not only simplifies interaction but also enhances the visual appeal of the game interface, providing players with quick access to actions without overwhelming them with complex menus.

## Showcase overview

Creating a radial menu in Gameface is a straightforward process. We provide a [custom component ](https://coherentlabs.github.io/GameUIComponents/en/components/radial-menu/)for the radial menu, which serves as an easily adaptable base. Additionally, its default styles can be overridden to suit your specific requirements.

### Source location

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

To review it, simply run the `index.html` page in Gameface. **However, before doing so, ensure that you have executed the `npm i` command in the radial-menu directory to ensure proper installation and rendering of the radial menu component on the page.**

### Sample demonstration

In this sample, we have done the following:

1. Utilized the `radial-menu` component from the Gameface components suite.
2. Customized some default styles of the components to achieve the effects seen in the first image.
   1. Added appealing borders and animations when an item is selected.
   2. Modified the highlight of the currently selected item to resemble an arrow rather than a background.
3. Implemented logic to rotate the radial menu with perspective effect based on the selected item.
4. Displayed information in the center of the radial menu for the selected item.

## Radial menu in details

### Setting up the radial menu

Setting up a radial menu is remarkably simple. All it takes is importing the necessary styles, JavaScript, and custom elements into your HTML page.

```html
<head>
    <link rel="stylesheet" href="./node_modules/coherent-gameface-radial-menu/style.css"/>
</head>

<body>
    <div class="radial-menu-wrapper">
        <gameface-radial-menu class="radial-menu" data-name="Radial Menu" data-open-key-code="shift" data-change-event-name="radialMenuItemChanged">
             <div
                slot="radial-menu-center"
                class="radial-menu-center"
            >
                <div class="radial-menu-text-wrapper">
                    <span class="weapon-text"></span>
                    <p cohinline>
                        <img
                            class="ammo-icon"
                            src="./assets/ammo-icon.svg"
                        >
                        <span class="ammo-text"></span>
                    </p>
                </div>
            </div>
        </gameface-radial-menu>
    </div>

    <script src="./node_modules/coherent-gameface-radial-menu/dist/radial-menu.production.min.js"></script>
</body>
```

Additionally, you can specify a custom event name to trigger actions in JavaScript when the active element changes via `data-change-event-name` attribute.

### Adding perspective view to the radial menu

To give the radial menu a perspective view based on the selected item, follow these steps:

1. Apply a `perspective` CSS property to the `radial-menu-wrapper`, allowing for perspective rotation.
2. Calculate the rotation of the `radial-menu` in JavaScript when the active element changes.

Here’s an example of applying the `perspective` property to the `radial-menu-wrapper` in CSS:

```css
.radial-menu-wrapper {
    perspective: 150vh;
    perspective-origin: center;
}
```

Following this adjustment, it becomes necessary to incorporate the `transform: rotateX() rotateY()` property, which will be handled via JavaScript to ensure the accurate calculation of the radial menu’s rotation.

Initially, we must attach a custom event to the radial menu, previously defined within the `gameface-radial-menu` element with the attribute `data-change-event-name="radialMenuItemChanged"`.

```javascript
radialMenu.addEventListener('radialMenuItemChanged', onRadialMenuItemChanged);
```

Upon triggering this event, we proceed to rotate the menu from a perspective angle, achieved through the `rotateMenuInPerspective` method.

```javascript
function onRadialMenuItemChanged() {
    const radialMenuItemsElements = radialMenu.querySelectorAll('.guic-radial-menu-item');
    rotateMenuInPerspective(radialMenuItemsElements);
}
```

The `rotateMenuInPerspective` method calculates the necessary rotation based on the selected item’s position.

```javascript
function rotateMenuInPerspective(radialMenuItemsElements) {
    const { x, y, width, height } = radialMenu.getBoundingClientRect();
    const [centerX, centerY] = getSelectedItemElementCenterPoint(radialMenuItemsElements);

    const calcX = -(centerX - x - (width / 2)) / constrain;
    const calcY = (centerY - y - (height / 2)) / constrain;
    radialMenu.style.transform = `rotateX(${calcY}deg) rotateY(${calcX}deg)`;
}
```

This function operates by first determining the bounding box of the radial menu, capturing its top-left coordinates (x, y) and dimensions (width, height). Then, it retrieves the center coordinates of the selected item through the `getSelectedItemElementCenterPoint` function.

Next, the function calculates the X and Y rotation angles necessary to position the radial menu correctly within the perspective view. This calculation involves determining the distance between the selected item’s center and the radial menu’s center, adjusting for perspective constraints.

Finally, the function applies the calculated rotation using CSS `transform` property, ensuring that the radial menu aligns appropriately with the selected item while considering perspective effects.

We referred to [this ](https://armandocanals.com/posts/CSS-transform-rotating-a-3D-object-perspective-based-on-mouse-position.html)guide to help us achieve the desired perspective view.

#### Determining the center of the selected element

To find the center of the selected item, use the `getSelectedItemElementCenterPoint` method. This method calculates the center point based on the item’s angle and adjusts for different quadrants of the radial menu.

```javascript
function getSelectedItemElementCenterPoint(radialMenuItemsElements) {
    const selectedElementDegPosition = radialMenu.segmentDegrees * radialMenu.currentSegmentId;
    const selectedItemElement = radialMenuItemsElements[radialMenu.currentSegmentId];
    const { left, right, top, bottom } = selectedItemElement.getBoundingClientRect();

    if (selectedElementDegPosition >= 0 && selectedElementDegPosition <= 90) return [right, top];
    if (selectedElementDegPosition > 90 && selectedElementDegPosition <= 180) return [right, bottom];
    if (selectedElementDegPosition > 180 && selectedElementDegPosition <= 270) return [left, bottom];
    if (selectedElementDegPosition > 270 && selectedElementDegPosition < 360) return [left, top];

    return [0, 0];
}
```

This method ensures that the `(x, y)` center coordinates are correctly obtained based on the position of the selected item. This adaptation is necessary due to the way the radial menu segments are generated, as illustrated by the item wrapper elements depicted in the image below:

For instance, to determine the center of the orange line between 0 and 90 degrees, the right-top point of its bounding box must be retrieved, as demonstrated:

Because the item wrappers span multiple quadrants of the coordinate system, different points of their bounding boxes are required to accurately locate the center of the selected item. For instance, the same orange line covers also the range between 180 and 270 degrees, necessitating the retrieval of the left-bottom point as the center:

### Update dynamically the information in the central circle

Achieving dynamic updates in the central circle is straightforward. You can accomplish this through `radialMenuItemChanged` event listener that respond to changes in the active item within the radial menu.

```javascript
function onRadialMenuItemChanged() {
    updateCenterTextValue();
    ...
}

radialMenu.addEventListener('radialMenuItemChanged', onRadialMenuItemChanged);
```

Where the `updateCenterTextValue` method does the following:

```javascript
const weaponTextElement = radialMenu.querySelector('.weapon-text');
const ammoTextElement = radialMenu.querySelector('.ammo-text');

...

function updateCenterTextValue() {
    weaponTextElement.textContent = radialMenu.items[radialMenu.currentSegmentId].name;
    ammoTextElement.textContent = radialMenu.items[radialMenu.currentSegmentId].ammo;
}
```

This script dynamically updates the central text whenever the active item changes. It changes the `textContent` of the elements with classes `.weapon-text` and `.ammo-text` that are defined inside the custom `radial-menu-center` slot that overwites the default circle background and text.

```html
<div
    slot="radial-menu-center"
    class="radial-menu-center"
>
    <div class="radial-menu-text-wrapper">
        <span class="weapon-text"></span>
        <p cohinline>
            <img
                class="ammo-icon"
                src="./assets/ammo-icon.svg"
            >
            <span class="ammo-text"></span>
        </p>
    </div>
</div>
```

For more information on how this slot works you can check the [following ](https://coherentlabs.github.io/GameUIComponents/en/components/radial-menu/#slots)section of the `radial-menu` documentation.

### Customizing default radial menu styles

Customizing the default styles of the radial menu involves several steps. Firstly, ensure that the active item is visually distinguished by adding a `selected` class. This step is essential since the radial menu may not do it automatically.

```javascript
function addSelectedClassToItem(radialMenuItemsElements) {
    radialMenu.querySelector('.guic-radial-menu-item.selected')?.classList.remove('selected');
    radialMenuItemsElements[radialMenu.currentSegmentId]?.classList.add('selected');
}

function onRadialMenuItemChanged() {
    updateCenterTextValue();

    const radialMenuItemsElements = radialMenu.querySelectorAll('.guic-radial-menu-item');

    addSelectedClassToItem(radialMenuItemsElements);
    rotateMenuInPerspective(radialMenuItemsElements);
}

radialMenu.addEventListener('radialMenuItemChanged', onRadialMenuItemChanged);
```

After ensuring the active item is visually indicated, proceed to overwrite default styles and introduce new ones tailored to your preferences.

For instance, to incorporate an arrow pointer style:

```css
.guic-radial-menu-selector {
    z-index: 2;
    clip-path: polygon(50% 25%, 48% 28%, 52% 28%) !important;
}
```

Alternatively, to modify the background of the custom center element:

```css
.radial-menu-center {
    width: 80%;
    height: 80%;
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    background-color: black;
    background-image: url(./assets/selected-border.png);
    background-size: contain;
    border-radius: 50%;

}
```

Explore further style modifications and overrides in the specified CSS file - `${Gameface package}/Samples/uiresources/UITutorials/RadialMenu/style.css`. This allows for extensive customization to match your game’s visual design.

## On this page
