Skip to content

Commit 83fc862

Browse files
committed
Consolidate ImGuiMod_ enums into ImGuiKey_ (experimental)
1 parent c8e5568 commit 83fc862

File tree

4 files changed

+45
-22
lines changed

4 files changed

+45
-22
lines changed

src/CodeGenerator/ImguiDefinitions.cs

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using Newtonsoft.Json;
77
using Newtonsoft.Json.Linq;
8+
using System.Xml.Linq;
89

910
namespace CodeGenerator
1011
{
@@ -228,21 +229,35 @@ class EnumDefinition
228229
{
229230
private readonly Dictionary<string, string> _sanitizedNames;
230231

231-
public string Name { get; }
232-
public string FriendlyName { get; }
232+
public string[] Names { get; }
233+
public string[] FriendlyNames { get; }
233234
public EnumMember[] Members { get; }
234235

235236
public EnumDefinition(string name, EnumMember[] elements)
236237
{
237-
Name = name;
238-
if (Name.EndsWith('_'))
238+
if (TypeInfo.AlternateEnumPrefixes.TryGetValue(name, out string altName))
239239
{
240-
FriendlyName = Name.Substring(0, Name.Length - 1);
240+
Names = new[] { name, altName };
241241
}
242242
else
243243
{
244-
FriendlyName = Name;
244+
Names = new[] { name };
245+
}
246+
FriendlyNames = new string[Names.Length];
247+
for (int i = 0; i < Names.Length; i++)
248+
{
249+
string n = Names[i];
250+
if (n.EndsWith('_'))
251+
{
252+
FriendlyNames[i] = n.Substring(0, n.Length - 1);
253+
}
254+
else
255+
{
256+
FriendlyNames[i] = n;
257+
}
258+
245259
}
260+
246261
Members = elements;
247262

248263
_sanitizedNames = new Dictionary<string, string>();
@@ -265,12 +280,15 @@ public string SanitizeNames(string text)
265280
private string SanitizeMemberName(string memberName)
266281
{
267282
string ret = memberName;
268-
if (memberName.StartsWith(Name))
283+
foreach (string name in Names)
269284
{
270-
ret = memberName.Substring(Name.Length);
271-
if (ret.StartsWith("_"))
285+
if (memberName.StartsWith(name))
272286
{
273-
ret = ret.Substring(1);
287+
ret = memberName.Substring(name.Length);
288+
if (ret.StartsWith("_"))
289+
{
290+
ret = ret.Substring(1);
291+
}
274292
}
275293
}
276294

@@ -375,7 +393,7 @@ public TypeReference(string name, string type, int asize, string templateType, E
375393

376394
TypeVariants = typeVariants;
377395

378-
IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type || TypeInfo.WellKnownEnums.Contains(type));
396+
IsEnum = enums.Any(t => t.Names.Contains(type) || t.FriendlyNames.Contains(type) || TypeInfo.WellKnownEnums.Contains(type));
379397
}
380398

381399
private int ParseSizeString(string sizePart, EnumDefinition[] enums)
@@ -394,7 +412,7 @@ private int ParseSizeString(string sizePart, EnumDefinition[] enums)
394412
{
395413
foreach (EnumDefinition ed in enums)
396414
{
397-
if (sizePart.StartsWith(ed.Name))
415+
if (ed.Names.Any(n => sizePart.StartsWith(n)))
398416
{
399417
foreach (EnumMember member in ed.Members)
400418
{

src/CodeGenerator/Program.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,14 @@ static void Main(string[] args)
8484

8585
foreach (EnumDefinition ed in defs.Enums)
8686
{
87-
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, ed.FriendlyName + ".gen.cs")))
87+
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, ed.FriendlyNames[0] + ".gen.cs")))
8888
{
8989
writer.PushBlock($"namespace {projectNamespace}");
90-
if (ed.FriendlyName.Contains("Flags"))
90+
if (ed.FriendlyNames[0].Contains("Flags"))
9191
{
9292
writer.WriteLine("[System.Flags]");
9393
}
94-
writer.PushBlock($"public enum {ed.FriendlyName}");
94+
writer.PushBlock($"public enum {ed.FriendlyNames[0]}");
9595
foreach (EnumMember member in ed.Members)
9696
{
9797
string sanitizedName = ed.SanitizeNames(member.Name);

src/CodeGenerator/TypeInfo.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public class TypeInfo
6262
{
6363
"ImGuiMouseButton"
6464
};
65+
66+
public static readonly Dictionary<string, string> AlternateEnumPrefixes = new Dictionary<string, string>()
67+
{
68+
{ "ImGuiKey", "ImGuiMod" },
69+
};
6570

6671
public static readonly Dictionary<string, string> WellKnownFieldReplacements = new Dictionary<string, string>()
6772
{

src/ImGui.NET/Generated/ImGuiKey.gen.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,13 @@ public enum ImGuiKey
144144
ReservedForModAlt = 650,
145145
ReservedForModSuper = 651,
146146
COUNT = 652,
147-
ImGuiMod_None = 0,
148-
ImGuiMod_Ctrl = 4096,
149-
ImGuiMod_Shift = 8192,
150-
ImGuiMod_Alt = 16384,
151-
ImGuiMod_Super = 32768,
152-
ImGuiMod_Mask = 61440,
153-
ImGuiMod_Shortcut = 4096,
147+
None = 0,
148+
Ctrl = 4096,
149+
Shift = 8192,
150+
Alt = 16384,
151+
Super = 32768,
152+
Mask = 61440,
153+
Shortcut = 4096,
154154
NamedKey_BEGIN = 512,
155155
NamedKey_END = 652,
156156
NamedKey_COUNT = 140,

0 commit comments

Comments
 (0)