Progress on skin system, added skin stack.
This commit is contained in:
@@ -4,36 +4,65 @@ using RecrownedAthenaeum.SpecialTypes;
|
||||
using RecrownedAthenaeum.UI.Skin.Definitions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
|
||||
namespace RecrownedAthenaeum.UI.Skin
|
||||
{
|
||||
/// <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
|
||||
public class Skin : IDisposable, ISkin
|
||||
{
|
||||
/// <summary>
|
||||
/// Texture atlas containing the skins textures.
|
||||
/// Whether or not this skin is completed being built and thus ready to use.
|
||||
/// </summary>
|
||||
public readonly TextureAtlas textureAtlas;
|
||||
/// <summary>
|
||||
/// Colors stored in this skin.
|
||||
/// </summary>
|
||||
public readonly Dictionary<string, Color> colors;
|
||||
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>
|
||||
public Skin(TextureAtlas textureAtlas)
|
||||
/// <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>
|
||||
@@ -43,8 +72,9 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
/// <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 void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -54,10 +84,12 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
/// <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 ISkinDefinitionData ObtainDefinition(string definitionName, Type type)
|
||||
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 ArgumentException("Could not find skin of type " + type.Name + " with name " + definitionName);
|
||||
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];
|
||||
}
|
||||
|
||||
@@ -66,8 +98,9 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
/// </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 ISkinDefinitionData ObtainDefinition(Type type)
|
||||
public virtual ISkinDefinitionData ObtainDefinition(Type type)
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
return ObtainDefinition(null, type);
|
||||
}
|
||||
|
||||
@@ -78,8 +111,9 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
/// <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 T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData
|
||||
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);
|
||||
}
|
||||
@@ -90,8 +124,9 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
/// <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 T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData
|
||||
public virtual T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData
|
||||
{
|
||||
if (disposed) throw new ObjectDisposedException(GetType().Name);
|
||||
return ObtainDefinition<T>(null, type);
|
||||
}
|
||||
|
||||
@@ -100,8 +135,10 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
/// </summary>
|
||||
/// <param name="definitionName">The name of the definition.</param>
|
||||
/// <param name="skinDefinition">The definition itself.</param>
|
||||
public void AddDefinition(string definitionName, ISkinDefinitionData skinDefinition)
|
||||
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>());
|
||||
@@ -111,19 +148,55 @@ namespace RecrownedAthenaeum.UI.Skin
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Removes the definition.
|
||||
/// Adds color to skin.
|
||||
/// </summary>
|
||||
/// <param name="definitionName">The name of the definition.</param>
|
||||
/// <param name="definitionType">The type of the definition.</param>
|
||||
public void RemoveDefinition(string definitionName, Type definitionType)
|
||||
/// <param name="name"></param>
|
||||
/// <param name="color"></param>
|
||||
public virtual void AddColor(string name, Color color)
|
||||
{
|
||||
if (definitions.ContainsKey(definitionType) && definitions[definitionType].ContainsKey(definitionName))
|
||||
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)
|
||||
{
|
||||
definitions[definitionType].Remove(definitionName);
|
||||
} else
|
||||
{
|
||||
throw new ArgumentException("a definition of type " + definitionType.Name + " with a name of " + definitionName + " does not exist.");
|
||||
textureAtlas.Dispose();
|
||||
CursorTexture.Dispose();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Destructor. Calls the dispose with false.
|
||||
/// </summary>
|
||||
~Skin()
|
||||
{
|
||||
Dispose(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user