refactor.
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
using RecrownedAthenaeum.UI.Modular.Modules.Interactive;
|
||||
using System;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem.Definitions
|
||||
{
|
||||
/// <summary>
|
||||
/// Definition for a button.
|
||||
/// </summary>
|
||||
public class ButtonSkinDefinition : ISkinDefinitionData
|
||||
{
|
||||
/// <summary>
|
||||
/// Names for the regions in the texture atlas respectively.
|
||||
/// </summary>
|
||||
public string upRegion, downRegion, disabledRegion, selectedRegion;
|
||||
|
||||
///<inheritDoc/>
|
||||
public Type UIModuleType { get { return typeof(Button); } }
|
||||
|
||||
/// <summary>
|
||||
/// Constructs the definition with minimum requirements.
|
||||
/// </summary>
|
||||
/// <param name="downRegion">Name of region specifying the texture shown for when the button is pressed down.</param>
|
||||
/// <param name="upRegion">Name of region specifying the texture shown for when the button is not pressed.</param>
|
||||
public ButtonSkinDefinition(string downRegion, string upRegion)
|
||||
{
|
||||
this.downRegion = downRegion;
|
||||
this.upRegion = upRegion;
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem.Definitions
|
||||
{
|
||||
/// <summary>
|
||||
/// A definition containing the data for the skin system. Should be in data transfer object model.
|
||||
/// </summary>
|
||||
public interface ISkinDefinitionData
|
||||
{
|
||||
/// <summary>
|
||||
/// The module type this definition is definining.
|
||||
/// </summary>
|
||||
Type UIModuleType { get; }
|
||||
}
|
||||
}
|
@@ -0,0 +1,31 @@
|
||||
using RecrownedAthenaeum.UI.Modular.Modules.Interactive;
|
||||
using System;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem.Definitions
|
||||
{
|
||||
/// <summary>
|
||||
/// Definition for a text button for a skin theme.
|
||||
/// </summary>
|
||||
public class TextButtonSkinDefinition : ButtonSkinDefinition
|
||||
{
|
||||
/// <summary>
|
||||
/// Name of color from the skin to use for the font.
|
||||
/// </summary>
|
||||
public string fontColor;
|
||||
|
||||
/// <summary>
|
||||
/// The type of module that will be using this definition.
|
||||
/// </summary>
|
||||
public new Type UIModuleType => typeof(TextButton);
|
||||
|
||||
/// <summary>
|
||||
/// Creates this definition with the most minimal requirements.
|
||||
/// </summary>
|
||||
/// <param name="downRegion">Texture region from skin that represents when the button is pressed down.</param>
|
||||
/// <param name="upRegion">The texture region that represents when the button is not pressed.</param>
|
||||
public TextButtonSkinDefinition(string downRegion, string upRegion) : base(downRegion, upRegion)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using static System.Net.Mime.MediaTypeNames;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem.Definitions
|
||||
{
|
||||
class TextSkinDefinition : ISkinDefinitionData
|
||||
{
|
||||
public string color;
|
||||
public Type UIModuleType { get { return typeof(Text); } }
|
||||
|
||||
public TextSkinDefinition(string color)
|
||||
{
|
||||
this.color = color;
|
||||
}
|
||||
}
|
||||
}
|
76
RecrownedAthenaeum/UI/SkinSystem/ISkin.cs
Normal file
76
RecrownedAthenaeum/UI/SkinSystem/ISkin.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using System;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.SpecialTypes;
|
||||
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// The output requirements of a skin. This allows for very customized skin systems if needed.
|
||||
/// </summary>
|
||||
public interface ISkin
|
||||
{
|
||||
/// <summary>
|
||||
/// The texture for the cursor.
|
||||
/// </summary>
|
||||
Texture2D CursorTexture { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Draws a region from the texture atlas.
|
||||
/// </summary>
|
||||
/// <param name="regionName">Region to draw.</param>
|
||||
/// <param name="color">The color to tint the region.</param>
|
||||
/// <param name="batch">The batch to use.</param>
|
||||
/// <param name="destination">The destination to draw to.</param>
|
||||
/// <param name="rotation">The rotation to use in radians.</param>
|
||||
/// <param name="origin">The origin for the rotation.</param>
|
||||
void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2));
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="Color"/> with given name of defined color;
|
||||
/// </summary>
|
||||
/// <param name="name">Name of defined color.</param>
|
||||
/// <returns>The defined color based on the name given.</returns>
|
||||
Color GetColor(string name);
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="TextureAtlas.Region"/> with given name of region.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of region.</param>
|
||||
/// <returns>The region corresponding to the name.</returns>
|
||||
TextureAtlas.Region GetTextureAtlasRegion(string name);
|
||||
|
||||
/// <summary>
|
||||
/// Returns an <see cref="ISkinDefinitionData"/> of the given name and type.
|
||||
/// </summary>
|
||||
/// <param name="definitionName">Name of definition of the <paramref name="type"/></param>
|
||||
/// <param name="type">The UIModule the definition defines.</param>
|
||||
/// <returns>The interface for the definition.</returns>
|
||||
ISkinDefinitionData ObtainDefinition(string definitionName, Type type);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the default <see cref="ISkinDefinitionData"/> of the given parameters.
|
||||
/// </summary>
|
||||
/// <param name="type">The type of definition the default should be coming from.</param>
|
||||
/// <returns>The default definition for the given type.</returns>
|
||||
ISkinDefinitionData ObtainDefinition(Type type);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam>
|
||||
/// <param name="definitionName">The name of the definition.</param>
|
||||
/// <param name="type">UIModule type the definition defines.</param>
|
||||
/// <returns>The definition cast to T.</returns>
|
||||
T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData;
|
||||
|
||||
/// <summary>
|
||||
/// Returns the default definition.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convenience to cast to T.</typeparam>
|
||||
/// <param name="type">The type of the UIModule to retrieve the default from.</param>
|
||||
/// <returns>The default definition for the given type.</returns>
|
||||
T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData;
|
||||
}
|
||||
}
|
143
RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs
Normal file
143
RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs
Normal file
@@ -0,0 +1,143 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.SpecialTypes;
|
||||
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem
|
||||
{
|
||||
internal class MergedSkin : ISkin
|
||||
{
|
||||
/// <summary>
|
||||
/// The skin to try to use first.
|
||||
/// </summary>
|
||||
public ISkin mainSkin;
|
||||
|
||||
/// <summary>
|
||||
/// The fallback skin.
|
||||
/// </summary>
|
||||
public ISkin alternateSkin;
|
||||
|
||||
public Texture2D CursorTexture
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.CursorTexture;
|
||||
} catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.CursorTexture;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
|
||||
{
|
||||
try
|
||||
{
|
||||
mainSkin.Draw(regionName, color, batch, destination, rotation, origin);
|
||||
} catch (KeyNotFoundException)
|
||||
{
|
||||
alternateSkin.Draw(regionName, color, batch, destination, rotation, origin);
|
||||
} catch (NullReferenceException)
|
||||
{
|
||||
alternateSkin.Draw(regionName, color, batch, destination, rotation, origin);
|
||||
}
|
||||
}
|
||||
|
||||
public Color GetColor(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.GetColor(name);
|
||||
} catch (KeyNotFoundException)
|
||||
{
|
||||
return alternateSkin.GetColor(name);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.GetColor(name);
|
||||
}
|
||||
}
|
||||
|
||||
public TextureAtlas.Region GetTextureAtlasRegion(string name)
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.GetTextureAtlasRegion(name);
|
||||
} catch (KeyNotFoundException)
|
||||
{
|
||||
return alternateSkin.GetTextureAtlasRegion(name);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.GetTextureAtlasRegion(name);
|
||||
}
|
||||
}
|
||||
|
||||
public ISkinDefinitionData ObtainDefinition(string definitionName, Type type)
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.ObtainDefinition(definitionName, type);
|
||||
} catch (KeyNotFoundException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition(definitionName, type);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition(definitionName, type);
|
||||
}
|
||||
}
|
||||
|
||||
public ISkinDefinitionData ObtainDefinition(Type type)
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.ObtainDefinition(type);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition(type);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition(type);
|
||||
}
|
||||
}
|
||||
|
||||
public T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.ObtainDefinition<T>(definitionName, type);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition<T>(definitionName, type);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition<T>(definitionName, type);
|
||||
}
|
||||
}
|
||||
|
||||
public T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData
|
||||
{
|
||||
try
|
||||
{
|
||||
return mainSkin.ObtainDefinition<T>(type);
|
||||
}
|
||||
catch (KeyNotFoundException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition<T>(type);
|
||||
}
|
||||
catch (NullReferenceException)
|
||||
{
|
||||
return alternateSkin.ObtainDefinition<T>(type);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
201
RecrownedAthenaeum/UI/SkinSystem/Skin.cs
Normal file
201
RecrownedAthenaeum/UI/SkinSystem/Skin.cs
Normal file
@@ -0,0 +1,201 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RecrownedAthenaeum.SpecialTypes;
|
||||
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// A skin is used to group a theme which can then be applied to the UI via the use of modules.
|
||||
/// </summary>
|
||||
public class Skin : IDisposable, ISkin
|
||||
{
|
||||
/// <summary>
|
||||
/// Whether or not this skin is completed being built and thus ready to use.
|
||||
/// </summary>
|
||||
public bool Laminated { get; private set; }
|
||||
private bool disposed;
|
||||
|
||||
private TextureAtlas textureAtlas;
|
||||
|
||||
Dictionary<string, Color> colors;
|
||||
|
||||
Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions;
|
||||
|
||||
/// <summary>
|
||||
/// The texture for the cursor.
|
||||
/// </summary>
|
||||
public virtual Texture2D CursorTexture { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a basic unfilled skin.
|
||||
/// </summary>
|
||||
/// <param name="textureAtlas">The texture atlas to use for this skin.</param>
|
||||
/// <param name="cursorTexture">The texture the cursor will be.</param>
|
||||
public Skin(TextureAtlas textureAtlas, Texture2D cursorTexture)
|
||||
{
|
||||
this.textureAtlas = textureAtlas;
|
||||
this.CursorTexture = cursorTexture;
|
||||
colors = new Dictionary<string, Color>();
|
||||
definitions = new Dictionary<Type, Dictionary<string, ISkinDefinitionData>>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="TextureAtlas.Region"/> with given name of region.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of region.</param>
|
||||
/// <returns>The region corresponding to the name.</returns>
|
||||
public virtual TextureAtlas.Region GetTextureAtlasRegion(string name)
|
||||
{
|
||||
return textureAtlas[name];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a <see cref="Color"/> with given name of defined color;
|
||||
/// </summary>
|
||||
/// <param name="name">Name of defined color.</param>
|
||||
/// <returns>The defined color based on the name given.</returns>
|
||||
public virtual Color GetColor(string name)
|
||||
{
|
||||
return colors[name];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Draws a region from the texture atlas.
|
||||
/// </summary>
|
||||
/// <param name="regionName">Region to draw.</param>
|
||||
/// <param name="color">The color to tint the region.</param>
|
||||
/// <param name="batch">The batch to use.</param>
|
||||
/// <param name="destination">The destination to draw to.</param>
|
||||
/// <param name="rotation">The rotation to use in radians.</param>
|
||||
/// <param name="origin">The origin for the rotation.</param>
|
||||
public virtual void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns an <see cref="ISkinDefinitionData"/> of the given name and type.
|
||||
/// </summary>
|
||||
/// <param name="definitionName">Name of definition of the <paramref name="type"/></param>
|
||||
/// <param name="type">The UIModule the definition defines.</param>
|
||||
/// <returns>The interface for the definition.</returns>
|
||||
public virtual ISkinDefinitionData ObtainDefinition(string definitionName, Type type)
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
if (!Laminated) throw new InvalidOperationException("Skin has yet to be laminated yet.");
|
||||
if (definitionName == null) definitionName = "default";
|
||||
if (!definitions.ContainsKey(type) || !definitions[type].ContainsKey(definitionName)) throw new KeyNotFoundException("Could not find skin of type " + type.Name + " with name " + definitionName);
|
||||
return definitions[type][definitionName];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the default <see cref="ISkinDefinitionData"/> of the given parameters.
|
||||
/// </summary>
|
||||
/// <param name="type">The type of definition the default should be coming from.</param>
|
||||
/// <returns>The default definition for the given type.</returns>
|
||||
public virtual ISkinDefinitionData ObtainDefinition(Type type)
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
return ObtainDefinition(null, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam>
|
||||
/// <param name="definitionName">The name of the definition.</param>
|
||||
/// <param name="type">UIModule type the definition defines.</param>
|
||||
/// <returns>The definition cast to T.</returns>
|
||||
public virtual T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
if (definitionName == null) definitionName = "default";
|
||||
return (T)ObtainDefinition(definitionName, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the default definition.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Convenience to cast to T.</typeparam>
|
||||
/// <param name="type">The type of the UIModule to retrieve the default from.</param>
|
||||
/// <returns>The default definition for the given type.</returns>
|
||||
public virtual T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
return ObtainDefinition<T>(null, type);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds the definition.
|
||||
/// </summary>
|
||||
/// <param name="definitionName">The name of the definition.</param>
|
||||
/// <param name="skinDefinition">The definition itself.</param>
|
||||
public virtual void AddDefinition(string definitionName, ISkinDefinitionData skinDefinition)
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
if (Laminated) throw new InvalidOperationException("This object has been laminated and cannot be edited.");
|
||||
if (!definitions.ContainsKey(skinDefinition.UIModuleType))
|
||||
{
|
||||
definitions.Add(skinDefinition.UIModuleType, new Dictionary<string, ISkinDefinitionData>());
|
||||
} else if (definitions[skinDefinition.UIModuleType].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");
|
||||
|
||||
definitions[skinDefinition.UIModuleType].Add(definitionName, skinDefinition);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds color to skin.
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="color"></param>
|
||||
public virtual void AddColor(string name, Color color)
|
||||
{
|
||||
if (Laminated) throw new InvalidOperationException("This object has been laminated and cannot be edited.");
|
||||
colors.Add(name, color);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Laminates the skin. Making sure no more additions are done and sets the skin to be ready for use.
|
||||
/// Needs to be called before any use of skin. Building skin needs to be done before lamination.
|
||||
/// </summary>
|
||||
public void Laminate()
|
||||
{
|
||||
Laminated = true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes <see cref="textureAtlas"/> and the <see cref="Texture2D"/> holding the cursor texture.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
Dispose(true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Overridable dispose function.
|
||||
/// </summary>
|
||||
/// <param name="disposing">true when it's a user call to dispose.</param>
|
||||
public virtual void Dispose(bool disposing)
|
||||
{
|
||||
disposed = true;
|
||||
if (disposing && !disposed)
|
||||
{
|
||||
textureAtlas.Dispose();
|
||||
CursorTexture.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor. Calls the dispose with false.
|
||||
/// </summary>
|
||||
~Skin()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
226
RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs
Normal file
226
RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs
Normal file
@@ -0,0 +1,226 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using Newtonsoft.Json;
|
||||
using RecrownedAthenaeum.ContentReaders;
|
||||
using RecrownedAthenaeum.Data;
|
||||
using RecrownedAthenaeum.SpecialTypes;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.SkinSystem
|
||||
{
|
||||
/// <summary>
|
||||
/// Called when the skin manager has completed a async action.
|
||||
/// </summary>
|
||||
/// <param name="actionCompleted">The completed action.</param>
|
||||
public delegate void AsyncComplete(SkinManager.Action actionCompleted);
|
||||
|
||||
/// <summary>
|
||||
/// Manages reference to default and loading of custom skins.
|
||||
/// </summary>
|
||||
public class SkinManager
|
||||
{
|
||||
private const string EXTENSION = ".rbskin";
|
||||
private readonly MergedSkin mergedSkin = new MergedSkin();
|
||||
private Thread thread;
|
||||
private Action action;
|
||||
private GraphicsDevice graphicsDevice;
|
||||
private string selectedSkinPath;
|
||||
private SkinData skinDataToUse;
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not the skin manager is set up with a <see cref="BaseSkin"/> and <see cref="loadedSkin"/>.
|
||||
/// </summary>
|
||||
public bool MergingSkins { get { return (loadedSkin != null && SkinUseable); } }
|
||||
|
||||
/// <summary>
|
||||
/// Whether or not this manager has been set up with at least a base skin.
|
||||
/// </summary>
|
||||
public bool SkinUseable { get { return BaseSkin == null; } }
|
||||
|
||||
/// <summary>
|
||||
/// The list of paths for all found skins by <see cref="SearchSkinDirectory"/>. May be null.
|
||||
/// </summary>
|
||||
public volatile List<string> skinPaths;
|
||||
|
||||
/// <summary>
|
||||
/// The event that is called when a state changes.
|
||||
/// </summary>
|
||||
public event AsyncComplete AsyncCompleteEvent;
|
||||
|
||||
/// <summary>
|
||||
/// The various possible states a skin manager could be in.
|
||||
/// </summary>
|
||||
public enum Action
|
||||
{
|
||||
/// <summary>
|
||||
/// After a search has completed.
|
||||
/// </summary>
|
||||
SEARCH,
|
||||
/// <summary>
|
||||
/// Having the skin generated to be in a useable state.
|
||||
/// </summary>
|
||||
LOAD
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// the skin that favors the selected skin, but still has a fallback to the default skin in case anything is missing.
|
||||
/// </summary>
|
||||
public ISkin Skin { get { return mergedSkin; } }
|
||||
|
||||
/// <summary>
|
||||
/// The user loaded skin. Set by the skin loaded by calling <see cref="LoadSkin(SkinData, string, GraphicsDevice)"/>.
|
||||
/// </summary>
|
||||
public ISkin loadedSkin { get { return mergedSkin.mainSkin; } private set { mergedSkin.mainSkin = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// The default skin in case the selected skin doesn't cover a specific definition or color.
|
||||
/// </summary>
|
||||
public ISkin BaseSkin { get { return mergedSkin.alternateSkin; } set { mergedSkin.alternateSkin = value; } }
|
||||
|
||||
/// <summary>
|
||||
/// The directory that contains the skins.
|
||||
/// </summary>
|
||||
public string skinsDirectory;
|
||||
|
||||
/// <summary>
|
||||
/// Performs a recursive asynchronous search of the directory given in a path set by <see cref="skinsDirectory"/>.
|
||||
/// </summary>
|
||||
public void SearchSkinDirectory()
|
||||
{
|
||||
action = Action.SEARCH;
|
||||
AttemptAsync();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads skin data if extension is valid.
|
||||
/// </summary>
|
||||
/// <param name="path">the path to load from.</param>
|
||||
/// <returns>A <see cref="SkinData"/> that holds all the information and some metadata for the loaded skin.</returns>
|
||||
public SkinData ReadSkinData(string path)
|
||||
{
|
||||
if (path.ToLower().EndsWith(EXTENSION))
|
||||
{
|
||||
return JsonConvert.DeserializeObject<SkinData>(File.ReadAllText(path));
|
||||
}
|
||||
throw new ArgumentException("The path given does not point to a file with the required extension \"" + EXTENSION + "\" rather, has \"" + Path.GetExtension(path) + "\".");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// loads a skin asynchronously to the <see cref="loadedSkin"/>.
|
||||
/// </summary>
|
||||
/// <param name="graphicsDevice">Graphics device to use for texture creation. Uses graphics device from <see cref="Configuration"/>by default.</param>
|
||||
/// <param name="skinData">The data to generate from.</param>
|
||||
/// <param name="path">The path pointing to the file with the extension "<see cref="EXTENSION"/>".</param>
|
||||
public void LoadSkin(SkinData skinData, string path, GraphicsDevice graphicsDevice = null)
|
||||
{
|
||||
if (graphicsDevice == null) graphicsDevice = Configuration.graphicsDeviceManager.GraphicsDevice;
|
||||
action = Action.LOAD;
|
||||
this.graphicsDevice = graphicsDevice;
|
||||
this.selectedSkinPath = path;
|
||||
this.skinDataToUse = skinData;
|
||||
|
||||
AttemptAsync();
|
||||
}
|
||||
|
||||
private void AttemptAsync()
|
||||
{
|
||||
if (thread != null && thread.IsAlive) throw new InvalidOperationException("Already performing task: " + action);
|
||||
thread = new Thread(ThreadStart);
|
||||
}
|
||||
|
||||
private void ThreadStart()
|
||||
{
|
||||
switch (action)
|
||||
{
|
||||
case Action.SEARCH:
|
||||
SearchForSkins();
|
||||
OnAsyncComplete(action);
|
||||
break;
|
||||
case Action.LOAD:
|
||||
loadedSkin = LoadSkinFromData(skinDataToUse, selectedSkinPath, graphicsDevice);
|
||||
OnAsyncComplete(action);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private void SearchForSkins()
|
||||
{
|
||||
skinPaths.Clear();
|
||||
skinPaths = RecursiveSkinSearch(skinsDirectory);
|
||||
}
|
||||
|
||||
private Skin LoadSkinFromData(SkinData skinData, string path, GraphicsDevice graphicsDevice)
|
||||
{
|
||||
TextureAtlasDataReader textureAtlasDataReader = new TextureAtlasDataReader();
|
||||
FileInfo[] skinFiles = Directory.GetParent(path).GetFiles();
|
||||
Dictionary<string, string> fileNameWithPath = new Dictionary<string, string>();
|
||||
for (int i = 0; i < skinFiles.Length; i++)
|
||||
{
|
||||
fileNameWithPath.Add(skinFiles[i].Name, skinFiles[i].FullName);
|
||||
}
|
||||
TextureAtlasData atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(fileNameWithPath[skinData.nameOfTextureAtlas]);
|
||||
Texture2D atlasTexture;
|
||||
using (FileStream stream = new FileStream(fileNameWithPath[atlasData.textureName], FileMode.Open))
|
||||
{
|
||||
atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
|
||||
}
|
||||
TextureAtlas.Region[] regions = textureAtlasDataReader.GenerateAtlasRegionsFromData(atlasData, atlasTexture);
|
||||
TextureAtlas textureAtlas = new TextureAtlas(atlasTexture, regions);
|
||||
Texture2D cursorTexture;
|
||||
if (Path.HasExtension(skinData.cursorTextureName) && fileNameWithPath.ContainsKey(skinData.cursorTextureName))
|
||||
{
|
||||
using (FileStream stream = new FileStream(fileNameWithPath[skinData.cursorTextureName], FileMode.Open))
|
||||
{
|
||||
cursorTexture = Texture2D.FromStream(graphicsDevice, stream);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
cursorTexture = textureAtlas[skinData.cursorTextureName].AsTexture2D(graphicsDevice);
|
||||
}
|
||||
Skin skin = new Skin(new TextureAtlas(atlasTexture, regions), cursorTexture);
|
||||
|
||||
for (int i = 0; i < skinData.colors.Length; i++)
|
||||
{
|
||||
SkinData.ColorData colorData = skinData.colors[i];
|
||||
skin.AddColor(colorData.name, new Color(colorData.r, colorData.g, colorData.b, colorData.a));
|
||||
}
|
||||
|
||||
for (int i = 0; i < skinData.definitions.Length; i++)
|
||||
{
|
||||
SkinData.DefinitionData definitionData = skinData.definitions[i];
|
||||
skin.AddDefinition(definitionData.name, definitionData.skin);
|
||||
}
|
||||
|
||||
return skin;
|
||||
}
|
||||
|
||||
private List<string> RecursiveSkinSearch(string path)
|
||||
{
|
||||
string[] files = Directory.GetFiles(path);
|
||||
string[] folders = Directory.GetDirectories(path);
|
||||
List<string> skins = new List<string>();
|
||||
for (int i = 0; i < files.Length; i++)
|
||||
{
|
||||
if (files[i].ToLower().EndsWith(EXTENSION))
|
||||
{
|
||||
skins.Add(files[i]);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < folders.Length; i++)
|
||||
{
|
||||
skins.AddRange(RecursiveSkinSearch(folders[i]));
|
||||
}
|
||||
|
||||
return skins;
|
||||
}
|
||||
|
||||
private void OnAsyncComplete(Action newState)
|
||||
{
|
||||
AsyncCompleteEvent?.Invoke(newState);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user