CargoConsoleMenu.xaml.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System.Linq;
  2. using Content.Client.UserInterface.Controls;
  3. using Content.Shared.Cargo;
  4. using Content.Shared.Cargo.Components;
  5. using Content.Shared.Cargo.Prototypes;
  6. using Robust.Client.AutoGenerated;
  7. using Robust.Client.GameObjects;
  8. using Robust.Client.UserInterface.Controls;
  9. using Robust.Client.UserInterface.XAML;
  10. using Robust.Shared.Prototypes;
  11. using static Robust.Client.UserInterface.Controls.BaseButton;
  12. namespace Content.Client.Cargo.UI
  13. {
  14. [GenerateTypedNameReferences]
  15. public sealed partial class CargoConsoleMenu : FancyWindow
  16. {
  17. private IEntityManager _entityManager;
  18. private IPrototypeManager _protoManager;
  19. private SpriteSystem _spriteSystem;
  20. private EntityUid _owner;
  21. public event Action<ButtonEventArgs>? OnItemSelected;
  22. public event Action<ButtonEventArgs>? OnOrderApproved;
  23. public event Action<ButtonEventArgs>? OnOrderCanceled;
  24. private readonly List<string> _categoryStrings = new();
  25. private string? _category;
  26. public CargoConsoleMenu(EntityUid owner, IEntityManager entMan, IPrototypeManager protoManager, SpriteSystem spriteSystem)
  27. {
  28. RobustXamlLoader.Load(this);
  29. _entityManager = entMan;
  30. _protoManager = protoManager;
  31. _spriteSystem = spriteSystem;
  32. _owner = owner;
  33. Title = Loc.GetString("cargo-console-menu-title");
  34. SearchBar.OnTextChanged += OnSearchBarTextChanged;
  35. Categories.OnItemSelected += OnCategoryItemSelected;
  36. }
  37. private void OnCategoryItemSelected(OptionButton.ItemSelectedEventArgs args)
  38. {
  39. SetCategoryText(args.Id);
  40. PopulateProducts();
  41. }
  42. private void OnSearchBarTextChanged(LineEdit.LineEditEventArgs args)
  43. {
  44. PopulateProducts();
  45. }
  46. private void SetCategoryText(int id)
  47. {
  48. _category = id == 0 ? null : _categoryStrings[id];
  49. Categories.SelectId(id);
  50. }
  51. public IEnumerable<CargoProductPrototype> ProductPrototypes
  52. {
  53. get
  54. {
  55. var allowedGroups = _entityManager.GetComponentOrNull<CargoOrderConsoleComponent>(_owner)?.AllowedGroups;
  56. foreach (var cargoPrototype in _protoManager.EnumeratePrototypes<CargoProductPrototype>())
  57. {
  58. if (!allowedGroups?.Contains(cargoPrototype.Group) ?? false)
  59. continue;
  60. yield return cargoPrototype;
  61. }
  62. }
  63. }
  64. /// <summary>
  65. /// Populates the list of products that will actually be shown, using the current filters.
  66. /// </summary>
  67. public void PopulateProducts()
  68. {
  69. Products.RemoveAllChildren();
  70. var products = ProductPrototypes.ToList();
  71. products.Sort((x, y) =>
  72. string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase));
  73. var search = SearchBar.Text.Trim().ToLowerInvariant();
  74. foreach (var prototype in products)
  75. {
  76. // if no search or category
  77. // else if search
  78. // else if category and not search
  79. if (search.Length == 0 && _category == null ||
  80. search.Length != 0 && prototype.Name.ToLowerInvariant().Contains(search) ||
  81. search.Length != 0 && prototype.Description.ToLowerInvariant().Contains(search) ||
  82. search.Length == 0 && _category != null && Loc.GetString(prototype.Category).Equals(_category))
  83. {
  84. var button = new CargoProductRow
  85. {
  86. Product = prototype,
  87. ProductName = { Text = prototype.Name },
  88. MainButton = { ToolTip = prototype.Description },
  89. PointCost = { Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", prototype.Cost.ToString())) },
  90. Icon = { Texture = _spriteSystem.Frame0(prototype.Icon) },
  91. };
  92. button.MainButton.OnPressed += args =>
  93. {
  94. OnItemSelected?.Invoke(args);
  95. };
  96. Products.AddChild(button);
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// Populates the list of products that will actually be shown, using the current filters.
  102. /// </summary>
  103. public void PopulateCategories()
  104. {
  105. _categoryStrings.Clear();
  106. Categories.Clear();
  107. foreach (var prototype in ProductPrototypes)
  108. {
  109. if (!_categoryStrings.Contains(Loc.GetString(prototype.Category)))
  110. {
  111. _categoryStrings.Add(Loc.GetString(prototype.Category));
  112. }
  113. }
  114. _categoryStrings.Sort();
  115. // Add "All" category at the top of the list
  116. _categoryStrings.Insert(0, Loc.GetString("cargo-console-menu-populate-categories-all-text"));
  117. foreach (var str in _categoryStrings)
  118. {
  119. Categories.AddItem(str);
  120. }
  121. }
  122. /// <summary>
  123. /// Populates the list of orders and requests.
  124. /// </summary>
  125. public void PopulateOrders(IEnumerable<CargoOrderData> orders)
  126. {
  127. Orders.DisposeAllChildren();
  128. Requests.DisposeAllChildren();
  129. foreach (var order in orders)
  130. {
  131. var product = _protoManager.Index<EntityPrototype>(order.ProductId);
  132. var productName = product.Name;
  133. var row = new CargoOrderRow
  134. {
  135. Order = order,
  136. Icon = { Texture = _spriteSystem.Frame0(product) },
  137. ProductName =
  138. {
  139. Text = Loc.GetString(
  140. "cargo-console-menu-populate-orders-cargo-order-row-product-name-text",
  141. ("productName", productName),
  142. ("orderAmount", order.OrderQuantity),
  143. ("orderRequester", order.Requester))
  144. },
  145. Description = {Text = Loc.GetString("cargo-console-menu-order-reason-description",
  146. ("reason", order.Reason))}
  147. };
  148. row.Cancel.OnPressed += (args) => { OnOrderCanceled?.Invoke(args); };
  149. if (order.Approved)
  150. {
  151. row.Approve.Visible = false;
  152. row.Cancel.Visible = false;
  153. Orders.AddChild(row);
  154. }
  155. else
  156. {
  157. // TODO: Disable based on access.
  158. row.Approve.OnPressed += (args) => { OnOrderApproved?.Invoke(args); };
  159. Requests.AddChild(row);
  160. }
  161. }
  162. }
  163. public void UpdateCargoCapacity(int count, int capacity)
  164. {
  165. // TODO: Rename + Loc.
  166. ShuttleCapacityLabel.Text = $"{count}/{capacity}";
  167. }
  168. public void UpdateBankData(string name, int points)
  169. {
  170. AccountNameLabel.Text = name;
  171. PointsLabel.Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", points.ToString()));
  172. }
  173. }
  174. }