Skip to content

Transitions

A transition animates a whole UI when it opens and when it closes. Popups play one by default; any other UI can play one too, and you can write your own.

You will learn

  • What a popup plays when it opens and closes, and how to change it
  • How to give any UI a transition
  • How to write a transition of your own
  • What happens to the UI while a transition plays

A popup, a UI whose UIDataPopup sets active = true, plays a PopTransition: it grows from 75 % to its full size around its anchor in 130 ms when it opens, and shrinks back in 130 ms when it closes.

The pop transition, in and out; the backdrop of the popup fades with it.

The transition element of @UIDataPopup chooses which half plays:

@UIDataPopup(active = true, transition = PopupTransition.IN)

Of the four values of UIDataPopup.PopupTransition, IN_OUT is the default, IN animates the opening only, OUT the closing only, and NONE shows and hides the popup at once. Overlays and popups explains popups themselves.

UI.transition gives a transition to any UI: a screen, an overlay or a popup. When the UI always enters the same way, set it in its constructor:

public SlidePopupUI() {
super.transition(SlideTransition.create());
}

Full file: SlidePopupUI.java

When the code that opens the UI decides how it enters, set the transition before JOID.open:

RectNode
.create(1120, 160, 240, 60)
.color(MenuUI.ACCENT)
.onClick(event -> JOID.open(new PopPopupUI().transition(SlideTransition.create())))
.label(Text.create("Pop, sliding", label))
.attach(this);

Full file: MenuUI.java

The same popup as the Pop button, opened with SlideTransition: the transition set before open replaces the one of @UIDataPopup.

A transition set this way replaces the one of @UIDataPopup, and transition(null) removes every transition, the popup one included. The opening transition starts once init() has run, so a transition set in init() plays as well.

A transition is a configuration that several UIs can share, such as a constant. transition(...) copies its two steps into the UI, and each UI plays its own copies: a UI that closes never stops another one. The settings and the listeners are copied at that moment, so a change made later to the shared transition reaches no UI that already has it. The state of a playing transition is on the UI: UI.getTransitionIn and UI.getTransitionOut return the steps it plays, with isRunning() and getAnimator().

A transition is a class that extends Transition and passes two TransitionStep values to its constructor: one for the opening, one for the closing. This one slides the UI up from below its place with a small overshoot, and drops it down when it closes. The step moves the UI with the model-view matrix stack of the render bridge, getModelView(), inside with(...) (Use the matrix stack).

package com.example.style;
import dev.joid.lib.animation.tween.TweenEquations;
import dev.joid.lib.bridge.BridgeHandler;
import dev.joid.lib.ui.core.transition.Transition;
import dev.joid.lib.ui.core.transition.TransitionStep;
public class SlideTransition extends Transition {
protected SlideTransition() {
super(
TransitionStep
.create(500F, TweenEquations.BACK_OUT)
.draw((ui, progress, body) -> SlideTransition.slide(1F - progress, body)),
TransitionStep
.create(300F, TweenEquations.QUART_IN)
.draw((ui, progress, body) -> SlideTransition.slide(progress, body))
);
}
public static SlideTransition create() {
return new SlideTransition();
}
private static void slide(final float away, final Runnable body) {
BridgeHandler.RENDER.get().getModelView().with(matrix -> {
matrix.translate(0D, 300D * away, 0D);
body.run();
});
}
}

Each step has its own duration and easing: the slide overshoots on the way in and falls away on the way out.

TransitionStep.create(duration, easing) gives the duration in milliseconds and the easing of the step. draw((ui, progress, body) -> ...) receives the progress of the step, from 0F to 1F with its easing applied, and body, which draws the UI: wrap body.run() in the transformation of this progress. Both steps count their progress from 0F at their start, so the opening places the UI at 1F - progress away, and the closing at progress away.

The translations of the matrix stack are in design pixels, like the positions of the nodes: 300D moves the UI by 300 design pixels whatever the size of the window. enabled(false) on a step turns it off, and an override of TransitionStep.init prepares what depends on the UI each time it opens or reloads; read a value that follows the window, such as its size, in the drawing of the step.

Know what happens while a transition plays

Section titled “Know what happens while a transition plays”
  • The backdrop of a UI whose UIData sets background = true fades in and out with the transition.
  • On JOID.close(ui) or Escape, the UI stays open while its closing transition plays, and JOID removes it when the transition ends.
  • A close during the opening transition plays the closing one from the point that the opening reached, so the UI turns back without a jump.
  • JOID.open(ui) during its closing transition stops the transition and keeps the UI open.
  • Opening a screen over another screen plays the closing transition of the open one first; the screen you open appears when it ends.
  • JOID.forceClose closes at once, without the closing transition, and so does a close of a UI that is hidden or not drawn.

Open and close a UI says when the opening transition plays.

  • A popup plays a PopTransition by default; @UIDataPopup(transition = ...) keeps the opening, the closing, both or none.
  • transition(...) gives a transition to any UI: in its constructor when the UI decides, before JOID.open when the caller decides.
  • A UI plays copies of the steps of its transition, so several UIs can share one transition.
  • A transition of your own extends Transition with two TransitionStep values: the opening, then the closing.
  • A step draws the UI through body, wrapped in the transformation of its progress, from 0F to 1F with its easing.
  • The UI closes when its closing transition ends; JOID.forceClose skips it.

Slide from the side. Change SlideTransition so that the UI enters from the right edge of the design canvas.

Solution

Translate on the x axis by the width of the canvas instead of 300 design pixels on y:

matrix.translate(1920D * away, 0D, 0D);

A popup that only opens softly. Keep the pop when the popup opens, and close it at once.

Solution
@UIDataPopup(active = true, transition = PopupTransition.IN)

Report a problem with this page