Skip to content

Write a node effect

An effect changes how a node is drawn without changing the node itself, and any node can take it. This guide writes a badge effect: a dot on the top-right corner of a node, shown while a count is above zero.

The badge that the effect draws over the button, until a click sets the count to zero.

Extend NodeEffect typed for Node, so that every node takes it, keep each setting in a Supplier, and give the class a create(...) factory for its required value (Custom effects):

public class BadgeNodeEffect extends NodeEffect<Node> {
private Supplier<Color> color;
private Supplier<Boolean> shown;
protected BadgeNodeEffect(final Supplier<Color> color) {
this.color = color;
this.shown = () -> true;
}
public static BadgeNodeEffect create(final Color color) {
return new BadgeNodeEffect(Signal.from(color));
}

Full file: BadgeNodeEffect.java

Check: the class compiles.

Run body first, then draw the badge on top of it (Draw around a node):

@Override
public void draw(final Node node, final Runnable body) {
body.run();
DrawUtils.SHAPE.drawCircle(node.getX() + node.getWidth() - 6D, node.getY() + 6D, this.color.get(), 12D);
}

Full file: BadgeNodeEffect.java

Check: give a node the effect in the blue Color new Color(91, 140, 255), with effect(BadgeNodeEffect.create(new Color(91, 140, 255))): a dot shows on its top-right corner.

Give each setting a value setter and a Supplier setter, as a node does for its properties (Give the node a property):

public final <E extends BadgeNodeEffect> E color(final Color color) {
return this.color(Signal.from(color));
}
public final <E extends BadgeNodeEffect> E color(final Supplier<Color> color) {
this.color = color;
return (E) this;
}
public final <E extends BadgeNodeEffect> E shown(final boolean shown) {
return this.shown(Signal.from(shown));
}
public final <E extends BadgeNodeEffect> E shown(final Supplier<Boolean> shown) {
this.shown = shown;
return (E) this;
}

Full file: BadgeNodeEffect.java

Check: BadgeNodeEffect.create(new Color(91, 140, 255)).shown(false) compiles, and the dot still shows: nothing reads shown yet.

Return the shown setting from shouldApply(node), so the badge disappears while it is false (Change how a node draws):

@Override
public boolean shouldApply(final Node node) {
return this.shown.get();
}

Full file: BadgeNodeEffect.java

Check: BadgeNodeEffect.create(new Color(91, 140, 255)).shown(false) hides the dot.

The count of unread messages is an IntegerSignal that starts at 3:

final IntegerSignal unread = new IntegerSignal(3);

Full file: EffectGuideUI.java

effect(...) adds the effect next to the other effects of the node, and a click sets the count to zero:

RectNode
.create(810, 500, 300, 80)
.color(EffectGuideUI.SURFACE)
.effect(RoundedNodeEffect.create(12F))
.effect(BorderNodeEffect.create(EffectGuideUI.ELEMENT, 1.5F))
.label(Text.create("Inbox", info))
.effect(BadgeNodeEffect.create(EffectGuideUI.ACCENT).shown(unread.get() > 0))
.onClick(event -> unread.set(0))
.attach(this);

Full file: EffectGuideUI.java

Check: run the UI: the Inbox button shows a blue dot; a click sets the count to zero, shouldApply returns false, and the dot disappears.

  1. Nothing appears: shouldApply returns false, or the badge is drawn away from the node; draw from node.getX() and node.getY().
  2. The badge appears under the node: run body before drawing it (Draw around a node).
  3. The badge does not follow the count: the count is a plain int, read once; keep it in a signal and pass unread.get() > 0 (What JOID follows).
  4. effect(...) does not compile: type the effect NodeEffect<Node> (Place your effect among the others).
  5. The badge is cut at the edge of a parent: the parent clips its children (Layout); move the badge inside the node, or leave room around the node.

Report a problem with this page