Skip to content

TemperatureTextIndicator

roosit edited this page May 31, 2026 · 3 revisions

TemperatureTextIndicator

Up | Previous | Next | JSON | Русский

Full source code with brief explanations

Below is the full source code of the class in documentation form. Comments inside the block explain the purpose of class parts, but they are not stored in the working file TemperatureTextIndicator.java: production code stays clean, and explanations live in the wiki.

package com.velitask.plugin.official;

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.TemperatureSensorAtom;
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.TemperatureAtomProperty;
import com.velitask.sdk.properties.TextAlignProperty;
import com.velitask.sdk.properties.TextTemplateProperty;
import com.velitask.sdk.properties.measurement.MeasurementProperty;
import com.velitask.units.UnitKind;
import com.velitask.units.format.UnitValue;
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 temperature indicator: reads a temperature sensor value,
 * converts it to the selected temperature scale, and draws a string.
 */
public class TemperatureTextIndicator extends Indicator {

    /* Stable indicator name for saved projects, templates, and localization. */
    public static final String NAME = "temperatureText";

    /* Base localization key for title, description, tags, template, and skin. */
    private static final String KEY = _KEY + "." + NAME;

    /* Temperature data source. The atom stores temperature in degrees Celsius. */
    private final TemperatureAtomProperty mTemperature = new TemperatureAtomProperty();

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

    /* Text alignment inside the layer. */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    /*
     * Unit selection only for temperature.
     * Celsius/Fahrenheit/Kelvin conversion is done in the velitask.units library.
     */
    private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.TEMPERATURE);

    /* String template with currNum, currUnit, and celsius variables. */
    private final TemperatureTextTemplateProperty mTemplate = new TemperatureTextTemplateProperty();

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

    @Override
    public String getTitle() {
        return localized(KEY + ".title");
    }

    @Override
    public String getDescription() {
        return localized(KEY + ".description");
    }

    @Override
    public String getTags() {
        return localized(KEY + ".tags");
    }

    /*
     * Initial layout: top-right, 100 px margin, 360 x 120 layer.
     */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.RIGHT);
        maket.setLayerMargin(null, 100d, 100d, null);
        maket.setLayerSize(360d, 120d);
    }

    /*
     * Registers properties in UI order:
     * source, font, alignment, units, template.
     */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mTemperature,
            mText,
            mTextAlign,
            mUnits,
            mTemplate
        };
    }

    /*
     * In the property panel, alignment and units belong to appearance.
     */
    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mTextAlign, PropertyGroup.APPEARANCE);
        config.set(mUnits, PropertyGroup.APPEARANCE);
    }

    /*
     * Compact skin changes only the template: no space between number and unit.
     */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("compact", localized(KEY + ".skin.compact"))
            .set(mTemplate.skinCompact())
            .build()
        };
    }

    /*
     * Render reads the current temperature atom, formats it through
     * MeasurementProperty.Context, and draws the final text string.
     */
    @Override
    public void render(IndicatorContext indicatorContext) {
        TemperatureTextContext ctx = (TemperatureTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /*
         * Player time is UTC, while sensor data is queried by raw-time.
         * In preview, time is clamped to the selected sensor range.
         */
        long rawTime = mTemperature.convertToRawTime(ctx.player.time);
        if (ctx.player.isPreview) {
            rawTime = mTemperature.clampToSensorRange(rawTime);
        }
        TemperatureSensorAtom atom = mTemperature.queryAtom(rawTime);
        if (atom == null) {
            return;
        }

        /*
         * The atom interpolates temperature at rawTime.
         * temperatureParts returns the value and label for the selected unit.
         */
        double celsius = atom.calcTemperature(rawTime);
        UnitValue curr = ctx.units.temperatureParts(celsius);

        /* The template assembles the final string from selected units and raw Celsius. */
        String text = ctx.template.makeText(
                curr.value(), curr.unit(), celsius
        );

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                RenderingHints.VALUE_ANTIALIAS_ON);

        Font font = ctx.text.toFont(ctx.scale);
        g.setFont(font);
        g.setColor(ctx.text.color.value);

        ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
    }

    /*
     * Nested template property.
     * Variable order must match makeText argument order.
     */
    static class TemperatureTextTemplateProperty 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";

        @Override
        protected String defineTemplate() {
            return localized(KEY_PROPERTY_TEMPLATE + ".default");
        }

        @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")),
                new Var("celsius", "%.1f", localized(KEY_PROPERTY_VAR + ".celsius"))
            };
        }

        public IndicatorSkinTransfer skinCompact() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
        }
    }

    /* Context fixes property snapshots for a specific frame. */
    public class TemperatureTextContext extends IndicatorContext {

        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final MeasurementProperty.Context units;
        public final TextTemplateProperty.TextTemplateContext template;

        public TemperatureTextContext(Player player, Canvas canvas) {
            super(player, canvas);
            text = mText.createContext();
            textAlign = mTextAlign.createContext();
            units = mUnits.createContext();
            template = mTemplate.createContext();
        }
    }

    @Override
    public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
        return new TemperatureTextContext(player, canvas);
    }
}

Purpose

TemperatureTextIndicator is a text indicator for current temperature. It reads the value from the selected temperature sensor, converts it to the selected measurement scale, and outputs a string through an editable template. The main use case is showing GPX/telemetry temperature as a simple text overlay.

How it works

Data. Through TemperatureAtomProperty, the indicator reads the current TemperatureSensorAtom. Player time is converted to the selected source raw-time through the sensor property; in preview it is also clamped to the sensor range. The value comes from atom.calcTemperature(rawTime), so interpolation is used inside the atom window.

Settings. Five properties: temperature sensor, font+color (FontColorProperty), alignment (TextAlignProperty), measurement units (MeasurementProperty for UnitKind.TEMPERATURE), and template (TextTemplateProperty). Units and labels come from velitask.units: Celsius, Fahrenheit, or Kelvin.

Drawing. Celsius temperature is passed to MeasurementProperty.Context.temperatureParts(celsius), which returns UnitValue: numeric value in the selected scale and localized unit label. Then TextTemplateProperty assembles the string, and TextAlignProperty.drawText draws it inside the layer with antialiasing enabled.

Features and limitations

  • If no temperature sensor is selected or there is no atom at the current time, the indicator draws nothing.
  • 0.0 C is a valid temperature value and is not treated as missing data.
  • Skin compact changes only the template: {currNum}{currUnit} instead of {currNum} {currUnit}.
  • The template receives both the converted value (currNum/currUnit) and the original Celsius value (celsius) for special labels.

Clone this wiki locally