recrownedathenaeum/RecrownedAthenaeum/UI/SkinSystem/Skin.cs

202 lines
8.6 KiB
C#
Raw Normal View History

2018-12-11 07:12:34 +00:00
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2019-01-15 23:33:55 +00:00
using RecrownedAthenaeum.SpecialTypes;
2019-01-27 23:39:18 +00:00
using RecrownedAthenaeum.UI.SkinSystem.Definitions;
2018-12-11 07:12:34 +00:00
using System;
using System.Collections.Generic;
2019-01-27 23:39:18 +00:00
namespace RecrownedAthenaeum.UI.SkinSystem
2018-12-11 07:12:34 +00:00
{
2019-01-14 06:34:35 +00:00
/// <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
2018-12-11 07:12:34 +00:00
{
2019-01-14 06:34:35 +00:00
/// <summary>
/// Whether or not this skin is completed being built and thus ready to use.
2019-01-14 06:34:35 +00:00
/// </summary>
public bool Laminated { get; private set; }
private bool disposed;
private TextureAtlas textureAtlas;
Dictionary<string, Color> colors;
2019-01-16 01:34:02 +00:00
2019-01-15 23:03:17 +00:00
Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions;
2018-12-11 07:12:34 +00:00
/// <summary>
/// The texture for the cursor.
/// </summary>
public virtual Texture2D CursorTexture { get; private set; }
2019-01-14 06:34:35 +00:00
/// <summary>
2019-01-14 07:26:46 +00:00
/// Creates a basic unfilled skin.
2019-01-14 06:34:35 +00:00
/// </summary>
2019-01-14 07:26:46 +00:00
/// <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)
2018-12-11 07:12:34 +00:00
{
this.textureAtlas = textureAtlas;
this.CursorTexture = cursorTexture;
2018-12-11 07:12:34 +00:00
colors = new Dictionary<string, Color>();
2019-01-15 23:03:17 +00:00
definitions = new Dictionary<Type, Dictionary<string, ISkinDefinitionData>>();
2018-12-11 07:12:34 +00:00
}
/// <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];
}
2019-01-14 07:26:46 +00:00
/// <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))
2018-12-11 07:12:34 +00:00
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin);
}
/// <summary>
2019-01-15 23:03:17 +00:00
/// 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>
2019-01-15 23:03:17 +00:00
/// 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);
2018-12-11 07:12:34 +00:00
}
2019-01-14 07:26:46 +00:00
/// <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)
2018-12-11 07:12:34 +00:00
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
if (Laminated) throw new InvalidOperationException("This object has been laminated and cannot be edited.");
2018-12-11 07:12:34 +00:00
if (!definitions.ContainsKey(skinDefinition.UIModuleType))
{
2019-01-15 23:03:17 +00:00
definitions.Add(skinDefinition.UIModuleType, new Dictionary<string, ISkinDefinitionData>());
2018-12-11 07:12:34 +00:00
} else if (definitions[skinDefinition.UIModuleType].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");
definitions[skinDefinition.UIModuleType].Add(definitionName, skinDefinition);
}
2019-01-14 07:26:46 +00:00
/// <summary>
/// Adds color to skin.
2019-01-14 07:26:46 +00:00
/// </summary>
/// <param name="name"></param>
/// <param name="color"></param>
public virtual void AddColor(string name, Color color)
2018-12-11 07:12:34 +00:00
{
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)
2018-12-11 07:12:34 +00:00
{
textureAtlas.Dispose();
CursorTexture.Dispose();
2018-12-11 07:12:34 +00:00
}
}
/// <summary>
/// Destructor. Calls the dispose with false.
/// </summary>
~Skin()
{
Dispose(false);
}
2018-12-11 07:12:34 +00:00
}
}