// write: tap a blank paper (or press enter to conjure your own) to bring it up and type on real physical paper. import { onFrame, onPhase } from './world.js'; import { phys } from './phys.js'; import { blanks, commit } from './things.js'; import { you } from './you.js'; import * as dollsMod from './dolls.js'; import { inkOn } from './ink.js'; const inp = document.createElement('input'); inp.maxLength = 90; inp.autocapitalize = 'off'; inp.autocomplete = 'off'; inp.spellcheck = false; inp.setAttribute('aria-label', 'write on the paper, enter to show or leave it'); inp.style.cssText = 'position:fixed;left:0;top:0;width:1px;height:1px;opacity:0;border:0;padding:0;font-size:16px;pointer-events:none'; let cur = null, activeChat = null, shown = '', blinkOn = false, noteStamp = ''; const PHASE_CSS = [ ['#fffdf0', '#0b0b12', '#2a14d6'], ['#f4f3dc', '#000000', '#e0081a'], ['#05061f', '#f4f3dc', '#c6f03a'], ]; function openBlank() { if (activeChat && phys.reading === activeChat.slip) return activeChat; for (const b of blanks) if (phys.reading === b.slip) return b; return null; } function take(b) { if (cur && cur !== b) { cur.text = inp.value; cur.draw(cur.text, false, cur.kind === 'say'); } cur = b; inp.maxLength = b?.kind === 'say' ? 80 : 90; if (b) inp.value = b.text; } function conjureChatPaper() { if (openBlank()) return; if (activeChat) { you.cancelPaper(activeChat); activeChat = null; } const rec = you.conjurePaper(); activeChat = rec; shown = '__init__'; take(rec); inp.focus(); } function cancelChatPaper() { if (!activeChat) return; const rec = activeChat; activeChat = null; if (cur === rec) { cur = null; inp.value = ''; shown = ''; } inp.blur(); you.cancelPaper(rec); } function submitCurrent() { if (!cur || cur.busy) return; if (cur.kind === 'say') { const rec = cur; const t = inp.value.trim(); activeChat = null; cur = null; inp.value = ''; shown = ''; inp.blur(); you.commitPaper(rec, t); return; } const t = inp.value.trim(); if (!t) return; const b = cur; b.text = t; commit(b, t).then((ok) => { if (ok) { if (cur === b) { inp.value = ''; cur = null; shown = ''; } for (const n of blanks) arm(n); } else { take(b); inp.focus(); if (phys.reading !== b.slip) phys.read(b.slip); } }); } const redraw = (rec) => { you.nudgePen?.(); rec.draw(rec.text, blinkOn && document.activeElement === inp, true); }; export function init() { document.body.appendChild(inp); inkOn(() => (activeChat && !activeChat.busy && phys.reading === activeChat.slip ? activeChat : null), redraw); for (const b of blanks) arm(b); const helpEl = document.getElementById('hud-help'); const toggleBtn = document.getElementById('hh-toggle'); const paperBtn = document.getElementById('hh-paper'); if (dollsMod.setCordMode) dollsMod.setCordMode('bundle'); const titleEl = helpEl?.querySelector('.hh-title'); const setFolded = (f) => { if (!helpEl || !toggleBtn) return; helpEl.classList.toggle('folded', f); toggleBtn.textContent = f ? '[?]' : '[-]'; if (titleEl) titleEl.textContent = f ? '[ help ]' : '[ controls ]'; try { localStorage.setItem('duet.helpFold', f ? '1' : '0'); } catch {} }; try { if (localStorage.getItem('duet.helpFold') === '1') setFolded(true); } catch {} toggleBtn?.addEventListener('pointerdown', (e) => e.stopPropagation()); toggleBtn?.addEventListener('click', (e) => { e.stopPropagation(); setFolded(!helpEl.classList.contains('folded')); }); paperBtn?.addEventListener('pointerdown', (e) => e.stopPropagation()); paperBtn?.addEventListener('click', (e) => { e.stopPropagation(); if (activeChat && phys.reading === activeChat.slip) submitCurrent(); else conjureChatPaper(); }); onPhase((pc) => { const [bg, fg, acc] = PHASE_CSS[pc] || PHASE_CSS[0]; const r = document.documentElement.style; r.setProperty('--w-bg', bg); r.setProperty('--w-fg', fg); r.setProperty('--w-acc', acc); }); addEventListener('keydown', (e) => { const bNow = openBlank(); if (e.key === 'Escape' && (phys.reading || document.activeElement === inp || bNow)) { e.preventDefault(); e.stopImmediatePropagation(); if (activeChat) { cancelChatPaper(); return; } if (cur && !cur.busy) { cur.note = null; } inp.blur(); phys.unread(); return; } if (bNow?.ink?.length && ((e.key === 'Backspace' && !inp.value) || (e.key === 'z' && (e.ctrlKey || e.metaKey)))) { e.preventDefault(); e.stopImmediatePropagation(); bNow.ink.pop(); redraw(bNow); return; } if (e.target === inp && bNow) { if (e.key === 'Enter') { e.preventDefault(); e.stopImmediatePropagation(); submitCurrent(); } return; } if (e.ctrlKey || e.metaKey || e.altKey) return; if (bNow) { if (e.key === 'Enter') { e.preventDefault(); e.stopImmediatePropagation(); submitCurrent(); return; } if (e.key.length === 1 && !bNow.busy) { take(bNow); inp.focus(); } return; } if (e.key === '?' && !e.repeat) { if (helpEl) setFolded(!helpEl.classList.contains('folded')); return; } if ((e.key === 'Enter' || e.key === '/') && !e.repeat) { e.preventDefault(); e.stopImmediatePropagation(); conjureChatPaper(); } }, true); inp.addEventListener('input', () => { const bNow = openBlank(); if (!cur && bNow) take(bNow); if (cur && !cur.busy) { you.nudgePen?.(); cur.text = inp.value; if (cur.note && cur.note.color === '#f00' && !cur.note.retryUntil) cur.note = null; cur.draw(cur.text, true, cur.kind === 'say'); shown = cur.text; } }); inp.addEventListener('blur', () => { if (cur && !cur.busy) { cur.text = inp.value; cur.draw(cur.text, false, cur.kind === 'say'); shown = cur.text; } }); onFrame((dt, t) => { if (activeChat && phys.reading !== activeChat.slip) { const rec = activeChat; const txt = inp.value.trim(); activeChat = null; if (cur === rec) { cur = null; inp.value = ''; shown = ''; } inp.blur(); you.commitPaper(rec, txt); return; } const activeB = openBlank(); you.setWriting?.(!!(activeB && !activeB.busy)); if (!activeB && document.activeElement === inp) inp.blur(); const focus = document.activeElement === inp && !!activeB; if (!cur || !focus) return; const bl = !cur.busy && Math.floor(t * 2.5) % 2 === 0, ns = cur.note ? cur.note.text : ''; if (cur.text !== shown || bl !== blinkOn || ns !== noteStamp) { shown = cur.text; blinkOn = bl; noteStamp = ns; cur.draw(cur.text, bl, cur.kind === 'say'); } }); } function arm(b) { if (b.armed) return; b.armed = true; b.slip.h.onTap = () => { if (b.busy) return; take(b); phys.read(b.slip); inp.focus(); }; }