From b0bf55842b9612cfefa9dc4a287fa166a2a22d1a Mon Sep 17 00:00:00 2001 From: Ellpeck Date: Sat, 16 Nov 2024 13:53:04 +0100 Subject: [PATCH] Improved the naming of SpriteBatchExtensions texture generation methods --- CHANGELOG.md | 6 +++- MLEM.Ui/Style/UntexturedStyle.cs | 26 +++++++------- MLEM/Graphics/SpriteBatchExtensions.cs | 49 ++++++++++++++++++++------ Tests/TexturePackerTests.cs | 8 +---- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d918228..3e451f8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,11 +21,15 @@ Jump to version: ### MLEM Improvements - Allow getting and setting TextureData by index -- Improve TextureExtensions.PremultipliedCopy memory performance +- Improved TextureExtensions.PremultipliedCopy memory performance +- Improved the naming of SpriteBatchExtensions texture generation methods Fixes - Fixed formatting codes at the start of strings not being added to the AllCodes collection +Removals +- Marked SpriteBatchExtensions GenerateTexture and GenerateSquareTexture as obsolete in favor of their more clearly named replacements + ### MLEM.Ui Additions - Added Panel.IsVisible method to check if a child element is visible diff --git a/MLEM.Ui/Style/UntexturedStyle.cs b/MLEM.Ui/Style/UntexturedStyle.cs index f89c003..4f798d0 100644 --- a/MLEM.Ui/Style/UntexturedStyle.cs +++ b/MLEM.Ui/Style/UntexturedStyle.cs @@ -5,7 +5,7 @@ using MLEM.Graphics; namespace MLEM.Ui.Style { /// /// The default, untextured . - /// Note that, as MLEM does not provide any texture or font assets, this default style is made up of single-color textures that were generated using . + /// Note that, as MLEM does not provide any texture or font assets, this default style is made up of single-color textures that were generated using . /// public class UntexturedStyle : UiStyle { @@ -14,18 +14,18 @@ namespace MLEM.Ui.Style { /// /// The sprite batch to generate the textures with public UntexturedStyle(SpriteBatch batch) { - this.SelectionIndicator = batch.GenerateTexture(Color.Transparent, Color.Red); - this.ButtonTexture = batch.GenerateTexture(Color.CadetBlue); - this.PanelTexture = batch.GenerateTexture(Color.Gray); - this.TextFieldTexture = batch.GenerateTexture(Color.MediumBlue); - this.ScrollBarBackground = batch.GenerateTexture(Color.LightBlue); - this.ScrollBarScrollerTexture = batch.GenerateTexture(Color.Blue); - this.CheckboxTexture = batch.GenerateTexture(Color.LightBlue); - this.CheckboxCheckmark = batch.GenerateTexture(Color.Blue).Region; - this.RadioTexture = batch.GenerateTexture(Color.AliceBlue); - this.RadioCheckmark = batch.GenerateTexture(Color.CornflowerBlue).Region; - this.TooltipBackground = batch.GenerateTexture(Color.Black * 0.65F, Color.Black * 0.65F); - this.ProgressBarTexture = batch.GenerateTexture(Color.RoyalBlue); + this.SelectionIndicator = batch.GenerateNinePatch(Color.Transparent, Color.Red); + this.ButtonTexture = batch.GenerateNinePatch(Color.CadetBlue, Color.Black); + this.PanelTexture = batch.GenerateNinePatch(Color.Gray, Color.Black); + this.TextFieldTexture = batch.GenerateNinePatch(Color.MediumBlue, Color.Black); + this.ScrollBarBackground = batch.GenerateNinePatch(Color.LightBlue, Color.Black); + this.ScrollBarScrollerTexture = batch.GenerateNinePatch(Color.Blue, Color.Black); + this.CheckboxTexture = batch.GenerateNinePatch(Color.LightBlue, Color.Black); + this.CheckboxCheckmark = batch.GenerateNinePatch(Color.Blue, Color.Black).Region; + this.RadioTexture = batch.GenerateNinePatch(Color.AliceBlue, Color.Black); + this.RadioCheckmark = batch.GenerateNinePatch(Color.CornflowerBlue, Color.Black).Region; + this.TooltipBackground = batch.GenerateNinePatch(Color.Black * 0.65F, Color.Black * 0.65F); + this.ProgressBarTexture = batch.GenerateNinePatch(Color.RoyalBlue, Color.Black); } } diff --git a/MLEM/Graphics/SpriteBatchExtensions.cs b/MLEM/Graphics/SpriteBatchExtensions.cs index e44f774..6b3a533 100644 --- a/MLEM/Graphics/SpriteBatchExtensions.cs +++ b/MLEM/Graphics/SpriteBatchExtensions.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using MLEM.Maths; @@ -18,11 +19,8 @@ namespace MLEM.Graphics { /// The sprite batch /// A 1x1 pixel white texture public static Texture2D GetBlankTexture(this SpriteBatch batch) { - if (SpriteBatchExtensions.blankTexture == null) { - SpriteBatchExtensions.blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1); - SpriteBatchExtensions.blankTexture.SetData(new[] {Color.White}); - SpriteBatchExtensions.AutoDispose(batch, SpriteBatchExtensions.blankTexture); - } + if (SpriteBatchExtensions.blankTexture == null) + SpriteBatchExtensions.blankTexture = batch.GenerateTexture(Color.White, 1, 1); return SpriteBatchExtensions.blankTexture; } @@ -34,13 +32,25 @@ namespace MLEM.Graphics { /// The fill color of the texture /// The outline color of the texture /// A containing a 3x3 texture with an outline + [Obsolete("Use the new GenerateNinePatch instead")] public static NinePatch GenerateTexture(this SpriteBatch batch, Color color, Color? outlineColor = null) { - var outli = outlineColor ?? Color.Black; + return batch.GenerateNinePatch(color, outlineColor ?? Color.Black); + } + + /// + /// Generates a that has a texture with a given color and outline color. + /// This texture is automatically disposed of when the batch is disposed. + /// + /// The sprite batch + /// The fill color of the texture + /// The outline color of the texture + /// A containing a 3x3 texture with an outline + public static NinePatch GenerateNinePatch(this SpriteBatch batch, Color color, Color outlineColor) { var tex = new Texture2D(batch.GraphicsDevice, 3, 3); tex.SetData(new[] { - outli, outli, outli, - outli, color, outli, - outli, outli, outli + outlineColor, outlineColor, outlineColor, + outlineColor, color, outlineColor, + outlineColor, outlineColor, outlineColor }); SpriteBatchExtensions.AutoDispose(batch, tex); return new NinePatch(tex, 1); @@ -53,9 +63,26 @@ namespace MLEM.Graphics { /// The sprite batch /// The color of the texture /// A new texture with the given data + [Obsolete("Use the new GenerateTexture instead")] public static Texture2D GenerateSquareTexture(this SpriteBatch batch, Color color) { - var tex = new Texture2D(batch.GraphicsDevice, 1, 1); - tex.SetData(new[] {color}); + return batch.GenerateTexture(color, 1, 1); + } + + /// + /// Generates a texture with the given and , which will be filled with the given . + /// This texture is automatically disposed of when the batch is disposed. + /// + /// The sprite batch + /// The color of the texture + /// The width of the resulting texture + /// The height of the resulting texture + /// A new texture with the given data + public static Texture2D GenerateTexture(this SpriteBatch batch, Color color, int width, int height) { + var tex = new Texture2D(batch.GraphicsDevice, width, height); + using (var data = tex.GetTextureData()) { + for (var i = 0; i < data.Length; i++) + data[i] = color; + } SpriteBatchExtensions.AutoDispose(batch, tex); return tex; } diff --git a/Tests/TexturePackerTests.cs b/Tests/TexturePackerTests.cs index 38f4e51..3980c46 100644 --- a/Tests/TexturePackerTests.cs +++ b/Tests/TexturePackerTests.cs @@ -135,13 +135,7 @@ public class TexturePackerTests : GameTestFixture { private TextureRegion MakeTextureRegion(int width, int height) { var color = ColorHelper.FromHexRgb(SingleRandom.Int(this.generatedTextures.Count)); - var texture = new Texture2D(this.Game.GraphicsDevice, Math.Max(width, 1), Math.Max(height, 1)); - using (var data = texture.GetTextureData()) { - for (var x = 0; x < texture.Width; x++) { - for (var y = 0; y < texture.Height; y++) - data[x, y] = color; - } - } + var texture = this.Game.SpriteBatch.GenerateTexture(color, Math.Max(width, 1), Math.Max(height, 1)); var region = new TextureRegion(texture, 0, 0, width, height); this.generatedTextures.Add(region); return region;