Skip to content

GForceTextIndicator

roosit edited this page Nov 15, 2026 · 1 revision

GForceTextIndicator

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 GForceTextIndicator.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.CurrentAtom;
import com.velitask.sdk.data.GForceSensorAtom;
import com.velitask.sdk.db.DataCacheRule;
import com.velitask.sdk.db.DataParams;
import com.velitask.sdk.db.SensorData;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.GForceAtomProperty;
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 com.velitask.sdk.properties.measurement.MeasurementProperty;
import com.velitask.units.UnitKind;
import com.velitask.units.acceleration.Acceleration;
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 G-force indicator: the total value, the longitudinal and lateral components, the peak. */
public class GForceTextIndicator extends Indicator {

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

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

    /* The pause option captions are shared with the circular indicator. */
    private static final String KEY_PAUSE = _KEY + ".gforce.pause";

    /* The dash for a pause with the CENTER option is also shared with the circular indicator. */
    private static final String KEY_DASH = _KEY + ".gforce.dash";

    /* The G-force sensor (JSON key "GForce"). */
    private final GForceAtomProperty mGForce = new GForceAtomProperty();

    {
        /* Peak aggregate: the maximum of the sum of squares of the longitudinal and lateral components among the records up to the current time. */
        mGForce.aggregate("peak")
                .select("MAX(longitudinal * longitudinal + lateral * lateral) AS peak2")
                .where("timeRaw <= {to}")
                .cache(DataCacheRule.none())
                .build();
    }

    /* Font and color of the text (JSON key "font"), default size. */
    private final FontColorProperty mText = new FontColorProperty();

    /* Position of the text inside the layer; centered by default. */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    /* Measurement system and acceleration unit (JSON key "measurement"). */
    private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.ACCELERATION);

    /* String template with variables. */
    private final GForceTextTemplateProperty mTemplate = new GForceTextTemplateProperty();

    /* Behavior in a recording pause; a dash (CENTER) by default. */
    private final EnumArrayProperty<GForcePause> mPause
            = new EnumArrayProperty<>(GForcePause.class) {
        @Override
        public String getName() {
            return "pause";
        }

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

        @Override
        protected String[] defineTitles() {
            GForcePause[] vs = GForcePause.values();
            String[] t = new String[vs.length];
            for (int i = 0; i < vs.length; i++) {
                t[i] = localized(KEY_PAUSE + "." + vs[i].name().toLowerCase());
            }
            return t;
        }
    };

    @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 320 x 120, top/left alignment, 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(320d, 120d);
    }

    /* The property order = the order in the settings panel and in JSON. */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mGForce,
            mText,
            mTextAlign,
            mUnits,
            mTemplate,
            mPause
        };
    }

    /* The alignment and the units are moved to the "Appearance" group. */
    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mTextAlign, PropertyGroup.APPEARANCE);
        config.set(mUnits, PropertyGroup.APPEARANCE);
    }

    /* The only skin, "compact", replaces the template with {total}. */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("compact", localized(KEY + ".skin.compact"))
            .set(mTemplate.skinCompact())
            .build()
        };
    }

    /* Drawing: choosing the text, the font and output by the alignment. */
    @Override
    public void render(IndicatorContext indicatorContext) {
        GForceTextContext ctx = (GForceTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        CurrentAtom<GForceSensorAtom> current = ctx.gforce.queryCurrent();
        GForceSensorAtom atom = current.atom();
        boolean fromGap = current.inPause();
        /* Outside the recording, and in a pause without a preceding recording, the layer stays empty. */
        if (atom == null) {
            return;
        }

        String text;
        /* In a pause with the CENTER option a dash is output instead of the template. */
        if (fromGap && ctx.pause.value == GForcePause.CENTER) {
            text = localized(KEY_DASH);
        } else {
            text = makeText(ctx, atom, fromGap);
        }

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

    /* Collects the values for the template: the total, the components and the peak are converted to the chosen unit. */
    private String makeText(GForceTextContext ctx, GForceSensorAtom atom, boolean fromGap) {
        long time = ctx.gforce.rawTime;
        int longitudinal = fromGap ? atom.getNextLongitudinal() : atom.calcLongitudinal(time);
        int lateral = fromGap ? atom.getNextLateral() : atom.calcLateral(time);
        Integer vertical = fromGap ? atom.getNextVertical() : atom.calcVertical(time);

        double total = Acceleration.magnitude(longitudinal, lateral, vertical);
        UnitValue totalParts = ctx.units.accelerationParts((int) Math.round(total));
        UnitValue longitudinalParts = ctx.units.accelerationParts(longitudinal);
        UnitValue lateralParts = ctx.units.accelerationParts(lateral);

        /* Peak: the root of the maximum sum of squares, converted to the chosen unit; 0 if there is no data. */
        double peak = 0d;
        SensorData data = ctx.gforce.queryItem("peak",
                DataParams.of("sensorId", ctx.gforce.sensorId).set("to", time));
        if (data != null && data.has("peak2") && data.get("peak2") != null) {
            double peak2 = ((Number) data.get("peak2")).doubleValue();
            peak = ctx.units.accelerationParts((int) Math.round(Math.sqrt(peak2))).value();
        }

        /* The argument order matches the order of the variables in defineVars(): total, longitudinal, lateral, peak, unit. */
        return ctx.template.makeText(
                totalParts.value(),
                longitudinalParts.value(),
                lateralParts.value(),
                peak,
                totalParts.unit());
    }

    /* Text template of this indicator: default value, list of variables and skin. */
    static class GForceTextTemplateProperty 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: {total} {unit}. */
        @Override
        protected String defineTemplate() {
            return localized(KEY_PROPERTY_TEMPLATE + ".default");
        }

        /* Variables: name, default format and description. The order is the same as that of the makeText(...) arguments. */
        @Override
        protected Var[] defineVars() {
            return new Var[]{
                new Var("total", "%.2f", localized(KEY_PROPERTY_VAR + ".total")),
                new Var("longitudinal", "%.2f", localized(KEY_PROPERTY_VAR + ".longitudinal")),
                new Var("lateral", "%.2f", localized(KEY_PROPERTY_VAR + ".lateral")),
                new Var("peak", "%.2f", localized(KEY_PROPERTY_VAR + ".peak")),
                new Var("unit", "%s", localized(KEY_PROPERTY_VAR + ".unit"))
            };
        }

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

    /* Frame context: the values of all properties at the moment of the frame being drawn. */
    public class GForceTextContext extends IndicatorContext {

        public final GForceAtomProperty.SensorContext gforce;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final MeasurementProperty.Context units;
        public final TextTemplateProperty.TextTemplateContext template;
        public final EnumArrayProperty<GForcePause>.ArrayContext pause;

        public GForceTextContext(FrameContext frame) {
            super(frame);
            gforce = mGForce.createContext(frame);
            text = mText.createContext(frame);
            textAlign = mTextAlign.createContext(frame);
            units = mUnits.createContext(frame);
            template = mTemplate.createContext(frame);
            pause = mPause.createContext(frame);
        }
    }

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

Purpose

GForceTextIndicator shows G-force as a string by a template: the total value, the longitudinal and lateral components and the peak since the start of the recording. The data is taken from the G-force sensor of the selected source; the units (g or m/s²) are set by the measurement property. One skin, compact, is registered. The circular chart of the same G-force is drawn by GForceIndicator.

How it works

Data. GForceAtomProperty (JSON key GForce) creates ctx.gforce. queryCurrent() returns the record for the frame time and the pause flag (in a pause, the last record before it). Inside a recording the components are refined linearly (calcLongitudinal/Lateral/Vertical(rawTime)), in a pause the values at the end of the recording window are taken (getNext...). Outside a recording, and in a pause without a preceding recording, render draws nothing.

The peak is obtained by the peak aggregate from the initialization block: MAX(longitudinal * longitudinal + lateral * lateral) AS peak2 over the records with timeRaw <= {to}. In makeText the root is taken from peak2, the value is rounded to a whole mm/s² and converted to the chosen unit. If there is no data, the peak is 0.

Settings. defineProperties() has six properties in this order: GForce, font, textAlign, measurement, textTemplate, pause.

  • font — FontColorProperty with the default size (48, white).
  • textAlign — TextAlignProperty, centered by default (4).
  • measurement — MeasurementProperty(UnitKind.ACCELERATION): the measurement system, the unit caption format and the acceleration unit.
  • textTemplate — the nested GForceTextTemplateProperty with five variables; the default template is {total} {unit}.
  • pause — EnumArrayProperty<GForcePause>: CENTER (the default) or LAST, see GForcePause. The option captions are taken from the circular indicator keys (gforce.pause.*), the title is its own.

In configureDisplay() the alignment and the units are moved to the "Appearance" group.

Skin. compact replaces the template with {total}; the variable formats do not change.

Drawing. The text is chosen: with CENTER in a pause it is the dash —, otherwise the string from ctx.template.makeText(total, longitudinal, lateral, peak, unit). Then anti-aliasing is turned on, the font (the size is multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the string.

All numbers in makeText are already in the chosen unit: total is Acceleration.magnitude(longitudinal, lateral, vertical) rounded to a whole mm/s², longitudinal and lateral are the components with their own sign, unit is the unit caption.

Features and limitations

  • The peak takes into account only the longitudinal and lateral components and the record values without interpolation, while total also includes the vertical component and interpolation, so {peak} can turn out smaller than {total}.
  • The components are passed to the template with a sign, but without an explicit +: the sign is visible only for negative values.
  • In a pause with CENTER the template is not applied at all: only — is output, regardless of the template text.
  • The %d format for numeric variables gives an empty substitution (the formatting error is suppressed); for {unit} use %s.
  • The number of variables in defineVars() (five) must match the number of makeText(...) arguments.
  • The pause property stores the option index: the order of the GForcePause values must not be changed.

Clone this wiki locally