|
| 1 | +using Content.Client.UserInterface.Controls; |
| 2 | +using Content.Shared.DeltaV.VendingMachines; |
| 3 | +using Content.Shared.Stacks; |
| 4 | +using Robust.Client.AutoGenerated; |
| 5 | +using Robust.Client.Graphics; |
| 6 | +using Robust.Client.Player; |
| 7 | +using Robust.Client.UserInterface; |
| 8 | +using Robust.Client.UserInterface.Controls; |
| 9 | +using Robust.Client.UserInterface.XAML; |
| 10 | +using Robust.Shared.Prototypes; |
| 11 | +using Robust.Shared.Timing; |
| 12 | +using System.Numerics; |
| 13 | + |
| 14 | +namespace Content.Client.DeltaV.VendingMachines.UI; |
| 15 | + |
| 16 | +[GenerateTypedNameReferences] |
| 17 | +public sealed partial class ShopVendorWindow : FancyWindow |
| 18 | +{ |
| 19 | + [Dependency] private readonly IComponentFactory _factory = default!; |
| 20 | + [Dependency] private readonly IEntityManager _entMan = default!; |
| 21 | + [Dependency] private readonly IPlayerManager _player = default!; |
| 22 | + [Dependency] private readonly IPrototypeManager _proto = default!; |
| 23 | + private readonly ShopVendorSystem _vendor; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// Event fired with the listing index to purchase. |
| 27 | + /// </summary> |
| 28 | + public event Action<int>? OnItemSelected; |
| 29 | + |
| 30 | + private EntityUid _owner; |
| 31 | + private readonly StyleBoxFlat _style = new() { BackgroundColor = new Color(70, 73, 102) }; |
| 32 | + private readonly StyleBoxFlat _styleBroke = new() { BackgroundColor = Color.FromHex("#303133") }; |
| 33 | + private readonly List<ListContainerButton> _buttons = new(); |
| 34 | + private uint _balance = 1; |
| 35 | + |
| 36 | + public ShopVendorWindow() |
| 37 | + { |
| 38 | + RobustXamlLoader.Load(this); |
| 39 | + IoCManager.InjectDependencies(this); |
| 40 | + |
| 41 | + _vendor = _entMan.System<ShopVendorSystem>(); |
| 42 | + |
| 43 | + VendingContents.SearchBar = SearchBar; |
| 44 | + VendingContents.DataFilterCondition += DataFilterCondition; |
| 45 | + VendingContents.GenerateItem += GenerateButton; |
| 46 | + VendingContents.ItemKeyBindDown += (args, data) => OnItemSelected?.Invoke(((ShopVendorListingData) data).Index); |
| 47 | + } |
| 48 | + |
| 49 | + public void SetEntity(EntityUid owner) |
| 50 | + { |
| 51 | + _owner = owner; |
| 52 | + |
| 53 | + if (!_entMan.TryGetComponent<ShopVendorComponent>(owner, out var comp)) |
| 54 | + return; |
| 55 | + |
| 56 | + var pack = _proto.Index(comp.Pack); |
| 57 | + Populate(pack.Listings); |
| 58 | + |
| 59 | + UpdateBalance(); |
| 60 | + } |
| 61 | + |
| 62 | + private void UpdateBalance(uint balance) |
| 63 | + { |
| 64 | + if (_balance == balance) |
| 65 | + return; |
| 66 | + |
| 67 | + _balance = balance; |
| 68 | + |
| 69 | + BalanceLabel.Text = Loc.GetString("shop-vendor-balance", ("points", balance)); |
| 70 | + |
| 71 | + // disable items that are too expensive to buy |
| 72 | + foreach (var button in _buttons) |
| 73 | + { |
| 74 | + if (button.Data is ShopVendorListingData data) |
| 75 | + button.Disabled = data.Cost > balance; |
| 76 | + |
| 77 | + button.StyleBoxOverride = button.Disabled ? _styleBroke : _style; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void UpdateBalance() |
| 82 | + { |
| 83 | + if (_player.LocalEntity is {} user) |
| 84 | + UpdateBalance(_vendor.GetBalance(_owner, user)); |
| 85 | + } |
| 86 | + |
| 87 | + private bool DataFilterCondition(string filter, ListData data) |
| 88 | + { |
| 89 | + if (data is not ShopVendorListingData { Text: var text }) |
| 90 | + return false; |
| 91 | + |
| 92 | + if (string.IsNullOrEmpty(filter)) |
| 93 | + return true; |
| 94 | + |
| 95 | + return text.Contains(filter, StringComparison.CurrentCultureIgnoreCase); |
| 96 | + } |
| 97 | + |
| 98 | + private void GenerateButton(ListData data, ListContainerButton button) |
| 99 | + { |
| 100 | + if (data is not ShopVendorListingData cast) |
| 101 | + return; |
| 102 | + |
| 103 | + _buttons.Add(button); |
| 104 | + button.AddChild(new ShopVendorItem(cast.ItemId, cast.Text, cast.Cost)); |
| 105 | + |
| 106 | + button.ToolTip = cast.Text; |
| 107 | + button.Disabled = cast.Cost > _balance; |
| 108 | + button.StyleBoxOverride = button.Disabled ? _styleBroke : _style; |
| 109 | + } |
| 110 | + |
| 111 | + public void Populate(List<ShopListing> listings) |
| 112 | + { |
| 113 | + var longestEntry = string.Empty; |
| 114 | + var listData = new List<ShopVendorListingData>(); |
| 115 | + for (var i = 0; i < listings.Count; i++) |
| 116 | + { |
| 117 | + var listing = listings[i]; |
| 118 | + var proto = _proto.Index(listing.Id); |
| 119 | + var text = proto.Name; |
| 120 | + if (proto.TryGetComponent<StackComponent>(out var stack, _factory) && stack.Count > 1) |
| 121 | + { |
| 122 | + text += " "; |
| 123 | + text += Loc.GetString("shop-vendor-stack-suffix", ("count", stack.Count)); |
| 124 | + } |
| 125 | + listData.Add(new ShopVendorListingData(i, listing.Id, text, listing.Cost)); |
| 126 | + } |
| 127 | + |
| 128 | + _buttons.Clear(); |
| 129 | + VendingContents.PopulateList(listData); |
| 130 | + SetSizeAfterUpdate(longestEntry.Length, listings.Count); |
| 131 | + } |
| 132 | + |
| 133 | + private void SetSizeAfterUpdate(int longestEntryLength, int contentCount) |
| 134 | + { |
| 135 | + SetSize = new Vector2(Math.Clamp((longestEntryLength + 2) * 12, 250, 400), |
| 136 | + Math.Clamp(contentCount * 50, 150, 350)); |
| 137 | + } |
| 138 | + |
| 139 | + protected override void FrameUpdate(FrameEventArgs args) |
| 140 | + { |
| 141 | + base.FrameUpdate(args); |
| 142 | + |
| 143 | + UpdateBalance(); |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +public record ShopVendorListingData(int Index, EntProtoId ItemId, string Text, uint Cost) : ListData; |
0 commit comments