76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using System.Numerics;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Client.UserInterface;
|
|
using Content.Client.UserInterface.Controls;
|
|
using Content.Shared._DV.SmartFridge;
|
|
|
|
namespace Content.Client._DV.SmartFridge;
|
|
|
|
public record SmartFridgeListData(EntityUid Representative, SmartFridgeEntry Entry, int Amount) : ListData;
|
|
|
|
[GenerateTypedNameReferences]
|
|
public sealed partial class SmartFridgeMenu : FancyWindow
|
|
{
|
|
[Dependency] private readonly IEntityManager _entityManager = default!;
|
|
|
|
public event Action<GUIBoundKeyEventArgs, ListData>? OnItemSelected;
|
|
|
|
private readonly StyleBoxFlat _styleBox = new() { BackgroundColor = new Color(70, 73, 102) };
|
|
|
|
public SmartFridgeMenu()
|
|
{
|
|
RobustXamlLoader.Load(this);
|
|
IoCManager.InjectDependencies(this);
|
|
|
|
VendingContents.SearchBar = SearchBar;
|
|
VendingContents.DataFilterCondition += DataFilterCondition;
|
|
VendingContents.GenerateItem += GenerateButton;
|
|
VendingContents.ItemKeyBindDown += (args, data) => OnItemSelected?.Invoke(args, data);
|
|
}
|
|
|
|
private bool DataFilterCondition(string filter, ListData data)
|
|
{
|
|
if (data is not SmartFridgeListData entry)
|
|
return false;
|
|
|
|
if (string.IsNullOrEmpty(filter))
|
|
return true;
|
|
|
|
return entry.Entry.Name.Contains(filter, StringComparison.CurrentCultureIgnoreCase);
|
|
}
|
|
|
|
private void GenerateButton(ListData data, ListContainerButton button)
|
|
{
|
|
if (data is not SmartFridgeListData entry)
|
|
return;
|
|
|
|
var label = Loc.GetString("smart-fridge-list-item", ("item", entry.Entry.Name), ("amount", entry.Amount));
|
|
button.AddChild(new SmartFridgeItem(entry.Representative, label));
|
|
|
|
button.ToolTip = label;
|
|
button.StyleBoxOverride = _styleBox;
|
|
}
|
|
|
|
public void Populate(Entity<SmartFridgeComponent> ent)
|
|
{
|
|
var listData = new List<ListData>();
|
|
|
|
foreach (var item in ent.Comp.Entries)
|
|
{
|
|
if (!ent.Comp.ContainedEntries.TryGetValue(item, out var items) || items.Count == 0)
|
|
{
|
|
listData.Add(new SmartFridgeListData(EntityUid.Invalid, item, 0));
|
|
}
|
|
else
|
|
{
|
|
var representative = _entityManager.GetEntity(items[0]);
|
|
listData.Add(new SmartFridgeListData(representative, item, items.Count));
|
|
}
|
|
}
|
|
|
|
VendingContents.PopulateList(listData);
|
|
}
|
|
}
|