| 1 | import { |
| 2 | ViewDistance, |
| 3 | LightingMode, |
| 4 | BloomMode, |
| 5 | loadGraphicsSettings, |
| 6 | saveGraphicsSettings, |
| 7 | normalizeGraphicsSettings |
| 8 | } from "/shared/graphics/settings.js"; |
| 9 | |
| 10 | /** |
| 11 | * @param {{ |
| 12 | * onChange: (settings: object) => void, |
| 13 | * onSuppressWorldClick?: () => void |
| 14 | * }} options |
| 15 | */ |
| 16 | export function initGraphicsSettings ({ onChange, onSuppressWorldClick }) |
| 17 | { |
| 18 | const toggle = document.getElementById("settings-toggle"); |
| 19 | const panel = document.getElementById("settings-panel"); |
| 20 | const closeBtn = document.getElementById("settings-close"); |
| 21 | if (!toggle || !panel) |
| 22 | { |
| 23 | return { |
| 24 | getSettings: () => loadGraphicsSettings(), |
| 25 | apply: () => {}, |
| 26 | close: () => {}, |
| 27 | isBlocking: () => false |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | let settings = loadGraphicsSettings(); |
| 32 | |
| 33 | function syncButtons () |
| 34 | { |
| 35 | for (const btn of panel.querySelectorAll("[data-graphics-key]")) |
| 36 | { |
| 37 | const key = btn.getAttribute("data-graphics-key"); |
| 38 | const value = btn.getAttribute("data-graphics-value"); |
| 39 | btn.classList.toggle("active", settings[key] === value); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | function emit () |
| 44 | { |
| 45 | settings = saveGraphicsSettings(settings); |
| 46 | syncButtons(); |
| 47 | onChange?.(settings); |
| 48 | } |
| 49 | |
| 50 | function open () |
| 51 | { |
| 52 | panel.hidden = false; |
| 53 | syncButtons(); |
| 54 | } |
| 55 | |
| 56 | function close () |
| 57 | { |
| 58 | panel.hidden = true; |
| 59 | } |
| 60 | |
| 61 | function isBlocking () |
| 62 | { |
| 63 | return !panel.hidden; |
| 64 | } |
| 65 | |
| 66 | function suppressWorld (event) |
| 67 | { |
| 68 | event.preventDefault(); |
| 69 | event.stopPropagation(); |
| 70 | onSuppressWorldClick?.(); |
| 71 | } |
| 72 | |
| 73 | toggle.addEventListener("mousedown", suppressWorld); |
| 74 | toggle.addEventListener("click", (event) => |
| 75 | { |
| 76 | event.preventDefault(); |
| 77 | event.stopPropagation(); |
| 78 | onSuppressWorldClick?.(); |
| 79 | if (panel.hidden) |
| 80 | { open(); } |
| 81 | else |
| 82 | { close(); } |
| 83 | }); |
| 84 | closeBtn?.addEventListener("mousedown", suppressWorld); |
| 85 | closeBtn?.addEventListener("click", (event) => |
| 86 | { |
| 87 | event.preventDefault(); |
| 88 | event.stopPropagation(); |
| 89 | onSuppressWorldClick?.(); |
| 90 | close(); |
| 91 | }); |
| 92 | |
| 93 | panel.addEventListener("mousedown", suppressWorld); |
| 94 | panel.addEventListener("click", (event) => |
| 95 | { |
| 96 | event.stopPropagation(); |
| 97 | onSuppressWorldClick?.(); |
| 98 | const btn = event.target.closest("[data-graphics-key]"); |
| 99 | if (!btn || !panel.contains(btn)) |
| 100 | { return; } |
| 101 | const key = btn.getAttribute("data-graphics-key"); |
| 102 | const value = btn.getAttribute("data-graphics-value"); |
| 103 | if (!key || !value) |
| 104 | { return; } |
| 105 | settings = normalizeGraphicsSettings({ ...settings, [key]: value }); |
| 106 | emit(); |
| 107 | }); |
| 108 | |
| 109 | syncButtons(); |
| 110 | |
| 111 | return { |
| 112 | getSettings: () => ({ ...settings }), |
| 113 | /** Re-apply current settings (e.g. after world enter). */ |
| 114 | apply () |
| 115 | { onChange?.(settings); }, |
| 116 | close, |
| 117 | isBlocking |
| 118 | }; |
| 119 | } |
| 120 | |
| 121 | export { ViewDistance, LightingMode, BloomMode }; |