Repository navigation
GyroscopeTextIndicator
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 GyroscopeTextIndicator.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.GyroscopeSensorAtom;
import com.velitask.sdk.properties.GyroscopeAtomProperty;
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 angular velocity around the three camera axes and the vector magnitude from the gyroscope sensor. */
public class GyroscopeTextIndicator 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 = "gyroscopeText";
/* Prefix of the localization keys of this indicator. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the gyroscope sensor (angular velocity around the camera axes) (JSON key Gyroscope). */
private final GyroscopeAtomProperty mGyroscope = new GyroscopeAtomProperty();
/* 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 GyroscopeTextTemplateProperty mTemplate = new GyroscopeTextTemplateProperty();
@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[]{
mGyroscope,
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) {
GyroscopeTextContext ctx = (GyroscopeTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The current sample for the frame time; inside a pause it is the last sample before it. */
CurrentAtom<GyroscopeSensorAtom> current = ctx.gyroscope.queryCurrent();
/* The sample itself: null outside the recording. */
GyroscopeSensorAtom 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 °/s), calculates the magnitude and substitutes the values and the unit label into the template. */
private String makeText(GyroscopeTextContext ctx, GyroscopeSensorAtom atom, boolean fromGap) {
long time = ctx.gyroscope.rawTime;
double x = fromGap ? atom.getNextX() : atom.calcX(time);
double y = fromGap ? atom.getNextY() : atom.calcY(time);
double z = fromGap ? atom.getNextZ() : atom.calcZ(time);
double total = Math.sqrt(x * x + y * y + z * z);
String unit = localized(KEY + ".unit");
return ctx.template.makeText(total, x, y, z, unit);
}
/* Text template of this indicator: the default value, the list of variables and the skin. */
static class GyroscopeTextTemplateProperty 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", "%.1f", localized(KEY_PROPERTY_VAR + ".total")),
new Var("x", "%.1f", localized(KEY_PROPERTY_VAR + ".x")),
new Var("y", "%.1f", localized(KEY_PROPERTY_VAR + ".y")),
new Var("z", "%.1f", 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 GyroscopeTextContext extends IndicatorContext {
public final GyroscopeAtomProperty.SensorContext gyroscope;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public GyroscopeTextContext(FrameContext frame) {
super(frame);
gyroscope = mGyroscope.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 GyroscopeTextContext(frame);
}
}GyroscopeTextIndicator shows the angular velocity of the camera rotation around the three axes (X, Y, Z) and the magnitude of this vector in degrees per second. The data comes from the gyroscope sensor of the selected source (currently it is created by the GoPro telemetry import); the look of the string is set by the template.
Data. GyroscopeAtomProperty (table sensors_gyro_data) creates ctx.gyroscope. queryCurrent() returns a CurrentAtom<GyroscopeSensorAtom>: 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 real numbers in degrees per second. 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. Four properties in the order Gyroscope, font, textAlign, textTemplate. There is no measurement property: there is a single angular velocity unit. The compact skin sets the template {total}.
Drawing. The magnitude is calculated as sqrt(x² + y² + z²). The unit label is read from localization (KEY + ".unit", this is "°/s"), 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.
- 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.
- There is a single angular velocity unit ("°/s"); there is no unit selection, unlike
AccelerometerTextIndicator. - 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()(five) must match the number ofmakeText(...)arguments.