-
Notifications
You must be signed in to change notification settings - Fork 622
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
238 additions
and
2 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
Content.Client/_NF/Tools/Components/ComponentCyclerStatusControl.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using Content.Client.Message; | ||
using Content.Client.Stylesheets; | ||
using Content.Shared._NF.Item.ItemToggle.Components; | ||
using Content.Shared.Tools.Components; | ||
using Robust.Client.UserInterface; | ||
using Robust.Client.UserInterface.Controls; | ||
using Robust.Shared.Timing; | ||
|
||
namespace Content.Client._NF.Tools.Components; | ||
|
||
public sealed class ComponentCyclerStatusControl : Control | ||
{ | ||
private readonly ComponentCyclerComponent _parent; | ||
private readonly RichTextLabel _label; | ||
|
||
public ComponentCyclerStatusControl(ComponentCyclerComponent parent) | ||
{ | ||
_parent = parent; | ||
_label = new RichTextLabel { StyleClasses = { StyleNano.StyleClassItemStatus } }; | ||
_label.SetMarkup(_parent.StatusShowBehavior ? _parent.Entries[_parent.CurrentEntry].QualityName : string.Empty); | ||
AddChild(_label); | ||
} | ||
|
||
protected override void FrameUpdate(FrameEventArgs args) | ||
{ | ||
base.FrameUpdate(args); | ||
|
||
if (_parent.UiUpdateNeeded) | ||
{ | ||
_parent.UiUpdateNeeded = false; | ||
Update(); | ||
} | ||
} | ||
|
||
public void Update() | ||
{ | ||
_label.SetMarkup(_parent.StatusShowBehavior ? _parent.Entries[_parent.CurrentEntry].QualityName : string.Empty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using Content.Client._NF.Tools.Components; | ||
using Content.Client.Items; | ||
using Content.Shared._NF.Item.ItemToggle.Components; | ||
|
||
namespace Content.Client._NF.Tools.Systems; | ||
|
||
public sealed class ComponentCyclerSystem : EntitySystem | ||
{ | ||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
Subs.ItemStatus<ComponentCyclerComponent>(ent => new ComponentCyclerStatusControl(ent)); | ||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
Content.Shared/_NF/Item/ItemToggle/ComponentCyclerSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
using Content.Shared._NF.Item.ItemToggle.Components; | ||
using Content.Shared.Interaction; | ||
using Robust.Shared.Audio.Systems; | ||
using Robust.Shared.Prototypes; | ||
|
||
namespace Content.Shared._NF.Item.ItemToggle; | ||
|
||
/// <summary> | ||
/// Handles <see cref="ComponentCyclerComponent"/> component manipulation. | ||
/// </summary> | ||
public sealed class ComponentCyclerSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly SharedAudioSystem _audioSystem = default!; | ||
[Dependency] private readonly IPrototypeManager _protoMan = default!; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
//SubscribeLocalEvent<ComponentCyclerComponent, ItemToggledEvent>(OnCycle); | ||
|
||
SubscribeLocalEvent<ComponentCyclerComponent, ComponentStartup>(OnComponentCyclerStartup); | ||
SubscribeLocalEvent<ComponentCyclerComponent, ActivateInWorldEvent>(OnComponentCyclerActivated); | ||
SubscribeLocalEvent<ComponentCyclerComponent, AfterAutoHandleStateEvent>(OnComponentCyclerHandleState); | ||
} | ||
|
||
private void OnComponentCyclerHandleState(Entity<ComponentCyclerComponent> ent, ref AfterAutoHandleStateEvent args) | ||
{ | ||
SetComponentCyclerInternal(ent, ent.Comp.CurrentEntry); | ||
} | ||
|
||
private void OnComponentCyclerStartup(Entity<ComponentCyclerComponent> ent, ref ComponentStartup args) | ||
{ | ||
SetComponentCyclerInternal(ent, ent.Comp.CurrentEntry); | ||
} | ||
|
||
private void OnComponentCyclerActivated(Entity<ComponentCyclerComponent> ent, ref ActivateInWorldEvent args) | ||
{ | ||
if (args.Handled || !args.Complex) | ||
return; | ||
|
||
args.Handled = CycleComponentCyclerInternal(ent, args.User); | ||
} | ||
|
||
public bool CycleComponentCycler(Entity<ComponentCyclerComponent?> ent, EntityUid? user = null) | ||
{ | ||
if (!Resolve(ent, ref ent.Comp)) | ||
return false; | ||
return CycleComponentCyclerInternal((ent.Owner, ent.Comp), user); | ||
} | ||
|
||
public void SetComponentCycler(Entity<ComponentCyclerComponent?> ent, | ||
int newIndex, | ||
ComponentRegistry? comp = null, | ||
bool playSound = false, | ||
EntityUid? user = null) | ||
{ | ||
if (!Resolve(ent, ref ent.Comp)) | ||
return; | ||
SetComponentCyclerInternal((ent.Owner, ent.Comp), newIndex, comp, playSound, user); | ||
} | ||
|
||
private bool CycleComponentCyclerInternal(Entity<ComponentCyclerComponent> ent, EntityUid? user = null) | ||
{ | ||
if (ent.Comp.Entries.Length == 0) | ||
return false; | ||
|
||
var newIndex = ((ent.Comp.CurrentEntry + 1) % ent.Comp.Entries.Length); | ||
SetComponentCyclerInternal(ent, newIndex, playSound: true, user: user); | ||
|
||
return true; | ||
} | ||
|
||
public void SetComponentCyclerInternal(Entity<ComponentCyclerComponent> ent, | ||
int newIndex, | ||
ComponentRegistry? comp = null, | ||
bool playSound = false, | ||
EntityUid? user = null) | ||
{ | ||
if (newIndex >= ent.Comp.Entries.Length) | ||
return; | ||
|
||
// Remove previous components if index is within bounds | ||
if (ent.Comp.CurrentEntry < ent.Comp.Entries.Length) | ||
{ | ||
var previous = ent.Comp.Entries[ent.Comp.CurrentEntry]; | ||
|
||
EntityManager.RemoveComponents(ent, previous.Components); | ||
} | ||
|
||
// Update previous entry | ||
ent.Comp.CurrentEntry = newIndex; | ||
var current = ent.Comp.Entries[ent.Comp.CurrentEntry]; | ||
|
||
// Add new components | ||
EntityManager.AddComponents(ent, current.Components); | ||
|
||
if (playSound && current.ChangeSound != null) | ||
_audioSystem.PlayPredicted(current.ChangeSound, ent, user); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
Content.Shared/_NF/Item/ItemToggle/Components/ComponentCyclerComponent.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using Robust.Shared.Audio; | ||
using Robust.Shared.GameStates; | ||
using Robust.Shared.Prototypes; | ||
using Robust.Shared.Utility; | ||
|
||
namespace Content.Shared._NF.Item.ItemToggle.Components; | ||
|
||
/// <summary> | ||
/// Adds or removes components when toggled. | ||
/// Requires <see cref="ComponentCyclerComponent"/>. | ||
/// </summary> | ||
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState(true)] | ||
public sealed partial class ComponentCyclerComponent : Component | ||
{ | ||
[DataDefinition] | ||
public sealed partial class CompEntry | ||
{ | ||
[DataField(required: true)] | ||
public ComponentRegistry Components = new(); | ||
|
||
[DataField] | ||
public SoundSpecifier? UseSound; | ||
|
||
[DataField] | ||
public SoundSpecifier? ChangeSound; | ||
|
||
[DataField] | ||
public SpriteSpecifier? Sprite; | ||
|
||
[DataField] | ||
public string QualityName = string.Empty; | ||
} | ||
|
||
[DataField(required: true)] | ||
public CompEntry[] Entries { get; private set; } = Array.Empty<CompEntry>(); | ||
|
||
[ViewVariables] | ||
[AutoNetworkedField] | ||
public int CurrentEntry = 0; | ||
|
||
[ViewVariables(VVAccess.ReadWrite)] | ||
public bool UiUpdateNeeded; | ||
|
||
[DataField] | ||
public bool StatusShowBehavior = true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters