Repository navigation
RectangleIndicator
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 stored in the working file RectangleIndicator.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.BorderProperty;
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 RectangleIndicator extends Indicator {
/* Indicator name; the full layer name in JSON: com.velitask.plagin.official.rectangle. */
public static final String NAME = "rectangle";
/* Prefix of the indicator localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Fill color (black by default); its alpha channel is not used when drawing. */
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")
);
/* Fill opacity 0..255 (128 by default); not part of the skins. */
private final IntegerProperty mFillTransparency = new IntegerProperty() {
{
setRange(0, 255);
setSkinnable(false);
}
@Override
public String getName() {
return "fillTransparency";
}
@Override
public Integer getDefault() {
return 128;
}
@Override
public String getTitle() {
return localized(KEY + ".fillTransparency.title");
}
};
/* Border: color, thickness, line style, dash pattern and corner radius. */
private final BorderProperty mBorder = new BorderProperty();
@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, a 400 x 200 layer. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(100d, 100d, null, null);
maket.setLayerSize(400d, 200d);
}
@Override
/* The order of the properties defines the order in the panel and in JSON. */
public IProperty[] defineProperties() {
return new IProperty[]{mFillColor, mFillTransparency, mBorder};
}
@Override
/* The fill is shown in the "Appearance" group; transparency is a slider. */
public void configureDisplay(DisplayConfig config) {
config.set(mFillColor, PropertyGroup.APPEARANCE);
config.set(mFillTransparency, PropertyGroup.APPEARANCE, DisplayHint.SLIDER);
}
@Override
/* Skins define the color and the parameters of the border. */
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("outline", localized(KEY + ".skin.outline"))
.set(mFillColor.skin(0, 0, 0, 0))
.set(mBorder.getColor().skin(Color.WHITE))
.set(mBorder.getThickness().skin(3))
.set(mBorder.getRadius().skin(0))
.build(),
IndicatorSkin.builder("rounded", localized(KEY + ".skin.rounded"))
.set(mFillColor.skin(0, 0, 0, 160))
.set(mBorder.getColor().skin(Color.WHITE))
.set(mBorder.getThickness().skin(0))
.set(mBorder.getRadius().skin(20))
.build()
};
}
@Override
/* Drawing of one frame. */
public void render(IndicatorContext indicatorContext) {
RectangleContext ctx = (RectangleContext) indicatorContext;
Graphics2D g = ctx.graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
/* Border thickness and corner radius in pixels, taking the scale into account. */
int bw = (int) (ctx.border.thickness * ctx.scale);
int cr = (int) (ctx.border.radius * ctx.scale);
/* The border is drawn inside the layer: the shape 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);
/* The fill color is taken from fillColor, and the alpha from fillTransparency. */
Color base = ctx.fillColor.value;
int alpha = Math.max(0, Math.min(255, ctx.fillTransparency.value));
g.setColor(new Color(base.getRed(), base.getGreen(), base.getBlue(), alpha));
if (cr > 0) {
g.fillRoundRect(x, y, w, h, cr, cr);
} else {
g.fillRect(x, y, w, h);
}
/* The border is drawn only with a non-zero thickness; the line style and the dash pattern are not applied here. */
if (bw > 0) {
g.setColor(ctx.border.color);
g.setStroke(new BasicStroke(bw));
if (cr > 0) {
g.drawRoundRect(x, y, w, h, cr, cr);
} else {
g.drawRect(x, y, w, h);
}
}
}
/* Snapshot of the property values for one frame. */
public class RectangleContext extends IndicatorContext {
public final ColorProperty.ColorContext fillColor;
public final IntegerProperty.IntegerContext fillTransparency;
public final BorderProperty.BorderContext border;
public RectangleContext(FrameContext frame) {
super(frame);
fillColor = mFillColor.createContext(frame);
fillTransparency = mFillTransparency.createContext(frame);
border = mBorder.createContext(frame);
}
}
@Override
/* Creation of the frame context. */
public IndicatorContext createContext(FrameContext frame) {
return new RectangleContext(frame);
}
}RectangleIndicator is a decorative shape: a rectangle with a fill, a border and rounded corners. It uses no data from sources; it usually serves as a background or a backing for other indicators.
Data. Sensors and sources are not used; the indicator depends only on its own properties and the size of the layer.
Settings. Three properties in the order of defineProperties():
-
fillColorisColorProperty, black(0, 0, 0, 128)by default; only the red, green and blue channels of the color are used; -
fillTransparencyisIntegerProperty, range 0..255, 128 by default, not part of the skins (setSkinnable(false)); it is the alpha channel of the fill (0 is transparent, 255 is opaque); -
borderisBorderProperty(a descendant ofLineProperty) with the nested propertieslineColor(white by default),thickness(0..50, 1 by default),style(line style, solid by default),dashPattern(empty by default) andcornerRadius(0..500, 0 by default).
fillColor and fillTransparency (a slider, DisplayHint.SLIDER) are placed in the PropertyGroup.APPEARANCE group; the border is shown with its own set of fields. Skins: outline (fill (0, 0, 0, 0), white border 3, radius 0) and rounded (fill (0, 0, 0, 160), white border 0, radius 20).
Drawing. The border thickness bw and the radius cr are recalculated taking the scale into account: (int)(value × ctx.scale). The shape is fitted with an inset of bw / 2 on all sides, and the size w × h is reduced by bw. First it is filled with the fillColor color with the alpha fillTransparency (fillRoundRect if cr > 0, otherwise fillRect), then, if bw > 0, the border is drawn with a line of width bw in the border.color color.
-
The alpha channel of
fillColoris ignored. The fill transparency is set only byfillTransparency; therefore theoutlineandroundedskins, which change the alpha channel offillColor, do not affect the final transparency, andfillTransparencyis not part of the skins. -
The line style and the dash pattern of the border are not applied. The border is always drawn with a solid line:
BasicStroke(bw)without a dash array, although thestyleanddashPatternproperties are available in the panel. -
The corner radius is passed to
fillRoundRect/drawRoundRectas the arc diameter (arcWidth/arcHeight), so a radius of 20 gives an arc with a diameter of 20 pixels. - The border is visible by default: thickness 1 and white color. To remove the border, a thickness of 0 is needed.
- The name "Fill transparency" is misleading: a larger value gives a less transparent fill.