mirror of
https://github.com/Ellpeck/MLEM.git
synced 2024-12-25 01:39:23 +01:00
added the ability for nine patch regions to have their patches scaled
This commit is contained in:
parent
a4eede5fe5
commit
8af040787c
5 changed files with 29 additions and 23 deletions
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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<Rectangle> 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<Rectangle> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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."));
|
||||
|
|
Loading…
Reference in a new issue