Skip to content

Commit

Permalink
Add persistence for panel state in resizable mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Apr 30, 2023
1 parent 90de46a commit f98bb3a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions EOLib/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static class Constants

public const string InventoryFile = "config/inventory.ini";
public const string SpellsFile = "config/spells.ini";
public const string PanelLayoutFile = "config/panels.ini";
public const string ChatLogFile = "chatlog.txt";

//Should be easily customizable between different clients (based on graphics)
Expand Down
33 changes: 33 additions & 0 deletions EndlessClient/GameExecution/GameStateActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using EndlessClient.Rendering;
using Microsoft.Xna.Framework;
using XNAControls.Input;
using EndlessClient.HUD.Panels;
using EOLib;

namespace EndlessClient.GameExecution
{
Expand All @@ -21,6 +23,7 @@ public class GameStateActions : IGameStateActions
private readonly IControlSetFactory _controlSetFactory;
private readonly IEndlessGameProvider _endlessGameProvider;
private readonly IPlayerInfoRepository _playerInfoRepository;
private readonly IClientWindowSizeProvider _clientWindowSizeProvider;
private readonly ISfxPlayer _sfxPlayer;
private readonly IMfxPlayer _mfxPlayer;

Expand All @@ -29,6 +32,7 @@ public GameStateActions(IGameStateRepository gameStateRepository,
IControlSetFactory controlSetFactory,
IEndlessGameProvider endlessGameProvider,
IPlayerInfoRepository playerInfoRepository,
IClientWindowSizeProvider clientWindowSizeProvider,
ISfxPlayer sfxPlayer,
IMfxPlayer mfxPlayer)
{
Expand All @@ -37,6 +41,7 @@ public GameStateActions(IGameStateRepository gameStateRepository,
_controlSetFactory = controlSetFactory;
_endlessGameProvider = endlessGameProvider;
_playerInfoRepository = playerInfoRepository;
_clientWindowSizeProvider = clientWindowSizeProvider;
_sfxPlayer = sfxPlayer;
_mfxPlayer = mfxPlayer;
}
Expand All @@ -47,8 +52,13 @@ public void ChangeToState(GameStates newState)
return;

if (_gameStateRepository.CurrentState == GameStates.PlayingTheGame)
{
_playerInfoRepository.PlayerIsInGame = false;

StorePanelLayout(Game, EventArgs.Empty);
Game.Exiting -= StorePanelLayout;
}

var currentSet = _controlSetRepository.CurrentControlSet;
var nextSet = _controlSetFactory.CreateControlsForState(newState, currentSet);

Expand All @@ -70,6 +80,11 @@ public void ChangeToState(GameStates newState)
}
break;
case GameStates.LoggedIn: _sfxPlayer.PlaySfx(SoundEffectID.Login); break;
case GameStates.PlayingTheGame:
{
Game.Exiting += StorePanelLayout;
}
break;
}
}

Expand Down Expand Up @@ -118,6 +133,24 @@ private List<IGameComponent> FindUnusedComponents(IControlSet current, IControlS
.ToList();
}

private void StorePanelLayout(object sender, EventArgs e)
{
if (!_clientWindowSizeProvider.Resizable) return;

var panelConfig = new EOLib.Config.IniReader(Constants.PanelLayoutFile);
panelConfig.Sections["PANELS"] = new SortedList<string, string>();

var panels = _controlSetRepository.CurrentControlSet.AllComponents.OfType<DraggableHudPanel>();
foreach (var panel in panels)
{
panelConfig.Sections["PANELS"][$"{panel.GetType().Name}:X"] = ((int)panel.DrawPositionWithParentOffset.X).ToString();
panelConfig.Sections["PANELS"][$"{panel.GetType().Name}:Y"] = ((int)panel.DrawPositionWithParentOffset.Y).ToString();
panelConfig.Sections["PANELS"][$"{panel.GetType().Name}:Visible"] = panel.Visible.ToString();
}

panelConfig.Save();
}

private IEndlessGame Game => _endlessGameProvider.Game;
}
}
2 changes: 2 additions & 0 deletions EndlessClient/GameExecution/IEndlessGame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ namespace EndlessClient.GameExecution
{
public interface IEndlessGame : IDisposable
{
event EventHandler<EventArgs> Exiting;

GameComponentCollection Components { get; }

ContentManager Content { get; }
Expand Down
17 changes: 16 additions & 1 deletion EndlessClient/HUD/Controls/HudControlsFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
using EndlessClient.Rendering.Map;
using EndlessClient.Rendering.NPC;
using EndlessClient.UIControls;
using EOLib;
using EOLib.Config;
using EOLib.Domain.Character;
using EOLib.Domain.Login;
using EOLib.Domain.Map;
Expand All @@ -30,7 +32,6 @@

namespace EndlessClient.HUD.Controls
{
//todo: this class is doing a lot. Might be a good idea to split it into multiple factories.
[AutoMappedType(IsSingleton = true)]
public class HudControlsFactory : IHudControlsFactory
{
Expand Down Expand Up @@ -399,6 +400,20 @@ private IGameComponent CreateStatePanel(InGameStates whichState)

updateDrawArea();
_clientWindowSizeRepository.GameWindowSizeChanged += (_, _) => updateDrawArea();

var panelConfig = new IniReader(Constants.PanelLayoutFile);
if (panelConfig.Load())
{
if (panelConfig.GetValue("PANELS", $"{retPanel.GetType().Name}:X", out int x) &&
panelConfig.GetValue("PANELS", $"{retPanel.GetType().Name}:Y", out int y))
{
retPanel.DrawPosition = new Vector2(x, y);
}
if (panelConfig.GetValue("PANELS", $"{retPanel.GetType().Name}:Visible", out bool visible))
{
retPanel.Visible = visible;
}
}
}

return retPanel;
Expand Down

0 comments on commit f98bb3a

Please sign in to comment.