Crayons ECS (#6364)

This commit is contained in:
metalgearsloth
2022-02-06 23:32:32 +11:00
committed by GitHub
parent 1e10314900
commit 627cbba2b6
6 changed files with 207 additions and 187 deletions

View File

@@ -1,72 +1,15 @@
using Content.Client.Items.Components;
using Content.Client.Message;
using Content.Client.Stylesheets;
using Content.Shared.Crayon;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.Localization;
using Robust.Shared.Timing;
using Robust.Shared.ViewVariables;
namespace Content.Client.Crayon
{
[RegisterComponent]
public class CrayonComponent : SharedCrayonComponent, IItemStatus
public sealed class CrayonComponent : SharedCrayonComponent
{
[ViewVariables(VVAccess.ReadWrite)] private bool _uiUpdateNeeded;
[ViewVariables(VVAccess.ReadWrite)] private string Color => _color;
[ViewVariables] private int Charges { get; set; }
[ViewVariables] private int Capacity { get; set; }
Control IItemStatus.MakeControl()
{
return new StatusControl(this);
}
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not CrayonComponentState state)
return;
_color = state.Color;
SelectedState = state.State;
Charges = state.Charges;
Capacity = state.Capacity;
_uiUpdateNeeded = true;
}
private sealed class StatusControl : Control
{
private readonly CrayonComponent _parent;
private readonly RichTextLabel _label;
public StatusControl(CrayonComponent parent)
{
_parent = parent;
_label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } };
AddChild(_label);
parent._uiUpdateNeeded = true;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (!_parent._uiUpdateNeeded)
{
return;
}
_parent._uiUpdateNeeded = false;
_label.SetMarkup(Loc.GetString("crayon-drawing-label",
("color",_parent.Color),
("state",_parent.SelectedState),
("charges", _parent.Charges),
("capacity",_parent.Capacity)));
}
}
[ViewVariables(VVAccess.ReadWrite)] public bool UIUpdateNeeded;
[ViewVariables(VVAccess.ReadWrite)] public string Color => _color;
[ViewVariables] public int Charges { get; set; }
[ViewVariables] public int Capacity { get; set; }
}
}

View File

@@ -0,0 +1,72 @@
using Content.Client.Items;
using Content.Client.Message;
using Content.Client.Stylesheets;
using Content.Shared.Crayon;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Shared.GameObjects;
using Robust.Shared.GameStates;
using Robust.Shared.Localization;
using Robust.Shared.Timing;
namespace Content.Client.Crayon;
public sealed class CrayonSystem : EntitySystem
{
// Didn't do in shared because I don't think most of the server stuff can be predicted.
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<CrayonComponent, ComponentHandleState>(OnCrayonHandleState);
SubscribeLocalEvent<CrayonComponent, ItemStatusCollectMessage>(OnCrayonItemStatus);
}
private static void OnCrayonHandleState(EntityUid uid, CrayonComponent component, ref ComponentHandleState args)
{
if (args.Current is not CrayonComponentState state) return;
component._color = state.Color;
component.SelectedState = state.State;
component.Charges = state.Charges;
component.Capacity = state.Capacity;
component.UIUpdateNeeded = true;
}
private static void OnCrayonItemStatus(EntityUid uid, CrayonComponent component, ItemStatusCollectMessage args)
{
args.Controls.Add(new StatusControl(component));
}
private sealed class StatusControl : Control
{
private readonly CrayonComponent _parent;
private readonly RichTextLabel _label;
public StatusControl(CrayonComponent parent)
{
_parent = parent;
_label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } };
AddChild(_label);
parent.UIUpdateNeeded = true;
}
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if (!_parent.UIUpdateNeeded)
{
return;
}
_parent.UIUpdateNeeded = false;
_label.SetMarkup(Loc.GetString("crayon-drawing-label",
("color",_parent.Color),
("state",_parent.SelectedState),
("charges", _parent.Charges),
("capacity",_parent.Capacity)));
}
}
}

View File

@@ -47,6 +47,7 @@ namespace Content.Client.Crayon.UI
if (disposing)
{
_menu?.Close();
_menu = null;
}
}
}