Skip to content

TimeTextIndicator

roosit edited this page May 31, 2026 · 3 revisions

TimeTextIndicator

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 TimeTextIndicator.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.IndicatorSkinTransfer;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FontColorProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
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 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;

/*
 * Text time indicator: shows current player time in the selected zone,
 * plus elapsed time since project start and remaining time until the end.
 * Format is configured by DateTimeFormatter pattern; template combines variables.
 */
public class TimeTextIndicator extends Indicator {

    /* Stable technical indicator name. */
    public static final String NAME = "timeText";

    /* Base localization key for this indicator. */
    private static final String KEY = _KEY + "." + NAME;

    /* Font + main text color. */
    private final FontColorProperty mFont = new FontColorProperty();

    /* Alignment of the ready string inside the layer. */
    private final TextAlignProperty mTextAlign = new TextAlignProperty();

    /* Display timezone; UTC from player → user-selected zone. */
    private final TimeZoneProperty mTimeZone = new TimeZoneProperty();

    /* Pattern for current time, for example "yyyy-MM-dd HH:mm:ss". */
    private final StringProperty mTimeFormat = new StringProperty(
            localized(KEY + ".timeFormat.default"),
            "timeFormat",
            localized(KEY + ".timeFormat.title")
    );

    /* Pattern for duration (passed/left), for example "HH:mm:ss". */
    private final StringProperty mDurationFormat = new StringProperty(
            localized(KEY + ".durationFormat.default"),
            "durationFormat",
            localized(KEY + ".durationFormat.title")
    );

    /* String template: curr / tzid / passed / left variables. */
    private final TimeTextTemplateProperty mTemplate = new TimeTextTemplateProperty();

    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");
    }

    /*
     * Initial layout: indicator appears top-left, right offset 300 / bottom 100,
     * base size 600 x 300.
     */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.TOP);
        maket.setHorizontal(HorizontalAlign.LEFT);
        maket.setLayerMargin(null, 300d, 100d, null);
        maket.setLayerSize(600d, 300d);
    }

    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{
            mFont, mTextAlign,
            mTimeZone, mTimeFormat, mDurationFormat,
            getTemplate()
        };
    }

    /*
     * Property grouping in UI: alignment, zone, and both formats go to APPEARANCE.
     * Font+color and template use their standard groups.
     */
    @Override
    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);
    }

    /*
     * Two built-in skins:
     *   compact — short template without labels;
     *   withZone — extended, adds timezone name.
     */
    @Override
    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()
        };
    }

    /*
     * Render: computes current time, elapsed, remaining; formats through
     * DateTimeFormatter; assembles string through template; draws with antialiasing.
     */
    @Override
    public void render(IndicatorContext indicatorContext) {
        TimeTextContext ctx = (TimeTextContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* Player time and period bounds — all in UTC milliseconds. */
        long time = ctx.player.time;
        long passed = time - ctx.player.period.start;
        long left = ctx.player.period.end - time;

        /*
         * Timezone from settings + parsers for both patterns.
         * parseFormatter silently falls back on invalid input.
         */
        ZoneId zone = ctx.timeZone.zoneId;
        DateTimeFormatter timeFmt = parseFormatter(ctx.timeFormat.value, "yyyy-MM-dd HH:mm:ss");
        DateTimeFormatter durFmt = parseFormatter(ctx.durationFormat.value, "HH:mm:ss");

        /* Format three values and the human-readable zone name. */
        String currStr = timeFmt.format(
                LocalDateTime.ofInstant(Instant.ofEpochMilli(time), zone)
        );
        String tzidStr = zone.getDisplayName(TextStyle.FULL, Locale.getDefault());
        String passedStr = formatDuration(passed, durFmt);
        String leftStr = formatDuration(left, durFmt);

        /* Template assembles the final string from four variables. */
        String text = ctx.template.makeText(currStr, tzidStr, passedStr, leftStr);

        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        /* Font with scale, color from FontColorContext, drawing through TextAlign. */
        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);
    }

    /*
     * Duration is formatted as UTC LocalDateTime from 0:
     * pattern "HH:mm:ss" gives "00:01:23" for 83000 ms.
     * Math.max(0, ...) protects from negative values.
     */
    private static String formatDuration(long durationMs, DateTimeFormatter fmt) {
        LocalDateTime dt = LocalDateTime.ofInstant(
                Instant.ofEpochMilli(Math.max(0, durationMs)),
                ZoneId.of("UTC"));
        return fmt.format(dt);
    }

    /* Safe pattern parser: returns fallback on IllegalArgumentException. */
    private static DateTimeFormatter parseFormatter(String pattern, String fallback) {
        try {
            return DateTimeFormatter.ofPattern(pattern);
        } catch (Exception ex) {
            return DateTimeFormatter.ofPattern(fallback);
        }
    }

    /*
     * Template with variables curr / tzid / passed / left.
     * Two skins: compact (minimum fields), withZone (with zone name).
     */
    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
        protected String defineTemplate() {
            return localized(KEY_PROPERTY_TEMPLATE + ".default");
        }

        @Override
        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"))
            };
        }

        public IndicatorSkinTransfer skinCompact() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".compact"));
        }

        public IndicatorSkinTransfer skinWithZone() {
            return skin(localized(KEY_PROPERTY_TEMPLATE + ".withZone"));
        }
    }

    /*
     * Runtime context with snapshots of all properties for the current frame.
     * Render uses context, not direct property reads — this ensures
     * frame consistency under parallel edits.
     */
    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 TimeTextContext(Player player, Canvas canvas) {
            super(player, canvas);
            text = mFont.createContext();
            textAlign = mTextAlign.createContext();
            template = getTemplate().createContext();
            timeZone = mTimeZone.createContext();
            timeFormat = mTimeFormat.createContext();
            durationFormat = mDurationFormat.createContext();
        }
    }

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

Purpose

TimeTextIndicator is a text time indicator. It shows current player time in the user-selected timezone, plus elapsed time since project start and time remaining until the end. Time and duration formats are configured through DateTimeFormatter patterns; the template lets four variables (curr, tzid, passed, left) be mixed in any composition.

How it works

Data. No sensor is used — all three values are taken from IndicatorContext: ctx.player.time (UTC ms) for current time, time - period.start for elapsed, period.end - time for remaining. Timezone name is standard JDK ZoneId.getDisplayName.

Settings. Six properties: FontColorProperty (font+color), TextAlignProperty (alignment), TimeZoneProperty (display timezone), two StringProperty values with DateTimeFormatter pattern (separate for current time and duration), TextTemplateProperty with variables curr / tzid / passed / left and two skins (compact, withZone).

Drawing. Current time is formatted as LocalDateTime in the user's timezone; passed/left through formatDuration, which interprets millis as a UTC-based LocalDateTime from 0 (pattern HH:mm:ss gives 00:01:23 for 83000 ms). All four strings are substituted into the template, and the result is drawn through TextAlign.drawText with antialiasing.

Features and limitations

  • Invalid pattern does not break the indicator. parseFormatter silently falls back (yyyy-MM-dd HH:mm:ss for time, HH:mm:ss for duration) — the user sees a "wrong but working" format instead of an empty layer.
  • Durations > 24h render incorrectly with pattern HH:mm:ss — day is truncated. Long projects need an explicit numeric pattern.
  • Negative durations are clamped to 0 through Math.max(0, ...) — for example, when time is outside period.
  • Sensor sources are not used — the indicator is not bound to a specific source and works in any project.

Clone this wiki locally