implemented skin system to ui modules; progress on functional text button.

This commit is contained in:
Harrison Deng 2018-12-14 00:43:38 -06:00
parent 4fd37b6675
commit 8ab83b5188
7 changed files with 142 additions and 21 deletions

View File

@ -54,6 +54,7 @@
<Compile Include="ContentSystem\ContentManagerController.cs" /> <Compile Include="ContentSystem\ContentManagerController.cs" />
<Compile Include="ContentSystem\IContentPathModifier.cs" /> <Compile Include="ContentSystem\IContentPathModifier.cs" />
<Compile Include="ContentSystem\NormalContentResolver.cs" /> <Compile Include="ContentSystem\NormalContentResolver.cs" />
<Compile Include="DataTypes\ISpecialDrawable.cs" />
<Compile Include="DataTypes\NinePatch.cs" /> <Compile Include="DataTypes\NinePatch.cs" />
<Compile Include="DataTypes\Resolution.cs" /> <Compile Include="DataTypes\Resolution.cs" />
<Compile Include="DataTypes\TextureAtlas.cs" /> <Compile Include="DataTypes\TextureAtlas.cs" />
@ -78,6 +79,7 @@
<Compile Include="UI\Modular\UIModuleGroup.cs" /> <Compile Include="UI\Modular\UIModuleGroup.cs" />
<Compile Include="UI\Skin\Definitions\ButtonSkinDefinition.cs" /> <Compile Include="UI\Skin\Definitions\ButtonSkinDefinition.cs" />
<Compile Include="UI\Skin\Definitions\ISkinDefinition.cs" /> <Compile Include="UI\Skin\Definitions\ISkinDefinition.cs" />
<Compile Include="UI\Skin\Definitions\TextButtonSkinDefinition.cs" />
<Compile Include="UI\Skin\Definitions\TextSkinDefinition.cs" /> <Compile Include="UI\Skin\Definitions\TextSkinDefinition.cs" />
<Compile Include="UI\Skin\Skin.cs" /> <Compile Include="UI\Skin\Skin.cs" />
</ItemGroup> </ItemGroup>

View File

@ -8,6 +8,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using RecrownedAthenaeum.UI.Skin.Definitions;
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{ {
@ -15,17 +16,47 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
public class Button : UIModule public class Button : UIModule
{ {
private NinePatch background; private ButtonSkinDefinition skinDefinition;
private ISpecialDrawable downTexture, upTexture, highlightedTexture, disabledTexture;
public event Clicked Listeners; 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<ButtonSkinDefinition>(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) 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); base.Draw(batch);
} }
@ -33,6 +64,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{ {
if (InputUtilities.MouseWithinBoundries(bounds)) if (InputUtilities.MouseWithinBoundries(bounds))
{ {
if (state.LeftButton == ButtonState.Pressed)
{
pressed = true;
}
else
{
pressed = false;
}
if (InputUtilities.MouseClicked()) if (InputUtilities.MouseClicked())
{ {
OnClick(); OnClick();
@ -42,6 +81,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
else else
{ {
Highlighted = false; Highlighted = false;
pressed = false;
} }
return base.MouseStateChanged(state); return base.MouseStateChanged(state);
@ -57,6 +97,5 @@ namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
Listeners?.Invoke(); Listeners?.Invoke();
} }
public bool Highlighted { get; private set; }
} }
} }

View File

@ -8,29 +8,40 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using RecrownedAthenaeum.UI.Skin.Definitions;
namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive namespace RecrownedAthenaeum.UI.Modular.Modules.Interactive
{ {
public class TextButton : Button 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); this.text = new Text(font, text)
label.autoScale = true; {
autoScale = true
};
}
public TextButton(string text, Skin.Skin skin, string definitionName = null) : base(skin, definitionName)
{
TextButtonSkinDefinition skinDefinition = skin.ObtainDefinition<TextButtonSkinDefinition>(definitionName, GetType());
this.text = new Text(skin.fonts[skinDefinition.fontName], text);
FontColor = skin.colors[skinDefinition.fontColor];
} }
public override void Update(GameTime gameTime) public override void Update(GameTime gameTime)
{ {
label.bounds = bounds; text.bounds = bounds;
label.Update(gameTime); text.Update(gameTime);
base.Update(gameTime); base.Update(gameTime);
} }
public override void Draw(SpriteBatch batch) public override void Draw(SpriteBatch batch)
{ {
label.Draw(batch); text.Draw(batch);
base.Draw(batch); base.Draw(batch);
} }
} }

View File

@ -1,6 +1,8 @@
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using RecrownedAthenaeum.Camera; using RecrownedAthenaeum.Camera;
using RecrownedAthenaeum.UI.Skin;
using RecrownedAthenaeum.UI.Skin.Definitions;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -11,6 +13,7 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
{ {
public class Text : UIModule public class Text : UIModule
{ {
private TextSkinDefinition skinDefinition;
private SpriteFont font; private SpriteFont font;
private float scale; private float scale;
private Vector2 position; private Vector2 position;
@ -22,7 +25,8 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
public bool useEllipses; public bool useEllipses;
public string ellipsis = "..."; public string ellipsis = "...";
private string ModifiedText { get { return displayedText; } set { displayedText = value; modifiedTextSize = font.MeasureString(value); } } 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) public Text(SpriteFont font, string content = null)
{ {
@ -30,6 +34,14 @@ namespace RecrownedAthenaeum.UI.Modular.Modules
this.font = font; this.font = font;
} }
public Text(Skin.Skin skin, string skinDefinitionName = null, string content = null)
{
Content = content;
skinDefinition = skin.ObtainDefinition<TextSkinDefinition>(skinDefinitionName, GetType());
font = skin.fonts[skinDefinition.font];
color = skin.colors[skinDefinition.color];
}
public override void Update(GameTime gameTime) public override void Update(GameTime gameTime)
{ {
position.X = bounds.X; position.X = bounds.X;

View File

@ -9,9 +9,7 @@ namespace RecrownedAthenaeum.UI.Skin.Definitions
{ {
public class ButtonSkinDefinition : ISkinDefinition public class ButtonSkinDefinition : ISkinDefinition
{ {
public string downRegion; public string upRegion, downRegion, disabledRegion, selectedRegion;
public string upRegion;
public string disabledRegion;
public Type UIModuleType { get { return typeof(Button); } } public Type UIModuleType { get { return typeof(Button); } }
public ButtonSkinDefinition(string downRegion, string upRegion) public ButtonSkinDefinition(string downRegion, string upRegion)

View File

@ -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)
{
}
}
}

View File

@ -12,9 +12,9 @@ namespace RecrownedAthenaeum.UI.Skin
{ {
public class Skin public class Skin
{ {
TextureAtlas textureAtlas; public readonly TextureAtlas textureAtlas;
Dictionary<string, Color> colors; public readonly Dictionary<string, Color> colors;
Dictionary<string, SpriteFont> fonts; public readonly Dictionary<string, SpriteFont> fonts;
Dictionary<Type, Dictionary<string, ISkinDefinition>> definitions; Dictionary<Type, Dictionary<string, ISkinDefinition>> definitions;
public Skin(TextureAtlas textureAtlas) public Skin(TextureAtlas textureAtlas)
@ -30,14 +30,51 @@ namespace RecrownedAthenaeum.UI.Skin
textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin); textureAtlas.Draw(regionName, batch, destination, colors[color], rotation, origin);
} }
/// <summary>
/// Returns an <see cref="ISkinDefinition"/> 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 ISkinDefinition ObtainDefinition(string definitionName, Type type) 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]; return definitions[type][definitionName];
} }
public T ObtainDefinition<T>(string definitionName) where T : ISkinDefinition /// <summary>
/// Returns the default <see cref="ISkinDefinition"/> 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 ISkinDefinition ObtainDefinition(Type type)
{ {
return (T)definitions[typeof(T)][definitionName]; return ObtainDefinition(null, type);
}
/// <summary>
/// Returns the proper definition for the given parameters or throws exception in the case the requested definition does not exist.
/// </summary>
/// <typeparam name="T">Convenience to cast to the needed definition type.</typeparam>
/// <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>
public T ObtainDefinition<T>(string definitionName, Type type) where T : ISkinDefinition
{
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 T ObtainDefinition<T>(Type type) where T : ISkinDefinition
{
return ObtainDefinition<T>(null, type);
} }
public void AddDefinition(string definitionName, ISkinDefinition skinDefinition) public void AddDefinition(string definitionName, ISkinDefinition skinDefinition)