Skip to content

LineIndicator

roosit edited this page May 15, 2026 · 3 revisions

LineIndicator

Up | Previous | Next | Русский

Full source code with brief explanations

Below is the full source code of the class in documentation form. Comments inside the block explain the purpose of class parts, but they are not stored in the working file LineIndicator.java: production code stays clean, and explanations live in the wiki.

package com.velitask.plugin.official;

import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.IndicatorSkin;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.LineProperty;
import com.velitask.sdk.properties.LineStyle;
import com.velitask.sdk.properties.PropertyGroup;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;

/*
 * Decorative line: horizontal, vertical, or diagonal (UP/DOWN)
 * through the entire layer. Color, thickness, and style (solid/dash) — through LineProperty.
 */
public class LineIndicator extends Indicator {

    /* Four line directions. render() calculates endpoint coordinates. */
    public static enum LineDirection {
        HORIZONTAL,
        VERTICAL,
        DIAGONAL_DOWN,
        DIAGONAL_UP
    }

    public static final String NAME = "line";

    private static final String KEY = _KEY + "." + NAME;

    /* Line style: color, thickness, dash pattern. */
    private final LineProperty mLine = new LineProperty();

    /* Anonymous EnumArrayProperty with localized labels for the UI picker. */
    private final EnumArrayProperty<LineDirection> mDirection
            = new EnumArrayProperty<>(LineDirection.class) {
        @Override public String getName() { return "direction"; }
        @Override public String getTitle() { return localized(KEY + ".direction.title"); }

        @Override
        protected String[] defineTitles() {
            LineDirection[] vs = LineDirection.values();
            String[] t = new String[vs.length];
            for (int i = 0; i < vs.length; i++) {
                t[i] = localized(KEY + ".direction." + vs[i].name().toLowerCase());
            }
            return t;
        }
    };

    @Override public String getName() { return NAME; }
    @Override public String getTitle() { return localized(KEY + ".title"); }
    @Override public String getDescription() { return localized(KEY + ".description"); }
    @Override public String getTags() { return localized(KEY + ".tags"); }

    /* Initial layout: top-left, 100/100 margins, size 400x20 (narrow horizontal). */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.LEFT);
        maket.setLayerMargin(100d, 100d, null, null);
        maket.setLayerSize(400d, 20d);
    }

    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{mLine, mDirection};
    }

    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mLine, PropertyGroup.APPEARANCE);
        config.set(mDirection, PropertyGroup.APPEARANCE);
    }

    /*
     * Two built-in skins:
     *   dashed — white 2px dashed;
     *   bold — white thick 8px solid.
     */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("dashed", localized(KEY + ".skin.dashed"))
            .set(mLine.getColor().skin(Color.WHITE))
            .set(mLine.getThickness().skin(2))
            .set(mLine.getStyle().skin(LineStyle.DASH))
            .build(),
            IndicatorSkin.builder("bold", localized(KEY + ".skin.bold"))
            .set(mLine.getColor().skin(Color.WHITE))
            .set(mLine.getThickness().skin(8))
            .set(mLine.getStyle().skin(LineStyle.SOLID))
            .build()
        };
    }

    /*
     * Render: calculates line endpoints by direction, draws
     * with a built BasicStroke (with dash when a pattern is present).
     */
    @Override
    public void render(IndicatorContext indicatorContext) {
        LineIndicatorContext ctx = (LineIndicatorContext) indicatorContext;
        Graphics2D g = ctx.graphics;
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        /* Minimum thickness is 1 — otherwise invisible. */
        float stroke = (float) (ctx.line.thickness * ctx.scale);
        if (stroke < 1) stroke = 1;

        g.setColor(ctx.line.color);
        g.setStroke(buildStroke(stroke, ctx.line.dashPattern));

        /* Line endpoint coordinates for four directions. */
        int x1, y1, x2, y2;
        LineDirection dir = ctx.direction == null ? LineDirection.HORIZONTAL : ctx.direction;
        switch (dir) {
            case VERTICAL -> { x1 = x2 = ctx.width / 2; y1 = 0; y2 = ctx.height; }
            case DIAGONAL_DOWN -> { x1 = 0; y1 = 0; x2 = ctx.width; y2 = ctx.height; }
            case DIAGONAL_UP -> { x1 = 0; y1 = ctx.height; x2 = ctx.width; y2 = 0; }
            default -> { x1 = 0; x2 = ctx.width; y1 = y2 = ctx.height / 2; }
        }
        g.drawLine(x1, y1, x2, y2);
    }

    /*
     * Helper: builds BasicStroke from width and dash-pattern string.
     * Pattern format "10,5,3,5" → array of float dashes.
     * With invalid pattern → solid stroke (graceful fallback).
     */
    private static BasicStroke buildStroke(float width, String pattern) {
        if (pattern == null || pattern.isBlank()) {
            return new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
        }
        String[] parts = pattern.split(",");
        float[] dash = new float[parts.length];
        try {
            for (int i = 0; i < parts.length; i++) {
                dash[i] = Float.parseFloat(parts[i].trim());
                if (dash[i] <= 0) throw new NumberFormatException();
            }
        } catch (NumberFormatException ex) {
            return new BasicStroke(width, BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER);
        }
        return new BasicStroke(width,
                BasicStroke.CAP_BUTT, BasicStroke.JOIN_MITER,
                10f, dash, 0f
        );
    }

    public class LineIndicatorContext extends IndicatorContext {

        public final LineProperty.LineContext line;
        public final LineDirection direction;

        public LineIndicatorContext(Player player, Canvas canvas) {
            super(player, canvas);
            line = mLine.createContext();
            direction = mDirection.get();
        }
    }

    @Override
    public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
        return new LineIndicatorContext(player, canvas);
    }

}

Purpose

LineIndicator is a decorative line through the layer: horizontal, vertical, or diagonal (two types). Color, thickness, and style (solid/dashed with arbitrary dash pattern) are configurable. Useful as a divider / underline for composition elements, or as a decorative accent.

How it works

Data. Sensors are not used — the line is purely decorative.

Settings. Two properties: LineProperty (composite — color, thickness, dash pattern) and an anonymous EnumArrayProperty<LineDirection> with four values (HORIZONTAL/VERTICAL/DIAGONAL_DOWN/DIAGONAL_UP). Direction labels are localized through localized(...) dynamically in defineTitles().

Drawing. Based on the selected direction, endpoint (x1, y1)/(x2, y2) are calculated across the entire layer width/height. BasicStroke is assembled from thickness (minimum 1px) and dash pattern (a string like "10,5,3,5" is parsed into a float array; invalid pattern → graceful fallback to solid stroke).

Features and limitations

  • Line always spans the entire layer — endpoints at (0, *) and (width, *) (or the vertical equivalent). To make the line shorter, reduce the layer size in Maket.
  • Dash pattern is a comma-separated string, not an array. This is a UI simplification: the user enters "10,5" as "10 black, 5 gaps". Invalid input does not break the indicator — it simply uses solid stroke.
  • stroke < 1 is clamped to 1 — otherwise the line may become invisible on a high-DPI canvas.
  • DIAGONAL goes from corner to corner — the diagonal proportion depends on layer size (45° for a square, another angle for a rectangle).

Clone this wiki locally