Repository navigation
SliderTimelineMap
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 SliderTimelineMap.java: the production code stays clean, and the explanations live in the wiki.
package com.velitask.plugin.official;
import com.velitask.sdk.IProject;
import com.velitask.sdk.ISource;
import com.velitask.sdk.ISourceList;
import com.velitask.sdk.SourceType;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
/* The slider map: an ordered array of non-overlapping segments covering the project period. */
/* Built from the real starts of the project sources, a greedy layout. */
final class SliderTimelineMap {
/* Segments by ascending time. */
private final SliderSegment[] segments;
private SliderTimelineMap(SliderSegment[] segments) {
this.segments = segments;
}
/* Binary search for the segment covering the time t; null if t is outside the map. */
SliderSegment find(long t) {
int lo = 0;
int hi = segments.length - 1;
int res = -1;
while (lo <= hi) {
int mid = (lo + hi) >>> 1;
if (segments[mid].start <= t) {
res = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
if (res < 0) {
return null;
}
SliderSegment seg = segments[res];
return t < seg.end ? seg : null;
}
int size() {
return segments.length;
}
SliderSegment get(int index) {
return segments[index];
}
/* Builds the map. windowMs and minPauseMs are already multiplied by the player speed */
/* (a slide keeps the given duration in real time). coef is the time multiplier of the mode. */
static SliderTimelineMap build(IProject project, long periodStart, long periodEnd,
long windowMs, long minPauseMs, double coef) {
List<SliderSegment> out = new ArrayList<>();
if (project != null && project.getRoot() != null && periodEnd > periodStart) {
ISourceList all = project.getRoot().getChildsRecurse();
/* Only the video and image sources of the project, in the order of the real start. */
List<ISource> vis = new ArrayList<>();
for (int i = 0; i < all.size(); i++) {
ISource s = all.get(i);
SourceType type = s.getType();
if (type == SourceType.VIDEO || type == SourceType.IMAGE) {
vis.add(s);
}
}
vis.sort(Comparator.comparingLong(ISource::getStartTime));
long cursor = periodStart;
/* A new slide cannot start earlier than this moment (the end of the previous slot and pause). */
long nextAllowed = periodStart;
for (ISource s : vis) {
long st = s.getStartTime();
if (st >= periodEnd) {
break;
}
/* The video window is no shorter than its length (a long clip is visible in full, a short one freezes until the slide length); */
/* the image window is always the slide duration. */
boolean video = s.getType() == SourceType.VIDEO;
long win = video ? Math.max(s.getDuration(), windowMs) : windowMs;
long ownEnd = st + (video ? s.getDuration() : win);
/* The source ended before the start of the period: skip it. */
if (ownEnd <= periodStart) {
continue;
}
long natural = Math.max(st, periodStart);
/* A slide starts at the moment of the source start, but not earlier than nextAllowed: this way the sources go in a chain and are not lost. */
long segStart = Math.max(natural, nextAllowed);
if (segStart >= periodEnd) {
break;
}
/* A shifted slide is shown from its beginning, an unshifted one from the place where it is at the moment segStart. */
long anchor = segStart > natural ? segStart : st;
/* End of the slot: no shorter than the slide duration. */
long slotEnd = Math.max(anchor + win, segStart + windowMs);
long clipEnd = Math.min(slotEnd, periodEnd);
if (clipEnd <= segStart) {
continue;
}
/* The gap between slides is filled with an empty segment. */
if (segStart > cursor) {
out.add(SliderSegment.empty(cursor, segStart));
}
out.add(SliderSegment.source(segStart, clipEnd, s.getType(), s.getId(),
s.getStartTime(), anchor, s.getDuration(), coef));
cursor = clipEnd;
/* The next slide is no earlier than the end of the slot plus the minimum pause. */
nextAllowed = slotEnd + minPauseMs;
}
if (cursor < periodEnd) {
out.add(SliderSegment.empty(cursor, periodEnd));
}
/* Merge neighboring segments of the same source and neighboring empty ones. */
out = merge(out);
}
if (out.isEmpty() && periodEnd > periodStart) {
out.add(SliderSegment.empty(periodStart, periodEnd));
}
return new SliderTimelineMap(out.toArray(new SliderSegment[0]));
}
/* Merging: two consecutive empty segments or two segments of the same source become one. */
private static List<SliderSegment> merge(List<SliderSegment> in) {
List<SliderSegment> r = new ArrayList<>();
for (SliderSegment s : in) {
if (!r.isEmpty()) {
SliderSegment last = r.get(r.size() - 1);
boolean same = last.kind == s.kind && last.end == s.start
&& (s.kind == SliderSegmentKind.EMPTY || last.sourceId == s.sourceId);
if (same) {
SliderSegment merged = s.kind == SliderSegmentKind.EMPTY
? SliderSegment.empty(last.start, s.end)
: SliderSegment.source(last.start, s.end, last.sourceType,
last.sourceId, last.contentBase, last.anchor, last.sourceDuration, last.coef);
r.set(r.size() - 1, merged);
continue;
}
}
r.add(s);
}
return r;
}
}SliderTimelineMap is the schedule of the slider. From the list of project sources and the settings "Slide duration, s", "Min pause, s" and "Time mode" it builds an array of segments covering the whole project period: at every moment of time it is known which source is shown now or that the slider is empty.
Data. All sources of the types VIDEO and IMAGE from the whole tree (getChildsRecurse()) are taken from the project, sorted by start time. Other source types are ignored.
Settings. The parameters of build(...): windowMs is the slide duration, minPauseMs is the minimum pause (both multiplied by the player speed), coef is the multiplier of the time mode.
Drawing. The class does not draw, it only lays out time. The algorithm is greedy:
- For each source in the order of start the show window
winis calculated: for a videomax(clip length, windowMs), for an imagewindowMs. - A source that ended before the start of the period is skipped; a source that starts after the end of the period stops the construction.
- The start of a slide is the start of the source (or the start of the period if the source is already running), but not earlier than
nextAllowed. If the start falls into the slot or the pause of the previous slide, the slide is shifted to its end: the sources go in a chain and do not disappear. - The end of the slot is
max(anchor + win, segStart + windowMs); the end of the segment is clipped by the project period. - Gaps between slides, the beginning and the end of the period are filled with
EMPTYsegments. -
merge(...)glues neighboringEMPTYsegments and neighboring segments of the same source.
If there are no sources, the map consists of one EMPTY for the whole period. find(t) is a binary search by start.
- The map is cached in
SliderIndicatorand is rebuilt when the period, the settings, the player speed or the set of videos and images change (the keymapKey(...)). - The window and the pause are multiplied by the player speed, so the layout depends on it.
- The start time is taken from the project (
getStartTime()), so the order of slides is determined by the position of the sources on the scale, not by their order in the list. - The class is package-private.