Skip to content

LapTextIndicator

roosit edited this page Oct 16, 2026 · 2 revisions

LapTextIndicator

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

Full source with brief notes

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 LapTextIndicator.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.LapSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.LapAtomProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.TextAlignProperty;
import com.velitask.sdk.properties.TextTemplateProperty;
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 current lap number; the total number of laps and the remainder are available. */
public class LapTextIndicator extends Indicator {

    /* Stable indicator name: part of the layer identifier in a project, must not be changed. */
    public static final String NAME = "lapText";

    /* Prefix of this indicator's localization keys. */
    private static final String KEY = _KEY + "." + NAME;

    /* Data source: the lap sensor (JSON key Lap). */
    private final LapAtomProperty mLap = new LapAtomProperty();

    /* Text font and color. */
    private final FontColorProperty mText = new FontColorProperty();

    /* Text position inside the layer (nine options, centered by default). */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    /* Template of the resulting string with variables in curly braces. */
    private final LapTextTemplateProperty mTemplate = new LapTextTemplateProperty();

    @Override
    public String getName() {
        return NAME;
    }

    /* The title, description, and tags come from the plugin localization. */
    @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: top left, margins 100 from the top and right, size 320×120. */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.LEFT);
        maket.setLayerMargin(null, 100d, 100d, null);
        maket.setLayerSize(320d, 120d);
    }

    /* The order of properties defines the order of settings and the order of keys in JSON. */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mLap,
            mText,
            mTextAlign,
            mTemplate
        };
    }

    /* Text alignment is shown in the "Appearance" group. */
    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mTextAlign, PropertyGroup.APPEARANCE);
    }

    /* The only skin, "full" ("Lap N of M"), changes only the text template. */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("full", localized(KEY + ".skin.full"))
            .set(mTemplate.skinFull())
            .build()
        };
    }

    /* Draws one frame: reads the sensor data, builds the string from the template, and outputs it. */
    @Override
    public void render(IndicatorContext indicatorContext) {
        LapTextContext ctx = (LapTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* Sensor sample for the current frame time; if there is no data, the frame stays empty. */
        LapSensorAtom atom = ctx.lap.queryAtom();
        if (atom == null) {
            return;
        }

        /* Index of the current lap at the frame moment. */
        int lap = atom.lapIndex;
        /* The last lap of the sensor: the total number and the remainder come from it. */
        LapSensorAtom last = ctx.lap.queryLast();
        int lastIndex = last != null ? last.lapIndex : lap;
        /* Total laps = index of the last one + 1. */
        int total = lastIndex + 1;
        /* Laps left; never negative. */
        int left = Math.max(0, lastIndex - lap);
        /* Argument order: lap, total, left. */
        String text = ctx.template.makeText(lap, total, left);

        /* Text anti-aliasing. */
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        /* The font size scales together with the layer. */
        Font font = ctx.text.toFont(ctx.scale);
        g.setFont(font);
        g.setColor(ctx.text.color.value);

        /* The text is placed inside the layer by the selected alignment. */
        ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
    }

    /* This indicator's text template: default value, list of variables, and skin. */
    static class LapTextTemplateProperty 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 comes from the localization. */
        @Override
        protected String defineTemplate() {
            return localized(KEY_PROPERTY_TEMPLATE + ".default");
        }

        /* Template variables: name, default format, and description. The order must match the order of the makeText(...) arguments. */
        @Override
        protected Var[] defineVars() {
            return new Var[]{
                new Var("lap", "%d", localized(KEY_PROPERTY_VAR + ".lap")),
                new Var("total", "%d", localized(KEY_PROPERTY_VAR + ".total")),
                new Var("left", "%d", localized(KEY_PROPERTY_VAR + ".left"))
            };
        }

        /* The skin changes only the template text, the variable formats stay the same. */
        public IndicatorSkinTransfer skinFull() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".full"));
        }
    }

    /* Values of all properties, fixed for one frame. */
    public class LapTextContext extends IndicatorContext {

        public final LapAtomProperty.SensorContext lap;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public LapTextContext(FrameContext frame) {
            super(frame);
            lap = mLap.createContext(frame);
            text = mText.createContext(frame);
            textAlign = mTextAlign.createContext(frame);
            template = mTemplate.createContext(frame);
        }
    }

    /* Creates the frame context for the indicator. */
    @Override
    public IndicatorContext createContext(FrameContext frame) {
        return new LapTextContext(frame);
    }
}

Purpose

LapTextIndicator shows the number of the current lap and, optionally, the total number of laps and the remainder. The data comes from the lap sensor of the selected source. By default only {lap} is output; the "Lap N of M" skin sets Lap {lap} of {total}.

How it works

Data. LapAtomProperty creates ctx.lap. queryAtom() returns the LapSensorAtom in effect at the frame time, and lapIndex is read from it. queryLast() returns the last lap of the sensor (a LAST manager with a single-value cache). From them total = lastIndex + 1 and left = max(0, lastIndex - lap) are computed; if there is no last lap, lastIndex equals the current index. There are no fractional values: all the data is integer.

Settings. Four properties: LapAtomProperty, FontColorProperty, TextAlignProperty (CENTER by default), and the internal LapTextTemplateProperty with three variables. There is no units property. The skin is named full and sets the template Lap {lap} of {total}.

Drawing. The string is built with ctx.template.makeText(lap, total, left), then anti-aliasing, the font (the size is multiplied by ctx.scale), and the color are set, and ctx.textAlign.drawText(...) outputs the text.

Notes and limitations

  • If there is no lap sample at the current moment, the layer is empty.
  • Numbering depends on the data: in the FIT/TCX loaders the lap index starts at zero (mLapCounter++), as does the geodata lap detector. The calculation total = lastIndex + 1 assumes the same zero-based numbering, so {lap} for the first lap is 0, and in the "Lap N of M" skin the first lap looks like "Lap 0 of N". This is the behavior of the code, not a setting.
  • The variable formats are integer only (%d): floating-point formats produce an empty substitution.
  • The number of variables in defineVars() (three) must match the number of makeText(...) arguments.
  • The only indicator in this group whose skin is named full rather than compact.

Clone this wiki locally