Repository navigation
CompassIndicator
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 CompassIndicator.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.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 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")
);
/* Ring and tick color (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 substituted into the elements by CSS selectors. */
/* This property is not saved in JSON and is not visible 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 one frame. */
public void render(IndicatorContext indicatorContext) {
CompassContext ctx = (CompassContext) indicatorContext;
Graphics2D g = ctx.graphics;
/* No atom (outside the recording or in a pause): the compass is not drawn. */
GeoSensorAtom atom = ctx.geoSensor.queryAtom();
if (atom == null) {
return;
}
/* The bearing is calculated from the coordinate increment and the latitude. */
double bearing = Coordinates.bearing(atom.latDelta, atom.lonDelta, atom.lat);
/* The compass is square and 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 one 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);
}
}CompassIndicator draws a round compass rose that rotates by 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.
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. The atom is taken exact (the level of the geo sensor with the original coordinates, not a simplified copy of the track). If there is no atom, render returns immediately and the compass is not drawn.
Settings. Five properties in the order of defineProperties():
-
Geo:GeoSensorProperty; -
bodyColor:ColorProperty,223344by default; -
ringColor:ColorProperty, white by default; -
needleNorthColor:ColorProperty,CC3333by default; -
needleSouthColor:ColorProperty,DDDDDDby 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 visible in the panel. Its selectors bind the properties to the SVG: .body (fill) from bodyColor, .ring and .tick (stroke) from ringColor, .needle-n (fill) from needleNorthColor, .needle-s (fill) from needleSouthColor.
Drawing. The side of the square is size = min(width, height); the square is centered in the layer. The SVG is drawn by the method ctx.compassSvg.render(g, x, y, size, size, bearing) with a rotation by the bearing.
-
Without GPS data (outside the recording, and also in a recording pause:
queryAtom()returnsnullin a pause) the compass is not drawn: the layer is empty. Previously in this case the needle pointed north. - The bearing is from movement, not from the magnetic field: it is calculated from the GPS coordinate increments, not from a compass sensor.
- The layer does not have to be square: the compass is always fitted to the smaller side and centered.