Repository navigation
PedalDynamicsTextIndicator
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 PedalDynamicsTextIndicator.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.PedalDynamicsSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PedalDynamicsAtomProperty;
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 pedalling dynamics of one leg: the platform center offset and the power phase and peak angles. */
public class PedalDynamicsTextIndicator 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 = "pedalDynamicsText";
/* Prefix of the localization keys of this indicator. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the pedal dynamics sensor; the leg is determined by the selected sensor (JSON key PedalDynamics). */
private final PedalDynamicsAtomProperty mDynamics = new PedalDynamicsAtomProperty();
/* 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 PedalDynamicsTextTemplateProperty mTemplate = new PedalDynamicsTextTemplateProperty();
@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[]{
mDynamics,
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) {
PedalDynamicsTextContext ctx = (PedalDynamicsTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The sample for the current frame time; if there is no data, the frame stays empty. */
PedalDynamicsSensorAtom atom = ctx.dynamics.queryAtom();
if (atom == null) {
return;
}
/* The platform center offset in millimeters, refined between neighboring samples by the frame time. */
int pco = atom.calcPcoMm(ctx.dynamics.rawTime);
/* Argument order: pco, phaseStart, phaseEnd, peakStart, peakEnd; the phase and peak angles are step values, rounded to whole degrees. */
String text = ctx.template.makeText(pco, (int) Math.round(atom.phaseStartDeg), (int) Math.round(atom.phaseEndDeg), (int) Math.round(atom.peakStartDeg), (int) Math.round(atom.peakEndDeg));
/* 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);
}
/* Text template of this indicator: the default value, the list of variables and the skin. */
static class PedalDynamicsTextTemplateProperty 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("pco", "%d", localized(KEY_PROPERTY_VAR + ".pco")),
new Var("phaseStart", "%d", localized(KEY_PROPERTY_VAR + ".phaseStart")),
new Var("phaseEnd", "%d", localized(KEY_PROPERTY_VAR + ".phaseEnd")),
new Var("peakStart", "%d", localized(KEY_PROPERTY_VAR + ".peakStart")),
new Var("peakEnd", "%d", localized(KEY_PROPERTY_VAR + ".peakEnd"))
};
}
/* 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 pedalling dynamics of one leg: the platform center offset and the power phase and peak angles. */
public class PedalDynamicsTextContext extends IndicatorContext {
public final PedalDynamicsAtomProperty.SensorContext dynamics;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public PedalDynamicsTextContext(FrameContext frame) {
super(frame);
dynamics = mDynamics.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 PedalDynamicsTextContext(frame);
}
}PedalDynamicsTextIndicator shows the pedalling dynamics of one leg: the platform center offset (PCO) in millimeters, the power phase angles and the peak angles in degrees. The leg is determined by the selected sensor. The data is available only from power-meter pedals that record these fields (FIT).
Data. PedalDynamicsAtomProperty creates ctx.dynamics. queryAtom() returns a PedalDynamicsSensorAtom for the frame time: atom.calcPcoMm(ctx.dynamics.rawTime) linearly refines the offset between samples, while the angles phaseStartDeg, phaseEndDeg, peakStartDeg, peakEndDeg are taken as they are (the angles are not interpolated, to avoid false values at the crossing of 360°). The indicator knows nothing about leg channels: the sensor is selected in the property. If there is no sample (a pause or outside the recording), render draws nothing.
Settings. PedalDynamicsAtomProperty (the sensor), FontColorProperty, TextAlignProperty (default CENTER) and the internal PedalDynamicsTextTemplateProperty. There is no units property: "mm" and "°" are part of the default template as plain text. The compact skin sets the template {pco}.
Drawing. The values are brought to integers, the string is built with ctx.template.makeText(pco, phaseStart, phaseEnd, peakStart, peakEnd), then smoothing, the font (its size multiplied by ctx.scale) and the color are set, and ctx.textAlign.drawText(...) outputs the text.
- No sensor data, a pause, or outside the recording means an empty layer.
- The data is available only from power-meter pedals that record these fields (FIT).
- One leg is shown, the one whose sensor is selected; a second indicator is needed for the other leg.
- The
%dformats are meant for an integer: floating-point formats (%.1f) give an empty substitution (the formatting error is suppressed). - The labels "mm" and "°" in the template are plain text: the default template depends on the language, but an already saved template stays unchanged.
- The number of variables in
defineVars()(five) must match the number ofmakeText(...)arguments.