Skip to content

AccelerometerTextIndicator

roosit edited this page Nov 15, 2026 · 1 revision

AccelerometerTextIndicator

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 AccelerometerTextIndicator.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.AccelerometerSensorAtom;
import com.velitask.sdk.properties.AccelerometerAtomProperty;
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.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 with the acceleration along the three camera axes and the vector magnitude from the accelerometer sensor. */
public class AccelerometerTextIndicator extends Indicator {

    /* Stable indicator name: it is part of the layer identifier in a project, it must not be changed. */
    public static final String NAME = "accelerometerText";

    /* Prefix of the localization keys of this indicator. */
    private static final String KEY = _KEY + "." + NAME;

    /* Data source: the accelerometer sensor (acceleration along the camera axes) (JSON key Accelerometer). */
    private final AccelerometerAtomProperty mAccelerometer = new AccelerometerAtomProperty();

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

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

    /* Acceleration units: the measurement system, the label format and the unit (JSON key "measurement"). */
    private final MeasurementProperty mUnits = new MeasurementProperty(UnitKind.ACCELERATION);

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

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

    /* Default maket: top left, margins 100 from the top and the 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 order of the properties defines the order of the settings and the order of the keys in JSON. */
    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mAccelerometer,
            mText,
            mTextAlign,
            mUnits,
            mTemplate
        };
    }

    /* The text alignment and the 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 only 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 by the template and outputs it. */
    @Override
    public void render(IndicatorContext indicatorContext) {
        AccelerometerTextContext ctx = (AccelerometerTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* The current sample for the frame time; inside a pause it is the last sample before it. */
        CurrentAtom<AccelerometerSensorAtom> current = ctx.accelerometer.queryCurrent();
        /* The sample itself: null outside the recording. */
        AccelerometerSensorAtom atom = current.atom();
        /* Flag: the value is taken from a pause rather than from a sample. */
        boolean fromGap = current.inPause();
        if (atom == null) {
            return;
        }

        /* The string by the template from the atom values. */
        String text = makeText(ctx, atom, fromGap);

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

    /* Builds the string: takes x, y, z (in mm/s²), calculates the magnitude, converts the values to the selected unit and substitutes them into the template. */
    private String makeText(AccelerometerTextContext ctx, AccelerometerSensorAtom atom, boolean fromGap) {
        long time = ctx.accelerometer.rawTime;
        int x = fromGap ? atom.getNextX() : atom.calcX(time);
        int y = fromGap ? atom.getNextY() : atom.calcY(time);
        int z = fromGap ? atom.getNextZ() : atom.calcZ(time);

        double total = Acceleration.magnitude(x, y, z);
        UnitValue totalParts = ctx.units.accelerationParts((int) Math.round(total));

        return ctx.template.makeText(
                totalParts.value(),
                ctx.units.accelerationParts(x).value(),
                ctx.units.accelerationParts(y).value(),
                ctx.units.accelerationParts(z).value(),
                totalParts.unit());
    }

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

        /* Template variables: the name, the default format and the explanation. The order must match the order of the makeText(...) arguments. */
        @Override
        protected Var[] defineVars() {
            return new Var[]{
                new Var("total", "%.2f", localized(KEY_PROPERTY_VAR + ".total")),
                new Var("x", "%.2f", localized(KEY_PROPERTY_VAR + ".x")),
                new Var("y", "%.2f", localized(KEY_PROPERTY_VAR + ".y")),
                new Var("z", "%.2f", localized(KEY_PROPERTY_VAR + ".z")),
                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: a snapshot of the values of all properties at the current time. */
    public class AccelerometerTextContext extends IndicatorContext {

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

        public AccelerometerTextContext(FrameContext frame) {
            super(frame);
            accelerometer = mAccelerometer.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 AccelerometerTextContext(frame);
    }
}

Purpose

AccelerometerTextIndicator shows the acceleration along the three camera axes (X, Y, Z) and the magnitude of this vector. The data comes from the accelerometer sensor of the selected source (currently it is created by the GoPro telemetry import); the units, g or m/s², are set by the measurement property, and the look of the string is set by the template.

How it works

Data. AccelerometerAtomProperty (table sensors_accel_data) creates ctx.accelerometer. queryCurrent() returns a CurrentAtom<AccelerometerSensorAtom>: atom() is the sample for the frame time, inPause() is the pause flag. Inside a recording this is the sample covering the frame time, and calcX/calcY/calcZ(rawTime) linearly refine the values between the current and the next sample. The axes are stored as integers in mm/s². Inside a pause this is the last sample before it, and the values at the end of its window are read from it: getNextX/Y/Z(). If there is no sample (outside the recording), render draws nothing.

Settings. Five properties in the order Accelerometer, font, textAlign, measurement, textTemplate. measurement is MeasurementProperty(UnitKind.ACCELERATION): the measurement system, the label format and the acceleration unit (g, m/s²). In configureDisplay() the alignment and the units are moved to the "Appearance" group. The compact skin sets the template {total}.

Drawing. The magnitude is calculated as Acceleration.magnitude(x, y, z) over the integer values in mm/s² and rounded to an integer, then ctx.units.accelerationParts(...) converts it to the selected unit and returns a "number, label" pair (UnitValue). The X, Y, Z axes are converted to the same unit one by one. The string is built with ctx.template.makeText(total, x, y, z, unit), then smoothing, the font (its size multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.

Features and limitations

  • Outside the recording (no sample) the layer is empty; during a pause the value at the end of the last sample before it is shown. There is no separate pause-behavior setting (as in GForceTextIndicator).
  • The X, Y, Z axes are tied to the camera, not to the direction of movement.
  • With the measurement system "From global settings" the acceleration unit is taken from the application's global settings.
  • The {unit} label depends on the unit and the selected label format and is not set separately; for a different label remove {unit} from the template and write the text yourself.
  • The format %.2f is meant for a fractional number: the format %d for numeric variables gives an empty substitution (the formatting error is suppressed).
  • The number of variables in defineVars() (five) must match the number of makeText(...) arguments.

Clone this wiki locally