| 1 | const BUBBLE_MS = 8000; |
| 2 | |
| 3 | export function initChat ({ socket, MessageType, getSettings }) |
| 4 | { |
| 5 | const bar = document.getElementById("chat-bar"); |
| 6 | const input = document.getElementById("chat-input"); |
| 7 | const layer = document.getElementById("chat-bubbles"); |
| 8 | const bubbles = new Map(); |
| 9 | let open = false; |
| 10 | |
| 11 | function maxChars () |
| 12 | { |
| 13 | return Math.max(1, Math.round(getSettings()?.chatMaxCharacters || 100)); |
| 14 | } |
| 15 | |
| 16 | function close () |
| 17 | { |
| 18 | open = false; |
| 19 | bar.hidden = true; |
| 20 | input.value = ""; |
| 21 | input.blur(); |
| 22 | } |
| 23 | |
| 24 | function openBar () |
| 25 | { |
| 26 | open = true; |
| 27 | input.maxLength = maxChars(); |
| 28 | bar.hidden = false; |
| 29 | input.focus(); |
| 30 | } |
| 31 | |
| 32 | function submit () |
| 33 | { |
| 34 | const text = input.value.trim().slice(0, maxChars()); |
| 35 | close(); |
| 36 | if (!text) |
| 37 | { return; } |
| 38 | socket.send({ type: MessageType.chatSay, text }); |
| 39 | } |
| 40 | |
| 41 | function playingHudReady () |
| 42 | { |
| 43 | return !!( |
| 44 | document.getElementById("overlay")?.hidden |
| 45 | && !document.getElementById("hud")?.hidden |
| 46 | && document.getElementById("inspect-modal")?.hidden |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | input.addEventListener("keydown", (event) => |
| 51 | { |
| 52 | event.stopPropagation(); |
| 53 | if (event.key === "Enter") |
| 54 | { |
| 55 | event.preventDefault(); |
| 56 | submit(); |
| 57 | } |
| 58 | if (event.key === "Escape") |
| 59 | { |
| 60 | event.preventDefault(); |
| 61 | close(); |
| 62 | } |
| 63 | }); |
| 64 | input.addEventListener("keyup", (event) => event.stopPropagation()); |
| 65 | |
| 66 | window.addEventListener("keydown", (event) => |
| 67 | { |
| 68 | if (event.key !== "Enter" || event.repeat) |
| 69 | { return; } |
| 70 | if (document.activeElement === input) |
| 71 | { return; } |
| 72 | if (!playingHudReady()) |
| 73 | { return; } |
| 74 | event.preventDefault(); |
| 75 | event.stopPropagation(); |
| 76 | if (open) |
| 77 | { submit(); } |
| 78 | else |
| 79 | { openBar(); } |
| 80 | }, true); |
| 81 | |
| 82 | return { |
| 83 | isOpen: () => open, |
| 84 | isBlocking () |
| 85 | { |
| 86 | if (!open) |
| 87 | { return false; } |
| 88 | const box = bar.getBoundingClientRect(); |
| 89 | const x = GameInput.mousePosition.x; |
| 90 | const y = GameInput.mousePosition.y; |
| 91 | return x >= box.left && x < box.right && y >= box.top && y < box.bottom; |
| 92 | }, |
| 93 | close, |
| 94 | reset () |
| 95 | { |
| 96 | close(); |
| 97 | layer.replaceChildren(); |
| 98 | bubbles.clear(); |
| 99 | }, |
| 100 | showBubble (message) |
| 101 | { |
| 102 | if (!message?.text) |
| 103 | { return; } |
| 104 | let bubble = bubbles.get(message.entityId); |
| 105 | if (!bubble) |
| 106 | { |
| 107 | bubble = { element: document.createElement("div"), until: 0 }; |
| 108 | bubble.element.className = "chat-bubble"; |
| 109 | layer.append(bubble.element); |
| 110 | bubbles.set(message.entityId, bubble); |
| 111 | } |
| 112 | bubble.element.textContent = message.text; |
| 113 | const readability = Math.min(1, Math.max(0, Number(message.readability ?? 1))); |
| 114 | bubble.element.style.opacity = String(readability); |
| 115 | bubble.until = Date.now() + BUBBLE_MS; |
| 116 | }, |
| 117 | syncSettings () |
| 118 | { |
| 119 | input.maxLength = maxChars(); |
| 120 | if (input.value.length > input.maxLength) |
| 121 | { input.value = input.value.slice(0, input.maxLength); } |
| 122 | }, |
| 123 | update (view, scene) |
| 124 | { |
| 125 | const now = Date.now(); |
| 126 | const canvas = scene?.canvas; |
| 127 | if (!canvas) |
| 128 | { return; } |
| 129 | const rect = canvas.getBoundingClientRect(); |
| 130 | for (const [entityId, bubble] of [...bubbles.entries()]) |
| 131 | { |
| 132 | if (now >= bubble.until) |
| 133 | { |
| 134 | bubble.element.remove(); |
| 135 | bubbles.delete(entityId); |
| 136 | continue; |
| 137 | } |
| 138 | const entity = view.entities.get(entityId); |
| 139 | const anchor = entity ? view.nametagAnchor(entity) : null; |
| 140 | if (!anchor) |
| 141 | { |
| 142 | bubble.element.hidden = true; |
| 143 | continue; |
| 144 | } |
| 145 | bubble.element.hidden = false; |
| 146 | bubble.element.style.left = `${rect.left + anchor.x}px`; |
| 147 | bubble.element.style.top = `${rect.top + anchor.y - 36}px`; |
| 148 | } |
| 149 | } |
| 150 | }; |
| 151 | } |