1
0

FlatpackCreatorMenu.xaml.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System.Linq;
  2. using Content.Client.Materials;
  3. using Content.Client.Materials.UI;
  4. using Content.Client.Message;
  5. using Content.Client.UserInterface.Controls;
  6. using Content.Shared.Construction.Components;
  7. using Content.Shared.Containers.ItemSlots;
  8. using Content.Shared.Materials;
  9. using Robust.Client.AutoGenerated;
  10. using Robust.Client.GameObjects;
  11. using Robust.Client.UserInterface.XAML;
  12. using Robust.Shared.Prototypes;
  13. using Robust.Shared.Timing;
  14. using Robust.Shared.Utility;
  15. namespace Content.Client.Construction.UI;
  16. [GenerateTypedNameReferences]
  17. public sealed partial class FlatpackCreatorMenu : FancyWindow
  18. {
  19. [Dependency] private readonly IEntityManager _entityManager = default!;
  20. [Dependency] private readonly IPrototypeManager _prototypeManager = default!;
  21. private readonly ItemSlotsSystem _itemSlots;
  22. private readonly FlatpackSystem _flatpack;
  23. private readonly MaterialStorageSystem _materialStorage;
  24. private EntityUid _owner;
  25. [ValidatePrototypeId<EntityPrototype>]
  26. public const string NoBoardEffectId = "FlatpackerNoBoardEffect";
  27. private EntityUid? _currentBoard = EntityUid.Invalid;
  28. public event Action? PackButtonPressed;
  29. public FlatpackCreatorMenu()
  30. {
  31. RobustXamlLoader.Load(this);
  32. IoCManager.InjectDependencies(this);
  33. _itemSlots = _entityManager.System<ItemSlotsSystem>();
  34. _flatpack = _entityManager.System<FlatpackSystem>();
  35. _materialStorage = _entityManager.System<MaterialStorageSystem>();
  36. PackButton.OnPressed += _ => PackButtonPressed?.Invoke();
  37. InsertLabel.SetMarkup(Loc.GetString("flatpacker-ui-insert-board"));
  38. }
  39. public void SetEntity(EntityUid uid)
  40. {
  41. _owner = uid;
  42. MaterialStorageControl.SetOwner(uid);
  43. }
  44. protected override void FrameUpdate(FrameEventArgs args)
  45. {
  46. base.FrameUpdate(args);
  47. if (!_entityManager.TryGetComponent<FlatpackCreatorComponent>(_owner, out var flatpacker) ||
  48. !_itemSlots.TryGetSlot(_owner, flatpacker.SlotId, out var itemSlot))
  49. return;
  50. if (flatpacker.Packing)
  51. {
  52. PackButton.Disabled = true;
  53. }
  54. else if (_currentBoard != null)
  55. {
  56. Dictionary<string, int> cost;
  57. if (_entityManager.TryGetComponent<MachineBoardComponent>(_currentBoard, out var machineBoardComp))
  58. cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), (_currentBoard.Value, machineBoardComp));
  59. else
  60. cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), null);
  61. PackButton.Disabled = !_materialStorage.CanChangeMaterialAmount(_owner, cost);
  62. }
  63. if (_currentBoard == itemSlot.Item)
  64. return;
  65. _currentBoard = itemSlot.Item;
  66. CostHeaderLabel.Visible = _currentBoard != null;
  67. InsertLabel.Visible = _currentBoard == null;
  68. if (_currentBoard is not null)
  69. {
  70. string? prototype = null;
  71. Dictionary<string, int>? cost = null;
  72. if (_entityManager.TryGetComponent<MachineBoardComponent>(_currentBoard, out var newMachineBoardComp))
  73. {
  74. prototype = newMachineBoardComp.Prototype;
  75. cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), (_currentBoard.Value, newMachineBoardComp));
  76. }
  77. else if (_entityManager.TryGetComponent<ComputerBoardComponent>(_currentBoard, out var computerBoard))
  78. {
  79. prototype = computerBoard.Prototype;
  80. cost = _flatpack.GetFlatpackCreationCost((_owner, flatpacker), null);
  81. }
  82. if (prototype is not null && cost is not null)
  83. {
  84. var proto = _prototypeManager.Index<EntityPrototype>(prototype);
  85. MachineSprite.SetPrototype(prototype);
  86. MachineNameLabel.SetMessage(proto.Name);
  87. CostLabel.SetMarkup(GetCostString(cost));
  88. }
  89. }
  90. else
  91. {
  92. MachineSprite.SetPrototype(NoBoardEffectId);
  93. CostLabel.SetMessage(Loc.GetString("flatpacker-ui-no-board-label"));
  94. MachineNameLabel.SetMessage(" ");
  95. PackButton.Disabled = true;
  96. }
  97. }
  98. private string GetCostString(Dictionary<string, int> costs)
  99. {
  100. var orderedCosts = costs.OrderBy(p => p.Value).ToArray();
  101. var msg = new FormattedMessage();
  102. for (var i = 0; i < orderedCosts.Length; i++)
  103. {
  104. var (mat, amount) = orderedCosts[i];
  105. var matProto = _prototypeManager.Index<MaterialPrototype>(mat);
  106. var sheetVolume = _materialStorage.GetSheetVolume(matProto);
  107. var sheets = (float) -amount / sheetVolume;
  108. var amountText = Loc.GetString("lathe-menu-material-amount",
  109. ("amount", sheets),
  110. ("unit", Loc.GetString(matProto.Unit)));
  111. var text = Loc.GetString("lathe-menu-tooltip-display",
  112. ("amount", amountText),
  113. ("material", Loc.GetString(matProto.Name)));
  114. msg.TryAddMarkup(text, out _);
  115. if (i != orderedCosts.Length - 1)
  116. msg.PushNewline();
  117. }
  118. return msg.ToMarkup();
  119. }
  120. }