diff --git a/Content.Client/Storage/Systems/EntityStorageSystem.cs b/Content.Client/Storage/Systems/EntityStorageSystem.cs index e2a106346b..6d37beb078 100644 --- a/Content.Client/Storage/Systems/EntityStorageSystem.cs +++ b/Content.Client/Storage/Systems/EntityStorageSystem.cs @@ -1,6 +1,7 @@ using System.Diagnostics.CodeAnalysis; using Content.Client.Storage.Components; using Content.Shared.Destructible; +using Content.Shared.Foldable; using Content.Shared.Interaction; using Content.Shared.Lock; using Content.Shared.Movement.Events; @@ -23,6 +24,7 @@ public sealed class EntityStorageSystem : SharedEntityStorageSystem SubscribeLocalEvent(OnDestruction); SubscribeLocalEvent>(AddToggleOpenVerb); SubscribeLocalEvent(OnRelayMovement); + SubscribeLocalEvent(OnFoldAttempt); SubscribeLocalEvent(OnGetState); SubscribeLocalEvent(OnHandleState); diff --git a/Content.Server/Storage/EntitySystems/EntityStorageSystem.cs b/Content.Server/Storage/EntitySystems/EntityStorageSystem.cs index 2b7ee7267a..6281daab0d 100644 --- a/Content.Server/Storage/EntitySystems/EntityStorageSystem.cs +++ b/Content.Server/Storage/EntitySystems/EntityStorageSystem.cs @@ -5,6 +5,7 @@ using Content.Server.Construction.Components; using Content.Server.Storage.Components; using Content.Server.Tools.Systems; using Content.Shared.Destructible; +using Content.Shared.Foldable; using Content.Shared.Interaction; using Content.Shared.Lock; using Content.Shared.Movement.Events; @@ -35,6 +36,7 @@ public sealed class EntityStorageSystem : SharedEntityStorageSystem SubscribeLocalEvent(OnDestruction); SubscribeLocalEvent>(AddToggleOpenVerb); SubscribeLocalEvent(OnRelayMovement); + SubscribeLocalEvent(OnFoldAttempt); SubscribeLocalEvent(OnGetState); SubscribeLocalEvent(OnHandleState); diff --git a/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs b/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs index 15a0a0a9f2..1549129dc0 100644 --- a/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs +++ b/Content.Shared/Buckle/SharedBuckleSystem.Strap.cs @@ -2,6 +2,7 @@ using Content.Shared.Buckle.Components; using Content.Shared.Destructible; using Content.Shared.DragDrop; +using Content.Shared.Foldable; using Content.Shared.Interaction; using Content.Shared.Storage; using Content.Shared.Verbs; @@ -31,6 +32,7 @@ public abstract partial class SharedBuckleSystem SubscribeLocalEvent(OnStrapDragDropTarget); SubscribeLocalEvent(OnCanDropTarget); + SubscribeLocalEvent(OnAttemptFold); SubscribeLocalEvent(OnStrapMoveEvent); } @@ -115,7 +117,7 @@ public abstract partial class SharedBuckleSystem if (args.Handled) return; - ToggleBuckle(args.User, args.User, uid); + args.Handled = ToggleBuckle(args.User, args.User, uid); } private void AddStrapVerbs(EntityUid uid, StrapComponent component, GetVerbsEvent args) @@ -199,6 +201,14 @@ public abstract partial class SharedBuckleSystem args.Handled = true; } + private void OnAttemptFold(EntityUid uid, StrapComponent component, ref FoldAttemptEvent args) + { + if (args.Cancelled) + return; + + args.Cancelled = component.BuckledEntities.Count != 0; + } + private void OnStrapDragDropTarget(EntityUid uid, StrapComponent component, ref DragDropTargetEvent args) { if (!StrapCanDragDropOn(uid, args.User, uid, args.Dragged, component)) diff --git a/Content.Shared/Foldable/FoldableSystem.cs b/Content.Shared/Foldable/FoldableSystem.cs index 1a0a9bd112..7bf00a5d24 100644 --- a/Content.Shared/Foldable/FoldableSystem.cs +++ b/Content.Shared/Foldable/FoldableSystem.cs @@ -1,6 +1,4 @@ -using System.Linq; using Content.Shared.Buckle; -using Content.Shared.Buckle.Components; using Content.Shared.Storage.Components; using Content.Shared.Verbs; using Robust.Shared.Containers; @@ -91,69 +89,61 @@ public sealed class FoldableSystem : EntitySystem args.Cancel(); } - public bool TryToggleFold(EntityUid uid, FoldableComponent comp) + public bool TryToggleFold(EntityUid uid, FoldableComponent comp) + { + return TrySetFolded(uid, comp, !comp.IsFolded); + } + + public bool CanToggleFold(EntityUid uid, FoldableComponent? fold = null) + { + if (!Resolve(uid, ref fold)) + return false; + + // Can't un-fold in any container (locker, hands, inventory, whatever). + if (_container.IsEntityInContainer(uid)) + return false; + + var ev = new FoldAttemptEvent(); + RaiseLocalEvent(uid, ref ev); + return !ev.Cancelled; + } + + /// + /// Try to fold/unfold + /// + public bool TrySetFolded(EntityUid uid, FoldableComponent comp, bool state) + { + if (state == comp.IsFolded) + return false; + + if (!CanToggleFold(uid, comp)) + return false; + + SetFolded(uid, comp, state); + return true; + } + + #region Verb + + private void AddFoldVerb(EntityUid uid, FoldableComponent component, GetVerbsEvent args) + { + if (!args.CanAccess || !args.CanInteract || args.Hands == null || !CanToggleFold(uid, component)) + return; + + AlternativeVerb verb = new() { - return TrySetFolded(uid, comp, !comp.IsFolded); - } + Act = () => TryToggleFold(uid, component), + Text = component.IsFolded ? Loc.GetString("unfold-verb") : Loc.GetString("fold-verb"), + Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/fold.svg.192dpi.png")), - public bool CanToggleFold(EntityUid uid, FoldableComponent? fold = null) - { - if (!Resolve(uid, ref fold)) - return false; + // If the object is unfolded and they click it, they want to fold it, if it's folded, they want to pick it up + Priority = component.IsFolded ? 0 : 2, + }; - // Can't un-fold in any container (locker, hands, inventory, whatever). - if (_container.IsEntityInContainer(uid)) - return false; + args.Verbs.Add(verb); + } - // If an entity is buckled to the object we can't pick it up or fold it - if (TryComp(uid, out StrapComponent? strap) && strap.BuckledEntities.Any()) - return false; - - if (TryComp(uid, out SharedEntityStorageComponent? storage)) - { - if (storage.Open || storage.Contents.ContainedEntities.Any()) - return false; - } - - return true; - } - - /// - /// Try to fold/unfold - /// - public bool TrySetFolded(EntityUid uid, FoldableComponent comp, bool state) - { - if (state == comp.IsFolded) - return false; - - if (!CanToggleFold(uid, comp)) - return false; - - SetFolded(uid, comp, state); - return true; - } - - #region Verb - - private void AddFoldVerb(EntityUid uid, FoldableComponent component, GetVerbsEvent args) - { - if (!args.CanAccess || !args.CanInteract || args.Hands == null || !CanToggleFold(uid, component)) - return; - - AlternativeVerb verb = new() - { - Act = () => TryToggleFold(uid, component), - Text = component.IsFolded ? Loc.GetString("unfold-verb") : Loc.GetString("fold-verb"), - Icon = new SpriteSpecifier.Texture(new ("/Textures/Interface/VerbIcons/fold.svg.192dpi.png")), - - // If the object is unfolded and they click it, they want to fold it, if it's folded, they want to pick it up - Priority = component.IsFolded ? 0 : 2, - }; - - args.Verbs.Add(verb); - } - - #endregion + #endregion [Serializable, NetSerializable] public enum FoldedVisuals : byte @@ -161,3 +151,10 @@ public sealed class FoldableSystem : EntitySystem State } } + +/// +/// Event raised on an entity to determine if it can be folded. +/// +/// +[ByRefEvent] +public record struct FoldAttemptEvent(bool Cancelled = false); diff --git a/Content.Shared/Item/SharedItemSystem.cs b/Content.Shared/Item/SharedItemSystem.cs index cd68c3e31a..cca871d153 100644 --- a/Content.Shared/Item/SharedItemSystem.cs +++ b/Content.Shared/Item/SharedItemSystem.cs @@ -21,7 +21,7 @@ public abstract class SharedItemSystem : EntitySystem { base.Initialize(); SubscribeLocalEvent>(AddPickupVerb); - SubscribeLocalEvent(OnHandInteract); + SubscribeLocalEvent(OnHandInteract, before: new []{typeof(SharedItemSystem)}); SubscribeLocalEvent(OnStackCountChanged); SubscribeLocalEvent(OnGetState); diff --git a/Content.Shared/Storage/EntitySystems/SharedEntityStorageSystem.cs b/Content.Shared/Storage/EntitySystems/SharedEntityStorageSystem.cs index 26cb2042fa..5e3a257d1c 100644 --- a/Content.Shared/Storage/EntitySystems/SharedEntityStorageSystem.cs +++ b/Content.Shared/Storage/EntitySystems/SharedEntityStorageSystem.cs @@ -3,6 +3,7 @@ using System.Linq; using System.Numerics; using Content.Shared.Body.Components; using Content.Shared.Destructible; +using Content.Shared.Foldable; using Content.Shared.Hands.Components; using Content.Shared.Interaction; using Content.Shared.Item; @@ -130,6 +131,13 @@ public abstract class SharedEntityStorageSystem : EntitySystem } } + protected void OnFoldAttempt(EntityUid uid, SharedEntityStorageComponent component, ref FoldAttemptEvent args) + { + if (args.Cancelled) + return; + args.Cancelled = component.Open || component.Contents.ContainedEntities.Count != 0; + } + protected void AddToggleOpenVerb(EntityUid uid, SharedEntityStorageComponent component, GetVerbsEvent args) { if (!args.CanAccess || !args.CanInteract)