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 (#39195)
* feat: allow removing empty smart fridge entries * review --------- Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user