Skip to content

AttitudeTextIndicator

roosit edited this page Nov 15, 2026 · 1 revision

AttitudeTextIndicator

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 AttitudeTextIndicator.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.AttitudeSensorAtom;
import com.velitask.sdk.properties.AttitudeAtomProperty;
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.units.angle.Angle;
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 yaw, pitch and roll of the aircraft or gimbal of a drone from the attitude sensor. */
public class AttitudeTextIndicator 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 = "attitudeText";

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

    /* Data source: the attitude sensor; the channel (aircraft or gimbal) is selected in the sensor list (JSON key Attitude). */
    private final AttitudeAtomProperty mAttitude = new AttitudeAtomProperty();

    /* 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();

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

    @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[]{
            mAttitude,
            mText,
            mTextAlign,
            mTemplate
        };
    }

    /* The text alignment is shown in the "Appearance" group. */
    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mTextAlign, 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) {
        AttitudeTextContext ctx = (AttitudeTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* The current sample for the frame time; inside a pause it is the last sample before it. */
        CurrentAtom<AttitudeSensorAtom> current = ctx.attitude.queryCurrent();
        /* The sample itself: null outside the recording. */
        AttitudeSensorAtom 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: refines the yaw, pitch and roll for the frame time, brings each angle to the range (-180°, 180°] and substitutes them and the unit label into the template. */
    private String makeText(AttitudeTextContext ctx, AttitudeSensorAtom atom, boolean fromGap) {
        long time = fromGap ? atom.getNextTimeRaw() : ctx.attitude.rawTime;
        double yaw = Angle.normalizeDegSigned(atom.calcYaw(time));
        double pitch = Angle.normalizeDegSigned(atom.calcPitch(time));
        double roll = Angle.normalizeDegSigned(atom.calcRoll(time));
        String unit = localized(KEY + ".unit");

        return ctx.template.makeText(yaw, pitch, roll, unit);
    }

    /* Text template of this indicator: the default value, the list of variables and the skin. */
    static class AttitudeTextTemplateProperty 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("yaw", "%.1f", localized(KEY_PROPERTY_VAR + ".yaw")),
                new Var("pitch", "%.1f", localized(KEY_PROPERTY_VAR + ".pitch")),
                new Var("roll", "%.1f", localized(KEY_PROPERTY_VAR + ".roll")),
                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 AttitudeTextContext extends IndicatorContext {

        public final AttitudeAtomProperty.SensorContext attitude;
        public final FontColorProperty.FontColorContext text;
        public final TextAlignProperty.TextAlignContext textAlign;
        public final TextTemplateProperty.TextTemplateContext template;

        public AttitudeTextContext(FrameContext frame) {
            super(frame);
            attitude = mAttitude.createContext(frame);
            text = mText.createContext(frame);
            textAlign = mTextAlign.createContext(frame);
            template = mTemplate.createContext(frame);
        }
    }

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

Purpose

AttitudeTextIndicator shows the orientation angles of the aircraft or gimbal of a drone: yaw, pitch and roll, in degrees. The data comes from the attitude sensor of the selected source (currently it is created by the DJI telemetry import from the .SRT file); the look of the string is set by the template.

How it works

Data. AttitudeAtomProperty (table sensors_attitude_data) creates ctx.attitude. queryCurrent() returns a CurrentAtom<AttitudeSensorAtom>: atom() is the sample for the frame time, inPause() is the pause flag. The angles in the atom are stored in degrees, as the device recorded them; calcYaw, calcPitch, calcRoll refine them by time, interpolating by the *Delta increment (the shortest arc between neighboring samples, so a crossing of the circle boundary does not sweep the whole circle). Before display each angle is brought to the range (−180°, 180°] by the function Angle.normalizeDegSigned from velitask.units. Inside a pause the last sample before it is taken, and the angles are calculated for the moment atom.getNextTimeRaw(), the end of the window of this sample. If there is no sample (outside the recording), render draws nothing.

Channel. The attitude sensor has several channels: the aircraft (aircraft) and the gimbal (gimbal); the angles have no main channel. The channel is selected in the sensor list of the indicator settings; by default the first one is taken, the aircraft, because its sensor is created first.

Settings. Four properties in the order Attitude, font, textAlign, textTemplate. There is no measurement property: the angles are always in degrees. The compact skin sets the template {yaw}.

Drawing. The unit label is read from localization (KEY + ".unit", this is "°"), the string is built with ctx.template.makeText(yaw, pitch, roll, 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 shown angles lie in the range (−180°, 180°]; the value in the database is not changed. An angle can abruptly change sign at the range boundary.
  • The unit label cannot be changed by a property; for a different label remove {unit} from the template and write the text yourself.
  • The format %.1f 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() (four) must match the number of makeText(...) arguments.

Clone this wiki locally