Skip to content

HeartTextIndicator

roosit edited this page Oct 16, 2026 · 1 revision

HeartTextIndicator

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 HeartTextIndicator.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.HeartSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.HeartAtomProperty;
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 heart rate from a heart-rate sensor. */
public class HeartTextIndicator 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 = "heartText";

    /* Localization key prefix of this indicator. */
    private static final String KEY = _KEY + "." + NAME;

    /* Data source: the heart-rate sensor (JSON key Heart). */
    private final HeartAtomProperty mHeart = new HeartAtomProperty();

    /* Text font and color. */
    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 HeartTextTemplateProperty mTemplate = new HeartTextTemplateProperty();

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

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

        /* The heart rate refined between neighboring samples at the frame time (an integer). */
        int hr = atom.calcHr(ctx.heart.rawTime);
        /* The unit label depends on the plugin language. */
        String unit = localized(KEY + ".unit");
        /* Argument order: currNum, currUnit. */
        String text = ctx.template.makeText(hr, unit);

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

        /* The font size is scaled 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 with the selected alignment. */
        ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
    }

    /* Text template of this indicator: the default value, the list of variables and the skin. */
    static class HeartTextTemplateProperty 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", "%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 HeartTextContext extends IndicatorContext {

        public final HeartAtomProperty.SensorContext heart;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public HeartTextContext(FrameContext frame) {
            super(frame);
            heart = mHeart.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 HeartTextContext(frame);
    }
}

Purpose

HeartTextIndicator shows the current heart rate as a number with a unit label. The data comes from the heart-rate sensor of the selected source. Units are not configurable: the label "уд/мин" (or bpm) is determined by the plugin language.

How it works

Data. HeartAtomProperty creates ctx.heart. queryAtom() returns the HeartSensorAtom for the frame time; atom.calcHr(ctx.heart.rawTime) returns an integer heart rate, linearly refined between the current and the next sample (the fractional part is discarded). If there is no sample, render draws nothing.

Settings. Four properties: HeartAtomProperty (the sensor), FontColorProperty, TextAlignProperty (CENTER by default) and the inner HeartTextTemplateProperty. Unlike temperature, there is no units property. The compact skin sets the template {currNum}{currUnit}.

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

  • No sensor data — the layer is empty.
  • The %d format is meant for an integer: floating-point formats (%.1f) for {currNum} give an empty substitution (the formatting error is suppressed).
  • The unit label cannot be changed by a property; for a different label, 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