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.
What you will build
Section titled “What you will build”Before you start
Section titled “Before you start”- What effects are and how they combine: Effects.
- The model of an effect of your own: Custom effects.
- The shapes you can draw: Drawing shapes and text.
1. Create the effect
Section titled “1. Create the effect”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.
2. Draw the badge
Section titled “2. Draw the badge”Run body first, then draw the badge on top of it (Draw around a node):
@Overridepublic 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.
3. Add the setters
Section titled “3. Add the setters”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.
4. Hide it when the count is zero
Section titled “4. Hide it when the count is zero”Return the shown setting from shouldApply(node), so the badge disappears while it is false (Change how a node draws):
@Overridepublic 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.
5. Apply it to a node
Section titled “5. Apply it to a node”The count of unread messages is an IntegerSignal that starts at 3:
final IntegerSignal unread = new IntegerSignal(3);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);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.
If it does not work
Section titled “If it does not work”- Nothing appears:
shouldApplyreturnsfalse, or the badge is drawn away from the node; draw fromnode.getX()andnode.getY(). - The badge appears under the node: run
bodybefore drawing it (Draw around a node). - The badge does not follow the count: the count is a plain
int, read once; keep it in a signal and passunread.get() > 0(What JOID follows). effect(...)does not compile: type the effectNodeEffect<Node>(Place your effect among the others).- 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.
