1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-21 12:33:29 +01:00

Improved the naming of SpriteBatchExtensions texture generation methods

This commit is contained in:
Ell 2024-11-16 13:53:04 +01:00
parent 201fd8d294
commit b0bf55842b
4 changed files with 57 additions and 32 deletions

View file

@ -21,11 +21,15 @@ Jump to version:
### MLEM ### MLEM
Improvements Improvements
- Allow getting and setting TextureData by index - 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 Fixes
- Fixed formatting codes at the start of strings not being added to the AllCodes collection - 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 ### MLEM.Ui
Additions Additions
- Added Panel.IsVisible method to check if a child element is visible - Added Panel.IsVisible method to check if a child element is visible

View file

@ -5,7 +5,7 @@ using MLEM.Graphics;
namespace MLEM.Ui.Style { namespace MLEM.Ui.Style {
/// <summary> /// <summary>
/// The default, untextured <see cref="UiStyle"/>. /// 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> /// </summary>
public class UntexturedStyle : UiStyle { public class UntexturedStyle : UiStyle {
@ -14,18 +14,18 @@ namespace MLEM.Ui.Style {
/// </summary> /// </summary>
/// <param name="batch">The sprite batch to generate the textures with</param> /// <param name="batch">The sprite batch to generate the textures with</param>
public UntexturedStyle(SpriteBatch batch) { public UntexturedStyle(SpriteBatch batch) {
this.SelectionIndicator = batch.GenerateTexture(Color.Transparent, Color.Red); this.SelectionIndicator = batch.GenerateNinePatch(Color.Transparent, Color.Red);
this.ButtonTexture = batch.GenerateTexture(Color.CadetBlue); this.ButtonTexture = batch.GenerateNinePatch(Color.CadetBlue, Color.Black);
this.PanelTexture = batch.GenerateTexture(Color.Gray); this.PanelTexture = batch.GenerateNinePatch(Color.Gray, Color.Black);
this.TextFieldTexture = batch.GenerateTexture(Color.MediumBlue); this.TextFieldTexture = batch.GenerateNinePatch(Color.MediumBlue, Color.Black);
this.ScrollBarBackground = batch.GenerateTexture(Color.LightBlue); this.ScrollBarBackground = batch.GenerateNinePatch(Color.LightBlue, Color.Black);
this.ScrollBarScrollerTexture = batch.GenerateTexture(Color.Blue); this.ScrollBarScrollerTexture = batch.GenerateNinePatch(Color.Blue, Color.Black);
this.CheckboxTexture = batch.GenerateTexture(Color.LightBlue); this.CheckboxTexture = batch.GenerateNinePatch(Color.LightBlue, Color.Black);
this.CheckboxCheckmark = batch.GenerateTexture(Color.Blue).Region; this.CheckboxCheckmark = batch.GenerateNinePatch(Color.Blue, Color.Black).Region;
this.RadioTexture = batch.GenerateTexture(Color.AliceBlue); this.RadioTexture = batch.GenerateNinePatch(Color.AliceBlue, Color.Black);
this.RadioCheckmark = batch.GenerateTexture(Color.CornflowerBlue).Region; this.RadioCheckmark = batch.GenerateNinePatch(Color.CornflowerBlue, Color.Black).Region;
this.TooltipBackground = batch.GenerateTexture(Color.Black * 0.65F, Color.Black * 0.65F); this.TooltipBackground = batch.GenerateNinePatch(Color.Black * 0.65F, Color.Black * 0.65F);
this.ProgressBarTexture = batch.GenerateTexture(Color.RoyalBlue); this.ProgressBarTexture = batch.GenerateNinePatch(Color.RoyalBlue, Color.Black);
} }
} }

View file

@ -1,3 +1,4 @@
using System;
using Microsoft.Xna.Framework; using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Graphics;
using MLEM.Maths; using MLEM.Maths;
@ -18,11 +19,8 @@ namespace MLEM.Graphics {
/// <param name="batch">The sprite batch</param> /// <param name="batch">The sprite batch</param>
/// <returns>A 1x1 pixel white texture</returns> /// <returns>A 1x1 pixel white texture</returns>
public static Texture2D GetBlankTexture(this SpriteBatch batch) { public static Texture2D GetBlankTexture(this SpriteBatch batch) {
if (SpriteBatchExtensions.blankTexture == null) { if (SpriteBatchExtensions.blankTexture == null)
SpriteBatchExtensions.blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1); SpriteBatchExtensions.blankTexture = batch.GenerateTexture(Color.White, 1, 1);
SpriteBatchExtensions.blankTexture.SetData(new[] {Color.White});
SpriteBatchExtensions.AutoDispose(batch, SpriteBatchExtensions.blankTexture);
}
return SpriteBatchExtensions.blankTexture; return SpriteBatchExtensions.blankTexture;
} }
@ -34,13 +32,25 @@ namespace MLEM.Graphics {
/// <param name="color">The fill color of the texture</param> /// <param name="color">The fill color of the texture</param>
/// <param name="outlineColor">The outline 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> /// <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) { 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); var tex = new Texture2D(batch.GraphicsDevice, 3, 3);
tex.SetData(new[] { tex.SetData(new[] {
outli, outli, outli, outlineColor, outlineColor, outlineColor,
outli, color, outli, outlineColor, color, outlineColor,
outli, outli, outli outlineColor, outlineColor, outlineColor
}); });
SpriteBatchExtensions.AutoDispose(batch, tex); SpriteBatchExtensions.AutoDispose(batch, tex);
return new NinePatch(tex, 1); return new NinePatch(tex, 1);
@ -53,9 +63,26 @@ namespace MLEM.Graphics {
/// <param name="batch">The sprite batch</param> /// <param name="batch">The sprite batch</param>
/// <param name="color">The color of the texture</param> /// <param name="color">The color of the texture</param>
/// <returns>A new texture with the given data</returns> /// <returns>A new texture with the given data</returns>
[Obsolete("Use the new GenerateTexture instead")]
public static Texture2D GenerateSquareTexture(this SpriteBatch batch, Color color) { public static Texture2D GenerateSquareTexture(this SpriteBatch batch, Color color) {
var tex = new Texture2D(batch.GraphicsDevice, 1, 1); return batch.GenerateTexture(color, 1, 1);
tex.SetData(new[] {color}); }
/// <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); SpriteBatchExtensions.AutoDispose(batch, tex);
return tex; return tex;
} }

View file

@ -135,13 +135,7 @@ public class TexturePackerTests : GameTestFixture {
private TextureRegion MakeTextureRegion(int width, int height) { private TextureRegion MakeTextureRegion(int width, int height) {
var color = ColorHelper.FromHexRgb(SingleRandom.Int(this.generatedTextures.Count)); var color = ColorHelper.FromHexRgb(SingleRandom.Int(this.generatedTextures.Count));
var texture = new Texture2D(this.Game.GraphicsDevice, Math.Max(width, 1), Math.Max(height, 1)); var texture = this.Game.SpriteBatch.GenerateTexture(color, 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 region = new TextureRegion(texture, 0, 0, width, height); var region = new TextureRegion(texture, 0, 0, width, height);
this.generatedTextures.Add(region); this.generatedTextures.Add(region);
return region; return region;