Progress on skin system, added skin stack.

This commit is contained in:
Harrison Deng 2019-01-20 01:08:38 -06:00
parent 6fdcdcf923
commit ea8f96d877
10 changed files with 393 additions and 64 deletions

View File

@ -14,13 +14,13 @@ namespace RecrownedAthenaeum.SpecialTypes
private Texture2D texture; private Texture2D texture;
private bool disposed; private bool disposed;
private Dictionary<string, Region> dictionaryOfRegions = new Dictionary<string, Region>(); private Dictionary<string, Region> dictionaryOfRegions = new Dictionary<string, Region>();
/// <summary> /// <summary>
/// Given a name, can return a <see cref="Region"/>. /// Given a name, can return a <see cref="Region"/>.
/// </summary> /// </summary>
/// <param name="name">Name of <see cref="Region"/> to obtain.</param> /// <param name="name">Name of <see cref="Region"/> to obtain.</param>
/// <returns><see cref="Region"/> based off name.</returns> /// <returns><see cref="Region"/> based off name.</returns>
public Region this[string name] { get { if (name != null && dictionaryOfRegions.ContainsKey(name)) return dictionaryOfRegions[name]; else return null; } } public Region this[string name] { get { if (name != null && dictionaryOfRegions.ContainsKey(name)) return dictionaryOfRegions[name]; else throw new KeyNotFoundException("Given key \"" + name + "\" does not exist."); } }
/// <summary> /// <summary>
/// Creates a texture atlas with given main texture as well as an array of <see cref="Region"/> to represent locations of which textures reside within the atlas. Region names will be used to refer to the regions within the dictionary. /// Creates a texture atlas with given main texture as well as an array of <see cref="Region"/> to represent locations of which textures reside within the atlas. Region names will be used to refer to the regions within the dictionary.
@ -92,7 +92,12 @@ namespace RecrownedAthenaeum.SpecialTypes
texture.Dispose(); texture.Dispose();
for (int i = 0; i < dictionaryOfRegions.Count; i++) for (int i = 0; i < dictionaryOfRegions.Count; i++)
{ {
dictionaryOfRegions.ElementAt(i).Value.Dispose(); Region region = dictionaryOfRegions.ElementAt(i).Value;
if (!region.Disposed)
{
dictionaryOfRegions.ElementAt(i).Value.Dispose();
}
} }
dictionaryOfRegions.Clear(); dictionaryOfRegions.Clear();
} }
@ -123,7 +128,11 @@ namespace RecrownedAthenaeum.SpecialTypes
readonly NinePatch ninepatch; readonly NinePatch ninepatch;
Texture2D atlasTexture; Texture2D atlasTexture;
Texture2D regionTexture; Texture2D regionTexture;
private bool disposed;
/// <summary>
/// If region has already been disposed.
/// </summary>
public bool Disposed { get; private set; }
/// <summary> /// <summary>
/// A specified region in a texture atlas. /// A specified region in a texture atlas.
@ -150,12 +159,13 @@ namespace RecrownedAthenaeum.SpecialTypes
/// <param name="origin">The origin of the drawing. Ignored if is a 9patch.</param> /// <param name="origin">The origin of the drawing. Ignored if is a 9patch.</param>
public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2)) public void Draw(SpriteBatch batch, Rectangle destination, Color color, float rotation = 0, Vector2 origin = default(Vector2))
{ {
if (disposed) throw new ObjectDisposedException(GetType().Name); if (Disposed) throw new ObjectDisposedException(GetType().Name);
if (ninepatch != null) if (ninepatch != null)
{ {
ninepatch.Draw(batch, destination); ninepatch.Draw(batch, destination);
} else }
else
{ {
batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f); batch.Draw(atlasTexture, destination, sourceRectangle, color, rotation, origin, SpriteEffects.None, 0f);
} }
@ -168,7 +178,7 @@ namespace RecrownedAthenaeum.SpecialTypes
/// <returns>The texture of the region.</returns> /// <returns>The texture of the region.</returns>
public Texture2D AsTexture2D(GraphicsDevice graphicsDevice) public Texture2D AsTexture2D(GraphicsDevice graphicsDevice)
{ {
if (disposed) throw new ObjectDisposedException(GetType().Name); if (Disposed) throw new ObjectDisposedException(GetType().Name);
if (regionTexture == null) if (regionTexture == null)
{ {
@ -195,7 +205,7 @@ namespace RecrownedAthenaeum.SpecialTypes
/// </summary> /// </summary>
public void Dispose() public void Dispose()
{ {
if (disposed) throw new ObjectDisposedException(GetType().Name); if (Disposed) throw new ObjectDisposedException(GetType().Name);
Dispose(true); Dispose(true);
GC.SuppressFinalize(this); GC.SuppressFinalize(this);
} }
@ -206,13 +216,13 @@ namespace RecrownedAthenaeum.SpecialTypes
/// <param name="disposing">Whether or not this was a user made call.</param> /// <param name="disposing">Whether or not this was a user made call.</param>
public virtual void Dispose(bool disposing) public virtual void Dispose(bool disposing)
{ {
if (disposing && !disposed) if (disposing && !Disposed)
{ {
regionTexture?.Dispose(); regionTexture?.Dispose();
} }
disposed = true; Disposed = true;
} }
/// <summary> /// <summary>
/// Destructor. /// Destructor.
/// </summary> /// </summary>

View File

@ -55,11 +55,11 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// <param name="definitionName">The name of the definition in the skin. Can be null to select the default.</param> /// <param name="definitionName">The name of the definition in the skin. Can be null to select the default.</param>
public Button(Skin.Skin skin, string definitionName = null) public Button(Skin.Skin skin, string definitionName = null)
{ {
this.skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName, GetType()); skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName, GetType());
downTexture = skin.textureAtlas[skinDefinition.downRegion]; downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion);
upTexture = skin.textureAtlas[skinDefinition.upRegion]; upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion);
disabledTexture = skin.textureAtlas[skinDefinition.disabledRegion]; disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion);
highlightedTexture = skin.textureAtlas[skinDefinition.selectedRegion]; highlightedTexture = skin.GetTextureAtlasRegion(skinDefinition.selectedRegion);
} }
/// <summary> /// <summary>

View File

@ -38,13 +38,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// Constructs a text button using a skin and definition. /// Constructs a text button using a skin and definition.
/// </summary> /// </summary>
/// <param name="text">The text to display.</param> /// <param name="text">The text to display.</param>
/// <param name="font">The font to be used.</param>
/// <param name="skin">The skin to use.</param> /// <param name="skin">The skin to use.</param>
/// <param name="definitionName">Name of the definition for this type in the skin given.</param> /// <param name="definitionName">Name of the definition for this type in the skin given.</param>
public TextButton(string text, Skin.Skin skin, string definitionName = null) : base(skin, definitionName) public TextButton(string text, SpriteFont font, Skin.Skin skin, string definitionName = null) : base(skin, definitionName)
{ {
TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName, GetType()); TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName, GetType());
this.text = new Text(skin.fonts[skinDefinition.fontName], text); this.text = new Text(font, text);
FontColor = skin.colors[skinDefinition.fontColor]; FontColor = skin.GetColor(skinDefinition.fontColor);
} }
/// <summary> /// <summary>

View File

@ -57,13 +57,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <summary> /// <summary>
/// Creates a UI text object /// Creates a UI text object
/// </summary> /// </summary>
/// <param name="skin"></param> /// <param name="skin">The skin to be used.</param>
/// <param name="skinDefinitionName"></param> /// <param name="skinDefinitionName">The name of the skin's definition to use of a <see cref="TextSkinDefinition"/>.</param>
/// <param name="content"></param> /// <param name="font">The font to be used.</param>
public Text(Skin.Skin skin, string skinDefinitionName = null, string content = null) : this(skin.fonts[skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, typeof(Text)).font]) /// <param name="content">The string of text to be displayed.</param>
public Text(Skin.Skin skin, SpriteFont font, string skinDefinitionName = null, string content = null) : this(font, content)
{ {
skinDefinition = skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, GetType()); skinDefinition = skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, GetType());
color = skin.colors[skinDefinition.color]; color = skin.GetColor(skinDefinition.color);
} }
/// <summary> /// <summary>

View File

@ -8,11 +8,6 @@ namespace RecrownedAthenaeum.UI.Skin.Definitions
/// </summary> /// </summary>
public class TextButtonSkinDefinition : ButtonSkinDefinition public class TextButtonSkinDefinition : ButtonSkinDefinition
{ {
/// <summary>
/// Name of font from the skin to use.
/// </summary>
public string fontName;
/// <summary> /// <summary>
/// Name of color from the skin to use for the font. /// Name of color from the skin to use for the font.
/// </summary> /// </summary>

View File

@ -5,13 +5,11 @@ namespace RecrownedAthenaeum.UI.Skin.Definitions
{ {
class TextSkinDefinition : ISkinDefinitionData class TextSkinDefinition : ISkinDefinitionData
{ {
public string font;
public string color; public string color;
public Type UIModuleType { get { return typeof(Text); } } public Type UIModuleType { get { return typeof(Text); } }
public TextSkinDefinition(string font, string color) public TextSkinDefinition(string color)
{ {
this.font = font;
this.color = color; this.color = color;
} }
} }

View File

@ -0,0 +1,76 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.Skin.Definitions;
namespace RecrownedAthenaeum.UI.Skin
{
/// <summary>
/// The output requirements of a skin. This allows for very customized skin systems if needed.
/// </summary>
public interface ISkin
{
/// <summary>
/// The texture for the cursor.
/// </summary>
Texture2D CursorTexture { get; }
/// <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>
void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2));
/// <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>
Color GetColor(string name);
/// <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>
TextureAtlas.Region GetTextureAtlasRegion(string name);
/// <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>
ISkinDefinitionData ObtainDefinition(string definitionName, Type type);
/// <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>
ISkinDefinitionData ObtainDefinition(Type 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>
T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData;
/// <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>
T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData;
}
}

View File

@ -4,36 +4,65 @@ using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.Skin.Definitions; using RecrownedAthenaeum.UI.Skin.Definitions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
namespace RecrownedAthenaeum.UI.Skin namespace RecrownedAthenaeum.UI.Skin
{ {
/// <summary> /// <summary>
/// A skin is used to group a theme which can then be applied to the UI via the use of modules. /// A skin is used to group a theme which can then be applied to the UI via the use of modules.
/// </summary> /// </summary>
public class Skin public class Skin : IDisposable, ISkin
{ {
/// <summary> /// <summary>
/// Texture atlas containing the skins textures. /// Whether or not this skin is completed being built and thus ready to use.
/// </summary> /// </summary>
public readonly TextureAtlas textureAtlas; public bool Laminated { get; private set; }
/// <summary> private bool disposed;
/// Colors stored in this skin.
/// </summary> private TextureAtlas textureAtlas;
public readonly Dictionary<string, Color> colors;
Dictionary<string, Color> colors;
Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions; Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions;
/// <summary>
/// The texture for the cursor.
/// </summary>
public virtual Texture2D CursorTexture { get; private set; }
/// <summary> /// <summary>
/// Creates a basic unfilled skin. /// Creates a basic unfilled skin.
/// </summary> /// </summary>
/// <param name="textureAtlas">The texture atlas to use for this skin.</param> /// <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.textureAtlas = textureAtlas;
this.CursorTexture = cursorTexture;
colors = new Dictionary<string, Color>(); colors = new Dictionary<string, Color>();
definitions = new Dictionary<Type, Dictionary<string, ISkinDefinitionData>>(); 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> /// <summary>
/// Draws a region from the texture atlas. /// Draws a region from the texture atlas.
/// </summary> /// </summary>
@ -43,8 +72,9 @@ namespace RecrownedAthenaeum.UI.Skin
/// <param name="destination">The destination to draw to.</param> /// <param name="destination">The destination to draw to.</param>
/// <param name="rotation">The rotation to use in radians.</param> /// <param name="rotation">The rotation to use in radians.</param>
/// <param name="origin">The origin for the rotation.</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); 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="definitionName">Name of definition of the <paramref name="type"/></param>
/// <param name="type">The UIModule the definition defines.</param> /// <param name="type">The UIModule the definition defines.</param>
/// <returns>The interface for the definition.</returns> /// <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 (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]; return definitions[type][definitionName];
} }
@ -66,8 +98,9 @@ namespace RecrownedAthenaeum.UI.Skin
/// </summary> /// </summary>
/// <param name="type">The type of definition the default should be coming from.</param> /// <param name="type">The type of definition the default should be coming from.</param>
/// <returns>The default definition for the given type.</returns> /// <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); return ObtainDefinition(null, type);
} }
@ -78,8 +111,9 @@ namespace RecrownedAthenaeum.UI.Skin
/// <param name="definitionName">The name of the definition.</param> /// <param name="definitionName">The name of the definition.</param>
/// <param name="type">UIModule type the definition defines.</param> /// <param name="type">UIModule type the definition defines.</param>
/// <returns>The definition cast to T.</returns> /// <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"; if (definitionName == null) definitionName = "default";
return (T)ObtainDefinition(definitionName, type); return (T)ObtainDefinition(definitionName, type);
} }
@ -90,8 +124,9 @@ namespace RecrownedAthenaeum.UI.Skin
/// <typeparam name="T">Convenience to cast to T.</typeparam> /// <typeparam name="T">Convenience to cast to T.</typeparam>
/// <param name="type">The type of the UIModule to retrieve the default from.</param> /// <param name="type">The type of the UIModule to retrieve the default from.</param>
/// <returns>The default definition for the given type.</returns> /// <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); return ObtainDefinition<T>(null, type);
} }
@ -100,8 +135,10 @@ namespace RecrownedAthenaeum.UI.Skin
/// </summary> /// </summary>
/// <param name="definitionName">The name of the definition.</param> /// <param name="definitionName">The name of the definition.</param>
/// <param name="skinDefinition">The definition itself.</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)) if (!definitions.ContainsKey(skinDefinition.UIModuleType))
{ {
definitions.Add(skinDefinition.UIModuleType, new Dictionary<string, ISkinDefinitionData>()); definitions.Add(skinDefinition.UIModuleType, new Dictionary<string, ISkinDefinitionData>());
@ -111,19 +148,55 @@ namespace RecrownedAthenaeum.UI.Skin
} }
/// <summary> /// <summary>
/// Removes the definition. /// Adds color to skin.
/// </summary> /// </summary>
/// <param name="definitionName">The name of the definition.</param> /// <param name="name"></param>
/// <param name="definitionType">The type of the definition.</param> /// <param name="color"></param>
public void RemoveDefinition(string definitionName, Type definitionType) 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); textureAtlas.Dispose();
} else CursorTexture.Dispose();
{
throw new ArgumentException("a definition of type " + definitionType.Name + " with a name of " + definitionName + " does not exist.");
} }
} }
/// <summary>
/// Destructor. Calls the dispose with false.
/// </summary>
~Skin()
{
Dispose(false);
}
} }
} }

View File

@ -2,8 +2,9 @@
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using Newtonsoft.Json; using Newtonsoft.Json;
using RecrownedAthenaeum.Pipeline; using RecrownedAthenaeum.Pipeline;
using RecrownedAthenaeum.Serializable; using RecrownedAthenaeum.Data;
using RecrownedAthenaeum.SpecialTypes; using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.Skin.Definitions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
@ -16,10 +17,29 @@ namespace RecrownedAthenaeum.UI.Skin
public class SkinManager public class SkinManager
{ {
private const string EXTENSION = ".rbskin"; private const string EXTENSION = ".rbskin";
private readonly MergedSkin mergedSkin = new MergedSkin();
/// <summary>
/// the skin that favors the selected skin, but still has a fallback to the default skin in case anything is missing.
/// </summary>
public ISkin Skin { get { return mergedSkin; } }
/// <summary>
/// The user selected skin.
/// </summary>
public ISkin SelectedSkin { get { return mergedSkin.mainSkin; } set { mergedSkin.mainSkin = value; } }
/// <summary>
/// The fallback skin in case the selected skin doesn't cover a specific definition or color.
/// </summary>
public ISkin BaseSkin { get { return mergedSkin.alternateSkin; } set { mergedSkin.alternateSkin = value; } }
/// <summary> /// <summary>
/// The directory that contains the skins. /// The directory that contains the skins.
/// </summary> /// </summary>
public string skinsDirectory; public string skinsDirectory;
List<string> skinPaths = new List<string>(); List<string> skinPaths = new List<string>();
/// <summary> /// <summary>
@ -49,27 +69,43 @@ namespace RecrownedAthenaeum.UI.Skin
/// Generate skin from skin data. /// Generate skin from skin data.
/// </summary> /// </summary>
/// <param name="skinData">The skin data to generate skin from.</param> /// <param name="skinData">The skin data to generate skin from.</param>
/// <param name="path">The path pointing the ".rbskin" file.</param> /// <param name="path">The path pointing to the file with the extension "<see cref="EXTENSION"/>".</param>
/// <param name="graphicsDevice">The graphics device to generate the texture.</param> /// <param name="graphicsDevice">The graphics device to generate the texture.</param>
/// <returns>The skin generated from the skin data.</returns> /// <returns>The skin generated from the skin data.</returns>
public Skin GenerateSkinFromData(SkinData skinData, string path, GraphicsDevice graphicsDevice) public Skin GenerateSkinFromData(SkinData skinData, string path, GraphicsDevice graphicsDevice)
{ {
TextureAtlasDataReader textureAtlasDataReader = new TextureAtlasDataReader(); TextureAtlasDataReader textureAtlasDataReader = new TextureAtlasDataReader();
DirectoryInfo parentFolder = Directory.GetParent(path); FileInfo[] skinFiles = Directory.GetParent(path).GetFiles();
TextureAtlasData atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(parentFolder.FullName + "/" + skinData.nameOfTextureAtlas); Dictionary<string, string> fileNameWithPath = new Dictionary<string, string>();
for (int i = 0; i < skinFiles.Length; i++)
{
fileNameWithPath.Add(skinFiles[i].Name, skinFiles[i].FullName);
}
TextureAtlasData atlasData = JsonConvert.DeserializeObject<TextureAtlasData>(fileNameWithPath[skinData.nameOfTextureAtlas]);
Texture2D atlasTexture; Texture2D atlasTexture;
using (FileStream stream = new FileStream(parentFolder.FullName + "/" + atlasData.textureName, FileMode.Open)) using (FileStream stream = new FileStream(fileNameWithPath[atlasData.textureName], FileMode.Open))
{ {
atlasTexture = Texture2D.FromStream(graphicsDevice, stream); atlasTexture = Texture2D.FromStream(graphicsDevice, stream);
} }
TextureAtlas.Region[] regions = textureAtlasDataReader.GenerateAtlasRegionsFromData(atlasData, atlasTexture); TextureAtlas.Region[] regions = textureAtlasDataReader.GenerateAtlasRegionsFromData(atlasData, atlasTexture);
TextureAtlas textureAtlas = new TextureAtlas(atlasTexture, regions);
Skin skin = new Skin(new TextureAtlas(atlasTexture, regions)); Texture2D cursorTexture;
if (Path.HasExtension(skinData.cursorTextureName) && fileNameWithPath.ContainsKey(skinData.cursorTextureName))
{
using (FileStream stream = new FileStream(fileNameWithPath[skinData.cursorTextureName], FileMode.Open))
{
cursorTexture = Texture2D.FromStream(graphicsDevice, stream);
}
} else
{
cursorTexture = textureAtlas[skinData.cursorTextureName].AsTexture2D(graphicsDevice);
}
Skin skin = new Skin(new TextureAtlas(atlasTexture, regions), cursorTexture);
for (int i = 0; i < skinData.colors.Length; i++) for (int i = 0; i < skinData.colors.Length; i++)
{ {
SkinData.ColorData colorData = skinData.colors[i]; SkinData.ColorData colorData = skinData.colors[i];
skin.colors.Add(colorData.name, new Color(colorData.r, colorData.g, colorData.b, colorData.a)); skin.AddColor(colorData.name, new Color(colorData.r, colorData.g, colorData.b, colorData.a));
} }
for (int i = 0; i < skinData.definitions.Length; i++) for (int i = 0; i < skinData.definitions.Length; i++)
@ -100,5 +136,6 @@ namespace RecrownedAthenaeum.UI.Skin
return skins; return skins;
} }
} }
} }

View File

@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.SpecialTypes;
using RecrownedAthenaeum.UI.Skin.Definitions;
namespace RecrownedAthenaeum.UI.Skin
{
internal class MergedSkin : ISkin
{
/// <summary>
/// The skin to try to use first.
/// </summary>
public ISkin mainSkin;
/// <summary>
/// The fallback skin.
/// </summary>
public ISkin alternateSkin;
public Texture2D CursorTexture
{
get
{
if (mainSkin.CursorTexture != null) return mainSkin.CursorTexture;
return alternateSkin.CursorTexture;
}
}
public void Draw(string regionName, string color, SpriteBatch batch, Rectangle destination, float rotation = 0, Vector2 origin = default(Vector2))
{
try
{
mainSkin.Draw(regionName, color, batch, destination, rotation, origin);
} catch (KeyNotFoundException)
{
alternateSkin.Draw(regionName, color, batch, destination, rotation, origin);
} catch (NullReferenceException)
{
alternateSkin.Draw(regionName, color, batch, destination, rotation, origin);
}
}
public Color GetColor(string name)
{
try
{
return mainSkin.GetColor(name);
} catch (KeyNotFoundException)
{
return alternateSkin.GetColor(name);
}
catch (NullReferenceException)
{
return alternateSkin.GetColor(name);
}
}
public TextureAtlas.Region GetTextureAtlasRegion(string name)
{
try
{
return mainSkin.GetTextureAtlasRegion(name);
} catch (KeyNotFoundException)
{
return alternateSkin.GetTextureAtlasRegion(name);
}
catch (NullReferenceException)
{
return alternateSkin.GetTextureAtlasRegion(name);
}
}
public ISkinDefinitionData ObtainDefinition(string definitionName, Type type)
{
try
{
return mainSkin.ObtainDefinition(definitionName, type);
} catch (KeyNotFoundException)
{
return alternateSkin.ObtainDefinition(definitionName, type);
}
catch (NullReferenceException)
{
return alternateSkin.ObtainDefinition(definitionName, type);
}
}
public ISkinDefinitionData ObtainDefinition(Type type)
{
try
{
return mainSkin.ObtainDefinition(type);
}
catch (KeyNotFoundException)
{
return alternateSkin.ObtainDefinition(type);
}
catch (NullReferenceException)
{
return alternateSkin.ObtainDefinition(type);
}
}
public T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData
{
try
{
return mainSkin.ObtainDefinition<T>(definitionName, type);
}
catch (KeyNotFoundException)
{
return alternateSkin.ObtainDefinition<T>(definitionName, type);
}
catch (NullReferenceException)
{
return alternateSkin.ObtainDefinition<T>(definitionName, type);
}
}
public T ObtainDefinition<T>(Type type) where T : ISkinDefinitionData
{
try
{
return mainSkin.ObtainDefinition<T>(type);
}
catch (KeyNotFoundException)
{
return alternateSkin.ObtainDefinition<T>(type);
}
catch (NullReferenceException)
{
return alternateSkin.ObtainDefinition<T>(type);
}
}
}
}