Files
Delta-v/Content.Client/_DV/Shipyard/UI/ShipyardConsoleMenu.xaml.cs
T
Vanessaandpre-commit-ci[bot] 58e500d254 CentComm Shipyard (#5172)
* Added CentComm shipyard console. Brought some things in Shipyard console up to modern standards.

* More shipyard stuff

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* PR Feedback

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2026-01-16 19:37:33 +01:00

122 lines
4.4 KiB
C#

using Content.Client.UserInterface.Controls;
using Content.Shared.Access.Systems;
using Content.Shared.Shipyard;
using Content.Shared.Shipyard.Prototypes;
using Content.Shared.Whitelist;
using Robust.Client.AutoGenerated;
using Robust.Client.Player;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
namespace Content.Client._DV.Shipyard.UI;
[GenerateTypedNameReferences]
public sealed partial class ShipyardConsoleMenu : FancyWindow
{
private readonly AccessReaderSystem _access;
private readonly IPlayerManager _player;
public event Action<string>? OnPurchased;
private readonly List<VesselPrototype> _vessels = [];
private readonly List<VesselCategoryPrototype> _categories = [];
public Entity<ShipyardConsoleComponent> Console;
// The currently selected category
private ProtoId<VesselCategoryPrototype>? _category;
public ShipyardConsoleMenu(EntityUid console, IPrototypeManager proto, IEntityManager entMan, IPlayerManager player, AccessReaderSystem access, EntityWhitelistSystem whitelist)
{
RobustXamlLoader.Load(this);
IoCManager.InjectDependencies(this);
Console = (console, entMan.GetComponent<ShipyardConsoleComponent>(console));
_access = access;
_player = player;
_categories.Clear();
foreach (var vesselCategoryProto in Console.Comp.Categories)
{
if (proto.Resolve(vesselCategoryProto, out var vesselCategory))
_categories.Add(vesselCategory);
}
// don't include ships that aren't allowed by whitelist, server won't accept them anyway
foreach (var vessel in proto.EnumeratePrototypes<VesselPrototype>())
{
if (whitelist.IsWhitelistPassOrNull(vessel.Whitelist, console))
_vessels.Add(vessel);
}
_vessels.Sort((x, y) => string.Compare(x.Name, y.Name, StringComparison.CurrentCultureIgnoreCase));
// inserting here and not adding at the start so it doesn't get affected by sort
PopulateCategories(Console);
SearchBar.OnTextChanged += _ => PopulateProducts(Console);
Categories.OnItemSelected += args =>
{
_category = _categories[args.Id];
Categories.SelectId(args.Id);
PopulateProducts(Console);
};
PopulateProducts(Console);
}
/// <summary>
/// Populates the list of products that will actually be shown, using the current filters.
/// </summary>
private void PopulateProducts(Entity<ShipyardConsoleComponent> entity)
{
Vessels.RemoveAllChildren();
var access = _player.LocalSession?.AttachedEntity is { } player
&& _access.IsAllowed(player, Console);
var search = SearchBar.Text.Trim().ToLowerInvariant();
foreach (var vessel in _vessels)
{
if (search.Length != 0 && !vessel.Name.Contains(search, StringComparison.InvariantCultureIgnoreCase))
continue;
if (_category != null && !vessel.Categories.Contains(_category.Value.Id))
continue;
var vesselEntry = new VesselRow(vessel, access, isFree: !entity.Comp.UseStationFunds);
vesselEntry.OnPurchasePressed += () => OnPurchased?.Invoke(vessel.ID);
Vessels.AddChild(vesselEntry);
}
}
/// <summary>
/// Populates the list categories that will actually be shown, using the current filters.
/// </summary>
private void PopulateCategories(Entity<ShipyardConsoleComponent> entity)
{
Categories.Clear();
// Guh, there's gotta be an easier way to select a category by default...
var selectedId = 0; // Default to the first category. May get overridden later.
for (var i = 0; i < _categories.Count; i++)
{
Categories.AddItem(_categories[i].LocalizedName, i);
if (entity.Comp.DefaultCategory.HasValue && entity.Comp.DefaultCategory.Value.Id.Equals(_categories[i].ID, StringComparison.CurrentCultureIgnoreCase))
selectedId = i;
}
if (selectedId >= 0 && selectedId < _categories.Count)
{
_category = _categories[selectedId];
Categories.SelectId(selectedId);
}
}
public void UpdateState(ShipyardConsoleState state)
{
BankAccountLabel.Text = Loc.GetString("cargo-console-menu-points-amount", ("amount", state.Balance.ToString()));
PopulateProducts(Console);
}
}