Skip to content

TemperatureTextIndicator

roosit edited this page Oct 16, 2026 · 3 revisions

TemperatureTextIndicator

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 kept in the working file TemperatureTextIndicator.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.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 with the current temperature from a temperature sensor; the units of measurement are configurable. */
public class TemperatureTextIndicator extends Indicator {

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

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

    /* The data source: a temperature sensor (JSON key Temperature). */
    private final TemperatureAtomProperty mTemperature = new TemperatureAtomProperty();

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

    /* The position of the text inside the layer (nine options, centered by default). */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    /* The temperature units: system, label format and scale. */
    private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.TEMPERATURE);

    /* The template of the resulting string with variables in curly braces. */
    private final TemperatureTextTemplateProperty mTemplate = new TemperatureTextTemplateProperty();

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

    /* The title, description and tags are taken 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");
    }

    /* The default maket: top right, margins of 100 at the top and right, size 360×120. */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.RIGHT);
        maket.setLayerMargin(null, 100d, 100d, null);
        maket.setLayerSize(360d, 120d);
    }

    /* The property order sets the order of the settings and the order of the keys in JSON. */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mTemperature,
            mText,
            mTextAlign,
            mUnits,
            mTemplate
        };
    }

    /* Text alignment and units are shown in the "Appearance" group. */
    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mTextAlign, PropertyGroup.APPEARANCE);
        config.set(mUnits, 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 draws it. */
    @Override
    public void render(IndicatorContext indicatorContext) {
        TemperatureTextContext ctx = (TemperatureTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

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

        /* The temperature in degrees Celsius, refined between neighboring samples by the frame time. */
        double celsius = atom.calcTemperature(ctx.temperature.rawTime);
        /* Conversion to the selected scale and the unit label according to the format. */
        UnitValue curr = ctx.units.temperatureParts(celsius);

        /* Argument order: currNum, currUnit, celsius. */
        String text = ctx.template.makeText(
                curr.value(), curr.unit(), celsius
        );

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

    /* This indicator's text template: the default value, the list of variables and a skin. */
    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";

        /* The default template is taken from the localization. */
        @Override
        protected String defineTemplate() {
            return localized(KEY_PROPERTY_TEMPLATE + ".default");
        }

        /* The template variables: name, default format and explanation. The order must match the order of the makeText(...) arguments. */
        @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"))
            };
        }

        /* The skin changes only the template text; the variable formats stay the same. */
        public IndicatorSkinTransfer skinCompact() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
        }
    }

    /* The values of all properties captured for one frame. */
    public class TemperatureTextContext extends IndicatorContext {

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

        public TemperatureTextContext(FrameContext frame) {
            super(frame);
            temperature = mTemperature.createContext(frame);
            text = mText.createContext(frame);
            textAlign = mTextAlign.createContext(frame);
            units = mUnits.createContext(frame);
            template = mTemplate.createContext(frame);
        }
    }

    /* Creates the frame context for the indicator. */
    @Override
    public IndicatorContext createContext(FrameContext frame) {
        return new TemperatureTextContext(frame);
    }
}

Purpose

TemperatureTextIndicator is a text indicator of the current temperature. The value is taken from the temperature sensor of the selected source, converted to the selected scale (Celsius, Fahrenheit, Kelvin) and inserted into a configurable template. There is a "Compact" skin with no space between the number and the unit.

How it works

Data. ctx.temperature is available in the frame context through TemperatureAtomProperty. queryAtom() returns the TemperatureSensorAtom for the frame time (rawTime; in preview mode the time is limited to the sensor range). The temperature is taken as atom.calcTemperature(ctx.temperature.rawTime) — a linear refinement between the current sample and the next one, in degrees Celsius. If there is no sample (atom == null), render draws nothing.

Settings. Five properties: TemperatureAtomProperty (the sensor), FontColorProperty (font and color), TextAlignProperty (alignment, CENTER by default), MeasurementProperty(UnitKind.TEMPERATURE) (the system, format and temperature scale) and the internal TemperatureTextTemplateProperty (the template). configureDisplay places the alignment and the units in the PropertyGroup.APPEARANCE group. The only skin, compact, sets the template {currNum}{currUnit}.

Drawing. The layer's Graphics2D, the font (the size is multiplied by ctx.scale) and the color are taken from the context. The string is built through ctx.template.makeText(curr.value(), curr.unit(), celsius), anti-aliasing is turned on, and ctx.textAlign.drawText(...) draws the text into the ctx.width × ctx.height rectangle.

Particulars and limitations

  • No sensor data — the layer is empty (even the unit label is not drawn).
  • The %.1f variable format is applied to a double; an integer format (%d) for {currNum} and {celsius} leads to an empty substitution: the formatting error is not thrown outward, and the variable is replaced with an empty string.
  • The number of variables in defineVars() must match the number of makeText(...) arguments (three here), otherwise an exception is thrown.
  • The template and context classes are nested: the template is a static class, the context is a public class.
  • With the "From global settings" measurement system, the scale is taken from the general unit settings, not from the temperature property.

Clone this wiki locally