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

Compare commits

...

3 commits

4 changed files with 48 additions and 10 deletions

View file

@ -29,12 +29,16 @@ Improvements
- Added style properties for a lot of hardcoded default element styles - Added style properties for a lot of hardcoded default element styles
- Allow style properties to set style values with a higher priority, which allows elements to style their default children - Allow style properties to set style values with a higher priority, which allows elements to style their default children
- Allow changing the entire ui style for a single element - Allow changing the entire ui style for a single element
- Skip unnecessary area updates for elements with dirty parents
Fixes Fixes
- Fixed VerticalSpace height parameter being an integer - Fixed VerticalSpace height parameter being an integer
- Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule - Fixed text not being pasted into a text field at all if it contains characters that don't match the input rule
### MLEM.Data ### MLEM.Data
Additions
- Allow RuntimeTexturePacker to automatically dispose submitted textures when packing
Improvements Improvements
- Use TitleContainer for opening streams where possible - Use TitleContainer for opening streams where possible

View file

@ -15,11 +15,6 @@ namespace MLEM.Data {
/// </summary> /// </summary>
public class RuntimeTexturePacker : IDisposable { public class RuntimeTexturePacker : IDisposable {
private readonly List<Request> textures = new List<Request>();
private readonly bool autoIncreaseMaxWidth;
private readonly bool forcePowerOfTwo;
private readonly bool forceSquare;
/// <summary> /// <summary>
/// The generated packed texture. /// The generated packed texture.
/// This value is null before <see cref="Pack"/> is called. /// This value is null before <see cref="Pack"/> is called.
@ -37,6 +32,13 @@ namespace MLEM.Data {
/// The time that <see cref="Pack"/> took the last time it was called /// The time that <see cref="Pack"/> took the last time it was called
/// </summary> /// </summary>
public TimeSpan LastTotalTime => this.LastCalculationTime + this.LastPackTime; public TimeSpan LastTotalTime => this.LastCalculationTime + this.LastPackTime;
private readonly List<Request> textures = new List<Request>();
private readonly bool autoIncreaseMaxWidth;
private readonly bool forcePowerOfTwo;
private readonly bool forceSquare;
private readonly bool disposeTextures;
private int maxWidth; private int maxWidth;
/// <summary> /// <summary>
@ -46,11 +48,13 @@ namespace MLEM.Data {
/// <param name="autoIncreaseMaxWidth">Whether the maximum width should be increased if there is a texture to be packed that is wider than <see cref="maxWidth"/>. Defaults to false.</param> /// <param name="autoIncreaseMaxWidth">Whether the maximum width should be increased if there is a texture to be packed that is wider than <see cref="maxWidth"/>. Defaults to false.</param>
/// <param name="forcePowerOfTwo">Whether the resulting <see cref="PackedTexture"/> should have a width and height that is a power of two</param> /// <param name="forcePowerOfTwo">Whether the resulting <see cref="PackedTexture"/> should have a width and height that is a power of two</param>
/// <param name="forceSquare">Whether the resulting <see cref="PackedTexture"/> should be square regardless of required size</param> /// <param name="forceSquare">Whether the resulting <see cref="PackedTexture"/> should be square regardless of required size</param>
public RuntimeTexturePacker(int maxWidth = 2048, bool autoIncreaseMaxWidth = false, bool forcePowerOfTwo = false, bool forceSquare = false) { /// <param name="disposeTextures">Whether the original textures submitted to this texture packer should be disposed after packing</param>
public RuntimeTexturePacker(int maxWidth = 2048, bool autoIncreaseMaxWidth = false, bool forcePowerOfTwo = false, bool forceSquare = false, bool disposeTextures = false) {
this.maxWidth = maxWidth; this.maxWidth = maxWidth;
this.autoIncreaseMaxWidth = autoIncreaseMaxWidth; this.autoIncreaseMaxWidth = autoIncreaseMaxWidth;
this.forcePowerOfTwo = forcePowerOfTwo; this.forcePowerOfTwo = forcePowerOfTwo;
this.forceSquare = forceSquare; this.forceSquare = forceSquare;
this.disposeTextures = disposeTextures;
} }
/// <summary> /// <summary>
@ -124,8 +128,12 @@ namespace MLEM.Data {
this.LastPackTime = stopwatch.Elapsed; this.LastPackTime = stopwatch.Elapsed;
// invoke callbacks // invoke callbacks
foreach (var request in this.textures) foreach (var request in this.textures) {
request.Result.Invoke(new TextureRegion(this.PackedTexture, request.PackedArea)); request.Result.Invoke(new TextureRegion(this.PackedTexture, request.PackedArea));
if (this.disposeTextures)
request.Texture.Texture.Dispose();
}
this.textures.Clear(); this.textures.Clear();
} }

View file

@ -35,6 +35,11 @@ namespace MLEM.Ui.Elements {
this.Style = value?.Style; this.Style = value?.Style;
} }
} }
/// <summary>
/// This Element's current <see cref="UiStyle"/>.
/// When this value is changed, <see cref="InitStyle"/> is called.
/// Note that this value is automatically set to the <see cref="UiSystem"/>'s ui style when it is changed.
/// </summary>
public UiStyle Style { public UiStyle Style {
get => this.style; get => this.style;
set { set {
@ -516,11 +521,15 @@ namespace MLEM.Ui.Elements {
} }
/// <summary> /// <summary>
/// Updates this element's <see cref="Area"/> list if <see cref="areaDirty"/> is true. /// Updates this element's <see cref="Area"/> and all of its <see cref="Children"/> by calling <see cref="ForceUpdateArea"/> if <see cref="areaDirty"/> is true.
/// </summary> /// </summary>
public void UpdateAreaIfDirty() { /// <returns>Whether <see cref="areaDirty"/> was true and <see cref="ForceUpdateArea"/> was called</returns>
if (this.areaDirty) public bool UpdateAreaIfDirty() {
if (this.areaDirty) {
this.ForceUpdateArea(); this.ForceUpdateArea();
return true;
}
return false;
} }
/// <summary> /// <summary>
@ -531,6 +540,10 @@ namespace MLEM.Ui.Elements {
this.areaDirty = false; this.areaDirty = false;
if (this.IsHidden) if (this.IsHidden)
return; return;
// if the parent's area is dirty, it would get updated anyway when querying its ChildPaddedArea,
// which would cause our ForceUpdateArea code to be run twice, so we only update our parent instead
if (this.Parent != null && this.Parent.UpdateAreaIfDirty())
return;
var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.system.Viewport; var parentArea = this.Parent != null ? this.Parent.ChildPaddedArea : (RectangleF) this.system.Viewport;
var parentCenterX = parentArea.X + parentArea.Width / 2; var parentCenterX = parentArea.X + parentArea.Width / 2;

View file

@ -8,18 +8,21 @@ namespace Tests {
public class TexturePackerTests { public class TexturePackerTests {
private Texture2D testTexture; private Texture2D testTexture;
private Texture2D disposedTestTexture;
private TestGame game; private TestGame game;
[SetUp] [SetUp]
public void SetUp() { public void SetUp() {
this.game = TestGame.Create(); this.game = TestGame.Create();
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256); this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
this.disposedTestTexture = new Texture2D(this.game.GraphicsDevice, 16, 16);
} }
[TearDown] [TearDown]
public void TearDown() { public void TearDown() {
this.game?.Dispose(); this.game?.Dispose();
this.testTexture?.Dispose(); this.testTexture?.Dispose();
this.disposedTestTexture?.Dispose();
} }
[Test] [Test]
@ -37,6 +40,16 @@ namespace Tests {
Assert.AreEqual(packer.PackedTexture.Height, 64); Assert.AreEqual(packer.PackedTexture.Height, 64);
} }
[Test]
public void TestDisposal() {
using var packer = new RuntimeTexturePacker(128, disposeTextures: true);
packer.Add(new TextureRegion(this.disposedTestTexture), StubResult);
packer.Add(new TextureRegion(this.disposedTestTexture, 0, 0, 8, 8), StubResult);
packer.Pack(this.game.GraphicsDevice);
Assert.True(this.disposedTestTexture.IsDisposed);
Assert.False(packer.PackedTexture.IsDisposed);
}
[Test] [Test]
public void TestBounds() { public void TestBounds() {
// test forced max width // test forced max width