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
- 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
- Skip unnecessary area updates for elements with dirty parents
Fixes
- 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
### MLEM.Data
Additions
- Allow RuntimeTexturePacker to automatically dispose submitted textures when packing
Improvements
- Use TitleContainer for opening streams where possible

View file

@ -15,11 +15,6 @@ namespace MLEM.Data {
/// </summary>
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>
/// The generated packed texture.
/// 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
/// </summary>
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;
/// <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="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>
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.autoIncreaseMaxWidth = autoIncreaseMaxWidth;
this.forcePowerOfTwo = forcePowerOfTwo;
this.forceSquare = forceSquare;
this.disposeTextures = disposeTextures;
}
/// <summary>
@ -124,8 +128,12 @@ namespace MLEM.Data {
this.LastPackTime = stopwatch.Elapsed;
// invoke callbacks
foreach (var request in this.textures)
foreach (var request in this.textures) {
request.Result.Invoke(new TextureRegion(this.PackedTexture, request.PackedArea));
if (this.disposeTextures)
request.Texture.Texture.Dispose();
}
this.textures.Clear();
}

View file

@ -35,6 +35,11 @@ namespace MLEM.Ui.Elements {
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 {
get => this.style;
set {
@ -516,11 +521,15 @@ namespace MLEM.Ui.Elements {
}
/// <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>
public void UpdateAreaIfDirty() {
if (this.areaDirty)
/// <returns>Whether <see cref="areaDirty"/> was true and <see cref="ForceUpdateArea"/> was called</returns>
public bool UpdateAreaIfDirty() {
if (this.areaDirty) {
this.ForceUpdateArea();
return true;
}
return false;
}
/// <summary>
@ -531,6 +540,10 @@ namespace MLEM.Ui.Elements {
this.areaDirty = false;
if (this.IsHidden)
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 parentCenterX = parentArea.X + parentArea.Width / 2;

View file

@ -8,18 +8,21 @@ namespace Tests {
public class TexturePackerTests {
private Texture2D testTexture;
private Texture2D disposedTestTexture;
private TestGame game;
[SetUp]
public void SetUp() {
this.game = TestGame.Create();
this.testTexture = new Texture2D(this.game.GraphicsDevice, 256, 256);
this.disposedTestTexture = new Texture2D(this.game.GraphicsDevice, 16, 16);
}
[TearDown]
public void TearDown() {
this.game?.Dispose();
this.testTexture?.Dispose();
this.disposedTestTexture?.Dispose();
}
[Test]
@ -37,6 +40,16 @@ namespace Tests {
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]
public void TestBounds() {
// test forced max width