Controls
A control holds a value that the user changes: a checkbox, a toggle, a switch, a selector, a slider.
Every control of JOID follows the same pattern: options, a value, a signal bound both ways, and the events beforeChange and onChange.
Learn the pattern once on this page, then the options of each control.
You will learn
- How to bind a control to a signal
- How to react to a change, and refuse one
- Why a control keeps a value that its bounds refuse
- In which order a change runs its events
- How to use the options of each control, and show a progress
Bind a control to a signal
Section titled “Bind a control to a signal”A control is abstract: its look comes from its parts, the nodes that draw its box, its track or its thumb.
The controls of this page get their parts where they are created, in an empty subclass of the control, such as a CheckboxNode created with new CheckboxNode(...) {}, followed by the setters of the parts; a number field such as IntegerFieldNode, which draws its own text, has a create(...) factory instead (Text fields).
Give your controls a look gathers them in reusable skins, and Know the parts of each control lists the parts of each control.
A part created with a size of 0 takes the size that the control gives it (Skin a control with parts).
private final IntegerSignal volume = new IntegerSignal(7);private final BooleanSignal subtitles = new BooleanSignal(true);new CheckboxNode(240, 40, 50, 50) {}.box(RectNode.create(0, 0, 0, 0).color(SettingsUI.ELEMENT)).mark(RectNode.create(0, 0, 25, 25).color(SettingsUI.ACCENT)).checked(this.subtitles).attach(card);new IntegerSliderNode(240, 140, 300, 40) {}.range(0, 10).track(RectNode.create(0, 0, 0, 0).color(SettingsUI.ELEMENT)).thumb(RectNode.create(0, 0, 0, 0).color(SettingsUI.ACCENT)).value(this.volume).attach(card);IntegerFieldNode.create(580, 130, 160, 60).min(0).max(10).value(this.volume).info(info).background(RectNode.create(0, 0, 0, 0).color(SettingsUI.ELEMENT)).marginHorizontal(12D).attach(card);volume: while the user drags the slider, the field and the text follow.
The value of a control has two setters, and the argument decides which one runs:
- a plain value, such as
checked(true)orvalue(7), gives the control its start value; - a signal, such as
checked(this.subtitles), binds the control both ways: the control shows the value of the signal, and writes into it each value that the user picks.
A bound signal must be writable: a derived value, which cannot be written, throws an IllegalArgumentException.
Two controls bound to the same signal show the same value, as the slider and the field above.
change(value) sets the value as if the user picked it, with its before event (Refuse a change).
React to a change
Section titled “React to a change”onChange runs after each real change of the value, whatever its source: a gesture, a setter, change(...) or the bound signal.
It never runs for a value equal to the current one, and not for the start value.
Its event holds the old value (getPrevious()) and the new one (getValue()):
checkbox.onChange(event -> this.save(event.getValue()));Most of the time, a signal makes onChange useless: the nodes that show the value read the signal.
Keep onChange for an action, such as saving the settings or playing a sound.
Refuse a change
Section titled “Refuse a change”beforeChange runs before a change that the user or change(...) asks for, and cancel() refuses it:
new IntegerSliderNode(60, 60, 480, 40) {}.range(0, 10).track(RectNode.create(0, 0, 0, 0).color(RefuseUI.ELEMENT)).thumb(RectNode.create(0, 0, 0, 0).color(RefuseUI.ACCENT)).value(this.volume).beforeChange(event -> { if (event.getValue() > 8) { event.cancel(); }}).attach(card);RectNode.create(60, 150, 200, 60).color(RefuseUI.ELEMENT).onClick(event -> this.volume.set(10)).label(Text.create("Set to 10", info)).attach(card);beforeChange refuses the values above 8 that the user asks for; "Set to 10" writes the signal, which is never refused.
The setters and the bound signal are never refused: a refusal belongs to the user, the value of your code always applies. A refused value does not stop the drag of a slider: the value keeps the last step that passed, and the thumb follows the pointer up to half a step past it, then settles on it at the release.
Keep a value that the bounds refuse
Section titled “Keep a value that the bounds refuse”A control remembers the last value that it was asked for, by a setter, its signal or the user, and shows it within its bounds. A value that the bounds refuse is the pending value: the control asks for it again each time the bounds change:
new CheckboxNode(260, 40, 50, 50) {}.box(RectNode.create(0, 0, 0, 0).color(PendingUI.ELEMENT)).mark(RectNode.create(0, 0, 25, 25).color(PendingUI.ACCENT)).checked(this.limited).attach(card);IntegerFieldNode.create(60, 130, 200, 60).max(this.limited.get() ? 10 : 100).value(this.tickets).info(info).background(RectNode.create(0, 0, 0, 0).color(PendingUI.ELEMENT)).marginHorizontal(12D).attach(card);10 and tickets holds 10; once the box is unchecked, the field and the signal are back to 50.
- The value shown is the last value asked for, kept within the bounds:
max(...)andmin(...)of a number field, the options of a switch, a selector or a slider. - The bound signal holds the value shown, so every node that reads it agrees with the control.
- The pending value waits until the bounds allow it: the next value that the user, a setter or the signal gives replaces it.
- Writing to the signal the value that it already holds changes nothing, so the pending value stays.
While the pending value is not one of its options, a choice (switch, selector, slider) shows a fallback.
An IntegerSliderNode or a DoubleSliderNode shows the option nearest to the pending value: 50 on range(0, 10) shows 10.
Any other choice keeps its current value, or shows its first option when the current one is gone too.
The BaseChoiceNode.fallback hook picks that value, for a choice of your own.
The order of the setters does not matter either: value(7) before values(...) gives the same control as after it.
Know the order of a change
Section titled “Know the order of a change”One change of a control runs its steps in this order:
beforeChange, for a gesture or a call tochange(...); a cancel stops everything;- the value applies, and the control writes it into its bound signal;
- the listeners of that signal run: every other node bound to it shows the new value;
- the
onChangelisteners of the controls, in the order of the changes.
In an onChange listener, every node bound to the same signal already shows the new value.
When a listener corrects the value, such as if (event.getValue() > 50) slider.change(50), the listeners receive the change from 0 to 80, then from 80 to 50: the last onChange always gives the final value.
Inside your own Signal.batch, onChange runs at once, and the nodes bound to the signal follow when the batch ends (Derived values).
Turn an option on or off
Section titled “Turn an option on or off”new CheckboxNode(240, 40, 50, 50) {}.box(RectNode.create(0, 0, 0, 0).color(SettingsUI.ELEMENT)).mark(RectNode.create(0, 0, 25, 25).color(SettingsUI.ACCENT)).checked(this.subtitles).attach(card);subtitles follows it.
BaseCheckboxNode holds a boolean: checked(...) gives it, isChecked() reads it, and a press toggles it.
Choose between two values
Section titled “Choose between two values”new ToggleNode<String, String>(60, 60, 160, 60) {}.track(RectNode.create(0, 0, 0, 0).color(ChoicesUI.ELEMENT)).knob(RectNode.create(0, 0, 0, 0).color(ChoicesUI.ACCENT)).state("Night", "Day").toggle(this.night).attach(card);night holds the new state.
BaseToggleNode holds a boolean with toggle(...), and a press switches it.
state(onValue, offValue) gives the two values that getValue() returns, here "Night" and "Day".
Its two type parameters are the types of the on value and of the off value.
ToggleNode.speed sets how fast a ToggleNode slides to its new state: the part of the way done in 1/60 s, and 1F moves at once.
Choose one of a few options
Section titled “Choose one of a few options”new SwitchNode<String>(60, 60, 600, 60) {}.option((value, index) -> { return RectNode .create(0, 0, 0, 0) .color(() -> value.equals(this.difficulty.get()) ? ChoicesUI.ACCENT : ChoicesUI.ELEMENT) .label(() -> Text.create(value, value.equals(this.difficulty.get()) ? selected : info));}).values("Story", "Normal", "Hard").value(this.difficulty).attach(card);The choice controls share the options of BaseChoiceNode:
values(...)gives the options, at least one; a control attached without options throws anIllegalStateException;value(...)selects an option, and throws anIllegalArgumentExceptionfor a value that is not one;valueIndex(...)selects an option by its index, a plain one or anIntegerSignalbound both ways;getValue(),getValueIndex()andgetValueList()read them.
New options keep the value when it is still one of them; otherwise the control shows its fallback.
getValueList() is a read-only view that follows the options set later.
A SwitchNode builds one node per option with its option((value, index) -> node) and lays them out side by side.
The option function runs once per value, so value acts like the variable of a loop: the color and the label read difficulty in a lambda (What JOID follows).
Choose from a drop-down list
Section titled “Choose from a drop-down list”new SelectorNode<String>(60, 40, 280, 60) {}.field(RectNode.create(0, 0, 0, 0).color(ChoicesUI.ACCENT)).option(value -> { return RectNode .create(0, 0, 0, 0) .color(ChoicesUI.ELEMENT) .hoveredColor(ChoicesUI.HOVER) .label(Text.create(value, info));}).values("English", "French", "German", "Japanese").value(this.language).attach(card);A SelectorNode shows its selected option and opens a list of the others.
SelectorNode.direction opens it DOWN, the default, or UP near the bottom of the screen.
BaseSelectorNode.open and BaseSelectorNode.close act from your code; beforeOpen, onOpen, beforeClose and onClose follow it, and active(...) binds the open state to a signal.
The list closes when the selector is detached.
Pick a value on a track
Section titled “Pick a value on a track”new StringSliderNode(60, 60, 600, 40) {}.values(Quality.values()).track(RectNode.create(0, 0, 0, 0).color(ChoicesUI.ELEMENT)).thumb(RectNode.create(0, 0, 0, 0).color(ChoicesUI.ACCENT)).value(this.quality).attach(card);public enum Quality {
LOW, MEDIUM, HIGH, ULTRA;
}A slider is a choice whose options sit along a track: a press on the track jumps to the nearest step, and a drag follows the pointer.
| Slider | Options |
|---|---|
IntegerSliderNode | range(min, max) or range(min, max, step) |
DoubleSliderNode | range(min, max, step) |
StringSliderNode | values(...) of texts or of the constants of an enum, and value(constant) |
A StringSliderNode holds the name of each constant, a String: bind it to a Signal<String>, such as new Signal<>("HIGH").
BaseSliderNode.inset keeps the first and the last step away from the ends, by half the thumb by default.
SliderNode.speed eases the thumb to a new value, and 1F, the default, moves it at once; BaseSliderNode.isSliding is true during a drag.
Show a progress
Section titled “Show a progress”A ProgressNode shows a value and takes no input:
ProgressNode.create(60, 130, 480, 30).track(RectNode.create(0, 0, 0, 0).color(ProgressUI.ELEMENT)).fill(RectNode.create(0, 0, 0, 0).color(ProgressUI.ACCENT)).progress(this.loaded.get() / 10F).attach(card);ProgressNode.create(60, 200, 480, 30).track(RectNode.create(0, 0, 0, 0).color(ProgressUI.ELEMENT)).fill(RectNode.create(0, 0, 0, 0).color(ProgressUI.ACCENT)).direction(ProgressDirection.RIGHT_TO_LEFT).progress(this.loaded.get() / 10F).attach(card);0.6F fills 60 % of the track, from the side of the direction.
The progress goes from 0F to 1F, and a value out of it shows an empty or a full bar.
ProgressNode.direction fills from the left (the default), the right, the top or the bottom.
- A control is abstract: a skin gives it its parts.
- A plain value gives the start value; a signal binds the value both ways;
change(...)acts like the user. onChangeruns on every real change, whatever its source, never for an equal value nor for the start value.beforeChangerefuses what the user orchange(...)asks for; setters and signals are never refused.- A control keeps the pending value that its bounds refuse and asks for it again when they change; the signal holds the value shown.
- A change runs
beforeChange, then applies, then updates the bound nodes, then runsonChange. - Choices take their options with
values(...); new options keep the value when they still hold it, else the choice shows its fallback. ProgressNodeshows a value from0Fto1Fand takes no input.
Try it
Section titled “Try it”Hide subtitles at zero.
In SettingsUI, uncheck “Subtitles” when the volume reaches 0.
Solution
An action on a change goes in onChange:
new IntegerSliderNode(240, 140, 300, 40) {}.range(0, 10).track(RectNode.create(0, 0, 0, 0).color(MuteUI.ELEMENT)).thumb(RectNode.create(0, 0, 0, 0).color(MuteUI.ACCENT)).value(this.volume).onChange(event -> this.subtitles.set(event.getValue() > 0 && this.subtitles.peek())).attach(card);A switch on an index.
In ChoicesUI, keep the difficulty as an IntegerSignal from 0 to 2 instead of a text.
Solution
valueIndex(...) binds the index both ways:
new SwitchNode<String>(60, 60, 600, 60) {}.option(...).values("Story", "Normal", "Hard").valueIndex(this.level).attach(card);