Skip to content

Pop up Menus UI Display and Action Handling

default-jamc edited this page Aug 30, 2021 · 3 revisions

Purpose

Pop-up menus throughout the game function similarly regardless of their reason for appearing. To minimise duplicate code and encourage modularity, two classes were built to manage creating the UI elements and on-click actions common to all, or many menus.

Introduction

The display of the UI and control of actions common to all menus is handled by the UIPopupHandler and PopupMenuActions classes. The UIPopupHandler class controls the setting up of the menu background, buttons and triggers on the buttons. The PopupMenuActions class controls actions common to all menus (such as returning to the Main Menu and Replaying the level).

Implementation & Usage

UI Handler

When a new PopupMenu is added to a UI, it should be passed a new PopupUIHandler. The PopupUIHandler constructor takes a String[] textures as an argument which contains the filepaths of the menu's background and buttons to be placed onto the pop-up menu. This textures object must have been loaded by the resource service (ServiceLocator.getResourceService()) prior to being passed to the PopupUIHandler. textures must be ordered as:

  1. the background image for the menu
  2. the button(s) you would like to place onto the menu.

Once the pop-up menu appears, a _Display.java class should be created as part of the pop-up's UI. This class will call on methods from the PopupUIHandler. For example, in the PlayerWinDisplay addActors method:

// Set up the background image
Table background = new Table();
handler.setupBackground(background);

// Place the buttons onto the UI.
Table buttonHolder = new Table();
ArrayList<Image> button = handler.setupButtons(buttonHolder, 125, 15);

// Set up the on-click triggers for the buttons.
final String[] actions = {"replayLevel","homeMenu","continue"};
handler.setupButtonClicks(buttons, actions, entity);

// Add to the stage to display the pop-up menu
stage.addActor(backgroundFrame);
stage.addActor(buttonHolder);

Action Handler

PopupMenuActions must be added as a Component to the over-arching UI. As the PopupMenus are also Components of the UI, they are able to access the same Entity object. For example, in the MainGameScreen createUI method:

Entity ui = new Entity();
ui.addComponent(new PopUpMenu(this.game, new PopupUIHandler));
  .addComponent(new PopupMenuActions(this.game));

When the pop-up menu's UI is created, an _Actions.java class should be created as part of the UI. This class is passed a reference to the over-arching UI. For example, in the PlayerWinPopup createUI method:

Entity ui = new Entity();
ui.addComponent(new PlayerWinActions(game, upperUI))
  .addCompoonent(new PlayerWinDisplay(handler));

For common functionality, these Action classes will call on the PopupMenuActions class. For example, in the PlayerWinActions create() method:

// Functionality which is common to all pop-up menus
entity.getEvents().addListener("replayLevel", this.mainGameUI.getComponent(PopupMenuActions.class)::onReplay); 

// Functionality on this menu only
entity.getEvents().addListener("continue", this::onContinue); 

Table of Contents

Home

Introduction

Main Menu

Main Game Screen

Gameplay

Player Movement

Character Animations

Enemy Monster Design and Animations

Game basic functionalities

User Testing

GitHub Wiki Tutorial

Game Engine

Getting Started

Documentation

Entities and Components

Service Locator

Loading Resources

Logging

Unit Testing

Debug Terminal

Input Handling

UI

Animations

Audio

AI

Physics

Game Screens and Areas

Terrain

Concurrency & Threading

Settings

Troubleshooting

MacOS Setup Guide

Clone this wiki locally