From ebe22fa9dcf1b152ee3c4ec512120a9b4fdc1ee7 Mon Sep 17 00:00:00 2001 From: Recrown Date: Thu, 7 Mar 2019 23:43:25 -0600 Subject: [PATCH] Added better exception throwing for missing regions for skins by adding a required option for regions. --- .../UI/Modular/Modules/Interactive/Button.cs | 8 ++++---- .../UI/Modular/Modules/Interactive/TextButton.cs | 4 ++-- RecrownedAthenaeum/UI/SkinSystem/ISkin.cs | 5 +++-- RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs | 4 ++-- RecrownedAthenaeum/UI/SkinSystem/Skin.cs | 15 +++++++++++---- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs index fce85e2..67fd775 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs @@ -58,8 +58,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive public Button(ISkin skin, string definitionName = null) { skinDefinition = skin.ObtainDefinition(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 /// The skin the definition is defined in. /// The definition itself. 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)) {} diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs index be0d978..67b8579 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs @@ -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)) { } diff --git a/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs b/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs index d6b1e19..8530eb5 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs @@ -39,8 +39,9 @@ namespace RecrownedAthenaeum.UI.SkinSystem /// Returns a with given name of region. /// /// Name of region. - /// The region corresponding to the name. - TextureAtlas.Region GetTextureAtlasRegion(string name); + /// 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. + /// The region corresponding to the name and may return null depending on if the region exists, and is required. + TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false); /// /// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist. diff --git a/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs b/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs index 99cb2bc..386a59e 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs @@ -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); } } diff --git a/RecrownedAthenaeum/UI/SkinSystem/Skin.cs b/RecrownedAthenaeum/UI/SkinSystem/Skin.cs index 5741d5b..3af4677 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/Skin.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/Skin.cs @@ -47,11 +47,17 @@ namespace RecrownedAthenaeum.UI.SkinSystem /// 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) + public TextureAtlas.Region GetTextureAtlasRegion(string name, bool required = false) { - if (name == null || !textureAtlas.ContainsRegion(name)) return null; - return textureAtlas[name]; + if (!required && (name == null || !textureAtlas.ContainsRegion(name))) + { + return null; + } else + { + return textureAtlas[name]; + } } /// @@ -115,7 +121,8 @@ namespace RecrownedAthenaeum.UI.SkinSystem { moduleTypeOfDefinition.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!"); + } + else if (definitions[skinDefinition.uiModuleTypeFullName].ContainsKey(definitionName)) throw new ArgumentException("Type of definition with that name already exists!"); definitions[skinDefinition.uiModuleTypeFullName].Add(definitionName, skinDefinition); }