Stack components are now entirely logicless.

- GetState is handled in SharedStackSystem
- Adds friend attributes to the stack components
This commit is contained in:
Vera Aguilera Puerto
2021-08-30 11:49:09 +02:00
parent f15ed2ba50
commit d1fe278afc
5 changed files with 19 additions and 20 deletions

View File

@@ -4,6 +4,7 @@ using Content.Client.Stylesheets;
using Content.Shared.Stacks; using Content.Shared.Stacks;
using Robust.Client.UserInterface; using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.Controls;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.Localization; using Robust.Shared.Localization;
using Robust.Shared.Timing; using Robust.Shared.Timing;
@@ -11,23 +12,18 @@ using Robust.Shared.ViewVariables;
namespace Content.Client.Stack namespace Content.Client.Stack
{ {
[RegisterComponent] [RegisterComponent, Friend(typeof(StackSystem), typeof(StatusControl))]
[ComponentReference(typeof(SharedStackComponent))] [ComponentReference(typeof(SharedStackComponent))]
public class StackComponent : SharedStackComponent, IItemStatus public class StackComponent : SharedStackComponent, IItemStatus
{ {
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables]
private bool _uiUpdateNeeded; public bool UiUpdateNeeded { get; set; }
public Control MakeControl() public Control MakeControl()
{ {
return new StatusControl(this); return new StatusControl(this);
} }
public void DirtyUI()
{
_uiUpdateNeeded = true;
}
private sealed class StatusControl : Control private sealed class StatusControl : Control
{ {
private readonly StackComponent _parent; private readonly StackComponent _parent;
@@ -39,19 +35,19 @@ namespace Content.Client.Stack
_label = new RichTextLabel {StyleClasses = {StyleNano.StyleClassItemStatus}}; _label = new RichTextLabel {StyleClasses = {StyleNano.StyleClassItemStatus}};
AddChild(_label); AddChild(_label);
parent._uiUpdateNeeded = true; parent.UiUpdateNeeded = true;
} }
protected override void FrameUpdate(FrameEventArgs args) protected override void FrameUpdate(FrameEventArgs args)
{ {
base.FrameUpdate(args); base.FrameUpdate(args);
if (!_parent._uiUpdateNeeded) if (!_parent.UiUpdateNeeded)
{ {
return; return;
} }
_parent._uiUpdateNeeded = false; _parent.UiUpdateNeeded = false;
_label.SetMarkup(Loc.GetString("comp-stack-status", ("count", _parent.Count))); _label.SetMarkup(Loc.GetString("comp-stack-status", ("count", _parent.Count)));
} }

View File

@@ -17,7 +17,7 @@ namespace Content.Client.Stack
private void OnStackCountChanged(EntityUid uid, StackComponent component, StackCountChangedEvent args) private void OnStackCountChanged(EntityUid uid, StackComponent component, StackCountChangedEvent args)
{ {
// Dirty the UI now that the stack count has changed. // Dirty the UI now that the stack count has changed.
component.DirtyUI(); component.UiUpdateNeeded = true;
} }
} }
} }

View File

@@ -1,11 +1,12 @@
using Content.Shared.Stacks; using Content.Shared.Stacks;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables; using Robust.Shared.ViewVariables;
namespace Content.Server.Stack namespace Content.Server.Stack
{ {
// TODO: Naming and presentation and such could use some improvement. // TODO: Naming and presentation and such could use some improvement.
[RegisterComponent] [RegisterComponent, Friend(typeof(StackSystem))]
[ComponentReference(typeof(SharedStackComponent))] [ComponentReference(typeof(SharedStackComponent))]
public class StackComponent : SharedStackComponent public class StackComponent : SharedStackComponent
{ {

View File

@@ -1,4 +1,5 @@
using System; using System;
using Robust.Shared.Analyzers;
using Robust.Shared.GameObjects; using Robust.Shared.GameObjects;
using Robust.Shared.GameStates; using Robust.Shared.GameStates;
using Robust.Shared.Players; using Robust.Shared.Players;
@@ -9,7 +10,7 @@ using Robust.Shared.ViewVariables;
namespace Content.Shared.Stacks namespace Content.Shared.Stacks
{ {
[NetworkedComponent()] [NetworkedComponent, Friend(typeof(SharedStackSystem))]
public abstract class SharedStackComponent : Component, ISerializationHooks public abstract class SharedStackComponent : Component, ISerializationHooks
{ {
public sealed override string Name => "Stack"; public sealed override string Name => "Stack";
@@ -20,7 +21,7 @@ namespace Content.Shared.Stacks
/// <summary> /// <summary>
/// Current stack count. /// Current stack count.
/// Do NOT set this directly, raise the <see cref="StackChangeCountEvent"/> event instead. /// Do NOT set this directly, use the <see cref="SharedStackSystem.SetCount"/> method instead.
/// </summary> /// </summary>
[ViewVariables(VVAccess.ReadWrite)] [ViewVariables(VVAccess.ReadWrite)]
[DataField("count")] [DataField("count")]
@@ -35,11 +36,6 @@ namespace Content.Shared.Stacks
[ViewVariables] [ViewVariables]
public int AvailableSpace => MaxCount - Count; public int AvailableSpace => MaxCount - Count;
public override ComponentState GetComponentState(ICommonSession player)
{
return new StackComponentState(Count, MaxCount);
}
} }
[Serializable, NetSerializable] [Serializable, NetSerializable]

View File

@@ -13,6 +13,7 @@ namespace Content.Shared.Stacks
{ {
base.Initialize(); base.Initialize();
SubscribeLocalEvent<SharedStackComponent, ComponentGetState>(OnStackGetState);
SubscribeLocalEvent<SharedStackComponent, ComponentHandleState>(OnStackHandleState); SubscribeLocalEvent<SharedStackComponent, ComponentHandleState>(OnStackHandleState);
SubscribeLocalEvent<SharedStackComponent, ComponentStartup>(OnStackStarted); SubscribeLocalEvent<SharedStackComponent, ComponentStartup>(OnStackStarted);
SubscribeLocalEvent<SharedStackComponent, ExaminedEvent>(OnStackExamined); SubscribeLocalEvent<SharedStackComponent, ExaminedEvent>(OnStackExamined);
@@ -62,6 +63,11 @@ namespace Content.Shared.Stacks
RaiseLocalEvent(uid, new StackCountChangedEvent(old, component.Count)); RaiseLocalEvent(uid, new StackCountChangedEvent(old, component.Count));
} }
private void OnStackGetState(EntityUid uid, SharedStackComponent component, ref ComponentGetState args)
{
args.State = new StackComponentState(component.Count, component.MaxCount);
}
private void OnStackHandleState(EntityUid uid, SharedStackComponent component, ref ComponentHandleState args) private void OnStackHandleState(EntityUid uid, SharedStackComponent component, ref ComponentHandleState args)
{ {
if (args.Current is not StackComponentState cast) if (args.Current is not StackComponentState cast)