From 8af040787c6fc777e81fa7129c84ed126d759a43 Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sun, 11 Aug 2019 21:38:03 +0200 Subject: [PATCH] added the ability for nine patch regions to have their patches scaled --- MLEM.Ui/Elements/Button.cs | 2 +- MLEM.Ui/Elements/Panel.cs | 2 +- MLEM.Ui/Elements/TextField.cs | 2 +- MLEM/Textures/NinePatch.cs | 44 ++++++++++++++++++++--------------- Tests/GameImpl.cs | 2 +- 5 files changed, 29 insertions(+), 23 deletions(-) diff --git a/MLEM.Ui/Elements/Button.cs b/MLEM.Ui/Elements/Button.cs index c8901fd..2e0dd55 100644 --- a/MLEM.Ui/Elements/Button.cs +++ b/MLEM.Ui/Elements/Button.cs @@ -29,7 +29,7 @@ namespace MLEM.Ui.Elements { tex = this.HoveredTexture; color = this.HoveredColor * alpha; } - batch.Draw(tex, this.DisplayArea, color); + batch.Draw(tex, this.DisplayArea, color, this.Scale); base.Draw(time, batch, alpha); } diff --git a/MLEM.Ui/Elements/Panel.cs b/MLEM.Ui/Elements/Panel.cs index 04e010f..fbc7e98 100644 --- a/MLEM.Ui/Elements/Panel.cs +++ b/MLEM.Ui/Elements/Panel.cs @@ -16,7 +16,7 @@ namespace MLEM.Ui.Elements { } public override void Draw(GameTime time, SpriteBatch batch, float alpha) { - batch.Draw(this.Texture, this.DisplayArea, Color.White * alpha); + batch.Draw(this.Texture, this.DisplayArea, Color.White * alpha, this.Scale); base.Draw(time, batch, alpha); } diff --git a/MLEM.Ui/Elements/TextField.cs b/MLEM.Ui/Elements/TextField.cs index eee4f06..8ade483 100644 --- a/MLEM.Ui/Elements/TextField.cs +++ b/MLEM.Ui/Elements/TextField.cs @@ -75,7 +75,7 @@ namespace MLEM.Ui.Elements { tex = this.HoveredTexture; color = this.HoveredColor * alpha; } - batch.Draw(tex, this.DisplayArea, color); + batch.Draw(tex, this.DisplayArea, color, this.Scale); var caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : ""; var text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret; this.font.DrawCenteredString(batch, text, this.DisplayArea.Location.ToVector2() + new Vector2(this.TextOffsetX, this.DisplayArea.Height / 2), this.TextScale * this.Scale, Color.White * alpha, false, true); diff --git a/MLEM/Textures/NinePatch.cs b/MLEM/Textures/NinePatch.cs index 782cbe8..584a380 100644 --- a/MLEM/Textures/NinePatch.cs +++ b/MLEM/Textures/NinePatch.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using MLEM.Extensions; namespace MLEM.Textures { public class NinePatch { @@ -33,31 +34,36 @@ namespace MLEM.Textures { public NinePatch(TextureRegion texture, int padding) : this(texture, padding, padding, padding, padding) { } - public IEnumerable CreateRectangles(Rectangle area) { - var centerW = area.Width - this.PaddingLeft - this.PaddingRight; - var centerH = area.Height - this.PaddingTop - this.PaddingBottom; - var leftX = area.X + this.PaddingLeft; - var rightX = area.X + area.Width - this.PaddingRight; - var topY = area.Y + this.PaddingTop; - var bottomY = area.Y + area.Height - this.PaddingBottom; + public IEnumerable CreateRectangles(Rectangle area, float patchScale = 1) { + var pl = (int) (this.PaddingLeft * patchScale); + var pr = (int) (this.PaddingRight * patchScale); + var pt = (int) (this.PaddingTop * patchScale); + var pb = (int) (this.PaddingBottom * patchScale); - yield return new Rectangle(area.X, area.Y, this.PaddingLeft, this.PaddingTop); - yield return new Rectangle(leftX, area.Y, centerW, this.PaddingTop); - yield return new Rectangle(rightX, area.Y, this.PaddingRight, this.PaddingTop); - yield return new Rectangle(area.X, topY, this.PaddingLeft, centerH); + var centerW = area.Width - pl - pr; + var centerH = area.Height - pt - pb; + var leftX = area.X + pl; + var rightX = area.X + area.Width - pr; + var topY = area.Y + pt; + var bottomY = area.Y + area.Height - pb; + + yield return new Rectangle(area.X, area.Y, pl, pt); + yield return new Rectangle(leftX, area.Y, centerW, pt); + yield return new Rectangle(rightX, area.Y, pr, pt); + yield return new Rectangle(area.X, topY, pl, centerH); yield return new Rectangle(leftX, topY, centerW, centerH); - yield return new Rectangle(rightX, topY, this.PaddingRight, centerH); - yield return new Rectangle(area.X, bottomY, this.PaddingLeft, this.PaddingBottom); - yield return new Rectangle(leftX, bottomY, centerW, this.PaddingBottom); - yield return new Rectangle(rightX, bottomY, this.PaddingRight, this.PaddingBottom); + yield return new Rectangle(rightX, topY, pr, centerH); + yield return new Rectangle(area.X, bottomY, pl, pb); + yield return new Rectangle(leftX, bottomY, centerW, pb); + yield return new Rectangle(rightX, bottomY, pr, pb); } } public static class NinePatchExtensions { - public static void Draw(this SpriteBatch batch, NinePatch texture, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) { - var dest = texture.CreateRectangles(destinationRectangle); + public static void Draw(this SpriteBatch batch, NinePatch texture, Rectangle destinationRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth, float patchScale = 1) { + var dest = texture.CreateRectangles(destinationRectangle, patchScale); var count = 0; foreach (var rect in dest) { if (!rect.IsEmpty) @@ -66,8 +72,8 @@ namespace MLEM.Textures { } } - public static void Draw(this SpriteBatch batch, NinePatch texture, Rectangle destinationRectangle, Color color) { - batch.Draw(texture, destinationRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0); + public static void Draw(this SpriteBatch batch, NinePatch texture, Rectangle destinationRectangle, Color color, float patchScale = 1) { + batch.Draw(texture, destinationRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0, patchScale); } } diff --git a/Tests/GameImpl.cs b/Tests/GameImpl.cs index 0470fae..2208b86 100644 --- a/Tests/GameImpl.cs +++ b/Tests/GameImpl.cs @@ -41,7 +41,7 @@ namespace Tests { this.UiSystem.Style = style; this.UiSystem.GlobalScale = 5; - var root = new Panel(Anchor.BottomLeft, new Vector2(100, 120), Point.Zero, true); + var root = new Panel(Anchor.Center, new Vector2(100, 120), Point.Zero, true); this.UiSystem.Add("Test", root); root.AddChild(new Paragraph(Anchor.AutoLeft, 1, "This is a test text that is hopefully long enough to cover at least a few lines, otherwise it would be very sad."));