Repository navigation
SliderIndicator
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 SliderIndicator.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.IProject;
import com.velitask.sdk.ISource;
import com.velitask.sdk.ISourceList;
import com.velitask.sdk.Indicator;
import com.velitask.sdk.IndicatorContext;
import com.velitask.sdk.SourceType;
import com.velitask.sdk.TickContext;
import com.velitask.sdk.properties.DoubleSpinnerProperty;
import com.velitask.sdk.properties.EnumArrayProperty;
import com.velitask.sdk.properties.IFrameValues;
import com.velitask.sdk.properties.IProperty;
import com.velitask.sdk.properties.IntegerSpinnerProperty;
import com.velitask.sdk.properties.VideoSourceProperty;
import com.velitask.sdk.timemap.TimeMap;
import com.velitask.sdk.timemap.TimeMapBlock;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONObject;
/* The slider is a slide-show CONTROLLER: it draws nothing itself. */
/* It builds a schedule from the project videos and images and substitutes the time of the nested indicators. */
public class SliderIndicator extends Indicator {
/* Stable indicator name in saved projects; it must not be changed. */
public static final String NAME = "slider";
private static final String KEY = _KEY + "." + NAME;
/* Hidden property: needed only for access to the project (scanning the sources). */
/* It is not shown in the interface and is saved to JSON as an empty object. */
private final VideoSourceProperty mProjectAccess = new VideoSourceProperty() {
{
setVisible(false);
}
@Override
public String getName() {
return "sliderProject";
}
@Override
public void applyDefaultSource(ISourceList sourceList, long playerTime) {
}
@Override
public JSONObject toJSON(boolean hasTemplate) {
return new JSONObject();
}
@Override
public void fromJSON(JSONObject json) {
}
};
/* Slide duration in seconds (15 by default). In the schedule it is converted to ms and multiplied by the player speed. */
private final IntegerSpinnerProperty mSlideDuration
= new IntegerSpinnerProperty(15, "slideDuration", localized(KEY + ".slideDuration.title"));
/* Minimum pause between slides in seconds (0 by default). */
/* A source that started inside the slot or pause of the previous slide is shifted to its end and is not lost. */
private final IntegerSpinnerProperty mPauseDuration
= new IntegerSpinnerProperty(0, "pauseDuration", localized(KEY + ".pauseDuration.title"));
/* Time mode: parent speed (default), freeze frame, or real speed. */
private final EnumArrayProperty<SliderTimeMode> mTimeMode
= new EnumArrayProperty<>(SliderTimeMode.class) {
@Override
public String getName() {
return "timeMode";
}
@Override
public String getTitle() {
return SliderIndicator.this.localized(KEY + ".timeMode.title");
}
@Override
protected String[] defineTitles() {
SliderTimeMode[] vs = SliderTimeMode.values();
String[] t = new String[vs.length];
for (int i = 0; i < vs.length; i++) {
t[i] = switch (vs[i]) {
case FROZEN ->
SliderIndicator.this.localized(KEY + ".timeMode.frozen");
case REAL ->
SliderIndicator.this.localized(KEY + ".timeMode.real");
case PARENT ->
SliderIndicator.this.localized(KEY + ".timeMode.parent");
};
}
return t;
}
};
/* Appearance duration in seconds, step 0.1 (1.0 by default; 0 - no effect). */
private final DoubleSpinnerProperty mAppearanceDuration = new DoubleSpinnerProperty(
1.0, "appearanceDuration", localized(KEY + ".appearanceDuration.title"));
/* Leave (fade-out) duration in seconds, step 0.1 (1.0 by default; 0 - no effect). */
private final DoubleSpinnerProperty mDisappearanceDuration = new DoubleSpinnerProperty(
1.0, "disappearanceDuration", localized(KEY + ".disappearanceDuration.title"));
/* The point in base pixels of the parent for the "From point" and "To point" modes (960 x 540 by default). */
private final IntegerSpinnerProperty mPointX
= new IntegerSpinnerProperty(960, "pointX", localized(KEY + ".pointX.title"));
private final IntegerSpinnerProperty mPointY
= new IntegerSpinnerProperty(540, "pointY", localized(KEY + ".pointY.title"));
/* How a slide appears (None by default). */
private final EnumArrayProperty<SliderAppearanceIn> mAppearanceIn
= new EnumArrayProperty<>(SliderAppearanceIn.class) {
@Override
public String getName() {
return "appearanceIn";
}
@Override
public String getTitle() {
return SliderIndicator.this.localized(KEY + ".appearanceIn.title");
}
@Override
protected String[] defineTitles() {
SliderAppearanceIn[] vs = SliderAppearanceIn.values();
String[] t = new String[vs.length];
for (int i = 0; i < vs.length; i++) {
t[i] = SliderIndicator.this.localized(KEY + ".appearanceIn." + vs[i].name().toLowerCase());
}
return t;
}
};
/* How a slide leaves (None by default). */
private final EnumArrayProperty<SliderAppearanceOut> mAppearanceOut
= new EnumArrayProperty<>(SliderAppearanceOut.class) {
@Override
public String getName() {
return "appearanceOut";
}
@Override
public String getTitle() {
return SliderIndicator.this.localized(KEY + ".appearanceOut.title");
}
@Override
protected String[] defineTitles() {
SliderAppearanceOut[] vs = SliderAppearanceOut.values();
String[] t = new String[vs.length];
for (int i = 0; i < vs.length; i++) {
t[i] = SliderIndicator.this.localized(KEY + ".appearanceOut." + vs[i].name().toLowerCase());
}
return t;
}
};
/* The time for empty segments: no source will be found under it, so nested indicators draw nothing. */
private static final long DEAD_TIME = Long.MIN_VALUE;
/* Schedule cache: rebuilt when the key changes (period, settings, speed, set of sources). */
private String mMapKey;
private SliderTimelineMap mMap;
/* Cache of the project time map and its version: the version grows with every rebuild. */
private SliderTimelineMap mTimeMapSource;
private long mTimeMapPauseMs = -1L;
private volatile TimeMap mTimeMap;
private volatile long mTimeMapVersion;
@Override
public String getName() {
return NAME;
}
@Override
public String getTitle() {
return localized(KEY + ".title");
}
@Override
/* The order of the properties defines the order in the settings panel. */
public IProperty[] defineProperties() {
return new IProperty[]{
mProjectAccess, mSlideDuration, mPauseDuration, mTimeMode,
mAppearanceIn, mAppearanceOut,
mAppearanceDuration, mDisappearanceDuration,
mPointX, mPointY
};
}
@Override
/* The slider can be a provider of the project time map (it stretches the pauses between slides). */
public boolean providesTimeMap() {
return true;
}
@Override
public TimeMap getTimeMap() {
return mTimeMap;
}
@Override
public long getTimeMapVersion() {
return mTimeMapVersion;
}
@Override
/* Frame context: finds the segment under the current time and applies the appear or leave effect to the slider itself. */
public IndicatorContext createContext(FrameContext frame) {
IndicatorContext.Player p = frame.player;
long t = p.time;
double speed = p.speed > 0 ? p.speed : 1.0;
SliderTimelineMap map = ensureMap(frame, speed);
ensureTimeMap(frame, map);
SliderSegment seg = findSegment(map, t, frame.previous);
SliderContext ctx = new SliderContext(frame, map, seg);
applyAppearance(ctx, seg, t, speed, frame);
return ctx;
}
/* The segment of the previous frame is reused while the time is inside it and the schedule is the same; otherwise a binary search. */
private SliderSegment findSegment(SliderTimelineMap map, long t, IndicatorContext prev) {
if (prev instanceof SliderContext sc && sc.map == map && sc.segment != null
&& t >= sc.segment.start && t < sc.segment.end) {
return sc.segment;
}
return map.find(t);
}
/* The time and speed received by the nested indicators. Shared math for rendering and for the tick. */
private IndicatorContext.Player childrenPlayerFor(IFrameValues frame, SliderSegment seg, double speed) {
IndicatorContext.Player p = frame.player();
long tPrime;
if (seg == null || seg.kind == SliderSegmentKind.EMPTY) {
/* Empty or a pause: the time is unreachable, nested indicators draw nothing. */
tPrime = DEAD_TIME;
} else {
/* Time inside the source, then clamping at the edges: before the start - the first frame, after the end - the last. */
long raw = seg.contentBase + (long) (seg.coef * (p.time - seg.anchor));
long maxC = seg.contentBase + Math.max(0L, seg.sourceDuration - 1);
tPrime = Math.min(Math.max(seg.contentBase, raw), maxC);
}
double childSpeed;
if (seg == null || seg.kind == SliderSegmentKind.EMPTY) {
childSpeed = speed;
} else {
/* The speed of the nested indicators depends on the time mode. */
childSpeed = switch (mTimeMode.get(frame)) {
case FROZEN ->
0.0;
case REAL ->
1.0;
case PARENT ->
speed;
};
}
return new IndicatorContext.Player(p.period, tPrime, p.outTime, p.isPreview, childSpeed, p.sourceId);
}
@Override
/* Tick hooks: the engine calls them outside of drawing to resolve the time, visibility, and volume of nested indicators. */
public IndicatorContext.Player resolveChildrenTick(TickContext tick) {
double speed = tick.player.speed > 0 ? tick.player.speed : 1.0;
SliderTimelineMap map = ensureMap(tick, speed);
ensureTimeMap(tick, map);
return childrenPlayerFor(tick, findSegment(map, tick.player.time, null), speed);
}
@Override
/* Nested indicators are hidden on empty segments and outside the schedule. */
public boolean resolveChildrenHidden(TickContext tick) {
double speed = tick.player.speed > 0 ? tick.player.speed : 1.0;
SliderSegment seg = findSegment(ensureMap(tick, speed), tick.player.time, null);
return seg == null || seg.kind == SliderSegmentKind.EMPTY;
}
@Override
/* Volume of nested indicators: 0 outside a slide and in freeze-frame mode; it changes smoothly at the slide edges */
/* if an appear or leave effect is selected. */
public double resolveChildrenVolume(TickContext tick) {
double speed = tick.player.speed > 0 ? tick.player.speed : 1.0;
SliderSegment seg = findSegment(ensureMap(tick, speed), tick.player.time, null);
if (seg == null || seg.kind != SliderSegmentKind.SOURCE
|| mTimeMode.get(tick) == SliderTimeMode.FROZEN) {
return 0.0;
}
AppearanceWindow w = appearanceWindow(tick, seg, speed);
long t = tick.player.time;
if (w.tdIn() > 0 && t < seg.start + w.tdIn()
&& mAppearanceIn.get(tick) != SliderAppearanceIn.NONE) {
return (t - seg.start) / w.tdIn();
}
if (w.tdOut() > 0 && t >= seg.end - w.tdOut()
&& mAppearanceOut.get(tick) != SliderAppearanceOut.NONE) {
return (seg.end - t) / w.tdOut();
}
return 1.0;
}
/* The slider context carries the schedule and the current segment for the next frame. */
private static final class SliderContext extends IndicatorContext {
private final SliderTimelineMap map;
private final SliderSegment segment;
SliderContext(FrameContext frame, SliderTimelineMap map, SliderSegment segment) {
super(frame);
this.map = map;
this.segment = segment;
}
}
/* Returns the schedule, rebuilding it only when the key changes. */
/* synchronized: called both from the tick and from the drawing loop. */
private synchronized SliderTimelineMap ensureMap(IFrameValues frame, double speed) {
IndicatorContext.Player p = frame.player();
double coef = switch (mTimeMode.get(frame)) {
case FROZEN ->
0.0;
case REAL ->
1.0 / speed;
case PARENT ->
1.0;
};
/* The slide duration and the pause are kept in real time, so in track time they are multiplied by the player speed. */
long windowMs = (long) (mSlideDuration.get(frame) * 1000.0 * speed);
long minPauseMs = (long) (mPauseDuration.get(frame) * 1000.0 * speed);
IProject project = mProjectAccess.getProject();
String key = mapKey(project, p.period, windowMs, minPauseMs, mTimeMode.get(frame), speed);
if (!key.equals(mMapKey)) {
mMap = SliderTimelineMap.build(project, p.period.start, p.period.end,
windowMs, minPauseMs, coef);
mMapKey = key;
}
return mMap;
}
/* Schedule key: the period, durations, mode, speed, and all the project videos and images. */
private static String mapKey(IProject project, IndicatorContext.Player.Period period,
long windowMs, long minPauseMs, SliderTimeMode mode, double speed) {
StringBuilder sb = new StringBuilder();
sb.append(period.start).append(':').append(period.end).append('|')
.append(windowMs).append('|').append(minPauseMs).append('|')
.append(mode).append('|').append(speed).append('|');
if (project != null && project.getRoot() != null) {
ISourceList all = project.getRoot().getChildsRecurse();
for (int i = 0; i < all.size(); i++) {
ISource s = all.get(i);
SourceType type = s.getType();
if (type == SourceType.VIDEO || type == SourceType.IMAGE) {
sb.append(s.getId()).append(',').append(s.getStartTime()).append(',')
.append(s.getDuration()).append(',').append(type).append(';');
}
}
}
return sb.toString();
}
/* The project time map is rebuilt when the schedule or the pause changes. */
private synchronized void ensureTimeMap(IFrameValues frame, SliderTimelineMap map) {
IndicatorContext.Player p = frame.player();
long pauseMs = (long) (mPauseDuration.get(frame) * 1000.0);
if (map == mTimeMapSource && pauseMs == mTimeMapPauseMs) {
return;
}
mTimeMap = buildTimeMap(map, p.period.start, p.period.end, pauseMs);
mTimeMapSource = map;
mTimeMapPauseMs = pauseMs;
mTimeMapVersion++;
}
/* The map blocks follow the schedule segments with a multiplier of 1; a pause between two slides is stretched to the set value. */
private static TimeMap buildTimeMap(SliderTimelineMap map, long periodStart, long periodEnd, long pauseMs) {
List<TimeMapBlock> blocks = new ArrayList<>();
long out = 0L;
for (int i = 0; i < map.size(); i++) {
SliderSegment seg = map.get(i);
long len = seg.end - seg.start;
long outLen = isGapBetweenSlides(map, i) ? Math.max(len, pauseMs) : len;
blocks.add(TimeMapBlock.linear(seg.start, seg.end, out, out + outLen));
out += outLen;
}
if (blocks.isEmpty()) {
blocks.add(TimeMapBlock.linear(periodStart, periodEnd, 0L, periodEnd - periodStart));
}
return TimeMap.build(blocks);
}
/* An empty segment strictly between two slides is a pause. */
private static boolean isGapBetweenSlides(SliderTimelineMap map, int index) {
return map.get(index).kind == SliderSegmentKind.EMPTY
&& index > 0 && map.get(index - 1).kind == SliderSegmentKind.SOURCE
&& index + 1 < map.size() && map.get(index + 1).kind == SliderSegmentKind.SOURCE;
}
/* Durations of the appear and leave windows in ms of track time. */
private record AppearanceWindow(double tdIn, double tdOut) {
}
/* If both windows do not fit into the segment, they are shortened proportionally. */
private AppearanceWindow appearanceWindow(IFrameValues frame, SliderSegment seg, double speed) {
double tdIn = Math.max(0.0, mAppearanceDuration.get(frame)) * 1000.0 * speed;
double tdOut = Math.max(0.0, mDisappearanceDuration.get(frame)) * 1000.0 * speed;
double segLen = seg.end - seg.start;
if (tdIn + tdOut > segLen) {
double k = segLen / (tdIn + tdOut);
tdIn *= k;
tdOut *= k;
}
return new AppearanceWindow(tdIn, tdOut);
}
/* The appear or leave effect as a transformation of the slider itself (opacity, shift, scale). */
/* It works only on segments with a source and if there is a parent. */
private void applyAppearance(IndicatorContext ctx, SliderSegment seg, long t,
double speed, FrameContext frame) {
FrameContext.Parent parent = frame.parent;
if (seg == null || seg.kind != SliderSegmentKind.SOURCE || parent == null) {
return;
}
AppearanceWindow window = appearanceWindow(frame, seg, speed);
double tdIn = window.tdIn();
double tdOut = window.tdOut();
if (tdIn <= 0 && tdOut <= 0) {
return;
}
double w = parent.width;
double h = parent.height;
IndicatorContext.Transform tr = ctx.getTransform();
if (tdIn > 0 && t < seg.start + tdIn) {
/* Appearance progress 0..1. */
double inP = (t - seg.start) / tdIn;
switch (mAppearanceIn.get(frame)) {
case FADE ->
tr.setOpacity(inP);
case FROM_LEFT ->
tr.setOffset(-w * (1 - inP), 0);
case FROM_RIGHT ->
tr.setOffset(w * (1 - inP), 0);
case FROM_TOP ->
tr.setOffset(0, -h * (1 - inP));
case FROM_BOTTOM ->
tr.setOffset(0, h * (1 - inP));
case FROM_POINT ->
applyPoint(tr, inP, ctx, frame);
case NONE -> {
}
}
} else if (tdOut > 0 && t >= seg.end - tdOut) {
/* Leave progress: 1 at the start of the window, 0 at the end. */
double outP = (seg.end - t) / tdOut;
switch (mAppearanceOut.get(frame)) {
case FADE ->
tr.setOpacity(outP);
case TO_LEFT ->
tr.setOffset(-w * (1 - outP), 0);
case TO_RIGHT ->
tr.setOffset(w * (1 - outP), 0);
case TO_TOP ->
tr.setOffset(0, -h * (1 - outP));
case TO_BOTTOM ->
tr.setOffset(0, h * (1 - outP));
case TO_POINT ->
applyPoint(tr, outP, ctx, frame);
case NONE -> {
}
}
}
}
/* Appearing and leaving through a point: the box shrinks to the point (pointX, pointY) when s -> 0 */
/* and takes its place when s = 1. */
private void applyPoint(IndicatorContext.Transform tr, double s,
IndicatorContext ctx, FrameContext frame) {
double sc = ctx.scale;
double px = mPointX.get(frame) * sc;
double py = mPointY.get(frame) * sc;
double effW = ctx.width * s;
double effH = ctx.height * s;
double offX = (1 - s) * (px - effW / 2 - frame.selfX);
double offY = (1 - s) * (py - effH / 2 - frame.selfY);
tr.setScale(s);
tr.setOffset(offX, offY);
}
@Override
/* The controller draws nothing: the picture is provided by the nested Video and Image. */
public void render(IndicatorContext indicatorContext) {
}
}SliderIndicator (slider, shown as "Slider" in the interface) turns the set of project videos and images into a slide show. It draws nothing itself: the slide card is drawn by nested indicators — Video and Image with the "Adaptive source" setting turned on (see VideoIndicator, ImageIndicator). The slider:
- lays the project sources out in time and decides which slide is visible at a given moment;
- substitutes the time and speed of the nested indicators so that an adaptive child finds the right source;
- hides the nested indicators between slides and controls their volume;
- applies appear and leave effects to the slide;
- optionally provides the project with a time map (it stretches the pauses between slides).
The slider is type-independent: it operates only on time, so a child that has nothing to show at that time simply stays silent.
The slider reads not its own source properties but the whole project: through the hidden sliderProject property (VideoSourceProperty), it gets an IProject and takes all VIDEO and IMAGE sources from the source tree. They are turned into a SliderTimelineMap — an array of SliderSegment segments of the SOURCE or EMPTY type (SliderSegmentKind).
The schedule is cached in mMap; the mapKey(...) key includes the period boundaries, the slide duration and the pause (already multiplied by the speed), the time mode, the player speed, and the list (id, start, duration, type) of all videos and images. While the key is the same, ensureMap(...) returns the cache. The segment of the previous frame is taken from frame.previous (SliderContext) while the time stays inside it.
For the nested indicators, childrenPlayerFor(...) builds an IndicatorContext.Player:
| Segment | Time of nested indicators | Speed of nested indicators |
|---|---|---|
SOURCE |
contentBase + coef * (t - anchor), clamped to [contentBase, contentBase + sourceDuration - 1]
|
0 (freeze frame), 1 (real), or the player speed (parent) |
EMPTY or outside the schedule |
Long.MIN_VALUE |
player speed |
A player speed that is not greater than zero is replaced with 1.0.
| JSON name | SDK type | Default | What it does |
|---|---|---|---|
sliderProject |
VideoSourceProperty (hidden) |
empty object | Access to the project; not configurable. |
slideDuration |
IntegerSpinnerProperty |
15 |
Slide duration, s. For a video, the display window is no shorter than the clip itself; for an image, it is exactly the set value. |
pauseDuration |
IntegerSpinnerProperty |
0 |
Minimum pause between slides, s. |
timeMode |
EnumArrayProperty<SliderTimeMode> |
0 (PARENT) |
Time mode. |
appearanceIn |
EnumArrayProperty<SliderAppearanceIn> |
0 (NONE) |
Appearance method. |
appearanceOut |
EnumArrayProperty<SliderAppearanceOut> |
0 (NONE) |
Leave method. |
appearanceDuration |
DoubleSpinnerProperty |
1.0 |
Appearance duration, s (step 0.1, minimum 0.0). |
disappearanceDuration |
DoubleSpinnerProperty |
1.0 |
Leave duration, s (step 0.1, minimum 0.0). |
pointX, pointY
|
IntegerSpinnerProperty |
960, 540
|
The point for "From point" and "To point", in base pixels of the parent. |
The values are read through get(frame) from the frame snapshot. The detailed format is on the SliderIndicator JSON page, and the user description is on the Slider page.
render(...) is empty. All the work happens in createContext(...) and in the tick hooks:
-
createContext(frame)finds the segment under the player time and creates aSliderContext. - On a
SOURCEsegment,applyAppearance(...)sets theTransformof the slider itself. At the start of the segment (the appearance window),appearanceInworks; at the end (the leave window),appearanceOutworks; if the windows do not fit into the segment, they are shortened proportionally. Shifts are calculated from the size of the parent; "From point" and "To point" use scalingsaround the point (pointX,pointY, multiplied by the maket scale). -
resolveChildrenTick(...)gives the nested indicators the distorted time and speed,resolveChildrenHidden(...)hides them onEMPTY, andresolveChildrenVolume(...)sets the volume:0outside a slide and in "Freeze frame" mode, a linear rise and fall at the slide edges when effects are selected, otherwise1. - As a provider of the time map (
providesTimeMap()), the slider builds aTimeMapfrom the same segments: multiplier1, and an empty segment between two slides is stretched to the "Min pause, s" value if it is shorter. The map version (getTimeMapVersion()) grows with every rebuild.
- How to work with nested indicators. Add a slider, then place the child indicators "Video" and/or "Image" inside it and turn on "Adaptive source" in them. The videos and images are placed on the project timeline where needed: the slider itself shows the one whose time is running now. Nested indicators that have nothing to draw output nothing.
- The schedule is built from all videos and images of the project, not only those nested in the slider.
- The window and the pause are multiplied by the player speed: with accelerated playback, a slide stays for the same number of seconds of real time.
- The "From point" and "To point" modes shrink the slider to size
0; the shift modes use the width and height of the parent (frame.parent); without a parent the effects are not applied. - Any selected effect other than "None" also changes the volume of the nested indicators at the slide edges.
- For
slideDurationandpauseDurationno range is set in the code: negative values are not cut off. The effect durations have a lower bound of0.0, andMath.max(0.0, ...)is additionally applied when calculating the windows. - The appear and leave windows are shortened even if one of the effects is selected as "None" (the duration is still taken into account when calculating the windows).
- When saving, the
sliderProjectproperty writes an empty object and is not restored from JSON.