Repository navigation
SpeedTextIndicator
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 SpeedTextIndicator.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.IndicatorSkinTransfer;
import com.velitask.sdk.data.SpeedSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SpeedAtomProperty;
import com.velitask.sdk.properties.TextAlignProperty;
import com.velitask.sdk.properties.TextTemplateProperty;
import com.velitask.sdk.properties.measurement.MeasurementProperty;
import com.velitask.units.UnitKind;
import com.velitask.units.format.UnitValue;
import java.awt.Font;
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;
/* Text speed from the speed sensor in the selected units. */
public class SpeedTextIndicator extends Indicator {
/* Stable indicator name in the layer JSON. Must not be changed without a migration. */
public static final String NAME = "speedText";
/* Localization key prefix: Plugin.Indicator.speedText. */
private static final String KEY = _KEY + "." + NAME;
/* Speed sensor (JSON key "Speed"). */
private final SpeedAtomProperty mSpeed = new SpeedAtomProperty();
/* Font and color (JSON key "font"), size 48, white color. */
private final FontColorProperty mText = new FontColorProperty();
/* Text alignment; CENTER by default (index 4). */
private final TextAlignProperty mTextAlign = new TextAlignProperty();
/* Measurement system, unit format and speed unit (JSON key "measurement"). */
private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.SPEED);
/* String template with two variables. */
private final SpeedTextTemplateProperty mTemplate = new SpeedTextTemplateProperty();
@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");
}
/* Default maket: layer 360 x 120, aligned bottom/left, margins (left, top, right, bottom) = null, 100, 100, null. */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.BOTTOM);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(null, 100d, 100d, null);
maket.setLayerSize(360d, 120d);
}
/* Property order = order in the settings panel and in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mSpeed,
mText,
mTextAlign,
mUnits,
mTemplate
};
}
/* Alignment and units of measurement are in the "Appearance" group. */
@Override
public void configureDisplay(DisplayConfig config) {
config.set(mTextAlign, PropertyGroup.APPEARANCE);
config.set(mUnits, PropertyGroup.APPEARANCE);
}
/* The "compact" skin changes only the text template. */
@Override
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("compact", localized(KEY + ".skin.compact"))
.set(mTemplate.skinCompact())
.build()
};
}
/* Rendering: speed atom, conversion to units, substitution into the template, text output. */
@Override
public void render(IndicatorContext indicatorContext) {
SpeedTextContext ctx = (SpeedTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The atom for the frame time. No data - the frame is not drawn. */
SpeedSensorAtom atom = ctx.speed.queryAtom();
if (atom == null) {
return;
}
/* Speed linearly interpolated to the frame time within the atom window. */
int speedRaw = atom.calcSpeed(ctx.speed.rawTime);
/* The number and the unit label according to the measurement settings. */
UnitValue curr = ctx.units.speedParts(speedRaw * 1000);
/* The order of the values matches the order of the variables: currNum, currUnit. */
String text = ctx.template.makeText(curr.value(), curr.unit());
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);
/* The text is drawn with the selected alignment within the layer bounds. */
ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
}
/* Template. NAME inside the class is the inherited "textTemplate", hence the localization keys ...speedText.textTemplate.* */
static class SpeedTextTemplateProperty 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";
/* The default template from localization: {currNum} {currUnit}. */
@Override
protected String defineTemplate() {
return localized(KEY_PROPERTY_TEMPLATE + ".default");
}
/* Two variables and their default formats. */
@Override
protected Var[] defineVars() {
return new Var[]{
new Var("currNum", "%.1f", localized(KEY_PROPERTY_VAR + ".currNum")),
new Var("currUnit", "%s", localized(KEY_PROPERTY_VAR + ".currUnit"))
};
}
/* Skin template: {currNum}{currUnit} without a space, the formats do not change. */
public IndicatorSkinTransfer skinCompact() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
}
}
/* Frame context: property values at the moment of the frame being rendered. */
public class SpeedTextContext extends IndicatorContext {
public final SpeedAtomProperty.SensorContext speed;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final MeasurementProperty.Context units;
public final TextTemplateProperty.TextTemplateContext template;
/* Property contexts are created per frame (createContext(frame)). */
public SpeedTextContext(FrameContext frame) {
super(frame);
speed = mSpeed.createContext(frame);
text = mText.createContext(frame);
textAlign = mTextAlign.createContext(frame);
units = mUnits.createContext(frame);
template = mTemplate.createContext(frame);
}
}
/* Context factory, called for every frame. */
@Override
public IndicatorContext createContext(FrameContext frame) {
return new SpeedTextContext(frame);
}
}SpeedTextIndicator outputs the current speed as text. The data comes from a dedicated speed sensor, the unit of measurement is chosen by the measurement property, and the look of the string is set by a template.
Data. SpeedAtomProperty (JSON key Speed) provides ctx.speed. render calls ctx.speed.queryAtom(); if there is no atom for the frame, the layer is not drawn. The speed is computed as atom.calcSpeed(ctx.speed.rawTime) — linear interpolation within the atom window at the raw frame time. The result, multiplied by 1000 (from 1/1000 km/h to mm/h), is passed to ctx.units.speedParts(...), which returns a "number, unit label" pair (UnitValue).
Settings. Five properties in the order Speed, font, textAlign, measurement, textTemplate.
-
font—FontColorProperty, size 48, white color. -
textAlign—TextAlignProperty,CENTERby default. -
measurement—MeasurementProperty(UnitKind.SPEED): measurement system, unit format and speed unit (km/h, m/s, mph, knots, ft/s). -
textTemplate— the nestedSpeedTextTemplatePropertywith the variablescurrNum(%.1f) andcurrUnit(%s); the default template is{currNum} {currUnit}.
In configureDisplay(), alignment and units of measurement are moved to the "Appearance" group.
Skin. compact replaces the template with {currNum}{currUnit} (without a space); the variable formats stay the same.
Rendering. Anti-aliasing is enabled, the font is taken with ctx.scale applied, the font color is set and the text is output through ctx.textAlign.drawText(...).
- No data on the frame — the layer is empty.
- The speed sensor stores
speedin 1/1000 km/h (metres per hour), while the unit converterspeedParts(...)takes millimetres per hour, so the value is multiplied by 1000 before it is passed. - With the "From global settings" measurement system, the speed unit is taken from the general application settings.
- Template values are formatted through
String.format; on a format error the variable is replaced with an empty string. - The property names (
Speed,font,textAlign,measurement,textTemplate) are part of the saved format: renaming them breaks project loading.