1
0

BulletRender.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. using System.Numerics;
  2. using Content.Client.Resources;
  3. using Robust.Client.Graphics;
  4. using Robust.Client.ResourceManagement;
  5. using Robust.Client.UserInterface;
  6. namespace Content.Client.Weapons.Ranged.ItemStatus;
  7. public abstract class BaseBulletRenderer : Control
  8. {
  9. private int _capacity;
  10. private LayoutParameters _params;
  11. public int Rows { get; set; } = 2;
  12. public int Count { get; set; }
  13. public int Capacity
  14. {
  15. get => _capacity;
  16. set
  17. {
  18. if (_capacity == value)
  19. return;
  20. _capacity = value;
  21. InvalidateMeasure();
  22. }
  23. }
  24. protected LayoutParameters Parameters
  25. {
  26. get => _params;
  27. set
  28. {
  29. _params = value;
  30. InvalidateMeasure();
  31. }
  32. }
  33. protected override Vector2 MeasureOverride(Vector2 availableSize)
  34. {
  35. var countPerRow = Math.Min(Capacity, CountPerRow(availableSize.X));
  36. var rows = Math.Min((int) MathF.Ceiling(Capacity / (float) countPerRow), Rows);
  37. var height = _params.ItemHeight * rows + (_params.VerticalSeparation * rows - 1);
  38. var width = RowWidth(countPerRow);
  39. return new Vector2(width, height);
  40. }
  41. protected override void Draw(DrawingHandleScreen handle)
  42. {
  43. // Scale rendering in this control by UIScale.
  44. var currentTransform = handle.GetTransform();
  45. handle.SetTransform(Matrix3Helpers.CreateScale(new Vector2(UIScale)) * currentTransform);
  46. var countPerRow = CountPerRow(Size.X);
  47. var pos = new Vector2();
  48. var spent = Capacity - Count;
  49. var bulletsDone = 0;
  50. // Draw by rows, bottom to top.
  51. for (var row = 0; row < Rows; row++)
  52. {
  53. var altColor = false;
  54. var thisRowCount = Math.Min(countPerRow, Capacity - bulletsDone);
  55. if (thisRowCount <= 0)
  56. break;
  57. // Handle MinCountPerRow
  58. // We only do this if:
  59. // 1. The next row would have less than MinCountPerRow bullets.
  60. // 2. The next row is actually visible (we aren't the last row).
  61. // 3. MinCountPerRow is actually smaller than the count per row (avoid degenerate cases).
  62. // 4. There's enough bullets that at least one will end up on the next row.
  63. var nextRowCount = Capacity - bulletsDone - thisRowCount;
  64. if (nextRowCount < _params.MinCountPerRow && row != Rows - 1 && _params.MinCountPerRow < countPerRow && nextRowCount > 0)
  65. thisRowCount -= _params.MinCountPerRow - nextRowCount;
  66. // Account for row width to right-align.
  67. var rowWidth = RowWidth(thisRowCount);
  68. pos.X += Size.X - rowWidth;
  69. // Draw row left to right (so overlapping works)
  70. for (var bullet = 0; bullet < thisRowCount; bullet++)
  71. {
  72. var absIdx = Capacity - bulletsDone - thisRowCount + bullet;
  73. var renderPos = pos;
  74. renderPos.Y = Size.Y - renderPos.Y - _params.ItemHeight;
  75. DrawItem(handle, renderPos, absIdx < spent, altColor);
  76. pos.X += _params.ItemSeparation;
  77. altColor ^= true;
  78. }
  79. bulletsDone += thisRowCount;
  80. pos.X = 0;
  81. pos.Y += _params.ItemHeight + _params.VerticalSeparation;
  82. }
  83. }
  84. protected abstract void DrawItem(DrawingHandleScreen handle, Vector2 renderPos, bool spent, bool altColor);
  85. private int CountPerRow(float width)
  86. {
  87. return (int) ((width - _params.ItemWidth + _params.ItemSeparation) / _params.ItemSeparation);
  88. }
  89. private int RowWidth(int count)
  90. {
  91. return (count - 1) * _params.ItemSeparation + _params.ItemWidth;
  92. }
  93. protected struct LayoutParameters
  94. {
  95. public int ItemHeight;
  96. public int ItemSeparation;
  97. public int ItemWidth;
  98. public int VerticalSeparation;
  99. /// <summary>
  100. /// Try to ensure there's at least this many bullets on one row.
  101. /// </summary>
  102. /// <remarks>
  103. /// For example, if there are two rows and the second row has only two bullets,
  104. /// we "steal" some bullets from the row below it to make it look nicer.
  105. /// </remarks>
  106. public int MinCountPerRow;
  107. }
  108. }
  109. /// <summary>
  110. /// Renders one or more rows of bullets for item status.
  111. /// </summary>
  112. /// <remarks>
  113. /// This is a custom control to allow complex responsive layout logic.
  114. /// </remarks>
  115. public sealed class BulletRender : BaseBulletRenderer
  116. {
  117. public const int MinCountPerRow = 7;
  118. public const int BulletHeight = 12;
  119. public const int VerticalSeparation = 2;
  120. private static readonly LayoutParameters LayoutNormal = new LayoutParameters
  121. {
  122. ItemHeight = BulletHeight,
  123. ItemSeparation = 3,
  124. ItemWidth = 5,
  125. VerticalSeparation = VerticalSeparation,
  126. MinCountPerRow = MinCountPerRow
  127. };
  128. private static readonly LayoutParameters LayoutTiny = new LayoutParameters
  129. {
  130. ItemHeight = BulletHeight,
  131. ItemSeparation = 2,
  132. ItemWidth = 2,
  133. VerticalSeparation = VerticalSeparation,
  134. MinCountPerRow = MinCountPerRow
  135. };
  136. private static readonly Color ColorA = Color.FromHex("#b68f0e");
  137. private static readonly Color ColorB = Color.FromHex("#d7df60");
  138. private static readonly Color ColorGoneA = Color.FromHex("#000000");
  139. private static readonly Color ColorGoneB = Color.FromHex("#222222");
  140. private readonly Texture _bulletTiny;
  141. private readonly Texture _bulletNormal;
  142. private BulletType _type = BulletType.Normal;
  143. public BulletType Type
  144. {
  145. get => _type;
  146. set
  147. {
  148. if (_type == value)
  149. return;
  150. Parameters = _type switch
  151. {
  152. BulletType.Normal => LayoutNormal,
  153. BulletType.Tiny => LayoutTiny,
  154. _ => throw new ArgumentOutOfRangeException()
  155. };
  156. _type = value;
  157. }
  158. }
  159. public BulletRender()
  160. {
  161. var resC = IoCManager.Resolve<IResourceCache>();
  162. _bulletTiny = resC.GetTexture("/Textures/Interface/ItemStatus/Bullets/tiny.png");
  163. _bulletNormal = resC.GetTexture("/Textures/Interface/ItemStatus/Bullets/normal.png");
  164. Parameters = LayoutNormal;
  165. }
  166. protected override void DrawItem(DrawingHandleScreen handle, Vector2 renderPos, bool spent, bool altColor)
  167. {
  168. Color color;
  169. if (spent)
  170. color = altColor ? ColorGoneA : ColorGoneB;
  171. else
  172. color = altColor ? ColorA : ColorB;
  173. var texture = _type == BulletType.Tiny ? _bulletTiny : _bulletNormal;
  174. handle.DrawTexture(texture, renderPos, color);
  175. }
  176. public enum BulletType
  177. {
  178. Normal,
  179. Tiny
  180. }
  181. }
  182. public sealed class BatteryBulletRenderer : BaseBulletRenderer
  183. {
  184. private static readonly Color ItemColor = Color.FromHex("#E00000");
  185. private static readonly Color ItemColorGone = Color.Black;
  186. private const int SizeH = 10;
  187. private const int SizeV = 10;
  188. private const int Separation = 4;
  189. public BatteryBulletRenderer()
  190. {
  191. Parameters = new LayoutParameters
  192. {
  193. ItemWidth = SizeH,
  194. ItemHeight = SizeV,
  195. ItemSeparation = SizeH + Separation,
  196. MinCountPerRow = 3,
  197. VerticalSeparation = Separation
  198. };
  199. }
  200. protected override void DrawItem(DrawingHandleScreen handle, Vector2 renderPos, bool spent, bool altColor)
  201. {
  202. var color = spent ? ItemColorGone : ItemColor;
  203. handle.DrawRect(UIBox2.FromDimensions(renderPos, new Vector2(SizeH, SizeV)), color);
  204. }
  205. }