Repository navigation
DistanceTextIndicator
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 DistanceTextIndicator.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.properties.DisplayConfig;
import com.velitask.sdk.properties.DistanceSensorProperty;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.FontProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
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 with the covered distance and the full distance of the track in the selected units. */
public class DistanceTextIndicator extends Indicator {
/* Stable indicator name in the layer JSON. It must not be changed without a migration. */
public static final String NAME = "distanceText";
/* Prefix of the localization keys: Plugin.Indicator.distanceText. */
private static final String KEY = _KEY + "." + NAME;
/* Distance sensor (JSON key "Distance"). */
private final DistanceSensorProperty mDistanceSensor = new DistanceSensorProperty();
/* Text alignment in the layer; CENTER by default (index 4). */
private final TextAlignProperty mTextAlign = new TextAlignProperty();
/* Font and color of the whole text (JSON key "valueText"), size 48. */
private final FontColorProperty mValueText
= new FontColorProperty("valueText", localized(KEY + ".valueFont.title"), 48);
/* Unit font (JSON key "unitFont"), size 28. Not used in render(). */
private final FontProperty mUnitFont
= new FontProperty("unitFont", localized(KEY + ".unitFont.title"), 28);
/* Measurement system, unit format and distance unit (JSON key "measurement"). */
private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.DISTANCE);
/* String template with four variables. */
private final DistanceTextTemplateProperty mTemplate = new DistanceTextTemplateProperty();
@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 750 x 100, aligned bottom/left, margins (left, top, right, bottom) = 100, null, null, 100. */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.BOTTOM);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(100D, null, null, 100d);
maket.setLayerSize(750d, 100d);
}
/* Order of properties = order in the settings panel and in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mDistanceSensor,
mValueText, mUnitFont,
mTextAlign,
mUnits,
mTemplate
};
}
/* The unit font is in the "Font" group, the measurement units are in the "Appearance" group. */
@Override
public void configureDisplay(DisplayConfig config) {
config.set(mUnitFont, PropertyGroup.FONT);
config.set(mUnits, PropertyGroup.APPEARANCE);
}
/* Drawing: distance atom, conversion to units, substitution into the template, text output. */
@Override
public void render(IndicatorContext indicatorContext) {
DistanceTextContext ctx = (DistanceTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The atom for the frame time; in a recording pause, the last atom before the pause. No data: the frame is not drawn. */
DistanceSensorAtom atom = ctx.distance.queryAtomOrBefore();
if (atom == null) {
return;
}
/* The last atom of the sensor: the full distance is determined from it. */
DistanceSensorAtom lastAtom = ctx.distance.queryLast();
/* The full distance is the distance at the end of the window of the last atom, in millimeters. */
long distanceFullMm = lastAtom != null ? lastAtom.getNextDistance() : 0;
/* The number and the unit caption. The unit for curr and full is chosen independently. */
UnitValue curr = ctx.units.distanceParts(atom.distance);
UnitValue full = ctx.units.distanceParts(distanceFullMm);
/* The order of the values matches the order of the variables: currNum, currUnit, fullNum, fullUnit. */
String text = ctx.template.makeText(
curr.value(), curr.unit(),
full.value(), full.unit()
);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setColor(ctx.valueText.color.value);
Font font = ctx.valueText.toFont(ctx.scale);
g.setFont(font);
/* All the text is drawn with one valueText font. */
ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
}
/* The "minimal" skin changes only the text template. */
@Override
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("minimal", localized(KEY + ".skin.minimal"))
.set(mTemplate.skinMinimal())
.build()
};
}
/* The template. NAME inside the class is the inherited "textTemplate", hence the localization keys ...distanceText.textTemplate.* */
static class DistanceTextTemplateProperty 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} of {fullNum} {fullUnit} (in the English locale). */
@Override
protected String defineTemplate() {
return localized(KEY_PROPERTY_TEMPLATE + ".default");
}
/* Four variables and their default formats. */
@Override
protected Var[] defineVars() {
return new Var[]{
new Var("currNum", "%.2f", localized(KEY_PROPERTY_VAR + ".currNum")),
new Var("currUnit", "%s", localized(KEY_PROPERTY_VAR + ".currUnit")),
new Var("fullNum", "%.0f", localized(KEY_PROPERTY_VAR + ".fullNum")),
new Var("fullUnit", "%s", localized(KEY_PROPERTY_VAR + ".fullUnit"))
};
}
/* The skin template: {currNum} {currUnit}, the variable formats do not change. */
public IndicatorSkinTransfer skinMinimal() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".minimal"));
}
}
/* Frame context: the property values at the moment of the frame being drawn. */
public class DistanceTextContext extends IndicatorContext {
public final DistanceSensorProperty.SensorContext distance;
public final FontColorProperty.FontColorContext valueText;
public final FontProperty.FontContext unitFont;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public final MeasurementProperty.Context units;
/* The property contexts are created per frame (createContext(frame)). */
public DistanceTextContext(FrameContext frame) {
super(frame);
distance = mDistanceSensor.createContext(frame);
valueText = mValueText.createContext(frame);
unitFont = mUnitFont.createContext(frame);
textAlign = mTextAlign.createContext(frame);
template = mTemplate.createContext(frame);
units = mUnits.createContext(frame);
}
}
/* Context factory, called for every frame. */
@Override
public IndicatorContext createContext(FrameContext frame) {
return new DistanceTextContext(frame);
}
}DistanceTextIndicator outputs as text the distance covered up to the current frame and the full distance of the track. The units are chosen by a separate measurement property, and the look of the string is set by the text template.
Data. DistanceSensorProperty (JSON key Distance) provides ctx.distance. The current distance is taken from ctx.distance.queryAtomOrBefore(): the field atom.distance in millimeters. In a recording pause (a stop, a device auto-pause) this is the last atom before the pause, so the counter does not disappear and shows the value reached by the start of the pause. The full distance is taken from ctx.distance.queryLast() as getNextDistance(), that is, the distance at the end of the window of the last atom; if there is no last atom, it equals 0. If there is no atom for the frame (outside the recording), render draws nothing.
Settings. Six properties in the order Distance, valueText, unitFont, textAlign, measurement, textTemplate.
-
valueText:FontColorProperty("Value font"), size 48; the whole text is drawn with it. -
unitFont:FontProperty("Unit font"), size 28. The value is saved and shown in the panel, butrender()does not use it: the unit is output with the same font as the number. -
textAlign:TextAlignProperty,CENTERby default. -
measurement:MeasurementProperty(UnitKind.DISTANCE): the measurement system, the unit format and the distance unit.ctx.units.distanceParts(mm)returns a "number, caption" pair. If no unit is selected (the system "From global settings"), the unit is chosen automatically for the value, so{currUnit}and{fullUnit}can differ (for example, "850.00 m of 10 km"). -
textTemplate: a nestedDistanceTextTemplatePropertywith the variablescurrNum(%.2f),currUnit(%s),fullNum(%.0f),fullUnit(%s).
In configureDisplay() the unit font belongs to the "Font" group, and the measurement units belong to the "Appearance" group.
Skin. minimal replaces only the template text with {currNum} {currUnit}; the variable formats stay the same.
Drawing. Antialiasing is turned on, the valueText font is taken with ctx.scale applied, the font color is set, and the text is output through ctx.textAlign.drawText(...) within the bounds of the layer.
- Outside the recording the layer is empty, in a recording pause the value before the pause is shown (unlike
SpeedometerIndicator, which draws a zero value). -
unitFontdoes not affect drawing yet; the size of the unit cannot be changed separately from the number. - The units of
currandfullare chosen independently if the automatic unit is selected. - The template values are formatted through
String.format; on a format error (for example, a wrong string invars) the variable is replaced with an empty string. - The property names (
Distance,valueText,unitFont, etc.) are part of the saved format: renaming breaks the loading of projects.