1
0
Fork 0
mirror of https://github.com/Ellpeck/MLEM.git synced 2024-11-23 21:28:35 +01:00

Compare commits

...

2 commits

5 changed files with 87 additions and 47 deletions

View file

@ -19,9 +19,17 @@ Jump to version:
## 8.0.0 (In Development)
### MLEM
Improvements
- Allow getting and setting TextureData by index
- 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

View file

@ -5,7 +5,7 @@ using MLEM.Graphics;
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 {
@ -14,18 +14,18 @@ namespace MLEM.Ui.Style {
/// </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);
}
}

View file

@ -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 {
/// <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;
}
@ -34,13 +32,25 @@ namespace MLEM.Graphics {
/// <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);
@ -53,9 +63,26 @@ namespace MLEM.Graphics {
/// <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;
}

View file

@ -24,15 +24,12 @@ namespace MLEM.Textures {
/// <param name="texture">The texture of which to create a premultiplied copy.</param>
/// <returns>The premultiplied copy of the <paramref name="texture"/>.</returns>
public static Texture2D PremultipliedCopy(this Texture2D texture) {
var data = new Color[texture.Width * texture.Height];
texture.GetData(data);
for (var i = 0; i < data.Length; i++)
data[i] = Color.FromNonPremultiplied(data[i].ToVector4());
var ret = new Texture2D(texture.GraphicsDevice, texture.Width, texture.Height);
using (var textureData = texture.GetTextureData()) {
using (var retData = ret.GetTextureData()) {
for (var x = 0; x < ret.Width; x++) {
for (var y = 0; y < ret.Height; y++)
retData[x, y] = Color.FromNonPremultiplied(textureData[x, y].ToVector4());
}
}
}
ret.SetData(data);
return ret;
}
@ -51,20 +48,34 @@ namespace MLEM.Textures {
/// <param name="x">The x coordinate of the texture location</param>
/// <param name="y">The y coordinate of the texture location</param>
public Color this[int x, int y] {
get => this.data[this.ToIndex(x, y)];
get => this[this.ToIndex(x, y)];
set => this[this.ToIndex(x, y)] = value;
}
/// <summary>
/// Returns the color at the given x,y position of the texture, where 0,0 represents the bottom left.
/// </summary>
/// <param name="point">The x and y coordinates of the texture location</param>
public Color this[Point point] {
get => this[point.X, point.Y];
set => this[point.X, point.Y] = value;
}
/// <summary>
/// Returns the color at the given index of the texture data, which is the index in the <see cref="Texture2D.GetData{T}(T[])"/> array.
/// </summary>
/// <param name="index">The index.</param>
public Color this[int index] {
get => this.data[index];
set {
var index = this.ToIndex(x, y);
if (this.data[index] != value) {
this.data[index] = value;
this.dirty = true;
}
}
}
/// <inheritdoc cref="this[int,int]"/>
public Color this[Point point] {
get => this[point.X, point.Y];
set => this[point.X, point.Y] = value;
}
/// <summary>
/// The length of the underlying texture data array, which is the texture's width multiplied by its height.
/// </summary>
public int Length => this.data.Length;
/// <summary>
/// Creates a new texture data instance for the given texture.
@ -107,7 +118,7 @@ namespace MLEM.Textures {
/// <returns>The corresponding texture coordinate</returns>
/// <exception cref="ArgumentException">If the given index is out of bounds</exception>
public Point FromIndex(int index) {
if (index < 0 || index >= this.data.Length)
if (index < 0 || index >= this.Length)
throw new ArgumentException();
return new Point(index % this.texture.Width, index / this.texture.Width);
}

View file

@ -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;