const BUBBLE_MS = 8000; export function initChat ({ socket, MessageType, getSettings }) { const bar = document.getElementById("chat-bar"); const input = document.getElementById("chat-input"); const layer = document.getElementById("chat-bubbles"); const bubbles = new Map(); let open = false; function maxChars () { return Math.max(1, Math.round(getSettings()?.chatMaxCharacters || 100)); } function close () { open = false; bar.hidden = true; input.value = ""; input.blur(); } function openBar () { open = true; input.maxLength = maxChars(); bar.hidden = false; input.focus(); } function submit () { const text = input.value.trim().slice(0, maxChars()); close(); if (!text) { return; } socket.send({ type: MessageType.chatSay, text }); } function playingHudReady () { return !!( document.getElementById("overlay")?.hidden && !document.getElementById("hud")?.hidden && document.getElementById("inspect-modal")?.hidden ); } input.addEventListener("keydown", (event) => { event.stopPropagation(); if (event.key === "Enter") { event.preventDefault(); submit(); } if (event.key === "Escape") { event.preventDefault(); close(); } }); input.addEventListener("keyup", (event) => event.stopPropagation()); window.addEventListener("keydown", (event) => { if (event.key !== "Enter" || event.repeat) { return; } if (document.activeElement === input) { return; } if (!playingHudReady()) { return; } event.preventDefault(); event.stopPropagation(); if (open) { submit(); } else { openBar(); } }, true); return { isOpen: () => open, isBlocking () { if (!open) { return false; } const box = bar.getBoundingClientRect(); const x = GameInput.mousePosition.x; const y = GameInput.mousePosition.y; return x >= box.left && x < box.right && y >= box.top && y < box.bottom; }, close, reset () { close(); layer.replaceChildren(); bubbles.clear(); }, showBubble (message) { if (!message?.text) { return; } let bubble = bubbles.get(message.entityId); if (!bubble) { bubble = { element: document.createElement("div"), until: 0 }; bubble.element.className = "chat-bubble"; layer.append(bubble.element); bubbles.set(message.entityId, bubble); } bubble.element.textContent = message.text; const readability = Math.min(1, Math.max(0, Number(message.readability ?? 1))); bubble.element.style.opacity = String(readability); bubble.until = Date.now() + BUBBLE_MS; }, syncSettings () { input.maxLength = maxChars(); if (input.value.length > input.maxLength) { input.value = input.value.slice(0, input.maxLength); } }, update (view, scene) { const now = Date.now(); const canvas = scene?.canvas; if (!canvas) { return; } const rect = canvas.getBoundingClientRect(); for (const [entityId, bubble] of [...bubbles.entries()]) { if (now >= bubble.until) { bubble.element.remove(); bubbles.delete(entityId); continue; } const entity = view.entities.get(entityId); const anchor = entity ? view.nametagAnchor(entity) : null; if (!anchor) { bubble.element.hidden = true; continue; } bubble.element.hidden = false; bubble.element.style.left = `${rect.left + anchor.x}px`; bubble.element.style.top = `${rect.top + anchor.y - 36}px`; } } }; }