0.2.0-dev Download ZIP
172 lines ·5.8 KB Raw
| 1 | using System.Globalization; |
| 2 | using AngleSharp.Dom; |
| 3 | using EvilEngine.Client.Html; |
| 4 | using Godot; |
| 5 | |
| 6 | namespace EvilEngine.Client; |
| 7 | |
| 8 | public partial class MenuRealm |
| 9 | { |
| 10 | private HtmlView settings = null!; |
| 11 | private int settingsEdits; |
| 12 | |
| 13 | private bool SettingsOpen => settings.Visible; |
| 14 | |
| 15 | private void ReadySettings() |
| 16 | { |
| 17 | settings = GetNode<HtmlView>("Settings"); |
| 18 | settings.Activated += SettingsCommand; |
| 19 | settings.ValueChanged += SettingsValue; |
| 20 | settings.Toggled += SettingsToggled; |
| 21 | settings.Reloaded += () => |
| 22 | { |
| 23 | if (SettingsOpen) |
| 24 | FillSettings(); |
| 25 | }; |
| 26 | ClientSettings.Machine.Changed += SaveSettingsSoon; |
| 27 | } |
| 28 | |
| 29 | private void ShowSettings(string heading) |
| 30 | { |
| 31 | settings.Html!.Document.GetElementById("heading")!.TextContent = heading; |
| 32 | ShowSettingsTab("audio"); |
| 33 | FillSettings(); |
| 34 | settings.Visible = true; |
| 35 | } |
| 36 | |
| 37 | private void HideSettings() |
| 38 | { |
| 39 | if (!SettingsOpen) |
| 40 | return; |
| 41 | settings.Visible = false; |
| 42 | ClientSettings.Machine.Save(); |
| 43 | } |
| 44 | |
| 45 | private bool SettingsInput(InputEvent @event) |
| 46 | { |
| 47 | if (!SettingsOpen || !Back(@event)) |
| 48 | return false; |
| 49 | HideSettings(); |
| 50 | return true; |
| 51 | } |
| 52 | |
| 53 | private void ShowSettingsTab(string name) |
| 54 | { |
| 55 | var document = settings.Html!.Document; |
| 56 | foreach (var panel in document.QuerySelectorAll("[data-panel]")) |
| 57 | Show(panel, panel.Id == name); |
| 58 | foreach (var tab in document.GetElementById("tabs")!.Children) |
| 59 | if (tab.Id == $"tab-{name}") |
| 60 | tab.SetAttribute("data-selected", ""); |
| 61 | else |
| 62 | tab.RemoveAttribute("data-selected"); |
| 63 | settings.DocumentChanged(); |
| 64 | } |
| 65 | |
| 66 | private void FillSettings() |
| 67 | { |
| 68 | var document = settings.Html!.Document; |
| 69 | var master = document.GetElementById("master")!; |
| 70 | var branches = document.GetElementById("branches")!; |
| 71 | master.InnerHtml = branches.InnerHtml = ""; |
| 72 | var buses = ClientSettings.Buses(); |
| 73 | for (var index = 0; index < buses.Count; index++) |
| 74 | (index == 0 ? master : branches).AppendChild(VolumeRow(document, buses[index])); |
| 75 | var liquids = document.GetElementById("liquids")!; |
| 76 | liquids.InnerHtml = ""; |
| 77 | liquids.AppendChild(VideoToggle(document, "remastered_liquids", "Remastered liquids", |
| 78 | "Draw every liquid with the engine's own shimmering water instead of each game's own", |
| 79 | ClientSettings.Machine.RemasteredLiquids())); |
| 80 | settings.DocumentChanged(); |
| 81 | } |
| 82 | |
| 83 | private static IElement VideoToggle(IDocument document, string key, string title, string detail, bool on) |
| 84 | { |
| 85 | var row = Element(document, "label", "flex items-center gap-3 rounded px-3 py-1.5"); |
| 86 | var box = (IElement)document.GetElementById("kit-check")!.Clone(); |
| 87 | box.RemoveAttribute("id"); |
| 88 | box.SetAttribute("data-video", key); |
| 89 | if (on) |
| 90 | box.SetAttribute("checked", ""); |
| 91 | row.AppendChild(box); |
| 92 | row.AppendChild(Element(document, "span", "shrink-0 text-sm font-medium text-ink", title)); |
| 93 | row.AppendChild(Element(document, "span", "min-w-0 truncate text-xs text-ink-dull", detail)); |
| 94 | return row; |
| 95 | } |
| 96 | |
| 97 | private void SettingsToggled(IElement element, bool on) |
| 98 | { |
| 99 | if (element.GetAttribute("data-video") == "remastered_liquids") |
| 100 | ClientSettings.Machine.SetRemasteredLiquids(on); |
| 101 | } |
| 102 | |
| 103 | private static IElement VolumeRow(IDocument document, string bus) |
| 104 | { |
| 105 | var percent = Percent(ClientSettings.Machine.Volume(bus)); |
| 106 | var row = Element(document, "label", "flex items-center gap-4"); |
| 107 | row.AppendChild(Element(document, "span", "w-28 shrink-0 text-sm text-ink", bus)); |
| 108 | var input = (IElement)document.GetElementById("kit-slider")!.Clone(); |
| 109 | input.SetAttribute("id", $"volume-{bus}"); |
| 110 | input.SetAttribute("data-bus", bus); |
| 111 | input.SetAttribute("min", "0"); |
| 112 | input.SetAttribute("max", "100"); |
| 113 | input.SetAttribute("step", "1"); |
| 114 | input.SetAttribute("value", percent.ToString(CultureInfo.InvariantCulture)); |
| 115 | row.AppendChild(input); |
| 116 | var shown = Element(document, "span", "w-12 shrink-0 text-right text-sm text-ink-dull", $"{percent}%"); |
| 117 | shown.Id = $"volume-{bus}-shown"; |
| 118 | row.AppendChild(shown); |
| 119 | return row; |
| 120 | } |
| 121 | |
| 122 | private static int Percent(float linear) |
| 123 | { |
| 124 | return (int)Math.Round(linear * 100); |
| 125 | } |
| 126 | |
| 127 | private void SettingsCommand(IElement element) |
| 128 | { |
| 129 | switch (element.GetAttribute("command")) |
| 130 | { |
| 131 | case "--close": |
| 132 | HideSettings(); |
| 133 | break; |
| 134 | case "--tab-audio": |
| 135 | ShowSettingsTab("audio"); |
| 136 | break; |
| 137 | case "--tab-video": |
| 138 | ShowSettingsTab("video"); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | private void SettingsValue(IElement element, string value) |
| 144 | { |
| 145 | if (element.GetAttribute("data-bus") is not { } bus || |
| 146 | !double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var percent)) |
| 147 | return; |
| 148 | ClientSettings.Machine.SetVolume(bus, (float)(percent / 100)); |
| 149 | if (settings.Html!.Document.GetElementById($"volume-{bus}-shown") is { } shown) |
| 150 | { |
| 151 | shown.TextContent = $"{(int)Math.Round(percent)}%"; |
| 152 | settings.DocumentChanged(shown); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | private void SaveSettingsSoon() |
| 157 | { |
| 158 | var edit = ++settingsEdits; |
| 159 | GetTree().CreateTimer(1.0).Timeout += () => |
| 160 | { |
| 161 | if (edit == settingsEdits) |
| 162 | ClientSettings.Machine.Save(); |
| 163 | }; |
| 164 | } |
| 165 | |
| 166 | public override void _ExitTree() |
| 167 | { |
| 168 | ClientSettings.Machine.Changed -= SaveSettingsSoon; |
| 169 | ClientSettings.Machine.Save(); |
| 170 | SaveServerNow(); |
| 171 | } |
| 172 | } |