attempt at fixing skin system as well as improved convenience.

This commit is contained in:
Harrison Deng 2019-01-28 19:43:41 -06:00
parent 7892bff6be
commit 8ea9ae3921
7 changed files with 26 additions and 129 deletions

View File

@ -57,7 +57,7 @@ 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(ISkin skin, string definitionName = null) public Button(ISkin skin, string definitionName = null)
{ {
skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName, GetType()); skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName);
downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion); downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion);
upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion); upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion);
disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion); disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion);

View File

@ -45,7 +45,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// <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, SpriteFont font, ISkin skin, string definitionName = null) : base(skin, definitionName) public TextButton(string text, SpriteFont font, ISkin skin, string definitionName = null) : base(skin, definitionName)
{ {
TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName, GetType()); TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName);
this.text = new Text(font, text); this.text = new Text(font, text);
FontColor = skin.GetColor(skinDefinition.fontColor); FontColor = skin.GetColor(skinDefinition.fontColor);
} }

View File

@ -64,7 +64,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
/// <param name="content">The string of text to be displayed.</param> /// <param name="content">The string of text to be displayed.</param>
public Text(ISkin skin, SpriteFont font, string skinDefinitionName = null, string content = null) : this(font, content) public Text(ISkin skin, SpriteFont font, string skinDefinitionName = null, string content = null) : this(font, content)
{ {
skinDefinition = skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, GetType()); skinDefinition = skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName);
color = skin.GetColor(skinDefinition.color); color = skin.GetColor(skinDefinition.color);
} }

View File

@ -41,36 +41,12 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// <returns>The region corresponding to the name.</returns> /// <returns>The region corresponding to the name.</returns>
TextureAtlas.Region GetTextureAtlasRegion(string name); 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> /// <summary>
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist. /// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
/// </summary> /// </summary>
/// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam> /// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam>
/// <param name="definitionName">The name of the definition.</param> /// <param name="definitionName">The name of the definition. Default is null and will be replaced with "default" for name.</param>
/// <param name="type">UIModule type the definition defines.</param>
/// <returns>The definition cast to T.</returns> /// <returns>The definition cast to T.</returns>
T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData; T ObtainDefinition<T>(string definitionName = null) 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

@ -77,66 +77,15 @@ namespace RecrownedAthenaeum.UI.SkinSystem
} }
} }
public ISkinDefinitionData ObtainDefinition(string definitionName, Type type) public T ObtainDefinition<T>(string definitionName = null) where T : ISkinDefinitionData
{ {
try try
{ {
return mainSkin.ObtainDefinition(definitionName, type); return mainSkin.ObtainDefinition<T>(definitionName);
} catch (KeyNotFoundException)
{
return alternateSkin.ObtainDefinition(definitionName, type);
} }
catch (NullReferenceException) catch (NullReferenceException)
{ {
return alternateSkin.ObtainDefinition(definitionName, type); return alternateSkin.ObtainDefinition<T>(definitionName);
}
}
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);
} }
} }
} }

View File

@ -21,8 +21,8 @@ namespace RecrownedAthenaeum.UI.SkinSystem
private TextureAtlas textureAtlas; private TextureAtlas textureAtlas;
Dictionary<string, Color> colors; Dictionary<string, Color> colors;
readonly Dictionary<string, Type> moduleTypeOfDefinition;
Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions; readonly Dictionary<Type, Dictionary<string, ISkinDefinitionData>> definitions;
/// <summary> /// <summary>
/// The texture for the cursor. /// The texture for the cursor.
@ -39,6 +39,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
this.textureAtlas = textureAtlas; this.textureAtlas = textureAtlas;
this.CursorTexture = cursorTexture; this.CursorTexture = cursorTexture;
colors = new Dictionary<string, Color>(); colors = new Dictionary<string, Color>();
moduleTypeOfDefinition = new Dictionary<string, Type>();
definitions = new Dictionary<Type, Dictionary<string, ISkinDefinitionData>>(); definitions = new Dictionary<Type, Dictionary<string, ISkinDefinitionData>>();
} }
@ -77,73 +78,44 @@ namespace RecrownedAthenaeum.UI.SkinSystem
textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin); textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin);
} }
/// <summary> private ISkinDefinitionData ObtainDefinition(Type type, string definitionName)
/// 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 (disposed) throw new ObjectDisposedException(GetType().Name);
if (!Laminated) throw new InvalidOperationException("Skin has yet to be laminated yet."); 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 KeyNotFoundException("Could not find skin of type " + type.Name + " with name " + definitionName); 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]; 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 virtual ISkinDefinitionData ObtainDefinition(Type type)
{
if (disposed) throw new ObjectDisposedException(GetType().Name);
return ObtainDefinition(null, type);
}
/// <summary> /// <summary>
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist. /// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
/// </summary> /// </summary>
/// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam> /// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam>
/// <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>
/// <returns>The definition cast to T.</returns> /// <returns>The definition cast to T.</returns>
public virtual T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinitionData public virtual T ObtainDefinition<T>(string definitionName = null) where T : ISkinDefinitionData
{ {
if (disposed) throw new ObjectDisposedException(GetType().Name); return (T)ObtainDefinition(moduleTypeOfDefinition[typeof(T).FullName], definitionName);
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);
} }
/// <summary> /// <summary>
/// Adds the definition. /// Adds the definition.
/// </summary> /// </summary>
/// <param name="definitionName">The name of the definition.</param> /// <param name="definitionName">The name of the definition. Default (if left blank) name is "default".</param>
/// <param name="skinDefinition">The definition itself.</param> /// <param name="skinDefinition">The definition itself.</param>
public virtual void AddDefinition(string definitionName, ISkinDefinitionData skinDefinition) public virtual void AddDefinition(ISkinDefinitionData skinDefinition, string definitionName = null)
{ {
if (disposed) throw new ObjectDisposedException(GetType().Name); if (disposed) throw new ObjectDisposedException(GetType().Name);
if (Laminated) throw new InvalidOperationException("This object has been laminated and cannot be edited."); if (Laminated) throw new InvalidOperationException("This skin has been laminated and cannot be edited.");
if (!definitions.ContainsKey(skinDefinition.UIModuleType)) 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<string, ISkinDefinitionData>()); 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!"); } else if (definitions[moduleTypeOfDefinition[skinDefinition.GetType().FullName]].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");
definitions[skinDefinition.UIModuleType].Add(definitionName, skinDefinition); definitions[moduleTypeOfDefinition[skinDefinition.GetType().FullName]].Add(definitionName, skinDefinition);
} }
/// <summary> /// <summary>
@ -153,7 +125,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// <param name="color"></param> /// <param name="color"></param>
public virtual void AddColor(string name, Color color) public virtual void AddColor(string name, Color color)
{ {
if (Laminated) throw new InvalidOperationException("This object has been laminated and cannot be edited."); if (Laminated) throw new InvalidOperationException("This skin has been laminated and cannot be edited.");
colors.Add(name, color); colors.Add(name, color);
} }

View File

@ -192,7 +192,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
for (int i = 0; i < skinData.definitions.Length; i++) for (int i = 0; i < skinData.definitions.Length; i++)
{ {
SkinData.DefinitionData definitionData = skinData.definitions[i]; SkinData.DefinitionData definitionData = skinData.definitions[i];
skin.AddDefinition(definitionData.name, definitionData.skin); skin.AddDefinition(definitionData.skin, definitionData.name);
} }
return skin; return skin;