Skip to content

TextIndicator

roosit edited this page Oct 16, 2026 · 2 revisions

TextIndicator

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 TextIndicator.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.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.StringProperty;
import com.velitask.sdk.properties.TextAlignProperty;
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;

/* Static text: a string, font, color and alignment. */
public class TextIndicator extends Indicator {

    /* Stable indicator name in the layer JSON. Must not be changed without a migration. */
    public static final String NAME = "text";

    /* Localization key prefix: Plugin.Indicator.text. */
    private static final String KEY = _KEY + "." + NAME;

    /* The string to output (JSON key "value"). The initial value comes from localization: "Текст" / "Text". */
    private final StringProperty mValue = new StringProperty(
            localized(KEY + ".value.default"),
            "value",
            localized(KEY + ".value.title")
    );

    /* Font and color (JSON key "font"), size 48, white color. */
    private final FontColorProperty mFont = new FontColorProperty();

    /* Text alignment; CENTER by default (index 4). */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    @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");
    }

    /* Default maket: layer 600 x 120, aligned top/left, margins (left, top, right, bottom) = null, 100, 100, null. */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.LEFT);
        maket.setLayerMargin(null, 100d, 100d, null);
        maket.setLayerSize(600d, 120d);
    }

    /* Property order = order in the settings panel and in JSON. There are no skins or display groups. */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mValue,
            mFont,
            mTextAlign
        };
    }

    /* Rendering: string, font, color, alignment. */
    @Override
    public void render(IndicatorContext indicatorContext) {
        TextContext ctx = (TextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* The string comes from the property; the indicator does not use sensor data. */
        String text = ctx.value.value;
        /* Guard against an empty string. */
        if (text == null || text.isEmpty()) {
            return;
        }

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

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

        /* The text is drawn with the selected alignment within the layer bounds. */
        ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
    }

    /* Frame context: property values at the moment of the frame being rendered. */
    public class TextContext extends IndicatorContext {

        public final StringProperty.StringContext value;
        public final FontColorProperty.FontColorContext font;
        public final TextAlignProperty.TextAlignContext textAlign;

        /* Property contexts are created per frame (createContext(frame)). */
        public TextContext(FrameContext frame) {
            super(frame);
            value = mValue.createContext(frame);
            font = mFont.createContext(frame);
            textAlign = mTextAlign.createContext(frame);
        }
    }

    /* Context factory, called for every frame. */
    @Override
    public IndicatorContext createContext(FrameContext frame) {
        return new TextContext(frame);
    }
}

Purpose

TextIndicator outputs an arbitrary static string. Project data and sensors are not used: the text does not depend on time.

How it works

Data. The only source is the value property (StringProperty). The initial value comes from localization (Plugin.Indicator.text.value.default): in the Russian locale it is "Текст", in the English one Text.

Settings. Three properties in the order value, font, textAlign.

  • value — the string; when the saved string is empty, StringProperty.get() returns the default value.
  • font — FontColorProperty, size 48, white color.
  • textAlign — TextAlignProperty, CENTER by default.

The indicator has no skins, no text template and no configureDisplay(), so all settings are shown in the main group.

Rendering. Anti-aliasing is enabled, the font is taken with ctx.scale applied, the color is set and the string is drawn through ctx.textAlign.drawText(...).

Features and limitations

  • An empty string cannot be set: StringProperty substitutes the default value for an empty string, so the text.isEmpty() check in render() never triggers in practice.
  • Multi-line text is not supported: the string is drawn with a single drawString call without splitting on line breaks.
  • The text is not wrapped to the layer width and is not shrunk: a long string goes beyond the layer bounds.
  • The property names (value, font, textAlign) are part of the saved format: renaming them breaks project loading.

Clone this wiki locally