import { ViewDistance, LightingMode, BloomMode, loadGraphicsSettings, saveGraphicsSettings, normalizeGraphicsSettings } from "/shared/graphics/settings.js"; /** * @param {{ * onChange: (settings: { viewDistance: string, lighting: string, bloom: string }) => void * }} options */ export function initGraphicsSettings ({ onChange }) { const toggle = document.getElementById("settings-toggle"); const panel = document.getElementById("settings-panel"); const closeBtn = document.getElementById("settings-close"); if (!toggle || !panel) { return { getSettings: () => loadGraphicsSettings(), apply: () => {}, close: () => {} }; } let settings = loadGraphicsSettings(); function syncButtons () { for (const btn of panel.querySelectorAll("[data-graphics-key]")) { const key = btn.getAttribute("data-graphics-key"); const value = btn.getAttribute("data-graphics-value"); btn.classList.toggle("active", settings[key] === value); } } function emit () { settings = saveGraphicsSettings(settings); syncButtons(); onChange?.(settings); } function open () { panel.hidden = false; syncButtons(); } function close () { panel.hidden = true; } toggle.addEventListener("click", () => { if (panel.hidden) { open(); } else { close(); } }); closeBtn?.addEventListener("click", close); panel.addEventListener("click", (event) => { const btn = event.target.closest("[data-graphics-key]"); if (!btn || !panel.contains(btn)) { return; } const key = btn.getAttribute("data-graphics-key"); const value = btn.getAttribute("data-graphics-value"); if (!key || !value) { return; } settings = normalizeGraphicsSettings({ ...settings, [key]: value }); emit(); }); syncButtons(); return { getSettings: () => ({ ...settings }), /** Re-apply current settings (e.g. after world enter). */ apply () { onChange?.(settings); }, close }; } export { ViewDistance, LightingMode, BloomMode };