1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-26 06:28:35 +01:00

added defaults for most element properties

This commit is contained in:
Ellpeck 2019-08-10 13:28:58 +02:00
parent 9d5d00434c
commit 88da47bc00
6 changed files with 25 additions and 23 deletions

View file

@ -8,11 +8,9 @@ namespace MLEM.Ui.Elements {
public class AutoScaledText : Element { public class AutoScaledText : Element {
private readonly IGenericFont font; private readonly IGenericFont font;
private readonly bool centered;
private float scale; private float scale;
private string text; private string text;
public IGenericFont Font => this.font ?? this.System.DefaultFont;
public string Text { public string Text {
get => this.text; get => this.text;
set { set {
@ -22,10 +20,9 @@ namespace MLEM.Ui.Elements {
} }
public Color Color; public Color Color;
public AutoScaledText(Anchor anchor, Vector2 size, string text, bool centered = false, Color? color = null, IGenericFont font = null) : base(anchor, size) { public AutoScaledText(Anchor anchor, Vector2 size, string text, Color? color = null, IGenericFont font = null) : base(anchor, size) {
this.Text = text; this.Text = text;
this.centered = centered; this.font = font ?? Paragraph.DefaultFont;
this.font = font;
this.Color = color ?? Color.White; this.Color = color ?? Color.White;
} }
@ -36,13 +33,13 @@ namespace MLEM.Ui.Elements {
Vector2 measure; Vector2 measure;
do { do {
this.scale += 0.1F; this.scale += 0.1F;
measure = this.Font.MeasureString(this.Text) * this.scale; measure = this.font.MeasureString(this.Text) * this.scale;
} while (measure.X <= this.DisplayArea.Size.X && measure.Y <= this.DisplayArea.Size.Y); } while (measure.X <= this.DisplayArea.Size.X && measure.Y <= this.DisplayArea.Size.Y);
} }
public override void Draw(GameTime time, SpriteBatch batch, Color color) { public override void Draw(GameTime time, SpriteBatch batch, Color color) {
var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2; var pos = this.DisplayArea.Location.ToVector2() + this.DisplayArea.Size.ToVector2() / 2;
this.Font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color.CopyAlpha(color), true, true); this.font.DrawCenteredString(batch, this.Text, pos, this.scale, this.Color.CopyAlpha(color), true, true);
base.Draw(time, batch, color); base.Draw(time, batch, color);
} }

View file

@ -6,18 +6,22 @@ using MLEM.Textures;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
public class Button : Element { public class Button : Element {
public static NinePatch DefaultTexture;
public static NinePatch DefaultHoveredTexture;
public static Color DefaultHoveredColor = Color.LightGray;
public NinePatch Texture; public NinePatch Texture;
public NinePatch HoveredTexture; public NinePatch HoveredTexture;
public Color HoveredColor; public Color HoveredColor;
public AutoScaledText Text; public AutoScaledText Text;
public Button(Anchor anchor, Vector2 size, NinePatch texture, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) { public Button(Anchor anchor, Vector2 size, NinePatch texture = null, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) {
this.Texture = texture; this.Texture = texture ?? DefaultTexture;
this.HoveredTexture = hoveredTexture; this.HoveredTexture = hoveredTexture ?? DefaultHoveredTexture;
this.HoveredColor = hoveredColor ?? Color.LightGray; this.HoveredColor = hoveredColor ?? DefaultHoveredColor;
if (text != null) { if (text != null) {
this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text, true) { this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text) {
IgnoresMouse = true IgnoresMouse = true
}; };
this.AddChild(this.Text); this.AddChild(this.Text);

View file

@ -5,10 +5,12 @@ using MLEM.Textures;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
public class Panel : Element { public class Panel : Element {
public static NinePatch DefaultTexture;
private readonly NinePatch texture; private readonly NinePatch texture;
public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture) : base(anchor, size) { public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture = null) : base(anchor, size) {
this.texture = texture; this.texture = texture ?? DefaultTexture;
this.PositionOffset = positionOffset; this.PositionOffset = positionOffset;
this.ChildPadding = new Point(5); this.ChildPadding = new Point(5);
} }

View file

@ -9,6 +9,8 @@ using MLEM.Font;
namespace MLEM.Ui.Elements { namespace MLEM.Ui.Elements {
public class Paragraph : Element { public class Paragraph : Element {
public static IGenericFont DefaultFont;
private string text; private string text;
private float lineHeight; private float lineHeight;
private string[] splitText; private string[] splitText;
@ -24,23 +26,22 @@ namespace MLEM.Ui.Elements {
} }
} }
public IGenericFont Font => this.font ?? this.System.DefaultFont;
public Paragraph(Anchor anchor, float width, string text, float textScale = 1, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) { public Paragraph(Anchor anchor, float width, string text, float textScale = 1, bool centerText = false, IGenericFont font = null) : base(anchor, new Vector2(width, 0)) {
this.text = text; this.text = text;
this.font = font; this.font = font ?? DefaultFont;
this.TextScale = textScale; this.TextScale = textScale;
this.centerText = centerText; this.centerText = centerText;
} }
protected override Point CalcActualSize(Rectangle parentArea) { protected override Point CalcActualSize(Rectangle parentArea) {
var size = base.CalcActualSize(parentArea); var size = base.CalcActualSize(parentArea);
this.splitText = this.Font.SplitString(this.text, size.X, this.TextScale).ToArray(); this.splitText = this.font.SplitString(this.text, size.X, this.TextScale).ToArray();
this.lineHeight = 0; this.lineHeight = 0;
var height = 0F; var height = 0F;
foreach (var strg in this.splitText) { foreach (var strg in this.splitText) {
var strgHeight = this.Font.MeasureString(strg).Y * this.TextScale; var strgHeight = this.font.MeasureString(strg).Y * this.TextScale;
height += strgHeight + 1; height += strgHeight + 1;
if (strgHeight > this.lineHeight) if (strgHeight > this.lineHeight)
this.lineHeight = strgHeight; this.lineHeight = strgHeight;
@ -55,9 +56,9 @@ namespace MLEM.Ui.Elements {
var offset = new Vector2(); var offset = new Vector2();
foreach (var line in this.splitText) { foreach (var line in this.splitText) {
if (this.centerText) { if (this.centerText) {
this.Font.DrawCenteredString(batch, line, pos + offset + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale, color); this.font.DrawCenteredString(batch, line, pos + offset + new Vector2(this.DisplayArea.Width / 2, 0), this.TextScale, color);
} else { } else {
this.Font.DrawString(batch, line, pos + offset, color, 0, Vector2.Zero, this.TextScale, SpriteEffects.None, 0); this.font.DrawString(batch, line, pos + offset, color, 0, Vector2.Zero, this.TextScale, SpriteEffects.None, 0);
} }
offset.Y += this.lineHeight + 1; offset.Y += this.lineHeight + 1;
} }

View file

@ -24,7 +24,6 @@ namespace MLEM.Ui {
root.Element.ForceUpdateArea(); root.Element.ForceUpdateArea();
} }
} }
public IGenericFont DefaultFont;
public Rectangle ScaledViewport { public Rectangle ScaledViewport {
get { get {
var bounds = this.graphicsDevice.Viewport.Bounds; var bounds = this.graphicsDevice.Viewport.Bounds;

View file

@ -9,7 +9,6 @@ using MLEM.Startup;
using MLEM.Textures; using MLEM.Textures;
using MLEM.Ui; using MLEM.Ui;
using MLEM.Ui.Elements; using MLEM.Ui.Elements;
using MonoGame.Extended.TextureAtlases;
namespace Tests { namespace Tests {
public class GameImpl : MlemGame { public class GameImpl : MlemGame {
@ -26,7 +25,7 @@ namespace Tests {
this.testTexture = LoadContent<Texture2D>("Textures/Test"); this.testTexture = LoadContent<Texture2D>("Textures/Test");
this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8); this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8);
this.UiSystem.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont")); Paragraph.DefaultFont = new GenericSpriteFont(LoadContent<SpriteFont>("Fonts/TestFont"));
this.UiSystem.GlobalScale = 5; this.UiSystem.GlobalScale = 5;
var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch); var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);