1
0

CargoOrderConsoleBoundUserInterface.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using Content.Shared.Cargo;
  2. using Content.Client.Cargo.UI;
  3. using Content.Shared.Cargo.BUI;
  4. using Content.Shared.Cargo.Events;
  5. using Content.Shared.Cargo.Prototypes;
  6. using Content.Shared.IdentityManagement;
  7. using Robust.Client.GameObjects;
  8. using Robust.Client.Player;
  9. using Robust.Shared.Utility;
  10. using Robust.Shared.Prototypes;
  11. using static Robust.Client.UserInterface.Controls.BaseButton;
  12. namespace Content.Client.Cargo.BUI
  13. {
  14. public sealed class CargoOrderConsoleBoundUserInterface : BoundUserInterface
  15. {
  16. [ViewVariables]
  17. private CargoConsoleMenu? _menu;
  18. /// <summary>
  19. /// This is the separate popup window for individual orders.
  20. /// </summary>
  21. [ViewVariables]
  22. private CargoConsoleOrderMenu? _orderMenu;
  23. [ViewVariables]
  24. public string? AccountName { get; private set; }
  25. [ViewVariables]
  26. public int BankBalance { get; private set; }
  27. [ViewVariables]
  28. public int OrderCapacity { get; private set; }
  29. [ViewVariables]
  30. public int OrderCount { get; private set; }
  31. /// <summary>
  32. /// Currently selected product
  33. /// </summary>
  34. [ViewVariables]
  35. private CargoProductPrototype? _product;
  36. public CargoOrderConsoleBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  37. {
  38. }
  39. protected override void Open()
  40. {
  41. base.Open();
  42. var spriteSystem = EntMan.System<SpriteSystem>();
  43. var dependencies = IoCManager.Instance!;
  44. _menu = new CargoConsoleMenu(Owner, EntMan, dependencies.Resolve<IPrototypeManager>(), spriteSystem);
  45. var localPlayer = dependencies.Resolve<IPlayerManager>().LocalEntity;
  46. var description = new FormattedMessage();
  47. string orderRequester;
  48. if (EntMan.TryGetComponent<MetaDataComponent>(localPlayer, out var metadata))
  49. orderRequester = Identity.Name(localPlayer.Value, EntMan);
  50. else
  51. orderRequester = string.Empty;
  52. _orderMenu = new CargoConsoleOrderMenu();
  53. _menu.OnClose += Close;
  54. _menu.OnItemSelected += (args) =>
  55. {
  56. if (args.Button.Parent is not CargoProductRow row)
  57. return;
  58. description.Clear();
  59. description.PushColor(Color.White); // Rich text default color is grey
  60. if (row.MainButton.ToolTip != null)
  61. description.AddText(row.MainButton.ToolTip);
  62. _orderMenu.Description.SetMessage(description);
  63. _product = row.Product;
  64. _orderMenu.ProductName.Text = row.ProductName.Text;
  65. _orderMenu.PointCost.Text = row.PointCost.Text;
  66. _orderMenu.Requester.Text = orderRequester;
  67. _orderMenu.Reason.Text = "";
  68. _orderMenu.Amount.Value = 1;
  69. _orderMenu.OpenCentered();
  70. };
  71. _menu.OnOrderApproved += ApproveOrder;
  72. _menu.OnOrderCanceled += RemoveOrder;
  73. _orderMenu.SubmitButton.OnPressed += (_) =>
  74. {
  75. if (AddOrder())
  76. {
  77. _orderMenu.Close();
  78. }
  79. };
  80. _menu.OpenCentered();
  81. }
  82. private void Populate(List<CargoOrderData> orders)
  83. {
  84. if (_menu == null) return;
  85. _menu.PopulateProducts();
  86. _menu.PopulateCategories();
  87. _menu.PopulateOrders(orders);
  88. }
  89. protected override void UpdateState(BoundUserInterfaceState state)
  90. {
  91. base.UpdateState(state);
  92. if (state is not CargoConsoleInterfaceState cState)
  93. return;
  94. OrderCapacity = cState.Capacity;
  95. OrderCount = cState.Count;
  96. BankBalance = cState.Balance;
  97. AccountName = cState.Name;
  98. Populate(cState.Orders);
  99. _menu?.UpdateCargoCapacity(OrderCount, OrderCapacity);
  100. _menu?.UpdateBankData(AccountName, BankBalance);
  101. }
  102. protected override void Dispose(bool disposing)
  103. {
  104. base.Dispose(disposing);
  105. if (!disposing) return;
  106. _menu?.Dispose();
  107. _orderMenu?.Dispose();
  108. }
  109. private bool AddOrder()
  110. {
  111. var orderAmt = _orderMenu?.Amount.Value ?? 0;
  112. if (orderAmt < 1 || orderAmt > OrderCapacity)
  113. {
  114. return false;
  115. }
  116. SendMessage(new CargoConsoleAddOrderMessage(
  117. _orderMenu?.Requester.Text ?? "",
  118. _orderMenu?.Reason.Text ?? "",
  119. _product?.ID ?? "",
  120. orderAmt));
  121. return true;
  122. }
  123. private void RemoveOrder(ButtonEventArgs args)
  124. {
  125. if (args.Button.Parent?.Parent is not CargoOrderRow row || row.Order == null)
  126. return;
  127. SendMessage(new CargoConsoleRemoveOrderMessage(row.Order.OrderId));
  128. }
  129. private void ApproveOrder(ButtonEventArgs args)
  130. {
  131. if (args.Button.Parent?.Parent is not CargoOrderRow row || row.Order == null)
  132. return;
  133. if (OrderCount >= OrderCapacity)
  134. return;
  135. SendMessage(new CargoConsoleApproveOrderMessage(row.Order.OrderId));
  136. // Most of the UI isn't predicted anyway so.
  137. // _menu?.UpdateCargoCapacity(OrderCount + row.Order.Amount, OrderCapacity);
  138. }
  139. }
  140. }