Skip to content

DistanceTextIndicator

roosit edited this page May 15, 2026 · 4 revisions

DistanceTextIndicator

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

Full source code with brief explanations

Below is the full source code of the class in documentation form. Comments inside the block explain the purpose of class parts, but they are not stored in the working file DistanceTextIndicator.java: production code stays clean, and explanations live in the wiki.

package com.velitask.plugin.official;

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 distance indicator: receives distance from a sensor source,
 * formats current and full distance, and draws a string on the layer.
 */
public class DistanceTextIndicator extends Indicator {

    /* Stable technical indicator name for SDK, localization, and saved projects. */
    public static final String NAME = "distanceText";

    /* Base localization key for title, description, tags, properties, and skin. */
    private static final String KEY = _KEY + "." + NAME;

    /* Distance data source: current atom and last track atom. */
    private final DistanceSensorProperty mDistanceSensor = new DistanceSensorProperty();

    /* Alignment of the final text string inside the layer. */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    /* Main font and color of the distance value. */
    private final FontColorProperty mValueText
            = new FontColorProperty("valueText", localized(KEY + ".valueFont.title"), 48);

    /*
     * Unit font.
     * Currently the context is created, but render draws the whole string with one font.
     */
    private final FontProperty mUnitFont
            = new FontProperty("unitFont", localized(KEY + ".unitFont.title"), 28);

    /* User-selected measurement units for distance. */
    private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.DISTANCE);

    /* String template: which variables to output and in which format. */
    private final DistanceTextTemplateProperty mTemplate = new DistanceTextTemplateProperty();

    /*
     * Returns the stable technical indicator name.
     * SDK uses it as the type identifier when creating and loading a project.
     */
    @Override
    public String getName() {
        return NAME;
    }

    /*
     * Returns the localized user-facing indicator title.
     * The string is taken from the distanceText key namespace.
     */
    @Override
    public String getTitle() {
        return localized(KEY + ".title");
    }

    /*
     * Returns the localized indicator description.
     * The description is used in the selection UI or indicator help.
     */
    @Override
    public String getDescription() {
        return localized(KEY + ".description");
    }

    /*
     * Returns localized tags.
     * Tags help search for the indicator in the user interface.
     */
    @Override
    public String getTags() {
        return localized(KEY + ".tags");
    }

    /*
     * Sets the initial layer layout.
     * Indicator appears bottom-left with base size 750 x 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);
    }

    /*
     * Registers indicator properties for the SDK.
     * Array order defines property display order in the settings panel.
     */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mDistanceSensor,
            mValueText, mUnitFont,
            mTextAlign,
            mUnits,
            mTemplate
        };
    }

    /*
     * Refines display groups of individual properties in the UI.
     * Unit font goes to the FONT group, unit selection to APPEARANCE.
     */
    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mUnitFont, PropertyGroup.FONT);
        config.set(mUnits, PropertyGroup.APPEARANCE);
    }

    /*
     * Renders the indicator for the current frame.
     * The method gets distance data, formats text, and draws it with selected alignment.
     */
    @Override
    public void render(IndicatorContext indicatorContext) {
        DistanceTextContext ctx = (DistanceTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /*
         * Player time is stored in UTC. SensorProperty.queryAtom expects sensor raw-time,
         * so UTC is first converted to the selected source scale.
         */
        long rawTime = mDistanceSensor.convertToRawTime(ctx.player.time);
        /*
         * In editor preview the user can scrub the timeline outside the
         * sensor range. clampToSensorRange clamps time to the nearest
         * record so the editor still shows a value. In final rendering,
         * clamp is not applied: outside data, the indicator honestly draws nothing.
         */
        if (ctx.player.isPreview) {
            rawTime = mDistanceSensor.clampToSensorRange(rawTime);
        }
        /* If there is no atom at this moment in the source, the indicator draws nothing. */
        DistanceSensorAtom atom = mDistanceSensor.queryAtom(rawTime);
        if (atom == null) {
            return;
        }

        /* Last atom is used as the source of full track distance. */
        DistanceSensorAtom lastAtom = mDistanceSensor.queryLast();
        long distanceFullMm = lastAtom != null ? lastAtom.getNextDistance() : 0;

        /* Base values are converted into user-selected units. */
        UnitValue curr = ctx.units.distanceParts(atom.distance);
        UnitValue full = ctx.units.distanceParts(distanceFullMm);

        /* Template assembles the final string from current and full distance. */
        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);

        ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
    }

    /*
     * Declares available indicator skin presets.
     * Currently there is one preset: minimal, which changes the text template.
     */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("minimal", localized(KEY + ".skin.minimal"))
            .set(mTemplate.skinMinimal())
            .build()
        };
    }

    /*
     * Nested template property.
     * It declares default/minimal text and available variables.
     */
    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";

        /*
         * Returns the localized default template.
         * This is what is used until the user changes the text format.
         */
        @Override
        protected String defineTemplate() {
            return localized(KEY_PROPERTY_TEMPLATE + ".default");
        }

        /*
         * Declares template variables.
         * Variable order must match makeText argument order in render.
         */
        @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"))
            };
        }

        /*
         * Creates transfer for the minimal skin.
         * It replaces only the text template and does not change other properties.
         */
        public IndicatorSkinTransfer skinMinimal() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".minimal"));
        }
    }

    /*
     * Runtime context fixes property snapshots for a specific render frame.
     * Render uses prepared context objects instead of reading properties directly.
     */
    public class DistanceTextContext extends IndicatorContext {

        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;

        /*
         * Creates render context.
         * Internally prepares context objects of all properties needed by render.
         */
        public DistanceTextContext(Player player, Canvas canvas) {
            super(player, canvas);
            valueText = mValueText.createContext();
            unitFont = mUnitFont.createContext();
            textAlign = mTextAlign.createContext();
            template = mTemplate.createContext();
            units = mUnits.createContext();
        }
    }

    /*
     * Creates a specialized indicator context.
     * SDK calls this method before rendering the layer.
     */
    @Override
    public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
        return new DistanceTextContext(player, canvas);
    }

}

Purpose

DistanceTextIndicator is a text indicator for traveled distance. It takes the value from the user-selected source, converts it to the user's measurement system, formats it through a template, and draws a string on the layer. Suitable for overlays like "current distance / full track distance".

How it works

Data. Through DistanceSensorProperty, the indicator takes two DistanceSensorAtom values: current (at ctx.player.time, converted to sensor raw-time) and queryLast() for calculating full track distance. In editor preview, time is clamped to the sensor range (clampToSensorRange) — not in final renders.

Settings. Six user properties: data source, main text font+color (FontColorProperty), unit font (reserved, see below), alignment (TextAlignProperty), unit system (MeasurementProperty for UnitKind.DISTANCE), and string template (TextTemplateProperty with variables currNum / currUnit / fullNum / fullUnit).

Drawing. Current and full distances are converted to selected units through MeasurementProperty.Context.distanceParts(mm) → pair value + unit. These pairs are substituted into the template, and the final string is drawn through TextAlignProperty.drawText with antialiasing enabled. No borders or background — only text.

Features and limitations

  • mUnitFont is declared and included in context, but is not used in the current render(): the whole string is drawn with one font from mValueText. If a separate unit style is needed, render must be split into parts.
  • Without an atom (queryAtom == null), the indicator completely skips drawing — no placeholder and no last known value.
  • Without a last atom (queryLast == null), full distance is considered 0; current distance is still shown.
  • Skin minimal changes only the text template; font, color, source, units are untouched.

Clone this wiki locally