Files
RobustToolbox/Robust.Client/GameObjects/Components/Input/ClickableComponent.cs
ComicIronic 486fd670e2 Change DrawDepth to int, add content constant hook (#1090)
This allows DrawDepth to be defined in the content layer.
2020-05-31 00:40:48 +02:00

73 lines
2.2 KiB
C#

using Robust.Client.Graphics.Shaders;
using Robust.Client.Interfaces.GameObjects;
using Robust.Client.Interfaces.GameObjects.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects.Components;
using Robust.Shared.Input;
using Robust.Shared.Interfaces.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
using Robust.Shared.ViewVariables;
namespace Robust.Client.GameObjects
{
// Notice: Most actual logic for clicking is done by the game screen.
public class ClickableComponent : Component, IClientClickableComponent
{
private Box2? _localBounds;
public override string Name => "Clickable";
public override uint? NetID => NetIDs.CLICKABLE;
[ViewVariables]
public Box2? LocalBounds
{
get => _localBounds;
set => _localBounds = value;
}
/// <inheritdoc />
public override void ExposeData(ObjectSerializer serializer)
{
serializer.DataField(ref _localBounds, "bounds", null);
}
/// <inheritdoc />
public override void HandleComponentState(ComponentState curState, ComponentState nextState)
{
if (curState is ClickableComponentState state)
{
_localBounds = state.LocalBounds;
}
}
/// <inheritdoc />
//TODO: This needs to accept MapPosition, not a Vector2
public bool CheckClick(Vector2 worldPos, out int drawdepth)
{
if (LocalBounds.HasValue)
{
var worldBounds = LocalBounds.Value.Translated(Owner.Transform.WorldPosition);
if (!worldBounds.Contains(worldPos))
{
drawdepth = default;
return false;
}
}
if (Owner.TryGetComponent(out ISpriteComponent sprite) && !sprite.Visible)
{
drawdepth = default;
return false;
}
var component = Owner.GetComponent<IClickTargetComponent>();
drawdepth = component.DrawDepth;
return true;
}
}
}