Repository navigation
EllipseIndicator
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 EllipseIndicator.java: the 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.BorderWidthProperty;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DisplayHint;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerProperty;
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 EllipseIndicator extends Indicator {
/* Indicator name; the full layer name in JSON: com.velitask.plagin.official.ellipse. */
public static final String NAME = "ellipse";
/* Prefix of the indicator localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Fill color including transparency (black by default, alpha 128). */
private static final String FILL_COLOR = "fillColor";
private final ColorProperty mFillColor = new ColorProperty(
new Color(0, 0, 0, 128),
FILL_COLOR,
localized(KEY + "." + FILL_COLOR + ".title")
);
/* Border color (white by default). */
private static final String BORDER_COLOR = "borderColor";
private final ColorProperty mBorderColor = new ColorProperty(
Color.WHITE,
BORDER_COLOR,
localized(KEY + "." + BORDER_COLOR + ".title")
);
/* Border thickness 0..50; for the ellipse the default is 0, that is, no border. */
private final BorderWidthProperty mBorderWidth = new BorderWidthProperty() {
{
setRange(0, 50);
}
@Override
public Integer getDefault() {
return 0;
}
};
@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 200 x 200. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(100d, 100d, null, null);
maket.setLayerSize(200d, 200d);
}
@Override
/* The order of the properties defines the order in the panel and in JSON. */
public IProperty[] defineProperties() {
return new IProperty[]{mFillColor, mBorderColor, mBorderWidth};
}
@Override
/* The properties are shown in the "Appearance" group; the thickness is a spinner field. */
public void configureDisplay(DisplayConfig config) {
config.set(mFillColor, PropertyGroup.APPEARANCE);
config.set(mBorderColor, PropertyGroup.APPEARANCE);
config.set(mBorderWidth, PropertyGroup.APPEARANCE, DisplayHint.SPINNER);
}
@Override
/* The skins set the fill, the border color, and the border thickness. */
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("outline", localized(KEY + ".skin.outline"))
.set(mFillColor.skin(0, 0, 0, 0))
.set(mBorderColor.skin(Color.WHITE))
.set(mBorderWidth.skin(3))
.build(),
IndicatorSkin.builder("solidLight", localized(KEY + ".skin.solidLight"))
.set(mFillColor.skin(255, 255, 255, 180))
.set(mBorderColor.skin(Color.BLACK))
.set(mBorderWidth.skin(2))
.build()
};
}
@Override
/* Drawing a single frame. */
public void render(IndicatorContext indicatorContext) {
EllipseContext ctx = (EllipseContext) indicatorContext;
Graphics2D g = ctx.graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
/* Border thickness in pixels, taking the scale into account. */
int bw = (int) (ctx.borderWidth.value * ctx.scale);
/* The border is drawn inside the layer: the ellipse is shifted and reduced by the line thickness. */
int x = bw / 2, y = bw / 2;
int w = Math.max(1, ctx.width - bw);
int h = Math.max(1, ctx.height - bw);
g.setColor(ctx.fillColor.value);
g.fillOval(x, y, w, h);
/* The border is drawn only when the thickness is nonzero. */
if (bw > 0) {
g.setColor(ctx.borderColor.value);
g.setStroke(new BasicStroke(bw));
g.drawOval(x, y, w, h);
}
}
/* Snapshot of the property values for one frame. */
public class EllipseContext extends IndicatorContext {
public final ColorProperty.ColorContext fillColor;
public final ColorProperty.ColorContext borderColor;
public final IntegerProperty.IntegerContext borderWidth;
public EllipseContext(FrameContext frame) {
super(frame);
fillColor = mFillColor.createContext(frame);
borderColor = mBorderColor.createContext(frame);
borderWidth = mBorderWidth.createContext(frame);
}
}
@Override
/* Creating the frame context. */
public IndicatorContext createContext(FrameContext frame) {
return new EllipseContext(frame);
}
}EllipseIndicator is a decorative shape: an ellipse (a circle for a square layer) with a fill and an optional border. It uses no data from sources.
Data. Sensors and sources are not used; the indicator depends only on its own properties and the layer size.
Settings. Three properties in the order of defineProperties():
-
fillColor—ColorProperty, default(0, 0, 0, 128); the alpha channel is used when drawing; -
borderColor—ColorProperty, white by default; -
borderWidth—BorderWidthProperty(IntegerSpinnerProperty), range 0..50; the default value is overridden to 0 (in the base property it is 5).
All three are shown in the PropertyGroup.APPEARANCE group, and the thickness is a spinner field (DisplayHint.SPINNER). Skins: outline (fill (0, 0, 0, 0), white border 3) and solidLight (fill (255, 255, 255, 180), black border 2).
Drawing. The border thickness is bw = (int)(borderWidth × ctx.scale). The ellipse is fitted with an offset of bw / 2, and the w × h size is reduced by bw. First fillOval with the fill color, then, when bw > 0, drawOval with a line of width bw in the border color.
-
The border is off by default (
borderWidth = 0), unlike the common "Border Width" property, whose default is 5. -
The alpha channel of
fillColorworks: the fill transparency is set by the color itself, and there is no separate property (unlike the rectangle). - The border is drawn inside the layer, so the size of the ellipse together with the border does not exceed the layer.
- The border is always solid; the ellipse has no line style setting.