Skip to content

GradeTextIndicator

roosit edited this page Nov 15, 2026 · 1 revision

GradeTextIndicator

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 stored in the working file GradeTextIndicator.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.CurrentAtom;
import com.velitask.sdk.data.GradeSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.GradeAtomProperty;
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 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 grade recorded by the device itself (a separate sensor, not calculated from elevation and distance). */
public class GradeTextIndicator extends Indicator {

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

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

    /* Data source: the device grade sensor (JSON key Grade). */
    private final GradeAtomProperty mGrade = new GradeAtomProperty();

    /* Font and color of the text. */
    private final FontColorProperty mText = new FontColorProperty();

    /* Position of the text 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 GradeTextTemplateProperty mTemplate = new GradeTextTemplateProperty();

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

    /* 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 of 100 at 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 property order defines the order of the settings and the order of the keys in JSON. */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mGrade,
            mText,
            mTextAlign,
            mTemplate
        };
    }

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

    /* The only skin, "compact", changes just the text template. */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("compact", localized(KEY + ".skin.compact"))
            .set(mTemplate.skinCompact())
            .build()
        };
    }

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

        /* Current sample for the frame time; inside a pause, the last sample before it. */
        CurrentAtom<GradeSensorAtom> current = ctx.grade.queryCurrent();
        /* The sample itself: null outside the recording. */
        GradeSensorAtom atom = current.atom();
        /* Flag: the value is taken from a pause, not from a sample. */
        boolean fromGap = current.inPause();
        if (atom == null) {
            return;
        }

        /* In a pause, the value at the end of the window of the last sample before the pause; otherwise the grade refined by the frame time. */
        double grade = fromGap
                ? atom.getNextGradePercent()
                : atom.calcGradePercent(ctx.grade.rawTime);
        /* The unit caption ("%") depends on the plugin language. */
        String unit = localized(KEY + ".unit");
        /* Argument order: currNum, currUnit. */
        String text = ctx.template.makeText(grade, unit);

        /* 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 chosen alignment. */
        ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
    }

    /* Text template of this indicator: default value, list of variables and skin. */
    static class GradeTextTemplateProperty 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 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("currNum", "%.1f", localized(KEY_PROPERTY_VAR + ".currNum")),
                new Var("currUnit", "%s", localized(KEY_PROPERTY_VAR + ".currUnit"))
            };
        }

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

    /* Text with the grade recorded by the device itself (a separate sensor, not calculated from elevation and distance). */
    public class GradeTextContext extends IndicatorContext {

        public final GradeAtomProperty.SensorContext grade;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public GradeTextContext(FrameContext frame) {
            super(frame);
            grade = mGrade.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 GradeTextContext(frame);
    }
}

Purpose

GradeTextIndicator shows the grade recorded by the device itself (a separate sensor, not calculated from elevation and distance), in percent. The data is taken from the device grade sensor of the selected source.

How it works

Data. GradeAtomProperty creates ctx.grade. queryCurrent() returns a CurrentAtom<GradeSensorAtom>: atom() is the grade sample for the frame time, inPause() is the pause flag. Inside the recording this is the sample that covers the frame time, and atom.calcGradePercent(ctx.grade.rawTime) linearly refines the grade between the current and the next sample. Inside a pause this is the last sample before it, and the value at the end of its window is read from it, atom.getNextGradePercent() (SlopeTextIndicator shows the last value before a pause in the same way). If there is no sample (outside the recording), render draws nothing.

Settings. GradeAtomProperty (the sensor), FontColorProperty, TextAlignProperty (CENTER by default) and the inner GradeTextTemplateProperty. The compact skin sets the template {currNum}{currUnit}.

Drawing. The unit caption is read from localization (KEY + ".unit", it is "%"), the string is built through ctx.template.makeText(grade, unit), then anti-aliasing, the font (the size is multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.

Features and limitations

  • Outside the recording (neither an atom nor a pause) the layer is empty; in a pause the value at the end of the last sample before it is shown.
  • The device grade is a separate sensor: it does not match the grade calculated from elevation and distance.
  • The %.1f format is designed for a fractional number: the %d format for {currNum} gives an empty substitution (the formatting error is suppressed).
  • The unit caption cannot be changed with a property; for a different caption, remove {currUnit} from the template and write the text yourself.
  • The number of variables in defineVars() (two) must match the number of makeText(...) arguments.

Clone this wiki locally