Skip to content

Commit

Permalink
Ensure dialogs do not stay open when switching away from PlayingTheGa…
Browse files Browse the repository at this point in the history
…me game state
  • Loading branch information
ethanmoffat committed Apr 1, 2022
1 parent ba0b8da commit a452c4f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
8 changes: 6 additions & 2 deletions EndlessClient/ControlSets/ControlSetFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -40,7 +42,8 @@ public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager,
ICharacterInfoPanelFactory characterInfoPanelFactory,
ICharacterSelectorProvider characterSelectorProvider,
IEndlessGameProvider endlessGameProvider,
IUserInputRepository userInputRepository)
IUserInputRepository userInputRepository,
IActiveDialogRepository activeDialogRepository)
{
_nativeGraphicsManager = nativeGraphicsManager;
_messageBoxFactory = messageBoxFactory;
Expand All @@ -52,6 +55,7 @@ public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager,
_characterSelectorProvider = characterSelectorProvider;
_endlessGameProvider = endlessGameProvider;
_userInputRepository = userInputRepository;
_activeDialogRepository = activeDialogRepository;
}

public IControlSet CreateControlsForState(GameStates newState, IControlSet currentControlSet)
Expand Down Expand Up @@ -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);
}
}
Expand Down
17 changes: 15 additions & 2 deletions EndlessClient/ControlSets/InGameControlSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using EndlessClient.HUD.Controls;
using EOLib.Localization;
using Microsoft.Xna.Framework;
using Optional;
using XNAControls;

namespace EndlessClient.ControlSets
Expand All @@ -16,18 +17,20 @@ public class InGameControlSet : BackButtonControlSet
{
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly IHudControlsFactory _hudControlsFactory;

private readonly IActiveDialogRepository _activeDialogRepository;
private IReadOnlyDictionary<HudControlIdentifier, IGameComponent> _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<HudControlIdentifier, IGameComponent>();
}

Expand Down Expand Up @@ -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);
}
}
}
14 changes: 12 additions & 2 deletions EndlessClient/Dialogs/ActiveDialogRepository.cs
Original file line number Diff line number Diff line change
@@ -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<ScrollingListDialog> FriendIgnoreDialog { get; }

Expand All @@ -15,7 +16,7 @@ public interface IActiveDialogProvider
IReadOnlyList<Option<IXNADialog>> ActiveDialogs { get; }
}

public interface IActiveDialogRepository
public interface IActiveDialogRepository : IDisposable
{
Option<ScrollingListDialog> FriendIgnoreDialog { get; set; }

Expand Down Expand Up @@ -46,5 +47,14 @@ IReadOnlyList<Option<IXNADialog>> ActiveDialogs
IReadOnlyList<Option<IXNADialog>> IActiveDialogRepository.ActiveDialogs => ActiveDialogs;

IReadOnlyList<Option<IXNADialog>> IActiveDialogProvider.ActiveDialogs => ActiveDialogs;

public void Dispose()
{
foreach (var dlg in ActiveDialogs)
dlg.MatchSome(d => d.Dispose());

FriendIgnoreDialog = Option.None<ScrollingListDialog>();
PaperdollDialog = Option.None<PaperdollDialog>();
}
}
}
2 changes: 2 additions & 0 deletions EndlessClient/GameExecution/GameStateActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<IGameComponent> FindUnusedComponents(IControlSet current, IControlSet next)
Expand Down

0 comments on commit a452c4f

Please sign in to comment.