StoreBoundUserInterface.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. using Content.Shared.Store;
  2. using JetBrains.Annotations;
  3. using System.Linq;
  4. using Content.Shared.Store.Components;
  5. using Robust.Client.UserInterface;
  6. using Robust.Shared.Prototypes;
  7. namespace Content.Client.Store.Ui;
  8. [UsedImplicitly]
  9. public sealed class StoreBoundUserInterface : BoundUserInterface
  10. {
  11. private IPrototypeManager _prototypeManager = default!;
  12. [ViewVariables]
  13. private StoreMenu? _menu;
  14. [ViewVariables]
  15. private string _search = string.Empty;
  16. [ViewVariables]
  17. private HashSet<ListingDataWithCostModifiers> _listings = new();
  18. public StoreBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
  19. {
  20. }
  21. protected override void Open()
  22. {
  23. base.Open();
  24. _menu = this.CreateWindow<StoreMenu>();
  25. if (EntMan.TryGetComponent<StoreComponent>(Owner, out var store))
  26. _menu.Title = Loc.GetString(store.Name);
  27. _menu.OnListingButtonPressed += (_, listing) =>
  28. {
  29. SendMessage(new StoreBuyListingMessage(listing.ID));
  30. };
  31. _menu.OnCategoryButtonPressed += (_, category) =>
  32. {
  33. _menu.CurrentCategory = category;
  34. _menu?.UpdateListing();
  35. };
  36. _menu.OnWithdrawAttempt += (_, type, amount) =>
  37. {
  38. SendMessage(new StoreRequestWithdrawMessage(type, amount));
  39. };
  40. _menu.SearchTextUpdated += (_, search) =>
  41. {
  42. _search = search.Trim().ToLowerInvariant();
  43. UpdateListingsWithSearchFilter();
  44. };
  45. _menu.OnRefundAttempt += (_) =>
  46. {
  47. SendMessage(new StoreRequestRefundMessage());
  48. };
  49. }
  50. protected override void UpdateState(BoundUserInterfaceState state)
  51. {
  52. base.UpdateState(state);
  53. switch (state)
  54. {
  55. case StoreUpdateState msg:
  56. _listings = msg.Listings;
  57. _menu?.UpdateBalance(msg.Balance);
  58. UpdateListingsWithSearchFilter();
  59. _menu?.SetFooterVisibility(msg.ShowFooter);
  60. _menu?.UpdateRefund(msg.AllowRefund);
  61. break;
  62. }
  63. }
  64. private void UpdateListingsWithSearchFilter()
  65. {
  66. if (_menu == null)
  67. return;
  68. var filteredListings = new HashSet<ListingDataWithCostModifiers>(_listings);
  69. if (!string.IsNullOrEmpty(_search))
  70. {
  71. filteredListings.RemoveWhere(listingData => !ListingLocalisationHelpers.GetLocalisedNameOrEntityName(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search) &&
  72. !ListingLocalisationHelpers.GetLocalisedDescriptionOrEntityDescription(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search));
  73. }
  74. _menu.PopulateStoreCategoryButtons(filteredListings);
  75. _menu.UpdateListing(filteredListings.ToList());
  76. }
  77. }