This part will focus on creating the UI for the inventory.
We will achieve this by:
Using Gameface data binding to generate and render the inventory items.
Applying CSS transforms to create the screens from the image.
Utilizing the progress bar Gameface component to display weapon stats.
Utilizing the interaction manager to allow the player to navigate through the items list using the keyboard.
Getting started - UI layout overview
To create the inventory UI, we will divide the screens into three parts:
We will create a main wrapper element for all UI parts using display flex in a row direction. Inside this wrapper, we will have two elements that together occupy 100% of the wrapper’s width.
The first element will display the weapon stats and preview screen.
The second element will use display flex in a column direction and contain two children:
The first child will display the inventory items.
The second child will display the currencies.
Setting the wrappers
The HTML representation of the above layout is as follows:
The main wrapper element is menu. Inside it, we have menu-left and menu-right to split the screen into two parts as described. Inside them, we add submenu-top and submenu-bottom elements. Since the left menu only shows weapon stats, we omit submenu-bottom. The right menu’s submenu-top shows the inventory, and submenu-bottom shows the currencies.
The wrappers will have the following styles:
As you can see, we also set the transformations for each screen. To achieve a 3D effect, we use the perspective CSS property on the menu wrapper. For the other elements, we use different transform properties to achieve the effect shown in the video or images.
To simplify the process, we used our Inspector to transform the wrappers, allowing temporary updates to the UI that are immediately visible in our standalone application called the Player. Once satisfied with the result, you can copy the CSS from the Styles tab and paste it into your CSS file.
Note: Since the Gameface view in the game created in the previous tutorial also has transformations, it is advisable to check the final result directly in the game. You can open the inspector while the game is running and use the same approach to temporarily edit the elements as with the standalone application.
Creating the holographic background for menus
To achieve the holographic effect for the menu background, follow these steps:
First, create a border as a PNG image for the element. Then, use the border-image CSS property to apply a nine-slice scaling technique, ensuring the border’s edges maintain their ratio when the wrapper element is scaled. We will use the following image:
Next, add the noise animation created earlier in the series. To ensure it stays within the border image boundaries and acts as a background, use the border-radius CSS property.
Finally, translate and scale the wrapper slightly to create an offset behind the menu wrapper, producing a depth effect.
For each submenu, add this element that is positioned absolutely to the menu. Here is an example in our HTML page:
Each background is tagged with a specific class to apply different transition animations. For example, the weapon preview background is tagged with the border-bg-weapon-preview class.
The styles are defined as follows:
This will result in a similar effect:
Weapon preview and stats menu
To create the weapon preview menu, we will design a separate submenu wrapper element that displays its children in a column layout.
Next, we will create two reusable elements within the submenu wrapper: one for the menu item title and one for the menu item container. This will help us define the layout based on images and videos.
Weapon preview
We will use the following structure in our HTML file:
We use the menu-item-title to display the text “Weapon preview” and the menu-selected-item-preview to show a preview image of the currently selected weapon. The data-bind-style-background-image-url attribute ensures that the image updates when the selected weapon changes. We will later set up all data models using mock models.
To match the submenu with the previously created background, we will set a border radius and overflow to hidden on the submenu-wrapper:
Progress bar initialization
To use the progress bar in our UI, run npm i coherent-gameface-progress-bar inside the Content/uiresources/HologramUI/ folder and then import its styles and JavaScript into the page.
Weapon stats
For the weapon stats, we will add a menu-item-title with the text “Stats” and use the menu-item-container to display all the stats. Each stat will be wrapped in a menu-item-stat element, with menu-item-stat-label displaying the stat name and menu-item-stat-value showing the stat value using the gameface progress bar or simple text via the data-bind-value attribute.
Here is a snippet demonstrating how different stats are structured in the HTML page:
We apply values from the active item in our model via data binding attributes, which we will set up later. To dynamically change the progress bar value, we will use the data-bind-progress custom attribute, which we will define later as well.
Inventory menu
To build the inventory, we will reuse some elements from the weapon stats. We’ll create a new submenu-top inside the menu-right wrapper, adding the border-bg and submenu-wrapper elements.
For displaying all items, we’ll use a grid-like layout. Each item from our data model will be rendered using the data-bind-for attribute.
Each player-item has data-bind-mouseover and data-bind-focus events. These events mark the item as active when the player hovers over or clicks on it. The onItemFocused function, defined later in our JavaScript file, handles these events.
The player-item-selected class styles the active item by changing its background. This class is toggled using the data-bind-class-toggle attribute.
When an item is active, an overlay is added to highlight it. The overlay is shown only when the item is active using the data-bind-if attribute.
The player-item-image element displays the item image, setting the background image using the data-bind-style-background-image-url attribute.
Currencies menu
The currencies menu uses the same structure as the inventory menu. We’ll create a new submenu-bottom inside the menu-right wrapper, adding the border-bg and submenu-wrapper elements.
To dynamically show the active item’s price, we’ll use the data-bind-value attribute.
Showing menu controls hints
To guide the player on navigating through the items, we’ll add hints at the bottom of the screen, positioned after the currencies wrapper.
The arrow icon is an SVG that will be rotated using the transform: rotate CSS property based on the arrow direction.
Setting up the data model
In this tutorial, we will mock our data directly in JavaScript for faster iteration. However, it is necessary to create all the models directly in the game and use the Gameface data binding to display them in the UI.
To set up the mock data model, first, we will import the cohtml.js library and the player-inventory.js file in our player-inventory.html file.
After that, we can proceed with the data model setup:
Active item selection
To retrieve the active item, we will use an observable model that will hold the currently selected item. We will create a new model called activeState and set the selectedItem to the first item in the player.items array.
Focusing the items
To focus the items, we will create a function called onItemFocused that will set the activeState.selectedItem to the item that is currently focused.
We will modify the progress bar value using the targetValue property of the element whenever the model’s value changes.
Inventory items navigation
To enable the player to navigate through the items, install the interaction manager library by running npm i coherent-gameface-interaction-manager inside the Content/uiresources/HologramUI/ folder and then import it:
Once the library is imported, initialize spatial navigation and set the focusable elements to the items in the inventory.
We also focus the first item in the inventory so the player can start navigating through the items when the menu opens.
Enabling entry animations for the menus when the player opens the inventory
When the player presses the P button in the game, it will trigger a JS event from the game to the UI as set in the previous tutorial. To handle this event and start the opening animations of our menu, we will subscribe to the openMenu engine event in the player-inventory.js file.
When the event is triggered, we remove the hide-screen class from the menu element so the menu becomes visible and animations start.
Closing the menu
As mentioned in the previous tutorial, closing the menu will be handled from the UI side. We will add a keypress event responsible for this operation. When the player presses the P button, we will close the menu by adding the hide-screen class and enabling the closing animations. Also, we will trigger an event to the engine indicating that the menu is closed.
Animations for the menu
For the closing and opening animations of our menu, we will use transitions. We will change the transform property for each screen so they are opened or closed based on whether the hide-screen class is set on the menu element.
As you can see, we added a small delay between the animations so they do not all start at the same time. This will give a better visual effect when the menu is opened or closed.