React to the player
In this lesson the menu reacts: buttons light up under the pointer, Settings opens a popup, S opens it from the keyboard, the statistic cards explain themselves, and Quit ends the program.
Starting point
Section titled “Starting point”The program of the previous lesson shows the whole menu, but nothing reacts. The buttons keep their color under the pointer, a click does nothing, and the only way out is to close the window.
1. Write the settings popup
Section titled “1. Write the settings popup”Add a lighter grey, HOVER, to the palette of Theme, for the buttons under the pointer:
package com.example.starfall;
import dev.joid.lib.color.Color;import dev.joid.lib.font.impl.msdf.MsdfFont;import dev.joid.lib.font.impl.msdf.MsdfFontLoader;
public final class Theme {
public static final MsdfFont FONT = MsdfFontLoader.load( "fonts/Montserrat/Montserrat-Regular.ttf", "fonts/Montserrat/Montserrat-Bold.ttf" ).join();
public static final Color HOVER = new Color(54, 61, 77).lock(); public static final Color TEXT = new Color(231, 233, 238).lock(); public static final Color ELEMENT = new Color(38, 43, 53).lock(); public static final Color SURFACE = new Color(23, 26, 33).lock();
private Theme() {}
}Settings opens a second UI above the menu.
The annotation UIDataPopup, @UIDataPopup(active = true), makes SettingsUI a popup, and the option background = true of UIData darkens the menu behind it with a veil, here black at 50 % opacity (backgroundColor = "#00000080"):
package com.example.starfall;
import dev.joid.lib.JOID;import dev.joid.lib.draw.text.Text;import dev.joid.lib.font.FontWeight;import dev.joid.lib.font.TextInfo;import dev.joid.lib.ui.core.UI;import dev.joid.lib.ui.core.data.UIData;import dev.joid.lib.ui.core.data.UIDataPopup;import dev.joid.lib.ui.node.impl.shape.RectNode;import dev.joid.lib.ui.node.impl.text.TextNode;
@UIDataPopup(active = true)@UIData(background = true, backgroundColor = "#00000080")public class SettingsUI extends UI {
@Override public void init() { final TextInfo heading = TextInfo.create(Theme.FONT, 40F).weight(FontWeight.BOLD).color(Theme.TEXT); final TextInfo caption = TextInfo.create(Theme.FONT, 30F).color(Theme.TEXT); final TextInfo label = TextInfo.create(Theme.FONT, 30F).color(Theme.TEXT);
RectNode .create(660, 290, 600, 500) .color(Theme.SURFACE) .body(card -> { TextNode.create(48, 40).text(Text.create("Settings", heading)).attach(card); TextNode.create(48, 150).text(Text.create("Music: on", caption)).attach(card); TextNode.create(48, 260).text(Text.create("Volume: 80 %", caption)).attach(card); RectNode .create(48, 380, 504, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> JOID.close(this)) .label(Text.create("Close", label)) .attach(card); }) .attach(this); }
}The Close button closes the popup with JOID.close(this), and a popup also closes when the player presses Escape.
Nothing opens it yet, so the menu looks the same.
2. Make the buttons react
Section titled “2. Make the buttons react”Give each button a hoveredColor, the color it turns to while the pointer is over it.
Then give Settings an onClick that opens the popup, and Quit one that closes the menu:
FlexNode.vertical(160, 420, 360).margin(20D).body(flex -> { RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .label(Text.create("Play", label)) .attach(flex); RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> JOID.open(new SettingsUI())) .label(Text.create("Settings", label)) .attach(flex); RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> JOID.close(this)) .label(Text.create("Quit", label)) .attach(flex);}).attach(this);Run it. Hover a button: it lights up. Click Settings: the popup grows into view over the darkened menu. Click Close or press Escape: it shrinks away and the menu is back.
3. Open the popup with a key
Section titled “3. Open the popup with a key”A Shortcut runs code when the player presses a key.
Add it at the start of init() in MenuUI, after the styles:
Shortcut.create(Key.S).onTrigger(event -> JOID.open(new SettingsUI())).attach(this);attach(this) gives the shortcut to the menu, so it works while the menu receives the keyboard.
Run it. Press S: the settings open. Press S again: nothing happens, because the keys of the player go to the popup, not to the menu under it.
4. Explain the cards with tooltips
Section titled “4. Explain the cards with tooltips”Give each statistic card a tooltip with hover(...):
GridNode.create(48, 120, 904, 150).margin(32D).body(grid -> { RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .hover(() -> "Rounds you played") .body(card -> { TextNode.create(24, 24).text(Text.create("Games", caption)).attach(card); TextNode.create(24, 64).text(Text.create("3", value)).attach(card); }) .attach(grid); RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .hover(() -> "Your highest score") .body(card -> { TextNode.create(24, 24).text(Text.create("Best", caption)).attach(card); TextNode.create(24, 64).text(Text.create("640", value)).attach(card); }) .attach(grid); RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .hover(() -> "Every point you scored") .body(card -> { TextNode.create(24, 24).text(Text.create("Total", caption)).attach(card); TextNode.create(24, 64).text(Text.create("1370", value)).attach(card); }) .attach(grid);}).attach(panel);The window UI bridge, the part of the backend that shows your UIs in the window, draws the tooltips, and it needs a text style for them, because JOID ships no font.
Set it in Main, right after register():
final WindowUIBridge bridge = BridgeHandler.UI.getBridge(WindowUIBridge.class);bridge.hoverInfo(TextInfo.create(Theme.FONT, 24F).color(Theme.TEXT));Run it. Rest the pointer on a card: its tooltip shows next to the pointer.
5. Quit the program
Section titled “5. Quit the program”Quit closes the menu, but the loop of Main keeps running on an empty window.
Stop the loop when no menu is open anymore:
package com.example.starfall;
import org.lwjgl.glfw.GLFW;import org.lwjgl.glfw.GLFWErrorCallback;import org.lwjgl.opengl.GL;
import dev.joid.backend.lwjgl3.GlContextRequest;import dev.joid.backend.lwjgl3.Lwjgl3Backend;import dev.joid.lib.JOID;import dev.joid.lib.bridge.BridgeHandler;import dev.joid.lib.bridge.ui.WindowUIBridge;import dev.joid.lib.font.TextInfo;
public final class Main {
private Main() {}
public static void main(final String[] arguments) { GLFWErrorCallback.createPrint(System.err).set(); if (!GLFW.glfwInit()) { throw new IllegalStateException("Unable to initialize GLFW"); }
GlContextRequest.CORE_33.apply(); final long window = GLFW.glfwCreateWindow(1280, 720, "Starfall", 0L, 0L); if (window == 0L) { throw new IllegalStateException("Unable to create the window"); }
GLFW.glfwMakeContextCurrent(window); GL.createCapabilities(); final Lwjgl3Backend backend = Lwjgl3Backend.create(window).register(); final WindowUIBridge bridge = BridgeHandler.UI.getBridge(WindowUIBridge.class); bridge.hoverInfo(TextInfo.create(Theme.FONT, 24F).color(Theme.TEXT));
JOID.open(new MenuUI()); while (!GLFW.glfwWindowShouldClose(window) && JOID.isOpen(MenuUI.class)) { GLFW.glfwPollEvents(); backend.frame(); GLFW.glfwSwapBuffers(window); }
bridge.closeAll(); GLFW.glfwDestroyWindow(window); GLFW.glfwTerminate(); }
}The highlighted lines are every change to Main in this lesson, the tooltip style of step 4 included.
JOID.isOpen(MenuUI.class) tells whether a UI of that class is open.
A screen also closes on Escape, so Escape quits the game from the menu.
Run it. Hover the buttons and the cards, open and close the settings with the mouse and with S, then click Quit: the window closes.
Play still does nothing, and the numbers of the panel never change: the next lesson gives them data.
The complete MenuUI.java
package com.example.starfall;
import dev.joid.lib.JOID;import dev.joid.lib.draw.text.Text;import dev.joid.lib.font.FontWeight;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.node.impl.layout.FlexNode;import dev.joid.lib.ui.node.impl.layout.GridNode;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;
public class MenuUI extends UI {
@Override public void init() { final TextInfo title = TextInfo.create(Theme.FONT, 120F).weight(FontWeight.BOLD).color(Theme.TEXT); final TextInfo tagline = TextInfo.create(Theme.FONT, 36F).color(Theme.TEXT); final TextInfo label = TextInfo.create(Theme.FONT, 36F).color(Theme.TEXT); final TextInfo heading = TextInfo.create(Theme.FONT, 40F).weight(FontWeight.BOLD).color(Theme.TEXT); final TextInfo caption = TextInfo.create(Theme.FONT, 26F).color(Theme.TEXT); final TextInfo value = TextInfo.create(Theme.FONT, 56F).weight(FontWeight.BOLD).color(Theme.TEXT);
Shortcut.create(Key.S).onTrigger(event -> JOID.open(new SettingsUI())).attach(this);
TextNode.create(160, 160).text(Text.create("Starfall", title)).attach(this);
TextNode.create(164, 310).text(Text.create("Catch the falling stars", tagline)).attach(this);
FlexNode .vertical(160, 420, 360) .margin(20D) .body(flex -> { RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .label(Text.create("Play", label)) .attach(flex); RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> JOID.open(new SettingsUI())) .label(Text.create("Settings", label)) .attach(flex); RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> JOID.close(this)) .label(Text.create("Quit", label)) .attach(flex); }) .attach(this);
RectNode .create(760, 160, 1000, 760) .color(Theme.SURFACE) .body(panel -> { TextNode.create(48, 40).text(Text.create("Profile", heading)).attach(panel); GridNode .create(48, 120, 904, 150) .margin(32D) .body(grid -> { RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .hover(() -> "Rounds you played") .body(card -> { TextNode.create(24, 24).text(Text.create("Games", caption)).attach(card); TextNode.create(24, 64).text(Text.create("3", value)).attach(card); }) .attach(grid); RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .hover(() -> "Your highest score") .body(card -> { TextNode.create(24, 24).text(Text.create("Best", caption)).attach(card); TextNode.create(24, 64).text(Text.create("640", value)).attach(card); }) .attach(grid); RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .hover(() -> "Every point you scored") .body(card -> { TextNode.create(24, 24).text(Text.create("Total", caption)).attach(card); TextNode.create(24, 64).text(Text.create("1370", value)).attach(card); }) .attach(grid); }) .attach(panel); TextNode.create(48, 320).text(Text.create("Recent rounds", heading)).attach(panel); FlexNode .vertical(48, 390, 904) .margin(12D) .body(flex -> { final String[][] rounds = {{"Round 3", "412"}, {"Round 2", "640"}, {"Round 1", "318"}}; for (final String[] round : rounds) { RectNode .create(0, 0, 904, 72) .color(Theme.ELEMENT) .body(row -> { TextNode .create(24, row.dh(2D)) .text(Text.create(round[0], caption)) .anchorY(Align.CENTER) .attach(row); TextNode .create(row.aw(-24D), row.dh(2D)) .text(Text.create(round[1], caption)) .anchorX(Align.END) .anchorY(Align.CENTER) .attach(row); }) .attach(flex); } }) .attach(panel); }) .attach(this); }
}Challenge
Section titled “Challenge”Show the tooltip “Escape quits too” on the Quit button.
Solution
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).hoveredColor(Theme.HOVER).onClick(event -> JOID.close(this)).hover(() -> "Escape quits too").label(Text.create("Quit", label)).attach(flex);Any node takes a tooltip, a button as well as a card.
What you learned
Section titled “What you learned”hoveredColor,onClickandhoverare the most common reactions of a node (Events, Hover, tooltips and cursors).- A popup is a UI opened above another with
JOID.open, and closed withJOID.closeor Escape (Overlays and popups). - A shortcut runs code on a key press, for the UI that receives the keyboard (Keyboard and shortcuts).
- The window UI bridge draws tooltips once it has a text style (Choosing a backend).
