Files
tbd-station-14/Content.Server/VoiceMask/VoiceMaskSystem.cs
beck-thompson fe2145d3b9 Voice Mask refactor (#30798)
* First commit

* Added base.Initialize()

* Voice wire fix (Electricty name)

* Various minor cleanups

* Localized default voice mask name

* Added VoiceOverride stuff

* Removed unused stuff

* Typo

* Better localized stuff

* Typo / spelling stuff / comments

* Blessed
2024-09-26 18:55:59 +02:00

104 lines
4.0 KiB
C#

using Content.Shared.Actions;
using Content.Shared.Administration.Logs;
using Content.Shared.Chat;
using Content.Shared.Clothing;
using Content.Shared.Database;
using Content.Shared.Inventory;
using Content.Shared.Popups;
using Content.Shared.Preferences;
using Content.Shared.Speech;
using Content.Shared.VoiceMask;
using Robust.Shared.Prototypes;
namespace Content.Server.VoiceMask;
public sealed partial class VoiceMaskSystem : EntitySystem
{
[Dependency] private readonly SharedUserInterfaceSystem _uiSystem = default!;
[Dependency] private readonly SharedPopupSystem _popupSystem = default!;
[Dependency] private readonly ISharedAdminLogManager _adminLogger = default!;
[Dependency] private readonly IPrototypeManager _proto = default!;
[Dependency] private readonly SharedActionsSystem _actions = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<VoiceMaskComponent, InventoryRelayedEvent<TransformSpeakerNameEvent>>(OnTransformSpeakerName);
SubscribeLocalEvent<VoiceMaskComponent, VoiceMaskChangeNameMessage>(OnChangeName);
SubscribeLocalEvent<VoiceMaskComponent, VoiceMaskChangeVerbMessage>(OnChangeVerb);
SubscribeLocalEvent<VoiceMaskComponent, ClothingGotEquippedEvent>(OnEquip);
SubscribeLocalEvent<VoiceMaskSetNameEvent>(OpenUI);
}
private void OnTransformSpeakerName(Entity<VoiceMaskComponent> entity, ref InventoryRelayedEvent<TransformSpeakerNameEvent> args)
{
args.Args.VoiceName = GetCurrentVoiceName(entity);
args.Args.SpeechVerb = entity.Comp.VoiceMaskSpeechVerb ?? args.Args.SpeechVerb;
}
#region User inputs from UI
private void OnChangeVerb(Entity<VoiceMaskComponent> entity, ref VoiceMaskChangeVerbMessage msg)
{
if (msg.Verb is { } id && !_proto.HasIndex<SpeechVerbPrototype>(id))
return;
entity.Comp.VoiceMaskSpeechVerb = msg.Verb;
// verb is only important to metagamers so no need to log as opposed to name
_popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-success"), entity, msg.Actor);
UpdateUI(entity);
}
private void OnChangeName(Entity<VoiceMaskComponent> entity, ref VoiceMaskChangeNameMessage message)
{
if (message.Name.Length > HumanoidCharacterProfile.MaxNameLength || message.Name.Length <= 0)
{
_popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-failure"), entity, message.Actor, PopupType.SmallCaution);
return;
}
entity.Comp.VoiceMaskName = message.Name;
_adminLogger.Add(LogType.Action, LogImpact.Medium, $"{ToPrettyString(message.Actor):player} set voice of {ToPrettyString(entity):mask}: {entity.Comp.VoiceMaskName}");
_popupSystem.PopupEntity(Loc.GetString("voice-mask-popup-success"), entity, message.Actor);
UpdateUI(entity);
}
#endregion
#region UI
private void OnEquip(EntityUid uid, VoiceMaskComponent component, ClothingGotEquippedEvent args)
{
_actions.AddAction(args.Wearer, ref component.ActionEntity, component.Action, uid);
}
private void OpenUI(VoiceMaskSetNameEvent ev)
{
var maskEntity = ev.Action.Comp.Container;
if (!TryComp<VoiceMaskComponent>(maskEntity, out var voiceMaskComp))
return;
if (!_uiSystem.HasUi(maskEntity.Value, VoiceMaskUIKey.Key))
return;
_uiSystem.OpenUi(maskEntity.Value, VoiceMaskUIKey.Key, ev.Performer);
UpdateUI((maskEntity.Value, voiceMaskComp));
}
private void UpdateUI(Entity<VoiceMaskComponent> entity)
{
if (_uiSystem.HasUi(entity, VoiceMaskUIKey.Key))
_uiSystem.SetUiState(entity.Owner, VoiceMaskUIKey.Key, new VoiceMaskBuiState(GetCurrentVoiceName(entity), entity.Comp.VoiceMaskSpeechVerb));
}
#endregion
#region Helper functions
private string GetCurrentVoiceName(Entity<VoiceMaskComponent> entity)
{
return entity.Comp.VoiceMaskName ?? Loc.GetString("voice-mask-default-name-override");
}
#endregion
}