Repository navigation
LineIndicator
Up | Previous | Next | Русский
Below is the full source of the class in documentation form. The comments inside the block explain the purpose of the parts of the class, but they are not kept in the working file LineIndicator.java: production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official;
import com.velitask.sdk.FrameContext;
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;
public class LineIndicator extends Indicator {
/* Line direction; the order of the constants defines the index in JSON. */
public static enum LineDirection {
HORIZONTAL,
VERTICAL,
DIAGONAL_DOWN,
DIAGONAL_UP
}
/* Indicator name; the full layer name in JSON: com.velitask.plagin.official.line. */
public static final String NAME = "line";
/* Prefix of the indicator's localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Line: color, thickness, line style, and dash pattern. */
private final LineProperty mLine = new LineProperty();
/* Line direction; horizontal by default (index 0). */
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");
}
@Override
/* Default maket: top left, layer 400 x 20. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(100d, 100d, null, null);
maket.setLayerSize(400d, 20d);
}
@Override
/* The property order defines the order in the panel and in JSON. */
public IProperty[] defineProperties() {
return new IProperty[]{mLine, mDirection};
}
@Override
/* Both properties are shown in the "Appearance" group. */
public void configureDisplay(DisplayConfig config) {
config.set(mLine, PropertyGroup.APPEARANCE);
config.set(mDirection, PropertyGroup.APPEARANCE);
}
@Override
/* Skins set the color, thickness, and style of the line. */
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()
};
}
@Override
/* Rendering of a single frame. */
public void render(IndicatorContext indicatorContext) {
LineIndicatorContext ctx = (LineIndicatorContext) indicatorContext;
Graphics2D g = ctx.graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
/* Thickness with the scale applied, but not less than one pixel. */
float stroke = (float) (ctx.line.thickness * ctx.scale);
if (stroke < 1) {
stroke = 1;
}
g.setColor(ctx.line.color);
/* The stroke is built from the line pattern. */
g.setStroke(buildStroke(stroke, ctx.line.dashPattern));
int x1, y1, x2, y2;
/* The ends of the segment depend on the direction; an unknown direction is treated as horizontal. */
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);
}
/* The pattern is numbers separated by commas; with an empty or invalid pattern the line is solid. */
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
);
}
/* Snapshot of the property values for a single frame. */
public class LineIndicatorContext extends IndicatorContext {
public final LineProperty.LineContext line;
public final LineDirection direction;
public LineIndicatorContext(FrameContext frame) {
super(frame);
line = mLine.createContext(frame);
direction = mDirection.get(frame);
}
}
@Override
/* Creation of the frame context. */
public IndicatorContext createContext(FrameContext frame) {
return new LineIndicatorContext(frame);
}
}LineIndicator is a decorative straight line: horizontal, vertical, or one of the two diagonals of the layer. The color, thickness, and line style (solid, dashed, and others) are configurable. It does not use data from sources.
Data. Sensors and sources are not used; the indicator depends only on its own properties and on the layer size.
Settings. Two properties in the order of defineProperties():
-
line-LinePropertywith the nestedlineColor(white by default),thickness(0..50, 1 by default),style(the line style, index 0..7:SOLID,DASH,DOT,DASH_DOT,DASH_DOT_DOT,LONG_DASH,SHORT_DASH,CUSTOM;SOLIDby default), anddashPattern(a string of numbers separated by commas); -
direction-EnumArrayProperty<LineDirection>:HORIZONTAL(0),VERTICAL(1),DIAGONAL_DOWN(2, ↘),DIAGONAL_UP(3, ↗); 0 by default.
Both properties are shown in the PropertyGroup.APPEARANCE group. Skins: dashed (white, thickness 2, dashed) and bold (white, thickness 8, solid).
Rendering. The thickness is stroke = thickness × ctx.scale, but not less than 1. The stroke is built by buildStroke: if dashPattern is empty or cannot be parsed (a non-numeric value or a number ≤ 0), the line is solid; otherwise the pattern is passed to BasicStroke as an array of dashes. The ends of the segment: horizontal - the left and right edges at mid-height; vertical - the top and bottom at mid-width; DIAGONAL_DOWN - from the top-left to the bottom-right corner; DIAGONAL_UP - from the bottom-left to the top-right corner.
-
The dash pattern is chosen by the line style. When
stylechanges,dashPatternreceives the pattern of the selected style (for example,12,6forDASH); when the pattern is edited by hand, the style switches toCUSTOM. - The dash lengths are not scaled, unlike the thickness: on a large canvas the dashes look smaller relative to the line.
- A thickness of 0 still gives a 1-pixel line.
-
The line ends are butt caps (
CAP_BUTT), and the joins are mitered (JOIN_MITER). - The line layer is usually narrow (default maket 400 × 20): a horizontal line lies at the middle of the layer height.