diff --git a/CHANGELOG.md b/CHANGELOG.md index b5639e1..5394dd7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ Improvements - Multi-target net452, making MLEM compatible with MonoGame for consoles - Allow retrieving the cost of a calculated path when using AStar - Added trimming and AOT annotations and made MLEM trimmable +- Allow specifying percentage-based padding for a NinePatch - **Drastically improved StaticSpriteBatch batching performance** - **Made GenericFont and TokenizedString support UTF-32 characters like emoji** diff --git a/Demos/GameImpl.cs b/Demos/GameImpl.cs index 5badbb6..d05a49a 100644 --- a/Demos/GameImpl.cs +++ b/Demos/GameImpl.cs @@ -91,8 +91,8 @@ namespace Demos { TextScale = 0.1F, PanelTexture = new NinePatch(new TextureRegion(tex, 0, 8, 24, 24), 8), ButtonTexture = new NinePatch(new TextureRegion(tex, 24, 8, 16, 16), 4), - ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 1, 1, 2, 2), - ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 1, 1, 2, 2), + ScrollBarBackground = new NinePatch(new TextureRegion(tex, 12, 0, 4, 8), 0.25F, paddingPercent: true), + ScrollBarScrollerTexture = new NinePatch(new TextureRegion(tex, 8, 0, 4, 8), 0.25F, paddingPercent: true), LinkColor = Color.CornflowerBlue }; } diff --git a/MLEM/Misc/Padding.cs b/MLEM/Misc/Padding.cs index 92f6aaf..adec88c 100644 --- a/MLEM/Misc/Padding.cs +++ b/MLEM/Misc/Padding.cs @@ -95,6 +95,13 @@ namespace MLEM.Misc { return new Padding(p.Left * scale, p.Right * scale, p.Top * scale, p.Bottom * scale); } + /// + /// Scales a padding by a . + /// + public static Padding operator *(Padding p, Vector2 scale) { + return new Padding(p.Left * scale.X, p.Right * scale.X, p.Top * scale.Y, p.Bottom * scale.Y); + } + /// /// Adds two paddings together in a memberwise fashion. /// diff --git a/MLEM/Textures/NinePatch.cs b/MLEM/Textures/NinePatch.cs index 4de6384..1a3481c 100644 --- a/MLEM/Textures/NinePatch.cs +++ b/MLEM/Textures/NinePatch.cs @@ -17,7 +17,7 @@ namespace MLEM.Textures { /// public readonly TextureRegion Region; /// - /// The padding in each direction that marks where the outline area stops + /// The padding in each direction that marks where the outline area stops, in pixels /// public readonly Padding Padding; /// @@ -33,11 +33,12 @@ namespace MLEM.Textures { /// Creates a new nine patch from a texture and a padding /// /// The texture to use - /// The padding that marks where the outline area stops + /// The padding that marks where the outline area stops in pixels, or as a percentage if is /// The mode to use for drawing this nine patch, defaults to - public NinePatch(TextureRegion texture, Padding padding, NinePatchMode mode = NinePatchMode.Stretch) { + /// Whether the padding should represent a percentage of the underlying 's size, rather than an absolute pixel amount + public NinePatch(TextureRegion texture, Padding padding, NinePatchMode mode = NinePatchMode.Stretch, bool paddingPercent = false) { this.Region = texture; - this.Padding = padding; + this.Padding = paddingPercent ? padding * texture.Size.ToVector2() : padding; this.Mode = mode; this.SourceRectangles = new Rectangle[9]; for (var i = 0; i < this.SourceRectangles.Length; i++) @@ -48,26 +49,28 @@ namespace MLEM.Textures { /// Creates a new nine patch from a texture and a padding /// /// The texture to use - /// The padding on the left edge - /// The padding on the right edge - /// The padding on the top edge - /// The padding on the bottom edge + /// The padding on the left edge in pixels, or as a percentage if is + /// The padding on the right edge in pixels, or as a percentage if is + /// The padding on the top edge in pixels, or as a percentage if is + /// The padding on the bottom edge in pixels, or as a percentage if is /// The mode to use for drawing this nine patch, defaults to - public NinePatch(TextureRegion texture, int paddingLeft, int paddingRight, int paddingTop, int paddingBottom, NinePatchMode mode = NinePatchMode.Stretch) : this(texture, new Padding(paddingLeft, paddingRight, paddingTop, paddingBottom), mode) {} + /// Whether the padding should represent a percentage of the underlying 's size, rather than an absolute pixel amount + public NinePatch(TextureRegion texture, float paddingLeft, float paddingRight, float paddingTop, float paddingBottom, NinePatchMode mode = NinePatchMode.Stretch, bool paddingPercent = false) : this(texture, new Padding(paddingLeft, paddingRight, paddingTop, paddingBottom), mode, paddingPercent) {} - /// - public NinePatch(Texture2D texture, int paddingLeft, int paddingRight, int paddingTop, int paddingBottom, NinePatchMode mode = NinePatchMode.Stretch) : this(new TextureRegion(texture), paddingLeft, paddingRight, paddingTop, paddingBottom, mode) {} + /// + public NinePatch(Texture2D texture, float paddingLeft, float paddingRight, float paddingTop, float paddingBottom, NinePatchMode mode = NinePatchMode.Stretch, bool paddingPercent = false) : this(new TextureRegion(texture), paddingLeft, paddingRight, paddingTop, paddingBottom, mode, paddingPercent) {} /// /// Creates a new nine patch from a texture and a uniform padding /// /// The texture to use - /// The padding that each edge should have + /// The padding that each edge should have in pixels, or as a percentage if is /// The mode to use for drawing this nine patch, defaults to - public NinePatch(Texture2D texture, int padding, NinePatchMode mode = NinePatchMode.Stretch) : this(new TextureRegion(texture), padding, mode) {} + /// Whether the padding should represent a percentage of the underlying 's size, rather than an absolute pixel amount + public NinePatch(Texture2D texture, float padding, NinePatchMode mode = NinePatchMode.Stretch, bool paddingPercent = false) : this(new TextureRegion(texture), padding, mode, paddingPercent) {} - /// - public NinePatch(TextureRegion texture, int padding, NinePatchMode mode = NinePatchMode.Stretch) : this(texture, padding, padding, padding, padding, mode) {} + /// + public NinePatch(TextureRegion texture, float padding, NinePatchMode mode = NinePatchMode.Stretch, bool paddingPercent = false) : this(texture, padding, padding, padding, padding, mode, paddingPercent) {} internal RectangleF GetRectangleForIndex(RectangleF area, int index, float patchScale = 1) { var pl = this.Padding.Left * patchScale;