BlueGit

timfa questward

Files

questward / client / src / play / chat.js

chat.js

chat.js 4.6 KiB Download Raw
1import { itemAssetPathForStack } from "/shared/definitions/items.js";
2import { applyItemIcon } from "../render/potionIcon.js";
3
4const BUBBLE_MS = 8000;
5const ITEM_BUBBLE_MS = 4000;
6
7export 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 if (!playingHudReady())
78 { return; }
79 event.preventDefault();
80 event.stopPropagation();
81 if (open)
82 { submit(); }
83 else
84 { openBar(); }
85 }, true);
86
87 function positionBubble (bubble, view, rect)
88 {
89 const entity = view.entities.get(bubble.entityId);
90 const anchor = entity ? view.nametagAnchor(entity) : null;
91 if (!anchor)
92 {
93 bubble.element.hidden = true;
94 return;
95 }
96 bubble.element.hidden = false;
97 bubble.element.style.left = `${rect.left + anchor.x}px`;
98 bubble.element.style.top = `${rect.top + anchor.y - 36}px`;
99 }
100
101 return {
102 isOpen: () => open,
103 isBlocking ()
104 {
105 if (!open)
106 { return false; }
107 const box = bar.getBoundingClientRect();
108 const x = GameInput.mousePosition.x;
109 const y = GameInput.mousePosition.y;
110 return x >= box.left && x < box.right && y >= box.top && y < box.bottom;
111 },
112 close,
113 reset ()
114 {
115 close();
116 layer.replaceChildren();
117 bubbles.clear();
118 itemBubbles.clear();
119 },
120 showBubble (message)
121 {
122 if (!message?.text)
123 { return; }
124 let bubble = bubbles.get(message.entityId);
125 if (!bubble)
126 {
127 bubble = { element: document.createElement("div"), until: 0, entityId: message.entityId };
128 bubble.element.className = "chat-bubble";
129 layer.append(bubble.element);
130 bubbles.set(message.entityId, bubble);
131 }
132 bubble.element.textContent = message.text;
133 const readability = Math.min(1, Math.max(0, Number(message.readability ?? 1)));
134 bubble.element.style.opacity = String(readability);
135 bubble.until = Date.now() + BUBBLE_MS;
136 },
137 showItemBubble (message)
138 {
139 if (message?.itemId == null || message?.entityId == null)
140 { return; }
141 let bubble = itemBubbles.get(message.entityId);
142 if (!bubble)
143 {
144 bubble = {
145 element: document.createElement("div"),
146 until: 0,
147 entityId: message.entityId
148 };
149 bubble.element.className = "item-bubble";
150 const img = document.createElement("img");
151 img.alt = "";
152 bubble.element.append(img);
153 layer.append(bubble.element);
154 itemBubbles.set(message.entityId, bubble);
155 }
156 const stack = {
157 itemId: message.itemId,
158 quantity: Math.max(1, message.quantity | 0 || 1)
159 };
160 const img = bubble.element.querySelector("img");
161 applyItemIcon(img, stack, itemAssetPathForStack(stack));
162 const readability = Math.min(1, Math.max(0, Number(message.readability ?? 1)));
163 bubble.element.style.opacity = String(readability);
164 bubble.until = Date.now() + ITEM_BUBBLE_MS;
165 },
166 syncSettings ()
167 {
168 input.maxLength = maxChars();
169 if (input.value.length > input.maxLength)
170 { input.value = input.value.slice(0, input.maxLength); }
171 },
172 update (view, scene)
173 {
174 const now = Date.now();
175 const canvas = scene?.canvas;
176 if (!canvas)
177 { return; }
178 const rect = canvas.getBoundingClientRect();
179 for (const map of [bubbles, itemBubbles])
180 {
181 for (const [entityId, bubble] of [...map.entries()])
182 {
183 if (now >= bubble.until)
184 {
185 bubble.element.remove();
186 map.delete(entityId);
187 continue;
188 }
189 positionBubble(bubble, view, rect);
190 }
191 }
192 }
193 };
194}