Move MultipleTool to shared (#9964)
This commit is contained in:
@@ -1,72 +1,50 @@
|
||||
using Content.Client.Items.Components;
|
||||
using Content.Client.Message;
|
||||
using Content.Client.Stylesheets;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Client.UserInterface;
|
||||
using Robust.Client.UserInterface.Controls;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization.Manager.Attributes;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.ViewVariables;
|
||||
|
||||
namespace Content.Client.Tools.Components
|
||||
{
|
||||
[RegisterComponent]
|
||||
public sealed class MultipleToolComponent : SharedMultipleToolComponent, IItemStatus
|
||||
[ComponentReference(typeof(SharedMultipleToolComponent))]
|
||||
public sealed class MultipleToolComponent : SharedMultipleToolComponent
|
||||
{
|
||||
private string? _behavior;
|
||||
[ViewVariables(VVAccess.ReadWrite)]
|
||||
public bool UiUpdateNeeded;
|
||||
|
||||
[DataField("statusShowBehavior")]
|
||||
private bool _statusShowBehavior = true;
|
||||
public bool StatusShowBehavior = true;
|
||||
}
|
||||
|
||||
[ViewVariables(VVAccess.ReadWrite)] private bool _uiUpdateNeeded;
|
||||
[ViewVariables] public bool StatusShowBehavior => _statusShowBehavior;
|
||||
[ViewVariables] public string? Behavior => _behavior;
|
||||
public sealed class MultipleToolStatusControl : Control
|
||||
{
|
||||
private readonly MultipleToolComponent _parent;
|
||||
private readonly RichTextLabel _label;
|
||||
|
||||
public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
|
||||
public MultipleToolStatusControl(MultipleToolComponent parent)
|
||||
{
|
||||
base.HandleComponentState(curState, nextState);
|
||||
|
||||
if (curState is not MultipleToolComponentState tool) return;
|
||||
|
||||
_behavior = tool.QualityName;
|
||||
_uiUpdateNeeded = true;
|
||||
|
||||
_parent = parent;
|
||||
_label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } };
|
||||
_label.SetMarkup(_parent.StatusShowBehavior ? _parent.CurrentQualityName : string.Empty);
|
||||
AddChild(_label);
|
||||
}
|
||||
|
||||
public Control MakeControl() => new StatusControl(this);
|
||||
|
||||
private sealed class StatusControl : Control
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
private readonly MultipleToolComponent _parent;
|
||||
private readonly RichTextLabel _label;
|
||||
base.FrameUpdate(args);
|
||||
|
||||
public StatusControl(MultipleToolComponent parent)
|
||||
if (_parent.UiUpdateNeeded)
|
||||
{
|
||||
_parent = parent;
|
||||
_label = new RichTextLabel {StyleClasses = {StyleNano.StyleClassItemStatus}};
|
||||
AddChild(_label);
|
||||
|
||||
UpdateDraw();
|
||||
}
|
||||
|
||||
protected override void FrameUpdate(FrameEventArgs args)
|
||||
{
|
||||
base.FrameUpdate(args);
|
||||
|
||||
if (!_parent._uiUpdateNeeded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
_parent.UiUpdateNeeded = false;
|
||||
Update();
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
{
|
||||
_parent._uiUpdateNeeded = false;
|
||||
|
||||
_label.SetMarkup(_parent.StatusShowBehavior ? _parent.Behavior ?? string.Empty : string.Empty);
|
||||
}
|
||||
public void Update()
|
||||
{
|
||||
_label.SetMarkup(_parent.StatusShowBehavior ? _parent.CurrentQualityName : string.Empty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,48 @@
|
||||
using Content.Client.Items;
|
||||
using Content.Client.Tools.Components;
|
||||
using Content.Shared.Tools;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.GameObjects;
|
||||
using Robust.Client.GameObjects;
|
||||
using Robust.Shared.GameStates;
|
||||
|
||||
namespace Content.Client.Tools
|
||||
{
|
||||
public sealed class ToolSystem : EntitySystem
|
||||
public sealed class ToolSystem : SharedToolSystem
|
||||
{
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<WelderComponent, ComponentHandleState>(OnWelderHandleState);
|
||||
SubscribeLocalEvent<MultipleToolComponent, ItemStatusCollectMessage>(OnGetStatusMessage);
|
||||
}
|
||||
|
||||
public override void SetMultipleTool(EntityUid uid,
|
||||
SharedMultipleToolComponent? multiple = null,
|
||||
ToolComponent? tool = null,
|
||||
bool playSound = false,
|
||||
EntityUid? user = null)
|
||||
{
|
||||
if (!Resolve(uid, ref multiple))
|
||||
return;
|
||||
|
||||
base.SetMultipleTool(uid, multiple, tool, playSound, user);
|
||||
((MultipleToolComponent)multiple).UiUpdateNeeded = true;
|
||||
|
||||
// TODO replace this with appearance + visualizer
|
||||
// in order to convert this to a specifier, the manner in which the sprite is specified in yaml needs to be updated.
|
||||
|
||||
if (multiple.Entries.Length > multiple.CurrentEntry && TryComp(uid, out SpriteComponent? sprite))
|
||||
{
|
||||
var current = multiple.Entries[multiple.CurrentEntry];
|
||||
if (current.Sprite != null)
|
||||
sprite.LayerSetSprite(0, current.Sprite);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnGetStatusMessage(EntityUid uid, MultipleToolComponent welder, ItemStatusCollectMessage args)
|
||||
{
|
||||
args.Controls.Add(new MultipleToolStatusControl(welder));
|
||||
}
|
||||
|
||||
private void OnWelderHandleState(EntityUid uid, WelderComponent welder, ref ComponentHandleState args)
|
||||
|
||||
@@ -1,39 +1,9 @@
|
||||
using Content.Shared.Tools;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Server.Tools.Components
|
||||
namespace Content.Server.Tools.Components;
|
||||
|
||||
[RegisterComponent]
|
||||
[ComponentReference(typeof(SharedMultipleToolComponent))]
|
||||
public sealed class MultipleToolComponent : SharedMultipleToolComponent
|
||||
{
|
||||
/// <summary>
|
||||
/// Not to be confused with Multitool (power)
|
||||
/// </summary>
|
||||
[RegisterComponent]
|
||||
public sealed class MultipleToolComponent : SharedMultipleToolComponent
|
||||
{
|
||||
[DataDefinition]
|
||||
public sealed class ToolEntry
|
||||
{
|
||||
[DataField("behavior", required:true)]
|
||||
public PrototypeFlags<ToolQualityPrototype> Behavior { get; } = new();
|
||||
|
||||
[DataField("useSound")]
|
||||
public SoundSpecifier? Sound { get; } = null;
|
||||
|
||||
[DataField("changeSound")]
|
||||
public SoundSpecifier? ChangeSound { get; } = null;
|
||||
|
||||
[DataField("sprite")]
|
||||
public SpriteSpecifier? Sprite { get; } = null;
|
||||
}
|
||||
|
||||
[DataField("entries", required:true)]
|
||||
public ToolEntry[] Entries { get; } = Array.Empty<ToolEntry>();
|
||||
|
||||
[ViewVariables]
|
||||
public int CurrentEntry = 0;
|
||||
|
||||
[ViewVariables]
|
||||
public string CurrentQualityName = string.Empty;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Tools.Components;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Tools;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Player;
|
||||
|
||||
namespace Content.Server.Tools
|
||||
{
|
||||
public sealed partial class ToolSystem
|
||||
{
|
||||
private void InitializeMultipleTools()
|
||||
{
|
||||
SubscribeLocalEvent<MultipleToolComponent, ComponentStartup>(OnMultipleToolStartup);
|
||||
SubscribeLocalEvent<MultipleToolComponent, ActivateInWorldEvent>(OnMultipleToolActivated);
|
||||
SubscribeLocalEvent<MultipleToolComponent, ComponentGetState>(OnMultipleToolGetState);
|
||||
}
|
||||
|
||||
private void OnMultipleToolStartup(EntityUid uid, MultipleToolComponent multiple, ComponentStartup args)
|
||||
{
|
||||
// Only set the multiple tool if we have a tool component.
|
||||
if(EntityManager.TryGetComponent(uid, out ToolComponent? tool))
|
||||
SetMultipleTool(uid, multiple, tool);
|
||||
}
|
||||
|
||||
private void OnMultipleToolActivated(EntityUid uid, MultipleToolComponent multiple, ActivateInWorldEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = CycleMultipleTool(uid, multiple);
|
||||
}
|
||||
|
||||
private void OnMultipleToolGetState(EntityUid uid, MultipleToolComponent multiple, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new MultipleToolComponentState(multiple.CurrentQualityName);
|
||||
}
|
||||
|
||||
public bool CycleMultipleTool(EntityUid uid, MultipleToolComponent? multiple = null)
|
||||
{
|
||||
if (!Resolve(uid, ref multiple))
|
||||
return false;
|
||||
|
||||
if (multiple.Entries.Length == 0)
|
||||
return false;
|
||||
|
||||
multiple.CurrentEntry = (multiple.CurrentEntry + 1) % multiple.Entries.Length;
|
||||
SetMultipleTool(uid, multiple);
|
||||
|
||||
var current = multiple.Entries[multiple.CurrentEntry];
|
||||
|
||||
if(current.ChangeSound is {} changeSound)
|
||||
SoundSystem.Play(changeSound.GetSound(), Filter.Pvs(uid), uid);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public void SetMultipleTool(EntityUid uid, MultipleToolComponent? multiple = null, ToolComponent? tool = null, SpriteComponent? sprite = null)
|
||||
{
|
||||
if (!Resolve(uid, ref multiple, ref tool))
|
||||
return;
|
||||
|
||||
// Sprite is optional.
|
||||
Resolve(uid, ref sprite, false);
|
||||
|
||||
if (multiple.Entries.Length == 0)
|
||||
{
|
||||
multiple.CurrentQualityName = Loc.GetString("multiple-tool-component-no-behavior");
|
||||
multiple.Dirty();
|
||||
return;
|
||||
}
|
||||
|
||||
var current = multiple.Entries[multiple.CurrentEntry];
|
||||
|
||||
tool.UseSound = current.Sound;
|
||||
tool.Qualities = current.Behavior;
|
||||
|
||||
if (_prototypeManager.TryIndex(current.Behavior.First(), out ToolQualityPrototype? quality))
|
||||
{
|
||||
multiple.CurrentQualityName = Loc.GetString(quality.Name);
|
||||
}
|
||||
|
||||
multiple.Dirty();
|
||||
|
||||
if (current.Sprite == null || sprite == null)
|
||||
return;
|
||||
|
||||
sprite.LayerSetSprite(0, current.Sprite);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +1,25 @@
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Content.Server.Atmos.EntitySystems;
|
||||
using Content.Server.Chemistry.EntitySystems;
|
||||
using Content.Server.DoAfter;
|
||||
using Content.Server.Popups;
|
||||
using Content.Shared.Audio;
|
||||
using Content.Shared.Item;
|
||||
using Content.Shared.Tools;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Server.GameObjects;
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Player;
|
||||
using Robust.Shared.Prototypes;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Content.Server.Tools
|
||||
{
|
||||
// TODO move tool system to shared, and make it a friend of Tool Component.
|
||||
public sealed partial class ToolSystem : EntitySystem
|
||||
public sealed partial class ToolSystem : SharedToolSystem
|
||||
{
|
||||
[Dependency] private readonly ITileDefinitionManager _tileDefinitionManager = default!;
|
||||
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly DoAfterSystem _doAfterSystem = default!;
|
||||
[Dependency] private readonly SolutionContainerSystem _solutionContainerSystem = default!;
|
||||
[Dependency] private readonly AtmosphereSystem _atmosphereSystem = default!;
|
||||
@@ -34,7 +33,6 @@ namespace Content.Server.Tools
|
||||
|
||||
InitializeTilePrying();
|
||||
InitializeWelders();
|
||||
InitializeMultipleTools();
|
||||
|
||||
SubscribeLocalEvent<ToolDoAfterComplete>(OnDoAfterComplete);
|
||||
SubscribeLocalEvent<ToolDoAfterCancelled>(OnDoAfterCancelled);
|
||||
|
||||
@@ -1,21 +1,47 @@
|
||||
using Robust.Shared.Audio;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Serialization;
|
||||
using Robust.Shared.Utility;
|
||||
|
||||
namespace Content.Shared.Tools.Components
|
||||
{
|
||||
[NetworkedComponent]
|
||||
public abstract class SharedMultipleToolComponent : Component
|
||||
{
|
||||
[DataDefinition]
|
||||
public sealed class ToolEntry
|
||||
{
|
||||
[DataField("behavior", required: true)]
|
||||
public PrototypeFlags<ToolQualityPrototype> Behavior { get; } = new();
|
||||
|
||||
[DataField("useSound")]
|
||||
public SoundSpecifier? Sound { get; } = null;
|
||||
|
||||
[DataField("changeSound")]
|
||||
public SoundSpecifier? ChangeSound { get; } = null;
|
||||
|
||||
[DataField("sprite")]
|
||||
public SpriteSpecifier? Sprite { get; } = null;
|
||||
}
|
||||
|
||||
[DataField("entries", required: true)]
|
||||
public ToolEntry[] Entries { get; } = Array.Empty<ToolEntry>();
|
||||
|
||||
[ViewVariables]
|
||||
public uint CurrentEntry = 0;
|
||||
|
||||
[ViewVariables]
|
||||
public string CurrentQualityName = String.Empty;
|
||||
}
|
||||
|
||||
[NetSerializable, Serializable]
|
||||
public sealed class MultipleToolComponentState : ComponentState
|
||||
{
|
||||
public string QualityName { get; }
|
||||
public readonly uint Selected;
|
||||
|
||||
public MultipleToolComponentState(string qualityName)
|
||||
public MultipleToolComponentState(uint selected)
|
||||
{
|
||||
QualityName = qualityName;
|
||||
Selected = selected;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
using System.Linq;
|
||||
using Content.Shared.Interaction;
|
||||
using Content.Shared.Tools.Components;
|
||||
using Robust.Shared.GameStates;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Shared.Tools;
|
||||
|
||||
public abstract class SharedToolSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _protoMan = default!;
|
||||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
SubscribeLocalEvent<SharedMultipleToolComponent, ComponentStartup>(OnMultipleToolStartup);
|
||||
SubscribeLocalEvent<SharedMultipleToolComponent, ActivateInWorldEvent>(OnMultipleToolActivated);
|
||||
SubscribeLocalEvent<SharedMultipleToolComponent, ComponentGetState>(OnMultipleToolGetState);
|
||||
SubscribeLocalEvent<SharedMultipleToolComponent, ComponentHandleState>(OnMultipleToolHandleState);
|
||||
}
|
||||
|
||||
private void OnMultipleToolHandleState(EntityUid uid, SharedMultipleToolComponent component, ref ComponentHandleState args)
|
||||
{
|
||||
if (args.Current is not MultipleToolComponentState state)
|
||||
return;
|
||||
|
||||
component.CurrentEntry = state.Selected;
|
||||
SetMultipleTool(uid, component);
|
||||
}
|
||||
|
||||
private void OnMultipleToolStartup(EntityUid uid, SharedMultipleToolComponent multiple, ComponentStartup args)
|
||||
{
|
||||
// Only set the multiple tool if we have a tool component.
|
||||
if(EntityManager.TryGetComponent(uid, out ToolComponent? tool))
|
||||
SetMultipleTool(uid, multiple, tool);
|
||||
}
|
||||
|
||||
private void OnMultipleToolActivated(EntityUid uid, SharedMultipleToolComponent multiple, ActivateInWorldEvent args)
|
||||
{
|
||||
if (args.Handled)
|
||||
return;
|
||||
|
||||
args.Handled = CycleMultipleTool(uid, multiple, args.User);
|
||||
}
|
||||
|
||||
private void OnMultipleToolGetState(EntityUid uid, SharedMultipleToolComponent multiple, ref ComponentGetState args)
|
||||
{
|
||||
args.State = new MultipleToolComponentState(multiple.CurrentEntry);
|
||||
}
|
||||
|
||||
public bool CycleMultipleTool(EntityUid uid, SharedMultipleToolComponent? multiple = null, EntityUid? user = null)
|
||||
{
|
||||
if (!Resolve(uid, ref multiple))
|
||||
return false;
|
||||
|
||||
if (multiple.Entries.Length == 0)
|
||||
return false;
|
||||
|
||||
multiple.CurrentEntry = (uint) ((multiple.CurrentEntry + 1) % multiple.Entries.Length);
|
||||
SetMultipleTool(uid, multiple, playSound: true, user: user);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public virtual void SetMultipleTool(EntityUid uid,
|
||||
SharedMultipleToolComponent? multiple = null,
|
||||
ToolComponent? tool = null,
|
||||
bool playSound = false,
|
||||
EntityUid? user = null)
|
||||
{
|
||||
if (!Resolve(uid, ref multiple, ref tool))
|
||||
return;
|
||||
|
||||
Dirty(multiple);
|
||||
|
||||
if (multiple.Entries.Length <= multiple.CurrentEntry)
|
||||
{
|
||||
multiple.CurrentQualityName = Loc.GetString("multiple-tool-component-no-behavior");
|
||||
return;
|
||||
}
|
||||
|
||||
var current = multiple.Entries[multiple.CurrentEntry];
|
||||
tool.UseSound = current.Sound;
|
||||
tool.Qualities = current.Behavior;
|
||||
|
||||
if (playSound && current.ChangeSound != null)
|
||||
_audioSystem.PlayPredicted(current.ChangeSound, uid, user);
|
||||
|
||||
if (_protoMan.TryIndex(current.Behavior.First(), out ToolQualityPrototype? quality))
|
||||
multiple.CurrentQualityName = Loc.GetString(quality.Name);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user