feat: pattern gallery as a preset editor; chase steps; sim split with the preset editor

- Pattern gallery shows the controller's presets, one at a time, with a dropdown, Save, Add preset, and
  the controller's field labels, delay limits and colour counts. Edits show in the preview; Save writes to
  the controller only.
- Presets are always timed in ms (beat_time is no longer sent to drivers); beats stay in sequences.
- Chase steps of 0 hold still; the marquee style is removed from the controller, the gallery and the
  driver (submodule commit 12a8e22).
- The rig's split pane opens the preset editor too (Rig / Presets switch and a Preset editor menu entry).
- Sequence editor, room layout and simulator controls from earlier work are included.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Haiku 5.5 committed 2026-11-17 07:16:51 +13:00
1 parent 8c783ab926
commit cb51a726bc
79 files changed
+5183 -934

No files matched your search

+280 -78
View File
@@ -59,6 +59,11 @@
queue: [],
booting: 0,
view: null,
sel: new Set(), // the selected strips; app.selected is the one the drawer shows (when exactly one)
marquee: null, // the box being dragged on the floor, canvas px
zoom: 1, // the room's view: z about its fitted size, pan x/y in canvas px (view only, never saved)
pan: { x: 0, y: 0 },
spaceDown: false,
geo: [],
dirty: true,
selected: null,
@@ -456,6 +461,7 @@
}
for (const s of old.values()) {
powerOff(s);
app.sel.delete(s);
if (app.selected === s) {
app.selected = null;
}
@@ -465,9 +471,6 @@
layoutView();
updateChips();
renderDrawer();
if (app.cfg) {
renderHint();
}
}
function scheduleSave() {
@@ -515,6 +518,7 @@
powerOff(s);
}
app.strips = [];
app.sel = new Set();
app.selected = null;
applyConfig(cfg);
if (readAuto()) {
@@ -529,8 +533,9 @@
const sel = $('room-select');
sel.replaceChildren(...cfg.rooms.map((n) => new Option(n, n, false, n === cfg.room_name)));
app.lastDef = Object.keys(app.room.definitions)[0];
app.zoom = 1; // each room opens fitted
app.pan = { x: 0, y: 0 };
syncStrips();
renderHint();
}
/** A label for a new strip: drivers.name with n from its MAC, which no other room uses, so the name is unique too. */
@@ -551,8 +556,8 @@
async function addFixture(x, y) {
let mac;
try {
const taken = Object.values(app.room.fixtures).map((f) => f.mac).join(',');
mac = (await api('GET', `/sim/api/next-mac?taken=${encodeURIComponent(taken)}`)).mac;
const taken = Object.values(app.room.fixtures).map((f) => f.mac);
mac = (await api('POST', '/sim/api/next-mac', { taken })).mac;
} catch (err) {
banner(`Could not add a strip: ${err.message}`);
return;
@@ -567,19 +572,50 @@
direction: 90,
};
changed();
app.selected = app.strips.find((s) => s.id === label) || null;
select(app.strips.find((s) => s.id === label) || null);
renderDrawer();
}
function deleteFixture(s) {
if (!s || !(s.id in app.room.fixtures)) {
/** Delete every selected strip from the room. */
function deleteSelected() {
const doomed = [...app.sel].filter((s) => s.id in app.room.fixtures);
if (!doomed.length) {
return;
}
delete app.room.fixtures[s.id];
if (app.selected === s) {
app.selected = null;
for (const s of doomed) {
delete app.room.fixtures[s.id];
}
setSelection([]);
changed();
}
/** Move every strip by one offset (snapped to 0.05 m, spacing kept) so their bounding box sits in the middle
* of the room: x = 0 (the room runs -width/2 .. width/2), y = depth / 2. */
function centreStrips() {
const infos = RigLayout.roomStrips(app.room);
if (!infos.length) {
return;
}
let x0 = Infinity;
let x1 = -Infinity;
let y0 = Infinity;
let y1 = -Infinity;
for (const i of infos) {
for (const [x, y] of [[i.x, i.y], [i.x1, i.y1]]) {
x0 = Math.min(x0, x);
x1 = Math.max(x1, x);
y0 = Math.min(y0, y);
y1 = Math.max(y1, y);
}
}
const dx = Math.round((0 - (x0 + x1) / 2) / SNAP_M) * SNAP_M;
const dy = Math.round((app.room.depth / 2 - (y0 + y1) / 2) / SNAP_M) * SNAP_M;
for (const f of Object.values(app.room.fixtures)) {
f.x = round3(f.x + dx);
f.y = round3(f.y + dy);
}
changed();
renderDrawer();
}
// ----- the controller: rig layout and the browser bridge -------------------------------
@@ -658,34 +694,10 @@
return;
}
const b = app.cfg.bridge;
$('bridge-chip').textContent = `${b.name} · ${b.mac}`;
const cc = $('controller-chip');
const on = app.wsOpen && b.running;
cc.textContent = !app.wsOpen ? 'controller: –' : b.running ? 'browser bridge: on' : 'browser bridge: off';
cc.className = `chip ${on ? 'ok' : 'warn'}`;
// "Add the browser bridge" shows when the controller is connected but its bridge is not running
$('add-bridge').hidden = !app.wsOpen || b.running;
const running = app.strips.filter((s) => s.state === 'running').length;
const booting = app.strips.filter((s) => s.state === 'queued' || s.state === 'loading').length;
const crashed = app.strips.filter((s) => s.state === 'crashed').length;
let text = `strips: ${running}/${app.strips.length} running`;
if (booting) {
text += ` · ${booting} booting`;
}
if (crashed) {
text += ` · ${crashed} stopped`;
}
const sc = $('strips-chip');
sc.textContent = text;
sc.className = `chip ${crashed ? 'warn' : running ? 'ok' : ''}`;
}
function renderHint() {
$('hint').innerHTML =
`Room <b>${app.roomName}</b>: ${app.room.width} × ${app.room.depth} m, ${app.strips.length} bars. ` +
'Each bar is the C++ led-driver; it registers with this controller when it powers on, ' +
'and its place in the room goes to the controller\'s rig layout. ' +
'<a href="/" target="_top">Open the controller</a> to drive them.';
}
function renderFacts() {
const s = app.selected;
@@ -735,6 +747,15 @@
const wasHidden = $('drawer').hidden;
$('drawer').hidden = !s && !app.edit;
$('d-empty').hidden = Boolean(s);
if (app.emptyHtml === undefined) {
app.emptyHtml = $('d-empty').innerHTML; // the default text, restored when the selection is not several strips
}
if (app.sel.size > 1) {
$('d-empty').textContent =
`${app.sel.size} strips selected. Drag one of them to move them all · Shift-click adds or removes · Delete removes them.`;
} else {
$('d-empty').innerHTML = app.emptyHtml;
}
$('d-body').hidden = !s;
if (wasHidden !== $('drawer').hidden) {
requestAnimationFrame(layoutView);
@@ -755,17 +776,20 @@
el.scrollTop = el.scrollHeight;
}
function select(s) {
app.selected = s;
/** The selection is a set of strips. The drawer shows the strip when exactly one is selected. */
function setSelection(list) {
app.sel = new Set(list);
app.selected = list.length === 1 ? list[0] : null;
app.dirty = true;
renderDrawer();
}
function select(s) {
setSelection(s ? [s] : []);
}
function setButtons() {
const ok = !app.takenOver;
for (const id of ['power-on-all', 'power-cycle-all', 'power-off-all']) {
$(id).disabled = !ok;
}
renderDrawer();
}
@@ -953,11 +977,48 @@
app.dpr = window.devicePixelRatio || 1;
c.width = Math.max(1, Math.round(rect.width * app.dpr));
c.height = Math.max(1, Math.round(rect.height * app.dpr));
app.view = RigLayout.fit(app.room, rect.width, rect.height);
app.size = { w: rect.width, h: rect.height };
app.view = RigLayout.fit(app.room, rect.width, rect.height, 28, {
z: app.zoom,
x: app.pan.x,
y: app.pan.y,
});
app.geo = RigLayout.stripGeometry(app.view, app.strips.map((s) => s.info));
app.dirty = true;
}
const ZOOM_MIN = 0.5;
const ZOOM_MAX = 8;
/** Zoom by factor f, keeping the room point under canvas (px, py) where it is (the centre without one). */
function zoomAt(f, px, py) {
if (!app.view || !app.room) {
return;
}
const cx = px == null ? app.size.w / 2 : px;
const cy = py == null ? app.size.h / 2 : py;
const wx = app.view.mx(cx);
const wy = app.view.my(cy);
const z = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, app.zoom * f));
const s = app.view.base * z;
app.zoom = z;
// so the point under (cx, cy) stays there: sx(wx) = cx and sy(wy) = cy at the new scale
app.pan = {
x: cx - wx * s - app.size.w / 2,
y: cy + wy * s - app.size.h / 2 - (app.room.depth / 2) * s,
};
layoutView();
}
function zoomFit() {
if (!app.room) {
return;
}
app.zoom = 1;
app.pan = { x: 0, y: 0 };
layoutView();
}
function colourAt(frame, i) {
const j = i * 3;
const r = frame[j] | 0;
@@ -983,11 +1044,13 @@
ctx.strokeStyle = GRID;
ctx.lineWidth = 1;
ctx.beginPath();
for (let x = Math.ceil(-w / 2); x <= w / 2; x += 1) {
// the grid step in metres: 1 m, or a coarser one where zoomed out so lines stay at least 6 px apart
const step = [1, 2, 5, 10, 20, 50].find((s) => s * v.scale >= 6) || 100;
for (let x = Math.ceil(-w / 2 / step) * step; x <= w / 2; x += step) {
ctx.moveTo(Math.round(v.sx(x)) + 0.5, y0);
ctx.lineTo(Math.round(v.sx(x)) + 0.5, y1);
}
for (let y = 1; y < d; y += 1) {
for (let y = step; y < d; y += step) {
ctx.moveTo(x0, Math.round(v.sy(y)) + 0.5);
ctx.lineTo(x1, Math.round(v.sy(y)) + 0.5);
}
@@ -999,7 +1062,7 @@
ctx.fillStyle = LABEL;
ctx.textAlign = 'left';
ctx.textBaseline = 'bottom';
ctx.fillText(`${app.roomName} · ${w} × ${d} m · 1 m grid`, x0, y0 - 6);
ctx.fillText(`${app.roomName} · ${w} × ${d} m · ${step} m grid · ${Math.round(app.zoom * 100)}%`, x0, y0 - 6);
}
function drawStrip(s, sg) {
@@ -1019,7 +1082,7 @@
ctx.fillStyle = col || OFF_COLOUR;
ctx.fillRect(i * sg.pitch, -LED_H / 2, Math.max(1, sg.pitch - gap), LED_H);
}
if (app.selected === s) {
if (app.sel.has(s)) {
ctx.strokeStyle = SELECT;
ctx.lineWidth = 1.5;
ctx.strokeRect(-1.5, -LED_H / 2 - 1.5, sg.len + 3, LED_H + 3);
@@ -1032,7 +1095,7 @@
ctx.restore();
if (app.edit) {
// turn handle at the strip's far end
ctx.strokeStyle = app.selected === s ? SELECT : LABEL;
ctx.strokeStyle = app.sel.has(s) ? SELECT : LABEL;
ctx.lineWidth = 1.5;
ctx.beginPath();
ctx.arc(sg.bx, sg.by, HANDLE_R, 0, Math.PI * 2);
@@ -1060,10 +1123,34 @@
ctx.fillRect(0, 0, c.width / app.dpr, c.height / app.dpr);
drawRoom();
app.strips.forEach((s, i) => drawStrip(s, app.geo[i]));
if (app.marquee) {
const m = app.marquee;
ctx.save();
ctx.setLineDash([5, 4]);
ctx.strokeStyle = SELECT;
ctx.fillStyle = 'rgba(90,169,255,0.08)';
ctx.lineWidth = 1;
ctx.fillRect(m.x0, m.y0, m.x1 - m.x0, m.y1 - m.y0);
ctx.strokeRect(m.x0 + 0.5, m.y0 + 0.5, m.x1 - m.x0, m.y1 - m.y0);
ctx.restore();
}
}
requestAnimationFrame(frame);
}
/** Whether a strip's segment passes through the box (canvas px), sampled along its length. */
function stripTouchesBox(sg, x0, y0, x1, y1) {
for (let i = 0; i <= 20; i += 1) {
const t = i / 20;
const x = sg.ax + (sg.bx - sg.ax) * t;
const y = sg.ay + (sg.by - sg.ay) * t;
if (x >= x0 && x <= x1 && y >= y0 && y <= y1) {
return true;
}
}
return false;
}
/** What is under the pointer: {s, part: 'handle' | 'body'} or null. */
function hitTest(x, y) {
if (app.edit) {
@@ -1093,26 +1180,74 @@
function wireCanvas() {
const c = app.canvas;
// Middle-button drag would autoscroll the page: the canvas pans instead
c.addEventListener('mousedown', (ev) => {
if (ev.button === 1) {
ev.preventDefault();
}
});
// Right-drag pans the view, so the browser's menu stays off the canvas
c.addEventListener('contextmenu', (ev) => ev.preventDefault());
c.addEventListener(
'wheel',
(ev) => {
ev.preventDefault();
const [x, y] = pointerPos(ev);
const dy = ev.deltaMode === 1 ? ev.deltaY * 16 : ev.deltaY; // lines -> px
zoomAt(Math.exp(-dy * 0.0015), x, y);
},
{ passive: false },
);
c.addEventListener('pointerdown', (ev) => {
const [x, y] = pointerPos(ev);
if (ev.button === 1 || ev.button === 2 || app.spaceDown) {
// middle or right button, or Space held: pan the view, whatever is under the pointer
app.drag = { kind: 'pan', x, y, panX: app.pan.x, panY: app.pan.y };
c.setPointerCapture(ev.pointerId);
c.style.cursor = 'grabbing';
return;
}
const hit = hitTest(x, y);
if (!app.edit) {
if (hit) {
select(app.selected === hit.s ? null : hit.s);
} else if (app.zoom > 1) {
// zoomed in: a drag on empty floor moves the view
app.drag = { kind: 'pan', x, y, panX: app.pan.x, panY: app.pan.y };
c.setPointerCapture(ev.pointerId);
c.style.cursor = 'grabbing';
}
return;
}
if (!hit) {
app.drag = { kind: 'floor', x, y };
// a click adds a strip. A drag selects the strips in its box (Shift adds to the selection), or, zoomed in,
// pans the view (see pointermove)
app.drag = { kind: 'floor', x, y, panX: app.pan.x, panY: app.pan.y, shift: ev.shiftKey };
return;
}
const f = app.room.fixtures[hit.s.id];
select(hit.s);
const additive = ev.shiftKey || ev.ctrlKey || ev.metaKey;
if (hit.part === 'handle') {
select(hit.s);
app.drag = { kind: 'turn', s: hit.s, moved: false };
c.setPointerCapture(ev.pointerId);
return;
}
if (additive) {
if (app.sel.has(hit.s)) {
// Shift-click on a selected strip takes it out of the selection
setSelection([...app.sel].filter((s) => s !== hit.s));
return;
}
setSelection([...app.sel, hit.s]);
} else if (!app.sel.has(hit.s)) {
setSelection([hit.s]);
} // else: a strip of the selection, so the whole selection moves together
// every selected strip moves by the same offset, from where it was when the drag began
const items = [...app.sel].map((s) => ({ s, startX: app.room.fixtures[s.id].x, startY: app.room.fixtures[s.id].y }));
app.drag = {
kind: hit.part === 'handle' ? 'turn' : 'move',
kind: 'move',
s: hit.s,
startX: f.x,
startY: f.y,
items,
mx: app.view.mx(x),
my: app.view.my(y),
moved: false,
@@ -1123,15 +1258,34 @@
const [x, y] = pointerPos(ev);
const d = app.drag;
if (!d) {
if (app.edit) {
if (app.spaceDown) {
c.style.cursor = 'grab';
} else if (app.edit) {
const hit = hitTest(x, y);
c.style.cursor = hit ? (hit.part === 'handle' ? 'crosshair' : 'move') : 'copy';
c.style.cursor = hit ? (hit.part === 'handle' ? 'crosshair' : 'move') : app.zoom > 1 ? 'grab' : 'copy';
} else {
c.style.cursor = hitTest(x, y) ? 'pointer' : 'default';
c.style.cursor = hitTest(x, y) ? 'pointer' : app.zoom > 1 ? 'grab' : 'default';
}
return;
}
if (d.kind === 'pan') {
app.pan = { x: d.panX + x - d.x, y: d.panY + y - d.y };
layoutView();
return;
}
if (d.kind === 'floor' && app.zoom > 1 && !d.shift && Math.hypot(x - d.x, y - d.y) >= 4) {
// zoomed in, the drag is a pan, not a box: the view follows the pointer from where the drag began
app.drag = { kind: 'pan', x: d.x, y: d.y, panX: d.panX, panY: d.panY };
app.marquee = null;
c.setPointerCapture(ev.pointerId);
c.style.cursor = 'grabbing';
app.pan = { x: d.panX + x - d.x, y: d.panY + y - d.y };
layoutView();
return;
}
if (d.kind === 'floor') {
app.marquee = { x0: d.x, y0: d.y, x1: x, y1: y };
app.dirty = true;
return;
}
const f = app.room.fixtures[d.s.id];
@@ -1142,15 +1296,28 @@
const wx = app.view.mx(x);
const wy = app.view.my(y);
if (d.kind === 'move') {
// one snapped offset for the whole selection, so the strips keep their spacing
const snap = ev.altKey ? 0.001 : SNAP_M;
f.x = round3(Math.round((d.startX + wx - d.mx) / snap) * snap);
f.y = round3(Math.round((d.startY + wy - d.my) / snap) * snap);
const dx = Math.round((wx - d.mx) / snap) * snap;
const dy = Math.round((wy - d.my) / snap) * snap;
for (const it of d.items) {
const g = app.room.fixtures[it.s.id];
if (g) {
g.x = round3(it.startX + dx);
g.y = round3(it.startY + dy);
}
}
} else {
const deg = (Math.atan2(wx - f.x, wy - f.y) * 180) / Math.PI;
f.direction = wrapDeg(ev.shiftKey ? deg : Math.round(deg / SNAP_DEG) * SNAP_DEG);
}
d.moved = true;
d.s.info = Object.assign(d.s.info, RigLayout.roomStrips(app.room).find((i) => i.id === d.s.id));
const infos = new Map(RigLayout.roomStrips(app.room).map((i) => [i.id, i]));
for (const s of d.kind === 'move' ? d.items.map((it) => it.s) : [d.s]) {
if (infos.has(s.id)) {
s.info = Object.assign(s.info, infos.get(s.id));
}
}
app.geo = RigLayout.stripGeometry(app.view, app.strips.map((s) => s.info));
app.dirty = true;
renderFacts();
@@ -1163,15 +1330,30 @@
return;
}
if (d.kind === 'floor') {
const m = app.marquee;
app.marquee = null;
app.dirty = true;
const [x, y] = pointerPos(ev);
if (Math.hypot(x - d.x, y - d.y) < 4 && ev.type === 'pointerup') {
const wx = app.view.mx(x);
const wy = app.view.my(y);
if (Math.abs(wx) <= app.room.width / 2 && wy >= 0 && wy <= app.room.depth) {
addFixture(wx, wy);
} else {
select(null);
}
if (ev.type !== 'pointerup') {
return;
}
if (Math.hypot(x - d.x, y - d.y) >= 4) {
// a box: the strips it touches are selected (Shift: added to the selection)
const x0 = Math.min(d.x, x);
const x1 = Math.max(d.x, x);
const y0 = Math.min(d.y, y);
const y1 = Math.max(d.y, y);
const inside = app.strips.filter((s, i) => stripTouchesBox(app.geo[i], x0, y0, x1, y1));
const additive = ev.shiftKey || ev.ctrlKey || ev.metaKey;
setSelection(additive ? [...new Set([...app.sel, ...inside])] : inside);
return;
}
const wx = app.view.mx(x);
const wy = app.view.my(y);
if (Math.abs(wx) <= app.room.width / 2 && wy >= 0 && wy <= app.room.depth) {
addFixture(wx, wy);
} else {
select(null);
}
return;
}
@@ -1186,23 +1368,30 @@
// ----- boot --------------------------------------------------------------------
function wireUi() {
$('power-on-all').onclick = () => app.strips.forEach(powerOn);
$('power-off-all').onclick = () => app.strips.forEach(powerOff);
$('power-cycle-all').onclick = () => {
app.strips.forEach(powerOff);
app.strips.forEach(powerOn);
};
const auto = $('auto-power');
auto.checked = readAuto();
auto.onchange = () => writeAuto(auto.checked);
$('room-select').onchange = () => switchRoom($('room-select').value);
$('add-bridge').onclick = addBridge;
$('edit-toggle').onclick = () => setEdit(!app.edit);
$('zoom-in').onclick = () => zoomAt(1.25);
$('zoom-out').onclick = () => zoomAt(1 / 1.25);
$('zoom-fit').onclick = () => zoomFit();
$('add-strip').onclick = () => {
// the middle of what the canvas shows, kept inside the room
const w = app.room.width;
const d = app.room.depth;
addFixture(
Math.min(w / 2, Math.max(-w / 2, app.view.mx(app.size.w / 2))),
Math.min(d, Math.max(0, app.view.my(app.size.h / 2))),
);
};
$('centre-strips').onclick = () => centreStrips();
$('d-close').onclick = () => select(null);
$('d-on').onclick = () => app.selected && powerOn(app.selected);
$('d-off').onclick = () => app.selected && powerOff(app.selected);
$('d-reset').onclick = () => app.selected && reset(app.selected);
$('d-delete').onclick = () => deleteFixture(app.selected);
$('d-delete').onclick = () => deleteSelected();
$('d-clear').onclick = () => {
if (app.selected) {
app.selected.logs = [];
@@ -1220,13 +1409,26 @@
new ResizeObserver(() => layoutView()).observe($('rig'));
window.addEventListener('keydown', (ev) => {
const typing = /^(INPUT|SELECT|TEXTAREA)$/.test(document.activeElement && document.activeElement.tagName);
if (ev.key === 'Escape' && app.selected && !typing) {
if (ev.key === 'Escape' && app.sel.size && !typing) {
select(null);
} else if (app.edit && app.selected && !typing && (ev.key === 'Delete' || ev.key === 'Backspace')) {
} else if (app.edit && app.sel.size && !typing && (ev.key === 'Delete' || ev.key === 'Backspace')) {
ev.preventDefault();
deleteFixture(app.selected);
deleteSelected();
} else if (ev.key === ' ' && !typing) {
app.spaceDown = true; // hold Space and drag the canvas to pan
ev.preventDefault();
} else if (ev.key === '0' && !typing && !ev.ctrlKey && !ev.metaKey && !ev.altKey) {
zoomFit();
}
});
window.addEventListener('keyup', (ev) => {
if (ev.key === ' ') {
app.spaceDown = false;
}
});
window.addEventListener('blur', () => {
app.spaceDown = false;
});
}
async function main() {