Skip to content

Embedding JOID

Your application owns the window and the loop, and often draws other things than UIs: a game scene, a viewport, a video. This page fits JOID into that loop without taking it over.

You will learn

  • Where frame() goes in your loop, and what it leaves untouched
  • How to keep your own input handlers next to JOID
  • How to draw JOID over a scene of your own and follow the screens it opens
  • How to draw a UI inside your own projection
  • How to add a UI bridge of your own and order the bridges with priorities

Your application is the host: it owns the window and the loop, and JOID runs inside them. JOID reaches your window through bridges, small interfaces that the backend fills in. The one you meet on this page is the UI bridge: it holds the open UIs, routes the input to them and draws them every frame. The backend registers one, the window UI bridge, and you can register more.

A loop that shows JOID polls the window events, calls frame() of the backend, then shows the frame, as the loop of Choosing a backend does. One call to Backend.frame follows the size of the window, updates the open UIs of every UI bridge, clears the window, then draws the bridges from the lowest priority to the highest. The frame lifecycle details each step.

On the OpenGL backends, the frame gives back the OpenGL state it found, so your own drawing code before and after frame() runs unchanged. After the loop, closeAll() of each UI bridge closes the UIs still open.

Call frame() from the thread that registered the backend: Threads explains how other threads hand their work to it. UIBridgeRegistry.onFrame of BridgeHandler.UI runs a task of yours at the start of every frame, before the UIs update. Outside a UI, BridgeHandler.WINDOW.get() returns the window as JOID reads it, an IWindowBridge: the size of the window in screen pixels, the position of the mouse, the keys held, and the clipboard of the system.

On LWJGL 3 and Vulkan, register() attaches a GlfwInputForwarder to the window, which installs the GLFW callbacks. Each event goes to the UIs first; an event that no UI consumes goes on to the callback your application set before register():

GLFW.glfwSetKeyCallback(window, (handle, key, scancode, action, mods) -> {
if (key == GLFW.GLFW_KEY_ESCAPE && action == GLFW.GLFW_PRESS) {
GLFW.glfwSetWindowShouldClose(handle, true);
}
});

Full file: HostApp.java

Here, Escape closes the window only when no UI used it, for example to close a screen. To compare a position of your own with the nodes of a UI, UI.getView converts between screen pixels and design pixels (Convert between the window and the UI). The same rule holds for releases: when a UI consumed a press, your callback receives neither the press nor its release, and when your callback received a press, it receives its release. An overlay that receives the input is the exception: cancelClick, cancelScroll and cancelKeyboard of UIDataOverlayInteraction decide whether an event it used still reaches your callback (Overlays and popups).

With LWJGL 2, poll() of Lwjgl2InputForwarder empties the Mouse and Keyboard queues. To share them with your code, read the queues yourself and call Lwjgl2InputForwarder.forwardMouseEvent after each Mouse.next() and Lwjgl2InputForwarder.forwardKeyboardEvent after each Keyboard.next(): each returns true when a UI consumed the event, and your code handles only the others.

The window UI bridge clears the window before every frame, so it would erase a scene you drew before frame(). Register a bridge that keeps the colors and clears only the depth and the stencil:

package com.example.running;
import dev.joid.lib.bridge.BridgeHandler;
import dev.joid.lib.bridge.render.IRenderBridge;
import dev.joid.lib.bridge.ui.WindowUIBridge;
public class SceneUIBridge extends WindowUIBridge {
protected SceneUIBridge() {}
public static SceneUIBridge create() {
return new SceneUIBridge();
}
@Override
protected void drawBackground() {
final IRenderBridge render = BridgeHandler.RENDER.get();
render.clearDepth();
render.clearStencil();
}
}

Register it before the backend: the backend then adds no window UI bridge of its own. Give it a text style for the tooltips once the backend is registered, so that the font loads after the folders apply:

final SceneUIBridge bridge = SceneUIBridge.create();
BridgeHandler.UI.register(bridge);
final Lwjgl3Backend backend = Lwjgl3Backend.create(window).register();
bridge.hoverInfo(TextInfo.create(ExampleFont.REGULAR, 20F).color(HostApp.TEXT));

Full file: HostApp.java

Draw your scene, then let JOID draw over it:

final HostScene scene = new HostScene();
JOID.open(new HostOverlayUI());
double last = GLFW.glfwGetTime();
while (!GLFW.glfwWindowShouldClose(window)) {
GLFW.glfwPollEvents();
final double now = GLFW.glfwGetTime();
scene.update(now - last);
last = now;
scene.draw(window);
backend.frame();
GLFW.glfwSwapBuffers(window);
}
bridge.closeAll();

Full file: HostApp.java

HostScene stands for your scene: it fills the window and slides a square with plain OpenGL calls.

The complete HostScene.java
package com.example.running;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.opengl.GL11;
import dev.joid.lib.color.Color;
public class HostScene {
private static final Color ACCENT = new Color(91, 140, 255).lock();
private static final Color BACKGROUND = new Color(14, 16, 20).lock();
private double time;
public void update(final double seconds) {
this.time += seconds;
}
public void draw(final long window) {
final int[] width = new int[1];
final int[] height = new int[1];
GLFW.glfwGetFramebufferSize(window, width, height);
final int x = (int) Math.round(400D + 280D * Math.sin(this.time * 2D));
HostScene.fill(0, 0, width[0], height[0], HostScene.BACKGROUND);
HostScene.fill(x, height[0] - 600, 160, 160, HostScene.ACCENT);
}
private static void fill(final int x, final int y, final int width, final int height, final Color color) {
GL11.glEnable(GL11.GL_SCISSOR_TEST);
GL11.glScissor(x, y, width, height);
GL11.glClearColor(color.getRedF(), color.getGreenF(), color.getBlueF(), 1F);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL11.glDisable(GL11.GL_SCISSOR_TEST);
}
}

HostOverlayUI is an overlay (UIDataOverlay) that receives the input, so its Tab shortcut opens HostMenuUI, a screen:

package com.example.running;
import com.example.ExampleFont;
import dev.joid.lib.JOID;
import dev.joid.lib.color.Color;
import dev.joid.lib.draw.text.Text;
import dev.joid.lib.font.TextInfo;
import dev.joid.lib.input.key.Key;
import dev.joid.lib.ui.core.UI;
import dev.joid.lib.ui.core.data.UIDataOverlay;
import dev.joid.lib.ui.core.data.UIDataOverlayInteraction;
import dev.joid.lib.ui.node.impl.shape.RectNode;
import dev.joid.lib.ui.node.impl.text.TextNode;
import dev.joid.lib.ui.shortcut.Shortcut;
import dev.joid.lib.utils.align.Align;
@UIDataOverlay(active = true, interaction = @UIDataOverlayInteraction(active = true))
public class HostOverlayUI extends UI {
private static final Color TEXT = new Color(231, 233, 238).lock();
private static final Color ELEMENT = new Color(38, 43, 53).lock();
@Override
public void init() {
final TextInfo info = TextInfo.create(ExampleFont.REGULAR, 24F).color(HostOverlayUI.TEXT);
Shortcut.create(Key.TAB).onTrigger(event -> JOID.open(new HostMenuUI())).attach(this);
RectNode
.create(40, 40, 220, 60)
.color(HostOverlayUI.ELEMENT)
.body(rect -> {
TextNode
.create(0, 0, 220, 60)
.text(Text.create("Tab: menu", info).align(Align.CENTER))
.attach(rect);
})
.attach(this);
}
}

It sets no background in UIData, so the scene shows around its hint; a UI backdrop (background = true) covers the whole window and hides the scene. SceneUIBridge extends WindowUIBridge, so it keeps the tooltips and every setting of the window UI bridge. On Vulkan, the frame of JOID loads the image of the host without clearing it: the scene is what the host recorded into its command buffer before frame().

Your application can also hide every overlay of JOID, for a screenshot or a cutscene: override isOverlayHidden() of UIBridge in your bridge and return true while the interface must disappear. Overlays with always = true in UIDataOverlayRender stay visible. To make your UIs larger or smaller, such as for a scale option of your settings, override getInterfaceScale(ui) in your bridge and return the interface scale of each UI; the window UI bridge returns 1D.

A game pauses its scene and frees the mouse it captures while a menu of JOID is open. Your bridge follows the screens of JOID with two hooks of StackUIBridge, attachScreen() and detachScreen():

package com.example.running;
import org.lwjgl.glfw.GLFW;
public class GameUIBridge extends SceneUIBridge {
private final long window;
private boolean paused;
protected GameUIBridge(final long window) {
this.window = window;
}
public static GameUIBridge create(final long window) {
return new GameUIBridge(window);
}
public boolean isPaused() {
return this.paused;
}
@Override
protected void attachScreen() {
this.paused = true;
GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_NORMAL);
}
@Override
protected void detachScreen() {
this.paused = false;
GLFW.glfwSetInputMode(this.window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED);
}
}

JOID calls attachScreen() when the first screen or popup of the bridge opens, and detachScreen() when the last one closes. Overlays do not count, so HostOverlayUI never pauses the game, and a screen that replaces another one calls neither hook.

GameUIBridge extends SceneUIBridge, so the scene stays under the UIs. GameApp registers it in place of SceneUIBridge, and captures the mouse for its game:

final GameUIBridge bridge = GameUIBridge.create(window);
BridgeHandler.UI.register(bridge);
final Lwjgl3Backend backend = Lwjgl3Backend.create(window).register();
GLFW.glfwSetInputMode(window, GLFW.GLFW_CURSOR, GLFW.GLFW_CURSOR_DISABLED);

Full file: GameApp.java

In the loop, the game skips its update while isPaused() returns true:

if (!bridge.isPaused()) {
scene.update(now - last);
}

Full file: GameApp.java

The scene of the host shows around the overlay and under the screen. Only the screen pauses the game and frees the mouse.
The complete HostMenuUI.java
package com.example.running;
import com.example.ExampleFont;
import dev.joid.lib.color.Color;
import dev.joid.lib.draw.text.Text;
import dev.joid.lib.font.TextInfo;
import dev.joid.lib.ui.core.UI;
import dev.joid.lib.ui.node.effect.impl.BorderNodeEffect;
import dev.joid.lib.ui.node.effect.impl.RoundedNodeEffect;
import dev.joid.lib.ui.node.impl.shape.RectNode;
import dev.joid.lib.ui.node.impl.text.TextNode;
import dev.joid.lib.utils.align.Align;
public class HostMenuUI extends UI {
private static final Color TEXT = new Color(231, 233, 238).lock();
private static final Color ELEMENT = new Color(38, 43, 53).lock();
private static final Color SURFACE = new Color(23, 26, 33).lock();
private static final Color MUTED = new Color(139, 147, 163).lock();
@Override
public void init() {
final TextInfo title = TextInfo.create(ExampleFont.REGULAR, 32F).color(HostMenuUI.TEXT);
final TextInfo hint = TextInfo.create(ExampleFont.REGULAR, 22F).color(HostMenuUI.MUTED);
RectNode
.create(300, 140, 480, 200)
.color(HostMenuUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(HostMenuUI.ELEMENT, 1.5F))
.body(card -> {
TextNode.create(0, 40, 480, 50).text(Text.create("Menu", title).align(Align.CENTER)).attach(card);
TextNode
.create(0, 110, 480, 40)
.text(Text.create("Escape: back to the game", hint).align(Align.CENTER))
.attach(card);
})
.attach(this);
}
}

JOID draws each UI with its own orthographic projection over the window. @UIData(projection = false) keeps the projection already in place on the render bridge (IRenderBridge.getProjection) instead: a host that sets a perspective there draws the UI inside its 3D scene. zlevel of UIData adds a depth offset before the UI draws, so that the UIs of that scene keep their order under the depth test.

A second UI bridge gives a group of UIs its own place in the window, with its own routing and tooltips. For example, a library shows its panels above every UI of the application. Each bridge has a priority, an int: the highest priority receives the input first and is drawn on top.

Three stacked dark boxes: PanelUIBridge with priority 10 on top, your bridge with priority 0 in the middle, and WindowUIBridge, the default that clears the window, at the bottom. A blue arrow on the left goes down, labeled Input; a blue arrow on the right goes up, labeled Drawing.

Input tries the highest bridge first; drawing starts from the lowest, so the highest is drawn on top.

The bridge decides which UIs it hosts with canHandle:

package com.example.running;
import dev.joid.lib.bridge.ui.WindowUIBridge;
import dev.joid.lib.ui.core.UI;
public class PanelUIBridge extends WindowUIBridge {
protected PanelUIBridge() {}
public static PanelUIBridge create() {
return new PanelUIBridge();
}
@Override
public boolean canHandle(final Class<? extends UI> clazz) {
return RunningPanelUI.class.isAssignableFrom(clazz);
}
}

The library registers it with a priority, once JOID is ready, and gives it its own tooltip style:

public static void install() {
JOID.onReady(() -> {
final PanelUIBridge panels = PanelUIBridge.create();
panels.hoverInfo(TextInfo.create(ExampleFont.REGULAR, 20F).color(PanelPlugin.TEXT));
BridgeHandler.UI.register(panels, 10);
JOID.open(new RunningPanelUI());
});
}

Full file: PanelPlugin.java

The UI registry, BridgeHandler.UI, orders its bridges by priority:

  • BridgeRegistry.register without a priority uses 0. Between equal priorities, the latest registered counts as the highest, and registering a bridge again moves it.
  • The backend adds its window UI bridge only into an empty registry, and below every priority, so any bridge you register later sits above it.
  • JOID.open gives the UI to the highest bridge whose canHandle accepts it. An open UI stays with the bridge that holds it, whatever you register afterwards.
  • Input goes to the bridges from the highest and stops at the first one that consumes it. A key release goes to every bridge, and a mouse release to every bridge that received the press.
  • Only the lowest bridge clears the window, so the bridges above it never clear the window background.
  • Each bridge draws its own tooltips with its own hoverInfo, and the window shows the cursor of the hovered node of the highest bridge that has one, the default arrow when that node and its parents set none.

BridgeRegistry.getBridgeList lists the bridges from the lowest to the highest priority, and BridgeRegistry.unregister removes one. Writing the contract of a UI bridge of your own belongs to the Build on JOID documentation.

  • Call frame() once per frame, between polling the events and showing the frame.
  • An event that no UI consumes reaches the GLFW callbacks you set before register().
  • To draw JOID over your own scene, register before the backend a bridge whose drawBackground() clears only the depth and the stencil.
  • attachScreen() and detachScreen() tell your bridge when the first JOID screen or popup opens and when the last one closes.
  • projection = false draws a UI inside the projection of the host, and zlevel keeps the UIs of that scene in order.
  • register(bridge, priority): the highest priority receives input first and is drawn on top; equal priorities favor the latest.
  • The window UI bridge of the backend is a default: it sits below every bridge you register and clears the window.
  • canHandle decides which bridge receives a UI; an open UI stays with its bridge.

Panels under the game. The application registers a UI bridge of its own with register(game), and opens its UIs there. Your panels must stay below them. Which priority do you give the bridge of the panels?

Solution

A negative one, such as BridgeHandler.UI.register(panels, -10): the bridge of the application has the priority 0, so the panels draw under it and receive the input after it. No priority goes below the default window UI bridge, so panels can never sit under the UIs it holds.

Replace a screen. GameUIBridge pauses the game when the menu opens. The player opens the settings from the menu, which replaces the menu, then closes them: is the game still paused?

Solution

No. Replacing the menu by the settings calls neither hook, so the game stays paused while the settings are open, and closing the last screen calls detachScreen(), which resumes it.

Report a problem with this page