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