From 4fafb55477b04eb12900068c1bae3f1d9ef524c5 Mon Sep 17 00:00:00 2001 From: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Date: Sun, 11 Jan 2026 23:48:01 +0100 Subject: [PATCH] Predict BarSignBoundUserinterface (#42364) fix bar sign prediction --- Content.Client/BarSign/BarSignSystem.cs | 53 ------------------- .../BarSign/BarSignVisualizerSystem.cs | 30 +++++++++++ .../BarSign/Ui/BarSignBoundUserInterface.cs | 19 +++---- Content.Shared/BarSign/BarSignComponent.cs | 22 ++++++++ Content.Shared/BarSign/BarSignSystem.cs | 29 ++++++++++ 5 files changed, 88 insertions(+), 65 deletions(-) delete mode 100644 Content.Client/BarSign/BarSignSystem.cs create mode 100644 Content.Client/BarSign/BarSignVisualizerSystem.cs diff --git a/Content.Client/BarSign/BarSignSystem.cs b/Content.Client/BarSign/BarSignSystem.cs deleted file mode 100644 index 1ea99864a1..0000000000 --- a/Content.Client/BarSign/BarSignSystem.cs +++ /dev/null @@ -1,53 +0,0 @@ -using Content.Client.BarSign.Ui; -using Content.Shared.BarSign; -using Content.Shared.Power; -using Robust.Client.GameObjects; -using Robust.Shared.Prototypes; - -namespace Content.Client.BarSign; - -public sealed class BarSignSystem : VisualizerSystem -{ - [Dependency] private readonly IPrototypeManager _prototypeManager = default!; - [Dependency] private readonly UserInterfaceSystem _ui = default!; - - public override void Initialize() - { - base.Initialize(); - SubscribeLocalEvent(OnAfterAutoHandleState); - } - - private void OnAfterAutoHandleState(EntityUid uid, BarSignComponent component, ref AfterAutoHandleStateEvent args) - { - if (_ui.TryGetOpenUi(uid, BarSignUiKey.Key, out var bui)) - bui.Update(component.Current); - - UpdateAppearance(uid, component); - } - - protected override void OnAppearanceChange(EntityUid uid, BarSignComponent component, ref AppearanceChangeEvent args) - { - UpdateAppearance(uid, component, args.Component, args.Sprite); - } - - private void UpdateAppearance(EntityUid id, BarSignComponent sign, AppearanceComponent? appearance = null, SpriteComponent? sprite = null) - { - if (!Resolve(id, ref appearance, ref sprite)) - return; - - AppearanceSystem.TryGetData(id, PowerDeviceVisuals.Powered, out var powered, appearance); - - if (powered - && sign.Current != null - && _prototypeManager.Resolve(sign.Current, out var proto)) - { - SpriteSystem.LayerSetSprite((id, sprite), 0, proto.Icon); - sprite.LayerSetShader(0, "unshaded"); - } - else - { - SpriteSystem.LayerSetRsiState((id, sprite), 0, "empty"); - sprite.LayerSetShader(0, null, null); - } - } -} diff --git a/Content.Client/BarSign/BarSignVisualizerSystem.cs b/Content.Client/BarSign/BarSignVisualizerSystem.cs new file mode 100644 index 0000000000..3e641fed70 --- /dev/null +++ b/Content.Client/BarSign/BarSignVisualizerSystem.cs @@ -0,0 +1,30 @@ +using Content.Shared.BarSign; +using Content.Shared.Power; +using Robust.Client.GameObjects; +using Robust.Shared.Prototypes; + +namespace Content.Client.BarSign; + +public sealed class BarSignVisualizerSystem : VisualizerSystem +{ + [Dependency] private readonly IPrototypeManager _prototypeManager = default!; + + protected override void OnAppearanceChange(EntityUid uid, BarSignComponent component, ref AppearanceChangeEvent args) + { + AppearanceSystem.TryGetData(uid, PowerDeviceVisuals.Powered, out var powered, args.Component); + AppearanceSystem.TryGetData(uid, BarSignVisuals.BarSignPrototype, out var currentSign, args.Component); + + if (powered + && currentSign != null + && _prototypeManager.Resolve(currentSign, out var proto)) + { + SpriteSystem.LayerSetSprite((uid, args.Sprite), 0, proto.Icon); + args.Sprite?.LayerSetShader(0, "unshaded"); + } + else + { + SpriteSystem.LayerSetRsiState((uid, args.Sprite), 0, "empty"); + args.Sprite?.LayerSetShader(0, null, null); + } + } +} diff --git a/Content.Client/BarSign/Ui/BarSignBoundUserInterface.cs b/Content.Client/BarSign/Ui/BarSignBoundUserInterface.cs index fe07f0f1d1..8265877edf 100644 --- a/Content.Client/BarSign/Ui/BarSignBoundUserInterface.cs +++ b/Content.Client/BarSign/Ui/BarSignBoundUserInterface.cs @@ -19,32 +19,27 @@ public sealed class BarSignBoundUserInterface(EntityUid owner, Enum uiKey) : Bou var sign = EntMan.GetComponentOrNull(Owner)?.Current is { } current ? _prototype.Index(current) : null; - var allSigns = Shared.BarSign.BarSignSystem.GetAllBarSigns(_prototype) + var allSigns = BarSignSystem.GetAllBarSigns(_prototype) .OrderBy(p => Loc.GetString(p.Name)) .ToList(); _menu = new(sign, allSigns); _menu.OnSignSelected += id => { - SendMessage(new SetBarSignMessage(id)); + SendPredictedMessage(new SetBarSignMessage(id)); }; _menu.OnClose += Close; _menu.OpenCentered(); } - public void Update(ProtoId? sign) + public override void Update() { - if (_prototype.Resolve(sign, out var signPrototype)) - _menu?.UpdateState(signPrototype); - } - - protected override void Dispose(bool disposing) - { - base.Dispose(disposing); - if (!disposing) + if (!EntMan.TryGetComponent(Owner, out var signComp)) return; - _menu?.Dispose(); + + if (_prototype.Resolve(signComp.Current, out var signPrototype)) + _menu?.UpdateState(signPrototype); } } diff --git a/Content.Shared/BarSign/BarSignComponent.cs b/Content.Shared/BarSign/BarSignComponent.cs index 98c6e815cc..f62d2f7a93 100644 --- a/Content.Shared/BarSign/BarSignComponent.cs +++ b/Content.Shared/BarSign/BarSignComponent.cs @@ -4,6 +4,10 @@ using Robust.Shared.Serialization; namespace Content.Shared.BarSign; +/// +/// Makes it possible to switch this entity's sprite and name using a BUI. +/// +/// [RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)] public sealed partial class BarSignComponent : Component { @@ -14,14 +18,32 @@ public sealed partial class BarSignComponent : Component public ProtoId? Current; } +/// +/// The key for the BoundUserInterface. +/// [Serializable, NetSerializable] public enum BarSignUiKey : byte { Key } +/// +/// The enum to be used for appearance data of the bar sign. +/// +[Serializable, NetSerializable] +public enum BarSignVisuals : byte +{ + BarSignPrototype, +} + +/// +/// Send from the client when setting the bar sign. +/// [Serializable, NetSerializable] public sealed class SetBarSignMessage(ProtoId sign) : BoundUserInterfaceMessage { + /// + /// The new prototype to use. + /// public ProtoId Sign = sign; } diff --git a/Content.Shared/BarSign/BarSignSystem.cs b/Content.Shared/BarSign/BarSignSystem.cs index 0c9770bfe9..57ba72ecf9 100644 --- a/Content.Shared/BarSign/BarSignSystem.cs +++ b/Content.Shared/BarSign/BarSignSystem.cs @@ -1,4 +1,5 @@ using System.Linq; +using Robust.Shared.GameObjects; using Robust.Shared.Prototypes; using Robust.Shared.Random; @@ -9,10 +10,14 @@ public sealed class BarSignSystem : EntitySystem [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IRobustRandom _random = default!; [Dependency] private readonly MetaDataSystem _metaData = default!; + [Dependency] private readonly SharedUserInterfaceSystem _ui = default!; + [Dependency] private readonly SharedAppearanceSystem _appearance = default!; + public override void Initialize() { SubscribeLocalEvent(OnMapInit); + SubscribeLocalEvent(OnAfterAutoHandleState); Subs.BuiEvents(BarSignUiKey.Key, subs => { @@ -29,25 +34,49 @@ public sealed class BarSignSystem : EntitySystem SetBarSign(ent, newPrototype); } + private void OnAfterAutoHandleState(Entity ent, ref AfterAutoHandleStateEvent args) + { + // Update the UI if the component was changed. + if (_ui.TryGetOpenUi(ent.Owner, BarSignUiKey.Key, out var bui)) + bui.Update(); + } + private void OnSetBarSignMessage(Entity ent, ref SetBarSignMessage args) { if (!_prototypeManager.Resolve(args.Sign, out var signPrototype)) return; + if (signPrototype.Hidden) + return; // Hidden signs cannot be selected from the BUI. + SetBarSign(ent, signPrototype); } + /// + /// Set the sprite, name and description of the bar sign to a given . + /// public void SetBarSign(Entity ent, BarSignPrototype newPrototype) { + if (ent.Comp.Current == newPrototype.ID) + return; + var meta = MetaData(ent); var name = Loc.GetString(newPrototype.Name); _metaData.SetEntityName(ent, name, meta); _metaData.SetEntityDescription(ent, Loc.GetString(newPrototype.Description), meta); + _appearance.SetData(ent.Owner, BarSignVisuals.BarSignPrototype, newPrototype.ID); ent.Comp.Current = newPrototype.ID; Dirty(ent); + + // Predict updating the BUI if it's open. + if (_ui.TryGetOpenUi(ent.Owner, BarSignUiKey.Key, out var bui)) + bui.Update(); } + /// + /// Returns a list of all s that are not hidden. + /// public static List GetAllBarSigns(IPrototypeManager prototypeManager) { return prototypeManager -- 2.52.0