Repository navigation
TimeTextIndicator
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 TimeTextIndicator.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.IndicatorSkinTransfer;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.LongestDurationStrategy;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.SourceProperty;
import com.velitask.sdk.properties.StringProperty;
import com.velitask.sdk.properties.TextAlignProperty;
import com.velitask.sdk.properties.TextTemplateProperty;
import com.velitask.sdk.properties.TimeZoneProperty;
import com.velitask.units.duration.Duration;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.TextStyle;
import java.util.Locale;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;
public class TimeTextIndicator extends Indicator {
/* Indicator name; the full layer name in JSON: com.velitask.plagin.official.timeText. */
public static final String NAME = "timeText";
/* Prefix of the indicator's localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Reference source: {passed} and {left} are calculated from its start and end. */
/* Accepts only sources with a positive duration. */
private final SourceProperty mReferenceSource = new SourceProperty(
null,
src -> src != null && src.getDuration() > 0L
) {
@Override
public String getName() {
return "referenceSource";
}
@Override
public String getTitle() {
return localized(KEY + ".referenceSource.title");
}
};
/* Font and text color. */
private final FontColorProperty mFont = new FontColorProperty();
/* Text alignment inside the layer (centered by default). */
private final TextAlignProperty mTextAlign = new TextAlignProperty();
/* Time zone for {curr}; the system one by default. */
private final TimeZoneProperty mTimeZone = new TimeZoneProperty();
/* Date and time format for {curr} (a DateTimeFormatter pattern). */
private final StringProperty mTimeFormat = new StringProperty(
localized(KEY + ".timeFormat.default"),
"timeFormat",
localized(KEY + ".timeFormat.title")
);
/* Duration format for {passed} and {left} (tokens d, H, m, s). */
private final StringProperty mDurationFormat = new StringProperty(
localized(KEY + ".durationFormat.default"),
"durationFormat",
localized(KEY + ".durationFormat.title")
);
/* Text template with the variables curr, tzid, passed, left. */
private final TimeTextTemplateProperty mTemplate = new TimeTextTemplateProperty();
/* If the user has not chosen a source, the source with the longest duration becomes the reference. */
public TimeTextIndicator() {
mReferenceSource.setStrategy(new LongestDurationStrategy());
}
private TextTemplateProperty getTemplate() {
return mTemplate;
}
@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 left, layer 600 x 300. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.TOP);
maket.setHorizontal(HorizontalAlign.LEFT);
maket.setLayerMargin(null, 300d, 100d, null);
maket.setLayerSize(600d, 300d);
}
@Override
/* The property order defines the order in the panel and in JSON. */
public IProperty[] defineProperties() {
return new IProperty[]{
mReferenceSource,
mFont, mTextAlign,
mTimeZone, mTimeFormat, mDurationFormat,
getTemplate()
};
}
@Override
/* Four properties are shown in the "Appearance" group. */
public void configureDisplay(DisplayConfig config) {
config.set(mTextAlign, PropertyGroup.APPEARANCE);
config.set(mTimeZone, PropertyGroup.APPEARANCE);
config.set(mTimeFormat, PropertyGroup.APPEARANCE);
config.set(mDurationFormat, PropertyGroup.APPEARANCE);
}
@Override
/* Both skins change only the text template. */
public IndicatorSkin[] defineSkins() {
return new IndicatorSkin[]{
IndicatorSkin.builder("compact", localized(KEY + ".skin.compact"))
.set(mTemplate.skinCompact())
.build(),
IndicatorSkin.builder("withZone", localized(KEY + ".skin.withZone"))
.set(mTemplate.skinWithZone())
.build()
};
}
@Override
/* Rendering of a single frame. */
public void render(IndicatorContext indicatorContext) {
TimeTextContext ctx = (TimeTextContext) indicatorContext;
Graphics2D g = ctx.graphics;
long time = ctx.player.time;
long periodStart;
long periodEnd;
/* A reference source is selected and has a duration: the period is taken from it, otherwise from the project period. */
if (ctx.refSource.sourceId != 0L && ctx.refSource.duration > 0L) {
periodStart = ctx.refSource.startTime;
periodEnd = ctx.refSource.endTime;
} else {
periodStart = ctx.player.period.start;
periodEnd = ctx.player.period.end;
}
/* passed - from the start of the period, left - to its end. */
long passed = time - periodStart;
long left = periodEnd - time;
ZoneId zone = ctx.timeZone.zoneId;
/* An invalid time format is replaced with a fallback one. */
DateTimeFormatter timeFmt = parseFormatter(ctx.timeFormat.value, "yyyy-MM-dd HH:mm:ss");
String currStr = timeFmt.format(
LocalDateTime.ofInstant(Instant.ofEpochMilli(time), zone)
);
/* The full name of the zone in the system language. */
String tzidStr = zone.getDisplayName(TextStyle.FULL, Locale.getDefault());
/* Durations are formatted by Duration.format: negative values are treated as zero. */
String passedStr = Duration.format(passed, ctx.durationFormat.value);
String leftStr = Duration.format(left, ctx.durationFormat.value);
/* The order of the values matches the order of the variables in defineVars(). */
String text = ctx.template.makeText(currStr, tzidStr, passedStr, leftStr);
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Font font = ctx.text.toFont(ctx.scale);
g.setFont(font);
g.setColor(ctx.text.color.value);
ctx.textAlign.drawText(g, text, ctx.width, ctx.height);
}
/* Format parsing with a fallback on error. */
private static DateTimeFormatter parseFormatter(String pattern, String fallback) {
try {
return DateTimeFormatter.ofPattern(pattern);
} catch (Exception ex) {
return DateTimeFormatter.ofPattern(fallback);
}
}
/* The "Text Template" property with four variables. */
static class TimeTextTemplateProperty extends TextTemplateProperty {
private static final String KEY_PROPERTY = KEY + "." + NAME;
private static final String KEY_PROPERTY_TEMPLATE = KEY_PROPERTY + ".template";
private static final String KEY_PROPERTY_VAR = KEY_PROPERTY + ".var";
@Override
/* The default template is a localized string. */
protected String defineTemplate() {
return localized(KEY_PROPERTY_TEMPLATE + ".default");
}
@Override
/* All variables are strings, format "%s". */
protected Var[] defineVars() {
return new Var[]{
new Var("curr", "%s", localized(KEY_PROPERTY_VAR + ".curr")),
new Var("tzid", "%s", localized(KEY_PROPERTY_VAR + ".tzid")),
new Var("passed", "%s", localized(KEY_PROPERTY_VAR + ".passed")),
new Var("left", "%s", localized(KEY_PROPERTY_VAR + ".left"))
};
}
/* The "Compact" skin substitutes the template {curr}. */
public IndicatorSkinTransfer skinCompact() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
}
/* The "With time zone" skin substitutes the template {curr} ({tzid}). */
public IndicatorSkinTransfer skinWithZone() {
return skin(localized(KEY_PROPERTY_TEMPLATE + ".withZone"));
}
}
/* Snapshot of the property values for a single frame. */
public class TimeTextContext extends IndicatorContext {
public final FontColorProperty.FontColorContext text;
public final TextAlignProperty.TextAlignContext textAlign;
public final TextTemplateProperty.TextTemplateContext template;
public final TimeZoneProperty.TimeZoneContext timeZone;
public final StringProperty.StringContext timeFormat;
public final StringProperty.StringContext durationFormat;
public final SourceProperty.SourceContext refSource;
public TimeTextContext(FrameContext frame) {
super(frame);
text = mFont.createContext(frame);
textAlign = mTextAlign.createContext(frame);
template = getTemplate().createContext(frame);
timeZone = mTimeZone.createContext(frame);
timeFormat = mTimeFormat.createContext(frame);
durationFormat = mDurationFormat.createContext(frame);
refSource = mReferenceSource.createContext(frame);
}
}
@Override
/* Creation of the frame context. */
public IndicatorContext createContext(FrameContext frame) {
return new TimeTextContext(frame);
}
}TimeTextIndicator shows as text the current time of the frame, the name of the time zone, and two intervals: how much time has passed since the start of the period and how much is left until its end. The period is taken either from the selected "reference" source or from the period of the whole project. The look of the text is defined by a template with the variables {curr}, {tzid}, {passed}, {left}.
Data. Sensors are not used. From the frame context, the player time ctx.player.time (UTC, milliseconds) and the period ctx.player.period are taken. If a reference source is selected (ctx.refSource.sourceId != 0) and its duration is greater than zero, periodStart and periodEnd are taken from its startTime and endTime; otherwise from the project period. passed = time - periodStart, left = periodEnd - time. The time {curr} is converted to the selected time zone (LocalDateTime.ofInstant) and formatted with DateTimeFormatter; the zone name for {tzid} is zone.getDisplayName(TextStyle.FULL, Locale.getDefault()). The durations {passed} and {left} are formatted by com.velitask.units.duration.Duration.format(ms, pattern), not by DateTimeFormatter: the tokens d, H, m, s are supported (a doubled token adds a leading zero), literals are put in single quotes, and negative values are treated as zero.
Settings. Seven properties in the order of defineProperties():
-
referenceSource-SourcePropertywith the filter "duration greater than zero"; theLongestDurationStrategystrategy picks the source with the longest duration if the user has not specified another one; -
font-FontColorProperty; -
textAlign-TextAlignProperty(centered by default); -
timeZone-TimeZoneProperty(the system zone by default); -
timeFormat-StringProperty, aDateTimeFormatterpattern (dd.MM.yyyy HH:mm:ssby default); -
durationFormat-StringProperty, aDuration.formatpattern (HH:mm:ssby default); -
textTemplate-TimeTextTemplateProperty(aTextTemplatePropertywith four variables).
The properties textAlign, timeZone, timeFormat, durationFormat are shown in the PropertyGroup.APPEARANCE group. The two skins (compact, withZone) change only the text template.
Rendering. Anti-aliasing is turned on, the font and color from ctx.text are set, and then the finished string is output through ctx.textAlign.drawText(g, text, ctx.width, ctx.height). A single line, no wrapping.
-
The reference source affects only
{passed}and{left}. The value of{curr}is always taken from the player time. - If no reference source is found (the project has no sources with a positive duration), the project period is used. The same happens if the selected source has a duration of zero.
-
Hours in
Duration.format. The tokenHgives the hour within a day (0-23), and full days are output by a separate tokend. The comment inDuration.formatpromises "full hours, may be ≥ 24", but the code computeshours = (total % 86400) / 3600: to see "27:00:00" you need a pattern withd, for exampled 'd' HH:mm:ss. -
An invalid time format (
timeFormat) does not cause an error: the fallbackyyyy-MM-dd HH:mm:ssis used. The duration format does not need such a check: unknown characters are output as they are. - The zone name depends on the system language, not on the Velitask interface language.
-
The skins "Compact" and "With time zone" replace the whole text template; the variable formats stay
%s. - If the template does not contain a variable, its value is simply not output, but it is still calculated.