TranslateGizmo draws three axis handles at the selected GameObject's world position by projecting real 3D points through the real camera's View/Projection (GizmoMath.WorldToScreen) onto ImGui's foreground draw list. Nothing OpenGL-side draws lines yet, so this isn't 3D geometry in the GL sense — but it IS driven by the actual camera matrices, which is what the earlier scoped-2D-vs-full-3D-pipeline choice was actually about: a handle dragged in screen space has to map onto a real 3D axis, and that only means something once there's a real camera to project through. The screenshot below shows exactly that — the axes aren't screen-perpendicular, because the camera at (0,3,6) looking at the origin means they shouldn't be. Dragging a handle re-projects the mouse delta onto the axis's own screen-space direction (GizmoMath.ProjectDragOntoAxis) and writes the result back through GizmoMath.WorldToLocalPosition, which inverts the parent's WorldMatrix rather than writing LocalPosition directly — a parent with non-identity scale or rotation means "move 1 world unit" and "add 1 to LocalPosition" are different amounts, and samples/WindowDemo's ChildQuad (parented under a (2,2,1)-scaled Quad) is exactly that case. Split the actual math into GizmoMath (Engine.Editor.Contracts, no GL/ ImGui/mouse dependency) so it's unit-testable without a window or a real mouse — neither exists in a headless test run, and dragging is exactly the kind of interaction that's easy to get subtly wrong (screen-space ratio direction, perspective sign, parent-scale correctness) without something to check it against beyond eyeballing a screenshot. New Engine.Editor.Tests project, 8 tests: screen-center projection, a behind-camera point returning null, drag-ratio math on both an axis-aligned and a diagonal screen direction, and the parent-scale/ parent-translation inverse-transform cases. All 8 passed on the first run — including the non-uniform-scale case, the one most likely to be subtly wrong. Full suite: 73 tests, all green (65 previous + 8 new). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N1qPfzq8TDCUMFMV3UwV5N
117 lines
4.2 KiB
C#
117 lines
4.2 KiB
C#
using Engine.Editor.Contracts;
|
|
using Engine.Input.Contracts;
|
|
using Engine.Kernel.Diagnostics;
|
|
using Engine.Kernel.Plugins;
|
|
using Engine.Kernel.Scheduling;
|
|
using Engine.Kernel.World;
|
|
using Engine.Render.Contracts;
|
|
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 readonly TranslateGizmo _gizmo = new();
|
|
private GL? _gl;
|
|
private ImGuiController? _controller;
|
|
private ITime? _time;
|
|
private PlayModeController? _playMode;
|
|
private IEngineWindow? _window;
|
|
private ICameraService? _camera;
|
|
|
|
public void Configure(IPluginContext ctx)
|
|
{
|
|
var window = ctx.Services.Require<IEngineWindow>();
|
|
var input = ctx.Services.Require<IEngineInput>();
|
|
_time = ctx.Time;
|
|
_window = window;
|
|
_camera = ctx.Services.Require<ICameraService>();
|
|
|
|
window.Native.GLContext!.MakeCurrent();
|
|
_gl = window.Native.CreateOpenGL();
|
|
_controller = new ImGuiController(_gl, window.Native, input.Native);
|
|
|
|
_playMode = new PlayModeController(ctx.World, ctx.Log);
|
|
ctx.Services.Provide<IPlayModeController>(_playMode);
|
|
|
|
ctx.Schedule.Add(Stage.Render, DrawUi);
|
|
ctx.Log.Info("editor UI ready (ImGui)");
|
|
}
|
|
|
|
public void Shutdown(IPluginContext ctx)
|
|
{
|
|
ctx.Schedule.RemoveAllFrom("engine.editor");
|
|
ctx.Services.Revoke<IPlayModeController>();
|
|
_controller?.Dispose();
|
|
_gl?.Dispose();
|
|
_controller = null;
|
|
_gl = null;
|
|
_time = null;
|
|
_playMode = null;
|
|
_window = null;
|
|
_camera = 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];
|
|
|
|
// FirstUseEver, not every frame: a real editor session lets the
|
|
// user drag panels wherever they want, and re-forcing a position
|
|
// every frame would fight that the moment they did. This only
|
|
// picks a sane, non-overlapping default before ImGui has ever
|
|
// seen these windows (or after Reset Layout, once that exists).
|
|
ImGui.SetNextWindowPos(new(10, 10), ImGuiCond.FirstUseEver);
|
|
ImGui.SetNextWindowSize(new(220, 90), ImGuiCond.FirstUseEver);
|
|
ImGui.Begin("Lingua Editor");
|
|
ImGui.Text($"FPS: {1f / MathF.Max(_time.DeltaTime, 0.0001f):F0}");
|
|
ImGui.Text($"Frame: {_time.FrameCount}");
|
|
ImGui.Separator();
|
|
|
|
if (ImGui.Button(_playMode!.IsPlaying ? "Stop" : "Play"))
|
|
{
|
|
if (_playMode.IsPlaying)
|
|
_playMode.ExitPlay();
|
|
else
|
|
_playMode.EnterPlay();
|
|
}
|
|
|
|
ImGui.SameLine();
|
|
ImGui.Text(_playMode.IsPlaying ? "(Playing)" : "(Edit mode)");
|
|
ImGui.End();
|
|
|
|
HierarchyPanel.Draw(world, _state);
|
|
InspectorPanel.Draw(_state);
|
|
_gizmo.Draw(_state, _camera!, _window!);
|
|
|
|
_controller.Render();
|
|
}
|
|
}
|