Skip to content

VideoIndicator

roosit edited this page May 31, 2026 · 3 revisions

VideoIndicator

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 VideoIndicator.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.properties.BorderProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.PropertyGroup;
import com.velitask.sdk.properties.VideoSourceProperty;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;

/*
 * Video frame indicator: shows a frame of the selected video source at the
 * current player time. Supports border (color, thickness) and rounded
 * corners — both through BorderProperty.
 */
public class VideoIndicator extends Indicator {

    public static final String NAME = "video";

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

    /* Video source — VideoSourceProperty gives frameAt(rawTime, w, h) → BufferedImage. */
    private final VideoSourceProperty mVideo = new VideoSourceProperty();

    /* Border with configurable color, thickness, and corner radius. */
    private final BorderProperty mBorder = new BorderProperty();

    /* Access for other components (for example figures that use a video sensor). */
    public VideoSourceProperty getVideo() {
        return mVideo;
    }

    public BorderProperty getBorder() {
        return mBorder;
    }

    @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: bottom-right, 50/50 margin, size 480x270 (16:9). */
    @Override
    public void defineMaket(Maket maket) {
        maket.setVertical(VerticalAlign.BOTTOM);
        maket.setHorizontal(HorizontalAlign.RIGHT);
        maket.setLayerMargin(null, null, 50d, 50d);
        maket.setLayerSize(480d, 270d);
    }

    @Override
    public IProperty[] defineProperties() {
        return new IProperty[]{mVideo, mBorder};
    }

    @Override
    public void configureDisplay(DisplayConfig config) {
        config.set(mBorder, PropertyGroup.APPEARANCE);
    }

    /*
     * Two built-in skins:
     *   framed — white 4px border without rounding (classic "photo frame");
     *   rounded — no border, but 30px rounded corners (borderless smooth window).
     */
    @Override
    public IndicatorSkin[] defineSkins() {
        return new IndicatorSkin[]{
            IndicatorSkin.builder("framed", localized(KEY + ".skin.framed"))
            .set(mBorder.getColor().skin(Color.WHITE))
            .set(mBorder.getThickness().skin(4))
            .set(mBorder.getRadius().skin(0))
            .build(),
            IndicatorSkin.builder("rounded", localized(KEY + ".skin.rounded"))
            .set(mBorder.getColor().skin(Color.WHITE))
            .set(mBorder.getThickness().skin(0))
            .set(mBorder.getRadius().skin(30))
            .build()
        };
    }

    /*
     * Render: gets the video frame for the current raw time, draws it inside the border
     * with optional clipping by rounded corners, then draws the border on top.
     */
    @Override
    public void render(IndicatorContext indicatorContext) {
        VideoContext ctx = (VideoContext) indicatorContext;
        Graphics2D g = ctx.graphics;

        /* Inner size accounting for border (border thickness scaled to canvas). */
        int bw = (int) (ctx.border.thickness * ctx.scale);
        int cr = (int) (ctx.border.radius * ctx.scale);
        int innerW = Math.max(1, ctx.width - bw);
        int innerH = Math.max(1, ctx.height - bw);

        /*
         * Request frame at the needed size — VideoSource performs scaling itself.
         * If there is no frame (outside video range, decoder error) — draw nothing.
         */
        long rawTime = mVideo.convertToRawTime(ctx.player.time);
        if (ctx.player.isPreview) {
            rawTime = mVideo.clampToSensorRange(rawTime);
        }
        BufferedImage frame = ctx.video.frameAt(rawTime, innerW, innerH);
        if (frame == null) {
            return;
        }

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

        /*
         * Rounded corners — clip through RoundRectangle2D before drawImage,
         * then restore the original clip.
         */
        Shape originalClip = g.getClip();
        if (cr > 0) {
            g.setClip(new RoundRectangle2D.Float(
                    bw / 2f, bw / 2f, innerW, innerH, cr, cr)
            );
        }
        g.drawImage(frame, bw / 2, bw / 2, innerW, innerH, null);
        g.setClip(originalClip);

        /* Border over the frame — with rounding or without. */
        if (bw > 0) {
            g.setColor(ctx.border.color);
            g.setStroke(new BasicStroke(bw));
            if (cr > 0) {
                g.drawRoundRect(bw / 2, bw / 2, innerW, innerH, cr, cr);
            } else {
                g.drawRect(bw / 2, bw / 2, innerW, innerH);
            }
        }
    }

    /* Context with border and video-source snapshots. */
    public class VideoContext extends IndicatorContext {

        public final BorderProperty.BorderContext border;
        public final VideoSourceProperty.VideoSourceContext video;

        public VideoContext(Player player, Canvas canvas) {
            super(player, canvas);
            border = mBorder.createContext();
            video = mVideo.createContext();
        }
    }

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

Purpose

VideoIndicator is a video-frame window in a project: it shows a frame of the selected video source at the current player time. Convenient for picture-in-picture: for example, main camera + additional camera (action cam from another angle) or replay. Supports configurable border and rounded corners.

How it works

Data. VideoSourceProperty — the user selects a project video source; ctx.video.frameAt(rawTime, w, h) returns a BufferedImage of the needed size (Source performs scaling itself, not the indicator). UTC → raw-time with preview clamp as in other indicators. If the frame is unavailable (outside video range, decoder error), the indicator is empty.

Settings. Two properties: video source and BorderProperty (composite — color, thickness, corner radius).

Drawing. Inner size (width-bw) × (height-bw) is calculated. If radius > 0, a RoundRectangle2D clip is set and the frame is drawn through drawImage with scaled dimensions for innerW/innerH. Then the clip is restored and the border is drawn with BasicStroke(bw) through drawRect or drawRoundRect.

Features and limitations

  • Source performs scaling — the indicator requests a frame at (innerW, innerH) and does not do g.drawImage(scale) itself. This is more efficient: the decoder can choose the video resolution closest to the request.
  • framed and rounded skins are incompatible — both override the same three border properties. Applying the second erases the first (this is the basic SkinTransfer semantics).
  • bw / 2 offset in coordinates — the standard technique for drawRect with stroke: stroke is drawn along the line center, so shifting by half-thickness inward frames the border without clipping.
  • Without rounded corners (cr == 0), the clip operation is skipped — a small optimization for the classic case.

Clone this wiki locally