From 57b42d8daace146bacd125ff9d06bdf34fdfd568 Mon Sep 17 00:00:00 2001 From: max Date: Tue, 22 Oct 2024 23:54:36 +0200 Subject: [PATCH] Really basic component inspection --- Nerfed.Editor/Program.cs | 5 +- .../Systems/EditorHierarchyWindow.cs | 1 + .../Systems/EditorInspectorWindow.cs | 56 +++++++++++++++++++ Nerfed.Runtime/Components/LocalTransform.cs | 15 ++--- 4 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 Nerfed.Editor/Systems/EditorInspectorWindow.cs diff --git a/Nerfed.Editor/Program.cs b/Nerfed.Editor/Program.cs index accbefc..dcf929b 100644 --- a/Nerfed.Editor/Program.cs +++ b/Nerfed.Editor/Program.cs @@ -30,6 +30,9 @@ private static void HandleOnInitialize() systems.Add(new LocalToWorldSystem(world)); editorSystems.Add(new EditorProfilerWindow(world)); editorSystems.Add(new EditorHierarchyWindow(world)); +#if DEBUG + editorSystems.Add(new EditorInspectorWindow(world)); +#endif Entity ent1 = world.CreateEntity("parent"); world.Set(ent1, new Root()); @@ -56,7 +59,7 @@ private static void HandleOnInitialize() Entity parent = newEnt; for (int j = 0; j < 2; j++) { Entity newChildEnt = world.CreateEntity(); - world.Set(newChildEnt, new LocalTransform(new Vector3(j, j, j), Quaternion.Identity, Vector3.One)); + world.Set(newChildEnt, new LocalTransform(new Vector3(i + j * i, i - j * i, j - i * i), Quaternion.Identity, Vector3.One)); Transform.SetParent(world, newChildEnt, parent); parent = newChildEnt; } diff --git a/Nerfed.Editor/Systems/EditorHierarchyWindow.cs b/Nerfed.Editor/Systems/EditorHierarchyWindow.cs index 4cbda6d..b19b8c5 100644 --- a/Nerfed.Editor/Systems/EditorHierarchyWindow.cs +++ b/Nerfed.Editor/Systems/EditorHierarchyWindow.cs @@ -96,6 +96,7 @@ private void DrawEntityAndChildren(in Entity entity) if (ImGui.TreeNodeEx($"{entity.ID} | {GetTag(entity)}", flags)) { + // TODO: fix selection, look at ImGui 1.91, https://github.com/ocornut/imgui/wiki/Multi-Select // Selection. if (ImGui.IsItemClicked() && !ImGui.IsItemToggledOpen()) { diff --git a/Nerfed.Editor/Systems/EditorInspectorWindow.cs b/Nerfed.Editor/Systems/EditorInspectorWindow.cs new file mode 100644 index 0000000..de899a6 --- /dev/null +++ b/Nerfed.Editor/Systems/EditorInspectorWindow.cs @@ -0,0 +1,56 @@ +using ImGuiNET; +using MoonTools.ECS; +using Nerfed.Editor.Components; + +#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().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 components = World.Debug_GetAllComponentTypes(entity); + + foreach (Type type in components) + { + ImGui.Text(type.Name); + } + + ImGui.Separator(); + + // TODO: explore something without reflection. + // Maybe generate some look up dictionary> for 'custom editors'. + // Look into serializing of entities and components. + foreach (Type component in components) + { + 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 \ No newline at end of file diff --git a/Nerfed.Runtime/Components/LocalTransform.cs b/Nerfed.Runtime/Components/LocalTransform.cs index 29207c0..bb0b810 100644 --- a/Nerfed.Runtime/Components/LocalTransform.cs +++ b/Nerfed.Runtime/Components/LocalTransform.cs @@ -2,18 +2,11 @@ namespace Nerfed.Runtime.Components { - public struct LocalTransform + public readonly record struct LocalTransform(Vector3 position, Quaternion rotation, Vector3 scale) { - public Vector3 position; - public Quaternion rotation; - public Vector3 scale; - - public LocalTransform(Vector3 position, Quaternion rotation, Vector3 scale) - { - this.position = position; - this.rotation = rotation; - this.scale = scale; - } + public readonly Vector3 position = position; + public readonly Quaternion rotation = rotation; + public readonly Vector3 scale = scale; public static readonly LocalTransform Identity = new(Vector3.Zero, Quaternion.Identity, Vector3.One); }