Save the settings
In this lesson the settings popup works: the player turns the music on and off and sets the volume, the menu shows the result, and both survive a restart.
Starting point
Section titled “Starting point”The program of the previous lesson fills the panel from a signal. The settings popup still shows fixed texts, and nothing remains when the program stops.
1. Write a permanent store
Section titled “1. Write a permanent store”A store holds values that outlive a UI.
Its scope decides how long: PERMANENT keeps it for the whole run, shares it between every UI that asks for it, and saves it to a file.
Create SettingsStore.java:
package com.example.starfall;
import java.util.LinkedHashMap;import java.util.Map;
import dev.joid.lib.signal.impl.primitive.BooleanSignal;import dev.joid.lib.signal.impl.primitive.IntegerSignal;import dev.joid.lib.ui.core.hook.store.UIStore;import dev.joid.lib.ui.core.hook.store.UIStoreData;import dev.joid.lib.ui.core.hook.store.UIStoreScope;
@UIStoreData(id = "settings", scope = UIStoreScope.PERMANENT)public class SettingsStore extends UIStore {
private final IntegerSignal volume = new IntegerSignal(8); private final BooleanSignal music = new BooleanSignal(true);
public BooleanSignal getMusic() { return this.music; }
public IntegerSignal getVolume() { return this.volume; }
@Override public Map<String, Object> toJson() { final Map<String, Object> json = new LinkedHashMap<>(); json.put("music", this.music.peek()); json.put("volume", this.volume.peek()); return json; }
@Override public void fromJson(final Map<String, Object> json) { if (json.containsKey("music")) { this.music.set((Boolean) json.get("music")); }
if (json.containsKey("volume")) { this.volume.set(((Number) json.get("volume")).intValue()); } }
}- The annotation
UIStoreDatanames the file of the store, and its scope, aUIStoreScope, sets how long the store lives:@UIStoreData(id = "settings", scope = UIStoreScope.PERMANENT). toJson()writes the values when JOID saves the store;fromJson(...)reads them back when a file exists, and without one the store keeps the defaults of its fields.- The values are signals, so every node built from them follows them.
The values travel in a Map, and a number comes back from the file as a Long or a Double: ((Number) json.get("volume")).intValue() reads it as an int (Keep a store on disk).
Nothing uses the store yet, so the menu looks the same.
2. Change the settings in the popup
Section titled “2. Change the settings in the popup”SettingsUI gets the store with useStore(...), and replaces the two fixed lines with buttons:
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.signal.impl.primitive.BooleanSignal;import dev.joid.lib.signal.impl.primitive.IntegerSignal;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;import dev.joid.lib.utils.align.Align;
@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); final SettingsStore settings = super.useStore(SettingsStore.class, SettingsStore::new); final BooleanSignal music = settings.getMusic(); final IntegerSignal volume = settings.getVolume();
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, 165).text(Text.create("Music", caption)).anchorY(Align.CENTER).attach(card); RectNode .create(392, 130, 160, 70) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> music.toggle()) .label(Text.create(music.get() ? "On" : "Off", label)) .attach(card); TextNode.create(48, 275).text(Text.create("Volume", caption)).anchorY(Align.CENTER).attach(card); RectNode .create(312, 240, 70, 70) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> volume.set(Math.max(0, volume.peek() - 1))) .label(Text.create("-", label)) .attach(card); TextNode .create(432, 275) .text(Text.create(volume.get() * 10 + " %", caption)) .anchor(Align.CENTER) .attach(card); RectNode .create(482, 240, 70, 70) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .onClick(event -> volume.set(Math.min(10, volume.peek() + 1))) .label(Text.create("+", label)) .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); }
}useStore(SettingsStore.class, SettingsStore::new)returns the store, and creates it with the factory the first time.musicandvolumehold the two signals of the store: a setter follows a signal read from a local variable that holds it, in any expression (What JOID follows).- The On/Off button toggles
music, and its label is built from it. - The minus and plus buttons change
volumeby one step, between 0 and 10; the text shows it as a percentage.
Run it. Open the settings: the music button switches between On and Off, and the volume moves by 10 % between 0 % and 100 %. Close the popup and open it again: it shows the values you left.
3. Show the settings in the menu
Section titled “3. Show the settings in the menu”MenuUI asks for the same store; a PERMANENT store exists once, so both UIs share the same signals.
Add these two lines at the top of init(), under the other styles: a style for the line of the settings, then the store:
final TextInfo status = TextInfo.create(Theme.FONT, 32F).weight(FontWeight.BOLD).color(Theme.TEXT);final SettingsStore settings = super.useStore(SettingsStore.class, SettingsStore::new);Add a line under the buttons, built from both settings:
final BooleanSignal music = settings.getMusic();final IntegerSignal volume = settings.getVolume();
TextNode.create(164, 760).text(Text.create((music.get() ? "Music on" : "Music off") + ", volume " + volume.get() * 10 + " %", status)).attach(this);As in SettingsUI, read both signals through local variables (What JOID follows).
Run it.
Open the settings and change the music and the volume: the line under the buttons changes at once, behind the popup.
Close the window and run the program again: the menu shows your settings.
They live in joid/store/settings.store, under the working directory: Where JOID stores files explains the folder.
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.signal.impl.primitive.BooleanSignal;import dev.joid.lib.signal.impl.primitive.IntegerSignal;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); final TextInfo status = TextInfo.create(Theme.FONT, 32F).weight(FontWeight.BOLD).color(Theme.TEXT); final SettingsStore settings = super.useStore(SettingsStore.class, SettingsStore::new);
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);
final BooleanSignal music = settings.getMusic(); final IntegerSignal volume = settings.getVolume();
TextNode .create(164, 760) .text(Text.create((music.get() ? "Music on" : "Music off") + ", volume " + volume.get() * 10 + " %", status)) .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”Count the launches of the game, and show “Launch 3” under the settings line on the third one.
Use a field annotated with UIProperty, which JOID saves when the UI closes and restores before init().
Solution
Add the field to MenuUI:
@UIPropertyprivate int launches;Then count and show it in init():
this.launches++;TextNode.create(164, 810).text(Text.create("Launch " + this.launches, caption)).attach(this);A property suits a value that belongs to one UI; a store suits values that several UIs share.
What you learned
Section titled “What you learned”- A store holds values that outlive a UI; its scope decides how long it lives and whether it is saved.
- Two UIs that ask for the same
PERMANENTstore share its signals (Use a store from several UIs). - JOID saves stores and properties when a UI closes; close the UIs before the program ends so that they close properly.
- A field marked
@UIPropertykeeps its value from one run to the next (Stores and remembered properties).
