Repository navigation
PedalSmoothnessTextIndicator
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 PedalSmoothnessTextIndicator.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.PedalSmoothnessSensorAtom;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PedalSmoothnessAtomProperty;
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 overall pedal smoothness. */
public class PedalSmoothnessTextIndicator 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 = "pedalSmoothnessText";
/* Prefix of the localization keys of this indicator. */
private static final String KEY = _KEY + "." + NAME;
/* Data source: the pedal smoothness sensor (JSON key PedalSmoothness). */
private final PedalSmoothnessAtomProperty mSmoothness = new PedalSmoothnessAtomProperty();
/* 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 PedalSmoothnessTextTemplateProperty mTemplate = new PedalSmoothnessTextTemplateProperty();
@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[]{
mSmoothness,
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) {
PedalSmoothnessTextContext ctx = (PedalSmoothnessTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The sample for the current frame time; if there is no data, the frame stays empty. */
PedalSmoothnessSensorAtom atom = ctx.smoothness.queryAtom();
if (atom == null) {
return;
}
/* The pedal smoothness, refined by the frame time and rounded to whole percent. */
int ps = (int) Math.round(atom.calcSmoothnessPercent(ctx.smoothness.rawTime));
/* Argument order: ps. */
String text = ctx.template.makeText(ps);
/* 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 PedalSmoothnessTextTemplateProperty 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("ps", "%d", localized(KEY_PROPERTY_VAR + ".ps"))
};
}
/* 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 overall pedal smoothness. */
public class PedalSmoothnessTextContext extends IndicatorContext {
public final PedalSmoothnessAtomProperty.SensorContext smoothness;
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public PedalSmoothnessTextContext(FrameContext frame) {
super(frame);
smoothness = mSmoothness.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 PedalSmoothnessTextContext(frame);
}
}PedalSmoothnessTextIndicator shows the overall pedal smoothness in percent. There is one smoothness sensor per source and it has no separate leg channels. The data is available only from power-meter pedals that record these fields (FIT).
Data. PedalSmoothnessAtomProperty creates ctx.smoothness. queryAtom() returns a PedalSmoothnessSensorAtom for the frame time; atom.calcSmoothnessPercent(ctx.smoothness.rawTime) linearly refines the value between the current and the next sample. The smoothness sensor has no leg channels: the overall smoothness of the selected sensor is shown. If there is no sample (a pause or outside the recording), render draws nothing.
Settings. PedalSmoothnessAtomProperty (the sensor), FontColorProperty, TextAlignProperty (default CENTER) and the internal PedalSmoothnessTextTemplateProperty. There is no units property. The compact skin sets the template {ps}%.
Drawing. The value is brought to an integer, the string is built with ctx.template.makeText(ps), 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).
- The
%dformat is meant for an integer: floating-point formats (%.1f) give an empty substitution (the formatting error is suppressed). - The percent sign in the template is plain text, you have to write it yourself (
{ps}%). - The number of variables in
defineVars()(one) must match the number ofmakeText(...)arguments.