Skip to content
0.2.0-dev Download ZIP
152 lines ·6.4 KB Raw
1using AngleSharp.Dom;
2using EvilEngine.Host.Sessions;
3using EvilEngine.Shared.Sessions;
4
5namespace EvilEngine.Client;
6
7public partial class MenuRealm
8{
9 private const string LightingLink = "evil:lighting/";
10
11 private static readonly (string Key, string Title, string Detail)[] RealtimeEffects =
12 [
13 ("global_illumination", "Global illumination", "SDFGI, light bouncing off the world"),
14 ("ambient_occlusion", "Ambient occlusion", "SSAO, shade in corners and creases"),
15 ("indirect_light", "Indirect light", "SSIL, light bouncing off what's on screen"),
16 ("reflections", "Reflections", "SSR, the screen reflected in shiny surfaces")
17 ];
18
19 private static readonly (string Key, string Title, string Detail)[] EnvironmentEffects =
20 [
21 ("fog", "Fog", "The map's fog, or its sky's"),
22 ("bloom", "Bloom", "The map's glow around bright light")
23 ];
24
25 private void ReadyLighting()
26 {
27 pause.Toggled += LightingToggled;
28 }
29
30 private void ShowServerTab(string name)
31 {
32 var document = pause.Html!.Document;
33 foreach (var panel in document.QuerySelectorAll("[data-server-panel]"))
34 Show(panel, panel.Id == $"server-{name}");
35 foreach (var tab in document.GetElementById("server-tabs")!.Children)
36 if (tab.Id == $"server-tab-{name}")
37 tab.SetAttribute("data-selected", "");
38 else
39 tab.RemoveAttribute("data-selected");
40 pause.DocumentChanged(document.GetElementById("server")!);
41 }
42
43 private void ShowLighting()
44 {
45 var document = pause.Html!.Document;
46 var lighting = ServerSettings.Host.Lighting;
47 document.GetElementById("lighting-state")!.TextContent = lighting.Realtime
48 ? "Lit in realtime by Godot: the sky's sun and ambient and the map's lights, with nothing the map baked."
49 : "Lit by what the map baked, as its game made it.";
50 var modes = document.GetElementById("lighting-modes")!;
51 modes.InnerHtml = "";
52 modes.AppendChild(ModeRow(document, LightingMode.Baked, "The map's own", "baked lightmaps",
53 !lighting.Realtime));
54 modes.AppendChild(ModeRow(document, LightingMode.Realtime, "Realtime", "Godot Forward+", lighting.Realtime));
55 var effects = document.GetElementById("lighting-effects")!;
56 effects.InnerHtml = "";
57 foreach (var (key, title, detail) in RealtimeEffects)
58 effects.AppendChild(LightingToggle(document, key, title, detail, Flag(lighting, key), lighting.Realtime));
59 var environment = document.GetElementById("lighting-environment")!;
60 environment.InnerHtml = "";
61 foreach (var (key, title, detail) in EnvironmentEffects)
62 environment.AppendChild(LightingToggle(document, key, title, detail, Flag(lighting, key), true));
63 pause.DocumentChanged(document.GetElementById("server-lighting")!);
64 }
65
66 private static IElement ModeRow(IDocument document, LightingMode mode, string title, string detail, bool picked)
67 {
68 var row = Element(document, "a",
69 "flex items-baseline gap-3 px-3 py-1.5 text-ink no-underline hover:bg-armed data-[selected]:bg-evil-lo data-[selected]:text-ink-bright");
70 row.SetAttribute("href", LightingLink + mode.ToString().ToLowerInvariant());
71 if (picked)
72 row.SetAttribute("data-selected", "");
73 row.AppendChild(Element(document, "span", "shrink-0 font-medium", title));
74 row.AppendChild(Element(document, "span", "min-w-0 truncate text-xs opacity-60", detail));
75 return row;
76 }
77
78 private static IElement LightingToggle(IDocument document, string key, string title, string detail, bool on,
79 bool enabled, string attribute = "data-lighting")
80 {
81 var row = Element(document, "label",
82 enabled
83 ? "flex items-center gap-3 rounded px-3 py-1.5"
84 : "flex items-center gap-3 rounded px-3 py-1.5 opacity-40");
85 var box = (IElement)document.GetElementById("kit-check")!.Clone();
86 box.RemoveAttribute("id");
87 box.SetAttribute(attribute, key);
88 if (on)
89 box.SetAttribute("checked", "");
90 if (!enabled)
91 box.SetAttribute("disabled", "");
92 row.AppendChild(box);
93 row.AppendChild(Element(document, "span", "shrink-0 text-sm font-medium text-neutral-200", title));
94 row.AppendChild(Element(document, "span", "min-w-0 truncate text-xs text-neutral-500", detail));
95 return row;
96 }
97
98 private static bool Flag(ServerLighting lighting, string key)
99 {
100 return key switch
101 {
102 "global_illumination" => lighting.GlobalIllumination,
103 "ambient_occlusion" => lighting.AmbientOcclusion,
104 "indirect_light" => lighting.IndirectLight,
105 "reflections" => lighting.Reflections,
106 "fog" => lighting.Fog,
107 "bloom" => lighting.Bloom,
108 _ => throw new ArgumentOutOfRangeException(nameof(key), key, null)
109 };
110 }
111
112 private static ServerLighting WithFlag(ServerLighting lighting, string key, bool on)
113 {
114 return key switch
115 {
116 "global_illumination" => lighting with { GlobalIllumination = on },
117 "ambient_occlusion" => lighting with { AmbientOcclusion = on },
118 "indirect_light" => lighting with { IndirectLight = on },
119 "reflections" => lighting with { Reflections = on },
120 "fog" => lighting with { Fog = on },
121 "bloom" => lighting with { Bloom = on },
122 _ => lighting
123 };
124 }
125
126 private bool LightingLinkFollowed(string href)
127 {
128 if (!href.StartsWith(LightingLink, StringComparison.Ordinal))
129 return false;
130 if (Enum.TryParse<LightingMode>(href[LightingLink.Length..], true, out var mode))
131 Relight(ServerSettings.Host.Lighting with { Mode = mode });
132 return true;
133 }
134
135 private void LightingToggled(IElement element, bool on)
136 {
137 if (hosting && element.GetAttribute("data-lighting") is { } key)
138 Relight(WithFlag(ServerSettings.Host.Lighting, key, on));
139 }
140
141 private void Relight(ServerLighting lighting)
142 {
143 if (lighting == ServerSettings.Host.Lighting)
144 return;
145 ServerSettings.Host.SetLighting(lighting);
146 if (server is { } local)
147 local.Session.Relight(lighting);
148 else
149 world?.ShowLighting(lighting);
150 ShowLighting();
151 }
152}