Adaptive Music v0.8.3 multiplayer and floating music controls
Validate and generate manual release / package (push) Waiting to run
Validate and generate manual release / package (push) Waiting to run
This commit is contained in:
1 parent
acc8cbafc6
commit
fbc265b526
33 files changed
+1673
-131
No files matched your search
@@ -0,0 +1,157 @@
|
||||
import {musicModeLabel} from '../core/scene-music.mjs';
|
||||
|
||||
export function mountMusicBar({document, getPreferences, savePreferences, getListening, changeListening,
|
||||
enableAudio, gm, onError}) {
|
||||
const win = document.defaultView;
|
||||
const root = document.createElement('section');
|
||||
root.className = 'adaptive-music-bar';
|
||||
root.setAttribute('aria-label', 'Adaptive Music bar');
|
||||
const make = (parent, tag, text) => {
|
||||
const element = document.createElement(tag);
|
||||
if (text) element.textContent = text;
|
||||
parent.append(element);
|
||||
return element;
|
||||
};
|
||||
const setText = (element, text) => {
|
||||
if (element.textContent !== text) element.textContent = text;
|
||||
};
|
||||
const header = make(root, 'header');
|
||||
const grip = make(header, 'strong', 'Adaptive Music');
|
||||
grip.className = 'adaptive-bar-grip';
|
||||
const button = (parent, text, action) => {
|
||||
const control = make(parent, 'button', text);
|
||||
control.type = 'button';
|
||||
control.addEventListener('click', action);
|
||||
return control;
|
||||
};
|
||||
let preferences = getPreferences() ?? {}, busy = false, disposed = false;
|
||||
const collapse = button(header, 'Hide', () => {
|
||||
saveLayout({collapsed: !preferences.collapsed});
|
||||
render();
|
||||
fit();
|
||||
});
|
||||
collapse.setAttribute('aria-controls', 'adaptive-music-bar-controls');
|
||||
const content = make(root, 'div');
|
||||
content.id = 'adaptive-music-bar-controls';
|
||||
const status = make(content, 'p');
|
||||
status.setAttribute('role', 'status');
|
||||
const primary = make(content, 'div');
|
||||
primary.className = 'adaptive-bar-actions';
|
||||
const enable = button(primary, 'Enable audio', () => void run(enableAudio));
|
||||
const mute = button(primary, 'Mute', () => void run(() => changeListening({muted: !getListening().muted})));
|
||||
const volume = slider(content, 'My volume',
|
||||
value => write(() => changeListening({volume: value}, {save: false})),
|
||||
value => write(() => changeListening({volume: value})));
|
||||
let pause, skip, settings, automatic, campaignVolume;
|
||||
if (gm) {
|
||||
const transport = make(content, 'div');
|
||||
transport.className = 'adaptive-bar-actions';
|
||||
pause = button(transport, 'Pause all music', () => void run(() => gm.setPaused(!gm.status().campaignPaused)));
|
||||
skip = button(transport, 'Skip track', () => void run(gm.skip));
|
||||
settings = button(transport, 'Playback controls', () => void run(() => gm.status().connected || gm.status().automationRunning
|
||||
? gm.openPlayback() : gm.openSetup()));
|
||||
automatic = button(transport, 'Return to scene music', () => void run(gm.returnToScene));
|
||||
campaignVolume = slider(content, 'Campaign volume', undefined, value => void run(() => gm.setVolume(value)));
|
||||
}
|
||||
|
||||
function slider(parent, title, preview, commit) {
|
||||
const row = make(parent, 'label');
|
||||
make(row, 'span', title);
|
||||
const input = make(row, 'input');
|
||||
input.type = 'range'; input.min = '0'; input.max = '100'; input.value = '100';
|
||||
const readout = make(row, 'span', '100%');
|
||||
readout.setAttribute('aria-hidden', 'true');
|
||||
input.addEventListener('input', () => { showValue(input.value); preview?.(Number(input.value)); });
|
||||
input.addEventListener('change', () => commit(Number(input.value)));
|
||||
input.addEventListener('blur', render);
|
||||
function showValue(value) {
|
||||
input.setAttribute('aria-valuetext', `${value}%`);
|
||||
setText(readout, `${value}%`);
|
||||
}
|
||||
return (value, disabled = false) => {
|
||||
input.disabled = disabled;
|
||||
if (document.activeElement !== input || disabled) input.value = String(value);
|
||||
showValue(input.value);
|
||||
};
|
||||
}
|
||||
function write(action) {
|
||||
try { Promise.resolve(action()).then(render, onError); }
|
||||
catch (error) { onError(error); }
|
||||
}
|
||||
async function run(action) {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
try { await action(); }
|
||||
catch (error) { onError(error); }
|
||||
finally { busy = false; render(); }
|
||||
}
|
||||
function render() {
|
||||
if (disposed) return;
|
||||
const listening = getListening();
|
||||
content.hidden = preferences.collapsed === true;
|
||||
setText(collapse, content.hidden ? 'Show' : 'Hide');
|
||||
collapse.setAttribute('aria-expanded', String(!content.hidden));
|
||||
setText(mute, listening.muted ? 'Unmute' : 'Mute');
|
||||
mute.setAttribute('aria-pressed', String(listening.muted));
|
||||
volume(listening.volume);
|
||||
let nextStatus = listening.status;
|
||||
if (gm) {
|
||||
const state = gm.status(), mode = musicModeLabel(state.musicMode);
|
||||
nextStatus = state.campaignPaused ? 'Campaign music paused'
|
||||
: (mode ?? (state.connected ? 'Music Assistant connected' : 'Connect Music Assistant'));
|
||||
enable.hidden = !state.canControl || !state.connected || !state.needsActivation;
|
||||
setText(pause, state.campaignPaused ? 'Play all music' : 'Pause all music');
|
||||
pause.disabled = !state.canControl || busy || (!state.connected && !state.automationRunning && !state.campaignPaused);
|
||||
skip.disabled = !state.canControl || busy || !state.canSkip || state.campaignPaused;
|
||||
setText(settings, state.connected || state.automationRunning ? 'Playback controls' : 'Music setup');
|
||||
automatic.hidden = !state.overrideActive && !state.awaitingSceneChange;
|
||||
automatic.disabled = !state.canControl || busy;
|
||||
campaignVolume(state.volume, !state.canControl || !state.connected);
|
||||
}
|
||||
setText(status, nextStatus);
|
||||
}
|
||||
function saveLayout(patch) {
|
||||
preferences = {...preferences, ...patch};
|
||||
write(() => savePreferences(preferences));
|
||||
}
|
||||
function place(x, y) {
|
||||
const next = {
|
||||
x: Math.min(Math.max(8, x), Math.max(8, win.innerWidth - root.offsetWidth - 8)),
|
||||
y: Math.min(Math.max(8, y), Math.max(8, win.innerHeight - root.offsetHeight - 8)),
|
||||
};
|
||||
root.style.left = `${next.x}px`;
|
||||
root.style.top = `${next.y}px`;
|
||||
root.style.right = 'auto';
|
||||
return next;
|
||||
}
|
||||
function fit() {
|
||||
if (Number.isFinite(preferences.x) && Number.isFinite(preferences.y)) place(preferences.x, preferences.y);
|
||||
}
|
||||
let drag;
|
||||
grip.addEventListener('pointerdown', event => {
|
||||
if (event.button !== 0) return;
|
||||
const rect = root.getBoundingClientRect();
|
||||
drag = {x: event.clientX - rect.left, y: event.clientY - rect.top};
|
||||
grip.setPointerCapture(event.pointerId);
|
||||
});
|
||||
grip.addEventListener('pointermove', event => {
|
||||
if (drag) drag.position = place(event.clientX - drag.x, event.clientY - drag.y);
|
||||
});
|
||||
function endDrag() {
|
||||
if (drag?.position) saveLayout(drag.position);
|
||||
drag = undefined;
|
||||
}
|
||||
grip.addEventListener('pointerup', endDrag);
|
||||
grip.addEventListener('pointercancel', endDrag);
|
||||
win.addEventListener('resize', fit);
|
||||
document.body.append(root);
|
||||
render();
|
||||
fit();
|
||||
const timer = win.setInterval(render, 750);
|
||||
return () => {
|
||||
disposed = true;
|
||||
win.clearInterval(timer);
|
||||
win.removeEventListener('resize', fit);
|
||||
root.remove();
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user