Decal placement preview (#13460)

This commit is contained in:
metalgearsloth
2023-01-16 07:35:36 +11:00
committed by GitHub
parent da7f65439b
commit 59f7fc58fc
5 changed files with 84 additions and 7 deletions

View File

@@ -0,0 +1,99 @@
using Content.Shared.Decals;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Enums;
using Robust.Shared.Prototypes;
using Robust.Shared.Utility;
namespace Content.Client.Decals.Overlays
{
public sealed class DecalOverlay : Overlay
{
private readonly DecalSystem _decals;
private readonly SpriteSystem _sprites;
private readonly IEntityManager _entManager;
private readonly IPrototypeManager _prototypeManager;
public override OverlaySpace Space => OverlaySpace.WorldSpaceBelowEntities;
private readonly Dictionary<string, (Texture Texture, bool SnapCardinals)> _cachedTextures = new(64);
public DecalOverlay(
DecalSystem decals,
SpriteSystem sprites,
IEntityManager entManager,
IPrototypeManager prototypeManager)
{
_decals = decals;
_sprites = sprites;
_entManager = entManager;
_prototypeManager = prototypeManager;
}
protected override void Draw(in OverlayDrawArgs args)
{
// Shouldn't need to clear cached textures unless the prototypes get reloaded.
var handle = args.WorldHandle;
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();
var eyeAngle = args.Viewport.Eye?.Rotation ?? Angle.Zero;
foreach (var (gridId, zIndexDictionary) in _decals.DecalRenderIndex)
{
if (zIndexDictionary.Count == 0)
continue;
if (!xformQuery.TryGetComponent(gridId, out var xform))
{
Logger.Error($"Tried to draw decals on a non-existent grid. GridUid: {gridId}");
continue;
}
if (xform.MapID != args.MapId)
continue;
var (_, worldRot, worldMatrix) = xform.GetWorldPositionRotationMatrix(xformQuery);
handle.SetTransform(worldMatrix);
foreach (var decals in zIndexDictionary.Values)
{
foreach (var decal in decals.Values)
{
if (!_cachedTextures.TryGetValue(decal.Id, out var cache) && _prototypeManager.TryIndex<DecalPrototype>(decal.Id, out var decalProto))
{
var sprite = GetDecalSprite(decal.Id);
cache = (_sprites.Frame0(sprite), decalProto.SnapCardinals);
_cachedTextures[decal.Id] = cache;
}
var cardinal = Angle.Zero;
if (cache.SnapCardinals)
{
var worldAngle = eyeAngle + worldRot;
cardinal = worldAngle.GetCardinalDir().ToAngle();
}
var angle = decal.Angle - cardinal;
if (angle.Equals(Angle.Zero))
handle.DrawTexture(cache.Texture, decal.Coordinates, decal.Color);
else
handle.DrawTexture(cache.Texture, decal.Coordinates, angle, decal.Color);
}
}
}
handle.SetTransform(Matrix3.Identity);
}
public SpriteSpecifier GetDecalSprite(string id)
{
if (_prototypeManager.TryIndex<DecalPrototype>(id, out var proto))
return proto.Sprite;
Logger.Error($"Unknown decal prototype: {id}");
return new SpriteSpecifier.Texture(new ResourcePath("/Textures/noSprite.png"));
}
}
}

View File

@@ -0,0 +1,67 @@
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Client.Input;
using Robust.Shared.Enums;
using Robust.Shared.Map;
namespace Content.Client.Decals.Overlays;
public sealed class DecalPlacementOverlay : Overlay
{
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;
[Dependency] private readonly IMapManager _mapManager = default!;
private readonly DecalPlacementSystem _placement;
private readonly SharedTransformSystem _transform;
private readonly SpriteSystem _sprite;
public override OverlaySpace Space => OverlaySpace.WorldSpace;
public DecalPlacementOverlay(DecalPlacementSystem placement, SharedTransformSystem transform, SpriteSystem sprite)
{
IoCManager.InjectDependencies(this);
_placement = placement;
_transform = transform;
_sprite = sprite;
}
protected override void Draw(in OverlayDrawArgs args)
{
var (decal, snap, rotation, color) = _placement.GetActiveDecal();
if (decal == null)
return;
var mouseScreenPos = _inputManager.MouseScreenPosition;
var mousePos = _eyeManager.ScreenToMap(mouseScreenPos);
if (mousePos.MapId != args.MapId)
return;
// No map support for decals
if (!_mapManager.TryFindGridAt(mousePos, out var grid))
{
return;
}
var worldMatrix = _transform.GetWorldMatrix(grid.Owner);
var invMatrix = _transform.GetInvWorldMatrix(grid.Owner);
var handle = args.WorldHandle;
handle.SetTransform(worldMatrix);
var localPos = invMatrix.Transform(mousePos.Position);
if (snap)
{
localPos = (Vector2) localPos.Floored() + grid.TileSize / 2f;
}
// Nothing uses snap cardinals so probably don't need preview?
var aabb = Box2.UnitCentered.Translated(localPos);
var box = new Box2Rotated(aabb, rotation, localPos);
handle.DrawTextureRect(_sprite.Frame0(decal.Sprite), box, color);
handle.SetTransform(Matrix3.Identity);
}
}