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