Files
mrgameeng/src/MrGameEng.UI/SceneUiExtensions.cs
T
Leonid Pershin fd6343bd09
CI / build-test (push) Failing after 1m8s
@
Add MrGameEng.AI utility-AI module; format codebase with CSharpier

New MrGameEng.AI module (ResponseCurve, Consideration, UtilityAction,
UtilityAi selector, Blackboard) plus CSharpier formatting applied across
the whole engine. Documents the CSharpier convention in CLAUDE.md.
@
2026-06-12 07:19:10 +03:00

75 lines
2.6 KiB
C#

using Friflo.Engine.ECS.Systems;
using Microsoft.Xna.Framework;
using MrGameEng.Core;
using Myra;
using Myra.Graphics2D.UI;
namespace MrGameEng.UI;
/// <summary>
/// Draw system rendering the scene's Myra <see cref="Desktop"/>. Must run after the scene's
/// world rendering (register UI last in the draw phase) — the UI is drawn on top in window
/// pixels and also processes mouse/keyboard interaction during render. While
/// <see cref="InputCapture.Captured"/> is set (developer console open), the desktop is drawn
/// without processing input, so overlay clicks and keystrokes do not fall through to it.
/// </summary>
public sealed class UiRenderSystem : BaseSystem
{
private readonly Desktop _desktop;
private readonly InputCapture? _capture;
/// <summary>Creates the system for <paramref name="desktop"/>.</summary>
public UiRenderSystem(Desktop desktop, InputCapture? capture = null)
{
_desktop = desktop;
_capture = capture;
}
/// <inheritdoc />
protected override void OnUpdateGroup()
{
if (_capture?.Captured == true)
{
// Render() = UpdateInput + UpdateLayout + RenderVisual; пропускаем только ввод.
_desktop.UpdateLayout();
_desktop.RenderVisual();
}
else
{
_desktop.Render();
}
}
}
/// <summary>Wires the UI module (Myra) into a <see cref="Scene"/>.</summary>
public static class SceneUiExtensions
{
/// <summary>
/// Creates a Myra <see cref="Desktop"/> for this scene and registers
/// <see cref="UiRenderSystem"/> in the draw phase. Call from <c>OnLoad</c>
/// <b>after</b> <c>UseRenderer2D()</c> so the UI draws on top of the world.
/// Build the UI by assigning <see cref="Desktop.Root"/>. The desktop is disposed
/// automatically when the scene unloads.
/// </summary>
public static Desktop UseUI(this Scene scene)
{
var services = scene.Context.Services;
// Присваивание идемпотентно; геттер MyraEnvironment.Game бросает исключение, пока
// Game не задан, поэтому проверять текущее значение перед записью нельзя.
MyraEnvironment.Game = services.Get<Game>();
var capture = services.GetOrDefault<InputCapture>();
if (capture is null)
{
capture = new InputCapture();
services.Add(capture);
}
var desktop = new Desktop();
scene.RegisterUnload(desktop.Dispose);
scene.DrawSystems.Add(new UiRenderSystem(desktop, capture));
return desktop;
}
}