recrownedgtk/RecrownedAthenaeum/UI/Skin/Skin.cs

130 lines
5.9 KiB
C#

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.Skin.Definitions;
using System;
using System.Collections.Generic;
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
{
/// <summary>
/// Texture atlas containing the skins textures.
/// </summary>
public readonly TextureAtlas textureAtlas;
/// <summary>
/// Colors stored in this skin.
/// </summary>
public readonly Dictionary<string, Color> colors;
Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions;
/// <summary>
/// Creates a basic unfilled skin.
/// </summary>
/// <param name="textureAtlas">The texture atlas to use for this skin.</param>
public Skin(TextureAtlas textureAtlas)
{
this.textureAtlas = textureAtlas;
colors = new Dictionary<string, Color>();
definitions = new Dictionary<Type, Dictionary<string, ISkinDefinitionData>>();
}
/// <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 void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
{
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 ISkinDefinitionData ObtainDefinition(string definitionName, Type type)
{
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);
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 ISkinDefinitionData ObtainDefinition(Type type)
{
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 T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData
{
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 T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData
{
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 void AddDefinition(string definitionName, ISkinDefinitionData skinDefinition)
{
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>
/// Removes the definition.
/// </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)
{
if (definitions.ContainsKey(definitionType) && definitions[definitionType].ContainsKey(definitionName))
{
definitions[definitionType].Remove(definitionName);
} else
{
throw new ArgumentException("a definition of type " + definitionType.Name + " with a name of " + definitionName + " does not exist.");
}
}
}
}