From 02440be55d59655f6a61d78cd0452ed8f1c5a083 Mon Sep 17 00:00:00 2001 From: jimmy Date: Tue, 17 Nov 2026 00:28:51 +1300 Subject: [PATCH] feat: sequence editor undo/redo, collapsible browser, playhead easing Sequence editor: undo and redo, a collapsible browser with chips, search over sequences too, name in the header row, Beats/Seconds dropdown, Delete beside Save. Playhead eases between poll results. Lanes keep their phase when a step ends late, so they no longer drift. The rig's default room is two columns of bars facing each other, and the simulator has a read-only debug hook for the playback checks. Co-Authored-By: Claude Haiku 5.5 --- led-simulator/web/app.js | 5 + main/seqplay.c | 22 ++- sim-controller/components/sim/sim_rig.c | 13 +- web/static/seq_timeline.js | 186 ++++++++++++++++++++---- web/static/sequences.js | 23 --- web/static/style.css | 78 ++++++---- web/templates/index.html | 34 ++--- 7 files changed, 252 insertions(+), 109 deletions(-) diff --git a/led-simulator/web/app.js b/led-simulator/web/app.js index fb8070f..16bcb9a 100644 --- a/led-simulator/web/app.js +++ b/led-simulator/web/app.js @@ -1259,5 +1259,10 @@ } } + // Test hook: ?debug=1 exposes the strips' current frames (read-only) for the playback checks + if (params.get('debug') === '1') { + window.simDebug = () => app.strips.map((s) => ({ name: s.name, frame: Array.from(s.frame) })); + } + main(); })(); diff --git a/main/seqplay.c b/main/seqplay.c index 2ff84a5..d5c5b40 100644 --- a/main/seqplay.c +++ b/main/seqplay.c @@ -80,7 +80,7 @@ typedef struct { } player_t; typedef struct { - char m[MACS_MAX][13]; + char m[MACS_MAX][GROUP_ID_MAX]; // a MAC, or a Wi-Fi driver's hostname int n; } macs_t; @@ -184,8 +184,10 @@ static bool macs_has(const macs_t *s, const char *mac) static void macs_add(macs_t *s, const char *mac) { - if (s->n < MACS_MAX && !macs_has(s, mac)) { - snprintf(s->m[s->n++], 13, "%s", mac); + size_t len = strlen(mac); + if (s->n < MACS_MAX && len < sizeof(s->m[0]) && !macs_has(s, mac)) { + memcpy(s->m[s->n], mac, len + 1); + s->n++; } } @@ -836,6 +838,16 @@ static void fire(const char *seq_id, const char *zone_id, const char *profile_id static void step_lane(player_t *p, int i, int64_t now) { lane_t *l = &p->lane[i]; + // A time lane's next step starts when this one was due, not when the player noticed: restarting from + // now loses the lateness on every step, more on lanes with more steps, so lanes drift apart. One that + // fell far behind (a stall) starts from now rather than racing through steps to catch up. + int64_t start = now; + if (l->timed) { + int64_t due = l->t0 + (int64_t)l->step[l->idx].ms * 1000; + if (due <= now && now - due < 500000) { + start = due; + } + } if (l->idx + 1 >= l->n) { if (p->loop) { if (i == 0) { @@ -844,7 +856,7 @@ static void step_lane(player_t *p, int i, int64_t now) } l->count = 0; l->idx = 0; - l->t0 = now; + l->t0 = start; l->last[0] = 0; // step 0 goes up again on each wrap if (l->chase) { chase_move(l); @@ -857,7 +869,7 @@ static void step_lane(player_t *p, int i, int64_t now) } else { l->count = 0; l->idx++; - l->t0 = now; + l->t0 = start; if (l->chase) { chase_move(l); } diff --git a/sim-controller/components/sim/sim_rig.c b/sim-controller/components/sim/sim_rig.c index 0b95897..60f99da 100644 --- a/sim-controller/components/sim/sim_rig.c +++ b/sim-controller/components/sim/sim_rig.c @@ -413,7 +413,7 @@ static cJSON *validate_room(const cJSON *raw, const char *name, char *err, size_ return room; } -// The original rig: 16 bars of 119 LEDs in 2 columns x 8 rows, pixel 0 at the centre line +// The original rig: 16 bars of 119 LEDs in 2 columns x 8 rows, pixel 0 at the centre line (see the loop below) static cJSON *default_room(void) { const int rows = 8, cols = 2, leds = 119; @@ -430,9 +430,10 @@ static cJSON *default_room(void) cJSON_AddNumberToObject(def, "pitch_m", pitch); cJSON *fx = cJSON_AddObjectToObject(raw, "fixtures"); for (int i = 0; i < rows * cols; i++) { - int row = i / cols, col = i % cols; - bool reversed = col < cols / 2; // left of the centre line: runs leftwards from its right end - double left = -span / 2 + col * (length + gap); + // Bars 1-8 are the left column, top to bottom; 9-16 the right column. Pixel 0 sits at the centre line + // and the bar runs outwards from it: the left column leftwards, the right column rightwards. + int row = i % rows, col = i / rows; + bool left_col = col == 0; char label[16], mac[18]; snprintf(label, sizeof(label), "sim-%02d", i + 1); snprintf(mac, sizeof(mac), MAC_PREFIX ":00:00:%02x", i + 1); @@ -440,9 +441,9 @@ static cJSON *default_room(void) cJSON_AddStringToObject(f, "label", label); cJSON_AddStringToObject(f, "def", "bar-119"); cJSON_AddStringToObject(f, "mac", mac); - cJSON_AddNumberToObject(f, "x", reversed ? left + length : left); + cJSON_AddNumberToObject(f, "x", left_col ? -gap / 2 : gap / 2); cJSON_AddNumberToObject(f, "y", depth - 0.3 - (row + 0.5) * row_h); - cJSON_AddNumberToObject(f, "direction", reversed ? -90 : 90); + cJSON_AddNumberToObject(f, "direction", left_col ? -90 : 90); } char err[160]; cJSON *room = validate_room(raw, "default", err, sizeof(err)); diff --git a/web/static/seq_timeline.js b/web/static/seq_timeline.js index 7e4cbf8..cb07ed0 100644 --- a/web/static/seq_timeline.js +++ b/web/static/seq_timeline.js @@ -25,7 +25,9 @@ const seqTl = { loopEndSet: false, // the user moved the end marker brush: '', // the browser item double-click adds lastLen: TL_BAR, - head: null, // playhead {pos, at, rate, max} + head: null, // playhead {pos, at, rate, max, wrap} + headErr: 0, // the playhead's easing offset after a status poll: glides to the server's position instead of jumping + headErrT: 0, pollTimer: null, raf: 0, drag: null, @@ -364,6 +366,9 @@ function seqTlOnOpen(doc) { : '', ); seqTl.on = true; + tlHist.undo = []; + tlHist.redo = []; + tlHist.cur = null; // the first render records the opened state tlRender(); tlStartPoll(); } @@ -415,6 +420,7 @@ function tlSelect(sel) { function tlRender() { const canvas = tlEl('tl-canvas'); if (!canvas || !seqTl.on) return; + tlRecord(); // every edit ends in a render: an edit since the last one becomes an undo step if (seqTl.snapAuto) seqTl.snap = tlAutoSnap(); // Auto follows the zoom, so drags land on the ticks shown const ppb = seqTl.ppb; const timed = tlTimed(); @@ -426,14 +432,9 @@ function tlRender() { canvas.style.setProperty('--ppb', String(ppb)); canvas.innerHTML = ''; - // Units toggle and BPM readout - document.querySelectorAll('#tl-units button').forEach((b) => { - b.classList.toggle('on', (b.dataset.v === 'time') === timed); - // A one-shot is always in seconds: Beats is greyed out while Loop is off - b.disabled = !loop && b.dataset.v !== 'time'; - }); - const bpm = tlEl('tl-bpm'); - if (bpm) bpm.textContent = `${Math.round(tlBpm())} BPM`; + // A one-shot is always in seconds: Beats is greyed out while Loop is off + const beatsOpt = document.querySelector('#sequence-editor-mode option[value="beats"]'); + if (beatsOpt) beatsOpt.disabled = !loop; // Ruler const rrow = document.createElement('div'); @@ -721,11 +722,7 @@ function tlRenderInspector() { const track = sel && sel.track ? tlTrack(sel.track) : null; if (clip) return tlInspectClip(host, clip); if (track) return tlInspectTrack(host, track); - const hint = document.createElement('div'); - hint.className = 'muted-text'; - hint.textContent = - 'Drag a preset from the browser onto a track. Drag clips to move them and their edges to resize. Double-click a track to add the preset picked in the browser. Click a track name for its groups. Delete removes the selected clip.'; - host.appendChild(hint); + // Nothing selected: the inspector stays empty (and hidden by the stylesheet) } function tlInspectClip(host, c) { @@ -797,7 +794,6 @@ function tlInspectClip(host, c) { const btns = document.createElement('div'); btns.className = 'tl-insp-btns'; btns.appendChild(tlBtn('Edit', 'Edit the preset or sequence', () => tlEditPlay(c.play))); - btns.appendChild(tlBtn('Duplicate', 'Copy it to the next free space (Ctrl+D)', () => tlDuplicate(c))); btns.appendChild(tlBtn('Delete', 'Remove the clip (Delete)', () => tlDelete(c), 'btn btn-danger btn-small')); host.appendChild(btns); } @@ -888,6 +884,71 @@ function tlInspectTrack(host, t) { host.appendChild(btns); } +/* --------------------------------------------------------------- undo/redo */ + +const TL_HISTORY_MAX = 100; +const tlHist = { undo: [], redo: [], cur: null }; // states as JSON; cur is the one on screen + +/** What undo brings back: clips, tracks, the window and units. Not the selection, zoom or scroll. */ +function tlSnapshot() { + return JSON.stringify({ + clips: seqTl.clips, + targets: seqEd.targets, + loopEnd: seqTl.loopEnd, + loopEndSet: seqTl.loopEndSet, + mode: (tlEl('sequence-editor-mode') || {}).value || '', + loop: tlLoop(), + }); +} + +/** Called on each render: if the timeline changed since the last one, the old state is an undo step. */ +function tlRecord() { + const s = tlSnapshot(); + if (tlHist.cur !== null && s !== tlHist.cur) { + tlHist.undo.push(tlHist.cur); + if (tlHist.undo.length > TL_HISTORY_MAX) tlHist.undo.shift(); + tlHist.redo = []; + } + tlHist.cur = s; + tlUndoButtons(); +} + +function tlUndoButtons() { + const u = tlEl('tl-undo'); + const r = tlEl('tl-redo'); + if (u) u.disabled = !tlHist.undo.length; + if (r) r.disabled = !tlHist.redo.length; +} + +function tlRestore(s) { + const st = JSON.parse(s); + seqTl.clips = st.clips; + seqEd.targets = st.targets; + seqTl.loopEnd = st.loopEnd; + seqTl.loopEndSet = st.loopEndSet; + const mode = tlEl('sequence-editor-mode'); + if (mode && st.mode) mode.value = st.mode; + const loop = tlEl('sequence-editor-loop'); + if (loop) loop.checked = st.loop; + const sel = seqTl.sel; + if (sel && ((sel.clip && !tlClip(sel.clip)) || (sel.track && !tlTrack(sel.track)))) seqTl.sel = null; + tlHist.cur = s; // so the render below doesn't record the restore as a new edit + tlBuildSnap(); + tlRender(); +} + +function tlUndo() { + if (!tlHist.undo.length || seqTl.drag) return; + tlHist.redo.push(tlHist.cur); + tlRestore(tlHist.undo.pop()); +} + +function tlRedo() { + if (!tlHist.redo.length || seqTl.drag) return; + tlHist.undo.push(tlHist.cur); + tlRestore(tlHist.redo.pop()); +} + /* ------------------------------------------------------------------- edits */ function tlAddAt(uid, start, play) { @@ -967,7 +1028,6 @@ function tlSetUnits(timed) { seqTl.loopEnd = q(seqTl.loopEnd); seqTl.lastLen = timed ? 4 : TL_BAR; mode.value = timed ? 'time' : 'beats'; - mode.dispatchEvent(new Event('change')); tlBuildSnap(); tlRender(); } @@ -1221,7 +1281,6 @@ async function tlPoll() { let lanes = null; if (st && st.overlay && st.overlay.active && String(st.overlay.sequence_id) === sequenceEditorId) lanes = st.overlay.lanes; else if (st && st.active && String(st.sequence_id) === sequenceEditorId) lanes = st.lanes; - seqTl.head = null; if (Array.isArray(lanes)) { const { map } = tlToLanes(); const li = lanes.findIndex((l, i) => l && l.started && !l.done && map[i]); @@ -1231,11 +1290,27 @@ async function tlPoll() { const timed = l.mode === 'time'; const s = m.starts[l.step] != null ? m.starts[l.step] : 0; const into = timed ? (l.elapsed_ms || 0) / 1000 : l.beat_in_step || 0; - seqTl.head = { pos: s + into, at: performance.now(), rate: timed ? 1 : tlBpm() / 60, max: m.ends[l.step] != null ? m.ends[l.step] : s + into }; - } - } + const looping = !!(st && st.active && String(st.sequence_id) === sequenceEditorId); + const now = performance.now(); + const next = { + pos: s + into, + at: now, + rate: timed ? 1 : tlBpm() / 60, + max: m.ends[l.step] != null ? m.ends[l.step] : s + into, + wrap: looping ? tlLoopLen() : 0, // a looping sequence wraps at its window; a one-shot stops at its lane's end + }; + // Keep the head where it was on screen and ease it onto the server's position, so a poll never jumps it + const old = seqTl.head; + if (old) { + const d = tlHeadDiff(tlHeadAt(old, now) + seqTl.headErr, tlHeadAt(next, now), next.wrap); + seqTl.headErr = Math.abs(d) < 0.5 ? d : 0; + } else seqTl.headErr = 0; + seqTl.headErrT = now; + seqTl.head = next; + } else seqTl.head = null; + } else seqTl.head = null; } catch (_) { - /* try again */ + /* try again: the head stays where it is */ } } else { seqTl.head = null; @@ -1243,12 +1318,31 @@ async function tlPoll() { seqTl.pollTimer = setTimeout(tlPoll, 250); } +/** Where the head is at time now, extrapolated from its last poll. */ +function tlHeadAt(h, now) { + const x = h.pos + ((now - h.at) / 1000) * h.rate; + return h.wrap > 0 ? ((x % h.wrap) + h.wrap) % h.wrap : Math.min(h.max, x); +} + +/** a - b, the short way round a loop of length wrap (no wrap: plain difference). */ +function tlHeadDiff(a, b, wrap) { + const d = a - b; + return wrap > 0 ? ((((d + wrap / 2) % wrap) + wrap) % wrap) - wrap / 2 : d; +} + function tlDrawHead() { const ph = tlEl('tl-playhead'); const lcd = tlEl('tl-pos'); const h = seqTl.head; + const now = performance.now(); let pos = 0; - if (h) pos = Math.min(h.max, h.pos + ((performance.now() - h.at) / 1000) * h.rate); + if (h) { + pos = tlHeadAt(h, now) + seqTl.headErr; + if (h.wrap > 0) pos = ((pos % h.wrap) + h.wrap) % h.wrap; + else pos = Math.min(h.max, pos); + seqTl.headErr *= Math.exp(-(now - seqTl.headErrT) / 150); // ease out the correction + seqTl.headErrT = now; + } else seqTl.headErr = 0; if (ph) { ph.hidden = !h; if (h) ph.style.left = `calc(var(--tl-head-w) + ${pos * seqTl.ppb}px)`; @@ -1279,12 +1373,36 @@ document.addEventListener('DOMContentLoaded', () => { if (el) el.addEventListener(ev, fn); }; on('tl-add-track', 'change', (e) => tlAddTrack(e.target.value)); - on('tl-per-driver', 'click', () => { - addTargetsPerDriver(); - if (seqEd.targets.length > TL_TRACKS_MAX) alert(`Only the first ${TL_TRACKS_MAX} tracks can have clips.`); - tlRender(); - }); + on('tl-undo', 'click', tlUndo); + on('tl-redo', 'click', tlRedo); on('tl-browser-search', 'input', tlRenderBrowser); + // The browser panel collapses to a strip with its arrow, giving the tracks the width; the choice is remembered + const setBrowser = (show) => { + const panel = document.querySelector('#seq-tl .tl-browser'); + const btn = tlEl('tl-browser-toggle'); + if (panel) panel.classList.toggle('collapsed', !show); + if (btn) { + btn.innerHTML = show ? '›' : '‹'; + btn.title = show ? 'Hide the browser' : 'Show the browser'; + btn.setAttribute('aria-expanded', String(show)); + } + }; + let browserShown = true; + try { + browserShown = localStorage.getItem('seqTlBrowser') !== '0'; + } catch (_) { + /* private mode */ + } + setBrowser(browserShown); + on('tl-browser-toggle', 'click', () => { + browserShown = !browserShown; + setBrowser(browserShown); + try { + localStorage.setItem('seqTlBrowser', browserShown ? '1' : '0'); + } catch (_) { + /* private mode */ + } + }); on('tl-browser-refresh', 'click', async () => { try { const r = await fetch('/presets', { headers: { Accept: 'application/json' } }); @@ -1309,7 +1427,12 @@ document.addEventListener('DOMContentLoaded', () => { }); } on('tl-zoom-out', 'click', () => tlZoom(1 / 1.4, seqTl.hoverX)); - document.querySelectorAll('#tl-units button').forEach((b) => b.addEventListener('click', () => tlSetUnits(b.dataset.v === 'time'))); + // Units dropdown: put the old value back first, so tlSetUnits sees a change and converts the clips + on('sequence-editor-mode', 'change', (e) => { + const timed = e.target.value === 'time'; + e.target.value = timed ? 'beats' : 'time'; + tlSetUnits(timed); + }); on('tl-play', 'click', async () => { const ok = await saveSequenceEditor(); if (ok === false || !sequenceEditorId) return; @@ -1340,6 +1463,13 @@ document.addEventListener('DOMContentLoaded', () => { if (!seqTl.on || !modal || !modal.classList.contains('active')) return; if (e.target && e.target.closest && e.target.closest('input, select, textarea, [contenteditable]')) return; if (document.querySelector('.modal.active:not(#sequence-editor-modal)')) return; + const k = e.key.toLowerCase(); + if ((e.ctrlKey || e.metaKey) && (k === 'z' || k === 'y')) { + e.preventDefault(); + if (k === 'y' || e.shiftKey) tlRedo(); + else tlUndo(); + return; + } const c = seqTl.sel && seqTl.sel.clip ? tlClip(seqTl.sel.clip) : null; if (!c) return; if (e.key === 'Delete' || e.key === 'Backspace') { diff --git a/web/static/sequences.js b/web/static/sequences.js index c21b6df..aeace1f 100644 --- a/web/static/sequences.js +++ b/web/static/sequences.js @@ -691,29 +691,6 @@ function addSeqTarget(t) { return true; } -/** One target per driver in the current zone's groups, in device-list order (for chases). */ -function addTargetsPerDriver() { - const macs = []; - const gids = seqEd.zoneGroupIds.length ? seqEd.zoneGroupIds : Object.keys(seqEd.groupsMap); - gids.forEach((gid) => { - const g = seqEd.groupsMap[gid]; - (g && Array.isArray(g.devices) ? g.devices : []).forEach((m) => { - const mac = String(m).toLowerCase().replace(/[^0-9a-f]/g, ''); - if (mac.length === 12 && !macs.includes(mac)) macs.push(mac); - }); - }); - if (!macs.length) { - alert('No drivers found in this zone\'s groups.'); - return; - } - const have = new Set(seqEd.targets.filter((t) => t.devices.length === 1 && !t.group_ids.length).map((t) => t.devices[0])); - macs.forEach((mac) => { - if (have.has(mac)) return; - const d = seqEd.devicesMap[mac]; - addSeqTarget({ name: (d && d.name) || mac, devices: [mac] }); - }); -} - /** Clip "what plays" picker: presets or one-shot sequences (a rest too). Values: p:, a:, rest. */ function buildStepPlaySelect(step) { const sel = document.createElement('select'); diff --git a/web/static/style.css b/web/static/style.css index f30ae20..6303ae8 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -1535,6 +1535,26 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { flex-shrink: 0; } +/* Sequence editor: the name field sits in the header row, between the title and the buttons */ +#sequence-editor-modal .modal-head { + align-items: center; +} + +#sequence-editor-modal .modal-head h2 { + flex: 0 0 auto; +} + +#sequence-editor-modal .modal-head #sequence-editor-name { + flex: 1 1 12rem; + max-width: 24rem; + min-width: 0; + margin: 0; +} + +#sequence-editor-modal .modal-head .modal-top-actions { + margin-left: auto; +} + .modal-content label { display: block; margin-top: 1rem; @@ -2710,10 +2730,13 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { * presets; --ppb is pixels per beat (or second), set on the canvas. */ #sequence-editor-modal .modal-content { - max-width: min(1280px, 96vw); - width: 96vw; - max-height: 94vh; + max-width: 100vw; + width: 100vw; + height: 100dvh; + max-height: 100dvh; + border-radius: 0; overflow-y: auto; + box-sizing: border-box; } .tl { @@ -2770,6 +2793,12 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { background: #55555d; } +.tl-tbtn:disabled { + color: #777; + background: #45454c; + cursor: default; +} + #tl-play { color: #6cdc6c; } @@ -3144,21 +3173,6 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { padding: 1.25rem; } -.tl-addtrack { - display: flex; - flex-wrap: wrap; - gap: 0.5rem; - align-items: center; - padding: 0.45rem 0.6rem; - background: #26262b; - border-top: 1px solid #111; -} - -#sequence-editor-modal .tl-addtrack select { - width: auto; - margin: 0; -} - .tl-browser { flex: 0 0 13rem; display: flex; @@ -3170,6 +3184,22 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { min-width: 0; } +/* Collapsed: only the arrow shows, in a thin strip */ +.tl-browser.collapsed { + flex: 0 0 auto; + padding: 0.5rem 0.3rem; +} + +.tl-browser.collapsed > :not(.tl-browser-head), +.tl-browser.collapsed .tl-browser-head > :not(#tl-browser-toggle) { + display: none; +} + +.tl-browser-head > span { + margin-right: auto; + margin-left: 0.4rem; +} + .tl-browser-head { display: flex; justify-content: space-between; @@ -3268,6 +3298,10 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { text-align: left; } +.tl-inspector:empty { + display: none; +} + .tl-insp-title { font-size: 0.72rem; text-transform: uppercase; @@ -3418,14 +3452,6 @@ html.simulated-bpm-mode .seq-switch-toggle-wrap { text-overflow: ellipsis; white-space: nowrap; } - #sequence-editor-modal .tl-addtrack { - flex-wrap: wrap; - gap: 0.4rem; - } - #sequence-editor-modal .tl-addtrack select { - flex: 1 1 10rem; - min-width: 0; - } #sequence-editor-modal .tl-inspector { padding: 0.5rem; } diff --git a/web/templates/index.html b/web/templates/index.html index c56f604..4dd541b 100644 --- a/web/templates/index.html +++ b/web/templates/index.html @@ -377,16 +377,14 @@ -