Keep the state
In this lesson Play starts a round, and the whole panel follows: the number of games, the best score, the total and the list of recent rounds.
Starting point
Section titled “Starting point”The program of the previous lesson reacts to the mouse and the keyboard, but Play does nothing. The numbers of the panel are written in the code, so they can never change.
1. Hold the rounds in a signal
Section titled “1. Hold the rounds in a signal”Add these fields to MenuUI:
private final Random random = new Random(7L);private final ListSignal<Integer> rounds = new ListSignal<>();private final ComputedSignal<Integer> best = this.rounds.map(list -> list.stream().reduce(0, Math::max));private final ComputedSignal<Integer> total = this.rounds.map(list -> list.stream().reduce(0, Integer::sum));roundsis aListSignal: a list that tells JOID when it changes.totalandbestare derived fromroundswithmap(...): they compute their value from the list, and compute it again when the list changes.randomdraws the points of a round. Its fixed seed gives the same rounds at every run, so your panel shows the numbers of the capture above.
Nothing reads these fields yet, so the menu looks the same.
2. Start a round on Play
Section titled “2. Start a round on Play”Give Play an onClick that adds a round of 100 to 999 points:
RectNode.create(0, 0, 360, 80).color(Theme.ELEMENT).hoveredColor(Theme.HOVER).onClick(event -> this.rounds.add(this.random.nextInt(900) + 100)).label(Text.create("Play", label)).attach(flex);Play now adds rounds, but nothing shows them yet: the panel still has fixed texts.
3. Show the numbers from the signals
Section titled “3. Show the numbers from the signals”Build the value of each card from the signals instead of a fixed text:
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(String.valueOf(this.rounds.size()), 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(String.valueOf(this.best.get()), 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(String.valueOf(this.total.get()), value)).attach(card); }) .attach(grid);}).attach(panel);Text.create(String.valueOf(this.best.get()), value) reads the signal best, and this.rounds.size() reads rounds in the same way.
JOID records that this text depends on it, and builds the text again each time best changes: Signals explains how.
Run it. The three cards start at 0. Click Play: they update at each click, while the list still shows its fixed rows.
4. Rebuild the list of rounds
Section titled “4. Rebuild the list of rounds”A list does not hold one value: it holds as many rows as there are rounds.
watch(...) rebuilds the children of a node when a signal changes:
FlexNode.vertical(48, 390, 904).margin(12D).watch(this.rounds, WatchProperty.CLEAR_CHILDREN, WatchProperty.BODY).body(flex -> { final List<Integer> list = this.rounds.peek(); if (list.isEmpty()) { TextNode.create(0, 0).text(Text.create("Press Play to start a round", caption)).attach(flex); }
for (int index = list.size() - 1; index >= Math.max(0, list.size() - 4); index--) { final String[] round = {"Round " + (index + 1), String.valueOf(list.get(index))}; 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);WatchProperty.CLEAR_CHILDRENremoves the rows, thenWatchProperty.BODYruns the body again.- The body reads the list with
peek(), which reads without following: thewatchalready runs the body again at each change (Read a value without following it). - It shows the four latest rounds, the newest first, or a hint when there is none.
Run it. Click Play a few times: each click adds a row at the top of the list and updates the three cards.
Close the program and run it again: the rounds are gone, since they live in memory only. Open the settings: the music and the volume are still fixed texts. The next lesson stores the settings.
The complete MenuUI.java
package com.example.starfall;
import java.util.List;import java.util.Random;
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.signal.ComputedSignal;import dev.joid.lib.signal.impl.iterable.ListSignal;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.node.property.WatchProperty;import dev.joid.lib.ui.shortcut.Shortcut;import dev.joid.lib.utils.align.Align;
public class MenuUI extends UI {
private final Random random = new Random(7L); private final ListSignal<Integer> rounds = new ListSignal<>(); private final ComputedSignal<Integer> best = this.rounds.map(list -> list.stream().reduce(0, Math::max)); private final ComputedSignal<Integer> total = this.rounds.map(list -> list.stream().reduce(0, Integer::sum));
@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) .onClick(event -> this.rounds.add(this.random.nextInt(900) + 100)) .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(String.valueOf(this.rounds.size()), 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(String.valueOf(this.best.get()), 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(String.valueOf(this.total.get()), 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) .watch(this.rounds, WatchProperty.CLEAR_CHILDREN, WatchProperty.BODY) .body(flex -> { final List<Integer> list = this.rounds.peek(); if (list.isEmpty()) { TextNode.create(0, 0).text(Text.create("Press Play to start a round", caption)).attach(flex); }
for (int index = list.size() - 1; index >= Math.max(0, list.size() - 4); index--) { final String[] round = {"Round " + (index + 1), String.valueOf(list.get(index))}; 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”Let the player start a round with the P key.
Solution
Add a second shortcut next to the one of S:
Shortcut.create(Key.P).onTrigger(event -> this.rounds.add(this.random.nextInt(900) + 100)).attach(this);The shortcut changes the same signal as the button, so the panel follows in the same way: nothing else to write.
What you learned
Section titled “What you learned”- A signal holds a value, and a node set from an expression that reads it follows it.
map(...)derives a signal from another (Derived values).watch(...)rebuilds the children of a node when a signal changes (Rebuilding the tree).- You change the data, never the nodes (How JOID works).
