Nerfed/Nerfed.Editor/Systems/EditorInspectorWindow.cs

100 lines
3.5 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);
2024-11-13 21:11:50 +01:00
// 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));
2024-11-02 21:59:35 +01:00
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);
}
2024-11-13 21:11:50 +01:00
else if (ComponentHelper.GetComponentByType.TryGetValue(componentType, out Func<World, Entity, ValueType> componentGetter))
{
ValueType component = componentGetter.Invoke(World, entity);
ImGui.Text(component.ToString());
}
2024-11-02 21:59:35 +01:00
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));
2024-10-22 23:54:36 +02:00
2024-11-13 21:11:50 +01:00
// 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());
// }
2024-10-22 23:54:36 +02:00
}
}
}
#endif