mirror of
https://github.com/space-wizards/space-station-14.git
synced 2026-02-14 19:29:53 +01:00
* feat: allow removing empty smart fridge entries * review --------- Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
85 lines
2.7 KiB
C#
85 lines
2.7 KiB
C#
using System.Linq;
|
|
using System.Numerics;
|
|
using Content.Client.UserInterface.Controls;
|
|
using Content.Shared.SmartFridge;
|
|
using Robust.Client.AutoGenerated;
|
|
using Robust.Client.Graphics;
|
|
using Robust.Client.UserInterface.XAML;
|
|
using Robust.Client.UserInterface;
|
|
|
|
namespace Content.Client.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;
|
|
public event Action<SmartFridgeListData>? OnRemoveButtonPressed;
|
|
|
|
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));
|
|
var item = new SmartFridgeItem(entry.Representative, label);
|
|
item.RemoveButtonPressed += () => OnRemoveButtonPressed?.Invoke(entry);
|
|
|
|
button.AddChild(item);
|
|
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.First());
|
|
listData.Add(new SmartFridgeListData(representative, item, items.Count));
|
|
}
|
|
}
|
|
|
|
VendingContents.PopulateList(listData);
|
|
}
|
|
|
|
public void SetFlavorText(string flavor)
|
|
{
|
|
LeftFlavorLabel.Text = flavor;
|
|
}
|
|
}
|