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

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

View File

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

View File

@@ -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<TextButtonSkinDefinition>(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);
}
}

View File

@@ -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<TextSkinDefinition>(skinDefinitionName, GetType());
font = skin.fonts[skinDefinition.font];
color = skin.colors[skinDefinition.color];
}
public override void Update(GameTime gameTime)
{
position.X = bounds.X;