1
0

StripeBack.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. using System.Numerics;
  2. using Robust.Client.Graphics;
  3. using Robust.Client.UserInterface.Controls;
  4. namespace Content.Client.UserInterface.Controls
  5. {
  6. public sealed class StripeBack : Container
  7. {
  8. private const float PadSize = 4;
  9. private const float EdgeSize = 2;
  10. private static readonly Color EdgeColor = Color.FromHex("#525252ff");
  11. private bool _hasTopEdge = true;
  12. private bool _hasBottomEdge = true;
  13. private bool _hasMargins = true;
  14. public const string StylePropertyBackground = "background";
  15. public bool HasTopEdge
  16. {
  17. get => _hasTopEdge;
  18. set
  19. {
  20. _hasTopEdge = value;
  21. InvalidateMeasure();
  22. }
  23. }
  24. public bool HasBottomEdge
  25. {
  26. get => _hasBottomEdge;
  27. set
  28. {
  29. _hasBottomEdge = value;
  30. InvalidateMeasure();
  31. }
  32. }
  33. public bool HasMargins
  34. {
  35. get => _hasMargins;
  36. set
  37. {
  38. _hasMargins = value;
  39. InvalidateMeasure();
  40. }
  41. }
  42. protected override Vector2 MeasureOverride(Vector2 availableSize)
  43. {
  44. var padSize = HasMargins ? PadSize : 0;
  45. var padSizeTotal = 0f;
  46. if (HasBottomEdge)
  47. padSizeTotal += padSize + EdgeSize;
  48. if (HasTopEdge)
  49. padSizeTotal += padSize + EdgeSize;
  50. var size = Vector2.Zero;
  51. availableSize.Y -= padSizeTotal;
  52. foreach (var child in Children)
  53. {
  54. child.Measure(availableSize);
  55. size = Vector2.Max(size, child.DesiredSize);
  56. }
  57. return size + new Vector2(0, padSizeTotal);
  58. }
  59. protected override Vector2 ArrangeOverride(Vector2 finalSize)
  60. {
  61. var box = new UIBox2(Vector2.Zero, finalSize);
  62. var padSize = HasMargins ? PadSize : 0;
  63. if (HasTopEdge)
  64. {
  65. box += (0, padSize + EdgeSize, 0, 0);
  66. }
  67. if (HasBottomEdge)
  68. {
  69. box += (0, 0, 0, -(padSize + EdgeSize));
  70. }
  71. foreach (var child in Children)
  72. {
  73. child.Arrange(box);
  74. }
  75. return finalSize;
  76. }
  77. protected override void Draw(DrawingHandleScreen handle)
  78. {
  79. UIBox2 centerBox = PixelSizeBox;
  80. var padSize = HasMargins ? PadSize : 0;
  81. if (HasTopEdge)
  82. {
  83. centerBox += (0, (padSize + EdgeSize) * UIScale, 0, 0);
  84. handle.DrawRect(new UIBox2(0, padSize * UIScale, PixelWidth, centerBox.Top), EdgeColor);
  85. }
  86. if (HasBottomEdge)
  87. {
  88. centerBox += (0, 0, 0, -((padSize + EdgeSize) * UIScale));
  89. handle.DrawRect(new UIBox2(0, centerBox.Bottom, PixelWidth, PixelHeight - padSize * UIScale),
  90. EdgeColor);
  91. }
  92. GetActualStyleBox()?.Draw(handle, centerBox, UIScale);
  93. }
  94. private StyleBox? GetActualStyleBox()
  95. {
  96. return TryGetStyleProperty(StylePropertyBackground, out StyleBox? box) ? box : null;
  97. }
  98. }
  99. }