Skip to content

User Interface (UI)

⚠️ Experimental feature

The UI system in the Saxion Game App is new and experimental. It is designed to integrate LibGDX's scene2d UI elements into your screens. Use it with caution — some behaviors may change in future updates.

The UI system allows you to add interactive elements on top of your game world or HUD, such as:

  • Buttons
  • Checkboxes
  • Sliders
  • Text fields
  • Select boxes
  • Labels

UI elements are rendered in their on top of the HUD (see previous article). You interact with them using keys and a simple callback system, similar to how the input handling with events works.

Skins

All UI elements in LibGDX use skins to define their appearance (colors, fonts, textures, etc.). Skins consist of a JSON file, texture atlases, and sometimes fonts. You can create your own skin with the LibGDX Skin Composer.

If you want to play with an existing skin, then please download our default skin here.

You can load skins easily in your project, in the same way as all the other resources:

// Load a skin from JSON (must include atlas references)
GameApp.addSkin("mainSkin", "skins/ui.json");

// Check if a skin is loaded
boolean hasSkin = GameApp.hasSkin("mainSkin");

// Dispose of a skin when no longer needed
GameApp.disposeSkin("mainSkin");
  • addSkin(String key, String filePath) loads a skin and stores it internally by key.
  • All UI elements reference skins by their key.

Tip: Load skins during the show() method of your screen and dispose them in hide() to save memory.

Adding UI Components

UI elements are added through GameApp, not directly on the screen. Each element requires:

  • A unique key for identification
  • A skin key for styling
  • Position and size (in HUD coordinates)

Buttons

// name, skin name, x, y, width, height, text
GameApp.addButton("startButton", "mainSkin", 100, 100, 200, 50, "Text on button");
  • "startButton" is the unique key.
  • "mainSkin" is the skin key.
  • 100, 100 are HUD coordinates (bottom-left).
  • 200, 50 are width and height in pixels.
  • "Text on button" is the text that is displayed

Checkboxes

// name, skin name, x, y, text, default value
GameApp.addCheckBox("musicToggle", "mainSkin", 50, 50, "Play music", true);
  • "Play music" is the text of the checkbox
  • Last parameter is the default checked state (true or false).

Sliders

// name, skin name, x, y, width, height, min, max, step, isVertical
GameApp.addSlider("volumeSlider", "mainSkin", 100, 150, 200, 20, 0f, 1f, 0.2f, false);
  • "volumeSlider" is name
  • "mainSkin" is skin
  • x = 100
  • y = 150
  • width = 200
  • height = 20
  • 0f, 1f are min/max values.
  • 0.2f is the step value (so this slider has 6 steps)
  • false indicates that we don't want a vertical slider (but a horizontal one)

Text Fields

Allows players to input text.

// name, skin name, x, y, width, height, intial message
GameApp.addTextField("nameInput", "mainSkin", 100, 200, 300, 40, "Type your name");
  • Last parameter determines the initial message that is shown in the input

Select Boxes

Options are provided as a string array.

// name, skin name, x, y, width, height, text, options
GameApp.addSelectBox("difficultyBox", "mainSkin", 100, 250, 200, 40, new String[]{"Easy", "Medium", "Hard"});

Labels

Non-interactive text elements.

// name, skin name, x, y, text
GameApp.addLabel("scoreLabel", "mainSkin", 500, 700, "Score: 0");

Progressbar

Very similar to the slider, but now you can't click on it:

// name, skin name, x, y, width, height, min, max, isVertical, default value
GameApp.addProgressBar("healthBar", "defaultSkin", 100, 50, 200, 20, 0, 100, false, 50);

Handling UI interactions

Whenever a user interacts with a UI element, the onUIInteraction method will be executed. You can overwrite this method in your screen (it is already available, but hidden).

@Override
public void onUIInteraction(String key, Object value) {
    switch (key) {
        case "startButton":
            GameApp.log("Start button clicked!");
            break;
        case "musicToggle":
            boolean enabled = (Boolean) value;
            GameApp.log("Music toggled: " + enabled);
            break;
        case "volumeSlider":
            float volume = (Float) value;
            GameApp.log("Volume: " + volume);
            break;
        case "nameInput":
            String text = (String) value;
            GameApp.log("Entered name: " + text);
            break;
    }
}

The parameters indicate which element was interacted with and what the new value is:

  • Checkboxes return Boolean (checked state).
  • Sliders return Float (current value).
  • Text fields return String (current content).
  • Select boxes return String (selected option).

Querying UI values

You can also query the state of UI elements at any time:

boolean musicEnabled = GameApp.getCheckBoxValue("musicToggle");
float volume = GameApp.getSliderValue("volumeSlider");
String playerName = GameApp.getTextFieldValue("nameInput");
String difficulty = GameApp.getSelectBoxValue("difficultyBox");
float health = GameApp.getProgressBarValue("healthBar");

Setting UI values

You can also update the state of UI elements programmatically at any time. For example:

// Check or uncheck a checkbox
GameApp.setCheckBoxValue("musicToggle", true);

// Change the value of a slider
GameApp.setSliderValue("volumeSlider", 0.75f);

// Update the text in a text field
GameApp.setTextFieldValue("nameInput", "PlayerOne");

// Select a value in a select box
GameApp.setSelectBoxValue("difficultyBox", "Hard");

// Update the progress bar
GameApp.setProgressBarValue("healthBar", 75);

Please note:

  • All setter methods will throw an IllegalArgumentException if the specified UI element does not exist.
  • Values should match the type of the UI element (e.g., boolean for checkboxes, float for sliders, String for text fields and select boxes).
  • After setting a value, the corresponding UI element on the HUD is immediately updated visually.

Disposing UI elements

Make sure that you dispose the UI elements when you hide the screen.

GameApp.disposeUIElements();
  • Frees memory associated with the UI elements.

Drawing UI elements

You will need to let the gameloop know when you want to render the UI elements. This can be done by calling the renderUI() method.

Please note: this automatically switches the drawing mode to HUD rendering mode (just like calling switchToHudRendering()).

@Override
public void render(float delta) {
    super.render(delta);

    // Draw the world
    ...

    // Now draw the UI elements
    renderUI();
}

Accessing raw UI elements

Sometimes you need access to the raw LibGDX Actor object behind a UI element—for example, to apply custom animations, actions, or additional listeners. GameApp provides two methods for this purpose:

Get a UI element

Actor actor = GameApp.getUIElement("myButton");
  • Retrieves the raw Actor associated with a given key.
  • Works for any UI element added via addButton, addSlider, addCheckBox, addTextField, addSelectBox, addProgressBar, or addCustomUIComponent.
  • Throws IllegalArgumentException if the element does not exist.

Check if a UI element exists

boolean exists = GameApp.hasUIElement("mySlider");
  • Returns true if a UI element with the specified key exists in the current HUD, false otherwise.
  • Useful for conditional logic before querying or modifying an element.

Adding custom UI components

If you want to add a completely custom LibGDX Actor to the HUD, you can use:

GameApp.addCustomUIComponent("myCustomActor", myActor);
  • name: unique key for the custom UI element.
  • uiComponent: the Actor instance to add to the HUD stage.
  • Once added, the element is automatically registered and can be retrieved with getUIElement or checked with hasUIElement.
  • You are responsible for setting the position, size, and behavior of the custom actor.

These methods give you full flexibility to extend the HUD beyond the standard UI elements, while still keeping all elements tracked under a unified system.