IconSmoothSystem.Edge.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Numerics;
  2. using Content.Shared.IconSmoothing;
  3. using Robust.Client.GameObjects;
  4. namespace Content.Client.IconSmoothing;
  5. public sealed partial class IconSmoothSystem
  6. {
  7. // Handles drawing edge sprites on the non-smoothed edges.
  8. private void InitializeEdge()
  9. {
  10. SubscribeLocalEvent<SmoothEdgeComponent, ComponentStartup>(OnEdgeStartup);
  11. SubscribeLocalEvent<SmoothEdgeComponent, ComponentShutdown>(OnEdgeShutdown);
  12. }
  13. private void OnEdgeStartup(EntityUid uid, SmoothEdgeComponent component, ComponentStartup args)
  14. {
  15. if (!TryComp<SpriteComponent>(uid, out var sprite))
  16. return;
  17. sprite.LayerSetOffset(EdgeLayer.South, new Vector2(0, -1f));
  18. sprite.LayerSetOffset(EdgeLayer.East, new Vector2(1f, 0f));
  19. sprite.LayerSetOffset(EdgeLayer.North, new Vector2(0, 1f));
  20. sprite.LayerSetOffset(EdgeLayer.West, new Vector2(-1f, 0f));
  21. sprite.LayerSetVisible(EdgeLayer.South, false);
  22. sprite.LayerSetVisible(EdgeLayer.East, false);
  23. sprite.LayerSetVisible(EdgeLayer.North, false);
  24. sprite.LayerSetVisible(EdgeLayer.West, false);
  25. }
  26. private void OnEdgeShutdown(EntityUid uid, SmoothEdgeComponent component, ComponentShutdown args)
  27. {
  28. if (!TryComp<SpriteComponent>(uid, out var sprite))
  29. return;
  30. sprite.LayerMapRemove(EdgeLayer.South);
  31. sprite.LayerMapRemove(EdgeLayer.East);
  32. sprite.LayerMapRemove(EdgeLayer.North);
  33. sprite.LayerMapRemove(EdgeLayer.West);
  34. }
  35. private void CalculateEdge(EntityUid uid, DirectionFlag directions, SpriteComponent? sprite = null, SmoothEdgeComponent? component = null)
  36. {
  37. if (!Resolve(uid, ref sprite, ref component, false))
  38. return;
  39. for (var i = 0; i < 4; i++)
  40. {
  41. var dir = (DirectionFlag) Math.Pow(2, i);
  42. var edge = GetEdge(dir);
  43. if ((dir & directions) != 0x0)
  44. {
  45. sprite.LayerSetVisible(edge, false);
  46. continue;
  47. }
  48. sprite.LayerSetVisible(edge, true);
  49. }
  50. }
  51. private EdgeLayer GetEdge(DirectionFlag direction)
  52. {
  53. switch (direction)
  54. {
  55. case DirectionFlag.South:
  56. return EdgeLayer.South;
  57. case DirectionFlag.East:
  58. return EdgeLayer.East;
  59. case DirectionFlag.North:
  60. return EdgeLayer.North;
  61. case DirectionFlag.West:
  62. return EdgeLayer.West;
  63. default:
  64. throw new ArgumentOutOfRangeException();
  65. }
  66. }
  67. private enum EdgeLayer : byte
  68. {
  69. South,
  70. East,
  71. North,
  72. West
  73. }
  74. }