Repository navigation
ImageIndicator
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 ImageIndicator.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.geom.FitGeometry;
import com.velitask.sdk.geom.FitMode;
import com.velitask.sdk.properties.BooleanProperty;
import com.velitask.sdk.properties.BorderProperty;
import com.velitask.sdk.properties.DisplayConfig;
import com.velitask.sdk.properties.FitModeProperty;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.ImageSourceProperty;
import com.velitask.sdk.properties.PropertyGroup;
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.Size;
import org.abricos.core.state.maket.HorizontalAlign;
import org.abricos.core.state.maket.Maket;
import org.abricos.core.state.maket.VerticalAlign;
/* Image overlay: a still image from a project source, with a border. */
public class ImageIndicator extends Indicator {
/* Stable indicator name. */
public static final String NAME = "image";
/* Prefix of the indicator's localization keys. */
private static final String KEY = _KEY + "." + NAME;
/* Adaptive source: off by default (false). */
private final BooleanProperty mAdaptive
= new BooleanProperty(false, "adaptive", localized(KEY + ".adaptive.title"));
/* The selected image source; not saved in a template. */
private final ImageSourceProperty mImage = new ImageSourceProperty();
/* Border: color, thickness, line style, pattern, corner radius. */
private final BorderProperty mBorder = new BorderProperty();
/* Mode of fitting the picture into the layer; COVER by default. */
private final FitModeProperty mFit = new FitModeProperty();
public ImageSourceProperty getImage() {
return mImage;
}
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");
}
@Override
/* Default placement: bottom-right corner, a margin of 50 on the right and at the bottom, size 480x270. */
public void defineMaket(Maket maket) {
maket.setVertical(VerticalAlign.BOTTOM);
maket.setHorizontal(HorizontalAlign.RIGHT);
maket.setLayerMargin(null, null, 50d, 50d);
maket.setLayerSize(480d, 270d);
}
@Override
/* Property order = order in JSON and in the settings panel. */
public IProperty[] defineProperties() {
return new IProperty[]{
mImage, mAdaptive, mFit, mBorder
};
}
@Override
/* The border goes to the "Appearance" group; the rest stays in the main group. */
public void configureDisplay(DisplayConfig config) {
config.set(mBorder, PropertyGroup.APPEARANCE);
}
@Override
/* Two skins change only the border: "Framed" (white, 4, no rounding) and "Rounded" (no line, radius 30). */
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()
};
}
@Override
/* Drawing: geometry calculation, getting the picture at the needed size, clipping by the border, outline. */
public void render(IndicatorContext indicatorContext) {
ImageContext ctx = (ImageContext) indicatorContext;
/* In adaptive mode with no suitable image at the current time, nothing is drawn. */
if (ctx.adaptive && ctx.image.sourceId == 0) {
return;
}
Graphics2D g = ctx.graphics;
/* The border thickness and radius are scaled together with the layer. */
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);
Size nat = ctx.image.imageSize();
int nw = nat != null ? nat.getWidthInt() : innerW;
int nh = nat != null ? nat.getHeightInt() : innerH;
/* d = {x, y, width, height} of the picture inside the inner area; the native size is taken from the metadata. */
int[] d = FitGeometry.fit(ctx.fit, nw, nh, innerW, innerH);
long rawTime = ctx.image.convertToRawTime(ctx.player.time);
/* The picture is static: time does not affect the result, and the frame is cached by size. */
BufferedImage frame = ctx.image.frameAt(rawTime, d[2], d[3]);
if (frame == null) {
return;
}
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
int x = bw / 2;
int y = bw / 2;
/* The picture is drawn inside a clip region (rounded when the radius is > 0). */
Shape originalClip = g.getClip();
if (cr > 0) {
g.setClip(new RoundRectangle2D.Float(x, y, innerW, innerH, cr, cr));
} else {
g.setClip(x, y, innerW, innerH);
}
g.drawImage(frame, x + d[0], y + d[1], d[2], d[3], null);
g.setClip(originalClip);
/* The border is drawn on top only with a non-zero thickness; the line style and pattern are not used here. */
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);
}
}
}
/* Snapshot of the property values for a frame: border, source, fit mode, adaptivity. */
public class ImageContext extends IndicatorContext {
public final BorderProperty.BorderContext border;
public final ImageSourceProperty.ImageSourceContext image;
final FitMode fit;
final boolean adaptive;
public ImageContext(FrameContext frame) {
super(frame);
adaptive = mAdaptive.get(frame);
/* The adaptive mode is passed to the source property before the source context is created. */
mImage.setAdaptive(adaptive);
fit = mFit.get(frame);
border = mBorder.createContext(frame);
image = mImage.createContext(frame);
}
}
@Override
/* The context is created from the FrameContext of the current frame. */
public IndicatorContext createContext(FrameContext frame) {
return new ImageContext(frame);
}
}ImageIndicator overlays a still image (a logo, a photo, a picture caption) from the project sources on the video. The image can be fitted in different ways, outlined with a border, and given rounded corners. The default placement is the bottom-right corner, 480×270, with a margin of 50 on the right and at the bottom. The indicator has no sound.
Data. ImageSourceProperty creates an ImageSourceContext: from it come sourceId, the native size of the picture imageSize(), and the scaled image frameAt(rawTime, w, h). The picture is static: rawTime is accepted for symmetry with video and does not affect the result, and the scaled images are cached by the property by size.
Settings. Four properties in the order of defineProperties():
-
imageSource(ImageSourceProperty) - the choice of the image source. -
adaptive(BooleanProperty,false) - whentrue, the source is picked automatically: from the image sources of the project, the one whose interval contains the current time is chosen (if there are several, the one that started later). If there is no suitable one, the indicator draws nothing. -
fit(FitModeProperty,COVER) - the fit mode:COVER,CONTAIN,FILL,NONE; the geometry is calculated byFitGeometry.fit. -
border(BorderProperty) - the border; in thePropertyGroup.APPEARANCEgroup.
The two skins ("framed" and "rounded") override only the border color, thickness, and radius; there are no text templates.
Rendering.
- If the mode is adaptive and no source is found, exit.
- The border thickness and radius are multiplied by
ctx.scale; the inner area is the layer minus the border thickness. -
FitGeometry.fitcalculates the position and size of the picture from its native size (if there is no metadata, the size is taken equal to the inner area). - A picture of the needed size is requested;
nullmeans exit without drawing. - Anti-aliasing and bilinear interpolation are turned on; a clip region is set (rounded when the radius is > 0), the picture is drawn into it, and then the region is restored.
- When the border thickness is > 0, an outline in the border color is drawn on top.
- The code almost repeats
VideoIndicatorwithout sound, time, and the preview mode: the image is the same throughout the source. - A template does not store the source: after a template is applied, the source is chosen again (or picked in adaptive mode).
- The border's
styleanddashPatternare available in the settings but are not used when drawing: the outline is always solid. - The
NONEmode draws the picture at its native size in the center; everything that goes beyond the layer is clipped. - The save format is described in ImageIndicator JSON; the user pages are Image and Image JSON.