Nerfed/Nerfed.Editor/Systems/EditorInspectorWindow.cs
2024-11-02 21:59:35 +01:00

79 lines
2.7 KiB
C#

using System.Numerics;
using ImGuiNET;
using MoonTools.ECS;
using Nerfed.Editor.Components;
using Nerfed.Runtime.Serialization;
#if DEBUG
namespace Nerfed.Editor.Systems
{
// Window that draws entities.
internal class EditorInspectorWindow : MoonTools.ECS.DebugSystem
{
private readonly Filter selectedEntityFilter;
public EditorInspectorWindow(World world) : base(world)
{
selectedEntityFilter = FilterBuilder.Include<SelectedInHierachy>().Build();
}
public override void Update(TimeSpan delta)
{
ImGui.Begin("Inspector");
foreach (Entity entity in selectedEntityFilter.Entities)
{
DrawEntityComponents(entity);
}
ImGui.End();
}
private void DrawEntityComponents(Entity entity)
{
World.ComponentTypeEnumerator componentTypes = World.Debug_GetAllComponentTypes(entity);
ImGui.Text("ComponentInspectorByType");
foreach (Type componentType in componentTypes)
{
if (ComponentHelper.ComponentInspectorByType.TryGetValue(componentType, out Action<World, Entity> componentInspector))
{
componentInspector(World, entity);
}
else
{
ImGui.Text(componentType.Name);
}
ImGui.Separator();
}
ImGui.Dummy(new Vector2(16, 16));
ImGui.Text("GetComponentByType");
foreach (Type componentType in componentTypes)
{
if (!ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter)) continue;
ValueType component = componentGetter.Invoke(World, entity);
ImGui.Text(component.ToString());
ImGui.Separator();
}
ImGui.Dummy(new Vector2(16, 16));
ImGui.Text("Reflection");
// TODO: explore something without reflection.
// Maybe generate some look up dictionary<type, Action<world, entity>> for 'custom editors'.
// Look into serializing of entities and components.
foreach (Type component in componentTypes)
{
System.Reflection.MethodInfo getMethodInfo = typeof(World).GetMethod("Get");
System.Reflection.MethodInfo getComponentMethod = getMethodInfo.MakeGenericMethod(component);
object result = getComponentMethod.Invoke(World, [entity]);
// process here
ImGui.Text(result.ToString());
}
}
}
}
#endif