Repository navigation
GearsTextIndicator
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 GearsTextIndicator.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.GearsSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.GearsAtomProperty;
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 current gear: the gear numbers and tooth counts of the sprockets, and the gear ratio. */
public class GearsTextIndicator extends Indicator {
/* Stable indicator name: it is part of the layer identifier in a project and must not be changed. */
public static final String NAME = "gearsText";
/* Prefix of this indicator's localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the gears sensor (JSON key Gears). */
private final GearsAtomProperty mGears = new GearsAtomProperty();
/* Font and color of the text. */
private final FontColorProperty mText = new FontColorProperty();
/* Position of the text inside the layer (nine options, centered by default). */
private final TextAlignProperty mTextAlign = new TextAlignProperty();
/* Template of the resulting string with variables in curly braces. */
private final GearsTextTemplateProperty mTemplate = new GearsTextTemplateProperty();
@Override
public String getName() {
return NAME;
}
/* Title, description and tags come 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 of 100 at the top and 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 property order defines the order of the settings and the order of the keys in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mGears,
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 just 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 from the template and outputs it. */
@Override
public void render(IndicatorContext indicatorContext) {
GearsTextContext ctx = (GearsTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* Gears sample for the current frame time: the values are discrete and are not interpolated; if there is no data, the frame stays empty. */
GearsSensorAtom atom = ctx.gears.queryAtom();
if (atom == null) {
return;
}
/* Argument order: front, rear, frontTeeth, rearTeeth, ratio (the ratio is the front sprocket teeth divided by the rear sprocket teeth). */
String text = ctx.template.makeText(atom.frontGearNum, atom.rearGearNum, atom.frontGear, atom.rearGear, atom.getRatio());
/* Text anti-aliasing. */
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
/* The font size scales 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 chosen alignment. */
ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
}
/* Text template of this indicator: default value, list of variables and skin. */
static class GearsTextTemplateProperty 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. */
@Override
protected String defineTemplate() {
return localized(KEY_PROPERTY_TEMPLATE + ".default");
}
/* Template variables: name, default format and description. The order must match the order of the makeText(...) arguments. */
@Override
protected Var[] defineVars() {
return new Var[]{
new Var("front", "%d", localized(KEY_PROPERTY_VAR + ".front")),
new Var("rear", "%d", localized(KEY_PROPERTY_VAR + ".rear")),
new Var("frontTeeth", "%d", localized(KEY_PROPERTY_VAR + ".frontTeeth")),
new Var("rearTeeth", "%d", localized(KEY_PROPERTY_VAR + ".rearTeeth")),
new Var("ratio", "%.2f", localized(KEY_PROPERTY_VAR + ".ratio"))
};
}
/* The skin changes only the template text; the variable formats stay the same. */
public IndicatorSkinTransfer skinCompact() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
}
}
/* Text with the current gear: the gear numbers and tooth counts of the sprockets, and the gear ratio. */
public class GearsTextContext extends IndicatorContext {
public final GearsAtomProperty.SensorContext gears;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public GearsTextContext(FrameContext frame) {
super(frame);
gears = mGears.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 GearsTextContext(frame);
}
}GearsTextIndicator shows the current gear: the numbers and tooth counts of the front and rear sprockets and the gear ratio. The data is taken from the gears sensor of the selected source. The sensor exists only for electronic groupsets: the values are restored from FIT shift events.
Data. GearsAtomProperty creates ctx.gears. queryAtom() returns a GearsSensorAtom for the frame time: the atom describes the gear in effect from one shift to the next, the values are discrete and are not interpolated. frontGearNum, rearGearNum, frontGear, rearGear and getRatio() are read. If there is no atom (a pause or outside the recording), render draws nothing.
Settings. GearsAtomProperty (the sensor), FontColorProperty, TextAlignProperty (CENTER by default) and the inner GearsTextTemplateProperty. There is no units property. The compact skin sets the template {front}-{rear}.
Drawing. The string is built through ctx.template.makeText(front, rear, frontTeeth, rearTeeth, ratio), then anti-aliasing, the font (the size is multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.
- No gears atom (a pause, outside the recording, or the source has no electronic groupset): the layer is empty.
- The data exists only for electronic groupsets that report shift events.
- The first four variables are designed for an integer (
%d),{ratio}for a fractional number (%.2f); a type mismatch gives an empty substitution (the formatting error is suppressed). - The number of variables in
defineVars()(five) must match the number ofmakeText(...)arguments.