1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-22 20:58:34 +01:00

added the ability for nine patch regions to have their patches scaled

This commit is contained in:
Ellpeck 2019-08-11 21:38:03 +02:00
parent a4eede5fe5
commit 8af040787c
5 changed files with 29 additions and 23 deletions

View file

@ -29,7 +29,7 @@ namespace MLEM.Ui.Elements {
tex = this.HoveredTexture; tex = this.HoveredTexture;
color = this.HoveredColor * alpha; color = this.HoveredColor * alpha;
} }
batch.Draw(tex, this.DisplayArea, color); batch.Draw(tex, this.DisplayArea, color, this.Scale);
base.Draw(time, batch, alpha); base.Draw(time, batch, alpha);
} }

View file

@ -16,7 +16,7 @@ namespace MLEM.Ui.Elements {
} }
public override void Draw(GameTime time, SpriteBatch batch, float alpha) { 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); base.Draw(time, batch, alpha);
} }

View file

@ -75,7 +75,7 @@ namespace MLEM.Ui.Elements {
tex = this.HoveredTexture; tex = this.HoveredTexture;
color = this.HoveredColor * alpha; 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 caret = this.IsSelected && this.caretBlinkTimer >= 0.5F ? "|" : "";
var text = this.Text.ToString(this.textStartIndex, this.Text.Length - this.textStartIndex) + caret; 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); 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);

View file

@ -2,6 +2,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Extensions;
namespace MLEM.Textures { namespace MLEM.Textures {
public class NinePatch { public class NinePatch {
@ -33,31 +34,36 @@ namespace MLEM.Textures {
public NinePatch(TextureRegion texture, int padding) : this(texture, padding, padding, padding, padding) { public NinePatch(TextureRegion texture, int padding) : this(texture, padding, padding, padding, padding) {
} }
public IEnumerable<Rectangle> CreateRectangles(Rectangle area) { public IEnumerable<Rectangle> CreateRectangles(Rectangle area, float patchScale = 1) {
var centerW = area.Width - this.PaddingLeft - this.PaddingRight; var pl = (int) (this.PaddingLeft * patchScale);
var centerH = area.Height - this.PaddingTop - this.PaddingBottom; var pr = (int) (this.PaddingRight * patchScale);
var leftX = area.X + this.PaddingLeft; var pt = (int) (this.PaddingTop * patchScale);
var rightX = area.X + area.Width - this.PaddingRight; var pb = (int) (this.PaddingBottom * patchScale);
var topY = area.Y + this.PaddingTop;
var bottomY = area.Y + area.Height - this.PaddingBottom;
yield return new Rectangle(area.X, area.Y, this.PaddingLeft, this.PaddingTop); var centerW = area.Width - pl - pr;
yield return new Rectangle(leftX, area.Y, centerW, this.PaddingTop); var centerH = area.Height - pt - pb;
yield return new Rectangle(rightX, area.Y, this.PaddingRight, this.PaddingTop); var leftX = area.X + pl;
yield return new Rectangle(area.X, topY, this.PaddingLeft, centerH); 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(leftX, topY, centerW, centerH);
yield return new Rectangle(rightX, topY, this.PaddingRight, centerH); yield return new Rectangle(rightX, topY, pr, centerH);
yield return new Rectangle(area.X, bottomY, this.PaddingLeft, this.PaddingBottom); yield return new Rectangle(area.X, bottomY, pl, pb);
yield return new Rectangle(leftX, bottomY, centerW, this.PaddingBottom); yield return new Rectangle(leftX, bottomY, centerW, pb);
yield return new Rectangle(rightX, bottomY, this.PaddingRight, this.PaddingBottom); yield return new Rectangle(rightX, bottomY, pr, pb);
} }
} }
public static class NinePatchExtensions { 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) { 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); var dest = texture.CreateRectangles(destinationRectangle, patchScale);
var count = 0; var count = 0;
foreach (var rect in dest) { foreach (var rect in dest) {
if (!rect.IsEmpty) if (!rect.IsEmpty)
@ -66,8 +72,8 @@ namespace MLEM.Textures {
} }
} }
public static void Draw(this SpriteBatch batch, NinePatch texture, Rectangle destinationRectangle, Color color) { 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); batch.Draw(texture, destinationRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0, patchScale);
} }
} }

View file

@ -41,7 +41,7 @@ namespace Tests {
this.UiSystem.Style = style; this.UiSystem.Style = style;
this.UiSystem.GlobalScale = 5; 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); 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.")); 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."));