6
StarHorizon_Public/Content.Client/_NF/Cargo/UI/NFCargoConsoleMenu.xaml.cs
2025-11-07 12:32:48 +03:00

189 lines
6.4 KiB
C#

using System.Linq;
using Content.Client.Cargo.UI;
using Content.Client.UserInterface.Controls;
using Content.Shared._NF.Bank;
using Content.Shared._NF.Cargo;
using Content.Shared.Cargo.Components;
using Content.Shared.Cargo.Prototypes;
using Robust.Client.AutoGenerated;
using Robust.Client.GameObjects;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using static Robust.Client.UserInterface.Controls.BaseButton;
namespace Content.Client._NF.Cargo.UI;
[GenerateTypedNameReferences]
public sealed partial class NFCargoConsoleMenu : FancyWindow
{
private IEntityManager _entityManager;
private IPrototypeManager _protoManager;
private SpriteSystem _spriteSystem;
private EntityUid _owner;
public event Action<ButtonEventArgs>? OnItemSelected;
private readonly List<string> _categoryStrings = new();
private string? _category;
public NFCargoConsoleMenu(EntityUid owner, IEntityManager entMan, IPrototypeManager protoManager, SpriteSystem spriteSystem)
{
RobustXamlLoader.Load(this);
_entityManager = entMan;
_protoManager = protoManager;
_spriteSystem = spriteSystem;
_owner = owner;
Title = Loc.GetString("cargo-console-menu-title");
SearchBar.OnTextChanged += OnSearchBarTextChanged;
Categories.OnItemSelected += OnCategoryItemSelected;
}
private void OnCategoryItemSelected(OptionButton.ItemSelectedEventArgs args)
{
SetCategoryText(args.Id);
PopulateProducts();
}
private void OnSearchBarTextChanged(LineEdit.LineEditEventArgs args)
{
PopulateProducts();
}
private void SetCategoryText(int id)
{
_category = id == 0 ? null : _categoryStrings[id];
Categories.SelectId(id);
}
public IEnumerable<CargoProductPrototype> ProductPrototypes
{
get
{
var allowedGroups = _entityManager.GetComponentOrNull<CargoOrderConsoleComponent>(_owner)?.AllowedGroups;
foreach (var cargoPrototype in _protoManager.EnumeratePrototypes<CargoProductPrototype>())
{
if (!allowedGroups?.Contains(cargoPrototype.Group) ?? false)
continue;
yield return cargoPrototype;
}
}
}
/// <summary>
/// Populates the list of products that will actually be shown, using the current filters.
/// </summary>
public void PopulateProducts()
{
Products.RemoveAllChildren();
var products = ProductPrototypes.ToList();
products.Sort((x, y) =>
string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase));
var search = SearchBar.Text.Trim().ToLowerInvariant();
foreach (var prototype in products)
{
// if no search or category
// else if search
// else if category and not search
if (search.Length == 0 && _category == null ||
search.Length != 0 && prototype.Name.ToLowerInvariant().Contains(search) ||
search.Length != 0 && prototype.Description.ToLowerInvariant().Contains(search) ||
search.Length == 0 && _category != null && Loc.GetString(prototype.Category).Equals(_category))
{
var button = new CargoProductRow
{
Product = prototype,
ProductName = { Text = prototype.Name },
MainButton = { ToolTip = prototype.Description },
PointCost = { Text = BankSystemExtensions.ToSpesoString(prototype.Cost) }, // Frontier:
Icon = { Texture = _spriteSystem.Frame0(prototype.Icon) },
};
button.MainButton.OnPressed += args =>
{
OnItemSelected?.Invoke(args);
};
Products.AddChild(button);
}
}
}
/// <summary>
/// Populates the list of products that will actually be shown, using the current filters.
/// </summary>
public void PopulateCategories()
{
_categoryStrings.Clear();
Categories.Clear();
foreach (var prototype in ProductPrototypes)
{
if (!_categoryStrings.Contains(Loc.GetString(prototype.Category)))
{
_categoryStrings.Add(Loc.GetString(prototype.Category));
}
}
_categoryStrings.Sort();
// Add "All" category at the top of the list
_categoryStrings.Insert(0, Loc.GetString("cargo-console-menu-populate-categories-all-text"));
foreach (var str in _categoryStrings)
{
Categories.AddItem(str);
}
}
/// <summary>
/// Populates the list of orders and requests.
/// </summary>
public void PopulateOrders(IEnumerable<NFCargoOrderData> orders, int orderCapacity)
{
Orders.DisposeAllChildren();
var quantitySum = 0;
foreach (var order in orders)
{
if (!_protoManager.TryIndex<EntityPrototype>(order.ProductId, out var product))
continue;
var productName = product.Name;
var row = new NFCargoOrderRow
{
Order = order,
Icon = { Texture = _spriteSystem.Frame0(product) },
ProductName =
{
Text = Loc.GetString(
"cargo-console-menu-nf-populate-orders-cargo-order-row-product-name-text",
("total", order.OrderQuantity),
("productName", productName),
("purchaser", order.Purchaser))
},
Quantity =
{
Text = Loc.GetString(
"cargo-console-menu-nf-populate-orders-cargo-order-row-product-quantity-text",
("remaining", order.OrderQuantity - order.NumDispatched)
)
}
};
Orders.AddChild(row);
quantitySum += order.OrderQuantity - order.NumDispatched;
}
ShuttleCapacityLabel.Text = Loc.GetString("cargo-console-menu-nf-order-capacity", ("count", quantitySum), ("capacity", orderCapacity));
}
public void UpdateBankData(string name, int bankBalance)
{
AccountNameLabel.Text = name;
PointsLabel.Text = BankSystemExtensions.ToSpesoString(bankBalance);
}
}