Files
RobustToolbox/Robust.Client/Debugging/DebugDrawingManager.cs
T
Acruid a5125a0561 Allows Overlays to draw to multiple spaces on the screen.
Removed Overlay.Shader property, you need to manage the field yourself and apply the shader before you draw.
Made all overlays compatible with the new changes.
2020-07-31 14:00:12 -07:00

113 lines
3.5 KiB
C#

using Robust.Client.Interfaces.Debugging;
using Robust.Shared.Interfaces.Network;
using Robust.Shared.IoC;
using Robust.Shared.Network.Messages;
using System;
using System.Collections.Generic;
using Robust.Client.Graphics.Overlays;
using Robust.Client.Graphics.Drawing;
using Robust.Shared.Maths;
using Robust.Client.Interfaces.Graphics.Overlays;
using Robust.Shared.Timing;
using Robust.Shared.Interfaces.Timing;
namespace Robust.Client.Debugging
{
internal class DebugDrawingManager : IDebugDrawingManager
{
[Dependency] private readonly IClientNetManager _net = default!;
[Dependency] private readonly IOverlayManager _overlayManager = default!;
[Dependency] private readonly IGameTiming _gameTimer = default!;
private readonly List<RayWithLifetime> raysWithLifeTime = new List<RayWithLifetime>();
private bool _debugDrawRays;
private struct RayWithLifetime
{
public Vector2 RayOrigin;
public Vector2 RayHit;
public TimeSpan LifeTime;
public bool DidActuallyHit;
}
public bool DebugDrawRays
{
get => _debugDrawRays;
set
{
if (value == DebugDrawRays)
{
return;
}
_debugDrawRays = value;
if (value)
{
_overlayManager.AddOverlay(new DebugDrawRayOverlay(this));
}
else
{
_overlayManager.RemoveOverlay(nameof(DebugDrawRayOverlay));
}
}
}
public TimeSpan DebugRayLifetime { get; set; } = TimeSpan.FromSeconds(5);
public void Initialize()
{
_net.RegisterNetMessage<MsgRay>(MsgRay.NAME, HandleDrawRay);
}
private void HandleDrawRay(MsgRay msg)
{
// Rays are disposed with DebugDrawRayOverlay.FrameUpdate.
// We don't accept incoming rays when that's off because else they'd pile up constantly.
if (!_debugDrawRays)
{
return;
}
var newRayWithLifetime = new RayWithLifetime
{
DidActuallyHit = msg.DidHit,
RayOrigin = msg.RayOrigin,
RayHit = msg.RayHit,
LifeTime = _gameTimer.RealTime + DebugRayLifetime
};
raysWithLifeTime.Add(newRayWithLifetime);
}
private sealed class DebugDrawRayOverlay : Overlay
{
private readonly DebugDrawingManager _owner;
public override OverlaySpace Space => OverlaySpace.WorldSpace;
public DebugDrawRayOverlay(DebugDrawingManager owner) : base(nameof(DebugDrawRayOverlay))
{
_owner = owner;
}
protected override void Draw(DrawingHandleBase handle, OverlaySpace currentSpace)
{
foreach (var ray in _owner.raysWithLifeTime)
{
handle.DrawLine(
ray.RayOrigin,
ray.RayHit,
ray.DidActuallyHit ? Color.Yellow : Color.Magenta);
}
}
protected internal override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
_owner.raysWithLifeTime.RemoveAll(r => r.LifeTime < _owner._gameTimer.RealTime);
}
}
}
}