Tried EntityEditor in C#

This commit is contained in:
Simon Lübeß
2023-11-10 12:41:58 +01:00
parent b41eaad788
commit 6f1467046d
13 changed files with 285 additions and 5 deletions
+130
View File
@@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using GlitchyEngine.Core;
using ImGuiNET;
namespace GlitchyEngine.Editor;
internal class EntityEditor
{
public static bool ShowFieldInEditor(FieldInfo fieldInfo)
{
if (fieldInfo.IsPublic)
return true;
var showInEditorAttribute = fieldInfo.GetCustomAttribute<ShowInEditorAttribute>();
if (showInEditorAttribute != null)
return true;
return false;
}
public static void ShowDefaultEntityEditor(UUID entityId, Type entityType)
{
object instance = ScriptGlue.Entity_GetScriptInstance(entityId);
ShowEditor(entityType, instance);
}
public static void ShowEditor(Type type, object reference)
{
T GetValue<T>(FieldInfo fieldInfo)
{
return default;
}
int i = 0;
foreach (FieldInfo field in type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public))
{
i++;
ImGui.PushID(i);
if (ShowFieldInEditor(field))
{
Type fieldType = field.FieldType;
if (fieldType.IsGenericType)
{
if (fieldType.GetGenericTypeDefinition() == typeof(List<>))
{
ImGui.Text($"{field.Name} List");
}
else if (fieldType.GetGenericTypeDefinition() == typeof(Dictionary<,>))
{
ImGui.Text($"{field.Name} Dictionary");
}
}
else if (fieldType.IsArray)
{
ImGui.Text($"{field.Name} Array");
}
else if (fieldType.IsEnum)
{
ImGui.Text($"{field.Name} Enum");
}
else if (fieldType.IsPrimitive)
{
ImGui.Text($"{field.Name} Primitive");
if (reference != null)
{
if (fieldType == typeof(float))
{
float f = (float)field.GetValue(reference);
if (ImGui.DragFloat(field.Name, ref f))
{
field.SetValue(reference, f);
}
}
}
}
else if (fieldType.IsValueType)
{
// Struct
if (ImGui.TreeNode(field.Name))
{
object @struct = (reference != null) ? field.GetValue(reference) : null;
ShowEditor(fieldType, @struct);
if (@struct != null)
{
field.SetValue(reference, @struct);
}
ImGui.TreePop();
}
}
else if (fieldType.IsSubclassOf(typeof(Entity)) || fieldType == typeof(Entity))
{
// TODO: Show entity drop target
ImGui.Text($"{field.Name} Entity ({fieldType.Name})");
}
else if (fieldType.IsSubclassOf(typeof(Component)))
{
// TODO: Show component drop target
ImGui.Text($"{field.Name} Component ({fieldType.Name})");
}
else
{
if (ImGui.TreeNode(field.Name))
{
// TODO: Button to create / destroy instance
object @class = (reference != null) ? field.GetValue(reference) : null;
ShowEditor(fieldType, @class);
ImGui.TreePop();
}
}
}
ImGui.PopID();
}
}
}
+10
View File
@@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Linq;
namespace GlitchyEngine.Extensions;
public static class EnumExtension
{
public static IEnumerable<(T item, int index)> WithIndex<T>(this IEnumerable<T> self)
=> self.Select((item, index) => (item, index));
}
+1
View File
@@ -18,6 +18,7 @@
<ItemGroup>
<ProjectReference Include="..\ScriptCoreGenerator\ScriptCoreGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\vendor\ImGui.NET\src\ImGui.NET\ImGui.NET.csproj" />
</ItemGroup>
</Project>
+13 -1
View File
@@ -3,7 +3,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.6.33723.286
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScriptCore", "ScriptCore.csproj", "{454CBEB0-2425-47B4-B75B-BE11EAF56E34}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCore", "ScriptCore.csproj", "{454CBEB0-2425-47B4-B75B-BE11EAF56E34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ScriptCoreGenerator", "..\ScriptCoreGenerator\ScriptCoreGenerator.csproj", "{5E8CE036-B41E-4382-AC53-7F9A39ED529D}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImGui.NET", "..\vendor\ImGui.NET\src\ImGui.NET\ImGui.NET.csproj", "{9D60C9CF-044C-4F2D-ADF0-B5A60BE3DADB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -15,6 +19,14 @@ Global
{454CBEB0-2425-47B4-B75B-BE11EAF56E34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{454CBEB0-2425-47B4-B75B-BE11EAF56E34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{454CBEB0-2425-47B4-B75B-BE11EAF56E34}.Release|Any CPU.Build.0 = Release|Any CPU
{5E8CE036-B41E-4382-AC53-7F9A39ED529D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E8CE036-B41E-4382-AC53-7F9A39ED529D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E8CE036-B41E-4382-AC53-7F9A39ED529D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E8CE036-B41E-4382-AC53-7F9A39ED529D}.Release|Any CPU.Build.0 = Release|Any CPU
{9D60C9CF-044C-4F2D-ADF0-B5A60BE3DADB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9D60C9CF-044C-4F2D-ADF0-B5A60BE3DADB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9D60C9CF-044C-4F2D-ADF0-B5A60BE3DADB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9D60C9CF-044C-4F2D-ADF0-B5A60BE3DADB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE