Files
OldThink/Content.Client/Interactable/Components/InteractionOutlineComponent.cs

67 lines
2.3 KiB
C#
Raw Normal View History

using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
2021-06-09 22:19:39 +02:00
namespace Content.Client.Interactable.Components
{
[RegisterComponent]
public sealed class InteractionOutlineComponent : Component
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
2021-12-08 12:09:43 +01:00
[Dependency] private readonly IEntityManager _entMan = default!;
2021-04-19 09:52:40 +02:00
private const float DefaultWidth = 1;
2020-04-22 17:50:55 +02:00
private const string ShaderInRange = "SelectionOutlineInrange";
private const string ShaderOutOfRange = "SelectionOutline";
2021-04-19 09:52:40 +02:00
private bool _inRange;
private ShaderInstance? _shader;
private int _lastRenderScale;
2021-04-19 09:52:40 +02:00
public void OnMouseEnter(bool inInteractionRange, int renderScale)
{
2021-04-19 09:52:40 +02:00
_lastRenderScale = renderScale;
_inRange = inInteractionRange;
2021-12-08 12:09:43 +01:00
if (_entMan.TryGetComponent(Owner, out ISpriteComponent? sprite))
{
2021-04-19 09:52:40 +02:00
sprite.PostShader = MakeNewShader(inInteractionRange, renderScale);
2021-12-08 12:09:43 +01:00
sprite.RenderOrder = _entMan.CurrentTick.Value;
}
}
public void OnMouseLeave()
{
2021-12-08 12:09:43 +01:00
if (_entMan.TryGetComponent(Owner, out ISpriteComponent? sprite))
{
sprite.PostShader = null;
sprite.RenderOrder = 0;
}
2021-04-19 09:52:40 +02:00
_shader?.Dispose();
_shader = null;
}
2021-04-19 09:52:40 +02:00
public void UpdateInRange(bool inInteractionRange, int renderScale)
{
2021-12-08 12:09:43 +01:00
if (_entMan.TryGetComponent(Owner, out ISpriteComponent? sprite)
2021-04-19 09:52:40 +02:00
&& (inInteractionRange != _inRange || _lastRenderScale != renderScale))
{
2021-04-19 09:52:40 +02:00
_inRange = inInteractionRange;
_lastRenderScale = renderScale;
_shader = MakeNewShader(_inRange, _lastRenderScale);
sprite.PostShader = _shader;
}
}
2021-04-19 09:52:40 +02:00
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;
}
}
}