Skip to content

DistanceTextIndicator

roosit edited this page Oct 16, 2026 · 4 revisions

DistanceTextIndicator

Up | Previous | Next | Русский

Full source with short explanations

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 distance covered and the full track distance 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";

    /* Localization key prefix: 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 the 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 units of measurement 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, insertion 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. No data - the frame is not drawn. */
        DistanceSensorAtom atom = ctx.distance.queryAtom();
        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 label. 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);

        /* The whole text is drawn with the single 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", so the localization keys are ...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: 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;

        /* 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);
    }

}

Purpose

DistanceTextIndicator shows as text the distance covered up to the current frame and the full distance of the track. The units of measurement are selected by a separate measurement property, and the look of the string is set by the text template.

How it works

Data. DistanceSensorProperty (JSON key Distance) provides ctx.distance. The current distance is taken from ctx.distance.queryAtom() — the atom.distance field in millimeters. 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 is 0. If there is no atom for the frame, 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, but render() does not use it: the unit is drawn with the same font as the number.
  • textAlign — TextAlignProperty, CENTER by default.
  • measurement — MeasurementProperty(UnitKind.DISTANCE): the measurement system, the unit format, and the distance unit. ctx.units.distanceParts(mm) returns a "number, label" pair. If no unit is selected (the "From global settings" system), the unit is chosen automatically for the value, so {currUnit} and {fullUnit} may differ (for example, "850.00 m of 10 km").
  • textTemplate — the nested DistanceTextTemplateProperty with the variables currNum (%.2f), currUnit (%s), fullNum (%.0f), fullUnit (%s).

In configureDisplay(), the unit font belongs to the "Font" group, and the units of measurement belong to the "Appearance" group.

Skin. minimal replaces only the template text with {currNum} {currUnit}; the variable formats stay the same.

Drawing. Antialiasing is enabled, the valueText font is taken with ctx.scale applied, the font color is set, and the text is drawn through ctx.textAlign.drawText(...) within the layer bounds.

Notes and limitations

  • No data on the frame — the layer is empty (unlike SpeedometerIndicator, which draws a zero value).
  • unitFont does not affect drawing yet; the unit size cannot be changed separately from the number.
  • The curr and full units are chosen independently if the automatic unit is selected.
  • Template values are formatted through String.format; on a format error (for example, an invalid string in vars) the variable is replaced with an empty string.
  • Property names (Distance, valueText, unitFont, and so on) are part of the saved format: renaming breaks project loading.

Clone this wiki locally