Repository navigation
OrientationTextIndicator
Up | Previous | Next | Русский
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 OrientationTextIndicator.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.OrientationSensorAtom;
import com.velitask.sdk.properties.OrientationAtomProperty;
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 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 roll, pitch and yaw of the camera calculated from the orientation sensor. */
public class OrientationTextIndicator 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 = "orientationText";
/* Prefix of the localization keys of this indicator. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the orientation sensor (the camera rotation quaternion) (JSON key Orientation). */
private final OrientationAtomProperty mOrientation = new OrientationAtomProperty();
/* 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 OrientationTextTemplateProperty mTemplate = new OrientationTextTemplateProperty();
@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[]{
mOrientation,
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) {
OrientationTextContext ctx = (OrientationTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The current sample for the frame time; inside a pause it is the last sample before it. */
CurrentAtom<OrientationSensorAtom> current = ctx.orientation.queryCurrent();
/* The sample itself: null outside the recording. */
OrientationSensorAtom 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: converts the quaternion to the roll, pitch and yaw angles (degrees) and substitutes them and the unit label into the template. */
private String makeText(OrientationTextContext ctx, OrientationSensorAtom atom, boolean fromGap) {
long time = fromGap ? atom.getNextTimeRaw() : ctx.orientation.rawTime;
double[] angles = OrientationAtomProperty.anglesDeg(atom, time);
String unit = localized(KEY + ".unit");
return ctx.template.makeText(angles[0], angles[1], angles[2], unit);
}
/* Text template of this indicator: the default value, the list of variables and the skin. */
static class OrientationTextTemplateProperty 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("roll", "%.1f", localized(KEY_PROPERTY_VAR + ".roll")),
new Var("pitch", "%.1f", localized(KEY_PROPERTY_VAR + ".pitch")),
new Var("yaw", "%.1f", localized(KEY_PROPERTY_VAR + ".yaw")),
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 OrientationTextContext extends IndicatorContext {
public final OrientationAtomProperty.SensorContext orientation;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public OrientationTextContext(FrameContext frame) {
super(frame);
orientation = mOrientation.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 OrientationTextContext(frame);
}
}OrientationTextIndicator shows the spatial position of the camera by three angles: roll, pitch and yaw, in degrees. The data comes from the orientation sensor of the selected source (currently it is created by the GoPro telemetry import), where the rotation is stored as a quaternion; the look of the string is set by the template.
Data. OrientationAtomProperty (table sensors_orient_data) creates ctx.orientation. queryCurrent() returns a CurrentAtom<OrientationSensorAtom>: atom() is the sample for the frame time, inPause() is the pause flag. The quaternion components w, x, y, z are refined by time through calcW/calcX/calcY/calcZ, the quaternion is normalized, after which the static method OrientationAtomProperty.anglesDeg(atom, time) converts it to angles: roll = atan2(2(wx + yz), 1 - 2(x² + y²)), pitch = asin(2(wy - zx)) (the argument is limited to -1..1), yaw = atan2(2(wz + xy), 1 - 2(y² + z²)); the result is converted from radians to degrees. 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.
Settings. Four properties in the order Orientation, font, textAlign, textTemplate. There is no measurement property: the angles are always in degrees. The compact skin sets the template {roll}.
Drawing. The unit label is read from localization (KEY + ".unit", this is "°"), the string is built with ctx.template.makeText(roll, pitch, yaw, unit), then smoothing, the font (its size multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.
- Outside the recording (no sample) the layer is empty; during a pause the orientation at the end of the last sample before it is shown. There is no separate pause-behavior setting (as in
GForceTextIndicator). - The angle ranges follow from the formulas:
rollandyaware-180..180,pitchis-90..90. Angles can abruptly change sign at the range boundary. - A zero quaternion (norm 0) is not normalized and gives zero angles.
- 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
%.1fis meant for a fractional number: the format%dfor numeric variables gives an empty substitution (the formatting error is suppressed). - The number of variables in
defineVars()(four) must match the number ofmakeText(...)arguments.