Skip to content

CompassIndicator

roosit edited this page May 15, 2026 · 4 revisions

CompassIndicator

Up | Previous | Next | Русский

Full source code with brief explanations

Below is the full source code of the class in documentation form. Comments inside the block explain the purpose of class parts, but they are not stored in the working file CompassIndicator.java: production code stays clean, and 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.GeoSensorAtom;
import com.velitask.sdk.properties.ColorProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.GeoSensorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SvgProperty;
import com.velitask.units.coordinates.Coordinates;
import java.awt.Color;
import java.awt.Graphics2D;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;

/*
 * Compass: SVG image with a needle rotating by track bearing.
 * Bearing is calculated from the geo sensor (latDelta, lonDelta) — direction
 * of movement, not magnetic north (magnetic declination is not accounted for).
 */
public class CompassIndicator extends Indicator {

    public static final String NAME = "compass";

    private static final String KEY = _KEY + "." + NAME;

    /* Geo data source — atoms with latDelta/lonDelta are needed for bearing. */
    private final GeoSensorProperty mGeoSensor = new GeoSensorProperty();

    /* Color of the compass body (background circle). */
    private static final String BODY_COLOR = "bodyColor";
    private final ColorProperty mBodyColor = new ColorProperty(
            new Color(0x22, 0x33, 0x44), BODY_COLOR,
            localized(KEY + "." + BODY_COLOR + ".title"));

    /* Color of the outer ring + tick marks. */
    private static final String RING_COLOR = "ringColor";
    private final ColorProperty mRingColor = new ColorProperty(
            Color.WHITE, RING_COLOR,
            localized(KEY + "." + RING_COLOR + ".title"));

    /* North needle color. */
    private static final String NEEDLE_NORTH_COLOR = "needleNorthColor";
    private final ColorProperty mNeedleNorthColor = new ColorProperty(
            new Color(0xcc, 0x33, 0x33), NEEDLE_NORTH_COLOR,
            localized(KEY + "." + NEEDLE_NORTH_COLOR + ".title"));

    /* South needle color. */
    private static final String NEEDLE_SOUTH_COLOR = "needleSouthColor";
    private final ColorProperty mNeedleSouthColor = new ColorProperty(
            new Color(0xdd, 0xdd, 0xdd), NEEDLE_SOUTH_COLOR,
            localized(KEY + "." + NEEDLE_SOUTH_COLOR + ".title"));

    /*
     * SVG compass with bindings:
     *   .body — fill mBodyColor;
     *   .ring and .tick — stroke mRingColor (same color);
     *   .needle-n — fill mNeedleNorthColor;
     *   .needle-s — fill mNeedleSouthColor.
     */
    private final SvgProperty mCompassSvg = new SvgProperty("CompassSvg", "svg/compass.svg")
            .bind(".body", "fill", mBodyColor)
            .bind(".ring", "stroke", mRingColor)
            .bind(".tick", "stroke", mRingColor)
            .bind(".needle-n", "fill", mNeedleNorthColor)
            .bind(".needle-s", "fill", mNeedleSouthColor);

    @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, 50/50 margin, size 200x200. */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.RIGHT);
        maket.setLayerMargin(null, 50d, 50d, null);
        maket.setLayerSize(200d, 200d);
    }

    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{mGeoSensor, mBodyColor, mRingColor, mNeedleNorthColor, mNeedleSouthColor};
    }

    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mBodyColor, PropertyGroup.APPEARANCE);
        config.set(mRingColor, PropertyGroup.APPEARANCE);
        config.set(mNeedleNorthColor, PropertyGroup.APPEARANCE);
        config.set(mNeedleSouthColor, PropertyGroup.APPEARANCE);
    }

    /*
     * Two built-in skins:
     *   dark — dark body + light ring + bright red north needle;
     *   brass — brass tones (copper body, sandy ring, burgundy needle).
     */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("dark", localized(KEY + ".skin.dark"))
            .set(mBodyColor.skin(0x1a, 0x1a, 0x1a))
            .set(mRingColor.skin(0xcc, 0xcc, 0xcc))
            .set(mNeedleNorthColor.skin(0xff, 0x44, 0x44))
            .set(mNeedleSouthColor.skin(0xaa, 0xaa, 0xaa))
            .build(),
            IndicatorSkin.builder("brass", localized(KEY + ".skin.brass"))
            .set(mBodyColor.skin(0x8b, 0x6f, 0x3a))
            .set(mRingColor.skin(0xd4, 0xa7, 0x5f))
            .set(mNeedleNorthColor.skin(0x6b, 0x2b, 0x1c))
            .set(mNeedleSouthColor.skin(0xe8, 0xd9, 0xb0))
            .build()
        };
    }

    /*
     * Render: calculates bearing from the latest deltatime, draws SVG with rotation.
     * Without an atom, bearing = 0 (needle points upward).
     */
    @Override
    public void render(IndicatorContext indicatorContext) {
        CompassContext ctx = (CompassContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        double bearing = 0;
        long rawTime = mGeoSensor.convertToRawTime(ctx.player.time);
        if (ctx.player.isPreview) {
            rawTime = mGeoSensor.clampToSensorRange(rawTime);
        }
        GeoSensorAtom atom = mGeoSensor.queryAtom(rawTime);
        if (atom != null) {
            /*
             * Bearing from the direction vector (latDelta, lonDelta) at latitude lat.
             * This is direction of movement (track bearing), not magnetic.
             */
            bearing = Coordinates.bearing(atom.latDelta, atom.lonDelta, atom.lat);
        }

        /* Square inscribed size with centering in the layer. */
        int size = Math.min(ctx.width, ctx.height);
        int x = (ctx.width - size) / 2;
        int y = (ctx.height - size) / 2;

        /* Render SVG with rotation = bearing. */
        ctx.compassSvg.render(g, x, y, size, size, bearing);
    }

    /* Context with prepared SVG context (applies cached color binds). */
    public class CompassContext extends IndicatorContext {

        public final SvgProperty.SvgContext compassSvg;

        public CompassContext(Player player, Canvas canvas) {
            super(player, canvas);
            compassSvg = mCompassSvg.createContext();
        }
    }

    @Override
    public IndicatorContext createContext(IndicatorContext.Player player, IndicatorContext.Canvas canvas) {
        return new CompassContext(player, canvas);
    }

}

Purpose

CompassIndicator is a compass showing track movement direction. SVG image with body, ring, ticks, and a two-sided needle (north/south); rotates around the center by bearing. Bearing is calculated from movement direction (delta lat/lon at the current latitude), not from magnetic north — magnetic declination is not accounted for.

How it works

Data. GeoSensorProperty → GeoSensorAtom at the current moment. Bearing is computed through Coordinates.bearing(latDelta, lonDelta, lat) — a function from com.velitask.units.coordinates that accounts for latitude (at high latitudes, a degree of longitude is shorter). Without an atom, bearing = 0 (needle points upward — north).

Settings. Five properties: geo source and four ColorProperty values for body / ring (dividing ticks) / needle-north / needle-south. Graphics — one SvgProperty (svg/compass.svg) with five binds (.body/.ring/.tick/.needle-n/.needle-s) — runtime substitutes current colors into SVG attributes.

Drawing. Square inscribed size (min(width,height)) with centering. SVG is rendered through ctx.compassSvg.render(g, x, y, size, size, bearing) — the last parameter is rotation in degrees. The SVG itself has static elements (body, ring, ticks) and a rotating needle — rotation is applied to the whole SVG, so ticks rotate too (this is intentional for the "look where you are going" scenario).

Features and limitations

  • Bearing is track-bearing, not magnetic. If magnetic north with declination compensation is needed, that is a separate feature and will require an additional data source.
  • Without latDelta/lonDelta (stationary track, point) bearing is unstable. On low-speed GPS with noise, the needle jitters.
  • SVG bind is applied once at createContext(), not every frame. If color changes at runtime (for example through a skin), a new context is needed. This happens automatically because render is called with a fresh context.
  • Skins change all 4 colors at once — these are theme presets, not point settings. For fine tuning, edit Color properties manually.

Clone this wiki locally