Repository navigation
SlopeSpeedIndicator
Up | Previous | Next | Русский
Below is the full class source in documentation form. The comments inside the block explain the purpose of each part of the class, but they are not kept in the working SlopeSpeedIndicator.java: production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official;
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;
/*
* Two-line indicator: the top line is the current slope with a type arrow
* and percentage, and the bottom line is current speed. Slope color depends
* on the type (UP/DOWN/PLAIN). Uses slope and distance sensors at the same time.
*/
public class SlopeSpeedIndicator extends Indicator {
public static final String NAME = "slopeSpeed";
private static final String KEY = _KEY + "." + NAME;
/* Slope data source (slope type + percentage). */
private final SlopeAtomProperty mSlope = new SlopeAtomProperty();
/* Speed source (distance sensor, speed field). */
private final DistanceSensorProperty mDistance = new DistanceSensorProperty();
{
/*
* Custom "slope" manager with group_id > 0 filter: only atoms bound
* to slope groups are needed (the plugin marks them in
* VelitaskPlagin.onSourceImported). Without a group, the slope line is empty.
*/
mSlope.addManager(
new SensorDataManager.Builder<>("slope", mSlope.getAtomClass())
.table(SlopeAtomProperty.TABLE_NAME)
.where("group_id > 0")
.byTime()
.build()
);
}
/* Font for both lines, with an anonymous override for its localization key. */
private final FontProperty mFont = new FontProperty() {
@Override
public String getName() {
return "Font";
}
@Override
public String getTitle() {
return localized(KEY + ".font.title");
}
};
/* Colors for the three slope types. */
private static final String SLOPE_COLOR_UP = "slopeColorUp";
private final ColorProperty mSlopeColorUp = new ColorProperty(
new Color(220, 80, 80), SLOPE_COLOR_UP,
localized(KEY + "." + SLOPE_COLOR_UP + ".title"));
private static final String SLOPE_COLOR_DOWN = "slopeColorDown";
private final ColorProperty mSlopeColorDown = new ColorProperty(
new Color(80, 220, 80), SLOPE_COLOR_DOWN,
localized(KEY + "." + SLOPE_COLOR_DOWN + ".title"));
private static final String SLOPE_COLOR_PLAIN = "slopeColorPlain";
private final ColorProperty mSlopeColorPlain = new ColorProperty(
Color.GRAY, SLOPE_COLOR_PLAIN,
localized(KEY + "." + SLOPE_COLOR_PLAIN + ".title"));
/* Color of the bottom line (speed). */
private static final String SPEED_COLOR = "speedColor";
private final ColorProperty mSpeedColor = new ColorProperty(
Color.WHITE, SPEED_COLOR,
localized(KEY + "." + SPEED_COLOR + ".title"));
/* Alignment for both lines (same for the whole layer). */
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"); }
/* Initial layout: top-right, 100/100 margin, 400 x 200 size. */
@Override
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.RIGHT);
maket.setLayerMargin(null, 100d, 100d, null);
maket.setLayerSize(400d, 200d);
}
@Override
public IProperty[] defineProperties() {
return new IProperty[]{
mSlope, mDistance, mFont,
mSlopeColorUp, mSlopeColorDown, mSlopeColorPlain,
mSpeedColor, mTextAlign
};
}
@Override
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);
}
/*
* One "neutral" skin: all three slope colors and the speed color are white.
* Used when coloring by slope type is not needed.
*/
@Override
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()
};
}
/*
* Render: draws two lines. Top is slope (if a slope atom exists),
* bottom is speed (if a distance atom exists). If both are null,
* the indicator is completely empty.
*/
@Override
public void render(IndicatorContext indicatorContext) {
SlopeSpeedContext ctx = (SlopeSpeedContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* Time for slope and distance is converted separately; they may have different sources. */
long slopeRawTime = mSlope.convertToRawTime(ctx.player.time);
long distRawTime = mDistance.convertToRawTime(ctx.player.time);
if (ctx.player.isPreview) {
slopeRawTime = mSlope.clampToSensorRange(slopeRawTime);
distRawTime = mDistance.clampToSensorRange(distRawTime);
}
SlopeSensorAtom slope = mSlope.queryAtom("slope", slopeRawTime);
DistanceSensorAtom dist = mDistance.queryAtom(distRawTime);
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);
/* The layer is split in half: top half is slope, bottom half is speed. */
int halfHeight = ctx.height / 2;
if (slope != null) {
/* Arrow by slope type: up (UP), down (DOWN), dash (PLAIN/other). */
String arrow = switch (slope.slopeType) {
case SlopeSensorAtom.TYPE_DOWN -> "▼";
case SlopeSensorAtom.TYPE_UP -> "▲";
default -> "—";
};
/* Slope percentage from elevation, not from distance as in SlopeText. */
float percent = slope.getElevationPercent();
String text = String.format("%s %.1f%%", arrow, percent);
/* Color by slope 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);
}
if (dist != null) {
/*
* Speed: speed is stored as "1/1 000 000 km/h" -> /1_000_000.0
* (unlike Speedometer, where it is /1000.0; the data units differ).
*/
double speedKmH = dist.speed / 1_000_000.0;
String text = String.format(localized(KEY + ".speedText.format"), speedKmH);
g.setColor(ctx.speedColor.value);
/* Translate into the bottom half, drawText, then undo translate. */
g.translate(0, halfHeight);
ctx.textAlign.drawText(g, text, ctx.width, halfHeight);
g.translate(0, -halfHeight);
}
}
/* Context with snapshots of all properties. */
public class SlopeSpeedContext extends IndicatorContext {
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(Player player, Canvas canvas) {
super(player, canvas);
font = mFont.createContext();
slopeColorUp = mSlopeColorUp.createContext();
slopeColorDown = mSlopeColorDown.createContext();
slopeColorPlain = mSlopeColorPlain.createContext();
speedColor = mSpeedColor.createContext();
textAlign = mTextAlign.createContext();
}
}
@Override
public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
return new SlopeSpeedContext(player, canvas);
}
}SlopeSpeedIndicator is a compact two-line indicator: the top line shows the current slope with a type arrow (up / down / dash) and percentage, and the bottom line shows current speed. The slope color depends on the type (UP/DOWN/PLAIN), while the speed color is configured separately. One neutral skin disables color coding by making everything white.
Data. Two sensors are used at the same time: SlopeAtomProperty for the current SlopeSensorAtom and DistanceSensorProperty for DistanceSensorAtom. The custom "slope" manager filters WHERE group_id > 0, so only atoms marked into slope groups during source import are read (VelitaskPlagin.onSourceImported). If both atoms are null, the indicator is empty.
Settings. Eight properties: two sources (slope + distance), FontProperty, three ColorProperty values (for UP/DOWN/PLAIN), one ColorProperty for speed, and TextAlignProperty (one alignment for both lines).
Drawing. The layer is split into two halves (halfHeight). Top: slope-type arrow plus percentage from slope.getElevationPercent() (percentage by elevation, unlike SlopeTextIndicator, which uses the group's slopePercent), colored by type. Bottom: speed, formatted as speed / 1_000_000.0 (sensor format "1/1 000 000 km/h"; different from Speedometer, where it is /1000), with the lower line drawn through g.translate(0, halfHeight) + drawText + restore translate.
-
speedis divided by 1_000_000.0, whileSpeedometerIndicatordivides it by 1000.0. Different units for the same atom field are a potential source of copy-paste mistakes. See the format in thespeedText.formatlocalization string. -
slopeType.PLAINand unknown types are both shown as a dash; this is an intentional simplification for the two-line format. -
slope.getElevationPercent()is calculated from the elevation change of a specific atom, not from the group'sslopePercentas inSlopeTextIndicator. Because of that, it can look jumpier than the group-aware version. - If the source has no slope sensor, the top line is empty, but speed is still shown.