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.db.SensorDataManager;
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. Must not be changed without a migration. */
public static final String NAME = "slopeSpeed";
/* Localization key prefix: 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();
{
/* The "slope" manager: slope atoms from the sensors_slope_data table by time. */
mSlope.addManager(
new SensorDataManager.Builder<>("slope", mSlope.getAtomClass())
.table(SlopeAtomProperty.TABLE_NAME)
.byTime()
.build()
);
}
/* 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 when going uphill (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 when going downhill (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 flat ground (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: a 400x200 layer at the top right corner, margins 100 from the top and 100 from 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 property order 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 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
/* Frame drawing: the upper half of the layer is the slope, the lower half is the speed. */
public void render(IndicatorContext indicatorContext) {
SlopeSpeedContext ctx = (SlopeSpeedContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* The slope and distance atoms for the current moment; either may be missing. */
SlopeSensorAtom slope = ctx.slope.queryAtom("slope", ctx.slope.rawTime);
DistanceSensorAtom dist = ctx.distance.queryAtom();
/* Neither is present: draw nothing. */
if (slope == null && dist == null) {
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;
/* Upper line: the arrow and the slope percent of the current atom, the color by type. */
if (slope != null) {
/* Arrow by slope type: downhill, uphill, flat. */
String arrow = switch (slope.slopeType) {
case SlopeSensorAtom.TYPE_DOWN ->
"▼";
case SlopeSensorAtom.TYPE_UP ->
"▲";
default ->
"—";
};
/* The percent is computed from the atom increments (height / distance * 100). */
float percent = slope.getElevationPercent();
String text = String.format("%s %.1f%%", arrow, percent);
/* 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);
}
/* Lower line: the speed. */
if (dist != null) {
/* The atom speed is stored in 1/1000 km/h; dividing by a thousand gives km/h. */
double speedKmH = dist.speed / 1000.0;
String text = String.format(localized(KEY + ".speedText.format"), speedKmH);
g.setColor(ctx.speedColor.value);
/* Shift down by half the height, then shift back. */
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 indicator's save format is described on the page SlopeSpeedIndicator JSON. User guide: Slope and Speed.
SlopeSpeedIndicator (name in JSON slopeSpeed, UI title "Slope and Speed") shows two lines of text: at the top the slope with an arrow and a percent (the color depends on uphill, downhill, or flat), at the bottom the current speed in km/h.
- Two sensors:
SlopeAtomProperty(JSON keySlope, "Slope") andDistanceSensorProperty(JSON keyDistance, "Distance"). Both are queried for the frame time and either of them may be empty independently of the other. - Slope:
ctx.slope.queryAtom("slope", ctx.slope.rawTime). Theslopemanager selects the atom by time (byTime) from thesensors_slope_datatable. - Speed:
ctx.distance.queryAtom(); thespeedfield is stored in 1/1000 km/h, so km/h is obtained by dividing by 1000. - The slope percent is taken from the atom:
slope.getElevationPercent()is the ratio of the height increment to the distance increment in the atom window, multiplied by 100; with a zero increment it is 0. The indicator does not use slope groups (slope_groups).
| JSON key | SDK type | Default | Role |
|---|---|---|---|
Slope |
SlopeAtomProperty |
not selected | Slope sensor. |
Distance |
DistanceSensorProperty |
not selected | Speed sensor. |
Font |
FontProperty |
Dialog, plain, 48 |
Font of both lines; the colors are set separately. |
slopeColorUp |
ColorProperty |
220, 80, 80 | Color of the slope line when going uphill. |
slopeColorDown |
ColorProperty |
80, 220, 80 | Color of the slope line when going downhill. |
slopeColorPlain |
ColorProperty |
128, 128, 128 | Color of the slope line on flat ground. |
speedColor |
ColorProperty |
255, 255, 255 | Color of the speed line. |
TextAlign |
TextAlignProperty |
4 (centered) | Position of both lines in their half of the layer (0..8). |
Font is placed in the "Font" group, the other settings (except the sensors) are in "Appearance". The "Neutral" skin (neutral) makes all four colors white.
The format of the speed line comes from the localization speedText.format (%.1f km/h in the English locale, %.1f км/ч in the Russian one); the slope line is always "<arrow> <percent with one decimal>%".
- If there is neither a slope atom nor a distance atom, the frame stays empty.
- The layer is divided into two halves by height.
- If there is a slope atom: the arrow
▼(downhill),▲(uphill), or—(flat), the percent%.1f%%, and the color by type; the text is output bytextAlign.drawTextin the upper half. - If there is a distance atom: the speed line in the
speedColorcolor; the context is shifted down by half the height, the line is output in the lower half, and the shift is restored.
-
Does not depend on groups. The type and percent are taken from the atom itself, not from
slope_groups. - Percent sign. For a downhill the percent is negative.
- Units are fixed. The speed is always in km/h, and there is no units setting.
- One line per half. A long text with a large font may extend beyond the bounds of its half of the layer.