Repository navigation
LapTextIndicator
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 kept in the working file LapTextIndicator.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.LapSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.LapAtomProperty;
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 lap number; the total number of laps and the remainder are available. */
public class LapTextIndicator extends Indicator {
/* Stable indicator name: it is part of the layer identifier in the project, it must not be changed. */
public static final String NAME = "lapText";
/* Prefix of the localization keys of this indicator. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the lap sensor (JSON key Lap). */
private final LapAtomProperty mLap = new LapAtomProperty();
/* 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 LapTextTemplateProperty mTemplate = new LapTextTemplateProperty();
@Override
public String getName() {
return NAME;
}
/* The title, the description and the 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 at the top and on 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 properties determines the order of settings and the order of keys in JSON. */
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mLap,
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 "full" ("Lap N of M") changes only the text template. */
@Override
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("full", localized(KEY + ".skin.full"))
.set(mTemplate.skinFull())
.build()
};
}
/* Draws one frame: reads the sensor data, builds the string by the template and outputs it. */
@Override
public void render(IndicatorContext indicatorContext) {
LapTextContext ctx = (LapTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The sensor sample for the current frame time; if there is no data, the frame stays empty. */
LapSensorAtom atom = ctx.lap.queryAtom();
if (atom == null) {
return;
}
/* Index of the current lap at the frame moment. */
int lap = atom.lapIndex;
/* The last lap of the sensor: the total number and the remainder come from here. */
LapSensorAtom last = ctx.lap.queryLast();
int lastIndex = last != null ? last.lapIndex : lap;
/* Total laps = the index of the last one + 1. */
int total = lastIndex + 1;
/* Laps left; never negative. */
int left = Math.max(0, lastIndex - lap);
/* The order of arguments: lap, total, left. */
String text = ctx.template.makeText(lap, total, left);
/* Text antialiasing. */
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);
}
/* The text template of this indicator: the default value, the list of variables and the skin. */
static class LapTextTemplateProperty 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 the 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("lap", "%d", localized(KEY_PROPERTY_VAR + ".lap")),
new Var("total", "%d", localized(KEY_PROPERTY_VAR + ".total")),
new Var("left", "%d", localized(KEY_PROPERTY_VAR + ".left"))
};
}
/* The skin changes only the template text, the variable formats stay the same. */
public IndicatorSkinTransfer skinFull() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".full"));
}
}
/* The values of all properties fixed for one frame. */
public class LapTextContext extends IndicatorContext {
public final LapAtomProperty.SensorContext lap;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public LapTextContext(FrameContext frame) {
super(frame);
lap = mLap.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 LapTextContext(frame);
}
}LapTextIndicator shows the number of the current lap and, optionally, the total number of laps and the remainder. The data is taken from the lap sensor of the selected source. By default only {lap} is output; the "Lap N of M" skin sets Lap {lap} of {total}.
Data. LapAtomProperty creates ctx.lap. queryAtom() returns the LapSensorAtom in effect at the frame time, and lapIndex is read from it. queryLast() returns the last lap of the sensor (a LAST manager with a cache of one value). From them total = lastIndex + 1 and left = max(0, lastIndex - lap) are calculated; if there is no last lap, lastIndex equals the current index. There are no fractional values: all the data is integer.
Settings. Four properties: LapAtomProperty, FontColorProperty, TextAlignProperty (CENTER by default) and the internal LapTextTemplateProperty with three variables. There is no units property. The skin is called full and sets the template Lap {lap} of {total}.
Drawing. The string is built through ctx.template.makeText(lap, total, left), then antialiasing, the font (the size is multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.
- No lap sample at the moment: the layer is empty.
- The numbering depends on the data: in the FIT/TCX loaders the lap index starts from zero (
mLapCounter++), as in the lap detector by geodata. The calculationtotal = lastIndex + 1assumes the same numbering from zero, so{lap}for the first lap equals0, and in the "Lap N of M" skin the first lap looks like "Lap 0 of N". This is the behavior of the code, not a setting. - The formats of the variables are integer only (
%d): floating-point formats give an empty substitution. - The number of variables in
defineVars()(three) must match the number ofmakeText(...)arguments. - The only indicator of this group where the skin is called
full, notcompact.