_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));
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>
[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;
}
}
[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) };
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;
}
sub =>
{
sub.Event<SmartFridgeDispenseItemMessage>(OnDispenseItem);
+ sub.Event<SmartFridgeRemoveEntryMessage>(OnRemoveEntry);
});
}
});
}
+ 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))
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
{
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;
+}