Repository navigation
PowerTextIndicator
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 PowerTextIndicator.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.CyclingPowerSensorAtom;
import com.velitask.sdk.properties.CyclingPowerAtomProperty;
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 current power of the rider from the power sensor. */
public class PowerTextIndicator 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 = "powerText";
/* Prefix of the localization keys of this indicator. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the power sensor (JSON key CyclingPower). */
private final CyclingPowerAtomProperty mPower = new CyclingPowerAtomProperty();
/* 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 PowerTextTemplateProperty mTemplate = new PowerTextTemplateProperty();
@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 right, 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.RIGHT);
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[]{
mPower,
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) {
PowerTextContext ctx = (PowerTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The sensor sample for the current frame time; if there is no data, the frame stays empty. */
CyclingPowerSensorAtom atom = ctx.power.queryAtom();
if (atom == null) {
return;
}
/* Power in watts, refined between neighboring samples (an integer). */
int power = atom.calcPower(ctx.power.rawTime);
/* The unit caption depends on the plugin language. */
String unit = localized(KEY + ".unit");
/* The order of arguments: currNum, currUnit. */
String text = ctx.template.makeText(power, unit);
/* 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 PowerTextTemplateProperty 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("currNum", "%d", localized(KEY_PROPERTY_VAR + ".currNum")),
new Var("currUnit", "%s", localized(KEY_PROPERTY_VAR + ".currUnit"))
};
}
/* The skin changes only the template text, the variable formats stay the same. */
public IndicatorSkinTransfer skinCompact() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
}
}
/* The values of all properties fixed for one frame. */
public class PowerTextContext extends IndicatorContext {
public final CyclingPowerAtomProperty.SensorContext power;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public PowerTextContext(FrameContext frame) {
super(frame);
power = mPower.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 PowerTextContext(frame);
}
}PowerTextIndicator shows the current power of the rider in watts. The data is taken from the power sensor (SensorType.CYCLING_POWER) of the selected source. The unit caption ("Вт" / W) is determined by the language of the plugin and is not configurable.
Data. CyclingPowerAtomProperty creates ctx.power. queryAtom() gives a CyclingPowerSensorAtom for the frame time; atom.calcPower(ctx.power.rawTime) returns the integer power, linearly refined between the current and the next sample. If there is no sample, render draws nothing.
Settings. Four properties: CyclingPowerAtomProperty (the sensor, JSON key CyclingPower), FontColorProperty, TextAlignProperty (CENTER by default) and the internal PowerTextTemplateProperty. There is no units property. The compact skin sets the template {currNum}{currUnit}.
Drawing. The unit caption is read from the localization (KEY + ".unit"), the string is built through ctx.template.makeText(power, unit), then antialiasing, the font (the size is multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.
- No sensor data: the layer is empty.
- The name of the Java field of the sensor property is
mPower, and the JSON key isCyclingPower(the name is taken from the SDK property). - The
%dformat is meant for an integer: floating-point formats for{currNum}give an empty substitution. - The number of variables in
defineVars()(two) must match the number ofmakeText(...)arguments.