101 lines
6.2 KiB
JavaScript
101 lines
6.2 KiB
JavaScript
import {connectionMessage} from './connection-wizard.mjs';
|
|||
|
|
import {referenceKey,selection} from '../core/scene-music.mjs';
|
||
|
|
|
||
|
|
const clock=value=>`${Math.floor(value/60)}:${String(value%60).padStart(2,'0')}`;
|
||
|
|
const states={playing:'Playing',paused:'Paused',idle:'Stopped'};
|
||
|
|
|
||
|
|
export function mountPlaybackPanel({game,container,connection,getChoices,start,stop,setOverrides,onError}) {
|
||
|
|
const dom=container.ownerDocument;
|
||
|
|
const make=(tag,text,parent=container)=>{const node=dom.createElement(tag);if(text)node.textContent=text;parent.append(node);return node;};
|
||
|
|
make('h2','Playback');
|
||
|
|
const now=make('section');now.className='adaptive-now';
|
||
|
|
const state=make('p','Stopped',now);state.className='adaptive-state';
|
||
|
|
const track=make('h3','Nothing playing',now);
|
||
|
|
const timing=make('p',null,now);
|
||
|
|
const controls=make('div');controls.className='adaptive-actions';
|
||
|
|
const output=make('div');output.className='adaptive-fields';
|
||
|
|
const volumeLabel=make('label','Campaign volume',output),volume=make('input',null,volumeLabel);
|
||
|
|
volume.type='range';volume.min='0';volume.max='100';volume.value='100';
|
||
|
|
const seekLabel=make('label','Playback position',output),seek=make('input',null,seekLabel);
|
||
|
|
seek.type='range';seek.min='0';seek.max='0';seek.step='1';seek.value='0';
|
||
|
|
const summary=make('div');summary.className='adaptive-playback-summary';
|
||
|
|
const automationText=make('p',null,summary),listeners=make('p',null,summary);
|
||
|
|
const status=make('p');status.setAttribute('role','status');
|
||
|
|
const failure=make('p');failure.setAttribute('role','alert');failure.hidden=true;
|
||
|
|
let trackStatus,refreshing=false,busy=false,volumeEditing=false,seekEditing=false,oldChoices='';
|
||
|
|
|
||
|
|
const button=(parent,label,action)=>{
|
||
|
|
const b=make('button',label,parent);b.type='button';b.addEventListener('click',()=>run(action));return b;
|
||
|
|
};
|
||
|
|
const toggle=button(controls,'Pause',()=>connection.control(trackStatus.state==='playing'?'pause':'resume'));
|
||
|
|
toggle.className='adaptive-primary';
|
||
|
|
const stopTrack=button(controls,'Stop playback',()=>connection.control('stop'));
|
||
|
|
const unlock=button(controls,'Enable GM audio',()=>connection.resume());
|
||
|
|
const automation=button(summary,'Enable automation',async()=>{
|
||
|
|
const enable=!isRunning();
|
||
|
|
if(!enable)stop();
|
||
|
|
await game.settings.set('adaptive-music','enabled',enable);
|
||
|
|
if(enable)await start();
|
||
|
|
status.textContent=enable?'Scene automation enabled.':'Scene automation disabled.';
|
||
|
|
});
|
||
|
|
|
||
|
|
const manual=make('details');make('summary','Manual playlist override',manual);
|
||
|
|
const label=make('label','Override playlist',manual),select=make('select',null,label);
|
||
|
|
const manualActions=make('div',null,manual);manualActions.className='adaptive-actions';
|
||
|
|
button(manualActions,'Apply override',()=>setOverrides([{id:'gm-manual',playlist:selection(select.value,getChoices())}]));
|
||
|
|
button(manualActions,'Clear override',()=>setOverrides([]));
|
||
|
|
function isRunning(){return connection?.status.automationRunning??game.settings.get('adaptive-music','enabled');}
|
||
|
|
|
||
|
|
volume.addEventListener('input',()=>{volumeEditing=true;volume.setAttribute('aria-valuetext',`${volume.value}%`);});
|
||
|
|
volume.addEventListener('change',()=>run(()=>connection.setVolume(Number(volume.value))).finally(()=>{volumeEditing=false;}));
|
||
|
|
seek.addEventListener('input',()=>{seekEditing=true;seek.setAttribute('aria-valuetext',clock(Number(seek.value)));});
|
||
|
|
seek.addEventListener('change',()=>run(()=>connection.control('seek',Number(seek.value))).finally(()=>{seekEditing=false;}));
|
||
|
|
|
||
|
|
async function run(action){
|
||
|
|
if(busy||!game.user.isGM)return;
|
||
|
|
busy=true;failure.hidden=true;render();
|
||
|
|
try{await action();}
|
||
|
|
catch(error){failure.textContent=`Playback action failed. ${connectionMessage(error)}`;failure.hidden=false;onError(error);}
|
||
|
|
finally{busy=false;await refresh();}
|
||
|
|
}
|
||
|
|
function render(){
|
||
|
|
const d=connection?.status??{},connected=d.state==='connected',playing=trackStatus?.state==='playing',paused=trackStatus?.state==='paused';
|
||
|
|
const canControl=connected&&trackStatus?.source==='Music Assistant'&&d.lastError?.code!=='uncertain';
|
||
|
|
state.textContent=states[trackStatus?.state]??(trackStatus?'Playback status unavailable':'Stopped');
|
||
|
|
track.textContent=trackStatus?.title||'Nothing playing';
|
||
|
|
timing.textContent=trackStatus?.duration?`${clock(trackStatus.position)} / ${clock(trackStatus.duration)}`:'';
|
||
|
|
if(!volumeEditing){volume.value=String(d.volume??100);volume.setAttribute('aria-valuetext',`${volume.value}%`);}
|
||
|
|
if(!seekEditing){
|
||
|
|
seek.max=String(Math.max(0,(trackStatus?.duration??0)-1));seek.value=String(Math.min(Number(seek.max),trackStatus?.position??0));
|
||
|
|
seek.setAttribute('aria-valuetext',clock(Number(seek.value)));
|
||
|
|
}
|
||
|
|
toggle.textContent=paused?'Resume':'Pause';toggle.disabled=busy||!canControl||!(playing||paused);
|
||
|
|
stopTrack.disabled=busy||!canControl||!(playing||paused);
|
||
|
|
volume.disabled=busy||!connected;seek.disabled=busy||!canControl||!(playing||paused)||Number(seek.max)<=0;
|
||
|
|
unlock.hidden=!connected||d.audioContext==='running';unlock.disabled=busy;
|
||
|
|
const enabled=isRunning();
|
||
|
|
automationText.textContent=enabled?'Automation active':game.settings.get('adaptive-music','enabled')?'Automation paused':'Automation off';
|
||
|
|
automation.textContent=enabled?'Disable automation':'Enable automation';automation.disabled=busy;
|
||
|
|
const count=d.peers??0;listeners.textContent=`${count} connected ${count===1?'listener':'listeners'}`;
|
||
|
|
if(d.lastError?.code==='uncertain'){
|
||
|
|
failure.textContent=`Queue needs review. ${connectionMessage(d.lastError)}`;failure.hidden=false;
|
||
|
|
}
|
||
|
|
const choices=getChoices();const display=JSON.stringify(choices.map(choice=>[referenceKey(choice),choice.name]));
|
||
|
|
if(display!==oldChoices){
|
||
|
|
const previous=select.value;select.replaceChildren();
|
||
|
|
const empty=make('option','Silence',select);empty.value='';
|
||
|
|
for(const choice of choices){const option=make('option',choice.name,select);option.value=referenceKey(choice);}
|
||
|
|
if(choices.some(choice=>referenceKey(choice)===previous))select.value=previous;
|
||
|
|
oldChoices=display;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
async function refresh(){
|
||
|
|
render();if(refreshing||busy)return;
|
||
|
|
refreshing=true;
|
||
|
|
try{trackStatus=await connection?.playback?.();}
|
||
|
|
catch{trackStatus={state:'unavailable'};}
|
||
|
|
finally{refreshing=false;render();}
|
||
|
|
}
|
||
|
|
render();
|
||
|
|
return refresh;
|
||
|
|
}
|