| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- using Content.Shared.Store;
- using JetBrains.Annotations;
- using System.Linq;
- using Content.Shared.Store.Components;
- using Robust.Client.UserInterface;
- using Robust.Shared.Prototypes;
- namespace Content.Client.Store.Ui;
- [UsedImplicitly]
- public sealed class StoreBoundUserInterface : BoundUserInterface
- {
- private IPrototypeManager _prototypeManager = default!;
- [ViewVariables]
- private StoreMenu? _menu;
- [ViewVariables]
- private string _search = string.Empty;
- [ViewVariables]
- private HashSet<ListingDataWithCostModifiers> _listings = new();
- public StoreBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
- {
- }
- protected override void Open()
- {
- base.Open();
- _menu = this.CreateWindow<StoreMenu>();
- if (EntMan.TryGetComponent<StoreComponent>(Owner, out var store))
- _menu.Title = Loc.GetString(store.Name);
- _menu.OnListingButtonPressed += (_, listing) =>
- {
- SendMessage(new StoreBuyListingMessage(listing.ID));
- };
- _menu.OnCategoryButtonPressed += (_, category) =>
- {
- _menu.CurrentCategory = category;
- _menu?.UpdateListing();
- };
- _menu.OnWithdrawAttempt += (_, type, amount) =>
- {
- SendMessage(new StoreRequestWithdrawMessage(type, amount));
- };
- _menu.SearchTextUpdated += (_, search) =>
- {
- _search = search.Trim().ToLowerInvariant();
- UpdateListingsWithSearchFilter();
- };
- _menu.OnRefundAttempt += (_) =>
- {
- SendMessage(new StoreRequestRefundMessage());
- };
- }
- protected override void UpdateState(BoundUserInterfaceState state)
- {
- base.UpdateState(state);
- switch (state)
- {
- case StoreUpdateState msg:
- _listings = msg.Listings;
- _menu?.UpdateBalance(msg.Balance);
- UpdateListingsWithSearchFilter();
- _menu?.SetFooterVisibility(msg.ShowFooter);
- _menu?.UpdateRefund(msg.AllowRefund);
- break;
- }
- }
- private void UpdateListingsWithSearchFilter()
- {
- if (_menu == null)
- return;
- var filteredListings = new HashSet<ListingDataWithCostModifiers>(_listings);
- if (!string.IsNullOrEmpty(_search))
- {
- filteredListings.RemoveWhere(listingData => !ListingLocalisationHelpers.GetLocalisedNameOrEntityName(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search) &&
- !ListingLocalisationHelpers.GetLocalisedDescriptionOrEntityDescription(listingData, _prototypeManager).Trim().ToLowerInvariant().Contains(_search));
- }
- _menu.PopulateStoreCategoryButtons(filteredListings);
- _menu.UpdateListing(filteredListings.ToList());
- }
- }
|