Files
lingua-engine/plugins/engine.editor/Engine.Editor/EditorPlugin.cs
T
EmilandClaude Sonnet 5 20e0f8c53f M3: reflection-based Inspector — edit any component's fields live
InspectorPanel shows EditorState.Selected's Transform (Position/Scale as
draggable Vector3 widgets, Rotation shown read-only as a quaternion — a
real rotate control needs Euler conversion or a gizmo, out of scope for
just the Inspector) and, below it, every attached Component's public
fields via reflection. No per-component-type drawer code exists anywhere:
int/float/bool/string/Vector3 fields get a live-editable widget, anything
else falls back to a read-only ToString(), and a brand new component type
in any plugin gets an Inspector for free the moment it's attached — that
genericity is the entire reason to do this by reflection instead of a
registry.

Field edits write straight back through FieldInfo.SetValue, same gap
GameObject.AddComponent's doc comment already names: components are plain
fields with nothing to intercept a direct mutation, so this needs no
SystemAccessScope declaration any more than a hand-written `component.
Value = 5` would.

EditorPlugin now defaults EditorState.Selected to the first root once,
if nothing's been clicked yet — an empty Inspector on every fresh launch
had nothing useful to show. Verified by screenshot: Inspector displays
"Quad"'s real Position (0,0,0) and Scale (2,2,1), matching scene.json
exactly, plus a QuadRenderer header correctly showing "(no fields)" since
it has none yet.

Full suite still green: 65 tests.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
2026-09-02 16:21:26 +03:00

82 lines
2.8 KiB
C#

using Engine.Input.Contracts;
using Engine.Kernel.Diagnostics;
using Engine.Kernel.Plugins;
using Engine.Kernel.Scheduling;
using Engine.Kernel.World;
using Engine.Windowing.Contracts;
using ImGuiNET;
using Silk.NET.OpenGL;
using Silk.NET.OpenGL.Extensions.ImGui;
namespace Engine.Editor;
/// <summary>
/// M3's editor shell: an ImGui overlay drawn on top of the running scene.
/// Registers on Stage.Render, after engine.render's own Draw (per
/// project.json's plugin order) so it paints into the same back buffer the
/// scene just drew into — and before Stage.Present's SwapBuffers, so the
/// UI isn't delayed a frame. See Stage.Present's doc comment for why that
/// split exists at all.
///
/// Nothing here reads or writes a Component through GetComponent/Query —
/// HierarchyPanel walks IWorld.Roots/GameObject.Children directly, and the
/// not-yet-built Inspector will walk GameObject.Components the same way —
/// which is why this plugin never needs to declare Reads/Writes on its
/// system: those checks only guard the typed accessors, not plain property
/// reads. See GameObject.AddComponent's own doc comment on the same gap.
/// </summary>
public sealed class EditorPlugin : IPlugin
{
private readonly EditorState _state = new();
private GL? _gl;
private ImGuiController? _controller;
private ITime? _time;
public void Configure(IPluginContext ctx)
{
var window = ctx.Services.Require<IEngineWindow>();
var input = ctx.Services.Require<IEngineInput>();
_time = ctx.Time;
window.Native.GLContext!.MakeCurrent();
_gl = window.Native.CreateOpenGL();
_controller = new ImGuiController(_gl, window.Native, input.Native);
ctx.Schedule.Add(Stage.Render, DrawUi);
ctx.Log.Info("editor UI ready (ImGui)");
}
public void Shutdown(IPluginContext ctx)
{
ctx.Schedule.RemoveAllFrom("engine.editor");
_controller?.Dispose();
_gl?.Dispose();
_controller = null;
_gl = null;
_time = null;
}
private void DrawUi(IWorld world)
{
_controller!.Update(_time!.DeltaTime);
// Nothing selected yet and there's something to select: default to
// the first root rather than opening on an empty, useless
// Inspector. Only fires once — any real click overwrites it, and
// it never fights a deliberate deselect because there's no way to
// deselect yet.
if (_state.Selected is null && world.Roots.Count > 0)
_state.Selected = world.Roots[0];
ImGui.Begin("Lingua Editor");
ImGui.Text($"FPS: {1f / MathF.Max(_time.DeltaTime, 0.0001f):F0}");
ImGui.Text($"Frame: {_time.FrameCount}");
ImGui.End();
HierarchyPanel.Draw(world, _state);
InspectorPanel.Draw(_state);
_controller.Render();
}
}