From 8ea9ae39215f4f35ad04401693a7da2ca8caf1e6 Mon Sep 17 00:00:00 2001 From: Recrown Date: Mon, 28 Jan 2019 19:43:41 -0600 Subject: [PATCH] attempt at fixing skin system as well as improved convenience. --- .../UI/Modular/Modules/Interactive/Button.cs | 2 +- .../Modular/Modules/Interactive/TextButton.cs | 2 +- RecrownedAthenaeum/UI/Modular/Modules/Text.cs | 2 +- RecrownedAthenaeum/UI/SkinSystem/ISkin.cs | 28 +-------- .../UI/SkinSystem/MergedSkin.cs | 57 +---------------- RecrownedAthenaeum/UI/SkinSystem/Skin.cs | 62 +++++-------------- .../UI/SkinSystem/SkinManager.cs | 2 +- 7 files changed, 26 insertions(+), 129 deletions(-) diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs index 284421f..2260141 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs @@ -57,7 +57,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive /// The name of the definition in the skin. Can be null to select the default. public Button(ISkin skin, string definitionName = null) { - skinDefinition = skin.ObtainDefinition(definitionName, GetType()); + skinDefinition = skin.ObtainDefinition(definitionName); downTexture = skin.GetTextureAtlasRegion(skinDefinition.downRegion); upTexture = skin.GetTextureAtlasRegion(skinDefinition.upRegion); disabledTexture = skin.GetTextureAtlasRegion(skinDefinition.disabledRegion); diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs index b7d2e04..627cf1c 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs @@ -45,7 +45,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive /// Name of the definition for this type in the skin given. public TextButton(string text, SpriteFont font, ISkin skin, string definitionName = null) : base(skin, definitionName) { - TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition(definitionName, GetType()); + TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition(definitionName); this.text = new Text(font, text); FontColor = skin.GetColor(skinDefinition.fontColor); } diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs index 3a20cdc..02ed475 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs @@ -64,7 +64,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules /// The string of text to be displayed. public Text(ISkin skin, SpriteFont font, string skinDefinitionName = null, string content = null) : this(font, content) { - skinDefinition = skin.ObtainDefinition(skinDefinitionName, GetType()); + skinDefinition = skin.ObtainDefinition(skinDefinitionName); color = skin.GetColor(skinDefinition.color); } diff --git a/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs b/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs index 1fcef87..6ca8d56 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/ISkin.cs @@ -41,36 +41,12 @@ namespace RecrownedAthenaeum.UI.SkinSystem /// The region corresponding to the name. TextureAtlas.Region GetTextureAtlasRegion(string name); - /// - /// Returns an of the given name and type. - /// - /// Name of definition of the - /// The UIModule the definition defines. - /// The interface for the definition. - ISkinDefinitionData ObtainDefinition(string definitionName, Type type); - - /// - /// Returns the default of the given parameters. - /// - /// The type of definition the default should be coming from. - /// The default definition for the given type. - ISkinDefinitionData ObtainDefinition(Type type); - /// /// 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. - /// UIModule type the definition defines. + /// The name of the definition. Default is null and will be replaced with "default" for name. /// The definition cast to T. - T ObtainDefinition(string definitionName, Type type) where T : ISkinDefinitionData; - - /// - /// Returns the default definition. - /// - /// Convenience to cast to T. - /// The type of the UIModule to retrieve the default from. - /// The default definition for the given type. - T ObtainDefinition(Type type) where T : ISkinDefinitionData; + T ObtainDefinition(string definitionName = null) where T : ISkinDefinitionData; } } \ No newline at end of file diff --git a/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs b/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs index cc7f294..a57731d 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/MergedSkin.cs @@ -77,66 +77,15 @@ namespace RecrownedAthenaeum.UI.SkinSystem } } - public ISkinDefinitionData ObtainDefinition(string definitionName, Type type) + public T ObtainDefinition(string definitionName = null) where T : ISkinDefinitionData { try { - return mainSkin.ObtainDefinition(definitionName, type); - } catch (KeyNotFoundException) - { - return alternateSkin.ObtainDefinition(definitionName, type); + return mainSkin.ObtainDefinition(definitionName); } 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(string definitionName, Type type) where T : ISkinDefinitionData - { - try - { - return mainSkin.ObtainDefinition(definitionName, type); - } - catch (KeyNotFoundException) - { - return alternateSkin.ObtainDefinition(definitionName, type); - } - catch (NullReferenceException) - { - return alternateSkin.ObtainDefinition(definitionName, type); - } - } - - public T ObtainDefinition(Type type) where T : ISkinDefinitionData - { - try - { - return mainSkin.ObtainDefinition(type); - } - catch (KeyNotFoundException) - { - return alternateSkin.ObtainDefinition(type); - } - catch (NullReferenceException) - { - return alternateSkin.ObtainDefinition(type); + return alternateSkin.ObtainDefinition(definitionName); } } } diff --git a/RecrownedAthenaeum/UI/SkinSystem/Skin.cs b/RecrownedAthenaeum/UI/SkinSystem/Skin.cs index df52a4f..10cb13b 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/Skin.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/Skin.cs @@ -21,8 +21,8 @@ namespace RecrownedAthenaeum.UI.SkinSystem private TextureAtlas textureAtlas; Dictionary colors; - - Dictionary> definitions; + readonly Dictionary moduleTypeOfDefinition; + readonly Dictionary> definitions; /// /// The texture for the cursor. @@ -39,6 +39,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem this.textureAtlas = textureAtlas; this.CursorTexture = cursorTexture; colors = new Dictionary(); + moduleTypeOfDefinition = new Dictionary(); definitions = new Dictionary>(); } @@ -77,73 +78,44 @@ namespace RecrownedAthenaeum.UI.SkinSystem textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin); } - /// - /// Returns an of the given name and type. - /// - /// Name of definition of the - /// The UIModule the definition defines. - /// The interface for the definition. - public virtual ISkinDefinitionData ObtainDefinition(string definitionName, Type type) + 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) || !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]; } - /// - /// Returns the default of the given parameters. - /// - /// The type of definition the default should be coming from. - /// The default definition for the given type. - public virtual ISkinDefinitionData ObtainDefinition(Type type) - { - if (disposed) throw new ObjectDisposedException(GetType().Name); - return ObtainDefinition(null, type); - } - /// /// 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. - /// UIModule type the definition defines. /// The definition cast to T. - public virtual T ObtainDefinition(string definitionName, Type type) where T : ISkinDefinitionData + public virtual T ObtainDefinition(string definitionName = null) where T : ISkinDefinitionData { - if (disposed) throw new ObjectDisposedException(GetType().Name); - if (definitionName == null) definitionName = "default"; - return (T)ObtainDefinition(definitionName, type); - } - - /// - /// Returns the default definition. - /// - /// Convenience to cast to T. - /// The type of the UIModule to retrieve the default from. - /// The default definition for the given type. - public virtual T ObtainDefinition(Type type) where T : ISkinDefinitionData - { - if (disposed) throw new ObjectDisposedException(GetType().Name); - return ObtainDefinition(null, type); + return (T)ObtainDefinition(moduleTypeOfDefinition[typeof(T).FullName], definitionName); } /// /// Adds the definition. /// - /// The name of the definition. + /// The name of the definition. Default (if left blank) name is "default". /// The definition itself. - 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 (Laminated) throw new InvalidOperationException("This object has been laminated and cannot be edited."); - if (!definitions.ContainsKey(skinDefinition.UIModuleType)) + 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[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); } /// @@ -153,7 +125,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem /// 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); } diff --git a/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs b/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs index a3e059f..2047c48 100644 --- a/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs +++ b/RecrownedAthenaeum/UI/SkinSystem/SkinManager.cs @@ -192,7 +192,7 @@ namespace RecrownedAthenaeum.UI.SkinSystem for (int i = 0; i < skinData.definitions.Length; i++) { SkinData.DefinitionData definitionData = skinData.definitions[i]; - skin.AddDefinition(definitionData.name, definitionData.skin); + skin.AddDefinition(definitionData.skin, definitionData.name); } return skin;