diff --git a/EndlessClient/ControlSets/ControlSetFactory.cs b/EndlessClient/ControlSets/ControlSetFactory.cs index 8dcd4c74a..941526442 100644 --- a/EndlessClient/ControlSets/ControlSetFactory.cs +++ b/EndlessClient/ControlSets/ControlSetFactory.cs @@ -2,6 +2,7 @@ using AutomaticTypeMapper; using EndlessClient.Content; using EndlessClient.Controllers; +using EndlessClient.Dialogs; using EndlessClient.Dialogs.Factories; using EndlessClient.GameExecution; using EndlessClient.HUD.Controls; @@ -26,6 +27,7 @@ public class ControlSetFactory : IControlSetFactory private readonly ICharacterSelectorProvider _characterSelectorProvider; private readonly IEndlessGameProvider _endlessGameProvider; private readonly IUserInputRepository _userInputRepository; + private readonly IActiveDialogRepository _activeDialogRepository; private IMainButtonController _mainButtonController; private IAccountController _accountController; private ILoginController _loginController; @@ -40,7 +42,8 @@ public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager, ICharacterInfoPanelFactory characterInfoPanelFactory, ICharacterSelectorProvider characterSelectorProvider, IEndlessGameProvider endlessGameProvider, - IUserInputRepository userInputRepository) + IUserInputRepository userInputRepository, + IActiveDialogRepository activeDialogRepository) { _nativeGraphicsManager = nativeGraphicsManager; _messageBoxFactory = messageBoxFactory; @@ -52,6 +55,7 @@ public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager, _characterSelectorProvider = characterSelectorProvider; _endlessGameProvider = endlessGameProvider; _userInputRepository = userInputRepository; + _activeDialogRepository = activeDialogRepository; } public IControlSet CreateControlsForState(GameStates newState, IControlSet currentControlSet) @@ -105,7 +109,7 @@ private IControlSet GetSetBasedOnState(GameStates newState) _endlessGameProvider, _userInputRepository); case GameStates.PlayingTheGame: - return new InGameControlSet(_mainButtonController, _messageBoxFactory, _hudControlsFactory); + return new InGameControlSet(_mainButtonController, _messageBoxFactory, _hudControlsFactory, _activeDialogRepository); default: throw new ArgumentOutOfRangeException(nameof(newState), newState, null); } } diff --git a/EndlessClient/ControlSets/InGameControlSet.cs b/EndlessClient/ControlSets/InGameControlSet.cs index 994376899..e3257d2c2 100644 --- a/EndlessClient/ControlSets/InGameControlSet.cs +++ b/EndlessClient/ControlSets/InGameControlSet.cs @@ -8,6 +8,7 @@ using EndlessClient.HUD.Controls; using EOLib.Localization; using Microsoft.Xna.Framework; +using Optional; using XNAControls; namespace EndlessClient.ControlSets @@ -16,18 +17,20 @@ public class InGameControlSet : BackButtonControlSet { private readonly IEOMessageBoxFactory _messageBoxFactory; private readonly IHudControlsFactory _hudControlsFactory; - + private readonly IActiveDialogRepository _activeDialogRepository; private IReadOnlyDictionary _controls; public override GameStates GameState => GameStates.PlayingTheGame; public InGameControlSet(IMainButtonController mainButtonController, IEOMessageBoxFactory messageBoxFactory, - IHudControlsFactory hudControlsFactory) + IHudControlsFactory hudControlsFactory, + IActiveDialogRepository activeDialogRepository) : base(mainButtonController) { _messageBoxFactory = messageBoxFactory; _hudControlsFactory = hudControlsFactory; + _activeDialogRepository = activeDialogRepository; _controls = new Dictionary(); } @@ -55,5 +58,15 @@ protected override async void DoBackButtonClick(object sender, EventArgs e) if (result == XNADialogResult.OK) _mainButtonController.GoToInitialStateAndDisconnect(); } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + _activeDialogRepository.Dispose(); + } + + base.Dispose(disposing); + } } } diff --git a/EndlessClient/Dialogs/ActiveDialogRepository.cs b/EndlessClient/Dialogs/ActiveDialogRepository.cs index f3ea8ef1c..99d81e9d8 100644 --- a/EndlessClient/Dialogs/ActiveDialogRepository.cs +++ b/EndlessClient/Dialogs/ActiveDialogRepository.cs @@ -1,12 +1,13 @@ using AutomaticTypeMapper; using Optional; +using System; using System.Collections.Generic; using System.Linq; using XNAControls; namespace EndlessClient.Dialogs { - public interface IActiveDialogProvider + public interface IActiveDialogProvider : IDisposable { Option FriendIgnoreDialog { get; } @@ -15,7 +16,7 @@ public interface IActiveDialogProvider IReadOnlyList> ActiveDialogs { get; } } - public interface IActiveDialogRepository + public interface IActiveDialogRepository : IDisposable { Option FriendIgnoreDialog { get; set; } @@ -46,5 +47,14 @@ IReadOnlyList> ActiveDialogs IReadOnlyList> IActiveDialogRepository.ActiveDialogs => ActiveDialogs; IReadOnlyList> IActiveDialogProvider.ActiveDialogs => ActiveDialogs; + + public void Dispose() + { + foreach (var dlg in ActiveDialogs) + dlg.MatchSome(d => d.Dispose()); + + FriendIgnoreDialog = Option.None(); + PaperdollDialog = Option.None(); + } } } diff --git a/EndlessClient/GameExecution/GameStateActions.cs b/EndlessClient/GameExecution/GameStateActions.cs index c8b6d299e..ef9b2e1c5 100644 --- a/EndlessClient/GameExecution/GameStateActions.cs +++ b/EndlessClient/GameExecution/GameStateActions.cs @@ -76,6 +76,8 @@ private void RemoveOldComponents(IControlSet currentSet, IControlSet nextSet) component.Dispose(); foreach (var component in componentsToRemove.Where(Game.Components.Contains)) Game.Components.Remove(component); + + currentSet.Dispose(); } private List FindUnusedComponents(IControlSet current, IControlSet next)