Skip to content

SliderTimelineMap

roosit edited this page Oct 16, 2026 · 2 revisions

SliderTimelineMap

Up | Previous | Русский

Full source with short explanations

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. */
/* It is built from the real start times of the project sources, with a greedy layout. */
final class SliderTimelineMap {

    /* Segments in ascending time order. */
    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 set 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 project videos and images, in the order of their 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 plus the pause). */
            long nextAllowed = periodStart;
            for (ISource s : vis) {
                long st = s.getStartTime();
                if (st >= periodEnd) {
                    break;
                }
                /* A video window is no shorter than its length (a long clip is fully visible, a short one freezes until the slide length); */
                /* an 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 period began - skip it. */
                if (ownEnd <= periodStart) {
                    continue;
                }
                long natural = Math.max(st, periodStart);
                /* A slide starts at the moment the source starts, 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 the beginning, an unshifted one - from the place where it is at the moment segStart. */
                long anchor = segStart > natural ? segStart : st;
                /* The 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 - not earlier than the end of the slot plus the minimum pause. */
                nextAllowed = slotEnd + minPauseMs;
            }
            if (cursor < periodEnd) {
                out.add(SliderSegment.empty(cursor, periodEnd));
            }
            /* Merge adjacent segments of the same source and adjacent 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;
    }
}

Purpose

SliderTimelineMap is the slider schedule. From the list of project sources and the "Slide duration, s", "Min pause, s", and "Time mode" settings, it builds an array of segments covering the whole project period: at every moment it is known which source is being shown or that the slider is empty.

How it works

Data. All sources of the VIDEO and IMAGE types are taken from the whole tree of the project (getChildsRecurse()), 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:

  1. For each source in start order, the display window win is calculated: for a video max(clip length, windowMs), for an image windowMs.
  2. A source that ended before the period began is skipped; a source that begins after the end of the period stops the building.
  3. A slide starts at the start of the source (or at the start of the period, if the source is already running), but not earlier than nextAllowed. If the start falls into the slot or pause of the previous slide, the slide is shifted to its end — the sources go in a chain and do not disappear.
  4. The end of the slot is max(anchor + win, segStart + windowMs); the end of the segment is cut off by the project period.
  5. The gaps between slides, and the beginning and end of the period, are filled with EMPTY segments.
  6. merge(...) merges adjacent EMPTY segments and adjacent segments of the same source.

If there are no sources, the map consists of a single EMPTY for the whole period. find(t) is a binary search by start.

Notes and limitations

  • The map is cached in SliderIndicator and rebuilt when the period, the settings, the player speed, or the set of videos and images changes (the mapKey(...) key).
  • 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 defined by the position of the sources on the timeline, not by their order in the list.
  • The class is package-private.

Clone this wiki locally