StoreListingControl.xaml.cs 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using Content.Client.GameTicking.Managers;
  2. using Content.Shared.Store;
  3. using Robust.Client.AutoGenerated;
  4. using Robust.Client.Graphics;
  5. using Robust.Client.UserInterface;
  6. using Robust.Client.UserInterface.XAML;
  7. using Robust.Shared.Prototypes;
  8. using Robust.Shared.Timing;
  9. namespace Content.Client.Store.Ui;
  10. [GenerateTypedNameReferences]
  11. public sealed partial class StoreListingControl : Control
  12. {
  13. [Dependency] private readonly IPrototypeManager _prototype = default!;
  14. [Dependency] private readonly IEntityManager _entity = default!;
  15. [Dependency] private readonly IGameTiming _timing = default!;
  16. private readonly ClientGameTicker _ticker;
  17. private readonly ListingDataWithCostModifiers _data;
  18. private readonly bool _hasBalance;
  19. private readonly string _price;
  20. private readonly string _discount;
  21. public StoreListingControl(ListingDataWithCostModifiers data, string price, string discount, bool hasBalance, Texture? texture = null)
  22. {
  23. IoCManager.InjectDependencies(this);
  24. RobustXamlLoader.Load(this);
  25. _ticker = _entity.System<ClientGameTicker>();
  26. _data = data;
  27. _hasBalance = hasBalance;
  28. _price = price;
  29. _discount = discount;
  30. StoreItemName.Text = ListingLocalisationHelpers.GetLocalisedNameOrEntityName(_data, _prototype);
  31. StoreItemDescription.SetMessage(ListingLocalisationHelpers.GetLocalisedDescriptionOrEntityDescription(_data, _prototype));
  32. UpdateBuyButtonText();
  33. StoreItemBuyButton.Disabled = !CanBuy();
  34. StoreItemTexture.Texture = texture;
  35. }
  36. private bool CanBuy()
  37. {
  38. if (!_hasBalance)
  39. return false;
  40. var stationTime = _timing.CurTime.Subtract(_ticker.RoundStartTimeSpan);
  41. if (_data.RestockTime > stationTime)
  42. return false;
  43. return true;
  44. }
  45. private void UpdateBuyButtonText()
  46. {
  47. var stationTime = _timing.CurTime.Subtract(_ticker.RoundStartTimeSpan);
  48. if (_data.RestockTime > stationTime)
  49. {
  50. var timeLeftToBuy = stationTime - _data.RestockTime;
  51. StoreItemBuyButton.Text = timeLeftToBuy.Duration().ToString(@"mm\:ss");
  52. }
  53. else
  54. {
  55. DiscountSubText.Text = _discount;
  56. StoreItemBuyButton.Text = _price;
  57. }
  58. }
  59. private void UpdateName()
  60. {
  61. var name = ListingLocalisationHelpers.GetLocalisedNameOrEntityName(_data, _prototype);
  62. var stationTime = _timing.CurTime.Subtract(_ticker.RoundStartTimeSpan);
  63. if (_data.RestockTime > stationTime)
  64. {
  65. name += Loc.GetString("store-ui-button-out-of-stock");
  66. }
  67. StoreItemName.Text = name;
  68. }
  69. protected override void FrameUpdate(FrameEventArgs args)
  70. {
  71. base.FrameUpdate(args);
  72. UpdateBuyButtonText();
  73. UpdateName();
  74. StoreItemBuyButton.Disabled = !CanBuy();
  75. }
  76. }