Files
tbd-station-14/Content.Client/Cargo/BUI/CargoOrderConsoleBoundUserInterface.cs
Nemanja 12b75beeab Departmental Economy (#36445)
* Cargo Accounts, Request Consoles, and lock boxes

* Funding Allocation Computer

* final changes

* test fix

* remove dumb code

* ScarKy0 review

* first cour

* second cour

* Update machines.yml

* review

---------

Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com>
Co-authored-by: Milon <milonpl.git@proton.me>
2025-04-13 15:22:36 +02:00

193 lines
5.9 KiB
C#

using Content.Shared.Cargo;
using Content.Client.Cargo.UI;
using Content.Shared.Cargo.BUI;
using Content.Shared.Cargo.Components;
using Content.Shared.Cargo.Events;
using Content.Shared.Cargo.Prototypes;
using Content.Shared.IdentityManagement;
using Robust.Client.GameObjects;
using Robust.Client.Player;
using Robust.Shared.Utility;
using Robust.Shared.Prototypes;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client.Cargo.BUI
{
public sealed class CargoOrderConsoleBoundUserInterface : BoundUserInterface
{
private readonly SharedCargoSystem _cargoSystem;
[ViewVariables]
private CargoConsoleMenu? _menu;
/// <summary>
/// This is the separate popup window for individual orders.
/// </summary>
[ViewVariables]
private CargoConsoleOrderMenu? _orderMenu;
[ViewVariables]
public string? AccountName { get; private set; }
[ViewVariables]
public int BankBalance { get; private set; }
[ViewVariables]
public int OrderCapacity { get; private set; }
[ViewVariables]
public int OrderCount { get; private set; }
/// <summary>
/// Currently selected product
/// </summary>
[ViewVariables]
private CargoProductPrototype? _product;
public CargoOrderConsoleBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
_cargoSystem = EntMan.System<SharedCargoSystem>();
}
protected override void Open()
{
base.Open();
var spriteSystem = EntMan.System<SpriteSystem>();
var dependencies = IoCManager.Instance!;
_menu = new CargoConsoleMenu(Owner, EntMan, dependencies.Resolve<IPrototypeManager>(), spriteSystem);
var localPlayer = dependencies.Resolve<IPlayerManager>().LocalEntity;
var description = new FormattedMessage();
string orderRequester;
if (EntMan.EntityExists(localPlayer))
orderRequester = Identity.Name(localPlayer.Value, EntMan);
else
orderRequester = string.Empty;
_orderMenu = new CargoConsoleOrderMenu();
_menu.OnClose += Close;
_menu.OnItemSelected += (args) =>
{
if (args.Button.Parent is not CargoProductRow row)
return;
description.Clear();
description.PushColor(Color.White); // Rich text default color is grey
if (row.MainButton.ToolTip != null)
description.AddText(row.MainButton.ToolTip);
_orderMenu.Description.SetMessage(description);
_product = row.Product;
_orderMenu.ProductName.Text = row.ProductName.Text;
_orderMenu.PointCost.Text = row.PointCost.Text;
_orderMenu.Requester.Text = orderRequester;
_orderMenu.Reason.Text = "";
_orderMenu.Amount.Value = 1;
_orderMenu.OpenCentered();
};
_menu.OnOrderApproved += ApproveOrder;
_menu.OnOrderCanceled += RemoveOrder;
_orderMenu.SubmitButton.OnPressed += (_) =>
{
if (AddOrder())
{
_orderMenu.Close();
}
};
_menu.OnAccountAction += (account, amount) =>
{
SendMessage(new CargoConsoleWithdrawFundsMessage(account, amount));
};
_menu.OnToggleUnboundedLimit += _ =>
{
SendMessage(new CargoConsoleToggleLimitMessage());
};
_menu.OpenCentered();
}
private void Populate(List<CargoOrderData> orders)
{
if (_menu == null)
return;
_menu.PopulateProducts();
_menu.PopulateCategories();
_menu.PopulateOrders(orders);
_menu.PopulateAccountActions();
}
protected override void UpdateState(BoundUserInterfaceState state)
{
base.UpdateState(state);
if (state is not CargoConsoleInterfaceState cState || !EntMan.TryGetComponent<CargoOrderConsoleComponent>(Owner, out var orderConsole))
return;
var station = EntMan.GetEntity(cState.Station);
OrderCapacity = cState.Capacity;
OrderCount = cState.Count;
BankBalance = _cargoSystem.GetBalanceFromAccount(station, orderConsole.Account);
AccountName = cState.Name;
_menu?.UpdateStation(station);
Populate(cState.Orders);
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
if (!disposing)
return;
_menu?.Dispose();
_orderMenu?.Dispose();
}
private bool AddOrder()
{
var orderAmt = _orderMenu?.Amount.Value ?? 0;
if (orderAmt < 1 || orderAmt > OrderCapacity)
{
return false;
}
SendMessage(new CargoConsoleAddOrderMessage(
_orderMenu?.Requester.Text ?? "",
_orderMenu?.Reason.Text ?? "",
_product?.ID ?? "",
orderAmt));
return true;
}
private void RemoveOrder(ButtonEventArgs args)
{
if (args.Button.Parent?.Parent is not CargoOrderRow row || row.Order == null)
return;
SendMessage(new CargoConsoleRemoveOrderMessage(row.Order.OrderId));
}
private void ApproveOrder(ButtonEventArgs args)
{
if (args.Button.Parent?.Parent is not CargoOrderRow row || row.Order == null)
return;
if (OrderCount >= OrderCapacity)
return;
SendMessage(new CargoConsoleApproveOrderMessage(row.Order.OrderId));
}
}
}