Skip to content

CadenceTextIndicator

roosit edited this page Oct 16, 2026 · 1 revision

CadenceTextIndicator

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 CadenceTextIndicator.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.CadencSensorAtom;
import com.velitask.sdk.properties.CadenceAtomProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
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 current pedalling cadence. */
public class CadenceTextIndicator extends Indicator {

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

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

    /* Data source: the cadence sensor (JSON key Cadence). */
    private final CadenceAtomProperty mCadence = new CadenceAtomProperty();

    /* 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 CadenceTextTemplateProperty mTemplate = new CadenceTextTemplateProperty();

    @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: bottom right, margins of 100 at the top and right, size 320×120. */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.BOTTOM);
        maket.setHorizontal(HorizontalAlign.RIGHT);
        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[]{
            mCadence,
            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, "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) {
        CadenceTextContext ctx = (CadenceTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

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

        /* Cadence in revolutions per minute, refined between neighboring samples. */
        double rpm = atom.calcRpm(ctx.cadence.rawTime);
        /* The unit label depends on the plugin language. */
        String unit = localized(KEY + ".unit");
        /* Argument order: currNum, currUnit. */
        String text = ctx.template.makeText(rpm, unit);

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

    /* This indicator's text template: default value, list of variables and skin. */
    static class CadenceTextTemplateProperty 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 makeText(...) arguments. */
        @Override
        protected Var[] defineVars() {
            return new Var[]{
                new Var("currNum", "%.0f", 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 as they were. */
        public IndicatorSkinTransfer skinCompact() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
        }
    }

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

        public final CadenceAtomProperty.SensorContext cadence;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public CadenceTextContext(FrameContext frame) {
            super(frame);
            cadence = mCadence.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 CadenceTextContext(frame);
    }
}

Purpose

CadenceTextIndicator shows the current pedalling cadence in revolutions per minute. The data comes from the cadence sensor (SensorType.CADENC) of the selected source. The unit label ("rpm" / "об/мин") depends on the plugin language and is not configurable.

How it works

Data. CadenceAtomProperty creates ctx.cadence. queryAtom() returns a CadencSensorAtom (this is exactly what the class is called in the SDK) for the frame time; atom.calcRpm(ctx.cadence.rawTime) returns a double, linearly refined between the current and the next sample. If there is no sample, render draws nothing.

Settings. Four properties: CadenceAtomProperty, FontColorProperty, TextAlignProperty (default CENTER) and the internal CadenceTextTemplateProperty. There is no units property. The default format of {currNum} is %.0f. The compact skin sets the template {currNum}{currUnit}.

Drawing. The unit label is read from localization (KEY + ".unit"), the string is built with ctx.template.makeText(rpm, unit), then smoothing, the font (its size multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.

Features and limitations

  • No sensor data means an empty layer.
  • The value is a double: floating-point formats (%.0f, %.1f) work, while the integer %d gives an empty substitution.
  • The default maket pins the layer to the bottom edge (VerticalAlign.BOTTOM), unlike the other text sensors.
  • The number of variables in defineVars() (two) must match the number of makeText(...) arguments.

Clone this wiki locally