Skip to content

BalanceTextIndicator

roosit edited this page Nov 15, 2026 · 1 revision

BalanceTextIndicator

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 BalanceTextIndicator.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.PowerBalanceSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PowerBalanceAtomProperty;
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 power balance between the left and right leg from the power balance sensor. */
public class BalanceTextIndicator 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 = "balanceText";

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

    /* Data source: the power balance sensor (JSON key PowerBalance). */
    private final PowerBalanceAtomProperty mBalance = new PowerBalanceAtomProperty();

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

    @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[]{
            mBalance,
            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) {
        BalanceTextContext ctx = (BalanceTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

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

        /* Right leg share, refined between neighboring samples by the frame time. */
        double right = atom.calcRightPercent(ctx.balance.rawTime);
        /* Percentages are rounded to integers; the left leg share is 100 minus the right leg share. */
        int rightPct = (int) Math.round(right);
        int leftPct = (int) Math.round(100d - right);
        /* Argument order: left, right. */
        String text = ctx.template.makeText(leftPct, rightPct);

        /* 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 BalanceTextTemplateProperty 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("left", "%d", localized(KEY_PROPERTY_VAR + ".left")),
                new Var("right", "%d", localized(KEY_PROPERTY_VAR + ".right"))
            };
        }

        /* 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 power balance between the left and right leg from the power balance sensor. */
    public class BalanceTextContext extends IndicatorContext {

        public final PowerBalanceAtomProperty.SensorContext balance;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public BalanceTextContext(FrameContext frame) {
            super(frame);
            balance = mBalance.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 BalanceTextContext(frame);
    }
}

Purpose

BalanceTextIndicator shows the power balance between the legs: the share of the left and right leg in percent. The data is taken from the power balance sensor of the selected source. The value comes only from power meters that report balance (FIT).

How it works

Data. PowerBalanceAtomProperty creates ctx.balance. queryAtom() returns a PowerBalanceSensorAtom for the frame time; the atom stores the right leg share, and atom.calcRightPercent(ctx.balance.rawTime) linearly refines it between the current and the next sample. The left leg share is calculated as 100 minus the right leg share. If there is no sample (a pause or outside the recording), render draws nothing.

Settings. PowerBalanceAtomProperty (the sensor), FontColorProperty, TextAlignProperty (CENTER by default) and the inner BalanceTextTemplateProperty. There is no units property. The compact skin sets the template {left}/{right}.

Drawing. The right and left leg shares are converted to integers, the string is built through ctx.template.makeText(left, right), 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

  • No sensor data, a pause or outside the recording: the layer is empty.
  • The data exists only for power-meter pedals that report balance; other devices have no balance sensor.
  • The %d formats are designed for an integer: floating-point formats (%.1f) give an empty substitution (the formatting error is suppressed).
  • The number of variables in defineVars() (two) must match the number of makeText(...) arguments.

Clone this wiki locally