From da91e0b7d8837f71a2916d9307a862b7963d6a0c Mon Sep 17 00:00:00 2001 From: fox Date: Mon, 9 Sep 2024 15:29:23 +0300 Subject: [PATCH] Sqash: Simple-Station/Einstein-Engines#893 FIX THE DAMN LANGUAGE WINDOW Fix another issue fix --- .../Language/LanguageMenuWindow.xaml.cs | 16 ++++++++++++- .../Language/Systems/LanguageSystem.cs | 6 +++++ .../Language/LanguageMenuUIController.cs | 24 +++++++++---------- Content.Server/Language/TranslatorSystem.cs | 24 ++++++++++++------- 4 files changed, 48 insertions(+), 22 deletions(-) diff --git a/Content.Client/Language/LanguageMenuWindow.xaml.cs b/Content.Client/Language/LanguageMenuWindow.xaml.cs index 11d1c290d16..ed6ec6b3e2d 100644 --- a/Content.Client/Language/LanguageMenuWindow.xaml.cs +++ b/Content.Client/Language/LanguageMenuWindow.xaml.cs @@ -1,4 +1,5 @@ using Content.Client.Language.Systems; +using Content.Shared.Language.Events; using Robust.Client.AutoGenerated; using Robust.Client.UserInterface.Controls; using Robust.Client.UserInterface.CustomControls; @@ -7,7 +8,7 @@ namespace Content.Client.Language; [GenerateTypedNameReferences] -public sealed partial class LanguageMenuWindow : DefaultWindow +public sealed partial class LanguageMenuWindow : DefaultWindow, IEntityEventSubscriber { private readonly LanguageSystem _clientLanguageSystem; private readonly List _entries = new(); @@ -17,6 +18,14 @@ public LanguageMenuWindow() { RobustXamlLoader.Load(this); _clientLanguageSystem = IoCManager.Resolve().GetEntitySystem(); + + _clientLanguageSystem.OnLanguagesChanged += OnUpdateState; + } + + protected override void Dispose(bool disposing) + { + base.Dispose(disposing); + _clientLanguageSystem.OnLanguagesChanged -= OnUpdateState; } protected override void Opened() @@ -28,6 +37,11 @@ protected override void Opened() } + private void OnUpdateState(object? sender, LanguagesUpdatedMessage args) + { + UpdateState(args.CurrentLanguage, args.Spoken); + } + public void UpdateState(string currentLanguage, List spokenLanguages) { var langName = Loc.GetString($"language-{currentLanguage}-name"); diff --git a/Content.Client/Language/Systems/LanguageSystem.cs b/Content.Client/Language/Systems/LanguageSystem.cs index 5dc2fc1f4e7..cb6bb60512b 100644 --- a/Content.Client/Language/Systems/LanguageSystem.cs +++ b/Content.Client/Language/Systems/LanguageSystem.cs @@ -29,6 +29,8 @@ public sealed class LanguageSystem : SharedLanguageSystem /// public List UnderstoodLanguages { get; private set; } = new(); + public event EventHandler? OnLanguagesChanged; + public override void Initialize() { base.Initialize(); @@ -39,9 +41,13 @@ public override void Initialize() private void OnLanguagesUpdated(LanguagesUpdatedMessage message) { + // TODO this entire thing is horrible. If someone is willing to refactor this, LanguageSpeakerComponent should become shared with SendOnlyToOwner = true + // That way, this system will be able to use the existing networking infrastructure instead of relying on this makeshift... whatever this is. CurrentLanguage = message.CurrentLanguage; SpokenLanguages = message.Spoken; UnderstoodLanguages = message.Understood; + + OnLanguagesChanged?.Invoke(this, message); } private void OnRunLevelChanged(object? sender, RunLevelChangedEventArgs args) diff --git a/Content.Client/UserInterface/Systems/Language/LanguageMenuUIController.cs b/Content.Client/UserInterface/Systems/Language/LanguageMenuUIController.cs index f36521ce819..e351a16bfb4 100644 --- a/Content.Client/UserInterface/Systems/Language/LanguageMenuUIController.cs +++ b/Content.Client/UserInterface/Systems/Language/LanguageMenuUIController.cs @@ -2,7 +2,6 @@ using Content.Client.Gameplay; using Content.Client.UserInterface.Controls; using Content.Shared.Input; -using Content.Shared.Language.Events; using Robust.Client.UserInterface.Controllers; using Robust.Client.UserInterface.Controls; using Robust.Shared.Input.Binding; @@ -18,12 +17,6 @@ public sealed class LanguageMenuUIController : UIController, IOnStateEntered UIManager.GetActiveUIWidgetOrNull()?.LanguageButton; - public override void Initialize() - { - SubscribeNetworkEvent((LanguagesUpdatedMessage message, EntitySessionEventArgs _) => - LanguageWindow?.UpdateState(message.CurrentLanguage, message.Spoken)); - } - public void OnStateEntered(GameplayState state) { DebugTools.Assert(LanguageWindow == null); @@ -31,6 +24,17 @@ public void OnStateEntered(GameplayState state) LanguageWindow = UIManager.CreateWindow(); LayoutContainer.SetAnchorPreset(LanguageWindow, LayoutContainer.LayoutPreset.CenterTop); + LanguageWindow.OnClose += () => + { + if (LanguageButton != null) + LanguageButton.Pressed = false; + }; + LanguageWindow.OnOpen += () => + { + if (LanguageButton != null) + LanguageButton.Pressed = true; + }; + CommandBinds.Builder.Bind(ContentKeyFunctions.OpenLanguageMenu, InputCmdHandler.FromDelegate(_ => ToggleWindow())).Register(); } @@ -60,12 +64,6 @@ public void LoadButton() return; LanguageButton.OnPressed += LanguageButtonPressed; - - if (LanguageWindow == null) - return; - - LanguageWindow.OnClose += () => LanguageButton.Pressed = false; - LanguageWindow.OnOpen += () => LanguageButton.Pressed = true; } private void LanguageButtonPressed(ButtonEventArgs args) diff --git a/Content.Server/Language/TranslatorSystem.cs b/Content.Server/Language/TranslatorSystem.cs index 6cdca124405..a893993e884 100644 --- a/Content.Server/Language/TranslatorSystem.cs +++ b/Content.Server/Language/TranslatorSystem.cs @@ -9,6 +9,7 @@ using Content.Shared.PowerCell; using Content.Shared.Language.Components.Translators; using Robust.Shared.Containers; +using Robust.Shared.Timing; namespace Content.Server.Language; @@ -27,7 +28,7 @@ public override void Initialize() SubscribeLocalEvent(OnProxyDetermineLanguages); SubscribeLocalEvent(OnTranslatorInserted); - SubscribeLocalEvent(OnTranslatorRemoved); + SubscribeLocalEvent(OnTranslatorParentChanged); SubscribeLocalEvent(OnTranslatorToggle); SubscribeLocalEvent(OnPowerCellSlotEmpty); } @@ -65,8 +66,7 @@ private void OnProxyDetermineLanguages(EntityUid uid, HoldsTranslatorComponent c private void OnTranslatorInserted(EntityUid translator, HandheldTranslatorComponent component, EntGotInsertedIntoContainerMessage args) { - if (args.Container.Owner is not {Valid: true} holder - || !EntityManager.HasComponent(holder)) + if (args.Container.Owner is not {Valid: true} holder || !HasComp(holder)) return; var intrinsic = EnsureComp(holder); @@ -75,14 +75,19 @@ private void OnTranslatorInserted(EntityUid translator, HandheldTranslatorCompon _language.UpdateEntityLanguages(holder); } - private void OnTranslatorRemoved(EntityUid translator, HandheldTranslatorComponent component, EntGotRemovedFromContainerMessage args) + private void OnTranslatorParentChanged(EntityUid translator, HandheldTranslatorComponent component, EntParentChangedMessage args) { - if (args.Container.Owner is not {Valid: true} holder - || !EntityManager.TryGetComponent(holder, out var intrinsic)) + if (!HasComp(args.OldParent)) return; - intrinsic.Translators.RemoveWhere(it => it.Owner == translator); - _language.UpdateEntityLanguages(holder); + // Update the translator on the next tick - this is necessary because there's a good chance the removal from a container + // Was caused by the player moving the translator within their inventory rather than removing it. + // If that is not the case, then OnProxyDetermineLanguages will remove this translator from HoldsTranslatorComponent.Translators. + Timer.Spawn(0, () => + { + if (Exists(args.OldParent) && TryComp(args.OldParent, out var speaker)) + _language.UpdateEntityLanguages(args.OldParent.Value, speaker); + }); } private void OnTranslatorToggle(EntityUid translator, HandheldTranslatorComponent translatorComp, ActivateInWorldEvent args) @@ -125,6 +130,9 @@ private void OnPowerCellSlotEmpty(EntityUid translator, HandheldTranslatorCompon component.Enabled = false; _powerCell.SetPowerCellDrawEnabled(translator, false); OnAppearanceChange(translator, component); + + if (_containers.TryGetContainingContainer(translator, out var holderCont) && TryComp(holderCont.Owner, out var languageComp)) + _language.UpdateEntityLanguages(holderCont.Owner, languageComp); } private void CopyLanguages(BaseTranslatorComponent from, DetermineEntityLanguagesEvent to, LanguageKnowledgeComponent knowledge)