Repository navigation
EN SpeedometerIndicator
Up | Previous | JSON | Русский
Below is the full source of the class in documentation form. Comments inside the block explain the purpose of the class parts, but they are not stored in the working file SpeedometerIndicator.java: production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.example;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.IndicatorSkin;
import com.velitask.sdk.IndicatorSkinTransfer;
import com.velitask.sdk.data.DistanceSensorAtom;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DistanceSensorProperty;
import com.velitask.sdk.properties.DoubleProperty;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SvgProperty;
import com.velitask.sdk.properties.TextTemplateProperty;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Arc2D;
import java.awt.geom.Rectangle2D;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;
/*
* Circular speedometer with SVG rendering, binding to the speed sensor,
* configurable background / scale / arrow colors, range selection
* (40 / 80 / 160 / 320 km/h), and a text template with skin presets.
*/
public class SpeedometerIndicator extends Indicator {
/* Stable technical indicator name. The SDK uses it as the type identifier. */
public static final String NAME = "speedometer";
/* Base localization key. Plagin.KEY_INDICATOR + ".speedometer". */
private static final String KEY = Plagin.KEY_INDICATOR + "." + NAME;
/* Speed data source. atom.speed (mm/hour) is converted to km/h in render(). */
private final DistanceSensorProperty mDistance = new DistanceSensorProperty();
/*
* Available speedometer ranges. The enum value is the maximum km/h
* at which the arrow makes a full turn.
*/
public static enum SpeedScale {
SCALE_40(40),
SCALE_80(80),
SCALE_160(160),
SCALE_320(320);
public final int value;
SpeedScale(int value) {
this.value = value;
}
}
/*
* Anonymous EnumArrayProperty with default SCALE_160 and numeric
* labels (40, 80, 160, 320): no localization, the numbers speak for themselves.
*/
private static final String SPEED_SCALE = "speedScale";
private final EnumArrayProperty<SpeedScale> mSpeedScale = new EnumArrayProperty<>(SpeedScale.class) {
{
set(SpeedScale.SCALE_160);
}
@Override
public String getName() {
return SPEED_SCALE;
}
@Override
public String getTitle() {
return localized(KEY + "." + SPEED_SCALE + ".title");
}
@Override
protected String[] defineTitles() {
SpeedScale[] vs = SpeedScale.values();
String[] t = new String[vs.length];
for (int i = 0; i < vs.length; i++) {
t[i] = String.valueOf(vs[i].value);
}
return t;
}
};
/* Font and color of the numeric speed value in the center of the speedometer. */
private final FontColorProperty mText = new FontColorProperty();
/* Background circle color. Semi-transparent gray by default. */
private static final String BG_COLOR = "bgColor";
private final ColorProperty mBgColor = new ColorProperty(
new Color(204, 204, 204, 51),
BG_COLOR,
localized(KEY + "." + BG_COLOR + ".title"));
/* Tick and numeric scale color. White by default. */
private static final String SCALE_COLOR = "scaleColor";
private final ColorProperty mScaleColor = new ColorProperty(
Color.WHITE,
SCALE_COLOR,
localized(KEY + "." + SCALE_COLOR + ".title"));
/* Arrow fill color. Warm red by default. */
private static final String ARROW_FILL_COLOR = "arrowFillColor";
private final ColorProperty mArrowFillColor = new ColorProperty(
new Color(0xcc, 0x58, 0x54),
ARROW_FILL_COLOR,
localized(KEY + "." + ARROW_FILL_COLOR + ".title"));
/* Arrow outline and central hub color. White by default. */
private static final String ARROW_STROKE_COLOR = "arrowStrokeColor";
private final ColorProperty mArrowStrokeColor = new ColorProperty(
Color.WHITE,
ARROW_STROKE_COLOR,
localized(KEY + "." + ARROW_STROKE_COLOR + ".title"));
/* Arrow outline width in SVG. Default is 2.5 px. */
private final DoubleProperty mArrowStrokeWidth = new DoubleProperty() {
@Override
public String getName() {
return "arrowStrokeWidth";
}
@Override
public String getTitle() {
return localized(KEY + ".arrowStrokeWidth.title");
}
@Override
public Double getDefault() {
return 2.5;
}
};
/* Speed value text template. Default = "%.0f", float skin = "%.1f". */
private final SpeedometerTextTemplateProperty mTemplate = new SpeedometerTextTemplateProperty();
private TextTemplateProperty getTemplate() {
return mTemplate;
}
/* Base SVG asset folder in classpath resources. */
private static final String SVG_PATH = "svg/speedometer/";
/*
* SVG assets with property binding to selectors inside SVG.
* .bind(selector, attr, property): runtime replaces the DOM node attribute
* with the current property value on each render.
*/
private final SvgProperty mBgSvg = new SvgProperty("BgSvg", SVG_PATH + "background.svg")
.bind(".ring", "fill", mBgColor);
private final SvgProperty mDelimiterSvg = new SvgProperty("delimiterSvg", SVG_PATH + "delimiter.svg")
.bind(".tick-major", "stroke", mScaleColor)
.bind(".tick-minor", "stroke", mScaleColor);
/*
* Four numeric scale variants, one per SpeedScale.
* Render chooses the needed SVG by the current scale ordinal.
*/
private final SvgProperty[] mScaleSvgs = new SvgProperty[] {
new SvgProperty("scale40", SVG_PATH + "numeric_40.svg")
.bind(".number", "fill", mScaleColor),
new SvgProperty("scale80", SVG_PATH + "numeric_80.svg")
.bind(".number", "fill", mScaleColor),
new SvgProperty("scale160", SVG_PATH + "numeric_160.svg")
.bind(".number", "fill", mScaleColor),
new SvgProperty("scale320", SVG_PATH + "numeric_320.svg")
.bind(".number", "fill", mScaleColor), };
/* Arrow with fill/stroke/stroke-width bound to user properties. */
private final SvgProperty mArrowSvg = new SvgProperty("ArrowSvg", SVG_PATH + "arrow.svg")
.bind(".arrow-body", "fill", mArrowFillColor)
.bind(".arrow-body", "stroke", mArrowStrokeColor)
.bind(".arrow-body", "stroke-width", mArrowStrokeWidth)
.bind(".arrow-hub", "fill", mArrowStrokeColor);
@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-right, margins 50/50, size 500x500 (square). */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.RIGHT);
maket.setLayerMargin(null, null, 50d, 50d);
maket.setLayerSize(500d, 500d);
}
@Override
public IProperty[] defineProperties() {
return new IProperty[] {
mDistance, mSpeedScale,
mText, getTemplate(),
mBgColor, mScaleColor,
mArrowFillColor, mArrowStrokeColor, mArrowStrokeWidth
};
}
@Override
public void configureDisplay(DisplayConfig config) {
config.set(mSpeedScale, PropertyGroup.APPEARANCE);
config.set(mBgColor, PropertyGroup.APPEARANCE);
config.set(mScaleColor, PropertyGroup.APPEARANCE);
config.set(mArrowFillColor, PropertyGroup.APPEARANCE);
config.set(mArrowStrokeColor, PropertyGroup.APPEARANCE);
config.set(mArrowStrokeWidth, PropertyGroup.APPEARANCE);
}
/*
* Two built-in skins: night (dark) and racing (yellow-red).
* Each skin overrides colors through .skin(...).
*/
@Override
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[] {
IndicatorSkin.builder("night", localized(KEY + ".skin.night"))
.set(mText.getColor().skin(0xaa, 0xaa, 0xaa))
.set(mBgColor.skin(0x11, 0x11, 0x11, 0xaa))
.set(mScaleColor.skin(0x88, 0x88, 0x88))
.set(mArrowFillColor.skin(0x55, 0x2b, 0x2a))
.set(mArrowStrokeColor.skin(0xaa, 0xaa, 0xaa))
.build(),
IndicatorSkin.builder("racing", localized(KEY + ".skin.racing"))
.set(mText.getColor().skin(0xff, 0xff, 0x00))
.set(mBgColor.skin(0x11, 0x11, 0x11, 0xcc))
.set(mScaleColor.skin(0xff, 0xff, 0x00))
.set(mArrowFillColor.skin(0xff, 0x30, 0x30))
.set(mArrowStrokeColor.skin(0xff, 0xff, 0x00))
.build()
};
}
/*
* Render: calculates arrow angle = speed/scale * 360 degrees, draws layers
* background -> tail-arc -> ticks -> numeric scale -> arrow -> text.
*/
@Override
public void render(IndicatorContext indicatorContext) {
SpeedometerContext ctx = (SpeedometerContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The speedometer is square: take min(width,height) and center it in the layer. */
int size = Math.min(ctx.width, ctx.height);
int x = (ctx.width - size) / 2;
int y = (ctx.height - size) / 2;
/* 1. Speedometer background. */
ctx.bgSvg.render(g, x, y, size, size);
/*
* 2. Speed: UTC -> raw-time with preview clamp, atom.speed (mm/hour) -> km/h.
* If there is no atom, speed = 0; the speedometer is still drawn (arrow at zero).
*/
double speedKmh = 0;
long rawTime = mDistance.convertToRawTime(ctx.player.time);
if (ctx.player.isPreview) {
rawTime = mDistance.clampToSensorRange(rawTime);
}
DistanceSensorAtom atom = mDistance.queryAtom(rawTime);
if (atom != null) {
speedKmh = atom.speed / 1000.0;
}
/* Arrow angle: 0..scale -> 0..360 degrees. Above scale: clamp to 360 (arrow at maximum). */
int scale = Math.max(1, ctx.speedScale.value);
double angleDeg = Math.min(speedKmh, scale) * 360.0 / scale;
/* 3. Arrow tail sector (semi-transparent fill from 12 o'clock to the current angle). */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
drawArrowTailArc(g, x, y, size, angleDeg, withAlpha(ctx.arrowFill.value, 112));
/* 4. Ticks and numeric scale (selected SVG for the current scale). */
ctx.delimiterSvg.render(g, x, y, size, size);
ctx.scaleSvg.render(g, x, y, size, size);
/* 5. Arrow with rotation = angleDeg. */
ctx.arrowSvg.render(g, x, y, size, size, angleDeg);
/* 6. Numeric speed value in the center, by template. */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = ctx.text.toFont(ctx.scale);
g.setFont(font);
g.setColor(ctx.text.color.value);
String text = ctx.template.makeText(speedKmh);
FontMetrics fm = g.getFontMetrics(font);
Rectangle2D r = fm.getStringBounds(text, g);
int tx = (ctx.width - (int) r.getWidth()) / 2;
int ty = (ctx.height - (int) r.getHeight()) / 2 + fm.getAscent();
g.drawString(text, tx, ty);
}
/*
* Tail sector: arc from 12 o'clock (90 degrees) clockwise.
* Thickness and radius are proportional to layer size (base 200, as in SVG assets).
*/
private static void drawArrowTailArc(Graphics2D g, int x, int y, int size,
double angleDeg, Color color) {
if (angleDeg <= 0) {
return;
}
float thickness = size * 5f / 200f;
float arcRadius = size * 82.5f / 200f;
float inset = size / 2f - arcRadius;
Arc2D arc = new Arc2D.Float(
x + inset, y + inset,
size - 2 * inset, size - 2 * inset,
90f, (float) -angleDeg, Arc2D.OPEN);
Graphics2D g2 = (Graphics2D) g.create();
try {
g2.setStroke(new BasicStroke(thickness, BasicStroke.CAP_BUTT, BasicStroke.JOIN_ROUND));
g2.setColor(color);
g2.draw(arc);
} finally {
g2.dispose();
}
}
/* Utility: copy of Color with new opacity (0..255). */
private static Color withAlpha(Color c, int alpha) {
return new Color(c.getRed(), c.getGreen(), c.getBlue(), Math.max(0, Math.min(255, alpha)));
}
/*
* Numeric label template: only the "curr" variable.
* Default = "%.0f" (integer), float skin = "%.1f" (one decimal place).
*/
static class SpeedometerTextTemplateProperty extends TextTemplateProperty {
private static final String KEY_PROPERTY = KEY + "." + NAME;
private static final String KEY_PROPERTY_TEMPLATE = KEY_PROPERTY + ".template";
private static final String KEY_PROPERTY_VAR = KEY_PROPERTY + ".var";
private static final String KEY_PROPERTY_VAR_CURR = KEY_PROPERTY_VAR + ".curr";
@Override
protected String defineTemplate() {
return localized(KEY_PROPERTY_TEMPLATE + ".default");
}
@Override
protected Var[] defineVars() {
return new Var[] {
new Var("curr", "%.0f", localized(KEY_PROPERTY_VAR_CURR))
};
}
public IndicatorSkinTransfer skinFloat() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".float"),
new Var[] { new Var("curr", "%.1f", localized(KEY_PROPERTY_VAR_CURR)) });
}
}
/* Context with snapshots of all properties and ready SVG contexts for rendering. */
public class SpeedometerContext extends IndicatorContext {
public final SpeedScale speedScale;
public final FontColorProperty.FontColorContext text;
public final ColorProperty.ColorContext arrowFill;
public final TextTemplateProperty.TextTemplateContext template;
public final SvgProperty.SvgContext bgSvg;
public final SvgProperty.SvgContext delimiterSvg;
public final SvgProperty.SvgContext scaleSvg;
public final SvgProperty.SvgContext arrowSvg;
public SpeedometerContext(Player player, Canvas canvas) {
super(player, canvas);
speedScale = mSpeedScale.get();
text = mText.createContext();
arrowFill = mArrowFillColor.createContext();
template = getTemplate().createContext();
bgSvg = mBgSvg.createContext();
delimiterSvg = mDelimiterSvg.createContext();
/* Numeric scale depends on the selected range: take the matching SVG. */
scaleSvg = scaleSvgFor(speedScale).createContext();
arrowSvg = mArrowSvg.createContext();
}
private SvgProperty scaleSvgFor(SpeedScale scale) {
return mScaleSvgs[scale.ordinal()];
}
}
@Override
public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
return new SpeedometerContext(player, canvas);
}
}SpeedometerIndicator is a classic circular speedometer with an arrow and a numeric value in the center. Speed comes from the distance sensor (the speed field, converted from mm/hour to km/h). The scale is chosen from four ready-made options: 40 / 80 / 160 / 320 km/h. The graphics are assembled from SVG parts with configurable colors; there are two built-in skins: night (dark) and racing (yellow-red racing).
This is the reference "SVG-driven" Velitask indicator: most of the visual part is external SVG assets with dynamic bind bindings, while the Java code is responsible for drawing order, calculating the arrow angle, and overlaying text.
Data. Through DistanceSensorProperty, it takes a DistanceSensorAtom for the current moment (UTC -> raw-time through convertToRawTime; in editor preview mode, clampToSensorRange additionally clamps time to the sensor range). The atom.speed field is stored in "mm/hour"; divide by 1000.0 to get km/h. Arrow angle = min(speed, scale) / scale * 360 degrees; above the scale, the arrow is fixed at the maximum.
Settings. Nine properties: speed source, scale choice (anonymous EnumArrayProperty with four default values), FontColorProperty for the number in the center, four ColorProperty instances (background, scale, arrow fill, arrow outline), DoubleProperty for outline width, and TextTemplateProperty for the numeric label. The persisted form of these settings is documented in SpeedometerIndicator JSON. Graphics: five SvgProperty instances (background, delimiter, four scale variants, arrow); colors are bound through .bind(selector, attr, property): runtime substitutes the current property value into the SVG node attribute during rendering.
Drawing. Layers are strictly ordered: background -> tail-arc -> delimiter -> scale numbers -> arrow -> text. Tail-arc is an Arc2D.Float from 12 o'clock by angleDeg, with thickness proportional to layer size. The arrow.svg is rendered with rotation = angleDeg. The numeric value is centered through FontMetrics.
- Speed > selected scale: the arrow stays at maximum (360 degrees), while the number in the center remains real. This is intentional: the user sees "off scale, real value N".
-
Without an atom (
null), the speedometer still draws an empty dial (arrow at zero, number "0"). It does not skip rendering, unlike text indicators. -
atom.speedin "mm/hour" format requires/ 1000.0for km/h. This is a sensor-format detail, not the SDK. -
mArrowStrokeWidthaffects the arrow outline width in SVG, but not the tail arc thickness (it is calculated from layer size). -
mScaleSvgs[]is not reactive: adding a new scale requires a new SVG, a new enum element, and a new array element. -
The
racingskin uses bright yellow text on a dark background, which may be less readable thannight. If readability is needed, usenight.