Files
wwdpublic/Content.Client/Interactable/Components/InteractionOutlineComponent.cs
RedFoxIV 6d4215b08d dollar store spookston (#258)
* initial sidestream port

* ru locale

* blyatison

* упс

* jannie qol (#6)

* initial sidestream port

* blyadison

* cs1.4 (#4)

* initial sidestream port
* blyatison

* antitryaska (#7)

* initial sidestream port (still fucked though)

* blyatison

* o fugg (#8) speedmerge

* o fugg

* fugg :-DDD

* attempt numero uno (#9)

* fix desword sound (#10)

* раз уж я тут сижу

* whoops

* shit

---------

Co-authored-by: Spatison <137375981+Spatison@users.noreply.github.com>
2025-03-03 14:29:21 +02:00

73 lines
2.6 KiB
C#

using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Prototypes;
namespace Content.Client.Interactable.Components
{
[RegisterComponent]
public sealed partial class InteractionOutlineComponent : Component
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEntityManager _entMan = default!;
private const float DefaultWidth = 1;
[ValidatePrototypeId<ShaderPrototype>]
private const string ShaderInRange = "SelectionOutlineInrange";
[ValidatePrototypeId<ShaderPrototype>]
private const string ShaderOutOfRange = "SelectionOutline";
private bool _inRange;
private ShaderInstance? _shader;
private int _lastRenderScale;
public void OnMouseEnter(EntityUid uid, bool inInteractionRange, int renderScale)
{
_lastRenderScale = renderScale;
_inRange = inInteractionRange;
if (_entMan.TryGetComponent(uid, out SpriteComponent? sprite) && sprite.PostShader == null)
{
// TODO why is this creating a new instance of the outline shader every time the mouse enters???
_shader = MakeNewShader(inInteractionRange, renderScale);
sprite.PostShader = _shader;
}
}
public void OnMouseLeave(EntityUid uid)
{
if (_entMan.TryGetComponent(uid, out SpriteComponent? sprite))
{
if (sprite.PostShader == _shader)
sprite.PostShader = null;
}
_shader?.Dispose();
_shader = null;
}
public void UpdateInRange(EntityUid uid, bool inInteractionRange, int renderScale)
{
if (_entMan.TryGetComponent(uid, out SpriteComponent? sprite)
&& sprite.PostShader == _shader
&& (inInteractionRange != _inRange || _lastRenderScale != renderScale))
{
_inRange = inInteractionRange;
_lastRenderScale = renderScale;
_shader = MakeNewShader(_inRange, _lastRenderScale);
sprite.PostShader = _shader;
}
}
private ShaderInstance MakeNewShader(bool inRange, int renderScale)
{
var shaderName = inRange ? ShaderInRange : ShaderOutOfRange;
var instance = _prototypeManager.Index<ShaderPrototype>(shaderName).InstanceUnique();
instance.SetParameter("outline_width", DefaultWidth * renderScale);
return instance;
}
}
}