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 kept 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.DistanceSensorAtom;
import com.velitask.sdk.data.SensorGap;
import com.velitask.sdk.data.SpeedSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DistanceSensorProperty;
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. It must not be changed without a migration. */
public static final String NAME = "speedText";
/* Prefix of the localization keys: Plugin.Indicator.speedText. */
private static final String KEY = _KEY + "." + NAME;
/* Speed sensor (JSON key "Speed"). */
private final SpeedAtomProperty mSpeed = new SpeedAtomProperty();
/* Fallback distance sensor (JSON key "Distance"): the speed is taken from it if the speed sensor is not selected. */
private final DistanceSensorProperty mDistance = new DistanceSensorProperty();
/* 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);
}
/* Order of properties = order in the settings panel and in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mSpeed,
mDistance,
mText,
mTextAlign,
mUnits,
mTemplate
};
}
/* The alignment and the measurement units 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()
};
}
/* Drawing: the 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;
/*
* Speed in 1/1000 km/h. If the speed sensor is selected, it is taken from it, otherwise from the distance sensor.
* Inside a recording pause (no atom, but there is a pause) 0 is shown, outside the recording the frame is not drawn.
*/
int speedRaw = 0;
if (ctx.speed.getSensor() != null) {
SpeedSensorAtom atom = ctx.speed.queryAtom();
if (atom == null) {
SensorGap<SpeedSensorAtom> gap = ctx.speed.queryGap();
if (gap == null) {
return;
}
} else {
/* The speed linearly interpolated to the frame time inside the atom window. */
speedRaw = atom.calcSpeed(ctx.speed.rawTime);
}
} else {
DistanceSensorAtom atom = ctx.distance.queryAtom();
if (atom == null) {
SensorGap<DistanceSensorAtom> gap = ctx.distance.queryGap();
if (gap == null) {
return;
}
} else {
speedRaw = atom.calcSpeed(ctx.distance.rawTime);
}
}
/* The number and the unit caption by 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 output by the selected alignment within the bounds of the layer. */
ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
}
/* The 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 the 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"))
};
}
/* The skin template: {currNum}{currUnit} without a space, the formats do not change. */
public IndicatorSkinTransfer skinCompact() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
}
}
/* Frame context: the property values at the moment of the frame being drawn. */
public class SpeedTextContext extends IndicatorContext {
public final SpeedAtomProperty.SensorContext speed;
public final DistanceSensorProperty.SensorContext distance;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final MeasurementProperty.Context units;
public final TextTemplateProperty.TextTemplateContext template;
/* The property contexts are created per frame (createContext(frame)). */
public SpeedTextContext(FrameContext frame) {
super(frame);
speed = mSpeed.createContext(frame);
distance = mDistance.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 is taken from a separate speed sensor, and if it is not selected, from the speed of the distance sensor (the fallback Distance sensor). The units are chosen by the measurement property, and the look of the string is set by the template.
Data. SpeedAtomProperty (JSON key Speed) provides ctx.speed, DistanceSensorProperty (JSON key Distance, "Distance" in the interface) provides ctx.distance. The source is chosen in render: if ctx.speed has a sensor (getSensor() != null), the atom ctx.speed.queryAtom() is taken, otherwise the atom ctx.distance.queryAtom() of the distance sensor. If there is no atom, the recording pause is checked (queryGap()): inside a pause the speed is 0 and the text is drawn, and if there is no pause (outside the recording) the layer is not drawn. The speed is calculated as atom.calcSpeed(rawTime): a linear interpolation inside the atom window at the raw time of the frame. The result, multiplied by 1000 (a conversion from 1/1000 km/h to mm/h), is passed to ctx.units.speedParts(...), which returns a "number, unit caption" pair (UnitValue).
Settings. Six properties in the order Speed, Distance, font, textAlign, measurement, textTemplate.
-
Distance:DistanceSensorProperty: the fallback sensor the speed is taken from while theSpeedsensor is not selected. -
font:FontColorProperty, size 48, white color. -
textAlign:TextAlignProperty,CENTERby default. -
measurement:MeasurementProperty(UnitKind.SPEED): the measurement system, the unit format and the speed unit (km/h, m/s, mph, knots, ft/s). -
textTemplate: a nestedSpeedTextTemplatePropertywith the variablescurrNum(%.1f) andcurrUnit(%s); the default template is{currNum} {currUnit}.
In configureDisplay() the alignment and the measurement units are moved into the "Appearance" group.
Skin. compact replaces the template with {currNum}{currUnit} (without a space); the variable formats stay the same.
Drawing. Antialiasing is turned on, the font is taken with ctx.scale applied, the font color is set and the text is output through ctx.textAlign.drawText(...).
- Outside the recording the layer is empty; inside a recording pause (a stop, a device auto-pause) a zero speed is shown, not the last value before the pause.
- The distance sensor is used only when
Speedis empty: if both are selected, the speed is taken fromSpeed, andDistanceis ignored. - The speed sensor stores
speedin 1/1000 km/h (meters per hour), while the unit converterspeedParts(...)accepts millimeters per hour, so the value is multiplied by 1000 before being passed. - With the measurement system "From global settings" the speed unit is taken from the general application settings.
- The template values are formatted through
String.format; on a format error the variable is replaced with an empty string. - The property names (
Speed,Distance,font,textAlign,measurement,textTemplate) are part of the saved format: renaming breaks the loading of projects.