using System.Linq; using Content.Client.Message; using Content.Client.UserInterface.Controls; using Content.Shared.Cargo.Components; using Content.Shared.Cargo.Prototypes; using Robust.Client.AutoGenerated; using Robust.Client.UserInterface; using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.XAML; using Robust.Shared.Prototypes; using Robust.Shared.Timing; namespace Content.Client.Cargo.UI; [GenerateTypedNameReferences] public sealed partial class FundingAllocationMenu : FancyWindow { [Dependency] private readonly IEntityManager _entityManager = default!; [Dependency] private readonly IPrototypeManager _prototypeManager = default!; private readonly EntityQuery _bankQuery; public event Action, int>>? OnSavePressed; private EntityUid? _station; private readonly HashSet _addedControls = new(); private readonly List _spinBoxes = new(); private readonly Dictionary, RichTextLabel> _balanceLabels = new(); public FundingAllocationMenu() { RobustXamlLoader.Load(this); IoCManager.InjectDependencies(this); _bankQuery = _entityManager.GetEntityQuery(); SaveButton.OnPressed += _ => { if (!_entityManager.TryGetComponent(_station, out var bank)) return; var accounts = bank.Accounts.Keys.OrderBy(p => p.Id).ToList(); var dicts = new Dictionary, int>(); for (var i = 0; i< accounts.Count; i++) { dicts.Add(accounts[i], _spinBoxes[i].Value); } OnSavePressed?.Invoke(dicts); SaveButton.Disabled = true; }; BuildEntries(); } private void BuildEntries() { if (!_entityManager.TryGetComponent(_station, out var bank)) return; HelpLabel.Text = Loc.GetString("cargo-funding-alloc-console-label-help", ("percent", (int) (bank.PrimaryCut * 100))); foreach (var ctrl in _addedControls) { ctrl.Orphan(); } _addedControls.Clear(); _spinBoxes.Clear(); _balanceLabels.Clear(); var accounts = bank.Accounts.ToList().OrderBy(p => p.Key); foreach (var (account, balance) in accounts) { var accountProto = _prototypeManager.Index(account); var accountNameLabel = new RichTextLabel { Modulate = accountProto.Color, Margin = new Thickness(0, 0, 10, 0) }; accountNameLabel.SetMarkup($"[bold]{Loc.GetString(accountProto.Name)}[/bold]"); EntriesContainer.AddChild(accountNameLabel); var codeLabel = new RichTextLabel { Text = $"[font=\"Monospace\"]{Loc.GetString(accountProto.Code)}[/font]", HorizontalAlignment = HAlignment.Center, Margin = new Thickness(5, 0), }; EntriesContainer.AddChild(codeLabel); var balanceLabel = new RichTextLabel { Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", balance)), HorizontalExpand = true, HorizontalAlignment = HAlignment.Center, Margin = new Thickness(5, 0), }; EntriesContainer.AddChild(balanceLabel); var box = new SpinBox { HorizontalAlignment = HAlignment.Center, HorizontalExpand = true, Value = (int) (bank.RevenueDistribution[account] * 100), IsValid = val => val is >= 0 and <= 100, }; box.ValueChanged += _ => UpdateButtonDisabled(); EntriesContainer.AddChild(box); _spinBoxes.Add(box); _balanceLabels.Add(account, balanceLabel); _addedControls.Add(accountNameLabel); _addedControls.Add(codeLabel); _addedControls.Add(balanceLabel); _addedControls.Add(box); } } private void UpdateButtonDisabled() { if (!_entityManager.TryGetComponent(_station, out var bank)) return; var sum = _spinBoxes.Sum(s => s.Value); var incorrectSum = sum != 100; var differs = false; var accounts = bank.Accounts.Keys.OrderBy(p => p.Id).ToList(); for (var i = 0; i < accounts.Count; i++) { var percent = _spinBoxes[i].Value; if (percent != (int) Math.Round(bank.RevenueDistribution[accounts[i]] * 100)) { differs = true; break; } } SaveButton.Disabled = !differs || incorrectSum; var diff = sum - 100; SaveAlertLabel.Visible = incorrectSum; SaveAlertLabel.SetMarkup(Loc.GetString("cargo-funding-alloc-console-label-save-fail", ("pos", Math.Sign(diff)), ("val", Math.Abs(diff)))); } public void Update(FundingAllocationConsoleBuiState state) { _station = _entityManager.GetEntity(state.Station); BuildEntries(); UpdateButtonDisabled(); } protected override void FrameUpdate(FrameEventArgs args) { base.FrameUpdate(args); if (!_bankQuery.TryComp(_station, out var bank)) return; foreach (var (account, label) in _balanceLabels) { label.Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", bank.Accounts[account])); } } }