summaryrefslogtreecommitdiff
path: root/Utility.cs
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2026-10-26 17:48:37 -0400
committerAndrew Opalach <andrew@akon.city> 2026-10-26 18:34:14 -0400
commitfbafacc1ddb0d0d6973f337c49441089a990cf68 (patch)
tree67a1f59a41014e763e5d519ec2e1b538b98c2b01 /Utility.cs
parent5658a33e763cb3ed296f683b5dd128dc94f113e9 (diff)
downloadWorldTuningTool-fbafacc1ddb0d0d6973f337c49441089a990cf68.tar.gz
WorldTuningTool-fbafacc1ddb0d0d6973f337c49441089a990cf68.tar.bz2
WorldTuningTool-fbafacc1ddb0d0d6973f337c49441089a990cf68.zip
Improve imgui navigation, add Screen parameters
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'Utility.cs')
-rw-r--r--Utility.cs162
1 files changed, 159 insertions, 3 deletions
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<T>
+ {
+ internal string Selected = "";
+ internal bool comboLastFocused = false;
+ internal bool renaming = false;
+ internal bool confirmDelete = false;
+ internal string typed = "";
+ internal Dictionary<string, List<T>> dict;
+ internal string typeName;
+
+ internal PresetSelection(Dictionary<string, List<T>> 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<T>();
+ 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<T> 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<T> 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;
+ }
+ }
}
}