CatStation/Content.Client/DoAfter/DoAfterOverlay.cs
metalgearsloth 408ac85084
Stop overlays resetting world transforms (#45547)
* Stop overlays resetting world transforms

* oop
2026-10-05 17:43:35 +11:00

183 lines
8 KiB
C#

using System.Numerics;
using Content.Shared.DoAfter;
using Content.Client.UserInterface.Systems;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Enums;
using Robust.Client.Player;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using Robust.Shared.Containers;
namespace Content.Client.DoAfter;
public sealed class DoAfterOverlay : Overlay
{
private static readonly ProtoId<ShaderPrototype> UnshadedShader = "unshaded";
private readonly IEntityManager _entManager;
private readonly IGameTiming _timing;
private readonly IPlayerManager _player;
private readonly TransformSystem _transform;
private readonly MetaDataSystem _meta;
private readonly ProgressColorSystem _progressColor;
private readonly SharedContainerSystem _container;
private readonly SpriteSystem _sprite;
private readonly Texture _barTexture;
private readonly ShaderInstance _unshadedShader;
/// <summary>
/// Flash time for cancelled DoAfters
/// </summary>
private const float FlashTime = 0.125f;
// Hardcoded width of the progress bar because it doesn't match the texture.
private const float StartX = 2;
private const float EndX = 22f;
// Time after which the doafter will lerp to max alpha.
private static readonly TimeSpan MaxAlphaTime = TimeSpan.FromSeconds(0.3f);
// After finishing, how long it takes to fade out to 0 alpha
private static readonly TimeSpan FadeoutAlphaTime = TimeSpan.FromSeconds(0.2f);
// Time after which the doafter will lerp to its final y offset.
private static readonly TimeSpan MaxYPosTime = TimeSpan.FromSeconds(0.5f);
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowFOV;
public DoAfterOverlay(IEntityManager entManager, IPrototypeManager protoManager, IGameTiming timing, IPlayerManager player)
{
_entManager = entManager;
_timing = timing;
_player = player;
_transform = _entManager.EntitySysManager.GetEntitySystem<TransformSystem>();
_meta = _entManager.EntitySysManager.GetEntitySystem<MetaDataSystem>();
_container = _entManager.EntitySysManager.GetEntitySystem<SharedContainerSystem>();
_progressColor = _entManager.System<ProgressColorSystem>();
_sprite = _entManager.System<SpriteSystem>();
var sprite = new SpriteSpecifier.Rsi(new("/Textures/Interface/Misc/progress_bar.rsi"), "icon");
_barTexture = _entManager.EntitySysManager.GetEntitySystem<SpriteSystem>().Frame0(sprite);
_unshadedShader = protoManager.Index(UnshadedShader).Instance();
}
protected override void Draw(in OverlayDrawArgs args)
{
var handle = args.WorldHandle;
var rotation = args.Viewport.Eye?.Rotation ?? Angle.Zero;
// If you use the display UI scale then need to set max(1f, displayscale) because 0 is valid.
const float scale = 1f;
var scaleMatrix = Matrix3Helpers.CreateScale(new Vector2(scale, scale));
var rotationMatrix = Matrix3Helpers.CreateRotation(-rotation);
var curTime = _timing.CurTime;
var bounds = args.WorldAABB.Enlarged(5f);
var localEnt = _player.LocalSession?.AttachedEntity;
var metaQuery = _entManager.GetEntityQuery<MetaDataComponent>();
var enumerator = _entManager.AllEntityQueryEnumerator<ActiveDoAfterComponent, DoAfterComponent, SpriteComponent, TransformComponent>();
while (enumerator.MoveNext(out var uid, out _, out var comp, out var sprite, out var xform))
{
if (xform.MapID != args.MapId)
continue;
if (comp.DoAfters.Count == 0)
continue;
var worldPosition = _transform.GetRenderWorldPosition((uid, xform));
if (!bounds.Contains(worldPosition))
continue;
// shades the do-after bar if the do-after bar belongs to other players
// does not shade do-afters belonging to the local player
if (uid != localEnt)
handle.UseShader(null);
else
handle.UseShader(_unshadedShader);
// If the entity is paused, we will draw the do-after as it was when the entity got paused.
var meta = metaQuery.GetComponent(uid);
var time = meta.EntityPaused
? curTime - _meta.GetPauseTime(uid, meta)
: curTime;
var worldMatrix = Matrix3Helpers.CreateTranslation(worldPosition);
var scaledWorld = Matrix3x2.Multiply(scaleMatrix, worldMatrix);
var matty = Matrix3x2.Multiply(rotationMatrix, scaledWorld);
handle.SetTransform(matty);
var offset = 0f;
var isInContainer = _container.IsEntityOrParentInContainer(uid, meta, xform);
foreach (var doAfter in comp.DoAfters.Values)
{
// Hide some DoAfters from other players for stealthy actions (ie: thieving gloves)
var maxAlpha = 1f;
if (doAfter.Args.Hidden || isInContainer)
{
if (uid != localEnt)
continue;
// Hints to the local player that this do-after is not visible to other players.
maxAlpha = 0.5f;
}
var elapsed = time - doAfter.StartTime;
var alpha = MathHelper.Lerp(0f, maxAlpha, (float)Math.Clamp(elapsed / MaxAlphaTime, 0.0, 1.0));
// fade out if doafter finished
if (elapsed >= doAfter.Args.Delay)
alpha = MathHelper.Lerp(maxAlpha, 0f, (float)Math.Clamp((elapsed - doAfter.Args.Delay) / FadeoutAlphaTime, 0.0, 1.0));
// Use the sprite itself if we know its bounds. This means short or tall sprites don't get overlapped
// by the bar.
var spriteBounds = _sprite.GetLocalBounds((uid, sprite));
var yFinished = spriteBounds.Height / 2f + 0.05f;
var yStart = yFinished / 6f;
var yOffset = MathHelper.Lerp(yStart, yFinished, Easings.OutSine((float)Math.Clamp(elapsed / MaxYPosTime, 0.0, 1.0)));
// Position above the entity (we've already applied the matrix transform to the entity itself)
// Offset by the texture size for every do_after we have.
var position = new Vector2(-_barTexture.Width / 2f / EyeManager.PixelsPerMeter,
yOffset / scale + offset / EyeManager.PixelsPerMeter * scale);
// Draw the underlying bar texture
handle.DrawTexture(_barTexture, position, Color.White.WithAlpha(alpha));
Color color;
float elapsedRatio;
// if we're cancelled then flick red / off.
if (doAfter.CancelledTime != null)
{
elapsed = doAfter.CancelledTime.Value - doAfter.StartTime;
elapsedRatio = (float)Math.Min(1, elapsed.TotalSeconds / doAfter.Args.Delay.TotalSeconds);
var cancelElapsed = (time - doAfter.CancelledTime.Value).TotalSeconds;
var flash = Math.Floor(cancelElapsed / FlashTime) % 2 == 0;
color = GetProgressColor(0, flash ? alpha : 0);
}
else
{
elapsedRatio = (float)Math.Min(1, elapsed.TotalSeconds / doAfter.Args.Delay.TotalSeconds);
color = GetProgressColor(elapsedRatio, alpha);
}
var xProgress = (EndX - StartX) * elapsedRatio + StartX;
var box = new Box2(new Vector2(StartX, 3f) / EyeManager.PixelsPerMeter, new Vector2(xProgress, 4f) / EyeManager.PixelsPerMeter);
box = box.Translated(position);
handle.DrawRect(box, color);
offset += _barTexture.Height / scale;
}
}
}
public Color GetProgressColor(float progress, float alpha = 1f)
{
return _progressColor.GetProgressColor(progress).WithAlpha(alpha);
}
}