Radial menus in Gameface
5/13/2024
Kaloyan Geshev
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 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:
- Utilized the
radial-menu
component from the Gameface components suite. - Customized some default styles of the components to achieve the effects seen in the first image.
- Added appealing borders and animations when an item is selected.
- Modified the highlight of the currently selected item to resemble an arrow rather than a background.
- Implemented logic to rotate the radial menu with perspective effect based on the selected item.
- 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.
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:
- Apply a
perspective
CSS property to theradial-menu-wrapper
, allowing for perspective rotation. - 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:
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"
.
Upon triggering this event, we proceed to rotate the menu from a perspective angle, achieved through the rotateMenuInPerspective
method.
The rotateMenuInPerspective
method calculates the necessary rotation based on the selected item’s position.
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 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.
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.
Where the updateCenterTextValue
method does the following:
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.
For more information on how this slot works you can check the following 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.
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:
Alternatively, to modify the background of the custom center element:
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.