Skip to content

Commit

Permalink
Abstract content manager away from consuming classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanmoffat committed Mar 24, 2022
1 parent 8cbdc19 commit 4c822df
Show file tree
Hide file tree
Showing 26 changed files with 189 additions and 106 deletions.
94 changes: 94 additions & 0 deletions EndlessClient/Content/ContentProvider.cs
@@ -0,0 +1,94 @@
using AutomaticTypeMapper;
using EOLib;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;

namespace EndlessClient.Content
{
public interface IContentProvider
{
IReadOnlyDictionary<string, Texture2D> Textures { get; }

IReadOnlyDictionary<string, SpriteFont> Fonts { get; }

void SetContentManager(ContentManager content);

void Load();
}

[AutoMappedType(IsSingleton = true)]
public class ContentProvider : IContentProvider
{
private readonly Dictionary<string, Texture2D> _textures;
private readonly Dictionary<string, SpriteFont> _fonts;

private ContentManager _content;

public const string Cursor = "cursor";

public const string TBBack = "tbBack";
public const string TBLeft = "tbLeft";
public const string TBRight = "tbRight";

public const string ChatTL = @"ChatBubble\TL";
public const string ChatTM = @"ChatBubble\TM";
public const string ChatTR = @"ChatBubble\TR";
public const string ChatML = @"ChatBubble\ML";
public const string ChatMM = @"ChatBubble\MM";
public const string ChatMR = @"ChatBubble\MR";
public const string ChatRL = @"ChatBubble\RL";
public const string ChatRM = @"ChatBubble\RM";
public const string ChatRR = @"ChatBubble\RR";
public const string ChatNUB = @"ChatBubble\NUB";

public IReadOnlyDictionary<string, Texture2D> Textures => _textures;

public IReadOnlyDictionary<string, SpriteFont> Fonts => _fonts;

public ContentProvider()
{
_textures = new Dictionary<string, Texture2D>();
_fonts = new Dictionary<string, SpriteFont>();
}

public void SetContentManager(ContentManager content)
{
_content = content;
}

public void Load()
{
RefreshTextures();
RefreshFonts();
}

private void RefreshTextures()
{
if (_content == null)
return;

_textures[Cursor] = _content.Load<Texture2D>(Cursor);

_textures[TBBack] = _content.Load<Texture2D>(TBBack);
_textures[TBLeft] = _content.Load<Texture2D>(TBLeft);
_textures[TBRight] = _content.Load<Texture2D>(TBRight);

_textures[ChatTL] = _content.Load<Texture2D>(ChatTL);
_textures[ChatTM] = _content.Load<Texture2D>(ChatTM);
_textures[ChatTR] = _content.Load<Texture2D>(ChatTR);
_textures[ChatML] = _content.Load<Texture2D>(ChatML);
_textures[ChatMM] = _content.Load<Texture2D>(ChatMM);
_textures[ChatMR] = _content.Load<Texture2D>(ChatMR);
_textures[ChatRL] = _content.Load<Texture2D>(ChatRL);
_textures[ChatRM] = _content.Load<Texture2D>(ChatRM);
_textures[ChatRR] = _content.Load<Texture2D>(ChatRR);
_textures[ChatNUB] = _content.Load<Texture2D>(ChatNUB);
}

private void RefreshFonts()
{
_fonts[Constants.FontSize08] = _content.Load<SpriteFont>(Constants.FontSize08);
}
}
}
22 changes: 0 additions & 22 deletions EndlessClient/Content/IContentManagerRepository.cs

This file was deleted.

6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/BackButtonControlSet.cs
@@ -1,8 +1,8 @@
using System;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand All @@ -21,9 +21,9 @@ protected BackButtonControlSet(IMainButtonController mainButtonController)
_mainButtonController = mainButtonController;
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

_backButtonTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 24, true);
}
Expand Down
12 changes: 6 additions & 6 deletions EndlessClient/ControlSets/BaseControlSet.cs
@@ -1,10 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand Down Expand Up @@ -43,7 +43,7 @@ protected BaseControlSet()
}

public virtual void InitializeResources(INativeGraphicsManager gfxManager,
ContentManager xnaContentManager)
IContentProvider contentProvider)
{
if (_resourcesInitialized)
throw new InvalidOperationException("Error initializing resources: resources have already been initialized");
Expand All @@ -52,10 +52,10 @@ protected BaseControlSet()
_secondaryButtonTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 14, true);
_smallButtonSheet = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 15, true);

_textBoxBackground = xnaContentManager.Load<Texture2D>("tbBack");
_textBoxLeft = xnaContentManager.Load<Texture2D>("tbLeft");
_textBoxRight = xnaContentManager.Load<Texture2D>("tbRight");
_textBoxCursor = xnaContentManager.Load<Texture2D>("cursor");
_textBoxBackground = contentProvider.Textures[ContentProvider.TBBack];
_textBoxLeft = contentProvider.Textures[ContentProvider.TBLeft];
_textBoxRight = contentProvider.Textures[ContentProvider.TBRight];
_textBoxCursor = contentProvider.Textures[ContentProvider.Cursor];

_backgroundImages = new Texture2D[7];
for (int i = 0; i < _backgroundImages.Length; ++i)
Expand Down
8 changes: 4 additions & 4 deletions EndlessClient/ControlSets/ControlSetFactory.cs
Expand Up @@ -18,7 +18,7 @@ public class ControlSetFactory : IControlSetFactory
private readonly INativeGraphicsManager _nativeGraphicsManager;
private readonly IEOMessageBoxFactory _messageBoxFactory;
private readonly IHudControlsFactory _hudControlsFactory;
private readonly IContentManagerProvider _contentManagerProvider;
private readonly IContentProvider _contentProvider;
private readonly IKeyboardDispatcherProvider _keyboardDispatcherProvider;
private readonly IConfigurationProvider _configProvider;
private readonly ICharacterInfoPanelFactory _characterInfoPanelFactory;
Expand All @@ -30,15 +30,15 @@ public class ControlSetFactory : IControlSetFactory
public ControlSetFactory(INativeGraphicsManager nativeGraphicsManager,
IEOMessageBoxFactory messageBoxFactory,
IHudControlsFactory hudControlsFactory,
IContentManagerProvider contentManagerProvider,
IContentProvider contentProvider,
IKeyboardDispatcherProvider keyboardDispatcherProvider,
IConfigurationProvider configProvider,
ICharacterInfoPanelFactory characterInfoPanelFactory)
{
_nativeGraphicsManager = nativeGraphicsManager;
_messageBoxFactory = messageBoxFactory;
_hudControlsFactory = hudControlsFactory;
_contentManagerProvider = contentManagerProvider;
_contentProvider = contentProvider;
_keyboardDispatcherProvider = keyboardDispatcherProvider;
_configProvider = configProvider;
_characterInfoPanelFactory = characterInfoPanelFactory;
Expand All @@ -51,7 +51,7 @@ public IControlSet CreateControlsForState(GameStates newState, IControlSet curre
throw new InvalidOperationException("Missing controllers - the Unity container was initialized incorrectly");

var controlSet = GetSetBasedOnState(newState);
controlSet.InitializeResources(_nativeGraphicsManager, _contentManagerProvider.Content);
controlSet.InitializeResources(_nativeGraphicsManager, _contentProvider);
controlSet.InitializeControls(currentControlSet);
return controlSet;
}
Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/CreateAccountControlSet.cs
@@ -1,13 +1,13 @@
using System;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.Input;
using EOLib;
using EOLib.Domain.Account;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand Down Expand Up @@ -41,9 +41,9 @@ public class CreateAccountControlSet : IntermediateControlSet
_accountController = accountController;
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

_labelsTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 12, true);
}
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/ControlSets/EmptyControlSet.cs
@@ -1,9 +1,9 @@
using System.Collections.Generic;
using System.Linq;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using XNAControls;

namespace EndlessClient.ControlSets
Expand All @@ -19,7 +19,7 @@ public class EmptyControlSet : IControlSet

public IReadOnlyList<IXNAControl> XNAControlComponents => AllComponents.OfType<IXNAControl>().ToList();

public void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
}

Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/IControlSet.cs
@@ -1,9 +1,9 @@
using System;
using System.Collections.Generic;
using EndlessClient.Content;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using XNAControls;

namespace EndlessClient.ControlSets
Expand All @@ -29,8 +29,8 @@ public interface IControlSet : IDisposable
/// Initialize the required resources for the control set from the resource dependencies. Should be called before InitializeControls()
/// </summary>
/// <param name="gfxManager">An initialized native graphics manager</param>
/// <param name="xnaContentManager">The ContentManager for the game</param>
void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager);
/// <param name="contentProvider">The ContentProvider for the game</param>
void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider);

/// <summary>
/// Create the controls for this IControlSet based on an existing set of controls
Expand Down
5 changes: 3 additions & 2 deletions EndlessClient/ControlSets/InitialControlSet.cs
@@ -1,4 +1,5 @@
using System;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EOLib;
Expand Down Expand Up @@ -38,9 +39,9 @@ public class InitialControlSet : BaseControlSet
_randomGen = new Random();
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

for (int i = 0; i < _personSet1.Length; ++i)
_personSet1[i] = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 41 + i, true);
Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/ControlSets/IntermediateControlSet.cs
@@ -1,9 +1,9 @@
using System;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EOLib.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand All @@ -27,9 +27,9 @@ public abstract class IntermediateControlSet : BackButtonControlSet
_randomGen = new Random();
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

for (int i = 0; i < _personSet2.Length; ++i)
_personSet2[i] = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 61 + i, true);
Expand Down
5 changes: 3 additions & 2 deletions EndlessClient/ControlSets/LoginPromptControlSet.cs
@@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Threading;
using EndlessClient.Content;
using EndlessClient.Controllers;
using EndlessClient.GameExecution;
using EndlessClient.Input;
Expand Down Expand Up @@ -44,9 +45,9 @@ public class LoginPromptControlSet : InitialControlSet
_loginController = loginController;
}

public override void InitializeResources(INativeGraphicsManager gfxManager, ContentManager xnaContentManager)
public override void InitializeResources(INativeGraphicsManager gfxManager, IContentProvider contentProvider)
{
base.InitializeResources(gfxManager, xnaContentManager);
base.InitializeResources(gfxManager, contentProvider);

_loginBackgroundTexture = gfxManager.TextureFromResource(GFXTypes.PreLoginUI, 2);
}
Expand Down
4 changes: 2 additions & 2 deletions EndlessClient/Dialogs/ChangePasswordDialog.cs
Expand Up @@ -35,7 +35,7 @@ public class ChangePasswordDialog : BaseEODialog

public ChangePasswordDialog(INativeGraphicsManager nativeGraphicsManager,
IGameStateProvider gameStateProvider,
IContentManagerProvider contentManagerProvider,
IContentProvider contentProvider,
IEOMessageBoxFactory eoMessageBoxFactory,
IKeyboardDispatcherProvider keyboardDispatcherProvider,
IPlayerInfoProvider playerInfoProvider,
Expand All @@ -48,7 +48,7 @@ public class ChangePasswordDialog : BaseEODialog

BackgroundTexture = nativeGraphicsManager.TextureFromResource(GFXTypes.PreLoginUI, 21);

var cursorTexture = contentManagerProvider.Content.Load<Texture2D>("Cursor");
var cursorTexture = contentProvider.Textures[ContentProvider.Cursor];

_inputBoxes = new IXNATextBox[4];
for (int i = 0; i < _inputBoxes.Length; ++i)
Expand Down
6 changes: 3 additions & 3 deletions EndlessClient/Dialogs/CreateCharacterDialog.cs
@@ -1,4 +1,5 @@
using System;
using EndlessClient.Content;
using EndlessClient.Dialogs.Factories;
using EndlessClient.Dialogs.Services;
using EndlessClient.GameExecution;
Expand All @@ -9,7 +10,6 @@
using EOLib.Graphics;
using EOLib.Localization;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using XNAControls;

Expand Down Expand Up @@ -41,7 +41,7 @@ public class CreateCharacterDialog : BaseEODialog
INativeGraphicsManager nativeGraphicsManager,
IGameStateProvider gameStateProvider,
ICharacterRendererFactory rendererFactory,
ContentManager contentManager,
IContentProvider contentProvider,
KeyboardDispatcher dispatcher,
IEOMessageBoxFactory messageBoxFactory,
IEODialogButtonService eoDialogButtonService)
Expand All @@ -52,7 +52,7 @@ public class CreateCharacterDialog : BaseEODialog

_charCreateSheet = nativeGraphicsManager.TextureFromResource(GFXTypes.PreLoginUI, 22);

var cursorTexture = contentManager.Load<Texture2D>("cursor");
var cursorTexture = contentProvider.Textures[ContentProvider.Cursor];
_inputBox = new XNATextBox(new Rectangle(80, 57, 138, 19), Constants.FontSize08, caretTexture: cursorTexture)
{
LeftPadding = 5,
Expand Down

0 comments on commit 4c822df

Please sign in to comment.