Nerfed/Nerfed.Editor/Systems/EditorInspectorWindow.cs

79 lines
2.7 KiB
C#
Raw Normal View History

2024-11-02 21:59:35 +01:00
using System.Numerics;
2024-10-22 23:54:36 +02:00
using ImGuiNET;
using MoonTools.ECS;
using Nerfed.Editor.Components;
2024-11-02 21:59:35 +01:00
using Nerfed.Runtime.Serialization;
2024-10-22 23:54:36 +02:00
#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)
{
2024-11-02 21:59:35 +01:00
World.ComponentTypeEnumerator componentTypes = World.Debug_GetAllComponentTypes(entity);
ImGui.Text("ComponentInspectorByType");
foreach (Type componentType in componentTypes)
2024-10-22 23:54:36 +02:00
{
2024-11-02 21:59:35 +01:00
if (ComponentHelper.ComponentInspectorByType.TryGetValue(componentType, out Action<World, Entity> componentInspector))
{
componentInspector(World, entity);
}
else
{
ImGui.Text(componentType.Name);
}
ImGui.Separator();
2024-10-22 23:54:36 +02:00
}
2024-11-02 21:59:35 +01:00
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));
2024-10-22 23:54:36 +02:00
2024-11-02 21:59:35 +01:00
ImGui.Text("Reflection");
2024-10-22 23:54:36 +02:00
// 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.
2024-11-02 21:59:35 +01:00
foreach (Type component in componentTypes)
2024-10-22 23:54:36 +02:00
{
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