2023-07-08 14:08:32 +10:00
|
|
|
using System.Numerics;
|
2021-06-09 22:19:39 +02:00
|
|
|
using Content.Client.Viewport;
|
2021-03-09 04:33:41 -06:00
|
|
|
using Robust.Client.Graphics;
|
2021-04-19 09:52:40 +02:00
|
|
|
using Robust.Client.State;
|
2022-09-20 11:49:02 +12:00
|
|
|
using Robust.Client.Player;
|
2021-03-09 04:33:41 -06:00
|
|
|
using Robust.Shared.Enums;
|
2023-09-11 19:18:06 +10:00
|
|
|
using Robust.Shared.Graphics;
|
2020-07-06 16:37:39 -05:00
|
|
|
using Robust.Shared.IoC;
|
|
|
|
|
using Robust.Shared.Maths;
|
|
|
|
|
using Robust.Shared.Prototypes;
|
2021-02-11 01:13:03 -08:00
|
|
|
using Robust.Shared.Timing;
|
2020-07-06 16:37:39 -05:00
|
|
|
using SixLabors.ImageSharp.PixelFormats;
|
|
|
|
|
|
2021-06-09 22:19:39 +02:00
|
|
|
namespace Content.Client.Flash
|
2020-07-06 16:37:39 -05:00
|
|
|
{
|
2022-02-16 00:23:23 -07:00
|
|
|
public sealed class FlashOverlay : Overlay
|
2020-07-06 16:37:39 -05:00
|
|
|
{
|
2020-08-24 14:10:28 +02:00
|
|
|
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
|
|
|
|
[Dependency] private readonly IClyde _displayManager = default!;
|
|
|
|
|
[Dependency] private readonly IGameTiming _gameTiming = default!;
|
2021-04-19 09:52:40 +02:00
|
|
|
[Dependency] private readonly IStateManager _stateManager = default!;
|
2022-09-20 11:49:02 +12:00
|
|
|
[Dependency] private readonly IEntityManager _entityManager = default!;
|
|
|
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
2020-07-06 16:37:39 -05:00
|
|
|
|
2023-07-31 00:41:24 -04:00
|
|
|
public override OverlaySpace Space => OverlaySpace.WorldSpace;
|
2020-07-31 14:01:34 -07:00
|
|
|
private readonly ShaderInstance _shader;
|
2021-01-24 08:17:45 +00:00
|
|
|
private double _startTime = -1;
|
|
|
|
|
private double _lastsFor = 1;
|
2021-03-10 14:48:29 +01:00
|
|
|
private Texture? _screenshotTexture;
|
2020-07-06 16:37:39 -05:00
|
|
|
|
2021-03-09 04:33:41 -06:00
|
|
|
public FlashOverlay()
|
2020-07-06 16:37:39 -05:00
|
|
|
{
|
|
|
|
|
IoCManager.InjectDependencies(this);
|
2020-07-31 14:01:34 -07:00
|
|
|
_shader = _prototypeManager.Index<ShaderPrototype>("FlashedEffect").Instance().Duplicate();
|
2021-01-24 08:17:45 +00:00
|
|
|
}
|
2020-07-06 16:37:39 -05:00
|
|
|
|
2021-01-24 08:17:45 +00:00
|
|
|
public void ReceiveFlash(double duration)
|
|
|
|
|
{
|
2021-04-19 09:52:40 +02:00
|
|
|
if (_stateManager.CurrentState is IMainViewportState state)
|
2020-07-06 16:37:39 -05:00
|
|
|
{
|
2021-04-19 09:52:40 +02:00
|
|
|
state.Viewport.Viewport.Screenshot(image =>
|
|
|
|
|
{
|
2021-06-09 22:19:39 +02:00
|
|
|
var rgba32Image = image.CloneAs<Rgba32>(SixLabors.ImageSharp.Configuration.Default);
|
2021-04-19 09:52:40 +02:00
|
|
|
_screenshotTexture = _displayManager.LoadTextureFromImage(rgba32Image);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-24 08:17:45 +00:00
|
|
|
_startTime = _gameTiming.CurTime.TotalSeconds;
|
|
|
|
|
_lastsFor = duration;
|
2020-07-06 16:37:39 -05:00
|
|
|
}
|
|
|
|
|
|
2021-04-19 09:52:40 +02:00
|
|
|
protected override void Draw(in OverlayDrawArgs args)
|
2020-07-06 16:37:39 -05:00
|
|
|
{
|
2024-02-13 22:48:39 +01:00
|
|
|
if (!_entityManager.TryGetComponent(_playerManager.LocalEntity, out EyeComponent? eyeComp))
|
2022-09-20 11:49:02 +12:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (args.Viewport.Eye != eyeComp.Eye)
|
|
|
|
|
return;
|
|
|
|
|
|
2021-01-24 08:17:45 +00:00
|
|
|
var percentComplete = (float) ((_gameTiming.CurTime.TotalSeconds - _startTime) / _lastsFor);
|
|
|
|
|
if (percentComplete >= 1.0f)
|
|
|
|
|
return;
|
2020-07-06 16:37:39 -05:00
|
|
|
|
2023-07-31 00:41:24 -04:00
|
|
|
var worldHandle = args.WorldHandle;
|
|
|
|
|
worldHandle.UseShader(_shader);
|
2021-04-19 09:52:40 +02:00
|
|
|
_shader.SetParameter("percentComplete", percentComplete);
|
|
|
|
|
|
2020-07-06 16:37:39 -05:00
|
|
|
if (_screenshotTexture != null)
|
|
|
|
|
{
|
2023-07-31 00:41:24 -04:00
|
|
|
worldHandle.DrawTextureRectRegion(_screenshotTexture, args.WorldBounds);
|
2020-07-06 16:37:39 -05:00
|
|
|
}
|
2022-08-13 10:58:53 +10:00
|
|
|
|
2023-07-31 00:41:24 -04:00
|
|
|
worldHandle.UseShader(null);
|
2020-07-06 16:37:39 -05:00
|
|
|
}
|
|
|
|
|
|
2021-03-09 04:33:41 -06:00
|
|
|
protected override void DisposeBehavior()
|
2020-07-06 16:37:39 -05:00
|
|
|
{
|
2022-12-19 03:06:16 +01:00
|
|
|
base.DisposeBehavior();
|
2020-07-06 16:37:39 -05:00
|
|
|
_screenshotTexture = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|