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 { /// /// 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 moduleTypeOfDefinition; 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(); moduleTypeOfDefinition = new Dictionary(); definitions = new Dictionary>(); } /// /// Returns a with given name of region. /// /// Name of region. /// The region corresponding to the name. public virtual TextureAtlas.Region GetTextureAtlasRegion(string name) { return textureAtlas[name]; } /// /// Returns a with given name of defined color; /// /// Name of defined color. /// The defined color based on the name given. public virtual Color GetColor(string name) { 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 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); } private ISkinDefinitionData ObtainDefinition(Type type, 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(type)) throw new KeyNotFoundException("Could not find any skin definition defining type \"" + type.FullName + "\""); if (!definitions[type].ContainsKey(definitionName)) throw new KeyNotFoundException("Could not find skin definition defining type \"" + type.Name + "\" with name \"" + definitionName + "\""); return definitions[type][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 virtual T ObtainDefinition(string definitionName = null) where T : ISkinDefinitionData { return (T)ObtainDefinition(moduleTypeOfDefinition[typeof(T).FullName], definitionName); } /// /// Adds the definition. /// /// The name of the definition. Default (if left blank) name is "default". /// The definition itself. public virtual void AddDefinition(ISkinDefinitionData 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(moduleTypeOfDefinition[skinDefinition.GetType().FullName])) { moduleTypeOfDefinition.Add(skinDefinition.GetType().FullName, skinDefinition.UIModuleType); definitions.Add(skinDefinition.UIModuleType, new Dictionary()); } else if (definitions[moduleTypeOfDefinition[skinDefinition.GetType().FullName]].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!"); definitions[moduleTypeOfDefinition[skinDefinition.GetType().FullName]].Add(definitionName, skinDefinition); } /// /// Adds color to skin. /// /// /// public virtual void AddColor(string name, Color 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); } } }