Re-organize all projects (#4166)
This commit is contained in:
204
Content.Client/Clickable/ClickMapManager.cs
Normal file
204
Content.Client/Clickable/ClickMapManager.cs
Normal file
@@ -0,0 +1,204 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.ResourceManagement;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.ViewVariables;
|
||||
using SixLabors.ImageSharp;
|
||||
using SixLabors.ImageSharp.PixelFormats;
|
||||
|
||||
namespace Content.Client.Clickable
|
||||
{
|
||||
internal class ClickMapManager : IClickMapManager, IPostInjectInit
|
||||
{
|
||||
private const float Threshold = 0.25f;
|
||||
private const int ClickRadius = 2;
|
||||
|
||||
[Dependency] private readonly IResourceCache _resourceCache = default!;
|
||||
|
||||
[ViewVariables]
|
||||
private readonly Dictionary<Texture, ClickMap> _textureMaps = new();
|
||||
|
||||
[ViewVariables] private readonly Dictionary<RSI, RsiClickMapData> _rsiMaps =
|
||||
new();
|
||||
|
||||
public void PostInject()
|
||||
{
|
||||
_resourceCache.OnRawTextureLoaded += OnRawTextureLoaded;
|
||||
_resourceCache.OnRsiLoaded += OnOnRsiLoaded;
|
||||
}
|
||||
|
||||
private void OnOnRsiLoaded(RsiLoadedEventArgs obj)
|
||||
{
|
||||
if (obj.Atlas is Image<Rgba32> rgba)
|
||||
{
|
||||
var clickMap = ClickMap.FromImage(rgba, Threshold);
|
||||
|
||||
var rsiData = new RsiClickMapData(clickMap, obj.AtlasOffsets);
|
||||
_rsiMaps[obj.Resource.RSI] = rsiData;
|
||||
}
|
||||
}
|
||||
|
||||
private void OnRawTextureLoaded(TextureLoadedEventArgs obj)
|
||||
{
|
||||
if (obj.Image is Image<Rgba32> rgba)
|
||||
{
|
||||
_textureMaps[obj.Resource] = ClickMap.FromImage(rgba, Threshold);
|
||||
}
|
||||
}
|
||||
|
||||
public bool IsOccluding(Texture texture, Vector2i pos)
|
||||
{
|
||||
if (!_textureMaps.TryGetValue(texture, out var clickMap))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return SampleClickMap(clickMap, pos, clickMap.Size, Vector2i.Zero);
|
||||
}
|
||||
|
||||
public bool IsOccluding(RSI rsi, RSI.StateId state, RSI.State.Direction dir, int frame, Vector2i pos)
|
||||
{
|
||||
if (!_rsiMaps.TryGetValue(rsi, out var rsiData))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!rsiData.Offsets.TryGetValue(state, out var stateDat) || stateDat.Length <= (int) dir)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var dirDat = stateDat[(int) dir];
|
||||
if (dirDat.Length <= frame)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var offset = dirDat[frame];
|
||||
return SampleClickMap(rsiData.ClickMap, pos, rsi.Size, offset);
|
||||
}
|
||||
|
||||
private static bool SampleClickMap(ClickMap map, Vector2i pos, Vector2i bounds, Vector2i offset)
|
||||
{
|
||||
var (width, height) = bounds;
|
||||
var (px, py) = pos;
|
||||
|
||||
for (var x = -ClickRadius; x <= ClickRadius; x++)
|
||||
{
|
||||
var ox = px + x;
|
||||
if (ox < 0 || ox >= width)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
for (var y = -ClickRadius; y <= ClickRadius; y++)
|
||||
{
|
||||
var oy = py + y;
|
||||
|
||||
if (oy < 0 || oy >= height)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (map.IsOccluded((ox, oy) + offset))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private sealed class RsiClickMapData
|
||||
{
|
||||
public readonly ClickMap ClickMap;
|
||||
public readonly Dictionary<RSI.StateId, Vector2i[][]> Offsets;
|
||||
|
||||
public RsiClickMapData(ClickMap clickMap, Dictionary<RSI.StateId, Vector2i[][]> offsets)
|
||||
{
|
||||
ClickMap = clickMap;
|
||||
Offsets = offsets;
|
||||
}
|
||||
}
|
||||
|
||||
internal sealed class ClickMap
|
||||
{
|
||||
[ViewVariables] private readonly byte[] _data;
|
||||
|
||||
public int Width { get; }
|
||||
public int Height { get; }
|
||||
[ViewVariables] public Vector2i Size => (Width, Height);
|
||||
|
||||
public bool IsOccluded(int x, int y)
|
||||
{
|
||||
var i = y * Width + x;
|
||||
return (_data[i / 8] & (1 << (i % 8))) != 0;
|
||||
}
|
||||
|
||||
public bool IsOccluded(Vector2i vector)
|
||||
{
|
||||
var (x, y) = vector;
|
||||
return IsOccluded(x, y);
|
||||
}
|
||||
|
||||
private ClickMap(byte[] data, int width, int height)
|
||||
{
|
||||
Width = width;
|
||||
Height = height;
|
||||
_data = data;
|
||||
}
|
||||
|
||||
public static ClickMap FromImage<T>(Image<T> image, float threshold) where T : unmanaged, IPixel<T>
|
||||
{
|
||||
var threshByte = (byte) (threshold * 255);
|
||||
var width = image.Width;
|
||||
var height = image.Height;
|
||||
|
||||
var dataSize = (int) Math.Ceiling(width * height / 8f);
|
||||
var data = new byte[dataSize];
|
||||
|
||||
var pixelSpan = image.GetPixelSpan();
|
||||
|
||||
for (var i = 0; i < pixelSpan.Length; i++)
|
||||
{
|
||||
Rgba32 rgba = default;
|
||||
pixelSpan[i].ToRgba32(ref rgba);
|
||||
if (rgba.A >= threshByte)
|
||||
{
|
||||
data[i / 8] |= (byte) (1 << (i % 8));
|
||||
}
|
||||
}
|
||||
|
||||
return new ClickMap(data, width, height);
|
||||
}
|
||||
|
||||
public string DumpText()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
for (var y = 0; y < Height; y++)
|
||||
{
|
||||
for (var x = 0; x < Width; x++)
|
||||
{
|
||||
sb.Append(IsOccluded(x, y) ? "1" : "0");
|
||||
}
|
||||
|
||||
sb.AppendLine();
|
||||
}
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public interface IClickMapManager
|
||||
{
|
||||
public bool IsOccluding(Texture texture, Vector2i pos);
|
||||
|
||||
public bool IsOccluding(RSI rsi, RSI.StateId state, RSI.State.Direction dir, int frame, Vector2i pos);
|
||||
}
|
||||
}
|
||||
138
Content.Client/Clickable/ClickableComponent.cs
Normal file
138
Content.Client/Clickable/ClickableComponent.cs
Normal file
@@ -0,0 +1,138 @@
|
||||
using System;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Client.Graphics;
|
||||
using Robust.Client.Utility;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.IoC;
|
||||
using Robust.Shared.Maths;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Client.Clickable
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class ClickableComponent : Component
|
||||
{
|
||||
public override string Name => "Clickable";
|
||||
|
||||
[Dependency] private readonly IClickMapManager _clickMapManager = default!;
|
||||
|
||||
[ViewVariables] [DataField("bounds")] private DirBoundData _data = DirBoundData.Default;
|
||||
|
||||
/// <summary>
|
||||
/// Used to check whether a click worked.
|
||||
/// </summary>
|
||||
/// <param name="worldPos">The world position that was clicked.</param>
|
||||
/// <param name="drawDepth">
|
||||
/// The draw depth for the sprite that captured the click.
|
||||
/// </param>
|
||||
/// <returns>True if the click worked, false otherwise.</returns>
|
||||
public bool CheckClick(Vector2 worldPos, out int drawDepth, out uint renderOrder)
|
||||
{
|
||||
if (!Owner.TryGetComponent(out ISpriteComponent? sprite) || !sprite.Visible)
|
||||
{
|
||||
drawDepth = default;
|
||||
renderOrder = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
var transform = Owner.Transform;
|
||||
var localPos = transform.InvWorldMatrix.Transform(worldPos);
|
||||
var spriteMatrix = Matrix3.Invert(sprite.GetLocalMatrix());
|
||||
|
||||
localPos = spriteMatrix.Transform(localPos);
|
||||
|
||||
var found = false;
|
||||
var worldRotation = transform.WorldRotation;
|
||||
|
||||
if (_data.All.Contains(localPos))
|
||||
{
|
||||
found = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
// TODO: diagonal support?
|
||||
|
||||
var modAngle = sprite.NoRotation ? SpriteComponent.CalcRectWorldAngle(worldRotation, 4) : Angle.Zero;
|
||||
var dir = sprite.EnableDirectionOverride ? sprite.DirectionOverride : worldRotation.GetCardinalDir();
|
||||
|
||||
modAngle += dir.ToAngle();
|
||||
|
||||
var layerPos = modAngle.RotateVec(localPos);
|
||||
|
||||
var boundsForDir = dir switch
|
||||
{
|
||||
Direction.East => _data.East,
|
||||
Direction.North => _data.North,
|
||||
Direction.South => _data.South,
|
||||
Direction.West => _data.West,
|
||||
_ => throw new InvalidOperationException()
|
||||
};
|
||||
|
||||
if (boundsForDir.Contains(layerPos))
|
||||
{
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!found)
|
||||
{
|
||||
foreach (var layer in sprite.AllLayers)
|
||||
{
|
||||
if (!layer.Visible) continue;
|
||||
|
||||
var dirCount = sprite.GetLayerDirectionCount(layer);
|
||||
var dir = layer.EffectiveDirection(worldRotation);
|
||||
var modAngle = sprite.NoRotation ? SpriteComponent.CalcRectWorldAngle(worldRotation, dirCount) : Angle.Zero;
|
||||
modAngle += dir.Convert().ToAngle();
|
||||
|
||||
var layerPos = modAngle.RotateVec(localPos);
|
||||
|
||||
var localOffset = layerPos * EyeManager.PixelsPerMeter * (1, -1);
|
||||
if (layer.Texture != null)
|
||||
{
|
||||
if (_clickMapManager.IsOccluding(layer.Texture,
|
||||
(Vector2i) (localOffset + layer.Texture.Size / 2f)))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else if (layer.RsiState != default)
|
||||
{
|
||||
var rsi = layer.ActualRsi;
|
||||
if (rsi == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var (mX, mY) = localOffset + rsi.Size / 2;
|
||||
|
||||
if (_clickMapManager.IsOccluding(rsi, layer.RsiState, dir,
|
||||
layer.AnimationFrame, ((int) mX, (int) mY)))
|
||||
{
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
drawDepth = sprite.DrawDepth;
|
||||
renderOrder = sprite.RenderOrder;
|
||||
return found;
|
||||
}
|
||||
|
||||
[DataDefinition]
|
||||
public sealed class DirBoundData
|
||||
{
|
||||
[ViewVariables] [DataField("all")] public Box2 All;
|
||||
[ViewVariables] [DataField("north")] public Box2 North;
|
||||
[ViewVariables] [DataField("south")] public Box2 South;
|
||||
[ViewVariables] [DataField("east")] public Box2 East;
|
||||
[ViewVariables] [DataField("west")] public Box2 West;
|
||||
|
||||
public static DirBoundData Default { get; } = new();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user