Repository navigation
SlopeSpeedIndicator
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 SlopeSpeedIndicator.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.data.DistanceSensorAtom;
import com.velitask.sdk.data.SlopeSensorAtom;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.DistanceSensorProperty;
import com.velitask.sdk.properties.FontProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SlopeAtomProperty;
import com.velitask.sdk.properties.TextAlignProperty;
import java.awt.Color;
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;
/* The "Slope and Speed" indicator: two lines of text, the slope with an arrow and the speed. */
public class SlopeSpeedIndicator extends Indicator {
/* Stable indicator name in the layer JSON. It must not be changed without a migration. */
public static final String NAME = "slopeSpeed";
/* Prefix of the localization keys: Plugin.Indicator.slopeSpeed. */
private static final String KEY = _KEY + "." + NAME;
/* Slope sensor (JSON "Slope"). */
private final SlopeAtomProperty mSlope = new SlopeAtomProperty();
/* Distance sensor (JSON "Distance"): the speed is taken from it. */
private final DistanceSensorProperty mDistance = new DistanceSensorProperty();
/* Font of both lines (JSON "Font"), without a color: the colors are set separately. */
private final FontProperty mFont = new FontProperty() {
@Override
public String getName() {
return "Font";
}
@Override
public String getTitle() {
return localized(KEY + ".font.title");
}
};
/* JSON name of the "Slope color (uphill)" property. */
private static final String SLOPE_COLOR_UP = "slopeColorUp";
/* Color of the slope line on a climb (220, 80, 80 by default). */
private final ColorProperty mSlopeColorUp = new ColorProperty(
new Color(220, 80, 80),
SLOPE_COLOR_UP,
localized(KEY + "." + SLOPE_COLOR_UP + ".title")
);
/* JSON name of the "Slope color (downhill)" property. */
private static final String SLOPE_COLOR_DOWN = "slopeColorDown";
/* Color of the slope line on a descent (80, 220, 80 by default). */
private final ColorProperty mSlopeColorDown = new ColorProperty(
new Color(80, 220, 80),
SLOPE_COLOR_DOWN,
localized(KEY + "." + SLOPE_COLOR_DOWN + ".title")
);
/* JSON name of the "Slope color (flat)" property. */
private static final String SLOPE_COLOR_PLAIN = "slopeColorPlain";
/* Color of the slope line on a flat section (gray by default). */
private final ColorProperty mSlopeColorPlain = new ColorProperty(
Color.GRAY,
SLOPE_COLOR_PLAIN,
localized(KEY + "." + SLOPE_COLOR_PLAIN + ".title")
);
/* JSON name of the "Speed color" property. */
private static final String SPEED_COLOR = "speedColor";
/* Color of the speed line (white by default). */
private final ColorProperty mSpeedColor = new ColorProperty(
Color.WHITE,
SPEED_COLOR,
localized(KEY + "." + SPEED_COLOR + ".title")
);
/* Alignment of both lines (JSON "TextAlign"), centered by default. */
private final TextAlignProperty mTextAlign = new TextAlignProperty() {
@Override
public String getName() {
return "TextAlign";
}
@Override
public String getTitle() {
return localized(KEY + ".textAlign.title");
}
};
@Override
public String getName() {
return NAME;
}
@Override
public String getTitle() {
return localized(KEY + ".title");
}
@Override
public String getDescription() {
return localized(KEY + ".description");
}
@Override
public String getTags() {
return localized(KEY + ".tags");
}
@Override
/* Default maket: layer 400x200 at the top right corner, margins 100 at the top and 100 on the right. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.RIGHT);
maket.setLayerMargin(null, 100d, 100d, null);
maket.setLayerSize(400d, 200d);
}
@Override
/* The order of properties matches the order in JSON. */
public IProperty[] defineProperties() {
return new IProperty[]{
mSlope,
mDistance,
mFont,
mSlopeColorUp,
mSlopeColorDown,
mSlopeColorPlain,
mSpeedColor,
mTextAlign
};
}
@Override
/* The font is in the "Font" group, the colors and the alignment are in "Appearance". */
public void configureDisplay(DisplayConfig config) {
config.set(mFont, PropertyGroup.FONT);
config.set(mSlopeColorUp, PropertyGroup.APPEARANCE);
config.set(mSlopeColorDown, PropertyGroup.APPEARANCE);
config.set(mSlopeColorPlain, PropertyGroup.APPEARANCE);
config.set(mSpeedColor, PropertyGroup.APPEARANCE);
config.set(mTextAlign, PropertyGroup.APPEARANCE);
}
@Override
/* The "Neutral" skin: all colors become white. */
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("neutral", localized(KEY + ".skin.neutral"))
.set(mSlopeColorUp.skin(Color.WHITE))
.set(mSlopeColorDown.skin(Color.WHITE))
.set(mSlopeColorPlain.skin(Color.WHITE))
.set(mSpeedColor.skin(Color.WHITE))
.build()
};
}
@Override
/* Drawing the frame: the top half of the layer is the slope, the bottom half is the speed. */
public void render(IndicatorContext indicatorContext) {
SlopeSpeedContext ctx = (SlopeSpeedContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The slope is for the frame time or the last one before a pause; the distance is only for the frame time. */
SlopeSensorAtom slope = ctx.slope.queryAtomOrBefore();
DistanceSensorAtom dist = ctx.distance.queryAtom();
/* The frame is inside a distance pause: the speed is shown as 0. */
boolean distanceGap = dist == null && ctx.distance.queryGap() != null;
/* Neither exists: nothing is drawn. */
if (slope == null && dist == null && !distanceGap) {
return;
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = ctx.font.toFont(ctx.scale);
g.setFont(font);
/* Each line takes half of the layer height. */
int halfHeight = ctx.height / 2;
/* The top line: the arrow and the slope percentage of the current atom, colored by type. */
if (slope != null) {
/* The arrow by slope type: descent, climb, flat. */
String arrow = switch (slope.slopeType) {
case SlopeSensorAtom.TYPE_DOWN ->
"▼";
case SlopeSensorAtom.TYPE_UP ->
"▲";
default ->
"—";
};
/* The percentage is calculated from the atom increments (elevation / distance * 100). */
float percent = slope.getElevationPercent();
String text = String.format("%s %.1f%%", arrow, percent);
/* The color of the slope line by type. */
Color color = switch (slope.slopeType) {
case SlopeSensorAtom.TYPE_UP ->
ctx.slopeColorUp.value;
case SlopeSensorAtom.TYPE_DOWN ->
ctx.slopeColorDown.value;
default ->
ctx.slopeColorPlain.value;
};
g.setColor(color);
ctx.textAlign.drawText(g, text, ctx.width, halfHeight);
}
/* The bottom line: the speed. */
if (dist != null || distanceGap) {
/* The atom speed is stored in 1/1000 km/h; dividing by a thousand gives km/h. In a pause, 0. */
double speedKmH = dist == null ? 0 : dist.speed / 1000.0;
String text = String.format(localized(KEY + ".speedText.format"), speedKmH);
g.setColor(ctx.speedColor.value);
/* Shift down by half of the height, then return. */
g.translate(0, halfHeight);
ctx.textAlign.drawText(g, text, ctx.width, halfHeight);
g.translate(0, -halfHeight);
}
}
/* Property values for the current frame. Created in createContext(frame). */
public class SlopeSpeedContext extends IndicatorContext {
public final SlopeAtomProperty.SensorContext slope;
public final DistanceSensorProperty.SensorContext distance;
public final FontProperty.FontContext font;
public final ColorProperty.ColorContext slopeColorUp;
public final ColorProperty.ColorContext slopeColorDown;
public final ColorProperty.ColorContext slopeColorPlain;
public final ColorProperty.ColorContext speedColor;
public final TextAlignProperty.TextAlignContext textAlign;
public SlopeSpeedContext(FrameContext frame) {
super(frame);
slope = mSlope.createContext(frame);
distance = mDistance.createContext(frame);
font = mFont.createContext(frame);
slopeColorUp = mSlopeColorUp.createContext(frame);
slopeColorDown = mSlopeColorDown.createContext(frame);
slopeColorPlain = mSlopeColorPlain.createContext(frame);
speedColor = mSpeedColor.createContext(frame);
textAlign = mTextAlign.createContext(frame);
}
}
@Override
/* Frame context factory. */
public IndicatorContext createContext(FrameContext frame) {
return new SlopeSpeedContext(frame);
}
}The save format of the indicator is described on the page SlopeSpeedIndicator JSON. The user guide: Slope and Speed.
SlopeSpeedIndicator (the JSON name is slopeSpeed, the interface title is "Slope and Speed") shows two lines of text: on top the slope with an arrow and a percentage (the color depends on a climb, a descent or flat), below it the current speed in km/h.
- Two sensors:
SlopeAtomProperty(JSON keySlope, "Slope") andDistanceSensorProperty(JSON keyDistance, "Distance"). Both are requested for the frame time and can be empty independently of each other. - The slope:
ctx.slope.queryAtomOrBefore()is the slope atom for the frame time, and in a recording pause the last atom before the pause (the slope line does not disappear). The indicator no longer registers its own data manager. - The speed:
ctx.distance.queryAtom(); thespeedfield is stored in 1/1000 km/h, so km/h is obtained by dividing by 1000. If there is no atom but the frame falls into a distance pause (ctx.distance.queryGap() != null), the speed line stays and shows0.0 km/h; outside the recording (neither an atom nor a pause) there is no speed line. - The slope percentage is taken from the atom:
slope.getElevationPercent()is the ratio of the elevation increment to the distance increment in the atom window, multiplied by 100; with a zero increment it is 0. The indicator does not use the slope groups (slope_groups).
| JSON key | SDK type | Default | Role |
|---|---|---|---|
Slope |
SlopeAtomProperty |
not selected | The slope sensor. |
Distance |
DistanceSensorProperty |
not selected | The speed sensor. |
Font |
FontProperty |
Dialog, plain, 48 |
The font of both lines; the colors are set separately. |
slopeColorUp |
ColorProperty |
220, 80, 80 | The color of the slope line on a climb. |
slopeColorDown |
ColorProperty |
80, 220, 80 | The color of the slope line on a descent. |
slopeColorPlain |
ColorProperty |
128, 128, 128 | The color of the slope line on a flat section. |
speedColor |
ColorProperty |
255, 255, 255 | The color of the speed line. |
TextAlign |
TextAlignProperty |
4 (centered) | The position of both lines in their half of the layer (0..8). |
Font is moved into the "Font" group, the other settings (except the sensors) into "Appearance". The "Neutral" skin (neutral) makes all four colors white.
The format of the speed line is taken from the localization speedText.format (in the English locale %.1f km/h); the slope line is always "<arrow> <percentage with one decimal place>%".
- Neither a slope atom nor a distance atom, and the frame does not fall into a distance pause: the layer stays empty.
- The layer is split into two halves by height.
- If there is a slope atom: the arrow
▼(descent),▲(climb) or—(flat), the percentage%.1f%%, the color by type; the text is output withtextAlign.drawTextin the top half. - If there is a distance atom or the frame is inside a distance pause (then the speed is 0): the speed line in the color
speedColor; the context is shifted down by half of the height, the line is output in the bottom half, the shift is returned.
-
It does not depend on groups. The type and the percentage are taken from the atom itself, not from
slope_groups. - Pauses. The slope in a recording pause stays at the last value before the pause, and the speed in a distance pause is shown as zero.
- The sign of the percentage. For a descent the percentage is negative.
- The units are fixed. The speed is always in km/h, there is no measurement units setting.
- One line per half. A long text with a large font can go beyond the bounds of its half of the layer.