feat: allow removing empty smart fridge entries (#39195)

* feat: allow removing empty smart fridge entries

* review

---------

Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
Perry Fraser
2026-01-09 09:48:38 -05:00
committed by GitHub
parent 51e7a39bad
commit 78343b2dbb
6 changed files with 52 additions and 2 deletions

View File

@@ -19,12 +19,13 @@ public sealed class SmartFridgeBoundUserInterface : BoundUserInterface
_menu = this.CreateWindow<SmartFridgeMenu>();
_menu.OnItemSelected += OnItemSelected;
_menu.OnRemoveButtonPressed += data => SendPredictedMessage(new SmartFridgeRemoveEntryMessage(data.Entry));
Refresh();
}
public void Refresh()
{
if (_menu is not {} menu || !EntMan.TryGetComponent(Owner, out SmartFridgeComponent? fridge))
if (_menu is not { } menu || !EntMan.TryGetComponent(Owner, out SmartFridgeComponent? fridge))
return;
menu.SetFlavorText(Loc.GetString(fridge.FlavorText));

View File

@@ -13,4 +13,10 @@
SizeFlagsStretchRatio="3"
HorizontalExpand="True"
ClipText="True"/>
<TextureButton Name="RemoveButton"
StyleClasses="CrossButtonRed"
VerticalAlignment="Center"
Margin="0 0 10 0"
Scale="0.75 0.75"
Visible="True" />
</BoxContainer>

View File

@@ -8,11 +8,18 @@ namespace Content.Client.SmartFridge;
[GenerateTypedNameReferences]
public sealed partial class SmartFridgeItem : BoxContainer
{
public Action? RemoveButtonPressed;
public SmartFridgeItem(EntityUid uid, string text)
{
RobustXamlLoader.Load(this);
EntityView.SetEntity(uid);
NameLabel.Text = text;
RemoveButton.OnPressed += _ => RemoveButtonPressed?.Invoke();
if (uid.IsValid())
RemoveButton.Visible = false;
}
}

View File

@@ -17,6 +17,7 @@ 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) };
@@ -48,8 +49,10 @@ public sealed partial class SmartFridgeMenu : FancyWindow
return;
var label = Loc.GetString("smart-fridge-list-item", ("item", entry.Entry.Name), ("amount", entry.Amount));
button.AddChild(new SmartFridgeItem(entry.Representative, label));
var item = new SmartFridgeItem(entry.Representative, label);
item.RemoveButtonPressed += () => OnRemoveButtonPressed?.Invoke(entry);
button.AddChild(item);
button.ToolTip = label;
button.StyleBoxOverride = _styleBox;
}

View File

@@ -41,6 +41,7 @@ public abstract class SharedSmartFridgeSystem : EntitySystem
sub =>
{
sub.Event<SmartFridgeDispenseItemMessage>(OnDispenseItem);
sub.Event<SmartFridgeRemoveEntryMessage>(OnRemoveEntry);
});
}
@@ -168,6 +169,22 @@ public abstract class SharedSmartFridgeSystem : EntitySystem
});
}
private void OnRemoveEntry(Entity<SmartFridgeComponent> ent, ref SmartFridgeRemoveEntryMessage args)
{
if (!Allowed(ent, args.Actor))
return;
if (!ent.Comp.ContainedEntries.TryGetValue(args.Entry, out var contained)
|| contained.Count > 0
|| !ent.Comp.Entries.Contains(args.Entry))
return;
ent.Comp.Entries.Remove(args.Entry);
ent.Comp.ContainedEntries.Remove(args.Entry);
Dirty(ent);
UpdateUI(ent);
}
private void OnGetDumpableVerb(Entity<SmartFridgeComponent> ent, ref GetDumpableVerbEvent args)
{
if (_accessReader.IsAllowed(args.User, ent))

View File

@@ -75,6 +75,10 @@ public sealed partial class SmartFridgeComponent : Component
public SoundSpecifier SoundDeny = new SoundCollectionSpecifier("VendingDeny");
}
/// <summary>
/// A single entry in the smart fridge UI.
/// May contain multiple items of the same type.
/// </summary>
[Serializable, NetSerializable, DataRecord]
public partial record struct SmartFridgeEntry
{
@@ -92,8 +96,20 @@ public enum SmartFridgeUiKey : byte
Key,
}
/// <summary>
/// Send by the client when trying to dispense an item inside the fridge.
/// </summary>
[Serializable, NetSerializable]
public sealed class SmartFridgeDispenseItemMessage(SmartFridgeEntry entry) : BoundUserInterfaceMessage
{
public SmartFridgeEntry Entry = entry;
}
/// <summary>
/// Send by the client when trying to remove an empty smart fridge entry from the list of items in the UI.
/// </summary>
[Serializable, NetSerializable]
public sealed class SmartFridgeRemoveEntryMessage(SmartFridgeEntry entry) : BoundUserInterfaceMessage
{
public SmartFridgeEntry Entry = entry;
}