| 1 | import { itemAssetPathForStack } from "/shared/definitions/items.js"; |
| 2 | import { applyItemIcon } from "../render/potionIcon.js"; |
| 3 | |
| 4 | const BUBBLE_MS = 8000; |
| 5 | const ITEM_BUBBLE_MS = 4000; |
| 6 | |
| 7 | export function initChat ({ socket, MessageType, getSettings }) |
| 8 | { |
| 9 | const bar = document.getElementById("chat-bar"); |
| 10 | const input = document.getElementById("chat-input"); |
| 11 | const layer = document.getElementById("chat-bubbles"); |
| 12 | const bubbles = new Map(); |
| 13 | const itemBubbles = new Map(); |
| 14 | let open = false; |
| 15 | |
| 16 | function maxChars () |
| 17 | { |
| 18 | return Math.max(1, Math.round(getSettings()?.chatMaxCharacters || 100)); |
| 19 | } |
| 20 | |
| 21 | function close () |
| 22 | { |
| 23 | open = false; |
| 24 | bar.hidden = true; |
| 25 | input.value = ""; |
| 26 | input.blur(); |
| 27 | } |
| 28 | |
| 29 | function openBar () |
| 30 | { |
| 31 | open = true; |
| 32 | input.maxLength = maxChars(); |
| 33 | bar.hidden = false; |
| 34 | input.focus(); |
| 35 | } |
| 36 | |
| 37 | function submit () |
| 38 | { |
| 39 | const text = input.value.trim().slice(0, maxChars()); |
| 40 | close(); |
| 41 | if (!text) |
| 42 | { return; } |
| 43 | socket.send({ type: MessageType.chatSay, text }); |
| 44 | } |
| 45 | |
| 46 | function playingHudReady () |
| 47 | { |
| 48 | return !!( |
| 49 | document.getElementById("overlay")?.hidden |
| 50 | && !document.getElementById("hud")?.hidden |
| 51 | && document.getElementById("inspect-modal")?.hidden |
| 52 | ); |
| 53 | } |
| 54 | |
| 55 | input.addEventListener("keydown", (event) => |
| 56 | { |
| 57 | event.stopPropagation(); |
| 58 | if (event.key === "Enter") |
| 59 | { |
| 60 | event.preventDefault(); |
| 61 | submit(); |
| 62 | } |
| 63 | if (event.key === "Escape") |
| 64 | { |
| 65 | event.preventDefault(); |
| 66 | close(); |
| 67 | } |
| 68 | }); |
| 69 | input.addEventListener("keyup", (event) => event.stopPropagation()); |
| 70 | |
| 71 | window.addEventListener("keydown", (event) => |
| 72 | { |
| 73 | if (event.key !== "Enter" || event.repeat) |
| 74 | { return; } |
| 75 | if (document.activeElement === input) |
| 76 | { return; } |
| 77 | const focus = document.activeElement; |
| 78 | const tag = focus?.tagName; |
| 79 | if (tag === "INPUT" || tag === "TEXTAREA" || tag === "SELECT" || focus?.isContentEditable) |
| 80 | { return; } |
| 81 | if (!playingHudReady()) |
| 82 | { return; } |
| 83 | event.preventDefault(); |
| 84 | event.stopPropagation(); |
| 85 | if (open) |
| 86 | { submit(); } |
| 87 | else |
| 88 | { openBar(); } |
| 89 | }, true); |
| 90 | |
| 91 | function positionBubble (bubble, view, rect) |
| 92 | { |
| 93 | const entity = view.entities.get(bubble.entityId); |
| 94 | const anchor = entity ? view.nametagAnchor(entity) : null; |
| 95 | if (!anchor) |
| 96 | { |
| 97 | bubble.element.hidden = true; |
| 98 | return; |
| 99 | } |
| 100 | bubble.element.hidden = false; |
| 101 | bubble.element.style.left = `${rect.left + anchor.x}px`; |
| 102 | bubble.element.style.top = `${rect.top + anchor.y - 36}px`; |
| 103 | } |
| 104 | |
| 105 | return { |
| 106 | isOpen: () => open, |
| 107 | isBlocking () |
| 108 | { |
| 109 | if (!open) |
| 110 | { return false; } |
| 111 | const box = bar.getBoundingClientRect(); |
| 112 | const x = GameInput.mousePosition.x; |
| 113 | const y = GameInput.mousePosition.y; |
| 114 | return x >= box.left && x < box.right && y >= box.top && y < box.bottom; |
| 115 | }, |
| 116 | close, |
| 117 | reset () |
| 118 | { |
| 119 | close(); |
| 120 | layer.replaceChildren(); |
| 121 | bubbles.clear(); |
| 122 | itemBubbles.clear(); |
| 123 | }, |
| 124 | showBubble (message) |
| 125 | { |
| 126 | if (!message?.text) |
| 127 | { return; } |
| 128 | let bubble = bubbles.get(message.entityId); |
| 129 | if (!bubble) |
| 130 | { |
| 131 | bubble = { element: document.createElement("div"), until: 0, entityId: message.entityId }; |
| 132 | bubble.element.className = "chat-bubble"; |
| 133 | layer.append(bubble.element); |
| 134 | bubbles.set(message.entityId, bubble); |
| 135 | } |
| 136 | bubble.element.textContent = message.text; |
| 137 | const readability = Math.min(1, Math.max(0, Number(message.readability ?? 1))); |
| 138 | bubble.element.style.opacity = String(readability); |
| 139 | bubble.until = Date.now() + BUBBLE_MS; |
| 140 | }, |
| 141 | showItemBubble (message) |
| 142 | { |
| 143 | if (message?.itemId == null || message?.entityId == null) |
| 144 | { return; } |
| 145 | let bubble = itemBubbles.get(message.entityId); |
| 146 | if (!bubble) |
| 147 | { |
| 148 | bubble = { |
| 149 | element: document.createElement("div"), |
| 150 | until: 0, |
| 151 | entityId: message.entityId |
| 152 | }; |
| 153 | bubble.element.className = "item-bubble"; |
| 154 | const img = document.createElement("img"); |
| 155 | img.alt = ""; |
| 156 | bubble.element.append(img); |
| 157 | layer.append(bubble.element); |
| 158 | itemBubbles.set(message.entityId, bubble); |
| 159 | } |
| 160 | const stack = { |
| 161 | itemId: message.itemId, |
| 162 | quantity: Math.max(1, message.quantity | 0 || 1) |
| 163 | }; |
| 164 | const img = bubble.element.querySelector("img"); |
| 165 | applyItemIcon(img, stack, itemAssetPathForStack(stack)); |
| 166 | const readability = Math.min(1, Math.max(0, Number(message.readability ?? 1))); |
| 167 | bubble.element.style.opacity = String(readability); |
| 168 | bubble.until = Date.now() + ITEM_BUBBLE_MS; |
| 169 | }, |
| 170 | syncSettings () |
| 171 | { |
| 172 | input.maxLength = maxChars(); |
| 173 | if (input.value.length > input.maxLength) |
| 174 | { input.value = input.value.slice(0, input.maxLength); } |
| 175 | }, |
| 176 | update (view, scene) |
| 177 | { |
| 178 | const now = Date.now(); |
| 179 | const canvas = scene?.canvas; |
| 180 | if (!canvas) |
| 181 | { return; } |
| 182 | const rect = canvas.getBoundingClientRect(); |
| 183 | for (const map of [bubbles, itemBubbles]) |
| 184 | { |
| 185 | for (const [entityId, bubble] of [...map.entries()]) |
| 186 | { |
| 187 | if (now >= bubble.until) |
| 188 | { |
| 189 | bubble.element.remove(); |
| 190 | map.delete(entityId); |
| 191 | continue; |
| 192 | } |
| 193 | positionBubble(bubble, view, rect); |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | }; |
| 198 | } |