Skip to content

Integration of ImPlot, ImNodes and ImGuizmo #218

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/CodeGenerator/CodeGenerator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,24 @@
</PropertyGroup>

<ItemGroup>
<Content Include="structs_and_enums.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="variants.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimgui\structs_and_enums.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimgui\definitions.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimgui\variants.json" CopyToOutputDirectory="PreserveNewest" />

<Content Include="definitions\cimplot\structs_and_enums.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimplot\definitions.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimplot\variants.json" CopyToOutputDirectory="PreserveNewest" />

<Content Include="definitions\cimnodes\structs_and_enums.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimnodes\definitions.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimnodes\variants.json" CopyToOutputDirectory="PreserveNewest" />

<Content Include="definitions\cimguizmo\structs_and_enums.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimguizmo\definitions.json" CopyToOutputDirectory="PreserveNewest" />
<Content Include="definitions\cimguizmo\variants.json" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>

</Project>
15 changes: 9 additions & 6 deletions src/CodeGenerator/ImguiDefinitions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void LoadFrom(string directory)
JObject variantsJson = null;
if (File.Exists(Path.Combine(directory, "variants.json")))
{
using (StreamReader fs = File.OpenText(Path.Combine(AppContext.BaseDirectory, "variants.json")))
using (StreamReader fs = File.OpenText(Path.Combine(directory, "variants.json")))
using (JsonTextReader jr = new JsonTextReader(fs))
{
variantsJson = JObject.Load(jr);
Expand All @@ -65,12 +65,12 @@ public void LoadFrom(string directory)
{
JProperty jp = (JProperty)jt;
string name = jp.Name;
if (typeLocations?[jp.Name]?.Value<string>() == "internal") {
if (typeLocations?[jp.Name]?.Value<string>().Contains("internal") ?? false) {
return null;
}
EnumMember[] elements = jp.Values().Select(v =>
{
return new EnumMember(v["name"].ToString(), v["value"].ToString());
return new EnumMember(v["name"].ToString(), v["calc_value"].ToString());
}).ToArray();
return new EnumDefinition(name, elements);
}).Where(x => x != null).ToArray();
Expand All @@ -79,7 +79,7 @@ public void LoadFrom(string directory)
{
JProperty jp = (JProperty)jt;
string name = jp.Name;
if (typeLocations?[jp.Name]?.Value<string>() == "internal") {
if (typeLocations?[jp.Name]?.Value<string>().Contains("internal") ?? false) {
return null;
}
TypeReference[] fields = jp.Values().Select(v =>
Expand Down Expand Up @@ -120,7 +120,7 @@ public void LoadFrom(string directory)
}
}
if (friendlyName == null) { return null; }
if (val["location"]?.ToString() == "internal") return null;
if (val["location"]?.ToString().Contains("internal") ?? false) return null;

string exportedName = ov_cimguiname;
if (exportedName == null)
Expand Down Expand Up @@ -275,6 +275,9 @@ private string SanitizeMemberName(string memberName)
ret = ret.Substring(0, ret.Length - 1);
}

if (Char.IsDigit(ret.First()))
ret = "_" + ret;

return ret;
}
}
Expand Down Expand Up @@ -368,7 +371,7 @@ public TypeReference(string name, string type, int asize, string templateType, E

TypeVariants = typeVariants;

IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type);
IsEnum = enums.Any(t => t.Name == type || t.FriendlyName == type || TypeInfo.WellKnownEnums.Contains(type));
}

private int ParseSizeString(string sizePart, EnumDefinition[] enums)
Expand Down
123 changes: 95 additions & 28 deletions src/CodeGenerator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,64 @@ static void Main(string[] args)
{
outputPath = AppContext.BaseDirectory;
}

string libraryName;
if (args.Length > 1)
{
libraryName = args[1];
}
else
{
libraryName = "cimgui";
}

string projectNamespace = libraryName switch
{
"cimgui" => "ImGuiNET",
"cimplot" => "ImPlotNET",
"cimnodes" => "ImNodesNET",
"cimguizmo" => "ImGuizmoNET",
_ => throw new NotImplementedException()
};

bool referencesImGui = libraryName switch
{
"cimgui" => false,
"cimplot" => true,
"cimnodes" => true,
"cimguizmo" => true,
_ => throw new NotImplementedException()
};

string classPrefix = libraryName switch
{
"cimgui" => "ImGui",
"cimplot" => "ImPlot",
"cimnodes" => "ImNodes",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking into this more, cimnodes actually wraps the imnodes project, which is similar but different to another project called ImNodes. Using ImNodes as the class name (instea of imnodes) might be confusing because of that.

"cimguizmo" => "ImGuizmo",
_ => throw new NotImplementedException()
};

string dllName = libraryName switch
{
"cimgui" => "cimgui",
"cimplot" => "cimplot",
"cimnodes" => "cimnodes",
"cimguizmo" => "cimguizmo",
_ => throw new NotImplementedException()
};

string definitionsPath = Path.Combine(AppContext.BaseDirectory, "definitions", libraryName);
var defs = new ImguiDefinitions();
defs.LoadFrom(AppContext.BaseDirectory);
defs.LoadFrom(definitionsPath);

Console.WriteLine($"Outputting generated code files to {outputPath}.");

foreach (EnumDefinition ed in defs.Enums)
{
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, ed.FriendlyName + ".gen.cs")))
{
writer.PushBlock("namespace ImGuiNET");
writer.PushBlock($"namespace {projectNamespace}");
if (ed.FriendlyName.Contains("Flags"))
{
writer.WriteLine("[System.Flags]");
Expand All @@ -61,8 +108,12 @@ static void Main(string[] args)
writer.Using("System.Numerics");
writer.Using("System.Runtime.CompilerServices");
writer.Using("System.Text");
if (referencesImGui)
{
writer.Using("ImGuiNET");
}
writer.WriteLine(string.Empty);
writer.PushBlock("namespace ImGuiNET");
writer.PushBlock($"namespace {projectNamespace}");

writer.PushBlock($"public unsafe partial struct {td.Name}");
foreach (TypeReference field in td.Fields)
Expand Down Expand Up @@ -209,7 +260,7 @@ static void Main(string[] args)
{
defaults.Add(orderedDefaults[j].Key, orderedDefaults[j].Value);
}
EmitOverload(writer, overload, defaults, "NativePtr");
EmitOverload(writer, overload, defaults, "NativePtr", classPrefix);
}
}
}
Expand All @@ -219,14 +270,18 @@ static void Main(string[] args)
}
}

using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, "ImGuiNative.gen.cs")))
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, $"{classPrefix}Native.gen.cs")))
{
writer.Using("System");
writer.Using("System.Numerics");
writer.Using("System.Runtime.InteropServices");
if (referencesImGui)
{
writer.Using("ImGuiNET");
}
writer.WriteLine(string.Empty);
writer.PushBlock("namespace ImGuiNET");
writer.PushBlock("public static unsafe partial class ImGuiNative");
writer.PushBlock($"namespace {projectNamespace}");
writer.PushBlock($"public static unsafe partial class {classPrefix}Native");
foreach (FunctionDefinition fd in defs.Functions)
{
foreach (OverloadDefinition overload in fd.Overloads)
Expand Down Expand Up @@ -273,12 +328,12 @@ static void Main(string[] args)

if (isUdtVariant)
{
writer.WriteLine($"[DllImport(\"cimgui\", CallingConvention = CallingConvention.Cdecl, EntryPoint = \"{exportedName}\")]");
writer.WriteLine($"[DllImport(\"{dllName}\", CallingConvention = CallingConvention.Cdecl, EntryPoint = \"{exportedName}\")]");

}
else
{
writer.WriteLine("[DllImport(\"cimgui\", CallingConvention = CallingConvention.Cdecl)]");
writer.WriteLine($"[DllImport(\"{dllName}\", CallingConvention = CallingConvention.Cdecl)]");
}
writer.WriteLine($"public static extern {ret} {methodName}({parameters});");
}
Expand All @@ -287,15 +342,19 @@ static void Main(string[] args)
writer.PopBlock();
}

using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, "ImGui.gen.cs")))
using (CSharpCodeWriter writer = new CSharpCodeWriter(Path.Combine(outputPath, $"{classPrefix}.gen.cs")))
{
writer.Using("System");
writer.Using("System.Numerics");
writer.Using("System.Runtime.InteropServices");
writer.Using("System.Text");
if (referencesImGui)
{
writer.Using("ImGuiNET");
}
writer.WriteLine(string.Empty);
writer.PushBlock("namespace ImGuiNET");
writer.PushBlock("public static unsafe partial class ImGui");
writer.PushBlock($"namespace {projectNamespace}");
writer.PushBlock($"public static unsafe partial class {classPrefix}");
foreach (FunctionDefinition fd in defs.Functions)
{
if (TypeInfo.SkippedFunctions.Contains(fd.Name)) { continue; }
Expand Down Expand Up @@ -336,7 +395,7 @@ static void Main(string[] args)
{
defaults.Add(orderedDefaults[j].Key, orderedDefaults[j].Value);
}
EmitOverload(writer, overload, defaults, null);
EmitOverload(writer, overload, defaults, null, classPrefix);
}
}
}
Expand Down Expand Up @@ -381,7 +440,8 @@ private static void EmitOverload(
CSharpCodeWriter writer,
OverloadDefinition overload,
Dictionary<string, string> defaultValues,
string selfName)
string selfName,
string classPrefix)
{
if (overload.Parameters.Where(tr => tr.Name.EndsWith("_begin") || tr.Name.EndsWith("_end"))
.Any(tr => !defaultValues.ContainsKey(tr.Name)))
Expand Down Expand Up @@ -481,6 +541,15 @@ private static void EmitOverload(
postCallLines.Add($"}}");
}
}
else if (defaultValues.TryGetValue(tr.Name, out string defaultVal))
{
if (!CorrectDefaultValue(defaultVal, tr, out string correctedDefault))
{
correctedDefault = defaultVal;
}
marshalledParameters[i] = new MarshalledParameter(nativeTypeName, false, correctedIdentifier, true);
preCallLines.Add($"{nativeTypeName} {correctedIdentifier} = {correctedDefault};");
}
else if (tr.Type == "char* []")
{
string nativeArgName = "native_" + tr.Name;
Expand Down Expand Up @@ -518,15 +587,6 @@ private static void EmitOverload(
preCallLines.Add($" offset += {correctedIdentifier}_byteCounts[i] + 1;");
preCallLines.Add("}");
}
else if (defaultValues.TryGetValue(tr.Name, out string defaultVal))
{
if (!CorrectDefaultValue(defaultVal, tr, out string correctedDefault))
{
correctedDefault = defaultVal;
}
marshalledParameters[i] = new MarshalledParameter(nativeTypeName, false, correctedIdentifier, true);
preCallLines.Add($"{nativeTypeName} {correctedIdentifier} = {correctedDefault};");
}
else if (tr.Type == "bool")
{
string nativeArgName = "native_" + tr.Name;
Expand Down Expand Up @@ -557,7 +617,7 @@ private static void EmitOverload(
marshalledParameters[i] = new MarshalledParameter(wrappedParamType, false, nativeArgName, false);
preCallLines.Add($"{tr.Type} {nativeArgName} = {correctedIdentifier}.NativePtr;");
}
else if ((tr.Type.EndsWith("*") || tr.Type.Contains("[") || tr.Type.EndsWith("&")) && tr.Type != "void*" && tr.Type != "ImGuiContext*")
else if ((tr.Type.EndsWith("*") || tr.Type.Contains("[") || tr.Type.EndsWith("&")) && tr.Type != "void*" && tr.Type != "ImGuiContext*" && tr.Type != "ImPlotContext*"&& tr.Type != "EditorContext*")
{
string nonPtrType;
if (tr.Type.Contains("["))
Expand Down Expand Up @@ -634,7 +694,7 @@ private static void EmitOverload(
targetName = targetName.Substring(0, targetName.IndexOf("_nonUDT"));
}

writer.WriteLine($"{ret}ImGuiNative.{targetName}({nativeInvocationStr});");
writer.WriteLine($"{ret}{classPrefix}Native.{targetName}({nativeInvocationStr});");

foreach (string line in postCallLines)
{
Expand Down Expand Up @@ -736,7 +796,7 @@ private static bool GetWrappedType(string nativeType, out string wrappedType)

private static bool CorrectDefaultValue(string defaultVal, TypeReference tr, out string correctedDefault)
{
if (tr.Type == "ImGuiContext*")
if (tr.Type == "ImGuiContext*" || tr.Type == "ImPlotContext*" || tr.Type == "EditorContext*")
{
correctedDefault = "IntPtr.Zero";
return true;
Expand All @@ -754,7 +814,14 @@ private static bool CorrectDefaultValue(string defaultVal, TypeReference tr, out

if (tr.IsEnum)
{
correctedDefault = $"({tr.Type}){defaultVal}";
if (defaultVal.StartsWith("-"))
{
correctedDefault = $"({tr.Type})({defaultVal})";
}
else
{
correctedDefault = $"({tr.Type}){defaultVal}";
}
return true;
}

Expand Down
Loading