Skip to content

PowerTextIndicator

roosit edited this page Oct 16, 2026 · 2 revisions

PowerTextIndicator

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 PowerTextIndicator.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.CyclingPowerSensorAtom;
import com.velitask.sdk.properties.CyclingPowerAtomProperty;
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 rider's current power from the power sensor. */
public class PowerTextIndicator extends Indicator {

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

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

    /* Data source: the power sensor (JSON key CyclingPower). */
    private final CyclingPowerAtomProperty mPower = new CyclingPowerAtomProperty();

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

    @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 right, margins 100 from the top and right, size 320×120. */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        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[]{
            mPower,
            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 only 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) {
        PowerTextContext ctx = (PowerTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

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

        /* Power in watts, refined between neighboring samples (an integer). */
        int power = atom.calcPower(ctx.power.rawTime);
        /* The unit label depends on the plugin language. */
        String unit = localized(KEY + ".unit");
        /* Argument order: currNum, currUnit. */
        String text = ctx.template.makeText(power, 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 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 PowerTextTemplateProperty 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("currNum", "%d", 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"));
        }
    }

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

        public final CyclingPowerAtomProperty.SensorContext power;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public PowerTextContext(FrameContext frame) {
            super(frame);
            power = mPower.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 PowerTextContext(frame);
    }
}

Purpose

PowerTextIndicator shows the rider's current power in watts. The data comes from the power sensor (SensorType.CYCLING_POWER) of the selected source. The unit label (W / "Вт" in Russian) is defined by the plugin language and is not configurable.

How it works

Data. CyclingPowerAtomProperty creates ctx.power. queryAtom() gives the CyclingPowerSensorAtom for the frame time; atom.calcPower(ctx.power.rawTime) returns an integer power, linearly refined between the current and the next sample. If there is no sample, render draws nothing.

Settings. Four properties: CyclingPowerAtomProperty (the sensor, JSON key CyclingPower), FontColorProperty, TextAlignProperty (CENTER by default), and the internal PowerTextTemplateProperty. There is no units property. The compact skin sets the template {currNum}{currUnit}.

Drawing. The unit label is read from the localization (KEY + ".unit"), the string is built with ctx.template.makeText(power, 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.

Notes and limitations

  • If there is no sensor data, the layer is empty.
  • The Java field name of the sensor property is mPower, while the JSON key is CyclingPower (the name comes from the SDK property).
  • The %d format is meant for an integer: floating-point formats for {currNum} produce an empty substitution.
  • The number of variables in defineVars() (two) must match the number of makeText(...) arguments.

Clone this wiki locally