BlueGit

timfa questward

Files

questward / client / src / play / graphicsSettings.js

graphicsSettings.js

graphicsSettings.js 1.9 KiB Download Raw
1import {
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: { viewDistance: string, lighting: string, bloom: string }) => void
13 * }} options
14 */
15export function initGraphicsSettings ({ onChange })
16{
17 const toggle = document.getElementById("settings-toggle");
18 const panel = document.getElementById("settings-panel");
19 const closeBtn = document.getElementById("settings-close");
20 if (!toggle || !panel)
21 {
22 return {
23 getSettings: () => loadGraphicsSettings(),
24 apply: () => {},
25 close: () => {}
26 };
27 }
28
29 let settings = loadGraphicsSettings();
30
31 function syncButtons ()
32 {
33 for (const btn of panel.querySelectorAll("[data-graphics-key]"))
34 {
35 const key = btn.getAttribute("data-graphics-key");
36 const value = btn.getAttribute("data-graphics-value");
37 btn.classList.toggle("active", settings[key] === value);
38 }
39 }
40
41 function emit ()
42 {
43 settings = saveGraphicsSettings(settings);
44 syncButtons();
45 onChange?.(settings);
46 }
47
48 function open ()
49 {
50 panel.hidden = false;
51 syncButtons();
52 }
53
54 function close ()
55 {
56 panel.hidden = true;
57 }
58
59 toggle.addEventListener("click", () =>
60 {
61 if (panel.hidden)
62 { open(); }
63 else
64 { close(); }
65 });
66 closeBtn?.addEventListener("click", close);
67
68 panel.addEventListener("click", (event) =>
69 {
70 const btn = event.target.closest("[data-graphics-key]");
71 if (!btn || !panel.contains(btn))
72 { return; }
73 const key = btn.getAttribute("data-graphics-key");
74 const value = btn.getAttribute("data-graphics-value");
75 if (!key || !value)
76 { return; }
77 settings = normalizeGraphicsSettings({ ...settings, [key]: value });
78 emit();
79 });
80
81 syncButtons();
82
83 return {
84 getSettings: () => ({ ...settings }),
85 /** Re-apply current settings (e.g. after world enter). */
86 apply ()
87 { onChange?.(settings); },
88 close
89 };
90}
91
92export { ViewDistance, LightingMode, BloomMode };