diff --git a/Content.Server/Chemistry/EntitySystems/ReagentDispenserSystem.cs b/Content.Server/Chemistry/EntitySystems/ReagentDispenserSystem.cs index 2d751ba441..1a11ccf139 100644 --- a/Content.Server/Chemistry/EntitySystems/ReagentDispenserSystem.cs +++ b/Content.Server/Chemistry/EntitySystems/ReagentDispenserSystem.cs @@ -24,7 +24,7 @@ namespace Content.Server.Chemistry.EntitySystems /// /// [UsedImplicitly] - public sealed class ReagentDispenserSystem : EntitySystem + public sealed partial class ReagentDispenserSystem : EntitySystem // Frontier - Made Partial, Auto-Label { [Dependency] private readonly AudioSystem _audioSystem = default!; [Dependency] private readonly SharedSolutionContainerSystem _solutionContainerSystem = default!; @@ -41,8 +41,8 @@ namespace Content.Server.Chemistry.EntitySystems SubscribeLocalEvent(SubscribeUpdateUiState); SubscribeLocalEvent(SubscribeUpdateUiState); - SubscribeLocalEvent(SubscribeUpdateUiState, after: [typeof(SharedStorageSystem)]); SubscribeLocalEvent(SubscribeUpdateUiState, after: [typeof(SharedStorageSystem)]); + //SubscribeLocalEvent(SubscribeUpdateUiState, after: [typeof(SharedStorageSystem)]); // Frontier - Auto-Label SubscribeLocalEvent(SubscribeUpdateUiState); SubscribeLocalEvent(OnSetDispenseAmountMessage); @@ -51,8 +51,8 @@ namespace Content.Server.Chemistry.EntitySystems SubscribeLocalEvent(OnClearContainerSolutionMessage); SubscribeLocalEvent(OnMapInit, before: new[] { typeof(ItemSlotsSystem) }); + InitializeAutoLabeling(); // Frontier - Auto-Label } - private void SubscribeUpdateUiState(Entity ent, ref T ev) { UpdateUiState(ent); diff --git a/Content.Server/_NF/Chemistry/Components/ReagentDispenserComponent.AutoLabel.cs b/Content.Server/_NF/Chemistry/Components/ReagentDispenserComponent.AutoLabel.cs new file mode 100644 index 0000000000..1c834663db --- /dev/null +++ b/Content.Server/_NF/Chemistry/Components/ReagentDispenserComponent.AutoLabel.cs @@ -0,0 +1,22 @@ +namespace Content.Server.Chemistry.Components; + +/// +/// Frontier - Extends ReagentDispenserComponent. +/// +/// Used primarily for the auto-labeling functionality. +/// +public sealed partial class ReagentDispenserComponent : Component +{ + /// + /// Returns if the component's entity has the ability to auto-label. + /// + [DataField] + public bool CanAutoLabel = true; + + /// + /// Returns if the entity has auto-labeling toggled on. + /// Will have no effect if is false. + /// + [ViewVariables] + public bool AutoLabelToggle = true; +} diff --git a/Content.Server/_NF/Chemistry/EntitySystems/ReagentDispenserSystem.AutoLabel.cs b/Content.Server/_NF/Chemistry/EntitySystems/ReagentDispenserSystem.AutoLabel.cs new file mode 100644 index 0000000000..431666de40 --- /dev/null +++ b/Content.Server/_NF/Chemistry/EntitySystems/ReagentDispenserSystem.AutoLabel.cs @@ -0,0 +1,97 @@ +using Content.Server.Chemistry.Components; +using Robust.Shared.Containers; +using Content.Shared.Chemistry.Reagent; +using Content.Shared.Verbs; +using Content.Shared.Examine; +using Content.Server.Popups; +using Content.Shared.Labels.EntitySystems; +using Content.Shared.Storage.EntitySystems; + +namespace Content.Server.Chemistry.EntitySystems; + +public sealed partial class ReagentDispenserSystem : EntitySystem +{ + [Dependency] private readonly LabelSystem _label = default!; + [Dependency] private readonly PopupSystem _popup = default!; + + private void InitializeAutoLabeling() + { + SubscribeLocalEvent(OnEntInserted, after: [typeof(SharedStorageSystem)]); + SubscribeLocalEvent>(OnExamineVerb); + SubscribeLocalEvent(OnExamined); + } + + private void OnEntInserted(Entity ent, ref EntInsertedIntoContainerMessage ev) + { + TryApplyAutoLabel(ent, ev.Entity); + SubscribeUpdateUiState(ent, ref ev); + } + + private void OnExamineVerb(Entity ent, ref GetVerbsEvent args) + { + if (!ent.Comp.CanAutoLabel) + return; + + args.Verbs.Add(new ExamineVerb() + { + Act = () => + { + SetAutoLabel(ent, !ent.Comp.AutoLabelToggle); + }, + Text = ent.Comp.AutoLabelToggle ? + Loc.GetString("reagent-dispenser-component-set-auto-label-off-verb") + : Loc.GetString("reagent-dispenser-component-set-auto-label-on-verb"), + Priority = -1, // Not important, low priority. + CloseMenu = true + }); + } + + private void OnExamined(Entity ent, ref ExaminedEvent args) + { + if (!args.IsInDetailsRange || !ent.Comp.CanAutoLabel) + return; + + if (ent.Comp.AutoLabelToggle) + args.PushMarkup(Loc.GetString("reagent-dispenser-component-examine-auto-label-on")); + else + args.PushMarkup(Loc.GetString("reagent-dispenser-component-examine-auto-label-off")); + } + + private void SetAutoLabel(Entity ent, bool autoLabel) + { + if (!ent.Comp.CanAutoLabel) + return; + + ent.Comp.AutoLabelToggle = autoLabel; + + var popupMessage = autoLabel ? Loc.GetString("reagent-dispenser-component-verb-auto-label-turn-on") + : Loc.GetString("reagent-dispenser-component-verb-auto-label-turn-off"); + + _popup.PopupEntity(popupMessage, ent.Owner); + } + + private void TryApplyAutoLabel(Entity dispenser, EntityUid insertedEntity) + { + if (!dispenser.Comp.CanAutoLabel) + return; + + if (!dispenser.Comp.AutoLabelToggle) + return; + + if (!_solutionContainerSystem.TryGetDrainableSolution(insertedEntity, out _, out var sol)) + return; + + if (sol.GetPrimaryReagentId() is not { } reagentProtoId) + return; + + if (!_prototypeManager.TryIndex(reagentProtoId.Prototype, out var reagent)) + return; + + var reagentQuantity = sol.GetReagentQuantity(reagentProtoId); + var totalQuantity = sol.Volume; + if (reagentQuantity == totalQuantity) + _label.Label(insertedEntity, reagent.LocalizedName); + else + _label.Label(insertedEntity, Loc.GetString("reagent-dispenser-component-impure-auto-label", ("reagent", reagent.LocalizedName), ("purity", 100.0f * reagentQuantity / totalQuantity))); + } +} diff --git a/Resources/Locale/en-US/_NF/chemistry/reagent-dispenser-component.ftl b/Resources/Locale/en-US/_NF/chemistry/reagent-dispenser-component.ftl new file mode 100644 index 0000000000..e217a0e015 --- /dev/null +++ b/Resources/Locale/en-US/_NF/chemistry/reagent-dispenser-component.ftl @@ -0,0 +1,8 @@ +# Frontier +reagent-dispenser-component-impure-auto-label = {$reagent} ({$purity}%) +reagent-dispenser-component-set-auto-label-on-verb = Turn on auto-labeler +reagent-dispenser-component-set-auto-label-off-verb = Turn off auto-labeler +reagent-dispenser-component-examine-auto-label-on = The auto-labeler is turned [color=darkgreen]on[/color]. +reagent-dispenser-component-examine-auto-label-off = The auto-labeler is turned [color=red]off[/color]. +reagent-dispenser-component-verb-auto-label-turn-on = The auto-labeler has been turned on. +reagent-dispenser-component-verb-auto-label-turn-off = The auto-labeler has been turned off.