using RecrownedAthenaeum.Graphics.Render;
using RecrownedAthenaeum.Types;
using RecrownedAthenaeum.Graphics.UI.SkinSystem.Definitions;
using System;
using System.Collections.Generic;
using OpenTK.Graphics;
using OpenTK;
namespace RecrownedAthenaeum.Graphics.UI.SkinSystem
{
///
/// A skin is used to group a theme which can then be applied to the UI via the use of modules.
///
public class Skin : IDisposable, ISkin
{
///
/// Whether or not this skin is completed being built and thus ready to use.
///
public bool Laminated { get; private set; }
private bool disposed;
private TextureAtlas textureAtlas;
Dictionary colors;
readonly Dictionary definitionOfType;
readonly Dictionary> definitions;
///
/// The texture for the cursor.
///
public virtual Texture2D CursorTexture { get; private set; }
///
/// Creates a basic unfilled skin.
///
/// The texture atlas to use for this skin.
/// The texture the cursor will be.
public Skin(TextureAtlas textureAtlas, Texture2D cursorTexture)
{
this.textureAtlas = textureAtlas;
this.CursorTexture = cursorTexture;
colors = new Dictionary();
definitionOfType = new Dictionary();
definitions = new Dictionary>();
}
///
/// Returns a with given name of region. Null values acceptable. Will return null if parameter is null.
///
/// Name of region.
/// Whether or not this texture is mandatory for the module to work. If true, will throw error on failing to retrieve.
/// The region corresponding to the name or null if the requested region doesn't exist.
public TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false)
{
if (!required && (name == null || !textureAtlas.ContainsRegion(name)))
{
return null;
} else
{
return textureAtlas[name];
}
}
///
/// Returns a with given name of defined color;
///
/// Name of defined color. Will use "default" if null. Default value is null.
/// The defined color based on the name given.
public Color4 GetColor(string name = null)
{
if (name == null) name = "default";
return colors[name];
}
///
/// Draws a region from the texture atlas.
///
/// Region to draw.
/// The color to tint the region.
/// The batch to use.
/// The destination to draw to.
/// The rotation to use in radians.
/// The origin for the rotation.
public void Draw(string regionName, string color, ConsistentSpriteBatch 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);
}
private SkinDefinitionData ObtainDefinition(string typeFullName, string definitionName)
{
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(typeFullName)) throw new KeyNotFoundException("Could not find any skin definition defining type \"" + typeFullName + "\"");
if (!definitions[typeFullName].ContainsKey(definitionName)) throw new KeyNotFoundException("Could not find skin definition defining type \"" + typeFullName + "\" with name \"" + definitionName + "\"");
return definitions[typeFullName][definitionName];
}
///
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
///
/// Convenience to cast to the needed definition type.
/// The name of the definition.
/// The definition cast to T.
public T ObtainDefinition(string definitionName = null) where T : SkinDefinitionData
{
return (T)ObtainDefinition(definitionOfType[typeof(T).FullName], definitionName);
}
///
/// Adds the definition.
///
/// The name of the definition. Default (if left blank) name is "default".
/// The definition itself.
public void AddDefinition(SkinDefinitionData skinDefinition, string definitionName = null)
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
if (Laminated) throw new InvalidOperationException("This skin has been laminated and cannot be edited.");
if (definitionName == null) definitionName = "default";
if (!definitions.ContainsKey(skinDefinition.uiModuleTypeFullName))
{
definitionOfType.Add(skinDefinition.GetType().FullName, skinDefinition.uiModuleTypeFullName);
definitions.Add(skinDefinition.uiModuleTypeFullName, new Dictionary());
}
else if (definitions[skinDefinition.uiModuleTypeFullName].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");
definitions[skinDefinition.uiModuleTypeFullName].Add(definitionName, skinDefinition);
}
///
/// Adds color to skin.
///
///
///
public void AddColor(string name, Color4 color)
{
if (Laminated) throw new InvalidOperationException("This skin has been laminated and cannot be edited.");
colors.Add(name, color);
}
///
/// 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.
///
public void Laminate()
{
Laminated = true;
}
///
/// Disposes and the holding the cursor texture.
///
public void Dispose()
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
Dispose(true);
GC.SuppressFinalize(this);
}
///
/// Overridable dispose function.
///
/// true when it's a user call to dispose.
public virtual void Dispose(bool disposing)
{
disposed = true;
if (disposing && !disposed)
{
textureAtlas.Dispose();
CursorTexture.Dispose();
}
}
///
/// Destructor. Calls the dispose with false.
///
~Skin()
{
Dispose(false);
}
}
}