From fbafacc1ddb0d0d6973f337c49441089a990cf68 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 26 Oct 2026 17:48:37 -0400 Subject: Improve imgui navigation, add Screen parameters Signed-off-by: Andrew Opalach --- Utility.cs | 162 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 159 insertions(+), 3 deletions(-) (limited to 'Utility.cs') diff --git a/Utility.cs b/Utility.cs index e48ee52..416e9a5 100644 --- a/Utility.cs +++ b/Utility.cs @@ -7,6 +7,7 @@ using System.Runtime.CompilerServices; using System.Diagnostics; #endif +using ImGuiNET; using SharpPluginLoader.Core; using SharpPluginLoader.Core.Memory; @@ -88,7 +89,7 @@ namespace WorldTuningTool internal static Vector2 StringToVector2(string s, char delim = ',') { - if (String.IsNullOrEmpty(s)) throw new FormatException(); + if (string.IsNullOrEmpty(s)) throw new FormatException(); string[] sp = s.Split(delim).Select((v) => v.Trim()).ToArray(); if (sp.Length != 2) throw new FormatException(); return new(StringToSingle(sp[0]), StringToSingle(sp[1])); @@ -96,7 +97,7 @@ namespace WorldTuningTool internal static Vector3 StringToVector3(string s, char delim = ',') { - if (String.IsNullOrEmpty(s)) throw new FormatException(); + if (string.IsNullOrEmpty(s)) throw new FormatException(); string[] sp = s.Split(delim).Select((v) => v.Trim()).ToArray(); if (sp.Length != 3) throw new FormatException(); return new(StringToSingle(sp[0]), StringToSingle(sp[1]), StringToSingle(sp[2])); @@ -104,7 +105,7 @@ namespace WorldTuningTool internal static Vector4 StringToVector4(string s, char delim = ',') { - if (String.IsNullOrEmpty(s)) throw new FormatException(); + if (string.IsNullOrEmpty(s)) throw new FormatException(); string[] sp = s.Split(delim).Select((v) => v.Trim()).ToArray(); if (sp.Length != 4) throw new FormatException(); return new(StringToSingle(sp[0]), StringToSingle(sp[1]), StringToSingle(sp[2]), StringToSingle(sp[3])); @@ -124,5 +125,160 @@ namespace WorldTuningTool { return string.Format(CultureInfo.InvariantCulture, "{0},{1},{2},{3}", v.X, v.Y, v.Z, v.W); } + + internal class PresetSelection + { + internal string Selected = ""; + internal bool comboLastFocused = false; + internal bool renaming = false; + internal bool confirmDelete = false; + internal string typed = ""; + internal Dictionary> dict; + internal string typeName; + + internal PresetSelection(Dictionary> dict, string typeName) + { + this.dict = dict; + this.typeName = typeName; + } + + internal string Draw(float width) + { + ImGui.PushItemWidth(width * 0.35f); + if (!comboLastFocused) + { + ImGui.PushItemFlag(ImGuiItemFlags.NoNav, true); + } + if (ImGui.Button("+")) + { + string name = $"New {typeName}"; + int dup = 0; + while (dict.ContainsKey(name)) + { + dup++; + name = $"New {typeName} ({dup})"; + } + dict[name] = new List(); + Selected = name; + renaming = true; + typed = Selected; + } + if (!comboLastFocused) + { + ImGui.PopItemFlag(); + } + ImGui.SameLine(); + if (renaming) + { + ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true); + ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * disableAlphaFactor); + } + bool beginCombo = ImGui.BeginCombo($"##{typeName}", Selected); + comboLastFocused = ImGui.IsItemActive() || ImGui.IsItemFocused(); + if (beginCombo) + { + confirmDelete = false; + if (ImGui.Selectable("(None)", false)) + { + Selected = ""; + } + foreach ((string name, List overrides) in dict) + { + bool isSelected = name == Selected; + if (ImGui.Selectable(name, isSelected)) + { + Selected = name; + } + if (isSelected) ImGui.SetItemDefaultFocus(); + } + ImGui.EndCombo(); + } + string preRenameSelected = Selected; + if (renaming) + { + ImGui.PopStyleVar(); + ImGui.PopItemFlag(); + } + ImGui.PopItemWidth(); + if (Selected != "") + { + ImGui.SameLine(); + ImGui.PushItemWidth(width * 0.2f); + if (renaming) + { + ImGui.InputText($"##{typeName} Name", ref typed, 99); + ImGui.SameLine(); + if (typed == "") + { + ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true); + ImGui.PushStyleVar(ImGuiStyleVar.Alpha, ImGui.GetStyle().Alpha * disableAlphaFactor); + } + if (ImGui.Button("Save")) + { + string name = typed; + if (name != Selected) + { + int dup = 0; + while (dict.ContainsKey(name)) + { + dup++; + name = $"{typed} ({dup})"; + } + List tmpOverrides = dict[Selected]; + dict.Remove(Selected); + dict[name] = tmpOverrides; + Selected = name; + } + renaming = false; + } + if (typed == "") + { + ImGui.PopStyleVar(); + ImGui.PopItemFlag(); + } + } + else + { + if (confirmDelete) + { + ImGui.PushItemFlag(ImGuiItemFlags.Disabled, true); + } + if (ImGui.Button("Rename")) + { + renaming = true; + typed = Selected; + } + if (confirmDelete) + { + ImGui.PopItemFlag(); + } + ImGui.SameLine(); + if (confirmDelete) + { + ImGui.AlignTextToFramePadding(); + ImGui.Text("Are you sure?"); + ImGui.SameLine(); + if (ImGui.Button("No")) + { + confirmDelete = false; + } + ImGui.SameLine(); + if (ImGui.Button("Yes")) + { + dict.Remove(Selected); + Selected = ""; + confirmDelete = false; + } + } + else if (ImGui.Button("Delete")) + { + confirmDelete = true; + } + } + ImGui.PopItemWidth(); + } + return preRenameSelected; + } + } } } -- cgit v1.2.3-101-g0448