using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using MLEM.Misc;
using MLEM.Textures;
namespace MLEM.Extensions {
///
/// A set of extensions for dealing with
///
public static class SpriteBatchExtensions {
private static Texture2D blankTexture;
///
/// Returns a 1x1 pixel white texture that can be used for drawing solid color shapes.
/// This texture is automatically disposed of when the batch is disposed.
///
/// The sprite batch
/// A 1x1 pixel white texture
public static Texture2D GetBlankTexture(this SpriteBatch batch) {
if (blankTexture == null) {
blankTexture = new Texture2D(batch.GraphicsDevice, 1, 1);
blankTexture.SetData(new[] {Color.White});
AutoDispose(batch, blankTexture);
}
return blankTexture;
}
///
/// 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 GenerateTexture(this SpriteBatch batch, Color color, Color? outlineColor = null) {
var outli = outlineColor ?? Color.Black;
var tex = new Texture2D(batch.GraphicsDevice, 3, 3);
tex.SetData(new[] {
outli, outli, outli,
outli, color, outli,
outli, outli, outli
});
AutoDispose(batch, tex);
return new NinePatch(tex, 1);
}
///
/// Generates a 1x1 texture with the given color.
/// This texture is automatically disposed of when the batch is disposed.
///
/// The sprite batch
/// The color of the texture
/// A new texture with the given data
public static Texture2D GenerateSquareTexture(this SpriteBatch batch, Color color) {
var tex = new Texture2D(batch.GraphicsDevice, 1, 1);
tex.SetData(new[] {color});
AutoDispose(batch, tex);
return tex;
}
///
/// Generates a texture with the given size that contains a circle.
/// The circle's center will be the center of the texture, and the circle will lead up to the edges of the texture.
/// This texture is automatically disposed of when the batch is disposed.
///
/// The sprite batch
/// The color of the texture
/// The width and height of the texture, and the diameter of the circle
/// A new texture with the given data
public static Texture2D GenerateCircleTexture(this SpriteBatch batch, Color color, int size) {
var tex = new Texture2D(batch.GraphicsDevice, size, size);
using (var data = tex.GetTextureData()) {
for (var x = 0; x < tex.Width; x++) {
for (var y = 0; y < tex.Height; y++) {
var dist = Vector2.Distance(new Vector2(size / 2), new Vector2(x, y));
data[x, y] = dist <= size / 2 ? color : Color.Transparent;
}
}
}
AutoDispose(batch, tex);
return tex;
}
///
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth) {
var source = sourceRectangle ?? new Rectangle(0, 0, texture.Width, texture.Height);
var scale = new Vector2(1F / source.Width, 1F / source.Height) * destinationRectangle.Size;
batch.Draw(texture, destinationRectangle.Location, sourceRectangle, color, rotation, origin, scale, effects, layerDepth);
}
///
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Rectangle? sourceRectangle, Color color) {
batch.Draw(texture, destinationRectangle, sourceRectangle, color, 0, Vector2.Zero, SpriteEffects.None, 0);
}
///
public static void Draw(this SpriteBatch batch, Texture2D texture, RectangleF destinationRectangle, Color color) {
batch.Draw(texture, destinationRectangle, null, color);
}
private static void AutoDispose(SpriteBatch batch, Texture2D texture) {
batch.Disposing += (sender, ars) => {
if (texture != null) {
texture.Dispose();
texture = null;
}
};
}
}
}