diff --git a/RecrownedAthenaeum/RecrownedAthenaeum.csproj b/RecrownedAthenaeum/RecrownedAthenaeum.csproj index ca82cdc..5f450f6 100644 --- a/RecrownedAthenaeum/RecrownedAthenaeum.csproj +++ b/RecrownedAthenaeum/RecrownedAthenaeum.csproj @@ -54,6 +54,7 @@ + @@ -78,6 +79,7 @@ + diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs index ab7cd53..a9c3122 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/Button.cs @@ -8,6 +8,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using RecrownedAthenaeum.UI.Skin.Definitions; namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive { @@ -15,17 +16,47 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive public class Button : UIModule { - private NinePatch background; + private ButtonSkinDefinition skinDefinition; + private ISpecialDrawable downTexture, upTexture, highlightedTexture, disabledTexture; public event Clicked Listeners; + private bool pressed; + public bool disabled = false; + public bool Highlighted { get; private set; } - public Button(NinePatch background = null) + public Button(ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null) { - this.background = background; + this.downTexture = down; + this.upTexture = up; + this.disabledTexture = disabled; + this.highlightedTexture = selected; + } + + public Button(Skin.Skin skin, string definitionName = null) + { + this.skinDefinition = skin.ObtainDefinition(definitionName, GetType()); + downTexture = skin.textureAtlas[skinDefinition.downRegion]; + upTexture = skin.textureAtlas[skinDefinition.upRegion]; + disabledTexture = skin.textureAtlas[skinDefinition.disabledRegion]; + highlightedTexture = skin.textureAtlas[skinDefinition.selectedRegion]; } public override void Draw(SpriteBatch batch) { - background?.Draw(batch, bounds); + if (disabled) + { + disabledTexture?.Draw(batch, bounds, color); + } + else + { + if (pressed) + { + downTexture.Draw(batch, bounds, color); + } + else + { + upTexture.Draw(batch, bounds, color); + } + } base.Draw(batch); } @@ -33,6 +64,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive { if (InputUtilities.MouseWithinBoundries(bounds)) { + if (state.LeftButton == ButtonState.Pressed) + { + pressed = true; + } + else + { + pressed = false; + } if (InputUtilities.MouseClicked()) { OnClick(); @@ -42,6 +81,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive else { Highlighted = false; + pressed = false; } return base.MouseStateChanged(state); @@ -57,6 +97,5 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive Listeners?.Invoke(); } - public bool Highlighted { get; private set; } } } diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs index 5496ed3..a1fea17 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Interactive/TextButton.cs @@ -8,29 +8,40 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; +using RecrownedAthenaeum.UI.Skin.Definitions; namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive { public class TextButton : Button { - private Text label; + private Text text; + public Color FontColor { get { return text.color; } set { text.color = value; } } - public TextButton(string text, SpriteFont font, NinePatch background) : base(background) + public TextButton(string text, SpriteFont font, ISpecialDrawable down, ISpecialDrawable up, ISpecialDrawable disabled = null, ISpecialDrawable selected = null) : base(down, up, disabled, selected) { - label = new Text(font, text); - label.autoScale = true; + this.text = new Text(font, text) + { + autoScale = true + }; + } + + public TextButton(string text, Skin.Skin skin, string definitionName = null) : base(skin, definitionName) + { + TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition(definitionName, GetType()); + this.text = new Text(skin.fonts[skinDefinition.fontName], text); + FontColor = skin.colors[skinDefinition.fontColor]; } public override void Update(GameTime gameTime) { - label.bounds = bounds; - label.Update(gameTime); + text.bounds = bounds; + text.Update(gameTime); base.Update(gameTime); } public override void Draw(SpriteBatch batch) { - label.Draw(batch); + text.Draw(batch); base.Draw(batch); } } diff --git a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs index ae659fb..140d658 100644 --- a/RecrownedAthenaeum/UI/Modular/Modules/Text.cs +++ b/RecrownedAthenaeum/UI/Modular/Modules/Text.cs @@ -1,6 +1,8 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using RecrownedAthenaeum.Camera; +using RecrownedAthenaeum.UI.Skin; +using RecrownedAthenaeum.UI.Skin.Definitions; using System; using System.Collections.Generic; using System.Linq; @@ -11,6 +13,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules { public class Text : UIModule { + private TextSkinDefinition skinDefinition; private SpriteFont font; private float scale; private Vector2 position; @@ -22,7 +25,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules public bool useEllipses; public string ellipsis = "..."; private string ModifiedText { get { return displayedText; } set { displayedText = value; modifiedTextSize = font.MeasureString(value); } } - public string Content { get { return originalText; } set { if (value == null) value = "..."; modifiedTextSize = font.MeasureString(value); originalText = value; displayedText = value; } } + public string Content { get { return originalText; } set { if (value == null) value = ellipsis; modifiedTextSize = font.MeasureString(value); originalText = value; displayedText = value; } } + public Text(SpriteFont font, string content = null) { @@ -30,6 +34,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules this.font = font; } + public Text(Skin.Skin skin, string skinDefinitionName = null, string content = null) + { + Content = content; + skinDefinition = skin.ObtainDefinition(skinDefinitionName, GetType()); + font = skin.fonts[skinDefinition.font]; + color = skin.colors[skinDefinition.color]; + } + public override void Update(GameTime gameTime) { position.X = bounds.X; diff --git a/RecrownedAthenaeum/UI/Skin/Definitions/ButtonSkinDefinition.cs b/RecrownedAthenaeum/UI/Skin/Definitions/ButtonSkinDefinition.cs index 97865f8..7347a97 100644 --- a/RecrownedAthenaeum/UI/Skin/Definitions/ButtonSkinDefinition.cs +++ b/RecrownedAthenaeum/UI/Skin/Definitions/ButtonSkinDefinition.cs @@ -9,9 +9,7 @@ namespace RecrownedAthenaeum.UI.Skin.Definitions { public class ButtonSkinDefinition : ISkinDefinition { - public string downRegion; - public string upRegion; - public string disabledRegion; + public string upRegion, downRegion, disabledRegion, selectedRegion; public Type UIModuleType { get { return typeof(Button); } } public ButtonSkinDefinition(string downRegion, string upRegion) diff --git a/RecrownedAthenaeum/UI/Skin/Definitions/TextButtonSkinDefinition.cs b/RecrownedAthenaeum/UI/Skin/Definitions/TextButtonSkinDefinition.cs new file mode 100644 index 0000000..5ff7d93 --- /dev/null +++ b/RecrownedAthenaeum/UI/Skin/Definitions/TextButtonSkinDefinition.cs @@ -0,0 +1,22 @@ +using RecrownedAthenaeum.UI.Modular.Modules.Interactive; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RecrownedAthenaeum.UI.Skin.Definitions +{ + public class TextButtonSkinDefinition : ButtonSkinDefinition + { + public string fontName; + public string fontColor; + + public new Type UIModuleType => typeof(TextButton); + + public TextButtonSkinDefinition(string downRegion, string upRegion) : base(downRegion, upRegion) + { + + } + } +} diff --git a/RecrownedAthenaeum/UI/Skin/Skin.cs b/RecrownedAthenaeum/UI/Skin/Skin.cs index c8e136b..fdfde56 100644 --- a/RecrownedAthenaeum/UI/Skin/Skin.cs +++ b/RecrownedAthenaeum/UI/Skin/Skin.cs @@ -12,9 +12,9 @@ namespace RecrownedAthenaeum.UI.Skin { public class Skin { - TextureAtlas textureAtlas; - Dictionary colors; - Dictionary fonts; + public readonly TextureAtlas textureAtlas; + public readonly Dictionary colors; + public readonly Dictionary fonts; Dictionary> definitions; public Skin(TextureAtlas textureAtlas) @@ -30,14 +30,51 @@ namespace RecrownedAthenaeum.UI.Skin 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 ISkinDefinition ObtainDefinition(string definitionName, Type type) { + 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); return definitions[type][definitionName]; } - public T ObtainDefinition(string definitionName) where T : ISkinDefinition + /// + /// 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 ISkinDefinition ObtainDefinition(Type type) { - return (T)definitions[typeof(T)][definitionName]; + 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 T ObtainDefinition(string definitionName, Type type) where T : ISkinDefinition + { + 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 T ObtainDefinition(Type type) where T : ISkinDefinition + { + return ObtainDefinition(null, type); } public void AddDefinition(string definitionName, ISkinDefinition skinDefinition)