Skip to content

CompassIndicator

roosit edited this page Oct 16, 2026 · 4 revisions

CompassIndicator

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

Full source with short comments

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 CompassIndicator.java: 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.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;

public class CompassIndicator extends Indicator {

    /* Indicator name; the full layer name in JSON: com.velitask.plagin.official.compass. */
    public static final String NAME = "compass";

    /* Prefix of the indicator's localization keys. */
    private static final String KEY = _KEY + "." + NAME;

    /* Geo sensor: the direction of movement is taken from it. */
    private final GeoSensorProperty mGeoSensor = new GeoSensorProperty();

    /* Body color (223344 by default). */
    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 ring and tick marks (white by default). */
    private static final String RING_COLOR = "ringColor";
    private final ColorProperty mRingColor = new ColorProperty(
            Color.WHITE,
            RING_COLOR,
            localized(KEY + "." + RING_COLOR + ".title")
    );

    /* Color of the north part of the needle (CC3333 by default). */
    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")
    );

    /* Color of the south part of the needle (DDDDDD by default). */
    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 of the compass rose: the property colors are applied to elements by CSS selectors. */
    /* This property is not saved to JSON and is not shown in the panel. */
    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");
    }

    @Override
    /* Default maket: top right, layer 200 x 200. */
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.RIGHT);
        maket.setLayerMargin(null, 50d, 50d, null);
        maket.setLayerSize(200d, 200d);
    }

    @Override
    /* Only the sensor and the four colors are saved and shown. */
    public IProperty[] defineProperties() {
        return new IProperty[]{mGeoSensor, mBodyColor, mRingColor, mNeedleNorthColor, mNeedleSouthColor};
    }

    @Override
    /* The colors are shown in the "Appearance" group. */
    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);
    }

    @Override
    /* Skins change all four colors at once. */
    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()
        };
    }

    @Override
    /* Rendering of a single frame. */
    public void render(IndicatorContext indicatorContext) {
        CompassContext ctx = (CompassContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* Without data the needle points north (bearing 0). */
        double bearing = 0;
        GeoSensorAtom atom = ctx.geoSensor.queryAtom();
        if (atom != null) {
            /* The bearing is calculated from the coordinate increments and the latitude. */
            bearing = Coordinates.bearing(atom.latDelta, atom.lonDelta, atom.lat);
        }

        /* The compass is square and is centered in the layer. */
        int size = Math.min(ctx.width, ctx.height);
        int x = (ctx.width - size) / 2;
        int y = (ctx.height - size) / 2;

        /* The SVG is rotated by the bearing. */
        ctx.compassSvg.render(g, x, y, size, size, bearing);
    }

    /* Snapshot of the property values for a single frame. */
    public class CompassContext extends IndicatorContext {

        public final GeoSensorProperty.GeoSensorContext geoSensor;
        public final SvgProperty.SvgContext compassSvg;

        public CompassContext(FrameContext frame) {
            super(frame);
            geoSensor = mGeoSensor.createContext(frame);
            compassSvg = mCompassSvg.createContext(frame);
        }
    }

    @Override
    /* Creation of the frame context. */
    public IndicatorContext createContext(FrameContext frame) {
        return new CompassContext(frame);
    }

}

Purpose

CompassIndicator draws a round compass rose that rotates according to the bearing of movement. The bearing is calculated from GPS data; the colors of the body, the ring, and the two halves of the needle are configurable.

How it works

Data. A GeoSensorAtom is requested through GeoSensorProperty (ctx.geoSensor.queryAtom()). The bearing is calculated as Coordinates.bearing(atom.latDelta, atom.lonDelta, atom.lat) - from the coordinate increments relative to the previous point and from the current latitude. If there is no atom, the bearing is 0 (the needle points north).

Settings. Five properties in the order of defineProperties():

  • Geo - GeoSensorProperty;
  • bodyColor - ColorProperty, 223344 by default;
  • ringColor - ColorProperty, white by default;
  • needleNorthColor - ColorProperty, CC3333 by default;
  • needleSouthColor - ColorProperty, DDDDDD by default.

All four colors are shown in the PropertyGroup.APPEARANCE group. Skins: dark and brass change all four colors. The SvgProperty with the compass graphics (svg/compass.svg) is not part of defineProperties(): it is not saved and is not shown in the panel. Its selectors tie the properties to the SVG: .body (fill) <- bodyColor, .ring and .tick (stroke) <- ringColor, .needle-n (fill) <- needleNorthColor, .needle-s (fill) <- needleSouthColor.

Rendering. The side of the square is size = min(width, height); the square is centered in the layer. The SVG is drawn with ctx.compassSvg.render(g, x, y, size, size, bearing), rotated by the bearing.

Features and limitations

  • Without GPS data the indicator is still drawn, and the needle points north.
  • The bearing comes from movement, not from the magnetic field: it is calculated from the increments of GPS coordinates, not from a compass sensor.
  • The layer does not have to be square: the compass is always fitted to the smaller side and centered.

Clone this wiki locally