Polish with effects and motion
In this last lesson you give Starfall its finish: rounded corners, a soft shadow under the panel, a hand pointer over the buttons, and a panel that slides into place when the menu opens.
Starting point
Section titled “Starting point”The program of the previous lesson works: Play fills the panel, and the settings survive a restart. Every shape is a sharp rectangle, the panel appears at once, and nothing tells the player that the buttons are clickable.
1. Round the corners and add a shadow
Section titled “1. Round the corners and add a shadow”An effect changes how a node is drawn, never where it sits or how it reacts.
Add a shadow color to Theme, black at 60 % opacity:
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(); public static final Color SHADOW = new Color(0, 0, 0, 153).lock();
private Theme() {}
}Then round the panel, and draw its shadow 12 design pixels lower, blurred over 40. Add the two effects to the panel, after its color:
RectNode.create(760, 160, 1000, 760).color(Theme.SURFACE).effect(RoundedNodeEffect.create(24F)).effect(ShadowNodeEffect.create(Theme.SHADOW, 40F).offsetY(12D))RoundedNodeEffect rounds the corners, and ShadowNodeEffect follows the rounded shape: Effects explains how effects combine.
Round the cards with 16 in the same way:
GridNode.create(48, 120, 904, 150).margin(32D).body(grid -> { RectNode .create(0, 0, 280, 150) .color(Theme.ELEMENT) .effect(RoundedNodeEffect.create(16F)) .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) .effect(RoundedNodeEffect.create(16F)) .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) .effect(RoundedNodeEffect.create(16F)) .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);And round the rows of the list with 12:
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) .effect(RoundedNodeEffect.create(12F)) .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);Run it. The panel, its cards and its rows have rounded corners, and a soft shadow lies under the panel.
2. Show a hand over the buttons
Section titled “2. Show a hand over the buttons”cursor(...) sets the pointer that the window shows while it is over a node.
Give each button the hand pointer, Cursor.POINTER, and rounded corners:
FlexNode.vertical(160, 420, 360).margin(20D).body(flex -> { RectNode .create(0, 0, 360, 80) .color(Theme.ELEMENT) .hoveredColor(Theme.HOVER) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(16F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(16F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(16F)) .onClick(event -> JOID.close(this)) .label(Text.create("Quit", label)) .attach(flex);}).attach(this);Do the same for the buttons of SettingsUI, with a radius of 12, and round its card with 24 and the same shadow as the panel.
Run it. Move the pointer over a button: it turns into a hand. Open the settings: the card and its buttons are rounded too.
3. Slide the panel in
Section titled “3. Slide the panel in”A TweenAnimator moves a value over time.
At the start of init(), create one that goes from 0 to 1 in 700 ms, fast at first and slow at the end, and start it:
final TweenAnimator intro = TweenAnimator.create(0F).sequence(700F, 1F, TweenEquations.CUBIC_OUT).start();Then let the panel read it. Add this setter to the panel, after its effects:
.y(160D + 80D * (1D - intro.get()))The animator is a signal, so the setter follows it: the panel starts 80 design pixels lower and rises to its place as the value reaches 1.
init() creates a new animator each time it runs, so the panel slides in again at each opening of the menu, and at each reload.
Run it. The panel slides up into place when the menu opens. Open the settings: the popup plays its own opening transition, which every popup has by default.
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.animation.animator.TweenAnimator;import dev.joid.lib.animation.tween.TweenEquations;import dev.joid.lib.draw.text.Text;import dev.joid.lib.font.FontWeight;import dev.joid.lib.font.TextInfo;import dev.joid.lib.input.cursor.Cursor;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.effect.impl.RoundedNodeEffect;import dev.joid.lib.ui.node.effect.impl.ShadowNodeEffect;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); final TweenAnimator intro = TweenAnimator.create(0F).sequence(700F, 1F, TweenEquations.CUBIC_OUT).start();
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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(16F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(16F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(16F)) .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) .effect(RoundedNodeEffect.create(24F)) .effect(ShadowNodeEffect.create(Theme.SHADOW, 40F).offsetY(12D)) .y(160D + 80D * (1D - intro.get())) .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) .effect(RoundedNodeEffect.create(16F)) .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) .effect(RoundedNodeEffect.create(16F)) .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) .effect(RoundedNodeEffect.create(16F)) .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) .effect(RoundedNodeEffect.create(12F)) .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); }
}The complete SettingsUI.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.cursor.Cursor;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.effect.impl.RoundedNodeEffect;import dev.joid.lib.ui.node.effect.impl.ShadowNodeEffect;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) .effect(RoundedNodeEffect.create(24F)) .effect(ShadowNodeEffect.create(Theme.SHADOW, 40F).offsetY(12D)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(12F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(12F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(12F)) .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) .cursor(Cursor.POINTER) .effect(RoundedNodeEffect.create(12F)) .onClick(event -> JOID.close(this)) .label(Text.create("Close", label)) .attach(card); }) .attach(this); }
}Challenge
Section titled “Challenge”Slide the column of buttons in from the left with the same animator, while the panel rises.
Solution
Add the highlighted setter to the column of buttons in MenuUI, after its margin, and keep its body as it is:
FlexNode.vertical(160, 420, 360).margin(20D).x(160D - 520D * (1D - intro.get()))Both nodes read the same value, so they move together. At 0 the column sits 520 design pixels to the left of its place, outside the canvas.
What you learned
Section titled “What you learned”- An effect changes how a node is drawn: rounded corners, shadows, and more.
- A color carries its own opacity.
cursor(...)sets the pointer over a node (Hover, tooltips and cursors).- A
TweenAnimatormoves a value over time, and a setter that reads it in an expression follows it (Animation). - Popups play an opening and a closing transition.
Where to go next
Section titled “Where to go next”You have used most of what JOID offers. Read How JOID works, then follow the Learn chapters to understand each tool in full, or browse the Guides for precise tasks.
