Nerfed/Nerfed.Editor/Systems/EditorInspectorWindow.cs
2024-11-13 21:11:50 +01:00

100 lines
3.5 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);
// Add button of all types that we can add. Also filter out types we already have.
List<Type> componentTypesToAdd = ComponentHelper.AddComponentByType.Keys.ToList();
foreach (Type componentType in componentTypes)
{
componentTypesToAdd.Remove(componentType);
}
const string popupId = "AddComponentPopup";
if (ImGui.Button("Add Component"))
{
ImGui.OpenPopup(popupId);
}
if (ImGui.BeginPopup(popupId))
{
foreach (Type componentType in componentTypesToAdd)
{
if (ImGui.Selectable(componentType.Name))
{
if (ComponentHelper.AddComponentByType.TryGetValue(componentType, out Action<World, Entity> componentSetter))
{
componentSetter.Invoke(World, entity);
}
}
}
ImGui.EndPopup();
}
ImGui.Dummy(new Vector2(16, 16));
ImGui.Text("ComponentInspectorByType");
foreach (Type componentType in componentTypes)
{
if (ComponentHelper.ComponentInspectorByType.TryGetValue(componentType, out Action<World, Entity> componentInspector))
{
componentInspector(World, entity);
}
else if (ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter))
{
ValueType component = componentGetter.Invoke(World, entity);
ImGui.Text(component.ToString());
}
else
{
ImGui.Text(componentType.Name);
}
ImGui.Separator();
}
ImGui.Dummy(new Vector2(16, 16));
// ImGui.Text("Reflection");
// 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