Skip to content

Commit

Permalink
Improved the naming of SpriteBatchExtensions texture generation methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Ellpeck committed Nov 16, 2024
1 parent 201fd8d commit b0bf558
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 32 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 13 additions & 13 deletions MLEM.Ui/Style/UntexturedStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace MLEM.Ui.Style {
/// <summary>
/// The default, untextured <see cref="UiStyle"/>.
/// 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 <see cref="SpriteBatchExtensions.GenerateTexture"/>.
/// 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 <see cref="SpriteBatchExtensions.GenerateNinePatch"/>.
/// </summary>
public class UntexturedStyle : UiStyle {

Expand All @@ -14,18 +14,18 @@ public class UntexturedStyle : UiStyle {
/// </summary>
/// <param name="batch">The sprite batch to generate the textures with</param>
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);
}

}
Expand Down
49 changes: 38 additions & 11 deletions MLEM/Graphics/SpriteBatchExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Maths;
Expand All @@ -18,11 +19,8 @@ public static class SpriteBatchExtensions {
/// <param name="batch">The sprite batch</param>
/// <returns>A 1x1 pixel white texture</returns>
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;
}

Expand All @@ -34,13 +32,25 @@ public static Texture2D GetBlankTexture(this SpriteBatch batch) {
/// <param name="color">The fill color of the texture</param>
/// <param name="outlineColor">The outline color of the texture</param>
/// <returns>A <see cref="NinePatch"/> containing a 3x3 texture with an outline</returns>
[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);
}

/// <summary>
/// Generates a <see cref="NinePatch"/> that has a texture with a given color and outline color.
/// This texture is automatically disposed of when the batch is disposed.
/// </summary>
/// <param name="batch">The sprite batch</param>
/// <param name="color">The fill color of the texture</param>
/// <param name="outlineColor">The outline color of the texture</param>
/// <returns>A <see cref="NinePatch"/> containing a 3x3 texture with an outline</returns>
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);
Expand All @@ -53,9 +63,26 @@ public static NinePatch GenerateTexture(this SpriteBatch batch, Color color, Col
/// <param name="batch">The sprite batch</param>
/// <param name="color">The color of the texture</param>
/// <returns>A new texture with the given data</returns>
[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);
}

/// <summary>
/// Generates a texture with the given <paramref name="width"/> and <paramref name="height"/>, which will be filled with the given <paramref name="color"/>.
/// This texture is automatically disposed of when the batch is disposed.
/// </summary>
/// <param name="batch">The sprite batch</param>
/// <param name="color">The color of the texture</param>
/// <param name="width">The width of the resulting texture</param>
/// <param name="height">The height of the resulting texture</param>
/// <returns>A new texture with the given data</returns>
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;
}
Expand Down
8 changes: 1 addition & 7 deletions Tests/TexturePackerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,7 @@ public void TestPackTimes([Values(1, 100, 1000, 5000, 10000)] int total) {

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;
Expand Down

0 comments on commit b0bf558

Please sign in to comment.