| 1 | import { bindHairControls } from "./hairControls.js"; |
| 2 | |
| 3 | export function initBarber ({ |
| 4 | socket, |
| 5 | MessageType, |
| 6 | resolveSpriteUrl, |
| 7 | getInventoryBag, |
| 8 | onSuppressWorldClick |
| 9 | }) |
| 10 | { |
| 11 | const backdrop = document.getElementById("barber-backdrop"); |
| 12 | const panel = document.getElementById("barber-panel"); |
| 13 | const statusEl = document.getElementById("barber-status"); |
| 14 | let open = false; |
| 15 | let sex = "male"; |
| 16 | |
| 17 | const controls = bindHairControls({ |
| 18 | root: panel, |
| 19 | resolveSpriteUrl, |
| 20 | getSex: () => sex, |
| 21 | getBag: () => getInventoryBag?.() ?? null, |
| 22 | hideHeadgear: true |
| 23 | }); |
| 24 | |
| 25 | function suppressWorldClick () |
| 26 | { |
| 27 | onSuppressWorldClick?.(); |
| 28 | } |
| 29 | |
| 30 | document.getElementById("barber-close")?.addEventListener("click", () => close()); |
| 31 | backdrop?.addEventListener("mousedown", (event) => |
| 32 | { |
| 33 | event.preventDefault(); |
| 34 | suppressWorldClick(); |
| 35 | }); |
| 36 | backdrop?.addEventListener("click", () => close()); |
| 37 | panel?.addEventListener("mousedown", (event) => |
| 38 | { |
| 39 | event.stopPropagation(); |
| 40 | suppressWorldClick(); |
| 41 | }); |
| 42 | document.getElementById("barber-apply")?.addEventListener("click", () => |
| 43 | { |
| 44 | statusEl.textContent = "Saving…"; |
| 45 | socket.send({ |
| 46 | type: MessageType.setHair, |
| 47 | hair: controls.currentHair() |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | function close () |
| 52 | { |
| 53 | if (!open) |
| 54 | { return; } |
| 55 | open = false; |
| 56 | panel.hidden = true; |
| 57 | backdrop.hidden = true; |
| 58 | statusEl.textContent = ""; |
| 59 | socket.send({ type: MessageType.closeBarber }); |
| 60 | } |
| 61 | |
| 62 | return { |
| 63 | isOpen: () => open, |
| 64 | isBlocking: () => open, |
| 65 | close, |
| 66 | handleSnapshot (message) |
| 67 | { |
| 68 | open = true; |
| 69 | panel.hidden = false; |
| 70 | backdrop.hidden = false; |
| 71 | sex = message.sex === "female" ? "female" : "male"; |
| 72 | controls.fillStyles(); |
| 73 | controls.setHair(message.hair); |
| 74 | statusEl.textContent = message.message ?? ""; |
| 75 | }, |
| 76 | handleClosed () |
| 77 | { |
| 78 | open = false; |
| 79 | panel.hidden = true; |
| 80 | backdrop.hidden = true; |
| 81 | } |
| 82 | }; |
| 83 | } |