Added better exception throwing for missing regions for skins by adding a required option for regions.

This commit is contained in:
Harrison Deng 2019-03-07 23:43:25 -06:00
parent 59771b00a8
commit ebe22fa9dc
5 changed files with 22 additions and 14 deletions

View File

@ -58,8 +58,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
public Button(ISkin skin, string definitionName = null)
{
skinDefinition = skin.ObtainDefinition<ButtonSkinDefinition>(definitionName);
downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion);
upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion);
downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion, true);
upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion, true);
disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion);
highlightedTexture = skin.GetTextureAtlasRegion(skinDefinition.selectedRegion);
}
@ -70,8 +70,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
/// <param name="skin">The skin the definition is defined in.</param>
/// <param name="skinDefinition">The definition itself.</param>
public Button(ISkin skin, ButtonSkinDefinition skinDefinition) :
this(skin.GetTextureAtlasRegion(skinDefinition.downRegion),
skin.GetTextureAtlasRegion(skinDefinition.upRegion),
this(skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
skin.GetTextureAtlasRegion(skinDefinition.selectedRegion))
{}

View File

@ -62,8 +62,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
public TextButton(string text, SpriteFont font, ISkin skin, TextButtonSkinDefinition skinDefinition) :
this(text,
font,
skin.GetTextureAtlasRegion(skinDefinition.downRegion),
skin.GetTextureAtlasRegion(skinDefinition.upRegion),
skin.GetTextureAtlasRegion(skinDefinition.downRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.upRegion, true),
skin.GetTextureAtlasRegion(skinDefinition.disabledRegion),
skin.GetTextureAtlasRegion(skinDefinition.selectedRegion))
{ }

View File

@ -39,8 +39,9 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// 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);
/// <param name="required">Whether or not the region is required. If true, it will throw an error if the region does not exist while if false, will return null if the region does not exist.</param>
/// <returns>The region corresponding to the name and may return null depending on if the region exists, and is required.</returns>
TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false);
/// <summary>
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.

View File

@ -62,7 +62,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
}
}
public TextureAtlas.Region GetTextureAtlasRegion(string name)
public TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false)
{
try
{
@ -70,7 +70,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem
}
catch (NullReferenceException)
{
return alternateSkin.GetTextureAtlasRegion(name);
return alternateSkin.GetTextureAtlasRegion(name, required);
}
}

View File

@ -47,12 +47,18 @@ namespace RecrownedAthenaeum.UI.SkinSystem
/// Returns a <see cref="TextureAtlas.Region"/> with given name of region. Null values acceptable. Will return null if parameter is null.
/// </summary>
/// <param name="name">Name of region.</param>
/// <param name="required">Whether or not this texture is mandatory for the module to work. If true, will throw error on failing to retrieve.</param>
/// <returns>The region corresponding to the name or null if the requested region doesn't exist.</returns>
public TextureAtlas.Region GetTextureAtlasRegion(string name)
public TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false)
{
if (!required && (name == null || !textureAtlas.ContainsRegion(name)))
{
return null;
} else
{
if (name == null || !textureAtlas.ContainsRegion(name)) return null;
return textureAtlas[name];
}
}
/// <summary>
/// Returns a <see cref="Color"/> with given name of defined color;
@ -115,7 +121,8 @@ namespace RecrownedAthenaeum.UI.SkinSystem
{
moduleTypeOfDefinition.Add(skinDefinition.GetType().FullName, skinDefinition.uiModuleTypeFullName);
definitions.Add(skinDefinition.uiModuleTypeFullName, new Dictionary<string, SkinDefinitionData>());
} else if (definitions[skinDefinition.uiModuleTypeFullName].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");
}
else if (definitions[skinDefinition.uiModuleTypeFullName].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!");
definitions[skinDefinition.uiModuleTypeFullName].Add(definitionName, skinDefinition);
}