using AngleSharp.Dom; using EvilEngine.Host.Sessions; using EvilEngine.Shared.Sessions; namespace EvilEngine.Client; public partial class MenuRealm { private const string LightingLink = "evil:lighting/"; private static readonly (string Key, string Title, string Detail)[] RealtimeEffects = [ ("global_illumination", "Global illumination", "SDFGI, light bouncing off the world"), ("ambient_occlusion", "Ambient occlusion", "SSAO, shade in corners and creases"), ("indirect_light", "Indirect light", "SSIL, light bouncing off what's on screen"), ("reflections", "Reflections", "SSR, the screen reflected in shiny surfaces") ]; private static readonly (string Key, string Title, string Detail)[] EnvironmentEffects = [ ("fog", "Fog", "The map's fog, or its sky's"), ("bloom", "Bloom", "The map's glow around bright light") ]; private void ReadyLighting() { pause.Toggled += LightingToggled; } private void ShowServerTab(string name) { var document = pause.Html!.Document; foreach (var panel in document.QuerySelectorAll("[data-server-panel]")) Show(panel, panel.Id == $"server-{name}"); foreach (var tab in document.GetElementById("server-tabs")!.Children) if (tab.Id == $"server-tab-{name}") tab.SetAttribute("data-selected", ""); else tab.RemoveAttribute("data-selected"); pause.DocumentChanged(document.GetElementById("server")!); } private void ShowLighting() { var document = pause.Html!.Document; var lighting = ServerSettings.Host.Lighting; document.GetElementById("lighting-state")!.TextContent = lighting.Realtime ? "Lit in realtime by Godot: the sky's sun and ambient and the map's lights, with nothing the map baked." : "Lit by what the map baked, as its game made it."; var modes = document.GetElementById("lighting-modes")!; modes.InnerHtml = ""; modes.AppendChild(ModeRow(document, LightingMode.Baked, "The map's own", "baked lightmaps", !lighting.Realtime)); modes.AppendChild(ModeRow(document, LightingMode.Realtime, "Realtime", "Godot Forward+", lighting.Realtime)); var effects = document.GetElementById("lighting-effects")!; effects.InnerHtml = ""; foreach (var (key, title, detail) in RealtimeEffects) effects.AppendChild(LightingToggle(document, key, title, detail, Flag(lighting, key), lighting.Realtime)); var environment = document.GetElementById("lighting-environment")!; environment.InnerHtml = ""; foreach (var (key, title, detail) in EnvironmentEffects) environment.AppendChild(LightingToggle(document, key, title, detail, Flag(lighting, key), true)); pause.DocumentChanged(document.GetElementById("server-lighting")!); } private static IElement ModeRow(IDocument document, LightingMode mode, string title, string detail, bool picked) { var row = Element(document, "a", "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"); row.SetAttribute("href", LightingLink + mode.ToString().ToLowerInvariant()); if (picked) row.SetAttribute("data-selected", ""); row.AppendChild(Element(document, "span", "shrink-0 font-medium", title)); row.AppendChild(Element(document, "span", "min-w-0 truncate text-xs opacity-60", detail)); return row; } private static IElement LightingToggle(IDocument document, string key, string title, string detail, bool on, bool enabled, string attribute = "data-lighting") { var row = Element(document, "label", enabled ? "flex items-center gap-3 rounded px-3 py-1.5" : "flex items-center gap-3 rounded px-3 py-1.5 opacity-40"); var box = (IElement)document.GetElementById("kit-check")!.Clone(); box.RemoveAttribute("id"); box.SetAttribute(attribute, key); if (on) box.SetAttribute("checked", ""); if (!enabled) box.SetAttribute("disabled", ""); row.AppendChild(box); row.AppendChild(Element(document, "span", "shrink-0 text-sm font-medium text-neutral-200", title)); row.AppendChild(Element(document, "span", "min-w-0 truncate text-xs text-neutral-500", detail)); return row; } private static bool Flag(ServerLighting lighting, string key) { return key switch { "global_illumination" => lighting.GlobalIllumination, "ambient_occlusion" => lighting.AmbientOcclusion, "indirect_light" => lighting.IndirectLight, "reflections" => lighting.Reflections, "fog" => lighting.Fog, "bloom" => lighting.Bloom, _ => throw new ArgumentOutOfRangeException(nameof(key), key, null) }; } private static ServerLighting WithFlag(ServerLighting lighting, string key, bool on) { return key switch { "global_illumination" => lighting with { GlobalIllumination = on }, "ambient_occlusion" => lighting with { AmbientOcclusion = on }, "indirect_light" => lighting with { IndirectLight = on }, "reflections" => lighting with { Reflections = on }, "fog" => lighting with { Fog = on }, "bloom" => lighting with { Bloom = on }, _ => lighting }; } private bool LightingLinkFollowed(string href) { if (!href.StartsWith(LightingLink, StringComparison.Ordinal)) return false; if (Enum.TryParse(href[LightingLink.Length..], true, out var mode)) Relight(ServerSettings.Host.Lighting with { Mode = mode }); return true; } private void LightingToggled(IElement element, bool on) { if (hosting && element.GetAttribute("data-lighting") is { } key) Relight(WithFlag(ServerSettings.Host.Lighting, key, on)); } private void Relight(ServerLighting lighting) { if (lighting == ServerSettings.Host.Lighting) return; ServerSettings.Host.SetLighting(lighting); if (server is { } local) local.Session.Relight(lighting); else world?.ShowLighting(lighting); ShowLighting(); } }