Files
tbd-station-14/Content.Shared/Foldable/FoldableSystem.cs
paige404 2e7f01b99e Fix broken layer hiding on clothes with multiple equipment slots (#34080)
* Fix broken layer hiding on clothes with multiple equipment slots

* Refactor ToggleVisualLayers, HideLayerClothingComponent, and ClothingComponent to allow more
precise layer hide behavior and more CPU efficient layer toggling.

* Adjust HumanoidAppearaceSystem to track which slots are hiding a given layer (e.g. gas mask and welding mask)
Add documentation
Change gas masks to use the new HideLayerClothingComponent structure as an example of its usage

* Fix the delayed snout bug

* Misc cleanup

* Make `bool permanent` implicit from SlotFlags

any non-permanent visibility toggle with `SlotFlags.None` isn't supported with how its set up. And similarly, the slot flags argument does nothing if permanent = true. So IMO it makes more sense to infer it from a nullable arg.

* Split into separate system

Too much pasta

* Remove (hopefully unnecessary) refresh

* Fisk mask networking

AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

* Keep old behaviour, use clearer names?

I'm just guessing at what this was meant to do

* english

* Separate slot name & flag

* dirty = true

* fix comment

* Improved SetLayerVisibility with dirtying logic suggested by @ElectroJr

* Only set mask toggled if DisableOnFold is true

* FoldableClothingSystem fixes

* fix bandana state

* Better comment

---------

Co-authored-by: ElectroJr <leonsfriedrich@gmail.com>
2025-03-21 00:30:47 +11:00

168 lines
5.4 KiB
C#

using Content.Shared.Body.Components;
using Content.Shared.Buckle;
using Content.Shared.Buckle.Components;
using Content.Shared.Storage.Components;
using Content.Shared.Verbs;
using Robust.Shared.Containers;
using Robust.Shared.Serialization;
using Robust.Shared.Utility;
namespace Content.Shared.Foldable;
public sealed class FoldableSystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
[Dependency] private readonly SharedBuckleSystem _buckle = default!;
[Dependency] private readonly SharedContainerSystem _container = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<FoldableComponent, GetVerbsEvent<AlternativeVerb>>(AddFoldVerb);
SubscribeLocalEvent<FoldableComponent, AfterAutoHandleStateEvent>(OnHandleState);
SubscribeLocalEvent<FoldableComponent, ComponentInit>(OnFoldableInit);
SubscribeLocalEvent<FoldableComponent, ContainerGettingInsertedAttemptEvent>(OnInsertEvent);
SubscribeLocalEvent<FoldableComponent, InsertIntoEntityStorageAttemptEvent>(OnStoreThisAttempt);
SubscribeLocalEvent<FoldableComponent, StorageOpenAttemptEvent>(OnFoldableOpenAttempt);
SubscribeLocalEvent<FoldableComponent, StrapAttemptEvent>(OnStrapAttempt);
}
private void OnHandleState(EntityUid uid, FoldableComponent component, ref AfterAutoHandleStateEvent args)
{
SetFolded(uid, component, component.IsFolded);
}
private void OnFoldableInit(EntityUid uid, FoldableComponent component, ComponentInit args)
{
SetFolded(uid, component, component.IsFolded);
}
private void OnFoldableOpenAttempt(EntityUid uid, FoldableComponent component, ref StorageOpenAttemptEvent args)
{
if (component.IsFolded)
args.Cancelled = true;
}
public void OnStoreThisAttempt(EntityUid uid, FoldableComponent comp, ref InsertIntoEntityStorageAttemptEvent args)
{
if (comp.IsFolded)
args.Cancelled = true;
}
public void OnStrapAttempt(EntityUid uid, FoldableComponent comp, ref StrapAttemptEvent args)
{
if (comp.IsFolded)
args.Cancelled = true;
}
/// <summary>
/// Returns false if the entity isn't foldable.
/// </summary>
public bool IsFolded(EntityUid uid, FoldableComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
return component.IsFolded;
}
/// <summary>
/// Set the folded state of the given <see cref="FoldableComponent"/>
/// </summary>
public void SetFolded(EntityUid uid, FoldableComponent component, bool folded)
{
component.IsFolded = folded;
Dirty(uid, component);
_appearance.SetData(uid, FoldedVisuals.State, folded);
_buckle.StrapSetEnabled(uid, !component.IsFolded);
var ev = new FoldedEvent(folded);
RaiseLocalEvent(uid, ref ev);
}
private void OnInsertEvent(EntityUid uid, FoldableComponent component, ContainerGettingInsertedAttemptEvent args)
{
if (!component.IsFolded && !component.CanFoldInsideContainer)
args.Cancel();
}
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 unless enabled (locker, hands, inventory, whatever).
if (_container.IsEntityInContainer(uid) && !fold.CanFoldInsideContainer)
return false;
var ev = new FoldAttemptEvent(fold);
RaiseLocalEvent(uid, ref ev);
return !ev.Cancelled;
}
/// <summary>
/// Try to fold/unfold
/// </summary>
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<AlternativeVerb> 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(component.UnfoldVerbText) : Loc.GetString(component.FoldVerbText),
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
[Serializable, NetSerializable]
public enum FoldedVisuals : byte
{
State
}
}
/// <summary>
/// Event raised on an entity to determine if it can be folded.
/// </summary>
/// <param name="Cancelled"></param>
[ByRefEvent]
public record struct FoldAttemptEvent(FoldableComponent Comp, bool Cancelled = false);
/// <summary>
/// Event raised on an entity after it has been folded.
/// </summary>
/// <param name="IsFolded"></param>
[ByRefEvent]
public readonly record struct FoldedEvent(bool IsFolded);