Repository navigation
RectangleIndicator
Up | Previous | Next | Русский
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 RectangleIndicator.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.BorderProperty;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.IProperty;
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 rectangle with fill and border. Supports rounded
* corners through BorderProperty.radius. Used as a background / container for
* text blocks, or simply as decor.
*/
public class RectangleIndicator extends Indicator {
public static final String NAME = "rectangle";
private static final String KEY = _KEY + "." + NAME;
/* Fill color. Default is semi-transparent black. */
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 with color, thickness, 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"); }
/* Initial layout: top-left, 100/100 margin, size 400x200. */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(100d, 100d, null, null);
maket.setLayerSize(400d, 200d);
}
@Override
public IProperty[] defineProperties() {
return new IProperty[]{mFillColor, mBorder};
}
@Override
public void configureDisplay(DisplayConfig config) {
config.set(mFillColor, PropertyGroup.APPEARANCE);
}
/*
* Two built-in skins:
* outline — transparent fill + white 3px border without rounding;
* rounded — semi-transparent black background + 20px rounded corners without border.
*/
@Override
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()
};
}
/*
* Render: fill + optional border. Coordinates with bw/2 offset,
* size with thickness compensation — the standard pattern for drawRect
* with stroke (stroke is drawn along the line center).
*/
@Override
public void render(IndicatorContext indicatorContext) {
RectangleContext ctx = (RectangleContext) indicatorContext;
Graphics2D g = ctx.graphics;
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
int bw = (int) (ctx.border.thickness * ctx.scale);
int cr = (int) (ctx.border.radius * ctx.scale);
int x = bw / 2, y = bw / 2;
int w = Math.max(1, ctx.width - bw);
int h = Math.max(1, ctx.height - bw);
/* Fill: rounded or regular depending on radius presence. */
g.setColor(ctx.fillColor.value);
if (cr > 0) {
g.fillRoundRect(x, y, w, h, cr, cr);
} else {
g.fillRect(x, y, w, h);
}
/* Border on top (if thickness > 0). */
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);
}
}
}
public class RectangleContext extends IndicatorContext {
public final ColorProperty.ColorContext fillColor;
public final BorderProperty.BorderContext border;
public RectangleContext(Player player, Canvas canvas) {
super(player, canvas);
fillColor = mFillColor.createContext();
border = mBorder.createContext();
}
}
@Override
public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
return new RectangleContext(player, canvas);
}
}RectangleIndicator is a decorative rectangle with fill and optional border. It supports rounded corners. Most often used as a "background" under text elements (for contrast against a video background) or as a decorative container.
Data. Sensors are not used.
Settings. Two properties: fill ColorProperty (default semi-transparent black, 128/255) and BorderProperty (composite — border color, thickness, corner radius).
Drawing. Inner rectangle with offset bw/2 and size (width - bw, height - bw) — the standard technique so stroke does not spill beyond the edges. Fill with fillRect or fillRoundRect (by cr > 0); if thickness > 0, border with the same method, but drawRect/drawRoundRect.
-
bw / 2— stroke compensation: stroke in Java2D is drawn along the line center, so without the offset the border would be half-redrawn outside the layer. -
Skins are mutually incompatible (as in
VideoIndicator) — applying the second cancels the first. - Behavior with
bw == 0andcr == 0— regular solid rectangle fill without border. Withbw == 0andcr > 0— fill with rounded corners and no visible border.