From 88da47bc001e780308b15061bbfcc70fbfd368f1 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 10 Aug 2019 13:28:58 +0200 Subject: [PATCH] added defaults for most element properties --- MLEM.Ui/Elements/AutoScaledText.cs | 11 ++++------- MLEM.Ui/Elements/Button.cs | 14 +++++++++----- MLEM.Ui/Elements/Panel.cs | 6 ++++-- MLEM.Ui/Elements/Paragraph.cs | 13 +++++++------ MLEM.Ui/UiSystem.cs | 1 - Tests/GameImpl.cs | 3 +-- 6 files changed, 25 insertions(+), 23 deletions(-) diff --git a/MLEM.Ui/Elements/AutoScaledText.cs b/MLEM.Ui/Elements/AutoScaledText.cs index c46c97c..4da441f 100644 --- a/MLEM.Ui/Elements/AutoScaledText.cs +++ b/MLEM.Ui/Elements/AutoScaledText.cs @@ -8,11 +8,9 @@ namespace MLEM.Ui.Elements { public class AutoScaledText : Element { private readonly IGenericFont font; - private readonly bool centered; private float scale; private string text; - public IGenericFont Font => this.font ?? this.System.DefaultFont; public string Text { get => this.text; set { @@ -22,10 +20,9 @@ namespace MLEM.Ui.Elements { } 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.centered = centered; - this.font = font; + this.font = font ?? Paragraph.DefaultFont; this.Color = color ?? Color.White; } @@ -36,13 +33,13 @@ namespace MLEM.Ui.Elements { Vector2 measure; do { 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); } public override void Draw(GameTime time, SpriteBatch batch, Color color) { 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); } diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index 84185dd..369b6c4 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -6,18 +6,22 @@ using MLEM.Textures; namespace MLEM.Ui.Elements { public class Button : Element { + public static NinePatch DefaultTexture; + public static NinePatch DefaultHoveredTexture; + public static Color DefaultHoveredColor = Color.LightGray; + public NinePatch Texture; public NinePatch HoveredTexture; public Color HoveredColor; public AutoScaledText Text; - public Button(Anchor anchor, Vector2 size, NinePatch texture, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) { - this.Texture = texture; - this.HoveredTexture = hoveredTexture; - this.HoveredColor = hoveredColor ?? Color.LightGray; + public Button(Anchor anchor, Vector2 size, NinePatch texture = null, string text = null, Color? hoveredColor = null, NinePatch hoveredTexture = null) : base(anchor, size) { + this.Texture = texture ?? DefaultTexture; + this.HoveredTexture = hoveredTexture ?? DefaultHoveredTexture; + this.HoveredColor = hoveredColor ?? DefaultHoveredColor; if (text != null) { - this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text, true) { + this.Text = new AutoScaledText(Anchor.Center, Vector2.One, text) { IgnoresMouse = true }; this.AddChild(this.Text); diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 47677dd..503e0f9 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -5,10 +5,12 @@ using MLEM.Textures; namespace MLEM.Ui.Elements { public class Panel : Element { + public static NinePatch DefaultTexture; + private readonly NinePatch texture; - public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture) : base(anchor, size) { - this.texture = texture; + public Panel(Anchor anchor, Vector2 size, Point positionOffset, NinePatch texture = null) : base(anchor, size) { + this.texture = texture ?? DefaultTexture; this.PositionOffset = positionOffset; this.ChildPadding = new Point(5); } diff --git a/MLEM.Ui/Elements/Paragraph.cs b/MLEM.Ui/Elements/Paragraph.cs index b6af4fa..2f7899c 100644 --- a/MLEM.Ui/Elements/Paragraph.cs +++ b/MLEM.Ui/Elements/Paragraph.cs @@ -9,6 +9,8 @@ using MLEM.Font; namespace MLEM.Ui.Elements { public class Paragraph : Element { + public static IGenericFont DefaultFont; + private string text; private float lineHeight; 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)) { this.text = text; - this.font = font; + this.font = font ?? DefaultFont; this.TextScale = textScale; this.centerText = centerText; } protected override Point CalcActualSize(Rectangle 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; var height = 0F; 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; if (strgHeight > this.lineHeight) this.lineHeight = strgHeight; @@ -55,9 +56,9 @@ namespace MLEM.Ui.Elements { var offset = new Vector2(); foreach (var line in this.splitText) { 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 { - 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; } diff --git a/MLEM.Ui/UiSystem.cs b/MLEM.Ui/UiSystem.cs index 16ad734..338178b 100644 --- a/MLEM.Ui/UiSystem.cs +++ b/MLEM.Ui/UiSystem.cs @@ -24,7 +24,6 @@ namespace MLEM.Ui { root.Element.ForceUpdateArea(); } } - public IGenericFont DefaultFont; public Rectangle ScaledViewport { get { var bounds = this.graphicsDevice.Viewport.Bounds; diff --git a/Tests/GameImpl.cs b/Tests/GameImpl.cs index 71cc57a..18ba9aa 100644 --- a/Tests/GameImpl.cs +++ b/Tests/GameImpl.cs @@ -9,7 +9,6 @@ using MLEM.Startup; using MLEM.Textures; using MLEM.Ui; using MLEM.Ui.Elements; -using MonoGame.Extended.TextureAtlases; namespace Tests { public class GameImpl : MlemGame { @@ -26,7 +25,7 @@ namespace Tests { this.testTexture = LoadContent("Textures/Test"); this.testPatch = new NinePatch(new TextureRegion(this.testTexture, 0, 8, 24, 24), 8); - this.UiSystem.DefaultFont = new GenericSpriteFont(LoadContent("Fonts/TestFont")); + Paragraph.DefaultFont = new GenericSpriteFont(LoadContent("Fonts/TestFont")); this.UiSystem.GlobalScale = 5; var root = new Panel(Anchor.BottomLeft, new Vector2(100, 100), new Point(5, 5), this.testPatch);