Files
wireless-docs/scrubber.js

352 lines
13 KiB
JavaScript

// scrubber.js
const SCRUBBER_CSS = `
.visualizer-ui { font-family: sans-serif; background: #222; padding: 12px; border-radius: 6px; display: flex; flex-direction: column; gap: 12px; color: white; user-select: none; }
.scrubber-row { display: flex; gap: 12px; align-items: center; }
.btn { background: #444; border: 1px solid #666; color: white; padding: 4px 8px; border-radius: 4px; cursor: pointer; min-width: 4ch; text-align: center; font-weight: bold; transition: background 0.1s; }
.btn:hover { background: #555; }
.track-container { flex-grow: 1; height: 24px; padding: 0 10px; cursor: pointer; display: flex; align-items: center; }
.track-inner { position: relative; width: 100%; height: 100%; pointer-events: none; }
.track-bg { position: absolute; left: -10px; right: -10px; top: 8px; height: 8px; background: rgba(255,255,255,0.2); border-radius: 4px; }
.track-fill { position: absolute; left: -10px; top: 8px; height: 8px; background: #f00; width: 10px; border-radius: 4px; }
.track-handle { position: absolute; width: 16px; height: 16px; background: #fff; border-radius: 50%; top: 12px; transform: translate(-50%, -50%); z-index: 3; }
.marker { position: absolute; top: 8px; height: 8px; transform: translateX(-50%); pointer-events: none; z-index: 1; }
.marker-event { width: 2px; background: rgba(255,255,255,0.5); }
.marker-label { width: 4px; background: gold; z-index: 2; }
`;
export class SubtickScrubber {
constructor(diorama) {
this.container = diorama.element;
this.frame = diorama.frame;
this.playMode = 'paused';
this.speed = 0.5;
this.isLooping = false;
this.lastFrameTime = performance.now();
this.playbackAccumulator = 0;
if (!document.getElementById('scrubber-styles')) {
const style = document.createElement('style');
style.id = 'scrubber-styles';
style.textContent = SCRUBBER_CSS;
document.head.appendChild(style);
}
this.buildUI();
this.frame.subscribe(() => this.syncUI());
this._loop = this.loop.bind(this);
requestAnimationFrame(this._loop);
}
buildUI() {
this.container.className = 'visualizer-ui';
this.subRow = document.createElement('div');
this.subRow.className = 'scrubber-row';
this.subRow.innerHTML = `
<div class="btn" id="btn-play-sub" title="Animate Events">S&gt;</div>
<div class="btn" id="btn-prev-sub" title="Previous Event">&lt;</div>
<div class="track-container" id="track-sub-container">
<div class="track-inner" id="track-sub-inner">
<div class="track-bg"></div>
<div class="track-fill" id="fill-sub"></div>
<div id="markers-sub"></div>
<div class="track-handle" id="handle-sub"></div>
</div>
</div>
<div class="btn" id="btn-next-sub" title="Next Event">&gt;</div>
`;
this.tickRow = document.createElement('div');
this.tickRow.className = 'scrubber-row';
this.tickRow.innerHTML = `
<div class="btn" id="btn-play" title="Play Realtime">R&gt;</div>
<div class="btn" id="btn-loop" title="Toggle Loop">Loop</div>
<div class="btn" id="btn-prev" title="Previous Tick">&lt;&lt;</div>
<div class="track-container" id="track-tick-container">
<div class="track-inner" id="track-tick-inner">
<div class="track-bg"></div>
<div class="track-fill" id="fill-tick"></div>
<div id="markers-tick"></div>
<div class="track-handle" id="handle-tick"></div>
</div>
</div>
<div class="btn" id="btn-next" title="Next Tick">&gt;&gt;</div>
`;
this.container.appendChild(this.subRow);
this.container.appendChild(this.tickRow);
this.bindEvents();
this.generateTickMarkers();
this.syncUI();
}
generateTickMarkers() {
const markerContainer = this.container.querySelector('#markers-tick');
const maxTime = this.getMaxTime();
if (maxTime <= 0) return;
const labeledTimes = new Set();
for (const idx of this.frame.world.labels.values()) {
if (idx < this.frame.world.events.length) {
labeledTimes.add(this.frame.world.events[idx].time);
}
}
for (const t of labeledTimes) {
if (t < 0) continue;
const pct = (t / maxTime) * 100;
const marker = document.createElement('div');
marker.className = 'marker marker-label';
marker.style.left = `${pct}%`; // Perfectly aligns relative to the inner padded box
markerContainer.appendChild(marker);
}
}
getMaxTime() {
const evs = this.frame.world.events;
return evs.length > 0 ? evs[evs.length - 1].time : 0;
}
setupDraggableTrack(containerId, innerId, onDrag) {
const wrapper = this.container.querySelector('#' + containerId);
const inner = this.container.querySelector('#' + innerId);
let isDragging = false;
const update = (e) => {
const rect = inner.getBoundingClientRect();
// Pct is calculated purely against the interior safe-zone
let pct = (e.clientX - rect.left) / rect.width;
pct = Math.max(0, Math.min(1, pct));
onDrag(pct);
};
wrapper.addEventListener('mousedown', (e) => {
isDragging = true;
this.playMode = 'paused';
update(e);
});
window.addEventListener('mousemove', (e) => {
if (isDragging) update(e);
});
window.addEventListener('mouseup', () => {
isDragging = false;
});
}
bindEvents() {
const getEl = id => this.container.querySelector('#' + id);
getEl('btn-loop').onclick = () => {
this.isLooping = !this.isLooping;
this.syncUI();
};
// -- Primary Playback --
getEl('btn-play').onclick = () => {
if (this.playMode === 'realtime') {
this.playMode = 'paused';
} else {
if (this.frame.currentTime >= this.getMaxTime() || this.frame.currentTime < 0) {
this.frame.seek(0);
}
this.playMode = 'realtime';
}
this.syncUI();
};
getEl('btn-prev').onclick = () => {
this.playMode = 'paused';
let t = Math.floor(this.frame.currentTime - 1);
if (t < 0) t = this.isLooping ? Math.floor(this.getMaxTime()) : 0;
this.frame.seek(t);
this.syncUI();
};
getEl('btn-next').onclick = () => {
this.playMode = 'paused';
let t = Math.floor(this.frame.currentTime + 1);
const maxT = this.getMaxTime();
if (t > maxT) t = this.isLooping ? 0 : maxT;
this.frame.seek(t);
this.syncUI();
};
this.setupDraggableTrack('track-tick-container', 'track-tick-inner', (pct) => {
// Force integral tick seeking
const targetTime = Math.round(pct * this.getMaxTime());
this.frame.seek(targetTime);
this.syncUI();
});
// -- Secondary Playback --
getEl('btn-play-sub').onclick = () => {
this.playMode = this.playMode === 'subtick' ? 'paused' : 'subtick';
this.syncUI();
};
getEl('btn-prev-sub').onclick = () => {
this.playMode = 'paused';
let idx = this.frame.currentIndex - 1;
if (idx < this.frame.world.initialIndex) {
idx = this.isLooping ? this.frame.world.events.length : this.frame.world.initialIndex;
}
this.frame.seekIndex(idx);
this.syncUI();
};
getEl('btn-next-sub').onclick = () => {
this.playMode = 'paused';
let idx = this.frame.currentIndex + 1;
if (idx > this.frame.world.events.length) {
idx = this.isLooping ? this.frame.world.initialIndex : this.frame.world.events.length;
}
this.frame.seekIndex(idx);
this.syncUI();
};
this.setupDraggableTrack('track-sub-container', 'track-sub-inner', (pct) => {
if (this.currentTickEventCount === 0) return;
const targetLocalIdx = Math.round(pct * this.currentTickEventCount);
// LOCK TIME: Dragging to the end of the subtick track completes the tick.
// If we don't lock currentTime, it jumps to the next tick instantly, breaking the UI drag loop.
const lockTime = this.frame.currentTime;
this.frame.seekIndex(this.currentTickStartIndex + targetLocalIdx);
this.frame.currentTime = lockTime;
this.syncUI();
});
}
syncUI() {
const getEl = id => this.container.querySelector('#' + id);
getEl('btn-play').textContent = this.playMode === 'realtime' ? '||' : 'R>';
getEl('btn-play-sub').textContent = this.playMode === 'subtick' ? '||' : 'S>';
getEl('btn-loop').style.background = this.isLooping ? '#4a4' : '#444';
getEl('btn-loop').style.borderColor = this.isLooping ? '#6c6' : '#666';
this.subRow.style.display = this.playMode === 'realtime' ? 'none' : 'flex';
// --- Sync Primary Slider ---
const maxTime = this.getMaxTime();
const displayTime = Math.max(0, this.frame.currentTime);
const tickPct = maxTime > 0 ? (displayTime / maxTime) * 100 : 0;
// Notice the exact CSS math mapping the width against the safe bounds
getEl('fill-tick').style.width = `calc(${tickPct}% + 10px)`;
getEl('handle-tick').style.left = `${tickPct}%`;
// --- Sync Secondary Slider ---
const events = this.frame.world.events;
let startIdx = 0, count = 0;
// Floor the current time so smooth interpolation doesn't lose the integer tick
const currentIntTick = Math.floor(this.frame.currentTime);
for (let i = 0; i < events.length; i++) {
if (events[i].time === currentIntTick) {
if (count === 0) startIdx = i;
count++;
}
}
this.currentTickStartIndex = startIdx;
this.currentTickEventCount = count;
const markerContainer = getEl('markers-sub');
markerContainer.innerHTML = '';
if (count > 0) {
const labeledIndices = new Set(this.frame.world.labels.values());
for (let i = 0; i <= count; i++) {
const absIdx = startIdx + i;
const pct = (i / count) * 100;
const marker = document.createElement('div');
marker.className = 'marker';
marker.style.left = `${pct}%`;
if (labeledIndices.has(absIdx)) marker.classList.add('marker-label');
else marker.classList.add('marker-event');
markerContainer.appendChild(marker);
}
let localCursor = this.frame.currentIndex - startIdx;
localCursor = Math.max(0, Math.min(count, localCursor));
const subPct = (localCursor / count) * 100;
getEl('fill-sub').style.width = `calc(${subPct}% + 10px)`;
getEl('handle-sub').style.left = `${subPct}%`;
getEl('handle-sub').style.display = 'block';
} else {
getEl('fill-sub').style.width = `0px`;
getEl('handle-sub').style.display = 'none';
}
}
loop(time) {
const dt = (time - this.lastFrameTime) / 1000;
this.lastFrameTime = time;
const maxTime = this.getMaxTime();
if (this.playMode === 'realtime') {
let nextTime = this.frame.currentTime + dt * 20 * this.speed;
if (nextTime >= maxTime) {
if (this.isLooping) {
nextTime = nextTime % maxTime;
this.frame.seek(nextTime);
} else {
this.frame.seek(maxTime);
this.playMode = 'paused';
}
} else {
this.frame.seek(nextTime);
}
this.syncUI();
} else if (this.playMode === 'subtick') {
this.playbackAccumulator += dt * 4 * this.speed;
if (this.playbackAccumulator >= 1.0) {
const steps = Math.floor(this.playbackAccumulator);
this.playbackAccumulator -= steps;
const nextIdx = this.frame.currentIndex + steps;
if (nextIdx > this.frame.world.events.length) {
if (this.isLooping) {
this.frame.seekIndex(this.frame.world.initialIndex);
} else {
this.frame.seekIndex(this.frame.world.events.length);
this.playMode = 'paused';
}
} else {
this.frame.seekIndex(nextIdx);
}
}
this.syncUI();
} else {
this.playbackAccumulator = 0;
}
requestAnimationFrame(this._loop);
}
}