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:
1 parent
8c783ab926
commit
cb51a726bc
79 files changed
+5183
-934
No files matched your search
+240
-26
@@ -11,7 +11,7 @@ const MAX_STEPS = 25; // per animation frame, after the tab was in the backgroun
|
||||
const LED_CAP = 512;
|
||||
|
||||
const $ = (id) => document.getElementById(id);
|
||||
const state = { leds: 119, speed: 1, paused: false, cards: [] };
|
||||
const state = { leds: 119, speed: 1, paused: false, cards: [], current: 0, wasm: null, defs: {} };
|
||||
|
||||
function banner(text) {
|
||||
const el = $('banner');
|
||||
@@ -39,6 +39,7 @@ async function instantiate(wasmModule) {
|
||||
});
|
||||
}
|
||||
|
||||
// The preview runs the card's current values, so edits show at once. Save writes them to the controller only.
|
||||
function presetJson(card) {
|
||||
return JSON.stringify(card.preset);
|
||||
}
|
||||
@@ -118,7 +119,7 @@ function colourList(card) {
|
||||
add.className = 'secondary';
|
||||
add.textContent = '+';
|
||||
add.title = 'Add a colour';
|
||||
add.disabled = card.preset.colors.length >= 8;
|
||||
add.disabled = card.preset.colors.length >= card.maxColors;
|
||||
add.onclick = () => {
|
||||
const last = card.preset.colors[card.preset.colors.length - 1];
|
||||
card.preset.colors.push(last ? [last[2], last[0], last[1]] : [255, 0, 0]);
|
||||
@@ -142,6 +143,22 @@ function colourList(card) {
|
||||
return l;
|
||||
}
|
||||
|
||||
function nameField(card) {
|
||||
const l = document.createElement('label');
|
||||
l.className = 'full';
|
||||
l.textContent = 'Name';
|
||||
const i = document.createElement('input');
|
||||
i.type = 'text';
|
||||
i.maxLength = 32;
|
||||
i.value = card.name;
|
||||
i.oninput = () => {
|
||||
card.name = i.value.trim() || card.name;
|
||||
card.el.querySelector('.card-name').textContent = card.name;
|
||||
};
|
||||
l.append(i);
|
||||
return l;
|
||||
}
|
||||
|
||||
function bgField(card) {
|
||||
const l = document.createElement('label');
|
||||
l.textContent = 'Background';
|
||||
@@ -156,11 +173,152 @@ function bgField(card) {
|
||||
return l;
|
||||
}
|
||||
|
||||
function buildCard(mod, index) {
|
||||
// The controller's pattern definitions (GET /patterns/definitions, as its preset editor reads them): the n-field
|
||||
// labels, the style options (n6 "Style: 0 a, 1 b"), delay limits and how many colours a pattern takes.
|
||||
function parseStyles(text) {
|
||||
if (typeof text !== 'string') return null;
|
||||
const out = {};
|
||||
for (const part of text.replace(/^\s*Style:\s*/i, '').split(',')) {
|
||||
const m = part.trim().match(/^(\d+)\s+(.*)$/);
|
||||
if (m) out[Number(m[1])] = m[2];
|
||||
}
|
||||
return Object.keys(out).length ? out : null;
|
||||
}
|
||||
|
||||
function defFor(pattern) {
|
||||
return (state.defs && state.defs[pattern]) || {};
|
||||
}
|
||||
|
||||
// Show only the n fields this pattern reads, with the controller's labels
|
||||
function applySpec(card) {
|
||||
const def = defFor(card.pattern);
|
||||
for (const k of ['n1', 'n2', 'n3', 'n4']) {
|
||||
const el = card.fields[k];
|
||||
el.hidden = !def[k];
|
||||
if (def[k]) el.firstChild.textContent = def[k];
|
||||
}
|
||||
card.fields.style.hidden = !parseStyles(def.n6);
|
||||
}
|
||||
|
||||
function numFields(card) {
|
||||
const fields = {};
|
||||
for (const k of ['n1', 'n2', 'n3', 'n4']) fields[k] = numField(card, k, k);
|
||||
fields.style = styleField(card);
|
||||
card.fields = fields;
|
||||
return Object.values(fields);
|
||||
}
|
||||
|
||||
function styleField(card) {
|
||||
const l = document.createElement('label');
|
||||
l.textContent = 'Mode';
|
||||
const sel = document.createElement('select');
|
||||
const styles = parseStyles(defFor(card.pattern).n6) || {};
|
||||
const values = Object.keys(styles).map(Number);
|
||||
if (!values.includes(card.preset.mode)) values.push(card.preset.mode);
|
||||
for (const v of values) {
|
||||
sel.append(new Option(styles[v] ? `${v} ${styles[v]}` : `${v} (unknown)`, String(v)));
|
||||
}
|
||||
sel.value = String(card.preset.mode);
|
||||
sel.onchange = () => {
|
||||
card.preset.mode = Number(sel.value);
|
||||
applySpec(card);
|
||||
};
|
||||
l.append(sel);
|
||||
return l;
|
||||
}
|
||||
|
||||
// A controller preset (GET /presets: "#RRGGBB" colours, n1..n6) as the gallery's fields
|
||||
function fromSaved(s) {
|
||||
const out = {};
|
||||
out.colors = (s.colors || []).filter((c) => typeof c === 'string').map(rgb);
|
||||
out.background = typeof s.background === 'string' ? rgb(s.background) : [0, 0, 0];
|
||||
for (const k of ['delay', 'brightness', 'auto', 'reverse', 'fade', 'n1', 'n2', 'n3', 'n4', 'n5']) {
|
||||
if (s[k] !== undefined && s[k] !== null) out[k] = s[k];
|
||||
}
|
||||
out.mode = s.n6 ?? s.mode ?? 0;
|
||||
return out;
|
||||
}
|
||||
|
||||
// The controller's preset fields for a card (colours and background as "#RRGGBB")
|
||||
function payloadFor(card) {
|
||||
const p = card.preset;
|
||||
return {
|
||||
name: card.name,
|
||||
pattern: card.pattern,
|
||||
colors: p.colors.map((c) => hex(c).toUpperCase()),
|
||||
background: hex(p.background || [0, 0, 0]).toUpperCase(),
|
||||
brightness: p.brightness,
|
||||
delay: p.delay,
|
||||
auto: p.auto,
|
||||
reverse: p.reverse,
|
||||
n1: p.n1,
|
||||
n2: p.n2,
|
||||
n3: p.n3,
|
||||
n4: p.n4,
|
||||
n5: p.n5,
|
||||
n6: p.mode,
|
||||
};
|
||||
}
|
||||
|
||||
const JSON_HEADERS = { 'Content-Type': 'application/json', Accept: 'application/json' };
|
||||
|
||||
// Rebuild the dropdown from the cards (keeps the shown one selected)
|
||||
function refreshPick() {
|
||||
const sel = $('g-pick');
|
||||
sel.replaceChildren(...state.cards.map((c, i) => new Option(`${i + 1}. ${c.name} (${c.pattern})`, String(i))));
|
||||
sel.value = String(state.current);
|
||||
}
|
||||
|
||||
async function save(card) {
|
||||
const status = card.el.querySelector('.save-status');
|
||||
status.textContent = 'Saving…';
|
||||
try {
|
||||
const r = await fetch(`/presets/${card.id}`, {
|
||||
method: 'PUT',
|
||||
headers: JSON_HEADERS,
|
||||
body: JSON.stringify(payloadFor(card)),
|
||||
});
|
||||
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
||||
card.el.querySelector('.card-name').textContent = card.name;
|
||||
card.applied = JSON.parse(JSON.stringify(card.preset));
|
||||
apply(card, false);
|
||||
refreshPick();
|
||||
status.textContent = 'Saved';
|
||||
} catch (err) {
|
||||
status.textContent = `Save failed: ${err.message}`;
|
||||
}
|
||||
}
|
||||
|
||||
// A new preset on the controller, copied from the shown card
|
||||
async function addPreset() {
|
||||
const src = state.cards[state.current];
|
||||
if (!src) return;
|
||||
try {
|
||||
const r = await fetch('/presets', {
|
||||
method: 'POST',
|
||||
headers: JSON_HEADERS,
|
||||
body: JSON.stringify({ ...payloadFor(src), name: `${src.name} copy` }),
|
||||
});
|
||||
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
||||
const out = await r.json();
|
||||
const id = Object.keys(out)[0];
|
||||
const mod = await instantiate(state.wasm);
|
||||
const card = buildCard(mod, src.index, { ...out[id], id });
|
||||
state.cards.push(card);
|
||||
$('gallery').append(card.el);
|
||||
$('g-count').textContent = `${state.cards.length} presets`;
|
||||
refreshPick();
|
||||
show(state.cards.length - 1);
|
||||
} catch (err) {
|
||||
banner(`Could not add the preset: ${err.message}`);
|
||||
}
|
||||
}
|
||||
|
||||
function buildCard(mod, index, saved) {
|
||||
mod._fx_init(state.leds);
|
||||
const name = mod.UTF8ToString(mod._fx_name(index));
|
||||
const name = saved.name || mod.UTF8ToString(mod._fx_name(index));
|
||||
const ptr = mod._fx_defaults_json(index);
|
||||
const preset = JSON.parse(mod.UTF8ToString(ptr));
|
||||
const preset = { ...JSON.parse(mod.UTF8ToString(ptr)), ...fromSaved(saved) };
|
||||
mod._free(ptr);
|
||||
preset.n5 = preset.n5 ?? 0;
|
||||
|
||||
@@ -169,14 +327,19 @@ function buildCard(mod, index) {
|
||||
el.innerHTML =
|
||||
'<div class="card-head"><span class="card-name"></span><span class="btns">' +
|
||||
'<button type="button" class="secondary" data-a="restart" title="Restart the pattern">↺</button>' +
|
||||
'<button type="button" class="secondary" data-a="edit" aria-expanded="false">Edit</button></span></div>' +
|
||||
'<button type="button" data-a="save" title="Save this preset to the controller">Save</button></span></div>' +
|
||||
'<span class="save-status" aria-live="polite"></span>' +
|
||||
'<canvas></canvas>';
|
||||
el.querySelector('.card-name').textContent = name;
|
||||
const card = {
|
||||
mod,
|
||||
index,
|
||||
id: saved.id,
|
||||
pattern: saved.pattern,
|
||||
maxColors: defFor(saved.pattern).max_colors ?? 8,
|
||||
name,
|
||||
preset,
|
||||
applied: JSON.parse(JSON.stringify(preset)),
|
||||
el,
|
||||
canvas: el.querySelector('canvas'),
|
||||
clock: 0,
|
||||
@@ -185,30 +348,27 @@ function buildCard(mod, index) {
|
||||
last: null,
|
||||
};
|
||||
const form = document.createElement('form');
|
||||
form.hidden = true;
|
||||
form.onsubmit = (ev) => ev.preventDefault();
|
||||
const def = defFor(saved.pattern);
|
||||
const colours = colourList(card);
|
||||
colours.hidden = card.maxColors === 0; // as the controller: no colour list for a pattern that takes none
|
||||
const reverse = checkField(card, 'reverse', 'reverse', false);
|
||||
reverse.hidden = !def.supports_reverse; // as the controller: only for patterns that support it
|
||||
form.append(
|
||||
colourList(card),
|
||||
nameField(card),
|
||||
colours,
|
||||
bgField(card),
|
||||
numField(card, 'delay', 'Delay d (ms)', { min: 10, max: 10000 }),
|
||||
numField(card, 'delay', 'Delay d (ms)', { min: def.min_delay ?? 10, max: def.max_delay ?? 10000 }),
|
||||
numField(card, 'brightness', 'Bright b', { min: 0, max: 255 }),
|
||||
numField(card, 'mode', 'Style n6', { min: 0 }),
|
||||
numField(card, 'n1', 'n1'),
|
||||
numField(card, 'n2', 'n2'),
|
||||
numField(card, 'n3', 'n3'),
|
||||
numField(card, 'n4', 'n4'),
|
||||
numField(card, 'n5', 'n5'),
|
||||
checkField(card, 'reverse', 'reverse', false),
|
||||
numField(card, 'fade', 'Fade (ms)', { min: 0, max: 10000, step: 50 }),
|
||||
...numFields(card),
|
||||
reverse,
|
||||
checkField(card, 'auto', 'auto a', true),
|
||||
checkField(card, 'beat_time', 'beat time', false),
|
||||
);
|
||||
el.append(form);
|
||||
el.querySelector('[data-a=edit]').onclick = (ev) => {
|
||||
form.hidden = !form.hidden;
|
||||
ev.currentTarget.setAttribute('aria-expanded', String(!form.hidden));
|
||||
ev.currentTarget.textContent = form.hidden ? 'Edit' : 'Close';
|
||||
};
|
||||
el.querySelector('[data-a=save]').onclick = () => save(card);
|
||||
el.querySelector('[data-a=restart]').onclick = () => apply(card, true);
|
||||
applySpec(card);
|
||||
apply(card, true);
|
||||
return card;
|
||||
}
|
||||
@@ -240,6 +400,19 @@ function draw(card) {
|
||||
}
|
||||
}
|
||||
|
||||
// Show one pattern card at a time (index wraps at the ends)
|
||||
function show(index) {
|
||||
const n = state.cards.length;
|
||||
if (!n) return;
|
||||
state.current = (index + n) % n;
|
||||
state.cards.forEach((c, i) => {
|
||||
c.el.hidden = i !== state.current;
|
||||
});
|
||||
$('g-pick').value = String(state.current);
|
||||
$('g-prev').disabled = n < 2;
|
||||
$('g-next').disabled = n < 2;
|
||||
}
|
||||
|
||||
let lastT = 0;
|
||||
function tick(t) {
|
||||
const dt = lastT ? Math.min(250, t - lastT) : 0;
|
||||
@@ -256,8 +429,13 @@ function tick(t) {
|
||||
if (card.next <= card.clock) {
|
||||
card.next = Math.ceil(card.clock / FRAME_MS) * FRAME_MS; // fell behind: skip ahead
|
||||
}
|
||||
draw(card);
|
||||
}
|
||||
const shown = state.cards[state.current];
|
||||
const status = shown.el.querySelector('.save-status');
|
||||
const dirty = JSON.stringify(shown.preset) !== JSON.stringify(shown.applied);
|
||||
if (dirty) status.textContent = 'Unsaved changes (press Save to keep them)';
|
||||
else if (status.textContent.startsWith('Unsaved')) status.textContent = '';
|
||||
draw(shown); // only the shown pattern is drawn
|
||||
}
|
||||
requestAnimationFrame(tick);
|
||||
}
|
||||
@@ -270,16 +448,43 @@ async function main() {
|
||||
banner(`Could not load the pattern engine (web/wasm/led_fx.wasm): ${err}. Build it with make sim-wasm, then rebuild the controller.`);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const r = await fetch('/patterns/definitions', { headers: { Accept: 'application/json' } });
|
||||
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
||||
state.defs = await r.json();
|
||||
} catch (err) {
|
||||
banner(`Could not load the pattern definitions from the controller: ${err}. Field labels are missing.`);
|
||||
}
|
||||
const first = await instantiate(wasmModule);
|
||||
const count = first._fx_count();
|
||||
$('g-count').textContent = `${count} patterns`;
|
||||
const patternNames = [];
|
||||
for (let i = 0; i < count; i += 1) patternNames.push(first.UTF8ToString(first._fx_name(i)));
|
||||
state.wasm = wasmModule;
|
||||
|
||||
let saved = [];
|
||||
try {
|
||||
const r = await fetch('/presets', { headers: { Accept: 'application/json' } });
|
||||
if (!r.ok) throw new Error(`HTTP ${r.status}`);
|
||||
saved = Object.values(await r.json());
|
||||
} catch (err) {
|
||||
banner(`Could not load the presets from the controller: ${err}`);
|
||||
}
|
||||
const list = saved
|
||||
.map((p) => ({ p, index: patternNames.indexOf(p.pattern) }))
|
||||
.filter((x) => x.index >= 0);
|
||||
const skipped = saved.length - list.length;
|
||||
if (skipped) banner(`${skipped} preset(s) use a pattern this build does not have and are not shown.`);
|
||||
$('g-count').textContent = `${list.length} presets`;
|
||||
|
||||
const grid = $('gallery');
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
for (let i = 0; i < list.length; i += 1) {
|
||||
const mod = i === 0 ? first : await instantiate(wasmModule);
|
||||
const card = buildCard(mod, i);
|
||||
const card = buildCard(mod, list[i].index, list[i].p);
|
||||
state.cards.push(card);
|
||||
grid.append(card.el);
|
||||
}
|
||||
refreshPick();
|
||||
$('g-pick').onchange = () => show(Number($('g-pick').value));
|
||||
$('g-leds').onchange = () => {
|
||||
const n = Math.max(1, Math.min(LED_CAP, Math.round(Number($('g-leds').value)) || 119));
|
||||
$('g-leds').value = n;
|
||||
@@ -297,6 +502,15 @@ async function main() {
|
||||
$('g-pause').textContent = state.paused ? 'Play' : 'Pause';
|
||||
};
|
||||
$('g-restart').onclick = () => state.cards.forEach((c) => apply(c, true));
|
||||
$('g-add').onclick = () => addPreset();
|
||||
$('g-prev').onclick = () => show(state.current - 1);
|
||||
$('g-next').onclick = () => show(state.current + 1);
|
||||
document.addEventListener('keydown', (ev) => {
|
||||
if (ev.target.closest('input, select, textarea')) return;
|
||||
if (ev.key === 'ArrowLeft') show(state.current - 1);
|
||||
if (ev.key === 'ArrowRight') show(state.current + 1);
|
||||
});
|
||||
show(0);
|
||||
requestAnimationFrame(tick);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user