M3: Hierarchy panel — click a GameObject to select it
HierarchyPanel walks IWorld.Roots/GameObject.Children recursively as ImGui tree nodes, clicking one sets EditorState.Selected — the shared selection the not-yet-built Inspector panel will read from the same instance. PushID(go.GetHashCode()) scopes each node's ID by object identity rather than name, since nothing stops two sibling GameObjects sharing a Name and ImGui's default label-based IDs would otherwise merge their open/selected state. Also gives samples/WindowDemo/scene.json a child GameObject (offset, half-scale, parented under Quad) — the existing scene only had one root, nothing to show a tree with. Verified by screenshot: both quads render at their correct composed WorldMatrix (the child visibly smaller and offset, confirming parent/child composition is still correct through this change), and Hierarchy lists "Quad" as a collapsible node. Full suite still green: 65 tests. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
This commit is contained in:
@@ -19,14 +19,15 @@ namespace Engine.Editor;
|
|||||||
/// split exists at all.
|
/// split exists at all.
|
||||||
///
|
///
|
||||||
/// Nothing here reads or writes a Component through GetComponent/Query —
|
/// Nothing here reads or writes a Component through GetComponent/Query —
|
||||||
/// the Hierarchy and Inspector panels (not built yet) will walk
|
/// HierarchyPanel walks IWorld.Roots/GameObject.Children directly, and the
|
||||||
/// IWorld.Roots and GameObject.Components directly instead, which is why
|
/// not-yet-built Inspector will walk GameObject.Components the same way —
|
||||||
/// this plugin never needs to declare Reads/Writes on its system: those
|
/// which is why this plugin never needs to declare Reads/Writes on its
|
||||||
/// checks only guard the typed accessors, not plain property reads. See
|
/// system: those checks only guard the typed accessors, not plain property
|
||||||
/// GameObject.AddComponent's own doc comment on the same gap.
|
/// reads. See GameObject.AddComponent's own doc comment on the same gap.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public sealed class EditorPlugin : IPlugin
|
public sealed class EditorPlugin : IPlugin
|
||||||
{
|
{
|
||||||
|
private readonly EditorState _state = new();
|
||||||
private GL? _gl;
|
private GL? _gl;
|
||||||
private ImGuiController? _controller;
|
private ImGuiController? _controller;
|
||||||
private ITime? _time;
|
private ITime? _time;
|
||||||
@@ -64,6 +65,8 @@ public sealed class EditorPlugin : IPlugin
|
|||||||
ImGui.Text($"Frame: {_time.FrameCount}");
|
ImGui.Text($"Frame: {_time.FrameCount}");
|
||||||
ImGui.End();
|
ImGui.End();
|
||||||
|
|
||||||
|
HierarchyPanel.Draw(world, _state);
|
||||||
|
|
||||||
_controller.Render();
|
_controller.Render();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
using Engine.Kernel.World;
|
||||||
|
|
||||||
|
namespace Engine.Editor;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Shared, per-session editor state — currently just which GameObject the
|
||||||
|
/// Hierarchy panel last clicked, so the Inspector panel (built against this
|
||||||
|
/// same instance) knows what to show. One instance per EditorPlugin, not
|
||||||
|
/// static: reloading engine.editor should start with nothing selected, not
|
||||||
|
/// hold a reference to a GameObject that may not even exist anymore.
|
||||||
|
/// </summary>
|
||||||
|
internal sealed class EditorState
|
||||||
|
{
|
||||||
|
public GameObject? Selected { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
using Engine.Kernel.World;
|
||||||
|
using ImGuiNET;
|
||||||
|
|
||||||
|
namespace Engine.Editor;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Walks IWorld.Roots/GameObject.Children directly — not Query<T>(),
|
||||||
|
/// there's no component type to query for here — so, like EditorPlugin
|
||||||
|
/// itself, this never touches SystemAccessScope and needs no declared
|
||||||
|
/// Reads/Writes.
|
||||||
|
/// </summary>
|
||||||
|
internal static class HierarchyPanel
|
||||||
|
{
|
||||||
|
public static void Draw(IWorld world, EditorState state)
|
||||||
|
{
|
||||||
|
ImGui.Begin("Hierarchy");
|
||||||
|
|
||||||
|
foreach (var root in world.Roots)
|
||||||
|
DrawNode(root, state);
|
||||||
|
|
||||||
|
ImGui.End();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void DrawNode(GameObject go, EditorState state)
|
||||||
|
{
|
||||||
|
// PushID/PopID, not relying on go.Name for identity: sibling
|
||||||
|
// GameObjects can share a name (nothing stops it), and ImGui's
|
||||||
|
// default ID-from-label would then merge their open/selected state.
|
||||||
|
ImGui.PushID(go.GetHashCode());
|
||||||
|
|
||||||
|
var flags = ImGuiTreeNodeFlags.OpenOnArrow | ImGuiTreeNodeFlags.SpanAvailWidth;
|
||||||
|
if (go.Children.Count == 0)
|
||||||
|
flags |= ImGuiTreeNodeFlags.Leaf | ImGuiTreeNodeFlags.NoTreePushOnOpen;
|
||||||
|
if (ReferenceEquals(state.Selected, go))
|
||||||
|
flags |= ImGuiTreeNodeFlags.Selected;
|
||||||
|
|
||||||
|
var open = ImGui.TreeNodeEx(go.Name, flags);
|
||||||
|
if (ImGui.IsItemClicked())
|
||||||
|
state.Selected = go;
|
||||||
|
|
||||||
|
if (open && go.Children.Count > 0)
|
||||||
|
{
|
||||||
|
foreach (var child in go.Children)
|
||||||
|
DrawNode(child, state);
|
||||||
|
ImGui.TreePop();
|
||||||
|
}
|
||||||
|
|
||||||
|
ImGui.PopID();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -9,6 +9,19 @@
|
|||||||
"components": [
|
"components": [
|
||||||
{ "type": "Engine.Render.Contracts.QuadRenderer, Engine.Render.Contracts", "data": {} }
|
{ "type": "Engine.Render.Contracts.QuadRenderer, Engine.Render.Contracts", "data": {} }
|
||||||
],
|
],
|
||||||
"children": []
|
"children": [
|
||||||
|
{
|
||||||
|
"name": "ChildQuad",
|
||||||
|
"transform": {
|
||||||
|
"position": [1.5, 0, 0],
|
||||||
|
"rotation": [0, 0, 0, 1],
|
||||||
|
"scale": [0.5, 0.5, 1]
|
||||||
|
},
|
||||||
|
"components": [
|
||||||
|
{ "type": "Engine.Render.Contracts.QuadRenderer, Engine.Render.Contracts", "data": {} }
|
||||||
|
],
|
||||||
|
"children": []
|
||||||
|
}
|
||||||
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user